1#ifndef AMREX_TABLE_DATA_H_
2#define AMREX_TABLE_DATA_H_
3#include <AMReX_Config.H>
18template <
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 ("
91template <
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];
171template <
typename T, Order ORDER = Order::F>
182 template <class U=T, std::enable_if_t<std::is_const_v<U>,
int> = 0>
186 stride1(rhs.stride1),
187 stride2(rhs.stride2),
197 stride1( len0(a_begin,a_end)),
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>
208 U& operator() (
int i,
int j,
int k)
const noexcept {
209#if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
212 if constexpr (ORDER == Order::F) {
219#if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
221 void index_assert (
int i,
int j,
int k)
const
223 if (i < begin[0] || i >= end[0] ||
224 j < begin[1] || j >= end[1] ||
225 k < begin[2] || k >= end[2]) {
228 i, j, k, begin[0], end[0]-1, begin[1], end[1]-1,
233 std::stringstream ss;
234 ss << " (" << i << "," << j << "," << k << ") is out of bound ("
235 << begin[0] << ":" << end[0]-1
236 << "," << begin[1] << ":" << end[1]-1
237 << "," << begin[2] << ":" << end[2]-1 << ")";
238 amrex::Abort(ss.str());
249 if constexpr (ORDER == Order::F) {
250 return a_end[0] - a_begin[0];
252 return a_end[2] - a_begin[2];
259 return a_end[1] - a_begin[1];
263template <
typename T, Order ORDER = Order::F>
275 template <class U=T, std::enable_if_t<std::is_const_v<U>,
int> = 0>
279 stride1(rhs.stride1),
280 stride2(rhs.stride2),
281 stride3(rhs.stride3),
291 stride1( len0(a_begin,a_end)),
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>
303 U& operator() (
int i,
int j,
int k,
int n)
const noexcept {
304#if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
305 index_assert(i,j,k,n);
307 if constexpr (ORDER == Order::F) {
314#if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
316 void index_assert (
int i,
int j,
int k,
int n)
const
318 if (i < begin[0] || i >= end[0] ||
319 j < begin[1] || j >= end[1] ||
320 k < begin[2] || k >= end[2] ||
321 n < begin[3] || n >= end[3]) {
324 i, j, k, n, begin[0], end[0]-1, begin[1], end[1]-1,
325 begin[2], end[2]-1, begin[3], end[3]-1);
329 std::stringstream ss;
330 ss << " (" << i << "," << j << "," << k << "," << n << ") is out of bound ("
331 << begin[0] << ":" << end[0]-1
332 << "," << begin[1] << ":" << end[1]-1
333 << "," << begin[2] << ":" << end[2]-1
334 << "," << begin[3] << ":" << end[3]-1 << ")";
335 amrex::Abort(ss.str());
346 if constexpr (ORDER == Order::F) {
347 return a_end[0] - a_begin[0];
349 return a_end[3] - a_begin[3];
356 if constexpr (ORDER == Order::F) {
357 return a_end[1] - a_begin[1];
359 return a_end[2] - a_begin[2];
366 if constexpr (ORDER == Order::F) {
367 return a_end[2] - a_begin[2];
369 return a_end[1] - a_begin[1];
412template <
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;
455 void copy (
TableData<T,N,ORDER> const& rhs) 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;
472template <typename T,
int N,
Order ORDER>
480template <
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;
493template <
typename T,
int N, Order 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;
511template <
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]");
521template <
typename T,
int N, Order ORDER>
536 }
else if (m_dptr ==
nullptr || !m_ptr_owner) {
539 }
else if (size() > m_truesize) {
545template <
typename T,
int N, Order ORDER>
550 for (
int i = 0; i < N; ++i) {
551 r *= m_hi[i] - m_lo[i] + 1;
556template <
typename T,
int N, Order ORDER>
569template <
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});
602template <
typename T,
int N, Order ORDER>
603typename TableData<T,N,ORDER>::table_type
606 return detail::make_table<T,ORDER>(m_dptr, m_lo, m_hi);
609template <
typename T,
int N, Order ORDER>
613 return detail::make_table<T const, ORDER>(m_dptr, m_lo, m_hi);
616template <
typename T,
int N, Order ORDER>
620 return detail::make_table<T const, ORDER>(m_dptr, m_lo, m_hi);
623template <
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) {
632 Gpu::dtod_memcpy_async(m_dptr, rhs.m_dptr, count);
633 }
else if (this_on_device && !rhs_on_device) {
634 Gpu::htod_memcpy_async(m_dptr, rhs.m_dptr, count);
635 }
else if (!this_on_device && rhs_on_device) {
636 Gpu::dtoh_memcpy_async(m_dptr, rhs.m_dptr, count);
640 std::memcpy(m_dptr, rhs.m_dptr, count);
#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
Array< int, N > const & hi() const noexcept
Definition AMReX_TableData.H:451
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
T value_type
Definition AMReX_TableData.H:419
Table1D< T > make_table(T *p, Array< int, 1 > const &lo, Array< int, 1 > const &hi)
Definition AMReX_TableData.H:585
Definition AMReX_Amr.cpp:49
Order
Definition AMReX_SmallMatrix.H:19
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Dim3 end(BoxND< dim > const &box) noexcept
Definition AMReX_Box.H:1890
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Dim3 begin(BoxND< dim > const &box) noexcept
Definition AMReX_Box.H:1881
void Abort(const std::string &msg)
Print out message to cerr and exit via abort().
Definition AMReX.cpp:230
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_Array.H:34
Definition AMReX_TableData.H:20
IDX begin
Definition AMReX_TableData.H:22
AMREX_GPU_HOST_DEVICE constexpr Table1D(T *a_p, IDX a_begin, IDX a_end) noexcept
Definition AMReX_TableData.H:36
T *AMREX_RESTRICT p
Definition AMReX_TableData.H:21
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
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
AMREX_GPU_HOST_DEVICE constexpr Table2D(T *a_p, GpuArray< int, 2 > const &a_begin, GpuArray< int, 2 > const &a_end) noexcept
Definition AMReX_TableData.H:111
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
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE U & operator()(int i, int j) const noexcept
Definition AMReX_TableData.H:125
Definition AMReX_TableData.H:173
AMREX_GPU_HOST_DEVICE constexpr Table3D(T *a_p, GpuArray< int, 3 > const &a_begin, GpuArray< int, 3 > const &a_end) noexcept
Definition AMReX_TableData.H:193
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
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
AMREX_GPU_HOST_DEVICE constexpr Table4D(T *a_p, GpuArray< int, 4 > const &a_begin, GpuArray< int, 4 > const &a_end) noexcept
Definition AMReX_TableData.H:287