6#ifndef PECLET_CORE_DECOMP_BLOCK_DECOMPOSER_HPP
7#define PECLET_CORE_DECOMP_BLOCK_DECOMPOSER_HPP
61 for (
int i = 0; i < Dim; ++i)
85 cellExtent_ = cellExtent;
101 cellExtent_ = cellExtent;
111 assert(weights.size() ==
static_cast<std::size_t
>([&] {
113 for (int i = 0; i < Dim; ++i)
117 "weights array must cover the global grid (x-fastest)");
120 for (
int i = 0; i < Dim; ++i)
151 for (
int k = 0; k < Dim; ++k) {
152 assert(align[k] >= 1 &&
"aligned weighted init(): align must be >= 1");
154 "aligned weighted init(): globalSize must be a multiple of align");
161 std::vector<Real> wc(
static_cast<std::size_t
>(nc), 0.0);
165 coarse.globalSize_ = gc;
166 assert(weights.size() ==
static_cast<std::size_t
>([&] {
168 for (int i = 0; i < Dim; ++i)
172 "weights array must cover the global grid (x-fastest)");
176 for (
int k = 0; k < Dim; ++k) {
177 c[k] = g[k] / align[k];
178 if (g[k] % align[k] != 0)
181 const std::size_t ic =
static_cast<std::size_t
>(coarse.
linearGlobal(c));
182 const Real wv = weights[
static_cast<std::size_t
>(fineIndex.
linearGlobal(g))];
188 coarse.cellExtent_ = align;
194 std::size_t
numBlocks()
const {
return origins_.size(); }
198 const std::vector<IVec<Dim>>&
origins()
const {
return origins_; }
200 const std::vector<IVec<Dim>>&
sizes()
const {
return sizes_; }
208 while (tree_[node].splitDim != -1) {
209 node = (g[tree_[node].splitDim] < tree_[node].splitValue) ? 2 * node + 1 : 2 * node + 2;
211 return static_cast<int>(tree_[node].splitValue);
219 void flattenTree(std::vector<int>& splitDim, std::vector<Index>& splitVal)
const {
220 splitDim.resize(tree_.size());
221 splitVal.resize(tree_.size());
222 for (std::size_t i = 0; i < tree_.size(); ++i) {
223 splitDim[i] = tree_[i].splitDim;
224 splitVal[i] = tree_[i].splitValue;
230 Index idx = g[Dim - 1];
231 for (
int i = Dim - 2; i >= 0; --i) {
232 idx *= globalSize_[i];
241 for (
int i = 0; i < Dim; ++i) {
242 g[i] = lin % globalSize_[i];
243 lin /= globalSize_[i];
256 for (
int k = 0; k < Dim; ++k) {
257 assert(ratio[k] >= 1 && globalSize_[k] % ratio[k] == 0 &&
258 "coarsened(): global size not divisible by ratio (build the decomposition aligned)");
259 c.globalSize_[k] = globalSize_[k] / ratio[k];
260 c.align_[k] = align_[k] / ratio[k] >= 1 ? align_[k] / ratio[k] : 1;
263 for (
auto& nd : c.tree_)
264 if (nd.splitDim != -1) {
265 assert(nd.splitValue % ratio[nd.splitDim] == 0 &&
266 "coarsened(): split not divisible by ratio (build the decomposition aligned)");
267 nd.splitValue /= ratio[nd.splitDim];
269 c.origins_.resize(origins_.size());
270 c.sizes_.resize(sizes_.size());
271 for (std::size_t b = 0; b < origins_.size(); ++b)
272 for (
int k = 0; k < Dim; ++k) {
273 assert(origins_[b][k] % ratio[k] == 0 && sizes_[b][k] % ratio[k] == 0 &&
274 "coarsened(): block origin/size not divisible by ratio (build aligned)");
275 c.origins_[b][k] = origins_[b][k] / ratio[k];
276 c.sizes_[b][k] = sizes_[b][k] / ratio[k];
297 for (
int k = 0; k < Dim; ++k) {
298 assert(ratio[k] >= 1 &&
"refined(): ratio must be >= 1");
299 f.globalSize_[k] = globalSize_[k] * ratio[k];
300 f.align_[k] = align_[k] * ratio[k];
301 f.cellExtent_[k] = 1;
304 for (
auto& nd : f.tree_)
305 if (nd.splitDim != -1)
306 nd.splitValue *= ratio[nd.splitDim];
307 f.origins_.resize(origins_.size());
308 f.sizes_.resize(sizes_.size());
309 for (std::size_t b = 0; b < origins_.size(); ++b)
310 for (
int k = 0; k < Dim; ++k) {
311 f.origins_[b][k] = origins_[b][k] * ratio[k];
312 f.sizes_[b][k] = sizes_[b][k] * ratio[k];
320 Index splitValue = 0;
328 depthWalk(0, 0, best);
345 std::vector<int>* rootOf =
nullptr)
const {
346 assert(depth >= 0 &&
"agglomerated(): depth must be >= 0");
348 a.globalSize_ = globalSize_;
350 a.cellExtent_ = cellExtent_;
351 a.tree_.assign(tree_.size(), TreeNode{});
352 std::vector<int> gmap(origins_.size(), -1);
353 std::vector<int> roots;
354 agglomWalk(0, 0, depth,
IVec<Dim>{}, globalSize_, a, gmap, roots);
355 a.tree_.resize(prunedSize(a.tree_));
364 void depthWalk(
Index node,
int d,
int& best)
const {
365 if (node >=
static_cast<Index>(tree_.size()))
367 if (tree_[node].splitDim == -1) {
372 depthWalk(2 * node + 1, d + 1, best);
373 depthWalk(2 * node + 2, d + 1, best);
376 void leavesUnder(
Index node, std::vector<int>& out)
const {
377 if (tree_[node].splitDim == -1) {
378 out.push_back(
static_cast<int>(tree_[node].splitValue));
381 leavesUnder(2 * node + 1, out);
382 leavesUnder(2 * node + 2, out);
386 const TreeNode& nd = tree_[node];
387 if (nd.splitDim == -1 || d == depth) {
389 const int newIdx =
static_cast<int>(a.origins_.size());
390 a.tree_[node] = TreeNode{-1, newIdx};
391 a.origins_.push_back(origin);
392 a.sizes_.push_back(size);
393 std::vector<int> leaves;
394 leavesUnder(node, leaves);
395 int root = leaves.front();
396 for (
int b : leaves) {
401 roots.push_back(root);
405 IVec<Dim> lsize = size, rorigin = origin, rsize = size;
406 lsize[nd.splitDim] = nd.splitValue - origin[nd.splitDim];
407 rorigin[nd.splitDim] = nd.splitValue;
408 rsize[nd.splitDim] = origin[nd.splitDim] + size[nd.splitDim] - nd.splitValue;
409 agglomWalk(2 * node + 1, d + 1, depth, origin, lsize, a, gmap, roots);
410 agglomWalk(2 * node + 2, d + 1, depth, rorigin, rsize, a, gmap, roots);
412 static std::size_t prunedSize(
const std::vector<TreeNode>& t) {
415 std::size_t last = 0;
416 std::vector<Index> stack{0};
417 while (!stack.empty()) {
418 const Index n = stack.back();
420 if (n >=
static_cast<Index>(t.size()))
422 if (
static_cast<std::size_t
>(n) > last)
423 last =
static_cast<std::size_t
>(n);
424 if (t[n].splitDim != -1) {
425 stack.push_back(2 * n + 1);
426 stack.push_back(2 * n + 2);
437 for (
int i = 0; i < Dim; ++i)
446 std::size_t numTotal)
const {
447 Index c = splitPosition(origin, size, k, numSub, numTotal,
nullptr);
448 const Index a = align_[k];
449 if (a > 1 && size[k] >= 2 * a) {
450 c = ((c + a / 2) / a) * a;
460 int balancedAxis(
const IVec<Dim>& origin,
const IVec<Dim>& size,
int kL, std::size_t numSub,
461 std::size_t numTotal)
const {
462 const Index m =
static_cast<Index>(numTotal), n1 =
static_cast<Index>(numSub);
466 for (
int k = 0; k < Dim; ++k) {
470 cand[k] = size[k] >= 2 && (k == kL || !(align_[k] > 1 && size[k] < 2 * align_[k]));
473 const Index c = alignedCut(origin, size, k, numSub, numTotal);
474 const Index d = c * m - size[k] * n1;
475 dev[k] = d < 0 ? -d : d;
482 const Index lhs = dev[k] * size[best], rhs = dev[best] * size[k];
483 if (lhs < rhs || (lhs == rhs && size[k] * cellExtent_[k] > size[best] * cellExtent_[best]))
488 if (cand[kL] && dev[kL] * size[best] == dev[best] * size[kL])
502 std::size_t numSub, std::size_t numTotal,
503 const std::vector<Real>* weights)
const;
510 std::vector<IVec<Dim>> origins_;
511 std::vector<IVec<Dim>> sizes_;
512 std::vector<TreeNode> tree_;
516void BlockDecomposer<Dim>::initImpl(std::size_t numBlocks,
IVec<Dim> globalSize,
517 const std::vector<Real>* weights) {
518 globalSize_ = globalSize;
519 for (
int k = 0; k < Dim; ++k)
520 if (cellExtent_[k] <= 0)
532 std::stack<StackBlock> stack;
533 stack.push(StackBlock{
IVec<Dim>{}, globalSize, numBlocks, 0});
536 while (!stack.empty()) {
537 StackBlock cur = stack.top();
540 if (
static_cast<std::size_t
>(cur.treeIndx) >= tree_.size()) {
541 tree_.resize(cur.treeIndx + 1);
544 if (cur.numSub > 1) {
551 for (
int k = 1; k < Dim; ++k) {
552 if (cur.size[k] * cellExtent_[k] > cur.size[kLargest] * cellExtent_[kLargest])
555 std::size_t numSub = cur.numSub / 2;
557 kLargest = balancedAxis(cur.origin, cur.size, kLargest, numSub, cur.numSub);
558 Index szSub = splitPosition(cur.origin, cur.size, kLargest, numSub, cur.numSub, weights);
564 const Index a = align_[kLargest];
565 if (a > 1 && cur.size[kLargest] >= 2 * a) {
566 szSub = ((szSub + a / 2) / a) * a;
569 if (szSub > cur.size[kLargest] - a)
570 szSub = cur.size[kLargest] - a;
573 StackBlock left = cur;
574 left.numSub = numSub;
575 left.size[kLargest] = szSub;
576 left.treeIndx = 2 * cur.treeIndx + 1;
578 StackBlock right = cur;
579 right.origin[kLargest] += szSub;
580 right.size[kLargest] -= szSub;
581 right.numSub = cur.numSub - numSub;
582 right.treeIndx = 2 * cur.treeIndx + 2;
584 tree_[cur.treeIndx] = TreeNode{kLargest, cur.origin[kLargest] + szSub};
588 tree_[cur.treeIndx] = TreeNode{-1, leafIndex++};
589 origins_.push_back(cur.origin);
590 sizes_.push_back(cur.size);
597 int kLargest, std::size_t numSub, std::size_t numTotal,
598 const std::vector<Real>* weights)
const {
599 const Index n = size[kLargest];
603 if (weights ==
nullptr || n <= 1) {
604 return static_cast<Index>(std::round(
static_cast<double>(n) *
static_cast<double>(numSub) /
605 static_cast<double>(numTotal)));
610 std::vector<double> slab(
static_cast<std::size_t
>(n), 0.0);
612 for (
int i = 0; i < Dim; ++i)
613 end[i] = origin[i] + size[i];
615 slab[
static_cast<std::size_t
>(g[kLargest] - origin[kLargest])] += (*weights)[linearGlobal(g)];
619 for (
double w : slab)
621 const double target = total *
static_cast<double>(numSub) /
static_cast<double>(numTotal);
626 double bestDist = std::numeric_limits<double>::max();
628 for (
Index s = 1; s < n; ++s) {
629 cum += slab[
static_cast<std::size_t
>(s - 1)];
630 const double dist = std::abs(cum - target);
631 if (dist <= bestDist) {
648 double total = 0.0, hi = 0.0;
649 for (std::size_t b = 0; b < nb; ++b) {
651 for (
int k = 0; k < Dim; ++k)
652 end[k] = bgn[k] + dec.
sizes()[b][k];
655 w += weights[
static_cast<std::size_t
>(dec.
linearGlobal(g))];
658 if (b == 0 || w > hi)
661 if (!(total > 0.0) || nb == 0)
662 return std::numeric_limits<Real>::infinity();
663 return hi / (total /
static_cast<double>(nb));
692template <std::size_t N, int Dim = static_cast<int>(N)>
694 const std::array<Index, N>& G,
695 const std::vector<Real>& weights,
697 int aMax = std::numeric_limits<int>::max()) {
699 for (
int k = 0; k < Dim; ++k) {
701 for (
Index g = G[k]; g > 0 && g % 2 == 0; g /= 2)
703 cap = tz < cap ? tz : cap;
707 const int a = top + 1;
710 for (
int k = 0; k < Dim; ++k) {
711 const Index gk = G[k] >> a;
714 cells *=
static_cast<double>(gk);
716 if (!ok || cells <
static_cast<double>(numBlocks))
720 for (
int a = top; a >= 1; --a) {
722 for (
int k = 0; k < Dim; ++k)
723 align[k] =
Index(1) << a;
725 c.
dec.init(numBlocks, G, weights, align);
727 for (
const auto& sz : c.
dec.sizes())
728 for (
int k = 0; k < Dim; ++k)
740 c.
dec.init(numBlocks, G, weights);
Orthogonal recursive bisection of a Dim-dimensional global cell grid into numBlocks axis-aligned bloc...
void flattenTree(std::vector< int > &splitDim, std::vector< Index > &splitVal) const
Flatten the implicit ORB tree into two parallel arrays for a device-callable ownerOf: for node i,...
Block< Dim > block(std::size_t b) const
Block b as (origin, size). Precondition: b < numBlocks() (unchecked).
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).
void init(std::size_t numBlocks, IVec< Dim > globalSize, const IVec< Dim > &align, const IVec< Dim > &cellExtent)
Anisotropic-cell aligned ORB: cellExtent[k] is how many underlying FINE cells one cell of this grid s...
void init(std::size_t numBlocks, IVec< Dim > globalSize)
Build the decomposition of a globalSize cell grid into numBlocks blocks (equal cell count).
BlockDecomposer(std::size_t numBlocks, IVec< Dim > globalSize, const std::vector< Real > &weights)
Weighted ORB: see init(numBlocks, globalSize, weights).
const std::vector< IVec< Dim > > & origins() const
Inclusive lower corner of every block, in global cell coordinates, indexed by block.
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...
void init(std::size_t numBlocks, IVec< Dim > globalSize, const IVec< Dim > &align)
Aligned ORB: force every split position (and hence every block origin/size) on axis k to be a multipl...
void init(std::size_t numBlocks, IVec< Dim > globalSize, const std::vector< Real > &weights)
Weighted ORB: balance the total weight per block instead of the cell count.
std::size_t numBlocks() const
Number of blocks.
BlockDecomposer()=default
BlockDecomposer(std::size_t numBlocks, IVec< Dim > globalSize)
Equal-cell-count ORB: see init(numBlocks, globalSize).
Index linearGlobal(const IVec< Dim > &g) const
Global multi-index -> global linear index (x-fastest: I = x + y*nx + z*nx*ny).
BlockDecomposer< Dim > refined(const IVec< Dim > &ratio) const
Derive the NESTED fine decomposition: the exact inverse of coarsened() — global size,...
BlockDecomposer(std::size_t numBlocks, IVec< Dim > globalSize, const IVec< Dim > &align)
Aligned ORB (coarsenable): see the aligned init() below.
int ownerOf(const IVec< Dim > &g) const
Owning block index of a global cell coordinate. Caller must wrap into [0, globalSize) first.
BlockDecomposer< Dim > coarsened(const IVec< Dim > &ratio) const
Derive the NESTED coarse decomposition: each block, split value and the global size divided by ratio ...
IVec< Dim > multiGlobal(Index lin) const
Global linear index -> global multi-index (inverse of linearGlobal).
void init(std::size_t numBlocks, IVec< Dim > globalSize, const std::vector< Real > &weights, const IVec< Dim > &align)
Aligned weighted ORB — COARSE-FIRST, never snap-after (amr/docs/amr_mg_core_boundary....
void init(std::size_t numBlocks, IVec< Dim > globalSize, const IVec< Dim > &align, const IVec< Dim > &cellExtent, SplitAxis rule)
The aligned anisotropic-cell ORB above with an explicit split-axis rule (see SplitAxis).
Real weightImbalance(const BlockDecomposer< Dim > &dec, const std::vector< Real > &weights)
Weight imbalance of a decomposition — the balancer's own metric: the heaviest block's total weight ov...
AlignedWeightedChoice< Dim > chooseAlignedWeighted(std::size_t numBlocks, const std::array< Index, N > &G, const std::vector< Real > &weights, Real budget=1.05, int aMax=std::numeric_limits< int >::max())
Choose the alignment of a weighted ORB from an imbalance budget (amr/docs/ amr_mg_core_boundary....
SplitAxis
Split-axis rule of the unweighted ORB construction (flow doc/vof_distributed_partition_design....
double Real
Default host floating type. Device kernels may use float; conversions happen at the boundary.
void forEachInBox(const IVec< Dim > &bgn, const IVec< Dim > &end, Func &&func)
Iterate the box [bgn, end) calling func(const IVec<Dim>&).
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).
What chooseAlignedWeighted returns: the partition, the alignment exponent a (align = 2^a on every axi...
Real imbalance
weightImbalance(dec, weights)
BlockDecomposer< Dim > dec
the chosen partition
int a
alignment exponent: align = 2^a on every axis (0 = unaligned)
A rank-owned axis-aligned block of the global cell grid.
IVec< Dim > size
extent in cells along each axis
IVec< Dim > origin
inclusive lower corner in global cell coordinates