forked from NVIDIA/stdexec
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsplit.cuh
More file actions
419 lines (361 loc) · 13 KB
/
split.cuh
File metadata and controls
419 lines (361 loc) · 13 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
/*
* 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/env.hpp"
#include "../../exec/split.hpp"
#include "../detail/cuda_atomic.cuh" // IWYU pragma: keep
#include "../detail/throw_on_cuda_error.cuh"
#include "common.cuh"
#include <atomic>
#include <memory>
#include <optional>
#include <type_traits>
#include <cuda/std/tuple>
STDEXEC_PRAGMA_PUSH()
STDEXEC_PRAGMA_IGNORE_EDG(cuda_compile)
namespace nv::execution::_strm
{
namespace _split
{
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(_split::_make_env(__declval<inplace_stop_source const &>(),
static_cast<stream_provider*>(nullptr)));
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)...);
}
template <class Sender, class SharedState>
struct receiver : stream_receiver_base
{
using receiver_concept = STDEXEC::receiver_t;
explicit receiver(SharedState& sh_state_t) noexcept
: sh_state_(sh_state_t)
{}
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 = sh_state_.opstate2_.get_stream();
sh_state_.index_ = __mapply<__mfind_i<tuple_t>, variant_t>::value;
copy_kernel<Tag, Args&&...>
<<<1, 1, 0, stream>>>(sh_state_.data_, static_cast<Args&&>(args)...);
sh_state_.stream_provider_.status_ = STDEXEC_LOG_CUDA_API(
cudaEventRecord(sh_state_.event_, stream));
}
else
{
sh_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)...);
sh_state_.notify();
}
template <class Error>
void set_error(Error&& __err) noexcept
{
set_result(set_error_t(), static_cast<Error&&>(__err));
sh_state_.notify();
}
void set_stopped() noexcept
{
set_result(set_stopped_t());
sh_state_.notify();
}
[[nodiscard]]
auto get_env() const noexcept -> env_t
{
return sh_state_.make_env();
}
private:
SharedState& sh_state_;
};
struct opstate_base
{
using notify_fn_t = void(opstate_base*) noexcept;
opstate_base* next_{};
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
{
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>;
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_))
, 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));
}
}
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()}))
{}
~sh_state()
{
if (!started_.test(::cuda::memory_order_relaxed))
{
if (task_)
{
task_->free_(task_);
}
}
if (data_)
{
STDEXEC_ASSERT_CUDA_API(cudaFree(data_));
if constexpr (stream_sender<Sender, env_t>)
{
STDEXEC_ASSERT_CUDA_API(cudaEventDestroy(event_));
}
}
}
auto make_env() const noexcept -> env_t
{
return _split::_make_env(stop_source_, &const_cast<stream_provider&>(stream_provider_));
}
void notify() noexcept
{
void* const completion_state = static_cast<void*>(this);
void* old = head_.exchange(completion_state, std::memory_order_acq_rel);
auto* opstate = static_cast<opstate_base*>(old);
while (opstate != nullptr)
{
opstate_base* next = opstate->next_;
opstate->notify_(opstate);
opstate = next;
}
}
context ctx_;
stream_provider stream_provider_;
inplace_stop_source stop_source_{};
std::atomic<void*> head_{nullptr};
unsigned int index_{0};
variant_t* data_{nullptr};
task_t* task_{nullptr};
cudaEvent_t event_;
host_ptr_t<__decay_t<env_t>> env_{};
inner_opstate_t opstate2_;
::cuda::std::atomic_flag started_{};
};
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_{};
std::shared_ptr<sh_state<Sender>> sh_state_;
public:
opstate(Receiver&& rcvr, std::shared_ptr<sh_state<Sender>> sh_state)
noexcept(std::is_nothrow_move_constructible_v<Receiver>)
: opstate_base{nullptr, notify}
, _strm::opstate_base<Receiver>(static_cast<Receiver&&>(rcvr), sh_state->ctx_)
, sh_state_(std::move(sh_state))
{}
STDEXEC_IMMOVABLE(opstate);
static void notify(opstate_base* self) noexcept
{
auto* op = static_cast<opstate*>(self);
op->on_stop_.reset();
cudaError_t& status = op->sh_state_->stream_provider_.status_;
if (status == cudaSuccess)
{
if constexpr (stream_sender<Sender, env_t>)
{
status = STDEXEC_LOG_CUDA_API(
cudaStreamWaitEvent(op->get_stream(), op->sh_state_->event_, 0));
}
nvexec::visit(
[&](auto& tupl) noexcept -> void
{
::cuda::std::apply([&]<class Tag, class... Args>(Tag, Args const &... args) noexcept
-> void { op->propagate_completion_signal(Tag(), args...); },
tupl);
},
*op->sh_state_->data_,
op->sh_state_->index_);
}
else
{
op->propagate_completion_signal(STDEXEC::set_error, std::as_const(status));
}
}
void start() & noexcept
{
sh_state<Sender>* sh_state = sh_state_.get();
std::atomic<void*>& head = sh_state->head_;
void* const completion_state = static_cast<void*>(sh_state);
void* old = head.load(std::memory_order_acquire);
if (old != completion_state)
{
on_stop_.emplace(get_stop_token(STDEXEC::get_env(this->rcvr_)),
__forward_stop_request{sh_state->stop_source_});
}
do
{
if (old == completion_state)
{
notify(this);
return;
}
next_ = static_cast<opstate_base*>(old);
}
while (!head.compare_exchange_weak(old,
static_cast<void*>(this),
std::memory_order_release,
std::memory_order_acquire));
if (old == nullptr)
{
// the inner sender isn't running
if (sh_state->stop_source_.stop_requested())
{
// 1. resets head to completion state
// 2. notifies waiting threads
// 3. propagates "stopped" signal to `out_r'`
sh_state->notify();
}
else
{
sh_state->started_.test_and_set(::cuda::memory_order_relaxed);
STDEXEC::start(sh_state->opstate2_);
}
}
}
};
} // namespace _split
template <class Sender>
struct split_sender : stream_sender_base
{
using sender_concept = STDEXEC::sender_t;
using sh_state_t = _split::sh_state<Sender>;
template <class Receiver>
using opstate_t = _split::opstate<Sender, Receiver>;
template <class... Tys>
using _set_value_t = completion_signatures<set_value_t(__decay_t<Tys> const &...)>;
template <class Ty>
using _set_error_t = completion_signatures<set_error_t(__decay_t<Ty> const &)>;
template <class>
static consteval auto get_completion_signatures()
{
return STDEXEC::__transform_completion_signatures_of_t<
Sender,
STDEXEC::prop<get_stop_token_t, inplace_stop_token>,
STDEXEC::completion_signatures<set_error_t(cudaError_t const &)>,
_set_value_t,
_set_error_t>();
}
template <receiver Receiver>
auto connect(Receiver rcvr) const & noexcept -> opstate_t<Receiver>
{
return opstate_t<Receiver>{static_cast<Receiver&&>(rcvr), sh_state_};
}
auto get_env() const noexcept -> stream_sender_attrs<Sender>
{
return {&sndr_};
}
explicit split_sender(context ctx, Sender sndr)
: sndr_(static_cast<Sender&&>(sndr))
, sh_state_{std::make_shared<sh_state_t>(sndr_, ctx)}
{}
private:
Sender sndr_;
std::shared_ptr<sh_state_t> sh_state_;
};
template <>
struct transform_sender_for<exec::split_t>
{
template <class Sender>
using _sender_t = split_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::split_t, _sender_t<Sender>, Env>();
}
}
};
} // namespace nv::execution::_strm
namespace nvexec = nv::execution;
namespace STDEXEC::__detail
{
template <class Sender>
extern __declfn_t<nvexec::_strm::split_sender<__demangle_t<Sender>>>
__demangle_v<nvexec::_strm::split_sender<Sender>>;
} // namespace STDEXEC::__detail
STDEXEC_PRAGMA_POP()