forked from NVIDIA/stdexec
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathensure_started.cuh
More file actions
437 lines (380 loc) · 14.1 KB
/
ensure_started.cuh
File metadata and controls
437 lines (380 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
* Copyright (c) 2022 NVIDIA Corporation
*
* Licensed under the Apache License Version 2.0 with LLVM Exceptions
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://llvm.org/LICENSE.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// clang-format Language: Cpp
#pragma once
#include "../../stdexec/execution.hpp"
// include these after execution.hpp
#include "../../exec/ensure_started.hpp"
#include "../../exec/env.hpp"
#include "../detail/throw_on_cuda_error.cuh"
#include "common.cuh"
#include <atomic>
#include <optional>
#include <type_traits>
#include <utility>
#include <cuda/std/tuple>
STDEXEC_PRAGMA_PUSH()
STDEXEC_PRAGMA_IGNORE_EDG(cuda_compile)
namespace nv::execution::_strm
{
namespace _ensure_started
{
template <class Tag, class... Args, class Variant>
STDEXEC_ATTRIBUTE(launch_bounds(1))
__global__ void copy_kernel(Variant* var, Args... args)
{
static_assert(trivially_copyable<Args...>);
using tuple_t = decayed_tuple_t<Tag, Args...>;
var->template emplace<tuple_t>(Tag(), static_cast<Args&&>(args)...);
}
inline auto
_make_env(inplace_stop_source const & stop_source, stream_provider* stream_provider) noexcept
{
return make_stream_env(exec::env_from{[&](get_stop_token_t) noexcept
{ return stop_source.get_token(); }},
stream_provider);
}
using env_t = decltype(_ensure_started::_make_env(__declval<inplace_stop_source const &>(),
static_cast<stream_provider*>(nullptr)));
template <class Sender, class SharedState>
class receiver : public stream_receiver_base
{
__intrusive_ptr<SharedState> shared_state_;
public:
explicit receiver(SharedState& shared_state) noexcept
: shared_state_(shared_state.__intrusive_from_this())
{}
template <class Tag, class... Args>
void _set_result(Tag, Args&&... args) noexcept
{
using tuple_t = decayed_tuple_t<Tag, Args...>;
using variant_t = typename SharedState::variant_t;
if constexpr (stream_sender<Sender, env_t>)
{
cudaStream_t stream = shared_state_->stream_provider_.own_stream_.value();
shared_state_->index_ = __mapply<__mfind_i<tuple_t>, variant_t>::value;
copy_kernel<Tag, Args&&...>
<<<1, 1, 0, stream>>>(shared_state_->data_, static_cast<Args&&>(args)...);
shared_state_->stream_provider_.status_ = STDEXEC_LOG_CUDA_API(
cudaEventRecord(shared_state_->event_, stream));
}
else
{
shared_state_->index_ = __mapply<__mfind_i<tuple_t>, variant_t>::value;
}
}
template <class... Args>
void set_value(Args&&... args) noexcept
{
_set_result(set_value_t(), static_cast<Args&&>(args)...);
shared_state_->notify();
shared_state_.reset();
}
template <class Error>
void set_error(Error&& __err) noexcept
{
_set_result(set_error_t(), static_cast<Error&&>(__err));
shared_state_->notify();
shared_state_.reset();
}
void set_stopped() noexcept
{
_set_result(set_stopped_t());
shared_state_->notify();
shared_state_.reset();
}
[[nodiscard]]
auto get_env() const noexcept -> env_t
{
return shared_state_->make_env();
}
};
struct opstate_base
{
using notify_fn_t = void(opstate_base*) noexcept;
notify_fn_t* notify_{};
};
template <class Type>
auto malloc_managed(cudaError_t& status) -> Type*
{
Type* ptr{};
if (status == cudaSuccess)
{
status = STDEXEC_LOG_CUDA_API(cudaMallocManaged(&ptr, sizeof(Type)));
if (status == cudaSuccess)
{
new (ptr) Type();
return ptr;
}
}
return nullptr;
}
template <class Sender>
struct sh_state : __enable_intrusive_from_this<sh_state<Sender>>
{
using variant_t = variant_storage_t<Sender, env_t>;
using inner_receiver_t = receiver<Sender, sh_state>;
using task_t = continuation_task<inner_receiver_t, variant_t>;
using enqueue_receiver_t = stream_enqueue_receiver<env_t, variant_t>;
using intermediate_receiver_t =
std::conditional_t<stream_sender<Sender, env_t>, inner_receiver_t, enqueue_receiver_t>;
using inner_opstate_t = connect_result_t<Sender, intermediate_receiver_t>;
auto make_env() const noexcept -> env_t
{
return _ensure_started::_make_env(stop_source_,
&const_cast<stream_provider&>(stream_provider_));
}
explicit sh_state(Sender& sndr, context ctx)
requires(stream_sender<Sender, env_t>)
: ctx_(ctx)
, stream_provider_(false, ctx)
, data_(malloc_managed<variant_t>(stream_provider_.status_))
, opstate1_{nullptr}
, opstate2_(connect(static_cast<Sender&&>(sndr), inner_receiver_t{*this}))
{
if (stream_provider_.status_ == cudaSuccess)
{
stream_provider_.status_ = STDEXEC_LOG_CUDA_API(
cudaEventCreateWithFlags(&event_, cudaEventDisableTiming));
}
STDEXEC::start(opstate2_);
}
explicit sh_state(Sender& sndr, context ctx)
: ctx_(ctx)
, stream_provider_(false, ctx)
, data_(malloc_managed<variant_t>(stream_provider_.status_))
, task_(host_allocate<task_t>(stream_provider_.status_,
ctx.pinned_resource_,
inner_receiver_t{*this},
data_,
stream_provider_.own_stream_.value(),
ctx.pinned_resource_)
.release())
, env_(host_allocate(this->stream_provider_.status_, ctx_.pinned_resource_, make_env()))
, opstate2_(connect(static_cast<Sender&&>(sndr),
enqueue_receiver_t{env_.get(), data_, task_, ctx.hub_->producer()}))
{
STDEXEC::start(opstate2_);
}
~sh_state()
{
if (stream_provider_.status_ == cudaSuccess)
{
if constexpr (stream_sender<Sender, env_t>)
{
STDEXEC_ASSERT_CUDA_API(cudaEventDestroy(event_));
}
}
if (data_)
{
STDEXEC_ASSERT_CUDA_API(cudaFree(data_));
}
}
void notify() noexcept
{
void* const completion_state = static_cast<void*>(this);
void* const old = opstate1_.exchange(completion_state, std::memory_order_acq_rel);
if (old != nullptr)
{
auto* op = static_cast<opstate_base*>(old);
op->notify_(op);
}
}
void detach() noexcept
{
stop_source_.request_stop();
}
context ctx_;
stream_provider stream_provider_;
cudaEvent_t event_{};
unsigned int index_{0};
variant_t* data_{nullptr};
task_t* task_{nullptr};
inplace_stop_source stop_source_{};
host_ptr_t<__decay_t<env_t>> env_{};
std::atomic<void*> opstate1_;
inner_opstate_t opstate2_;
};
template <class Sender, class Receiver>
struct opstate
: public opstate_base
, public _strm::opstate_base<Receiver>
{
using on_stop_t = std::optional<
stop_callback_for_t<stop_token_of_t<env_of_t<Receiver>>, __forward_stop_request>>;
on_stop_t on_stop_{};
__intrusive_ptr<sh_state<Sender>> shared_state_;
public:
opstate(Receiver rcvr, __intrusive_ptr<sh_state<Sender>> shared_state) noexcept
: opstate_base{notify}
, _strm::opstate_base<Receiver>(static_cast<Receiver&&>(rcvr), shared_state->ctx_)
, shared_state_(std::move(shared_state))
{}
~opstate()
{
// Check to see if this operation was ever started. If not,
// detach the (potentially still running) operation:
if (nullptr == shared_state_->opstate1_.load(std::memory_order_acquire))
{
shared_state_->detach();
}
}
STDEXEC_IMMOVABLE(opstate);
static void notify(opstate_base* self) noexcept
{
auto* op = static_cast<opstate*>(self);
op->on_stop_.reset();
cudaError_t& status = op->shared_state_->stream_provider_.status_;
if (status == cudaSuccess)
{
if constexpr (stream_sender<Sender, env_t>)
{
status = STDEXEC_LOG_CUDA_API(
cudaStreamWaitEvent(op->get_stream(), op->shared_state_->event_, 0));
}
nvexec::visit(
[&](auto& tupl) noexcept -> void
{
::cuda::std::apply([&]<class Tag, class... Args>(Tag, Args&... args) noexcept -> void
{ op->propagate_completion_signal(Tag(), std::move(args)...); },
tupl);
},
*op->shared_state_->data_,
op->shared_state_->index_);
}
else
{
op->propagate_completion_signal(STDEXEC::set_error, std::move(status));
}
}
void start() & noexcept
{
sh_state<Sender>* shared_state = shared_state_.get();
std::atomic<void*>& opstate1 = shared_state->opstate1_;
void* const completion_state = static_cast<void*>(shared_state);
void* const old = opstate1.load(std::memory_order_acquire);
if (old == completion_state)
{
notify(this);
}
else
{
// register stop callback:
on_stop_.emplace(get_stop_token(STDEXEC::get_env(this->rcvr_)),
__forward_stop_request{shared_state->stop_source_});
// Check if the stop_source has requested cancellation
if (shared_state->stop_source_.stop_requested())
{
// Stop has already been requested. Don't bother starting
// the child operations.
this->propagate_completion_signal(set_stopped_t{});
}
else
{
// Otherwise, the inner source hasn't notified completion.
// Set this operation as the opstate1 so it's notified.
void* old = nullptr;
if (!opstate1.compare_exchange_weak(old,
this,
std::memory_order_release,
std::memory_order_acquire))
{
// We get here when the task completed during the execution
// of this function. Complete the operation synchronously.
STDEXEC_ASSERT(old == completion_state);
notify(this);
}
}
}
}
};
} // namespace _ensure_started
template <class Sender>
struct ensure_started_sender : stream_sender_base
{
using sender_concept = STDEXEC::sender_t;
using sh_state_t = _ensure_started::sh_state<Sender>;
template <class Receiver>
using opstate_t = _ensure_started::opstate<Sender, Receiver>;
explicit ensure_started_sender(context ctx, Sender sndr)
: sndr_(static_cast<Sender&&>(sndr))
, shared_state_{__make_intrusive<sh_state_t>(sndr_, ctx)}
{}
// Move-only:
ensure_started_sender(ensure_started_sender&&) = default;
~ensure_started_sender()
{
if (nullptr != shared_state_)
{
// We're detaching a potentially running operation. Request cancellation.
shared_state_->detach(); // BUGBUG NOT TO SPEC
}
}
template <receiver Receiver, class Env = env<>>
requires receiver_of<Receiver, completion_signatures_of_t<ensure_started_sender, Env>>
auto connect(Receiver rcvr) && noexcept -> opstate_t<Receiver>
{
return opstate_t<Receiver>{static_cast<Receiver&&>(rcvr), std::move(shared_state_)};
}
auto get_env() const noexcept -> stream_sender_attrs<Sender>
{
return {&sndr_};
}
template <class... Tys>
using _set_value_t = completion_signatures<set_value_t(__decay_t<Tys>...)>;
template <class Ty>
using _set_error_t = completion_signatures<set_error_t(__decay_t<Ty>)>;
template <class Self, class... Env>
static consteval auto get_completion_signatures() -> __transform_completion_signatures_of_t<
Sender,
_ensure_started::env_t,
completion_signatures<set_error_t(cudaError_t), set_stopped_t()>,
_set_value_t,
_set_error_t>
{
return {};
}
Sender sndr_;
__intrusive_ptr<sh_state_t> shared_state_;
};
template <>
struct transform_sender_for<exec::ensure_started_t>
{
template <class Sender>
using _sender_t = ensure_started_sender<__decay_t<Sender>>;
template <class Env, class Sender>
auto operator()(Env const & env, __ignore, __ignore, Sender&& sndr) const
{
if constexpr (stream_completing_sender<Sender, Env>)
{
auto sched = get_completion_scheduler<set_value_t>(get_env(sndr), env);
return _sender_t<Sender>{sched.ctx_, static_cast<Sender&&>(sndr)};
}
else
{
return _strm::_no_stream_scheduler_in_env<exec::ensure_started_t, Sender, Env>();
}
}
};
} // namespace nv::execution::_strm
namespace nvexec = nv::execution;
namespace STDEXEC::__detail
{
template <class Sender>
extern __declfn_t<nvexec::_strm::ensure_started_sender<__demangle_t<Sender>>>
__demangle_v<nvexec::_strm::ensure_started_sender<Sender>>;
} // namespace STDEXEC::__detail
STDEXEC_PRAGMA_POP()