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, index_type, 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, index_type, MapType > &map)
 Swap with another Kokkos map.
 
KOKKOS_FUNCTION index_type size () const
 Get the size of map.
 
KOKKOS_FUNCTION index_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 (index_type idx) const
 Get the key of an index.
 
KOKKOS_FUNCTION const T2 & value (index_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 constexpr index_type invalid_id = std::numeric_limits<index_type>::max()
 

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, 1, index_type > _keys
 Keys on device.
 
Array< T2, 1, index_type > _values
 Values on device.
 
Array< index_type, 1, index_type > _offset
 Beginning offset into device arrays of each bucket.
 

Friends

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

Detailed Description

template<typename T1, typename T2, typename index_type, template< typename... > class MapType>
class Moose::Kokkos::MapBase< T1, T2, index_type, 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 68 of file KokkosMap.h.

Member Function Documentation

◆ begin() [1/2]

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

Get the beginning writeable iterator of the host map.

Returns
The beginning iterator

Definition at line 75 of file KokkosMap.h.

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

◆ begin() [2/2]

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

Get the beginning const iterator of the host map.

Returns
The beginning iterator

Definition at line 80 of file KokkosMap.h.

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

◆ clear()

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

Clear the underlying data.

Definition at line 245 of file KokkosMap.h.

246{
247 get().clear();
248
249 _keys.destroy();
252}
void destroy()
Free all data and reset.
Array< index_type, 1, index_type > _offset
Beginning offset into device arrays of each bucket.
Definition KokkosMap.h:233
Array< T2, 1, index_type > _values
Values on device.
Definition KokkosMap.h:229
Array< T1, 1, index_type > _keys
Keys on device.
Definition KokkosMap.h:225

◆ copy()

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

Internal method to copy host map to device.

Definition at line 257 of file KokkosMap.h.

258{
259 _keys.create(size());
261 _offset.create(size() + 1);
262 _offset = 0;
263
264 for (const auto & [key, value] : get())
265 {
266 auto bucket = hashKey(key) % size();
267
268 _offset[bucket]++;
269 }
270
271 std::exclusive_scan(_offset.begin(), _offset.end(), _offset.begin(), index_type(0));
272
274
275 std::vector<index_type> idx(size(), 0);
276
277 for (const auto & [key, value] : get())
278 {
279 auto bucket = hashKey(key) % size();
280
281 _keys[_offset[bucket] + idx[bucket]] = key;
282 _values[_offset[bucket] + idx[bucket]] = value;
283 idx[bucket]++;
284 }
285}
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 index_type size() const
Get the size of map.
Definition KokkosMap.h:143
KOKKOS_FUNCTION const T1 & key(index_type idx) const
Get the key of an index.
Definition KokkosMap.h:166
KOKKOS_FUNCTION const T2 & value(index_type idx) const
Get the value of an index.
Definition KokkosMap.h:177
KOKKOS_FUNCTION uint32_t hashKey(const T &key, uint32_t seed=0)
Hash a key for the device hash table using the MurmurHash3 implementation of Kokkos,...
Definition KokkosMap.h:35
unsigned int idx(const ElemType type, const unsigned int nx, const unsigned int i, const unsigned int j)

◆ copyToDevice()

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

Copy host map to device.

Definition at line 289 of file KokkosMap.h.

290{
291 copy();
292
295}
void copy()
Internal method to copy host map to device.
Definition KokkosMap.h:257

◆ copyToDeviceNested()

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

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

Definition at line 299 of file KokkosMap.h.

300{
301 copy();
302
305}
void copyToDeviceNested()
Copy all the nested Kokkos arrays including self from host to device.

◆ end() [1/2]

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

Get the end writeable iterator of the host map.

Returns
The end iterator

Definition at line 85 of file KokkosMap.h.

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

◆ end() [2/2]

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

Get the end const iterator of the host map.

Returns
The end iterator

Definition at line 90 of file KokkosMap.h.

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

◆ exists()

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

Get whether a key exists.

Parameters
keyThe key
Returns
Whether the key exists

Definition at line 160 of file KokkosMap.h.

160{ return find(key) != invalid_id; }
KOKKOS_FUNCTION index_type find(const T1 &key) const
Find the index of a key.
Definition KokkosMap.h:319
static constexpr index_type invalid_id
Definition KokkosMap.h:207

◆ find()

template<typename T1 , typename T2 , typename index_type , template< typename... > class MapType>
KOKKOS_FUNCTION index_type Moose::Kokkos::MapBase< T1, T2, index_type, 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 319 of file KokkosMap.h.

320{
321 if (!size())
322 return invalid_id;
323
324 auto bucket = hashKey(key) % size();
325 auto begin = _offset[bucket];
326 auto end = _offset[bucket + 1];
327
328 for (index_type i = begin; i < end; ++i)
329 if (_keys[i] == key)
330 return i;
331
332 return invalid_id;
333}
auto begin()
Get the beginning writeable iterator of the host map.
Definition KokkosMap.h:75
auto end()
Get the end writeable iterator of the host map.
Definition KokkosMap.h:85

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

◆ get() [1/2]

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

◆ get() [2/2]

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

Get the underlying const host map.

Returns
The const host map

Definition at line 106 of file KokkosMap.h.

107 {
108 if (!_map_host)
109 _map_host = std::make_shared<MapType<T1, T2>>();
110
111 return *_map_host;
112 }

◆ key()

template<typename T1 , typename T2 , typename index_type , template< typename... > class MapType>
KOKKOS_FUNCTION const T1 & Moose::Kokkos::MapBase< T1, T2, index_type, MapType >::key ( index_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 166 of file KokkosMap.h.

167 {
168 KOKKOS_ASSERT(idx != invalid_id);
169
170 return _keys[idx];
171 }

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

◆ operator()()

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

Definition at line 203 of file KokkosMap.h.

203{ return operator[](key); }
T2 & operator[](const T1 &key)
Call host map's operator[].
Definition KokkosMap.h:122

◆ operator[]() [1/2]

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

Call host map's operator[].

Parameters
keyThe key
Returns
The writeable reference of the value

Definition at line 122 of file KokkosMap.h.

122{ return get()[key]; }

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

◆ operator[]() [2/2]

template<typename T1 , typename T2 , typename index_type , template< typename... > class MapType>
KOKKOS_FUNCTION const T2 & Moose::Kokkos::MapBase< T1, T2, index_type, 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 189 of file KokkosMap.h.

190 {
191 KOKKOS_IF_ON_HOST(return get().at(key);)
192
193 auto idx = find(key);
194
195 KOKKOS_ASSERT(idx != invalid_id);
196
197 return _values[idx];
198 }

◆ size()

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

Get the size of map.

Returns
The size of map

Definition at line 143 of file KokkosMap.h.

144 {
145 KOKKOS_IF_ON_HOST(return get().size();)
146
147 return _keys.size();
148 }
KOKKOS_FUNCTION index_type size() const
Get the total array size.

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

◆ swap()

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

Swap with another Kokkos map.

Parameters
mapThe Kokkos map to be swapped

Definition at line 309 of file KokkosMap.h.

310{
311 get().swap(map.get());
312 _keys.swap(map._keys);
313 _values.swap(map._values);
314 _offset.swap(map._offset);
315}
void swap(ArrayBase< T, dimension, index_type > &array)
Swap with another Kokkos array.

◆ value()

template<typename T1 , typename T2 , typename index_type , template< typename... > class MapType>
KOKKOS_FUNCTION const T2 & Moose::Kokkos::MapBase< T1, T2, index_type, MapType >::value ( index_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 177 of file KokkosMap.h.

178 {
179 KOKKOS_ASSERT(idx != invalid_id);
180
181 return _values[idx];
182 }

Friends And Related Symbol Documentation

◆ dataLoad

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

Definition at line 349 of file KokkosMap.h.

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

◆ dataStore

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

Definition at line 337 of file KokkosMap.h.

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

Member Data Documentation

◆ _keys

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

◆ _map_host

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

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

Definition at line 221 of file KokkosMap.h.

Referenced by Moose::Kokkos::MapBase< T1, T2, index_type, MapType >::get(), and Moose::Kokkos::MapBase< T1, T2, index_type, MapType >::get().

◆ _offset

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

Beginning offset into device arrays of each bucket.

Definition at line 233 of file KokkosMap.h.

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

◆ _values

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

◆ invalid_id

template<typename T1 , typename T2 , typename index_type , template< typename... > class MapType>
constexpr index_type Moose::Kokkos::MapBase< T1, T2, index_type, MapType >::invalid_id = std::numeric_limits<index_type>::max()
staticconstexpr

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