https://mooseframework.inl.gov
Loading...
Searching...
No Matches
KokkosJaggedArray.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#include "KokkosArray.h"
13
14namespace Moose::Kokkos
15{
16
21template <unsigned int inner>
23{
27 unsigned int dim[inner] = {0};
31 unsigned int stride[inner + 1] = {0};
32
33#ifdef MOOSE_KOKKOS_SCOPE
39 KOKKOS_FUNCTION unsigned int operator[](unsigned int i) const { return dim[i]; }
40#endif
41};
42
43#ifdef MOOSE_KOKKOS_SCOPE
51template <typename T, unsigned int inner, LayoutType layout>
53{
54public:
61 : _data(data), _dim(dim)
62 {
63 }
64
69 KOKKOS_FUNCTION unsigned int size() const { return _dim.stride[inner]; }
75 KOKKOS_FUNCTION unsigned int n(unsigned int dim) const { return _dim[dim]; }
80 KOKKOS_FUNCTION T & operator[](unsigned int i) const
81 {
82 KOKKOS_ASSERT(i < _dim.stride[inner]);
83
84 return _data[i];
85 }
90 template <typename... indices>
91 KOKKOS_FUNCTION T & operator()(indices... i) const;
98 template <unsigned int dim, typename... indices>
99 KOKKOS_FUNCTION auto slice(indices... i) const;
100
101protected:
105 T * _data;
110};
111
112template <typename T, unsigned int inner, LayoutType layout>
113template <typename... indices>
114KOKKOS_FUNCTION T &
116{
117 static_assert((std::is_convertible<indices, unsigned int>::value && ...),
118 "All arguments must be convertible to unsigned int");
119 static_assert(sizeof...(i) == inner, "Number of arguments should match array dimension");
120
121#ifndef NDEBUG
122 {
123 unsigned int idx[inner] = {static_cast<unsigned int>(i)...};
124
125 for (unsigned int d = 0; d < sizeof...(i); ++d)
126 KOKKOS_ASSERT(idx[d] < _dim[d]);
127 }
128#endif
129
130 unsigned int idx = 0;
131 unsigned int d = 0;
132
133 if constexpr (layout == LayoutType::LEFT)
134 (((idx +=
135 (d == 0 ? static_cast<unsigned int>(i) : static_cast<unsigned int>(i) * _dim.stride[d])),
136 ++d),
137 ...);
138 else
139 (((idx += (d == inner - 1 ? static_cast<unsigned int>(i)
140 : static_cast<unsigned int>(i) * _dim.stride[d])),
141 ++d),
142 ...);
143
144 return _data[idx];
145}
146
152template <typename T>
154{
155public:
162 KOKKOS_FUNCTION
163 JaggedArrayInnerSlice(T * data, const unsigned int stride, const unsigned int size)
164 : _data(data), _stride(stride), _size(size)
165 {
166 }
167
172 KOKKOS_FUNCTION unsigned int size() const { return _size; }
177 KOKKOS_FUNCTION T & operator[](const unsigned int i) const
178 {
179 KOKKOS_ASSERT(i < _size);
180
181 return _data[i * _stride];
182 }
183
184private:
188 T * const _data;
192 const unsigned int _stride;
196 const unsigned int _size;
197};
198
199template <typename T, unsigned int inner, LayoutType layout>
200template <unsigned int dim, typename... indices>
201KOKKOS_FUNCTION auto
203{
204 static_assert(dim < inner, "The sliced dimension should be less than the inner array dimension");
205 static_assert(sizeof...(i) == inner - 1,
206 "Number of arguments should be the inner array dimension minus one");
207
208 const unsigned int fixed[inner - 1] = {static_cast<unsigned int>(i)...};
209
210 // The stride of every dimension is stored by reserve(), including the contiguous one
211 unsigned int offset = 0;
212
213 for (unsigned int d = 0, f = 0; d < inner; ++d)
214 if (d != dim)
215 {
216 KOKKOS_ASSERT(fixed[f] < _dim[d]);
217
218 offset += fixed[f++] * _dim.stride[d];
219 }
220
221 return JaggedArrayInnerSlice<T>(_data + offset, _dim.stride[dim], _dim[dim]);
222}
223#endif
224
244template <typename T,
245 unsigned int inner,
246 unsigned int outer,
247 typename index_type = MOOSE_KOKKOS_INDEX_TYPE,
250{
251public:
255 JaggedArray() = default;
256
257#ifdef MOOSE_KOKKOS_SCOPE
261 template <typename... size_type>
262 JaggedArray(size_type... n)
263 {
264 create(n...);
265 }
266
271 void create(const std::vector<index_type> & n);
276 template <typename... size_type>
277 void create(size_type... n);
283 void reserve(const std::array<uint64_t, outer> & index,
284 const std::array<uint64_t, inner> & dimension);
288 void finalize();
293 {
294 mooseAssert(_finalized, "KokkosJaggedArray not finalized.");
295
296 _data.copyToDevice();
297 }
302 {
303 mooseAssert(_finalized, "KokkosJaggedArray not finalized.");
304
305 _data.copyToHost();
306 }
311 {
312 mooseAssert(_finalized, "KokkosJaggedArray not finalized.");
313
314 _dims.moveToDevice();
315 _offsets.moveToDevice();
316 _data.moveToDevice();
317 }
322 {
323 mooseAssert(_finalized, "KokkosJaggedArray not finalized.");
324
325 _dims.moveToHost();
326 _offsets.moveToHost();
327 _data.moveToHost();
328 }
333 auto & array() { return _data; }
334
339 KOKKOS_FUNCTION bool isFinalized() const { return _finalized; }
344 KOKKOS_FUNCTION bool isHostAlloc() const { return _data.isHostAlloc(); }
349 KOKKOS_FUNCTION bool isDeviceAlloc() const { return _data.isDeviceAlloc(); }
354 KOKKOS_FUNCTION index_type size() const { return _data.size(); }
359 KOKKOS_FUNCTION index_type n() const { return _offsets.size(); }
365 KOKKOS_FUNCTION index_type n(unsigned int dim) const { return _offsets.n(dim); }
371 KOKKOS_FUNCTION auto operator[](index_type i) const;
377 template <typename... indices>
378 KOKKOS_FUNCTION auto operator()(indices... i) const;
379#endif
380
381protected:
397 bool _finalized = false;
398};
399
400#ifdef MOOSE_KOKKOS_SCOPE
401template <typename T,
402 unsigned int inner,
403 unsigned int outer,
404 typename index_type,
405 LayoutType layout>
406void
408{
409 _data.destroy();
410 _offsets.create(n);
411 _dims.create(n);
412
413 _finalized = false;
414}
415
416template <typename T,
417 unsigned int inner,
418 unsigned int outer,
419 typename index_type,
420 LayoutType layout>
421template <typename... size_type>
422void
424{
425 _data.destroy();
426 _offsets.create(n...);
427 _dims.create(n...);
428
429 _finalized = false;
430}
431
432template <typename T,
433 unsigned int inner,
434 unsigned int outer,
435 typename index_type,
436 LayoutType layout>
437void
439 const std::array<uint64_t, outer> & index, const std::array<uint64_t, inner> & dimension)
440{
441 mooseAssert(!_finalized, "KokkosJaggedArray already finalized.");
442
443 index_type idx = 0;
444 index_type stride = 1;
445
446 for (unsigned int o = 0; o < outer; ++o)
447 {
448 idx += index[o] * stride;
449 stride *= _offsets.n(o);
450 }
451
452 for (unsigned int i = 0; i < inner; ++i)
453 _dims[idx].dim[i] = dimension[i];
454
455 stride = 1;
456
457 if constexpr (layout == LayoutType::LEFT)
458 for (unsigned int i = 0; i < inner; ++i)
459 {
460 _dims[idx].stride[i] = stride;
461 stride *= dimension[i];
462 }
463 else
464 for (int i = inner - 1; i >= 0; --i)
465 {
466 _dims[idx].stride[i] = stride;
467 stride *= dimension[i];
468 }
469
470 _dims[idx].stride[inner] = stride;
471}
472
473template <typename T,
474 unsigned int inner,
475 unsigned int outer,
476 typename index_type,
477 LayoutType layout>
478void
480{
481 mooseAssert(!_finalized, "KokkosJaggedArray already finalized.");
482
483 index_type stride = 1;
484
485 for (index_type o = 0; o < _offsets.size(); ++o)
486 {
487 stride = 1;
488
489 for (unsigned int i = 0; i < inner; ++i)
490 stride *= _dims[o][i];
491
492 _offsets[o] = stride;
493 }
494
495 std::exclusive_scan(_offsets.begin(), _offsets.end(), _offsets.begin(), 0);
496
497 _dims.copyToDevice();
498 _offsets.copyToDevice();
499
500 // Pad an extra element at the end to avoid accessing the bound in the following operators when
501 // the last inner array has zero size. An empty outer array, e.g. on a process without local
502 // elements, has no last offset.
503 _data.create((_offsets.size() ? _offsets.last() + stride : 0) + 1);
504
505 _finalized = true;
506}
507
508template <typename T,
509 unsigned int inner,
510 unsigned int outer,
511 typename index_type,
512 LayoutType layout>
513KOKKOS_FUNCTION auto
515{
516 auto data = &_data[_offsets[i]];
517 const auto & dim = _dims[i];
518
520}
521
522template <typename T,
523 unsigned int inner,
524 unsigned int outer,
525 typename index_type,
526 LayoutType layout>
527template <typename... indices>
528KOKKOS_FUNCTION auto
530{
531 auto data = &_data[_offsets(i...)];
532 const auto & dim = _dims(i...);
533
535}
536#endif
537
538} // namespace Moose::Kokkos
The Kokkos array class.
The inner array wrapper class.
JaggedArrayInnerDim< inner > _dim
Inner array dimension information.
KOKKOS_FUNCTION auto slice(indices... i) const
Get a one-dimensional view of the inner array along a dimension.
KOKKOS_FUNCTION T & operator()(indices... i) const
Get an array entry.
T * _data
Pointer to the inner array data.
KOKKOS_FUNCTION T & operator[](unsigned int i) const
Get an array entry.
KOKKOS_FUNCTION unsigned int n(unsigned int dim) const
Get the size of a dimension of the inner array.
KOKKOS_FUNCTION JaggedArrayInnerData(T *data, JaggedArrayInnerDim< inner > dim)
Constructor.
KOKKOS_FUNCTION unsigned int size() const
Get the total inner array size.
A one-dimensional view of an inner array along one dimension, with the indices of all other dimension...
const unsigned int _stride
Distance between consecutive entries.
KOKKOS_FUNCTION unsigned int size() const
Get the number of entries.
T *const _data
Pointer to the first entry.
KOKKOS_FUNCTION T & operator[](const unsigned int i) const
Get an entry.
KOKKOS_FUNCTION JaggedArrayInnerSlice(T *data, const unsigned int stride, const unsigned int size)
Constructor.
const unsigned int _size
Number of entries.
The Kokkos jagged array class.
JaggedArray()=default
Default constructor.
KOKKOS_FUNCTION bool isHostAlloc() const
Get whether the array was allocated on host.
void moveToDevice()
Copy data from host to device and deallocate host.
JaggedArray(size_type... n)
Constructor.
bool _finalized
Whether the array was finalized.
KOKKOS_FUNCTION index_type size() const
Get the total data array size.
void moveToHost()
Copy data from device to host and deallocate device.
Array< index_type, outer, index_type > _offsets
Starting offset of each inner array into the sequential data array.
Array< JaggedArrayInnerDim< inner >, outer, index_type > _dims
Dimension information of each inner array.
KOKKOS_FUNCTION bool isFinalized() const
Get whether the array is finalized.
KOKKOS_FUNCTION auto operator()(indices... i) const
Get an inner array.
void copyToDevice()
Copy data from host to device.
void copyToHost()
Copy data from device to host.
void create(const std::vector< index_type > &n)
Allocate outer array.
KOKKOS_FUNCTION index_type n(unsigned int dim) const
Get the size of a dimension of the outer array.
KOKKOS_FUNCTION auto operator[](index_type i) const
Get an inner array.
void create(size_type... n)
Allocate outer array.
Array1D< T, index_type > _data
Sequential data array.
KOKKOS_FUNCTION index_type n() const
Get the total outer array size.
void reserve(const std::array< uint64_t, outer > &index, const std::array< uint64_t, inner > &dimension)
Reserve inner array for an outer array entry.
KOKKOS_FUNCTION bool isDeviceAlloc() const
Get whether the array was allocated on device.
auto & array()
Get the underlying data array.
void finalize()
Setup array structure.
LayoutType
The enumerator that dictates the memory layout.
Definition KokkosArray.h:53
static constexpr std::size_t dim
This is the dimension of all vector and tensor datastructures used in MOOSE.
Definition Moose.h:175
A simple object holding the dimension information of an inner array.
KOKKOS_FUNCTION unsigned int operator[](unsigned int i) const
Get the size of a dimension.
unsigned int stride[inner+1]
Stride of each dimension.
unsigned int dim[inner]
Size of each dimension.