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, Order 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)
120 #if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
122 void index_assert (
int i,
int j)
const
132 std::stringstream ss;
133 ss << " (" << i << "," << j << ") is out of bound ("
135 << "," <<
begin[1] << ":" <<
end[1]-1 << ")";
148 return a_end[0] - a_begin[0];
150 return a_end[1] - a_begin[1];
155 template <
typename T, Order ORDER = Order::F>
166 template <class U=T, std::enable_if_t<std::is_const_v<U>,
int> = 0>
170 stride1(rhs.stride1),
171 stride2(rhs.stride2),
181 stride1( len0(a_begin,a_end)),
182 stride2(stride1*len1(a_begin,a_end)),
188 explicit operator bool () const noexcept {
return p !=
nullptr; }
190 template <
class U=T, std::enable_if_t<!std::is_
void_v<U>,
int> = 0>
193 #if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
203 #if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
205 void index_assert (
int i,
int j,
int k)
const
217 std::stringstream ss;
218 ss << " (" << i << "," << j << "," << k << ") is out of bound ("
220 << "," <<
begin[1] << ":" <<
end[1]-1
221 << "," <<
begin[2] << ":" <<
end[2]-1 << ")";
234 return a_end[0] - a_begin[0];
236 return a_end[2] - a_begin[2];
243 return a_end[1] - a_begin[1];
247 template <
typename T, Order ORDER = Order::F>
259 template <class U=T, std::enable_if_t<std::is_const_v<U>,
int> = 0>
263 stride1(rhs.stride1),
264 stride2(rhs.stride2),
265 stride3(rhs.stride3),
275 stride1( len0(a_begin,a_end)),
276 stride2(stride1*len1(a_begin,a_end)),
277 stride3(stride2*len2(a_begin,a_end)),
283 explicit operator bool () const noexcept {
return p !=
nullptr; }
285 template <
class U=T, std::enable_if_t<!std::is_
void_v<U>,
int> = 0>
288 #if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
289 index_assert(i,j,k,n);
298 #if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
300 void index_assert (
int i,
int j,
int k,
int n)
const
313 std::stringstream ss;
314 ss << " (" << i << "," << j << "," << k << "," << n << ") is out of bound ("
316 << "," <<
begin[1] << ":" <<
end[1]-1
317 << "," <<
begin[2] << ":" <<
end[2]-1
318 << "," <<
begin[3] << ":" <<
end[3]-1 << ")";
331 return a_end[0] - a_begin[0];
333 return a_end[3] - a_begin[3];
341 return a_end[1] - a_begin[1];
343 return a_end[2] - a_begin[2];
351 return a_end[2] - a_begin[2];
353 return a_end[1] - a_begin[1];
396 template <
typename T,
int N, Order ORDER = Order::F>
402 template <
class U,
int M, Order O>
friend class TableData;
405 std::conditional_t<N==2, Table2D<T, ORDER>,
406 std::conditional_t<N==3, Table3D<T, ORDER>,
409 std::conditional_t<N==2, Table2D<T const, ORDER>,
410 std::conditional_t<N==3, Table3D<T const, ORDER>,
427 [[nodiscard]] constexpr
int dim () const noexcept {
return N; }
431 [[nodiscard]] Long
size () const noexcept;
433 Array<
int,N> const& lo () const noexcept {
return m_lo; }
437 void clear () noexcept;
441 table_type table () noexcept;
442 const_table_type table () const noexcept;
443 const_table_type const_table () const noexcept;
452 Long m_truesize = 0L;
453 bool m_ptr_owner = false;
456 template <typename T,
int N,
Order ORDER>
464 template <
typename T,
int N, Order ORDER>
470 m_truesize(rhs.m_truesize),
471 m_ptr_owner(rhs.m_ptr_owner)
473 rhs.m_dptr =
nullptr;
474 rhs.m_ptr_owner =
false;
477 template <
typename T,
int N, Order ORDER>
478 TableData<T,N,ORDER>&
483 m_arena = rhs.m_arena;
487 m_truesize = rhs.m_truesize;
488 m_ptr_owner = rhs.m_ptr_owner;
489 rhs.m_dptr =
nullptr;
490 rhs.m_ptr_owner =
false;
495 template <
typename T,
int N, Order ORDER>
498 static_assert(std::is_trivially_copyable<T>() &&
499 std::is_trivially_destructible<T>(),
500 "TableData<T,N,ORDER>: T must be trivially copyable and trivially destructible");
501 static_assert(N>=1 && N <=4,
"TableData<T,N,ORDER>: N must be in the range of [1,4]");
505 template <
typename T,
int N, Order ORDER>
520 }
else if (m_dptr ==
nullptr || !m_ptr_owner) {
523 }
else if (
size() > m_truesize) {
529 template <
typename T,
int N, Order ORDER>
534 for (
int i = 0; i < N; ++i) {
535 r *= m_hi[i] - m_lo[i] + 1;
540 template <
typename T,
int N, Order ORDER>
553 template <
typename T,
int N, Order ORDER>
559 if (m_truesize == 0) {
563 m_dptr =
static_cast<T*
>(this->alloc(m_truesize*
sizeof(T)));
568 template <
typename T, Order>
572 template <
typename T, Order ORDER>
576 template <
typename T, Order ORDER>
580 template <
typename T, Order ORDER>
582 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});
586 template <
typename T,
int N, Order ORDER>
590 return detail::make_table<T,ORDER>(m_dptr, m_lo, m_hi);
593 template <
typename T,
int N, Order ORDER>
597 return detail::make_table<T const, ORDER>(m_dptr, m_lo, m_hi);
600 template <
typename T,
int N, Order ORDER>
604 return detail::make_table<T const, ORDER>(m_dptr, m_lo, m_hi);
607 template <
typename T,
int N, Order ORDER>
611 std::size_t count =
sizeof(T)*
size();
613 bool this_on_device = arena()->isManaged() || arena()->isDevice();
614 bool rhs_on_device = rhs.arena()->isManaged() || rhs.arena()->isDevice();
615 if (this_on_device && rhs_on_device) {
617 }
else if (this_on_device && !rhs_on_device) {
619 }
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:399
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:407
TableData() noexcept=default
const_table_type const_table() const noexcept
Definition: AMReX_TableData.H:602
void resize(Array< int, N > const &lo, Array< int, N > const &hi, Arena *ar=nullptr)
Definition: AMReX_TableData.H:507
friend class TableData
Definition: AMReX_TableData.H:402
void copy(TableData< T, N, ORDER > const &rhs) noexcept
Definition: AMReX_TableData.H:609
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:411
Long size() const noexcept
Definition: AMReX_TableData.H:531
void define()
Definition: AMReX_TableData.H:555
TableData< T, N, ORDER > & operator=(TableData< T, N, ORDER > const &)=delete
T value_type
Definition: AMReX_TableData.H:403
Array< int, N > const & hi() const noexcept
Definition: AMReX_TableData.H:435
~TableData() noexcept
Definition: AMReX_TableData.H:496
void clear() noexcept
Definition: AMReX_TableData.H:542
table_type table() noexcept
Definition: AMReX_TableData.H:588
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:581
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
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:144
constexpr Table2D() noexcept=default
Definition: AMReX_TableData.H:157
static constexpr int len1(GpuArray< int, 3 > const &a_begin, GpuArray< int, 3 > const &a_end) noexcept
Definition: AMReX_TableData.H:240
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:230
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:177
Definition: AMReX_TableData.H:249
static constexpr int len1(GpuArray< int, 4 > const &a_begin, GpuArray< int, 4 > const &a_end) noexcept
Definition: AMReX_TableData.H:337
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:347
static constexpr int len0(GpuArray< int, 4 > const &a_begin, GpuArray< int, 4 > const &a_end) noexcept
Definition: AMReX_TableData.H:327
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:271