32 explicit operator bool() const noexcept {
return flag; }
42#if defined(AMREX_USE_GPU)
54template <
typename T,
bool SINGLE_WORD>
struct BlockStatus {};
57struct BlockStatus<T, true>
63 void operator=(Data<U>
const&) =
delete;
64 void operator=(Data<U> &&) =
delete;
69 void write (
char a_status, T a_value) {
70#if defined(AMREX_USE_CUDA)
71 volatile uint64_t tmp;
72 reinterpret_cast<STVA<T> volatile&
>(tmp).status = a_status;
73 reinterpret_cast<STVA<T> volatile&
>(tmp).value = a_value;
74 reinterpret_cast<uint64_t&
>(d.s) = tmp;
77 tmp.s = {a_status, a_value};
78 static_assert(
sizeof(
unsigned long long) ==
sizeof(uint64_t),
79 "HIP/SYCL: unsigned long long must be 64 bits");
80 Gpu::Atomic::Exch(
reinterpret_cast<unsigned long long*
>(&d),
81 reinterpret_cast<unsigned long long&
>(tmp));
86 T get_aggregate()
const {
return d.s.value; }
89 STVA<T> read ()
volatile {
90#if defined(AMREX_USE_CUDA)
91 volatile uint64_t tmp =
reinterpret_cast<uint64_t volatile&
>(d);
92 return {
reinterpret_cast<STVA<T> volatile&
>(tmp).status,
93 reinterpret_cast<STVA<T> volatile&
>(tmp).value };
95 static_assert(
sizeof(
unsigned long long) ==
sizeof(uint64_t),
96 "HIP/SYCL: unsigned long long must be 64 bits");
97 unsigned long long tmp = Gpu::Atomic::Add
98 (
reinterpret_cast<unsigned long long*
>(
const_cast<Data<T>*
>(&d)), 0ull);
99 return (*
reinterpret_cast<Data<T>*
>(&tmp)).s;
104 void set_status (
char a_status) { d.s.status = a_status; }
107 STVA<T> wait ()
volatile {
110#if defined(AMREX_USE_SYCL)
111 sycl::atomic_fence(sycl::memory_order::acq_rel, sycl::memory_scope::work_group);
113 __threadfence_block();
116 }
while (r.status ==
'x');
122struct BlockStatus<T, false>
129 void write (
char a_status, T a_value) {
130 if (a_status ==
'a') {
135#if defined(AMREX_USE_SYCL)
136 sycl::atomic_fence(sycl::memory_order::acq_rel, sycl::memory_scope::device);
144 T get_aggregate()
const {
return aggregate; }
147 STVA<T> read ()
volatile {
148#if defined(AMREX_USE_SYCL)
149 constexpr auto mo = sycl::memory_order::relaxed;
150 constexpr auto ms = sycl::memory_scope::device;
151 constexpr auto as = sycl::access::address_space::global_space;
155 }
else if (status ==
'a') {
156#if defined(AMREX_USE_SYCL)
157 sycl::atomic_ref<T,mo,ms,as> ar{
const_cast<T&
>(aggregate)};
158 return {
'a', ar.load()};
160 return {
'a', aggregate};
163#if defined(AMREX_USE_SYCL)
164 sycl::atomic_ref<T,mo,ms,as> ar{
const_cast<T&
>(
inclusive)};
165 return {
'p', ar.load()};
173 void set_status (
char a_status) { status = a_status; }
176 STVA<T> wait ()
volatile {
180#if defined(AMREX_USE_SYCL)
181 sycl::atomic_fence(sycl::memory_order::acq_rel, sycl::memory_scope::device);
185 }
while (r.status ==
'x');
193#if defined(AMREX_USE_SYCL)
195#ifndef AMREX_SYCL_NO_MULTIPASS_SCAN
196template <
typename T, std::
integral N,
typename FIN,
typename FOUT,
typename TYPE>
197T PrefixSum_mp (N n, FIN
const& fin, FOUT
const& fout, TYPE, RetSum a_ret_sum)
199 if (n <= 0) {
return 0; }
200 constexpr int nwarps_per_block = 8;
202 constexpr int nchunks = 12;
203 constexpr int nelms_per_block = nthreads * nchunks;
205 std::numeric_limits<int>::max())*nelms_per_block);
206 int nblocks = (
static_cast<Long>(n) + nelms_per_block - 1) / nelms_per_block;
210 std::size_t nbytes_blockresult =
Arena::align(
sizeof(T)*n);
211 std::size_t nbytes_blocksum =
Arena::align(
sizeof(T)*nblocks);
216 T* blockresult_p = (T*)dp;
217 T* blocksum_p = (T*)(dp + nbytes_blockresult);
218 T* totalsum_p = (T*)(dp + nbytes_blockresult + nbytes_blocksum);
220 amrex::launch<nthreads>(nblocks, sm, stream,
223 sycl::sub_group
const& sg = gh.item->get_sub_group();
224 int lane = sg.get_local_id()[0];
225 int warp = sg.get_group_id()[0];
226 int nwarps = sg.get_group_range()[0];
228 int threadIdxx = gh.item->get_local_id(0);
229 int blockIdxx = gh.item->get_group_linear_id();
230 int blockDimx = gh.item->get_local_range(0);
232 T* shared = (T*)(gh.local);
236 N ibegin =
static_cast<N
>(nelms_per_block) * blockIdxx;
237 N iend =
amrex::min(
static_cast<N
>(ibegin+nelms_per_block), n);
242 T sum_prev_chunk = 0;
243 for (
int ichunk = 0; ichunk < nchunks; ++ichunk) {
244 N
offset = ibegin + ichunk*blockDimx;
245 if (
offset >= iend) {
break; }
252 T s = sycl::shift_group_right(sg,
x, i);
253 if (lane >= i) {
x += s; }
263 gh.item->barrier(sycl::access::fence_space::local_space);
268 T
y = (lane < nwarps) ? shared[lane] : 0;
270 T s = sycl::shift_group_right(sg,
y, i);
271 if (lane >= i) {
y += s; }
274 if (lane < nwarps) { shared2[lane] =
y; }
277 gh.item->barrier(sycl::access::fence_space::local_space);
284 T sum_prev_warp = (warp == 0) ? 0 : shared2[warp-1];
285 T tmp_out = sum_prev_warp + sum_prev_chunk +
286 (std::is_same_v<std::decay_t<TYPE>,Type::Inclusive> ?
x :
x-x0);
287 sum_prev_chunk += shared2[nwarps-1];
290 blockresult_p[
offset] = tmp_out;
295 if (threadIdxx == 0) {
296 blocksum_p[blockIdxx] = sum_prev_chunk;
300 amrex::launch<nthreads>(1, sm, stream,
303 sycl::sub_group
const& sg = gh.item->get_sub_group();
304 int lane = sg.get_local_id()[0];
305 int warp = sg.get_group_id()[0];
306 int nwarps = sg.get_group_range()[0];
308 int threadIdxx = gh.item->get_local_id(0);
309 int blockDimx = gh.item->get_local_range(0);
311 T* shared = (T*)(gh.local);
314 T sum_prev_chunk = 0;
319 T s = sycl::shift_group_right(sg,
x, i);
320 if (lane >= i) {
x += s; }
330 gh.item->barrier(sycl::access::fence_space::local_space);
335 T
y = (lane < nwarps) ? shared[lane] : 0;
337 T s = sycl::shift_group_right(sg,
y, i);
338 if (lane >= i) {
y += s; }
341 if (lane < nwarps) { shared2[lane] =
y; }
344 gh.item->barrier(sycl::access::fence_space::local_space);
351 T sum_prev_warp = (warp == 0) ? 0 : shared2[warp-1];
352 T tmp_out = sum_prev_warp + sum_prev_chunk +
x;
353 sum_prev_chunk += shared2[nwarps-1];
356 blocksum_p[
offset] = tmp_out;
361 if (threadIdxx == 0) {
362 *totalsum_p = sum_prev_chunk;
366 amrex::launch<nthreads>(nblocks, 0, stream,
369 int threadIdxx = gh.item->get_local_id(0);
370 int blockIdxx = gh.item->get_group_linear_id();
371 int blockDimx = gh.item->get_local_range(0);
374 N ibegin =
static_cast<N
>(nelms_per_block) * blockIdxx;
375 N iend =
amrex::min(
static_cast<N
>(ibegin+nelms_per_block), n);
376 T prev_sum = (blockIdxx == 0) ? 0 : blocksum_p[blockIdxx-1];
398template <
typename T, std::
integral N,
typename FIN,
typename FOUT,
typename TYPE>
399requires (std::same_as<std::decay_t<TYPE>,Type::Inclusive> ||
400 std::same_as<std::decay_t<TYPE>,Type::Exclusive>)
401T
PrefixSum (N n, FIN && fin, FOUT && fout, TYPE type, RetSum a_ret_sum =
retSum)
403 if (n <= 0) {
return 0; }
404 constexpr int nwarps_per_block = 8;
406 constexpr int nchunks = 12;
407 constexpr int nelms_per_block = nthreads * nchunks;
409 std::numeric_limits<int>::max())*nelms_per_block);
410 int nblocks = (
static_cast<Long>(n) + nelms_per_block - 1) / nelms_per_block;
412#ifndef AMREX_SYCL_NO_MULTIPASS_SCAN
414 return PrefixSum_mp<T>(n, std::forward<FIN>(fin), std::forward<FOUT>(fout), type, a_ret_sum);
421 using BlockStatusT = std::conditional_t<
sizeof(detail::STVA<T>) <= 8,
422 detail::BlockStatus<T,true>, detail::BlockStatus<T,false> >;
424 std::size_t nbytes_blockstatus =
Arena::align(
sizeof(BlockStatusT)*nblocks);
425 std::size_t nbytes_blockid =
Arena::align(
sizeof(
unsigned int));
431 unsigned int*
AMREX_RESTRICT virtual_block_id_p = (
unsigned int*)(dp + nbytes_blockstatus);
432 T*
AMREX_RESTRICT totalsum_p = (T*)(dp + nbytes_blockstatus + nbytes_blockid);
435 BlockStatusT& block_status = block_status_p[i];
436 block_status.set_status(
'x');
438 *virtual_block_id_p = 0;
443 amrex::launch<nthreads>(nblocks, sm, stream,
446 sycl::sub_group
const& sg = gh.item->get_sub_group();
447 int lane = sg.get_local_id()[0];
448 int warp = sg.get_group_id()[0];
449 int nwarps = sg.get_group_range()[0];
451 int threadIdxx = gh.item->get_local_id(0);
452 int blockDimx = gh.item->get_local_range(0);
453 int gridDimx = gh.item->get_group_range(0);
455 T* shared = (T*)(gh.local);
461 int virtual_block_id = 0;
463 int& virtual_block_id_shared = *((
int*)(shared2+nwarps));
464 if (threadIdxx == 0) {
466 virtual_block_id_shared = bid;
468 gh.item->barrier(sycl::access::fence_space::local_space);
469 virtual_block_id = virtual_block_id_shared;
473 N ibegin =
static_cast<N
>(nelms_per_block) * virtual_block_id;
474 N iend =
amrex::min(
static_cast<N
>(ibegin+nelms_per_block), n);
475 BlockStatusT& block_status = block_status_p[virtual_block_id];
486 T sum_prev_chunk = 0;
488 for (
int ichunk = 0; ichunk < nchunks; ++ichunk) {
489 N
offset = ibegin + ichunk*blockDimx;
490 if (
offset >= iend) {
break; }
494 if (std::is_same_v<std::decay_t<TYPE>,Type::Exclusive> &&
offset == n-1) {
500 T s = sycl::shift_group_right(sg,
x, i);
501 if (lane >= i) {
x += s; }
511 gh.item->barrier(sycl::access::fence_space::local_space);
516 T
y = (lane < nwarps) ? shared[lane] : 0;
518 T s = sycl::shift_group_right(sg,
y, i);
519 if (lane >= i) {
y += s; }
522 if (lane < nwarps) { shared2[lane] =
y; }
525 gh.item->barrier(sycl::access::fence_space::local_space);
532 T sum_prev_warp = (warp == 0) ? 0 : shared2[warp-1];
533 tmp_out[ichunk] = sum_prev_warp + sum_prev_chunk +
534 (std::is_same_v<std::decay_t<TYPE>,Type::Inclusive> ?
x :
x-x0);
535 sum_prev_chunk += shared2[nwarps-1];
539 if (threadIdxx == 0 && gridDimx > 1) {
540 block_status.write((virtual_block_id == 0) ?
'p' :
'a',
544 if (virtual_block_id == 0) {
545 for (
int ichunk = 0; ichunk < nchunks; ++ichunk) {
546 N
offset = ibegin + ichunk*blockDimx + threadIdxx;
547 if (
offset >= iend) {
break; }
548 fout(
offset, tmp_out[ichunk]);
550 *totalsum_p += tmp_out[ichunk];
553 }
else if (virtual_block_id > 0) {
556 T exclusive_prefix = 0;
557 BlockStatusT
volatile* pbs = block_status_p;
560 int iblock = iblock0-lane;
561 detail::STVA<T> stva{
'p', 0};
563 stva = pbs[iblock].wait();
569 unsigned status_bf = (stva.status ==
'p') ? (0x1u << lane) : 0;
571 status_bf |= sycl::permute_group_by_xor(sg, status_bf, i);
574 bool stop_lookback = status_bf & 0x1u;
575 if (stop_lookback ==
false) {
576 if (status_bf != 0) {
578 if (lane > 0) {
x = 0; }
579 unsigned int bit_mask = 0x1u;
582 if (i == lane) {
x =
y; }
583 if (status_bf & bit_mask) {
584 stop_lookback =
true;
591 x += sycl::shift_group_left(sg,
x,i);
595 if (lane == 0) { exclusive_prefix +=
x; }
596 if (stop_lookback) {
break; }
600 block_status.write(
'p', block_status.get_aggregate() + exclusive_prefix);
601 shared[0] = exclusive_prefix;
605 gh.item->barrier(sycl::access::fence_space::local_space);
607 T exclusive_prefix = shared[0];
609 for (
int ichunk = 0; ichunk < nchunks; ++ichunk) {
610 N
offset = ibegin + ichunk*blockDimx + threadIdxx;
611 if (
offset >= iend) {
break; }
612 T t = tmp_out[ichunk] + exclusive_prefix;
638#elif defined(AMREX_USE_HIP)
640template <
typename T, std::
integral N,
typename FIN,
typename FOUT,
typename TYPE>
641requires (std::same_as<std::decay_t<TYPE>,Type::Inclusive> ||
642 std::same_as<std::decay_t<TYPE>,Type::Exclusive>)
643T
PrefixSum (N n, FIN
const& fin, FOUT
const& fout, TYPE, RetSum a_ret_sum =
retSum)
645 if (n <= 0) {
return 0; }
646 constexpr int nwarps_per_block = 4;
648 constexpr int nelms_per_thread =
sizeof(T) >= 8 ? 8 : 16;
649 constexpr int nelms_per_block = nthreads * nelms_per_thread;
651 std::numeric_limits<int>::max())*nelms_per_block);
652 int nblocks = (n + nelms_per_block - 1) / nelms_per_block;
656 using ScanTileState = rocprim::detail::lookback_scan_state<T>;
657 using OrderedBlockId = rocprim::detail::ordered_block_id<unsigned int>;
659#if (defined(HIP_VERSION_MAJOR) && (HIP_VERSION_MAJOR < 6)) || \
660 (defined(HIP_VERSION_MAJOR) && (HIP_VERSION_MAJOR == 6) && \
661 defined(HIP_VERSION_MINOR) && (HIP_VERSION_MINOR == 0))
663 std::size_t nbytes_tile_state = rocprim::detail::align_size
664 (ScanTileState::get_storage_size(nblocks));
665 std::size_t nbytes_block_id = OrderedBlockId::get_storage_size();
667 auto dp = (
char*)(
The_Arena()->
alloc(nbytes_tile_state+nbytes_block_id));
669 ScanTileState tile_state = ScanTileState::create(dp, nblocks);
673 std::size_t nbytes_tile_state;
674 AMREX_HIP_SAFE_CALL(ScanTileState::get_storage_size(nblocks, stream, nbytes_tile_state));
675 nbytes_tile_state = rocprim::detail::align_size(nbytes_tile_state);
677 std::size_t nbytes_block_id = OrderedBlockId::get_storage_size();
679 auto dp = (
char*)(
The_Arena()->
alloc(nbytes_tile_state+nbytes_block_id));
681 ScanTileState tile_state;
682 AMREX_HIP_SAFE_CALL(ScanTileState::create(tile_state, dp, nblocks, stream));
686 auto ordered_block_id = OrderedBlockId::create
687 (
reinterpret_cast<OrderedBlockId::id_type*
>(dp + nbytes_tile_state));
690 amrex::launch<nthreads>((nblocks+nthreads-1)/nthreads, 0, stream, [=]
AMREX_GPU_DEVICE ()
692 auto& scan_tile_state =
const_cast<ScanTileState&
>(tile_state);
693 auto& scan_bid =
const_cast<OrderedBlockId&
>(ordered_block_id);
694 const unsigned int gid = blockIdx.x*nthreads + threadIdx.x;
695 if (gid == 0) { scan_bid.reset(); }
696 scan_tile_state.initialize_prefix(gid, nblocks);
701 amrex::launch_global<nthreads> <<<nblocks, nthreads, sm, stream>>> (
704 using BlockLoad = rocprim::block_load<T, nthreads, nelms_per_thread,
705 rocprim::block_load_method::block_load_transpose>;
706 using BlockScan = rocprim::block_scan<T, nthreads,
707 rocprim::block_scan_algorithm::using_warp_scan>;
708 using BlockExchange = rocprim::block_exchange<T, nthreads, nelms_per_thread>;
709 using LookbackScanPrefixOp = rocprim::detail::lookback_scan_prefix_op
710 <T, rocprim::plus<T>, ScanTileState>;
712 __shared__
struct TempStorage {
713 typename OrderedBlockId::storage_type ordered_bid;
715 typename BlockLoad::storage_type load;
716 typename BlockExchange::storage_type exchange;
717 typename BlockScan::storage_type scan;
722 auto& scan_tile_state =
const_cast<ScanTileState&
>(tile_state);
723 auto& scan_bid =
const_cast<OrderedBlockId&
>(ordered_block_id);
725 auto const virtual_block_id = scan_bid.get(threadIdx.x, temp_storage.ordered_bid);
728 N ibegin =
static_cast<N
>(nelms_per_block) * virtual_block_id;
729 N iend =
amrex::min(
static_cast<N
>(ibegin+nelms_per_block), n);
731 auto input_begin = rocprim::make_transform_iterator(
732 rocprim::make_counting_iterator(N(0)),
733 [&] (N i) -> T {
return fin(i+ibegin); });
735 T data[nelms_per_thread];
736 if (
static_cast<int>(iend-ibegin) == nelms_per_block) {
737 BlockLoad().load(input_begin, data, temp_storage.load);
740 BlockLoad().load(input_begin, data, iend-ibegin, 0, temp_storage.load);
745 constexpr bool is_exclusive = std::is_same_v<std::decay_t<TYPE>,Type::Exclusive>;
747 if (virtual_block_id == 0) {
750 BlockScan().exclusive_scan(data, data, T{0}, block_agg, temp_storage.scan);
752 BlockScan().inclusive_scan(data, data, block_agg, temp_storage.scan);
754 if (threadIdx.x == 0) {
756 scan_tile_state.set_complete(0, block_agg);
757 }
else if (nblocks == 1 && totalsum_p) {
758 *totalsum_p = block_agg;
762 T last = data[nelms_per_thread-1];
764 LookbackScanPrefixOp prefix_op(virtual_block_id, rocprim::plus<T>(), scan_tile_state);
766 BlockScan().exclusive_scan(data, data, temp_storage.scan, prefix_op,
769 BlockScan().inclusive_scan(data, data, temp_storage.scan, prefix_op,
773 if (iend == n && threadIdx.x == nthreads-1) {
774 T tsum = data[nelms_per_thread-1];
783 BlockExchange().blocked_to_striped(data, data, temp_storage.exchange);
785 for (
int i = 0; i < nelms_per_thread; ++i) {
786 N
offset = ibegin + i*nthreads + threadIdx.x;
800 T ret = (a_ret_sum) ? *totalsum_p : T(0);
806#elif defined(AMREX_USE_CUDA)
808template <
typename T, std::
integral N,
typename FIN,
typename FOUT,
typename TYPE>
809requires (std::same_as<std::decay_t<TYPE>,Type::Inclusive> ||
810 std::same_as<std::decay_t<TYPE>,Type::Exclusive>)
813 if (n <= 0) {
return 0; }
814 constexpr int nwarps_per_block = 8;
816 constexpr int nelms_per_thread =
sizeof(T) >= 8 ? 4 : 8;
817 constexpr int nelms_per_block = nthreads * nelms_per_thread;
819 std::numeric_limits<int>::max())*nelms_per_block);
820 int nblocks = (n + nelms_per_block - 1) / nelms_per_block;
824 using ScanTileState = cub::ScanTileState<T>;
825 std::size_t tile_state_size = 0;
826 ScanTileState::AllocationSize(nblocks, tile_state_size);
828 std::size_t nbytes_tile_state =
Arena::align(tile_state_size);
829 auto tile_state_p = (
char*)(
The_Arena()->
alloc(nbytes_tile_state));
831 ScanTileState tile_state;
832 tile_state.Init(nblocks, tile_state_p, tile_state_size);
836 amrex::launch<nthreads>((nblocks+nthreads-1)/nthreads, 0, stream, [=]
AMREX_GPU_DEVICE ()
838 const_cast<ScanTileState&
>(tile_state).InitializeStatus(nblocks);
844 amrex::launch_global<nthreads> <<<nblocks, nthreads, sm, stream>>> (
847 using BlockLoad = cub::BlockLoad<T, nthreads, nelms_per_thread, cub::BLOCK_LOAD_WARP_TRANSPOSE>;
848 using BlockScan = cub::BlockScan<T, nthreads, cub::BLOCK_SCAN_WARP_SCANS>;
849 using BlockExchange = cub::BlockExchange<T, nthreads, nelms_per_thread>;
851#ifdef AMREX_CUDA_CCCL_VER_GE_2_8
852 using Sum = cuda::std::plus<T>;
854 using Sum = cub::Sum;
856 using TilePrefixCallbackOp = cub::TilePrefixCallbackOp<T, Sum, ScanTileState>;
858 __shared__
union TempStorage
860 typename BlockLoad::TempStorage load;
861 typename BlockExchange::TempStorage exchange;
863 typename BlockScan::TempStorage scan;
864 typename TilePrefixCallbackOp::TempStorage prefix;
869 auto& scan_tile_state =
const_cast<ScanTileState&
>(tile_state);
871 int virtual_block_id = blockIdx.x;
874 N ibegin =
static_cast<N
>(nelms_per_block) * virtual_block_id;
875 N iend =
amrex::min(
static_cast<N
>(ibegin+nelms_per_block), n);
877 auto input_lambda = [&] (N i) -> T {
return fin(i+ibegin); };
878#ifdef AMREX_CUDA_CCCL_VER_GE_2_8
879 thrust::transform_iterator<
decltype(input_lambda),thrust::counting_iterator<N> >
880 input_begin(thrust::counting_iterator<N>(0), input_lambda);
882 cub::TransformInputIterator<T,
decltype(input_lambda),cub::CountingInputIterator<N> >
883 input_begin(cub::CountingInputIterator<N>(0), input_lambda);
886 T data[nelms_per_thread];
887 if (
static_cast<int>(iend-ibegin) == nelms_per_block) {
888 BlockLoad(temp_storage.load).Load(input_begin, data);
890 BlockLoad(temp_storage.load).Load(input_begin, data, iend-ibegin, 0);
895 constexpr bool is_exclusive = std::is_same_v<std::decay_t<TYPE>,
Type::Exclusive>;
897 if (virtual_block_id == 0) {
900 BlockScan(temp_storage.scan_storeage.scan).ExclusiveSum(data, data, block_agg);
902 BlockScan(temp_storage.scan_storeage.scan).InclusiveSum(data, data, block_agg);
904 if (threadIdx.x == 0) {
906 scan_tile_state.SetInclusive(0, block_agg);
907 }
else if (nblocks == 1 && totalsum_p) {
908 *totalsum_p = block_agg;
912 T last = data[nelms_per_thread-1];
914 TilePrefixCallbackOp prefix_op(scan_tile_state, temp_storage.scan_storeage.prefix,
915 Sum{}, virtual_block_id);
917 BlockScan(temp_storage.scan_storeage.scan).ExclusiveSum(data, data, prefix_op);
919 BlockScan(temp_storage.scan_storeage.scan).InclusiveSum(data, data, prefix_op);
922 if (iend == n && threadIdx.x == nthreads-1) {
923 T tsum = data[nelms_per_thread-1];
932 BlockExchange(temp_storage.exchange).BlockedToStriped(data);
934 for (
int i = 0; i < nelms_per_thread; ++i) {
935 N
offset = ibegin + i*nthreads + threadIdx.x;
949 T ret = (a_ret_sum) ? *totalsum_p : T(0);
968template <std::
integral N,
typename T >
971 if (n <= 0) {
return 0; }
972#if defined(AMREX_USE_CUDA)
973 void* d_temp =
nullptr;
974 std::size_t temp_bytes = 0;
988#elif defined(AMREX_USE_HIP)
989 void* d_temp =
nullptr;
990 std::size_t temp_bytes = 0;
1004#elif defined(AMREX_USE_SYCL) && defined(AMREX_USE_ONEDPL)
1005 auto policy = oneapi::dpl::execution::make_device_policy(Gpu::Device::streamQueue());
1006 std::inclusive_scan(policy, in, in+n, out, std::plus<T>(), T(0));
1015 if (
static_cast<Long>(n) <=
static_cast<Long>(std::numeric_limits<int>::max())) {
1016 return PrefixSum<T>(
static_cast<int>(n),
1021 return PrefixSum<T>(n,
1040template <std::
integral N,
typename T >
1043 if (n <= 0) {
return 0; }
1044#if defined(AMREX_USE_CUDA)
1049 void* d_temp =
nullptr;
1050 std::size_t temp_bytes = 0;
1063 return in_last+out_last;
1064#elif defined(AMREX_USE_HIP)
1069 void* d_temp =
nullptr;
1070 std::size_t temp_bytes = 0;
1083 return in_last+out_last;
1084#elif defined(AMREX_USE_SYCL) && defined(AMREX_USE_ONEDPL)
1089 auto policy = oneapi::dpl::execution::make_device_policy(Gpu::Device::streamQueue());
1090 std::exclusive_scan(policy, in, in+n, out, T(0), std::plus<T>());
1097 return in_last+out_last;
1099 if (
static_cast<Long>(n) <=
static_cast<Long>(std::numeric_limits<int>::max())) {
1100 return PrefixSum<T>(
static_cast<int>(n),
1105 return PrefixSum<T>(n,
1115template <
typename T, std::
integral N,
typename FIN,
typename FOUT,
typename TYPE>
1116requires (std::same_as<std::decay_t<TYPE>,Type::Inclusive> ||
1117 std::same_as<std::decay_t<TYPE>,Type::Exclusive>)
1118T
PrefixSum (N n, FIN
const& fin, FOUT
const& fout, TYPE, RetSum =
retSum)
1120 if (n <= 0) {
return 0; }
1122 for (N i = 0; i < n; ++i) {
1135template <std::
integral N,
typename T >
1138 std::inclusive_scan(in, in+n, out);
1139 return (n > 0) ? out[n-1] : T(0);
1143template <std::
integral N,
typename T >
1146 if (n <= 0) {
return 0; }
1148 auto in_last = in[n-1];
1149 std::exclusive_scan(in, in+n, out, T(0));
1150 return in_last + out[n-1];