core
Shared MPI block decomposition + asynchronous ghost-layer exchange (header-only C++20)
Loading...
Searching...
No Matches
block_indexer.hpp
Go to the documentation of this file.
1// core — local<->global indexing for a block with a ghost layer.
2//
3// Ported from block_decomposer/src/BlockIndexer.hpp. A block stores an extended array =
4// inner cells + a ghost layer of width `ghostWidth` on every face. Local linear indexing is
5// x-fastest over the extended size.
6#ifndef PECLET_CORE_DECOMP_BLOCK_INDEXER_HPP
7#define PECLET_CORE_DECOMP_BLOCK_INDEXER_HPP
8
10
11namespace peclet::core::decomp {
12
13template <int Dim>
15 public:
16 BlockIndexer() = default;
17 BlockIndexer(IVec<Dim> origin, IVec<Dim> size, int ghostWidth) { init(origin, size, ghostWidth); }
18
19 void init(IVec<Dim> origin, IVec<Dim> size, int ghostWidth) {
20 ghostWidth_ = ghostWidth;
21 for (int i = 0; i < Dim; ++i) {
22 originGhost_[i] = origin[i] - ghostWidth;
23 sizeGhost_[i] = size[i] + 2 * ghostWidth;
24 sizeInner_[i] = size[i];
25 }
26 }
27
28 int ghostWidth() const { return ghostWidth_; }
29 const IVec<Dim>& originInclGhost() const { return originGhost_; }
30 const IVec<Dim>& sizeInclGhost() const { return sizeGhost_; }
31 const IVec<Dim>& sizeInner() const { return sizeInner_; }
32
35 Index n = 1;
36 for (int i = 0; i < Dim; ++i)
37 n *= sizeGhost_[i];
38 return n;
39 }
40
42 Index globalToLocal(const IVec<Dim>& g) const {
43 Index idx = g[Dim - 1] - originGhost_[Dim - 1];
44 for (int i = Dim - 2; i >= 0; --i) {
45 idx *= sizeGhost_[i];
46 idx += (g[i] - originGhost_[i]);
47 }
48 return idx;
49 }
50
53 Index idx = l[Dim - 1];
54 for (int i = Dim - 2; i >= 0; --i) {
55 idx *= sizeGhost_[i];
56 idx += l[i];
57 }
58 return idx;
59 }
60
63 IVec<Dim> l{};
64 for (int i = 0; i < Dim; ++i) {
65 l[i] = idx % sizeGhost_[i];
66 idx /= sizeGhost_[i];
67 }
68 return l;
69 }
70
73 IVec<Dim> g{};
74 for (int i = 0; i < Dim; ++i) {
75 g[i] = idx % sizeGhost_[i] + originGhost_[i];
76 idx /= sizeGhost_[i];
77 }
78 return g;
79 }
80
82 bool isInner(const IVec<Dim>& l) const {
83 for (int i = 0; i < Dim; ++i) {
84 if (l[i] < ghostWidth_ || l[i] >= ghostWidth_ + sizeInner_[i])
85 return false;
86 }
87 return true;
88 }
89
91 template <typename Func>
92 void forEachInner(Func&& func) const {
93 IVec<Dim> bgn{}, end{};
94 for (int i = 0; i < Dim; ++i) {
95 bgn[i] = ghostWidth_;
96 end[i] = sizeGhost_[i] - ghostWidth_;
97 }
99 }
100
102 template <typename Func>
103 void forEachAll(Func&& func) const {
104 IVec<Dim> bgn{}, end{};
105 for (int i = 0; i < Dim; ++i) {
106 bgn[i] = 0;
107 end[i] = sizeGhost_[i];
108 }
110 }
111
112 private:
113 IVec<Dim> originGhost_{};
114 IVec<Dim> sizeGhost_{};
115 IVec<Dim> sizeInner_{};
116 int ghostWidth_ = 1;
117};
118
119} // namespace peclet::core::decomp
120
121#endif // PECLET_CORE_DECOMP_BLOCK_INDEXER_HPP
void forEachInner(Func &&func) const
Visit every inner cell (func receives const IVec<Dim>& local multi-index).
BlockIndexer(IVec< Dim > origin, IVec< Dim > size, int ghostWidth)
void forEachAll(Func &&func) const
Visit every cell including ghosts.
const IVec< Dim > & originInclGhost() const
IVec< Dim > localToLocalMd(Index idx) const
Local linear index -> local multi-index (extended array coordinates, 0 at ghost corner).
Index numCellsInclGhost() const
Total number of cells in the extended (inner + ghost) array.
IVec< Dim > localToGlobalMd(Index idx) const
Local linear index -> global multi-index.
const IVec< Dim > & sizeInner() const
Index globalToLocal(const IVec< Dim > &g) const
Global multi-index -> local linear index in the extended array (x-fastest).
const IVec< Dim > & sizeInclGhost() const
bool isInner(const IVec< Dim > &l) const
True if a local multi-index lies in the inner (non-ghost) region.
void init(IVec< Dim > origin, IVec< Dim > size, int ghostWidth)
Index localMdToLocal(const IVec< Dim > &l) const
Local multi-index (in extended array, ghost included) -> local linear index (x-fastest).
Kokkos::View< T *, MemSpace > View
1D device array.
Definition view.hpp:26
std::int64_t Index
Signed index type for grids and particles (supersedes block_decomposer's long int IndxT).
Definition types.hpp:15