25#ifndef PECLET_CORE_DECOMP_STAGE_TARGET_HPP
26#define PECLET_CORE_DECOMP_STAGE_TARGET_HPP
70 Index m = std::numeric_limits<Index>::max();
71 for (
const auto& s : d.
sizes())
72 for (
int k = 0; k < Dim; ++k)
73 m = s[k] < m ? s[k] : m;
81 for (
const auto& s : d.
sizes()) {
83 for (
int k = 0; k < Dim; ++k)
114template <
int Dim,
class Liftable>
116 Liftable&& liftable,
int minExtent) {
117 for (
int d = cur.
treeDepth() - 1; d >= 0; --d) {
121 bool ok = liftable(m.
dec);
124 if (ok && minExtent > 0 && m.
dec.numBlocks() > 1 &&
152template <
int Dim,
class Liftable>
154 const std::type_identity_t<
IVec<Dim>>& levelGrid,
155 Liftable&& liftable,
int minExtent,
156 Index maxBlockCells) {
157 if (maxBlockCells <= 0)
158 throw std::invalid_argument(
"repartitionTarget: maxBlockCells must be positive");
161 for (
int k = 0; k < Dim; ++k)
162 cells *= levelGrid[k];
163 Index npL = (cells + maxBlockCells - 1) / maxBlockCells;
167 for (
int k = 0; k < Dim; ++k) {
168 const Index f = levelGrid[k] / (2 *
static_cast<Index>(minExtent));
171 cap = c < np ? c : np;
172 npL = npL < cap ? npL : cap;
175 while (up < npL && up < np)
177 npL = up < np ? up : np;
184 npL = npL < 1 ? 1 : npL;
186 while (2 * p2 <= npL)
188 std::vector<Index> tries;
190 tries.push_back(npL);
191 for (
Index n = p2; n >= 1; n /= 2)
193 for (
const Index n : tries) {
198 t.
dec = std::move(r);
199 t.
ownerOf.resize(
static_cast<std::size_t
>(n));
200 for (
Index b = 0; b < n; ++b)
201 t.
ownerOf[
static_cast<std::size_t
>(b)] =
static_cast<int>(b);
245template <
int Dim,
class Liftable>
247 const std::type_identity_t<
IVec<Dim>>& levelGrid,
248 Liftable&& liftable,
int minExtent,
Index maxBlockCells = 0) {
250 throw std::invalid_argument(
251 "chooseStageTarget: levelGrid differs from the current decomposition's global size");
252 if (maxBlockCells < 0)
253 throw std::invalid_argument(
"chooseStageTarget: maxBlockCells must be >= 0 (0 disables)");
258 const bool tooSmall = minExtent > 0 &&
minBlockExtent(cur) <
static_cast<Index>(minExtent);
259 if (nb <= 1 || (liftable(cur) && !tooSmall)) {
263 for (std::size_t b = 0; b < nb; ++b)
264 t.
ownerOf[b] =
static_cast<int>(b);
271 t.
dec = std::move(m->dec);
272 t.
ownerOf = std::move(m->ownerOf);
273 t.
groupOf = std::move(m->groupOf);
279 if (maxBlockCells > 0)
280 if (
auto r =
repartitionTarget(cur, levelGrid, liftable, minExtent, maxBlockCells))
281 return std::move(*r);
287 t.
dec.init(1, levelGrid);
Orthogonal recursive bisection of a Dim-dimensional global cell grid into numBlocks axis-aligned bloc...
const std::vector< IVec< Dim > > & sizes() const
Extent in cells of every block, indexed by block.
int treeDepth() const
Depth of the deepest leaf of the ORB tree (root = 0).
const IVec< Dim > & globalSize() const
Global grid size in cells per axis.
BlockDecomposer< Dim > agglomerated(int depth, std::vector< int > *groupOf=nullptr, std::vector< int > *rootOf=nullptr) const
Coarse-level TELESCOPING: the decomposition obtained by merging ORB siblings — truncate the bisection...
std::size_t numBlocks() const
Number of blocks.
Index largestBlockCells(const BlockDecomposer< Dim > &d)
Cell count of the largest block — replicated, like the decomposition.
StageKind
How a multigrid level moves when it can no longer coarsen in place (see chooseStageTarget and the fil...
@ Repartition
a fresh proportional ORB of the level grid on the first np_L parent ranks
@ InPlace
no stage: the level coarsens on its current decomposition
@ SiblingMerge
ORB tree truncated: contiguous runs of blocks merge onto their lowest rank.
@ Replicated
the whole level as one block, held identically on every rank
std::optional< StageTarget< Dim > > repartitionTarget(const BlockDecomposer< Dim > &cur, const std::type_identity_t< IVec< Dim > > &levelGrid, Liftable &&liftable, int minExtent, Index maxBlockCells)
The Repartition candidate (design §11.2): a fresh proportional, unweighted, unaligned ORB of the leve...
std::optional< SiblingMergeChoice< Dim > > shallowestLiftableMerge(const BlockDecomposer< Dim > &cur, Liftable &&liftable, int minExtent)
The sibling-merge SEARCH on its own, with no trigger: the shallowest merge — fewest merges,...
Index minBlockExtent(const BlockDecomposer< Dim > &d)
Smallest block extent over every block and axis — replicated, like the decomposition.
StageTarget< Dim > chooseStageTarget(const BlockDecomposer< Dim > &cur, const std::type_identity_t< IVec< Dim > > &levelGrid, Liftable &&liftable, int minExtent, Index maxBlockCells=0)
Choose the stage target of a level whose current decomposition is cur over the level grid levelGrid (...
std::int64_t Index
Signed index type for grids and particles (supersedes block_decomposer's long int IndxT).
std::array< Index, Dim > IVec
Multi-dimensional integer index / coordinate (x-fastest order by convention).
A sibling merge the search chose: the tree depth it truncates at, the merged decomposition,...
std::vector< int > groupOf
current block -> merged block
std::vector< int > ownerOf
merged block -> lowest parent rank of its group
BlockDecomposer< Dim > dec
the merged decomposition
int depth
ORB tree depth the merge truncates at (agglomerated(depth)).
Where a level goes: the result of chooseStageTarget / repartitionTarget.
BlockDecomposer< Dim > dec
The target decomposition, of the SAME level grid as the current one.
std::vector< int > groupOf
Current block -> target block. SiblingMerge only (agglomerated's groupOf); empty otherwise.
std::vector< int > ownerOf
Target block -> the rank that owns it, in the PARENT communicator (whose rank r owns block r of the c...
StageKind kind
which movement reaches dec