https://mooseframework.inl.gov
Loading...
Searching...
No Matches
KokkosMap.h
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9
10#pragma once
11
12#ifdef MOOSE_KOKKOS_SCOPE
13#include "Kokkos_Functional.hpp"
14#endif
15
16#include "KokkosArray.h"
17
18#include <memory>
19#include <map>
20#include <limits>
21
22namespace Moose::Kokkos
23{
24
25#ifdef MOOSE_KOKKOS_SCOPE
33template <typename T>
34KOKKOS_FUNCTION uint32_t
35hashKey(const T & key, uint32_t seed = 0)
36{
37 return ::Kokkos::pod_hash<T>()(key, seed);
38}
39
40template <typename T1, typename T2>
41struct Pair;
42
46template <typename T1, typename T2>
47KOKKOS_FUNCTION uint32_t
48hashKey(const Pair<T1, T2> & key, uint32_t seed = 0)
49{
50 return hashKey(key.second, hashKey(key.first, seed));
51}
52#endif
53
54template <typename T1, typename T2, typename index_type, template <typename...> class MapType>
55class MapBase;
56
57template <typename T1, typename T2, typename index_type, template <typename...> class MapType>
58void dataStore(std::ostream & stream, MapBase<T1, T2, index_type, MapType> & map, void * context);
59template <typename T1, typename T2, typename index_type, template <typename...> class MapType>
60void dataLoad(std::istream & stream, MapBase<T1, T2, index_type, MapType> & map, void * context);
61
67template <typename T1, typename T2, typename index_type, template <typename...> class MapType>
69{
70public:
75 auto begin() { return get().begin(); }
80 auto begin() const { return get().cbegin(); }
85 auto end() { return get().end(); }
90 auto end() const { return get().cend(); }
95 auto & get()
96 {
97 if (!_map_host)
98 _map_host = std::make_shared<MapType<T1, T2>>();
99
100 return *_map_host;
101 }
106 const auto & get() const
107 {
108 if (!_map_host)
109 _map_host = std::make_shared<MapType<T1, T2>>();
110
111 return *_map_host;
112 }
116 void clear();
122 T2 & operator[](const T1 & key) { return get()[key]; }
123
124#ifdef MOOSE_KOKKOS_SCOPE
138
143 KOKKOS_FUNCTION index_type size() const
144 {
145 KOKKOS_IF_ON_HOST(return get().size();)
146
147 return _keys.size();
148 }
154 KOKKOS_FUNCTION index_type find(const T1 & key) const;
160 KOKKOS_FUNCTION bool exists(const T1 & key) const { return find(key) != invalid_id; }
166 KOKKOS_FUNCTION const T1 & key(index_type idx) const
167 {
168 KOKKOS_ASSERT(idx != invalid_id);
169
170 return _keys[idx];
171 }
177 KOKKOS_FUNCTION const T2 & value(index_type idx) const
178 {
179 KOKKOS_ASSERT(idx != invalid_id);
180
181 return _values[idx];
182 }
189 KOKKOS_FUNCTION const T2 & operator[](const T1 & key) const
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 }
199 // Due to a stupid NVCC compiler bug, one cannot do var[i][j] for a variable whose type is
200 // Array<Map<...>> (the second operator[] seems to be interpreted as if it is the operator of
201 // Array), while var[i](j) works. Until we figure out what is going on, one can use the following
202 // operator as a workaround.
203 KOKKOS_FUNCTION const T2 & operator()(const T1 & key) const { return operator[](key); }
205#endif
206
207 static constexpr index_type invalid_id = std::numeric_limits<index_type>::max();
208
209private:
210#ifdef MOOSE_KOKKOS_SCOPE
214 void copy();
215#endif
216
221 mutable std::shared_ptr<MapType<T1, T2>> _map_host;
234
235 friend void dataStore<T1, T2, index_type, MapType>(std::ostream &,
237 void *);
238 friend void dataLoad<T1, T2, index_type, MapType>(std::istream &,
240 void *);
241};
242
243template <typename T1, typename T2, typename index_type, template <typename...> class MapType>
244void
246{
247 get().clear();
248
249 _keys.destroy();
250 _values.destroy();
251 _offset.destroy();
252}
253
254#ifdef MOOSE_KOKKOS_SCOPE
255template <typename T1, typename T2, typename index_type, template <typename...> class MapType>
256void
258{
259 _keys.create(size());
260 _values.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
273 _offset.copyToDevice();
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}
286
287template <typename T1, typename T2, typename index_type, template <typename...> class MapType>
288void
290{
291 copy();
292
293 _keys.copyToDevice();
294 _values.copyToDevice();
295}
296
297template <typename T1, typename T2, typename index_type, template <typename...> class MapType>
298void
300{
301 copy();
302
303 _keys.copyToDeviceNested();
304 _values.copyToDeviceNested();
305}
306
307template <typename T1, typename T2, typename index_type, template <typename...> class MapType>
308void
310{
311 get().swap(map.get());
312 _keys.swap(map._keys);
313 _values.swap(map._values);
314 _offset.swap(map._offset);
315}
316
317template <typename T1, typename T2, typename index_type, template <typename...> class MapType>
318KOKKOS_FUNCTION index_type
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}
334
335template <typename T1, typename T2, typename index_type, template <typename...> class MapType>
336void
337dataStore(std::ostream & stream, MapBase<T1, T2, index_type, MapType> & map, void * context)
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}
346
347template <typename T1, typename T2, typename index_type, template <typename...> class MapType>
348void
349dataLoad(std::istream & stream, MapBase<T1, T2, index_type, MapType> & map, void * context)
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}
358#endif
359
360template <typename T1, typename T2, typename index_type = MOOSE_KOKKOS_INDEX_TYPE>
362
363template <typename T1, typename T2, typename index_type = MOOSE_KOKKOS_INDEX_TYPE>
365
366} // namespace Moose::Kokkos
KOKKOS_FUNCTION index_type size() const
Get the total array size.
The Kokkos array class.
The Kokkos wrapper class for standard map.
Definition KokkosMap.h:69
KOKKOS_FUNCTION index_type size() const
Get the size of map.
Definition KokkosMap.h:143
auto begin()
Get the beginning writeable iterator of the host map.
Definition KokkosMap.h:75
void copyToDeviceNested()
Copy host map to device, perform nested copy for Kokkos arrays.
Definition KokkosMap.h:299
void clear()
Clear the underlying data.
Definition KokkosMap.h:245
auto & get()
Get the underlying writeable host map.
Definition KokkosMap.h:95
const auto & get() const
Get the underlying const host map.
Definition KokkosMap.h:106
KOKKOS_FUNCTION const T1 & key(index_type idx) const
Get the key of an index.
Definition KokkosMap.h:166
KOKKOS_FUNCTION const T2 & operator[](const T1 &key) const
Get the value corresponding to a key.
Definition KokkosMap.h:189
Array< index_type, 1, index_type > _offset
Beginning offset into device arrays of each bucket.
Definition KokkosMap.h:233
void copyToDevice()
Copy host map to device.
Definition KokkosMap.h:289
Array< T2, 1, index_type > _values
Values on device.
Definition KokkosMap.h:229
KOKKOS_FUNCTION index_type find(const T1 &key) const
Find the index of a key.
Definition KokkosMap.h:319
Array< T1, 1, index_type > _keys
Keys on device.
Definition KokkosMap.h:225
static constexpr index_type invalid_id
Definition KokkosMap.h:207
KOKKOS_FUNCTION bool exists(const T1 &key) const
Get whether a key exists.
Definition KokkosMap.h:160
auto begin() const
Get the beginning const iterator of the host map.
Definition KokkosMap.h:80
KOKKOS_FUNCTION const T2 & value(index_type idx) const
Get the value of an index.
Definition KokkosMap.h:177
T2 & operator[](const T1 &key)
Call host map's operator[].
Definition KokkosMap.h:122
KOKKOS_FUNCTION const T2 & operator()(const T1 &key) const
Definition KokkosMap.h:203
auto end()
Get the end writeable iterator of the host map.
Definition KokkosMap.h:85
std::shared_ptr< MapType< T1, T2 > > _map_host
Standard map on host Stored as a shared pointer to avoid deep copy.
Definition KokkosMap.h:221
auto end() const
Get the end const iterator of the host map.
Definition KokkosMap.h:90
void copy()
Internal method to copy host map to device.
Definition KokkosMap.h:257
void swap(MapBase< T1, T2, index_type, MapType > &map)
Swap with another Kokkos map.
Definition KokkosMap.h:309
void dataLoad(std::istream &stream, Array< T, dimension, index_type, layout > &array, void *context)
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
void dataStore(std::ostream &stream, Array< T, dimension, index_type, layout > &array, void *context)