flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_mg_fp32_vcycle_mpi.cpp
Go to the documentation of this file.
1
41#include <mpi.h>
42
43#include <algorithm>
44#include <cmath>
45#include <cstdio>
46#include <cstring>
47#include <Kokkos_Core.hpp>
48#include <stdexcept>
49#include <string>
50#include <vector>
51
52#include "mac_cutcell_mg.hpp"
53#include "mg_fp32_problems.hpp"
54
55using namespace peclet::flow;
56using namespace peclet_flow_fp32_problems;
57
58namespace {
59
60int rank = 0, size = 1, fails = 0;
61void check(bool ok, const std::string& what) {
62 int bad = ok ? 0 : 1, any = 0;
63 MPI_Allreduce(&bad, &any, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
64 if (rank == 0)
65 std::printf(" %-100s %s\n", what.c_str(), any ? "FAIL" : "PASS");
66 if (any)
67 ++fails;
68}
69
70// This rank's block of the global openness (inner faces; setOpenness fills the ghosts).
71void setUpDist(CutcellMG& mg, const Problem& P, int mode, int ca = kCaBoth, int teleForce = -1) {
72 mg.setBoundaryConditions(P.bc); // before initMpi (the per-level ghost width reads it)
73 mg.setCommAvoiding(ca);
74 mg.setTelescopeForceLevel(teleForce);
75 mg.initMpi(P.nx, P.ny, P.nz, P.levels, MPI_COMM_WORLD);
76 mg.setVcyclePrecision(mode);
77 const CutcellMG::Level& l0 = mg.level(0);
78 const C3 e = l0.ext, og = l0.og;
79 const int g = l0.g;
80 const std::size_t n = (std::size_t)e.x * e.y * e.z;
81 std::vector<double> ox(n, 0.0), oy(n, 0.0), oz(n, 0.0);
82 for (int z = g; z < e.z - g; ++z)
83 for (int y = g; y < e.y - g; ++y)
84 for (int x = g; x < e.x - g; ++x) {
85 const long li = (long)x + (long)y * e.x + (long)z * (long)e.x * e.y;
86 const long gi = P.idx(x - g + og.x, y - g + og.y, z - g + og.z);
87 ox[li] = P.ox[gi];
88 oy[li] = P.oy[gi];
89 oz[li] = P.oz[gi];
90 }
91 mg.setOpenness(CCConst(toDevice(ox, "ox")), CCConst(toDevice(oy, "oy")),
92 CCConst(toDevice(oz, "oz")), 1.0, 1.0, 1.0);
93}
94
95// G-K2 on every FP32 level: the cells where an exchanged copy of W differs from W.
96long ringMismatches(CutcellMG& mg, int L) {
97 CutcellMG::Level& lv = mg.level(L);
98 long bad = 0;
99 for (const VField* w : {&lv.WX, &lv.WY, &lv.WZ}) {
100 VField s("scratch", w->extent(0));
101 Kokkos::deep_copy(s, *w);
102 lv.devf->exchange(s);
103 const auto hw = toHost(*w), hs = toHost(s);
104 for (std::size_t i = 0; i < hw.size(); ++i)
105 if (std::memcmp(&hw[i], &hs[i], sizeof(VReal)) != 0)
106 ++bad;
107 }
108 return bad;
109}
110
111void ringChecks(CutcellMG& mg, const std::string& tag) {
112 const int last = mg.fp32Last();
113 int ca = 0;
114 for (int L = 0; L <= last; ++L) {
115 CutcellMG::Level& lv = mg.level(L);
116 const bool built = lv.devf != nullptr && lv.WX.extent(0) == lv.n && lv.wOk;
117 check(built, tag + ": L" + std::to_string(L) + " (g " + std::to_string(lv.g) +
118 ") carries the FP32 data and its GridHalo<float>");
119 if (!built)
120 continue;
121 ca += lv.g == 2 ? 1 : 0;
122 const long bad = ringMismatches(mg, L);
123 check(bad == 0, tag + ": G-K2 L" + std::to_string(L) + " ghost WX/WY/WZ == owner (" +
124 std::to_string(bad) + " cells differ)");
125 }
126 int caMax = 0;
127 MPI_Allreduce(&ca, &caMax, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
128 if (rank == 0)
129 std::printf(" %s: fp32Last %d of %d levels, %d CA (g = 2) FP32 levels\n", tag.c_str(), last,
130 mg.nLevels(), caMax);
131 bool ok = true;
132 for (int L = 0; L <= last; ++L)
133 ok = mg.fp32WeightsOk(L) && ok; // collective: the range flags are allreduced
134 check(ok, tag + ": the allreduced range flags pass on every FP32 level");
135}
136
137void problemChecks(const Problem& P) {
138 const std::string tag = P.name + " np" + std::to_string(size);
139 {
140 CutcellMG mg;
141 setUpDist(mg, P, CutcellMG::kVcycleFp32);
142 check(mg.fp32Last() >= 0 && mg.fp32Carriable(), tag + ": carriable distributed");
143 ringChecks(mg, tag + " build");
144 // the setter's rebuild from the stored openness
147 ringChecks(mg, tag + " setter rebuild");
148 const char* why = mg.fp32VcycleIneligible(true);
149 check(why == nullptr, tag + ": eligible distributed (WO-D2): " + (why ? why : "eligible"));
150 }
151 {
152 CutcellMG mg;
153 setUpDist(mg, P, CutcellMG::kVcycleFp64);
154 bool none = true;
155 for (int L = 0; L < mg.nLevels(); ++L)
156 none = none && mg.level(L).devf == nullptr && mg.level(L).WX.extent(0) == 0 &&
157 mg.level(L).xf.extent(0) == 0;
158 check(none, tag + ": an 'fp64' build creates no GridHalo<float> and no FP32 arrays");
159 }
160}
161
162// ---- WO-D2a ------------------------------------------------------------------------------------
163const bool kAutoFp32 = !CutcellMG::kHostMemory; // WO-A: 'auto' is FP32 on device backends only
164constexpr int kMaxit = 200;
165constexpr double kRtol = 1e-10;
166// G-DD2 (a)'s np-equality bound, scale-aware (Decision Desk vof-gdd2-tolerance, 2026-11-15): the
167// pure absolute 1e-8 failed box64 (max|x| 5.5e4) at OMP_NUM_THREADS=1 by reduction-order round-off
168// (1.36e-8 = 2.5e-13 relative). Never tighter than the old 1e-8.
169inline double gdd2aBound(double refMax) {
170 return std::max(1e-8, 1e-12 * refMax);
171}
172
173// Level 0's inner cell (x, y, z) (local, ghosts included) -> the global (single-rank) index.
174long globalIndex(const Problem& P, const CutcellMG::Level& l0, int x, int y, int z) {
175 return P.idx(x - l0.g + l0.og.x, y - l0.g + l0.og.y, z - l0.g + l0.og.z);
176}
177template <class F>
178void forInner(const CutcellMG::Level& l0, F f) {
179 const C3 e = l0.ext;
180 const int g = l0.g;
181 for (int z = g; z < e.z - g; ++z)
182 for (int y = g; y < e.y - g; ++y)
183 for (int x = g; x < e.x - g; ++x)
184 f(x, y, z, (long)x + (long)y * e.x + (long)z * (long)e.x * e.y);
185}
186// This rank's block of a global (single-rank-indexed) field, ghosts 0.
187std::vector<double> sliceOf(const Problem& P, const CutcellMG::Level& l0,
188 const std::vector<double>& global) {
189 std::vector<double> v(l0.n, 0.0);
190 forInner(l0, [&](int x, int y, int z, long li) { v[li] = global[globalIndex(P, l0, x, y, z)]; });
191 return v;
192}
193// Over this rank's inner cells: max|a - ref| (NaN-propagating) and the memcmp-unequal count; both
194// reduced over the ranks.
195struct Diff {
196 double maxAbs = 0.0, refMax = 0.0;
197 long bits = 0;
198};
199Diff diffInner(const Problem& P, const CutcellMG::Level& l0, const std::vector<double>& a,
200 const std::vector<double>& ref) {
201 Diff d;
202 forInner(l0, [&](int x, int y, int z, long li) {
203 const double r = ref[globalIndex(P, l0, x, y, z)];
204 const double v = std::fabs(a[li] - r);
205 if (!(v <= d.maxAbs))
206 d.maxAbs = v;
207 d.refMax = std::max(d.refMax, std::fabs(r));
208 if (std::memcmp(&a[li], &r, sizeof(double)) != 0)
209 ++d.bits;
210 });
211 double m[2] = {d.maxAbs, d.refMax}, gm[2];
212 MPI_Allreduce(m, gm, 2, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
213 long gb = 0;
214 MPI_Allreduce(&d.bits, &gb, 1, MPI_LONG, MPI_SUM, MPI_COMM_WORLD);
215 return Diff{gm[0], gm[1], gb};
216}
217std::string num(double v) {
218 char b[32];
219 std::snprintf(b, sizeof(b), "%.3e", v);
220 return b;
221}
222
223struct Run {
224 int it = -1, restarts = 0, discarded = -1;
225 bool failed = false, fp32 = false, threw = false;
226 std::vector<double> x; // the full level-0 array (single rank: the global array)
227 bool converged() const { return !threw && !failed && it < kMaxit; }
228};
229std::string itStr(const Run& r) {
230 return r.threw
231 ? std::string("threw")
232 : std::to_string(r.it) + (r.failed ? " failed" : "") + (r.fp32 ? " fp32" : " fp64");
233}
234bool gLineRelax = true; // A2.4: the line smoother switch every solveOn applies (default on)
235// One solve of P on `mg` (driver 0 = PCG, 1 = FCG) from x = 0 with the fault armed for it.
236Run solveOn(CutcellMG& mg, const std::vector<double>& hb, int driver, int kind = 0, int at = 0,
237 int maxit = kMaxit, double rtol = kRtol) {
238 const std::size_t n = hb.size();
239 CCField b = toDevice(hb, "b"), x("x", n), r("r", n), p("p", n), z("z", n), zp("zp", n),
240 Ap("Ap", n);
241 mg.setLineRelaxation(gLineRelax); // A2.4 (default on; lineChecks' control arm turns it off)
242 mg.debugSetFp32Fault(kind, at);
243 Run out;
244 try {
245 out.it = driver == 0 ? mg.solvePCG(b, x, r, p, z, Ap, maxit, rtol, 2, 2, 12)
246 : mg.solveFCG(b, x, r, p, z, zp, Ap, maxit, rtol, 2, 2, 12);
247 } catch (const std::runtime_error&) {
248 out.threw = true;
249 }
250 mg.debugSetFp32Fault(0, 0);
251 out.failed = mg.lastSolveFailed();
252 out.fp32 = mg.lastVcycleFp32();
253 out.restarts = mg.vcycleRestarts();
254 out.discarded = mg.lastDiscardedIters();
255 out.x = toHost(x);
256 return out;
257}
258// The single-rank reference, solved on rank 0 and broadcast (the ranks share one GPU on a device
259// backend: a redundant reference per rank would multiply the test's cost by np).
260Run single(const Problem& P, int mode, int driver, int kind = 0, int at = 0) {
261 Run r;
262 if (rank == 0) {
263 CutcellMG mg;
264 setUp(mg, P, mode);
265 r = solveOn(mg, rhsOf(P), driver, kind, at);
266 }
267 int v[6] = {r.it, r.restarts, r.discarded, r.failed, r.fp32, r.threw};
268 MPI_Bcast(v, 6, MPI_INT, 0, MPI_COMM_WORLD);
269 long n = (long)r.x.size();
270 MPI_Bcast(&n, 1, MPI_LONG, 0, MPI_COMM_WORLD);
271 r.x.resize((std::size_t)n);
272 MPI_Bcast(r.x.data(), (int)n, MPI_DOUBLE, 0, MPI_COMM_WORLD);
273 r.it = v[0];
274 r.restarts = v[1];
275 r.discarded = v[2];
276 r.failed = v[3] != 0;
277 r.fp32 = v[4] != 0;
278 r.threw = v[5] != 0;
279 return r;
280}
281Run dist(const Problem& P, int mode, int driver, int kind = 0, int at = 0, int ca = kCaBoth,
282 int maxit = kMaxit, double rtol = kRtol, int teleForce = -1) {
283 CutcellMG mg;
284 setUpDist(mg, P, mode, ca, teleForce);
285 return solveOn(mg, sliceOf(P, mg.level(0), rhsOf(P)), driver, kind, at, maxit, rtol);
286}
287// The level 0 of a distributed setup of P (for the index maps of a dist() result).
288struct DistLevel0 {
289 CutcellMG mg;
290 explicit DistLevel0(const Problem& P, int ca = kCaBoth) {
291 setUpDist(mg, P, CutcellMG::kVcycleFp64, ca);
292 }
293 const CutcellMG::Level& l0() { return mg.level(0); }
294};
295bool sameRun(const Run& a, const Run& b) {
296 return a.x.size() == b.x.size() &&
297 std::memcmp(a.x.data(), b.x.data(), a.x.size() * sizeof(double)) == 0 && a.it == b.it;
298}
299// Every rank's flag set (MPI_LAND of a local bool).
300bool allRanks(bool v) {
301 int a = v ? 1 : 0, g = 0;
302 MPI_Allreduce(&a, &g, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD);
303 return g == 1;
304}
305
306// One FP32 V-cycle z = B r of the mean-free r (distributed or single rank, as `mg` is set up).
307std::vector<double> oneVcycle(CutcellMG& mg, const std::vector<double>& hr) {
308 CutcellMG::Level& l0 = mg.level(0);
309 const std::size_t n = l0.n;
310 { // the driver's smoothing schedule (2, 2, 12): a zero-iteration solve sets it
311 CCField s0("s0", n), s1("s1", n), s2("s2", n), s3("s3", n), s4("s4", n), s5("s5", n);
312 mg.solvePCG(s0, s1, s2, s3, s4, s5, 0, 1e-8, 2, 2, 12);
313 }
314 CCField r = toDevice(hr, "r"), z("z", n);
315 mg.removeMean(l0, r);
316 mg.precondVcycleFp32(z, r, std::ilogb(mg.maxabs(l0, r)), -1, CCField());
317 return toHost(z);
318}
319
320// The true residual max|P(b - A x)| / max|P b| of a distributed solution (the attainable floor).
321double trueResidual(CutcellMG& mg, const std::vector<double>& hb, const std::vector<double>& hx) {
322 CutcellMG::Level& l0 = mg.level(0);
323 CCField b = toDevice(hb, "b"), x = toDevice(hx, "x"), Ap("Ap", l0.n);
324 mg.removeMean(l0, b);
325 const double bn = mg.maxabs(l0, b);
326 mg.matvecOverlap(l0, Ap, x);
327 mg.axpy(b, -1.0, Ap);
328 mg.removeMean(l0, b);
329 return mg.maxabs(l0, b) / bn;
330}
331
332int within(int ref) { // max(1, 5 %) of an iteration count
333 return std::max(1, (int)std::ceil(0.05 * ref));
334}
335
336void d2aChecks(const Problem& P) {
337 const std::string tag = P.name + " np" + std::to_string(size);
338 DistLevel0 D(P);
339 const CutcellMG::Level& l0 = D.l0();
340 {
342 setUpDist(probe, P, CutcellMG::kVcycleFp32);
343 const char* why = probe.fp32VcycleIneligible(true);
344 if (why != nullptr) {
345 check(false, tag + ": ineligible distributed: " + why);
346 return;
347 }
348 if (rank == 0)
349 std::printf(" %s: %d levels, fp32Last %d, eligible\n", tag.c_str(), probe.nLevels(),
350 probe.fp32Last());
351 }
352 const char* dn[2] = {"PCG", "FCG"};
353 std::vector<int> modes{CutcellMG::kVcycleFp32, CutcellMG::kVcycleFp64};
354 if (kAutoFp32)
355 modes.push_back(CutcellMG::kVcycleAuto);
356 auto mname = [](int m) {
357 return m == CutcellMG::kVcycleFp32 ? "fp32" : (m == CutcellMG::kVcycleFp64 ? "fp64" : "auto");
358 };
359 bool fp64Converges[2] = {false, false}; // at this np, per driver: (d) runs there
360 for (int driver : {0, 1}) {
361 Run ref[3], got[3];
362 for (std::size_t k = 0; k < modes.size(); ++k)
363 ref[k] = single(P, modes[k], driver);
364 // np > 1: (a) needs the configurations the single-rank solve converges on, (c) those 'fp64'
365 // converges on; a configuration where neither converges (ratio 1e4: cyl, pack, box64 PCG) is
366 // n/a at np > 1 and is not solved (the np = 1 memcmp of G-DD1 covers it)
367 const bool run = size == 1 || ref[0].converged() || ref[1].converged();
368 if (!run) {
369 if (rank == 0)
370 std::printf(" %-100s n/a\n", (tag + " " + dn[driver] +
371 ": G-DD2 (a), (c) single rank converges in neither mode (" +
372 itStr(ref[0]) + ", " + itStr(ref[1]) + ")")
373 .c_str());
374 continue;
375 }
376 for (std::size_t k = 0; k < modes.size(); ++k)
377 got[k] = dist(P, modes[k], driver);
378 fp64Converges[driver] = got[1].converged();
379 for (std::size_t k = 0; k < modes.size(); ++k) {
380 const std::string t = tag + " " + dn[driver] + " '" + mname(modes[k]) + "'";
381 const Diff d = diffInner(P, l0, got[k].x, ref[k].x);
382 const bool itEq = allRanks(got[k].it == ref[k].it);
383 if (modes[k] == CutcellMG::kVcycleFp64)
384 continue; // the FP64 classes are the existing gates; here only the iteration reference
385 if (!got[k].fp32 && !(modes[k] == CutcellMG::kVcycleAuto && got[k].restarts > 0)) {
386 check(false, t + ": did not run the FP32 V-cycle (" + itStr(got[k]) + ")");
387 continue;
388 }
389 if (size == 1) { // G-DD1 (b), (c)
390 check(d.bits == 0 && itEq && got[k].failed == ref[k].failed,
391 t + ": G-DD1 x memcmp == single rank (" + std::to_string(d.bits) +
392 " cells differ), iterations " + itStr(got[k]) + " / " + itStr(ref[k]));
393 } else if (ref[k].converged()) { // G-DD2 (a)
394 const bool itOk = std::abs(got[k].it - ref[k].it) <= within(ref[k].it);
395 check(d.maxAbs <= gdd2aBound(d.refMax) && allRanks(itOk),
396 t + ": G-DD2 (a) max|x - x1| " + num(d.maxAbs) + " (max|x| " + num(d.refMax) +
397 "), iterations " + itStr(got[k]) + " vs " + itStr(ref[k]));
398 } else if (rank == 0) {
399 std::printf(" %-100s n/a\n", (t + ": G-DD2 (a) single rank does not converge (" +
400 itStr(ref[k]) + "), np " + itStr(got[k]))
401 .c_str());
402 }
403 }
404 // G-DD2 (c): 'fp32' vs 'fp64' at this np where 'fp64' converges
405 if (got[1].converged())
406 check(got[0].it <= got[1].it + within(got[1].it),
407 tag + " " + dn[driver] + ": G-DD2 (c) 'fp32' " + itStr(got[0]) + " <= 'fp64' " +
408 itStr(got[1]) + " + max(1, 5 %)");
409 else if (rank == 0)
410 std::printf(" %-100s n/a\n",
411 (tag + " " + dn[driver] + ": G-DD2 (c) 'fp64' does not converge (" +
412 itStr(got[1]) + "), 'fp32' " + itStr(got[0]))
413 .c_str());
414 }
415 // G-DD1 (a) / G-DD2 (b): one V-cycle
416 const bool vcycleCase = P.name == "cyl" || P.name == "box64";
417 if (size == 1 || vcycleCase) {
418 CutcellMG ms, md;
420 setUpDist(md, P, CutcellMG::kVcycleFp32);
421 const bool sameDepth = ms.nLevels() == md.nLevels();
422 const std::vector<double> rhs = rhsOf(P);
423 const std::vector<double> zs = oneVcycle(ms, rhs);
424 const std::vector<double> zd = oneVcycle(md, sliceOf(P, md.level(0), rhs));
425 const Diff d = diffInner(P, md.level(0), zd, zs);
426 if (size == 1)
427 check(d.bits == 0, tag + ": G-DD1 (a) one FP32 V-cycle z memcmp == single rank (" +
428 std::to_string(d.bits) + " cells differ)");
429 else if (sameDepth)
430 check(d.maxAbs <= 1e-11 * d.refMax,
431 tag + ": G-DD2 (b) one FP32 V-cycle max|dz|/max|z| " + num(d.maxAbs / d.refMax));
432 else if (rank == 0)
433 std::printf(" %s: G-DD2 (b) n/a (depth %d vs single rank %d)\n", tag.c_str(), md.nLevels(),
434 ms.nLevels());
435 }
436 // G-DD2 (d): the attainable floor at np 2, where 'fp64' converges
437 if (size == 2)
438 for (int driver : {0, 1}) {
439 if (!fp64Converges[driver])
440 continue;
441 double fl[2];
442 int its[2];
443 for (int k = 0; k < 2; ++k) {
444 CutcellMG mg;
445 setUpDist(mg, P, k == 0 ? CutcellMG::kVcycleFp32 : CutcellMG::kVcycleFp64);
446 const std::vector<double> hb = sliceOf(P, mg.level(0), rhsOf(P));
447 const Run r = solveOn(mg, hb, driver, 0, 0, 300, 1e-14);
448 its[k] = r.it;
449 fl[k] = trueResidual(mg, hb, r.x);
450 }
451 check(fl[0] <= 2.0 * fl[1], tag + " " + dn[driver] + ": G-DD2 (d) floor fp32 " + num(fl[0]) +
452 " (" + std::to_string(its[0]) + " it) <= 2 x fp64 " +
453 num(fl[1]) + " (" + std::to_string(its[1]) + " it)");
454 }
455 // G-DD2 (e): CA on == CA off, bitwise (periodic problems; CA needs no domain BC)
456 if (size > 1 && vcycleCase) {
457 // level 0 is g = 1 either way (CA widens the coarse levels only): the same layout
458 // (30 iterations: bitwise equality needs no convergence)
459 const Run on = dist(P, CutcellMG::kVcycleFp32, 0, 0, 0, kCaBoth, 30);
460 const Run off = dist(P, CutcellMG::kVcycleFp32, 0, 0, 0, 0, 30);
461 check(allRanks(sameRun(on, off)),
462 tag + ": G-DD2 (e) CA on == CA off bitwise (" + itStr(on) + " / " + itStr(off) + ")");
463 }
464 // G-DD2 (g): two runs at np 2, bitwise
465 if (size == 2) {
466 const Run a = dist(P, CutcellMG::kVcycleFp32, 0, 0, 0, kCaBoth, 30),
467 b = dist(P, CutcellMG::kVcycleFp32, 0, 0, 0, kCaBoth, 30);
468 check(allRanks(sameRun(a, b)), tag + ": G-DD2 (g) two runs bitwise (" + itStr(a) + ")");
469 }
470}
471
472// G-R (h): the restart on the distributed driver.
473void restartChecks(const Problem& P) {
474 const std::string tag = P.name + " np" + std::to_string(size);
475 {
477 setUpDist(probe, P, CutcellMG::kVcycleFp32);
478 if (probe.fp32VcycleIneligible(true) != nullptr)
479 return; // reported by d2aChecks
480 }
481 for (int driver : {0, 1}) {
482 const std::string t = tag + (driver == 0 ? " PCG" : " FCG");
483 const Run ref64 = dist(P, CutcellMG::kVcycleFp64, driver);
484 auto restartedIsFp64 = [&](const Run& a) {
485 return allRanks(sameRun(a, ref64) && a.failed == ref64.failed && !a.fp32 && a.restarts == 1 &&
486 !a.threw);
487 };
488 auto restartsAgree = [&](const Run& a) {
489 int mn = 0, mx = 0;
490 MPI_Allreduce(&a.restarts, &mn, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD);
491 MPI_Allreduce(&a.restarts, &mx, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
492 return mn == mx;
493 };
494 if (!kAutoFp32) {
495 const Run a = dist(P, CutcellMG::kVcycleAuto, driver, 1, 3);
496 check(allRanks(sameRun(a, ref64) && !a.fp32 && a.restarts == 0),
497 t + ": G-R (h) host 'auto' == 'fp64', the fault inert, no restart (" + itStr(a) + ")");
498 } else {
499 const Run a = dist(P, CutcellMG::kVcycleAuto, driver, 1, 3);
500 check(restartedIsFp64(a) && restartsAgree(a),
501 t + ": G-R (h)(a) kind 1 at 3 -> restart == fp64 cold (" + itStr(a) + ", discarded " +
502 std::to_string(a.discarded) + ")");
503 const Run b = dist(P, CutcellMG::kVcycleAuto, driver, 2, 2);
504 check(restartedIsFp64(b) && b.discarded == 2 && restartsAgree(b),
505 t + ": G-R (h)(b) kind 2 at 2 -> restart == fp64 cold, discarded " +
506 std::to_string(b.discarded));
507 if (size == 1) {
508 const Run sa = single(P, CutcellMG::kVcycleAuto, driver, 1, 3);
509 DistLevel0 D(P);
510 const Diff d = diffInner(P, D.l0(), a.x, sa.x);
511 check(d.bits == 0 && a.it == sa.it && a.restarts == sa.restarts,
512 t + ": G-R (h) np 1 restart memcmp == the single-rank restart (" +
513 std::to_string(d.bits) + " cells differ)");
514 }
515 }
516 const Run c1 = dist(P, CutcellMG::kVcycleFp32, driver, 1, 3);
517 const Run c2 = dist(P, CutcellMG::kVcycleFp32, driver, 2, 2);
518 check(allRanks(c1.restarts == 0 && c1.failed && c1.fp32 && c2.restarts == 0 && c2.it == 2 &&
519 c2.fp32),
520 t + ": G-R (h)(c) forced 'fp32': no restart; kind 1 " + itStr(c1) + ", kind 2 " +
521 itStr(c2));
522 }
523}
524
525// G-DD2 (f) (WO-D2b1): a telescoped hierarchy, natural (teleForce < 0) or forced at teleForce.
526void teleChecks(const Problem& P, int teleForce) {
527 const std::string tag =
528 P.name + (teleForce >= 0 ? " forced-tele L" + std::to_string(teleForce) : " natural-tele") +
529 " np" + std::to_string(size);
531 setUpDist(probe, P, CutcellMG::kVcycleFp32, kCaBoth, teleForce);
532 int tl = -1;
533 for (int L = 0; L < probe.nLevels(); ++L)
534 if (probe.level(L).tele) {
535 tl = L;
536 break;
537 }
538 int tlMin = 0, tlMax = 0;
539 MPI_Allreduce(&tl, &tlMin, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD);
540 MPI_Allreduce(&tl, &tlMax, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
541 if (tlMax < 0) {
542 if (rank == 0)
543 std::printf(" %s: does not telescope at this np (n/a)\n", tag.c_str());
544 return;
545 }
546 check(tlMin == tlMax && probe.fp32Last() == tl,
547 tag + ": the first telescope level " + std::to_string(tl) + " is fp32Last (" +
548 std::to_string(probe.fp32Last()) + ") on every rank");
549 const char* why = probe.fp32VcycleIneligible(true);
550 check(why == nullptr, tag + ": eligible: " + (why ? why : "yes"));
551 if (why != nullptr)
552 return;
553 if (rank == 0)
554 std::printf(" %s: %d levels on rank 0, FP32 on 0..%d, FP64 below\n", tag.c_str(),
555 probe.nLevels(), tl);
556 const char* dn[2] = {"PCG", "FCG"};
557 std::vector<int> modes{CutcellMG::kVcycleFp32, CutcellMG::kVcycleFp64};
558 if (kAutoFp32)
559 modes.push_back(CutcellMG::kVcycleAuto);
560 for (int driver : {0, 1}) {
561 Run ref[3], got[3];
562 for (std::size_t k = 0; k < modes.size(); ++k) {
563 ref[k] = single(P, modes[k], driver);
564 got[k] = dist(P, modes[k], driver, 0, 0, kCaBoth, kMaxit, kRtol, teleForce);
565 }
566 for (std::size_t k = 0; k < modes.size(); ++k) {
567 if (modes[k] == CutcellMG::kVcycleFp64)
568 continue;
569 const std::string t =
570 tag + " " + dn[driver] + (modes[k] == CutcellMG::kVcycleFp32 ? " 'fp32'" : " 'auto'");
571 if (!got[k].fp32) {
572 check(false, t + ": did not run the FP32 V-cycle (" + itStr(got[k]) + ")");
573 continue;
574 }
575 const Diff d = diffInner(P, probe.level(0), got[k].x, ref[k].x);
576 if (ref[k].converged()) {
577 const bool itOk = std::abs(got[k].it - ref[k].it) <= within(ref[k].it);
578 check(d.maxAbs <= gdd2aBound(d.refMax) && allRanks(itOk),
579 t + ": G-DD2 (f)(a) max|x - x1| " + num(d.maxAbs) + " (max|x| " + num(d.refMax) +
580 "), iterations " + itStr(got[k]) + " vs " + itStr(ref[k]));
581 } else if (rank == 0) {
582 std::printf(
583 " %-100s n/a\n",
584 (t + ": G-DD2 (f)(a) single rank does not converge (" + itStr(ref[k]) + ")").c_str());
585 }
586 }
587 if (got[1].converged())
588 check(got[0].it <= got[1].it + within(got[1].it),
589 tag + " " + dn[driver] + ": G-DD2 (f)(c) 'fp32' " + itStr(got[0]) + " <= 'fp64' " +
590 itStr(got[1]) + " + max(1, 5 %)");
591 else if (rank == 0)
592 std::printf(" %-100s n/a\n",
593 (tag + " " + dn[driver] + ": G-DD2 (f)(c) 'fp64' does not converge (" +
594 itStr(got[1]) + ")")
595 .c_str());
596 }
597 // (b): one V-cycle against single rank (the telescope keeps the depth). (b)'s premise (§8) is a
598 // V-cycle of the same precision split: where the first telescope point is the last non-bottom
599 // level, the FP64 transition sits where single rank's FP32 -> FP64 bottom interface does and
600 // (b) is gated; above it the distributed V-cycle runs FP64 below the telescope point (§3.2)
601 // where single rank runs FP32, a different preconditioner -- reported against the single-rank
602 // FP32-vs-FP64 V-cycle difference for scale, not gated.
603 CutcellMG ms, md;
605 setUpDist(md, P, CutcellMG::kVcycleFp32, kCaBoth, teleForce);
606 int depth = md.nLevels(), depthMax = 0;
607 MPI_Allreduce(&depth, &depthMax, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
608 const std::vector<double> rhs = rhsOf(P);
609 const std::vector<double> zs = oneVcycle(ms, rhs);
610 const std::vector<double> zd = oneVcycle(md, sliceOf(P, md.level(0), rhs));
611 const Diff d = diffInner(P, md.level(0), zd, zs);
612 if (depthMax != ms.nLevels()) {
613 if (rank == 0)
614 std::printf(" %s: G-DD2 (f)(b) n/a (depth %d vs single rank %d)\n", tag.c_str(), depthMax,
615 ms.nLevels());
616 } else if (tl == ms.nLevels() - 2) {
617 check(d.maxAbs <= 1e-11 * d.refMax,
618 tag + ": G-DD2 (f)(b) one FP32 V-cycle max|dz|/max|z| " + num(d.maxAbs / d.refMax));
619 } else {
620 CutcellMG m64; // the single-rank FP64 V-cycle of the same r
622 CutcellMG::Level& l0 = m64.level(0);
623 {
624 CCField s0("s0", l0.n), s1("s1", l0.n), s2("s2", l0.n), s3("s3", l0.n), s4("s4", l0.n),
625 s5("s5", l0.n);
626 m64.solvePCG(s0, s1, s2, s3, s4, s5, 0, 1e-8, 2, 2, 12);
627 }
628 CCField r = toDevice(rhs, "r"), z("z", l0.n);
629 m64.removeMean(l0, r);
630 m64.precondVcycle(z, r);
631 const std::vector<double> z64 = toHost(z);
632 double dm = 0.0, zm = 0.0;
633 for (int zc = 1; zc <= P.nz; ++zc)
634 for (int y = 1; y <= P.ny; ++y)
635 for (int x = 1; x <= P.nx; ++x) {
636 const long i = P.idx(x - 1, y - 1, zc - 1);
637 dm = std::max(dm, std::fabs(z64[i] - zs[i]));
638 zm = std::max(zm, std::fabs(zs[i]));
639 }
640 if (rank == 0)
641 std::printf(
642 " %s: G-DD2 (f)(b) info: FP64 below L%d (single rank FP32 to L%d): "
643 "max|dz|/max|z| %s vs single rank (single-rank fp32 vs fp64 V-cycle %s)\n",
644 tag.c_str(), tl, ms.nLevels() - 2, num(d.maxAbs / d.refMax).c_str(),
645 num(dm / zm).c_str());
646 }
647}
648
649// doc/mg_depth_policy.md A2.4 / A2.5 G-LR (4), at the CutcellMG level: the zebra line smoother
650// (default on) on hierarchies with triggered smoothed levels, distributed against the single-rank
651// reference -- np 1 memcmp-equal x and equal iterations (MG-PCG, rtol 1e-10), np > 1 G-DD2 (a)'s
652// bound (max|dx| <= max(1e-8, 1e-12 max|x|), iterations within max(1, 5 %)), each with LR off as
653// the control. aniso: 64x64x25 periodic, a heavy blob at 1e2, AUTO depth (s = z periodic,
654// Sherman-Morrison); anisoW: the same between z walls (Thomas); thinW: 64x64x4 between z walls at 5
655// levels, 'fp64' and forced 'fp32' (every FP32 level even: the FP32 line kernel, distributed
656// through the FP32 stencil halo). The LR-off arm is the control (the point smoother's own np 1 / np
657// > 1 classes on the same problems).
658Problem lineProblem(const std::string& name, int nz, int levels, bool walls) {
659 int bc[6] = {0, 0, 0, 0, 0, 0};
660 if (walls)
661 bc[4] = bc[5] = 1;
662 auto rho = [nz](double x, double y, double z) {
663 const double dx = x - 24, dy = y - 40, dz = (z - 0.5 * nz) * 3.0;
664 return dx * dx + dy * dy + dz * dz < 144.0 ? 1e2 : 1.0;
665 };
666 return makeProblem(name, 64, 64, nz, levels, bc, [](double, double, double) { return 1.0; }, rho);
667}
668void lineChecks(const Problem& P, std::vector<int> modes) {
669 const std::string tag = P.name + " np" + std::to_string(size);
670 std::vector<int> ls;
671 {
674 ls = ms.lineSmoothedLevels();
675 }
676 check(!ls.empty(),
677 tag + ": the hierarchy has triggered smoothed levels (" + std::to_string(ls.size()) + ")");
678 int held = 0, on = 0; // the triggered levels this rank holds (telescoping), and how many LR
679 {
680 CutcellMG md;
681 setUpDist(md, P, CutcellMG::kVcycleFp64);
682 for (int L : ls)
683 if (L < md.nLevels()) {
684 ++held;
685 on += md.lineRelaxOn(md.level(L)) ? 1 : 0;
686 }
687 }
688 check(on == held && (size > 1 || held == (int)ls.size()),
689 tag + ": every triggered level a rank holds is line-relaxed (s whole on the blocks)");
690 // LR off first: the point smoother's own np 1 class on the same problem is the control. At np 1
691 // the LR memcmp is a check where the control is memcmp-equal; where it is not (aniso: the odd
692 // periodic z, a pre-existing np 1 difference of the point path) the LR line is printed only.
693 std::vector<bool> ctlEq(modes.size(), false);
694 for (int lr : {0, 1})
695 for (std::size_t k = 0; k < modes.size(); ++k) {
696 const int mode = modes[k];
697 gLineRelax = lr == 1;
698 const std::string t =
699 tag + (mode == CutcellMG::kVcycleFp32 ? " 'fp32'" : " 'fp64'") + (lr ? " LR" : " LR off");
700 const Run ref = single(P, mode, 0);
701 const Run got = dist(P, mode, 0);
702 DistLevel0 D(P);
703 const Diff d = diffInner(P, D.l0(), got.x, ref.x);
704 const bool itEq = allRanks(got.it == ref.it);
705 if (size == 1) {
706 const bool eq = d.bits == 0 && itEq && got.failed == ref.failed;
707 const std::string what = t + ": x memcmp == single rank (" + std::to_string(d.bits) +
708 " cells differ), iterations " + itStr(got) + " / " + itStr(ref);
709 if (!lr)
710 ctlEq[k] = eq;
711 if (lr && ctlEq[k])
712 check(eq, what);
713 else if (rank == 0)
714 std::printf(
715 " %-100s %s\n", what.c_str(),
716 lr ? "n/a (the LR-off control differs too)" : (eq ? "(control) =" : "(control) !="));
717 } else {
718 const bool itOk = allRanks(std::abs(got.it - ref.it) <= within(ref.it));
719 check(ref.converged() && got.converged() && d.maxAbs <= gdd2aBound(d.refMax) && itOk,
720 t + ": max|dx| " + num(d.maxAbs) + " <= " + num(gdd2aBound(d.refMax)) +
721 ", iterations " + itStr(got) + " vs " + itStr(ref));
722 }
723 }
724 gLineRelax = true;
725}
726} // namespace
727
728int main(int argc, char** argv) {
729 MPI_Init(&argc, &argv);
730 Kokkos::initialize(argc, argv);
731 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
732 MPI_Comm_size(MPI_COMM_WORLD, &size);
733 if (rank == 0)
734 std::printf("mg_fp32_vcycle_mpi np=%d (doc/vof_projection_distributed_design.md §5, G-K2)\n",
735 size);
736 for (const Problem& P : {cylProblem(), boxProblem()})
737 problemChecks(P);
738 for (const Problem& P :
740 d2aChecks(P);
741 restartChecks(columnProblem());
742 lineChecks(lineProblem("aniso", 25, 0, false), {CutcellMG::kVcycleFp64});
743 lineChecks(lineProblem("anisoW", 25, 0, true), {CutcellMG::kVcycleFp64});
744 lineChecks(lineProblem("thinW", 4, 5, true), {CutcellMG::kVcycleFp64, CutcellMG::kVcycleFp32});
745 if (size == 4 || size == 8) { // G-DD2 (f) (WO-D2b1)
746 Problem c6 = columnProblem();
747 c6.levels = 6;
748 c6.name = "column6";
749 teleChecks(c6, -1);
750 }
751 if (size == 4)
752 teleChecks(columnProblem(), 1);
753 if (rank == 0)
754 std::printf("%s (%d failure%s)\n", fails ? "FAIL" : "PASS", fails, fails == 1 ? "" : "s");
755 Kokkos::finalize();
756 MPI_Finalize();
757 return fails ? 1 : 0;
758}
static constexpr bool kHostMemory
void axpy(CCField y, double a, CCField x)
void removeMean(Level &lv, CCField f, bool stopGuard=false)
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 precondVcycleFp32(CCField zz, CCField rr, int e, int k, CCField p)
void matvecOverlap(Level &l0, CCField y, CCField v)
void debugSetFp32Fault(int kind, int at)
const char * fp32VcycleIneligible(bool krylov) const
void setOpenness(CCConst ox, CCConst oy, CCConst oz, double idx2, double idy2, double idz2)
void setBoundaryConditions(const int bc[6])
double maxabs(Level &lv, CCField a)
bool lineRelaxOn(const Level &lv) const
void precondVcycle(CCField zz, CCField rr)
void setLineRelaxation(bool on)
A2.4 (doc/mg_depth_policy.md, WO-7): zebra line relaxation along the blocked strong axis on the trigg...
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 fp32WeightsOk(int L) const
void setCommAvoiding(int mask)
Communication-avoiding smoothing mask (kCaMomentum | kCaMg); only kCaMg is read here.
void setVcyclePrecision(int mode)
flow — portable (Kokkos) geometric multigrid for the cut-cell (variable-openness) pressure Poisson.
The FP32 V-cycle test problems (column, cyl, slab, pack) and their right-hand side,...
y
Definition cmp.py:24
Kokkos::View< const double *, CCMem > CCConst
Kokkos::View< VReal *, CCMem > VField
Kokkos::View< double *, CCMem > CCField
std::vector< double > rhsOf(const Problem &P)
Problem slabProblem(double ratio=1e3)
CCField toDevice(const std::vector< double > &v, const char *name)
void setUp(CutcellMG &mg, const Problem &P, int mode)
Problem makeProblem(const std::string &name, int nx, int ny, int nz, int levels, const int bc[6], Open open, Rho rho)
std::vector< typename V::non_const_value_type > toHost(const V &f)
int m
Definition post.py:35
t
Definition post.py:21
P
Definition stab5.py:16
list ms
Definition summarize.py:60
dict its
Definition summarize.py:62
Kokkos::View< VReal *, CCMem > WX
Kokkos::View< VReal *, CCMem > xf
static void check(bool ok, const std::string &what)
static constexpr double F
static int run(int bc)
static int fails
static std::vector< double > sliceOf(const std::vector< double > &g, int ox, int oy, int oz, int lnx, int lny, int lnz)