https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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.
 
template<typename... size_type>
 JaggedArray (size_type... n)
 Constructor.
 
void create (const std::vector< index_type > &n)
 Allocate outer array.
 
template<typename... size_type>
void create (size_type... n)
 Allocate outer 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.
 
void finalize ()
 Setup array structure.
 
void copyToDevice ()
 Copy data from host to device.
 
void copyToHost ()
 Copy data from device to host.
 
void moveToDevice ()
 Copy data from host to device and deallocate host.
 
void moveToHost ()
 Copy data from device to host and deallocate device.
 
auto & array ()
 Get the underlying data array.
 
KOKKOS_FUNCTION bool isFinalized () const
 Get whether the array is finalized.
 
KOKKOS_FUNCTION bool isHostAlloc () const
 Get whether the array was allocated on host.
 
KOKKOS_FUNCTION bool isDeviceAlloc () const
 Get whether the array was allocated on device.
 
KOKKOS_FUNCTION index_type size () const
 Get the total data array size.
 
KOKKOS_FUNCTION index_type n () const
 Get the total outer array size.
 
KOKKOS_FUNCTION index_type n (unsigned int dim) const
 Get the size of a dimension of the outer array.
 
KOKKOS_FUNCTION auto operator[] (index_type i) const
 Get an inner array.
 
template<typename... indices>
KOKKOS_FUNCTION auto operator() (indices... i) const
 Get an inner array.
 

Protected Attributes

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.
 
bool _finalized = false
 Whether the array was finalized.
 

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 249 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 262 of file KokkosJaggedArray.h.

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

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 333 of file KokkosJaggedArray.h.

333{ 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 292 of file KokkosJaggedArray.h.

293 {
294 mooseAssert(_finalized, "KokkosJaggedArray not finalized.");
295
296 _data.copyToDevice();
297 }
bool _finalized
Whether the array was finalized.

◆ 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 301 of file KokkosJaggedArray.h.

302 {
303 mooseAssert(_finalized, "KokkosJaggedArray not finalized.");
304
305 _data.copyToHost();
306 }

◆ 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 407 of file KokkosJaggedArray.h.

408{
409 _data.destroy();
410 _offsets.create(n);
411 _dims.create(n);
412
413 _finalized = false;
414}
Array< index_type, outer, index_type > _offsets
Starting offset of each inner array into the sequential data array.
Array< JaggedArrayInnerDim< inner >, outer, index_type > _dims
Dimension information of each inner array.

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

◆ 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 423 of file KokkosJaggedArray.h.

424{
425 _data.destroy();
426 _offsets.create(n...);
427 _dims.create(n...);
428
429 _finalized = false;
430}

◆ 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 479 of file KokkosJaggedArray.h.

480{
481 mooseAssert(!_finalized, "KokkosJaggedArray already finalized.");
482
483 index_type stride = 1;
484
485 for (index_type o = 0; o < _offsets.size(); ++o)
486 {
487 stride = 1;
488
489 for (unsigned int i = 0; i < inner; ++i)
490 stride *= _dims[o][i];
491
492 _offsets[o] = stride;
493 }
494
495 std::exclusive_scan(_offsets.begin(), _offsets.end(), _offsets.begin(), 0);
496
497 _dims.copyToDevice();
498 _offsets.copyToDevice();
499
500 // Pad an extra element at the end to avoid accessing the bound in the following operators when
501 // the last inner array has zero size. An empty outer array, e.g. on a process without local
502 // elements, has no last offset.
503 _data.create((_offsets.size() ? _offsets.last() + stride : 0) + 1);
504
505 _finalized = true;
506}

◆ 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 349 of file KokkosJaggedArray.h.

349{ return _data.isDeviceAlloc(); }

◆ 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 339 of file KokkosJaggedArray.h.

339{ return _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 344 of file KokkosJaggedArray.h.

344{ return _data.isHostAlloc(); }

◆ 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 310 of file KokkosJaggedArray.h.

311 {
312 mooseAssert(_finalized, "KokkosJaggedArray not finalized.");
313
314 _dims.moveToDevice();
315 _offsets.moveToDevice();
316 _data.moveToDevice();
317 }

◆ 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 321 of file KokkosJaggedArray.h.

322 {
323 mooseAssert(_finalized, "KokkosJaggedArray not finalized.");
324
325 _dims.moveToHost();
326 _offsets.moveToHost();
327 _data.moveToHost();
328 }

◆ 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 359 of file KokkosJaggedArray.h.

359{ return _offsets.size(); }

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

◆ 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 365 of file KokkosJaggedArray.h.

365{ return _offsets.n(dim); }
static constexpr std::size_t dim
This is the dimension of all vector and tensor datastructures used in MOOSE.
Definition Moose.h:175

◆ 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 529 of file KokkosJaggedArray.h.

530{
531 auto data = &_data[_offsets(i...)];
532 const auto & dim = _dims(i...);
533
534 return JaggedArrayInnerData<T, inner, layout>(data, dim);
535}

◆ 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 514 of file KokkosJaggedArray.h.

515{
516 auto data = &_data[_offsets[i]];
517 const auto & dim = _dims[i];
518
519 return JaggedArrayInnerData<T, inner, layout>(data, dim);
520}

◆ 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 438 of file KokkosJaggedArray.h.

440{
441 mooseAssert(!_finalized, "KokkosJaggedArray already finalized.");
442
443 index_type idx = 0;
444 index_type stride = 1;
445
446 for (unsigned int o = 0; o < outer; ++o)
447 {
448 idx += index[o] * stride;
449 stride *= _offsets.n(o);
450 }
451
452 for (unsigned int i = 0; i < inner; ++i)
453 _dims[idx].dim[i] = dimension[i];
454
455 stride = 1;
456
457 if constexpr (layout == LayoutType::LEFT)
458 for (unsigned int i = 0; i < inner; ++i)
459 {
460 _dims[idx].stride[i] = stride;
461 stride *= dimension[i];
462 }
463 else
464 for (int i = inner - 1; i >= 0; --i)
465 {
466 _dims[idx].stride[i] = stride;
467 stride *= dimension[i];
468 }
469
470 _dims[idx].stride[inner] = stride;
471}
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 354 of file KokkosJaggedArray.h.

354{ return _data.size(); }

Referenced by Moose::Kokkos::Mesh::getNumSideBoundaryIDs().

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: