core 1.8.1
Shared MPI block decomposition, asynchronous ghost-layer exchange, SDF geometry, load balancing (header-only C++20)
Loading...
Searching...
No Matches
block_decomposer.hpp
Go to the documentation of this file.
1// core — orthogonal recursive bisection (ORB) domain decomposition.
2//
3// Ported from block_decomposer/src/BlockDecomposer.hpp (pbs::BlockDecomposer), modernized into the
4// peclet::core namespace. The global cell grid is split recursively along its largest axis into
5// `numBlocks` rank-owned blocks. Adds ownerOf() (a tree walk) for halo topology construction.
6#ifndef PECLET_CORE_DECOMP_BLOCK_DECOMPOSER_HPP
7#define PECLET_CORE_DECOMP_BLOCK_DECOMPOSER_HPP
8
9#include <array>
10#include <cassert>
11#include <cmath>
12#include <cstddef>
13#include <limits>
14#include <stack>
15#include <vector>
16
18
20
22template <int Dim>
27
35enum class SplitAxis { Longest, Balanced };
36
41template <int Dim>
43 public:
44 BlockDecomposer() = default;
48 BlockDecomposer(std::size_t numBlocks, IVec<Dim> globalSize, const std::vector<Real>& weights) {
49 init(numBlocks, globalSize, weights);
50 }
51
53 init(numBlocks, globalSize, align);
54 }
55
58 void init(std::size_t numBlocks, IVec<Dim> globalSize) {
59 splitAxis_ = SplitAxis::Longest;
60 align_ = IVec<Dim>{};
61 for (int i = 0; i < Dim; ++i)
62 align_[i] = 1;
63 initImpl(numBlocks, globalSize, nullptr);
64 }
65
72 void init(std::size_t numBlocks, IVec<Dim> globalSize, const IVec<Dim>& align) {
73 initAligned(numBlocks, globalSize, align, SplitAxis::Longest);
74 }
75
83 void init(std::size_t numBlocks, IVec<Dim> globalSize, const IVec<Dim>& align,
84 const IVec<Dim>& cellExtent) {
85 cellExtent_ = cellExtent;
86 init(numBlocks, globalSize, align);
87 }
88
99 void init(std::size_t numBlocks, IVec<Dim> globalSize, const IVec<Dim>& align,
100 const IVec<Dim>& cellExtent, SplitAxis rule) {
101 cellExtent_ = cellExtent;
102 initAligned(numBlocks, globalSize, align, rule);
103 }
104
110 void init(std::size_t numBlocks, IVec<Dim> globalSize, const std::vector<Real>& weights) {
111 assert(weights.size() == static_cast<std::size_t>([&] {
112 Index v = 1;
113 for (int i = 0; i < Dim; ++i)
114 v *= globalSize[i];
115 return v;
116 }()) &&
117 "weights array must cover the global grid (x-fastest)");
118 splitAxis_ = SplitAxis::Longest;
119 align_ = IVec<Dim>{};
120 for (int i = 0; i < Dim; ++i)
121 align_[i] = 1;
122 initImpl(numBlocks, globalSize, &weights);
123 }
124
147 void init(std::size_t numBlocks, IVec<Dim> globalSize, const std::vector<Real>& weights,
148 const IVec<Dim>& align) {
149 IVec<Dim> gc{};
150 Index nc = 1;
151 for (int k = 0; k < Dim; ++k) {
152 assert(align[k] >= 1 && "aligned weighted init(): align must be >= 1");
153 assert(globalSize[k] % align[k] == 0 &&
154 "aligned weighted init(): globalSize must be a multiple of align");
155 gc[k] = globalSize[k] / align[k];
156 nc *= gc[k];
157 }
158 // The coarse weight field. The fine grid is walked x-fastest, so every coarse cell receives
159 // its align-box in the box's own x-fastest order; the first cell of a box ASSIGNS (a one-term
160 // sum is then a bitwise copy), the rest accumulate. Deterministic, hence replicated.
161 std::vector<Real> wc(static_cast<std::size_t>(nc), 0.0);
162 BlockDecomposer<Dim> fineIndex; // x-fastest indexing on the fine grid only
163 fineIndex.globalSize_ = globalSize;
165 coarse.globalSize_ = gc;
166 assert(weights.size() == static_cast<std::size_t>([&] {
167 Index v = 1;
168 for (int i = 0; i < Dim; ++i)
169 v *= globalSize[i];
170 return v;
171 }()) &&
172 "weights array must cover the global grid (x-fastest)");
174 IVec<Dim> c{};
175 bool first = true;
176 for (int k = 0; k < Dim; ++k) {
177 c[k] = g[k] / align[k];
178 if (g[k] % align[k] != 0)
179 first = false;
180 }
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))];
183 if (first)
184 wc[ic] = wv;
185 else
186 wc[ic] += wv;
187 });
188 coarse.cellExtent_ = align;
189 coarse.init(numBlocks, gc, wc); // the existing weighted ORB (sets align_ = 1)
190 *this = coarse.refined(align); // the existing exact inverse of coarsened(); align_ = align
191 }
192
194 std::size_t numBlocks() const { return origins_.size(); }
196 const IVec<Dim>& globalSize() const { return globalSize_; }
198 const std::vector<IVec<Dim>>& origins() const { return origins_; }
200 const std::vector<IVec<Dim>>& sizes() const { return sizes_; }
201
203 Block<Dim> block(std::size_t b) const { return {origins_[b], sizes_[b]}; }
204
206 int ownerOf(const IVec<Dim>& g) const {
207 Index node = 0;
208 while (tree_[node].splitDim != -1) {
209 node = (g[tree_[node].splitDim] < tree_[node].splitValue) ? 2 * node + 1 : 2 * node + 2;
210 }
211 return static_cast<int>(tree_[node].splitValue);
212 }
213
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;
225 }
226 }
227
229 Index linearGlobal(const IVec<Dim>& g) const {
230 Index idx = g[Dim - 1];
231 for (int i = Dim - 2; i >= 0; --i) {
232 idx *= globalSize_[i];
233 idx += g[i];
234 }
235 return idx;
236 }
237
240 IVec<Dim> g{};
241 for (int i = 0; i < Dim; ++i) {
242 g[i] = lin % globalSize_[i];
243 lin /= globalSize_[i];
244 }
245 return g;
246 }
247
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;
261 }
262 c.tree_ = tree_;
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];
268 }
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];
277 }
278 return c;
279 }
280
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; // the refined grid's cells are the fine cells
302 }
303 f.tree_ = tree_;
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];
313 }
314 return f;
315 }
316
317 private:
318 struct TreeNode {
319 int splitDim = -1;
320 Index splitValue = 0;
321 };
322
323 public:
326 int treeDepth() const {
327 int best = 0;
328 depthWalk(0, 0, best);
329 return best;
330 }
331
344 BlockDecomposer<Dim> agglomerated(int depth, std::vector<int>* groupOf = nullptr,
345 std::vector<int>* rootOf = nullptr) const {
346 assert(depth >= 0 && "agglomerated(): depth must be >= 0");
348 a.globalSize_ = globalSize_;
349 a.align_ = align_;
350 a.cellExtent_ = cellExtent_;
351 a.tree_.assign(tree_.size(), TreeNode{}); // everything a leaf until visited
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_));
356 if (groupOf)
357 *groupOf = gmap;
358 if (rootOf)
359 *rootOf = roots;
360 return a;
361 }
362
363 private:
364 void depthWalk(Index node, int d, int& best) const {
365 if (node >= static_cast<Index>(tree_.size()))
366 return;
367 if (tree_[node].splitDim == -1) {
368 if (d > best)
369 best = d;
370 return;
371 }
372 depthWalk(2 * node + 1, d + 1, best);
373 depthWalk(2 * node + 2, d + 1, best);
374 }
375 // Collect the (in-order) original leaf indices under `node`.
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));
379 return;
380 }
381 leavesUnder(2 * node + 1, out);
382 leavesUnder(2 * node + 2, out);
383 }
384 void agglomWalk(Index node, int d, int depth, IVec<Dim> origin, IVec<Dim> size,
385 BlockDecomposer<Dim>& a, std::vector<int>& gmap, std::vector<int>& roots) const {
386 const TreeNode& nd = tree_[node];
387 if (nd.splitDim == -1 || d == depth) {
388 // becomes a block of the agglomerated decomposition
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) {
397 gmap[b] = newIdx;
398 if (b < root)
399 root = b;
400 }
401 roots.push_back(root);
402 return;
403 }
404 a.tree_[node] = nd; // kept internal node: same split
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);
411 }
412 static std::size_t prunedSize(const std::vector<TreeNode>& t) {
413 // keep up to the last node that is reachable (an internal node or a numbered leaf); leaves
414 // default to {-1, 0} which is indistinguishable from block 0, so keep by reachability instead
415 std::size_t last = 0;
416 std::vector<Index> stack{0};
417 while (!stack.empty()) {
418 const Index n = stack.back();
419 stack.pop_back();
420 if (n >= static_cast<Index>(t.size()))
421 continue;
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);
427 }
428 }
429 return last + 1;
430 }
431
432 private:
433 void initAligned(std::size_t numBlocks, IVec<Dim> globalSize, const IVec<Dim>& align,
434 SplitAxis rule) {
435 splitAxis_ = rule;
436 align_ = align;
437 for (int i = 0; i < Dim; ++i)
438 if (align_[i] < 1)
439 align_[i] = 1;
440 initImpl(numBlocks, globalSize, nullptr);
441 }
442
445 Index alignedCut(const IVec<Dim>& origin, const IVec<Dim>& size, int k, std::size_t numSub,
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;
451 if (c < a)
452 c = a;
453 if (c > size[k] - a)
454 c = size[k] - a;
455 }
456 return c;
457 }
458
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);
463 Index dev[Dim];
464 bool cand[Dim];
465 int best = -1;
466 for (int k = 0; k < Dim; ++k) {
467 // an aligned axis too short to cut aligned is excluded from the ALTERNATIVES (an unaligned
468 // cut there would cost nesting depth); kL itself is always measured, so an exact Longest cut
469 // is always kept (the identical-tree property)
470 cand[k] = size[k] >= 2 && (k == kL || !(align_[k] > 1 && size[k] < 2 * align_[k]));
471 if (!cand[k])
472 continue;
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;
476 // relative deviation dev[k] / (size[k] m): k beats best iff dev[k] size[best] <
477 // dev[best] size[k]; ties to the larger physical extent, then the lower index
478 if (best < 0) {
479 best = k;
480 continue;
481 }
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]))
484 best = k;
485 }
486 if (best < 0)
487 return kL;
488 if (cand[kL] && dev[kL] * size[best] == dev[best] * size[kL])
489 return kL; // the longest axis is among the least deviating: keep it
490 return best;
491 }
492
495 void initImpl(std::size_t numBlocks, IVec<Dim> globalSize, const std::vector<Real>* weights);
496
501 Index splitPosition(const IVec<Dim>& origin, const IVec<Dim>& size, int kLargest,
502 std::size_t numSub, std::size_t numTotal,
503 const std::vector<Real>* weights) const;
504
505 IVec<Dim> globalSize_{};
507 align_{};
508 IVec<Dim> cellExtent_{};
509 SplitAxis splitAxis_ = SplitAxis::Longest;
510 std::vector<IVec<Dim>> origins_;
511 std::vector<IVec<Dim>> sizes_;
512 std::vector<TreeNode> tree_;
513};
514
515template <int Dim>
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)
521 cellExtent_[k] = 1;
522 origins_.clear();
523 sizes_.clear();
524 tree_.clear();
525
526 struct StackBlock {
527 IVec<Dim> origin;
528 IVec<Dim> size;
529 std::size_t numSub;
530 Index treeIndx;
531 };
532 std::stack<StackBlock> stack;
533 stack.push(StackBlock{IVec<Dim>{}, globalSize, numBlocks, 0});
534
535 Index leafIndex = 0;
536 while (!stack.empty()) {
537 StackBlock cur = stack.top();
538 stack.pop();
539
540 if (static_cast<std::size_t>(cur.treeIndx) >= tree_.size()) {
541 tree_.resize(cur.treeIndx + 1);
542 }
543
544 if (cur.numSub > 1) {
545 // Split along the largest axis. The split position balances either the sub-block cell count
546 // (unweighted) or the cumulative weight (weighted) of the two children.
547 // Compare PHYSICAL extents: size*cellExtent. cellExtent is all-ones for an ordinary grid, so
548 // this is the plain largest-cell-count test unless an anisotropically coarsened grid says
549 // otherwise (see the cellExtent init).
550 int kLargest = 0;
551 for (int k = 1; k < Dim; ++k) {
552 if (cur.size[k] * cellExtent_[k] > cur.size[kLargest] * cellExtent_[kLargest])
553 kLargest = k;
554 }
555 std::size_t numSub = cur.numSub / 2;
556 if (splitAxis_ == SplitAxis::Balanced && weights == nullptr)
557 kLargest = balancedAxis(cur.origin, cur.size, kLargest, numSub, cur.numSub);
558 Index szSub = splitPosition(cur.origin, cur.size, kLargest, numSub, cur.numSub, weights);
559
560 // Snap the split to a multiple of align_[kLargest] so the global split position
561 // (cur.origin[kLargest] + szSub, with cur.origin already aligned) is a multiple too — the
562 // precondition for coarsened() to divide cleanly. Skip if the box is too small to split
563 // aligned. (alignedCut is the same expression, for Balanced's candidates.)
564 const Index a = align_[kLargest];
565 if (a > 1 && cur.size[kLargest] >= 2 * a) {
566 szSub = ((szSub + a / 2) / a) * a;
567 if (szSub < a)
568 szSub = a;
569 if (szSub > cur.size[kLargest] - a)
570 szSub = cur.size[kLargest] - a;
571 }
572
573 StackBlock left = cur;
574 left.numSub = numSub;
575 left.size[kLargest] = szSub;
576 left.treeIndx = 2 * cur.treeIndx + 1;
577
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;
583
584 tree_[cur.treeIndx] = TreeNode{kLargest, cur.origin[kLargest] + szSub};
585 stack.push(right);
586 stack.push(left);
587 } else {
588 tree_[cur.treeIndx] = TreeNode{-1, leafIndex++};
589 origins_.push_back(cur.origin);
590 sizes_.push_back(cur.size);
591 }
592 }
593}
594
595template <int Dim>
596Index BlockDecomposer<Dim>::splitPosition(const IVec<Dim>& origin, const IVec<Dim>& 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];
600
601 // Unweighted (and the degenerate non-splittable axis): the classic proportional-to-count split.
602 // Kept as the exact same expression so the unweighted API is byte-for-byte unchanged.
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)));
606 }
607
608 // Weighted: accumulate the weight of each slab (fixed kLargest-coordinate) within the box, then
609 // pick the boundary whose cumulative weight is closest to the target fraction of the total.
610 std::vector<double> slab(static_cast<std::size_t>(n), 0.0);
611 IVec<Dim> bgn = origin, end{};
612 for (int i = 0; i < Dim; ++i)
613 end[i] = origin[i] + size[i];
614 forEachInBox<Dim>(bgn, end, [&](const IVec<Dim>& g) {
615 slab[static_cast<std::size_t>(g[kLargest] - origin[kLargest])] += (*weights)[linearGlobal(g)];
616 });
617
618 double total = 0.0;
619 for (double w : slab)
620 total += w;
621 const double target = total * static_cast<double>(numSub) / static_cast<double>(numTotal);
622
623 // Search boundaries in [1, n-1] (non-empty children). Ties resolve to the larger boundary, which
624 // mirrors std::round's half-away-from-zero rule so equal weights reproduce the unweighted split.
625 double cum = 0.0;
626 double bestDist = std::numeric_limits<double>::max();
627 Index best = 1;
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) {
632 bestDist = dist;
633 best = s;
634 }
635 }
636 return best;
637}
638
645template <int Dim>
646Real weightImbalance(const BlockDecomposer<Dim>& dec, const std::vector<Real>& weights) {
647 const std::size_t nb = dec.numBlocks();
648 double total = 0.0, hi = 0.0;
649 for (std::size_t b = 0; b < nb; ++b) {
650 IVec<Dim> bgn = dec.origins()[b], end{};
651 for (int k = 0; k < Dim; ++k)
652 end[k] = bgn[k] + dec.sizes()[b][k];
653 double w = 0.0;
654 forEachInBox<Dim>(bgn, end, [&](const IVec<Dim>& g) {
655 w += weights[static_cast<std::size_t>(dec.linearGlobal(g))];
656 });
657 total += w;
658 if (b == 0 || w > hi)
659 hi = w;
660 }
661 if (!(total > 0.0) || nb == 0)
662 return std::numeric_limits<Real>::infinity();
663 return hi / (total / static_cast<double>(nb));
664}
665
668template <int Dim>
674
692template <std::size_t N, int Dim = static_cast<int>(N)>
694 const std::array<Index, N>& G,
695 const std::vector<Real>& weights,
696 Real budget = 1.05,
697 int aMax = std::numeric_limits<int>::max()) {
698 int cap = aMax;
699 for (int k = 0; k < Dim; ++k) {
700 int tz = 0;
701 for (Index g = G[k]; g > 0 && g % 2 == 0; g /= 2)
702 ++tz;
703 cap = tz < cap ? tz : cap;
704 }
705 int top = 0; // the largest a <= cap whose coarse grid still holds numBlocks, >= 2 cells per axis
706 while (top < cap) {
707 const int a = top + 1;
708 bool ok = true;
709 double cells = 1.0;
710 for (int k = 0; k < Dim; ++k) {
711 const Index gk = G[k] >> a;
712 if (gk < 2)
713 ok = false;
714 cells *= static_cast<double>(gk);
715 }
716 if (!ok || cells < static_cast<double>(numBlocks))
717 break;
718 top = a;
719 }
720 for (int a = top; a >= 1; --a) {
721 IVec<Dim> align{};
722 for (int k = 0; k < Dim; ++k)
723 align[k] = Index(1) << a;
725 c.dec.init(numBlocks, G, weights, align);
726 bool empty = false;
727 for (const auto& sz : c.dec.sizes())
728 for (int k = 0; k < Dim; ++k)
729 if (sz[k] <= 0)
730 empty = true;
731 if (empty)
732 continue;
733 c.imbalance = weightImbalance(c.dec, weights);
734 if (c.imbalance <= budget) {
735 c.a = a;
736 return c;
737 }
738 }
740 c.dec.init(numBlocks, G, weights);
741 c.a = 0;
742 c.imbalance = weightImbalance(c.dec, weights);
743 return c;
744}
745
746} // namespace peclet::core::decomp
747
748#endif // PECLET_CORE_DECOMP_BLOCK_DECOMPOSER_HPP
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(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.
Definition types.hpp:18
void forEachInBox(const IVec< Dim > &bgn, const IVec< Dim > &end, Func &&func)
Iterate the box [bgn, end) calling func(const IVec<Dim>&).
Definition types.hpp:65
std::int64_t Index
Signed index type for grids and particles (supersedes block_decomposer's long int IndxT).
Definition types.hpp:15
std::array< Index, Dim > IVec
Multi-dimensional integer index / coordinate (x-fastest order by convention).
Definition types.hpp:22
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