flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_mg_stencil_halo_mpi.cpp
Go to the documentation of this file.
1// flow — the pressure MG's stencil halo (doc/vof_distributed_host_comm_design.md §5.2, WO-K3).
2//
3// The 7-point stencil exchanges (the non-CA smoother, the residual, the matvec and their FP32
4// twins) run on a faces-only halo with the level's rank-local periodic axes M_L read in-kernel
5// (bit a of M_L: the level is ONE block along a, no outflow face on a, even inner extent on a).
6// The claim is class a: every stencil reads the values it read before -- a face ghost holds its
7// owner's value, a wrapped read reaches the cell the full exchange copies into the ghost, and the
8// edge / corner ghosts the faces-only halo no longer fills are never read. So the gate is BITWISE:
9// the default against the test hook debugSetStencilHaloOff(true), which forces the full `dev`
10// halo and wrapMask = 0 (today's code), on the same build and rank count -- per-step pressure
11// iterations identical and the solution's inner cells memcmp-equal.
12//
13// Cases (a walled / cut-cell / variable-coefficient operator rebuilt every "step", as
14// test_bottom_direct_mpi): walls +-y with periodic x, z; an inflow/outflow z (z never a wrap axis,
15// the outflow ghost re-imposed after the faces-only exchange); an odd coarse z extent (R-1: that
16// level keeps z unwrapped); a forced telescope (levels below it on fewer ranks, every axis wrapped
17// on a single-block level: the exchange-free path at np > 1); all-periodic (CA smoothing on the
18// coarse levels stays on `dev`); the FP32 V-cycle (sdevf); the band (non-exact) matvec.
19//
20// Structure checks: the stencil topology never has more neighbours or ghost cells than the full
21// one; at np 1 every level wraps all three axes (zero stencil exchanges).
22#include <mpi.h>
23
24#include <algorithm>
25#include <cmath>
26#include <cstdio>
27#include <cstring>
28#include <Kokkos_Core.hpp>
29#include <string>
30#include <vector>
31
32#include "mac_cutcell_mg.hpp"
33#include "peclet/core/common/types.hpp"
34
35using peclet::core::IVec;
40
41static constexpr int G = 1;
42static constexpr int STEPS = 4;
43
44static int wrap(int a, int n) {
45 return ((a % n) + n) % n;
46}
47
48// A sphere's open fraction times 1 / rho of a dense blob moving along z with the step.
49struct Scene {
50 IVec<3> gs;
51 int step = 0;
52 double open(double x, double y, double z) const {
53 const double cx = 0.5 * gs[0], cy = 0.5 * gs[1], cz = 0.5 * gs[2], R = 0.22 * gs[1];
54 const double phi =
55 std::sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy) + (z - cz) * (z - cz)) - R;
56 return std::clamp(phi + 0.5, 0.0, 1.0);
57 }
58 double coef(double x, double y, double z) const {
59 const double bz = std::fmod(0.2 * gs[2] + 3.0 * step, (double)gs[2]);
60 const double w = 0.12 * gs[0], dx = x - 0.3 * gs[0], dy = y - 0.35 * gs[1], dz = z - bz;
61 return 1.0 / (1.0 + 9.0 * std::exp(-(dx * dx + dy * dy + dz * dz) / (w * w)));
62 }
63 double face(int a, double x, double y, double z) const {
64 if (a == 0)
65 x -= 0.5;
66 if (a == 1)
67 y -= 0.5;
68 if (a == 2)
69 z -= 0.5;
70 return open(x, y, z) * coef(x, y, z);
71 }
72};
73
74static void fillScene(CCField ox, CCField oy, CCField oz, CCField b, C3 ext, C3 og,
75 const Scene& sc) {
76 auto hx = Kokkos::create_mirror_view(ox), hy = Kokkos::create_mirror_view(oy),
77 hz = Kokkos::create_mirror_view(oz), hb = Kokkos::create_mirror_view(b);
78 const IVec<3> gs = sc.gs;
79 double loc[2] = {0.0, 0.0}, glb[2];
80 for (int pass = 0; pass < 2; ++pass) {
81 for (int z = 0; z < ext.z; ++z)
82 for (int y = 0; y < ext.y; ++y)
83 for (int x = 0; x < ext.x; ++x) {
84 const long i = (long)x + (long)y * ext.x + (long)z * (long)ext.x * ext.y;
85 const int gx = wrap(x - G + og.x, (int)gs[0]), gy = wrap(y - G + og.y, (int)gs[1]),
86 gz = wrap(z - G + og.z, (int)gs[2]);
87 const double cx = gx + 0.5, cy = gy + 0.5, cz = gz + 0.5;
88 const bool inner =
89 x >= G && x < ext.x - G && y >= G && y < ext.y - G && z >= G && z < ext.z - G;
90 const double f = std::sin(2.0 * M_PI * gx / gs[0]) * std::cos(M_PI * cy / gs[1]) +
91 0.5 * std::sin(2.0 * M_PI * (gz + 0.3 * sc.step) / gs[2]) +
92 0.1 * cy / gs[1];
93 const bool fluid = sc.open(cx, cy, cz) > 0.0 || sc.open(cx - 0.5, cy, cz) > 0.0;
94 if (pass == 0) {
95 hx(i) = sc.face(0, cx, cy, cz);
96 hy(i) = sc.face(1, cx, cy, cz);
97 hz(i) = sc.face(2, cx, cy, cz);
98 if (inner && fluid) {
99 loc[0] += f;
100 loc[1] += 1.0;
101 }
102 } else {
103 hb(i) = (inner && fluid) ? f - glb[0] / glb[1] : 0.0;
104 }
105 }
106 if (pass == 0)
107 MPI_Allreduce(loc, glb, 2, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
108 }
109 Kokkos::deep_copy(ox, hx);
110 Kokkos::deep_copy(oy, hy);
111 Kokkos::deep_copy(oz, hz);
112 Kokkos::deep_copy(b, hb);
113}
114
115struct Case {
116 const char* name;
117 IVec<3> gs;
118 int nlev;
119 int bc[6];
121 int prec; // CutcellMG::kVcycleFp64 / kVcycleFp32
122 bool exact; // the exact double level-0 apply (else the bands)
123 bool wrap1; // np 1: every level wraps all three axes (no outflow face, every extent even)
124};
125
126struct Run {
127 std::vector<int> iters;
128 std::vector<double> x; // inner cells, x fastest
129 int levels = 0, wrapAll = 1;
130 long nbFull = 0, nbStencil = 0, ghFull = 0, ghStencil = 0; // summed over this rank's levels
131 bool fewer = true;
132};
133
134static Run solveSteps(const Case& c, bool hookOff) {
135 CutcellMG mg;
136 mg.setBoundaryConditions(c.bc); // before initMpi
137 mg.setCommAvoiding(peclet::flow::kCaBoth); // CA on every backend (device default: off)
138 mg.setTelescope(c.teleForce >= 0);
139 mg.setTelescopeForceLevel(c.teleForce);
140 mg.initMpi(c.gs[0], c.gs[1], c.gs[2], c.nlev, MPI_COMM_WORLD);
141 mg.setExactResidual(c.exact);
142 mg.setVcyclePrecision(c.prec);
143 mg.debugSetStencilHaloOff(hookOff);
144 CutcellMG::Level& l0 = mg.level(0);
145 const C3 ext = l0.ext, og = l0.og;
146 const std::size_t n = (std::size_t)ext.x * ext.y * ext.z;
147 CCField ox("ox", n), oy("oy", n), oz("oz", n), b("b", n), x("x", n), r("r", n), p("p", n),
148 z("z", n), Ap("Ap", n);
149 Run res;
150 Scene sc{c.gs, 0};
151 for (int s = 0; s < STEPS; ++s) {
152 sc.step = s;
153 fillScene(ox, oy, oz, b, ext, og, sc);
154 mg.setOpenness(CCConst(ox), CCConst(oy), CCConst(oz), 1.0, 1.0, 1.0);
155 Kokkos::deep_copy(x, 0.0);
156 res.iters.push_back(mg.solvePCG(b, x, r, p, z, Ap, /*maxit=*/300, /*rtol=*/1e-10, 2, 2, 8));
157 }
158 auto hx = Kokkos::create_mirror_view(x);
159 Kokkos::deep_copy(hx, x);
160 for (int zz = G; zz < ext.z - G; ++zz)
161 for (int yy = G; yy < ext.y - G; ++yy)
162 for (int xx = G; xx < ext.x - G; ++xx)
163 res.x.push_back(hx((long)xx + (long)yy * ext.x + (long)zz * (long)ext.x * ext.y));
164 res.levels = mg.localLevels();
165 for (int L = 0; L < res.levels; ++L) {
166 const CutcellMG::Level& lv = mg.level(L);
167 if (!lv.shalo)
168 continue;
169 res.wrapAll = res.wrapAll && lv.wrapAxes == 7;
170 const long nf = (long)lv.halo->numNeighbors(), ns = (long)lv.shalo->numNeighbors();
171 const long gf = (long)lv.halo->numGhostRecv(), gsn = (long)lv.shalo->numGhostRecv();
172 res.nbFull += nf;
173 res.nbStencil += ns;
174 res.ghFull += gf;
175 res.ghStencil += gsn;
176 res.fewer = res.fewer && ns <= nf && gsn <= gf;
177 }
178 return res;
179}
180
181int main(int argc, char** argv) {
182 MPI_Init(&argc, &argv);
183 Kokkos::initialize(argc, argv);
184 int rank = 0, size = 1;
185 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
186 MPI_Comm_size(MPI_COMM_WORLD, &size);
187 int fail = 0;
188 {
189 const int F64 = CutcellMG::kVcycleFp64, F32 = CutcellMG::kVcycleFp32;
190 const Case cases[] = {
191 {"walls y, periodic x z", {64, 48, 32}, 4, {0, 0, 1, 1, 0, 0}, -1, F64, true, true},
192 {"walls y, band matvec", {64, 48, 32}, 4, {0, 0, 1, 1, 0, 0}, -1, F64, false, true},
193 {"inflow -z / outflow +z, walls y",
194 {64, 48, 32},
195 4,
196 {0, 0, 1, 1, 2, 3},
197 -1,
198 F64,
199 true,
200 false},
201 {"odd coarse z (48x40x30), walls y",
202 {48, 40, 30},
203 3,
204 {0, 0, 1, 1, 0, 0},
205 -1,
206 F64,
207 true,
208 false},
209 {"forced telescope @1, walls y", {64, 48, 32}, 4, {0, 0, 1, 1, 0, 0}, 1, F64, true, true},
210 {"all periodic (CA coarse levels)",
211 {64, 48, 32},
212 4,
213 {0, 0, 0, 0, 0, 0},
214 -1,
215 F64,
216 true,
217 true},
218 {"FP32 V-cycle, walls y", {64, 48, 32}, 4, {0, 0, 1, 1, 0, 0}, -1, F32, true, true},
219 };
220 for (const Case& c : cases) {
221 const Run a = solveSteps(c, /*hookOff=*/false);
222 const Run h = solveSteps(c, /*hookOff=*/true);
223 const bool sameIt = a.iters == h.iters;
224 const bool sameX = a.x.size() == h.x.size() &&
225 std::memcmp(a.x.data(), h.x.data(), a.x.size() * sizeof(double)) == 0;
226 int bad = (sameIt && sameX && a.fewer) ? 0 : 1;
227 if (size == 1 && c.wrap1 && !a.wrapAll)
228 bad = 1; // np 1: every level wraps all three axes -> zero stencil exchanges
229 int anyBad = 0;
230 MPI_Allreduce(&bad, &anyBad, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
231 long cnt[4] = {a.nbFull, a.nbStencil, a.ghFull, a.ghStencil}, tot[4];
232 MPI_Allreduce(cnt, tot, 4, MPI_LONG, MPI_SUM, MPI_COMM_WORLD);
233 if (anyBad)
234 ++fail;
235 if (rank == 0) {
236 std::printf(" %-36s np=%d iters:", c.name, size);
237 for (std::size_t s = 0; s < a.iters.size(); ++s)
238 std::printf(" %d/%d", a.iters[s], h.iters[s]);
239 std::printf(" x %s nbrs full/stencil %ld/%ld ghosts %ld/%ld %s\n",
240 sameX ? "bitwise" : "DIFFERS", tot[0], tot[1], tot[2], tot[3],
241 anyBad ? "FAIL" : "ok");
242 }
243 }
244 }
245 int totalFail = 0;
246 MPI_Allreduce(&fail, &totalFail, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
247 if (rank == 0) {
248 if (totalFail == 0)
249 std::printf("OK (np=%d): stencil halo == full halo, bitwise\n", size);
250 else
251 std::fprintf(stderr, "FAILED (np=%d): %d case(s)\n", size, totalFail);
252 }
253 Kokkos::finalize();
254 MPI_Finalize();
255 return totalFail == 0 ? 0 : 1;
256}
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})
void setOpenness(CCConst ox, CCConst oy, CCConst oz, double idx2, double idy2, double idz2)
void setBoundaryConditions(const int bc[6])
void setExactResidual(bool on)
The exact double flux-form level-0 apply in the residual and the Krylov matvec.
void setCommAvoiding(int mask)
Communication-avoiding smoothing mask (kCaMomentum | kCaMg); only kCaMg is read here.
void setVcyclePrecision(int mode)
void debugSetStencilHaloOff(bool on)
flow — portable (Kokkos) geometric multigrid for the cut-cell (variable-openness) pressure Poisson.
Kokkos::View< const double *, CCMem > CCConst
Kokkos::View< double *, CCMem > CCField
const char * name
Kokkos::View< double *, Kokkos::HostSpace > x
double face(int a, double x, double y, double z) const
double coef(double x, double y, double z) const
double open(double x, double y, double z) const
static constexpr int STEPS
static constexpr int G
static int wrap(int a, int n)
static Run solveSteps(const Case &c, bool hookOff)
static void fillScene(CCField ox, CCField oy, CCField oz, CCField b, C3 ext, C3 og, const Scene &sc)
static constexpr double R