21 template <
unsigned int inner>
27 unsigned int dim[inner] = {0};
31 unsigned int stride[inner + 1] = {0};
33 #ifdef MOOSE_KOKKOS_SCOPE 39 KOKKOS_FUNCTION
unsigned int operator[](
unsigned int i)
const {
return dim[i]; }
43 #ifdef MOOSE_KOKKOS_SCOPE 51 template <
typename T,
unsigned int inner, LayoutType layout>
69 KOKKOS_FUNCTION
unsigned int size()
const {
return _dim.stride[inner]; }
75 KOKKOS_FUNCTION
unsigned int n(
unsigned int dim)
const {
return _dim[
dim]; }
82 KOKKOS_ASSERT(i <
_dim.stride[inner]);
90 template <
typename... indices>
91 KOKKOS_FUNCTION T &
operator()(indices... i)
const;
104 template <
typename T,
unsigned int inner, LayoutType layout>
105 template <
typename... indices>
109 static_assert((std::is_convertible<indices, unsigned int>::value && ...),
110 "All arguments must be convertible to unsigned int");
111 static_assert(
sizeof...(i) == inner,
"Number of arguments should match array dimension");
115 unsigned int idx[inner] = {
static_cast<unsigned int>(i)...};
117 for (
unsigned int d = 0; d <
sizeof...(i); ++d)
118 KOKKOS_ASSERT(
idx[d] < _dim[d]);
122 unsigned int idx = 0;
127 (d == 0 ?
static_cast<unsigned int>(i) : static_cast<unsigned int>(i) * _dim.stride[d])),
131 (((
idx += (d == inner - 1 ? static_cast<unsigned int>(i)
132 :
static_cast<unsigned int>(i) * _dim.stride[d])),
159 template <
typename T,
162 typename index_type = MOOSE_KOKKOS_INDEX_TYPE,
172 #ifdef MOOSE_KOKKOS_SCOPE 176 template <
typename... size_type>
186 void create(
const std::vector<index_type> &
n);
191 template <
typename... size_type>
198 void reserve(
const std::array<uint64_t, outer> & index,
199 const std::array<uint64_t, inner> & dimension);
209 mooseAssert(
_finalized,
"KokkosJaggedArray not finalized.");
211 _data.copyToDevice();
218 mooseAssert(
_finalized,
"KokkosJaggedArray not finalized.");
227 mooseAssert(
_finalized,
"KokkosJaggedArray not finalized.");
229 _dims.moveToDevice();
231 _data.moveToDevice();
238 mooseAssert(
_finalized,
"KokkosJaggedArray not finalized.");
269 KOKKOS_FUNCTION index_type
size()
const {
return _data.size(); }
286 KOKKOS_FUNCTION
auto operator[](index_type i)
const;
292 template <
typename... indices>
293 KOKKOS_FUNCTION
auto operator()(indices... i)
const;
315 #ifdef MOOSE_KOKKOS_SCOPE 316 template <
typename T,
331 template <
typename T,
336 template <
typename... size_type>
341 _offsets.create(n...);
347 template <
typename T,
354 const std::array<uint64_t, outer> & index,
const std::array<uint64_t, inner> & dimension)
356 mooseAssert(!_finalized,
"KokkosJaggedArray already finalized.");
359 index_type stride = 1;
361 for (
unsigned int o = 0; o < outer; ++o)
363 idx += index[o] * stride;
364 stride *= _offsets.n(o);
367 for (
unsigned int i = 0; i < inner; ++i)
368 _dims[
idx].
dim[i] = dimension[i];
373 for (
unsigned int i = 0; i < inner; ++i)
375 _dims[
idx].stride[i] = stride;
376 stride *= dimension[i];
379 for (
int i = inner - 1; i >= 0; --i)
381 _dims[
idx].stride[i] = stride;
382 stride *= dimension[i];
385 _dims[
idx].stride[inner] = stride;
388 template <
typename T,
396 mooseAssert(!_finalized,
"KokkosJaggedArray already finalized.");
398 index_type stride = 1;
400 for (index_type o = 0; o < _offsets.size(); ++o)
404 for (
unsigned int i = 0; i < inner; ++i)
405 stride *= _dims[o][i];
407 _offsets[o] = stride;
410 std::exclusive_scan(_offsets.begin(), _offsets.end(), _offsets.begin(), 0);
412 _dims.copyToDevice();
413 _offsets.copyToDevice();
417 _data.create(_offsets.last() + stride + 1);
422 template <
typename T,
430 auto data = &_data[_offsets[i]];
431 const auto &
dim = _dims[i];
436 template <
typename T,
441 template <
typename... indices>
445 auto data = &_data[_offsets(i...)];
446 const auto &
dim = _dims(i...);
JaggedArray()=default
Default constructor.
void moveToHost()
Copy data from device to host and deallocate device.
KOKKOS_FUNCTION index_type n(unsigned int dim) const
Get the size of a dimension.
bool _finalized
Whether the array was finalized.
void moveToHost(bool should_free_device=true)
Copy data from device to host and deallocate device.
KOKKOS_FUNCTION index_type n() const
Get the total outer array size.
JaggedArrayInnerDim< inner > _dim
Inner array dimension information.
LayoutType
The enumerator that dictates the memory layout.
Array1D< T, index_type > _data
Sequential data array.
Array< JaggedArrayInnerDim< inner >, outer, index_type > _dims
Dimension information of each inner array.
void moveToDevice(bool should_free_host=true)
Copy data from host to device and deallocate host.
KOKKOS_FUNCTION bool isDeviceAlloc() const
Get whether the array was allocated on device.
static constexpr std::size_t dim
This is the dimension of all vector and tensor datastructures used in MOOSE.
void moveToDevice()
Copy data from host to device and deallocate host.
void copyToHost()
Copy data from device to host.
Array< index_type, outer, index_type > _offsets
Starting offset of each inner array into the sequential data array.
KOKKOS_FUNCTION index_type size() const
Get the total data array size.
KOKKOS_FUNCTION unsigned int size() const
Get the total inner array size.
A simple object holding the dimension information of an inner array.
JaggedArray(size_type... n)
Constructor.
KOKKOS_FUNCTION bool isFinalized() const
Get whether the array is finalized.
unsigned int dim[inner]
Size of each dimension.
KOKKOS_FUNCTION index_type size() const
Get the total array size.
KOKKOS_FUNCTION auto operator[](index_type i) const
Get an inner array.
KOKKOS_FUNCTION T & operator()(indices... i) const
Get an array entry.
void create(const std::vector< index_type > &n)
Allocate outer array.
The inner array wrapper class.
auto & array()
Get the underlying data array.
T * _data
Pointer to the inner array data.
unsigned int stride[inner+1]
Stride of each dimension.
KOKKOS_FUNCTION unsigned int n(unsigned int dim) const
Get the size of a dimension of the inner array.
void reserve(const std::array< uint64_t, outer > &index, const std::array< uint64_t, inner > &dimension)
Reserve inner array for an outer array entry.
KOKKOS_FUNCTION auto operator()(indices... i) const
Get an inner array.
KOKKOS_FUNCTION index_type n(unsigned int dim) const
Get the size of a dimension of the outer array.
KOKKOS_FUNCTION unsigned int operator[](unsigned int i) const
Get the size of a dimension.
KOKKOS_FUNCTION bool isHostAlloc() const
Get whether the array was allocated on host.
The Kokkos jagged array class.
KOKKOS_FUNCTION JaggedArrayInnerData(T *data, JaggedArrayInnerDim< inner > dim)
Constructor.
void copyToDevice()
Copy data from host to device.
KOKKOS_FUNCTION T & operator[](unsigned int i) const
Get an array entry.
void finalize()
Setup array structure.