3#include <AMReX_Config.H>
18namespace Reduce::detail {
22 template <std::
size_t I,
typename T,
typename P>
24 void for_each_parallel (T& d, T
const& s, Gpu::Handler
const& h)
26 P().parallel_update(amrex::get<I>(d), amrex::get<I>(s), h);
29 template <std::size_t I,
typename T,
typename P,
typename P1,
typename... Ps>
31 void for_each_parallel (T& d, T
const& s, Gpu::Handler
const& h)
33 P().parallel_update(amrex::get<I>(d), amrex::get<I>(s), h);
34 for_each_parallel<I+1,T,P1,Ps...>(d, s, h);
37 template <std::
size_t I,
typename T,
typename P>
39 void for_each_parallel (T& d, T
const& s)
41 P().parallel_update(amrex::get<I>(d), amrex::get<I>(s));
44 template <std::size_t I,
typename T,
typename P,
typename P1,
typename... Ps>
46 void for_each_parallel (T& d, T
const& s)
48 P().parallel_update(amrex::get<I>(d), amrex::get<I>(s));
49 for_each_parallel<I+1,T,P1,Ps...>(d, s);
54 template <std::
size_t I,
typename T,
typename P>
56 void for_each_local (T& d, T
const& s)
58 P().local_update(amrex::get<I>(d), amrex::get<I>(s));
61 template <std::size_t I,
typename T,
typename P,
typename P1,
typename... Ps>
63 void for_each_local (T& d, T
const& s)
65 P().local_update(amrex::get<I>(d), amrex::get<I>(s));
66 for_each_local<I+1,T,P1,Ps...>(d, s);
69 template <std::
size_t I,
typename T,
typename P>
71 constexpr void for_each_init (T& t)
73 P().init(amrex::get<I>(t));
76 template <std::size_t I,
typename T,
typename P,
typename P1,
typename... Ps>
78 constexpr void for_each_init (T& t)
80 P().init(amrex::get<I>(t));
81 for_each_init<I+1,T,P1,Ps...>(t);
96 if (h.threadIdx() == 0) { d += r; }
99 template <
typename T,
int MT=AMREX_GPU_MAX_THREADS>
102 T r = Gpu::blockReduceSum<MT>(s);
103 if (threadIdx.x == 0) { d += r; }
108 template <
typename T>
112 template <
typename T>
113 constexpr void init (T& t)
const noexcept { t = 0; }
121 template <
typename T>
125 if (h.threadIdx() == 0) { d =
amrex::min(d,r); }
128 template <
typename T,
int MT=AMREX_GPU_MAX_THREADS>
131 T r = Gpu::blockReduceMin<MT>(s);
132 if (threadIdx.x == 0) { d =
amrex::min(d,r); }
137 template <
typename T>
141 template <
typename T>
142 constexpr std::enable_if_t<std::numeric_limits<T>::is_specialized>
143 init (T& t)
const noexcept { t = std::numeric_limits<T>::max(); }
145 template <
typename T>
146 constexpr std::enable_if_t<!std::numeric_limits<T>::is_specialized>
147 init (T& t)
const noexcept { t = T::max(); }
155 template <
typename T>
159 if (h.threadIdx() == 0) { d =
amrex::max(d,r); }
162 template <
typename T,
int MT=AMREX_GPU_MAX_THREADS>
165 T r = Gpu::blockReduceMax<MT>(s);
166 if (threadIdx.x == 0) { d =
amrex::max(d,r); }
171 template <
typename T>
175 template <
typename T>
176 constexpr std::enable_if_t<std::numeric_limits<T>::is_specialized>
177 init (T& t)
const noexcept { t = std::numeric_limits<T>::lowest(); }
179 template <
typename T>
180 constexpr std::enable_if_t<!std::numeric_limits<T>::is_specialized>
181 init (T& t)
const noexcept { t = T::lowest(); }
189 template <
typename T>
191 std::enable_if_t<std::is_integral_v<T>>
194 if (h.threadIdx() == 0) { d = d && r; }
197 template <
typename T,
int MT=AMREX_GPU_MAX_THREADS>
199 std::enable_if_t<std::is_integral_v<T>>
201 T r = Gpu::blockReduceLogicalAnd<MT>(s);
202 if (threadIdx.x == 0) { d = d && r; }
207 template <
typename T>
209 std::enable_if_t<std::is_integral_v<T>>
212 template <
typename T>
213 constexpr std::enable_if_t<std::is_integral_v<T>>
214 init (T& t)
const noexcept { t =
true; }
222 template <
typename T>
224 std::enable_if_t<std::is_integral_v<T>>
227 if (h.threadIdx() == 0) { d = d || r; }
230 template <
typename T,
int MT=AMREX_GPU_MAX_THREADS>
232 std::enable_if_t<std::is_integral_v<T>>
234 T r = Gpu::blockReduceLogicalOr<MT>(s);
235 if (threadIdx.x == 0) { d = d || r; }
240 template <
typename T>
242 std::enable_if_t<std::is_integral_v<T>>
245 template <
typename T>
246 constexpr std::enable_if_t<std::is_integral_v<T>>
247 init (T& t)
const noexcept { t =
false; }
250template <
typename... Ps>
class ReduceOps;
255template <
typename... Ts>
261 template <
typename... Ps>
269 reduce_op.resetResultReadiness();
270 static_assert(std::is_trivially_copyable<Type>(),
271 "ReduceData::Type must be trivially copyable");
272 static_assert(std::is_trivially_destructible<Type>(),
273 "ReduceData::Type must be trivially destructible");
275 new (m_host_tuple) Type();
294 template <
typename... Ps>
297 return reduce_op.
value(*
this);
319 int m_max_stream_index = 0;
321 Type* m_device_tuple =
nullptr;
327namespace Reduce::detail {
328 template <
typename F>
330 auto call_f (
F const& f,
int i,
int j,
int k,
IndexType)
331 noexcept ->
decltype(f(0,0,0))
336 template <
typename F>
338 auto call_f (F
const& f,
int i,
int j,
int k, IndexType t)
339 noexcept ->
decltype(f(Box()))
348 struct iterate_box {};
349 struct iterate_box_comp {};
352 template <
typename I,
typename F,
typename T,
typename... Ps,
353 std::enable_if_t<std::is_same<iterate_box,I>::value,
int> = 0>
355 void mf_call_f (F
const& f,
int ibox,
int i,
int j,
int k,
int, T& r)
noexcept
357 auto const& pr = f(ibox,i,j,k);
358 Reduce::detail::for_each_local<0, T, Ps...>(r, pr);
361 template <
typename I,
typename F,
typename T,
typename... Ps,
362 std::enable_if_t<std::is_same<iterate_box_comp,I>::value,
int> = 0>
364 void mf_call_f (F
const& f,
int ibox,
int i,
int j,
int k,
int ncomp, T& r)
noexcept
366 for (
int n = 0; n < ncomp; ++n) {
367 auto const& pr = f(ibox,i,j,k,n);
368 Reduce::detail::for_each_local<0, T, Ps...>(r, pr);
375template <
typename... Ps>
381 template <
typename I,
typename MF,
typename D,
typename F>
382 void eval_mf (I, MF
const& mf,
IntVect const& nghost,
int ncomp, D& reduce_data, F
const& f)
384 using ReduceTuple =
typename D::Type;
385 const int nboxes = mf.local_size();
387 auto const& parforinfo = mf.getParForInfo(nghost);
388 auto nblocks_per_box = parforinfo.getNBlocksPerBox(AMREX_GPU_MAX_THREADS);
390 const int nblocks = nblocks_per_box * nboxes;
391 const BoxIndexer* dp_boxes = parforinfo.getBoxes();
393 auto const& stream = Gpu::gpuStream();
394 auto pdst = reduce_data.devicePtr(stream);
395 int nblocks_ec = std::min(nblocks, reduce_data.maxBlocks());
397 reduce_data.nBlocks(stream) = nblocks_ec;
398 reduce_data.updateMaxStreamIndex(stream);
402 constexpr std::size_t shared_mem_bytes =
sizeof(
unsigned long long)*Gpu::Device::warp_size;
403 amrex::launch<AMREX_GPU_MAX_THREADS>(nblocks_ec, shared_mem_bytes, stream,
406 Dim1 blockIdx {gh.blockIdx()};
407 Dim1 threadIdx{gh.threadIdx()};
409 amrex::launch_global<AMREX_GPU_MAX_THREADS>
410 <<<nblocks_ec, AMREX_GPU_MAX_THREADS, 0, stream>>>
415 Reduce::detail::for_each_init<0, ReduceTuple, Ps...>(r);
416 ReduceTuple& dst =
pdst[blockIdx.x];
417 if (threadIdx.x == 0) {
420 for (
int iblock = blockIdx.x; iblock < nblocks; iblock += nblocks_ec) {
421 int ibox = iblock / nblocks_per_box;
422 auto icell = std::uint64_t(iblock-ibox*nblocks_per_box)*AMREX_GPU_MAX_THREADS + threadIdx.x;
425 if (icell < indexer.
numPts()) {
426 auto [i, j, k] = indexer(icell);
427 Reduce::detail::mf_call_f<I,
F, ReduceTuple, Ps...>
428 (f, ibox, i, j, k, ncomp, r);
432 Reduce::detail::for_each_parallel<0, ReduceTuple, Ps...>(dst, r, gh);
434 Reduce::detail::for_each_parallel<0, ReduceTuple, Ps...>(dst, r);
440 template <
typename MF,
typename D,
typename F>
441 std::enable_if_t<IsFabArray<MF>::value
442#ifndef AMREX_USE_CUDA
448 using ReduceTuple =
typename D::Type;
449 const int nboxes = mf.local_size();
452 }
else if (!mf.isFusingCandidate()) {
455 const int li = mfi.LocalIndex();
456 this->eval(b, reduce_data,
459 return f(li, i, j, k);
463 eval_mf(Reduce::detail::iterate_box{},
464 mf, nghost, 0, reduce_data, std::forward<F>(f));
468 template <
typename MF,
typename D,
typename F>
469 std::enable_if_t<IsFabArray<MF>::value
470#ifndef AMREX_USE_CUDA
474 eval (MF
const& mf,
IntVect const& nghost,
int ncomp, D& reduce_data, F&& f)
476 using ReduceTuple =
typename D::Type;
478 const int nboxes = mf.local_size();
482 }
else if (!mf.isFusingCandidate()) {
485 const int li = mfi.LocalIndex();
486 this->eval(b, ncomp, reduce_data,
489 return f(li, i, j, k, n);
493 eval_mf(Reduce::detail::iterate_box_comp{},
494 mf, nghost, ncomp, reduce_data, std::forward<F>(f));
498 template <
typename D,
typename F>
499 void eval (
Box const& box, D & reduce_data, F
const& f)
501 using ReduceTuple =
typename D::Type;
502 auto const& stream = Gpu::gpuStream();
503 auto dp = reduce_data.devicePtr(stream);
504 int& nblocks = reduce_data.nBlocks(stream);
505 int ncells = box.
numPts();
508 const auto lenxy = len.x*len.y;
509 const auto lenx = len.x;
511 constexpr int nitems_per_thread = 4;
512 int nblocks_ec = (ncells + nitems_per_thread*AMREX_GPU_MAX_THREADS-1)
513 / (nitems_per_thread*AMREX_GPU_MAX_THREADS);
514 nblocks_ec = std::min(nblocks_ec, reduce_data.maxBlocks());
515 reduce_data.updateMaxStreamIndex(stream);
518 constexpr std::size_t shared_mem_bytes =
sizeof(
unsigned long long)*Gpu::Device::warp_size;
519 amrex::launch<AMREX_GPU_MAX_THREADS>(nblocks_ec, shared_mem_bytes, stream,
522 Dim1 blockIdx {gh.blockIdx()};
523 Dim1 threadIdx{gh.threadIdx()};
524 Dim1 gridDim {gh.gridDim()};
526 amrex::launch<AMREX_GPU_MAX_THREADS>(nblocks_ec, 0, stream,
531 Reduce::detail::for_each_init<0, ReduceTuple, Ps...>(r);
532 ReduceTuple& dst = *(dp+blockIdx.x);
533 if (threadIdx.x == 0 &&
static_cast<int>(blockIdx.x) >= nblocks) {
536 for (
int icell = AMREX_GPU_MAX_THREADS*blockIdx.x+threadIdx.x, stride = AMREX_GPU_MAX_THREADS*gridDim.x;
537 icell < ncells; icell += stride) {
538 int k = icell / lenxy;
539 int j = (icell - k*lenxy) / lenx;
540 int i = (icell - k*lenxy) - j*lenx;
544 auto pr = Reduce::detail::call_f(f,i,j,k,ixtype);
545 Reduce::detail::for_each_local<0, ReduceTuple, Ps...>(r, pr);
548 Reduce::detail::for_each_parallel<0, ReduceTuple, Ps...>(dst, r, gh);
550 Reduce::detail::for_each_parallel<0, ReduceTuple, Ps...>(dst, r);
553 nblocks = std::max(nblocks, nblocks_ec);
556 template <
typename N,
typename D,
typename F,
557 typename M=std::enable_if_t<std::is_integral_v<N>> >
558 void eval (
Box const& box, N ncomp, D & reduce_data, F
const& f)
560 using ReduceTuple =
typename D::Type;
561 auto const& stream = Gpu::gpuStream();
562 auto dp = reduce_data.devicePtr(stream);
563 int& nblocks = reduce_data.nBlocks(stream);
564 int ncells = box.
numPts();
567 const auto lenxy = len.x*len.y;
568 const auto lenx = len.x;
569 constexpr int nitems_per_thread = 4;
570 int nblocks_ec = (ncells + nitems_per_thread*AMREX_GPU_MAX_THREADS-1)
571 / (nitems_per_thread*AMREX_GPU_MAX_THREADS);
572 nblocks_ec = std::min(nblocks_ec, reduce_data.maxBlocks());
573 reduce_data.updateMaxStreamIndex(stream);
576 constexpr std::size_t shared_mem_bytes =
sizeof(
unsigned long long)*Gpu::Device::warp_size;
577 amrex::launch<AMREX_GPU_MAX_THREADS>(nblocks_ec, shared_mem_bytes, stream,
580 Dim1 blockIdx {gh.blockIdx()};
581 Dim1 threadIdx{gh.threadIdx()};
582 Dim1 gridDim {gh.gridDim()};
584 amrex::launch<AMREX_GPU_MAX_THREADS>(nblocks_ec, 0, stream,
589 Reduce::detail::for_each_init<0, ReduceTuple, Ps...>(r);
590 ReduceTuple& dst = *(dp+blockIdx.x);
591 if (threadIdx.x == 0 &&
static_cast<int>(blockIdx.x) >= nblocks) {
594 for (
int icell = AMREX_GPU_MAX_THREADS*blockIdx.x+threadIdx.x, stride = AMREX_GPU_MAX_THREADS*gridDim.x;
595 icell < ncells; icell += stride) {
596 int k = icell / lenxy;
597 int j = (icell - k*lenxy) / lenx;
598 int i = (icell - k*lenxy) - j*lenx;
602 for (N n = 0; n < ncomp; ++n) {
603 auto pr = f(i,j,k,n);
604 Reduce::detail::for_each_local<0, ReduceTuple, Ps...>(r, pr);
608 Reduce::detail::for_each_parallel<0, ReduceTuple, Ps...>(dst, r, gh);
610 Reduce::detail::for_each_parallel<0, ReduceTuple, Ps...>(dst, r);
613 nblocks = std::max(nblocks, nblocks_ec);
616 template <
typename N,
typename D,
typename F,
617 typename M=std::enable_if_t<std::is_integral_v<N>> >
618 void eval (N n, D & reduce_data, F
const& f)
620 if (n <= 0) {
return; }
621 using ReduceTuple =
typename D::Type;
622 auto const& stream = Gpu::gpuStream();
623 auto dp = reduce_data.devicePtr(stream);
624 int& nblocks = reduce_data.nBlocks(stream);
625 constexpr int nitems_per_thread = 4;
626 int nblocks_ec = (n + nitems_per_thread*AMREX_GPU_MAX_THREADS-1)
627 / (nitems_per_thread*AMREX_GPU_MAX_THREADS);
628 nblocks_ec = std::min(nblocks_ec, reduce_data.maxBlocks());
629 reduce_data.updateMaxStreamIndex(stream);
632 constexpr std::size_t shared_mem_bytes =
sizeof(
unsigned long long)*Gpu::Device::warp_size;
633 amrex::launch<AMREX_GPU_MAX_THREADS>(nblocks_ec, shared_mem_bytes, stream,
636 Dim1 blockIdx {gh.blockIdx()};
637 Dim1 threadIdx{gh.threadIdx()};
638 Dim1 gridDim {gh.gridDim()};
640 amrex::launch<AMREX_GPU_MAX_THREADS>(nblocks_ec, 0, stream,
645 Reduce::detail::for_each_init<0, ReduceTuple, Ps...>(r);
646 ReduceTuple& dst = *(dp+blockIdx.x);
647 if (threadIdx.x == 0 &&
static_cast<int>(blockIdx.x) >= nblocks) {
650 for (N i = AMREX_GPU_MAX_THREADS*blockIdx.x+threadIdx.x, stride = AMREX_GPU_MAX_THREADS*gridDim.x;
651 i < n; i += stride) {
653 Reduce::detail::for_each_local<0, ReduceTuple, Ps...>(r,pr);
656 Reduce::detail::for_each_parallel<0, ReduceTuple, Ps...>(dst, r, gh);
658 Reduce::detail::for_each_parallel<0, ReduceTuple, Ps...>(dst, r);
664 template <
typename D>
665 typename D::Type
value (D & reduce_data)
667 auto hp = reduce_data.hostPtr();
669 if (m_result_is_ready) {
673 using ReduceTuple =
typename D::Type;
674 auto const& stream = Gpu::gpuStream();
675 auto dp = reduce_data.devicePtr();
676 auto const& nblocks = reduce_data.nBlocks();
677#if defined(AMREX_USE_SYCL)
678 if (reduce_data.maxStreamIndex() == 0 && nblocks[0] <= 4096) {
679 const int N = nblocks[0];
681 Reduce::detail::for_each_init<0, ReduceTuple, Ps...>(*hp);
684 Gpu::dtoh_memcpy_async(tmp.
data(), dp,
sizeof(ReduceTuple)*N);
685 Gpu::streamSynchronize();
686 for (
int i = 1; i < N; ++i) {
687 Reduce::detail::for_each_local<0, ReduceTuple, Ps...>(tmp[0], tmp[i]);
694 int maxblocks = reduce_data.maxBlocks();
697 constexpr std::size_t shared_mem_bytes =
sizeof(
unsigned long long)*Gpu::Device::warp_size;
698#ifndef AMREX_NO_SYCL_REDUCE_WORKAROUND
701 auto presult = dtmp.
data();
705 amrex::launch<AMREX_GPU_MAX_THREADS>(1, shared_mem_bytes, stream,
709 Reduce::detail::for_each_init<0, ReduceTuple, Ps...>(r);
711 for (
int istream = 0, nstreams = nblocks.size(); istream < nstreams; ++istream) {
712 auto dp_stream = dp+istream*maxblocks;
713 for (
int i = gh.item->get_global_id(0), stride = gh.item->get_global_range(0);
714 i < nblocks[istream]; i += stride) {
715 Reduce::detail::for_each_local<0, ReduceTuple, Ps...>(r, dp_stream[i]);
718 Reduce::detail::for_each_parallel<0, ReduceTuple, Ps...>(dst, r, gh);
719 if (gh.threadIdx() == 0) { *presult = dst; }
721#ifndef AMREX_NO_SYCL_REDUCE_WORKAROUND
722 Gpu::dtoh_memcpy_async(hp, dtmp.
data(),
sizeof(ReduceTuple));
725 amrex::launch<AMREX_GPU_MAX_THREADS>(1, 0, stream,
729 Reduce::detail::for_each_init<0, ReduceTuple, Ps...>(r);
731 for (
int istream = 0, nstreams = nblocks.size(); istream < nstreams; ++istream) {
732 auto dp_stream = dp+istream*maxblocks;
733 for (
int i = AMREX_GPU_MAX_THREADS*blockIdx.x+threadIdx.x, stride = AMREX_GPU_MAX_THREADS*gridDim.x;
734 i < nblocks[istream]; i += stride) {
735 Reduce::detail::for_each_local<0, ReduceTuple, Ps...>(r, dp_stream[i]);
738 Reduce::detail::for_each_parallel<0, ReduceTuple, Ps...>(dst, r);
739 if (threadIdx.x == 0) { *hp = dst; }
742 Gpu::streamSynchronize();
745 m_result_is_ready =
true;
750 bool m_result_is_ready =
false;
759template <
typename T,
typename N,
typename M=std::enable_if_t<std::is_
integral_v<N>> >
760T Sum (N n, T
const* v, T init_val = 0)
764 using ReduceTuple =
typename decltype(reduce_data)::Type;
766 ReduceTuple hv = reduce_data.
value(reduce_op);
767 return amrex::get<0>(hv) + init_val;
771template <
typename T,
typename N,
typename F,
772 typename M=std::enable_if_t<std::is_integral_v<N>> >
773T Sum (N n, F
const& f, T init_val = 0)
777 using ReduceTuple =
typename decltype(reduce_data)::Type;
779 ReduceTuple hv = reduce_data.
value(reduce_op);
780 return amrex::get<0>(hv) + init_val;
784template <
typename T,
typename N,
typename M=std::enable_if_t<std::is_
integral_v<N>> >
785T Min (N n, T
const* v, T init_val = std::numeric_limits<T>::max())
789 using ReduceTuple =
typename decltype(reduce_data)::Type;
791 ReduceTuple hv = reduce_data.
value(reduce_op);
792 return std::min(amrex::get<0>(hv),init_val);
796template <
typename T,
typename N,
typename F,
797 typename M=std::enable_if_t<std::is_integral_v<N>> >
798T Min (N n, F
const& f, T init_val = std::numeric_limits<T>::max())
802 using ReduceTuple =
typename decltype(reduce_data)::Type;
804 ReduceTuple hv = reduce_data.
value(reduce_op);
805 return std::min(amrex::get<0>(hv),init_val);
809template <
typename T,
typename N,
typename M=std::enable_if_t<std::is_
integral_v<N>> >
810T Max (N n, T
const* v, T init_val = std::numeric_limits<T>::lowest())
814 using ReduceTuple =
typename decltype(reduce_data)::Type;
816 ReduceTuple hv = reduce_data.
value(reduce_op);
817 return std::max(amrex::get<0>(hv),init_val);
821template <
typename T,
typename N,
typename F,
822 typename M=std::enable_if_t<std::is_integral_v<N>> >
823T Max (N n, F
const& f, T init_val = std::numeric_limits<T>::lowest())
827 using ReduceTuple =
typename decltype(reduce_data)::Type;
829 ReduceTuple hv = reduce_data.
value(reduce_op);
830 return std::max(amrex::get<0>(hv),init_val);
834template <
typename T,
typename N,
typename M=std::enable_if_t<std::is_
integral_v<N>> >
839 using ReduceTuple =
typename decltype(reduce_data)::Type;
843 auto hv = reduce_data.
value(reduce_op);
844 return std::make_pair(amrex::get<0>(hv), amrex::get<1>(hv));
848template <
typename T,
typename N,
typename F,
849 typename M=std::enable_if_t<std::is_integral_v<N>> >
854 using ReduceTuple =
typename decltype(reduce_data)::Type;
859 auto hv = reduce_data.
value(reduce_op);
860 return std::make_pair(amrex::get<0>(hv), amrex::get<1>(hv));
864template <
typename T,
typename N,
typename P,
typename M=std::enable_if_t<std::is_
integral_v<N>> >
865bool AnyOf (N n, T
const* v, P
const& pred)
871 ec.numBlocks.x = std::min(ec.numBlocks.x, Gpu::Device::maxBlocksPerLaunch());
874 const int num_ints = std::max(Gpu::Device::warp_size,
int(ec.numThreads.x)/Gpu::Device::warp_size) + 1;
875 const std::size_t shared_mem_bytes = num_ints*
sizeof(int);
876 amrex::launch<AMREX_GPU_MAX_THREADS>(ec.numBlocks.x, shared_mem_bytes, Gpu::gpuStream(),
878 int* has_any = &(
static_cast<int*
>(gh.sharedMemory())[num_ints-1]);
879 if (gh.threadIdx() == 0) { *has_any = *dp; }
885 for (N i = AMREX_GPU_MAX_THREADS*gh.blockIdx()+gh.threadIdx(), stride = AMREX_GPU_MAX_THREADS*gh.gridDim();
886 i < n && !r; i += stride)
888 r = pred(v[i]) ? 1 : 0;
891 r = Gpu::blockReduce<Gpu::Device::warp_size>
893 if (gh.threadIdx() == 0 && r) { *dp = 1; }
897 amrex::launch<AMREX_GPU_MAX_THREADS>(ec.numBlocks.x, 0, Gpu::gpuStream(),
899 __shared__
int has_any;
900 if (threadIdx.x == 0) { has_any = *dp; }
906 for (N i = AMREX_GPU_MAX_THREADS*blockIdx.x+threadIdx.x, stride = AMREX_GPU_MAX_THREADS*gridDim.x;
907 i < n && !r; i += stride)
909 r = pred(v[i]) ? 1 : 0;
911 r = Gpu::blockReduce<Gpu::Device::warp_size>
913 if (threadIdx.x == 0 && r) *dp = 1;
927 int ncells = box.
numPts();
930 const auto lenxy = len.x*len.y;
931 const auto lenx = len.x;
933 ec.numBlocks.x = std::min(ec.numBlocks.x, Gpu::Device::maxBlocksPerLaunch());
936 const int num_ints = std::max(Gpu::Device::warp_size,
int(ec.numThreads.x)/Gpu::Device::warp_size) + 1;
937 const std::size_t shared_mem_bytes = num_ints*
sizeof(int);
938 amrex::launch<AMREX_GPU_MAX_THREADS>(ec.numBlocks.x, shared_mem_bytes, Gpu::gpuStream(),
940 int* has_any = &(
static_cast<int*
>(gh.sharedMemory())[num_ints-1]);
941 if (gh.threadIdx() == 0) { *has_any = *dp; }
947 for (
int icell = AMREX_GPU_MAX_THREADS*gh.blockIdx()+gh.threadIdx(), stride = AMREX_GPU_MAX_THREADS*gh.gridDim();
948 icell < ncells && !r; icell += stride) {
949 int k = icell / lenxy;
950 int j = (icell - k*lenxy) / lenx;
951 int i = (icell - k*lenxy) - j*lenx;
955 r = pred(i,j,k) ? 1 : 0;
957 r = Gpu::blockReduce<Gpu::Device::warp_size>
959 if (gh.threadIdx() == 0 && r) { *dp = 1; }
966 __shared__
int has_any;
967 if (threadIdx.x == 0) { has_any = *dp; }
973 for (
int icell = AMREX_GPU_MAX_THREADS*blockIdx.x+threadIdx.x, stride = AMREX_GPU_MAX_THREADS*gridDim.x;
974 icell < ncells && !r; icell += stride) {
975 int k = icell / lenxy;
976 int j = (icell - k*lenxy) / lenx;
977 int i = (icell - k*lenxy) - j*lenx;
981 r = pred(i,j,k) ? 1 : 0;
983 r = Gpu::blockReduce<Gpu::Device::warp_size>
985 if (threadIdx.x == 0 && r) *dp = 1;
996template <
typename... Ts>
1000 using Type = GpuTuple<Ts...>;
1002 template <
typename... Ps>
1003 explicit ReduceData (ReduceOps<Ps...>& reduce_op)
1004 : m_tuple(OpenMP::in_parallel() ? 1 : OpenMP::get_max_threads()),
1005 m_fn_value([&reduce_op,this] () -> Type { return this->value(reduce_op); })
1007 reduce_op.resetResultReadiness();
1008 for (
auto& t : m_tuple) {
1009 Reduce::detail::for_each_init<0, Type, Ps...>(t);
1013 ~ReduceData () =
default;
1014 ReduceData (ReduceData<Ts...>
const&) =
delete;
1015 ReduceData (ReduceData<Ts...> &&) =
delete;
1016 void operator= (ReduceData<Ts...>
const&) =
delete;
1017 void operator= (ReduceData<Ts...> &&) =
delete;
1019 Type value () {
return m_fn_value(); }
1021 template <
typename... Ps>
1022 Type value (ReduceOps<Ps...>& reduce_op)
1024 return reduce_op.value(*
this);
1027 Vector<Type>& reference () {
return m_tuple; }
1029 Type& reference (
int tid)
1031 if (m_tuple.size() == 1) {
1035 return m_tuple[tid];
1040 Vector<Type> m_tuple;
1041 std::function<Type()> m_fn_value;
1044template <
typename... Ps>
1049 template <
typename D,
typename F>
1051 static auto call_f (Box
const& box,
typename D::Type & r, F
const& f)
1052 noexcept -> std::enable_if_t<std::is_same_v<std::decay_t<
decltype(f(0,0,0))>,
1055 using ReduceTuple =
typename D::Type;
1058 for (
int k = lo.z; k <= hi.z; ++k) {
1059 for (
int j = lo.y; j <= hi.y; ++j) {
1060 for (
int i = lo.x; i <= hi.x; ++i) {
1061 Reduce::detail::for_each_local<0, ReduceTuple, Ps...>(r, f(i,j,k));
1065 template <
typename D,
typename F>
1067 static auto call_f (Box
const& box,
typename D::Type & r, F
const& f)
1068 noexcept -> std::enable_if_t<std::is_same_v<std::decay_t<
decltype(f(
Box()))>,
1071 using ReduceTuple =
typename D::Type;
1072 Reduce::detail::for_each_local<0, ReduceTuple, Ps...>(r, f(box));
1077 template <
typename MF,
typename D,
typename F>
1078 std::enable_if_t<IsFabArray<MF>::value && IsCallable<F, int, int, int, int>::value>
1079 eval (MF
const& mf, IntVect
const& nghost, D & reduce_data, F
const& f)
1081 using ReduceTuple =
typename D::Type;
1087 Reduce::detail::for_each_init<0, ReduceTuple, Ps...>(rr);
1088 for (MFIter mfi(mf,
true); mfi.isValid(); ++mfi) {
1089 Box const& b = mfi.growntilebox(nghost);
1090 const int li = mfi.LocalIndex();
1093 for (
int k = lo.z; k <= hi.z; ++k) {
1094 for (
int j = lo.y; j <= hi.y; ++j) {
1095 for (
int i = lo.x; i <= hi.x; ++i) {
1096 Reduce::detail::for_each_local<0, ReduceTuple, Ps...>(rr, f(li,i,j,k));
1099 Reduce::detail::for_each_local<0, ReduceTuple, Ps...>(
1100 reduce_data.reference(OpenMP::get_thread_num()), rr);
1104 template <
typename MF,
typename D,
typename F>
1105 std::enable_if_t<IsFabArray<MF>::value && IsCallable<F, int, int, int, int, int>::value>
1106 eval (MF
const& mf, IntVect
const& nghost,
int ncomp, D & reduce_data, F
const& f)
1108 using ReduceTuple =
typename D::Type;
1114 Reduce::detail::for_each_init<0, ReduceTuple, Ps...>(rr);
1115 for (MFIter mfi(mf,
true); mfi.isValid(); ++mfi) {
1116 Box const& b = mfi.growntilebox(nghost);
1117 const int li = mfi.LocalIndex();
1120 for (
int n = 0; n < ncomp; ++n) {
1121 for (
int k = lo.z; k <= hi.z; ++k) {
1122 for (
int j = lo.y; j <= hi.y; ++j) {
1123 for (
int i = lo.x; i <= hi.x; ++i) {
1124 Reduce::detail::for_each_local<0, ReduceTuple, Ps...>(rr, f(li,i,j,k,n));
1127 Reduce::detail::for_each_local<0, ReduceTuple, Ps...>(
1128 reduce_data.reference(OpenMP::get_thread_num()), rr);
1132 template <
typename D,
typename F>
1133 void eval (Box
const& box, D & reduce_data, F&& f)
1135 using ReduceTuple =
typename D::Type;
1137 Reduce::detail::for_each_init<0, ReduceTuple, Ps...>(rr);
1138 call_f<D>(box, rr, std::forward<F>(f));
1139 Reduce::detail::for_each_local<0, ReduceTuple, Ps...>(
1140 reduce_data.reference(OpenMP::get_thread_num()), rr);
1143 template <
typename N,
typename D,
typename F,
1144 typename M=std::enable_if_t<std::is_integral_v<N>> >
1145 void eval (Box
const& box, N ncomp, D & reduce_data, F
const& f)
1147 using ReduceTuple =
typename D::Type;
1149 Reduce::detail::for_each_init<0, ReduceTuple, Ps...>(rr);
1152 for (N n = 0; n < ncomp; ++n) {
1153 for (
int k = lo.z; k <= hi.z; ++k) {
1154 for (
int j = lo.y; j <= hi.y; ++j) {
1155 for (
int i = lo.x; i <= hi.x; ++i) {
1156 Reduce::detail::for_each_local<0, ReduceTuple, Ps...>(rr, f(i,j,k,n));
1158 Reduce::detail::for_each_local<0, ReduceTuple, Ps...>(
1159 reduce_data.reference(OpenMP::get_thread_num()), rr);
1162 template <
typename N,
typename D,
typename F,
1163 typename M=std::enable_if_t<std::is_integral_v<N>> >
1164 void eval (N n, D & reduce_data, F
const& f)
1166 using ReduceTuple =
typename D::Type;
1168 Reduce::detail::for_each_init<0, ReduceTuple, Ps...>(rr);
1169 for (N i = 0; i < n; ++i) {
1170 Reduce::detail::for_each_local<0, ReduceTuple, Ps...>(rr, f(i));
1172 Reduce::detail::for_each_local<0, ReduceTuple, Ps...>(
1173 reduce_data.reference(OpenMP::get_thread_num()), rr);
1176 template <
typename D>
1177 typename D::Type value (D & reduce_data)
1179 auto& rrv = reduce_data.reference();
1180 if (! m_result_is_ready) {
1181 using ReduceTuple =
typename D::Type;
1182 if (rrv.size() > 1) {
1183 for (
int i = 1, N = rrv.size(); i < N; ++i) {
1184 Reduce::detail::for_each_local<0, ReduceTuple, Ps...>(rrv[0], rrv[i]);
1187 m_result_is_ready =
true;
1192 bool m_result_is_ready =
false;
1194 void resetResultReadiness () { m_result_is_ready =
false; }
1199template <
typename T,
typename N,
typename F,
1200 typename M=std::enable_if_t<std::is_integral_v<N>> >
1201T
Sum (N n, F
const& f, T init_val = 0)
1205#pragma omp parallel for reduction(+:r)
1207 for (N i = 0; i < n; ++i) {
1213template <
typename T,
typename N,
typename M=std::enable_if_t<std::is_
integral_v<N>> >
1214T
Sum (N n, T
const* v, T init_val = 0)
1216 return Sum(n, [=] (N i) -> T {
return v[i]; }, init_val);
1219template <
typename T,
typename N,
typename F,
1220 typename M=std::enable_if_t<std::is_integral_v<N>> >
1221T
Min (N n, F
const& f, T init_val = std::numeric_limits<T>::max())
1225#pragma omp parallel for reduction(min:r)
1227 for (N i = 0; i < n; ++i) {
1228 r = std::min(r,f(i));
1233template <
typename T,
typename N,
typename M=std::enable_if_t<std::is_
integral_v<N>> >
1234T
Min (N n, T
const* v, T init_val = std::numeric_limits<T>::max())
1236 return Reduce::Min(n, [=] (N i) -> T {
return v[i]; }, init_val);
1239template <
typename T,
typename N,
typename F,
1240 typename M=std::enable_if_t<std::is_integral_v<N>> >
1241T
Max (N n, F
const& f, T init_val = std::numeric_limits<T>::lowest())
1245#pragma omp parallel for reduction(max:r)
1247 for (N i = 0; i < n; ++i) {
1248 r = std::max(r,f(i));
1253template <
typename T,
typename N,
typename M=std::enable_if_t<std::is_
integral_v<N>> >
1254T
Max (N n, T
const* v, T init_val = std::numeric_limits<T>::lowest())
1256 return Reduce::Max(n, [=] (N i) -> T {
return v[i]; }, init_val);
1259template <
typename T,
typename N,
typename F,
1260 typename M=std::enable_if_t<std::is_integral_v<N>> >
1261std::pair<T,T>
MinMax (N n, F
const& f)
1263 T r_min = std::numeric_limits<T>::max();
1264 T r_max = std::numeric_limits<T>::lowest();
1266#pragma omp parallel for reduction(min:r_min) reduction(max:r_max)
1268 for (N i = 0; i < n; ++i) {
1270 r_min = std::min(r_min,tmp);
1271 r_max = std::max(r_max,tmp);
1273 return std::make_pair(r_min,r_max);
1276template <
typename T,
typename N,
typename M=std::enable_if_t<std::is_
integral_v<N>> >
1277std::pair<T,T>
MinMax (N n, T
const* v)
1279 return Reduce::MinMax<T>(n, [=] (N i) -> T {
return v[i]; });
1282template <
typename T,
typename N,
typename P,
typename M=std::enable_if_t<std::is_
integral_v<N>> >
1283bool AnyOf (N n, T
const* v, P&& pred)
1285 return std::any_of(v, v+n, std::forward<P>(pred));
1288template <
typename P>
1289bool AnyOf (Box
const& box, P
const& pred)
1293 for (
int k = lo.z; k <= hi.z; ++k) {
1294 for (
int j = lo.y; j <= hi.y; ++j) {
1295 for (
int i = lo.x; i <= hi.x; ++i) {
1296 if (pred(i,j,k)) {
return true; }
1309template <
typename... Ts,
typename... Ps>
1311constexpr GpuTuple<Ts...>
1315 Reduce::detail::for_each_init<0,
decltype(r), Ps...>(r);
1323template <
typename... Ts,
typename... Ps>
1325constexpr GpuTuple<Ts...>
1329 Reduce::detail::for_each_init<0,
decltype(r), Ps...>(r);
#define AMREX_ASSERT(EX)
Definition AMReX_BLassert.H:38
#define AMREX_FORCE_INLINE
Definition AMReX_Extension.H:119
#define AMREX_GPU_MAX_STREAMS
Definition AMReX_GpuDevice.H:21
#define AMREX_LAUNCH_KERNEL(MT, blocks, threads, sharedMem, stream,...)
Definition AMReX_GpuLaunch.H:35
#define AMREX_GPU_DEVICE
Definition AMReX_GpuQualifiers.H:18
#define AMREX_GPU_HOST_DEVICE
Definition AMReX_GpuQualifiers.H:20
Real * pdst
Definition AMReX_HypreMLABecLap.cpp:1090
#define AMREX_D_PICK(a, b, c)
Definition AMReX_SPACE.H:173
#define AMREX_D_DECL(a, b, c)
Definition AMReX_SPACE.H:171
virtual void free(void *pt)=0
A pure virtual function for deleting the arena pointed to by pt.
__host__ __device__ Long numPts() const noexcept
Return the number of points contained in the BoxND.
Definition AMReX_Box.H:356
__host__ __device__ IndexTypeND< dim > ixType() const noexcept
Return the indexing type.
Definition AMReX_Box.H:135
GPU-compatible tuple.
Definition AMReX_Tuple.H:98
static int streamIndex(gpuStream_t s=gpuStream()) noexcept
Definition AMReX_GpuDevice.cpp:697
Iterator for looping ever tiles and boxes of amrex::FabArray based containers.
Definition AMReX_MFIter.H:63
bool isValid() const noexcept
Is the iterator valid i.e. is it associated with a FAB?
Definition AMReX_MFIter.H:147
Dynamically allocated vector for trivially copyable data.
Definition AMReX_PODVector.H:308
T * data() noexcept
Definition AMReX_PODVector.H:666
Definition AMReX_Reduce.H:257
~ReduceData()
Definition AMReX_Reduce.H:279
int maxStreamIndex() const
Definition AMReX_Reduce.H:312
Type value()
Definition AMReX_Reduce.H:289
void updateMaxStreamIndex(gpuStream_t const &s)
Definition AMReX_Reduce.H:313
Type * m_host_tuple
Definition AMReX_Reduce.H:320
int & nBlocks(gpuStream_t const &s)
Definition AMReX_Reduce.H:308
ReduceData(ReduceOps< Ps... > &reduce_op)
Definition AMReX_Reduce.H:262
int m_max_blocks
Definition AMReX_Reduce.H:318
GpuArray< int, 8 > m_nblocks
Definition AMReX_Reduce.H:322
Type * devicePtr(gpuStream_t const &s)
Definition AMReX_Reduce.H:301
Type value(ReduceOps< Ps... > &reduce_op)
Definition AMReX_Reduce.H:295
Type * devicePtr()
Definition AMReX_Reduce.H:300
Type * m_device_tuple
Definition AMReX_Reduce.H:321
GpuArray< int, 8 > & nBlocks()
Definition AMReX_Reduce.H:307
ReduceData(ReduceData< Ts... > const &)=delete
std::function< Type()> m_fn_value
Definition AMReX_Reduce.H:323
Type * hostPtr()
Definition AMReX_Reduce.H:305
int maxBlocks() const
Definition AMReX_Reduce.H:310
ReduceData(ReduceData< Ts... > &&)=delete
Definition AMReX_Reduce.H:377
D::Type value(D &reduce_data)
Definition AMReX_Reduce.H:665
void eval(Box const &box, N ncomp, D &reduce_data, F const &f)
Definition AMReX_Reduce.H:558
void eval_mf(I, MF const &mf, IntVect const &nghost, int ncomp, D &reduce_data, F const &f)
Definition AMReX_Reduce.H:382
void eval(Box const &box, D &reduce_data, F const &f)
Definition AMReX_Reduce.H:499
std::enable_if_t< IsFabArray< MF >::value > eval(MF const &mf, IntVect const &nghost, D &reduce_data, F &&f)
Definition AMReX_Reduce.H:446
void resetResultReadiness()
Definition AMReX_Reduce.H:753
std::enable_if_t< IsFabArray< MF >::value > eval(MF const &mf, IntVect const &nghost, int ncomp, D &reduce_data, F &&f)
Definition AMReX_Reduce.H:474
void eval(N n, D &reduce_data, F const &f)
Definition AMReX_Reduce.H:618
amrex_long Long
Definition AMReX_INT.H:30
std::pair< T, T > MinMax(N n, T const *v)
Definition AMReX_Reduce.H:835
bool AnyOf(N n, T const *v, P const &pred)
Definition AMReX_Reduce.H:865
__host__ __device__ BoxND< dim > grow(const BoxND< dim > &b, int i) noexcept
Grow BoxND in all directions by given amount.
Definition AMReX_Box.H:1280
void Sum(T &v, MPI_Comm comm)
Definition AMReX_ParallelReduce.H:221
__host__ __device__ AMREX_FORCE_INLINE T Max(T *const m, T const value) noexcept
Definition AMReX_GpuAtomic.H:419
__host__ __device__ AMREX_FORCE_INLINE T Min(T *const m, T const value) noexcept
Definition AMReX_GpuAtomic.H:356
__device__ int blockReduceLogicalOr(int source) noexcept
Definition AMReX_GpuReduce.H:556
__device__ T blockReduceMax(T source) noexcept
Definition AMReX_GpuReduce.H:455
__device__ T blockReduceMin(T source) noexcept
Definition AMReX_GpuReduce.H:400
__device__ int blockReduceLogicalAnd(int source) noexcept
Definition AMReX_GpuReduce.H:508
__device__ T blockReduceSum(T source) noexcept
Definition AMReX_GpuReduce.H:350
Definition AMReX_Amr.cpp:49
__host__ __device__ Dim3 ubound(Array4< T > const &a) noexcept
Definition AMReX_Array4.H:319
__host__ __device__ void ignore_unused(const Ts &...)
This shuts up the compiler about unused variables.
Definition AMReX.H:138
__host__ __device__ Dim3 length(Array4< T > const &a) noexcept
Definition AMReX_Array4.H:326
cudaStream_t gpuStream_t
Definition AMReX_GpuControl.H:83
__host__ __device__ constexpr GpuTuple< Ts... > IdentityTuple(GpuTuple< Ts... >, ReduceOps< Ps... >) noexcept
Return a GpuTuple containing the identity element for each operation in ReduceOps....
Definition AMReX_Reduce.H:1312
BoxND< 3 > Box
Box is an alias for amrex::BoxND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:27
__host__ __device__ constexpr const T & min(const T &a, const T &b) noexcept
Definition AMReX_Algorithm.H:21
__host__ __device__ constexpr const T & max(const T &a, const T &b) noexcept
Definition AMReX_Algorithm.H:35
Arena * The_Pinned_Arena()
Definition AMReX_Arena.cpp:823
Arena * The_Arena()
Definition AMReX_Arena.cpp:783
__host__ __device__ Dim3 lbound(Array4< T > const &a) noexcept
Definition AMReX_Array4.H:312
Definition AMReX_Box.H:2152
__host__ __device__ std::uint64_t numPts() const
Definition AMReX_Box.H:2193
Fixed-size array that can be used on GPU.
Definition AMReX_Array.H:40
Definition AMReX_GpuMemory.H:56
T dataValue() const
Definition AMReX_GpuMemory.H:92
T * dataPtr()
Definition AMReX_GpuMemory.H:90
Definition AMReX_GpuLaunch.H:116
Definition AMReX_GpuTypes.H:86
Definition AMReX_GpuControl.H:131
Definition AMReX_GpuReduce.H:287
Test if a given type T is callable with arguments of type Args...
Definition AMReX_TypeTraits.H:213
Definition AMReX_Functional.H:14
Definition AMReX_Reduce.H:186
constexpr std::enable_if_t< std::is_integral_v< T > > init(T &t) const noexcept
Definition AMReX_Reduce.H:214
__device__ std::enable_if_t< std::is_integral_v< T > > parallel_update(T &d, T s) const noexcept
Definition AMReX_Reduce.H:200
__host__ __device__ std::enable_if_t< std::is_integral_v< T > > local_update(T &d, T s) const noexcept
Definition AMReX_Reduce.H:210
Definition AMReX_Reduce.H:219
constexpr std::enable_if_t< std::is_integral_v< T > > init(T &t) const noexcept
Definition AMReX_Reduce.H:247
__host__ __device__ std::enable_if_t< std::is_integral_v< T > > local_update(T &d, T s) const noexcept
Definition AMReX_Reduce.H:243
__device__ std::enable_if_t< std::is_integral_v< T > > parallel_update(T &d, T s) const noexcept
Definition AMReX_Reduce.H:233
Definition AMReX_Reduce.H:152
constexpr std::enable_if_t< std::numeric_limits< T >::is_specialized > init(T &t) const noexcept
Definition AMReX_Reduce.H:177
constexpr std::enable_if_t<!std::numeric_limits< T >::is_specialized > init(T &t) const noexcept
Definition AMReX_Reduce.H:181
__host__ __device__ void local_update(T &d, T const &s) const noexcept
Definition AMReX_Reduce.H:173
__device__ void parallel_update(T &d, T const &s) const noexcept
Definition AMReX_Reduce.H:164
Definition AMReX_Reduce.H:118
constexpr std::enable_if_t<!std::numeric_limits< T >::is_specialized > init(T &t) const noexcept
Definition AMReX_Reduce.H:147
__device__ void parallel_update(T &d, T const &s) const noexcept
Definition AMReX_Reduce.H:130
__host__ __device__ void local_update(T &d, T const &s) const noexcept
Definition AMReX_Reduce.H:139
constexpr std::enable_if_t< std::numeric_limits< T >::is_specialized > init(T &t) const noexcept
Definition AMReX_Reduce.H:143
Definition AMReX_Reduce.H:88
__device__ void parallel_update(T &d, T const &s) const noexcept
Definition AMReX_Reduce.H:101
__host__ __device__ void local_update(T &d, T const &s) const noexcept
Definition AMReX_Reduce.H:110
constexpr void init(T &t) const noexcept
Definition AMReX_Reduce.H:113
Struct for holding types.
Definition AMReX_TypeList.H:12