-
Notifications
You must be signed in to change notification settings - Fork 388
/
Copy pathAMReX_Array4.H
394 lines (341 loc) · 14 KB
/
AMReX_Array4.H
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#ifndef AMREX_ARRAY4_H_
#define AMREX_ARRAY4_H_
#include <AMReX_Config.H>
#include <AMReX.H>
#include <AMReX_IntVect.H>
#include <AMReX_GpuPrint.H>
#include <iostream>
#include <sstream>
namespace amrex {
template <typename T>
struct CellData // Data in a single cell
{
T* AMREX_RESTRICT p = nullptr;
Long stride = 0;
int ncomp = 0;
AMREX_GPU_HOST_DEVICE
constexpr CellData (T* a_p, Long a_stride, int a_ncomp)
: p(a_p), stride(a_stride), ncomp(a_ncomp)
{}
template <class U=T,
std::enable_if_t<std::is_const_v<U>,int> = 0>
AMREX_GPU_HOST_DEVICE
constexpr CellData (CellData<std::remove_const_t<T>> const& rhs) noexcept
: p(rhs.p), stride(rhs.stride), ncomp(rhs.ncomp)
{}
AMREX_GPU_HOST_DEVICE
explicit operator bool() const noexcept { return p != nullptr; }
[[nodiscard]] AMREX_GPU_HOST_DEVICE
int nComp() const noexcept { return ncomp; }
template <class U=T,
std::enable_if_t<!std::is_void_v<U>,int> = 0>
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
U& operator[] (int n) const noexcept {
#if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
if (n < 0 || n >= ncomp) {
AMREX_IF_ON_DEVICE((
AMREX_DEVICE_PRINTF(" %d is out of bound (0:%d)", n, ncomp-1);
))
AMREX_IF_ON_HOST((
std::stringstream ss;
ss << " " << n << " is out of bound: (0:" << ncomp-1 << ")";
amrex::Abort(ss.str());
))
}
#endif
return p[n*stride];
}
};
template <typename T>
struct Array4
{
T* AMREX_RESTRICT p;
Dim3 begin{1,1,1};
Dim3 len{0,0,0};
int ncomp=0;
AMREX_GPU_HOST_DEVICE
constexpr Array4 () noexcept : p(nullptr) {}
template <class U=T, std::enable_if_t<std::is_const_v<U>,int> = 0>
AMREX_GPU_HOST_DEVICE
constexpr Array4 (Array4<std::remove_const_t<T>> const& rhs) noexcept
: p(rhs.p),
begin(rhs.begin),
len(rhs.len),
ncomp(rhs.ncomp)
{}
AMREX_GPU_HOST_DEVICE
constexpr Array4 (T* a_p, Dim3 const& a_begin, Dim3 const& a_end, int a_ncomp) noexcept
: p(a_p),
begin(a_begin),
len{a_end.x-a_begin.x, a_end.y-a_begin.y, a_end.z-a_begin.z},
ncomp(a_ncomp)
{}
template <class U,
std::enable_if_t
<std::is_same_v<std::remove_const_t<T>,
std::remove_const_t<U>>,int> = 0>
AMREX_GPU_HOST_DEVICE
constexpr Array4 (Array4<U> const& rhs, int start_comp) noexcept
: p((T*)(rhs.p+start_comp*rhs.nstride())),
begin(rhs.begin),
len(rhs.len),
ncomp(rhs.ncomp-start_comp)
{}
template <class U,
std::enable_if_t
<std::is_same_v<std::remove_const_t<T>,
std::remove_const_t<U>>,int> = 0>
AMREX_GPU_HOST_DEVICE
constexpr Array4 (Array4<U> const& rhs, int start_comp, int num_comps) noexcept
: p((T*)(rhs.p+start_comp*rhs.nstride())),
begin(rhs.begin),
len(rhs.len),
ncomp(num_comps)
{}
AMREX_GPU_HOST_DEVICE
explicit operator bool() const noexcept { return p != nullptr; }
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
Long jstride () const noexcept { return Long(len.x); }
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
Long kstride () const noexcept { return Long(len.x)*Long(len.y); }
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
Long nstride () const noexcept { return Long(len.x)*Long(len.y)*Long(len.z); }
template <class U=T, std::enable_if_t<!std::is_void_v<U>,int> = 0>
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
U& operator() (int i, int j, int k) const noexcept {
#if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
index_assert(i,j,k,0);
#endif
return p[(i-begin.x)+Long(len.x)*((j-begin.y)+Long(len.y)*(k-begin.z))];
}
template <class U=T, std::enable_if_t<!std::is_void_v<U>,int> = 0>
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
U& operator() (int i, int j, int k, int n) const noexcept {
#if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
index_assert(i,j,k,n);
#endif
return p[(i-begin.x)+Long(len.x)*((j-begin.y)+Long(len.y)*((k-begin.z)+Long(len.z)*n))];
}
template <class U=T, std::enable_if_t<!std::is_void_v<U>,int> = 0>
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
T* ptr (int i, int j, int k) const noexcept {
#if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
index_assert(i,j,k,0);
#endif
return p + ((i-begin.x)+Long(len.x)*((j-begin.y)+Long(len.y)*(k-begin.z)));
}
template <class U=T, std::enable_if_t<!std::is_void_v<U>,int> = 0>
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
T* ptr (int i, int j, int k, int n) const noexcept {
#if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
index_assert(i,j,k,n);
#endif
return p + ((i-begin.x)+Long(len.x)*((j-begin.y)+Long(len.y)*((k-begin.z)+Long(len.z)*n)));
}
template <class U=T, std::enable_if_t<!std::is_void_v<U>,int> = 0>
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
U& operator() (IntVect const& iv) const noexcept {
#if (AMREX_SPACEDIM == 1)
return this->operator()(iv[0],0,0);
#elif (AMREX_SPACEDIM == 2)
return this->operator()(iv[0],iv[1],0);
#else
return this->operator()(iv[0],iv[1],iv[2]);
#endif
}
template <class U=T, std::enable_if_t<!std::is_void_v<U>,int> = 0>
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
U& operator() (IntVect const& iv, int n) const noexcept {
#if (AMREX_SPACEDIM == 1)
return this->operator()(iv[0],0,0,n);
#elif (AMREX_SPACEDIM == 2)
return this->operator()(iv[0],iv[1],0,n);
#else
return this->operator()(iv[0],iv[1],iv[2],n);
#endif
}
template <class U=T, std::enable_if_t<!std::is_void_v<U>,int> = 0>
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
T* ptr (IntVect const& iv) const noexcept {
#if (AMREX_SPACEDIM == 1)
return this->ptr(iv[0],0,0);
#elif (AMREX_SPACEDIM == 2)
return this->ptr(iv[0],iv[1],0);
#else
return this->ptr(iv[0],iv[1],iv[2]);
#endif
}
template <class U=T, std::enable_if_t<!std::is_void_v<U>,int> = 0>
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
T* ptr (IntVect const& iv, int n) const noexcept {
#if (AMREX_SPACEDIM == 1)
return this->ptr(iv[0],0,0,n);
#elif (AMREX_SPACEDIM == 2)
return this->ptr(iv[0],iv[1],0,n);
#else
return this->ptr(iv[0],iv[1],iv[2],n);
#endif
}
template <class U=T, std::enable_if_t<!std::is_void_v<U>,int> = 0>
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
U& operator() (Dim3 const& cell) const noexcept {
return this->operator()(cell.x,cell.y,cell.z);
}
template <class U=T, std::enable_if_t<!std::is_void_v<U>,int> = 0>
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
U& operator() (Dim3 const& cell, int n) const noexcept {
return this->operator()(cell.x,cell.y,cell.z,n);
}
template <class U=T, std::enable_if_t<!std::is_void_v<U>,int> = 0>
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
T* ptr (Dim3 const& cell) const noexcept {
return this->ptr(cell.x,cell.y,cell.z);
}
template <class U=T, std::enable_if_t<!std::is_void_v<U>,int> = 0>
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
T* ptr (Dim3 const& cell, int n) const noexcept {
return this->ptr(cell.x,cell.y,cell.z,n);
}
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
T* dataPtr () const noexcept {
return this->p;
}
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
std::size_t size () const noexcept {
return this->nstride() * this->ncomp;
}
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
int nComp () const noexcept { return ncomp; }
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
bool contains (int i, int j, int k) const noexcept {
return (i>=begin.x && i<(begin.x+len.x) && j>=begin.y && j<(begin.y+len.y) && k>=begin.z && k<(begin.z+len.z));
}
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
bool contains (IntVect const& iv) const noexcept {
return AMREX_D_TERM( iv[0]>=begin.x && iv[0]<(begin.x+len.x),
&& iv[1]>=begin.y && iv[1]<(begin.y+len.y),
&& iv[2]>=begin.z && iv[2]<(begin.z+len.z));
}
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
bool contains (Dim3 const& cell) const noexcept {
return this->contains(cell.x,cell.y,cell.z);
}
#if defined(AMREX_DEBUG) || defined(AMREX_BOUND_CHECK)
AMREX_GPU_HOST_DEVICE inline
void index_assert (int i, int j, int k, int n) const
{
if (i<begin.x || i>=(begin.x+len.x) || j<begin.y || j>=(begin.y+len.y) || k<begin.z || k>=(begin.z+len.z)
|| n < 0 || n >= ncomp) {
AMREX_IF_ON_DEVICE((
AMREX_DEVICE_PRINTF(" (%d,%d,%d,%d) is out of bound (%d:%d,%d:%d,%d:%d,0:%d)\n",
i, j, k, n, begin.x, (begin.x+len.x)-1, begin.y, (begin.y+len.y)-1,
begin.z, (begin.z+len.z)-1, ncomp-1);
amrex::Abort();
))
AMREX_IF_ON_HOST((
std::stringstream ss;
ss << " (" << i << "," << j << "," << k << "," << n
<< ") is out of bound ("
<< begin.x << ":" << (begin.x+len.x)-1 << ","
<< begin.y << ":" << (begin.y+len.y)-1 << ","
<< begin.z << ":" << (begin.z+len.z)-1 << ","
<< "0:" << ncomp-1 << ")";
amrex::Abort(ss.str());
))
}
}
#endif
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
CellData<T> cellData (int i, int j, int k) const noexcept {
return CellData<T>{this->ptr(i,j,k), nstride(), ncomp};
}
};
template <class Tto, class Tfrom>
[[nodiscard]] AMREX_GPU_HOST_DEVICE
Array4<Tto> ToArray4 (Array4<Tfrom> const& a_in) noexcept
{
return Array4<Tto>((Tto*)(a_in.p), a_in.begin,
Dim3{a_in.begin.x + a_in.len.x,
a_in.begin.y + a_in.len.y,
a_in.begin.z + a_in.len.z},
a_in.ncomp);
}
template <class T>
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
Dim3 lbound (Array4<T> const& a) noexcept
{
return a.begin;
}
template <class T>
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
Dim3 ubound (Array4<T> const& a) noexcept
{
return Dim3{a.begin.x+a.len.x-1,a.begin.y+a.len.y-1,a.begin.z+a.len.z-1};
}
template <class T>
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
Dim3 begin (Array4<T> const& a) noexcept
{
return a.begin;
}
template <class T>
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
Dim3 end (Array4<T> const& a) noexcept
{
return Dim3{a.begin.x+a.len.x,a.begin.y+a.len.y,a.begin.z+a.len.z};
}
template <class T>
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
Dim3 length (Array4<T> const& a) noexcept
{
return a.len;
}
template <typename T>
std::ostream& operator<< (std::ostream& os, const Array4<T>& a) {
os << "((" << lbound(a) << ',' << ubound(a) << ")," << a.ncomp << ')';
return os;
}
//
// Type traits for detecting if a class has a size() constexpr function.
//
template <class A, class Enable = void> struct HasMultiComp : std::false_type {};
//
template <class B>
struct HasMultiComp<B, std::enable_if_t<B().size() >= 1>>
: std::true_type {};
//
// PolymorphicArray4 can be used to access both AoS and SoA with
// (i,j,k,n). Here SoA refers multi-component BaseFab, and AoS refers
// to single-component BaseFab of multi-component GpuArray.
//
template <typename T>
struct PolymorphicArray4
: public Array4<T>
{
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
PolymorphicArray4 (Array4<T> const& a)
: Array4<T>{a} {}
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
T& operator() (int i, int j, int k) const noexcept {
return this->Array4<T>::operator()(i,j,k);
}
template <class U=T, std::enable_if_t< amrex::HasMultiComp<U>::value,int> = 0>
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
typename U::reference_type
operator() (int i, int j, int k, int n) const noexcept {
return this->Array4<T>::operator()(i,j,k,0)[n];
}
template <class U=T, std::enable_if_t<!amrex::HasMultiComp<U>::value,int> = 0>
[[nodiscard]] AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE
U& operator() (int i, int j, int k, int n) const noexcept {
return this->Array4<T>::operator()(i,j,k,n);
}
};
template <typename T>
[[nodiscard]] PolymorphicArray4<T>
makePolymorphic (Array4<T> const& a)
{
return PolymorphicArray4<T>(a);
}
}
#endif