1 #ifndef AMREX_TABLE_DATA_H_
2 #define AMREX_TABLE_DATA_H_
3 #include <AMReX_Config.H>
14 #include <type_traits>
27 template <class U=T, std::enable_if_t<std::is_const_v<U>,
int> = 0>
36 constexpr
Table1D (T* a_p,
int a_begin,
int a_end) noexcept
43 explicit operator bool () const noexcept {
return p !=
nullptr; }
45 template <
class U=T, std::enable_if_t<!std::is_
void_v<U>,
int> = 0>
48 #if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
54 #if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
56 void index_assert (
int i)
const
58 if (i < begin || i >=
end) {
66 ss << " (" << i << ") is out of bound ("
75 template <
typename T,
typename ORDER = Order::F>
85 template <class U=T, std::enable_if_t<std::is_const_v<U>,
int> = 0>
87 constexpr
Table2D (
Table2D<std::remove_const_t<T>, ORDER> const& rhs) noexcept
99 stride1(len0(a_begin,a_end)),
105 explicit operator bool () const noexcept {
return p !=
nullptr; }
107 template <
class U=T, std::enable_if_t<!std::is_
void_v<U>,
int> = 0>
110 #if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
113 static_assert(std::is_same_v<ORDER,Order::F> ||
114 std::is_same_v<ORDER,Order::C>);
115 if constexpr (std::is_same_v<ORDER,Order::F>) {
122 #if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
124 void index_assert (
int i,
int j)
const
134 std::stringstream ss;
135 ss << " (" << i << "," << j << ") is out of bound ("
137 << "," <<
begin[1] << ":" <<
end[1]-1 << ")";
149 if constexpr (std::is_same_v<ORDER,Order::F>) {
150 return a_end[0] - a_begin[0];
152 return a_end[1] - a_begin[1];
157 template <
typename T,
typename ORDER = Order::F>
168 template <class U=T, std::enable_if_t<std::is_const_v<U>,
int> = 0>
172 stride1(rhs.stride1),
173 stride2(rhs.stride2),
183 stride1( len0(a_begin,a_end)),
184 stride2(stride1*len1(a_begin,a_end)),
190 explicit operator bool () const noexcept {
return p !=
nullptr; }
192 template <
class U=T, std::enable_if_t<!std::is_
void_v<U>,
int> = 0>
195 #if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
198 static_assert(std::is_same_v<ORDER,Order::F> ||
199 std::is_same_v<ORDER,Order::C>);
200 if constexpr (std::is_same_v<ORDER,Order::F>) {
207 #if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
209 void index_assert (
int i,
int j,
int k)
const
221 std::stringstream ss;
222 ss << " (" << i << "," << j << "," << k << ") is out of bound ("
224 << "," <<
begin[1] << ":" <<
end[1]-1
225 << "," <<
begin[2] << ":" <<
end[2]-1 << ")";
237 if constexpr (std::is_same_v<ORDER,Order::F>) {
238 return a_end[0] - a_begin[0];
240 return a_end[2] - a_begin[2];
247 return a_end[1] - a_begin[1];
251 template <
typename T,
typename ORDER = Order::F>
263 template <class U=T, std::enable_if_t<std::is_const_v<U>,
int> = 0>
267 stride1(rhs.stride1),
268 stride2(rhs.stride2),
269 stride3(rhs.stride3),
279 stride1( len0(a_begin,a_end)),
280 stride2(stride1*len1(a_begin,a_end)),
281 stride3(stride2*len2(a_begin,a_end)),
287 explicit operator bool () const noexcept {
return p !=
nullptr; }
289 template <
class U=T, std::enable_if_t<!std::is_
void_v<U>,
int> = 0>
292 #if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
293 index_assert(i,j,k,n);
295 static_assert(std::is_same_v<ORDER,Order::F> ||
296 std::is_same_v<ORDER,Order::C>);
297 if constexpr (std::is_same_v<ORDER,Order::F>) {
304 #if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
306 void index_assert (
int i,
int j,
int k,
int n)
const
319 std::stringstream ss;
320 ss << " (" << i << "," << j << "," << k << "," << n << ") is out of bound ("
322 << "," <<
begin[1] << ":" <<
end[1]-1
323 << "," <<
begin[2] << ":" <<
end[2]-1
324 << "," <<
begin[3] << ":" <<
end[3]-1 << ")";
336 if constexpr (std::is_same_v<ORDER,Order::F>) {
337 return a_end[0] - a_begin[0];
339 return a_end[3] - a_begin[3];
346 if constexpr (std::is_same_v<ORDER,Order::F>) {
347 return a_end[1] - a_begin[1];
349 return a_end[2] - a_begin[2];
356 if constexpr (std::is_same_v<ORDER,Order::F>) {
357 return a_end[2] - a_begin[2];
359 return a_end[1] - a_begin[1];
402 template <
typename T,
int N,
typename ORDER = Order::F>
408 template <
class U,
int M,
class O>
friend class TableData;
411 std::conditional_t<N==2, Table2D<T, ORDER>,
412 std::conditional_t<N==3, Table3D<T, ORDER>,
415 std::conditional_t<N==2, Table2D<T const, ORDER>,
416 std::conditional_t<N==3, Table3D<T const, ORDER>,
433 [[nodiscard]] constexpr
int dim () const noexcept {
return N; }
437 [[nodiscard]] Long
size () const noexcept;
439 Array<
int,N> const& lo () const noexcept {
return m_lo; }
443 void clear () noexcept;
447 table_type table () noexcept;
448 const_table_type table () const noexcept;
449 const_table_type const_table () const noexcept;
458 Long m_truesize = 0L;
459 bool m_ptr_owner = false;
462 template <typename T,
int N, typename ORDER>
470 template <
typename T,
int N,
typename ORDER>
476 m_truesize(rhs.m_truesize),
477 m_ptr_owner(rhs.m_ptr_owner)
479 rhs.m_dptr =
nullptr;
480 rhs.m_ptr_owner =
false;
483 template <
typename T,
int N,
typename ORDER>
484 TableData<T,N,ORDER>&
489 m_arena = rhs.m_arena;
493 m_truesize = rhs.m_truesize;
494 m_ptr_owner = rhs.m_ptr_owner;
495 rhs.m_dptr =
nullptr;
496 rhs.m_ptr_owner =
false;
501 template <
typename T,
int N,
typename ORDER>
504 static_assert(std::is_trivially_copyable<T>() &&
505 std::is_trivially_destructible<T>(),
506 "TableData<T,N,ORDER>: T must be trivially copyable and trivially destructible");
507 static_assert(N>=1 && N <=4,
"TableData<T,N,ORDER>: N must be in the range of [1,4]");
508 static_assert(std::is_same_v<ORDER,Order::F> ||
509 std::is_same_v<ORDER,Order::C>,
510 "TableDat<T,N,ORDER>: ORDER must be either Order::F or Order::C");
514 template <
typename T,
int N,
typename ORDER>
529 }
else if (m_dptr ==
nullptr || !m_ptr_owner) {
532 }
else if (
size() > m_truesize) {
538 template <
typename T,
int N,
typename ORDER>
543 for (
int i = 0; i < N; ++i) {
544 r *= m_hi[i] - m_lo[i] + 1;
549 template <
typename T,
int N,
typename ORDER>
562 template <
typename T,
int N,
typename ORDER>
568 if (m_truesize == 0) {
572 m_dptr =
static_cast<T*
>(this->alloc(m_truesize*
sizeof(T)));
577 template <
typename T,
typename>
581 template <
typename T,
typename ORDER>
585 template <
typename T,
typename ORDER>
589 template <
typename T,
typename ORDER>
591 return Table4D<T,ORDER>(
p, {lo[0],lo[1],lo[2],lo[3]}, {hi[0]+1,hi[1]+1,hi[2]+1,hi[3]+1});
595 template <
typename T,
int N,
typename ORDER>
599 return detail::make_table<T,ORDER>(m_dptr, m_lo, m_hi);
602 template <
typename T,
int N,
typename ORDER>
606 return detail::make_table<T const, ORDER>(m_dptr, m_lo, m_hi);
609 template <
typename T,
int N,
typename ORDER>
613 return detail::make_table<T const, ORDER>(m_dptr, m_lo, m_hi);
616 template <
typename T,
int N,
typename ORDER>
620 std::size_t count =
sizeof(T)*
size();
622 bool this_on_device = arena()->isManaged() || arena()->isDevice();
623 bool rhs_on_device = rhs.arena()->isManaged() || rhs.arena()->isDevice();
624 if (this_on_device && rhs_on_device) {
626 }
else if (this_on_device && !rhs_on_device) {
628 }
else if (!this_on_device && rhs_on_device) {
#define AMREX_ASSERT(EX)
Definition: AMReX_BLassert.H:38
#define AMREX_FORCE_INLINE
Definition: AMReX_Extension.H:119
#define AMREX_RESTRICT
Definition: AMReX_Extension.H:37
#define AMREX_DEVICE_PRINTF(...)
Definition: AMReX_GpuPrint.H:21
#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
A virtual base class for objects that manage their own dynamic memory allocation.
Definition: AMReX_Arena.H:100
Multi-dimensional array class.
Definition: AMReX_TableData.H:405
std::conditional_t< N==1, Table1D< T >, std::conditional_t< N==2, Table2D< T, ORDER >, std::conditional_t< N==3, Table3D< T, ORDER >, Table4D< T, ORDER > > > > table_type
Definition: AMReX_TableData.H:413
TableData() noexcept=default
const_table_type const_table() const noexcept
Definition: AMReX_TableData.H:611
void resize(Array< int, N > const &lo, Array< int, N > const &hi, Arena *ar=nullptr)
Definition: AMReX_TableData.H:516
friend class TableData
Definition: AMReX_TableData.H:408
void copy(TableData< T, N, ORDER > const &rhs) noexcept
Definition: AMReX_TableData.H:618
std::conditional_t< N==1, Table1D< T const >, std::conditional_t< N==2, Table2D< T const, ORDER >, std::conditional_t< N==3, Table3D< T const, ORDER >, Table4D< T const, ORDER > > > > const_table_type
Definition: AMReX_TableData.H:417
Long size() const noexcept
Definition: AMReX_TableData.H:540
void define()
Definition: AMReX_TableData.H:564
TableData< T, N, ORDER > & operator=(TableData< T, N, ORDER > const &)=delete
T value_type
Definition: AMReX_TableData.H:409
Array< int, N > const & hi() const noexcept
Definition: AMReX_TableData.H:441
~TableData() noexcept
Definition: AMReX_TableData.H:502
void clear() noexcept
Definition: AMReX_TableData.H:551
table_type table() noexcept
Definition: AMReX_TableData.H:597
AMREX_GPU_HOST_DEVICE Long size(T const &b) noexcept
integer version
Definition: AMReX_GpuRange.H:26
void dtod_memcpy_async(void *p_d_dst, const void *p_d_src, const std::size_t sz) noexcept
Definition: AMReX_GpuDevice.H:279
void copy(HostToDevice, InIter begin, InIter end, OutIter result) noexcept
A host-to-device copy routine. Note this is just a wrapper around memcpy, so it assumes contiguous st...
Definition: AMReX_GpuContainers.H:121
void dtoh_memcpy_async(void *p_h, const void *p_d, const std::size_t sz) noexcept
Definition: AMReX_GpuDevice.H:265
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void * memcpy(void *dest, const void *src, std::size_t count)
Definition: AMReX_GpuUtility.H:214
void htod_memcpy_async(void *p_d, const void *p_h, const std::size_t sz) noexcept
Definition: AMReX_GpuDevice.H:251
Table4D< T > make_table(T *p, Array< int, 4 > const &lo, Array< int, 4 > const &hi)
Definition: AMReX_TableData.H:590
Definition: AMReX_Amr.cpp:49
void Abort(const std::string &msg)
Print out message to cerr and exit via abort().
Definition: AMReX.cpp:221
std::array< T, N > Array
Definition: AMReX_Array.H:23
Definition: AMReX_FabArrayCommI.H:841
Definition: AMReX_DataAllocator.H:9
Arena * arena() const noexcept
Definition: AMReX_DataAllocator.H:24
Definition: AMReX_TableData.H:20
int begin
Definition: AMReX_TableData.H:22
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE U & operator()(int i) const noexcept
Definition: AMReX_TableData.H:47
int end
Definition: AMReX_TableData.H:23
constexpr AMREX_GPU_HOST_DEVICE Table1D(T *a_p, int a_begin, int a_end) noexcept
Definition: AMReX_TableData.H:36
constexpr Table1D() noexcept=default
T *AMREX_RESTRICT p
Definition: AMReX_TableData.H:21
Definition: AMReX_TableData.H:77
constexpr AMREX_GPU_HOST_DEVICE Table2D(T *a_p, GpuArray< int, 2 > const &a_begin, GpuArray< int, 2 > const &a_end) noexcept
Definition: AMReX_TableData.H:95
static constexpr int len0(GpuArray< int, 2 > const &a_begin, GpuArray< int, 2 > const &a_end) noexcept
Definition: AMReX_TableData.H:146
constexpr Table2D() noexcept=default
Definition: AMReX_TableData.H:159
static constexpr int len1(GpuArray< int, 3 > const &a_begin, GpuArray< int, 3 > const &a_end) noexcept
Definition: AMReX_TableData.H:244
constexpr Table3D() noexcept=default
static constexpr int len0(GpuArray< int, 3 > const &a_begin, GpuArray< int, 3 > const &a_end) noexcept
Definition: AMReX_TableData.H:234
constexpr AMREX_GPU_HOST_DEVICE Table3D(T *a_p, GpuArray< int, 3 > const &a_begin, GpuArray< int, 3 > const &a_end) noexcept
Definition: AMReX_TableData.H:179
Definition: AMReX_TableData.H:253
static constexpr int len1(GpuArray< int, 4 > const &a_begin, GpuArray< int, 4 > const &a_end) noexcept
Definition: AMReX_TableData.H:343
constexpr Table4D() noexcept=default
static constexpr int len2(GpuArray< int, 4 > const &a_begin, GpuArray< int, 4 > const &a_end) noexcept
Definition: AMReX_TableData.H:353
static constexpr int len0(GpuArray< int, 4 > const &a_begin, GpuArray< int, 4 > const &a_end) noexcept
Definition: AMReX_TableData.H:333
constexpr AMREX_GPU_HOST_DEVICE Table4D(T *a_p, GpuArray< int, 4 > const &a_begin, GpuArray< int, 4 > const &a_end) noexcept
Definition: AMReX_TableData.H:275