114 using M =
typename Octree::M;
115 using Code =
typename Octree::Code;
130 template <
class OpenFn>
131 void build(
const DO& finest,
double h0, OpenFn&& openFn,
133 Index bottomExtent = 4) {
138 template <
class OpenFn>
139 void build(
const DO& finest,
const Vec<Dim>& h0, OpenFn&& openFn,
141 Index bottomExtent = 4) {
142 buildImpl(finest, h0, shared0, liftRoot, bottomExtent);
146 levels_[0]->ap.buildOpenness(openFn);
147 for (std::size_t L = 0; L + 1 < levels_.size(); ++L)
148 coarsenOpennessTo(L);
159 void buildRaw(
const DO& finest,
const Vec<Dim>& h0,
const std::vector<double>& alphaLocal,
160 bool liftRoot =
true, Index bottomExtent = 4) {
161 buildImpl(finest, h0,
nullptr, liftRoot, bottomExtent);
162 setOpennessLocalRows(0, alphaLocal);
163 for (std::size_t L = 0; L + 1 < levels_.size(); ++L)
164 coarsenOpennessTo(L);
186 stage_->setRemoveMean(on);
189 std::size_t
numLevels()
const {
return levels_.size(); }
202 bool hasStage()
const {
return stage_ !=
nullptr; }
204 const char*
stageKind()
const {
return stage_ ? stage_->kind() :
"none"; }
210 Index
stageLeaves(std::size_t L = 0)
const {
return stage_->numLeaves(L); }
212 const IVec<Dim>&
stageFrom()
const {
return stageFrom_; }
223 return stage_->bottomName() + stage_->diagnosticSuffix();
224 return bottomAmg_ ? std::string(
"amg") : std::string(
"jacobi");
240 stage_->setBottom(
b);
244 Index
numLeaves(std::size_t L = 0)
const {
return levels_[L]->n; }
245 Index
extendedSize(std::size_t L = 0)
const {
return levels_[L]->nExt; }
246 View<double>
x(std::size_t L = 0) {
return levels_[L]->x; }
247 View<double>
b(std::size_t L = 0) {
return levels_[L]->b; }
248 const FvOp&
op(std::size_t L = 0)
const {
return levels_[L]->op; }
249 MPI_Comm
comm()
const {
return comm_; }
254 void sync(std::size_t L, View<double> v)
const { levels_[L]->ex.exchange(v); }
259 void vcycle(
int pre = 2,
int post = 2,
int bottom = 40,
double omega = 0.8, std::size_t L = 0) {
260 Level& lv = *levels_[L];
261 View<const double> bc(lv.b);
262 if (L + 1 == levels_.size()) {
270 residualFv(lv.op, View<const double>(lv.x), bc, lv.res);
271 stage_->apply(View<const double>(lv.res), lv.tmp, lv.n, pre, post, bottom, omega);
274 Kokkos::parallel_for(
275 "amr::stage_add", lv.n, KOKKOS_LAMBDA(
const Index i) {
x(i) += c(i); });
276 }
else if (bottomAmg_) {
277 bottomAmg_->solve(bc, lv.x);
279 for (
int s = 0; s < bottom; ++s) {
281 jacobiFv(lv.op, lv.x, bc, lv.tmp, omega);
288 for (
int s = 0; s < pre; ++s) {
290 jacobiFv(lv.op, lv.x, bc, lv.tmp, omega);
293 residualFv(lv.op, View<const double>(lv.x), bc, lv.res);
294 Level& cl = *levels_[L + 1];
295 restrictField(lv.childStart, lv.childIdx, View<const double>(lv.res), cl.b, cl.n);
296 Kokkos::deep_copy(cl.x, 0.0);
297 vcycle(pre, post, bottom, omega, L + 1);
298 prolongAdd(lv.c2p, View<const double>(cl.x), lv.x, lv.n);
299 for (
int s = 0; s < post; ++s) {
301 jacobiFv(lv.op, lv.x, bc, lv.tmp, omega);
315 Index n = 0, nExt = 0;
320 MPI_Comm
comm = MPI_COMM_NULL;
321 View<double> x, b, res, tmp;
322 View<Index> c2p, childStart, childIdx;
323 std::vector<Index> c2pHost;
344 Level& lt = *levels_.back();
345 stageFrom_ = lt.d.globalRootSize();
347 MPI_Comm_size(comm_, &size);
349 for (
int a = 0; a < Dim; ++a)
350 mx = std::max(mx, stageFrom_[a]);
359 const auto A = lt.ap.assembleFv();
360 bool singular =
true;
361 for (
double d : A.bcDiag)
366 bottomAmg_ = std::make_unique<AmgBottom<Dim, Bits>>();
367 bottomAmg_->build(lt.ap, singular);
370 if (resolved_.enabled && mx > bottomExtent_) {
371 namespace cd = core::decomp;
372 auto liftable = [](
const cd::BlockDecomposer<Dim>& dec) {
375 const cd::StageTarget<Dim> t = cd::chooseStageTarget(
376 lt.d.decomposition(), stageFrom_, liftable, resolved_.minExtent, resolved_.maxBlockCells);
377 if (t.kind == cd::StageKind::SiblingMerge || t.kind == cd::StageKind::Repartition) {
378 using Topo = cd::RedistributeTopology<Dim, double>;
380 static_cast<int>((levels_.size() - 1) %
static_cast<std::size_t
>(Topo::kTagSpan));
381 auto st = std::make_unique<DistributedStage<Dim, Bits>>();
382 st->build(lt.d, h0_, lt.ap, lt.n, t,
id, bottomExtent_, resolved_, bottomKind_,
384 stage_ = std::move(st);
388 auto tail = std::make_unique<ReplicatedTailStage<Dim, Bits>>();
389 tail->build(lt.d, h0_, lt.ap, lt.n, lt.nExt, bottomExtent_, lt.comm);
390 tail->setRemoveMean(removeMean_);
391 tail->setBottom(bottomKind_);
392 stage_ = std::move(tail);
398 void setOpennessLocalRows(std::size_t L,
const std::vector<double>& alphaLocal) {
399 Level& lv = *levels_[L];
400 const int F = 2 * Dim;
401 const Index nl = lv.n;
402 const Index ng = lv.hp->numGhosts();
403 if (
static_cast<Index
>(alphaLocal.size()) != nl * F)
404 throw std::invalid_argument(
405 "amr::DistributedFlowMultigrid::buildRaw: alpha must hold numLeaves()*2*Dim local rows");
406 std::vector<double> ca(
static_cast<std::size_t
>(nl + ng) * F, 1.0);
407 std::copy(alphaLocal.begin(), alphaLocal.end(), ca.begin());
408 std::vector<double> col(
static_cast<std::size_t
>(lv.nExt), 0.0);
409 for (
int fi = 0; fi < F; ++fi) {
410 for (Index i = 0; i < nl; ++i)
411 col[
static_cast<std::size_t
>(i)] = ca[
static_cast<std::size_t
>(i) * F + fi];
412 lv.hp->exchangeHost(col);
413 for (Index g = 0; g < ng; ++g)
414 ca[
static_cast<std::size_t
>(nl + g) * F + fi] = col[
static_cast<std::size_t
>(nl + g)];
416 lv.ap.setOpennessRaw(std::move(ca));
419 void buildImpl(
const DO& finest,
const Vec<Dim>& h0,
const LeafHalo<Dim, Bits>* shared0,
420 bool liftRoot, Index bottomExtent) {
421 comm_ = finest.comm();
423 bottomExtent_ = bottomExtent;
425 resolveStagePolicy(finest);
426 buildLadder(finest, liftRoot, bottomExtent);
430 const Vec<Dim> gorigin = finest.globalGeometry().origin;
431 std::array<long, Dim> shift{};
432 for (
int a = 0; a < Dim; ++a)
433 shift[a] = finest.blockFineOrigin()[a];
436 for (
auto& lvp : levels_) {
437 initLevelTopology(*lvp, first ? shared0 :
nullptr, gorigin, shift);
441 for (std::size_t L = 0; L + 1 < levels_.size(); ++L)
446 void resolveStagePolicy(
const DO& finest) {
447 resolved_ = stagePolicy_;
448 if (resolved_.maxBlockCells < 0) {
449 long long nl =
static_cast<long long>(finest.local().
numLeaves()), mx = 0;
450 MPI_Allreduce(&nl, &mx, 1, MPI_LONG_LONG, MPI_MAX, comm_);
451 resolved_.maxBlockCells =
static_cast<Index
>(mx);
457 void buildLadder(
const DO& finest,
bool liftRoot, Index bottomExtent) {
458 auto l0 = std::make_unique<Level>();
460 levels_.push_back(std::move(l0));
462 Octree c = levels_.back()->d.local();
463 const Index before = c.numLeaves();
464 const Index merged = c.coarsenIf([](
Code,
unsigned) {
return true; });
465 if (merged == 0 || c.numLeaves() == before)
467 auto lv = std::make_unique<Level>();
468 lv->d = levels_.back()->d;
469 lv->d.local() = std::move(c);
470 levels_.push_back(std::move(lv));
471 if (levels_.back()->d.local().numLeaves() == 1)
477 int nl =
static_cast<int>(levels_.size()), gnl = nl;
478 MPI_Allreduce(&nl, &gnl, 1, MPI_INT, MPI_MAX, comm_);
479 while (
static_cast<int>(levels_.size()) < gnl) {
480 auto lv = std::make_unique<Level>();
481 lv->d = levels_.back()->d;
482 levels_.push_back(std::move(lv));
491 liftDepth_ = allowedLiftDepth(levels_.back()->d, bottomExtent);
492 for (Index j = 0; j < liftDepth_; ++j) {
493 auto lv = std::make_unique<Level>();
494 lv->d = levels_.back()->d;
496 const Index before = lv->d.local().numLeaves();
497 const Index merged = lv->d.local().coarsenIf([](
Code,
unsigned) {
return true; });
498 if (merged == 0 || lv->d.local().numLeaves() == before)
500 levels_.push_back(std::move(lv));
505 int nlv =
static_cast<int>(levels_.size()), lo = 0, hi = 0;
506 MPI_Allreduce(&nlv, &lo, 1, MPI_INT, MPI_MIN, comm_);
507 MPI_Allreduce(&nlv, &hi, 1, MPI_INT, MPI_MAX, comm_);
509 throw std::runtime_error(
510 "amr::DistributedFlowMultigrid: the pressure ladder has different level counts on "
511 "different ranks (the lift is not lockstep)");
517 void initLevelTopology(Level& lv,
const LeafHalo<Dim, Bits>* shared0,
const Vec<Dim>& gorigin,
518 const std::array<long, Dim>& shift) {
520 lv.n = lv.d.local().numLeaves();
521 lv.ap.init(lv.d.local(), h0_);
522 lv.ap.setOrigin(gorigin);
523 lv.ap.setFrameShift(shift);
525 auto installGhosts = [&](
const LeafHalo<Dim, Bits>& h) {
526 std::vector<std::array<long, Dim>> glo(
static_cast<std::size_t
>(h.numGhosts()));
527 std::vector<unsigned> glv(
static_cast<std::size_t
>(h.numGhosts()));
528 for (Index g = 0; g < h.numGhosts(); ++g) {
529 for (
int a = 0; a < Dim; ++a)
530 glo[
static_cast<std::size_t
>(g)][a] =
static_cast<long>(h.ghostCoord(g)[a]) - shift[a];
531 glv[
static_cast<std::size_t
>(g)] =
static_cast<unsigned>(h.level(h.numLocal() + g));
533 lv.ap.setGhosts(std::move(glo), std::move(glv));
539 lv.ap.setResolver([shared0, shift](
const std::array<long, Dim>& p) -> Index {
540 std::array<long, Dim> g = p;
541 for (
int a = 0; a < Dim; ++a)
543 return shared0->lookupGlobal(g);
545 installGhosts(*shared0);
549 LeafHalo<Dim, Bits>* hp = &lv.halo;
550 lv.ap.setResolver([hp, shift](
const std::array<long, Dim>& p) -> Index {
551 std::array<long, Dim> g = p;
552 for (
int a = 0; a < Dim; ++a)
554 return hp->resolveGlobal(g);
561 installGhosts(lv.halo);
562 for (Index i = 0; i < lv.n; ++i)
563 lv.ap.forEachFaceNeighbor(i, [](Index, Real,
int,
double) {});
564 if (lv.halo.resolveMisses() == 0)
569 lv.nExt = lv.hp->extendedSize();
574 void buildTransfer(std::size_t L) {
575 const Octree& f = levels_[L]->d.local();
576 const Octree& c = levels_[L + 1]->d.local();
577 const Index nf = f.numLeaves(), nc = c.numLeaves();
578 std::vector<Index>& c2p = levels_[L]->c2pHost;
579 c2p.assign(
static_cast<std::size_t
>(nf), -1);
580 std::vector<Index> cnt(
static_cast<std::size_t
>(nc), 0);
581 for (Index i = 0; i < nf; ++i) {
582 const Index p = c.find(f.code(i));
583 c2p[
static_cast<std::size_t
>(i)] = p;
585 ++cnt[
static_cast<std::size_t
>(p)];
587 std::vector<Index> start(
static_cast<std::size_t
>(nc) + 1, 0);
588 for (Index p = 0; p < nc; ++p)
589 start[
static_cast<std::size_t
>(p) + 1] =
590 start[
static_cast<std::size_t
>(p)] + cnt[
static_cast<std::size_t
>(p)];
591 std::vector<Index> idx(
static_cast<std::size_t
>(start[
static_cast<std::size_t
>(nc)]));
592 std::vector<Index> cur(start.begin(), start.end() - 1);
593 for (Index i = 0; i < nf; ++i) {
594 const Index p = c2p[
static_cast<std::size_t
>(i)];
596 idx[
static_cast<std::size_t
>(cur[
static_cast<std::size_t
>(p)]++)] = i;
598 levels_[L]->c2p = toDevice(c2p,
"dfmg_c2p");
599 levels_[L]->childStart = toDevice(start,
"dfmg_cstart");
600 levels_[L]->childIdx = toDevice(idx,
"dfmg_cidx");
607 void coarsenOpennessTo(std::size_t L) {
608 const Octree& f = levels_[L]->d.local();
609 const Octree& c = levels_[L + 1]->d.local();
610 Poisson& capL = levels_[L]->ap;
611 const int F = 2 * Dim;
612 const Index nc = c.numLeaves();
613 const Index ngc = levels_[L + 1]->hp->numGhosts();
614 std::vector<double> ca(
static_cast<std::size_t
>(nc + ngc) * F, 0.0);
615 std::vector<int> cnt(
static_cast<std::size_t
>(nc) * F, 0);
616 const std::vector<Index>& c2p = levels_[L]->c2pHost;
617 for (Index i = 0; i < f.numLeaves(); ++i) {
618 const Index p = c2p[
static_cast<std::size_t
>(i)];
621 const std::size_t base =
static_cast<std::size_t
>(p) * F;
622 if (c.level(p) == f.level(i)) {
623 for (
int axis = 0; axis < Dim; ++axis)
624 for (
int dir = -1; dir <= 1; dir += 2) {
626 ca[base +
static_cast<std::size_t
>(fi)] += capL.faceOpenness(i, axis, dir);
627 cnt[base +
static_cast<std::size_t
>(fi)] += 1;
630 const unsigned oct = M::from_code(f.code(i)).child_index(f.level(i));
631 for (
int axis = 0; axis < Dim; ++axis) {
632 const int dir = ((oct >> axis) & 1) ? +1 : -1;
634 ca[base +
static_cast<std::size_t
>(fi)] += capL.faceOpenness(i, axis, dir);
635 cnt[base +
static_cast<std::size_t
>(fi)] += 1;
639 for (std::size_t k = 0; k < static_cast<std::size_t>(nc) * F; ++k)
640 ca[k] = cnt[k] ? ca[k] / cnt[k] : 1.0;
642 const LeafHalo<Dim, Bits>& h = *levels_[L + 1]->hp;
643 std::vector<double> col(
static_cast<std::size_t
>(levels_[L + 1]->nExt), 0.0);
644 for (
int fi = 0; fi < F; ++fi) {
645 for (Index i = 0; i < nc; ++i)
646 col[
static_cast<std::size_t
>(i)] = ca[
static_cast<std::size_t
>(i) * F + fi];
648 for (Index g = 0; g < ngc; ++g)
649 ca[
static_cast<std::size_t
>(nc + g) * F + fi] = col[
static_cast<std::size_t
>(nc + g)];
651 levels_[L + 1]->ap.setOpennessRaw(std::move(ca));
657 for (
auto& lvp : levels_) {
659 auto A = lv.ap.assembleFv();
661 lv.op.invVol = toDevice(A.invVol,
"dfmg_invvol");
662 lv.op.faceStart = toDevice(A.start,
"dfmg_start");
663 lv.op.faceNbr = toDevice(A.nbr,
"dfmg_nbr");
664 lv.op.faceW = toDevice(A.coef,
"dfmg_w");
665 lv.op.bcDiag = toDevice(A.bcDiag,
"dfmg_bc");
666 lv.x = View<double>(
"dfmg_x",
static_cast<std::size_t
>(lv.nExt));
667 lv.b = View<double>(
"dfmg_b",
static_cast<std::size_t
>(lv.nExt));
668 lv.res = View<double>(
"dfmg_res",
static_cast<std::size_t
>(lv.n));
669 lv.tmp = View<double>(
"dfmg_tmp",
static_cast<std::size_t
>(lv.n));
670 Kokkos::deep_copy(lv.x, 0.0);
671 Kokkos::deep_copy(lv.b, 0.0);
677 static Index allowedLiftDepth(
const DO& d, Index bottomExtent) {
678 IVec<Dim> G = d.globalRootSize(), o = d.blockOriginRoot(),
b = d.blockBrick();
682 for (
int a = 0; a < Dim; ++a)
683 mx = std::max(mx, G[a]);
684 if (mx <= bottomExtent)
687 for (
int a = 0; a < Dim; ++a)
688 if ((G[a] % 2) != 0 || (G[a] / 2) < 2 || (o[a] % 2) != 0 || (
b[a] % 2) != 0) {
694 for (
int a = 0; a < Dim; ++a) {
701 int kGlobal = kLocal;
702 MPI_Allreduce(&kLocal, &kGlobal, 1, MPI_INT, MPI_MIN, d.comm());
703 return static_cast<Index
>(kGlobal);
706 MPI_Comm comm_ = MPI_COMM_NULL;
708 Index bottomExtent_ = 4;
709 Index liftDepth_ = 0;
710 IVec<Dim> stageFrom_{};
712 std::unique_ptr<MgStage<Dim, Bits>> stage_;
715 std::unique_ptr<AmgBottom<Dim, Bits>> bottomAmg_;
716 std::array<long, Dim> shift_{};
717 std::vector<std::unique_ptr<Level>> levels_;
718 bool removeMean_ =
false;