https://mooseframework.inl.gov
Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes | Friends | List of all members
Moose::Kokkos::Map< T1, T2 > 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...
 
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 (Map< T1, T2 > &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< std::map< 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 &, Map< T1, T2 > &, void *)
 
void dataLoad (std::istream &, Map< T1, T2 > &, void *)
 

Detailed Description

template<typename T1, typename T2>
class Moose::Kokkos::Map< T1, T2 >

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 60 of file KokkosMap.h.

Member Function Documentation

◆ begin() [1/2]

template<typename T1, typename T2>
auto Moose::Kokkos::Map< T1, T2 >::begin ( )
inline

Get the beginning writeable iterator of the host map.

Returns
The beginning iterator

Definition at line 80 of file KokkosMap.h.

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

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

◆ begin() [2/2]

template<typename T1, typename T2>
auto Moose::Kokkos::Map< T1, T2 >::begin ( ) const
inline

Get the beginning const iterator of the host map.

Returns
The beginning iterator

Definition at line 85 of file KokkosMap.h.

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

◆ copy()

template<typename T1 , typename T2 >
void Moose::Kokkos::Map< T1, T2 >::copy ( )
private

Internal method to copy host map to device.

Definition at line 243 of file KokkosMap.h.

244 {
245  _keys.create(size());
246  _values.create(size());
247  _offset.create(size() + 1);
248  _offset = 0;
249 
250  for (const auto & [key, value] : get())
251  {
252  auto bucket = fnv1aHash(key) % size();
253 
254  _offset[bucket]++;
255  }
256 
257  std::exclusive_scan(_offset.begin(), _offset.end(), _offset.begin(), 0);
258 
259  _offset.copyToDevice();
260 
261  std::vector<dof_id_type> idx(size(), 0);
262 
263  for (const auto & [key, value] : get())
264  {
265  auto bucket = fnv1aHash(key) % size();
266 
267  _keys[_offset[bucket] + idx[bucket]] = key;
268  _values[_offset[bucket] + idx[bucket]] = value;
269  idx[bucket]++;
270  }
271 }
Array< T1 > _keys
Keys on device.
Definition: KokkosMap.h:226
Array< T2 > _values
Values on device.
Definition: KokkosMap.h:230
KOKKOS_FUNCTION uint32_t fnv1aHash(const T &key, uint32_t hash)
Definition: KokkosMap.h:28
Array< dof_id_type > _offset
Beginning offset into device arrays of each bucket.
Definition: KokkosMap.h:234
KOKKOS_FUNCTION const T2 & value(dof_id_type idx) const
Get the value of an index.
Definition: KokkosMap.h:178
KOKKOS_FUNCTION const T1 & key(dof_id_type idx) const
Get the key of an index.
Definition: KokkosMap.h:167
KOKKOS_FUNCTION dof_id_type size() const
Get the size of map.
Definition: KokkosMap.h:144
unsigned int idx(const ElemType type, const unsigned int nx, const unsigned int i, const unsigned int j)

◆ copyToDevice()

template<typename T1 , typename T2 >
void Moose::Kokkos::Map< T1, T2 >::copyToDevice ( )

Copy host map to device.

Definition at line 275 of file KokkosMap.h.

276 {
277  copy();
278 
279  _keys.copyToDevice();
280  _values.copyToDevice();
281 }
Array< T1 > _keys
Keys on device.
Definition: KokkosMap.h:226
Array< T2 > _values
Values on device.
Definition: KokkosMap.h:230
void copy()
Internal method to copy host map to device.
Definition: KokkosMap.h:243

◆ copyToDeviceNested()

template<typename T1 , typename T2 >
void Moose::Kokkos::Map< T1, T2 >::copyToDeviceNested ( )

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

Definition at line 285 of file KokkosMap.h.

286 {
287  copy();
288 
289  _keys.copyToDeviceNested();
290  _values.copyToDeviceNested();
291 }
Array< T1 > _keys
Keys on device.
Definition: KokkosMap.h:226
Array< T2 > _values
Values on device.
Definition: KokkosMap.h:230
void copy()
Internal method to copy host map to device.
Definition: KokkosMap.h:243

◆ end() [1/2]

template<typename T1, typename T2>
auto Moose::Kokkos::Map< T1, T2 >::end ( )
inline

Get the end writeable iterator of the host map.

Returns
The end iterator

Definition at line 90 of file KokkosMap.h.

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

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

◆ end() [2/2]

template<typename T1, typename T2>
auto Moose::Kokkos::Map< T1, T2 >::end ( ) const
inline

Get the end const iterator of the host map.

Returns
The end iterator

Definition at line 95 of file KokkosMap.h.

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

◆ exists()

template<typename T1, typename T2>
KOKKOS_FUNCTION bool Moose::Kokkos::Map< T1, T2 >::exists ( const T1 &  key) const
inline

Get whether a key exists.

Parameters
keyThe key
Returns
Whether the key exists

Definition at line 161 of file KokkosMap.h.

Referenced by KokkosMatCoupledForce::computeQpOffDiagJacobian().

161 { return find(key) != invalid_id; }
static const dof_id_type invalid_id
Definition: KokkosMap.h:208
KOKKOS_FUNCTION dof_id_type find(const T1 &key) const
Find the index of a key.
Definition: KokkosMap.h:305
KOKKOS_FUNCTION const T1 & key(dof_id_type idx) const
Get the key of an index.
Definition: KokkosMap.h:167

◆ find()

template<typename T1, typename T2 >
KOKKOS_FUNCTION dof_id_type Moose::Kokkos::Map< T1, T2 >::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 305 of file KokkosMap.h.

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

306 {
307  if (!size())
308  return invalid_id;
309 
310  auto bucket = fnv1aHash(key) % size();
311  auto begin = _offset[bucket];
312  auto end = _offset[bucket + 1];
313 
314  for (dof_id_type i = begin; i < end; ++i)
315  if (_keys[i] == key)
316  return i;
317 
318  return invalid_id;
319 }
Array< T1 > _keys
Keys on device.
Definition: KokkosMap.h:226
static const dof_id_type invalid_id
Definition: KokkosMap.h:208
KOKKOS_FUNCTION uint32_t fnv1aHash(const T &key, uint32_t hash)
Definition: KokkosMap.h:28
Array< dof_id_type > _offset
Beginning offset into device arrays of each bucket.
Definition: KokkosMap.h:234
auto end()
Get the end writeable iterator of the host map.
Definition: KokkosMap.h:90
KOKKOS_FUNCTION const T1 & key(dof_id_type idx) const
Get the key of an index.
Definition: KokkosMap.h:167
auto begin()
Get the beginning writeable iterator of the host map.
Definition: KokkosMap.h:80
KOKKOS_FUNCTION dof_id_type size() const
Get the size of map.
Definition: KokkosMap.h:144
uint8_t dof_id_type

◆ get() [1/2]

template<typename T1, typename T2>
auto& Moose::Kokkos::Map< T1, T2 >::get ( )
inline

Get the underlying writeable host map.

Returns
The writeable host map

Definition at line 100 of file KokkosMap.h.

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

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

◆ get() [2/2]

template<typename T1, typename T2>
const auto& Moose::Kokkos::Map< T1, T2 >::get ( ) const
inline

Get the underlying const host map.

Returns
The const host map

Definition at line 111 of file KokkosMap.h.

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

◆ key()

template<typename T1, typename T2>
KOKKOS_FUNCTION const T1& Moose::Kokkos::Map< T1, T2 >::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 167 of file KokkosMap.h.

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

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

◆ operator()()

template<typename T1, typename T2>
KOKKOS_FUNCTION const T2& Moose::Kokkos::Map< T1, T2 >::operator() ( const T1 &  key) const
inline

Definition at line 204 of file KokkosMap.h.

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

◆ operator[]() [1/2]

template<typename T1, typename T2>
T2& Moose::Kokkos::Map< T1, T2 >::operator[] ( const T1 &  key)
inline

Call host map's operator[].

Parameters
keyThe key
Returns
The writeable reference of the value

Definition at line 123 of file KokkosMap.h.

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

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

◆ operator[]() [2/2]

template<typename T1, typename T2>
KOKKOS_FUNCTION const T2& Moose::Kokkos::Map< T1, T2 >::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 190 of file KokkosMap.h.

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

◆ size()

template<typename T1, typename T2>
KOKKOS_FUNCTION dof_id_type Moose::Kokkos::Map< T1, T2 >::size ( ) const
inline

Get the size of map.

Returns
The size of map

Definition at line 144 of file KokkosMap.h.

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

145  {
146  KOKKOS_IF_ON_HOST(return get().size();)
147 
148  return _keys.size();
149  }
Array< T1 > _keys
Keys on device.
Definition: KokkosMap.h:226
KOKKOS_FUNCTION dof_id_type size() const
Get the size of map.
Definition: KokkosMap.h:144

◆ swap()

template<typename T1, typename T2>
void Moose::Kokkos::Map< T1, T2 >::swap ( Map< T1, T2 > &  map)

Swap with another Kokkos map.

Parameters
mapThe Kokkos map to be swapped

Definition at line 295 of file KokkosMap.h.

296 {
297  get().swap(map.get());
298  _keys.swap(map._keys);
299  _values.swap(map._values);
300  _offset.swap(map._offset);
301 }
Array< T1 > _keys
Keys on device.
Definition: KokkosMap.h:226
Array< T2 > _values
Values on device.
Definition: KokkosMap.h:230
Array< dof_id_type > _offset
Beginning offset into device arrays of each bucket.
Definition: KokkosMap.h:234
void swap(Map< T1, T2 > &map)
Swap with another Kokkos map.
Definition: KokkosMap.h:295

◆ value()

template<typename T1, typename T2>
KOKKOS_FUNCTION const T2& Moose::Kokkos::Map< T1, T2 >::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 178 of file KokkosMap.h.

179  {
180  KOKKOS_ASSERT(idx != invalid_id);
181 
182  return _values[idx];
183  }
static const dof_id_type invalid_id
Definition: KokkosMap.h:208
Array< T2 > _values
Values on device.
Definition: KokkosMap.h:230
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>
void dataLoad ( std::istream &  stream,
Map< T1, T2 > &  map,
void context 
)
friend

Definition at line 335 of file KokkosMap.h.

336 {
338 
339  dataLoad(stream, map.get(), context);
340  dataLoad(stream, map._keys, context);
341  dataLoad(stream, map._values, context);
342  dataLoad(stream, map._offset, context);
343 }
friend void dataLoad(std::istream &, Map< T1, T2 > &, void *)
Definition: KokkosMap.h:335
void dataLoad(std::istream &stream, Map< T1, T2 > &map, void *context)
Definition: KokkosMap.h:335

◆ dataStore

template<typename T1, typename T2>
void dataStore ( std::ostream &  stream,
Map< T1, T2 > &  map,
void context 
)
friend

Definition at line 323 of file KokkosMap.h.

324 {
326 
327  dataStore(stream, map.get(), context);
328  dataStore(stream, map._keys, context);
329  dataStore(stream, map._values, context);
330  dataStore(stream, map._offset, context);
331 }
friend void dataStore(std::ostream &, Map< T1, T2 > &, void *)
Definition: KokkosMap.h:323
void dataStore(std::ostream &stream, Map< T1, T2 > &map, void *context)
Definition: KokkosMap.h:323

Member Data Documentation

◆ _keys

template<typename T1, typename T2>
Array<T1> Moose::Kokkos::Map< T1, T2 >::_keys
private

◆ _map_host

template<typename T1, typename T2>
std::shared_ptr<std::map<T1, T2> > Moose::Kokkos::Map< T1, T2 >::_map_host
mutableprivate

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

Definition at line 222 of file KokkosMap.h.

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

◆ _offset

template<typename T1, typename T2>
Array<dof_id_type> Moose::Kokkos::Map< T1, T2 >::_offset
private

Beginning offset into device arrays of each bucket.

Definition at line 234 of file KokkosMap.h.

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

◆ _values

template<typename T1, typename T2>
Array<T2> Moose::Kokkos::Map< T1, T2 >::_values
private

◆ invalid_id

template<typename T1, typename T2>
const dof_id_type Moose::Kokkos::Map< T1, T2 >::invalid_id = libMesh::DofObject::invalid_id
static

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