Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_Array4.H
Go to the documentation of this file.
1#ifndef AMREX_ARRAY4_H_
2#define AMREX_ARRAY4_H_
3
4#include <AMReX_Config.H>
5
10
11#include <AMReX.H>
12#include <AMReX_IntVect.H>
13#include <AMReX_GpuPrint.H>
14#include <AMReX_ConstexprFor.H>
15
16#include <iostream>
17#include <sstream>
18
20#define AMREX_ARRAY4_INDEX_ASSERT(i,j,k,n) \
21 if ((i)<begin.vect[0] || (i)>=end.vect[0] || \
22 (j)<begin.vect[1] || (j)>=end.vect[1] || \
23 (k)<begin.vect[2] || (k)>=end.vect[2] || \
24 (n)<0 || (n)>=end.vect[3]) \
25 { \
26 index_assert_print_error_message(i,j,k,n); \
27 }
29
30namespace amrex {
31
32 template<int dim>
33 class BoxND;
34
45 template <typename T>
46 struct CellData
47 {
49 T* AMREX_RESTRICT p = nullptr;
50 Long stride = 0;
51 int ncomp = 0;
53
64 constexpr CellData (T* a_p, Long a_stride, int a_ncomp)
65 : p(a_p), stride(a_stride), ncomp(a_ncomp)
66 {}
67
76 template <class U=T>
77 requires (std::is_const_v<U>)
79 constexpr CellData (CellData<std::remove_const_t<T>> const& rhs) noexcept
80 : p(rhs.p), stride(rhs.stride), ncomp(rhs.ncomp)
81 {}
82
89 explicit operator bool() const noexcept { return p != nullptr; }
90
92 [[nodiscard]] AMREX_GPU_HOST_DEVICE
93 int nComp() const noexcept { return ncomp; }
94
106 T& operator[] (int n) const
107 requires (!std::is_void_v<T>)
108
109 {
110#if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
111 if (n < 0 || n >= ncomp) {
113 AMREX_DEVICE_PRINTF(" %d is out of bound (0:%d)", n, ncomp-1);
114 amrex::Abort();
115 ))
117 std::stringstream ss;
118 ss << " " << n << " is out of bound: (0:" << ncomp-1 << ")";
119 amrex::Abort(ss.str());
120 ))
121 }
122#endif
123 return p[n*stride];
124 }
125 };
126
128 namespace detail {
129
130 template <int N> struct Stride { Long a[N] = {}; };
131 template <> struct Stride<0> {};
132
133 // int strides help GPUs; on CPUs, gcc handles Long strides better.
134#if defined(AMREX_USE_GPU) && !defined(AMREX_ARRAY4_64BIT_INDEX)
135 using Array4Int = int;
136#else
137 using Array4Int = Long;
138#endif
140 struct Array4Stride { Array4Int a[2] = {}; Long n = 0; };
141
142 template <int N, bool C> struct StrideType { using type = Stride<N-1>; };
143 template <> struct StrideType<4,true> { using type = Array4Stride; };
144
150 template <typename T>
151 struct IsValidIndexType {
152 static constexpr bool value = std::is_integral_v<T> && IsNonNarrowingConversion_v<T, int>;
153 };
154
161 template <int N, bool last_dim_component, typename... idx>
162 struct ArrayNDIndexCheck_impl {
163 private:
164 static constexpr int num_indices = sizeof...(idx);
165
166 template <typename... Ts>
167 struct AllExceptLastEnum;
168
169 template <typename first, typename... Ts>
170 struct AllExceptLastEnum<first, Ts...> {
171 static constexpr bool value =
172 IsValidIndexType<std::decay_t<first>>::value
173 && AllExceptLastEnum<Ts...>::value;
174 };
175
176 // Check if the last index type is an enum or valid index type.
177 // An enum is only allowed if last_dim_component is true.
178 template <typename last>
179 struct AllExceptLastEnum<last> {
180 static constexpr bool value = IsValidIndexType<std::decay_t<last>>::value
181 || (std::is_enum_v<std::decay_t<last>>);
182 };
183
184 static constexpr bool index_with_maybe_enum = AllExceptLastEnum<idx...>::value;
185
186 static constexpr bool index_all_values = Conjunction<
187 IsValidIndexType<std::decay_t<idx>>...>::value;
188
189 public:
190 static constexpr bool value = ((num_indices == N) && index_all_values) // N indices are integer types
191 || ((num_indices == N - 1) && index_all_values && last_dim_component) // N-1 indices are integer types, last is component
192 || ((num_indices == N) && index_with_maybe_enum && last_dim_component); // N indices, last dim is component and can be enum
193 };
194
195 template <int N, bool last_dim_component, class... idx>
196 inline constexpr bool ArrayNDIndexCheck_impl_v = ArrayNDIndexCheck_impl<N, last_dim_component, idx...>::value;
197
198 template<std::size_t... idx>
199 constexpr auto make_oob_message_impl (std::index_sequence<idx...>) {
200 constexpr std::size_t N = sizeof...(idx);
201 constexpr char prefix[] = " (";
202 constexpr char middle[] = ") is out of bound (";
203 constexpr char suffix[] = ")\n";
204
205 constexpr std::size_t size =
206 sizeof(prefix) - 1 +
207 (2*N + (N-1)) +
208 sizeof(middle) - 1 +
209 (5*N + (N-1)) +
210 sizeof(suffix); // include null terminator
211
212 std::array<char, size> buf{};
213
214 std::size_t pos = 0;
215
216 // prefix
217 for (char c : prefix) {
218 if (c != '\0') {
219 buf[pos++] = c;
220 }
221 }
222 // %d,%d,...
223 ((buf[pos++] = '%', buf[pos++] = 'd', idx + 1 < N ? buf[pos++] = ',' : 0), ...);
224
225 // ") is out of bound ("
226 for (char c : middle) {
227 if (c != '\0') {
228 buf[pos++] = c;
229 }
230 }
231
232 // %d:%d,%d:%d,...
233 ((buf[pos++] = '%', buf[pos++] = 'd', buf[pos++] = ':', buf[pos++] = '%', buf[pos++] = 'd',
234 idx + 1 < N ? buf[pos++] = ',' : 0), ...);
235
236 // suffix
237 for (char c : suffix) {
238 buf[pos++] = c;
239 }
240 return buf;
241 }
242
243 template <std::size_t... idx, std::size_t... idx2x, std::size_t... ichar>
245 void device_print_impl2 (std::index_sequence<idx...>,
246 std::index_sequence<idx2x...>,
247 std::index_sequence<ichar...>,
248 IntVectND<sizeof...(idx)> const& iv,
249 IntVectND<sizeof...(idx)> const& begin,
250 IntVectND<sizeof...(idx)> const& end)
251 {
252 // SYCL printf needs a format it can put in constant memory: a static char array
253 static constexpr auto buf = make_oob_message_impl(std::index_sequence<idx...>{});
254 static constexpr char msg[] = {buf[ichar]...};
255
257 iv.vect[idx]...,
258 ((idx2x % 2 == 0) ? begin.vect[idx2x / 2] : end.vect[idx2x / 2] - 1)...
259 );
260 }
261
262 template <int N>
263 requires (N >= 1)
265 void device_printf_impl (const IntVectND<N>& iv,
266 const IntVectND<N>& begin,
267 const IntVectND<N>& end)
268 {
269 device_print_impl2(
270 std::make_index_sequence<N>{},
271 std::make_index_sequence<N*2>{},
272 std::make_index_sequence<make_oob_message_impl(std::make_index_sequence<N>{}).size()>{},
273 iv, begin, end
274 );
275 }
276 }
278
303 template<typename T, int N, bool last_dim_component = false>
304 struct ArrayND
305 {
306 static_assert(N >= 1, "ArrayND must have at least one dimension");
307 static_assert(N > 1 || !last_dim_component, "ArrayND with N=1 cannot have last_dim_component=true");
308
310 static constexpr bool IsLastDimComponent_v = last_dim_component;
312 static constexpr bool IsArray4_v = (N==4 && last_dim_component);
313
314 T* AMREX_RESTRICT p = nullptr;
316 AMREX_NO_UNIQUE_ADDRESS typename detail::StrideType<N,last_dim_component>::type stride{};
320
327 constexpr ArrayND () noexcept : p(nullptr) {}
328
337 template <class U=T>
338 requires (std::is_const_v<U>)
340 constexpr ArrayND (ArrayND<std::remove_const_t<T>, N, last_dim_component> const& rhs) noexcept
341 : p(rhs.p), stride(rhs.stride), begin(rhs.begin), end(rhs.end) {}
342
348 // TODO: Make BoxND functions constexpr to allow this constructor to be constexpr.
350 ArrayND (T* a_p, BoxND<N> const& box) noexcept
351 requires (!last_dim_component)
352 : ArrayND(a_p, box.smallEnd(), box.bigEnd() + 1)
353 {}
354
361 template <int M>
362 requires (((M+1==N) || (N == 4 && M == AMREX_SPACEDIM))
363 && last_dim_component)
364 // TODO: Make BoxND functions constexpr to allow this constructor to be constexpr.
366 ArrayND (T* a_p, BoxND<M> const& box, int ncomp) noexcept
367 : ArrayND(a_p, box.smallEnd(), box.bigEnd() + 1, ncomp)
368 {}
369
380 constexpr ArrayND (T* a_p, IntVectND<N> const& a_begin, IntVectND<N> const& a_end) noexcept
381 requires (!last_dim_component)
382 : p(a_p), begin(a_begin), end(a_end)
383 {
384 set_stride();
385 }
386
398 constexpr ArrayND (T* a_p, Dim3 const& a_begin, Dim3 const& a_end, int a_ncomp) noexcept
399 requires (IsArray4_v)
400 : p(a_p), begin(a_begin.x, a_begin.y, a_begin.z, 0), end(a_end.x, a_end.y, a_end.z, a_ncomp)
401 {
402 set_stride();
403 }
404
415 template <int M>
416 requires (((M+1 == N) || (N == 4 && M == AMREX_SPACEDIM)) && last_dim_component)
418 constexpr ArrayND (T* a_p, IntVectND<M> const& a_begin, IntVectND<M> const& a_end, int ncomp) noexcept
419 : p(a_p)
420 {
421 constexpr_for<0, M>([&](int d) {
422 begin.vect[d] = a_begin.vect[d];
423 end.vect[d] = a_end.vect[d];
424 });
425 constexpr_for<M, N>([&](int d) {
426 begin.vect[d] = 0;
427 end.vect[d] = 1;
428 });
429 end.vect[N-1] = ncomp;
430
431 set_stride();
432 }
433
442 template <class U>
443 requires (std::is_same_v<std::remove_const_t<T>, std::remove_const_t<U>>
444 && (N >= 2) && last_dim_component)
446 constexpr ArrayND (ArrayND<U, N, last_dim_component> const& rhs, int start_comp) noexcept
447 : p((T*)(rhs.p + start_comp*rhs.template get_stride<N-1>())),
448 stride(rhs.stride),
449 begin(rhs.begin),
450 end(rhs.end)
451 {
452 begin.vect[N-1] = 0;
453 end.vect[N-1] = rhs.end.vect[N-1] - start_comp;
454 }
455
465 template <class U>
466 requires (std::is_same_v<std::remove_const_t<T>, std::remove_const_t<U>>
467 && (N >= 2) && last_dim_component)
469 constexpr ArrayND (ArrayND<U, N, last_dim_component> const& rhs, int start_comp, int num_comp) noexcept
470 : p((T*)(rhs.p + start_comp*rhs.template get_stride<N-1>())),
471 stride(rhs.stride),
472 begin(rhs.begin),
473 end(rhs.end)
474 {
475 begin.vect[N-1] = 0;
476 end.vect[N-1] = num_comp;
477 }
478
484 constexpr explicit operator bool() const noexcept { return p != nullptr; }
485
491 [[nodiscard]] AMREX_GPU_HOST_DEVICE
492 constexpr bool ok () const noexcept { return p != nullptr && end.allGT(begin); }
493
510 template <typename... idx>
511 requires (!std::is_void_v<T> && !IsArray4_v
512 && detail::ArrayNDIndexCheck_impl_v<N, last_dim_component, idx...>)
514 T& operator() (idx... i) const noexcept {
515 constexpr auto nidx = sizeof...(i);
516#if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
517 index_assert(IntVectND<nidx>{i...});
518#endif
519 return p[get_offset(IntVectND<nidx>{i...})];
520 }
521
534 template <int M>
535 requires (!std::is_void_v<T> &&
536 ((M == N) || (!IsArray4_v && last_dim_component && (M + 1 == N))))
538 T& operator() (IntVectND<M> const& iv) const noexcept {
539#if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
540 index_assert(iv);
541#endif
542 return p[get_offset(iv)];
543 }
544
556 template <int M>
557 requires (!std::is_void_v<T> && last_dim_component && !IsArray4_v
558 && (M + 1 == N))
560 T& operator() (IntVectND<M> const& iv, int n) const noexcept {
561#if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
562 index_assert(iv, n);
563#endif
564 return p[get_offset(iv, n)];
565 }
566
573 template <typename... idx>
574 requires (!std::is_void_v<T> && !IsArray4_v
575 && detail::ArrayNDIndexCheck_impl_v<N, last_dim_component, idx...>)
577 T* ptr (idx... i) const noexcept {
578 constexpr auto nidx = sizeof...(i);
579#if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
580 index_assert(IntVectND<nidx>{i...});
581#endif
582 return p + get_offset(IntVectND<nidx>{i...});
583 }
584
597 template <int M>
598 requires ((M == N) || (!IsArray4_v && last_dim_component && (M + 1 == N)))
600 T* ptr (IntVectND<M> const& iv) const noexcept {
601#if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
602 index_assert(iv);
603#endif
604 return p + get_offset(iv);
605 }
606
618 template <int M>
619 requires (last_dim_component && !IsArray4_v && (M + 1 == N))
621 T* ptr (IntVectND<M> const& iv, int n) const noexcept {
622#if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
623 index_assert(iv, n);
624#endif
625 return p + get_offset(iv, n);
626 }
627
633 constexpr T* dataPtr () const noexcept {
634 return this->p;
635 }
636
642 constexpr int nComp () const noexcept {
643 if constexpr (last_dim_component) {
644 return end.vect[N-1];
645 } else {
646 return 1;
647 }
648 }
649
659 constexpr std::size_t size () const noexcept {
660 if (ok()) {
661 std::size_t s = 1;
662 constexpr_for<0, N>([&](int d) {
663 s *= (end.vect[d] - begin.vect[d]);
664 });
665 return s;
666 } else {
667 return 0;
668 }
669 }
670
681 template <int d>
682 requires ((d < N) && (d >= 0))
684 constexpr Long get_stride () const noexcept {
685 if constexpr (IsArray4_v && d == 3) {
686 return stride.n;
687 } else if constexpr (N > 1 && d > 0) {
688 return stride.a[d-1];
689 } else {
690 return 1;
691 }
692 }
693
705 template <typename... idx>
706 requires (!IsArray4_v &&
707 detail::ArrayNDIndexCheck_impl_v<N, last_dim_component, idx...>)
709 constexpr bool contains (idx... i) const noexcept {
710 constexpr auto nidx = sizeof...(i);
711 return this->contains(IntVectND<nidx>{i...});
712 }
713
730 template <int M>
731 requires ((M == N) || (!IsArray4_v && last_dim_component && (M + 1 == N)))
733 constexpr bool contains (IntVectND<M> const& iv) const noexcept {
734 bool inside = true;
735 constexpr_for<0, M>([&](int d) {
736 inside = inside && (iv.vect[d] >= begin.vect[d]) && (iv.vect[d] < end.vect[d]);
737 });
738 return inside;
739 }
740
757 template <int M>
758 requires (last_dim_component && !IsArray4_v && (M + 1 == N))
760 constexpr bool contains (IntVectND<M> const& iv, int n) const noexcept {
761 bool inside = true;
762 constexpr_for<0, M>([&](int d) {
763 inside = inside && (iv.vect[d] >= begin.vect[d]) && (iv.vect[d] < end.vect[d]);
764 });
765 inside = inside && (n >= 0) && (n < end.vect[N-1]);
766 return inside;
767 }
768
786 CellData<T> cellData (int i, int j, int k) const noexcept
787 requires (IsArray4_v)
788
789 {
790 int ncomp = end.vect[N-1];
791 return CellData<T>(this->ptr(i,j,k), get_stride<N-1>(), ncomp);
792 }
793
801 template <int M>
802 requires ((M == N) || (last_dim_component && (M + 1 == N || M == AMREX_SPACEDIM)))
804 constexpr Long get_offset (IntVectND<M> const& iv) const noexcept
805 {
806 Long offset = iv.vect[0] - begin.vect[0];
807 // If M == N and we have a component at the end, we only loop up to N-1 for spatial.
808 // Otherwise, we loop up to M.
809 constexpr int idx = (last_dim_component && M == N) ? N - 1 : M;
810 if constexpr (N > 1) {
811 constexpr_for<1, idx>([&](int d) {
812 offset += Long(iv.vect[d] - begin.vect[d]) * stride.a[d-1];
813 });
814 }
815
816 // Handle the component offset if the input is the full N dimensions
817 // and the last dimension is a component.
818 if constexpr (last_dim_component && M == N) {
819 offset += iv.vect[N-1] * get_stride<N-1>();
820 }
821 return offset;
822 }
823
833 template <int M>
834 requires (last_dim_component
835 && ((M + 1 == N) || (N == 4 && M == AMREX_SPACEDIM)))
837 constexpr Long get_offset (IntVectND<M> const& iv, int n) const noexcept
838 {
839 Long offset = iv.vect[0] - begin.vect[0];
840 constexpr_for<1, M>([&](int d) {
841 offset += Long(iv.vect[d] - begin.vect[d]) * stride.a[d-1];
842 });
843 offset += n * get_stride<N-1>();
844 return offset;
845 }
846
847#if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
848#if defined(AMREX_USE_HIP)
850#else
852#endif
853 void index_assert (IntVectND<N> const& iv) const
854 {
855 bool out_of_bounds = false;
856 for (int d = 0; d < N; ++d) {
857 if (iv.vect[d] < begin.vect[d] || iv.vect[d] >= end.vect[d]) {
858 out_of_bounds = true;
859 }
860 }
861 if (out_of_bounds) {
863 detail::device_printf_impl(iv, begin, end);
864 amrex::Abort();
865 ))
867 std::stringstream ss;
868 ss << " (";
869 for (int d = 0; d < N; ++d) {
870 ss << iv.vect[d];
871 if (d + 1 < N) ss << ",";
872 }
873 ss << ") is out of bound (";
874 for (int d = 0; d < N; ++d) {
875 ss << begin.vect[d] << ":" << end.vect[d]-1;
876 if (d + 1 < N) ss << ",";
877 };
878 ss << ")";
879 amrex::Abort(ss.str());
880 ))
881 }
882 }
883
884 // index_assert overload for M == N-1 last index assumed 0
885 // Only valid when last_dim_component == true
886 template <int M>
887 requires (((M+1 == N) || (M == AMREX_SPACEDIM)) && last_dim_component)
888#if defined(AMREX_USE_HIP)
890#else
892#endif
893 void index_assert (IntVectND<M> const& iv) const
894 {
895 IntVectND<N> iv_full = iv.template expand<N>(0);
896 for (int d = M; d < N; ++d) {
897 iv_full.vect[d] = begin.vect[d];
898 }
899 index_assert(iv_full);
900 }
901
902 // index_assert overload for M == N-1 and last index n
903 // Only valid when last_dim_component == true
904 template <int M>
905 requires (((M+1 == N) || (M == AMREX_SPACEDIM)) && last_dim_component)
906#if defined(AMREX_USE_HIP)
908#else
910#endif
911 void index_assert (IntVectND<M> const& iv, int n) const
912 {
913 IntVectND<N> iv_full = iv.template expand<N>(0);
914 for (int d = M; d < N-1; ++d) {
915 iv_full.vect[d] = begin.vect[d];
916 }
917 iv_full.vect[N-1] = n;
918 index_assert(iv_full);
919 }
920#endif
921
922 //
923 // Specialization for Array4
924 //
925
936 constexpr Long offset4 (int i, int j, int k, int n) const noexcept
937 requires (IsArray4_v)
938 {
939#if defined(AMREX_USE_GPU) || defined(AMREX_DEBUG)
940 return (i-begin.vect[0]) +
941 (j-begin.vect[1]) * stride.a[0] +
942 (k-begin.vect[2]) * stride.a[1] +
943 n * stride.n;
944#else
945 Long idx1 = i + j*stride.a[0] + k*stride.a[1] + n*stride.n;
946 Long idx0 = begin.vect[0]
947 + begin.vect[1] * stride.a[0]
948 + begin.vect[2] * stride.a[1];
949 return idx1-idx0;
950#endif
951 }
952
971 T& operator() (int i, int j, int k) const noexcept
972 requires (!std::is_void_v<T> && IsArray4_v)
973
974 {
975#if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
976 AMREX_ARRAY4_INDEX_ASSERT(i,j,k,0);
977#endif
978 return p[offset4(i,j,k,0)];
979 }
980
997 T& operator() (int i, int j, int k, int n) const noexcept
998 requires (!std::is_void_v<T> && IsArray4_v)
999
1000 {
1001#if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
1002 AMREX_ARRAY4_INDEX_ASSERT(i,j,k,n);
1003#endif
1004 return p[offset4(i,j,k,n)];
1005 }
1006
1016 template <int M>
1017 requires (!std::is_void_v<T> && IsArray4_v && (M == 3 || M == AMREX_SPACEDIM))
1019 T& operator() (IntVectND<M> const& iv) const noexcept {
1020#if (AMREX_SPACEDIM == 1)
1021 if constexpr (M == 1) {
1022 return this->operator()(iv.vect[0],0,0);
1023 } else
1024#elif (AMREX_SPACEDIM == 2)
1025 if constexpr (M == 2) {
1026 return this->operator()(iv.vect[0],iv.vect[1],0);
1027 } else
1028#endif
1029 {
1030 return this->operator()(iv.vect[0],iv.vect[1],iv.vect[2]);
1031 }
1032 }
1033
1044 template <int M>
1045 requires (!std::is_void_v<T> && IsArray4_v && (M == 3 || M == AMREX_SPACEDIM))
1047 T& operator() (IntVectND<M> const& iv, int n) const noexcept {
1048#if (AMREX_SPACEDIM == 1)
1049 if constexpr (M == 1) {
1050 return this->operator()(iv.vect[0],0,0,n);
1051 } else
1052#elif (AMREX_SPACEDIM == 2)
1053 if constexpr (M == 2) {
1054 return this->operator()(iv.vect[0],iv.vect[1],0,n);
1055 } else
1056#endif
1057 {
1058 return this->operator()(iv.vect[0],iv.vect[1],iv.vect[2],n);
1059 }
1060 }
1061
1070 T& operator() (Dim3 const& cell) const noexcept
1071 requires (!std::is_void_v<T> && IsArray4_v)
1072
1073 {
1074 return this->operator()(cell.x,cell.y,cell.z);
1075 }
1076
1085 T& operator() (Dim3 const& cell, int n) const noexcept
1086 requires (!std::is_void_v<T> && IsArray4_v)
1087
1088 {
1089 return this->operator()(cell.x,cell.y,cell.z,n);
1090 }
1091
1101 T* ptr (int i, int j, int k) const noexcept
1102 requires (!std::is_void_v<T> && IsArray4_v)
1103
1104 {
1105#if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
1106 AMREX_ARRAY4_INDEX_ASSERT(i,j,k,0);
1107#endif
1108 return p + offset4(i,j,k,0);
1109 }
1110
1121 T* ptr (int i, int j, int k, int n) const noexcept
1122 requires (!std::is_void_v<T> && IsArray4_v)
1123
1124 {
1125#if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
1126 AMREX_ARRAY4_INDEX_ASSERT(i,j,k,n);
1127#endif
1128 return p + offset4(i,j,k,n);
1129 }
1130
1137 template <int M>
1138 requires (!std::is_void_v<T> && IsArray4_v && (M == 3 || M == AMREX_SPACEDIM))
1140 T* ptr (IntVectND<M> const& iv) const noexcept {
1141#if (AMREX_SPACEDIM == 1)
1142 if constexpr (M == 1) {
1143 return this->ptr(iv.vect[0],0,0);
1144 } else
1145#elif (AMREX_SPACEDIM == 2)
1146 if constexpr (M == 2) {
1147 return this->ptr(iv.vect[0],iv.vect[1],0);
1148 } else
1149#endif
1150 {
1151 return this->ptr(iv.vect[0],iv.vect[1],iv.vect[2]);
1152 }
1153 }
1154
1162 template <int M>
1163 requires (!std::is_void_v<T> && IsArray4_v && (M == 3 || M == AMREX_SPACEDIM))
1165 T* ptr (IntVectND<M> const& iv, int n) const noexcept {
1166#if (AMREX_SPACEDIM == 1)
1167 if constexpr (M == 1) {
1168 return this->ptr(iv.vect[0],0,0,n);
1169 } else
1170#elif (AMREX_SPACEDIM == 2)
1171 if constexpr (M == 2) {
1172 return this->ptr(iv.vect[0],iv.vect[1],0,n);
1173 } else
1174#endif
1175 {
1176 return this->ptr(iv.vect[0],iv.vect[1],iv.vect[2],n);
1177 }
1178 }
1179
1187 T* ptr (Dim3 const& cell) const noexcept
1188 requires (!std::is_void_v<T> && IsArray4_v)
1189
1190 {
1191 return this->ptr(cell.x,cell.y,cell.z);
1192 }
1193
1202 T* ptr (Dim3 const& cell, int n) const noexcept
1203 requires (!std::is_void_v<T> && IsArray4_v)
1204
1205 {
1206 return this->ptr(cell.x,cell.y,cell.z,n);
1207 }
1208
1218 bool contains (int i, int j, int k) const noexcept
1219 requires (IsArray4_v)
1220
1221 {
1222 return (i>=begin.vect[0] && i<end.vect[0] &&
1223 j>=begin.vect[1] && j<end.vect[1] &&
1224 k>=begin.vect[2] && k<end.vect[2]);
1225 }
1226
1233 template <int M>
1234 requires (IsArray4_v && (M == 3 || M == AMREX_SPACEDIM))
1236 bool contains (IntVectND<M> const& iv) const noexcept {
1237#if (AMREX_SPACEDIM < 3)
1238 if constexpr (M == AMREX_SPACEDIM) {
1239 return AMREX_D_TERM((iv.vect[0]>=begin.vect[0]) && (iv.vect[0]<end.vect[0]),
1240 && (iv.vect[1]>=begin.vect[1]) && (iv.vect[1]<end.vect[1]),
1241 && (iv.vect[2]>=begin.vect[2]) && (iv.vect[2]<end.vect[2]));
1242 } else
1243#endif
1244 {
1245 return (iv.vect[0]>=begin.vect[0]) && (iv.vect[0]<end.vect[0])
1246 && (iv.vect[1]>=begin.vect[1]) && (iv.vect[1]<end.vect[1])
1247 && (iv.vect[2]>=begin.vect[2]) && (iv.vect[2]<end.vect[2]);
1248 }
1249 }
1250
1258 bool contains (Dim3 const& cell) const noexcept
1259 requires (IsArray4_v)
1260
1261 {
1262 return (cell.x>=begin.vect[0] && cell.x<end.vect[0] &&
1263 cell.y>=begin.vect[1] && cell.y<end.vect[1] &&
1264 cell.z>=begin.vect[2] && cell.z<end.vect[2]);
1265 }
1266
1267#if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
1268#if defined(AMREX_USE_HIP)
1270#else
1272#endif
1273 void index_assert_print_error_message (int i, int j, int k, int n) const
1274 requires (IsArray4_v)
1275
1276 {
1278 AMREX_DEVICE_PRINTF(" (%d,%d,%d,%d) is out of bound (%d:%d,%d:%d,%d:%d,0:%d)\n",
1279 i, j, k, n,
1280 begin.vect[0], end.vect[0]-1,
1281 begin.vect[1], end.vect[1]-1,
1282 begin.vect[2], end.vect[2]-1,
1283 end.vect[3]-1);
1284 amrex::Abort();
1285 ))
1287 std::stringstream ss;
1288 ss << " (" << i << "," << j << "," << k << "," << n
1289 << ") is out of bound ("
1290 << begin.vect[0] << ":" << end.vect[0]-1 << ","
1291 << begin.vect[1] << ":" << end.vect[1]-1 << ","
1292 << begin.vect[2] << ":" << end.vect[2]-1 << ","
1293 << "0:" << end.vect[3]-1 << ")";
1294 amrex::Abort(ss.str());
1295 ))
1296 }
1297#endif
1298
1299 private:
1301 constexpr void set_stride () noexcept {
1302 if constexpr (IsArray4_v) {
1303 Long const nx = end.vect[0] - begin.vect[0];
1304 Long const nxy = nx * (end.vect[1] - begin.vect[1]);
1305 stride.a[0] = detail::Array4Int(nx);
1306 stride.a[1] = detail::Array4Int(nxy);
1307 stride.n = nxy * (end.vect[2] - begin.vect[2]);
1308 if constexpr (std::is_same_v<detail::Array4Int,int>) {
1309 AMREX_ASSERT(stride.n <= Long(std::numeric_limits<int>::max()));
1310 }
1311 } else if constexpr (N > 1) {
1312 Long current_stride = 1;
1313 constexpr_for<0, N-1>([&](int d) {
1314 Long len = end.vect[d] - begin.vect[d];
1315 current_stride *= len;
1316 stride.a[d] = current_stride;
1317 });
1318 }
1319 }
1320 };
1321
1323 // Deduction guides for ArrayND
1324 // 1: Matches ArrayND (T*, BoxND<N>) -> ArrayND<T,N, last_dim_component=false>
1325 template <typename T, int N>
1326 ArrayND (T*, BoxND<N> const&) -> ArrayND<T, N, false>;
1327
1328 // 2: Matches ArrayND (T*, BoxND<N>, ncomp) -> ArrayND<T,N+1, last_dim_component=true>
1329 // This supports the "N+1" logic (Spatial + Component)
1330 template <typename T, int N>
1331 ArrayND (T*, BoxND<N> const&, int) -> ArrayND<T, N+1, true>;
1332
1333 // 3: Matches ArrayND (T*, IntVectND<N>, IntVectND<N>) -> ArrayND<T,N, last_dim_component=false>
1334 template <typename T, int N>
1335 ArrayND (T*, IntVectND<N> const&, IntVectND<N> const&) -> ArrayND<T, N, false>;
1336
1337 // 4: Matches ArrayND (T*, IntVectND<N>, IntVectND<N>, int) -> ArrayND<T,N+1, last_dim_component=true>
1338 template <typename T, int N>
1339 ArrayND (T*, IntVectND<N> const&, IntVectND<N> const&, int) -> ArrayND<T, N+1, true>;
1340
1341 // 5: Matches ArrayND (T*, Dim3, Dim3) -> ArrayND<T,4, last_dim_component=true>
1342 template <typename T>
1343 ArrayND (T*, Dim3 const&, Dim3 const&, int) -> ArrayND<T, 4, true>;
1345
1351 template<typename T>
1353
1361 template <class T>
1363 Dim3 lbound (Array4<T> const& a) noexcept
1364 {
1365 return Dim3{.x = a.begin.vect[0], .y = a.begin.vect[1], .z = a.begin.vect[2]};
1366 }
1367
1375 template <class T>
1377 Dim3 ubound (Array4<T> const& a) noexcept
1378 {
1379 return Dim3{.x = a.end.vect[0]-1, .y = a.end.vect[1]-1, .z = a.end.vect[2]-1};
1380 }
1381
1389 template <class T>
1391 Dim3 length (Array4<T> const& a) noexcept
1392 {
1393 return Dim3{.x = a.end.vect[0]-a.begin.vect[0],
1394 .y = a.end.vect[1]-a.begin.vect[1],
1395 .z = a.end.vect[2]-a.begin.vect[2]};
1396 }
1397
1409 template <typename T, int N, bool C>
1410 std::ostream& operator<< (std::ostream& os, const ArrayND<T,N,C>& a) {
1411 os << "(" << a.begin << ',' << a.end-1 << ")";
1412 return os;
1413 }
1414
1416 //
1417 // Type traits for detecting if a class has a size() constexpr function.
1418 //
1419 template <class A> struct HasMultiComp : std::false_type {};
1420 //
1421 template <class B>
1422 requires (B().size() >= 1)
1423 struct HasMultiComp<B> : std::true_type {};
1425
1438 template <typename T>
1440 : public Array4<T>
1441 {
1444 : Array4<T>{a} {}
1445
1447 T& operator() (int i, int j, int k) const noexcept {
1448 return this->Array4<T>::operator()(i,j,k);
1449 }
1450
1452 typename T::reference_type
1453 operator() (int i, int j, int k, int n) const noexcept
1454 requires (amrex::HasMultiComp<T>::value)
1455
1456 {
1457 return this->Array4<T>::operator()(i,j,k,0)[n];
1458 }
1459
1461 T& operator() (int i, int j, int k, int n) const noexcept
1462 requires (!amrex::HasMultiComp<T>::value)
1463
1464 {
1465 return this->Array4<T>::operator()(i,j,k,n);
1466 }
1467 };
1468
1471 template <typename T>
1472 [[nodiscard]] PolymorphicArray4<T>
1474 {
1475 return PolymorphicArray4<T>(a);
1476 }
1477}
1478
1479#endif
Runtime initialization/finalization helpers and global diagnostics.
#define AMREX_ASSERT(EX)
Definition AMReX_BLassert.H:38
Compile-time unrolled loop utility.
#define AMREX_NO_UNIQUE_ADDRESS
Definition AMReX_Extension.H:306
#define AMREX_NO_INLINE
Definition AMReX_Extension.H:140
#define AMREX_FORCE_INLINE
Definition AMReX_Extension.H:123
#define AMREX_RESTRICT
Definition AMReX_Extension.H:37
#define AMREX_DEVICE_PRINTF(...)
Definition AMReX_GpuPrint.H:15
#define AMREX_IF_ON_DEVICE(CODE)
Definition AMReX_GpuQualifiers.H:56
#define AMREX_IF_ON_HOST(CODE)
Definition AMReX_GpuQualifiers.H:58
#define AMREX_GPU_HOST_DEVICE
Definition AMReX_GpuQualifiers.H:20
while parser_exe_t p
Definition AMReX_Parser_Exe_Body.H:33
#define AMREX_D_TERM(a, b, c)
Definition AMReX_SPACE.H:172
A Rectangular Domain on an Integer Lattice.
Definition AMReX_Box.H:54
An Integer Vector in dim-Dimensional Space.
Definition AMReX_IntVect.H:156
int vect[dim]
Definition AMReX_IntVect.H:914
amrex_long Long
Definition AMReX_INT.H:30
__host__ __device__ Dim3 ubound(Array4< T > const &a) noexcept
Return the inclusive upper bounds of an Array4 in Dim3 form.
Definition AMReX_Array4.H:1377
__host__ __device__ Dim3 length(Array4< T > const &a) noexcept
Return the spatial extents of an Array4 in Dim3 form.
Definition AMReX_Array4.H:1391
PolymorphicArray4< T > makePolymorphic(Array4< T > const &a)
Definition AMReX_Array4.H:1473
ArrayND< T, 4, true > Array4
Convenient alias for a 4D ArrayND with three spatial dimensions plus a component dimension.
Definition AMReX_Array4.H:1352
__host__ __device__ Dim3 lbound(Array4< T > const &a) noexcept
Return the inclusive lower bounds of an Array4 in Dim3 form.
Definition AMReX_Array4.H:1363
__host__ __device__ constexpr void constexpr_for(F const &f)
Compile-time unrolled loop from Idx (inclusive) to N (exclusive).
Definition AMReX_ConstexprFor.H:38
Definition AMReX_Amr.cpp:50
__host__ __device__ Dim3 begin(BoxND< dim > const &box) noexcept
Return the iterator begin coordinate of box as Dim3.
Definition AMReX_Box.H:2241
IntVectND(const Array< int, dim > &) -> IntVectND< dim >
@ y
Definition AMReX_Orientation.H:14
@ x
Definition AMReX_Orientation.H:14
@ z
Definition AMReX_Orientation.H:14
void Abort(const std::string &msg)
Print a fatal-error message to stderr and abort execution.
Definition AMReX.cpp:239
const int[]
Definition AMReX_BLProfiler.cpp:1665
__host__ __device__ Dim3 end(BoxND< dim > const &box) noexcept
Return the iterator end coordinate of box as Dim3.
Definition AMReX_Box.H:2259
std::ostream & operator<<(std::ostream &os, AmrMesh const &amr_mesh)
Stream helper; forwards to the friend declared inside AmrMesh.
Definition AMReX_AmrMesh.cpp:1379
A multidimensional array accessor.
Definition AMReX_Array4.H:305
__host__ __device__ T * ptr(int i, int j, int k) const noexcept
Return the pointer to an element.
Definition AMReX_Array4.H:1101
static constexpr bool IsArray4_v
Definition AMReX_Array4.H:312
__host__ __device__ ArrayND(T *a_p, BoxND< M > const &box, int ncomp) noexcept
Constructor using a BoxND and the number of components.
Definition AMReX_Array4.H:366
__host__ __device__ constexpr bool ok() const noexcept
Check if the ArrayND pointer is valid and bounds are valid.
Definition AMReX_Array4.H:492
__host__ __device__ T & operator()(idx... i) const noexcept
Multi-index operator() for accessing elements.
Definition AMReX_Array4.H:514
__host__ __device__ ArrayND(T *a_p, BoxND< N > const &box) noexcept
Constructor using a BoxND.
Definition AMReX_Array4.H:350
__host__ __device__ constexpr std::size_t size() const noexcept
Total number of elements in the ArrayND's index region.
Definition AMReX_Array4.H:659
T *__restrict__ p
Definition AMReX_Array4.H:314
__host__ __device__ constexpr ArrayND(T *a_p, IntVectND< N > const &a_begin, IntVectND< N > const &a_end) noexcept
IntVectND<N> constructor.
Definition AMReX_Array4.H:380
__host__ __device__ constexpr bool contains(idx... i) const noexcept
Definition AMReX_Array4.H:709
__host__ __device__ T * ptr(Dim3 const &cell, int n) const noexcept
Return the pointer to an element.
Definition AMReX_Array4.H:1202
static constexpr bool IsLastDimComponent_v
Definition AMReX_Array4.H:310
__host__ __device__ constexpr int nComp() const noexcept
Get number of components.
Definition AMReX_Array4.H:642
__host__ __device__ constexpr T * dataPtr() const noexcept
Get raw data pointer.
Definition AMReX_Array4.H:633
__host__ __device__ T * ptr(idx... i) const noexcept
Definition AMReX_Array4.H:577
__host__ __device__ T * ptr(int i, int j, int k, int n) const noexcept
Return the pointer to an element.
Definition AMReX_Array4.H:1121
IntVectND< N > end
Definition AMReX_Array4.H:319
IntVectND< N > begin
Definition AMReX_Array4.H:318
__host__ __device__ CellData< T > cellData(int i, int j, int k) const noexcept
Create a single-cell component accessor.
Definition AMReX_Array4.H:786
__host__ __device__ constexpr ArrayND(ArrayND< std::remove_const_t< T >, N, last_dim_component > const &rhs) noexcept
Construct a const accessor from a non-const accessor.
Definition AMReX_Array4.H:340
__host__ __device__ constexpr Long get_offset(IntVectND< M > const &iv) const noexcept
Compute the linear offset (in elements) for an IntVectND.
Definition AMReX_Array4.H:804
__host__ __device__ T * ptr(Dim3 const &cell) const noexcept
Return the pointer to an element.
Definition AMReX_Array4.H:1187
__host__ __device__ constexpr Long get_stride() const noexcept
Definition AMReX_Array4.H:684
__host__ __device__ constexpr Long offset4(int i, int j, int k, int n) const noexcept
Offset of element (i,j,k,n) from p.
Definition AMReX_Array4.H:936
__host__ __device__ constexpr ArrayND() noexcept
Default-construct an empty accessor.
Definition AMReX_Array4.H:327
__host__ __device__ constexpr ArrayND(T *a_p, Dim3 const &a_begin, Dim3 const &a_end, int a_ncomp) noexcept
Constructor for N=4 using Dim3.
Definition AMReX_Array4.H:398
__host__ __device__ bool contains(Dim3 const &cell) const noexcept
Test whether the spatial indices are inside the Array4 bounds.
Definition AMReX_Array4.H:1258
__host__ __device__ bool contains(int i, int j, int k) const noexcept
Test whether the spatial indices are inside the Array4 bounds.
Definition AMReX_Array4.H:1218
Lightweight accessor for data associated with a single cell.
Definition AMReX_Array4.H:47
__host__ __device__ constexpr CellData(T *a_p, Long a_stride, int a_ncomp)
Construct a CellData.
Definition AMReX_Array4.H:64
__host__ __device__ constexpr CellData(CellData< std::remove_const_t< T > > const &rhs) noexcept
Construct a const CellData from a non-const CellData.
Definition AMReX_Array4.H:79
__host__ __device__ T & operator[](int n) const
Access the n-th component of the cell.
Definition AMReX_Array4.H:106
__host__ __device__ int nComp() const noexcept
Return the number of components referenced by this accessor.
Definition AMReX_Array4.H:93
A simple struct holding 3 int values for a 3D index.
Definition AMReX_Dim3.H:24
Array4 subclass that provides uniform (i,j,k,n) access for both AoS and SoA data.
Definition AMReX_Array4.H:1441
__host__ __device__ PolymorphicArray4(Array4< T > const &a)
Definition AMReX_Array4.H:1443