https://mooseframework.inl.gov
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
Moose::Kokkos::Array< T, dimension, index_type, layout > Class Template Reference

The Kokkos array class. More...

#include <KokkosArray.h>

Inheritance diagram for Moose::Kokkos::Array< T, dimension, index_type, layout >:
[legend]

Public Types

using unsigned_index_type = index_type
 
using signed_index_type = typename std::make_signed< index_type >::type
 

Public Member Functions

 Array ()
 Default constructor.
 
 Array (const Array< T, dimension, index_type, layout > &array)
 Copy constructor.
 
template<typename... size_type>
 Array (size_type... n)
 Constructor Initialize and allocate array with given dimensions This allocates both host and device data.
 
auto & operator= (const Array< T, dimension, index_type, layout > &array)
 Shallow copy another Kokkos array.
 
template<typename... indices>
KOKKOS_FUNCTION T & operator() (indices... i) const
 Get an array entry.
 
KOKKOS_FUNCTION T & operator() (const signed_index_type(&idx)[dimension]) const
 Get an array entry using indices stored in an array.
 
void destroy ()
 Free all data and reset.
 
void shallowCopy (const ArrayBase< T, dimension, index_type > &array)
 Shallow copy another Kokkos array.
 
unsigned int useCount () const
 Get the reference count.
 
bool isSlotConstructed (index_type i) const
 Get whether slot i is tracked as constructed.
 
constructed_entry_range< false > constructedEntries ()
 Get host-side range over entries tracked as constructed.
 
constructed_entry_range< true > constructedEntries () const
 Get host-side range over entries tracked as constructed.
 
KOKKOS_FUNCTION bool isAlloc () const
 Get whether the array was allocated either on host or device.
 
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 bool isHostAlias () const
 Get whether the host array was aliased.
 
KOKKOS_FUNCTION bool isDeviceAlias () const
 Get whether the device array was aliased.
 
KOKKOS_FUNCTION index_type size () const
 Get the total array size.
 
KOKKOS_FUNCTION index_type n (unsigned int dim) const
 Get the size of a dimension.
 
KOKKOS_FUNCTION T * data () const
 Get the data pointer.
 
KOKKOS_FUNCTION T & first () const
 Get the first element.
 
KOKKOS_FUNCTION T & last () const
 Get the last element.
 
KOKKOS_FUNCTION T & operator[] (index_type i) const
 Get an array entry.
 
T * hostData () const
 Get the host data pointer.
 
T * deviceData () const
 Get the device data pointer.
 
auto hostView () const
 Get the host unmanaged view.
 
auto deviceView () const
 Get the device unmanaged view.
 
template<typename... size_type>
void init (size_type... n)
 Initialize array with given dimensions but do not allocate.
 
template<bool initialize = true>
void create (const std::vector< index_type > &n)
 Allocate array on host and device.
 
template<bool initialize = true, typename... size_type>
void create (size_type... n)
 Allocate array on host and device.
 
template<bool initialize = true>
void createHost (const std::vector< index_type > &n)
 Allocate array on host only.
 
template<bool initialize = true, typename... size_type>
void createHost (size_type... n)
 Allocate array on host only.
 
void createDevice (const std::vector< index_type > &n)
 Allocate array on device only.
 
template<typename... size_type>
void createDevice (size_type... n)
 Allocate array on device only.
 
void aliasHost (T *ptr)
 Point the host data to an external data instead of allocating it.
 
void aliasDevice (T *ptr)
 Point the device data to an external data instead of allocating it.
 
void offset (const std::vector< signed_index_type > &d)
 Apply starting index offsets to each dimension.
 
template<typename... offset_type>
void offset (offset_type... d)
 Apply starting index offsets to each dimension.
 
void copyToDevice ()
 Copy data from host to device.
 
void copyToHost ()
 Copy data from device to host.
 
void copyIn (const T *ptr, MemcpyType dir, index_type n, index_type offset=0)
 Copy data from an external data to this array.
 
void copyOut (T *ptr, MemcpyType dir, index_type n, index_type offset=0)
 Copy data to an external data from this array.
 
void copyToDeviceNested ()
 Copy all the nested Kokkos arrays including self from host to device.
 
void moveToDevice (bool should_free_host=true)
 Copy data from host to device and deallocate host.
 
void moveToHost (bool should_free_device=true)
 Copy data from device to host and deallocate device.
 
void deepCopy (const ArrayBase< T, dimension, index_type > &array)
 Deep copy another Kokkos array If ArrayDeepCopy<T>::value is true, it will copy-construct each entry If ArrayDeepCopy<T>::value is false, it will do a memory copy.
 
void swap (ArrayBase< T, dimension, index_type > &array)
 Swap with another Kokkos array.
 
KOKKOS_FUNCTION iterator begin () const
 Get the beginning iterator.
 
KOKKOS_FUNCTION iterator end () const
 Get the end iterator.
 

Protected Member Functions

template<bool host, bool device, bool initialize, typename... size_type>
void createInternal (size_type... n)
 The internal method to initialize and allocate this array.
 
template<bool host, bool device, bool initialize>
void createInternal (const std::vector< index_type > &n)
 The internal method to initialize and allocate this array.
 
template<bool initialize>
void createInternal (const std::vector< index_type > &n, bool host, bool device)
 The internal method to initialize and allocate this array.
 
template<typename TargetSpace , typename SourceSpace >
void copyInternal (T *target, const T *source, index_type n)
 The internal method to perform a memory copy.
 
template<typename... Args>
T & emplaceAt (index_type i, Args &&... args)
 Placement-new construct slot i from args, recording initialization.
 

Protected Attributes

index_type _n [dimension] = {0}
 Size of each dimension.
 
index_type _s [dimension] = {0}
 Stride of each dimension.
 
signed_index_type _d [dimension] = {0}
 Offset of each dimension.
 

Private Member Functions

 usingKokkosArrayBaseMembers (T, dimension, index_type)
 
template<bool initialize>
void allocHost ()
 Allocate host data for an initialized array that has not allocated host data.
 
void allocDevice ()
 Allocate device data for an initialized array that has not allocated device data.
 
void freeHost ()
 Free host data.
 
void freeDevice ()
 Free device data.
 
template<typename indices >
KOKKOS_FUNCTION index_type linearIndex (const indices(&idx)[dimension]) const
 Get the dimensionless index for indices stored in an array.
 
template<typename indices , unsigned int... i>
KOKKOS_FUNCTION index_type linearIndexHelper (const indices(&idx)[dimension], std::integer_sequence< unsigned int, i... >) const
 

Private Attributes

std::shared_ptr< unsigned int > _counter
 Reference counter.
 
std::shared_ptr< std::vector< bool > > _slots_constructed
 Non-null only for malloc-allocated arrays; tracks which slots have been emplace-constructed.
 
bool _is_init = false
 Flag whether array was initialized.
 
bool _is_host_alloc = false
 Flag whether host data was allocated.
 
bool _is_device_alloc = false
 Flag whether device data was allocated.
 
bool _is_host_alias = false
 Flag whether the host data points to an external data.
 
bool _is_device_alias = false
 Flag whether the device data points to an external data.
 
T * _host_data = nullptr
 Host data.
 
T * _device_data = nullptr
 Device data.
 
index_type _size = 0
 Total size.
 
const LayoutType _layout
 Memory layout type.
 
template<typename... indices>
KOKKOS_FUNCTION index_type linearIndex (indices... i) const
 Get the dimensionless index for a multi-dimensional index.
 
template<typename indices , typename... Args>
T & emplace (const indices(&idx)[dimension], Args &&... args)
 Placement-new construct an entry from args, recording initialization.
 

Detailed Description

template<typename T, unsigned int dimension, typename index_type, LayoutType layout>
class Moose::Kokkos::Array< T, dimension, index_type, layout >

The Kokkos array class.

The specialization of the Kokkos array class for each dimension.

All array data that needs to be accessed on device in Kokkos objects should use this class. If the array is populated on host and is to be accessed on device, make sure to call copyToDevice() after populating data. For a nested Kokkos array, either copyToDeviceNested() should be called for the outermost array or copyToDevice() should be called for each instance of Kokkos array from the innermost to the outermost. Do not store this object as reference in your Kokkos object if it is used on device, because the reference refers to a host object and therefore is not accessible on device. If storing it as a reference is required, see ReferenceWrapper.

Template Parameters
TThe data type
dimensionThe array dimension size
index_typeThe array index type
layoutThe memory layout type

Definition at line 1568 of file KokkosArray.h.

Member Typedef Documentation

◆ signed_index_type

template<typename T , unsigned int dimension, typename index_type >
using Moose::Kokkos::ArrayBase< T, dimension, index_type >::signed_index_type = typename std::make_signed<index_type>::type
inherited

Definition at line 115 of file KokkosArray.h.

◆ unsigned_index_type

template<typename T , unsigned int dimension, typename index_type >
using Moose::Kokkos::ArrayBase< T, dimension, index_type >::unsigned_index_type = index_type
inherited

Definition at line 114 of file KokkosArray.h.

Constructor & Destructor Documentation

◆ Array() [1/3]

template<typename T , unsigned int dimension, typename index_type , LayoutType layout>
Moose::Kokkos::Array< T, dimension, index_type, layout >::Array ( )
inline

Default constructor.

Definition at line 1578 of file KokkosArray.h.

1578: ArrayBase<T, dimension, index_type>(layout) {}

◆ Array() [2/3]

template<typename T , unsigned int dimension, typename index_type , LayoutType layout>
Moose::Kokkos::Array< T, dimension, index_type, layout >::Array ( const Array< T, dimension, index_type, layout > &  array)
inline

Copy constructor.

Definition at line 1582 of file KokkosArray.h.

1583 : ArrayBase<T, dimension, index_type>(array)
1584 {
1585 }

◆ Array() [3/3]

template<typename T , unsigned int dimension, typename index_type , LayoutType layout>
template<typename... size_type>
Moose::Kokkos::Array< T, dimension, index_type, layout >::Array ( size_type...  n)
inline

Constructor Initialize and allocate array with given dimensions This allocates both host and device data.

Parameters
nThe size of each dimension

Definition at line 1594 of file KokkosArray.h.

1594 : ArrayBase<T, dimension, index_type>(layout, n...)
1595 {
1596 }
KOKKOS_FUNCTION index_type n(unsigned int dim) const
Get the size of a dimension.

Member Function Documentation

◆ aliasDevice()

template<typename T , unsigned int dimension, typename index_type >
void Moose::Kokkos::ArrayBase< T, dimension, index_type >::aliasDevice ( T *  ptr)
inherited

Point the device data to an external data instead of allocating it.

Parameters
ptrThe pointer to the external device data

Definition at line 984 of file KokkosArray.h.

985{
986 if (!_is_init)
987 mooseError("Kokkos array error: attempted to alias device data before array initialization.");
988
990 mooseError("Kokkos array error: cannot alias device data because device data was not aliased.");
991
992 _device_data = ptr;
993 _is_device_alloc = true;
994 _is_device_alias = true;
995}
void mooseError(Args &&... args)
Emit an error message with the given stringified, concatenated args and terminate the application.
Definition MooseError.h:311
bool _is_init
Flag whether array was initialized.
T * _device_data
Device data.
bool _is_device_alias
Flag whether the device data points to an external data.
bool _is_device_alloc
Flag whether device data was allocated.

◆ aliasHost()

template<typename T , unsigned int dimension, typename index_type >
void Moose::Kokkos::ArrayBase< T, dimension, index_type >::aliasHost ( T *  ptr)
inherited

Point the host data to an external data instead of allocating it.

Parameters
ptrThe pointer to the external host data

Definition at line 969 of file KokkosArray.h.

970{
971 if (!_is_init)
972 mooseError("Kokkos array error: attempted to alias host data before array initialization.");
973
975 mooseError("Kokkos array error: cannot alias host data because host data was not aliased.");
976
977 _host_data = ptr;
978 _is_host_alloc = true;
979 _is_host_alias = true;
980}
bool _is_host_alias
Flag whether the host data points to an external data.
bool _is_host_alloc
Flag whether host data was allocated.

◆ allocDevice()

template<typename T , unsigned int dimension, typename index_type >
void Moose::Kokkos::ArrayBase< T, dimension, index_type >::allocDevice ( )
privateinherited

Allocate device data for an initialized array that has not allocated device data.

Definition at line 1024 of file KokkosArray.h.

1025{
1026 if (_is_device_alloc)
1027 return;
1028
1029 _device_data =
1030 static_cast<T *>(::Kokkos::kokkos_malloc<ExecSpace::memory_space>(_size * sizeof(T)));
1031
1032 _is_device_alloc = true;
1033}
index_type _size
Total size.

Referenced by Moose::Kokkos::ArrayBase< T, dimension, index_type >::copyToDevice(), and Moose::Kokkos::ArrayBase< T, dimension, index_type >::createInternal().

◆ allocHost()

template<typename T , unsigned int dimension, typename index_type >
template<bool initialize>
void Moose::Kokkos::ArrayBase< T, dimension, index_type >::allocHost ( )
privateinherited

Allocate host data for an initialized array that has not allocated host data.

Template Parameters
initializeWhether to initialize host data (calls default constructor)

Definition at line 1000 of file KokkosArray.h.

1001{
1002 if (_is_host_alloc)
1003 return;
1004
1005 if constexpr (initialize)
1006 {
1007 static_assert(
1008 std::is_default_constructible<T>::value,
1009 "Data type is not default-constructible. Initialization argument should be set to false.");
1010
1011 _host_data = new T[_size];
1012 }
1013 else
1014 {
1015 _host_data = static_cast<T *>(std::malloc(_size * sizeof(T)));
1016 _slots_constructed = std::make_shared<std::vector<bool>>(_size, false);
1017 }
1018
1019 _is_host_alloc = true;
1020}
std::shared_ptr< std::vector< bool > > _slots_constructed
Non-null only for malloc-allocated arrays; tracks which slots have been emplace-constructed.
void initialize(EquationSystems &es, const std::string &system_name)

◆ begin()

template<typename T , unsigned int dimension, typename index_type >
KOKKOS_FUNCTION iterator Moose::Kokkos::ArrayBase< T, dimension, index_type >::begin ( ) const
inlineinherited

Get the beginning iterator.

Returns
The beginning iterator

Definition at line 572 of file KokkosArray.h.

573 {
574 KOKKOS_IF_ON_HOST(return iterator(_host_data);)
575
576 return iterator(_device_data);
577 }

◆ constructedEntries() [1/2]

template<typename T , unsigned int dimension, typename index_type >
ArrayBase< T, dimension, index_type >::template constructed_entry_range< false > Moose::Kokkos::ArrayBase< T, dimension, index_type >::constructedEntries ( )
inherited

Get host-side range over entries tracked as constructed.

Returns
Range that skips slots for which isSlotConstructed() is false

Definition at line 806 of file KokkosArray.h.

807{
808 return constructed_entry_range<false>(*this);
809}

◆ constructedEntries() [2/2]

template<typename T , unsigned int dimension, typename index_type >
ArrayBase< T, dimension, index_type >::template constructed_entry_range< true > Moose::Kokkos::ArrayBase< T, dimension, index_type >::constructedEntries ( ) const
inherited

Get host-side range over entries tracked as constructed.

Returns
Range that skips slots for which isSlotConstructed() is false

Definition at line 813 of file KokkosArray.h.

814{
815 return constructed_entry_range<true>(*this);
816}

◆ copyIn()

template<typename T , unsigned int dimension, typename index_type >
void Moose::Kokkos::ArrayBase< T, dimension, index_type >::copyIn ( const T *  ptr,
MemcpyType  dir,
index_type  n,
index_type  offset = 0 
)
inherited

Copy data from an external data to this array.

Parameters
ptrThe pointer to the external data
dirThe copy direction
nThe number of entries to copy
offsetThe starting offset of this array

Definition at line 1252 of file KokkosArray.h.

1256{
1257 if (n > _size)
1258 mooseError("Kokkos array error: cannot copy in data larger than the array size.");
1259
1260 if (offset > _size)
1261 mooseError("Kokkos array error: offset cannot be larger than the array size.");
1262
1263 if (dir == MemcpyType::HOST_TO_HOST)
1264 {
1265 // If host side memory is not allocated, print error
1266 if (!_is_host_alloc)
1267 mooseError(
1268 "Kokkos array error: cannot copy in to the array because host memory was not allocated.");
1269
1270 // Copy from host to host
1271 copyInternal<::Kokkos::HostSpace, ::Kokkos::HostSpace>(_host_data + offset, ptr, n);
1272 }
1273 else if (dir == MemcpyType::HOST_TO_DEVICE)
1274 {
1275 // If device side memory is not allocated, print error
1276 if (!_is_device_alloc)
1277 mooseError("Kokkos array error: cannot copy in to the array because device memory was not "
1278 "allocated.");
1279
1280 // Copy from host to device
1281 copyInternal<MemSpace, ::Kokkos::HostSpace>(_device_data + offset, ptr, n);
1282 }
1283 else if (dir == MemcpyType::DEVICE_TO_HOST)
1284 {
1285 // If host side memory is not allocated, print error
1286 if (!_is_host_alloc)
1287 mooseError(
1288 "Kokkos array error: cannot copy in to the array because host memory was not allocated.");
1289
1290 // Copy from device to host
1291 copyInternal<::Kokkos::HostSpace, MemSpace>(_host_data + offset, ptr, n);
1292 }
1293 else if (dir == MemcpyType::DEVICE_TO_DEVICE)
1294 {
1295 // If device side memory is not allocated, print error
1296 if (!_is_device_alloc)
1297 mooseError("Kokkos array error: cannot copy in to the array because device memory was not "
1298 "allocated.");
1299
1300 // Copy from device to device
1301 copyInternal<MemSpace, MemSpace>(_device_data + offset, ptr, n);
1302 }
1303}
void offset(const std::vector< signed_index_type > &d)
Apply starting index offsets to each dimension.

Referenced by Moose::Kokkos::dataLoad().

◆ copyInternal()

template<typename T , unsigned int dimension, typename index_type >
template<typename TargetSpace , typename SourceSpace >
void Moose::Kokkos::ArrayBase< T, dimension, index_type >::copyInternal ( T *  target,
const T *  source,
index_type  n 
)
protectedinherited

The internal method to perform a memory copy.

Template Parameters
TargetSpaceThe Kokkos memory space of target data
SourcespaceThe Kokkos memory space of source data
Parameters
targetThe pointer to the target data
sourceThe pointer to the source data
nThe number of entries to copy

Definition at line 1133 of file KokkosArray.h.

1134{
1135 ::Kokkos::Impl::DeepCopy<TargetSpace, SourceSpace>(target, source, n * sizeof(T));
1136 ::Kokkos::fence();
1137}

◆ copyOut()

template<typename T , unsigned int dimension, typename index_type >
void Moose::Kokkos::ArrayBase< T, dimension, index_type >::copyOut ( T *  ptr,
MemcpyType  dir,
index_type  n,
index_type  offset = 0 
)
inherited

Copy data to an external data from this array.

Parameters
ptrThe pointer to the external data
dirThe copy direction
nThe number of entries to copy
offsetThe starting offset of this array

Definition at line 1307 of file KokkosArray.h.

1311{
1312 if (n > _size)
1313 mooseError("Kokkos array error: cannot copy out data larger than the array size.");
1314
1315 if (offset > _size)
1316 mooseError("Kokkos array error: offset cannot be larger than the array size.");
1317
1318 if (dir == MemcpyType::HOST_TO_HOST)
1319 {
1320 // If host side memory is not allocated, print error
1321 if (!_is_host_alloc)
1322 mooseError("Kokkos array error: cannot copy out from the array because host memory was not "
1323 "allocated.");
1324
1325 // Copy from host to host
1326 copyInternal<::Kokkos::HostSpace, ::Kokkos::HostSpace>(ptr, _host_data + offset, n);
1327 }
1328 else if (dir == MemcpyType::HOST_TO_DEVICE)
1329 {
1330 // If host side memory is not allocated, print error
1331 if (!_is_host_alloc)
1332 mooseError("Kokkos array error: cannot copy out from the array because host memory was not "
1333 "allocated.");
1334
1335 // Copy from host to device
1336 copyInternal<MemSpace, ::Kokkos::HostSpace>(ptr, _host_data + offset, n);
1337 }
1338 else if (dir == MemcpyType::DEVICE_TO_HOST)
1339 {
1340 // If device side memory is not allocated, print error
1341 if (!_is_device_alloc)
1342 mooseError("Kokkos array error: cannot copy out from the array because device memory was not "
1343 "allocated.");
1344
1345 // Copy from device to host
1346 copyInternal<::Kokkos::HostSpace, MemSpace>(ptr, _device_data + offset, n);
1347 }
1348 else if (dir == MemcpyType::DEVICE_TO_DEVICE)
1349 {
1350 // If device side memory is not allocated, print error
1351 if (!_is_device_alloc)
1352 mooseError("Kokkos array error: cannot copy out from the array because device memory was not "
1353 "allocated.");
1354
1355 // Copy from device to device
1356 copyInternal<MemSpace, MemSpace>(ptr, _device_data + offset, n);
1357 }
1358}

Referenced by Moose::Kokkos::dataStore().

◆ copyToDevice()

template<typename T , unsigned int dimension, typename index_type >
void Moose::Kokkos::ArrayBase< T, dimension, index_type >::copyToDevice ( )
inherited

Copy data from host to device.

Definition at line 1171 of file KokkosArray.h.

1172{
1173 // If host side memory is not allocated, do nothing
1174 if (!_is_host_alloc)
1175 return;
1176
1177 // If device side memory is not allocated,
1178 if (!_is_device_alloc)
1179 {
1180 if (_counter.use_count() == 1)
1181 // allocate memory if this array is not shared with other arrays
1182 allocDevice();
1183 else
1184 // print error if this array is shared with other arrays
1185 mooseError("Kokkos array error: cannot copy from host to device because device memory "
1186 "was not allocated. Cannot allocate device memory for copy because the array is "
1187 "being shared.");
1188 }
1189
1190 // Copy from host to device
1191 copyInternal<MemSpace, ::Kokkos::HostSpace>(_device_data, _host_data, _size);
1192}
std::shared_ptr< unsigned int > _counter
Reference counter.
void allocDevice()
Allocate device data for an initialized array that has not allocated device data.

Referenced by Moose::Kokkos::ArrayBase< T, dimension, index_type >::copyToDeviceNested(), Moose::Kokkos::dataLoad(), Moose::Kokkos::ArrayBase< T, dimension, index_type >::deepCopy(), Moose::Kokkos::ArrayBase< T, dimension, index_type >::moveToDevice(), Moose::Kokkos::VariableGradientTempl< is_ad >::VariableGradientTempl(), and Moose::Kokkos::VariableValueTempl< is_ad >::VariableValueTempl().

◆ copyToDeviceNested()

template<typename T , unsigned int dimension, typename index_type >
void Moose::Kokkos::ArrayBase< T, dimension, index_type >::copyToDeviceNested ( )
inherited

Copy all the nested Kokkos arrays including self from host to device.

Definition at line 1375 of file KokkosArray.h.

1376{
1377 for (const auto i : make_range(_size))
1379
1380 copyToDevice();
1381}
void copyToDevice()
Copy data from host to device.
void copyToDeviceInner(T &)
IntRange< T > make_range(T beg, T end)

◆ copyToHost()

template<typename T , unsigned int dimension, typename index_type >
void Moose::Kokkos::ArrayBase< T, dimension, index_type >::copyToHost ( )
inherited

Copy data from device to host.

Definition at line 1196 of file KokkosArray.h.

1197{
1198 // If device side memory is not allocated, do nothing
1199 if (!_is_device_alloc)
1200 return;
1201
1202 // If host side memory is not allocated,
1203 if (!_is_host_alloc)
1204 {
1205 if (_counter.use_count() == 1)
1206 // allocate memory if this array is not shared with other arrays
1207 allocHost<false>();
1208 else
1209 // print error if this array is shared with other arrays
1210 mooseError("Kokkos array error: cannot copy from device to host because host memory "
1211 "was not allocated. Cannot allocate host memory for copy because the array is "
1212 "being shared.");
1213 }
1214
1215 // Copy from device to host
1216 copyInternal<::Kokkos::HostSpace, MemSpace>(_host_data, _device_data, _size);
1217}

Referenced by Moose::Kokkos::ArrayBase< T, dimension, index_type >::moveToHost().

◆ create() [1/2]

template<typename T , unsigned int dimension, typename index_type >
template<bool initialize = true>
void Moose::Kokkos::ArrayBase< T, dimension, index_type >::create ( const std::vector< index_type > &  n)
inlineinherited

Allocate array on host and device.

Template Parameters
initializeWhether to initialize host data (calls default constructor)
Parameters
nThe vector containing the size of each dimension

Definition at line 396 of file KokkosArray.h.

397 {
398 createInternal<true, true, initialize>(n);
399 }

Referenced by Moose::Kokkos::ArrayBase< T, dimension, index_type >::ArrayBase(), Moose::Kokkos::VariableGradientTempl< is_ad >::VariableGradientTempl(), and Moose::Kokkos::VariableValueTempl< is_ad >::VariableValueTempl().

◆ create() [2/2]

template<typename T , unsigned int dimension, typename index_type >
template<bool initialize = true, typename... size_type>
void Moose::Kokkos::ArrayBase< T, dimension, index_type >::create ( size_type...  n)
inlineinherited

Allocate array on host and device.

Template Parameters
initializeWhether to initialize host data (calls default constructor)
Parameters
nThe size of each dimension

Definition at line 406 of file KokkosArray.h.

407 {
408 createInternal<true, true, initialize>(n...);
409 }

◆ createDevice() [1/2]

template<typename T , unsigned int dimension, typename index_type >
void Moose::Kokkos::ArrayBase< T, dimension, index_type >::createDevice ( const std::vector< index_type > &  n)
inlineinherited

Allocate array on device only.

Parameters
nThe vector containing the size of each dimension

Definition at line 434 of file KokkosArray.h.

434{ createInternal<false, true, false>(n); }

◆ createDevice() [2/2]

template<typename T , unsigned int dimension, typename index_type >
template<typename... size_type>
void Moose::Kokkos::ArrayBase< T, dimension, index_type >::createDevice ( size_type...  n)
inlineinherited

Allocate array on device only.

Parameters
nThe size of each dimension

Definition at line 440 of file KokkosArray.h.

441 {
442 createInternal<false, true, false>(n...);
443 }

◆ createHost() [1/2]

template<typename T , unsigned int dimension, typename index_type >
template<bool initialize = true>
void Moose::Kokkos::ArrayBase< T, dimension, index_type >::createHost ( const std::vector< index_type > &  n)
inlineinherited

Allocate array on host only.

Template Parameters
initializeWhether to initialize host data (calls default constructor)
Parameters
nThe vector containing the size of each dimension

Definition at line 416 of file KokkosArray.h.

417 {
418 createInternal<true, false, initialize>(n);
419 }

◆ createHost() [2/2]

template<typename T , unsigned int dimension, typename index_type >
template<bool initialize = true, typename... size_type>
void Moose::Kokkos::ArrayBase< T, dimension, index_type >::createHost ( size_type...  n)
inlineinherited

Allocate array on host only.

Template Parameters
initializeWhether to initialize host data (calls default constructor)
Parameters
nThe size of each dimension

Definition at line 426 of file KokkosArray.h.

427 {
428 createInternal<true, false, initialize>(n...);
429 }

◆ createInternal() [1/3]

template<typename T , unsigned int dimension, typename index_type >
template<bool host, bool device, bool initialize>
void Moose::Kokkos::ArrayBase< T, dimension, index_type >::createInternal ( const std::vector< index_type > &  n)
protectedinherited

The internal method to initialize and allocate this array.

Template Parameters
hostWhether host data will be allocated
deviceWhether device data will be allocated
initializeWhether to initialize host data (calls default constructor)
Parameters
nThe vector containing the size of each dimension

Definition at line 1038 of file KokkosArray.h.

1039{
1040 if (n.size() != dimension)
1041 mooseError("Kokkos array error: the number of dimensions provided (",
1042 n.size(),
1043 ") must match the array dimension (",
1044 dimension,
1045 ").");
1046
1047 if (_counter)
1048 destroy();
1049
1050 _counter = std::make_shared<unsigned int>();
1051
1052 uint64_t overflow_checker = 1;
1053
1054 _size = 1;
1055 _s[0] = 1;
1056
1057 for (const auto i : make_range(dimension))
1058 {
1059 overflow_checker *= n[i];
1060
1061 _n[i] = n[i];
1062 _size *= n[i];
1063 }
1064
1065 if (overflow_checker > std::numeric_limits<index_type>::max())
1066 mooseError("Kokkos array error: the dimensions provided (",
1068 ") has the total size of ",
1069 overflow_checker,
1070 " which exceeds the limit of ",
1071 MooseUtils::prettyCppType<index_type>(),
1072 ".");
1073
1075 {
1076 _s[0] = 1;
1077
1078 for (const auto i : make_range(1u, dimension))
1079 _s[i] = _s[i - 1] * _n[i - 1];
1080 }
1081 else
1082 {
1083 _s[dimension - 1] = 1;
1084
1085 for (int i = dimension - 2; i >= 0; --i)
1086 _s[i] = _s[i + 1] * _n[i + 1];
1087 }
1088
1089 if constexpr (host)
1090 allocHost<initialize>();
1091
1092 if constexpr (device)
1093 allocDevice();
1094
1095 _is_init = true;
1096}
index_type _n[dimension]
Size of each dimension.
const LayoutType _layout
Memory layout type.
void destroy()
Free all data and reset.
index_type _s[dimension]
Stride of each dimension.
std::string stringify(const T &t)
conversion to string
Definition Conversion.h:65

◆ createInternal() [2/3]

template<typename T , unsigned int dimension, typename index_type >
template<bool initialize>
void Moose::Kokkos::ArrayBase< T, dimension, index_type >::createInternal ( const std::vector< index_type > &  n,
bool  host,
bool  device 
)
protectedinherited

The internal method to initialize and allocate this array.

Template Parameters
initializeWhether to initialize host data (calls default constructor)
Parameters
nThe vector containing the size of each dimension
hostThe flag whether host data will be allocated
deviceThe flag whether device data will be allocated

Definition at line 1101 of file KokkosArray.h.

1104{
1105 if (host && device)
1106 createInternal<true, true, initialize>(n);
1107 else if (host && !device)
1108 createInternal<true, false, initialize>(n);
1109 else if (!host && device)
1110 createInternal<false, true, initialize>(n);
1111 else
1112 createInternal<false, false, initialize>(n);
1113}

◆ createInternal() [3/3]

template<typename T , unsigned int dimension, typename index_type >
template<bool host, bool device, bool initialize, typename... size_type>
void Moose::Kokkos::ArrayBase< T, dimension, index_type >::createInternal ( size_type...  n)
protectedinherited

The internal method to initialize and allocate this array.

Template Parameters
hostWhether host data will be allocated
deviceWhether device data will be allocated
initializeWhether to initialize host data (calls default constructor)
Parameters
nThe size of each dimension

Definition at line 1118 of file KokkosArray.h.

1119{
1120 static_assert((std::is_convertible<size_type, index_type>::value && ...),
1121 "All arguments must be convertible to index_type");
1122 static_assert(sizeof...(n) == dimension, "Number of arguments should match array dimension");
1123
1124 std::vector<index_type> dims;
1125 (dims.push_back(n), ...);
1126
1127 createInternal<host, device, initialize>(dims);
1128}

◆ data()

template<typename T , unsigned int dimension, typename index_type >
KOKKOS_FUNCTION T * Moose::Kokkos::ArrayBase< T, dimension, index_type >::data ( ) const
inlineinherited

Get the data pointer.

Returns
The pointer to the underlying data depending on the architecture this function is being called on

Definition at line 306 of file KokkosArray.h.

307 {
308 KOKKOS_IF_ON_HOST(return _host_data;)
309
310 return _device_data;
311 }

Referenced by Moose::Kokkos::copyToDeviceInner(), Moose::Kokkos::dataLoad(), Moose::Kokkos::dataStore(), and Moose::Kokkos::Matrix::find().

◆ deepCopy()

template<typename T , unsigned int dimension, typename index_type >
void Moose::Kokkos::ArrayBase< T, dimension, index_type >::deepCopy ( const ArrayBase< T, dimension, index_type > &  array)
inherited

Deep copy another Kokkos array If ArrayDeepCopy<T>::value is true, it will copy-construct each entry If ArrayDeepCopy<T>::value is false, it will do a memory copy.

Parameters
arrayThe Kokkos array to be deep copied

Definition at line 1385 of file KokkosArray.h.

1386{
1387 if (_layout != array._layout)
1388 mooseError("Kokkos array error: cannot deep copy arrays with different layouts.");
1389
1390 if (ArrayDeepCopy<T>::value && !array._is_host_alloc)
1391 mooseError(
1392 "Kokkos array error: cannot deep copy using constructor from array without host data.");
1393
1394 std::vector<index_type> n(std::begin(array._n), std::end(array._n));
1395
1396 createInternal<false>(n, array._is_host_alloc, array._is_device_alloc);
1397
1398 if (_is_host_alloc)
1399 for (const auto i : make_range(_size))
1400 if (array.isSlotConstructed(i))
1401 emplaceAt(i, array._host_data[i]);
1402
1404 copyToDevice();
1405 else if (_is_device_alloc)
1406 copyInternal<MemSpace, MemSpace>(_device_data, array._device_data, _size);
1407
1408 for (const auto i : make_range(dimension))
1409 {
1410 _d[i] = array._d[i];
1411 _s[i] = array._s[i];
1412 }
1413}
if(!dmm->_nl) SETERRQ(PETSC_COMM_WORLD
signed_index_type _d[dimension]
Offset of each dimension.
T & emplaceAt(index_type i, Args &&... args)
Placement-new construct slot i from args, recording initialization.
bool isSlotConstructed(index_type i) const
Get whether slot i is tracked as constructed.
static constexpr bool value
Definition KokkosArray.h:93

Referenced by Moose::Kokkos::ArrayBase< T, dimension, index_type >::ArrayBase().

◆ destroy()

template<typename T , unsigned int dimension, typename index_type >
void Moose::Kokkos::ArrayBase< T, dimension, index_type >::destroy ( )
inherited

Free all data and reset.

Definition at line 900 of file KokkosArray.h.

901{
902 if (!_counter)
903 return;
904
905 if (_counter.use_count() > 1)
906 {
907 _host_data = nullptr;
908 _device_data = nullptr;
909 }
910 else if (_counter.use_count() == 1)
911 {
912 freeHost();
913 freeDevice();
914 }
915
916 _size = 0;
917
918 for (const auto i : make_range(dimension))
919 {
920 _n[i] = 0;
921 _s[i] = 0;
922 _d[i] = 0;
923 }
924
925 _is_init = false;
926 _is_host_alloc = false;
927 _is_device_alloc = false;
928 _is_host_alias = false;
929 _is_device_alias = false;
930
931 _counter.reset();
932 _slots_constructed.reset();
933}
void freeHost()
Free host data.
void freeDevice()
Free device data.

Referenced by Moose::Kokkos::System::clearActiveMatrixTags(), Moose::Kokkos::System::clearActiveResidualTags(), Moose::Kokkos::System::clearActiveSolutionTags(), Moose::Kokkos::System::clearActiveVariables(), Moose::Kokkos::ArrayBase< T, dimension, index_type >::createInternal(), Moose::Kokkos::ArrayBase< T, dimension, index_type >::shallowCopy(), and Moose::Kokkos::ArrayBase< T, dimension, index_type >::~ArrayBase().

◆ deviceData()

template<typename T , unsigned int dimension, typename index_type >
T * Moose::Kokkos::ArrayBase< T, dimension, index_type >::deviceData ( ) const
inlineinherited

Get the device data pointer.

Returns
The pointer to the underlying device data

Definition at line 362 of file KokkosArray.h.

362{ return _device_data; }

◆ deviceView()

template<typename T , unsigned int dimension, typename index_type >
auto Moose::Kokkos::ArrayBase< T, dimension, index_type >::deviceView ( ) const
inlineinherited

Get the device unmanaged view.

Returns
The device unmanaged view

Definition at line 376 of file KokkosArray.h.

377 {
378 return ::Kokkos::View<T *, MemSpace, ::Kokkos::MemoryTraits<::Kokkos::Unmanaged>>(_device_data,
379 _size);
380 }

Referenced by Moose::Kokkos::ArrayBase< T, dimension, index_type >::operator=().

◆ emplace()

template<typename T , unsigned int dimension, typename index_type , LayoutType layout>
template<typename indices , typename... Args>
T & Moose::Kokkos::Array< T, dimension, index_type, layout >::emplace ( const indices(&)  idx[dimension],
Args &&...  args 
)

Placement-new construct an entry from args, recording initialization.

Parameters
idxThe array storing the indices
argsArguments forwarded to T's constructor
Returns
Reference to the constructed element

Definition at line 1672 of file KokkosArray.h.

1673{
1674 return this->emplaceAt(linearIndex(idx), std::forward<Args>(args)...);
1675}
KOKKOS_FUNCTION index_type linearIndex(indices... i) const
Get the dimensionless index for a multi-dimensional index.

◆ emplaceAt()

template<typename T , unsigned int dimension, typename index_type >
template<typename... Args>
T & Moose::Kokkos::ArrayBase< T, dimension, index_type >::emplaceAt ( index_type  i,
Args &&...  args 
)
protectedinherited

Placement-new construct slot i from args, recording initialization.

Parameters
iThe dimensionless slot index
argsArguments forwarded to T's constructor
Returns
Reference to the constructed element

Definition at line 834 of file KokkosArray.h.

835{
836 mooseAssert(_is_host_alloc && _slots_constructed,
837 "emplaceAt requires a malloc-allocated array (create<false>)");
838 mooseAssert(i < _size, "emplaceAt index out of bounds");
839
840 new (_host_data + i) T(std::forward<Args>(args)...);
841 (*_slots_constructed)[i] = true;
842
843 return _host_data[i];
844}

Referenced by Moose::Kokkos::ArrayBase< T, dimension, index_type >::deepCopy(), Moose::Kokkos::Array< T, 1, index_type, LayoutType::LEFT >::emplace(), and Moose::Kokkos::Array< T, dimension, index_type, layout >::emplace().

◆ end()

template<typename T , unsigned int dimension, typename index_type >
KOKKOS_FUNCTION iterator Moose::Kokkos::ArrayBase< T, dimension, index_type >::end ( ) const
inlineinherited

Get the end iterator.

Returns
The end iterator

Definition at line 582 of file KokkosArray.h.

583 {
584 KOKKOS_IF_ON_HOST(return iterator(_host_data + _size);)
585
586 return iterator(_device_data + _size);
587 }

◆ first()

template<typename T , unsigned int dimension, typename index_type >
KOKKOS_FUNCTION T & Moose::Kokkos::ArrayBase< T, dimension, index_type >::first ( ) const
inlineinherited

Get the first element.

Returns
The reference of the first element depending on the architecture this function is being called on

Definition at line 317 of file KokkosArray.h.

318 {
319 KOKKOS_ASSERT(_size);
320
321 KOKKOS_IF_ON_HOST(return _host_data[0];)
322
323 return _device_data[0];
324 }

Referenced by Moose::Kokkos::RPNEvaluator::eval().

◆ freeDevice()

template<typename T , unsigned int dimension, typename index_type >
void Moose::Kokkos::ArrayBase< T, dimension, index_type >::freeDevice ( )
privateinherited

Free device data.

Definition at line 882 of file KokkosArray.h.

883{
884 if (!_is_device_alloc)
885 return;
886
888 {
889 _device_data = nullptr;
890 _is_device_alias = false;
891 }
892 else
894
895 _is_device_alloc = false;
896}
void free(void *ptr)

Referenced by Moose::Kokkos::ArrayBase< T, dimension, index_type >::destroy(), and Moose::Kokkos::ArrayBase< T, dimension, index_type >::moveToHost().

◆ freeHost()

template<typename T , unsigned int dimension, typename index_type >
void Moose::Kokkos::ArrayBase< T, dimension, index_type >::freeHost ( )
privateinherited

Free host data.

Definition at line 849 of file KokkosArray.h.

850{
851 if (!_is_host_alloc)
852 return;
853
854 if (_is_host_alias)
855 {
856 _host_data = nullptr;
857 _is_host_alias = false;
858 }
859 else
860 {
862 // Allocated by new
863 delete[] _host_data;
864 else
865 {
866 // Allocated by malloc
867 for (const auto i : make_range(_size))
869 _host_data[i].~T();
870
871 std::free(_host_data);
872
873 _slots_constructed.reset();
874 }
875 }
876
877 _is_host_alloc = false;
878}

Referenced by Moose::Kokkos::ArrayBase< T, dimension, index_type >::destroy(), and Moose::Kokkos::ArrayBase< T, dimension, index_type >::moveToDevice().

◆ hostData()

template<typename T , unsigned int dimension, typename index_type >
T * Moose::Kokkos::ArrayBase< T, dimension, index_type >::hostData ( ) const
inlineinherited

Get the host data pointer.

Returns
The pointer to the underlying host data

Definition at line 357 of file KokkosArray.h.

357{ return _host_data; }

◆ hostView()

template<typename T , unsigned int dimension, typename index_type >
auto Moose::Kokkos::ArrayBase< T, dimension, index_type >::hostView ( ) const
inlineinherited

Get the host unmanaged view.

Returns
The host unmanaged view

Definition at line 367 of file KokkosArray.h.

368 {
369 return ::Kokkos::View<T *, ::Kokkos::HostSpace, ::Kokkos::MemoryTraits<::Kokkos::Unmanaged>>(
371 }

◆ init()

template<typename T , unsigned int dimension, typename index_type >
template<typename... size_type>
void Moose::Kokkos::ArrayBase< T, dimension, index_type >::init ( size_type...  n)
inlineinherited

Initialize array with given dimensions but do not allocate.

Parameters
nThe size of each dimension

Definition at line 386 of file KokkosArray.h.

387 {
388 createInternal<false, false, false>(n...);
389 }

◆ isAlloc()

template<typename T , unsigned int dimension, typename index_type >
KOKKOS_FUNCTION bool Moose::Kokkos::ArrayBase< T, dimension, index_type >::isAlloc ( ) const
inlineinherited

Get whether the array was allocated either on host or device.

Returns
Whether the array was allocated either on host or device

Definition at line 269 of file KokkosArray.h.

269{ return _is_host_alloc || _is_device_alloc; }

Referenced by Moose::Kokkos::dataStore(), Moose::Kokkos::VariableGradientTempl< is_ad >::VariableGradientTempl(), and Moose::Kokkos::VariableValueTempl< is_ad >::VariableValueTempl().

◆ isDeviceAlias()

template<typename T , unsigned int dimension, typename index_type >
KOKKOS_FUNCTION bool Moose::Kokkos::ArrayBase< T, dimension, index_type >::isDeviceAlias ( ) const
inlineinherited

Get whether the device array was aliased.

Returns
Whether the device array was aliased

Definition at line 289 of file KokkosArray.h.

289{ return _is_device_alias; }

◆ isDeviceAlloc()

template<typename T , unsigned int dimension, typename index_type >
KOKKOS_FUNCTION bool Moose::Kokkos::ArrayBase< T, dimension, index_type >::isDeviceAlloc ( ) const
inlineinherited

Get whether the array was allocated on device.

Returns
Whether the array was allocated on device

Definition at line 279 of file KokkosArray.h.

279{ return _is_device_alloc; }

Referenced by Moose::Kokkos::dataLoad(), and Moose::Kokkos::dataStore().

◆ isHostAlias()

template<typename T , unsigned int dimension, typename index_type >
KOKKOS_FUNCTION bool Moose::Kokkos::ArrayBase< T, dimension, index_type >::isHostAlias ( ) const
inlineinherited

Get whether the host array was aliased.

Returns
Whether the host array was aliased

Definition at line 284 of file KokkosArray.h.

284{ return _is_host_alias; }

◆ isHostAlloc()

template<typename T , unsigned int dimension, typename index_type >
KOKKOS_FUNCTION bool Moose::Kokkos::ArrayBase< T, dimension, index_type >::isHostAlloc ( ) const
inlineinherited

Get whether the array was allocated on host.

Returns
Whether the array was allocated on host

Definition at line 274 of file KokkosArray.h.

274{ return _is_host_alloc; }

Referenced by Moose::Kokkos::dataLoad().

◆ isSlotConstructed()

template<typename T , unsigned int dimension, typename index_type >
bool Moose::Kokkos::ArrayBase< T, dimension, index_type >::isSlotConstructed ( index_type  i) const
inherited

Get whether slot i is tracked as constructed.

For an array created with initialization (initialize = true) every slot is default-constructed at allocation, so all slots are reported as constructed. For an array created without initialization (initialize = false) only slots constructed through emplace() are tracked; placement-new directly into the storage is not recorded here, so such a slot is reported as not constructed and its destructor is skipped on free (a leak for types that own resources).

Parameters
iThe slot index
Returns
true if the slot is tracked as constructed

Definition at line 820 of file KokkosArray.h.

821{
822 mooseAssert(i < _size, "isSlotConstructed index out of bounds");
823
825 return _is_host_alloc;
826
827 return (*_slots_constructed)[i];
828}

Referenced by Moose::Kokkos::ArrayBase< T, dimension, index_type >::deepCopy(), and Moose::Kokkos::ArrayBase< T, dimension, index_type >::freeHost().

◆ last()

template<typename T , unsigned int dimension, typename index_type >
KOKKOS_FUNCTION T & Moose::Kokkos::ArrayBase< T, dimension, index_type >::last ( ) const
inlineinherited

Get the last element.

Returns
The reference of the last element depending on the architecture this function is being called on

Definition at line 330 of file KokkosArray.h.

331 {
332 KOKKOS_ASSERT(_size);
333
334 KOKKOS_IF_ON_HOST(return _host_data[_size - 1];)
335
336 return _device_data[_size - 1];
337 }

◆ linearIndex() [1/2]

template<typename T , unsigned int dimension, typename index_type , LayoutType layout>
template<typename indices >
KOKKOS_FUNCTION index_type Moose::Kokkos::Array< T, dimension, index_type, layout >::linearIndex ( const indices(&)  idx[dimension]) const
inlineprivate

Get the dimensionless index for indices stored in an array.

Definition at line 1654 of file KokkosArray.h.

1655 {
1656 return linearIndexHelper(idx, std::make_integer_sequence<unsigned int, dimension>{});
1657 }
KOKKOS_FUNCTION index_type linearIndexHelper(const indices(&idx)[dimension], std::integer_sequence< unsigned int, i... >) const

◆ linearIndex() [2/2]

template<typename T , unsigned int dimension, typename index_type , LayoutType layout>
template<typename... indices>
KOKKOS_FUNCTION index_type Moose::Kokkos::Array< T, dimension, index_type, layout >::linearIndex ( indices...  i) const
private

Get the dimensionless index for a multi-dimensional index.

Definition at line 1680 of file KokkosArray.h.

1681{
1682 static_assert((std::is_convertible<indices, signed_index_type>::value && ...),
1683 "All arguments must be convertible to signed_index_type");
1684 static_assert(sizeof...(i) == dimension, "Number of arguments should match array dimension");
1685
1686#ifndef NDEBUG
1687 {
1688 signed_index_type idx[dimension] = {static_cast<signed_index_type>(i)...};
1689
1690 for (unsigned int d = 0; d < sizeof...(i); ++d)
1691 KOKKOS_ASSERT(idx[d] - _d[d] >= 0 && static_cast<index_type>(idx[d] - _d[d]) < _n[d]);
1692 }
1693#endif
1694
1695 index_type idx = 0;
1696 unsigned int d = 0;
1697
1698 if constexpr (layout == LayoutType::LEFT)
1699 (((idx += (d == 0 ? static_cast<signed_index_type>(i) - _d[d]
1700 : (static_cast<signed_index_type>(i) - _d[d]) * _s[d])),
1701 ++d),
1702 ...);
1703 else
1704 (((idx += (d == dimension - 1 ? static_cast<signed_index_type>(i) - _d[d]
1705 : (static_cast<signed_index_type>(i) - _d[d]) * _s[d])),
1706 ++d),
1707 ...);
1708
1709 return idx;
1710}
typename std::make_signed< index_type >::type signed_index_type
unsigned int idx(const ElemType type, const unsigned int nx, const unsigned int i, const unsigned int j)

◆ linearIndexHelper()

template<typename T , unsigned int dimension, typename index_type , LayoutType layout>
template<typename indices , unsigned int... i>
KOKKOS_FUNCTION index_type Moose::Kokkos::Array< T, dimension, index_type, layout >::linearIndexHelper ( const indices(&)  idx[dimension],
std::integer_sequence< unsigned int, i... >   
) const
inlineprivate

Definition at line 1659 of file KokkosArray.h.

1661 {
1662 return linearIndex(idx[i]...);
1663 }

◆ moveToDevice()

template<typename T , unsigned int dimension, typename index_type >
void Moose::Kokkos::ArrayBase< T, dimension, index_type >::moveToDevice ( bool  should_free_host = true)
inherited

Copy data from host to device and deallocate host.

Parameters
should_free_hostWhether the host memory should be freed. Host memory cannot be freed when there are shallow copies of this array that are still alive. If should_free_host is true, and we cannot free for above reason, it will error.

Definition at line 1221 of file KokkosArray.h.

1222{
1223 static_assert(!is_kokkos_array<T>::value,
1224 "moveToDevice() not allowed for a nested array whose data type is another array.");
1225
1226 if (should_free_host && _counter.use_count() > 1)
1227 mooseError("Kokkos array error: cannot move array from host to device because there is at "
1228 "least one shallow copy of this array still alive.");
1229
1230 copyToDevice();
1231
1232 if (_counter.use_count() == 1)
1233 freeHost();
1234}

◆ moveToHost()

template<typename T , unsigned int dimension, typename index_type >
void Moose::Kokkos::ArrayBase< T, dimension, index_type >::moveToHost ( bool  should_free_device = true)
inherited

Copy data from device to host and deallocate device.

Parameters
should_free_deviceWhether the device memory should be freed. Device memory cannot be freed when there are shallow copies of this array that are still alive. If should_free_device is true, and we cannot free for above reason, it will error.

Definition at line 1238 of file KokkosArray.h.

1239{
1240 if (should_free_device && _counter.use_count() > 1)
1241 mooseError("Kokkos array error: cannot move array from device to host because there is at "
1242 "least one shallow copy of this array still alive.");
1243
1244 copyToHost();
1245
1246 if (_counter.use_count() == 1)
1247 freeDevice();
1248}
void copyToHost()
Copy data from device to host.

◆ n()

template<typename T , unsigned int dimension, typename index_type >
KOKKOS_FUNCTION index_type Moose::Kokkos::ArrayBase< T, dimension, index_type >::n ( unsigned int  dim) const
inlineinherited

◆ offset() [1/2]

template<typename T , unsigned int dimension, typename index_type >
void Moose::Kokkos::ArrayBase< T, dimension, index_type >::offset ( const std::vector< signed_index_type > &  d)
inherited

Apply starting index offsets to each dimension.

Parameters
dThe vector containing the offset of each dimension

Definition at line 1141 of file KokkosArray.h.

1142{
1143 if (d.size() > dimension)
1144 mooseError("Kokkos array error: the number of offsets provided (",
1145 d.size(),
1146 ") cannot be larger than the array dimension (",
1147 dimension,
1148 ").");
1149
1150 for (const auto i : index_range(d))
1151 _d[i] = d[i];
1152}
auto index_range(const T &sizable)

Referenced by Moose::Kokkos::ArrayBase< T, dimension, index_type >::copyIn(), Moose::Kokkos::ArrayBase< T, dimension, index_type >::copyOut(), and Moose::Kokkos::ArrayBase< T, dimension, index_type >::offset().

◆ offset() [2/2]

template<typename T , unsigned int dimension, typename index_type >
template<typename... offset_type>
void Moose::Kokkos::ArrayBase< T, dimension, index_type >::offset ( offset_type...  d)
inherited

Apply starting index offsets to each dimension.

Parameters
dThe offset of each dimension

Definition at line 1157 of file KokkosArray.h.

1158{
1159 static_assert((std::is_convertible<offset_type, signed_index_type>::value && ...),
1160 "All arguments must be convertible to signed_index_type");
1161 static_assert(sizeof...(d) == dimension, "Number of arguments should match array dimension");
1162
1163 std::vector<signed_index_type> offsets;
1164 (offsets.push_back(d), ...);
1165
1166 offset(offsets);
1167}

◆ operator()() [1/2]

template<typename T , unsigned int dimension, typename index_type , LayoutType layout>
KOKKOS_FUNCTION T & Moose::Kokkos::Array< T, dimension, index_type, layout >::operator() ( const signed_index_type(&)  idx[dimension]) const
inline

Get an array entry using indices stored in an array.

Parameters
idxThe array storing the indices
Returns
The reference of the entry depending on the architecture this function is being called on

Definition at line 1628 of file KokkosArray.h.

1629 {
1630 return this->operator[](linearIndex(idx));
1631 }
KOKKOS_FUNCTION T & operator[](index_type i) const
Get an array entry.

◆ operator()() [2/2]

template<typename T , unsigned int dimension, typename index_type , LayoutType layout>
template<typename... indices>
KOKKOS_FUNCTION T & Moose::Kokkos::Array< T, dimension, index_type, layout >::operator() ( indices...  i) const
inline

Get an array entry.

Parameters
iThe index of each dimension
Returns
The reference of the entry depending on the architecture this function is being called on

Definition at line 1618 of file KokkosArray.h.

1619 {
1620 return this->operator[](linearIndex(i...));
1621 }

◆ operator=()

template<typename T , unsigned int dimension, typename index_type , LayoutType layout>
auto & Moose::Kokkos::Array< T, dimension, index_type, layout >::operator= ( const Array< T, dimension, index_type, layout > &  array)
inline

Shallow copy another Kokkos array.

Parameters
arrayThe Kokkos array to be shallow copied

Definition at line 1603 of file KokkosArray.h.

1604 {
1605 this->shallowCopy(array);
1606
1607 return *this;
1608 }
void shallowCopy(const ArrayBase< T, dimension, index_type > &array)
Shallow copy another Kokkos array.

◆ operator[]()

template<typename T , unsigned int dimension, typename index_type >
KOKKOS_FUNCTION T & Moose::Kokkos::ArrayBase< T, dimension, index_type >::operator[] ( index_type  i) const
inlineinherited

Get an array entry.

Parameters
iThe dimensionless index
Returns
The reference of the entry depending on the architecture this function is being called on

Definition at line 344 of file KokkosArray.h.

345 {
346 KOKKOS_ASSERT(i < _size);
347
348 KOKKOS_IF_ON_HOST(return _host_data[i];)
349
350 return _device_data[i];
351 }

◆ shallowCopy()

template<typename T , unsigned int dimension, typename index_type >
void Moose::Kokkos::ArrayBase< T, dimension, index_type >::shallowCopy ( const ArrayBase< T, dimension, index_type > &  array)
inherited

Shallow copy another Kokkos array.

Parameters
arrayThe Kokkos array to be shallow copied

Definition at line 937 of file KokkosArray.h.

938{
939 if (_layout != array._layout)
940 mooseError("Kokkos array error: cannot shallow copy arrays with different layouts.");
941
942 destroy();
943
944 _counter = array._counter;
945 _slots_constructed = array._slots_constructed;
946
947 _size = array._size;
948
949 for (const auto i : make_range(dimension))
950 {
951 _n[i] = array._n[i];
952 _s[i] = array._s[i];
953 _d[i] = array._d[i];
954 }
955
956 _is_init = array._is_init;
957 _is_host_alloc = array._is_host_alloc;
958 _is_device_alloc = array._is_device_alloc;
959 _is_host_alias = array._is_host_alias;
960 _is_device_alias = array._is_device_alias;
961
962 _host_data = array._host_data;
963 _device_data = array._device_data;
964}

Referenced by Moose::Kokkos::ArrayBase< T, dimension, index_type >::ArrayBase(), and Moose::Kokkos::ArrayBase< T, dimension, index_type >::swap().

◆ size()

template<typename T , unsigned int dimension, typename index_type >
KOKKOS_FUNCTION index_type Moose::Kokkos::ArrayBase< T, dimension, index_type >::size ( ) const
inlineinherited

Get the total array size.

Returns
The total array size

Definition at line 294 of file KokkosArray.h.

294{ return _size; }

Referenced by Moose::Kokkos::ResidualObject::accumulateTaggedElementalMatrix(), Moose::Kokkos::ResidualObject::accumulateTaggedElementalMatrix(), Moose::Kokkos::ResidualObject::accumulateTaggedElementalResidual(), Moose::Kokkos::LinearSystemContributionObject::accumulateTaggedMatrix(), Moose::Kokkos::ResidualObject::accumulateTaggedNodalMatrix(), Moose::Kokkos::ResidualObject::accumulateTaggedNodalMatrix(), Moose::Kokkos::ResidualObject::accumulateTaggedNodalResidual(), Moose::Kokkos::LinearSystemContributionObject::accumulateTaggedVector(), Moose::Kokkos::ResidualObject::accumulateTaggedVectorNodalMatrix(), Moose::Kokkos::ResidualObject::accumulateTaggedVectorNodalResidual(), Moose::Kokkos::MemoryPool::allocate(), Moose::Kokkos::dataLoad(), Moose::Kokkos::dataStore(), KokkosPiecewiseBase::functionSize(), Moose::Kokkos::Mesh::getElementVolume(), Moose::Kokkos::Variable::mooseFieldVar(), Moose::Kokkos::Variable::mooseScalarVar(), Moose::Kokkos::Variable::mooseVar(), BlockRestrictable::numKokkosBlockElements(), BlockRestrictable::numKokkosBlockNodes(), BlockRestrictable::numKokkosBlockSides(), BoundaryRestrictable::numKokkosBoundaryNodes(), BoundaryRestrictable::numKokkosBoundarySides(), Moose::Kokkos::MaterialBase::numKokkosElements(), Moose::Kokkos::MaterialBase::numKokkosElementSides(), Moose::Kokkos::Vector::operator()(), Moose::Kokkos::Vector::operator[](), Moose::Kokkos::MapBase< T1, T2, MapType >::size(), and KokkosBoundNodalKernel::skipOnBoundary().

◆ swap()

template<typename T , unsigned int dimension, typename index_type >
void Moose::Kokkos::ArrayBase< T, dimension, index_type >::swap ( ArrayBase< T, dimension, index_type > &  array)
inherited

Swap with another Kokkos array.

Parameters
arrayThe Kokkos array to be swapped

Definition at line 1417 of file KokkosArray.h.

1418{
1419 ArrayBase<T, dimension, index_type> clone(_layout);
1420
1421 clone.shallowCopy(*this);
1422 this->shallowCopy(array);
1423 array.shallowCopy(clone);
1424}

◆ useCount()

template<typename T , unsigned int dimension, typename index_type >
unsigned int Moose::Kokkos::ArrayBase< T, dimension, index_type >::useCount ( ) const
inlineinherited

Get the reference count.

Returns
The reference count

Definition at line 174 of file KokkosArray.h.

174{ return _counter.use_count(); }

◆ usingKokkosArrayBaseMembers()

template<typename T , unsigned int dimension, typename index_type , LayoutType layout>
Moose::Kokkos::Array< T, dimension, index_type, layout >::usingKokkosArrayBaseMembers ( T  ,
dimension  ,
index_type   
)
private

Member Data Documentation

◆ _counter

template<typename T , unsigned int dimension, typename index_type >
std::shared_ptr<unsigned int> Moose::Kokkos::ArrayBase< T, dimension, index_type >::_counter
privateinherited

◆ _d

template<typename T , unsigned int dimension, typename index_type >
signed_index_type Moose::Kokkos::ArrayBase< T, dimension, index_type >::_d[dimension] = {0}
protectedinherited

◆ _device_data

template<typename T , unsigned int dimension, typename index_type >
T* Moose::Kokkos::ArrayBase< T, dimension, index_type >::_device_data = nullptr
privateinherited

◆ _host_data

template<typename T , unsigned int dimension, typename index_type >
T* Moose::Kokkos::ArrayBase< T, dimension, index_type >::_host_data = nullptr
privateinherited

◆ _is_device_alias

template<typename T , unsigned int dimension, typename index_type >
bool Moose::Kokkos::ArrayBase< T, dimension, index_type >::_is_device_alias = false
privateinherited

◆ _is_device_alloc

template<typename T , unsigned int dimension, typename index_type >
bool Moose::Kokkos::ArrayBase< T, dimension, index_type >::_is_device_alloc = false
privateinherited

◆ _is_host_alias

template<typename T , unsigned int dimension, typename index_type >
bool Moose::Kokkos::ArrayBase< T, dimension, index_type >::_is_host_alias = false
privateinherited

◆ _is_host_alloc

template<typename T , unsigned int dimension, typename index_type >
bool Moose::Kokkos::ArrayBase< T, dimension, index_type >::_is_host_alloc = false
privateinherited

◆ _is_init

template<typename T , unsigned int dimension, typename index_type >
bool Moose::Kokkos::ArrayBase< T, dimension, index_type >::_is_init = false
privateinherited

◆ _layout

template<typename T , unsigned int dimension, typename index_type >
const LayoutType Moose::Kokkos::ArrayBase< T, dimension, index_type >::_layout
privateinherited

◆ _n

template<typename T , unsigned int dimension, typename index_type >
index_type Moose::Kokkos::ArrayBase< T, dimension, index_type >::_n[dimension] = {0}
protectedinherited

◆ _s

template<typename T , unsigned int dimension, typename index_type >
index_type Moose::Kokkos::ArrayBase< T, dimension, index_type >::_s[dimension] = {0}
protectedinherited

◆ _size

template<typename T , unsigned int dimension, typename index_type >
index_type Moose::Kokkos::ArrayBase< T, dimension, index_type >::_size = 0
privateinherited

Total size.

Definition at line 713 of file KokkosArray.h.

Referenced by Moose::Kokkos::ArrayBase< T, dimension, index_type >::allocDevice(), Moose::Kokkos::ArrayBase< T, dimension, index_type >::allocHost(), Moose::Kokkos::ArrayBase< T, dimension, index_type >::copyIn(), Moose::Kokkos::ArrayBase< T, dimension, index_type >::copyOut(), Moose::Kokkos::ArrayBase< T, dimension, index_type >::copyToDevice(), Moose::Kokkos::ArrayBase< T, dimension, index_type >::copyToDeviceNested(), Moose::Kokkos::ArrayBase< T, dimension, index_type >::copyToHost(), Moose::Kokkos::ArrayBase< T, dimension, index_type >::createInternal(), Moose::Kokkos::ArrayBase< T, dimension, index_type >::deepCopy(), Moose::Kokkos::ArrayBase< T, dimension, index_type >::destroy(), Moose::Kokkos::ArrayBase< T, dimension, index_type >::deviceView(), Moose::Kokkos::ArrayBase< T, dimension, index_type >::emplaceAt(), Moose::Kokkos::ArrayBase< T, dimension, index_type >::end(), Moose::Kokkos::ArrayBase< T, dimension, index_type >::first(), Moose::Kokkos::ArrayBase< T, dimension, index_type >::freeHost(), Moose::Kokkos::ArrayBase< T, dimension, index_type >::hostView(), Moose::Kokkos::ArrayBase< T, dimension, index_type >::isSlotConstructed(), Moose::Kokkos::ArrayBase< T, dimension, index_type >::last(), Moose::Kokkos::ArrayBase< T, dimension, index_type >::operator=(), Moose::Kokkos::ArrayBase< T, dimension, index_type >::operator[](), Moose::Kokkos::ArrayBase< T, dimension, index_type >::shallowCopy(), and Moose::Kokkos::ArrayBase< T, dimension, index_type >::size().

◆ _slots_constructed

template<typename T , unsigned int dimension, typename index_type >
std::shared_ptr<std::vector<bool> > Moose::Kokkos::ArrayBase< T, dimension, index_type >::_slots_constructed
privateinherited

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