-
Notifications
You must be signed in to change notification settings - Fork 359
Expand file tree
/
Copy pathdevice_for.cuh
More file actions
1368 lines (1314 loc) · 51.3 KB
/
device_for.cuh
File metadata and controls
1368 lines (1314 loc) · 51.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
// SPDX-FileCopyrightText: Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause
#pragma once
#include <cub/config.cuh>
#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 <cub/device/dispatch/dispatch_for.cuh>
#include <cub/util_namespace.cuh>
#include <thrust/detail/raw_reference_cast.h>
#include <thrust/type_traits/is_contiguous_iterator.h>
#include <thrust/type_traits/unwrap_contiguous_iterator.h>
#include <cuda/__cmath/ceil_div.h>
#include <cuda/__functional/call_or.h>
#include <cuda/__stream/get_stream.h>
#include <cuda/std/__concepts/concept_macros.h>
#include <cuda/std/__fwd/mdspan.h>
#include <cuda/std/__iterator/distance.h>
#include <cuda/std/__mdspan/extents.h>
#include <cuda/std/__mdspan/layout_left.h>
#include <cuda/std/__mdspan/layout_right.h>
#include <cuda/std/__memory/is_sufficiently_aligned.h>
#include <cuda/std/__type_traits/enable_if.h>
#include <cuda/std/__type_traits/is_convertible.h>
#include <cuda/std/__type_traits/is_integral.h>
#include <cuda/std/array>
CUB_NAMESPACE_BEGIN
namespace detail::for_each
{
/**
* `op_wrapper_t` turns bulk into a for-each operation by wrapping the user-provided unary operator.
*/
template <class OffsetT, class OpT, class RandomAccessIteratorT>
struct op_wrapper_t
{
RandomAccessIteratorT input;
OpT op;
_CCCL_DEVICE _CCCL_FORCEINLINE void operator()(OffsetT i)
{
// Dereferencing `thrust::device_vector<T>` iterators returns a `thrust::device_reference<T>`
// instead of `T`. Since user-provided operator expects `T` as an argument, we need to unwrap.
(void) op(THRUST_NS_QUALIFIER::raw_reference_cast(*(input + i)));
}
};
/**
* `op_wrapper_vectorized_t` turns bulk into a for-each-copy operation.
* `op_wrapper_vectorized_t` is similar to `op_wrapper_t` but does not provide any guarantees about
* address of the input parameter. `OpT` might be given a copy of the value or an actual reference
* to the input iterator value (depending on the alignment of input iterator)
*/
template <class OffsetT, class OpT, class T>
struct op_wrapper_vectorized_t
{
const T* input; // Raw pointer to the input data
OpT op; // User-provided operator
OffsetT partially_filled_vector_id; // Index of the vector that doesn't have all elements
OffsetT num_items; // Total number of non-vectorized items
// TODO Can be extracted into tuning
constexpr static int vec_size = 4;
// Type of the vector that is used to load the input data
using vector_t = typename CubVector<T, vec_size>::Type;
_CCCL_DEVICE _CCCL_FORCEINLINE void operator()(OffsetT i)
{
// Surrounding `Bulk` call doesn't invoke this operator on invalid indices, so we don't need to
// check for out-of-bounds access here.
if (i != partially_filled_vector_id)
{ // Case of fully filled vector
const vector_t vec = *reinterpret_cast<const vector_t*>(input + vec_size * i);
_CCCL_PRAGMA_UNROLL_FULL()
for (int j = 0; j < vec_size; j++)
{
(void) op(*(reinterpret_cast<const T*>(&vec) + j));
}
}
else
{ // Case of partially filled vector
for (OffsetT j = i * vec_size; j < num_items; j++)
{
(void) op(input[j]);
}
}
}
};
} // namespace detail::for_each
struct DeviceFor
{
private:
template <bool UseVectorization, class RandomAccessOrContiguousIteratorT, class OffsetT, class OpT>
CUB_RUNTIME_FUNCTION static cudaError_t
for_each_n(RandomAccessOrContiguousIteratorT first, OffsetT num_items, OpT op, cudaStream_t stream)
{
if constexpr (UseVectorization)
{
auto* unwrapped_first = THRUST_NS_QUALIFIER::unwrap_contiguous_iterator(first);
using wrapped_op_t =
detail::for_each::op_wrapper_vectorized_t<OffsetT, OpT, detail::it_value_t<RandomAccessOrContiguousIteratorT>>;
if (::cuda::std::is_sufficiently_aligned<alignof(typename wrapped_op_t::vector_t)>(unwrapped_first))
{ // Vectorize loads
const OffsetT num_vec_items = ::cuda::ceil_div(num_items, wrapped_op_t::vec_size);
return detail::for_each::dispatch<OffsetT, wrapped_op_t>(
num_vec_items,
wrapped_op_t{
unwrapped_first, op, num_items % wrapped_op_t::vec_size ? num_vec_items - 1 : num_vec_items, num_items},
stream);
}
// Fallback to non-vectorized version
return for_each_n<false>(first, num_items, op, stream);
}
else
{
using wrapped_op_t = detail::for_each::op_wrapper_t<OffsetT, OpT, RandomAccessOrContiguousIteratorT>;
return detail::for_each::dispatch<OffsetT, wrapped_op_t>(num_items, wrapped_op_t{first, op}, stream);
}
}
public:
//! @rst
//! Overview
//! +++++++++++++++++++++++++++++++++++++++++++++
//!
//! Applies the function object ``op`` to each index in the provided shape
//! The algorithm is similar to
//! `bulk <https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2300r5.html#design-sender-adaptor-bulk>`_
//! from P2300.
//!
//! .. versionadded:: 2.4.0
//! First appears in CUDA Toolkit 12.5.
//!
//! - The return value of ``op``, if any, is ignored.
//! - @devicestorage
//!
//! A Simple Example
//! +++++++++++++++++++++++++++++++++++++++++++++
//!
//! The following code snippet demonstrates how to use Bulk to square each element in a device vector.
//!
//! .. literalinclude:: ../../../cub/test/catch2_test_device_for_api.cu
//! :language: c++
//! :dedent:
//! :start-after: example-begin bulk-square-t
//! :end-before: example-end bulk-square-t
//!
//! .. literalinclude:: ../../../cub/test/catch2_test_device_for_api.cu
//! :language: c++
//! :dedent:
//! :start-after: example-begin bulk-temp-storage
//! :end-before: example-end bulk-temp-storage
//!
//! @endrst
//!
//! @tparam ShapeT
//! is an integral type
//!
//! @tparam OpT
//! is a model of [Unary Function](https://en.cppreference.com/w/cpp/utility/functional/unary_function)
//!
//! @param[in] d_temp_storage
//! Device-accessible allocation of temporary storage. When `nullptr`,
//! the required allocation size is written to `temp_storage_bytes` and no work is done.
//!
//! @param[in,out] temp_storage_bytes
//! Reference to size in bytes of `d_temp_storage` allocation
//!
//! @param[in] shape
//! Shape of the index space to iterate over
//!
//! @param[in] op
//! Function object to apply to each index in the index space
//!
//! @param[in] stream
//! CUDA stream to launch kernels within. Default stream is `0`.
template <class ShapeT, class OpT>
CUB_RUNTIME_FUNCTION static cudaError_t
Bulk(void* d_temp_storage, size_t& temp_storage_bytes, ShapeT shape, OpT op, cudaStream_t stream = {})
{
static_assert(::cuda::std::is_integral_v<ShapeT>, "ShapeT must be an integral type");
if (d_temp_storage == nullptr)
{
temp_storage_bytes = 1;
return cudaSuccess;
}
return Bulk(shape, op, stream);
}
//! @rst
//! Overview
//! +++++++++++++++++++++++++++++++++++++++++++++
//!
//! Applies the function object ``op`` to each element in the range ``[first, first + num_items)``
//!
//! .. versionadded:: 2.4.0
//! First appears in CUDA Toolkit 12.5.
//!
//! - The return value of ``op``, if any, is ignored.
//! - @devicestorage
//!
//! A Simple Example
//! +++++++++++++++++++++++++++++++++++++++++++++
//!
//! The following code snippet demonstrates how to use `ForEachN` to square each element in a device vector.
//!
//! .. literalinclude:: ../../../cub/test/catch2_test_device_for_api.cu
//! :language: c++
//! :dedent:
//! :start-after: example-begin bulk-square-ref-t
//! :end-before: example-end bulk-square-ref-t
//!
//! .. literalinclude:: ../../../cub/test/catch2_test_device_for_api.cu
//! :language: c++
//! :dedent:
//! :start-after: example-begin for-each-n-temp-storage
//! :end-before: example-end for-each-n-temp-storage
//!
//! @endrst
//!
//! @tparam RandomAccessIteratorT
//! is a model of Random Access Iterator whose value type is convertible to `op`'s argument type.
//!
//! @tparam NumItemsT
//! is an integral type representing the number of elements to iterate over
//!
//! @tparam OpT
//! is a model of [Unary Function](https://en.cppreference.com/w/cpp/utility/functional/unary_function)
//!
//! @param[in] d_temp_storage
//! Device-accessible allocation of temporary storage. When `nullptr`,
//! the required allocation size is written to `temp_storage_bytes` and no work is done.
//!
//! @param[in,out] temp_storage_bytes
//! Reference to size in bytes of `d_temp_storage` allocation
//!
//! @param[in] first
//! The beginning of the sequence
//!
//! @param[in] num_items
//! Number of elements to iterate over
//!
//! @param[in] op
//! Function object to apply to each element in the range
//!
//! @param[in] stream
//! CUDA stream to launch kernels within. Default stream is `0`.
template <class RandomAccessIteratorT, class NumItemsT, class OpT>
CUB_RUNTIME_FUNCTION static cudaError_t ForEachN(
void* d_temp_storage,
size_t& temp_storage_bytes,
RandomAccessIteratorT first,
NumItemsT num_items,
OpT op,
cudaStream_t stream = {})
{
if (d_temp_storage == nullptr)
{
temp_storage_bytes = 1;
return cudaSuccess;
}
return ForEachN(first, num_items, op, stream);
}
//! @rst
//! Overview
//! +++++++++++++++++++++++++++++++++++++++++++++
//!
//! Applies the function object ``op`` to each element in the range ``[first, last)``
//!
//! .. versionadded:: 2.4.0
//! First appears in CUDA Toolkit 12.5.
//!
//! - The return value of ``op``, if any, is ignored.
//! - @devicestorage
//!
//! A Simple Example
//! +++++++++++++++++++++++++++++++++++++++++++++
//!
//! The following code snippet demonstrates how to use `ForEach` to square each element in a device vector.
//!
//! .. literalinclude:: ../../../cub/test/catch2_test_device_for_api.cu
//! :language: c++
//! :dedent:
//! :start-after: example-begin bulk-square-ref-t
//! :end-before: example-end bulk-square-ref-t
//!
//! .. literalinclude:: ../../../cub/test/catch2_test_device_for_api.cu
//! :language: c++
//! :dedent:
//! :start-after: example-begin for-each-temp-storage
//! :end-before: example-end for-each-temp-storage
//!
//! @endrst
//!
//! @tparam RandomAccessIteratorT
//! is a model of Random Access Iterator whose value type is convertible to `op`'s argument type.
//!
//! @tparam OpT
//! is a model of [Unary Function](https://en.cppreference.com/w/cpp/utility/functional/unary_function)
//!
//! @param[in] d_temp_storage
//! Device-accessible allocation of temporary storage. When `nullptr`,
//! the required allocation size is written to `temp_storage_bytes` and no work is done.
//!
//! @param[in,out] temp_storage_bytes
//! Reference to size in bytes of `d_temp_storage` allocation
//!
//! @param[in] first
//! The beginning of the sequence
//!
//! @param[in] last
//! The end of the sequence
//!
//! @param[in] op
//! Function object to apply to each element in the range
//!
//! @param[in] stream
//! CUDA stream to launch kernels within. Default stream is `0`.
template <class RandomAccessIteratorT, class OpT>
CUB_RUNTIME_FUNCTION static cudaError_t ForEach(
void* d_temp_storage,
size_t& temp_storage_bytes,
RandomAccessIteratorT first,
RandomAccessIteratorT last,
OpT op,
cudaStream_t stream = {})
{
if (d_temp_storage == nullptr)
{
temp_storage_bytes = 1;
return cudaSuccess;
}
return ForEach(first, last, op, stream);
}
//! @rst
//! Overview
//! +++++++++++++++++++++++++++++++++++++++++++++
//!
//! Applies the function object ``op`` to each element in the range ``[first, first + num_items)``.
//! Unlike the ``ForEachN`` algorithm, ``ForEachCopyN`` is allowed to invoke ``op`` on copies of the elements.
//! This relaxation allows ``ForEachCopyN`` to vectorize loads.
//!
//! .. versionadded:: 2.4.0
//! First appears in CUDA Toolkit 12.5.
//!
//! - Allowed to invoke ``op`` on copies of the elements
//! - The return value of ``op``, if any, is ignored.
//! - @devicestorage
//!
//! A Simple Example
//! +++++++++++++++++++++++++++++++++++++++++++++
//!
//! The following code snippet demonstrates how to use `ForEachCopyN` to count odd elements in a device vector.
//!
//! .. literalinclude:: ../../../cub/test/catch2_test_device_for_api.cu
//! :language: c++
//! :dedent:
//! :start-after: example-begin bulk-odd-count-t
//! :end-before: example-end bulk-odd-count-t
//!
//! .. literalinclude:: ../../../cub/test/catch2_test_device_for_api.cu
//! :language: c++
//! :dedent:
//! :start-after: example-begin for-each-copy-n-temp-storage
//! :end-before: example-end for-each-copy-n-temp-storage
//!
//! @endrst
//!
//! @tparam RandomAccessIteratorT
//! is a model of Random Access Iterator whose value type is convertible to `op`'s argument type.
//!
//! @tparam NumItemsT
//! is an integral type representing the number of elements to iterate over
//!
//! @tparam OpT
//! is a model of [Unary Function](https://en.cppreference.com/w/cpp/utility/functional/unary_function)
//!
//! @param[in] d_temp_storage
//! Device-accessible allocation of temporary storage. When `nullptr`,
//! the required allocation size is written to `temp_storage_bytes` and no work is done.
//!
//! @param[in,out] temp_storage_bytes
//! Reference to size in bytes of `d_temp_storage` allocation
//!
//! @param[in] first
//! The beginning of the sequence
//!
//! @param[in] num_items
//! Number of elements to iterate over
//!
//! @param[in] op
//! Function object to apply to a copy of each element in the range
//!
//! @param[in] stream
//! CUDA stream to launch kernels within. Default stream is `0`.
template <class RandomAccessIteratorT, class NumItemsT, class OpT>
CUB_RUNTIME_FUNCTION static cudaError_t ForEachCopyN(
void* d_temp_storage,
size_t& temp_storage_bytes,
RandomAccessIteratorT first,
NumItemsT num_items,
OpT op,
cudaStream_t stream = {})
{
if (d_temp_storage == nullptr)
{
temp_storage_bytes = 1;
return cudaSuccess;
}
return ForEachCopyN(first, num_items, op, stream);
}
//! @rst
//! Overview
//! +++++++++++++++++++++++++++++++++++++++++++++
//!
//! Applies the function object ``op`` to each element in the range ``[first, last)``.
//! Unlike the ``ForEach`` algorithm, ``ForEachCopy`` is allowed to invoke ``op`` on copies of the elements.
//! This relaxation allows ``ForEachCopy`` to vectorize loads.
//!
//! .. versionadded:: 2.4.0
//! First appears in CUDA Toolkit 12.5.
//!
//! - Allowed to invoke ``op`` on copies of the elements
//! - The return value of ``op``, if any, is ignored.
//! - @devicestorage
//!
//! A Simple Example
//! +++++++++++++++++++++++++++++++++++++++++++++
//!
//! The following code snippet demonstrates how to use `ForEachCopy` to count odd elements in a device vector.
//!
//! .. literalinclude:: ../../../cub/test/catch2_test_device_for_api.cu
//! :language: c++
//! :dedent:
//! :start-after: example-begin bulk-odd-count-t
//! :end-before: example-end bulk-odd-count-t
//!
//! .. literalinclude:: ../../../cub/test/catch2_test_device_for_api.cu
//! :language: c++
//! :dedent:
//! :start-after: example-begin for-each-copy-temp-storage
//! :end-before: example-end for-each-copy-temp-storage
//!
//! @endrst
//!
//! @tparam RandomAccessIteratorT
//! is a model of Random Access Iterator whose value type is convertible to `op`'s argument type.
//!
//! @tparam OpT
//! is a model of [Unary Function](https://en.cppreference.com/w/cpp/utility/functional/unary_function)
//!
//! @param[in] d_temp_storage
//! Device-accessible allocation of temporary storage. When `nullptr`,
//! the required allocation size is written to `temp_storage_bytes` and no work is done.
//!
//! @param[in,out] temp_storage_bytes
//! Reference to size in bytes of `d_temp_storage` allocation
//!
//! @param[in] first
//! The beginning of the sequence
//!
//! @param[in] last
//! The end of the sequence
//!
//! @param[in] op
//! Function object to apply to a copy of each element in the range
//!
//! @param[in] stream
//! CUDA stream to launch kernels within. Default stream is `0`.
template <class RandomAccessIteratorT, class OpT>
CUB_RUNTIME_FUNCTION static cudaError_t ForEachCopy(
void* d_temp_storage,
size_t& temp_storage_bytes,
RandomAccessIteratorT first,
RandomAccessIteratorT last,
OpT op,
cudaStream_t stream = {})
{
if (d_temp_storage == nullptr)
{
temp_storage_bytes = 1;
return cudaSuccess;
}
return ForEachCopy(first, last, op, stream);
}
//! @rst
//! Overview
//! +++++++++++++++++++++++++++++++++++++++++++++
//!
//! Applies the function object ``op`` to each index in the provided shape
//! The algorithm is similar to
//! `bulk <https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2300r5.html#design-sender-adaptor-bulk>`_
//! from P2300.
//!
//! .. versionadded:: 2.4.0
//! First appears in CUDA Toolkit 12.5.
//!
//! This is an environment-based API that allows customization of:
//!
//! - Stream: Query via ``cuda::get_stream``
//!
//! - The return value of ``op``, if any, is ignored.
//!
//! A Simple Example
//! +++++++++++++++++++++++++++++++++++++++++++++
//!
//! The following code snippet demonstrates how to use Bulk to square each element in a device vector.
//!
//! .. literalinclude:: ../../../cub/test/catch2_test_device_for_api.cu
//! :language: c++
//! :dedent:
//! :start-after: example-begin bulk-square-t
//! :end-before: example-end bulk-square-t
//!
//! .. literalinclude:: ../../../cub/test/catch2_test_device_for_api.cu
//! :language: c++
//! :dedent:
//! :start-after: example-begin bulk-wo-temp-storage
//! :end-before: example-end bulk-wo-temp-storage
//!
//! Environment Example
//! +++++++++++++++++++++++++++++++++++++++++++++
//!
//! The following code snippet demonstrates how to use Bulk with a custom stream via an environment.
//!
//! .. literalinclude:: ../../../cub/test/catch2_test_device_for_env_api.cu
//! :language: c++
//! :dedent:
//! :start-after: example-begin bulk-square-env-t
//! :end-before: example-end bulk-square-env-t
//!
//! .. literalinclude:: ../../../cub/test/catch2_test_device_for_env_api.cu
//! :language: c++
//! :dedent:
//! :start-after: example-begin bulk-env
//! :end-before: example-end bulk-env
//!
//! @endrst
//!
//! @tparam ShapeT
//! is an integral type
//!
//! @tparam OpT
//! is a model of [Unary Function](https://en.cppreference.com/w/cpp/utility/functional/unary_function)
//!
//! @tparam EnvT
//! **[inferred]** Execution environment type. Default is ``cuda::std::execution::env<>``.
//! Supports customization of stream via ``cuda::get_stream``.
//!
//! @param[in] shape
//! Shape of the index space to iterate over
//!
//! @param[in] op
//! Function object to apply to each index in the index space
//!
//! @param[in] env
//! @rst
//! **[optional]** Execution environment. Default is ``cuda::std::execution::env{}``.
//! @endrst
template <class ShapeT,
class OpT,
class EnvT = ::cuda::std::execution::env<>,
::cuda::std::enable_if_t<!::cuda::std::is_convertible_v<EnvT, cudaStream_t>, int> = 0>
CUB_RUNTIME_FUNCTION static cudaError_t Bulk(ShapeT shape, OpT op, EnvT env = {})
{
_CCCL_NVTX_RANGE_SCOPE("cub::DeviceFor::Bulk");
static_assert(::cuda::std::is_integral_v<ShapeT>, "ShapeT must be an integral type");
if (shape == 0)
{
return cudaSuccess;
}
auto stream = ::cuda::__call_or(::cuda::get_stream, ::cuda::stream_ref{cudaStream_t{}}, env);
return detail::for_each::dispatch</* OffsetT */ ShapeT, OpT>(shape, op, stream.get());
}
// we need this so the previous overload is not ambiguous with the next one
static_assert(!::cuda::std::is_convertible_v<::cuda::stream_ref, cudaStream_t>);
// We keep this overload around to support types that are convertible to `cudaStream_t` but not copyable
template <class ShapeT, class OpT>
CUB_RUNTIME_FUNCTION static cudaError_t Bulk(ShapeT shape, OpT op, cudaStream_t stream)
{
return Bulk(shape, op, ::cuda::stream_ref{stream});
}
private:
// Internal version without NVTX raNGE
template <class RandomAccessIteratorT, class NumItemsT, class OpT>
CUB_RUNTIME_FUNCTION static cudaError_t
ForEachNNoNVTX(RandomAccessIteratorT first, NumItemsT num_items, OpT op, cudaStream_t stream = {})
{
using offset_t = NumItemsT;
// Disable auto-vectorization for now:
// constexpr bool use_vectorization =
// detail::for_each::can_regain_copy_freedom<detail::it_value_t<RandomAccessIteratorT>, OpT>::value
// && THRUST_NS_QUALIFIER::is_contiguous_iterator_v<RandomAccessIteratorT>;
constexpr bool use_vectorization = false;
return for_each_n<use_vectorization, RandomAccessIteratorT, offset_t, OpT>(first, num_items, op, stream);
}
public:
//! @rst
//! Overview
//! +++++++++++++++++++++++++++++++++++++++++++++
//!
//! Applies the function object ``op`` to each element in the range ``[first, first + num_items)``
//!
//! .. versionadded:: 2.4.0
//! First appears in CUDA Toolkit 12.5.
//!
//! This is an environment-based API that allows customization of:
//!
//! - Stream: Query via ``cuda::get_stream``
//!
//! - The return value of ``op``, if any, is ignored.
//!
//! A Simple Example
//! +++++++++++++++++++++++++++++++++++++++++++++
//!
//! The following code snippet demonstrates how to use `ForEachN` to square each element in a device vector.
//!
//! .. literalinclude:: ../../../cub/test/catch2_test_device_for_api.cu
//! :language: c++
//! :dedent:
//! :start-after: example-begin bulk-square-ref-t
//! :end-before: example-end bulk-square-ref-t
//!
//! .. literalinclude:: ../../../cub/test/catch2_test_device_for_api.cu
//! :language: c++
//! :dedent:
//! :start-after: example-begin for-each-n-wo-temp-storage
//! :end-before: example-end for-each-n-wo-temp-storage
//!
//! Environment Example
//! +++++++++++++++++++++++++++++++++++++++++++++
//!
//! The following code snippet demonstrates how to use `ForEachN` with a custom stream via an environment.
//!
//! .. literalinclude:: ../../../cub/test/catch2_test_device_for_env_api.cu
//! :language: c++
//! :dedent:
//! :start-after: example-begin square-ref-env-t
//! :end-before: example-end square-ref-env-t
//!
//! .. literalinclude:: ../../../cub/test/catch2_test_device_for_env_api.cu
//! :language: c++
//! :dedent:
//! :start-after: example-begin for-each-n-env
//! :end-before: example-end for-each-n-env
//!
//! @endrst
//!
//! @tparam RandomAccessIteratorT
//! is a model of Random Access Iterator whose value type is convertible to `op`'s argument type.
//!
//! @tparam NumItemsT
//! is an integral type representing the number of elements to iterate over
//!
//! @tparam OpT
//! is a model of [Unary Function](https://en.cppreference.com/w/cpp/utility/functional/unary_function)
//!
//! @tparam EnvT
//! **[inferred]** Execution environment type. Default is ``cuda::std::execution::env<>``.
//! Supports customization of stream via ``cuda::get_stream``.
//!
//! @param[in] first
//! The beginning of the sequence
//!
//! @param[in] num_items
//! Number of elements to iterate over
//!
//! @param[in] op
//! Function object to apply to each element in the range
//!
//! @param[in] env
//! @rst
//! **[optional]** Execution environment. Default is ``cuda::std::execution::env{}``.
//! @endrst
template <class RandomAccessIteratorT,
class NumItemsT,
class OpT,
class EnvT = ::cuda::std::execution::env<>,
::cuda::std::enable_if_t<!::cuda::std::is_convertible_v<EnvT, cudaStream_t>, int> = 0>
CUB_RUNTIME_FUNCTION static cudaError_t
ForEachN(RandomAccessIteratorT first, NumItemsT num_items, OpT op, EnvT env = {})
{
_CCCL_NVTX_RANGE_SCOPE("cub::DeviceFor::ForEachN");
auto stream = ::cuda::__call_or(::cuda::get_stream, ::cuda::stream_ref{cudaStream_t{}}, env);
return ForEachNNoNVTX(first, num_items, op, stream.get());
}
// We keep this overload around to support types that are convertible to `cudaStream_t` but not copyable
template <class RandomAccessIteratorT, class NumItemsT, class OpT>
CUB_RUNTIME_FUNCTION static cudaError_t
ForEachN(RandomAccessIteratorT first, NumItemsT num_items, OpT op, cudaStream_t stream)
{
return ForEachN(first, num_items, op, ::cuda::stream_ref{stream});
}
//! @rst
//! Overview
//! +++++++++++++++++++++++++++++++++++++++++++++
//!
//! Applies the function object ``op`` to each element in the range ``[first, last)``
//!
//! .. versionadded:: 2.4.0
//! First appears in CUDA Toolkit 12.5.
//!
//! This is an environment-based API that allows customization of:
//!
//! - Stream: Query via ``cuda::get_stream``
//!
//! - The return value of ``op``, if any, is ignored.
//!
//! A Simple Example
//! +++++++++++++++++++++++++++++++++++++++++++++
//!
//! The following code snippet demonstrates how to use `ForEach` to square each element in a device vector.
//!
//! .. literalinclude:: ../../../cub/test/catch2_test_device_for_api.cu
//! :language: c++
//! :dedent:
//! :start-after: example-begin bulk-square-ref-t
//! :end-before: example-end bulk-square-ref-t
//!
//! .. literalinclude:: ../../../cub/test/catch2_test_device_for_api.cu
//! :language: c++
//! :dedent:
//! :start-after: example-begin for-each-wo-temp-storage
//! :end-before: example-end for-each-wo-temp-storage
//!
//! Environment Example
//! +++++++++++++++++++++++++++++++++++++++++++++
//!
//! The following code snippet demonstrates how to use `ForEach` with a custom stream via an environment.
//!
//! .. literalinclude:: ../../../cub/test/catch2_test_device_for_env_api.cu
//! :language: c++
//! :dedent:
//! :start-after: example-begin square-ref-env-t
//! :end-before: example-end square-ref-env-t
//!
//! .. literalinclude:: ../../../cub/test/catch2_test_device_for_env_api.cu
//! :language: c++
//! :dedent:
//! :start-after: example-begin for-each-env
//! :end-before: example-end for-each-env
//!
//! @endrst
//!
//! @tparam RandomAccessIteratorT
//! is a model of Random Access Iterator whose value type is convertible to `op`'s argument type.
//!
//! @tparam OpT
//! is a model of [Unary Function](https://en.cppreference.com/w/cpp/utility/functional/unary_function)
//!
//! @tparam EnvT
//! **[inferred]** Execution environment type. Default is ``cuda::std::execution::env<>``.
//! Supports customization of stream via ``cuda::get_stream``.
//!
//! @param[in] first
//! The beginning of the sequence
//!
//! @param[in] last
//! The end of the sequence
//!
//! @param[in] op
//! Function object to apply to each element in the range
//!
//! @param[in] env
//! @rst
//! **[optional]** Execution environment. Default is ``cuda::std::execution::env{}``.
//! @endrst
template <class RandomAccessIteratorT,
class OpT,
class EnvT = ::cuda::std::execution::env<>,
::cuda::std::enable_if_t<!::cuda::std::is_convertible_v<EnvT, cudaStream_t>, int> = 0>
CUB_RUNTIME_FUNCTION static cudaError_t
ForEach(RandomAccessIteratorT first, RandomAccessIteratorT last, OpT op, EnvT env = {})
{
_CCCL_NVTX_RANGE_SCOPE("cub::DeviceFor::ForEach");
auto stream = ::cuda::__call_or(::cuda::get_stream, ::cuda::stream_ref{cudaStream_t{}}, env);
using offset_t = detail::it_difference_t<RandomAccessIteratorT>;
const auto num_items = static_cast<offset_t>(::cuda::std::distance(first, last));
return ForEachNNoNVTX(first, num_items, op, stream.get());
}
// We keep this overload around to support types that are convertible to `cudaStream_t` but not copyable
template <class RandomAccessIteratorT, class OpT>
CUB_RUNTIME_FUNCTION static cudaError_t
ForEach(RandomAccessIteratorT first, RandomAccessIteratorT last, OpT op, cudaStream_t stream)
{
return ForEach(first, last, op, ::cuda::stream_ref{stream});
}
private:
// Internal version without NVTX range
template <class RandomAccessIteratorT, class NumItemsT, class OpT>
CUB_RUNTIME_FUNCTION static cudaError_t
ForEachCopyNNoNVTX(RandomAccessIteratorT first, NumItemsT num_items, OpT op, cudaStream_t stream = {})
{
using offset_t = NumItemsT;
constexpr bool use_vectorization = THRUST_NS_QUALIFIER::is_contiguous_iterator_v<RandomAccessIteratorT>;
return for_each_n<use_vectorization, RandomAccessIteratorT, offset_t, OpT>(first, num_items, op, stream);
}
public:
//! @rst
//! Overview
//! +++++++++++++++++++++++++++++++++++++++++++++
//!
//! Applies the function object ``op`` to each element in the range ``[first, first + num_items)``.
//! Unlike the ``ForEachN`` algorithm, ``ForEachCopyN`` is allowed to invoke ``op`` on copies of the elements.
//! This relaxation allows ``ForEachCopyN`` to vectorize loads.
//!
//! .. versionadded:: 2.4.0
//! First appears in CUDA Toolkit 12.5.
//!
//! This is an environment-based API that allows customization of:
//!
//! - Stream: Query via ``cuda::get_stream``
//!
//! - Allowed to invoke ``op`` on copies of the elements
//! - The return value of ``op``, if any, is ignored.
//!
//! A Simple Example
//! +++++++++++++++++++++++++++++++++++++++++++++
//!
//! The following code snippet demonstrates how to use `ForEachCopyN` to count odd elements in a device vector.
//!
//! .. literalinclude:: ../../../cub/test/catch2_test_device_for_api.cu
//! :language: c++
//! :dedent:
//! :start-after: example-begin bulk-odd-count-t
//! :end-before: example-end bulk-odd-count-t
//!
//! .. literalinclude:: ../../../cub/test/catch2_test_device_for_api.cu
//! :language: c++
//! :dedent:
//! :start-after: example-begin for-each-copy-n-wo-temp-storage
//! :end-before: example-end for-each-copy-n-wo-temp-storage
//!
//! Environment Example
//! +++++++++++++++++++++++++++++++++++++++++++++
//!
//! The following code snippet demonstrates how to use `ForEachCopyN` with a custom stream via an environment.
//!
//! .. literalinclude:: ../../../cub/test/catch2_test_device_for_env_api.cu
//! :language: c++
//! :dedent:
//! :start-after: example-begin odd-count-env-t
//! :end-before: example-end odd-count-env-t
//!
//! .. literalinclude:: ../../../cub/test/catch2_test_device_for_env_api.cu
//! :language: c++
//! :dedent:
//! :start-after: example-begin for-each-copy-n-env
//! :end-before: example-end for-each-copy-n-env
//!
//! @endrst
//!
//! @tparam RandomAccessIteratorT
//! is a model of Random Access Iterator whose value type is convertible to `op`'s argument type.
//!
//! @tparam NumItemsT
//! is an integral type representing the number of elements to iterate over
//!
//! @tparam OpT
//! is a model of [Unary Function](https://en.cppreference.com/w/cpp/utility/functional/unary_function)
//!
//! @tparam EnvT
//! **[inferred]** Execution environment type. Default is ``cuda::std::execution::env<>``.
//! Supports customization of stream via ``cuda::get_stream``.
//!
//! @param[in] first
//! The beginning of the sequence
//!
//! @param[in] num_items
//! Number of elements to iterate over
//!
//! @param[in] op
//! Function object to apply to a copy of each element in the range
//!
//! @param[in] env
//! @rst
//! **[optional]** Execution environment. Default is ``cuda::std::execution::env{}``.
//! @endrst
template <class RandomAccessIteratorT,
class NumItemsT,
class OpT,
class EnvT = ::cuda::std::execution::env<>,
::cuda::std::enable_if_t<!::cuda::std::is_convertible_v<EnvT, cudaStream_t>, int> = 0>
CUB_RUNTIME_FUNCTION static cudaError_t
ForEachCopyN(RandomAccessIteratorT first, NumItemsT num_items, OpT op, EnvT env = {})
{
_CCCL_NVTX_RANGE_SCOPE("cub::DeviceFor::ForEachCopyN");
auto stream = ::cuda::__call_or(::cuda::get_stream, ::cuda::stream_ref{cudaStream_t{}}, env);
return ForEachCopyNNoNVTX(first, num_items, op, stream.get());
}
// We keep this overload around to support types that are convertible to `cudaStream_t` but not copyable
template <class RandomAccessIteratorT, class NumItemsT, class OpT>
CUB_RUNTIME_FUNCTION static cudaError_t
ForEachCopyN(RandomAccessIteratorT first, NumItemsT num_items, OpT op, cudaStream_t stream)
{
return ForEachCopyN(first, num_items, op, ::cuda::stream_ref{stream});
}
//! @rst
//! Overview
//! +++++++++++++++++++++++++++++++++++++++++++++
//!
//! Applies the function object ``op`` to each element in the range ``[first, last)``.
//! Unlike the ``ForEach`` algorithm, ``ForEachCopy`` is allowed to invoke ``op`` on copies of the elements.
//! This relaxation allows ``ForEachCopy`` to vectorize loads.
//!
//! .. versionadded:: 2.4.0
//! First appears in CUDA Toolkit 12.5.
//!
//! This is an environment-based API that allows customization of:
//!
//! - Stream: Query via ``cuda::get_stream``
//!
//! - Allowed to invoke ``op`` on copies of the elements
//! - The return value of ``op``, if any, is ignored.
//!
//! A Simple Example
//! +++++++++++++++++++++++++++++++++++++++++++++
//!
//! The following code snippet demonstrates how to use `ForEachCopy` to count odd elements in a device vector.
//!
//! .. literalinclude:: ../../../cub/test/catch2_test_device_for_api.cu
//! :language: c++
//! :dedent:
//! :start-after: example-begin bulk-odd-count-t
//! :end-before: example-end bulk-odd-count-t
//!
//! .. literalinclude:: ../../../cub/test/catch2_test_device_for_api.cu
//! :language: c++
//! :dedent:
//! :start-after: example-begin for-each-copy-wo-temp-storage
//! :end-before: example-end for-each-copy-wo-temp-storage
//!
//! Environment Example
//! +++++++++++++++++++++++++++++++++++++++++++++
//!
//! The following code snippet demonstrates how to use `ForEachCopy` with a custom stream via an environment.
//!
//! .. literalinclude:: ../../../cub/test/catch2_test_device_for_env_api.cu
//! :language: c++
//! :dedent:
//! :start-after: example-begin odd-count-env-t
//! :end-before: example-end odd-count-env-t
//!
//! .. literalinclude:: ../../../cub/test/catch2_test_device_for_env_api.cu
//! :language: c++
//! :dedent:
//! :start-after: example-begin for-each-copy-env
//! :end-before: example-end for-each-copy-env
//!
//! @endrst
//!
//! @tparam RandomAccessIteratorT
//! is a model of Random Access Iterator whose value type is convertible to `op`'s argument type.
//!
//! @tparam OpT
//! is a model of [Unary Function](https://en.cppreference.com/w/cpp/utility/functional/unary_function)
//!
//! @tparam EnvT
//! **[inferred]** Execution environment type. Default is ``cuda::std::execution::env<>``.
//! Supports customization of stream via ``cuda::get_stream``.
//!
//! @param[in] first