https://mooseframework.inl.gov
Loading...
Searching...
No Matches
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.
 
auto begin () const
 Get the beginning const iterator of the host map.
 
auto end ()
 Get the end writeable iterator of the host map.
 
auto end () const
 Get the end const iterator of the host map.
 
auto & get ()
 Get the underlying writeable host map.
 
const auto & get () const
 Get the underlying const host map.
 
void clear ()
 Clear the underlying data.
 
T2 & operator[] (const T1 &key)
 Call host map's operator[].
 
void copyToDevice ()
 Copy host map to device.
 
void copyToDeviceNested ()
 Copy host map to device, perform nested copy for Kokkos arrays.
 
void swap (MapBase< T1, T2, MapType > &map)
 Swap with another Kokkos map.
 
KOKKOS_FUNCTION dof_id_type size () const
 Get the size of map.
 
KOKKOS_FUNCTION dof_id_type find (const T1 &key) const
 Find the index of a key.
 
KOKKOS_FUNCTION bool exists (const T1 &key) const
 Get whether a key exists.
 
KOKKOS_FUNCTION const T1 & key (dof_id_type idx) const
 Get the key of an index.
 
KOKKOS_FUNCTION const T2 & value (dof_id_type idx) const
 Get the value of an index.
 
KOKKOS_FUNCTION const T2 & operator[] (const T1 &key) const
 Get the value corresponding to a key.
 
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.
 

Private Attributes

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

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 71 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.

78{ return get().begin(); }
auto & get()
Get the underlying writeable host map.
Definition KokkosMap.h:98

◆ 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();
251}
void destroy()
Free all data and reset.
Array< T2 > _values
Values on device.
Definition KokkosMap.h:232
Array< dof_id_type > _offset
Beginning offset into device arrays of each bucket.
Definition KokkosMap.h:236
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());
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}
void copyToDevice()
Copy data from host to device.
KOKKOS_FUNCTION iterator end() const
Get the end iterator.
KOKKOS_FUNCTION iterator begin() const
Get the beginning iterator.
void create(const std::vector< index_type > &n)
Allocate array on host and device.
KOKKOS_FUNCTION const T1 & key(dof_id_type idx) const
Get the key of an index.
Definition KokkosMap.h:169
KOKKOS_FUNCTION dof_id_type size() const
Get the size of map.
Definition KokkosMap.h:146
KOKKOS_FUNCTION const T2 & value(dof_id_type idx) const
Get the value of an index.
Definition KokkosMap.h:180
KOKKOS_FUNCTION uint32_t fnv1aHash(const T &key, uint32_t hash)
Definition KokkosMap.h:26
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 copy()
Internal method to copy host map to device.
Definition KokkosMap.h:256

◆ 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}
void copyToDeviceNested()
Copy all the nested Kokkos arrays including self from host to device.

◆ 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.

88{ return get().end(); }

◆ 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; }
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.

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
auto begin()
Get the beginning writeable iterator of the host map.
Definition KokkosMap.h:78

Referenced by Moose::Kokkos::MapBase< T1, T2, MapType >::exists(), and Moose::Kokkos::MapBase< T1, T2, MapType >::operator[]().

◆ 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.

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

Referenced by Moose::Kokkos::MapBase< T1, T2, MapType >::begin(), Moose::Kokkos::MapBase< T1, T2, MapType >::begin(), Moose::Kokkos::MapBase< T1, T2, MapType >::end(), Moose::Kokkos::MapBase< T1, T2, MapType >::end(), Moose::Kokkos::MapBase< T1, T2, MapType >::operator[](), Moose::Kokkos::MapBase< T1, T2, MapType >::operator[](), Moose::Kokkos::MapBase< T1, T2, MapType >::size(), and Moose::Kokkos::MapBase< T1, T2, MapType >::swap().

◆ 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 }

◆ 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.

170 {
171 KOKKOS_ASSERT(idx != invalid_id);
172
173 return _keys[idx];
174 }

Referenced by Moose::Kokkos::MapBase< T1, T2, MapType >::exists(), Moose::Kokkos::MapBase< T1, T2, MapType >::operator()(), Moose::Kokkos::MapBase< T1, T2, MapType >::operator[](), and Moose::Kokkos::MapBase< T1, T2, MapType >::operator[]().

◆ 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); }
T2 & operator[](const T1 &key)
Call host map'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.

125{ return get()[key]; }

Referenced by Moose::Kokkos::MapBase< T1, T2, MapType >::operator()().

◆ 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 }

◆ 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.

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.

Referenced by Moose::Kokkos::MapBase< T1, T2, MapType >::size().

◆ 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.

◆ 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 }

Friends And Related Symbol 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{
350 using ::dataLoad;
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}
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{
338 using ::dataStore;
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}
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< T1, T2, MapType >::get(), and Moose::Kokkos::MapBase< T1, T2, MapType >::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::MapBase< T1, T2, MapType >::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: