forked from NVIDIA/cccl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplaces.cuh
More file actions
1895 lines (1602 loc) · 50.3 KB
/
places.cuh
File metadata and controls
1895 lines (1602 loc) · 50.3 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-2024 NVIDIA CORPORATION & AFFILIATES.
//
//===----------------------------------------------------------------------===//
/** @file
*
* @brief Defines abstractions for places where data is stored and places where execution is carried.
*
* TODO Add more documentation about this file here.
*/
#pragma once
#include <cuda/__cccl_config>
#if defined(_CCCL_IMPLICIT_SYSTEM_HEADER_GCC)
# pragma GCC system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_CLANG)
# pragma clang system_header
#elif defined(_CCCL_IMPLICIT_SYSTEM_HEADER_MSVC)
# pragma system_header
#endif // no system header
#include <cuda/experimental/__places/data_place_impl.cuh>
#include <cuda/experimental/__places/exec/green_ctx_view.cuh>
#include <cuda/experimental/__stf/utility/core.cuh>
#include <typeinfo>
// Used only for unit tests, not in the actual implementation
#ifdef UNITTESTED_FILE
# include <map>
#endif
#include <cuda/experimental/__stf/utility/cuda_safe_call.cuh>
#include <cuda/experimental/__stf/utility/dimensions.cuh>
#include <cuda/experimental/__stf/utility/scope_guard.cuh>
// Sync only will not move data....
// Data place none?
namespace cuda::experimental::stf
{
class exec_place;
// Green contexts are only supported since CUDA 12.4
//! Function type for computing executor placement from data coordinates
using partition_fn_t = pos4 (*)(pos4, dim4, dim4);
// Forward declaration for composite implementation
class data_place_composite;
/**
* @brief Designates where data will be stored (CPU memory vs. on device 0 (first GPU), device 1 (second GPU), ...)
*
* This class uses a polymorphic design where all place types (host, managed, device,
* composite, future extensions) implement a common data_place_interface. The data_place class
* holds a shared_ptr to this interface and delegates operations to it.
*/
class data_place
{
template <typename T>
static ::std::shared_ptr<data_place_interface> make_static_instance()
{
static T instance;
return ::std::shared_ptr<data_place_interface>(&instance, [](data_place_interface*) {});
}
public:
explicit data_place(::std::shared_ptr<data_place_interface> impl)
: pimpl_(mv(impl))
{}
/**
* @brief Default constructor. The object is initialized as invalid.
*/
data_place()
: pimpl_(make_static_instance<data_place_invalid>())
{}
data_place(const data_place&) = default;
data_place(data_place&&) = default;
data_place& operator=(const data_place&) = default;
data_place& operator=(data_place&&) = default;
/**
* @brief Represents an invalid `data_place` object.
*/
static data_place invalid()
{
return data_place(make_static_instance<data_place_invalid>());
}
/**
* @brief Represents the host CPU as the `data_place` (pinned host memory, or
* memory which should be pinned by CUDASTF).
*/
static data_place host()
{
return data_place(make_static_instance<data_place_host>());
}
/**
* @brief Represents a managed memory location as the `data_place`.
*/
static data_place managed()
{
return data_place(make_static_instance<data_place_managed>());
}
/// This actually does not define a data_place, but means that we should use
/// the data place affine to the execution place
static data_place affine()
{
return data_place(make_static_instance<data_place_affine>());
}
/**
* @brief Constant representing a placeholder that lets the library automatically select a GPU device as the
* `data_place`.
*/
static data_place device_auto()
{
return data_place(make_static_instance<data_place_device_auto>());
}
/** @brief Data is placed on device with index dev_id. */
static data_place device(int dev_id = 0)
{
static int const ndevs = [] {
int result;
cuda_safe_call(cudaGetDeviceCount(&result));
return result;
}();
EXPECT((dev_id >= 0 && dev_id < ndevs), "Invalid device ID ", dev_id);
static data_place_device* impls = [] {
auto* result = static_cast<data_place_device*>(::operator new[](ndevs * sizeof(data_place_device)));
for (int i = 0; i < ndevs; ++i)
{
new (result + i) data_place_device(i);
}
return result;
}();
return data_place(::std::shared_ptr<data_place_interface>(&impls[dev_id], [](data_place_interface*) {}));
}
/**
* @brief Select the embedded memory of the current device as `data_place`.
*/
static data_place current_device()
{
return device(cuda_try<cudaGetDevice>());
}
// User-visible API when using a different partitioner than the one of the grid
template <typename partitioner_t /*, typename scalar_exec_place_t */>
static data_place composite(partitioner_t p, const exec_place& g);
static data_place composite(partition_fn_t f, const exec_place& grid);
#if _CCCL_CTK_AT_LEAST(12, 4)
static data_place green_ctx(const green_ctx_view& gc_view);
#endif // _CCCL_CTK_AT_LEAST(12, 4)
bool operator==(const data_place& rhs) const
{
// Same pointer means same place
if (pimpl_.get() == rhs.pimpl_.get())
{
return true;
}
return pimpl_->cmp(*rhs.pimpl_) == 0;
}
bool operator!=(const data_place& rhs) const
{
return !(*this == rhs);
}
// To use in a ::std::map indexed by data_place
bool operator<(const data_place& rhs) const
{
return pimpl_->cmp(*rhs.pimpl_) < 0;
}
bool operator>(const data_place& rhs) const
{
return rhs < *this;
}
bool operator<=(const data_place& rhs) const
{
return !(rhs < *this);
}
bool operator>=(const data_place& rhs) const
{
return !(*this < rhs);
}
// Defined later after data_place_composite is complete
bool is_composite() const;
bool is_invalid() const
{
const auto& ref = *pimpl_;
return typeid(ref) == typeid(data_place_invalid);
}
bool is_host() const
{
const auto& ref = *pimpl_;
return typeid(ref) == typeid(data_place_host);
}
bool is_managed() const
{
const auto& ref = *pimpl_;
return typeid(ref) == typeid(data_place_managed);
}
bool is_affine() const
{
const auto& ref = *pimpl_;
return typeid(ref) == typeid(data_place_affine);
}
bool is_device() const
{
const auto& ref = *pimpl_;
return typeid(ref) == typeid(data_place_device);
}
bool is_device_auto() const
{
const auto& ref = *pimpl_;
return typeid(ref) == typeid(data_place_device_auto);
}
bool is_resolved() const
{
return pimpl_->is_resolved();
}
::std::string to_string() const
{
return pimpl_->to_string();
}
/**
* @brief Returns an index guaranteed to be >= 0 (0 for managed CPU, 1 for pinned CPU, 2 for device 0, 3 for device
* 1, ...). Requires that `p` is initialized and different from `data_place::invalid()`.
*/
friend inline size_t to_index(const data_place& p)
{
int devid = p.pimpl_->get_device_ordinal();
EXPECT(devid >= -2, "Data place with device id ", devid, " does not refer to a device.");
_CCCL_ASSERT(devid < cuda_try<cudaGetDeviceCount>(), "Invalid device id");
return devid + 2;
}
/**
* @brief Inverse of `to_index`: converts an index back to a `data_place`.
* Index 0 -> managed, 1 -> host, 2 -> device(0), 3 -> device(1), ...
*/
friend inline data_place from_index(size_t n)
{
if (n == 0)
{
return data_place::managed();
}
if (n == 1)
{
return data_place::host();
}
return data_place::device(static_cast<int>(n - 2));
}
/**
* @brief Returns the device ordinal (0 = first GPU, 1 = second GPU, ... and by convention the CPU is -1)
* Requires that `p` is initialized.
*/
friend inline int device_ordinal(const data_place& p)
{
return p.pimpl_->get_device_ordinal();
}
const partition_fn_t& get_partitioner() const
{
return pimpl_->get_partitioner();
}
// Defined later after exec_place is complete
exec_place affine_exec_place() const;
/**
* @brief Compute a hash value for this data place
*
* Used by std::hash specialization for unordered containers.
*/
size_t hash() const
{
return pimpl_->hash();
}
decorated_stream getDataStream() const;
/**
* @brief Get the underlying interface pointer
*
* This is primarily for internal use and backward compatibility.
*/
const ::std::shared_ptr<data_place_interface>& get_impl() const
{
return pimpl_;
}
/**
* @brief Create a physical memory allocation for this place (VMM API)
*/
CUresult mem_create(CUmemGenericAllocationHandle* handle, size_t size) const
{
return pimpl_->mem_create(handle, size);
}
/**
* @brief Allocate memory at this data place (raw allocation)
*/
void* allocate(::std::ptrdiff_t size, cudaStream_t stream = nullptr) const
{
return pimpl_->allocate(size, stream);
}
/**
* @brief Deallocate memory at this data place (raw deallocation)
*/
void deallocate(void* ptr, size_t size, cudaStream_t stream = nullptr) const
{
pimpl_->deallocate(ptr, size, stream);
}
/**
* @brief Returns true if allocation/deallocation is stream-ordered
*/
bool allocation_is_stream_ordered() const
{
return pimpl_->allocation_is_stream_ordered();
}
private:
::std::shared_ptr<data_place_interface> pimpl_;
};
/** Declaration for unqualified lookup (friend is only found via ADL when a \c data_place argument is present). */
inline data_place from_index(size_t n);
// Forward declaration
class exec_place_scope;
/**
* @brief Indicates where a computation takes place (CPU, dev0, dev1, ...)
*
* All execution places are modeled as grids. Scalar places (host, single device)
* are simply 1-element grids. This unified model eliminates special-casing and
* allows uniform iteration over any exec_place.
*/
class exec_place
{
public:
/*
* @brief Using the pimpl idiom. Public because a number of classes inherit from this.
*/
class impl : public ::std::enable_shared_from_this<impl>
{
public:
impl() = default;
impl(const impl&) = delete;
impl& operator=(const impl&) = delete;
virtual ~impl() = default;
explicit impl(data_place place)
: affine(mv(place))
{}
// ===== Grid interface (all places are grids) =====
/**
* @brief Get the dimensions of this grid
*
* For scalar places, returns dim4(1, 1, 1, 1).
*/
virtual dim4 get_dims() const
{
return dim4(1, 1, 1, 1);
}
/**
* @brief Get the total number of places in this grid
*/
virtual size_t size() const
{
return 1;
}
/**
* @brief Get the impl of the sub-place at the given linear index
*
* For scalar places, idx must be 0 and returns shared_from_this().
* For grids, returns the impl of the stored sub-place.
*/
virtual ::std::shared_ptr<impl> get_place(size_t idx);
// ===== Activation/deactivation (indexed) =====
/**
* @brief Activate the sub-place at the given index
*
* For scalar places, idx must be 0.
* Returns the previous execution state needed for deactivate().
*/
virtual exec_place activate(size_t idx) const = 0;
/**
* @brief Deactivate the sub-place at the given index, restoring previous state
*/
virtual void deactivate(const exec_place& prev, size_t idx = 0) const = 0;
// ===== Properties =====
virtual bool is_host() const
{
return false;
}
virtual bool is_device() const
{
return false;
}
virtual data_place affine_data_place() const
{
return affine;
}
virtual ::std::string to_string() const
{
return "exec(" + affine.to_string() + ")";
}
virtual void set_affine_data_place(data_place place)
{
affine = mv(place);
}
// ===== Comparison =====
/**
* @brief Three-way comparison
* @return -1 if *this < rhs, 0 if *this == rhs, 1 if *this > rhs
*/
virtual int cmp(const impl& rhs) const
{
if (typeid(*this) != typeid(rhs))
{
return typeid(*this).before(typeid(rhs)) ? -1 : 1;
}
return (rhs.affine < affine) - (affine < rhs.affine);
}
virtual size_t hash() const
{
return affine.hash();
}
// ===== Stream management =====
virtual stream_pool& get_stream_pool(bool for_computation) const
{
return for_computation ? pool_compute : pool_data;
}
static constexpr size_t pool_size = 4;
static constexpr size_t data_pool_size = 4;
protected:
friend class exec_place;
data_place affine = data_place::invalid();
mutable stream_pool pool_compute;
mutable stream_pool pool_data;
};
template <typename T>
static ::std::shared_ptr<impl> make_static_instance()
{
static T instance;
return ::std::shared_ptr<impl>(&instance, [](impl*) {});
}
exec_place() = default;
bool operator==(const exec_place& rhs) const
{
if (pimpl.get() == rhs.pimpl.get())
{
return true;
}
return pimpl->cmp(*rhs.pimpl) == 0;
}
bool operator!=(const exec_place& rhs) const
{
return !(*this == rhs);
}
bool operator<(const exec_place& rhs) const
{
return pimpl->cmp(*rhs.pimpl) < 0;
}
bool operator>(const exec_place& rhs) const
{
return rhs < *this;
}
bool operator<=(const exec_place& rhs) const
{
return !(rhs < *this);
}
bool operator>=(const exec_place& rhs) const
{
return !(*this < rhs);
}
size_t hash() const
{
return pimpl->hash();
}
// ===== Grid interface (all places are grids) =====
/**
* @brief Get the dimensions of this grid
*
* For scalar places (host, single device), returns dim4(1, 1, 1, 1).
*/
dim4 get_dims() const
{
return pimpl->get_dims();
}
/**
* @brief Get the total number of places in this grid
*/
size_t size() const
{
return pimpl->size();
}
/**
* @brief Get the sub-place at the given linear index
*
* For scalar places, idx must be 0 and returns the place itself.
*/
exec_place get_place(size_t idx) const
{
return exec_place(pimpl->get_place(idx));
}
/**
* @brief Get the sub-place at the given multi-dimensional position
*/
exec_place get_place(pos4 p) const
{
return get_place(get_dims().get_index(p));
}
// ===== Activation =====
/**
* @brief Activate the sub-place at the given index
*
* Returns an exec_place_scope RAII guard that automatically deactivates when destroyed.
* For scalar places, idx should be 0 (the default).
*
* @param idx The index of the sub-place to activate (default 0 for scalar places)
* @return An exec_place_scope guard that manages the activation lifetime
*/
inline exec_place_scope activate(size_t idx = 0) const;
// ===== Properties =====
::std::string to_string() const
{
return pimpl->to_string();
}
data_place affine_data_place() const
{
return pimpl->affine_data_place();
}
void set_affine_data_place(data_place place)
{
pimpl->set_affine_data_place(mv(place));
}
stream_pool& get_stream_pool(bool for_computation) const
{
return pimpl->get_stream_pool(for_computation);
}
decorated_stream getStream(bool for_computation) const;
cudaStream_t pick_stream(bool for_computation = true) const
{
return getStream(for_computation).stream;
}
const ::std::shared_ptr<impl>& get_impl() const
{
return pimpl;
}
bool is_host() const
{
return pimpl->is_host();
}
bool is_device() const
{
return pimpl->is_device();
}
/**
* @brief Get the dimension along a specific axis
* @deprecated Use get_dims().get(axis_id) instead
*/
size_t grid_dim(int axis_id) const
{
return get_dims().get(axis_id);
}
/**
* @brief Get all dimensions
* @deprecated Use get_dims() instead
*/
dim4 grid_dims() const
{
return get_dims();
}
/**
* @brief Returns *this for compatibility
* @deprecated All places are grids now; use exec_place methods directly
*/
const exec_place& as_grid() const
{
EXPECT(size() > 1, "as_grid() called on scalar exec_place");
return *this;
}
/* These helper methods provide convenient way to express execution places,
* for example exec_place::host or exec_place::device(4).
*/
static exec_place host();
static exec_place device_auto();
static exec_place device(int devid);
// Green contexts are only supported since CUDA 12.4
#if _CCCL_CTK_AT_LEAST(12, 4)
/**
* @brief Create a green context execution place
*
* @param gc_view The green context view
* @param use_green_ctx_data_place If true, use a green context data place as the
* affine data place. If false (default), use a regular device data place instead.
*/
static exec_place green_ctx(const green_ctx_view& gc_view, bool use_green_ctx_data_place = false);
#endif // _CCCL_CTK_AT_LEAST(12, 4)
static exec_place cuda_stream(cudaStream_t stream);
static exec_place cuda_stream(const decorated_stream& dstream);
/**
* @brief Returns the currently active device.
*
* @return exec_place
*/
static exec_place current_device()
{
return exec_place::device(cuda_try<cudaGetDevice>());
}
static exec_place all_devices();
static exec_place n_devices(size_t n, dim4 dims);
static exec_place n_devices(size_t n);
// For debug purpose on a machine with a single GPU, for example
static exec_place repeat(const exec_place& e, size_t cnt);
template <typename... Args>
auto partition_by_scope(Args&&... args);
/**
* @brief Execute lambda on this place.
*
* This method accepts a functor, saves the current CUDA device, changes it to the current execution place,
* invokes the lambda, and finally sets the current device back to the previous one. The last step is
* taken even if the lambda throws an exception.
*
* @tparam Fun A callable entity type
* @param fun Input functor that will be forwarded and executed
*
* @return auto the result of the executed functor.
*
*/
template <typename Fun>
auto operator->*(Fun&& fun) const;
public:
exec_place(::std::shared_ptr<impl> pimpl)
: pimpl(mv(pimpl))
{}
private:
// No other state
::std::shared_ptr<impl> pimpl;
};
/**
* @brief RAII guard that activates an execution place and restores the previous one on destruction.
*
* This class provides a scoped mechanism for temporarily switching the active execution place.
* When constructed, it activates the given execution place (e.g., sets the current CUDA device).
* When destroyed, it restores the previous execution place that was active before construction.
*
* For grids, the index specifies which sub-place to activate. For scalar places, the index
* should be 0 (the default).
*
* The guard is non-copyable but movable (like std::unique_lock).
*
* Example usage:
* @code
* // Scalar place activation
* {
* auto active = exec_place::device(1).activate();
* // Device 1 is now active
* // ... perform operations on device 1 ...
* }
* // Previous device is restored
*
* // Grid iteration
* exec_place grid = make_grid(...);
* for (size_t i = 0; i < grid.size(); i++) {
* auto active = grid.activate(i);
* // grid[i] is now active
* kernel<<<..., active.place().getStream()>>>(...);
* }
* @endcode
*/
class exec_place_scope
{
public:
/**
* @brief Default constructor creates an inactive scope.
*/
exec_place_scope() = default;
/**
* @brief Constructs the guard and activates the sub-place at the given index.
*
* @param place The execution place (or grid) containing the sub-place to activate
* @param idx The index of the sub-place to activate (default 0 for scalar places)
*/
exec_place_scope(exec_place place, size_t idx = 0)
: place_(mv(place))
, idx_(idx)
, current_(place_.get_place(idx_))
, prev_(place_.get_impl()->activate(idx_))
{}
/**
* @brief Deleted constructor for data_place to prevent accidental misuse.
*
* Use data_place::affine_exec_place() to get the exec_place first.
*/
template <typename T = void>
exec_place_scope(const data_place&)
{
static_assert(!::std::is_same_v<T, T>,
"exec_place_scope cannot be constructed from data_place; "
"use data_place::affine_exec_place() to get the exec_place first");
}
/**
* @brief Destructor that restores the previous execution place (if not moved-from).
*/
~exec_place_scope()
{
if (place_.get_impl())
{
place_.get_impl()->deactivate(prev_, idx_);
}
}
// Non-copyable
exec_place_scope(const exec_place_scope&) = delete;
exec_place_scope& operator=(const exec_place_scope&) = delete;
// Movable (like unique_lock)
exec_place_scope(exec_place_scope&& other) noexcept
: place_(mv(other.place_))
, idx_(other.idx_)
, current_(mv(other.current_))
, prev_(mv(other.prev_))
{
other.place_ = exec_place(); // Mark other as inactive
}
exec_place_scope& operator=(exec_place_scope&& other) noexcept
{
if (this != &other)
{
if (place_.get_impl())
{
place_.get_impl()->deactivate(prev_, idx_);
}
place_ = mv(other.place_);
idx_ = other.idx_;
current_ = mv(other.current_);
prev_ = mv(other.prev_);
other.place_ = exec_place(); // Mark other as inactive
}
return *this;
}
/**
* @brief Get the currently active sub-place
*/
const exec_place& place() const
{
return current_;
}
/**
* @brief Get the index within the grid (0 for scalar places)
*/
size_t index() const
{
return idx_;
}
/**
* @brief Check if this scope is active (not moved-from)
*/
bool is_active() const
{
return place_.get_impl() != nullptr;
}
/**
* @brief Early deactivation - restores previous state and marks scope as inactive.
*
* After calling reset(), the destructor becomes a no-op.
* Calling reset() on an inactive scope is safe (no-op).
*/
void reset()
{
if (place_.get_impl())
{
place_.get_impl()->deactivate(prev_, idx_);
place_ = exec_place(); // Mark as inactive
}
}
private:
exec_place place_; // The grid (or scalar place); empty means inactive
size_t idx_ = 0; // Index within grid
exec_place current_; // The activated sub-place
exec_place prev_; // Previous state to restore
};
inline exec_place_scope exec_place::activate(size_t idx) const
{
return exec_place_scope(*this, idx);
}
template <typename Fun>
auto exec_place::operator->*(Fun&& fun) const
{
auto active = activate();
return ::std::forward<Fun>(fun)();
}
inline decorated_stream stream_pool::next(const exec_place& place)
{
_CCCL_ASSERT(pimpl, "stream_pool::next called on empty pool");
::std::lock_guard<::std::mutex> locker(pimpl->mtx);
_CCCL_ASSERT(pimpl->index < pimpl->payload.size(), "stream_pool::next index out of range");
auto& result = pimpl->payload.at(pimpl->index);
if (!result.stream)
{
auto active = place.activate();
cuda_safe_call(cudaStreamCreateWithFlags(&result.stream, cudaStreamNonBlocking));
result.id = get_stream_id(result.stream);
result.dev_id = get_device_from_stream(result.stream);
}
_CCCL_ASSERT(result.stream != nullptr && result.dev_id != -1, "stream_pool slot invalid after creation");
if (++pimpl->index >= pimpl->payload.size())
{
pimpl->index = 0;
}
return result;
}
inline decorated_stream exec_place::getStream(bool for_computation) const
{
return get_stream_pool(for_computation).next(*this);
}
/**
* @brief Host execution place implementation.
*
* Host is modeled as a 1-element grid containing the host execution context.
*/
class exec_place_host_impl : public exec_place::impl
{
public:
exec_place_host_impl()
: exec_place::impl(data_place::host())
{}
// Grid interface - host is a 1-element grid
::std::shared_ptr<exec_place::impl> get_place(size_t idx) override
{
_CCCL_ASSERT(idx == 0, "Index out of bounds for host exec_place");
// Static instance - use no-op deleter instead of shared_from_this()
return ::std::shared_ptr<impl>(this, [](impl*) {});
}
// Activation - no-op for host
exec_place activate(size_t idx) const override
{
_CCCL_ASSERT(idx == 0, "Index out of bounds for host exec_place");
return exec_place();
}
void deactivate(const exec_place& prev, size_t idx = 0) const override
{
_CCCL_ASSERT(idx == 0, "Index out of bounds for host exec_place");
_CCCL_ASSERT(!prev.get_impl(), "Host deactivate expects empty prev");
}
bool is_host() const override
{
return true;
}
data_place affine_data_place() const override
{
return data_place::host();
}
stream_pool& get_stream_pool(bool for_computation) const override
{
return exec_place::current_device().get_stream_pool(for_computation);
}
::std::string to_string() const override
{
return "host";
}
};
inline exec_place exec_place::host()
{
return exec_place(make_static_instance<exec_place_host_impl>());
}
// Implementation for device_auto placeholder
class exec_place_device_auto_impl : public exec_place::impl
{
public:
exec_place_device_auto_impl()