https://mooseframework.inl.gov
Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes | Friends | List of all members
Moose::Kokkos::MapBase< T1, T2, MapType > Class Template Reference

The Kokkos wrapper class for standard map. More...

#include <KokkosMap.h>

Public Member Functions

auto begin ()
 Get the beginning writeable iterator of the host map. More...
 
auto begin () const
 Get the beginning const iterator of the host map. More...
 
auto end ()
 Get the end writeable iterator of the host map. More...
 
auto end () const
 Get the end const iterator of the host map. More...
 
auto & get ()
 Get the underlying writeable host map. More...
 
const auto & get () const
 Get the underlying const host map. More...
 
void clear ()
 Clear the underlying data. More...
 
T2 & operator[] (const T1 &key)
 Call host map's operator[]. More...
 
void copyToDevice ()
 Copy host map to device. More...
 
void copyToDeviceNested ()
 Copy host map to device, perform nested copy for Kokkos arrays. More...
 
void swap (MapBase< T1, T2, MapType > &map)
 Swap with another Kokkos map. More...
 
KOKKOS_FUNCTION dof_id_type size () const
 Get the size of map. More...
 
KOKKOS_FUNCTION dof_id_type find (const T1 &key) const
 Find the index of a key. More...
 
KOKKOS_FUNCTION bool exists (const T1 &key) const
 Get whether a key exists. More...
 
KOKKOS_FUNCTION const T1 & key (dof_id_type idx) const
 Get the key of an index. More...
 
KOKKOS_FUNCTION const T2 & value (dof_id_type idx) const
 Get the value of an index. More...
 
KOKKOS_FUNCTION const T2 & operator[] (const T1 &key) const
 Get the value corresponding to a key. More...
 
KOKKOS_FUNCTION const T2 & operator() (const T1 &key) const
 

Static Public Attributes

static const dof_id_type invalid_id = libMesh::DofObject::invalid_id
 

Private Member Functions

void copy ()
 Internal method to copy host map to device. More...
 

Private Attributes

std::shared_ptr< MapType< T1, T2 > > _map_host
 Standard map on host Stored as a shared pointer to avoid deep copy. More...
 
Array< T1 > _keys
 Keys on device. More...
 
Array< T2 > _values
 Values on device. More...
 
Array< dof_id_type_offset
 Beginning offset into device arrays of each bucket. More...
 

Friends

void dataStore (std::ostream &, MapBase< T1, T2, MapType > &, void *)
 
void dataLoad (std::istream &, MapBase< T1, T2, MapType > &, void *)
 

Detailed Description

template<typename T1, typename T2, template< typename... > class MapType>
class Moose::Kokkos::MapBase< T1, T2, MapType >

The Kokkos wrapper class for standard map.

The map can only be populated on host. Make sure to call copyToDevice() or copyToDeviceNested() after populating the map on host.

Definition at line 58 of file KokkosMap.h.

Member Function Documentation

◆ begin() [1/2]

template<typename T1, typename T2, template< typename... > class MapType>
auto Moose::Kokkos::MapBase< T1, T2, MapType >::begin ( )
inline

Get the beginning writeable iterator of the host map.

Returns
The beginning iterator

Definition at line 78 of file KokkosMap.h.

Referenced by Moose::Kokkos::MapBase< unsigned int, unsigned int >::begin().

78 { return get().begin(); }
auto begin()
Get the beginning writeable iterator of the host map.
Definition: KokkosMap.h:78

◆ begin() [2/2]

template<typename T1, typename T2, template< typename... > class MapType>
auto Moose::Kokkos::MapBase< T1, T2, MapType >::begin ( ) const
inline

Get the beginning const iterator of the host map.

Returns
The beginning iterator

Definition at line 83 of file KokkosMap.h.

83 { return get().cbegin(); }

◆ clear()

template<typename T1 , typename T2 , template< typename... > class MapType>
void Moose::Kokkos::MapBase< T1, T2, MapType >::clear ( )

Clear the underlying data.

Definition at line 244 of file KokkosMap.h.

245 {
246  get().clear();
247 
248  _keys.destroy();
249  _values.destroy();
250  _offset.destroy();
251 }
void clear()
Clear the underlying data.
Definition: KokkosMap.h:244
Array< dof_id_type > _offset
Beginning offset into device arrays of each bucket.
Definition: KokkosMap.h:236
Array< T2 > _values
Values on device.
Definition: KokkosMap.h:232
void destroy()
Free all data and reset.
Definition: KokkosArray.h:674
Array< T1 > _keys
Keys on device.
Definition: KokkosMap.h:228

◆ copy()

template<typename T1 , typename T2 , template< typename... > class MapType>
void Moose::Kokkos::MapBase< T1, T2, MapType >::copy ( )
private

Internal method to copy host map to device.

Definition at line 256 of file KokkosMap.h.

257 {
258  _keys.create(size());
259  _values.create(size());
260  _offset.create(size() + 1);
261  _offset = 0;
262 
263  for (const auto & [key, value] : get())
264  {
265  auto bucket = fnv1aHash(key) % size();
266 
267  _offset[bucket]++;
268  }
269 
270  std::exclusive_scan(_offset.begin(), _offset.end(), _offset.begin(), 0);
271 
273 
274  std::vector<dof_id_type> idx(size(), 0);
275 
276  for (const auto & [key, value] : get())
277  {
278  auto bucket = fnv1aHash(key) % size();
279 
280  _keys[_offset[bucket] + idx[bucket]] = key;
281  _values[_offset[bucket] + idx[bucket]] = value;
282  idx[bucket]++;
283  }
284 }
KOKKOS_FUNCTION const T2 & value(dof_id_type idx) const
Get the value of an index.
Definition: KokkosMap.h:180
KOKKOS_FUNCTION iterator end() const
Get the end iterator.
Definition: KokkosArray.h:489
KOKKOS_FUNCTION uint32_t fnv1aHash(const T &key, uint32_t hash)
Definition: KokkosMap.h:26
Array< dof_id_type > _offset
Beginning offset into device arrays of each bucket.
Definition: KokkosMap.h:236
void create(const std::vector< index_type > &n)
Allocate array on host and device.
Definition: KokkosArray.h:303
KOKKOS_FUNCTION const T1 & key(dof_id_type idx) const
Get the key of an index.
Definition: KokkosMap.h:169
void copyToDevice()
Copy data from host to device.
Definition: KokkosArray.h:947
Array< T2 > _values
Values on device.
Definition: KokkosMap.h:232
KOKKOS_FUNCTION iterator begin() const
Get the beginning iterator.
Definition: KokkosArray.h:479
KOKKOS_FUNCTION dof_id_type size() const
Get the size of map.
Definition: KokkosMap.h:146
Array< T1 > _keys
Keys on device.
Definition: KokkosMap.h:228
unsigned int idx(const ElemType type, const unsigned int nx, const unsigned int i, const unsigned int j)

◆ copyToDevice()

template<typename T1 , typename T2 , template< typename... > class MapType>
void Moose::Kokkos::MapBase< T1, T2, MapType >::copyToDevice ( )

Copy host map to device.

Definition at line 288 of file KokkosMap.h.

289 {
290  copy();
291 
294 }
void copyToDevice()
Copy data from host to device.
Definition: KokkosArray.h:947
Array< T2 > _values
Values on device.
Definition: KokkosMap.h:232
void copy()
Internal method to copy host map to device.
Definition: KokkosMap.h:256
Array< T1 > _keys
Keys on device.
Definition: KokkosMap.h:228

◆ copyToDeviceNested()

template<typename T1 , typename T2 , template< typename... > class MapType>
void Moose::Kokkos::MapBase< T1, T2, MapType >::copyToDeviceNested ( )

Copy host map to device, perform nested copy for Kokkos arrays.

Definition at line 298 of file KokkosMap.h.

299 {
300  copy();
301 
304 }
Array< T2 > _values
Values on device.
Definition: KokkosMap.h:232
void copyToDeviceNested()
Copy all the nested Kokkos arrays including self from host to device.
Definition: KokkosArray.h:1151
void copy()
Internal method to copy host map to device.
Definition: KokkosMap.h:256
Array< T1 > _keys
Keys on device.
Definition: KokkosMap.h:228

◆ end() [1/2]

template<typename T1, typename T2, template< typename... > class MapType>
auto Moose::Kokkos::MapBase< T1, T2, MapType >::end ( )
inline

Get the end writeable iterator of the host map.

Returns
The end iterator

Definition at line 88 of file KokkosMap.h.

Referenced by Moose::Kokkos::MapBase< unsigned int, unsigned int >::end().

88 { return get().end(); }
auto end()
Get the end writeable iterator of the host map.
Definition: KokkosMap.h:88

◆ end() [2/2]

template<typename T1, typename T2, template< typename... > class MapType>
auto Moose::Kokkos::MapBase< T1, T2, MapType >::end ( ) const
inline

Get the end const iterator of the host map.

Returns
The end iterator

Definition at line 93 of file KokkosMap.h.

93 { return get().cend(); }

◆ exists()

template<typename T1, typename T2, template< typename... > class MapType>
KOKKOS_FUNCTION bool Moose::Kokkos::MapBase< T1, T2, MapType >::exists ( const T1 &  key) const
inline

Get whether a key exists.

Parameters
keyThe key
Returns
Whether the key exists

Definition at line 163 of file KokkosMap.h.

163 { return find(key) != invalid_id; }
KOKKOS_FUNCTION const T1 & key(dof_id_type idx) const
Get the key of an index.
Definition: KokkosMap.h:169
static const dof_id_type invalid_id
Definition: KokkosMap.h:210
KOKKOS_FUNCTION dof_id_type find(const T1 &key) const
Find the index of a key.
Definition: KokkosMap.h:318

◆ find()

template<typename T1, typename T2 , template< typename... > class MapType>
KOKKOS_FUNCTION dof_id_type Moose::Kokkos::MapBase< T1, T2, MapType >::find ( const T1 &  key) const

Find the index of a key.

Parameters
keyThe key
Returns
The index of the key, invalid_id if the key does not exist

Definition at line 318 of file KokkosMap.h.

Referenced by Moose::Kokkos::MapBase< unsigned int, unsigned int >::exists().

319 {
320  if (!size())
321  return invalid_id;
322 
323  auto bucket = fnv1aHash(key) % size();
324  auto begin = _offset[bucket];
325  auto end = _offset[bucket + 1];
326 
327  for (dof_id_type i = begin; i < end; ++i)
328  if (_keys[i] == key)
329  return i;
330 
331  return invalid_id;
332 }
auto end()
Get the end writeable iterator of the host map.
Definition: KokkosMap.h:88
KOKKOS_FUNCTION uint32_t fnv1aHash(const T &key, uint32_t hash)
Definition: KokkosMap.h:26
Array< dof_id_type > _offset
Beginning offset into device arrays of each bucket.
Definition: KokkosMap.h:236
KOKKOS_FUNCTION const T1 & key(dof_id_type idx) const
Get the key of an index.
Definition: KokkosMap.h:169
static const dof_id_type invalid_id
Definition: KokkosMap.h:210
auto begin()
Get the beginning writeable iterator of the host map.
Definition: KokkosMap.h:78
KOKKOS_FUNCTION dof_id_type size() const
Get the size of map.
Definition: KokkosMap.h:146
Array< T1 > _keys
Keys on device.
Definition: KokkosMap.h:228
uint8_t dof_id_type

◆ get() [1/2]

template<typename T1, typename T2, template< typename... > class MapType>
auto& Moose::Kokkos::MapBase< T1, T2, MapType >::get ( )
inline

Get the underlying writeable host map.

Returns
The writeable host map

Definition at line 98 of file KokkosMap.h.

Referenced by Moose::Kokkos::dataLoad(), Moose::Kokkos::dataStore(), and Moose::Kokkos::MapBase< unsigned int, unsigned int >::swap().

99  {
100  if (!_map_host)
101  _map_host = std::make_shared<MapType<T1, T2>>();
102 
103  return *_map_host;
104  }
std::shared_ptr< MapType< T1, T2 > > _map_host
Standard map on host Stored as a shared pointer to avoid deep copy.
Definition: KokkosMap.h:224

◆ get() [2/2]

template<typename T1, typename T2, template< typename... > class MapType>
const auto& Moose::Kokkos::MapBase< T1, T2, MapType >::get ( ) const
inline

Get the underlying const host map.

Returns
The const host map

Definition at line 109 of file KokkosMap.h.

110  {
111  if (!_map_host)
112  _map_host = std::make_shared<MapType<T1, T2>>();
113 
114  return *_map_host;
115  }
std::shared_ptr< MapType< T1, T2 > > _map_host
Standard map on host Stored as a shared pointer to avoid deep copy.
Definition: KokkosMap.h:224

◆ key()

template<typename T1, typename T2, template< typename... > class MapType>
KOKKOS_FUNCTION const T1& Moose::Kokkos::MapBase< T1, T2, MapType >::key ( dof_id_type  idx) const
inline

Get the key of an index.

Parameters
idxThe index returned by find()
Returns
The const reference of the key

Definition at line 169 of file KokkosMap.h.

Referenced by Moose::Kokkos::MapBase< unsigned int, unsigned int >::exists(), Moose::Kokkos::MapBase< unsigned int, unsigned int >::operator()(), and Moose::Kokkos::MapBase< unsigned int, unsigned int >::operator[]().

170  {
171  KOKKOS_ASSERT(idx != invalid_id);
172 
173  return _keys[idx];
174  }
static const dof_id_type invalid_id
Definition: KokkosMap.h:210
Array< T1 > _keys
Keys on device.
Definition: KokkosMap.h:228
unsigned int idx(const ElemType type, const unsigned int nx, const unsigned int i, const unsigned int j)

◆ operator()()

template<typename T1, typename T2, template< typename... > class MapType>
KOKKOS_FUNCTION const T2& Moose::Kokkos::MapBase< T1, T2, MapType >::operator() ( const T1 &  key) const
inline

Definition at line 206 of file KokkosMap.h.

206 { return operator[](key); }
KOKKOS_FUNCTION const T1 & key(dof_id_type idx) const
Get the key of an index.
Definition: KokkosMap.h:169
T2 & operator[](const T1 &key)
Call host map&#39;s operator[].
Definition: KokkosMap.h:125

◆ operator[]() [1/2]

template<typename T1, typename T2, template< typename... > class MapType>
T2& Moose::Kokkos::MapBase< T1, T2, MapType >::operator[] ( const T1 &  key)
inline

Call host map's operator[].

Parameters
keyThe key
Returns
The writeable reference of the value

Definition at line 125 of file KokkosMap.h.

Referenced by Moose::Kokkos::MapBase< unsigned int, unsigned int >::operator()().

125 { return get()[key]; }
KOKKOS_FUNCTION const T1 & key(dof_id_type idx) const
Get the key of an index.
Definition: KokkosMap.h:169

◆ operator[]() [2/2]

template<typename T1, typename T2, template< typename... > class MapType>
KOKKOS_FUNCTION const T2& Moose::Kokkos::MapBase< T1, T2, MapType >::operator[] ( const T1 &  key) const
inline

Get the value corresponding to a key.

Parameters
keyThe key
Returns
The const reference of the value

Definition at line 192 of file KokkosMap.h.

193  {
194  KOKKOS_IF_ON_HOST(return get().at(key);)
195 
196  auto idx = find(key);
197 
198  KOKKOS_ASSERT(idx != invalid_id);
199 
200  return _values[idx];
201  }
KOKKOS_FUNCTION const T1 & key(dof_id_type idx) const
Get the key of an index.
Definition: KokkosMap.h:169
Array< T2 > _values
Values on device.
Definition: KokkosMap.h:232
static const dof_id_type invalid_id
Definition: KokkosMap.h:210
KOKKOS_FUNCTION dof_id_type find(const T1 &key) const
Find the index of a key.
Definition: KokkosMap.h:318
unsigned int idx(const ElemType type, const unsigned int nx, const unsigned int i, const unsigned int j)

◆ size()

template<typename T1, typename T2, template< typename... > class MapType>
KOKKOS_FUNCTION dof_id_type Moose::Kokkos::MapBase< T1, T2, MapType >::size ( ) const
inline

Get the size of map.

Returns
The size of map

Definition at line 146 of file KokkosMap.h.

Referenced by Moose::Kokkos::MapBase< unsigned int, unsigned int >::size().

147  {
148  KOKKOS_IF_ON_HOST(return get().size();)
149 
150  return _keys.size();
151  }
KOKKOS_FUNCTION index_type size() const
Get the total array size.
Definition: KokkosArray.h:205
KOKKOS_FUNCTION dof_id_type size() const
Get the size of map.
Definition: KokkosMap.h:146
Array< T1 > _keys
Keys on device.
Definition: KokkosMap.h:228

◆ swap()

template<typename T1, typename T2, template< typename... > class MapType>
void Moose::Kokkos::MapBase< T1, T2, MapType >::swap ( MapBase< T1, T2, MapType > &  map)

Swap with another Kokkos map.

Parameters
mapThe Kokkos map to be swapped

Definition at line 308 of file KokkosMap.h.

309 {
310  get().swap(map.get());
311  _keys.swap(map._keys);
312  _values.swap(map._values);
313  _offset.swap(map._offset);
314 }
void swap(ArrayBase< T, dimension, index_type > &array)
Swap with another Kokkos array.
Definition: KokkosArray.h:1201
Array< dof_id_type > _offset
Beginning offset into device arrays of each bucket.
Definition: KokkosMap.h:236
Array< T2 > _values
Values on device.
Definition: KokkosMap.h:232
void swap(MapBase< T1, T2, MapType > &map)
Swap with another Kokkos map.
Definition: KokkosMap.h:308
Array< T1 > _keys
Keys on device.
Definition: KokkosMap.h:228

◆ value()

template<typename T1, typename T2, template< typename... > class MapType>
KOKKOS_FUNCTION const T2& Moose::Kokkos::MapBase< T1, T2, MapType >::value ( dof_id_type  idx) const
inline

Get the value of an index.

Parameters
idxThe index returned by find()
Returns
The const reference of the value

Definition at line 180 of file KokkosMap.h.

181  {
182  KOKKOS_ASSERT(idx != invalid_id);
183 
184  return _values[idx];
185  }
Array< T2 > _values
Values on device.
Definition: KokkosMap.h:232
static const dof_id_type invalid_id
Definition: KokkosMap.h:210
unsigned int idx(const ElemType type, const unsigned int nx, const unsigned int i, const unsigned int j)

Friends And Related Function Documentation

◆ dataLoad

template<typename T1, typename T2, template< typename... > class MapType>
void dataLoad ( std::istream &  stream,
MapBase< T1, T2, MapType > &  map,
void context 
)
friend

Definition at line 348 of file KokkosMap.h.

349 {
351 
352  dataLoad(stream, map.get(), context);
353  dataLoad(stream, map._keys, context);
354  dataLoad(stream, map._values, context);
355  dataLoad(stream, map._offset, context);
356 }
void dataLoad(std::istream &stream, MapBase< T1, T2, MapType > &map, void *context)
Definition: KokkosMap.h:348
friend void dataLoad(std::istream &, MapBase< T1, T2, MapType > &, void *)
Definition: KokkosMap.h:348

◆ dataStore

template<typename T1, typename T2, template< typename... > class MapType>
void dataStore ( std::ostream &  stream,
MapBase< T1, T2, MapType > &  map,
void context 
)
friend

Definition at line 336 of file KokkosMap.h.

337 {
339 
340  dataStore(stream, map.get(), context);
341  dataStore(stream, map._keys, context);
342  dataStore(stream, map._values, context);
343  dataStore(stream, map._offset, context);
344 }
void dataStore(std::ostream &stream, MapBase< T1, T2, MapType > &map, void *context)
Definition: KokkosMap.h:336
friend void dataStore(std::ostream &, MapBase< T1, T2, MapType > &, void *)
Definition: KokkosMap.h:336

Member Data Documentation

◆ _keys

template<typename T1, typename T2, template< typename... > class MapType>
Array<T1> Moose::Kokkos::MapBase< T1, T2, MapType >::_keys
private

◆ _map_host

template<typename T1, typename T2, template< typename... > class MapType>
std::shared_ptr<MapType<T1, T2> > Moose::Kokkos::MapBase< T1, T2, MapType >::_map_host
mutableprivate

Standard map on host Stored as a shared pointer to avoid deep copy.

Definition at line 224 of file KokkosMap.h.

Referenced by Moose::Kokkos::MapBase< unsigned int, unsigned int >::get().

◆ _offset

template<typename T1, typename T2, template< typename... > class MapType>
Array<dof_id_type> Moose::Kokkos::MapBase< T1, T2, MapType >::_offset
private

Beginning offset into device arrays of each bucket.

Definition at line 236 of file KokkosMap.h.

Referenced by Moose::Kokkos::dataLoad(), Moose::Kokkos::dataStore(), and Moose::Kokkos::MapBase< unsigned int, unsigned int >::swap().

◆ _values

template<typename T1, typename T2, template< typename... > class MapType>
Array<T2> Moose::Kokkos::MapBase< T1, T2, MapType >::_values
private

◆ invalid_id

template<typename T1, typename T2, template< typename... > class MapType>
const dof_id_type Moose::Kokkos::MapBase< T1, T2, MapType >::invalid_id = libMesh::DofObject::invalid_id
static

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