-
Notifications
You must be signed in to change notification settings - Fork 175
/
Copy pathstream_context.cuh
332 lines (271 loc) · 12.2 KB
/
stream_context.cuh
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
/*
* 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.
*/
#pragma once
#include "../stdexec/execution.hpp"
#include <type_traits>
#include <memory_resource>
#include "detail/config.cuh"
#include "detail/memory.cuh"
#include "stream/sync_wait.cuh"
#include "stream/bulk.cuh"
#include "stream/let_xxx.cuh"
#include "stream/schedule_from.cuh"
#include "stream/start_detached.cuh"
#include "stream/submit.cuh"
#include "stream/split.cuh"
#include "stream/then.cuh"
#include "stream/continues_on.cuh"
#include "stream/launch.cuh"
#include "stream/upon_error.cuh"
#include "stream/upon_stopped.cuh"
#include "stream/when_all.cuh"
#include "stream/reduce.cuh"
#include "stream/ensure_started.cuh"
#include "stream/common.cuh"
#include "detail/queue.cuh"
#include "detail/throw_on_cuda_error.cuh"
namespace nvexec {
namespace STDEXEC_STREAM_DETAIL_NS {
template <sender Sender, std::integral Shape, class Fun>
using bulk_sender_th = __t<bulk_sender_t<__id<__decay_t<Sender>>, Shape, Fun>>;
template <sender Sender>
using split_sender_th = __t<split_sender_t<__id<__decay_t<Sender>>>>;
template <sender Sender, class Fun>
using then_sender_th = __t<then_sender_t<__id<__decay_t<Sender>>, Fun>>;
template <class Scheduler, sender... Senders>
using when_all_sender_th =
__t<when_all_sender_t<false, Scheduler, __id<__decay_t<Senders>>...>>;
template <class Scheduler, sender... Senders>
using transfer_when_all_sender_th =
__t<when_all_sender_t<true, Scheduler, __id<__decay_t<Senders>>...>>;
template <sender Sender, class Fun>
using upon_error_sender_th = __t<upon_error_sender_t<__id<__decay_t<Sender>>, Fun>>;
template <sender Sender, class Fun>
using upon_stopped_sender_th = __t<upon_stopped_sender_t<__id<__decay_t<Sender>>, Fun>>;
template <class Set, sender Sender, class Fun>
using let_xxx_th = __t<let_sender_t<__id<__decay_t<Sender>>, Fun, Set>>;
template <sender Sender>
using continues_on_sender_th = __t<continues_on_sender_t<__id<__decay_t<Sender>>>>;
template <sender Sender>
using ensure_started_th = __t<ensure_started_sender_t<__id<Sender>>>;
struct stream_scheduler {
using __t = stream_scheduler;
using __id = stream_scheduler;
friend stream_context;
template <sender Sender>
using schedule_from_sender_th =
stdexec::__t<schedule_from_sender_t<stream_scheduler, stdexec::__id<__decay_t<Sender>>>>;
template <class ReceiverId>
struct operation_state_ {
using Receiver = stdexec::__t<ReceiverId>;
struct __t : operation_state_base_t<ReceiverId> {
using __id = operation_state_;
cudaStream_t stream_{0};
cudaError_t status_{cudaSuccess};
__t(Receiver&& rcvr, context_state_t context_state)
: operation_state_base_t<ReceiverId>(static_cast<Receiver&&>(rcvr), context_state) {
}
void start() & noexcept {
this->propagate_completion_signal(set_value);
}
};
};
template <class ReceiverId>
using operation_state_t = stdexec::__t<operation_state_<ReceiverId>>;
struct env {
context_state_t context_state_;
template <class CPO>
stream_scheduler query(get_completion_scheduler_t<CPO>) const noexcept {
return stream_scheduler{context_state_};
}
};
struct sender_ {
struct __t : stream_sender_base {
using __id = sender_;
using completion_signatures =
completion_signatures<set_value_t(), set_error_t(cudaError_t)>;
template <class R>
auto connect(R rec) const & noexcept(__nothrow_move_constructible<R>) //
-> operation_state_t<stdexec::__id<R>> {
return operation_state_t<stdexec::__id<R>>(static_cast<R&&>(rec), env_.context_state_);
}
auto get_env() const noexcept -> const env& {
return env_;
};
STDEXEC_ATTRIBUTE((host, device)) inline __t(context_state_t context_state) noexcept
: env_{context_state} {
}
env env_;
};
};
using sender_t = stdexec::__t<sender_>;
template <sender S>
STDEXEC_MEMFN_DECL(auto schedule_from)(this const stream_scheduler& sch, S&& sndr) noexcept
-> schedule_from_sender_th<S> {
return schedule_from_sender_th<S>(sch.context_state_, static_cast<S&&>(sndr));
}
template <sender S, std::integral Shape, class Fn>
STDEXEC_MEMFN_DECL(
auto bulk)(this const stream_scheduler& sch, S&& sndr, Shape shape, Fn fun) noexcept
-> bulk_sender_th<S, Shape, Fn> {
return bulk_sender_th<S, Shape, Fn>{
{}, static_cast<S&&>(sndr), shape, static_cast<Fn&&>(fun)};
}
template <sender S, class Fn>
STDEXEC_MEMFN_DECL(
auto
then)(this const stream_scheduler& sch, S&& sndr, Fn fun) noexcept -> then_sender_th<S, Fn> {
return then_sender_th<S, Fn>{{}, static_cast<S&&>(sndr), static_cast<Fn&&>(fun)};
}
template <sender S>
STDEXEC_MEMFN_DECL(auto ensure_started)(this const stream_scheduler& sch, S&& sndr) noexcept
-> ensure_started_th<S> {
return ensure_started_th<S>(sch.context_state_, static_cast<S&&>(sndr));
}
template <sender S, class Fn>
STDEXEC_MEMFN_DECL(auto let_value)(this const stream_scheduler& sch, S&& sndr, Fn fun) noexcept
-> let_xxx_th<set_value_t, S, Fn> {
return let_xxx_th<set_value_t, S, Fn>{{}, static_cast<S&&>(sndr), static_cast<Fn&&>(fun)};
}
template <sender S, class Fn>
STDEXEC_MEMFN_DECL(auto let_error)(this const stream_scheduler& sch, S&& sndr, Fn fun) noexcept
-> let_xxx_th<set_error_t, S, Fn> {
return let_xxx_th<set_error_t, S, Fn>{{}, static_cast<S&&>(sndr), static_cast<Fn&&>(fun)};
}
template <sender S, class Fn>
STDEXEC_MEMFN_DECL(
auto let_stopped)(this const stream_scheduler& sch, S&& sndr, Fn fun) noexcept
-> let_xxx_th<set_stopped_t, S, Fn> {
return let_xxx_th<set_stopped_t, S, Fn>{{}, static_cast<S&&>(sndr), static_cast<Fn&&>(fun)};
}
template <sender S, class Fn>
STDEXEC_MEMFN_DECL(
auto upon_error)(this const stream_scheduler& sch, S&& sndr, Fn fun) noexcept
-> upon_error_sender_th<S, Fn> {
return upon_error_sender_th<S, Fn>{{}, static_cast<S&&>(sndr), static_cast<Fn&&>(fun)};
}
template <sender S, class Fn>
STDEXEC_MEMFN_DECL(
auto upon_stopped)(this const stream_scheduler& sch, S&& sndr, Fn fun) noexcept
-> upon_stopped_sender_th<S, Fn> {
return upon_stopped_sender_th<S, Fn>{{}, static_cast<S&&>(sndr), static_cast<Fn&&>(fun)};
}
template <stream_completing_sender... Senders>
STDEXEC_MEMFN_DECL(auto transfer_when_all)(
this const stream_scheduler& sch,
Senders&&... sndrs) //
noexcept -> transfer_when_all_sender_th<stream_scheduler, Senders...> {
return transfer_when_all_sender_th<stream_scheduler, Senders...>(
sch.context_state_, static_cast<Senders&&>(sndrs)...);
}
template <stream_completing_sender... Senders>
STDEXEC_MEMFN_DECL(auto transfer_when_all_with_variant)(
this const stream_scheduler& sch, //
Senders&&... sndrs) noexcept //
-> transfer_when_all_sender_th<stream_scheduler, __result_of<into_variant, Senders>...> {
return transfer_when_all_sender_th<stream_scheduler, __result_of<into_variant, Senders>...>(
sch.context_state_, into_variant(static_cast<Senders&&>(sndrs))...);
}
template <sender S, scheduler Sch>
STDEXEC_MEMFN_DECL(
auto continues_on)(this const stream_scheduler& sch, S&& sndr, Sch&& scheduler) //
noexcept -> __result_of<schedule_from, Sch, continues_on_sender_th<S>> {
return schedule_from(
static_cast<Sch&&>(scheduler),
continues_on_sender_th<S>(sch.context_state_, static_cast<S&&>(sndr)));
}
template <sender S>
STDEXEC_MEMFN_DECL(auto split)(this const stream_scheduler& sch, S&& sndr) noexcept //
-> split_sender_th<S> {
return split_sender_th<S>(sch.context_state_, static_cast<S&&>(sndr));
}
STDEXEC_ATTRIBUTE((host, device)) inline auto schedule() const noexcept -> sender_t {
return {context_state_};
}
STDEXEC_MEMFN_DECL(auto __has_algorithm_customizations)(this const stream_scheduler& self) //
noexcept -> std::true_type {
return {};
}
template <sender S>
STDEXEC_MEMFN_DECL(auto sync_wait)(this const stream_scheduler& self, S&& sndr) //
-> __call_result_t<_sync_wait::sync_wait_t, context_state_t, S> {
return _sync_wait::sync_wait_t{}(self.context_state_, static_cast<S&&>(sndr));
}
auto query(get_forward_progress_guarantee_t) const noexcept -> forward_progress_guarantee {
return forward_progress_guarantee::weakly_parallel;
}
bool operator==(const stream_scheduler& other) const noexcept {
return context_state_.hub_ == other.context_state_.hub_;
}
explicit stream_scheduler(context_state_t context_state) noexcept
: context_state_(context_state) {
}
// private: TODO
context_state_t context_state_;
};
template <stream_completing_sender Sender>
void tag_invoke(start_detached_t, Sender&& sndr) noexcept(false) {
_submit::submit_t{}(static_cast<Sender&&>(sndr), _start_detached::detached_receiver_t{});
}
template <stream_completing_sender... Senders>
when_all_sender_th<stream_scheduler, Senders...>
tag_invoke(when_all_t, Senders&&... sndrs) noexcept {
return when_all_sender_th<stream_scheduler, Senders...>{
context_state_t{nullptr, nullptr, nullptr, nullptr},
static_cast<Senders&&>(sndrs)...
};
}
template <stream_completing_sender... Senders>
when_all_sender_th<stream_scheduler, __result_of<into_variant, Senders>...>
tag_invoke(when_all_with_variant_t, Senders&&... sndrs) noexcept {
return when_all_sender_th<stream_scheduler, __result_of<into_variant, Senders>...>{
context_state_t{nullptr, nullptr, nullptr, nullptr},
into_variant(static_cast<Senders&&>(sndrs))...
};
}
template <sender S, class Fn>
upon_error_sender_th<S, Fn> tag_invoke(upon_error_t, S&& sndr, Fn fun) noexcept {
return upon_error_sender_th<S, Fn>{{}, static_cast<S&&>(sndr), static_cast<Fn&&>(fun)};
}
template <sender S, class Fn>
upon_stopped_sender_th<S, Fn> tag_invoke(upon_stopped_t, S&& sndr, Fn fun) noexcept {
return upon_stopped_sender_th<S, Fn>{{}, static_cast<S&&>(sndr), static_cast<Fn&&>(fun)};
}
} // namespace STDEXEC_STREAM_DETAIL_NS
using STDEXEC_STREAM_DETAIL_NS::stream_scheduler;
struct stream_context {
STDEXEC_STREAM_DETAIL_NS::resource_storage<STDEXEC_STREAM_DETAIL_NS::pinned_resource>
pinned_resource_{};
STDEXEC_STREAM_DETAIL_NS::resource_storage<STDEXEC_STREAM_DETAIL_NS::managed_resource>
managed_resource_{};
STDEXEC_STREAM_DETAIL_NS::stream_pools_t stream_pools_{};
static int get_device() {
int dev_id{};
cudaGetDevice(&dev_id);
return dev_id;
}
int dev_id_{};
STDEXEC_STREAM_DETAIL_NS::queue::task_hub_t hub_;
stream_context()
: dev_id_(get_device())
, hub_(dev_id_, pinned_resource_.get()) {
}
stream_scheduler get_scheduler(stream_priority priority = stream_priority::normal) {
return stream_scheduler{STDEXEC_STREAM_DETAIL_NS::context_state_t(
pinned_resource_.get(), managed_resource_.get(), &stream_pools_, &hub_, priority)};
}
};
} // namespace nvexec