https://mooseframework.inl.gov
Public Member Functions | Protected Attributes | List of all members
Moose::Kokkos::JaggedArray< T, inner, outer, index_type, layout > Class Template Reference

The Kokkos jagged array class. More...

#include <KokkosJaggedArray.h>

Public Member Functions

 JaggedArray ()=default
 Default constructor. More...
 
template<typename... size_type>
 JaggedArray (size_type... n)
 Constructor. More...
 
void create (const std::vector< index_type > &n)
 Allocate outer array. More...
 
template<typename... size_type>
void create (size_type... n)
 Allocate outer array. More...
 
void reserve (const std::array< uint64_t, outer > &index, const std::array< uint64_t, inner > &dimension)
 Reserve inner array for an outer array entry. More...
 
void finalize ()
 Setup array structure. More...
 
void copyToDevice ()
 Copy data from host to device. More...
 
void copyToHost ()
 Copy data from device to host. More...
 
void moveToDevice ()
 Copy data from host to device and deallocate host. More...
 
void moveToHost ()
 Copy data from device to host and deallocate device. More...
 
auto & array ()
 Get the underlying data array. More...
 
KOKKOS_FUNCTION bool isFinalized () const
 Get whether the array is finalized. More...
 
KOKKOS_FUNCTION bool isHostAlloc () const
 Get whether the array was allocated on host. More...
 
KOKKOS_FUNCTION bool isDeviceAlloc () const
 Get whether the array was allocated on device. More...
 
KOKKOS_FUNCTION index_type size () const
 Get the total data array size. More...
 
KOKKOS_FUNCTION index_type n () const
 Get the total outer array size. More...
 
KOKKOS_FUNCTION index_type n (unsigned int dim) const
 Get the size of a dimension of the outer array. More...
 
KOKKOS_FUNCTION auto operator[] (index_type i) const
 Get an inner array. More...
 
template<typename... indices>
KOKKOS_FUNCTION auto operator() (indices... i) const
 Get an inner array. More...
 

Protected Attributes

Array1D< T, index_type > _data
 Sequential data array. More...
 
Array< JaggedArrayInnerDim< inner >, outer, index_type > _dims
 Dimension information of each inner array. More...
 
Array< index_type, outer, index_type > _offsets
 Starting offset of each inner array into the sequential data array. More...
 
bool _finalized = false
 Whether the array was finalized. More...
 

Detailed Description

template<typename T, unsigned int inner, unsigned int outer, typename index_type = MOOSE_KOKKOS_INDEX_TYPE, LayoutType layout = LayoutType::LEFT>
class Moose::Kokkos::JaggedArray< T, inner, outer, index_type, layout >

The Kokkos jagged array class.

A Kokkos jagged array aids in treating jagged arrays conveniently and efficiently by using a sequential data storage and providing multi-dimensional indexing using internal dope vectors. A jagged array is divided into the inner and outer arrays. The outer array is the regular part of a jagged array. Each entry of the outer array can hold an inner array, whose size can vary with each other. Calling create() will allocate the outer array, and inner arrays should be reserved one-by-one through reserve(). Once the array structure is set, finalize() should be called. A finalized array cannot be restructured unless it is reset completely by calling create() again. The inner array returned by operator() or operator[] using the outer array index is held in a temporary wrapper object. To avoid the overhead of temporary object creation, it is recommended to store the object locally.

Template Parameters
TThe data type
innerThe inner array dimension size
outerThe outer array dimension size
index_typeThe array index type
layoutThe memory layout type

Definition at line 164 of file KokkosJaggedArray.h.

Constructor & Destructor Documentation

◆ JaggedArray() [1/2]

template<typename T , unsigned int inner, unsigned int outer, typename index_type = MOOSE_KOKKOS_INDEX_TYPE, LayoutType layout = LayoutType::LEFT>
Moose::Kokkos::JaggedArray< T, inner, outer, index_type, layout >::JaggedArray ( )
default

Default constructor.

◆ JaggedArray() [2/2]

template<typename T , unsigned int inner, unsigned int outer, typename index_type = MOOSE_KOKKOS_INDEX_TYPE, LayoutType layout = LayoutType::LEFT>
template<typename... size_type>
Moose::Kokkos::JaggedArray< T, inner, outer, index_type, layout >::JaggedArray ( size_type...  n)
inline

Constructor.

Definition at line 177 of file KokkosJaggedArray.h.

178  {
179  create(n...);
180  }
KOKKOS_FUNCTION index_type n() const
Get the total outer array size.
void create(const std::vector< index_type > &n)
Allocate outer array.

Member Function Documentation

◆ array()

template<typename T , unsigned int inner, unsigned int outer, typename index_type = MOOSE_KOKKOS_INDEX_TYPE, LayoutType layout = LayoutType::LEFT>
auto& Moose::Kokkos::JaggedArray< T, inner, outer, index_type, layout >::array ( )
inline

Get the underlying data array.

Returns
The data array

Definition at line 248 of file KokkosJaggedArray.h.

248 { return _data; }
Array1D< T, index_type > _data
Sequential data array.

◆ copyToDevice()

template<typename T , unsigned int inner, unsigned int outer, typename index_type = MOOSE_KOKKOS_INDEX_TYPE, LayoutType layout = LayoutType::LEFT>
void Moose::Kokkos::JaggedArray< T, inner, outer, index_type, layout >::copyToDevice ( )
inline

Copy data from host to device.

Definition at line 207 of file KokkosJaggedArray.h.

208  {
209  mooseAssert(_finalized, "KokkosJaggedArray not finalized.");
210 
211  _data.copyToDevice();
212  }
bool _finalized
Whether the array was finalized.
Array1D< T, index_type > _data
Sequential data array.

◆ copyToHost()

template<typename T , unsigned int inner, unsigned int outer, typename index_type = MOOSE_KOKKOS_INDEX_TYPE, LayoutType layout = LayoutType::LEFT>
void Moose::Kokkos::JaggedArray< T, inner, outer, index_type, layout >::copyToHost ( )
inline

Copy data from device to host.

Definition at line 216 of file KokkosJaggedArray.h.

217  {
218  mooseAssert(_finalized, "KokkosJaggedArray not finalized.");
219 
220  _data.copyToHost();
221  }
bool _finalized
Whether the array was finalized.
Array1D< T, index_type > _data
Sequential data array.

◆ create() [1/2]

template<typename T , unsigned int inner, unsigned int outer, typename index_type , LayoutType layout>
void Moose::Kokkos::JaggedArray< T, inner, outer, index_type, layout >::create ( const std::vector< index_type > &  n)

Allocate outer array.

Parameters
nThe vector containing the size of each dimension for the outer array

Definition at line 322 of file KokkosJaggedArray.h.

Referenced by Moose::Kokkos::JaggedArray< T, inner, outer, index_type, layout >::JaggedArray().

323 {
324  _data.destroy();
325  _offsets.create(n);
326  _dims.create(n);
327 
328  _finalized = false;
329 }
bool _finalized
Whether the array was finalized.
KOKKOS_FUNCTION index_type n() const
Get the total outer array size.
Array1D< T, index_type > _data
Sequential data array.
Array< JaggedArrayInnerDim< inner >, outer, index_type > _dims
Dimension information of each inner array.
Array< index_type, outer, index_type > _offsets
Starting offset of each inner array into the sequential data array.
void create(const std::vector< index_type > &n)
Allocate array on host and device.
Definition: KokkosArray.h:303

◆ create() [2/2]

template<typename T , unsigned int inner, unsigned int outer, typename index_type , LayoutType layout>
template<typename... size_type>
void Moose::Kokkos::JaggedArray< T, inner, outer, index_type, layout >::create ( size_type...  n)

Allocate outer array.

Parameters
nThe size of each dimension for the outer array

Definition at line 338 of file KokkosJaggedArray.h.

339 {
340  _data.destroy();
341  _offsets.create(n...);
342  _dims.create(n...);
343 
344  _finalized = false;
345 }
bool _finalized
Whether the array was finalized.
KOKKOS_FUNCTION index_type n() const
Get the total outer array size.
Array1D< T, index_type > _data
Sequential data array.
Array< JaggedArrayInnerDim< inner >, outer, index_type > _dims
Dimension information of each inner array.
Array< index_type, outer, index_type > _offsets
Starting offset of each inner array into the sequential data array.
void create(const std::vector< index_type > &n)
Allocate array on host and device.
Definition: KokkosArray.h:303

◆ finalize()

template<typename T , unsigned int inner, unsigned int outer, typename index_type , LayoutType layout>
void Moose::Kokkos::JaggedArray< T, inner, outer, index_type, layout >::finalize ( )

Setup array structure.

Definition at line 394 of file KokkosJaggedArray.h.

395 {
396  mooseAssert(!_finalized, "KokkosJaggedArray already finalized.");
397 
398  index_type stride = 1;
399 
400  for (index_type o = 0; o < _offsets.size(); ++o)
401  {
402  stride = 1;
403 
404  for (unsigned int i = 0; i < inner; ++i)
405  stride *= _dims[o][i];
406 
407  _offsets[o] = stride;
408  }
409 
410  std::exclusive_scan(_offsets.begin(), _offsets.end(), _offsets.begin(), 0);
411 
412  _dims.copyToDevice();
414 
415  // Pad an extra element at the end to avoid accessing the bound in the following operators when
416  // the last inner array has zero size
417  _data.create(_offsets.last() + stride + 1);
418 
419  _finalized = true;
420 }
bool _finalized
Whether the array was finalized.
KOKKOS_FUNCTION T & last() const
Get the last element.
Definition: KokkosArray.h:239
Array1D< T, index_type > _data
Sequential data array.
Array< JaggedArrayInnerDim< inner >, outer, index_type > _dims
Dimension information of each inner array.
KOKKOS_FUNCTION iterator end() const
Get the end iterator.
Definition: KokkosArray.h:489
Array< index_type, outer, index_type > _offsets
Starting offset of each inner array into the sequential data array.
void copyToDevice()
Copy data from host to device.
Definition: KokkosArray.h:947
KOKKOS_FUNCTION index_type size() const
Get the total array size.
Definition: KokkosArray.h:205
KOKKOS_FUNCTION iterator begin() const
Get the beginning iterator.
Definition: KokkosArray.h:479

◆ isDeviceAlloc()

template<typename T , unsigned int inner, unsigned int outer, typename index_type = MOOSE_KOKKOS_INDEX_TYPE, LayoutType layout = LayoutType::LEFT>
KOKKOS_FUNCTION bool Moose::Kokkos::JaggedArray< T, inner, outer, index_type, layout >::isDeviceAlloc ( ) const
inline

Get whether the array was allocated on device.

Returns
Whether the array was allocated on device

Definition at line 264 of file KokkosJaggedArray.h.

264 { return _data.isDeviceAlloc(); }
Array1D< T, index_type > _data
Sequential data array.

◆ isFinalized()

template<typename T , unsigned int inner, unsigned int outer, typename index_type = MOOSE_KOKKOS_INDEX_TYPE, LayoutType layout = LayoutType::LEFT>
KOKKOS_FUNCTION bool Moose::Kokkos::JaggedArray< T, inner, outer, index_type, layout >::isFinalized ( ) const
inline

Get whether the array is finalized.

Returns
Whether the array is finalized

Definition at line 254 of file KokkosJaggedArray.h.

254 { return _finalized; }
bool _finalized
Whether the array was finalized.

◆ isHostAlloc()

template<typename T , unsigned int inner, unsigned int outer, typename index_type = MOOSE_KOKKOS_INDEX_TYPE, LayoutType layout = LayoutType::LEFT>
KOKKOS_FUNCTION bool Moose::Kokkos::JaggedArray< T, inner, outer, index_type, layout >::isHostAlloc ( ) const
inline

Get whether the array was allocated on host.

Returns
Whether the array was allocated on host

Definition at line 259 of file KokkosJaggedArray.h.

259 { return _data.isHostAlloc(); }
Array1D< T, index_type > _data
Sequential data array.

◆ moveToDevice()

template<typename T , unsigned int inner, unsigned int outer, typename index_type = MOOSE_KOKKOS_INDEX_TYPE, LayoutType layout = LayoutType::LEFT>
void Moose::Kokkos::JaggedArray< T, inner, outer, index_type, layout >::moveToDevice ( )
inline

Copy data from host to device and deallocate host.

Definition at line 225 of file KokkosJaggedArray.h.

226  {
227  mooseAssert(_finalized, "KokkosJaggedArray not finalized.");
228 
229  _dims.moveToDevice();
231  _data.moveToDevice();
232  }
bool _finalized
Whether the array was finalized.
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.
Definition: KokkosArray.h:997
Array< index_type, outer, index_type > _offsets
Starting offset of each inner array into the sequential data array.

◆ moveToHost()

template<typename T , unsigned int inner, unsigned int outer, typename index_type = MOOSE_KOKKOS_INDEX_TYPE, LayoutType layout = LayoutType::LEFT>
void Moose::Kokkos::JaggedArray< T, inner, outer, index_type, layout >::moveToHost ( )
inline

Copy data from device to host and deallocate device.

Definition at line 236 of file KokkosJaggedArray.h.

237  {
238  mooseAssert(_finalized, "KokkosJaggedArray not finalized.");
239 
240  _dims.moveToHost();
242  _data.moveToHost();
243  }
bool _finalized
Whether the array was finalized.
void moveToHost(bool should_free_device=true)
Copy data from device to host and deallocate device.
Definition: KokkosArray.h:1014
Array1D< T, index_type > _data
Sequential data array.
Array< JaggedArrayInnerDim< inner >, outer, index_type > _dims
Dimension information of each inner array.
Array< index_type, outer, index_type > _offsets
Starting offset of each inner array into the sequential data array.

◆ n() [1/2]

template<typename T , unsigned int inner, unsigned int outer, typename index_type = MOOSE_KOKKOS_INDEX_TYPE, LayoutType layout = LayoutType::LEFT>
KOKKOS_FUNCTION index_type Moose::Kokkos::JaggedArray< T, inner, outer, index_type, layout >::n ( ) const
inline

Get the total outer array size.

Returns
The total outer array size

Definition at line 274 of file KokkosJaggedArray.h.

Referenced by Moose::Kokkos::JaggedArray< T, inner, outer, index_type, layout >::JaggedArray().

274 { return _offsets.size(); }
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 array size.
Definition: KokkosArray.h:205

◆ n() [2/2]

template<typename T , unsigned int inner, unsigned int outer, typename index_type = MOOSE_KOKKOS_INDEX_TYPE, LayoutType layout = LayoutType::LEFT>
KOKKOS_FUNCTION index_type Moose::Kokkos::JaggedArray< T, inner, outer, index_type, layout >::n ( unsigned int  dim) const
inline

Get the size of a dimension of the outer array.

Parameters
dimThe dimension index
Returns
The size of the dimension of the outer array

Definition at line 280 of file KokkosJaggedArray.h.

280 { return _offsets.n(dim); }
KOKKOS_FUNCTION index_type n(unsigned int dim) const
Get the size of a dimension.
Definition: KokkosArray.h:211
static constexpr std::size_t dim
This is the dimension of all vector and tensor datastructures used in MOOSE.
Definition: Moose.h:163
Array< index_type, outer, index_type > _offsets
Starting offset of each inner array into the sequential data array.

◆ operator()()

template<typename T , unsigned int inner, unsigned int outer, typename index_type , LayoutType layout>
template<typename... indices>
KOKKOS_FUNCTION auto Moose::Kokkos::JaggedArray< T, inner, outer, index_type, layout >::operator() ( indices...  i) const

Get an inner array.

Parameters
iThe index of each dimension
Returns
The inner array wrapper object

Definition at line 443 of file KokkosJaggedArray.h.

444 {
445  auto data = &_data[_offsets(i...)];
446  const auto & dim = _dims(i...);
447 
448  return JaggedArrayInnerData<T, inner, layout>(data, dim);
449 }
Array1D< T, index_type > _data
Sequential data array.
Array< JaggedArrayInnerDim< inner >, outer, index_type > _dims
Dimension information of each inner array.
static constexpr std::size_t dim
This is the dimension of all vector and tensor datastructures used in MOOSE.
Definition: Moose.h:163
Array< index_type, outer, index_type > _offsets
Starting offset of each inner array into the sequential data array.

◆ operator[]()

template<typename T , unsigned int inner, unsigned int outer, typename index_type , LayoutType layout>
KOKKOS_FUNCTION auto Moose::Kokkos::JaggedArray< T, inner, outer, index_type, layout >::operator[] ( index_type  i) const

Get an inner array.

Parameters
iThe dimensionless outer array index
Returns
The inner array wrapper object

Definition at line 428 of file KokkosJaggedArray.h.

429 {
430  auto data = &_data[_offsets[i]];
431  const auto & dim = _dims[i];
432 
433  return JaggedArrayInnerData<T, inner, layout>(data, dim);
434 }
Array1D< T, index_type > _data
Sequential data array.
Array< JaggedArrayInnerDim< inner >, outer, index_type > _dims
Dimension information of each inner array.
static constexpr std::size_t dim
This is the dimension of all vector and tensor datastructures used in MOOSE.
Definition: Moose.h:163
Array< index_type, outer, index_type > _offsets
Starting offset of each inner array into the sequential data array.

◆ reserve()

template<typename T , unsigned int inner, unsigned int outer, typename index_type , LayoutType layout>
void Moose::Kokkos::JaggedArray< T, inner, outer, index_type, layout >::reserve ( const std::array< uint64_t, outer > &  index,
const std::array< uint64_t, inner > &  dimension 
)

Reserve inner array for an outer array entry.

Parameters
indexThe array containing the index for the outer array
dimensionThe array containing the size of each dimension for the inner array

Definition at line 353 of file KokkosJaggedArray.h.

355 {
356  mooseAssert(!_finalized, "KokkosJaggedArray already finalized.");
357 
358  index_type idx = 0;
359  index_type stride = 1;
360 
361  for (unsigned int o = 0; o < outer; ++o)
362  {
363  idx += index[o] * stride;
364  stride *= _offsets.n(o);
365  }
366 
367  for (unsigned int i = 0; i < inner; ++i)
368  _dims[idx].dim[i] = dimension[i];
369 
370  stride = 1;
371 
372  if constexpr (layout == LayoutType::LEFT)
373  for (unsigned int i = 0; i < inner; ++i)
374  {
375  _dims[idx].stride[i] = stride;
376  stride *= dimension[i];
377  }
378  else
379  for (int i = inner - 1; i >= 0; --i)
380  {
381  _dims[idx].stride[i] = stride;
382  stride *= dimension[i];
383  }
384 
385  _dims[idx].stride[inner] = stride;
386 }
KOKKOS_FUNCTION index_type n(unsigned int dim) const
Get the size of a dimension.
Definition: KokkosArray.h:211
bool _finalized
Whether the array was finalized.
Array< JaggedArrayInnerDim< inner >, outer, index_type > _dims
Dimension information of each inner array.
static constexpr std::size_t dim
This is the dimension of all vector and tensor datastructures used in MOOSE.
Definition: Moose.h:163
Array< index_type, outer, index_type > _offsets
Starting offset of each inner array into the sequential data array.
unsigned int idx(const ElemType type, const unsigned int nx, const unsigned int i, const unsigned int j)

◆ size()

template<typename T , unsigned int inner, unsigned int outer, typename index_type = MOOSE_KOKKOS_INDEX_TYPE, LayoutType layout = LayoutType::LEFT>
KOKKOS_FUNCTION index_type Moose::Kokkos::JaggedArray< T, inner, outer, index_type, layout >::size ( ) const
inline

Get the total data array size.

Returns
The total data array size

Definition at line 269 of file KokkosJaggedArray.h.

269 { return _data.size(); }
Array1D< T, index_type > _data
Sequential data array.

Member Data Documentation

◆ _data

template<typename T , unsigned int inner, unsigned int outer, typename index_type = MOOSE_KOKKOS_INDEX_TYPE, LayoutType layout = LayoutType::LEFT>
Array1D<T, index_type> Moose::Kokkos::JaggedArray< T, inner, outer, index_type, layout >::_data
protected

◆ _dims

template<typename T , unsigned int inner, unsigned int outer, typename index_type = MOOSE_KOKKOS_INDEX_TYPE, LayoutType layout = LayoutType::LEFT>
Array<JaggedArrayInnerDim<inner>, outer, index_type> Moose::Kokkos::JaggedArray< T, inner, outer, index_type, layout >::_dims
protected

◆ _finalized

template<typename T , unsigned int inner, unsigned int outer, typename index_type = MOOSE_KOKKOS_INDEX_TYPE, LayoutType layout = LayoutType::LEFT>
bool Moose::Kokkos::JaggedArray< T, inner, outer, index_type, layout >::_finalized = false
protected

◆ _offsets

template<typename T , unsigned int inner, unsigned int outer, typename index_type = MOOSE_KOKKOS_INDEX_TYPE, LayoutType layout = LayoutType::LEFT>
Array<index_type, outer, index_type> Moose::Kokkos::JaggedArray< T, inner, outer, index_type, layout >::_offsets
protected

The documentation for this class was generated from the following file: