14#ifndef PECLET_FLOW_MAC_CUTCELL_MG_HPP
15#define PECLET_FLOW_MAC_CUTCELL_MG_HPP
24#include <Kokkos_Core.hpp>
36#include "peclet/core/solver/graph_amg.hpp"
47#include "peclet/core/decomp/block_decomposer.hpp"
48#include "peclet/core/decomp/grid_redistribute.hpp"
49#include "peclet/core/decomp/redistribute_topology.hpp"
50#include "peclet/core/decomp/stage_comm.hpp"
51#include "peclet/core/decomp/stage_target.hpp"
52#include "peclet/core/halo/grid_halo.hpp"
53#include "peclet/core/halo/grid_halo_topology.hpp"
60using peclet::core::halo::GridHalo;
61using peclet::core::halo::GridHaloTopology;
79#ifdef PECLET_FLOW_OPERATOR_DOUBLE
84using FPV = Kokkos::View<MReal*, CCMem>;
85using FPC = Kokkos::View<const MReal*, CCMem>;
93template <
class CV,
class FV>
95 const FV& oxf,
const FV& oyf,
const FV& ozf,
C3 cext,
96 C3 fext,
int gc,
int gf,
C3 ratio,
int icx,
int icy,
98 const int rx = ratio.x, ry = ratio.y, rz = ratio.z;
99 const int fx0 = rx * icx + gf, fy0 = ry * icy + gf, fz0 = rz * icz + gf;
100 const long fsy = fext.
x, fsz = (long)fext.
x * fext.
y;
101 auto F = [&](
const FV& T,
int x,
int y,
int z) {
102 return T((
long)x + (
long)y * fsy + (
long)z * fsz);
104 double sx = 0, sy = 0, sz = 0;
105 for (
int a = 0; a < ry; ++a)
106 for (
int b = 0; b < rz; ++b)
107 sx +=
F(oxf, fx0, fy0 + a, fz0 + b);
108 for (
int a = 0; a < rx; ++a)
109 for (
int b = 0; b < rz; ++b)
110 sy +=
F(oyf, fx0 + a, fy0, fz0 + b);
111 for (
int a = 0; a < rx; ++a)
112 for (
int b = 0; b < ry; ++b)
113 sz +=
F(ozf, fx0 + a, fy0 + b, fz0);
115 (long)(icx + gc) + (long)(icy + gc) * cext.
x + (long)(icz + gc) * (long)cext.
x * cext.
y;
116 oxc(ci) = sx / (double)(ry * rz);
117 oyc(ci) = sy / (double)(rx * rz);
118 ozc(ci) = sz / (double)(rx * ry);
123 "peclet::flow::coarsen_open",
C3{0, 0, 0},
C3{cinner.
x, cinner.
y, cinner.
z},
124 KOKKOS_LAMBDA(
int icx,
int icy,
int icz) {
125 coarsenOpenAvgCell(oxc, oyc, ozc, oxf, oyf, ozf, cext, fext, gc, gf, ratio, icx, icy, icz);
148 int dims[3] = {e.x, e.y, e.z};
149 long st[3] = {1, (long)e.x, (
long)e.x * e.y};
150 const int b = (a + 1) % 3, c = (a + 2) % 3;
151 const long sa = st[a], sb = st[b], sc = st[c];
152 const int bf = (s == 0) ? g : (dims[a] - g);
153 const int db = dims[b];
155 Kokkos::parallel_for(
156 "peclet::flow::mg_save_face_plane", MD(space, {0, 0}, {dims[b], dims[c]}),
157 KOKKOS_LAMBDA(
int p0,
int p1) {
158 dst((
long)p0 + (
long)p1 * db) = src((
long)p0 * sb + (
long)p1 * sc + (
long)bf * sa);
163 int dims[3] = {e.x, e.y, e.z};
164 long st[3] = {1, (long)e.x, (
long)e.x * e.y};
165 const int b = (a + 1) % 3, c = (a + 2) % 3;
166 const long sa = st[a], sb = st[b], sc = st[c];
167 const int bf = (s == 0) ? g : (dims[a] - g);
168 const int db = dims[b];
170 Kokkos::parallel_for(
171 "peclet::flow::mg_restore_face_plane", MD(space, {0, 0}, {dims[b], dims[c]}),
172 KOKKOS_LAMBDA(
int p0,
int p1) {
173 dst((
long)p0 * sb + (
long)p1 * sc + (
long)bf * sa) = src((
long)p0 + (
long)p1 * db);
184 C3 ratio,
int a,
int s) {
186 int cd[3] = {cext.
x, cext.
y, cext.
z}, fd[3] = {fext.
x, fext.
y, fext.
z};
187 int ci[3] = {cinner.
x, cinner.
y, cinner.
z}, rt[3] = {ratio.x, ratio.y, ratio.z};
188 long cst[3] = {1, (long)cext.
x, (
long)cext.
x * cext.
y};
189 long fst[3] = {1, (long)fext.
x, (
long)fext.
x * fext.
y};
190 const int b = (a + 1) % 3, c = (a + 2) % 3;
191 const long csa = cst[a], csb = cst[b], csc = cst[c];
192 const long fsa = fst[a], fsb = fst[b], fsc = fst[c];
193 const int cbf = (s == 0) ? gc : (cd[a] - gc);
194 const int fbf = (s == 0) ? gf : (fd[a] - gf);
195 const int rb = rt[b], rc = rt[c];
196 const int fdb = fd[b], fdc = fd[c];
197 const double inv = 1.0 / (double)(rb * rc);
199 Kokkos::parallel_for(
200 "peclet::flow::mg_coarsen_face_plane", MD(space, {0, 0}, {cd[b], cd[c]}),
201 KOKKOS_LAMBDA(
int p0,
int p1) {
202 const int ib = p0 - gc, ic = p1 - gc;
204 for (
int q0 = 0; q0 < rb; ++q0)
205 for (
int q1 = 0; q1 < rc; ++q1) {
206 int fb = rb * ib + gf + q0, fc = rc * ic + gf + q1;
207 fb =
fb < 0 ? 0 : (
fb >= fdb ? fdb - 1 :
fb);
208 fc = fc < 0 ? 0 : (fc >= fdc ? fdc - 1 : fc);
209 sum += of((
long)
fb * fsb + (
long)fc * fsc + (
long)fbf * fsa);
211 oc((
long)p0 * csb + (
long)p1 * csc + (
long)cbf * csa) = sum * inv;
217template <
class RV,
class XV,
class BV,
class OpV>
220 const OpV& AS,
const OpV& AN,
const OpV& AB,
221 const OpV& AT,
long i,
long xp,
long xm,
long yp,
222 long ym,
long zp,
long zm) {
223 const double Ax = (double)
AC(i) * x(i) + (double)AE(i) * x(xp) + (double)AW(i) * x(xm) +
224 (double)AN(i) * x(yp) + (double)AS(i) * x(ym) + (double)AT(i) * x(zp) +
225 (double)AB(i) * x(zm);
231 "peclet::flow::cc_residual",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
232 KOKKOS_LAMBDA(
int lx,
int ly,
int lz) {
233 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
234 const long i = (long)lx + (
long)ly * sy + (long)lz * sz;
235 cutcellResidualCell(r, x, b,
AC, AW, AE, AS, AN, AB, AT, i, i + sx, i - sx, i + sy, i - sy,
244 if (rhi.
x <= rlo.
x || rhi.
y <= rlo.
y || rhi.
z <= rlo.
z)
247 "peclet::flow::cc_residual_box", rlo, rhi, KOKKOS_LAMBDA(
int lx,
int ly,
int lz) {
248 if (lx >= slo.
x && lx < shi.x && ly >= slo.
y && ly < shi.y && lz >= slo.
z && lz < shi.
z)
250 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
251 const long i = (long)lx + (
long)ly * sy + (long)lz * sz;
252 cutcellResidualCell(r, x, b,
AC, AW, AE, AS, AN, AB, AT, i, i + sx, i - sx, i + sy, i - sy,
261 "peclet::flow::cc_residual",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
262 KOKKOS_LAMBDA(
int lx,
int ly,
int lz) {
263 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
264 const long i = (long)lx + (
long)ly * sy + (long)lz * sz;
265 cutcellResidualFaceCell(r, x, b,
AC, AFX, AFY, AFZ, i, sx, sy, sz, i + sx, i - sx, i + sy,
266 i - sy, i + sz, i - sz);
270 FPC AFZ,
C3 e,
C3 rlo,
C3 rhi,
C3 slo,
C3 shi,
int wrapMask = 0,
272 if (rhi.
x <= rlo.
x || rhi.
y <= rlo.
y || rhi.
z <= rlo.
z)
275 const C3 n{e.x - 2 * g, e.y - 2 * g, e.z - 2 * g};
276 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>) {
277 auto run = [=](
int ly,
int lz,
int x0,
int x1) {
278 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
279 ccMaskRow(ly, lz, x0, x1, 1, e, n, g, wrapMask,
280 [&](
long i,
long xp,
long xm,
long yp,
long ym,
long zp,
long zm) {
281 cutcellResidualFaceCell(r, x, b,
AC, AFX, AFY, AFZ, i, sx, sy, sz, xp, xm, yp,
289 "peclet::flow::cc_residual_box", rlo, rhi, KOKKOS_LAMBDA(
int lx,
int ly,
int lz) {
290 if (lx >= slo.
x && lx < shi.x && ly >= slo.
y && ly < shi.y && lz >= slo.
z && lz < shi.
z)
292 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
293 const long i = (long)lx + (
long)ly * sy + (long)lz * sz;
295 cutcellResidualFaceCell(r, x, b,
AC, AFX, AFY, AFZ, i, sx, sy, sz, w.xp, w.xm, w.yp, w.ym,
300 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>) {
303 auto run = [=](
int ly,
int lz,
int x0,
int x1) {
304 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
306 for (
int lx = x0; lx < x1; ++lx) {
307 const long i = (long)lx + (
long)ly * sy + (long)lz * sz;
308 cutcellResidualFaceCell(r, x, b,
AC, AFX, AFY, AFZ, i, sx, sy, sz, i + sx, i - sx, i + sy,
309 i - sy, i + sz, i - sz);
316 "peclet::flow::cc_residual_box", rlo, rhi, KOKKOS_LAMBDA(
int lx,
int ly,
int lz) {
317 if (lx >= slo.
x && lx < shi.x && ly >= slo.
y && ly < shi.y && lz >= slo.
z && lz < shi.
z)
319 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
320 const long i = (long)lx + (
long)ly * sy + (long)lz * sz;
321 cutcellResidualFaceCell(r, x, b,
AC, AFX, AFY, AFZ, i, sx, sy, sz, i + sx, i - sx, i + sy,
322 i - sy, i + sz, i - sz);
344 w.xm = (lx == g) ? i + (
long)(n.x - 1) : i - 1;
345 w.xp = (lx == g + n.x - 1) ? i - (
long)(n.x - 1) : i + 1;
346 w.ym = (ly == g) ? i + (
long)(n.y - 1) * sy : i - sy;
347 w.yp = (ly == g + n.y - 1) ? i - (
long)(n.y - 1) * sy : i + sy;
348 w.zm = (lz == g) ? i + (
long)(n.z - 1) * sz : i - sz;
349 w.zp = (lz == g + n.z - 1) ? i - (
long)(n.z - 1) * sz : i + sz;
361 const long sy = e.x, sz = (long)e.x * e.y;
362 const long base = (long)ly * sy + (
long)lz * sz;
363 const long dym = (ly == g) ? (
long)(n.y - 1) * sy : -sy;
364 const long dyp = (ly == g + n.y - 1) ? -(
long)(n.y - 1) * sy : sy;
365 const long dzm = (lz == g) ? (
long)(n.z - 1) * sz : -sz;
366 const long dzp = (lz == g + n.z - 1) ? -(
long)(n.z - 1) * sz : sz;
367 const int lxLast = g + n.x - 1;
370 const int last = lx0 + ((lxLast - lx0) / step) * step;
371 auto scalar = [&](
int lx) {
372 const long i = (long)lx + base;
374 cell(i, w.xp, w.xm, w.yp, w.ym, w.zp, w.zm);
376 int lo = lx0, hi = last;
381 const bool peelLast = hi == lxLast && hi >= lo;
385 for (
int lx = lo; lx <= hi; lx += step) {
386 const long i = (long)lx + base;
387 cell(i, i + 1, i - 1, i + dyp, i + dym, i + dzp, i + dzm);
397 const int nyi = e.y - 2 * g, nzi = e.z - 2 * g;
398 const long rows = (long)nyi * nzi;
399 auto row = [=](
long t) {
400 ccWrapRowPeeled(g + (
int)(t % nyi), g + (
int)(t / nyi), e, n, g, g, 1, cell);
404 for (
long t = 0; t < rows; ++t)
408 Kokkos::parallel_for(name, Kokkos::RangePolicy<CCExec>(space, 0, rows), row);
412 C3 e,
C3 n,
C3 og,
int g,
int color) {
414 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>) {
415 const int nyi = e.y - 2 * g, nzi = e.z - 2 * g;
416 const long cells = (long)nyi * nzi * (e.x - 2 * g);
417 auto pencil = KOKKOS_LAMBDA(
long t) {
418 const int ly = g + (int)(t % nyi), lz = g + (int)(t / nyi);
419 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
420 const int P = (color + og.x + og.y + ly + og.z + lz) & 1;
423 [&](
long i,
long xp,
long xm,
long yp,
long ym,
long zp,
long zm) {
424 cutcellSmoothFaceCell(phi, b,
AC, AFX, AFY, AFZ, i, sx, sy, sz, xp, xm, yp,
430 for (
long t = 0; t < (long)nyi * nzi; ++t)
434 Kokkos::parallel_for(
"peclet::flow::cc_smooth",
435 Kokkos::RangePolicy<CCExec>(space, 0, (
long)nyi * nzi), pencil);
439 Kokkos::parallel_for(
440 "peclet::flow::cc_smooth", MD(space, {g, g, g}, {e.x - g, e.y - g, e.z - g}),
441 KOKKOS_LAMBDA(
int lx,
int ly,
int lz) {
442 if (((og.x + lx + og.y + ly + og.z + lz) & 1) != color)
444 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
445 const long i = (long)lx + (
long)ly * sy + (long)lz * sz;
447 cutcellSmoothFaceCell(phi, b,
AC, AFX, AFY, AFZ, i, sx, sy, sz, w.xp, w.xm, w.yp, w.ym,
454 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>) {
455 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
457 [=](
long i,
long xp,
long xm,
long yp,
long ym,
long zp,
long zm) {
458 cutcellResidualFaceCell(r, x, b,
AC, AFX, AFY, AFZ, i, sx, sy, sz, xp, xm, yp,
464 "peclet::flow::cc_residual",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
465 KOKKOS_LAMBDA(
int lx,
int ly,
int lz) {
466 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
467 const long i = (long)lx + (
long)ly * sy + (long)lz * sz;
469 cutcellResidualFaceCell(r, x, b,
AC, AFX, AFY, AFZ, i, sx, sy, sz, w.xp, w.xm, w.yp, w.ym,
477 "peclet::flow::cc_apply",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
478 KOKKOS_LAMBDA(
int lx,
int ly,
int lz) {
479 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
480 const long i = (long)lx + (
long)ly * sy + (long)lz * sz;
482 y(i) =
cutcellApplyFaceCell(x,
AC, AFX, AFY, AFZ, i, sx, sy, sz, w.xp, w.xm, w.yp, w.ym,
487 C3 n,
int g,
double gfx,
double gfy,
double gfz) {
488 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>) {
489 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
491 [=](
long i,
long xp,
long xm,
long yp,
long ym,
long zp,
long zm) {
492 y(i) =
cutcellApplyExactCell(x, ox, oy, oz, i, sx, sy, sz, xp, xm, yp, ym, zp,
498 "peclet::flow::cc_apply_exact",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
499 KOKKOS_LAMBDA(
int lx,
int ly,
int lz) {
500 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
501 const long i = (long)lx + (
long)ly * sy + (long)lz * sz;
503 y(i) =
cutcellApplyExactCell(x, ox, oy, oz, i, sx, sy, sz, w.xp, w.xm, w.yp, w.ym, w.zp,
504 w.zm, gfx, gfy, gfz);
512template <
class CV,
class FV>
514 int gc,
int gf,
C3 ratio,
int icx,
int icy,
int icz) {
515 const long fsy = fext.
x, fsz = (long)fext.
x * fext.
y;
517 for (
int dz = 0; dz < ratio.z; ++dz)
518 for (
int dy = 0; dy < ratio.y; ++dy)
519 for (
int dx = 0; dx < ratio.x; ++dx) {
520 const int fx = ratio.x * icx + dx + gf, fy = ratio.y * icy + dy + gf,
521 fz = ratio.z * icz + dz + gf;
522 s += fine((
long)fx + (
long)fy * fsy + (
long)fz * fsz);
525 (long)(icx + gc) + (long)(icy + gc) * cext.
x + (long)(icz + gc) * (long)cext.
x * cext.
y;
526 coarse(ci) = s / (double)(ratio.x * ratio.y * ratio.z);
528template <
class FV,
class CV>
530 int gf,
int gc,
C3 ratio,
int ifx,
int ify,
int ifz) {
539 const int x0 = (ratio.x == 2) ? (ifx >> 1) + gc - 1 + (ifx & 1) : ifx + gc;
540 const int y0 = (ratio.y == 2) ? (ify >> 1) + gc - 1 + (ify & 1) : ify + gc;
541 const int z0 = (ratio.z == 2) ? (ifz >> 1) + gc - 1 + (ifz & 1) : ifz + gc;
542 const double wx = (ratio.x == 2) ? ((ifx & 1) ? 0.25 : 0.75) : 0.0;
543 const double wy = (ratio.y == 2) ? ((ify & 1) ? 0.25 : 0.75) : 0.0;
544 const double wz = (ratio.z == 2) ? ((ifz & 1) ? 0.25 : 0.75) : 0.0;
545 const long sy = cext.
x, sz = (long)cext.
x * cext.
y;
546 auto C = [&](
int xx,
int yy,
int zz) {
return coarse((
long)xx + (
long)yy * sy + (
long)zz * sz); };
547 const double c00 = C(x0, y0, z0) * (1 - wx) + C(x0 + 1, y0, z0) * wx;
548 const double c10 = C(x0, y0 + 1, z0) * (1 - wx) + C(x0 + 1, y0 + 1, z0) * wx;
549 const double c01 = C(x0, y0, z0 + 1) * (1 - wx) + C(x0 + 1, y0, z0 + 1) * wx;
550 const double c11 = C(x0, y0 + 1, z0 + 1) * (1 - wx) + C(x0 + 1, y0 + 1, z0 + 1) * wx;
551 const double c0 = c00 * (1 - wy) + c10 * wy, c1 = c01 * (1 - wy) + c11 * wy;
553 (long)(ifx + gf) + (long)(ify + gf) * fext.
x + (long)(ifz + gf) * (long)fext.
x * fext.
y;
554 fine(fi) += c0 * (1 - wz) + c1 * wz;
559template <
class FV,
class CV>
561 int gf,
int gc,
C3 ratio,
int ifx,
int ify,
563 const int cx = ((ratio.x == 2) ? (ifx >> 1) : ifx) + gc;
564 const int cy = ((ratio.y == 2) ? (ify >> 1) : ify) + gc;
565 const int cz = ((ratio.z == 2) ? (ifz >> 1) : ifz) + gc;
566 const long ci = (long)cx + (
long)cy * cext.
x + (long)cz * (
long)cext.
x * cext.
y;
568 (long)(ifx + gf) + (long)(ify + gf) * fext.
x + (long)(ifz + gf) * (long)fext.
x * fext.
y;
569 fine(fi) += coarse(ci);
572template <
class FV,
class AV>
580 "peclet::flow::restrict",
C3{0, 0, 0},
C3{cinner.
x, cinner.
y, cinner.
z},
581 KOKKOS_LAMBDA(
int icx,
int icy,
int icz) {
582 restrictAvgCell(coarse, fine, cext, fext, gc, gf, ratio, icx, icy, icz);
591 int gc,
int gf,
C3 cinner,
C3 ratio) {
593 "peclet::flow::restrict",
C3{0, 0, 0},
C3{cinner.
x, cinner.
y, cinner.
z},
594 KOKKOS_LAMBDA(
int icx,
int icy,
int icz) {
595 restrictAvgCell(coarse, fine, cext, fext, gc, gf, ratio, icx, icy, icz);
596 coarseX((
long)(icx + gc) + (
long)(icy + gc) * cext.
x +
597 (
long)(icz + gc) * (
long)cext.
x * cext.
y) = 0.0;
611template <
class OpV,
class WV>
614 double gfy,
double gfz,
int fp64,
int* bad) {
616 "peclet::flow::cc_build_op",
C3{0, 0, 0},
C3{e.x, e.y, e.z},
617 KOKKOS_LAMBDA(
int lx,
int ly,
int lz) {
618 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
619 const long i = (long)lx + (
long)ly * sy + (long)lz * sz;
622 lx >= gb && lx < e.x - gb && ly >= gb && ly < e.y - gb && lz >= gb && lz < e.z - gb;
624 cutcellBuildFaceOpCell(
AC, AFX, AFY, AFZ, ox, oy, oz, i, sx, sy, sz, gfx, gfy, gfz,
627 const double tw = ox(i) * gfx, ts = oy(i) * gfy, tb = oz(i) * gfz;
628 const double te = ox(i + sx) * gfx;
629 const double tn = oy(i + sy) * gfy;
630 const double tt = oz(i + sz) * gfz;
631 AC(i) = te + tw + tn + ts + tt + tb;
634 const double tw = ox(i) * gfx;
635 const double ts = oy(i) * gfy;
636 const double tb = oz(i) * gfz;
640 const bool okw = tw == 0.0 || (tw >= 1e-30 && tw <= 1e30);
641 const bool oks = ts == 0.0 || (ts >= 1e-30 && ts <= 1e30);
642 const bool okb = tb == 0.0 || (tb >= 1e-30 && tb <= 1e30);
643 if (!(okw && oks && okb))
644 Kokkos::atomic_max(bad, 1);
650 "peclet::flow::prolong",
C3{0, 0, 0},
C3{finner.
x, finner.
y, finner.
z},
651 KOKKOS_LAMBDA(
int ifx,
int ify,
int ifz) {
652 prolongAddCell(fine, coarse, fext, cext, gf, gc, ratio, ifx, ify, ifz);
657 C3 finner,
C3 ratio) {
659 "peclet::flow::prolong_const",
C3{0, 0, 0},
C3{finner.
x, finner.
y, finner.
z},
660 KOKKOS_LAMBDA(
int ifx,
int ify,
int ifz) {
725 using Member = Kokkos::TeamPolicy<CCExec>::member_type;
729 Kokkos::View<long*, CCMem>
cnt;
730 Kokkos::View<int*, CCMem>
nc;
734 Kokkos::View<int*, CCMem>
kc;
735 Kokkos::View<double*, CCMem>
aug;
736 KOKKOS_INLINE_FUNCTION
long cell(
int q,
int& lx,
int& ly,
int& lz)
const {
737 const int t = q /
inner.x;
738 lx = 1 + (q - t *
inner.x);
740 ly = 1 + (t - lz *
inner.y);
742 return (
long)lx + (long)ly *
ext.x + (
long)lz * (long)
ext.x *
ext.y;
745 KOKKOS_INLINE_FUNCTION
int sweep(
const Member& t,
const Kokkos::View<int*, CCMem>& src,
746 const Kokkos::View<int*, CCMem>& dst)
const {
748 const long sy =
ext.x, sz = (long)
ext.x *
ext.y;
750 Kokkos::parallel_reduce(
751 Kokkos::TeamThreadRange(t, ninner),
752 [&](
int q,
int& acc) {
754 const long i =
cell(q, lx, ly, lz);
758 const long nb[6] = {w.xp, w.xm, w.yp, w.ym, w.zp, w.zm};
759 const double cf[6] = {(double)
AFX(i + 1), (double)
AFX(i), (double)
AFY(i + sy),
760 (double)
AFY(i), (double)
AFZ(i + sz), (double)
AFZ(i)};
761 for (
int f = 0; f < 6; ++f) {
762 const int lj = src(nb[f]);
763 if (cf[f] < 0.0 && lj >= 0 && lj < m)
776 Kokkos::parallel_for(Kokkos::TeamThreadRange(t, ninner), [&](
int q) {
778 const long i =
cell(q, lx, ly, lz);
779 comp(i) = (
AC(i) > 1e-30f) ? (
int)i : -1;
790 Kokkos::single(Kokkos::PerTeam(t), [&]() {
792 for (
int q = 0; q < ninner; ++q) {
794 const long i =
cell(q, lx, ly, lz);
795 if (
comp(i) == (
int)i)
800 for (
int q = 0; q < ninner; ++q) {
802 const long i =
cell(q, lx, ly, lz);
817 for (
int pass = 0; pass < 2; ++pass)
818 for (
int q = 0; q < ninner; ++q) {
820 const long i =
cell(q, lx, ly, lz);
821 const int c =
comp(i);
824 const int k = (
slow == 0 ? lx : (
slow == 1 ? ly : lz)) - 1;
825 if (pass == 0 && k >
kc(c))
827 else if (pass == 1 && k ==
kc(c))
831 aug(c) = m[c] > 0 ? 1.0 / (double)m[c] : 0.0;
840template <
class FacReal =
float>
842 using Member = Kokkos::TeamPolicy<CCExec>::member_type;
852 Kokkos::View<const int*, CCMem>
comp;
853 Kokkos::View<const long*, CCMem>
ccnt;
855 Kokkos::View<double*, CCMem>
ks;
857 Kokkos::View<int*, CCMem>
info;
868 KOKKOS_INLINE_FUNCTION
static long cell(
const BottomLevel& l,
int q,
int& lx,
int& ly,
int& lz) {
870 const int t = q / nx;
871 lx = 1 + (q - t * nx);
873 ly = 1 + (t - lz * ny);
875 return (
long)lx + (long)ly * l.
ext.
x + (
long)lz * (long)l.
ext.
x * l.
ext.
y;
882 static constexpr bool kHostLane = std::is_same_v<CCMem, Kokkos::HostSpace>;
885 KOKKOS_INLINE_FUNCTION
static double teamSum(
const Member& t,
int n,
const F& f) {
892 for (
int q = 0; q < n; ++q)
898 Kokkos::parallel_reduce(Kokkos::TeamThreadRange(t, n), f, s);
904 KOKKOS_INLINE_FUNCTION
static double teamMax(
const Member& t,
int n,
const F& f) {
911 for (
int q = 0; q < n; ++q)
917 Kokkos::parallel_reduce(Kokkos::TeamThreadRange(t, n), f, Kokkos::Max<double>(s));
925 const long i =
cell(l, q, lx, ly, lz);
926 if (l.
AC(i) > 1e-30f)
934 for (
int c = 0; c <
nc; ++c) {
935 const double s =
teamSum(t,
nInner(l), [&](
int q,
double& acc) {
937 const long i =
cell(l, q, lx, ly, lz);
941 Kokkos::single(Kokkos::PerTeam(t), [&]() {
cmean(c) = s / (double)
ccnt(c); });
944 Kokkos::parallel_for(Kokkos::TeamThreadRange(t,
nInner(l)), [&](
int q) {
946 const long i =
cell(l, q, lx, ly, lz);
947 const int c =
comp(i);
967 const long sy = B0.
ext.
x, sz = (long)B0.
ext.
x * B0.
ext.
y;
968 Kokkos::parallel_for(Kokkos::TeamThreadRange(t,
nExt(B0)), [&](
int i) {
x(i) = 0.0; });
970 Kokkos::parallel_for(Kokkos::TeamThreadRange(t,
nInner(B0)), [&](
int q) {
972 const long i =
cell(B0, q, lx, ly, lz);
973 r(i) = (
comp(i) >= 0) ?
b(i) : 0.0;
978 auto rmax = [&](
int q,
double& acc) {
980 const long i =
cell(B0, q, lx, ly, lz);
981 if (B0.
AC(i) > 1e-30f) {
982 const double v = Kokkos::fabs(
r(i));
990 bool fail =
dir.stat(0) == 0;
991 if (r0 != 0.0 && !fail) {
993 Kokkos::parallel_for(Kokkos::TeamThreadRange(t,
nExt(B0)), [&](
int i) {
p(i) =
z(i); });
995 double rz =
dot(t, B0,
r,
z);
999 Kokkos::parallel_for(Kokkos::TeamThreadRange(t,
nInner(B0)), [&](
int q) {
1001 const long i =
cell(B0, q, lx, ly, lz);
1003 ap(i) =
cutcellApplyFaceCell(
p, B0.
AC, B0.
AFX, B0.
AFY, B0.
AFZ, i, 1L, sy, sz, w.xp, w.xm,
1004 w.yp, w.ym, w.zp, w.zm);
1007 const double pAp =
dot(t, B0,
p,
ap);
1008 if (!(Kokkos::isfinite(pAp) && pAp > 1e-300)) {
1009 fail = !Kokkos::isfinite(pAp);
1012 const double a = rz / pAp;
1015 Kokkos::parallel_for(Kokkos::TeamThreadRange(t,
nInner(B0)), [&](
int q) {
1017 const long i =
cell(B0, q, lx, ly, lz);
1030 const double rzc =
teamSum(t,
nInner(B0), [&](
int q,
double& acc) {
1032 const long i =
cell(B0, q, lx, ly, lz);
1033 if (B0.
AC(i) > 1e-30f)
1034 acc +=
r(i) * (
z(i) -
zp(i));
1036 const double beta = rzc / rz;
1037 rz =
dot(t, B0,
r,
z);
1038 if (!(Kokkos::isfinite(rz) && Kokkos::isfinite(beta))) {
1042 Kokkos::parallel_for(Kokkos::TeamThreadRange(t,
nInner(B0)), [&](
int q) {
1044 const long i =
cell(B0, q, lx, ly, lz);
1045 p(i) =
z(i) + beta *
p(i);
1051 Kokkos::parallel_for(Kokkos::TeamThreadRange(t,
nExt(B0)), [&](
int i) {
x(i) = 0.0; });
1052 Kokkos::single(Kokkos::PerTeam(t), [&]() {
ks(
flagSlot) = 1.0; });
1057 Kokkos::single(Kokkos::PerTeam(t), [&]() {
info(0) = it; });
1068 static const int lv = [] {
1069 const char* e = std::getenv(
"PECLET_FLOW_MG_DEBUG");
1070 return e ? std::atoi(e) : 0;
1075 static const int n = [] {
1076 const char* e = std::getenv(
"PECLET_FLOW_MG_DEBUG_SOLVES");
1077 return e ? std::atoi(e) : 3;
1096#ifdef PECLET_FLOW_MPI
1148#ifdef PECLET_FLOW_MPI
1149 std::shared_ptr<GridHaloTopology<3>> halo;
1150 std::shared_ptr<GridHalo<double>> dev;
1153 std::shared_ptr<GridHalo<VReal>> devf;
1160 std::shared_ptr<GridHaloTopology<3>> shalo;
1161 std::shared_ptr<GridHalo<double>> sdev;
1162 std::shared_ptr<GridHalo<VReal>> sdevf;
1170 MPI_Comm comm = MPI_COMM_NULL;
1175 std::shared_ptr<Telescope> tele;
1178#ifdef PECLET_FLOW_MPI
1202 peclet::core::decomp::StageComm comm;
1203 bool root()
const {
return comm.active; }
1204 bool repartition()
const {
return comm.kind == peclet::core::decomp::StageKind::Repartition; }
1207 C3 mInner{}, mOg{}, mExt{};
1209 std::vector<C3> memO, memS;
1211 peclet::core::decomp::RedistributeTopology<3, double> move;
1214 peclet::core::decomp::BlockDecomposer<3> srcDec, dstDec;
1215 std::vector<double> back;
1219 static constexpr int G = 1;
1225 return C3{lv.og.x - lv.g + 1, lv.og.y - lv.g + 1, lv.og.z - lv.g + 1};
1246 static C3 mgChooseRatio(
const double H[3],
const bool canA[3],
bool aniso,
double theta) {
1259 for (
int a = 0; a < 3; ++a)
1260 if (canA[a] && (!any || H[a] < hmin)) {
1266 const double lim = theta * hmin;
1267 if (canA[0] && H[0] < lim)
1269 if (canA[1] && H[1] < lim)
1271 if (canA[2] && H[2] < lim)
1286 const double H[3] = {hp[0] * (double)cf.x, hp[1] * (
double)cf.y, hp[2] * (double)cf.z};
1287 const int gd[3] = {g.x, g.y, g.z};
1289 for (
int a = 0; a < 3; ++a)
1290 if (H[a] < H[(a + 1) % 3] && H[a] < H[(a + 2) % 3])
1295 const double A = std::min(H[(s + 1) % 3], H[(s + 2) % 3]) / H[s];
1297 const bool blocked = !
canHalve(gd[s]);
1298 const bool outflow = bc[2 * s] == 3 || bc[2 * s + 1] == 3;
1299 return (L >= 1 && blocked && A > theta && !outflow) ? s : -1;
1304 for (
int L = 0; L < (int)lv_.size(); ++L)
1305 lv_[L].lineAxis =
lineAxisTrigger(L, lv_[L].gdim, lv_[L].cfac, hp_, aspectTheta_, bc_,
1306 &lv_[L].strongAspect);
1315 for (
int a = 0; a < 3; ++a)
1317 aniso_ = !(hp_[0] == 1.0 && hp_[1] == 1.0 && hp_[2] == 1.0);
1322 r.reserve(lv_.size());
1323 for (
const auto& v : lv_)
1324 r.push_back(v.ratio);
1340 C3 inner{nx, ny, nz}, cf{1, 1, 1};
1343 const bool autoDepth =
nLevels == 0;
1345 for (
int L = 0; L < nLevelsCap; ++L) {
1349 v.ext =
C3{inner.x + 2 *
G, inner.y + 2 *
G, inner.z + 2 *
G};
1351 v.n = (std::size_t)v.ext.x * v.ext.y * v.ext.z;
1355 if (L + 1 < nLevelsCap && !autoStop) {
1369 v.AC =
FPV(
"mg_A", v.n);
1371 v.nFluidDev = Kokkos::View<long, CCMem>(
"mg_nfluid");
1376 if (next.
x == inner.x && next.
y == inner.y && next.
z == inner.z)
1379 cf =
C3{cf.x * ratio.x, cf.y * ratio.y, cf.z * ratio.z};
1386 const C3 lb = lv_.back().inner;
1387 const bool more = (lb.
x % 2 == 0 && lb.
x / 2 >= 2) || (lb.
y % 2 == 0 && lb.
y / 2 >= 2) ||
1388 (lb.
z % 2 == 0 && lb.
z / 2 >= 2);
1389 printf(
"[mg] init %dx%dx%d single-rank -> %d levels (requested %s)\n", nx, ny, nz,
1392 for (
int L = 0; L < (int)lv_.size(); ++L)
1393 printf(
"[mg] L%d dims %4dx%4dx%4d ratio(%d,%d,%d)%s\n", L, lv_[L].inner.x, lv_[L].inner.y,
1394 lv_[L].inner.z, lv_[L].ratio.x, lv_[L].ratio.y, lv_[L].ratio.z,
1399#ifdef PECLET_FLOW_MPI
1428 static bool teleLiftable(
const peclet::core::decomp::BlockDecomposer<3>& d,
C3 gs) {
1429 const int ga[3] = {gs.x, gs.y, gs.z};
1430 for (
int ax = 0; ax < 3; ++ax)
1431 if (
canHalve(ga[ax]) && !evenBlocksOn(d, ax))
1441 static peclet::core::IVec<3> coarsenAlignment(
int gnx,
int gny,
int gnz) {
1442 C3 gs{gnx, gny, gnz};
1443 peclet::core::IVec<3>
a{1, 1, 1};
1444 for (
bool any =
true; any;) {
1471 for (
int k = 0;
k < 3; ++
k)
1487 static peclet::core::IVec<3> refineFactor(
int gnx,
int gny,
int gnz,
int levels) {
1488 C3
gs{gnx, gny, gnz};
1489 peclet::core::IVec<3>
r{1, 1, 1};
1490 for (
int L = 1;
L < levels; ++
L) {
1508 static unsigned splitAxes(
const peclet::core::decomp::BlockDecomposer<3>& d) {
1510 for (
const auto& s :
d.sizes())
1511 for (
int k = 0;
k < 3; ++
k)
1512 if (s[k] <
d.globalSize()[k])
1519 static std::uint64_t fingerprint(
const peclet::core::decomp::BlockDecomposer<3>& d) {
1520 std::uint64_t
h = 1469598103934665603ull;
1521 auto mix = [&
h](
long long v) {
1522 for (
int b = 0;
b < 8; ++
b) {
1523 h ^=
static_cast<std::uint64_t
>((
static_cast<unsigned long long>(
v) >> (8 *
b)) & 0xffu);
1524 h *= 1099511628211ull;
1527 for (
int k = 0;
k < 3; ++
k)
1528 mix((
long long)
d.globalSize()[k]);
1529 mix((
long long)
d.numBlocks());
1530 for (std::size_t b = 0;
b <
d.numBlocks(); ++
b)
1531 for (
int k = 0;
k < 3; ++
k) {
1532 mix((
long long)
d.origins()[b][k]);
1533 mix((
long long)
d.sizes()[b][k]);
1535 std::vector<int> dim;
1536 std::vector<peclet::core::Index>
val;
1537 d.flattenTree(dim,
val);
1538 mix((
long long)dim.size());
1539 for (std::size_t i = 0;
i < dim.size(); ++
i) {
1540 mix((
long long)dim[i]);
1541 mix((
long long)
val[i]);
1548 static int nestDepth(
const peclet::core::decomp::BlockDecomposer<3>& d0) {
1549 namespace cdec = peclet::core::decomp;
1550 cdec::BlockDecomposer<3>
d =
d0;
1551 C3
gs{(
int)
d.globalSize()[0], (
int)
d.globalSize()[1], (
int)
d.globalSize()[2]};
1553 for (;
n < 4; ++
n) {
1555 cdec::minBlockExtent(d) < 4)
1582 static peclet::core::decomp::BlockDecomposer<3> decomposition(std::size_t numBlocks,
int gnx,
1583 int gny,
int gnz,
int levels = 0,
1584 double maxImbalance = 1.05) {
1585 using Dec = peclet::core::decomp::BlockDecomposer<3>;
1586 const peclet::core::IVec<3>
G{gnx, gny, gnz};
1587 Dec A(numBlocks,
G, coarsenAlignment(gnx, gny, gnz));
1594 if (!(maxImbalance > 1.0))
1595 maxImbalance = 1.05;
1597 return legacyCoarseFirst(std::move(A), numBlocks, gnx, gny, gnz, levels, maxImbalance);
1598 return autoDecomposition(std::move(A), numBlocks, gnx, gny, gnz, maxImbalance);
1602 static std::size_t cellsOf(
const peclet::core::IVec<3>& s) {
1603 return static_cast<std::size_t
>(
s[0]) *
static_cast<std::size_t
>(s[1]) *
1604 static_cast<std::size_t
>(
s[2]);
1606 static std::size_t largestBlock(
const peclet::core::decomp::BlockDecomposer<3>& d) {
1608 for (
const auto& s :
d.sizes())
1609 hi = std::max(hi, cellsOf(s));
1612 static std::size_t smallestBlock(
const peclet::core::decomp::BlockDecomposer<3>& d) {
1613 std::size_t
lo = std::numeric_limits<std::size_t>::max();
1614 for (
const auto& s :
d.sizes())
1615 lo = std::min(lo, cellsOf(s));
1618 static double imbalanceOf(
const peclet::core::decomp::BlockDecomposer<3>& d) {
1619 const std::size_t
lo = smallestBlock(d);
1620 return lo ?
static_cast<double>(largestBlock(d)) /
static_cast<double>(lo)
1621 : std::numeric_limits<double>::infinity();
1627 static peclet::core::decomp::BlockDecomposer<3> coarseFirst(
1628 std::size_t numBlocks,
int gnx,
int gny,
int gnz,
int L,
bool& ok,
1629 peclet::core::decomp::SplitAxis axis) {
1630 using Dec = peclet::core::decomp::BlockDecomposer<3>;
1632 const peclet::core::IVec<3>
r = refineFactor(gnx, gny, gnz, L);
1633 if (r[0] == 1 && r[1] == 1 && r[2] == 1)
1635 const peclet::core::IVec<3> Gc{gnx /
r[0], gny /
r[1], gnz /
r[2]};
1636 if (cellsOf(Gc) < numBlocks)
1639 coarse.init(numBlocks, Gc, peclet::core::IVec<3>{1, 1, 1},
r,
axis);
1641 return coarse.refined(r);
1647 static peclet::core::decomp::BlockDecomposer<3> legacyCoarseFirst(
1648 peclet::core::decomp::BlockDecomposer<3> A, std::size_t numBlocks,
int gnx,
int gny,
int gnz,
1649 int levels,
double maxImbalance) {
1650 using Dec = peclet::core::decomp::BlockDecomposer<3>;
1651 const peclet::core::IVec<3>
one{1, 1, 1};
1652 for (
int L = levels;
L >= 2; --
L) {
1655 coarseFirst(numBlocks, gnx, gny, gnz, L, ok, peclet::core::decomp::SplitAxis::Longest);
1657 if (refineFactor(gnx, gny, gnz, L) == one)
1661 if (imbalanceOf(fine) <= maxImbalance) {
1663 printf(
"[mg] decomposition: coarse-first depth %d (imbalance %.3f)\n", L,
1671 static peclet::core::decomp::BlockDecomposer<3> autoDecomposition(
1672 peclet::core::decomp::BlockDecomposer<3> A, std::size_t numBlocks,
int gnx,
int gny,
int gnz,
1673 double maxImbalance) {
1674 namespace cdec = peclet::core::decomp;
1675 using Dec = cdec::BlockDecomposer<3>;
1676 const peclet::core::IVec<3>
G{gnx, gny, gnz},
one{1, 1, 1};
1679 printf(
"[mg] decomposition: auto %s (max/min %.3f; legacy %.3f)\n", branch, imbalanceOf(d),
1687 const unsigned axesA = splitAxes(A);
1688 const bool legacyImbalanced =
1689 4 * largestBlock(A) * numBlocks >= 5 * cellsOf(
G);
1690 auto within = [&](
const Dec&
d) {
1691 const unsigned ax = splitAxes(d);
1692 return (ax & ~axesA) == 0
u && (
ax == axesA || legacyImbalanced);
1696 bool guardHit =
false;
1697 auto guardOnly = [&](
const Dec&
d) {
return (splitAxes(d) & ~axesA) == 0
u && !within(d); };
1698 if (largestBlock(A) == smallestBlock(A)) {
1704 const int depthA = nestDepth(A);
1705 for (
int L : {5, 4}) {
1707 Dec C = coarseFirst(numBlocks, gnx, gny, gnz, L, ok, cdec::SplitAxis::Balanced);
1709 if (refineFactor(gnx, gny, gnz, L) == one)
1713 if (smallestBlock(C) > 0 && imbalanceOf(C) <= maxImbalance && within(C) &&
1714 nestDepth(C) >= depthA) {
1715 report(L == 5 ?
"C5" :
"C4", C);
1718 if (
mgDebugLevel() && smallestBlock(C) > 0 && imbalanceOf(C) <= maxImbalance &&
1719 nestDepth(C) >= depthA && guardOnly(C))
1723 A2.init(numBlocks,
G, coarsenAlignment(gnx, gny, gnz), one, cdec::SplitAxis::Balanced);
1724 if (largestBlock(A2) < largestBlock(A) && nestDepth(A2) >= std::min(depthA, 3) && within(A2)) {
1728 if (
mgDebugLevel() && largestBlock(A2) < largestBlock(A) &&
1729 nestDepth(A2) >= std::min(depthA, 3) && guardOnly(A2))
1731 report(guardHit ?
"A-fallback (guard)" :
"A-fallback", A);
1737 static bool canHalve(
int d) {
return (d % 2 == 0) && (d / 2 >= 2); }
1741 const double H[3] = {hp_[0] * (double)cf.x, hp_[1] * (
double)cf.y, hp_[2] * (double)cf.z};
1751#ifdef PECLET_FLOW_MPI
1753 static bool evenBlocksOn(
const peclet::core::decomp::BlockDecomposer<3>& d,
int ax) {
1754 for (std::size_t b = 0; b < d.sizes().size(); ++b)
1755 if ((d.origins()[b][ax] % 2) || (d.sizes()[b][ax] % 2))
1761 static long minBlockExtentOf(
const peclet::core::decomp::BlockDecomposer<3>& d) {
1762 long m = std::numeric_limits<long>::max();
1763 for (
const auto& s : d.sizes())
1764 for (
int k = 0; k < 3; ++k)
1765 m = std::min(m, (
long)s[k]);
1771 void initLevelLayout(
Level& v,
int L,
const peclet::core::decomp::BlockDecomposer<3>& dec,
1772 int curRank, MPI_Comm curComm,
C3 gs,
C3 cf)
const {
1773 std::array<bool, 3> per{
true,
true,
true};
1774 v.halo = std::make_shared<GridHaloTopology<3>>();
1784 bool oddPeriodic =
false;
1785 for (
int a = 0;
a < 3; ++
a)
1786 oddPeriodic = oddPeriodic || (per[a] && dec.globalSize()[a] % 2 != 0);
1788 L > 0 && !hasBC_ && (caMode_ &
kCaMg) && minBlockExtentOf(dec) >= 4 && !oddPeriodic;
1790 v.caOk = (
v.g == 2);
1791 v.halo->buildTopology(dec, curRank,
v.g, per, curComm);
1793 v.dev = std::make_shared<GridHalo<double>>();
1794 v.dev->init(*
v.halo);
1795 v.dev->setLabel(
"mg L" + std::to_string(L) +
" g" + std::to_string(
v.g) +
" ranks" +
1796 std::to_string(dec.numBlocks()));
1800 std::array<bool, 3> sper = per;
1803 for (
int a = 0;
a < 3; ++
a) {
1804 bool oneBlock =
true;
1805 for (std::size_t b = 0;
b < dec.sizes().
size(); ++
b)
1807 oneBlock && dec.origins()[
b][
a] == 0 && dec.sizes()[
b][
a] == dec.globalSize()[
a];
1809 v.wholeAxes |= 1 <<
a;
1810 const bool outflow = bc_[2 *
a] == 3 || bc_[2 *
a + 1] == 3;
1811 if (oneBlock && !outflow && dec.globalSize()[a] % 2 == 0) {
1812 v.wrapAxes |= 1 <<
a;
1816 v.shalo = std::make_shared<GridHaloTopology<3>>();
1817 v.shalo->buildTopology(dec, curRank,
v.g, sper, curComm,
1818 peclet::core::halo::Neighborhood::Faces);
1819 v.sdev = std::make_shared<GridHalo<double>>();
1820 v.sdev->init(*
v.shalo);
1821 v.sdev->setLabel(
"mg L" + std::to_string(L) +
" g" + std::to_string(
v.g) +
" stencil w" +
1822 std::to_string(
v.wrapAxes));
1824 const auto& idx =
v.halo->indexer();
1825 const auto eg = idx.sizeInclGhost(), ino = idx.sizeInner(), oig = idx.originInclGhost();
1826 v.ext = {(
int)eg[0], (
int)
eg[1], (
int)eg[2]};
1827 v.inner = {(
int)ino[0], (
int)ino[1], (
int)ino[2]};
1828 v.og = {(
int)oig[0] +
v.g, (
int)oig[1] +
v.g,
1832 v.n = idx.numCellsInclGhost();
1836 bool teleHere(
const peclet::core::decomp::BlockDecomposer<3>& curDec, C3 gs,
int L)
const {
1837 namespace cdec = peclet::core::decomp;
1840 auto liftable = [&](
const cdec::BlockDecomposer<3>&
d) {
return teleLiftable(d, gs); };
1841 const bool blocked = !liftable(curDec);
1842 const bool tooSmall =
1843 teleMinExtent_ > 0 && cdec::minBlockExtent(curDec) < (peclet::core::Index)teleMinExtent_;
1844 return canAny && curDec.numBlocks() > 1 &&
1845 ((telescope_ && (blocked || tooSmall)) || teleForce_ ==
L);
1848 static void allocLevelFields(
Level& v) {
1852 v.nFluidDev = Kokkos::View<long, CCMem>(
"mg_nfluid");
1856 for (
FPV* p : {&
v.AC, &
v.AFX, &
v.AFY, &
v.AFZ})
1857 *p =
FPV(
"mg_A",
v.n);
1861 void printMpiHierarchy(
int size,
int nLevels, C3 gs,
1862 const peclet::core::decomp::BlockDecomposer<3>& curDec)
const {
1863 const int gnx = gnxF_, gny = gnyF_, gnz = gnzF_;
1864 printf(
"[mg] initMpi %dx%dx%d np=%d -> %d levels (requested %s)%s\n", gnx, gny, gnz, size,
1866 telescope_ ?
" [telescope ON]" :
"");
1869 for (
int a = 0;
a < 3; ++
a) {
1870 const int d =
a == 0 ?
gs.x : (
a == 1 ?
gs.y :
gs.z);
1871 more = more || (
canHalve(d) && (telescope_ || evenBlocksOn(curDec, a)));
1875 C3
g{gnx, gny, gnz};
1877 for (
int L = 0;
L < (
int)lv_.size(); ++
L) {
1879 "[mg] L%d global %4dx%4dx%4d ranks %5d rank0 block %4dx%4dx%4d "
1880 "ratio(%d,%d,%d)%s%s\n",
1881 L,
g.x,
g.y,
g.z, ranks, lv_[L].inner.x, lv_[L].inner.y, lv_[L].inner.z, lv_[L].ratio.x,
1883 lv_[L].tele ? (lv_[L].tele->repartition() ?
" -> REPARTITION" :
" -> TELESCOPE") :
"");
1886 MPI_Comm_size(lv_[L].tele->comm.sub, &sub);
1889 g = C3{
g.x / lv_[
L].ratio.x,
g.y / lv_[
L].ratio.y,
g.z / lv_[
L].ratio.z};
1893 void initMpi(
int gnx,
int gny,
int gnz,
int nLevels, MPI_Comm comm,
1894 const peclet::core::decomp::BlockDecomposer<3>* dec0 =
nullptr) {
1900 distributed_ =
true;
1907 MPI_Comm_rank(comm, &rank);
1908 MPI_Comm_size(comm, &size);
1909 C3
gs{gnx, gny, gnz},
cf{1, 1, 1};
1915 peclet::core::decomp::BlockDecomposer<3> curDec;
1919 curDec = decomposition(
static_cast<std::size_t
>(size),
gs.x,
gs.y,
gs.z);
1924 MPI_Comm curComm =
comm;
1929 const peclet::core::Index maxBlockCells =
1930 repartition_ ? peclet::core::decomp::largestBlockCells(curDec) : 0;
1934 const bool autoDepth =
nLevels == 0;
1936 for (
int L = 0;
L < nLevelsCap; ++
L) {
1938 initLevelLayout(v, L, curDec, curRank, curComm, gs, cf);
1940 bool idleBelow =
false;
1942 if (L + 1 < nLevelsCap && !autoStop) {
1948 const bool doTele = teleHere(curDec, gs, L);
1950 if (
auto t = teleTarget(curDec, gs, L, maxBlockCells)) {
1951 v.tele = makeTelescope(v, L, curDec, *t, curComm);
1952 if (
v.tele->root()) {
1954 curComm =
v.tele->comm.sub;
1955 MPI_Comm_rank(curComm, &curRank);
1961 auto evenBlocks = [&](
int ax) {
return evenBlocksOn(curDec, ax); };
1973 allocLevelFields(v);
1976 teleActive_ =
false;
1979 if (next.
x ==
gs.x && next.
y ==
gs.y && next.
z ==
gs.z)
1985 curDec = curDec.coarsened(peclet::core::IVec<3>{
ratio.x,
ratio.y,
ratio.z});
1988 for (
Level& v : lv_)
1993 printMpiHierarchy(size,
nLevels, gs, curDec);
2003 std::optional<peclet::core::decomp::StageTarget<3>> teleTarget(
2004 const peclet::core::decomp::BlockDecomposer<3>& curDec, C3 gs,
int L,
2005 peclet::core::Index maxBlockCells)
const {
2006 namespace cdec = peclet::core::decomp;
2008 auto liftable = [&](
const cdec::BlockDecomposer<3>&
d) {
return teleLiftable(d, gs); };
2009 std::optional<cdec::StageTarget<3>>
t;
2010 if (teleForce_ == L) {
2011 if (
auto m = cdec::shallowestLiftableMerge(curDec, liftable, teleMinExtent_)) {
2013 t->kind = cdec::StageKind::SiblingMerge;
2014 t->dec = std::move(
m->dec);
2015 t->ownerOf = std::move(
m->ownerOf);
2016 t->groupOf = std::move(
m->groupOf);
2019 t = cdec::chooseStageTarget(curDec, peclet::core::IVec<3>{
gs.x,
gs.y,
gs.z}, liftable,
2020 teleMinExtent_, maxBlockCells);
2021 if (
t->kind != cdec::StageKind::SiblingMerge &&
t->kind != cdec::StageKind::Repartition)
2022 throw std::logic_error(
2023 "CutcellMG::initMpi: the telescope search found no merge (depth 0 always lifts "
2024 "under flow's rule)");
2031 std::shared_ptr<Telescope> makeTelescope(
const Level& v,
int L,
2032 const peclet::core::decomp::BlockDecomposer<3>& curDec,
2033 const peclet::core::decomp::StageTarget<3>& t,
2034 MPI_Comm curComm)
const {
2035 namespace cdec = peclet::core::decomp;
2036 auto T = std::make_shared<Telescope>();
2037 T->comm = cdec::makeStageComm(curComm, t);
2042 const int tb =
T->repartition() ?
T->comm.myTargetBlock :
T->comm.myGroup;
2044 const auto mb =
t.dec.block((std::size_t)tb);
2045 T->mOg = C3{(
int)mb.origin[0], (
int)mb.origin[1], (
int)mb.origin[2]};
2046 T->mInner = C3{(
int)mb.size[0], (
int)mb.size[1], (
int)mb.size[2]};
2047 T->mExt = C3{
T->mInner.x + 2 *
T->g,
T->mInner.y + 2 *
T->g,
T->mInner.z + 2 *
T->g};
2049 if (
T->repartition()) {
2054 for (
const int b :
T->comm.members) {
2055 const auto fb = curDec.block((std::size_t)b);
2056 T->memO.push_back(C3{(
int)fb.origin[0], (
int)fb.origin[1], (
int)fb.origin[2]});
2057 T->memS.push_back(C3{(
int)fb.size[0], (
int)fb.size[1], (
int)fb.size[2]});
2059 T->nMembers = (
int)
T->comm.members.size();
2064 const C3
lo =
v.og, le =
v.ext, mo =
T->mOg, me =
T->mExt;
2065 const int lg =
v.g, mg =
T->g;
2066 using peclet::core::Index;
2067 auto srcIdx = [&](
const peclet::core::IVec<3>&
c) {
2068 return (Index)(
c[0] -
lo.x + lg) + (Index)(
c[1] -
lo.y + lg) * le.x +
2069 (Index)(
c[2] -
lo.z + lg) * (Index)le.x * le.y;
2071 auto dstIdx = [&](
const peclet::core::IVec<3>&
c) {
2072 return (Index)(
c[0] - mo.x + mg) + (Index)(
c[1] - mo.y + mg) * me.x +
2073 (Index)(
c[2] - mo.z + mg) * (Index)me.x * me.y;
2075 T->move.build(curDec, t,
T->comm, srcIdx, dstIdx, L);
2076 T->back.assign(
v.n, 0.0);
2079 const std::size_t mn = (std::size_t)
T->mExt.x *
T->mExt.y *
T->mExt.z;
2095 fp32Last_ = (int)lv_.size() - 2;
2096#ifdef PECLET_FLOW_MPI
2098 for (
int L = 0; L < (int)lv_.size(); ++L)
2112 hasOutflow_ =
false;
2113 for (
int i = 0; i < 6; ++i) {
2129 B3 e{lv.ext.x, lv.ext.y, lv.ext.z};
2130 for (
int a = 0; a < 3; ++a)
2131 for (
int s = 0; s < 2; ++s)
2161 if (!hasBC_ || !bcGhost_)
2163 B3 e{lv.ext.x, lv.ext.y, lv.ext.z};
2164 for (
int a = 0; a < 3; ++a)
2165 for (
int s = 0; s < 2; ++s) {
2166 const int t = bc_[2 * a + s];
2174 const int a = f / 2;
2175 const int o = (a == 0) ? lv.og.x : (a == 1) ? lv.og.y : lv.og.z;
2176 const int n = (a == 0) ? lv.inner.x : (a == 1) ? lv.inner.y : lv.inner.z;
2177 const int gd = (a == 0) ? lv.gdim.x : (a == 1) ? lv.gdim.y : lv.gdim.z;
2178 return (f % 2 == 0) ? (o == 0) : (o + n == gd);
2186 CCField fo[3] = {fine->ox, fine->oy, fine->oz};
2199 B3 e{lv.ext.x, lv.ext.y, lv.ext.z};
2200 CCField oa[3] = {lv.ox, lv.oy, lv.oz};
2201 for (
int a = 0; a < 3; ++a)
2202 for (
int s = 0; s < 2; ++s) {
2203 const int t = bc_[2 * a + s];
2206 if (t == 1 || t == 2 || t == 4) {
2208 }
else if (t == 3) {
2209 if (!outflowCoeff_) {
2219 if (fo ==
nullptr) {
2231 CCField oa[3] = {lv.ox, lv.oy, lv.oz};
2232 int dims[3] = {lv.ext.x, lv.ext.y, lv.ext.z};
2233 for (
int a = 0; a < 3; ++a) {
2236 const int b = (a + 1) % 3, c = (a + 2) % 3;
2237 const std::size_t np = (std::size_t)dims[b] * dims[c];
2238 if (bcPlane_[a].extent(0) != np)
2240 Kokkos::view_alloc(
"peclet::flow::mg_bcplane", Kokkos::WithoutInitializing), np);
2251 lv.allFluid =
false;
2259 "mgfluidcount",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
2260 KOKKOS_LAMBDA(
int x,
int y,
int z,
long& k) {
2261 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
2267 lv.allFluid = cnt == (long)lv.inner.x * lv.inner.y * lv.inner.z;
2273 lv.nFluidDevValid =
false;
2275 if (distributed_ || lv.nFluidDev.data() ==
nullptr)
2281 "mgfluidcount",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
2282 KOKKOS_LAMBDA(
int x,
int y,
int z,
long& k) {
2283 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
2287 Kokkos::Sum<long, CCMem>(lv.nFluidDev));
2288 lv.nFluidDevValid =
true;
2294 return kHostMemory && !distributed_ && lv.nFluid >= 0 && lv.allFluid;
2302 const Level& f = lv_[0];
2303 return a == 0 ? f.ox : (a == 1 ? f.oy : f.oz);
2320 if (ox.data() != f.ox.data())
2321 Kokkos::deep_copy(f.ox, ox);
2322 if (oy.data() != f.oy.data())
2323 Kokkos::deep_copy(f.oy, oy);
2324 if (oz.data() != f.oz.data())
2325 Kokkos::deep_copy(f.oz, oz);
2348 f.ext,
G, idx2, idy2, idz2);
2351#ifdef PECLET_FLOW_MPI
2356 teleGather(lv_[0], lv_[0].ox, lv_[0].tele->ox);
2357 teleGather(lv_[0], lv_[0].oy, lv_[0].tele->oy);
2358 teleGather(lv_[0], lv_[0].oz, lv_[0].tele->oz);
2359 teleGatherOutflowPlanes(lv_[0]);
2362 for (
int L = 1; L < (int)lv_.size(); ++L) {
2364 Level& fin = lv_[L - 1];
2365#ifdef PECLET_FLOW_MPI
2371 Telescope& T = *fin.tele;
2373 c.g, T.g, c.inner, fin.
ratio);
2375 CCField so[3] = {T.ox, T.oy, T.oz};
2380 fin.
ext, c.g, fin.
g, c.inner, fin.
ratio);
2383#ifdef PECLET_FLOW_MPI
2386 const double sx = 1.0 / (double)(c.cfac.x * c.cfac.x),
2387 sy = 1.0 / (double)(c.cfac.y * c.cfac.y),
2388 sz = 1.0 / (double)(c.cfac.z * c.cfac.z);
2394 const bool deferC = defer && L + 1 < (int)lv_.size();
2405 c.ext, c.g == 2 ? c.g - 1 : c.g, idx2 * sx, idy2 * sy, idz2 * sz);
2409 c.nFluidDevValid =
false;
2414#ifdef PECLET_FLOW_MPI
2416 teleGather(c, c.ox, c.tele->ox);
2417 teleGather(c, c.oy, c.tele->oy);
2418 teleGather(c, c.oz, c.tele->oz);
2419 teleGatherOutflowPlanes(c);
2437 if (!bottomConnKnown_ && bottomStore_)
2439#ifdef PECLET_FLOW_MPI
2440 if (distributed_ &&
kHostMemory && !dirAgreedKnown_)
2441 agreeDirectBottom();
2456 double rtol,
int pre,
int post,
int bottom,
const StarOverlay* star =
nullptr,
2457 int nStar = 0,
C3 nnStar =
C3{0, 0, 0}) {
2459 return solvePCGAttempt(b, x, r, p, z, Ap, m, rtol, pre,
post, bottom, star, nStar, nnStar);
2470 template <
class Attempt>
2472 const int it = attempt(maxit);
2473 if (!(vprec_ ==
kVcycleAuto && lastVcycleFp32_ && !lastSolveConverged_ && attemptMaxit_ > 0))
2475 const char* why = solveFailed_ ?
"non-finite at iteration"
2476 : it >= attemptMaxit_ ?
"capped after"
2477 :
"degenerate direction at";
2478 lastDiscardedIters_ = it;
2481 fp32Latched_ =
true;
2483 Kokkos::deep_copy(
CCExec(), x, 0.0);
2491 m.guessHist_ = hist;
2492 m.zeroGuess_ = zero;
2494 } cold{*
this, guessV1_, guessHist_, zeroGuess_};
2498 const int it2 = attempt(maxit);
2500 std::fprintf(stderr,
2501 "peclet::flow CutcellMG: the FP32 V-cycle did not converge this solve (%s %d); "
2502 "re-solved with the FP64 V-cycle in %d iterations; 'auto' stays FP64 for the "
2503 "rest of the run (set_pressure_vcycle_precision re-arms)\n",
2511 lastVcycleFp32_ = f32;
2512 fp32FaultCount_ = 0;
2513 if (f32 && fp32FaultKind_ == 2)
2514 maxit = std::min(maxit, fp32FaultAt_);
2515 attemptMaxit_ = maxit;
2524 solveFailed_ =
true;
2526 "peclet::flow CutcellMG::%s: preconditioner produced non-finite z; "
2527 "returning zero correction (reported as %d/%d iterations, i.e. a CAPPED solve)\n",
2529 Kokkos::deep_copy(
CCExec(), x, 0.0);
2530 Kokkos::deep_copy(
CCExec(), l0.
x, x);
2532 throw std::runtime_error(std::string(
"peclet::flow CutcellMG::") + who +
2533 ": preconditioner produced non-finite z (set_pressure_strict)");
2540#ifdef PECLET_FLOW_MPI
2542 MPI_Comm_rank(comm_, &rank);
2551 Kokkos::deep_copy(
CCExec(), r, b);
2560 const double rref = stopRef_ > 0.0 ? stopRef_ : r0;
2569 if (meanRemovalAll_)
2571 return dot(l0, p, Ap);
2579 axpy(r, -alpha, Ap);
2585 Kokkos::deep_copy(
CCExec(), l0.
x, x);
2587 Kokkos::deep_copy(
CCExec(), x, l0.
x);
2592 if (!std::isfinite(pAp) || pAp <= 1e-300) {
2593 if (!std::isfinite(pAp))
2594 solveFailed_ =
true;
2601 bool cgConverged(
int& it,
double rn,
double r0,
double rtol,
double rref,
bool trace) {
2603 printf(
"[mg] it %3d |r|inf=%.6e r/r0=%.4e\n", it + 1, rn, rn / r0);
2604 if (rn < rtol * rref) {
2605 lastSolveConverged_ =
true;
2619 if (p.extent(0) != 0)
2620 Kokkos::deep_copy(
CCExec(), p, z);
2621#ifdef PECLET_FLOW_MPI
2622 return allreduce(
readSlot(k), MPI_SUM_, l0.comm);
2624 return allreduce(
readSlot(k), MPI_SUM_);
2629 if (p.extent(0) != 0)
2630 Kokkos::deep_copy(
CCExec(), p, z);
2634 if (p.extent(0) != 0)
2635 Kokkos::deep_copy(
CCExec(), p, z);
2636 return dot(l0, r, z);
2641 int nStar,
C3 nnStar) {
2643 return solvePCGResident(b, x, r, p, z, Ap, maxit, rtol, pre,
post, bottom, star, nStar,
2645 solveFailed_ =
false;
2646 lastSolveConverged_ =
false;
2652 Kokkos::deep_copy(
CCExec(), l0.
x, x);
2658 const double r0 = s0.r0;
2673 printf(
"[mg] solve %d: r0=%.6e rtol=%.1e (pre=%d post=%d bottom=%d)\n", dbgSolve_, r0, rtol,
2676 lastSolveConverged_ = r0 == 0.0;
2680 if (r0 > 0.0 && std::isfinite(r0)) {
2682 Kokkos::deep_copy(
CCExec(), z, 0.0);
2684 if (!std::isfinite(rz))
2686 for (; it < maxit; ++it) {
2687 const double pAp =
matvecPAp(l0, Ap, p, star, nStar, nnStar);
2690 const double alpha = rz / pAp;
2691 const double rn =
cgUpdate(l0, x, r, p, Ap, alpha, fuseK4);
2695 const double beta = rznew / rz;
2696 if (!std::isfinite(rznew)) {
2697 solveFailed_ =
true;
2706 guessTraceExit(l0, b, x, r0, s0.r0w, it, guessHist_, star, nStar, nnStar);
2750 Kokkos::View<double*, CCMem>
ks_;
2756 Kokkos::View<double*, PinnedHost>
kpk_;
2757 Kokkos::View<double*, PinnedHost>
kgp_;
2758 Kokkos::View<double, PinnedHost>
krd_;
2759 Kokkos::View<double, PinnedHost>
kcz_;
2763 ks_ = Kokkos::View<double*, CCMem>(
"peclet::flow::mg_krylov_scalars",
kNSlots);
2764 kcnt_ = Kokkos::View<long, CCMem>(
"peclet::flow::mg_mean_count");
2765 kpk_ = Kokkos::View<double*, PinnedHost>(
"peclet::flow::mg_krylov_packet",
kPacket);
2766 kgp_ = Kokkos::View<double*, PinnedHost>(
"peclet::flow::mg_guess_packet",
kGuessPacket);
2767 krd_ = Kokkos::View<double, PinnedHost>(
"peclet::flow::mg_slot_read");
2768 kcz_ = Kokkos::View<double, PinnedHost>(
"peclet::flow::mg_cheb_rz");
2770 Kokkos::View<double, CCMem>
slot(
int k)
const {
2771 return Kokkos::View<double, CCMem>(
ks_.data() + k);
2775 CCExec().fence(
"peclet::flow::mg_read_slot");
2788 "mgdot",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
2789 KOKKOS_LAMBDA(
int x,
int y,
int z,
double& acc) {
2790 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
2791 acc += aa(i) * bb(i);
2797 "mgdot",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
2798 KOKKOS_LAMBDA(
int x,
int y,
int z,
double& acc) {
2799 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
2801 acc += aa(i) * bb(i);
2813 Kokkos::deep_copy(
CCExec(), p, z);
2819 CCField pp = p, rr = r, zz = z;
2823 "mgdot",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
2824 KOKKOS_LAMBDA(
int x,
int y,
int zc,
double& acc) {
2825 const long i = (long)x + (
long)y * e.x + (long)zc * (
long)e.x * e.y;
2827 if (all || ac(i) > 1e-30f)
2828 acc += rr(i) * zz(i);
2846 const Level& l0 = lv_[0];
2853 if (!exactResidual_)
2861 if (exactResidual_) {
2863 const double gfx = gfx_, gfy = gfy_, gfz = gfz_;
2865 "peclet::flow::cc_apply_exact_dot",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
2866 KOKKOS_LAMBDA(
int lx,
int ly,
int lz,
double& acc) {
2867 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
2868 const long i = (long)lx + (
long)ly * sy + (long)lz * sz;
2870 const double a =
cutcellApplyExactCell(vv, ox, oy, oz, i, sx, sy, sz, w.xp, w.xm, w.yp,
2871 w.ym, w.zp, w.zm, gfx, gfy, gfz);
2873 if (all || ac(i) > 1e-30f)
2881 "peclet::flow::cc_apply_dot",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
2882 KOKKOS_LAMBDA(
int lx,
int ly,
int lz,
double& acc) {
2883 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
2884 const long i = (long)lx + (
long)ly * sy + (long)lz * sz;
2886 const double a =
cutcellApplyFaceCell(vv,
AC, AFX, AFY, AFZ, i, sx, sy, sz, w.xp, w.xm,
2887 w.yp, w.ym, w.zp, w.zm);
2889 if (all || ac(i) > 1e-30f)
2902 const C3 e = l0.
ext;
2904 CCField xx = x, rr = r, pp = p, aa = Ap;
2909 "mgpcg_update_sum",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
2910 KOKKOS_LAMBDA(
int x_,
int y,
int z,
double& s) {
2911 if (ks(
kStop) != 0.0)
2913 const long i = (long)x_ + (
long)y * e.x + (long)z * (
long)e.x * e.y;
2914 const double a = ks(
kAlpha), na = -a;
2916 rr(i) += na * aa(i);
2917 if (all || ac(i) > 1e-30f)
2920 Kokkos::Sum<double, CCMem>(
slot(
kMsum)));
2926 const C3 e = lv.ext;
2930 const bool all = lv.allFluid;
2931 const long cnt = lv.nFluid;
2934 "mgmeans_max",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
2935 KOKKOS_LAMBDA(
int x,
int y,
int z,
double& m) {
2936 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
2937 if (!(cnt == 0 || ks(
kStop) != 0.0)) {
2938 const double mean = ks(
kMsum) / (double)cnt;
2944 if (all || ac(i) > 1e-30f) {
2945 const double v = Kokkos::fabs(ff(i));
2950 Kokkos::Max<double, CCMem>(
slot(k)));
2959 "mgmax",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
2960 KOKKOS_LAMBDA(
int x,
int y,
int z,
double& acc) {
2961 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
2962 const double v = Kokkos::fabs(aa(i));
2966 Kokkos::Max<double, CCMem>(
slot(k)));
2970 "mgmax",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
2971 KOKKOS_LAMBDA(
int x,
int y,
int z,
double& acc) {
2972 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
2973 if (ac(i) > 1e-30f) {
2974 const double v = Kokkos::fabs(aa(i));
2979 Kokkos::Max<double, CCMem>(
slot(k)));
2985 dbgBrkWhich_ = which;
2995 static_assert(ND == 2 || ND == 3,
"matvecGuessDots: two or three dots");
2996 if (!exactResidual_)
3001 CCConst vv = v, ww = w, rr = r;
3004 const bool exact = exactResidual_;
3006 const double gfx = gfx_, gfy = gfy_, gfz = gfz_;
3008 auto cell = KOKKOS_LAMBDA(
int lx,
int ly,
int lz,
long& i)->double {
3009 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
3010 i = (long)lx + (
long)ly * sy + (long)lz * sz;
3012 const double a = exact ?
cutcellApplyExactCell(vv, ox, oy, oz, i, sx, sy, sz, nb.xp, nb.xm,
3013 nb.yp, nb.ym, nb.zp, nb.zm, gfx, gfy, gfz)
3014 :
cutcellApplyFaceCell(vv,
AC, AFX, AFY, AFZ, i, sx, sy, sz, nb.xp,
3015 nb.xm, nb.yp, nb.ym, nb.zp, nb.zm);
3019 if constexpr (ND == 2) {
3021 "peclet::flow::mg_guess_apply_dots",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
3022 KOKKOS_LAMBDA(
int lx,
int ly,
int lz,
double& svy,
double& svr) {
3024 const double a = cell(lx, ly, lz, i);
3025 if (all || ac(i) > 1e-30f) {
3027 svr += vv(i) * rr(i);
3030 Kokkos::Sum<double, CCMem>(
slot(kvy)), Kokkos::Sum<double, CCMem>(
slot(kvr)));
3033 "peclet::flow::mg_guess_apply_dots",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
3034 KOKKOS_LAMBDA(
int lx,
int ly,
int lz,
double& svy,
double& svr,
double& swy) {
3036 const double a = cell(lx, ly, lz, i);
3037 if (all || ac(i) > 1e-30f) {
3039 svr += vv(i) * rr(i);
3043 Kokkos::Sum<double, CCMem>(
slot(kvy)), Kokkos::Sum<double, CCMem>(
slot(kvr)),
3044 Kokkos::Sum<double, CCMem>(
slot(kwy)));
3066 const StarOverlay* star,
int nStar,
C3 nnStar,
bool fused,
double* r0w) {
3069 const int hist = guessHist_;
3071 Kokkos::deep_copy(
CCExec(), r, b);
3092 CCExec().fence(
"peclet::flow::mg_guess_packet");
3093#ifdef PECLET_FLOW_MPI
3095 const auto t0 = std::chrono::steady_clock::now();
3096 const MPI_Comm c = l0.comm == nullptr_comm() ? comm_ : l0.comm;
3097 MPI_Allreduce(MPI_IN_PLACE,
kgp_.data() + 1,
kGuessPacket - 1, MPI_DOUBLE, MPI_SUM, c);
3098 MPI_Allreduce(MPI_IN_PLACE,
kgp_.data(), 1, MPI_DOUBLE, MPI_MAX, c);
3100 std::chrono::duration<double>(std::chrono::steady_clock::now() - t0).count();
3101 allreduceCount_ += 2;
3106 double c1 = 0.0, c2 = 0.0;
3107 if (G11 > 0.0 && std::isfinite(G11)) {
3108 const double l11 = std::sqrt(G11);
3109 double l21 = 0.0, d = 0.0;
3112 d = G22 - l21 * l21;
3114 if (hist < 2 || !(d > 1e-10 * G22)) {
3118 const double y1 = g1 / l11, y2 = (g2 - l21 * y1) / std::sqrt(d);
3119 c2 = y2 / std::sqrt(d);
3120 c1 = (y1 - l21 * c2) / l11;
3122 if (!std::isfinite(c1) || !std::isfinite(c2))
3127 if (c1 == 0.0 && c2 == 0.0) {
3128 Kokkos::deep_copy(
CCExec(), x, 0.0);
3134 const C3 e = l0.
ext;
3137 CCConst vv1 = v1, a1 = av1, a2 = av2;
3140 const bool all =
hostAllFluid(l0), two = hist == 2 && c2 != 0.0;
3142 "peclet::flow::mg_guess_x",
C3{0, 0, 0},
C3{e.x, e.y, e.z},
3143 KOKKOS_LAMBDA(
int ix,
int iy,
int iz) {
3144 const long i = (long)ix + (
long)iy * e.x + (long)iz * (
long)e.x * e.y;
3145 if (ix < g || iy < g || iz < g || ix >= e.x - g || iy >= e.y - g || iz >= e.z - g) {
3149 const bool fl = all || ac(i) > 1e-30f;
3150 xx(i) = fl ? (two ? c1 * vv1(i) + c2 * xx(i) : c1 * vv1(i)) : 0.0;
3151 rr(i) -= two ? c1 * a1(i) + c2 * a2(i) : c1 * a1(i);
3164 if (guessX0_.extent(0) != x.extent(0)) {
3165 guessX0_ =
CCField(
"peclet::flow::mg_guess_x0", x.extent(0));
3166 guessTA_ =
CCField(
"peclet::flow::mg_guess_ta", x.extent(0));
3167 guessTB_ =
CCField(
"peclet::flow::mg_guess_tb", x.extent(0));
3169 Kokkos::deep_copy(
CCExec(), guessX0_, x);
3174 CCField ta = guessTA_, tb = guessTB_, x0 = guessX0_, xx = x;
3175 Kokkos::parallel_for(
3176 "peclet::flow::mg_guess_trace_diff", Kokkos::RangePolicy<CCExec>(
CCExec(), 0, x.extent(0)),
3177 KOKKOS_LAMBDA(std::size_t i) { ta(i) = xx(i) - x0(i); });
3179 const double eA2 =
dot(l0, ta, tb);
3180 Kokkos::deep_copy(
CCExec(), ta, x);
3182 const double xA2 =
dot(l0, ta, tb);
3183 Kokkos::deep_copy(
CCExec(), ta, b);
3186 const double rfin =
maxabs(l0, ta);
3189 const C3 e = l0.
ext;
3193 "peclet::flow::mg_guess_trace_nonfluid",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
3194 KOKKOS_LAMBDA(
int ix,
int iy,
int iz,
long& k) {
3195 const long i = (long)ix + (
long)iy * e.x + (long)iz * (
long)e.x * e.y;
3196 if (!(ac(i) > 1e-30f) && x0(i) != 0.0)
3202#ifdef PECLET_FLOW_MPI
3204 bad = (long)allreduce((
double)bad, MPI_SUM_, l0.comm);
3209 "[mg] guess solve %d: hist %d c1 %.17g c2 %.17g rref %.6e r0w %.6e iters %d "
3210 "eA %.6e xA %.6e eA/xA %.6e resid/rref %.6e x0_nonfluid_nonzero %ld\n",
3211 dbgSolve_ - 1, hist, guessC1_, guessC2_, rref, r0w, it, std::sqrt(std::max(eA2, 0.0)),
3212 std::sqrt(std::max(xA2, 0.0)), xA2 > 0.0 ? std::sqrt(std::max(eA2, 0.0) / xA2) : 0.0,
3213 rref > 0.0 ? rfin / rref : 0.0, bad);
3227 Kokkos::deep_copy(
CCExec(), r, b);
3230 Kokkos::deep_copy(
CCExec(), r, b);
3248 const bool guessOn = guessV1_.extent(0) > 0;
3250 double r0 = 0.0, r0w = 0.0;
3251 if (guessOn && guessHist_ > 0) {
3252 r0 =
projectedGuess(l0, b, x, r, Ap, z, star, nStar, nnStar, fused,
3253 traceGuess ? &r0w :
nullptr);
3267 if (fuse && !meanRemovalAll_) {
3271 if (meanRemovalAll_)
3280 const double ratio = rzNew != 0.0 ? std::abs(rzOld / rzNew) : 0.0;
3281 healthLog_.push_back(ratio);
3283 printf(
"[mg] it %3d health |r^T z_k| / |r^T z_{k+1}| = %.3e%s\n", it + 1, ratio,
3284 f32 ?
" (fp32 V-cycle)" :
"");
3307 else if (!copyFused)
3308 Kokkos::deep_copy(
CCExec(), p, z);
3314 if (dbgBrkWhich_ == 1 && it == dbgBrkIter_)
3315 setSlot(
kPAp, std::numeric_limits<double>::quiet_NaN());
3317 const double brkPAp =
kBrkPAp;
3318 Kokkos::parallel_for(
3319 "mgpcg_alpha", Kokkos::RangePolicy<CCExec>(
CCExec(), 0, 1), KOKKOS_LAMBDA(
int) {
3320 const double pAp = ks(
kPAp);
3321 if (ks(
kStop) == 0.0) {
3322 if (Kokkos::isfinite(pAp) && pAp > 1e-300)
3338 const std::size_t n = xx.extent(0);
3339 Kokkos::parallel_for(
3340 "mgpcg_update", Kokkos::RangePolicy<CCExec>(
CCExec(), 0, n),
3341 KOKKOS_LAMBDA(std::size_t i) {
3342 if (ks(
kStop) != 0.0)
3344 const double a = ks(
kAlpha), na = -a;
3346 rr(i) += na * aa(i);
3357 CCExec().fence(
"peclet::flow::mg_packet");
3358 const double pAp =
kpk_(0), stop =
kpk_(2);
3362 solveFailed_ =
true;
3367 if (!std::isfinite(pAp))
3368 solveFailed_ =
true;
3371 return cgConverged(it, rn, r0, rtol, rref, trace);
3377 bool health,
bool trace) {
3378 const double rzOld = health ?
dot(l0, r, z) : 0.0;
3383 if (dbgBrkWhich_ == 2 && it == dbgBrkIter_)
3390 const double brkRz =
kBrkRz;
3391 Kokkos::parallel_for(
3392 "mgpcg_beta", Kokkos::RangePolicy<CCExec>(
CCExec(), 0, 1), KOKKOS_LAMBDA(
int) {
3393 const double rznew = ks(
kRzNew);
3394 if (Kokkos::isfinite(rznew)) {
3401 Kokkos::parallel_for(
3402 "mgaypx", Kokkos::RangePolicy<CCExec>(
CCExec(), 0, n), KOKKOS_LAMBDA(std::size_t i) {
3403 if (ks(
kStop) != 0.0)
3405 pp(i) = zz(i) + ks(
kBeta) * pp(i);
3410 int nStar,
C3 nnStar) {
3411 solveFailed_ =
false;
3412 lastSolveConverged_ =
false;
3425 const double r0 = s0.r0;
3432 printf(
"[mg] solve %d: r0=%.6e rtol=%.1e (pre=%d post=%d bottom=%d)\n", dbgSolve_, r0, rtol,
3436 struct ExitDotScope {
3438 ~ExitDotScope() { d =
ExitDot{}; }
3446 const bool health = trace || healthTrace_;
3448 lastSolveConverged_ = r0 == 0.0;
3449 if (r0 > 0.0 && std::isfinite(r0)) {
3450 const double rz0 =
firstPrecondRz(l0, f32, fuseC, std::ilogb(r0), z, r, p);
3451 if (!std::isfinite(rz0))
3454 const std::size_t n = x.extent(0);
3455 bool exited =
false;
3456 for (; it < maxit; ++it) {
3469 solveFailed_ =
true;
3475 guessTraceExit(l0, b, x, r0, s0.r0w, it, guessHist_, star, nStar, nnStar);
3500 int maxit,
double rtol,
int pre,
int post,
int bottom,
3501 const StarOverlay* star =
nullptr,
int nStar = 0,
C3 nnStar =
C3{0, 0, 0}) {
3503 return solveFCGAttempt(b, x, r, p, z, zp, Ap, m, rtol, pre,
post, bottom, star, nStar,
3513 if (p.extent(0) != 0)
3514 Kokkos::deep_copy(
CCExec(), p, z);
3521 if (p.extent(0) != 0)
3522 Kokkos::deep_copy(
CCExec(), p, z);
3523 return dot(l0, r, z);
3532 void fcgNotePr(
int it,
double beta,
double rznew,
double rzcross,
bool trace) {
3534 printf(
"[mg] it %3d beta=%.6e pr=%.3e (|r^T z_k| / |r^T z_{k+1}|)\n", it + 1, beta,
3535 rznew != 0.0 ? std::abs(rzcross / rznew) : 0.0);
3537 healthLog_.push_back(rznew != 0.0 ? std::abs(rzcross / rznew) : 0.0);
3541 int maxit,
double rtol,
int pre,
int post,
int bottom,
3543 solveFailed_ =
false;
3544 lastSolveConverged_ =
false;
3550 Kokkos::deep_copy(
CCExec(), l0.
x, x);
3555 const bool fuseC4 = fuseC23 && !f32;
3565 const double r0 = s0.r0;
3569 printf(
"[mg] fcg solve %d: r0=%.6e rtol=%.1e (pre=%d post=%d bottom=%d)\n", dbgSolve_, r0,
3570 rtol, pre,
post, bottom);
3572 lastSolveConverged_ = r0 == 0.0;
3573 if (r0 > 0.0 && std::isfinite(r0)) {
3574 if (f32 && distributed_)
3575 Kokkos::deep_copy(
CCExec(), z, 0.0);
3576 double rz =
fcgPrecondRz(l0, f32, fuseC4, z, r, r0, p);
3577 if (!std::isfinite(rz))
3579 for (; it < maxit; ++it) {
3580 const double pAp =
matvecPAp(l0, Ap, p, star, nStar, nnStar);
3583 const double alpha = rz / pAp;
3584 const double rn =
cgUpdate(l0, x, r, p, Ap, alpha, fuseC23);
3587 Kokkos::deep_copy(
CCExec(), zp, z);
3589 const double rzcross =
dot(l0, r, zp);
3590 if (!std::isfinite(rznew) || !std::isfinite(rzcross)) {
3591 solveFailed_ =
true;
3594 const double beta = (rznew - rzcross) / rz;
3595 fcgNotePr(it, beta, rznew, rzcross, trace);
3602 guessTraceExit(l0, b, x, r0, s0.r0w, it, guessHist_, star, nStar, nnStar);
3611#ifdef PECLET_FLOW_MPI
3613 CCField xg2, GridHalo<double>* h2,
C3 ext2
3616#ifdef PECLET_FLOW_MPI
3617 if (distributed_ && h2) {
3618 stageG2(l0, q, xg2, ext2);
3620 unstageG2(l0, q, xg2);
3637 Kokkos::deep_copy(
CCExec(), l0.
rhs, rr);
3638 Kokkos::deep_copy(
CCExec(), l0.
x, 0.0);
3640 Kokkos::deep_copy(
CCExec(), zz, l0.
x);
3647 void noteBicgExit(
int exitCode,
int it,
int maxit,
double r0n,
double rnLast,
double rtol) {
3648 if (exitCode >= 1 && exitCode <= 5)
3649 ++dbgBicgBreakdowns_;
3650 else if (exitCode == 6)
3651 ++dbgBicgStagnations_;
3654 "[mg] bicgstab solve %d: exit=%d it=%d maxit=%d r0=%.6e |r|inf=%.6e rtol=%.1e "
3655 "r/r0=%.4e breakdowns=%d stagnations=%d\n",
3656 dbgSolve_, exitCode, it, maxit, r0n, rnLast, rtol, r0n > 0.0 ? rnLast / r0n : 0.0,
3657 dbgBicgBreakdowns_, dbgBicgStagnations_);
3676#ifdef PECLET_FLOW_MPI
3681 solveFailed_ =
false;
3682 OverlayScope overlay(overlaySolve_,
true);
3689#ifdef PECLET_FLOW_MPI
3690 gpMatvec(l0, y, q, ov, nOv, nn, xg2, h2, ext2);
3696 Kokkos::deep_copy(
CCExec(), r, b);
3699 Kokkos::deep_copy(
CCExec(), rh, r);
3700 const double r0n =
maxabs(l0, r);
3705 int exitCode = r0n > 0.0 ? (std::isfinite(r0n) ? 7 : 3) : 0;
3706 double rnLast = r0n;
3707 if (r0n > 0.0 && std::isfinite(r0n)) {
3708 double rho = 1.0, alpha = 1.0, omega = 1.0;
3710 int lastImprove = 0;
3711 Kokkos::deep_copy(
CCExec(), p, 0.0);
3712 Kokkos::deep_copy(
CCExec(), v, 0.0);
3713 for (; it < maxit; ++it) {
3714 const double rhoNew =
dot(l0, rh, r);
3715 if (!std::isfinite(rhoNew) || std::fabs(rhoNew) < 1e-300) {
3719 const double beta = (rhoNew / rho) * (alpha / omega);
3726 const double rhv =
dot(l0, rh, v);
3727 if (!std::isfinite(rhv) || std::fabs(rhv) < 1e-300) {
3734 double rn =
maxabs(l0, r);
3736 if (!std::isfinite(rn)) {
3740 if (rn < rtol * r0n) {
3749 const double tt =
dot(l0, t, t);
3750 if (!std::isfinite(tt) || tt < 1e-300) {
3755 omega =
dot(l0, t, r) / tt;
3756 if (!std::isfinite(omega) || std::fabs(omega) < 1e-300) {
3767 if (!std::isfinite(rn)) {
3771 if (rn < rtol * r0n) {
3776 if (rn < 0.999 * best) {
3779 }
else if (it - lastImprove > 30) {
3797#ifdef PECLET_FLOW_MPI
3806 Telescope&
T = *
lv.tele;
3807 auto hs = Kokkos::create_mirror_view(src);
3808 Kokkos::deep_copy(hs, src);
3810 T.move.forward({
hs.data()}, {});
3813 auto hd = Kokkos::create_mirror_view(dst);
3814 T.move.forward({
hs.data()}, {hd.data()});
3815 Kokkos::deep_copy(dst, hd);
3822 void teleGatherOutflowPlanes(
const Level& lv) {
3827 for (
int a = 0;
a < 3; ++
a)
3828 if (bc_[2 * a + 1] == 3) {
3829 if (
lv.tele->repartition())
3830 teleGatherPlaneRepartition(lv, src[a], dst[a], a);
3832 teleGatherPlane(lv, src[a], dst[a], a);
3843 Telescope&
T = *
lv.tele;
3844 using Blk = peclet::core::decomp::Block<3>;
3845 const int b = (
a + 1) % 3, c = (a + 2) % 3;
3846 const long gd[3] = {
lv.gdim.x,
lv.gdim.y,
lv.gdim.z};
3847 int rank = 0, np = 1;
3848 MPI_Comm_rank(
T.comm.parent, &rank);
3849 MPI_Comm_size(
T.comm.parent, &np);
3850 auto touches = [&](
const Blk&
k) {
return (
long)(
k.origin[
a] +
k.size[
a]) == gd[a]; };
3852 auto overlap = [&](
const Blk&
p,
const Blk&
q,
long lo[2],
long hi[2]) {
3853 const int ax[2] = {
b,
c};
3854 for (
int e = 0;
e < 2; ++
e) {
3855 lo[
e] = (long)std::max(
p.origin[ax[e]],
q.origin[ax[e]]);
3856 hi[
e] = (long)std::min(
p.origin[ax[e]] +
p.size[ax[e]],
q.origin[ax[e]] +
q.size[ax[e]]);
3862 std::vector<int>
sc((std::size_t)np, 0), sd((std::size_t)np, 0),
rc((std::size_t)np, 0),
3863 rd((std::size_t)np, 0);
3864 std::vector<double> sb,
rb;
3865 const Blk me =
T.srcDec.block((std::size_t)rank);
3868 const int ext[3] = {
lv.ext.x,
lv.ext.y,
lv.ext.z};
3869 const long st[3] = {1, (long)
lv.ext.x, (
long)
lv.ext.x *
lv.ext.y};
3870 auto hs = Kokkos::create_mirror_view(src);
3871 Kokkos::deep_copy(hs, src);
3872 const long pa = (long)(ext[a] - g) *
st[
a];
3873 for (std::size_t t = 0;
t <
T.dstDec.numBlocks(); ++
t) {
3875 const Blk
tb =
T.dstDec.block(t);
3876 if (!touches(tb) || !overlap(me, tb, lo, hi))
3878 sd[
t] = (
int)sb.size();
3879 for (
long k = lo[1];
k < hi[1]; ++
k)
3880 for (
long j = lo[0];
j < hi[0]; ++
j)
3881 sb.push_back(
hs(pa + (j - (
long)me.origin[b] + g) * st[b] +
3882 (k - (
long)me.origin[c] + g) * st[c]));
3883 sc[
t] = (
int)sb.size() - sd[
t];
3886 const bool land =
T.root() && touches(
T.dstDec.block((std::size_t)rank));
3888 const Blk
tb =
T.dstDec.block((std::size_t)rank);
3890 for (std::size_t s2 = 0;
s2 <
T.srcDec.numBlocks(); ++
s2) {
3892 const Blk sk =
T.srcDec.block(s2);
3893 if (!touches(sk) || !overlap(sk, tb, lo, hi))
3896 rc[
s2] = (
int)((hi[0] - lo[0]) * (hi[1] - lo[1]));
3899 rb.resize((std::size_t)n);
3901 MPI_Alltoallv(sb.data(),
sc.data(), sd.data(), MPI_DOUBLE,
rb.data(),
rc.data(), rd.data(),
3902 MPI_DOUBLE,
T.comm.parent);
3905 const Blk
tb =
T.dstDec.block((std::size_t)rank);
3906 const long mst[3] = {1, (long)
T.mExt.x, (
long)
T.mExt.x *
T.mExt.y};
3907 const int mext[3] = {
T.mExt.x,
T.mExt.y,
T.mExt.z};
3908 auto hd = Kokkos::create_mirror_view(dst);
3909 Kokkos::deep_copy(hd, dst);
3910 const long pa = (long)(mext[a] -
T.g) * mst[
a];
3911 for (std::size_t s2 = 0;
s2 <
T.srcDec.numBlocks(); ++
s2) {
3915 overlap(
T.srcDec.block(s2), tb, lo, hi);
3916 const double*
q =
rb.data() + rd[
s2];
3917 for (
long k = lo[1];
k < hi[1]; ++
k)
3918 for (
long j = lo[0];
j < hi[0]; ++
j)
3919 hd(pa + (j - (
long)
tb.origin[b] +
T.g) * mst[b] +
3920 (k - (
long)
tb.origin[c] +
T.g) * mst[c]) = *
q++;
3922 Kokkos::deep_copy(dst, hd);
3925 Telescope&
T = *
lv.tele;
3926 const int g =
lv.g,
b = (
a + 1) % 3, c = (a + 2) % 3;
3927 const int ext[3] = {
lv.ext.x,
lv.ext.y,
lv.ext.z},
3928 inn[3] = {
lv.inner.x,
lv.inner.y,
lv.inner.z};
3929 const long st[3] = {1, (long)
lv.ext.x, (
long)
lv.ext.x *
lv.ext.y};
3932 std::vector<double> sb;
3934 auto hs = Kokkos::create_mirror_view(src);
3935 Kokkos::deep_copy(hs, src);
3936 sb.resize((std::size_t)inn[b] * inn[c]);
3937 const long pa = (long)(ext[a] - g) *
st[
a];
3938 for (
int k = 0;
k < inn[
c]; ++
k)
3939 for (
int j = 0;
j < inn[
b]; ++
j)
3940 sb[(std::size_t)
j + (std::size_t)k * inn[b]] =
3941 hs(pa + (
long)(j + g) * st[b] + (
long)(k + g) * st[c]);
3944 std::vector<int> counts, displs;
3945 std::vector<double>
rb;
3946 const int gd[3] = {
lv.gdim.x,
lv.gdim.y,
lv.gdim.z};
3949 for (
int m = 0;
m <
T.nMembers; ++
m) {
3950 const int mo[3] = {
T.memO[
m].x,
T.memO[
m].y,
T.memO[
m].z};
3951 const int ms[3] = {
T.memS[
m].x,
T.memS[
m].y,
T.memS[
m].z};
3952 const int n = (mo[
a] +
ms[
a] == gd[
a]) ? ms[b] * ms[c] : 0;
3953 counts.push_back(n);
3954 displs.push_back(disp);
3957 rb.resize((std::size_t)disp);
3959 MPI_Gatherv(sb.data(), (
int)sb.size(), MPI_DOUBLE,
T.root() ?
rb.data() :
nullptr,
3960 T.root() ? counts.data() :
nullptr,
T.root() ? displs.data() :
nullptr, MPI_DOUBLE,
3964 const int mext[3] = {
T.mExt.x,
T.mExt.y,
T.mExt.z};
3965 const long mst[3] = {1, (long)
T.mExt.x, (
long)
T.mExt.x *
T.mExt.y};
3966 const int mog[3] = {
T.mOg.x,
T.mOg.y,
T.mOg.z};
3967 auto hd = Kokkos::create_mirror_view(dst);
3968 Kokkos::deep_copy(hd, dst);
3969 const long pa = (long)(mext[a] -
T.g) * mst[
a];
3970 for (
int m = 0;
m <
T.nMembers; ++
m) {
3973 const int mo[3] = {
T.memO[
m].x,
T.memO[
m].y,
T.memO[
m].z};
3974 const int ms[3] = {
T.memS[
m].x,
T.memS[
m].y,
T.memS[
m].z};
3975 const double*
q =
rb.data() + displs[
m];
3976 for (
int k = 0;
k <
ms[
c]; ++
k)
3977 for (
int j = 0;
j <
ms[
b]; ++
j)
3978 hd(pa + (
long)(mo[b] - mog[b] + j +
T.g) * mst[b] +
3979 (
long)(mo[c] - mog[c] + k +
T.g) * mst[c]) =
3980 q[(std::size_t)j + (std::size_t)
k *
ms[
b]];
3982 Kokkos::deep_copy(dst, hd);
3985 Telescope&
T = *
lv.tele;
3988 auto hs = Kokkos::create_mirror_view(src);
3989 Kokkos::deep_copy(hs, src);
3990 T.move.backward({
hs.data()}, {
T.back.data()});
3992 T.move.backward({}, {
T.back.data()});
3994 auto hd = Kokkos::create_mirror_view(dst);
3995 Kokkos::deep_copy(hd, dst);
3996 for (
int k = 0;
k <
lv.inner.z; ++
k)
3997 for (
int j = 0;
j <
lv.inner.y; ++
j)
3998 for (
int i = 0;
i <
lv.inner.x; ++
i) {
4000 (long)(i + g) + (long)(j + g) *
lv.ext.x + (long)(k + g) * (long)
lv.ext.x *
lv.ext.y;
4001 hd(
id) +=
T.back[(std::size_t)
id];
4003 Kokkos::deep_copy(dst, hd);
4013 assert(!l0.
caOk &&
"A5: level 0 must not exchange (write) its rhs");
4021 } restore{l0, l0.
rhs, l0.
x};
4024 Kokkos::deep_copy(
CCExec(), zz, 0.0);
4029 lastVcycleFp32_ =
false;
4032 throw std::runtime_error(
4033 std::string(
"set_pressure_vcycle_precision('fp32'): not eligible here: ") +
4040 const auto t0 = std::chrono::steady_clock::now();
4042 lvTime_.resize(lv_.size(), 0.0);
4043 lvTime_[L] += std::chrono::duration<double>(std::chrono::steady_clock::now() - t0).count();
4044 if (L == 0 && ++lvCycles_ % 50 == 0) {
4046 for (std::size_t i = 0; i < lvTime_.size(); ++i)
4047 tot += (i + 1 < lvTime_.size() ? lvTime_[i] - lvTime_[i + 1] : lvTime_[i]);
4048 printf(
"[mg] level times over %d V-cycles (total %.3f s):\n", lvCycles_, tot);
4049 for (std::size_t i = 0; i < lvTime_.size(); ++i) {
4050 const double self = (i + 1 < lvTime_.size() ? lvTime_[i] - lvTime_[i + 1] : lvTime_[i]);
4051 printf(
"[mg] L%zu %5dx%5dx%5d self %7.3f s (%5.1f%%)\n", i, lv_[i].inner.x,
4052 lv_[i].inner.y, lv_[i].inner.z, self, 100.0 * self / (tot + 1e-30));
4065 bool isBottom = (L + 1 == (int)lv_.size());
4066#ifdef PECLET_FLOW_MPI
4073 throw std::runtime_error(
4074 std::string(
"set_pressure_bottom_solver('direct'): not eligible here: ") + why);
4082 }
else if (symVcycle_ && bottom_ > 0) {
4085 smooth(lv, std::max(1, bottom_ / 2),
false);
4086 smooth(lv, std::max(1, bottom_ - bottom_ / 2),
true);
4088 smooth(lv, bottom_,
false);
4089 if (meanRemovalAll_)
4102 auto fullResidual = [&] {
4104 FPC(lv.AFY),
FPC(lv.AFZ), lv.ext, lv.g);
4106#ifdef PECLET_FLOW_MPI
4109 const int m = stencilWrap(lv);
4113 FPC(lv.AFY),
FPC(lv.AFZ), lv.ext, lo, hi,
C3{0, 0, 0},
C3{0, 0, 0},
4116 GridHalo<double>& h = stencilHalo(lv);
4117 h.exchangeBegin(lv.x);
4119 FPC(lv.AFY),
FPC(lv.AFZ), lv.ext, lo, hi,
C3{0, 0, 0},
C3{0, 0, 0},
4121 h.exchangeEnd(lv.x);
4124 FPC(lv.AFY),
FPC(lv.AFZ), lv.ext,
C3{g, g, g},
4125 C3{lv.ext.x - g, lv.ext.y - g, lv.ext.z - g}, lo, hi, m, g);
4132 FPC(lv.AFY),
FPC(lv.AFZ), lv.ext, lv.inner, lv.g);
4139#ifdef PECLET_FLOW_MPI
4141 teleTransition(L, sym, lv.res, lv.x);
4145 Level& cs = lv_[L + 1];
4146 if (!distributed_) {
4150 restrictAvg(cs.rhs,
CCConst(lv.res), cs.ext, lv.ext, cs.g, lv.g, cs.inner, lv.ratio);
4151 Kokkos::deep_copy(
CCExec(), cs.x, 0.0);
4166 prolongAdd(lv.x,
CCConst(cs.x), lv.ext, cs.ext, lv.g, cs.g, lv.inner, lv.ratio);
4180 if (meanRemovalAll_ || L == 0)
4183#ifdef PECLET_FLOW_MPI
4190 void teleTransition(
int L,
bool sym,
CCField src,
CCField target) {
4192 Telescope& T = *lv.tele;
4193 teleGather(lv, src, T.res);
4195 Level& cs = lv_[L + 1];
4197 Kokkos::deep_copy(
CCExec(), cs.x, 0.0);
4200 Kokkos::deep_copy(
CCExec(), T.x, 0.0);
4206 Kokkos::deep_copy(
CCExec(), T.x, 0.0);
4210 teleScatterAdd(lv,
T.x, target);
4216 bool caSmooth(
const Level& lv)
const {
return distributed_ && lv.caOk && !hasBC_; }
4221 return {(m & 1) ? g : g + 1, (m & 2) ? g : g + 1, (m & 4) ? g : g + 1};
4224 return {(m & 1) ? e.x - g : e.x - g - 1, (m & 2) ? e.y - g : e.y - g - 1,
4225 (m & 4) ? e.z - g : e.z - g - 1};
4227#ifdef PECLET_FLOW_MPI
4232 bool stencilHaloOn(
const Level& lv)
const {
4233 return lv.sdev && !overlaySolve_ && !stencilHaloOff_;
4235 int stencilWrap(
const Level& lv)
const {
return stencilHaloOn(lv) ? lv.wrapAxes : 0; }
4236 GridHalo<double>& stencilHalo(
Level& lv) {
return stencilHaloOn(lv) ? *lv.sdev : *lv.dev; }
4237 GridHalo<VReal>& stencilHaloF(
Level& lv) {
4238 return stencilHaloOn(lv) &&
lv.sdevf ? *
lv.sdevf : *
lv.devf;
4246 bool fusedWrapReads()
const {
return !distributed_ && !hasOutflow_ && !overlaySolve_; }
4248 return fusedWrapReads() && lv.inner.x % 2 == 0 && lv.inner.y % 2 == 0 && lv.inner.z % 2 == 0;
4271#ifdef PECLET_FLOW_MPI
4273 if (hasOutflow_ || fp32Last_ < 0 || meanRemovalAll_)
4275 if (!(exactResidual_ || std::is_same_v<MReal, double>))
4277 for (
int L = 0; L <= fp32Last_; ++L) {
4278 const C3 gi = lv_[L].gdim;
4279 if (gi.
x % 2 != 0 || gi.
y % 2 != 0 || gi.
z % 2 != 0)
4285 if (distributed_ || hasOutflow_ || lv_.size() < 2 || meanRemovalAll_)
4287 if (!(exactResidual_ || std::is_same_v<MReal, double>))
4289 for (std::size_t L = 0; L + 1 < lv_.size(); ++L) {
4290 const C3 in = lv_[L].inner;
4291 if (in.
x % 2 != 0 || in.
y % 2 != 0 || in.
z % 2 != 0)
4303 const bool beyond = distributed_ ? L > fp32Last_ : L + 1 >= (int)lv_.size();
4308 if (lv.WX.extent(0) != lv.n) {
4309 lv.WX = Kokkos::View<VReal*, CCMem>(
"peclet::flow::mg_wx", lv.n);
4310 lv.WY = Kokkos::View<VReal*, CCMem>(
"peclet::flow::mg_wy", lv.n);
4311 lv.WZ = Kokkos::View<VReal*, CCMem>(
"peclet::flow::mg_wz", lv.n);
4312 lv.xf = Kokkos::View<VReal*, CCMem>(
"peclet::flow::mg_xf", lv.n);
4313 lv.rhsf = Kokkos::View<VReal*, CCMem>(
"peclet::flow::mg_rhsf", lv.n);
4314 lv.resf = Kokkos::View<VReal*, CCMem>(
"peclet::flow::mg_resf", lv.n);
4316 if (lv.lineAxis >= 0 && lv.lncf.extent(0) != lv.n) {
4317 lv.lncf = Kokkos::View<VReal*, CCMem>(
"peclet::flow::mg_line_f", lv.n);
4318 lv.lnyf = Kokkos::View<VReal*, CCMem>(
"peclet::flow::mg_line_f", lv.n);
4319 lv.lnzf = Kokkos::View<VReal*, CCMem>(
"peclet::flow::mg_line_f", lv.n);
4321#ifdef PECLET_FLOW_MPI
4324 if (distributed_ && !lv.devf) {
4325 lv.devf = std::make_shared<GridHalo<VReal>>();
4326 lv.devf->init(*lv.halo);
4327 lv.devf->setLabel(
"mg L" + std::to_string(L) +
" g" + std::to_string(lv.g) +
" f32");
4329 if (distributed_ && !lv.sdevf && lv.shalo) {
4330 lv.sdevf = std::make_shared<GridHalo<VReal>>();
4331 lv.sdevf->init(*lv.shalo);
4332 lv.sdevf->setLabel(
"mg L" + std::to_string(L) +
" g" + std::to_string(lv.g) +
" f32 stencil");
4334 if (distributed_ && lv.tele && L == fp32Last_ && lv.tcorr.extent(0) != lv.n)
4335 lv.tcorr =
CCField(
"peclet::flow::mg_tcorr", lv.n);
4344 const auto need = [&v](
const auto& f) {
return f.extent(0) != v.n; };
4348 v.rhs =
CCField(
"mg_rhs", v.n);
4350 v.res =
CCField(
"mg_res", v.n);
4351 for (
FPV* p : {&v.AC, &v.AFX, &v.AFY, &v.AFZ})
4353 *p =
FPV(
"mg_A", v.n);
4360 for (
CCField* p : {&v.lnc, &v.lny, &v.lnz})
4361 if (p->extent(0) != v.n)
4377 if (wBad_.extent(0) != lv_.size()) {
4378 wBad_ = Kokkos::View<int*, CCMem>(
"peclet::flow::mg_wbad", lv_.size());
4379 wBadHost_ = Kokkos::View<int*, PinnedHost>(
"peclet::flow::mg_wbad_host", lv_.size());
4381 Kokkos::deep_copy(
CCExec(), Kokkos::subview(wBad_, L), 0);
4383 return wBad_.data() + L;
4387#ifdef PECLET_FLOW_MPI
4391 if (distributed_ && wBadStale_) {
4392 Kokkos::deep_copy(
CCExec(), wBadHost_, wBad_);
4393 CCExec().fence(
"peclet::flow::mg_wbad_read");
4395 if (fp32Last_ >= 0) {
4396 if (wBad_.extent(0) < (std::size_t)(fp32Last_ + 1))
4397 throw std::logic_error(
4398 "CutcellMG::fp32WeightsOk: the range flags do not cover levels 0 .. fp32Last_");
4399 MPI_Allreduce(MPI_IN_PLACE, wBadHost_.data(), fp32Last_ + 1, MPI_INT, MPI_MAX, comm_);
4406 Kokkos::deep_copy(
CCExec(), wBadHost_, wBad_);
4407 CCExec().fence(
"peclet::flow::mg_wbad_read");
4410 return wBadHost_(L) == 0;
4426 for (std::size_t L = 0; L + 1 < lv_.size(); ++L)
4431 for (
int L = 0; L + 1 < (int)lv_.size(); ++L) {
4433 const double sx = 1.0 / (double)(lv.cfac.x * lv.cfac.x),
4434 sy = 1.0 / (double)(lv.cfac.y * lv.cfac.y),
4435 sz = 1.0 / (double)(lv.cfac.z * lv.cfac.z);
4438 CCConst(lv.oy),
CCConst(lv.oz), lv.ext, lv.g, gfx_ * sx, gfy_ * sy,
4443 CCConst(lv.oz), lv.ext, lv.g, gfx_ * sx, gfy_ * sy, gfz_ * sz);
4453 for (std::size_t L = 0; L + 1 < lv_.size(); ++L) {
4468 v.nFluidDevValid =
false;
4471 fp64Stale_ = opBuilt_;
4479 for (
int L = 0; L < (int)lv_.size(); ++L) {
4483 const double sx = 1.0 / (double)(lv.cfac.x * lv.cfac.x),
4484 sy = 1.0 / (double)(lv.cfac.y * lv.cfac.y),
4485 sz = 1.0 / (double)(lv.cfac.z * lv.cfac.z);
4487 CCConst(lv.oy),
CCConst(lv.oz), lv.ext, lv.g, gfx_ * sx, gfy_ * sy,
4503 throw std::runtime_error(
4504 std::string(
"set_pressure_vcycle_precision('fp32'): not eligible here: ") + why);
4513 const std::vector<double>&
healthLog()
const {
return healthLog_; }
4525 std::ldexp(1.0, -e), l0.
ext,
parityOg(l0), l0.
g, 0, pre_ > 0);
4526#ifdef PECLET_FLOW_MPI
4528 vcycleFp32Dist(0,
true);
4532 double s = std::ldexp(1.0, e);
4533 if (fp32FaultKind_ == 1 && ++fp32FaultCount_ == fp32FaultAt_)
4534 s = std::numeric_limits<double>::quiet_NaN();
4542 for (
int k = 0; k < sweeps; ++k)
4543 for (
int s = 0; s < 2; ++s) {
4548 const int color = reverse ? (1 - s) : s;
4554 VConst(lv.WZ), lv.ext, lv.inner, og, lv.g, color);
4561 smoothFp32(lv, pre_,
false, L == 0 && pre_ > 0);
4563 VConst(lv.WZ), lv.ext, lv.inner, lv.g);
4564 Level& cs = lv_[L + 1];
4565 if (L + 2 == (
int)lv_.size()) {
4586 prolongFp32(lv.xf,
VConst(cs.xf), lv.ext, cs.ext, lv.g, cs.g, lv.inner, lv.ratio);
4591#ifdef PECLET_FLOW_MPI
4599 void vcycleFp32Dist(
int L,
bool sym) {
4601 smoothFp32Dist(lv, pre_,
false, L == 0 && pre_ > 0);
4602 residualFp32Dist(lv);
4605 Kokkos::deep_copy(
CCExec(), lv.tcorr, 0.0);
4606 teleTransition(L, sym, lv.res, lv.tcorr);
4608 }
else if (L == fp32Last_) {
4609 Level& cs = lv_[L + 1];
4612 Kokkos::deep_copy(
CCExec(), cs.x, 0.0);
4626 Kokkos::deep_copy(
CCExec(),
cs.xf, 0.0f);
4627 vcycleFp32Dist(L + 1, sym);
4636 smoothFp32Dist(lv, post_, sym,
false);
4641 void smoothFp32Dist(
Level& lv,
int sweeps,
bool reverse,
bool skipFirst) {
4643 for (
int k = 0;
k < sweeps; ++
k)
4644 for (
int s = 0;
s < 2; ++
s) {
4655 const C3
e =
lv.ext;
4656 const C3
lo{
g + 1,
g + 1,
g + 1};
4657 const C3 hi{
e.x -
g - 1,
e.y -
g - 1,
e.z -
g - 1};
4658 const C3 none{0, 0, 0};
4659 auto box = [&](
int color, C3
a, C3
b, C3 exLo, C3 exHi) {
4661 e, og, color, a, b, exLo, exHi);
4664 const C3 rlo{
g - 1,
g - 1,
g - 1};
4665 const C3 rhi{
e.x -
g + 1,
e.y -
g + 1,
e.z -
g + 1};
4666 lv.devf->exchange(
lv.rhsf);
4667 for (
int k = 0;
k < sweeps; ++
k) {
4668 const int c0 =
reverse ? 1 : 0,
c1 = 1 - c0;
4669 lv.devf->exchangeBegin(
lv.xf);
4670 box(c0, lo, hi, none, none);
4671 lv.devf->exchangeEnd(
lv.xf);
4672 box(c0, rlo, rhi, lo, hi);
4673 box(c1, C3{
g,
g,
g}, C3{
e.x -
g,
e.y -
g,
e.z -
g}, none, none);
4678 const int m = stencilWrap(lv);
4679 const C3 wlo =
boxLo(g, m), whi =
boxHi(e, g, m);
4680 auto wbox = [&](
int color, C3
a, C3
b, C3 exLo, C3 exHi) {
4682 e, og, color, a, b, exLo, exHi, m, g);
4684 for (
int k = 0;
k < sweeps; ++
k)
4685 for (
int s = 0;
s < 2; ++
s) {
4690 const int color =
reverse ? (1 -
s) :
s;
4692 wbox(color, wlo, whi, none, none);
4695 GridHalo<VReal>&
h = stencilHaloF(lv);
4696 h.exchangeBegin(
lv.xf);
4697 wbox(color, wlo, whi, none, none);
4698 h.exchangeEnd(
lv.xf);
4699 wbox(color, C3{
g,
g,
g}, C3{
e.x -
g,
e.y -
g,
e.z -
g}, wlo, whi);
4703 void residualFp32Dist(
Level& lv) {
4705 const C3
e =
lv.ext;
4706 const int m = stencilWrap(lv);
4710 VConst(
lv.WZ), e, lo, hi, C3{0, 0, 0}, C3{0, 0, 0}, m, g);
4713 GridHalo<VReal>&
h = stencilHaloF(lv);
4714 h.exchangeBegin(
lv.xf);
4716 VConst(
lv.WZ), e, lo, hi, C3{0, 0, 0}, C3{0, 0, 0}, m, g);
4717 h.exchangeEnd(
lv.xf);
4719 VConst(
lv.WZ), e, C3{g, g, g}, C3{e.x - g, e.y - g, e.z - g}, lo, hi, m, g);
4727 const C3 e = l0.
ext;
4734 const bool all = counted && l0.
allFluid;
4735 const long cnt0 = l0.
nFluid;
4742 auto joint = KOKKOS_LAMBDA(
int x,
int y,
int zc,
double& a,
long& c) {
4743 const long i = (long)x + (
long)y * e.x + (long)zc * (
long)e.x * e.y;
4744 if (ac(i) > 1e-30f) {
4749 if (!distributed_) {
4752 "peclet::flow::mgmeanr_f32",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
4753 KOKKOS_LAMBDA(
int x,
int y,
int zc,
double& a) {
4754 const long i = (long)x + (
long)y * e.x + (long)zc * (
long)e.x * e.y;
4758 Kokkos::Sum<double, CCMem>(
slot(
kMsum)));
4761 "peclet::flow::mgmeanr_f32",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
4762 KOKKOS_LAMBDA(
int x,
int y,
int zc,
double& a) {
4763 const long i = (long)x + (
long)y * e.x + (long)zc * (
long)e.x * e.y;
4764 if (all || ac(i) > 1e-30f)
4767 Kokkos::Sum<double, CCMem>(
slot(
kMsum)));
4769 ccReduce3Lanes(
"peclet::flow::mgmeanr_f32",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
4770 joint, Kokkos::Sum<double, CCMem>(
slot(
kMsum)),
4771 Kokkos::Sum<long, CCMem>(
kcnt_));
4773#ifdef PECLET_FLOW_MPI
4779 ccReduce3Lanes(
"peclet::flow::mgmeanr_f32",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g}, joint,
4781 double dcnt = (double)lcnt;
4782 allreduceSum2(sum, dcnt, l0.comm);
4783 const long gcnt = (long)dcnt;
4784 Kokkos::parallel_for(
4785 "peclet::flow::mgmeanr_f32_global", Kokkos::RangePolicy<CCExec>(
CCExec(), 0, 1),
4786 KOKKOS_LAMBDA(
int) {
4792 const bool copyP = p.extent(0) != 0;
4793 auto body = KOKKOS_LAMBDA(
int x,
int y,
int zc,
double& acc) {
4794 const long i = (long)x + (
long)y * e.x + (long)zc * (
long)e.x * e.y;
4795 const long cnt = counted ? cnt0 : kc();
4796 const bool fluid = all || ac(i) > 1e-30f;
4797 const double xd = (double)xf(i);
4798 const double zi = (fluid && cnt != 0) ? s * (xd - ks(
kMsum) / (
double)cnt) : s * xd;
4806 ccReduce3Lanes(
"peclet::flow::mgmeans_dot_f32",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
4810 "peclet::flow::mgmeans_f32",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
4811 KOKKOS_LAMBDA(
int x,
int y,
int zc) {
4812 double unused = 0.0;
4813 body(x, y, zc, unused);
4825 throw std::invalid_argument(
4826 "CutcellMG::setVcyclePrecision: kVcycleAuto | kVcycleFp64 | kVcycleFp32");
4827 fp32Latched_ =
false;
4846 fp32FaultKind_ = kind;
4856 const Level& lv = lv_[L];
4857 const C3 e = lv.ext;
4859 Kokkos::View<const VReal*, CCMem> WX = lv.WX, WY = lv.WY, WZ = lv.WZ;
4864 "peclet::flow::mg_fp32_decoupled",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
4865 KOKKOS_LAMBDA(
int lx,
int ly,
int lz,
long& a,
long& b) {
4866 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
4867 const long i = (long)lx + (
long)ly * sy + (long)lz * sz;
4868 if (WX(i) == 0.0f && WX(i + sx) == 0.0f && WY(i) == 0.0f && WY(i + sy) == 0.0f &&
4869 WZ(i) == 0.0f && WZ(i + sz) == 0.0f)
4880 for (
int L = 0; L < (int)lv_.size(); ++L)
4882 long n32 = 0, n64 = 0;
4884 assert(n32 == n64 &&
"D: the FP32 decoupled set must equal the FP64 AC < 1e-30 set");
4893 return "the V-cycle is not the preconditioner of the single-rank PCG or FCG driver";
4894#ifdef PECLET_FLOW_MPI
4896 return fp32VcycleIneligibleDist();
4899 return "an outflow face is present";
4901 return "the solve carries an overlay (star couplings / ghost projection)";
4903 return "the hierarchy has a single level";
4904 for (std::size_t L = 0; L + 1 < lv_.size(); ++L)
4906 return "a non-bottom level has an odd inner dimension";
4907 if (!(exactResidual_ || std::is_same_v<MReal, double>))
4908 return "the outer operator is float (MReal = float without the exact residual)";
4911 if (meanRemovalAll_)
4912 return "the mean-removal scope is 'all' (the FP32 V-cycle removes the mean at level 0 only)";
4913 for (
int L = 0; L + 1 < (int)lv_.size(); ++L)
4915 return "a face weight is outside {0} or [1e-30, 1e30] (or the FP32 weights are not built)";
4918#ifdef PECLET_FLOW_MPI
4923 const char* fp32VcycleIneligibleDist()
const {
4925 return "an outflow face is present";
4927 return "the solve carries an overlay (star couplings / ghost projection)";
4929 return "the hierarchy has a single level";
4930 for (
int L = 0; L <= fp32Last_; ++L) {
4931 const C3 gi = lv_[L].gdim;
4932 if (gi.
x % 2 != 0 || gi.
y % 2 != 0 || gi.
z % 2 != 0)
4933 return "a level the FP32 V-cycle runs on has an odd global inner dimension";
4935 if (!(exactResidual_ || std::is_same_v<MReal, double>))
4936 return "the outer operator is float (MReal = float without the exact residual)";
4937 if (meanRemovalAll_)
4938 return "the mean-removal scope is 'all' (the FP32 V-cycle removes the mean at level 0 only)";
4940 for (
int L = 0; L <= fp32Last_; ++L)
4943 return "a face weight is outside {0} or [1e-30, 1e30] (or the FP32 weights are not built)";
4952 return lineRelax_ && lv.lineAxis >= 0 && ((lv.wholeAxes >> lv.lineAxis) & 1);
4955 bool linePeriodic(
int s)
const {
return bc_[2 * s] == 0 && bc_[2 * s + 1] == 0; }
4961#ifdef PECLET_FLOW_MPI
4963 m = stencilWrap(lv);
4965 stencilHalo(lv).exchange(lv.x);
4986#ifdef PECLET_FLOW_MPI
4988 m = stencilWrap(lv);
4990 stencilHaloF(lv).exchange(lv.xf);
5005 for (
int k = 0; k < sweeps; ++k)
5006 for (
int s = 0; s < 2; ++s)
5011#ifdef PECLET_FLOW_MPI
5022 const C3 lo{g + 1, g + 1, g + 1};
5023 const C3 hi{lv.ext.x - g - 1, lv.ext.y - g - 1, lv.ext.z - g - 1};
5024 const C3 rlo{g - 1, g - 1, g - 1};
5025 const C3 rhi{lv.ext.x - g + 1, lv.ext.y - g + 1, lv.ext.z - g + 1};
5026 lv.dev->exchange(lv.rhs);
5027 for (
int k = 0; k < sweeps; ++k) {
5028 const int c0 = reverse ? 1 : 0, c1 = 1 - c0;
5029 lv.dev->exchangeBegin(lv.x);
5031 FPC(lv.AFZ), lv.ext, og, c0, lo, hi,
C3{0, 0, 0},
C3{0, 0, 0});
5032 lv.dev->exchangeEnd(lv.x);
5034 FPC(lv.AFZ), lv.ext, og, c0, rlo, rhi, lo, hi);
5036 FPC(lv.AFZ), lv.ext, og, g, c1);
5041 for (
int k = 0; k < sweeps; ++k)
5042 for (
int s = 0; s < 2; ++s) {
5043 const int color = reverse ? (1 - s) : s;
5044#ifdef PECLET_FLOW_MPI
5054 const int m = stencilWrap(lv);
5058 FPC(lv.AFZ), lv.ext, og, color, lo, hi,
C3{0, 0, 0},
5062 GridHalo<double>& h = stencilHalo(lv);
5063 h.exchangeBegin(lv.x);
5065 FPC(lv.AFZ), lv.ext, og, color, lo, hi,
C3{0, 0, 0},
5067 h.exchangeEnd(lv.x);
5070 FPC(lv.AFZ), lv.ext, og, color,
C3{g, g, g},
5071 C3{lv.ext.x - g, lv.ext.y - g, lv.ext.z - g}, lo, hi, m, g);
5077 FPC(lv.AFZ), lv.ext, lv.inner, og, lv.g, color);
5083 FPC(lv.AFZ), lv.ext, og, lv.g, color);
5110 if (agglomMode_ == 0)
5112 if (agglomMode_ == 1)
5117 long gx = gnxF_, gy = gnyF_, gz = gnzF_;
5118 for (
int L = 0; L + 1 < (int)lv_.size(); ++L) {
5119 gx /= lv_[L].ratio.x;
5120 gy /= lv_[L].ratio.y;
5121 gz /= lv_[L].ratio.z;
5141 const int thresh = agglomExtent_ > 0 ? agglomExtent_ : 4;
5142 return gx > thresh || gy > thresh || gz > thresh;
5152 const int e = extent > 0 ? extent : 4;
5153 if (g.x <= e && g.y <= e && g.z <= e)
5157 if (agglomMode == 0)
5159 const long n = (long)g.x * g.y * g.z;
5164 return bottomAdmissible(g, agglomExtent_, !removeMean_ || hasOutflow_, agglomMode_, bc_);
5167 return nLevels == 0 ? std::string(
"auto") : std::to_string(
nLevels);
5174 const int D = (int)lv_.size();
5181 const C3 b = lv_.back().gdim;
5187 line =
"; A2 line-trigger smoothed levels {";
5188 for (std::size_t i = 0; i < ls.size(); ++i)
5189 line += (i ?
"," :
"") + std::to_string(ls[i]);
5190 line += std::string(
"} along ") +
"xyz"[lv_[ls[0]].lineAxis];
5193 line += symVcycle_ ?
"; P const, bottom palindromic" :
"; P trilinear, bottom forward";
5195 printf(
"[mg-census] depth requested %s -> %d levels, bottom %dx%dx%d; auto -> %d%s%s\n",
5197 nLevels == 0 ? (
" (legacy 4 -> " + std::to_string(std::min(D, 4)) +
")").c_str() :
"",
5200 printf(
"[mg-census] depth requested %s -> %d levels, bottom %dx%dx%d; auto -> >%d%s\n",
5209 r.reserve(lv_.size());
5210 for (
const Level& l : lv_)
5211 r.push_back(l.lineAxis);
5216 const int D = (int)lv_.size();
5217 for (
int L = 0; L < D; ++L)
5225 const Level& l = lv_[L];
5227 return std::string();
5232 std::snprintf(buf,
sizeof buf,
" A' %g", l.
strongAspect);
5233 return std::string(buf);
5238 std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>;
5246 return "the solve is distributed (multi-rank)";
5247 if (!removeMean_ || hasOutflow_)
5248 return "the operator is not the singular one (an outflow face is present)";
5250 return "the bottom is not agglomerated (set_pressure_bottom / set_pressure_bottom_extent)";
5252 (
long)lv_.back().gdim.x * lv_.back().gdim.y * lv_.back().gdim.z >
kBottomMaxCells)
5253 return "the bottom level has more than 8192 inner cells";
5259 if (gbActive_ && !dirPlanes_.valid())
5261 if (!bottomConnKnown_)
5263 if (!bottomConnected_)
5265 if (!dirPlanes_.valid())
5272 return "the pressure operator has not been built (no setOpenness since the hierarchy)";
5274 return "the bottom level has no fluid cell or more than 64 fluid components (B1b)";
5276 return "no axis gives planes of at most 192 cells (the direct factor's cap)";
5292 return "the solve is distributed (multi-rank)";
5293 if (!removeMean_ || hasOutflow_)
5294 return "the operator is not the singular one (an outflow face is present)";
5295 if (!dirAgreedKnown_)
5297 const C3 e = dirAgreedExt_, d = dirAgreedGdim_;
5298 if (!(agglomMode_ == 1 || (agglomMode_ != 0 &&
agglomerateAuto(e.x, e.y, e.z))))
5299 return "the bottom is not agglomerated (set_pressure_bottom / set_pressure_bottom_extent)";
5301 return "the bottom level has more than 8192 inner cells";
5308 throw std::runtime_error(
5309 std::string(
"set_pressure_bottom_solver('direct'): not eligible here: ") + why);
5311#ifdef PECLET_FLOW_MPI
5315 void agreeDirectBottom() {
5316 int v[7] = {0, 0, 0, 0, 0, 0, 0}, a[7];
5317 const Level& bt = lv_.back();
5319 long gx = gnxF_, gy = gnyF_, gz = gnzF_;
5320 for (
int L = 0; L + 1 < (int)lv_.size(); ++L) {
5321 gx /= lv_[L].ratio.x;
5322 gy /= lv_[L].ratio.y;
5323 gz /= lv_[L].ratio.z;
5333 MPI_Allreduce(v, a, 7, MPI_INT, MPI_MAX, comm_);
5334 dirAgreedGdim_ = C3{
a[0],
a[1],
a[2]};
5335 dirAgreedExt_ = C3{
a[3],
a[4],
a[5]};
5336 dirAgreedCode_ =
a[6];
5337 dirAgreedKnown_ =
true;
5345 dirAgreedKnown_ =
false;
5348 bottomConnKnown_ =
false;
5349 bottomConnected_ =
false;
5350 bottomStore_ =
false;
5358 if (bt.g != 1 || (
long)bt.inner.x * bt.inner.y * bt.inner.z >
kBottomMaxCells)
5360 bottomStore_ =
true;
5361 for (
CCField* f : {&bottomR_, &bottomP_, &bottomZ_, &bottomZp_, &bottomAp_})
5362 *f =
CCField(
"mg_bottom_fcg", bt.n);
5363 bottomInfo_ = Kokkos::View<int*, CCMem>(
"peclet::flow::mg_bottom_info", 1);
5364 bottomComp_ = Kokkos::View<int*, CCMem>(
"peclet::flow::mg_bottom_comp", bt.n);
5365 bottomLabTmp_ = Kokkos::View<int*, CCMem>(
"peclet::flow::mg_bottom_labtmp", bt.n);
5391#ifdef PECLET_FLOW_MPI
5392 const Level& bt = lv_.back();
5395 const C3 gi = bt.gdim;
5404 g.ext =
C3{gi.
x + 2, gi.
y + 2, gi.
z + 2};
5405 g.n = (std::size_t)g.ext.x * g.ext.y * g.ext.z;
5406 g.x =
CCField(
"mg_gb_x", g.n);
5407 g.rhs =
CCField(
"mg_gb_rhs", g.n);
5408 g.ox =
CCField(
"mg_gb_ox", g.n);
5409 g.oy =
CCField(
"mg_gb_oy", g.n);
5410 g.oz =
CCField(
"mg_gb_oz", g.n);
5411 for (
FPV* p : {&g.AC, &g.AFX, &g.AFY, &g.AFZ})
5412 *p =
FPV(
"mg_gb_A", g.n);
5415 for (
int k = 0; k < bt.inner.z; ++k)
5416 for (
int j = 0; j < bt.inner.y; ++j)
5417 for (
int i = 0; i < bt.inner.x; ++i)
5418 gbLocal_.push_back((bt.og.x + i) + (bt.og.y + j) * gi.
x + (bt.og.z + k) * gi.
x * gi.
y);
5422 MPI_Comm_size(bt.comm, &size);
5423 const int nloc = (int)gbLocal_.size();
5424 gbCnt1_.assign(size, 0);
5425 MPI_Allgather(&nloc, 1, MPI_INT, gbCnt1_.data(), 1, MPI_INT, bt.comm);
5426 gbDsp1_.assign(size, 0);
5427 gbCnt3_.assign(size, 0);
5428 gbDsp3_.assign(size, 0);
5430 for (
int r = 0; r < size; ++r) {
5432 gbCnt3_[r] = 3 * gbCnt1_[r];
5433 gbDsp3_[r] = 3 * tot;
5436 gbGid_.resize((std::size_t)tot);
5437 MPI_Allgatherv(gbLocal_.data(), nloc, MPI_INT, gbGid_.data(), gbCnt1_.data(), gbDsp1_.data(),
5448 const int gx = gid % gb_.inner.x, t = gid / gb_.inner.x;
5449 const int gy = t % gb_.inner.y, gz = t / gb_.inner.y;
5450 return (
long)(gx + 1) + (
long)(gy + 1) * gb_.ext.x + (
long)(gz + 1) * gb_.ext.x * gb_.ext.y;
5457#ifdef PECLET_FLOW_MPI
5458 if (!removeMean_ || hasOutflow_)
5464 if (!dirPlanes_.valid() || (bottomConnKnown_ && !bottomConnected_))
5466 Level& bt = lv_.back();
5467 const int g = bt.g, ex = bt.ext.x, ey = bt.ext.y;
5468 auto hox = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), bt.ox);
5469 auto hoy = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), bt.oy);
5470 auto hoz = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), bt.oz);
5471 std::vector<double> lo;
5472 lo.reserve(3 * gbLocal_.size());
5473 for (
int k = 0; k < bt.inner.z; ++k)
5474 for (
int j = 0; j < bt.inner.y; ++j)
5475 for (
int i = 0; i < bt.inner.x; ++i) {
5476 const long p = (long)(i + g) + (long)(j + g) * ex + (long)(k + g) * ex * ey;
5477 lo.push_back(hox(p));
5478 lo.push_back(hoy(p));
5479 lo.push_back(hoz(p));
5481 std::vector<double> go(3 * gbGid_.size());
5482 MPI_Allgatherv(lo.data(), (
int)lo.size(), MPI_DOUBLE, go.data(), gbCnt3_.data(), gbDsp3_.data(),
5483 MPI_DOUBLE, bt.comm);
5485 auto gox = Kokkos::create_mirror_view(gb.
ox);
5486 auto goy = Kokkos::create_mirror_view(gb.
oy);
5487 auto goz = Kokkos::create_mirror_view(gb.
oz);
5488 for (std::size_t r = 0; r < gbGid_.size(); ++r) {
5489 const long q =
gbIndex(gbGid_[r]);
5491 goy(q) = go[3 * r + 1];
5492 goz(q) = go[3 * r + 2];
5494 Kokkos::deep_copy(gb.
ox, gox);
5495 Kokkos::deep_copy(gb.
oy, goy);
5496 Kokkos::deep_copy(gb.
oz, goz);
5501 const double sx = 1.0 / (double)(gb.
cfac.
x * gb.
cfac.
x),
5505 CCConst(gb.
oz), gb.
ext, gb.
g, gfx_ * sx, gfy_ * sy, gfz_ * sz);
5510#ifdef PECLET_FLOW_MPI
5511 Level& bt = lv_.back();
5512 const int g = bt.g, ex = bt.ext.x, ey = bt.ext.y;
5513 auto hrhs = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), bt.rhs);
5514 std::vector<double> lb;
5515 lb.reserve(gbLocal_.size());
5516 for (
int k = 0; k < bt.inner.z; ++k)
5517 for (
int j = 0; j < bt.inner.y; ++j)
5518 for (
int i = 0; i < bt.inner.x; ++i)
5519 lb.push_back(hrhs((
long)(i + g) + (
long)(j + g) * ex + (
long)(k + g) * ex * ey));
5520 std::vector<double> gbv(gbGid_.size());
5521 MPI_Allgatherv(lb.data(), (
int)lb.size(), MPI_DOUBLE, gbv.data(), gbCnt1_.data(),
5522 gbDsp1_.data(), MPI_DOUBLE, bt.comm);
5523 auto grhs = Kokkos::create_mirror_view(gb_.rhs);
5524 for (std::size_t r = 0; r < gbGid_.size(); ++r)
5525 grhs(
gbIndex(gbGid_[r])) = gbv[r];
5526 Kokkos::deep_copy(gb_.rhs, grhs);
5532 Level& bt = lv_.back();
5533 const int g = bt.g, ex = bt.ext.x, ey = bt.ext.y;
5534 auto hgx = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), gb_.x);
5535 auto hx = Kokkos::create_mirror_view(bt.x);
5536 Kokkos::deep_copy(hx, 0.0);
5538 for (
int k = 0; k < bt.inner.z; ++k)
5539 for (
int j = 0; j < bt.inner.y; ++j)
5540 for (
int i = 0; i < bt.inner.x; ++i)
5541 hx((
long)(i + g) + (
long)(j + g) * ex + (
long)(k + g) * ex * ey) =
5543 Kokkos::deep_copy(bt.x, hx);
5552 bottomConnKnown_ =
true;
5553 bottomConnected_ =
false;
5565 k.comp = bottomComp_;
5566 k.tmp = bottomLabTmp_;
5567 k.cnt = bottomCompCnt_;
5569 k.slow = dirPlanes_.s;
5572 Kokkos::TeamPolicy<CCExec> probe(
CCExec(), 1, 1);
5573 const int T = std::min(1024, probe.team_size_max(k, Kokkos::ParallelForTag()));
5574 Kokkos::parallel_for(
"peclet::flow::mg_bottom_label",
5575 Kokkos::TeamPolicy<CCExec>(
CCExec(), 1, T), k);
5576 Kokkos::deep_copy(bottomNComp_, Kokkos::subview(bottomInfo_, 0));
5579 printf(
"[mg] device bottom: %d fluid component(s) on the %dx%dx%d bottom%s\n", bottomNComp_,
5580 bt.inner.x, bt.inner.y, bt.inner.z,
5581 bottomConnected_ ?
"" :
" -> GraphAMG (more than 64, or none)");
5583 template <
class FR =
float>
5589 k.lv.inner = bt.inner;
5601 k.precondOnly = precondOnly ? 1 : 0;
5604 k.info = bottomInfo_;
5605 k.nc = bottomNComp_;
5606 k.comp = bottomComp_;
5607 k.ccnt = bottomCompCnt_;
5608 k.cmean = bottomCompMean_;
5615 bottomFlagPending_ =
false;
5617 solveFailed_ =
true;
5626 static int directTeam(
const K& k,
int team,
bool probe, std::size_t scratch = 0,
5628 if (team > 0 && !probe)
5630 Kokkos::TeamPolicy<CCExec> pol(
CCExec(), 1, 1);
5632 pol.set_scratch_size(0, Kokkos::PerTeam(scratch));
5633 const int tmax = std::min(1024, pol.team_size_max(k, Kokkos::ParallelForTag()));
5635 return std::min(team, tmax);
5636 return kHostMemory ? std::min(hostTeam, tmax) : tmax;
5643 bool teamAlgorithm =
false,
const CCExec& exec =
CCExec()) {
5650 k.comp = bottomComp_;
5653 k.tauPiv0 = tauPiv0;
5654 k.teamAlgorithm = teamAlgorithm ? 1 : 0;
5655#ifdef PECLET_FLOW_BOTTOM_PROBE
5656 if (bottomProbe_.extent(0) == 0)
5657 bottomProbe_ = Kokkos::View<long long*, CCMem>(
"peclet::flow::mg_bottom_probe", 16);
5658 k.probe = bottomProbe_;
5662 Kokkos::parallel_for(
5663 "peclet::flow::mg_bottom_factor",
5664 Kokkos::TeamPolicy<CCExec>(exec, 1, T).set_scratch_size(0, Kokkos::PerTeam(scratch)), k);
5672 bool teamAlgorithm =
false) {
5675 k.noMeanForTest = noMean ? 1 : 0;
5676 k.teamAlgorithm = teamAlgorithm ? 1 : 0;
5682 Kokkos::parallel_for(
"peclet::flow::mg_bottom_direct",
5683 Kokkos::TeamPolicy<CCExec>(
CCExec(), 1, T), k);
5697 return !distributed_ && bottomStore_ && bottomSolver_ !=
kBottomAlgebraic &&
5706 side_ = Kokkos::Experimental::partition_space(
CCExec(), std::vector<int>{1})[0];
5707 CCExec().fence(
"peclet::flow::mg_prefactor_ready");
5712 ++prefactorLaunches_;
5720 side_->fence(
"peclet::flow::mg_prefactor_join");
5721 facPending_ =
false;
5729 const bool first = dirTeam_ == 0;
5740 auto h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), dir_.stat);
5741 printf(
"[mg] direct bottom factor: ok %d, restarts %d (team %d)\n", h(0), h(1),
5751 "[mg] direct bottom: %dx%dx%d, slow axis %d, P = %d, b = %d, border %d; team sizes "
5752 "factor %d, solve %d\n",
5754 dirPlanes_.b, dirPlanes_.border, dirFacTeam_, dirTeam_);
5757 bottomFlagPending_ =
true;
5760 Kokkos::deep_copy(it, Kokkos::subview(bottomInfo_, 0));
5761 printf(
"[mg] direct bottom: %d inner iterations (team %d)\n", it, dirTeam_);
5771 amgGlobalN_ = gb.
x * gb.
y * gb.
z;
5773 amgGlobalOfLocal_ = local.
gid;
5777#ifdef PECLET_FLOW_MPI
5779 gatherAmgRows(local, rows, lv.comm);
5786 amg_ = std::make_shared<peclet::core::solver::GraphAMG>();
5799 int gbx = gnxF_, gby = gnyF_, gbz = gnzF_;
5800 for (
int L = 0; L + 1 < (int)lv_.size(); ++L) {
5801 gbx /= lv_[L].ratio.x;
5802 gby /= lv_[L].ratio.y;
5803 gbz /= lv_[L].ratio.z;
5805 return C3{gbx, gby, gbz};
5810 const int gbx = gb.
x, gby = gb.
y;
5811 const int nx = lv.inner.x, ny = lv.inner.y, nz = lv.inner.z, ex = lv.ext.x, ey = lv.ext.y;
5812 auto host = [](
FPV v) {
5813 auto h = Kokkos::create_mirror_view(v);
5814 Kokkos::deep_copy(h, v);
5817 auto hC = host(lv.AC), hAFX = host(lv.AFX), hAFY = host(lv.AFY), hAFZ = host(lv.AFZ);
5820 for (
int k = 0; k < nz; ++k)
5821 for (
int j = 0; j < ny; ++j)
5822 for (
int i = 0; i < nx; ++i) {
5823 const long p = (long)(i + g) + (long)(j + g) * ex + (long)(k + g) * ex * ey;
5824 const C3 gc{lv.og.x + i, lv.og.y + j, lv.og.z + k};
5825 const int gid = gc.
x + gc.
y * gbx + gc.
z * gbx * gby;
5826 o.gid.push_back(gid);
5829 const double dc = (double)hC(p);
5830 o.diag.push_back(dc != 0.0 ? dc : 1.0);
5831 o.solid.push_back(dc == 0.0 ? 1 : 0);
5833 const double bc[6] = {(double)hAFX(p), (double)hAFX(p + 1),
5834 (double)hAFY(p), (double)hAFY(p + ex),
5835 (double)hAFZ(p), (double)hAFZ(p + (
long)ex * ey)};
5843 const int band[6][3] = {{-1, 0, 0}, {1, 0, 0}, {0, -1, 0}, {0, 1, 0}, {0, 0, -1}, {0, 0, 1}};
5844 const int gbx = gb.
x, gby = gb.
y, gbz = gb.
z;
5845 for (
int d = 0; d < 6; ++d) {
5853 const int rx = gc.
x + band[d][0], ry = gc.
y + band[d][1], rz = gc.
z + band[d][2];
5854 const int axis = d / 2;
5855 const bool crosses = (axis == 0 && (rx < 0 || rx >= gbx)) ||
5856 (axis == 1 && (ry < 0 || ry >= gby)) ||
5857 (axis == 2 && (rz < 0 || rz >= gbz));
5858 if (crosses && bc_[d] != 0)
5860 const int ngx = (rx % gbx + gbx) % gbx;
5861 const int ngy = (ry % gby + gby) % gby;
5862 const int ngz = (rz % gbz + gbz) % gbz;
5863 o.row.push_back(gid);
5864 o.col.push_back(ngx + ngy * gbx + ngz * gbx * gby);
5865 o.val.push_back(bc[d]);
5868#ifdef PECLET_FLOW_MPI
5871 void gatherAmgRows(
const AmgRows& local, AmgRows& all, MPI_Comm c) {
5872 gatherv(local.gid, all.gid, c);
5873 gatherv(local.diag, all.diag, c);
5874 gatherv(local.row, all.row, c);
5875 gatherv(local.col, all.col, c);
5876 gatherv(local.val, all.val, c);
5877 gatherv(local.solid, all.solid, c);
5885 amgSolid_.assign((std::size_t)amgGlobalN_, 0);
5886 for (std::size_t r = 0; r < rows.gid.size(); ++r)
5887 amgSolid_[(std::size_t)rows.gid[r]] = rows.solid[r];
5888 std::vector<int> parent((std::size_t)amgGlobalN_);
5889 for (
int i = 0; i < amgGlobalN_; ++i)
5890 parent[(std::size_t)i] = i;
5891 auto find = [&](
int a) {
5892 while (parent[(std::size_t)a] != a)
5893 a = parent[(std::size_t)a] = parent[(std::size_t)parent[(std::size_t)a]];
5896 for (std::size_t e = 0; e < rows.row.size(); ++e) {
5897 const int ra = find(rows.row[e]), rb = find(rows.col[e]);
5899 parent[(std::size_t)ra] = rb;
5901 amgComp_.assign((std::size_t)amgGlobalN_, -1);
5902 std::vector<int> remap((std::size_t)amgGlobalN_, -1);
5904 for (
int i = 0; i < amgGlobalN_; ++i) {
5905 if (amgSolid_[(std::size_t)i])
5907 const int r = find(i);
5908 if (remap[(std::size_t)r] < 0)
5909 remap[(std::size_t)r] = amgNComp_++;
5910 amgComp_[(std::size_t)i] = remap[(std::size_t)r];
5916 std::vector<long> csize((std::size_t)amgNComp_, 0);
5917 for (
int i = 0; i < amgGlobalN_; ++i)
5918 if (amgComp_[(std::size_t)i] >= 0)
5919 ++csize[(std::size_t)amgComp_[(std::size_t)i]];
5920 printf(
"[agmg-build] fluid components=%d sizes:", amgNComp_);
5921 for (
int c = 0; c < std::min(amgNComp_, 12); ++c)
5922 printf(
" %ld", csize[(std::size_t)c]);
5923 printf(amgNComp_ > 12 ?
" ...\n" :
"\n");
5924 long nSolid = 0, nTiny30 = 0, nTiny12 = 0;
5925 double minFluidDiag = 1e300, maxFluidDiag = 0;
5926 for (std::size_t r = 0; r < rows.gid.size(); ++r) {
5927 if (rows.solid[r]) {
5931 const double ad = std::fabs(rows.diag[r]);
5932 minFluidDiag = std::min(minFluidDiag, ad);
5933 maxFluidDiag = std::max(maxFluidDiag, ad);
5934 nTiny30 += ad < 1e-30;
5935 nTiny12 += ad < 1e-12;
5938 "[agmg-build] n=%d solid=%ld fluid=%ld fluid|diag| min=%.3e max=%.3e "
5939 "tiny<1e-30=%ld <1e-12=%ld\n",
5940 amgGlobalN_, nSolid, (
long)rows.gid.size() - nSolid, minFluidDiag, maxFluidDiag, nTiny30,
5946 std::vector<double> rowsum((std::size_t)amgGlobalN_, 0.0);
5947 for (std::size_t r = 0; r < rows.gid.size(); ++r)
5948 rowsum[(std::size_t)rows.gid[r]] = rows.solid[r] ? 0.0 : rows.diag[r];
5949 for (std::size_t e = 0; e < rows.row.size(); ++e)
5950 if (!amgSolid_[(std::size_t)rows.row[e]])
5951 rowsum[(std::size_t)rows.row[e]] += rows.val[e];
5952 double defMax = 0, defRelMax = 0;
5953 for (std::size_t r = 0; r < rows.gid.size(); ++r)
5954 if (!rows.solid[r]) {
5955 const double d = std::fabs(rowsum[(std::size_t)rows.gid[r]]);
5956 defMax = std::max(defMax, d);
5957 defRelMax = std::max(defRelMax, d / std::fabs(rows.diag[r]));
5959 printf(
"[agmg-build] fluid row-sum defect max=%.3e rel=%.3e\n", defMax, defRelMax);
5964 peclet::core::solver::HostCsrOp A;
5966 A.diag.assign((std::size_t)amgGlobalN_, 0.0);
5967 for (std::size_t r = 0; r < rows.gid.size(); ++r)
5968 A.diag[(std::size_t)rows.gid[r]] = rows.diag[r];
5969 std::vector<std::vector<std::pair<int, double>>> crow((std::size_t)amgGlobalN_);
5970 for (std::size_t e = 0; e < rows.row.size(); ++e)
5971 crow[(std::size_t)rows.row[e]].push_back({rows.col[e], rows.val[e]});
5972 A.start.assign((std::size_t)amgGlobalN_ + 1, 0);
5973 for (
int r = 0; r < amgGlobalN_; ++r)
5974 A.start[(std::size_t)r + 1] = A.start[(std::size_t)r] + (
long)crow[(std::size_t)r].size();
5975 A.nbr.reserve(rows.row.size());
5976 A.coef.reserve(rows.row.size());
5977 for (
int r = 0; r < amgGlobalN_; ++r)
5978 for (
auto& [c, v] : crow[(std::size_t)r]) {
5980 A.coef.push_back(v);
5989 for (
int r = 0; r < amgGlobalN_; ++r)
5990 if (!amgSolid_[(std::size_t)r] && !crow[(std::size_t)r].empty()) {
5992 for (
auto& [c, v] : crow[(std::size_t)r])
5994 A.diag[(std::size_t)r] = -s;
6003 if (!amg_ && !distributed_)
6005#ifdef PECLET_FLOW_MPI
6006 if (distributed_ && amgGlobalN_ == 0)
6009 const int nx = lv.inner.x, ny = lv.inner.y, nz = lv.inner.z, ex = lv.ext.x, ey = lv.ext.y;
6011 auto hrhs = Kokkos::create_mirror_view(lv.rhs);
6012 Kokkos::deep_copy(hrhs, lv.rhs);
6013 std::vector<double> lb;
6014 lb.reserve(amgGlobalOfLocal_.size());
6015 for (
int k = 0; k < nz; ++k)
6016 for (
int j = 0; j < ny; ++j)
6017 for (
int i = 0; i < nx; ++i)
6018 lb.push_back((
double)hrhs((
long)(i + g) + (
long)(j + g) * ex + (
long)(k + g) * ex * ey));
6020 std::vector<double> z((std::size_t)std::max(amgGlobalN_, 1), 0.0);
6021#ifdef PECLET_FLOW_MPI
6023 std::vector<int> ggid;
6024 std::vector<double> gb;
6025 gatherv(amgGlobalOfLocal_, ggid, lv.comm);
6026 gatherv(lb, gb, lv.comm);
6027 std::vector<double> b((std::size_t)amgGlobalN_, 0.0);
6028 for (std::size_t r = 0; r < ggid.size(); ++r)
6029 b[(std::size_t)ggid[r]] = gb[r];
6034 std::vector<double> b(lb.begin(), lb.end());
6038 auto hx = Kokkos::create_mirror_view(lv.x);
6039 Kokkos::deep_copy(hx, 0.0);
6041 for (
int k = 0; k < nz; ++k)
6042 for (
int j = 0; j < ny; ++j)
6043 for (
int i = 0; i < nx; ++i)
6044 hx((
long)(i + g) + (
long)(j + g) * ex + (
long)(k + g) * ex * ey) =
6045 z[(std::size_t)amgGlobalOfLocal_[c++]];
6046 Kokkos::deep_copy(lv.x, hx);
6047 if (agmgDebug() && !distributed_) {
6054 FPC(lv.AFY),
FPC(lv.AFZ), lv.ext, lv.g);
6055 const double rn =
maxabs(lv, lv.res);
6056 auto hb = Kokkos::create_mirror_view(lv.rhs);
6057 Kokkos::deep_copy(hb, lv.rhs);
6059 for (std::size_t i = 0; i < hb.size(); ++i)
6060 bn = std::max(bn, std::fabs((
double)hb(i)));
6061 printf(
"[agmg] vcycle-op residual of CSR solution: max|b-Ax|=%.3e max|b|=%.3e rel=%.3e\n",
6062 rn, bn, bn > 0 ? rn / bn : 0.0);
6073 const std::size_t n = (std::size_t)amgGlobalN_;
6076 double sf = 0, sa = 0;
6077 for (std::size_t i = 0; i < n; ++i) {
6079 if (i < amgSolid_.size() && amgSolid_[i])
6080 a.bSolidMax = std::max(a.bSolidMax, std::fabs(b[i]));
6082 a.bFluidMax = std::max(a.bFluidMax, std::fabs(b[i]));
6087 a.bFluidMean = nf ? sf / (double)nf : 0.0;
6088 a.bAllMean = n ? sa / (double)n : 0.0;
6089 if (amgNComp_ > 1) {
6090 std::vector<double> cs((std::size_t)amgNComp_, 0.0);
6091 for (std::size_t i = 0; i < n; ++i)
6092 if (amgComp_[i] >= 0)
6093 cs[(std::size_t)amgComp_[i]] += b[i];
6095 a.bCompMax = std::max(a.bCompMax, std::fabs(s));
6096 a.bCompMax /= (a.bFluidMax > 0 ? a.bFluidMax : 1.0);
6113 const std::size_t n = (std::size_t)amgGlobalN_;
6114 std::vector<double> m((std::size_t)amgNComp_, 0.0);
6115 std::vector<long> cnt((std::size_t)amgNComp_, 0);
6116 for (std::size_t i = 0; i < n; ++i)
6117 if (amgComp_[i] >= 0) {
6118 m[(std::size_t)amgComp_[i]] += v[i];
6119 ++cnt[(std::size_t)amgComp_[i]];
6121 for (std::size_t c = 0; c < m.size(); ++c)
6122 m[c] = cnt[c] ? m[c] / (
double)cnt[c] : 0.0;
6123 for (std::size_t i = 0; i < n; ++i)
6124 if (amgComp_[i] >= 0)
6125 v[i] -= m[(std::size_t)amgComp_[i]];
6128 static double amgDot(
const std::vector<double>& a,
const std::vector<double>& c, std::size_t n) {
6130 for (std::size_t i = 0; i < n; ++i)
6138 int it,
double r0,
int dbg) {
6139 const std::size_t n = (std::size_t)amgGlobalN_;
6140 double xSolidMax = 0, xFluidMax = 0;
6141 for (std::size_t i = 0; i < n; ++i)
6142 if (i < amgSolid_.size() && amgSolid_[i])
6143 xSolidMax = std::max(xSolidMax, std::fabs(x[i]));
6145 xFluidMax = std::max(xFluidMax, std::fabs(x[i]));
6146 const double rn = std::sqrt(
amgDot(r, r, n));
6148 if (dbg >= 2 || agmgCalls_ <= 60 || it >= 100 || agmgCalls_ % 50 == 0) {
6150 "[agmg] call=%ld iters=%d relres=%.2e |b|sol=%.3e |b|fl=%.3e "
6151 "mean(b) fl=%.3e all=%.3e compat=%.2e |x|sol=%.3e |x|fl=%.3e%s\n",
6152 agmgCalls_, it, r0 > 0 ? rn / r0 : 0.0, a.bSolidMax, a.bFluidMax, a.bFluidMean,
6153 a.bAllMean, a.bCompMax, xSolidMax, xFluidMax, it >= 100 ?
" CAP" :
"");
6163 void pcgAmg(std::vector<double>& b, std::vector<double>& x) {
6164 const std::size_t n = (std::size_t)amgGlobalN_;
6165 const int dbg = agmgDebug();
6169 std::vector<double> r = b, z(n), p(n), Ap(n);
6173 double rz =
amgDot(r, z, n), r0 = std::sqrt(
amgDot(r, r, n));
6179 for (; it < 100 && r0 > 0; ++it) {
6188 const double pAp =
amgDot(p, Ap, n);
6191 const double a = rz / pAp;
6192 for (std::size_t i = 0; i < n; ++i) {
6196 if (std::sqrt(
amgDot(r, r, n)) <= 1e-8 * r0)
6200 const double rzn =
amgDot(r, z, n);
6201 const double beta = rzn / rz;
6203 for (std::size_t i = 0; i < n; ++i)
6204 p[i] = z[i] + beta * p[i];
6210#ifdef PECLET_FLOW_MPI
6212 void gatherv(
const std::vector<T>& local, std::vector<T>& all, MPI_Comm c) {
6217 MPI_Comm_size(c, &size);
6218 const int lbytes = (int)(local.size() *
sizeof(T));
6219 std::vector<int> bc(size), bd(size, 0);
6220 MPI_Allgather(&lbytes, 1, MPI_INT, bc.data(), 1, MPI_INT, c);
6222 for (
int r = 0; r < size; ++r) {
6226 all.resize((std::size_t)tot /
sizeof(T));
6227 MPI_Allgatherv(local.data(), lbytes, MPI_BYTE, all.data(), bc.data(), bd.data(), MPI_BYTE, c);
6236 const bool ex = exactResidual_;
6239#ifdef PECLET_FLOW_MPI
6241 const int m = stencilWrap(l0);
6243 auto apply = [&](
C3 rlo,
C3 rhi,
C3 slo,
C3 shi) {
6246 l0.
ext, rlo, rhi, slo, shi, gfx_, gfy_, gfz_, m,
G);
6249 l0.
ext, rlo, rhi, slo, shi, m,
G);
6252 apply(lo, hi,
C3{0, 0, 0},
C3{0, 0, 0});
6255 GridHalo<double>& h = stencilHalo(l0);
6257 apply(lo, hi,
C3{0, 0, 0},
C3{0, 0, 0});
6285#ifdef PECLET_FLOW_MPI
6287 lv.dev->exchange(f);
6296 if (lv.inner.x < lv.g || lv.inner.y < lv.g || lv.inner.z < lv.g) {
6310#ifdef PECLET_FLOW_MPI
6312 lv.devf->exchange(f);
6329 const C3 e = lv.ext;
6331 const int nx = lv.inner.x, ny = lv.inner.y, nz = lv.inner.z;
6332 const long exy = (long)e.x * e.y;
6335 const long nZ = 2L *
G * exy, nY = (long)nz * 2 *
G * e.x, nX = (
long)nz * ny * 2 *
G;
6336 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>) {
6343 const long rZ = 2L *
G * e.y, rY = (long)nz * 2 *
G, rX = (
long)nz * ny;
6345 auto copyRow = [=](
long dst,
long src) {
6346 for (
int x = 0; x <
G; ++x)
6347 ff(dst + x) = ff(src + x + nx);
6348 for (
int x =
G; x <
G + nx; ++x)
6349 ff(dst + x) = ff(src + x);
6350 for (
int x =
G + nx; x < e.x; ++x)
6351 ff(dst + x) = ff(src + x - nx);
6353 auto row = [=](
long t) {
6355 const int l = (int)(t / e.y), y = (int)(t - (
long)l * e.y);
6356 const int z = l <
G ? l : nz + l;
6357 const int sy = y <
G ? y + ny : (y >=
G + ny ? y - ny : y);
6358 const int sz = z <
G ? z + nz : z - nz;
6359 copyRow((
long)y * e.x + (
long)z * exy, (
long)sy * e.x + (
long)sz * exy);
6360 }
else if (t < rZ + rY) {
6361 const long t2 = t - rZ;
6362 const int zi = (int)(t2 / (2 *
G)), l = (int)(t2 - (
long)zi * 2 *
G);
6363 const int y = l <
G ? l : ny + l, z =
G + zi;
6364 const int sy = y <
G ? y + ny : y - ny;
6365 copyRow((
long)y * e.x + (
long)z * exy, (
long)sy * e.x + (
long)z * exy);
6367 const long t3 = t - rZ - rY;
6368 const int zi = (int)(t3 / ny), yi = (int)(t3 - (
long)zi * ny);
6369 const long base = (long)(
G + yi) * e.x + (long)(
G + zi) * exy;
6370 for (
int x = 0; x <
G; ++x)
6371 ff(base + x) = ff(base + x + nx);
6372 for (
int x =
G + nx; x < e.x; ++x)
6373 ff(base + x) = ff(base + x - nx);
6378 for (
long t = 0; t < rZ + rY + rX; ++t)
6382 Kokkos::parallel_for(
"peclet::flow::mg_pfill3",
6383 Kokkos::RangePolicy<CCExec>(space, 0, rZ + rY + rX), row);
6387 Kokkos::parallel_for(
6388 "peclet::flow::mg_pfill3", Kokkos::RangePolicy<CCExec>(space, 0, nZ + nY + nX),
6389 KOKKOS_LAMBDA(
long t) {
6392 const int l = (int)(t / exy);
6393 const long r = t - (long)l * exy;
6396 z = l <
G ? l : nz + l;
6397 }
else if (t < nZ + nY) {
6398 const long t2 = t - nZ, row = 2L *
G * e.x;
6399 const long zi = t2 / row, r = t2 - zi * row;
6400 const int l = (int)(r / e.x);
6402 y = l <
G ? l : ny + l;
6405 const long t3 = t - nZ - nY, row = (long)ny * 2 *
G;
6406 const long zi = t3 / row, r = t3 - zi * row;
6407 const int l = (int)(r % (2 *
G));
6408 x = l <
G ? l : nx + l;
6409 y =
G + (int)(r / (2 *
G));
6412 const int sx = x <
G ? x + nx : (x >=
G + nx ? x - nx : x);
6413 const int sy = y <
G ? y + ny : (y >=
G + ny ? y - ny : y);
6414 const int sz = z <
G ? z + nz : (z >=
G + nz ? z - nz : z);
6415 ff((
long)x + (
long)y * e.x + (
long)z * exy) =
6416 ff((
long)sx + (
long)sy * e.x + (
long)sz * exy);
6428 int N3[3] = {lv.inner.x, lv.inner.y, lv.inner.z};
6429 int dims[3] = {e.x, e.y, e.z};
6430 long st[3] = {1, e.x, (long)e.x * e.y};
6431 const int a = axis, b = (axis + 1) % 3, c = (axis + 2) % 3;
6432 const long sa = st[a], sb = st[b], sc = st[c];
6433 const int N = N3[a];
6435 Kokkos::parallel_for(
6436 "peclet::flow::mg_pfill",
MDRange2<CCExec>(space, {0, 0}, {dims[b], dims[c]}),
6437 KOKKOS_LAMBDA(
int p0,
int p1) {
6438 const long base = (long)p0 * sb + (
long)p1 * sc;
6439 for (
int gl = 0; gl <
G; ++gl) {
6440 ff(base + (
long)gl * sa) = ff(base + (
long)(gl +
N) * sa);
6441 ff(base + (
long)(
G +
N + gl) * sa) = ff(base + (
long)(
G + gl) * sa);
6445#ifdef PECLET_FLOW_MPI
6450 const C3 e1 = l0.ext, nn = l0.
inner;
6452 Kokkos::parallel_for(
6453 "peclet::flow::gp_stage_g2",
MDRange3<CCExec>(space, {0, 0, 0}, {nn.x, nn.y, nn.z}),
6454 KOKKOS_LAMBDA(
int x,
int y,
int z) {
6455 dst((
long)(x + 2) + (
long)(y + 2) * ext2.
x + (
long)(z + 2) * (
long)ext2.
x * ext2.
y) =
6456 src((
long)(x +
G) + (
long)(y +
G) * e1.
x + (
long)(z +
G) * (
long)e1.
x * e1.
y);
6463 const C3 e1 = l0.ext;
6464 const C3 ext2{e1.
x + 2, e1.
y + 2, e1.
z + 2};
6466 Kokkos::parallel_for(
6468 KOKKOS_LAMBDA(
int x,
int y,
int z) {
6469 dst((
long)x + (
long)y * e1.
x + (
long)z * (
long)e1.
x * e1.
y) =
6470 src((
long)(x + 1) + (
long)(y + 1) * ext2.
x + (
long)(z + 1) * (
long)ext2.
x * ext2.
y);
6477 std::size_t n = y.extent(0);
6478 Kokkos::parallel_for(
6479 "mgaxpy", Kokkos::RangePolicy<CCExec>(space, 0, n),
6480 KOKKOS_LAMBDA(std::size_t i) { yy(i) += a * xx(i); });
6485 std::size_t n = y.extent(0);
6486 Kokkos::parallel_for(
6487 "mgaypx", Kokkos::RangePolicy<CCExec>(space, 0, n),
6488 KOKKOS_LAMBDA(std::size_t i) { yy(i) = xx(i) + a * yy(i); });
6493 std::size_t n = y.extent(0);
6494 Kokkos::parallel_for(
6495 "mgscale", Kokkos::RangePolicy<CCExec>(space, 0, n),
6496 KOKKOS_LAMBDA(std::size_t i) { yy(i) *= a; });
6500 CCField oo = out, xx = x, yy = y;
6501 std::size_t n = out.extent(0);
6502 Kokkos::parallel_for(
6503 "mglin", Kokkos::RangePolicy<CCExec>(space, 0, n),
6504 KOKKOS_LAMBDA(std::size_t i) { oo(i) = a * xx(i) + b * yy(i); });
6511 std::size_t n = f.extent(0);
6512 Kokkos::parallel_for(
6513 "mgmasksolid", Kokkos::RangePolicy<CCExec>(space, 0, n), KOKKOS_LAMBDA(std::size_t i) {
6514 if (!(ac(i) > 1e-30f))
6524 const C3 e = lv.ext, og = lv.og;
6527 Kokkos::parallel_for(
6528 "peclet::flow::mg_seed_noise",
6530 KOKKOS_LAMBDA(
int x,
int y,
int z) {
6531 std::uint64_t h = (std::uint64_t)(og.x + x - g) * 0x9E3779B97F4A7C15ull ^
6532 (std::uint64_t)(og.y + y - g) * 0xC2B2AE3D27D4EB4Full ^
6533 (std::uint64_t)(og.z + z - g) * 0x165667B19E3779F9ull;
6535 h *= 0xBF58476D1CE4E5B9ull;
6537 h *= 0x94D049BB133111EBull;
6539 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
6540 ff(i) += amp * ((double)(h >> 11) * 0x1.0p-53 - 0.5);
6550 if (v.extent(0) != n)
6553 Kokkos::deep_copy(
CCExec(), v, 0.0);
6574 return eigWarm_ && !lv_.empty() && eigVmax_.extent(0) == lv_[0].n &&
6575 eigVmin_.extent(0) == lv_[0].n;
6601 const std::size_t n = l0.
n;
6610 auto tridiag = [&](
int m,
double* d,
double* e) {
6611 for (
int j = 0; j < m; ++j) {
6612 d[j] = 1.0 / alpha[j] + (j > 0 ? beta[j - 1] / alpha[j - 1] : 0.0);
6613 e[j] = j + 1 < m ? std::sqrt(beta[j]) / alpha[j] : 0.0;
6618 auto pass = [&](
int mFixed,
const double* c,
CCField acc,
double& top) {
6619 Kokkos::deep_copy(
CCExec(), r, v);
6621 double rho =
dot(l0, r, z);
6622 Kokkos::deep_copy(
CCExec(), p, z);
6631 if (k + 1 == mFixed)
6636 const double pAp =
dot(l0, p, w);
6639 alpha[k] = rho / pAp;
6649 axpy(r, -alpha[k], w);
6652 const double rhoNew =
dot(l0, r, z);
6653 beta[k] = rhoNew / rho;
6655 lin(p, 1.0, z, beta[k], p);
6660 const int m = pass(0,
nullptr,
CCField(), top);
6661 if (ritz && m > 0) {
6665 for (
int j = 0; j < m; ++j)
6666 c[j] = ((j & 1) ? -y[j] : y[j]) / std::sqrt(rhoj[j]);
6668 double unused = 0.0;
6669 pass(m, c, u, unused);
6670 Kokkos::deep_copy(
CCExec(), v, u);
6671 const double nr = std::sqrt(
dot(l0, v, v));
6680 static double symTridiagTop(
int m,
const double* d,
const double* e,
double* y) {
6682 for (
int i = 0; i < m; ++i) {
6686 A[i][i + 1] = A[i + 1][i] = e[i];
6689 double off = 0.0, nrm = 0.0;
6690 for (
int i = 0; i < m; ++i)
6691 for (
int j = 0; j < m; ++j)
6692 (i == j ? nrm : off) += A[i][j] * A[i][j];
6693 if (!(off > 1e-30 * nrm))
6695 for (
int pq = 0; pq < m; ++pq)
6696 for (
int q = pq + 1; q < m; ++q) {
6697 if (A[pq][q] == 0.0)
6699 const double th = 0.5 * (A[q][q] - A[pq][pq]) / A[pq][q];
6700 const double t = (th >= 0 ? 1.0 : -1.0) / (std::fabs(th) + std::sqrt(th * th + 1.0));
6701 const double c = 1.0 / std::sqrt(t * t + 1.0), sn = t * c;
6702 for (
int k = 0; k < m; ++k) {
6703 const double akp = A[k][pq], akq = A[k][q];
6704 A[k][pq] = c * akp - sn * akq;
6705 A[k][q] = sn * akp + c * akq;
6707 for (
int k = 0; k < m; ++k) {
6708 const double apk = A[pq][k], aqk = A[q][k];
6709 A[pq][k] = c * apk - sn * aqk;
6710 A[q][k] = sn * apk + c * aqk;
6712 for (
int k = 0; k < m; ++k) {
6713 const double vkp = V[k][pq], vkq = V[k][q];
6714 V[k][pq] = c * vkp - sn * vkq;
6715 V[k][q] = sn * vkp + c * vkq;
6720 for (
int i = 1; i < m; ++i)
6721 if (A[i][i] > A[top][top])
6724 for (
int i = 0; i < m; ++i)
6726 return m > 0 ? A[top][top] : 0.0;
6734 Kokkos::deep_copy(
CCExec(), srhs, seed);
6736 const double sm =
maxabs(l0, srhs);
6750 double nr = std::sqrt(
dot(l0, x, x));
6757 Kokkos::deep_copy(
CCExec(), x, src);
6764 if (dst.extent(0) != n)
6766 Kokkos::deep_copy(
CCExec(), dst, x);
6772 for (
int k = 0; k < iters; ++k) {
6780 double wz = 0.0, wv = 0.0;
6782 lmax = wv > 0.0 ? wz / wv : 0.0;
6784 lmax =
dot(l0, v, z);
6786 Kokkos::deep_copy(
CCExec(), v, z);
6795 for (
int k = 0; k < iters; ++k) {
6797 lin(z, shift, v, -1.0, z);
6799 Kokkos::deep_copy(
CCExec(), v, z);
6806 void eigBracket(
double shift,
double mu,
double& lmin,
double& lmax) {
6807 double e_hi = lmax, e_lo = shift - mu;
6808 lmin = e_lo < e_hi ? e_lo : e_hi;
6809 lmax = e_lo < e_hi ? e_hi : e_lo;
6810 if (lmin < 0.02 * lmax)
6812 if (dbgEigScale_ != 1.0) {
6813 lmin *= dbgEigScale_;
6814 lmax *= dbgEigScale_;
6826 solveFailed_ =
false;
6832 const std::size_t n = l0.
n;
6838 const double nrMax =
eigSeed(l0, v, warm ? eigVmax_ : srhs);
6839 if (symVcycle_ && !warm) {
6848 eigKeep(eigVmax_, v,
"ev_vmax", n);
6856 if (bracket > 1.0) {
6861 const double nrMin =
eigSeed(l0, v, warm ? eigVmin_ : srhs);
6862 const double mu =
eigPowerMin(l0, v, w, z, iters, shift);
6864 eigKeep(eigVmin_, v,
"ev_vmin", n);
6869 nrMax > 0.0 && nrMin > 0.0 && std::isfinite(lmax) && lmax > 0.0 && std::isfinite(mu);
6897 double a,
double bnd,
bool guard3 =
false,
bool guardGrowth =
false) {
6898 solveFailed_ =
false;
6899 chebGuardTripped_ =
false;
6904 const std::size_t n = l0.
n;
6916 const double theta = 0.5 * (bnd + a), delta = 0.5 * (bnd - a), sigma1 = theta / delta;
6917 double rho = 1.0 / sigma1;
6919 Kokkos::deep_copy(
CCExec(), r, b);
6922 const double r0 =
maxabs(l0, r);
6926 const double rref = stopRef_ > 0.0 ? stopRef_ : r0;
6932 auto launchRz = [&]() {
6942 lin(d, 1.0 / theta, z, 0.0, z);
6944 for (
int i = 1; i < maxit; ++i) {
6948 double rhoPrev = 0.0;
6951 if (rn < rtol * rref)
6956 else if (!(rhoPrev <= rho0)) {
6957 chebGuardTripped_ =
true;
6961 if (guard3 && i == 3 && !(rn <= r0)) {
6962 chebGuardTripped_ =
true;
6969 const double rho_new = 1.0 / (2.0 * sigma1 - rho);
6970 lin(d, rho_new * rho, d, 2.0 * rho_new / delta, z);
6983 "[mg] cheb solve %d: r0=%.6e rref=%.6e rtol=%.1e |r|inf=%.6e r/rref=%.4e vcycles=%d "
6984 "maxit=%d guard=%d\n",
6985 dbgSolve_, r0, rref, rtol, rnLast, rref > 0.0 ? rnLast / rref : 0.0, nvc, maxit,
6986 chebGuardTripped_ ? 1 : 0);
7021 Kokkos::deep_copy(scratch, b);
7023 return maxabs(l0, scratch);
7029 Kokkos::deep_copy(r, b);
7043 const StarOverlay* star =
nullptr,
int nStar = 0,
C3 nnStar =
C3{0, 0, 0}) {
7044 if (!(cgStopRef_ > 0.0) || !(cgStopRtol_ > 0.0))
7047 OverlayScope overlay(overlaySolve_, star !=
nullptr);
7051 Kokkos::deep_copy(
CCExec(), r, b);
7054 return maxabs(l0, r) / (cgStopRtol_ * cgStopRef_);
7066 "mgdot",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
7067 KOKKOS_LAMBDA(
int x,
int y,
int z,
double& acc) {
7068 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
7069 acc += aa(i) * bb(i);
7074 "mgdot",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
7075 KOKKOS_LAMBDA(
int x,
int y,
int z,
double& acc) {
7076 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
7078 acc += aa(i) * bb(i);
7081#ifdef PECLET_FLOW_MPI
7082 return allreduce(s, MPI_SUM_, lv.comm);
7084 return allreduce(s, MPI_SUM_);
7092 CCField aa = a, bb = b, cc = c;
7098 "mgdot2",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
7099 KOKKOS_LAMBDA(
int x,
int y,
int z,
double& sb,
double& sc) {
7100 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
7101 if (all || fl(i) > 1e-30f) {
7102 sb += aa(i) * bb(i);
7103 sc += aa(i) * cc(i);
7107 allreduceSum2(ab, ac
7108#ifdef PECLET_FLOW_MPI
7115#ifdef PECLET_FLOW_MPI
7116 return allreduce(
maxabsLocal(lv, a), MPI_MAX_, lv.comm);
7129#ifdef PECLET_FLOW_MPI
7131 const auto t0 = std::chrono::steady_clock::now();
7132 double v[2] = {m, sum}, g[2] = {0.0, 0.0};
7133 MPI_Allreduce(v, g, 2, MPI_DOUBLE, maxSumOp(), lv.comm);
7135 std::chrono::duration<double>(std::chrono::steady_clock::now() - t0).count();
7143#ifdef PECLET_FLOW_MPI
7145 static MPI_Op maxSumOp() {
7146 static MPI_Op op = [] {
7149 [](
void* in,
void* inout,
int* len, MPI_Datatype*) {
7150 const double* a =
static_cast<const double*
>(in);
7151 double* b =
static_cast<double*
>(inout);
7152 for (
int k = 0; k + 1 < *len; k += 2) {
7153 b[k] = a[k] > b[k] ? a[k] : b[k];
7154 b[k + 1] += a[k + 1];
7173 "mgmax",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
7174 KOKKOS_LAMBDA(
int x,
int y,
int z,
double& acc) {
7175 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
7176 const double v = Kokkos::fabs(aa(i));
7180 Kokkos::Max<double>(m));
7183 "mgmax",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
7184 KOKKOS_LAMBDA(
int x,
int y,
int z,
double& acc) {
7185 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
7186 if (ac(i) > 1e-30f) {
7187 const double v = Kokkos::fabs(aa(i));
7192 Kokkos::Max<double>(m));
7208 const bool all = lv.allFluid;
7211 "mgmeanr",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
7212 KOKKOS_LAMBDA(
int x,
int y,
int z,
double& s) {
7213 s += ff((
long)x + (
long)y * e.x + (
long)z * (
long)e.x * e.y);
7215 Kokkos::Sum<double, CCMem>(
slot(
kMsum)));
7218 "mgmeanr",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
7219 KOKKOS_LAMBDA(
int x,
int y,
int z,
double& s) {
7220 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
7224 Kokkos::Sum<double, CCMem>(
slot(
kMsum)));
7233 const bool all = lv.allFluid;
7235 const long cnt = lv.nFluid;
7236 const bool guard = stopGuard;
7238 "mgmeans",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g}, KOKKOS_LAMBDA(
int x,
int y,
int z) {
7239 if (cnt == 0 || (guard && ks(
kStop) != 0.0))
7241 const double mean = ks(
kMsum) / (double)cnt;
7242 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
7255 const C3 e = lv.ext;
7257 CCField zz = lv.x, rr = lv.rhs, pp = p;
7259 const bool all = lv.allFluid, copyP = p.extent(0) != 0;
7260 const long cnt = lv.nFluid;
7263 "mgmeans_dot",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
7264 KOKKOS_LAMBDA(
int x,
int y,
int zc,
double& acc) {
7265 const long i = (long)x + (
long)y * e.x + (long)zc * (
long)e.x * e.y;
7267 const double mean = ks(
kMsum) / (double)cnt;
7275 if (all || ac(i) > 1e-30f)
7276 acc += rr(i) * zz(i);
7295 auto body = KOKKOS_LAMBDA(
int x,
int y,
int z,
double& s,
long& k) {
7296 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
7297 if (ac(i) > 1e-30f) {
7302 if (!distributed_) {
7311 const bool devCounted = !
kHostMemory && lv.nFluidDevValid;
7314 "mgmeanr",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
7315 KOKKOS_LAMBDA(
int x,
int y,
int z,
double& s) {
7316 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
7320 Kokkos::Sum<double, CCMem>(
slot(
kMsum)));
7323 Kokkos::Sum<double, CCMem>(
slot(
kMsum)), Kokkos::Sum<long, CCMem>(
kcnt_));
7325 auto kc = devCounted ? lv.nFluidDev :
kcnt_;
7326 const bool guard = stopGuard;
7328 "mgmeans",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
7329 KOKKOS_LAMBDA(
int x,
int y,
int z) {
7330 const long cnt = kc();
7331 if (cnt == 0 || (guard && ks(
kStop) != 0.0))
7333 const double mean = ks(
kMsum) / (double)cnt;
7334 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
7341 ccReduce3Lanes(
"mgmeanr",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g}, body, sum, cnt);
7346 "mgmeans",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g}, KOKKOS_LAMBDA(
int x,
int y,
int z) {
7347 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
7355 double dcnt = (double)cnt;
7356 allreduceSum2(sum, dcnt
7357#ifdef PECLET_FLOW_MPI
7365 mean = sum / (double)cnt;
7390 const C3 e = l0.
ext;
7392 CCField xx = x, rr = r, pp = p, aa = Ap;
7394 const double a = alpha, na = -alpha;
7398 "mgpcg_update_sum",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
7399 KOKKOS_LAMBDA(
int x_,
int y,
int z,
double& s,
long& k) {
7400 const long i = (long)x_ + (
long)y * e.x + (long)z * (
long)e.x * e.y;
7402 rr(i) += na * aa(i);
7403 if (ac(i) > 1e-30f) {
7413 "mgmeans_max",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
7414 KOKKOS_LAMBDA(
int x_,
int y,
int z,
double& acc) {
7415 const long i = (long)x_ + (
long)y * e.x + (long)z * (
long)e.x * e.y;
7418 if (ac(i) > 1e-30f) {
7419 const double v = Kokkos::fabs(rr(i));
7424 Kokkos::Max<double>(m));
7425#ifdef PECLET_FLOW_MPI
7426 return allreduce(m, MPI_MAX_, l0.comm);
7428 return allreduce(m, MPI_MAX_);
7436 const C3 e = lv.ext;
7438 CCField zz = lv.x, rr = lv.rhs;
7443 "mgmeanr",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
7444 KOKKOS_LAMBDA(
int x,
int y,
int z,
double& s,
long& k) {
7445 const long i = (long)x + (
long)y * e.x + (long)z * (
long)e.x * e.y;
7446 if (ac(i) > 1e-30f) {
7456 "mgmeans_dot",
C3{g, g, g},
C3{e.x - g, e.y - g, e.z - g},
7457 KOKKOS_LAMBDA(
int x,
int y,
int zc,
double& d) {
7458 const long i = (long)x + (
long)y * e.x + (long)zc * (
long)e.x * e.y;
7484 return dot(l0, r, z);
7485#ifdef PECLET_FLOW_MPI
7486 return allreduce(
exitDotDist_.local, MPI_SUM_, l0.comm);
7501 meanRemovalAll_ = all;
7516 if (bottomFlagPending_)
7518 return solveFailed_;
7521 allreduceTime_ = 0.0;
7522 allreduceCount_ = 0;
7526 enum AllOp { kSum, kMax };
7527#ifdef PECLET_FLOW_MPI
7528 using MPI_Comm_or_void = MPI_Comm;
7529 static MPI_Comm nullptr_comm() {
return MPI_COMM_NULL; }
7531 using MPI_Comm_or_void =
void*;
7532 static void* nullptr_comm() {
return nullptr; }
7536 double allreduce(
double v, AllOp op, MPI_Comm_or_void c = nullptr_comm()) {
7537#ifdef PECLET_FLOW_MPI
7539 const auto t0 = std::chrono::steady_clock::now();
7541 MPI_Allreduce(&v, &g, 1, MPI_DOUBLE, op == kSum ? MPI_SUM : MPI_MAX,
7542 c == nullptr_comm() ? comm_ : c);
7544 std::chrono::duration<double>(std::chrono::steady_clock::now() - t0).count();
7554 void allreduceSum2(
double& a,
double& b, MPI_Comm_or_void c = nullptr_comm()) {
7555#ifdef PECLET_FLOW_MPI
7557 const auto t0 = std::chrono::steady_clock::now();
7558 double v[2] = {
a,
b},
g[2] = {0.0, 0.0};
7559 MPI_Allreduce(v, g, 2, MPI_DOUBLE, MPI_SUM, c == nullptr_comm() ? comm_ : c);
7561 std::chrono::duration<double>(std::chrono::steady_clock::now() - t0).count();
7568 static constexpr AllOp MPI_SUM_ = kSum, MPI_MAX_ = kMax;
7570 std::vector<Level> lv_;
7575 bool eigWarm_ =
false;
7576 bool chebGuardTripped_ =
false;
7580 double hp_[3] = {1.0, 1.0, 1.0};
7581 bool aniso_ =
false;
7582 int pre_ = 2, post_ = 2, bottom_ = 4;
7583 int bc_[6] = {0, 0, 0, 0, 0, 0};
7584 bool hasBC_ =
false, removeMean_ =
true, hasOutflow_ =
false;
7589 bool outflowCoeff_ =
false;
7594 bool meanRemovalAll_ =
false;
7595 bool distributed_ =
false;
7596 bool stencilHaloOff_ =
false;
7597 bool overlaySolve_ =
false;
7598 int dbgBrkWhich_ = 0, dbgBrkIter_ = -1;
7600 long eigenEstimates_ = 0;
7601 double dbgEigScale_ = 1.0;
7602 int dbgBicgBreakdowns_ = 0;
7603 int dbgBicgStagnations_ = 0;
7607 bool solveFailed_ =
false;
7611 CCField bottomR_, bottomP_, bottomZ_, bottomZp_, bottomAp_;
7612 Kokkos::View<int*, CCMem> bottomInfo_;
7613 bool bottomConnKnown_ =
false, bottomConnected_ =
false;
7614 int bottomNComp_ = 0;
7615 Kokkos::View<int*, CCMem> bottomComp_, bottomLabTmp_;
7616 Kokkos::View<long*, CCMem> bottomCompCnt_;
7618 bool bottomFlagPending_ =
false;
7619 int bottomSolver_ = 0;
7624 bool bottomStore_ =
false;
7629 bool gbActive_ =
false;
7630 std::vector<int> gbLocal_, gbGid_;
7633 std::vector<int> gbCnt1_, gbDsp1_, gbCnt3_, gbDsp3_;
7636 bool dirAgreedKnown_ =
false;
7637 int dirAgreedCode_ = 1;
7638 C3 dirAgreedGdim_{}, dirAgreedExt_{};
7639 BottomPlanes dirPlanes_;
7640 BottomDirect<float> dir_;
7641 Kokkos::View<int*, CCMem> dirKc_;
7642 Kokkos::View<double*, CCMem> dirAug_;
7643 bool facStale_ =
true;
7644 int dirTeam_ = 0, dirFacTeam_ = 0;
7645 std::optional<CCExec> side_;
7646 mutable bool facPending_ =
false;
7647 long prefactorLaunches_ = 0;
7649 double stopRef_ = -1.0;
7650 double cgStopRef_ = 0.0, cgStopRtol_ = 0.0;
7651 bool zeroGuess_ =
false;
7654 double guessC1_ = 0.0, guessC2_ = 0.0;
7655 CCField guessX0_, guessTA_, guessTB_;
7656 std::vector<double> lvTime_;
7663 bool bcGhost_ =
true;
7667 double aspectTheta_ = 2.0;
7670 bool symVcycle_ =
true;
7678 bool strictPressure_ =
false;
7681 bool exactResidual_ =
false;
7688 bool opBuilt_ =
false;
7690 Kokkos::View<int*, CCMem> wBad_;
7692 mutable Kokkos::View<int*, Kokkos::SharedHostPinnedSpace> wBadHost_;
7693 mutable bool wBadStale_ =
false;
7696 bool fp64Stale_ =
false;
7697 bool lastVcycleFp32_ =
false;
7703 bool fp32Latched_ =
false;
7704 bool lastSolveConverged_ =
false;
7705 int attemptMaxit_ = 0;
7706 int vcycleRestarts_ = 0;
7707 int lastDiscardedIters_ = 0;
7710 int fp32FaultKind_ = 0, fp32FaultAt_ = 0, fp32FaultCount_ = 0;
7711 bool healthTrace_ =
false;
7712 std::vector<double> healthLog_;
7715 int agglomExtent_ = 4;
7716 double allreduceTime_ = 0.0;
7717 long allreduceCount_ = 0;
7726 int agglomMode_ = 0;
7731 bool telescope_ =
true;
7732 int teleForce_ = -1;
7733 bool teleActive_ =
true;
7738 int teleMinExtent_ = 4;
7743 bool repartition_ =
false;
7744 int gnxF_ = 0, gnyF_ = 0, gnzF_ = 0;
7747 double gfx_ = 1.0, gfy_ = 1.0, gfz_ = 1.0;
7748 mutable std::shared_ptr<peclet::core::solver::GraphAMG>
7750 mutable peclet::core::solver::HostCsrOp
7752 mutable std::vector<int> amgOwnerCount_;
7753 mutable std::vector<int>
7755 mutable int amgGlobalN_ = 0;
7757 mutable std::vector<std::uint8_t> amgSolid_;
7758 mutable std::vector<int> amgComp_;
7759 mutable int amgNComp_ = 0;
7760 mutable long agmgCalls_ = 0;
7761 static int agmgDebug() {
7762 static const int v = [] {
7763 const char*
e = std::getenv(
"PECLET_FLOW_AGMG_DEBUG");
7764 return e ? std::atoi(e) : 0;
7768#ifdef PECLET_FLOW_MPI
7769 MPI_Comm comm_ = MPI_COMM_NULL;
7788 if (on != lineRelax_)
7800 if (on != symVcycle_)
7815 exactResidual_ = on;
7829#ifdef PECLET_FLOW_MPI
7830 for (
const Level& l : lv_)
7831 n += l.tele ? 1 : 0;
7838#ifdef PECLET_FLOW_MPI
7839 for (
const Level& l : lv_)
7840 n += (l.tele && l.tele->repartition()) ? 1 : 0;
7845 C3 g{gnxF_, gnyF_, gnzF_};
7846 for (std::size_t L = 0; L + 1 < lv_.size(); ++L)
7847 g =
C3{g.x / lv_[L].ratio.x, g.y / lv_[L].ratio.y, g.z / lv_[L].ratio.z};
7850#ifdef PECLET_FLOW_MPI
7862 C3 global, block, ratio;
7865 bool repartition =
false;
7884 static peclet::core::decomp::BlockDecomposer<3> predictLevel0(
7885 C3 gs,
int np,
int decompLevels,
double maxImbalance,
7886 const std::vector<peclet::core::Real>*
weights,
int* align,
7887 peclet::core::Index& maxBlockCells) {
7888 using Dec = peclet::core::decomp::BlockDecomposer<3>;
7889 const int gnx = gs.x, gny = gs.y, gnz = gs.z;
7892 if (
weights->size() != (std::size_t)gnx * (std::size_t)gny * (std::size_t)gnz)
7893 throw std::invalid_argument(
7894 "predict: weights must hold one value per global cell (gnx*gny*gnz, x-fastest)");
7895 auto c = peclet::core::decomp::chooseAlignedWeighted(
7896 static_cast<std::size_t
>(np), peclet::core::IVec<3>{gnx, gny, gnz}, *
weights);
7897 cur = std::move(c.dec);
7898 maxBlockCells = peclet::core::decomp::largestBlockCells(cur);
7902 cur = decomposition(
static_cast<std::size_t
>(np), gnx, gny, gnz, decompLevels, maxImbalance);
7913 static void predictStage(peclet::core::decomp::BlockDecomposer<3>& cur, C3 gs,
int minExtent,
7914 peclet::core::Index maxBlockCells, PlanRow& r) {
7915 using Dec = peclet::core::decomp::BlockDecomposer<3>;
7917 auto liftable = [&](
const Dec&
d) {
return teleLiftable(d, gs); };
7918 const bool blocked = !liftable(cur);
7919 const bool tooSmall =
7920 minExtent > 0 && peclet::core::decomp::minBlockExtent(cur) < (peclet::core::Index)minExtent;
7921 if ((blocked || tooSmall) && canAny && cur.numBlocks() > 1) {
7922 auto t = peclet::core::decomp::chooseStageTarget(cur, peclet::core::IVec<3>{
gs.x,
gs.y,
gs.z},
7923 liftable, minExtent, maxBlockCells);
7924 if (
t.kind == peclet::core::decomp::StageKind::SiblingMerge ||
7925 t.kind == peclet::core::decomp::StageKind::Repartition) {
7926 r.repartition =
t.kind == peclet::core::decomp::StageKind::Repartition;
7927 cur = std::move(
t.dec);
7934 static C3 predictRatio(
const peclet::core::decomp::BlockDecomposer<3>& cur, C3 gs, PlanRow& r) {
7936 if (
canHalve(
gs.x) && evenBlocksOn(cur, 0)) {
7940 if (
canHalve(
gs.y) && evenBlocksOn(cur, 1)) {
7944 if (
canHalve(
gs.z) && evenBlocksOn(cur, 2)) {
7950 static std::vector<PlanRow>
predict(
int gnx,
int gny,
int gnz,
int np,
int nLevels,
7951 bool telescope,
int minExtent = 4,
int decompLevels = 0,
7952 double maxImbalance = 1.05,
7953 const std::vector<peclet::core::Real>*
weights =
nullptr,
7954 int* align =
nullptr) {
7955 using Dec = peclet::core::decomp::BlockDecomposer<3>;
7956 std::vector<PlanRow>
rows;
7957 peclet::core::Index maxBlockCells = 0;
7958 C3
gs{gnx, gny, gnz};
7959 Dec cur = predictLevel0(gs, np, decompLevels, maxImbalance,
weights, align, maxBlockCells);
7962 const bool autoDepth =
nLevels == 0;
7964 const int perBc[6] = {0, 0, 0, 0, 0, 0};
7965 for (
int L = 0;
L < nLevelsCap; ++
L) {
7968 r.ranks = (
int)cur.numBlocks();
7969 r.block = C3{(
int)cur.sizes()[0][0], (
int)cur.sizes()[0][1], (
int)cur.sizes()[0][2]};
7971 r.ratio = C3{1, 1, 1};
7973 const bool autoStop =
7975 if (L + 1 < nLevelsCap && !autoStop) {
7977 predictStage(cur, gs, minExtent, maxBlockCells, r);
7978 next = predictRatio(cur, gs, r);
7981 if (next.
x ==
gs.x && next.
y ==
gs.y && next.
z ==
gs.z)
7984 cur = cur.coarsened(peclet::core::IVec<3>{
r.ratio.x,
r.ratio.y,
r.ratio.z});
7991 std::vector<PlanRow> builtPlan()
const {
7992 std::vector<PlanRow>
rows;
7993 for (
const Level& l : lv_) {
7999 if (l.comm != MPI_COMM_NULL)
8000 MPI_Comm_size(l.comm, &
r.ranks);
8001 r.tele = (bool)l.tele;
8002 r.repartition = l.tele && l.tele->repartition();
8019 agglomMode_ = on ? 1 : 0;
8035 if (!bottomStore_) {
8051 int* Tused =
nullptr,
bool teamAlgorithm =
false) {
8062 bool noMean =
false,
bool teamAlgorithm =
false) {
8070 Kokkos::deep_copy(it, Kokkos::subview(bottomInfo_, 0));
8080 bottomFlagPending_ =
true;
8083 *Tused = (Tf == Ts) ? Ts : -1;
8088 Kokkos::deep_copy(it, Kokkos::subview(bottomInfo_, 0));
8093 auto h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), dir_.stat);
8097#ifdef PECLET_FLOW_BOTTOM_PROBE
8100 Kokkos::View<long long*, CCMem> bottomProbe_;
void debugSetEigenScale(double s)
void fcgNotePr(int it, double beta, double rznew, double rzcross, bool trace)
CCField eigenWarmMax() const
void gpMatvec(Level &l0, CCField y, CCField q, const GpOverlayReal< MReal > &ov, int nOv, C3 nn)
int solveBiCGStab(CCField b, CCField x, CCField r, CCField rh, CCField p, CCField v, CCField t, CCField z, CCField z2, int maxit, double rtol, int pre, int post, int bottom, const GpOverlayReal< MReal > &ov, int nOv, C3 nn)
double maxabsLocal(Level &lv, CCField a)
int solveFCGAttempt(CCField b, CCField x, CCField r, CCField p, CCField z, CCField zp, CCField Ap, int maxit, double rtol, int pre, int post, int bottom, const StarOverlay *star, int nStar, C3 nnStar)
bool chebyshevGuardTripped() const
void meanSubtractHostCounted(Level &lv, CCField f, bool stopGuard)
void applyNeumannGhost(const Level &lv, CCField x, int g=G)
static std::string depthRequestLabel(int nLevels)
double matvecPAp(Level &l0, CCField Ap, CCField p, const StarOverlay *star, int nStar, C3 nnStar)
Kokkos::View< double *, PinnedHost > kgp_
void axpy(CCField y, double a, CCField x)
static constexpr int kGuessPacket
long prefactorLaunches() const
static int directTeam(const K &k, int team, bool probe, std::size_t scratch=0, int hostTeam=kBottomHostTeam)
static constexpr int kLanczosSteps
int * fp32RangeFlag(int L)
void exitFp32(Level &l0, CCField zz, CCField rr, double s, int k, CCField p)
Kokkos::View< double, PinnedHost > kcz_
void fillLocal(Level &lv, CCField f)
double residualNorm(CCField b, CCField x, CCField r, CCField Ap)
const BottomPlanes & directPlanes() const
void applyOutflowGhost(const Level &lv, CCField x, int g=G)
void setZeroInitialGuess(bool on)
void setStrictPressure(bool on)
Throw instead of reporting when the preconditioner returns a non-finite correction.
bool fusedWrapSmooth(const Level &lv) const
void removeMean(Level &lv, CCField f, bool stopGuard=false)
double cgTrueResidual(CCField b, CCField x, CCField r, CCField Ap, const StarOverlay *star=nullptr, int nStar=0, C3 nnStar=C3{0, 0, 0})
void scale(CCField y, double a)
void eigKeep(CCField &dst, CCField x, const char *label, std::size_t n)
void vcycleFp32Impl(int L, bool sym)
double precondDotDist(CCField z, CCField r)
Kokkos::SharedHostPinnedSpace PinnedHost
void setDebugBreakdown(int which, int iter)
void directMarkStaleForTest()
int solvePCGResident(CCField b, CCField x, CCField r, CCField p, CCField z, CCField Ap, int maxit, double rtol, int pre, int post, int bottom, const StarOverlay *star, int nStar, C3 nnStar)
static double amgDot(const std::vector< double > &a, const std::vector< double > &c, std::size_t n)
void addSeedNoise(Level &lv, CCField f, double amp)
int failNonFiniteZ(const char *who, Level &l0, CCField x, int maxit)
static constexpr double kLanczosTol
double maxabsWithLocalSum(Level &lv, CCField a, Kokkos::View< double, PinnedHost > part, double &sum)
void clearProjectedGuess()
bool hostKrylovFusion() const
bool beginAttemptPrecision(int &maxit)
double guessResidual(Level &l0, CCField b, CCField x, CCField r, CCField Ap, const StarOverlay *star, int nStar, C3 nnStar)
double readSlot(int k) const
void aypx(CCField y, double a, CCField x)
void matvecDotPAp(Level &l0, CCField Ap, CCField p, bool fuse, const StarOverlay *star, int nStar, C3 nnStar)
void fillAxis(Level &lv, CCField f, int axis)
bool distKrylovFusion() const
std::string lineAxisLabel(int L) const
int directSolveTeamForTest(int team, int *Tused=nullptr)
void fillOpenness(Level &lv)
int solvePCG(CCField b, CCField x, CCField r, CCField p, CCField z, CCField Ap, int maxit, double rtol, int pre, int post, int bottom, const StarOverlay *star=nullptr, int nStar=0, C3 nnStar=C3{0, 0, 0})
bool fp64Deferrable() const
const Level & bottomLv() const
bool pApBreakdown(double pAp)
static int lineAxisTrigger(int L, C3 g, C3 cf, const double hp[3], double theta, const int bc[6], double *aspect)
int bottomComponents() const
void precondVcycleFp32(CCField zz, CCField rr, int e, int k, CCField p)
void matvecOverlap(Level &l0, CCField y, CCField v)
const char * directBottomIneligible() const
int vcyclePrecision() const
void setOutflowCoefficient(bool on)
void resetAllreduceCounters()
const std::vector< double > & healthLog() const
void setMetric(const double hp[3])
Per-axis cell spacings h_a' = h_a / hRef of the physical domain (doc/anisotropic_metric....
void residentPrecondRz(Level &l0, bool f32, bool fuse, int e, CCField z, CCField r, int it, bool health, bool trace)
double initialResidual(Level &l0, CCField b, CCField x, CCField r, CCField Ap, const StarOverlay *star, int nStar, C3 nnStar)
const char * directBottomIneligibleAll() const
double eigPowerMin(Level &l0, CCField v, CCField w, CCField z, int iters, double shift)
bool eigenWarmReady() const
void debugSetOverlaySolve(bool on)
void noteFluidCellsDevice(Level &lv)
void amgMeanZero(std::vector< double > &v) const
CCField workVector(int slot, std::size_t n, const char *label)
void checkFp32DecoupledSets()
bool bottomAdmissibleHere(C3 g) const
void noteBicgExit(int exitCode, int it, int maxit, double r0n, double rnLast, double rtol)
void setGraphAmgBottom(bool on)
void fill(Level &lv, VField f)
static bool touchesGlobalFace(const Level &lv, int f)
static constexpr double kChebSymShift
std::vector< C3 > levelRatios() const
The per-level coarsening ratio actually chosen (the level table of §5, gate §8.5).
static C3 parityOg(const Level &lv)
bool directPrefactorEligible() const
bool buildGlobalBottomLevel()
void lineSmoothColorFp32(Level &lv, int s, int color)
void eigSeedRhs(Level &l0, CCField srhs, CCConst seed)
static double symTridiagTop(int m, const double *d, const double *e, double *y)
double cgUpdate(Level &l0, CCField x, CCField r, CCField p, CCField Ap, double alpha, bool fuse)
void gatherGlobalBottomRhs()
void debugSetFp32Fault(int kind, int at)
void directPivotTolForTest(double t)
void vcycle(int L, bool sym)
Kokkos::View< double, PinnedHost > krd_
const char * fp32VcycleIneligible(bool krylov) const
static constexpr double kBrkPAp
bool globalMean(Level &lv, double sum, long cnt, double &mean)
Kokkos::View< double, CCMem > slot(int k) const
void setSymmetricVcycle(bool on)
A3 (doc/mg_depth_policy.md Amendment A3, WO-S1): the symmetric pressure V-cycle – piecewise-constant ...
void amgCellCouplings(C3 gc, C3 gb, int gid, const double(&bc)[6], AmgRows &o) const
void pcgUpdateSum(Level &l0, CCField x, CCField r, CCField p, CCField Ap)
void amgReport(const AmgRhsAnatomy &a, const std::vector< double > &x, const std::vector< double > &r, int it, double r0, int dbg)
void setOpenness(CCConst ox, CCConst oy, CCConst oz, double idx2, double idy2, double idz2)
void guessMatvec(Level &l0, CCField y, CCField v, const StarOverlay *star, int nStar, C3 nnStar)
void lineSmoothColor(Level &lv, int s, int color)
double eigPowerMax(Level &l0, CCField v, CCField w, CCField z, int iters)
void setProjectedGuess(CCField v1, int hist)
void pcgAmg(std::vector< double > &b, std::vector< double > &x)
void noteBottomFlag(double v)
void matvecDotTo(Level &l0, CCField y, CCField v, int k)
void bottomForceForTest()
void setBoundaryConditions(const int bc[6])
void eigBracket(double shift, double mu, double &lmin, double &lmax)
long allreduceCount() const
double maxabs(Level &lv, CCField a)
void fp32DecoupledCounts(int L, long &n32, long &n64)
void vcycleImpl(int L, bool sym)
void applyBoundaryOpenness(Level &lv, Level *fine=nullptr)
void dotTo(Level &lv, CCField a, CCField b, int k)
peclet::core::solver::HostCsrOp amgAssembleCsr(const AmgRows &rows) const
std::vector< int > lineAxes() const
bool restartArmed() const
void precondVcycleCopy(CCField zz, CCField rr)
void setTelescope(bool on)
void setCoarseGhost(bool on)
Neumann coarse ghost on wall/inflow faces before the prolongation (see bcGhost_).
CCField level0Coefficient(int a) const
static bool bottomAdmissible(C3 g, int extent, bool anchored, int agglomMode, const int bc[6])
bool residentPacketExit(int &it, double rtol, double rref, double r0, bool trace, double &rn)
Kokkos::View< long, CCMem > kcnt_
void setAspectThreshold(double theta)
Anisotropic-coarsening aspect threshold theta (see aspectTheta_).
void copyDotTo(Level &lv, CCField p, CCField r, CCField z, int k)
bool lineRelaxOn(const Level &lv) const
void setRepartition(bool on)
Repartition stages for a weighted level-0 decomposition (see repartition_); before initMpi.
void requireDirectBottom() const
void removeMeanDotTo(Level &lv, int k, CCField p)
void precondVcycle(CCField zz, CCField rr)
double fcgPrecondRz(Level &l0, bool f32, bool fuse, CCField z, CCField r, double rn, CCField p)
long numEigenEstimates() const
bool fp32Carriable() const
void dotPair(Level &lv, CCField a, CCField b, CCField c, double &ab, double &ac)
void maskSolid(Level &lv, CCField f)
int repartitionCount() const
AmgRhsAnatomy amgRhsAnatomy(const std::vector< double > &b) const
void fp32Reconfigure(bool wasCarriable, bool wasDeferrable)
void setHealthTrace(bool on)
void residentBetaAypx(CCField pp, CCField zz, std::size_t n)
bool linePeriodic(int s) const
void removeMeanHostCounted(Level &lv, CCField f, bool stopGuard)
int launchDirectFactor(const BottomDirect< FR > &D, int team, double tauPiv0, bool probe=true, bool teamAlgorithm=false, const CCExec &exec=CCExec())
int lastDiscardedIters() const
void guessTraceExit(Level &l0, CCField b, CCField x, double rref, double r0w, int it, int hist, const StarOverlay *star, int nStar, C3 nnStar)
static constexpr double kChebBracket
void applyBoundaryOpennessFrom(Level &lv, const CCField *fo, C3 fext, int fg, C3 fratio)
static constexpr int kNSlots
bool symmetricVcycle() const
bool exactResidual() const
double pcgUpdateMeanMaxDist(Level &l0, CCField x, CCField r, CCField p, CCField Ap, double alpha)
double aspectThreshold() const
void smoothFp32(Level &lv, int sweeps, bool reverse, bool skipFirst)
bool bottomConnected() const
void estimateEigenvalues(CCConst seed, double &lmin, double &lmax, int iters, int pre, int post, int bottom, EigenStart start=EigenStart::Cold, double bracket=1.0)
void lin(CCField out, double a, CCField x, double b, CCField y)
CCField eigenWarmMin() const
int telescopeCount() const
void residentAlpha(int it)
double armStopTest(double r0, double rtol)
int solveChebyshev(CCField b, CCField x, int maxit, double rtol, int pre, int post, int bottom, double a, double bnd, bool guard3=false, bool guardGrowth=false)
double dot(Level &lv, CCField a, CCField b)
void fill(Level &lv, CCField f)
static void allocLine(Level &v)
void printAmgBuildCensus(const AmgRows &rows) const
void setTelescopeMinExtent(int e)
void setStopReference(double ref)
void setSlot(int k, double v)
double allreduceSeconds() const
Kokkos::View< double *, CCMem > ks_
bool hostAllFluid(const Level &lv) const
int directApplyForTest(const BottomDirect< FR > &D, CCField z, CCField r, int team, bool noMean=false, bool teamAlgorithm=false)
double firstPrecondRz(Level &l0, bool f32, bool fuse, int e, CCField z, CCField r, CCField p)
AmgRows amgLocalRows(const Level &lv, C3 gb) const
int agglomerationMode() const
void graphAmgSolveBottom(Level &lv)
std::vector< int > lineSmoothedLevels() const
static C3 mgChooseRatio(const double H[3], const bool canA[3], bool aniso, double theta)
int launchDirectKernel(const BottomDirect< FR > &D, bool precondOnly, int team, bool noMean, CCField zOut=CCField(), CCField rIn=CCField(), bool probe=true, bool teamAlgorithm=false)
double removeMeanDotDist(Level &lv)
static bool canHalve(int d)
void noteHealth(double rzOld, int it, bool trace, bool f32)
bool joinPrefactor() const
bool lastSolveConverged() const
C3 levelRatio(C3 gs, C3 cf, const bool canA[3], C3 &next) const
void buildGlobalBottomOp()
int solveWithRestart(CCField x, int maxit, Attempt &&attempt)
bool lastVcycleFp32() const
void setAgglomerationMode(int mode)
void smooth(Level &lv, int sweeps, bool reverse)
void eigApplyT(Level &l0, CCField out, CCField in, CCField w)
bool cgConverged(int &it, double rn, double r0, double rtol, double rref, bool trace)
void setTelescopeForceLevel(int L)
int effectiveVprec() const
void guessTraceSave(CCField x)
int directRestarts() const
void applyNeumannGhost(const Level &lv, VField x, int g=G)
void setAgglomerationExtent(int cells)
static const char * bottomLabelWhy(int code)
int solvePCGAttempt(CCField b, CCField x, CCField r, CCField p, CCField z, CCField Ap, int maxit, double rtol, int pre, int post, int bottom, const StarOverlay *star, int nStar, C3 nnStar)
bool outflowCoefficient() const
static constexpr bool kHostMemory
double lanczosTop(Level &l0, CCField v, bool ritz)
Kokkos::View< double *, PinnedHost > kpk_
bool strictPressure() const
void maxabsTo(Level &lv, CCField a, int k)
void applyNeumannGhostT(const Level &lv, V x, int g)
bool lineRelaxation() const
void printDepthCensus(int nLevels, bool more) const
int bottomLabelCode() const
void setLineRelaxation(bool on)
A2.4 (doc/mg_depth_policy.md, WO-7): zebra line relaxation along the blocked strong axis on the trigg...
void saveOutflowPlanes(Level &lv)
void amgLabelComponents(const AmgRows &rows)
C3 coarsestGlobal() const
void setExactResidual(bool on)
The exact double flux-form level-0 apply in the residual and the Krylov matvec.
int vcycleRestarts() const
void init(int nx, int ny, int nz, int nLevels)
void setEigenWarm(CCField vmax, CCField vmin)
double pcgPrecondRz(Level &l0, bool f32, bool fuse, CCField z, CCField r, double rn, int k, CCField p)
void matvecGuessDots(Level &l0, CCField y, CCField v, CCField w, CCField r, int kvy, int kvr, int kwy)
double eigSeed(Level &l0, CCField x, CCField src)
void meanSubtractMaxTo(Level &lv, CCField f, int k)
void scatterGlobalBottomX()
bool caSmooth(const Level &lv) const
void finalRemoveMean(Level &l0, CCField x)
int solveFCG(CCField b, CCField x, CCField r, CCField p, CCField z, CCField zp, CCField Ap, int maxit, double rtol, int pre, int post, int bottom, const StarOverlay *star=nullptr, int nStar=0, C3 nnStar=C3{0, 0, 0})
bool agglomerateAuto(long gx, long gy, long gz) const
void setMeanRemovalScope(bool all)
bool fp32WeightsOk(int L) const
void noteFluidCells(Level &lv)
static constexpr int kPacket
bool fusedWrapReads() const
static constexpr double kBrkRz
double rhsNorm(CCField b, CCField scratch)
bool precondDotTo(bool f32, bool fuse, int e, int k, CCField z, CCField r, CCField pz)
static C3 boxLo(int g, int m)
void residentUpdate(Level &l0, CCField xx, CCField rr, CCField pp, CCField aa, bool fuse)
void evalBottomComponents()
long gbIndex(int gid) const
int agglomerationExtent() const
void fillWrap(Level &lv, V f)
void setCommAvoiding(int mask)
Communication-avoiding smoothing mask (kCaMomentum | kCaMg); only kCaMg is read here.
double eigNormalize(Level &l0, CCField x)
StartResidual startResidual(CCField b, CCField x, CCField r, CCField Ap, CCField z, const StarOverlay *star, int nStar, C3 nnStar, bool fused, bool resident)
BottomDirect< FR > directFactorForTest(int team, double tauPiv0=kBottomPivotTol, int *Tused=nullptr, bool teamAlgorithm=false)
static C3 boxHi(C3 e, int g, int m)
void setVcyclePrecision(int mode)
void setBottomSolver(int m)
bool agglomerateBottom() const
void meanSumHostCounted(Level &lv, CCField f)
double projectedGuess(Level &l0, CCField b, CCField x, CCField r, CCField av1, CCField av2, const StarOverlay *star, int nStar, C3 nnStar, bool fused, double *r0w)
BottomKernel< FR > makeBottomKernel(bool precondOnly)
void debugSetStencilHaloOff(bool on)
flow — directional ghost-cell IBM projection overlay (experimental second staggered IBM).
flow — portable (Kokkos) native per-face domain boundary conditions for the MAC grid.
#define PECLET_FLOW_OMP_SIMD
flow — portable (Kokkos) cut-cell pressure operator + Chorin projection.
flow — the device pressure bottom's direct preconditioner (doc/vof_step_performance_ design....
constexpr int kBottomHostFactorTeam
double cutcellApplyExactCell(const XV &x, const OV &ox, const OV &oy, const OV &oz, long i, long sx, long sy, long sz, long xp, long xm, long yp, long ym, long zp, long zm, double gfx, double gfy, double gfz)
Kokkos::DefaultExecutionSpace CCExec
void mgSaveFacePlane(CCField dst, CCConst src, C3 e, int g, int a, int s)
void ccForRows3(const char *name, C3 lo, C3 hi, F f)
void residualCutcellFace(CCField r, CCConst x, CCConst b, FPC AC, FPC AFX, FPC AFY, FPC AFZ, C3 e, int g)
void cutcellSmoothColorFp32Box(VField x, VConst b, VConst WX, VConst WY, VConst WZ, C3 e, C3 og, int color, C3 lo, C3 hi, C3 exLo, C3 exHi, int wrapMask=0, int g=1)
void applyCutcellOpFaceWrap(CCField y, CCConst x, FPC AC, FPC AFX, FPC AFY, FPC AFZ, C3 e, C3 n, int g)
CcNbrs ccMaskNbrs(int lx, int ly, int lz, C3 n, int g, long i, long sy, long sz, int m)
void applyCutcellOpFace(CCField y, CCConst x, OpV AC, OpV AFX, OpV AFY, OpV AFZ, C3 e, int g)
Kokkos::MDRangePolicy< Exec, Kokkos::Rank< 3, Kokkos::Iterate::Left, Kokkos::Iterate::Left > > MDRange3
3-D range, x (the first index) fastest (both the tile order and the order within a tile).
void cutcellBuildFaceOpCell(const OpV &AC, const OpV &AFX, const OpV &AFY, const OpV &AFZ, const OV &ox, const OV &oy, const OV &oz, long i, long sx, long sy, long sz, double gfx, double gfy, double gfz, bool diag)
void mgRestoreFacePlane(CCField dst, CCConst src, C3 e, int g, int a, int s)
void prolongConstAdd(CCField fine, CCConst coarse, C3 fext, C3 cext, int gf, int gc, C3 finner, C3 ratio)
void bcSetOpenness(BField oa, B3 ext, int g, int a, int s, double val)
void restrictAvgZeroX(CCField coarse, CCField coarseX, CCConst fine, C3 cext, C3 fext, int gc, int gf, C3 cinner, C3 ratio)
void cutcellResidualFaceCell(const RV &r, const XV &x, const BV &b, const OpV &AC, const OpV &AFX, const OpV &AFY, const OpV &AFZ, long i, long sx, long sy, long sz, long xp, long xm, long yp, long ym, long zp, long zm)
void prolongAdd(CCField fine, CCConst coarse, C3 fext, C3 cext, int gf, int gc, C3 finner, C3 ratio)
constexpr int kBottomHostTeam
void bcZeroPressureGhost(BField phi, B3 ext, int g, int a, int s)
void prolongFp32FromD(VField fine, CCConst coarse, C3 fext, C3 cext, int gf, int gc, C3 finner, C3 ratio)
void cutcellSmoothFaceCell(const PV &phi, const BV &b, const OpV &AC, const OpV &AFX, const OpV &AFY, const OpV &AFZ, long i, long sx, long sy, long sz, long xp, long xm, long yp, long ym, long zp, long zm)
void prolongAddCell(const FV &fine, const CV &coarse, C3 fext, C3 cext, int gf, int gc, C3 ratio, int ifx, int ify, int ifz)
Kokkos::View< const double *, CCMem > CCConst
void cutcellSmoothColorBoxFace(CCField phi, CCConst b, OpV AC, OpV AFX, OpV AFY, OpV AFZ, C3 e, C3 og, int color, C3 rlo, C3 rhi, C3 slo, C3 shi, int wrapMask=0, int g=1)
void coarsenOpenAvg(CCField oxc, CCField oyc, CCField ozc, CCConst oxf, CCConst oyf, CCConst ozf, C3 cext, C3 fext, int gc, int gf, C3 cinner, C3 ratio)
void residualFp32Box(VField r, VConst x, VConst b, VConst WX, VConst WY, VConst WZ, C3 e, C3 lo, C3 hi, C3 exLo, C3 exHi, int wrapMask=0, int g=1)
void ccReduce3(const char *name, C3 lo, C3 hi, F f, R &&... reducers)
void residualCutcell(CCField r, CCConst x, CCConst b, FPC AC, FPC AW, FPC AE, FPC AS, FPC AN, FPC AB, FPC AT, C3 e, int g)
void applyCutcellOpBoxFace(CCField y, CCConst x, OpV AC, OpV AFX, OpV AFY, OpV AFZ, C3 e, C3 rlo, C3 rhi, C3 slo, C3 shi, int wrapMask=0, int g=1)
void residualFp32Wrap(VField r, VConst x, VConst b, VConst WX, VConst WY, VConst WZ, C3 e, C3 n, int g)
void mgCoarsenFacePlane(CCField oc, CCConst of, C3 cext, C3 fext, int gc, int gf, C3 cinner, C3 ratio, int a, int s)
BottomPlanes bottomChoosePlanes(C3 inner, const int bc[6])
void applyCutcellOpExact(CCField y, CCConst x, CCConst ox, CCConst oy, CCConst oz, C3 e, int g, double gfx, double gfy, double gfz)
void residualCutcellBoxFace(CCField r, CCConst x, CCConst b, FPC AC, FPC AFX, FPC AFY, FPC AFZ, C3 e, C3 rlo, C3 rhi, C3 slo, C3 shi, int wrapMask=0, int g=1)
void gpApplyDelta(CCField y, CCConst x, const GpOverlayReal< Real > &ov, int nOv, C3 nn, C3 extY, int gbY, C3 extX, int gbX, bool useGhost=false)
Overlay matvec correction: y(r) = rho_r * (y(r) + closure-face phi terms), where y currently holds th...
void cutcellResidualCell(const RV &r, const XV &x, const BV &b, const OpV &AC, const OpV &AW, const OpV &AE, const OpV &AS, const OpV &AN, const OpV &AB, const OpV &AT, long i, long xp, long xm, long yp, long ym, long zp, long zm)
void restrictFp32ToDZeroX(CCField coarse, CCField coarseX, VConst fine, C3 cext, C3 fext, int gc, int gf, C3 cinner, C3 ratio)
constexpr double kBottomTau
Kokkos::View< const MReal *, CCMem > FPC
void restrictFp32ZeroX(VField coarse, VField coarseX, VConst fine, C3 cext, C3 fext, int gc, int gf, C3 cinner, C3 ratio)
void prolongFp32(VField fine, VConst coarse, C3 fext, C3 cext, int gf, int gc, C3 finner, C3 ratio)
constexpr int kAutoMaxLevels
void ccBoxShellRuns(const char *name, C3 rlo, C3 rhi, C3 slo, C3 shi, Run run)
void fp32AddDouble(VField f, CCConst d, C3 e, int g)
void ccWrapForPeeled(const char *name, C3 e, C3 n, int g, const Cell &cell)
void fp32ToDouble(CCField d, VConst f, C3 e)
void buildCutcellOpFace(OpV AC, OpV AFX, OpV AFY, OpV AFZ, CCConst ox, CCConst oy, CCConst oz, C3 e, int gb, double gfx, double gfy, double gfz)
void applyCutcellOpExactWrap(CCField y, CCConst x, CCConst ox, CCConst oy, CCConst oz, C3 e, C3 n, int g, double gfx, double gfy, double gfz)
void residualCutcellFaceWrap(CCField r, CCConst x, CCConst b, FPC AC, FPC AFX, FPC AFY, FPC AFZ, C3 e, C3 n, int g)
void buildCutcellOpFaceFp32(OpV AC, OpV AFX, OpV AFY, OpV AFZ, WV WX, WV WY, WV WZ, CCConst ox, CCConst oy, CCConst oz, C3 e, int gb, double gfx, double gfy, double gfz, int fp64, int *bad)
Kokkos::View< VReal *, CCMem > VField
GpOverlayT< CCMem, Real > GpOverlayReal
void applyCutcellOpExactBox(CCField y, CCConst x, CCConst ox, CCConst oy, CCConst oz, C3 e, C3 rlo, C3 rhi, C3 slo, C3 shi, double gfx, double gfy, double gfz, int wrapMask=0, int g=1)
CcNbrs ccWrapNbrs(int lx, int ly, int lz, C3 n, int g, long i, long sy, long sz)
void cutcellSmoothColorFace(CCField phi, CCConst b, OpV AC, OpV AFX, OpV AFY, OpV AFZ, C3 e, C3 og, int g, int color)
void lineRelaxColorFace(CCField x, CCConst b, OpV AC, OpV AFX, OpV AFY, OpV AFZ, CCField c, CCField y, CCField z, C3 e, C3 og, int g, int s, bool periodic, int wrapMask, int color)
void residualCutcellBox(CCField r, CCConst x, CCConst b, FPC AC, FPC AW, FPC AE, FPC AS, FPC AN, FPC AB, FPC AT, C3 e, C3 rlo, C3 rhi, C3 slo, C3 shi)
Kokkos::View< double *, CCMem > CCField
void ccFor3(const char *name, C3 lo, C3 hi, F f)
Kokkos::View< MReal *, CCMem > FPV
void ccMaskRow(int ly, int lz, int lx0, int x1, int step, C3 e, C3 n, int g, int m, const Cell &cell)
void meanSubtractCell(const FV &f, const AV &ac, long i, double mean)
constexpr long kBottomMaxCells
void restrictAvg(CCField coarse, CCConst fine, C3 cext, C3 fext, int gc, int gf, C3 cinner, C3 ratio)
constexpr int kBottomMaxComponents
constexpr bool kAnchoredFollowsGrid
void coarsenOpenAvgCell(const CV &oxc, const CV &oyc, const CV &ozc, const FV &oxf, const FV &oyf, const FV &ozf, C3 cext, C3 fext, int gc, int gf, C3 ratio, int icx, int icy, int icz)
constexpr double kBottomPivotTol
void prolongConstAddCell(const FV &fine, const CV &coarse, C3 fext, C3 cext, int gf, int gc, C3 ratio, int ifx, int ify, int ifz)
void lineRelaxColorFp32(VField x, VConst b, VConst WX, VConst WY, VConst WZ, VField c, VField y, VField z, C3 e, C3 og, int g, int s, bool periodic, int wrapMask, int color)
constexpr int kAutoMinLevels
void ccReduce3Lanes(const char *name, C3 lo, C3 hi, F f, R &&... reducers)
void cutcellSmoothColorFaceWrap(CCField phi, CCConst b, FPC AC, FPC AFX, FPC AFY, FPC AFZ, C3 e, C3 n, C3 og, int g, int color)
void bcNeumannGhostT(FV f, B3 ext, int g, int a, int s)
void ccWrapRowPeeled(int ly, int lz, C3 e, C3 n, int g, int lx0, int step, const Cell &cell)
void entryFp32(VField xf, VField rhsf, CCConst rr, VConst WX, VConst WY, VConst WZ, double sigma, C3 e, C3 og, int g, int color, bool smoothFirst)
void cutcellSmoothColorFp32Wrap(VField x, VConst b, VConst WX, VConst WY, VConst WZ, C3 e, C3 n, C3 og, int g, int color)
bool hostRunSerial(long cells)
double cutcellApplyFaceCell(const XV &x, const OpV &AC, const OpV &AFX, const OpV &AFY, const OpV &AFZ, long i, long sx, long sy, long sz, long xp, long xm, long yp, long ym, long zp, long zm)
Kokkos::MDRangePolicy< Exec, Kokkos::Rank< 2, Kokkos::Iterate::Left, Kokkos::Iterate::Left > > MDRange2
2-D range, first index fastest (both the tile order and the order within a tile).
void prolongConstFp32(VField fine, VConst coarse, C3 fext, C3 cext, int gf, int gc, C3 finner, C3 ratio)
void restrictAvgCell(const CV &coarse, const FV &fine, C3 cext, C3 fext, int gc, int gf, C3 ratio, int icx, int icy, int icz)
void prolongConstFp32FromD(VField fine, CCConst coarse, C3 fext, C3 cext, int gf, int gc, C3 finner, C3 ratio)
Kokkos::View< const VReal *, CCMem > VConst
void starApplyDelta(CCField y, CCConst x, const StarOverlay &ov, int nOv, C3 nn, C3 extY, int gY, C3 extX, int gX, bool exact)
y += S_star x over the inner cells of the (extY, gY) block, x read from the (extX,...
one(n, rho_ratio, mu_ratio, stress, steps, adv, dtf, grav=False, sigma="none")
flow — the multidimensional iteration policies: iteration order follows storage.
flow — Design B of the fluid-only collocated constraint (route 2b): Kron (star-mesh) elimination of s...
OverlayScope(bool &f, bool on)
static BottomDirect allocate(const BottomPlanes &p, C3 ext)
static std::size_t scratchBytes(int b)
static double teamMax(const Member &t, int n, const F &f)
static double teamSum(const Member &t, int n, const F &f)
void removeMeanBottom(const Member &t, const CCField &f) const
Kokkos::View< const int *, CCMem > comp
Kokkos::View< int *, CCMem > info
void operator()(const Member &t) const
void applyM(const Member &t, const CCField &R, const CCField &Z, const CCField *keep) const
static int nInner(const BottomLevel &l)
static constexpr bool kHostLane
double dot(const Member &t, const BottomLevel &l, const CCField &a, const CCField &c) const
BottomDirect< FacReal > dir
Kokkos::View< double *, CCMem > ks
Kokkos::View< double *, CCMem > cmean
Kokkos::TeamPolicy< CCExec >::member_type Member
Kokkos::View< const long *, CCMem > ccnt
static long cell(const BottomLevel &l, int q, int &lx, int &ly, int &lz)
static int nExt(const BottomLevel &l)
Kokkos::View< int *, CCMem > kc
Kokkos::View< long *, CCMem > cnt
Kokkos::TeamPolicy< CCExec >::member_type Member
Kokkos::View< double *, CCMem > aug
Kokkos::View< int *, CCMem > nc
void operator()(const Member &t) const
Kokkos::View< int *, CCMem > tmp
long cell(int q, int &lx, int &ly, int &lz) const
Kokkos::View< int *, CCMem > comp
int sweep(const Member &t, const Kokkos::View< int *, CCMem > &src, const Kokkos::View< int *, CCMem > &dst) const
std::vector< std::uint8_t > solid
std::vector< double > diag
std::vector< double > val
Kokkos::View< VReal *, CCMem > resf
Kokkos::View< VReal *, CCMem > WY
Kokkos::View< VReal *, CCMem > lnzf
Kokkos::View< VReal *, CCMem > lncf
Kokkos::View< VReal *, CCMem > WX
Kokkos::View< VReal *, CCMem > WZ
Kokkos::View< VReal *, CCMem > xf
Kokkos::View< VReal *, CCMem > lnyf
Kokkos::View< long, CCMem > nFluidDev
Kokkos::View< VReal *, CCMem > rhsf
OverlayScope(bool &f, bool on)
One entry per eliminated solid-centered cell: packed INNER flat index + the apertures of its (up to 6...
static double val(long gi)
Kokkos::View< const double *, CCMem > CCConst
Kokkos::View< double *, CCMem > CCField
peclet::core::decomp::BlockDecomposer< 3 > Dec
static constexpr double AC
Kokkos::View< double *, peclet::flow::CCMem > OpV
static constexpr double F
Kokkos::View< float *, IMem > FV
static constexpr double R
static std::vector< peclet::core::Real > weights(int kind, Grid3 g)
static double mean(const std::vector< double > &a)