1 #ifndef AMREX_TABLE_DATA_H_
2 #define AMREX_TABLE_DATA_H_
3 #include <AMReX_Config.H>
14 #include <type_traits>
18 template <
typename T,
typename IDX =
int>
27 template <class U=T, std::enable_if_t<std::is_const_v<U>,
int> = 0>
36 constexpr
Table1D (T* a_p, IDX a_begin, IDX 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 (IDX i)
const
58 if (i < begin || i >=
end) {
59 if constexpr (std::is_same_v<IDX,int>) {
65 }
else if constexpr (std::is_same_v<IDX,long>) {
71 }
else if constexpr (std::is_same_v<IDX,long long>) {
82 ss <<
" (" << i <<
") is out of bound ("
91 template <
typename T, Order ORDER = Order::F>
101 template <class U=T, std::enable_if_t<std::is_const_v<U>,
int> = 0>
121 explicit operator bool () const noexcept {
return p !=
nullptr; }
123 template <
class U=T, std::enable_if_t<!std::is_
void_v<U>,
int> = 0>
126 #if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
136 #if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
138 void index_assert (
int i,
int j)
const
148 std::stringstream ss;
149 ss << " (" << i << "," << j << ") is out of bound ("
151 << "," <<
begin[1] << ":" <<
end[1]-1 << ")";
164 return a_end[0] - a_begin[0];
166 return a_end[1] - a_begin[1];
171 template <
typename T, Order ORDER = Order::F>
182 template <class U=T, std::enable_if_t<std::is_const_v<U>,
int> = 0>
187 stride2(rhs.stride2),
198 stride2(
stride1*len1(a_begin,a_end)),
204 explicit operator bool () const noexcept {
return p !=
nullptr; }
206 template <
class U=T, std::enable_if_t<!std::is_
void_v<U>,
int> = 0>
209 #if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
219 #if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
221 void index_assert (
int i,
int j,
int k)
const
233 std::stringstream ss;
234 ss << " (" << i << "," << j << "," << k << ") is out of bound ("
236 << "," <<
begin[1] << ":" <<
end[1]-1
237 << "," <<
begin[2] << ":" <<
end[2]-1 << ")";
250 return a_end[0] - a_begin[0];
252 return a_end[2] - a_begin[2];
259 return a_end[1] - a_begin[1];
263 template <
typename T, Order ORDER = Order::F>
275 template <class U=T, std::enable_if_t<std::is_const_v<U>,
int> = 0>
280 stride2(rhs.stride2),
281 stride3(rhs.stride3),
292 stride2(
stride1*len1(a_begin,a_end)),
293 stride3(stride2*len2(a_begin,a_end)),
299 explicit operator bool () const noexcept {
return p !=
nullptr; }
301 template <
class U=T, std::enable_if_t<!std::is_
void_v<U>,
int> = 0>
304 #if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
305 index_assert(i,j,k,n);
314 #if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
316 void index_assert (
int i,
int j,
int k,
int n)
const
329 std::stringstream ss;
330 ss << " (" << i << "," << j << "," << k << "," << n << ") is out of bound ("
332 << "," <<
begin[1] << ":" <<
end[1]-1
333 << "," <<
begin[2] << ":" <<
end[2]-1
334 << "," <<
begin[3] << ":" <<
end[3]-1 << ")";
347 return a_end[0] - a_begin[0];
349 return a_end[3] - a_begin[3];
357 return a_end[1] - a_begin[1];
359 return a_end[2] - a_begin[2];
367 return a_end[2] - a_begin[2];
369 return a_end[1] - a_begin[1];
412 template <
typename T,
int N, Order ORDER = Order::F>
418 template <
class U,
int M, Order O>
friend class TableData;
421 std::conditional_t<N==2, Table2D<T, ORDER>,
422 std::conditional_t<N==3, Table3D<T, ORDER>,
425 std::conditional_t<N==2, Table2D<T const, ORDER>,
426 std::conditional_t<N==3, Table3D<T const, ORDER>,
443 [[nodiscard]] constexpr
int dim () const noexcept {
return N; }
447 [[nodiscard]] Long
size () const noexcept;
449 Array<
int,N> const& lo () const noexcept {
return m_lo; }
453 void clear () noexcept;
457 table_type table () noexcept;
458 const_table_type table () const noexcept;
459 const_table_type const_table () const noexcept;
468 Long m_truesize = 0L;
469 bool m_ptr_owner = false;
472 template <typename T,
int N,
Order ORDER>
480 template <
typename T,
int N, Order ORDER>
486 m_truesize(rhs.m_truesize),
487 m_ptr_owner(rhs.m_ptr_owner)
489 rhs.m_dptr =
nullptr;
490 rhs.m_ptr_owner =
false;
493 template <
typename T,
int N, Order ORDER>
494 TableData<T,N,ORDER>&
499 m_arena = rhs.m_arena;
503 m_truesize = rhs.m_truesize;
504 m_ptr_owner = rhs.m_ptr_owner;
505 rhs.m_dptr =
nullptr;
506 rhs.m_ptr_owner =
false;
511 template <
typename T,
int N, Order ORDER>
514 static_assert(std::is_trivially_copyable<T>() &&
515 std::is_trivially_destructible<T>(),
516 "TableData<T,N,ORDER>: T must be trivially copyable and trivially destructible");
517 static_assert(N>=1 && N <=4,
"TableData<T,N,ORDER>: N must be in the range of [1,4]");
521 template <
typename T,
int N, Order ORDER>
536 }
else if (m_dptr ==
nullptr || !m_ptr_owner) {
539 }
else if (
size() > m_truesize) {
545 template <
typename T,
int N, Order ORDER>
550 for (
int i = 0; i < N; ++i) {
551 r *= m_hi[i] - m_lo[i] + 1;
556 template <
typename T,
int N, Order ORDER>
569 template <
typename T,
int N, Order ORDER>
575 if (m_truesize == 0) {
579 m_dptr =
static_cast<T*
>(this->alloc(m_truesize*
sizeof(T)));
584 template <
typename T, Order>
588 template <
typename T, Order ORDER>
592 template <
typename T, Order ORDER>
596 template <
typename T, Order ORDER>
598 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});
602 template <
typename T,
int N, Order ORDER>
606 return detail::make_table<T,ORDER>(m_dptr, m_lo, m_hi);
609 template <
typename T,
int N, Order ORDER>
613 return detail::make_table<T const, ORDER>(m_dptr, m_lo, m_hi);
616 template <
typename T,
int N, Order ORDER>
620 return detail::make_table<T const, ORDER>(m_dptr, m_lo, m_hi);
623 template <
typename T,
int N, Order ORDER>
627 std::size_t count =
sizeof(T)*
size();
629 bool this_on_device = arena()->isManaged() || arena()->isDevice();
630 bool rhs_on_device = rhs.arena()->isManaged() || rhs.arena()->isDevice();
631 if (this_on_device && rhs_on_device) {
633 }
else if (this_on_device && !rhs_on_device) {
635 }
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:415
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:423
TableData() noexcept=default
const_table_type const_table() const noexcept
Definition: AMReX_TableData.H:618
void resize(Array< int, N > const &lo, Array< int, N > const &hi, Arena *ar=nullptr)
Definition: AMReX_TableData.H:523
friend class TableData
Definition: AMReX_TableData.H:418
void copy(TableData< T, N, ORDER > const &rhs) noexcept
Definition: AMReX_TableData.H:625
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:427
Long size() const noexcept
Definition: AMReX_TableData.H:547
void define()
Definition: AMReX_TableData.H:571
TableData< T, N, ORDER > & operator=(TableData< T, N, ORDER > const &)=delete
T value_type
Definition: AMReX_TableData.H:419
Array< int, N > const & hi() const noexcept
Definition: AMReX_TableData.H:451
~TableData() noexcept
Definition: AMReX_TableData.H:512
void clear() noexcept
Definition: AMReX_TableData.H:558
table_type table() noexcept
Definition: AMReX_TableData.H:604
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:597
Definition: AMReX_Amr.cpp:49
Order
Definition: AMReX_SmallMatrix.H:19
void Abort(const std::string &msg)
Print out message to cerr and exit via abort().
Definition: AMReX.cpp:225
std::array< T, N > Array
Definition: AMReX_Array.H:24
Definition: AMReX_FabArrayCommI.H:896
Definition: AMReX_DataAllocator.H:9
Arena * arena() const noexcept
Definition: AMReX_DataAllocator.H:24
Definition: AMReX_TableData.H:20
IDX begin
Definition: AMReX_TableData.H:22
T *AMREX_RESTRICT p
Definition: AMReX_TableData.H:21
constexpr AMREX_GPU_HOST_DEVICE Table1D(T *a_p, IDX a_begin, IDX a_end) noexcept
Definition: AMReX_TableData.H:36
IDX end
Definition: AMReX_TableData.H:23
constexpr Table1D() noexcept=default
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE U & operator()(IDX i) const noexcept
Definition: AMReX_TableData.H:47
Definition: AMReX_TableData.H:93
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:111
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE U & operator()(int i, int j) const noexcept
Definition: AMReX_TableData.H:125
static constexpr int len0(GpuArray< int, 2 > const &a_begin, GpuArray< int, 2 > const &a_end) noexcept
Definition: AMReX_TableData.H:160
constexpr Table2D() noexcept=default
GpuArray< int, 2 > end
Definition: AMReX_TableData.H:97
GpuArray< int, 2 > begin
Definition: AMReX_TableData.H:96
T *AMREX_RESTRICT p
Definition: AMReX_TableData.H:94
Long stride1
Definition: AMReX_TableData.H:95
Definition: AMReX_TableData.H:173
static constexpr int len1(GpuArray< int, 3 > const &a_begin, GpuArray< int, 3 > const &a_end) noexcept
Definition: AMReX_TableData.H:256
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:246
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:193
Definition: AMReX_TableData.H:265
static constexpr int len1(GpuArray< int, 4 > const &a_begin, GpuArray< int, 4 > const &a_end) noexcept
Definition: AMReX_TableData.H:353
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:363
static constexpr int len0(GpuArray< int, 4 > const &a_begin, GpuArray< int, 4 > const &a_end) noexcept
Definition: AMReX_TableData.H:343
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:287