forked from NVIDIA/cccl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.cuh
More file actions
1828 lines (1604 loc) · 46.9 KB
/
context.cuh
File metadata and controls
1828 lines (1604 loc) · 46.9 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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===----------------------------------------------------------------------===//
//
// Part of CUDASTF in CUDA C++ Core Libraries,
// under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// SPDX-FileCopyrightText: Copyright (c) 2022-2025 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
/** @file
*
* @brief Main include file for the CUDASTF library.
*/
#pragma once
#include <cuda/std/__exception/exception_macros.h>
#include <cuda/experimental/__places/exec/cuda_stream.cuh>
#include <cuda/experimental/__stf/allocators/adapters.cuh>
#include <cuda/experimental/__stf/allocators/buddy_allocator.cuh>
#include <cuda/experimental/__stf/allocators/cached_allocator.cuh>
#include <cuda/experimental/__stf/allocators/pooled_allocator.cuh>
#include <cuda/experimental/__stf/allocators/uncached_allocator.cuh>
#include <cuda/experimental/__stf/graph/graph_ctx.cuh>
#include <cuda/experimental/__stf/internal/inner_shape.cuh>
#include <cuda/experimental/__stf/internal/reducer.cuh>
#include <cuda/experimental/__stf/internal/scalar_interface.cuh>
#include <cuda/experimental/__stf/internal/task_dep.cuh>
#include <cuda/experimental/__stf/internal/void_interface.cuh>
#include <cuda/experimental/__stf/stream/stream_ctx.cuh>
#include <map>
#include <stdexcept>
#include <variant>
namespace cuda::experimental::stf
{
#ifndef _CCCL_DOXYGEN_INVOKED // Do not document
/**
* @brief Invokes the provided callable on the value of the given variant.
*
* This function applies the callable `f` to the current value stored in
* the `std::variant` object `v`. The callable is forwarded as an rvalue
* reference. This is applicable to both non-const and const variants.
*
* @tparam Ts... Types of the variant's possible stored types.
* @tparam F Type of the callable (function, lambda, or functor).
*
* @param v A `std::variant` object containing one of the types `Ts...`.
* @param f The callable to invoke on the value stored in the variant.
*
* @return The result of the callable invocation on the variant's value.
*
* @overload
*/
template <typename... Ts, typename F>
decltype(auto) operator->*(::std::variant<Ts...>& v, F&& f)
{
return ::std::visit(::std::forward<F>(f), v);
}
/**
* @overload
*/
template <typename... Ts, typename F>
decltype(auto) operator->*(const ::std::variant<Ts...>& v, F&& f)
{
return ::std::visit(::std::forward<F>(f), v);
}
#endif // !_CCCL_DOXYGEN_INVOKED
/**
* @brief Generic context implementation
*
*/
class context
{
public:
template <typename T>
using logical_data_t = ::cuda::experimental::stf::logical_data<T>;
private:
template <typename T1, typename T2>
class unified_scope
{
public:
unified_scope(T1 arg)
: payload(mv(arg))
{}
unified_scope(T2 arg)
: payload(mv(arg))
{}
/// Get the string attached to the task for debugging purposes
const ::std::string& get_symbol() const
{
return payload->*[&](auto& self) {
return self.get_symbol();
};
}
auto&& set_exec_place(exec_place e_place) &
{
payload->*[&](auto& self) {
self.set_exec_place(mv(e_place));
};
return *this;
}
auto&& set_exec_place(exec_place e_place) &&
{
payload->*[&](auto& self) {
self.set_exec_place(mv(e_place));
};
return mv(*this);
}
auto& set_symbol(::std::string s) &
{
payload->*[&](auto& self) {
self.set_symbol(mv(s));
};
return *this;
}
auto&& set_symbol(::std::string s) &&
{
payload->*[&](auto& self) {
self.set_symbol(mv(s));
};
return mv(*this);
}
template <typename Fun>
void operator->*(Fun&& f)
{
payload->*[&](auto& self) {
self->*::std::forward<Fun>(f);
};
}
template <typename... Args>
auto& add_deps(Args&&... args)
{
payload->*[&](auto& self) {
self.add_deps(::std::forward<Args>(args)...);
};
return *this;
}
template <typename... Args>
auto& add_kernel_desc(Args&&... args)
{
payload->*[&](auto& self) {
self.add_kernel_desc(::std::forward<Args>(args)...);
};
return *this;
}
template <typename T>
decltype(auto) get(size_t submitted_index) const
{
return payload->*[&](auto& self) {
return self.template get<T>(submitted_index);
};
}
auto& start()
{
payload->*[&](auto& self) {
self.start();
};
return *this;
}
auto& end()
{
payload->*[&](auto& self) {
self.end();
};
return *this;
}
private:
::std::variant<T1, T2> payload;
};
public:
/*
* A task that can be either a stream task or a graph task.
*/
template <typename... Deps>
class unified_task
{
public:
unified_task(stream_task<Deps...> task)
: payload(mv(task))
{}
unified_task(graph_task<Deps...> task)
: payload(mv(task))
{}
auto& set_symbol(::std::string s) &
{
payload->*[&](auto& self) {
self.set_symbol(mv(s));
};
return *this;
}
auto&& set_symbol(::std::string s) &&
{
payload->*[&](auto& self) {
self.set_symbol(mv(s));
};
return mv(*this);
}
auto&& set_exec_place(exec_place e_place) &
{
payload->*[&](auto& self) {
self.set_exec_place(mv(e_place));
};
return *this;
}
auto&& set_exec_place(exec_place e_place) &&
{
payload->*[&](auto& self) {
self.set_exec_place(mv(e_place));
};
return mv(*this);
}
auto& start()
{
payload->*[&](auto& self) {
self.start();
};
return *this;
}
auto& end()
{
payload->*[&](auto& self) {
self.end();
};
return *this;
}
void enable_capture()
{
payload->*[&](auto& self) {
self.enable_capture();
};
}
/**
* @brief Add dependencies to this task.
*
* @tparam Args
* @param args
* @return stream_or_graph_dynamic_task&
*/
template <typename... Args>
unified_task& add_deps(Args&&... args)
{
payload->*[&](auto& self) {
self.add_deps(::std::forward<Args>(args)...);
};
return *this;
}
/**
* @brief retrieve the data instance associated to an
* index in a task.
*
* @tparam T
* @param submitted index
* @return slice<T>
*/
template <typename T>
decltype(auto) get(size_t submitted_index) const
{
return payload->*[&](auto& self) -> decltype(auto) {
return self.template get<T>(submitted_index);
};
}
template <typename Fun>
void operator->*(Fun&& f)
{
payload->*[&](auto& self) {
self->*f;
};
}
cudaStream_t get_stream() const
{
return payload->*[&](auto& self) {
return self.get_stream();
};
}
// Get the underlying task base class - both stream_task and graph_task inherit from task. This is convenient when
// we do not need the "typed" task, for example when using the "low-level" add_deps method.
::cuda::experimental::stf::task& get_base_task()
{
return payload->*[](auto& self) -> ::cuda::experimental::stf::task& {
return self.get_base_task();
};
}
const ::cuda::experimental::stf::task& get_base_task() const
{
return payload->*[](auto& self) -> const ::cuda::experimental::stf::task& {
return self.get_base_task();
};
}
private:
::std::variant<stream_task<Deps...>, graph_task<Deps...>> payload;
};
/**
* @brief Default constructor for the context class.
*/
context() = default;
/**
* @brief Constructs a stream context with a CUDA stream and an optional asynchronous resource handle.
*
* @param stream The CUDA stream to be used in the context.
* @param handle Optional asynchronous resource handle.
*/
context(cudaStream_t stream, async_resources_handle handle = async_resources_handle(nullptr))
: payload(stream_ctx(stream, handle))
{
// The default choice is stream_ctx, otherwise we should assign a graph_ctx with the appropriate parameters
}
/**
* @brief Constructs a stream context with an asynchronous resource handle.
*
* @param handle The asynchronous resource handle.
*/
context(async_resources_handle handle)
: payload(stream_ctx(handle))
{
// The default choice is stream_ctx, otherwise we should assign a graph_ctx with the appropriate parameters
}
/**
* @brief Constructs a context from a stream context.
*
* @param ctx The context to be assigned.
*/
context(stream_ctx ctx)
: payload(mv(ctx))
{}
/**
* @brief Constructs a context from a graph context.
*
* @param ctx The context to be assigned.
*/
context(graph_ctx ctx)
: payload(mv(ctx))
{}
/**
* @brief Assigns a specific context type to the context.
*
* @tparam Ctx The type of the context to be assigned.
* @param ctx The context to be assigned.
* @return Reference to the updated context.
*/
template <typename Ctx>
context& operator=(Ctx ctx)
{
payload = mv(ctx);
return *this;
}
/**
* @brief Converts the context to a string representation.
*
* @return A string representation of the context.
*/
::std::string to_string() const
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
return payload->*[&](auto& self) {
return self.to_string();
};
}
/**
* @brief Returns an event list which depends on the completion of work in the stream
*/
auto stream_to_event_list(cudaStream_t stream, ::std::string str) const
{
return payload->*[&](auto& self) {
return self.stream_to_event_list(stream, mv(str));
};
}
void set_graph_cache_policy(::std::function<bool()> policy)
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
payload->*[&](auto& self) {
self.set_graph_cache_policy(mv(policy));
};
}
auto get_graph_cache_policy() const
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
return payload->*[&](auto& self) {
return self.get_graph_cache_policy();
};
}
executable_graph_cache_stat* graph_get_cache_stat()
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
return payload->*[&](auto& self) {
return self.graph_get_cache_stat();
};
}
cudaGraph_t graph() const
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
return payload->*[&](auto& self) {
return self.graph();
};
}
size_t stage() const
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
return payload->*[&](auto& self) {
return self.stage();
};
}
/**
* @brief Returns the number of tasks created since the context was created or since the last fence (if any)
*/
size_t task_count() const
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
return payload->*[&](auto& self) {
return self.task_count();
};
}
/**
* @brief Creates logical data with specified sizes.
*
* @tparam T The type of the logical data.
* @tparam Sizes The sizes of the logical data dimensions.
* @param elements The number of elements.
* @param othersizes The sizes of other dimensions.
*/
template <typename T, typename... Sizes>
auto logical_data(size_t elements, Sizes... othersizes)
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
return payload->*[&](auto& self) {
return self.template logical_data<T>(elements, othersizes...);
};
}
/**
* @brief Creates logical data with specified parameters.
*
* @tparam P0 The type of the first parameter.
* @tparam Ps The types of the other parameters.
* @param p0 The first parameter.
* @param ps The other parameters.
*/
template <typename P0, typename... Ps>
auto logical_data(P0&& p0, Ps&&... ps)
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
using T0 = ::std::remove_reference_t<P0>;
if constexpr (::std::is_integral_v<T0>)
{
// Assume we create an array with the given length, so forward to the previous function.
return logical_data<T0>(size_t(p0), ::std::forward<Ps>(ps)...);
}
else
{
// Forward all parameters to the homonym function in the context.
return payload->*[&](auto& self) {
return self.logical_data(::std::forward<P0>(p0), ::std::forward<Ps>(ps)...);
};
}
}
auto token()
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
return payload->*[&](auto& self) {
return self.token();
};
}
template <typename T>
frozen_logical_data<T>
freeze(::cuda::experimental::stf::logical_data<T> d,
access_mode m = access_mode::read,
data_place where = data_place::invalid(),
bool user_freeze = true)
{
return payload->*[&](auto& self) {
return self.freeze(mv(d), m, mv(where), user_freeze);
};
}
frozen_logical_data_untyped
freeze(::cuda::experimental::stf::logical_data_untyped d,
access_mode m = access_mode::read,
data_place where = data_place::invalid(),
bool user_freeze = true)
{
return payload->*[&](auto& self) {
return self.freeze(mv(d), m, mv(where), user_freeze);
};
}
/**
* @brief Creates logical data from a pointer and size.
*
* @tparam T The type of the logical data.
* @param p The pointer to the data.
* @param n The number of elements.
* @param dplace The data place of the logical data (default is host).
* @return The created logical data.
*/
template <typename T>
auto logical_data(T* p, size_t n, data_place dplace = data_place::host())
{
_CCCL_ASSERT(!dplace.is_invalid(), "");
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
return payload->*[&](auto& self) {
return self.logical_data(make_slice(p, n), mv(dplace));
};
}
template <typename... Deps>
unified_task<Deps...> task(exec_place e_place, task_dep<Deps>... deps)
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
// Workaround: For some obscure reason `mv(deps)...` fails to compile
return payload->*[&](auto& self) {
return unified_task<Deps...>(self.task(mv(e_place), ::std::move(deps)...));
};
}
template <typename... Deps>
unified_task<Deps...> task(task_dep<Deps>... deps)
{
return task(default_exec_place(), mv(deps)...);
}
#if !defined(CUDASTF_DISABLE_CODE_GENERATION) && _CCCL_CUDA_COMPILATION()
/*
* parallel_for : apply an operation over a shaped index space
*/
template <typename exec_place_t,
typename S,
typename... Deps,
typename = ::std::enable_if_t<std::is_base_of_v<exec_place, exec_place_t>>>
auto parallel_for(exec_place_t e_place, S shape, Deps... deps)
{
if constexpr (::std::is_integral_v<S>)
{
return parallel_for(mv(e_place), box(shape), mv(deps)...);
}
else
{
EXPECT(payload.index() != ::std::variant_npos, "Context is not initialized.");
using result_t = unified_scope<reserved::parallel_for_scope<stream_ctx, exec_place_t, S, null_partition, Deps...>,
reserved::parallel_for_scope<graph_ctx, exec_place_t, S, null_partition, Deps...>>;
return payload->*[&](auto& self) {
return result_t(self.parallel_for(mv(e_place), mv(shape), deps...));
};
}
}
template <typename partitioner_t,
typename exec_place_t,
typename S,
typename... Deps,
typename = ::std::enable_if_t<std::is_base_of_v<exec_place, exec_place_t>>>
auto parallel_for(partitioner_t p, exec_place_t e_place, S shape, Deps... deps)
{
EXPECT(payload.index() != ::std::variant_npos, "Context is not initialized.");
using result_t = unified_scope<reserved::parallel_for_scope<stream_ctx, exec_place_t, S, partitioner_t, Deps...>,
reserved::parallel_for_scope<graph_ctx, exec_place_t, S, partitioner_t, Deps...>>;
return payload->*[&](auto& self) {
return result_t(self.parallel_for(mv(p), mv(e_place), mv(shape), deps...));
};
}
template <typename S, typename... Deps>
auto parallel_for(S shape, Deps... deps)
{
return parallel_for(default_exec_place(), mv(shape), mv(deps)...);
}
#endif // !defined(CUDASTF_DISABLE_CODE_GENERATION) && _CCCL_CUDA_COMPILATION()
template <typename... Deps>
auto host_launch(task_dep<Deps>... deps)
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
using result_t = unified_scope<reserved::host_launch_scope<stream_ctx, false, Deps...>,
reserved::host_launch_scope<graph_ctx, false, Deps...>>;
return payload->*[&](auto& self) {
return result_t(self.host_launch(deps...));
};
}
template <typename... Deps>
auto cuda_kernel(task_dep<Deps>... deps)
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
// false : we expect a single kernel descriptor in the lambda function return type
using result_t = unified_scope<reserved::cuda_kernel_scope<stream_ctx, false, Deps...>,
reserved::cuda_kernel_scope<graph_ctx, false, Deps...>>;
return payload->*[&](auto& self) {
return result_t(self.cuda_kernel(deps...));
};
}
template <typename... Deps>
auto cuda_kernel(exec_place e_place, task_dep<Deps>... deps)
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
// false : we expect a single kernel descriptor in the lambda function return type
using result_t = unified_scope<reserved::cuda_kernel_scope<stream_ctx, false, Deps...>,
reserved::cuda_kernel_scope<graph_ctx, false, Deps...>>;
return payload->*[&](auto& self) {
return result_t(self.cuda_kernel(e_place, deps...));
};
}
template <typename... Deps>
auto cuda_kernel_chain(task_dep<Deps>... deps)
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
// true : we expect a vector of cuda kernel descriptors in the lambda function return type
using result_t = unified_scope<reserved::cuda_kernel_scope<stream_ctx, true, Deps...>,
reserved::cuda_kernel_scope<graph_ctx, true, Deps...>>;
return payload->*[&](auto& self) {
return result_t(self.cuda_kernel_chain(deps...));
};
}
template <typename... Deps>
auto cuda_kernel_chain(exec_place e_place, task_dep<Deps>... deps)
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
// true : we expect a vector of cuda kernel descriptors in the lambda function return type
using result_t = unified_scope<reserved::cuda_kernel_scope<stream_ctx, true, Deps...>,
reserved::cuda_kernel_scope<graph_ctx, true, Deps...>>;
return payload->*[&](auto& self) {
return result_t(self.cuda_kernel_chain(e_place, deps...));
};
}
#if !defined(CUDASTF_DISABLE_CODE_GENERATION) && _CCCL_CUDA_COMPILATION()
template <typename thread_hierarchy_spec_t, typename... Deps>
auto launch(thread_hierarchy_spec_t spec, exec_place e_place, task_dep<Deps>... deps)
{
using result_t = unified_scope<reserved::launch_scope<stream_ctx, thread_hierarchy_spec_t, Deps...>,
reserved::launch_scope<graph_ctx, thread_hierarchy_spec_t, Deps...>>;
return payload->*[&](auto& self) {
return result_t(self.launch(mv(spec), mv(e_place), deps...));
};
}
// /* Default execution policy, explicit place */
// default depth to avoid breaking all codes (XXX temporary)
template <typename... Deps>
auto launch(exec_place e_place, task_dep<Deps>... deps)
{
return launch(par(par()), mv(e_place), (deps)...);
}
// /* Default execution policy, on automatically selected device */
template <typename... Deps>
auto launch(task_dep<Deps>... deps)
{
return launch(default_exec_place(), mv(deps)...);
}
template <auto... spec, typename... Deps>
auto launch(thread_hierarchy_spec<spec...> ths, task_dep<Deps>... deps)
{
return launch(mv(ths), default_exec_place(), mv(deps)...);
}
#endif // !defined(CUDASTF_DISABLE_CODE_GENERATION) && _CCCL_CUDA_COMPILATION()
auto repeat(size_t count)
{
using result_t = unified_scope<reserved::repeat_scope<stream_ctx>, reserved::repeat_scope<graph_ctx>>;
return payload->*[&](auto& self) {
return result_t(self.repeat(count));
};
}
auto repeat(::std::function<bool()> condition)
{
using result_t = unified_scope<reserved::repeat_scope<stream_ctx>, reserved::repeat_scope<graph_ctx>>;
return payload->*[&](auto& self) {
return result_t(self.repeat(mv(condition)));
};
}
cudaStream_t fence()
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
return payload->*[&](auto& self) {
return self.fence();
};
}
void finalize()
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
payload->*[](auto& self) {
self.finalize();
};
}
//! Add a resource to be managed by this context
void add_resource(::std::shared_ptr<ctx_resource> resource)
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
payload->*[&resource](auto& self) {
self.add_resource(mv(resource));
};
}
//! Release context resources using the provided stream.
//! Normally called automatically during finalize(); when using finalize_as_graph()
//! for replayable graphs, call once the graph will no longer be used.
void release_resources(cudaStream_t stream)
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
payload->*[stream](auto& self) {
self.release_resources(stream);
};
}
//! Take all resources from \p other (e.g. a nested context) and merge them into this context.
//! \p other will have no resources after this call.
void import_resources_from(context& other)
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
import_resources(other.export_resources());
}
private:
ctx_resource_set export_resources()
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
return payload->*[](auto& self) {
return self.export_resources();
};
}
void import_resources(ctx_resource_set&& other)
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
payload->*[&other](auto& self) {
self.import_resources(mv(other));
};
}
public:
void submit()
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
payload->*[](auto& self) {
self.submit();
};
}
void set_allocator(block_allocator_untyped custom_allocator)
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
payload->*[&](auto& self) {
self.set_allocator(mv(custom_allocator));
};
}
void attach_allocator(block_allocator_untyped custom_allocator)
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
payload->*[&](auto& self) {
self.attach_allocator(mv(custom_allocator));
};
}
void update_uncached_allocator(block_allocator_untyped custom)
{
payload->*[&](auto& self) {
self.update_uncached_allocator(mv(custom));
};
}
void change_stage()
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
payload->*[](auto& self) {
self.change_stage();
};
}
::std::shared_ptr<reserved::per_ctx_dot> get_dot()
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
return payload->*[](auto& self) {
return self.get_dot();
};
}
template <typename T>
auto wait(::cuda::experimental::stf::logical_data<T>& ldata)
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
return payload->*[&ldata](auto& self) {
return self.wait(ldata);
};
}
template <typename parent_ctx_t>
void set_parent_ctx(parent_ctx_t& parent_ctx)
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
reserved::per_ctx_dot::set_parent_ctx(parent_ctx.get_dot(), get_dot());
payload->*[&](auto& self) {
self.set_parent_ctx(parent_ctx.get_dot());
};
}
void enable_logical_data_stats()
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
payload->*[&](auto& self) {
self.enable_logical_data_stats();
};
}
/**
* @brief RAII-style description of a new section in the DOT file identified by its symbol
*/
auto dot_section(::std::string symbol) const
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
return payload->*[&](auto& self) {
return self.dot_section(mv(symbol));
};
}
/* Indicates whether the underlying context is a graph context, so that we
* may specialize code to deal with the specific constraints of CUDA graphs. */
bool is_graph_ctx() const
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
return payload->*[&](auto& self) {
return self.is_graph_ctx();
};
}
async_resources_handle& async_resources() const
{
// if (payload.index() == 0) {
// return ::std::get<0>(payload).async_resources();
// }
// EXPECT(payload.index() == 1, "Uninitialized context.");
// return ::std::get<1>(payload).async_resources();
return payload->*[&](auto& self) -> async_resources_handle& {
return self.async_resources();
};
}
// Shortcuts to manipulate the current affinity stored in the async_resources_handle of the ctx
void push_affinity(::std::vector<::std::shared_ptr<exec_place>> p) const
{
async_resources().push_affinity(mv(p));
}
void push_affinity(::std::shared_ptr<exec_place> p) const
{
async_resources().push_affinity(mv(p));
}
void pop_affinity() const
{
async_resources().pop_affinity();
}
const ::std::vector<::std::shared_ptr<exec_place>>& current_affinity() const
{
return async_resources().current_affinity();
}
const exec_place& current_exec_place() const
{
_CCCL_ASSERT(current_affinity().size() > 0, "current_exec_place no affinity set");
return *(current_affinity()[0]);
}
bool has_affinity() const
{
return async_resources().has_affinity();
}
/**
* @brief Determines the default execution place for a given context, which
* corresponds to the execution place when no place is provided.
*
* @return execution place used by constructs where the place is implicit.
*
* By default, we select the current device, unless an affinity was set in the
* context, in which case we take the first execution place in the current
* places.
*/
exec_place default_exec_place() const
{
return has_affinity() ? current_exec_place() : exec_place::current_device();
}
graph_ctx to_graph_ctx() const
{
// Check if payload holds graph_ctx (index == 1)
if (auto ctx = ::std::get_if<graph_ctx>(&payload))
{
return *ctx;
}
else
{
_CCCL_THROW(::std::runtime_error, "Payload does not hold graph_ctx");
}
}
/**
* @brief Get a CUDA stream from the stream pool associated to the context
*
* This helper is intended to avoid creating CUDA streams manually. Using
* this stream after the context has been finalized is an undefined
* behaviour.
*/
cudaStream_t pick_stream()
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
return payload->*[](auto& self) {
return self.pick_stream();
};
}
/**
* @brief Get a reference to the underlying untyped backend context
*
* @return Reference to the backend_ctx_untyped base class from the variant payload
*/
backend_ctx_untyped& get_backend()
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
return ::std::visit(
[](auto& ctx) -> backend_ctx_untyped& {
return static_cast<backend_ctx_untyped&>(ctx);
},
payload);
}
const backend_ctx_untyped& get_backend() const
{
_CCCL_ASSERT(payload.index() != ::std::variant_npos, "Context is not initialized");
return ::std::visit(
[](const auto& ctx) -> const backend_ctx_untyped& {
return static_cast<const backend_ctx_untyped&>(ctx);
},
payload);
}
public:
::std::variant<stream_ctx, graph_ctx> payload;
};
#ifdef UNITTESTED_FILE
UNITTEST("context")
{
context ctx;
ctx.fence();
ctx.submit();