flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_cutcellmg_aniso_mpi.cpp
Go to the documentation of this file.
1// G4 under MPI — the ANISOTROPIC pressure hierarchy, multi-rank
2// (flow/doc/anisotropic_metric.md §8.5 last bullet + §5.3/§5.4 and trap 6; Phase 2 commit C3).
3//
4// The SAME problem the single-rank `cutcellmg_aniso` gate solves — the Zick & Homsy SC sphere
5// (phi = 0.216, R = 0.3722 L) on the stretched box (N, 2N, N/2) of the cube L^3, apertures from
6// the solver's own `buildOpenness` with hp = (1, 1/2, 2), weights w = (1, 4, 1/4) — driven through
7// the PRODUCTION `CutcellMG::initMpi` and compared against the validated single-rank `init()`.
8//
9// Four statements:
10// 1. the LEVEL TABLE the aspect rule chooses is IDENTICAL on every rank. It has to be: every
11// rank computes it from the same doubles and the same replicated decomposition, with no
12// communication (§5.3). It is also the single-rank table wherever the even-block gate does
13// not bite.
14// 2. np = 1 is BIT-EXACT to the single-rank solve (the gating is byte-identical), np = 2/4 agree
15// to the MG-PCG reduction-order floor — the tolerance pattern of `test_cutcellmg_mpi`.
16// 3. TRAP 6: an axis DEFERRED by the aspect rule is NOT "blocked". With the economic trigger
17// off (`setTelescopeMinExtent(0)`, so a merge happens only when an axis that CAN coarsen
18// globally is not even on every rank) and telescoping ON, NO level telescopes — even though
19// the rule defers x and z for two levels. Deferral alone must never merge ranks.
20// 4. a FORCED telescope at level 1 (`setTelescopeForceLevel(1)`) still reaches the same answer
21// on the deferred hierarchy: the merge path and the aspect rule compose.
22#include <mpi.h>
23
24#include <cmath>
25#include <cstdio>
26#include <cstring>
27#include <exception>
28#include <Kokkos_Core.hpp>
29#include <utility>
30#include <vector>
31
32#include "mac_cutcell.hpp" // buildOpenness (the solver's own aperture model)
33#include "mac_cutcell_mg.hpp"
34#include "peclet/core/common/types.hpp"
35
36using peclet::core::IVec;
42
43static constexpr int G = 1, NLEV = 6;
44static constexpr int N = 32; // stretched grid (N, 2N, N/2) == N^3 cells
45static constexpr double LBOX = 1.0, RSPH = 0.3722; // phi = 0.216 in the unit cell
46
47// d' = (|x - c|_minimage - R)/dref at the PHYSICAL centre of the GLOBAL cell (gx, gy, gz).
48static double sdfAt(int gx, int gy, int gz, int nx, int ny, int nz, double dref) {
49 double px = (gx + 0.5) * (LBOX / nx) - 0.5 * LBOX, py = (gy + 0.5) * (LBOX / ny) - 0.5 * LBOX,
50 pz = (gz + 0.5) * (LBOX / nz) - 0.5 * LBOX;
51 px -= LBOX * std::round(px / LBOX);
52 py -= LBOX * std::round(py / LBOX);
53 pz -= LBOX * std::round(pz / LBOX);
54 return (std::sqrt(px * px + py * py + pz * pz) - RSPH * LBOX) / dref;
55}
56
57// Assemble the operator on `mg`'s level-0 block and solve MG-PCG to 1e-10; returns the host
58// iterate. `og` is this block's global inner origin (0 single-rank).
59using HostVec = decltype(Kokkos::create_mirror_view(std::declval<CCField>()));
60static HostVec setupAndSolve(CutcellMG& mg, C3 ext, C3 og, int nx, int ny, int nz, bool collective,
61 int* iters) {
62 const double hx = LBOX / nx, hy = LBOX / ny, hz = LBOX / nz, dref = hx;
63 const double hp[3] = {hx / dref, hy / dref, hz / dref};
64 const double wx = 1.0 / (hp[0] * hp[0]), wy = 1.0 / (hp[1] * hp[1]), wz = 1.0 / (hp[2] * hp[2]);
65 const std::size_t n = (std::size_t)ext.x * ext.y * ext.z;
66 CCField sdf("sdf", n), ox("ox", n), oy("oy", n), oz("oz", n);
67 {
68 auto m = Kokkos::create_mirror_view(sdf);
69 for (int z = 0; z < ext.z; ++z)
70 for (int y = 0; y < ext.y; ++y)
71 for (int x = 0; x < ext.x; ++x)
72 m((long)x + (long)y * ext.x + (long)z * (long)ext.x * ext.y) =
73 sdfAt(x - G + og.x, y - G + og.y, z - G + og.z, nx, ny, nz, dref);
74 Kokkos::deep_copy(sdf, m);
75 }
76 buildOpenness(ox, oy, oz, CCConst(sdf), ext, hp[0], hp[1], hp[2], 1);
77 mg.setOpenness(CCConst(ox), CCConst(oy), CCConst(oz), wx, wy, wz);
78
79 // The RHS of the single-rank gate: the smooth periodic mode on the rows that carry an open
80 // face, mean-removed over exactly those rows. The mean is GLOBAL, so it is an Allreduce here
81 // and a local sum single-rank — the same number either way (a plain sum of the same set).
82 std::vector<double> hb(n, 0.0);
83 {
84 auto hAC = Kokkos::create_mirror_view(mg.level(0).AC);
85 Kokkos::deep_copy(hAC, mg.level(0).AC);
86 const double k = 2.0 * M_PI / LBOX;
87 double loc[2] = {0.0, 0.0};
88 for (int z = G; z < ext.z - G; ++z)
89 for (int y = G; y < ext.y - G; ++y)
90 for (int x = G; x < ext.x - G; ++x) {
91 const std::size_t i =
92 (std::size_t)x + (std::size_t)y * ext.x + (std::size_t)z * (std::size_t)ext.x * ext.y;
93 if (!(hAC(i) > 0.0f))
94 continue;
95 const double px = (x - G + og.x + 0.5) * (LBOX / nx),
96 py = (y - G + og.y + 0.5) * (LBOX / ny),
97 pz = (z - G + og.z + 0.5) * (LBOX / nz);
98 hb[i] = std::cos(k * px) * std::cos(k * py) * std::cos(k * pz);
99 loc[0] += hb[i];
100 loc[1] += 1.0;
101 }
102 double glob[2] = {loc[0], loc[1]};
103 if (collective) // the distributed build owns one block per rank; the reference owns them all
104 MPI_Allreduce(loc, glob, 2, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
105 const double mean = glob[1] > 0.0 ? glob[0] / glob[1] : 0.0;
106 for (int z = G; z < ext.z - G; ++z)
107 for (int y = G; y < ext.y - G; ++y)
108 for (int x = G; x < ext.x - G; ++x) {
109 const std::size_t i =
110 (std::size_t)x + (std::size_t)y * ext.x + (std::size_t)z * (std::size_t)ext.x * ext.y;
111 if (hAC(i) > 0.0f)
112 hb[i] -= mean;
113 }
114 }
115 CCField b("b", n), x("x", n), r("r", n), p("p", n), z("z", n), Ap("Ap", n);
116 {
117 auto m = Kokkos::create_mirror_view(b);
118 for (std::size_t i = 0; i < n; ++i)
119 m(i) = hb[i];
120 Kokkos::deep_copy(b, m);
121 }
122 *iters = mg.solvePCG(b, x, r, p, z, Ap, /*maxit=*/500, /*rtol=*/1e-10, 2, 2, 12);
123 auto hx2 = Kokkos::create_mirror_view(x);
124 Kokkos::deep_copy(hx2, x);
125 return hx2;
126}
127
128int main(int argc, char** argv) {
129 MPI_Init(&argc, &argv);
130 Kokkos::initialize(argc, argv);
131 int fail = 0, size = 1, rank = 0;
132 {
133 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
134 MPI_Comm_size(MPI_COMM_WORLD, &size);
135 const int nx = N, ny = 2 * N, nz = N / 2;
136 const double hp[3] = {1.0, 0.5, 2.0};
137
138 // ---- the single-rank reference hierarchy (every rank builds it redundantly) ----------------
139 CutcellMG ref;
140 ref.setMetric(hp);
141 ref.init(nx, ny, nz, NLEV);
142 const C3 re{nx + 2 * G, ny + 2 * G, nz + 2 * G}, ro{0, 0, 0};
143 int refIt = 0;
144 auto hr = setupAndSolve(ref, re, ro, nx, ny, nz, /*collective=*/false, &refIt);
145 const std::vector<C3> refTab = ref.levelRatios();
146
147 // ---- PASS 1: the distributed hierarchy, telescoping ON, economic trigger OFF ---------------
148 // so a merge can only be triggered by a genuinely BLOCKED axis (trap 6).
149 CutcellMG mg;
150 mg.setMetric(hp);
151 mg.setTelescope(true);
153 mg.initMpi(nx, ny, nz, NLEV, MPI_COMM_WORLD);
154 CutcellMG::Level& l0 = mg.level(0);
155 int it = 0;
156 auto hx = setupAndSolve(mg, l0.ext, l0.og, nx, ny, nz, /*collective=*/true, &it);
157 const std::vector<C3> tab = mg.levelRatios();
158
159 // G-D1(d) of doc/vof_projection_cost_design.md §13 asserted "not eligible distributed"; since
160 // WO-D2a of doc/vof_projection_distributed_design.md the distributed FP32 V-cycle runs: the
161 // forced-'fp32' arm is checked against the single-rank 'fp32' solve after statement 2 below.
162
163 // 1. the level table is the same on every rank (a pure function of replicated data).
164 {
165 int nl = (int)tab.size(), nlMin = 0, nlMax = 0;
166 MPI_Allreduce(&nl, &nlMin, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD);
167 MPI_Allreduce(&nl, &nlMax, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
168 if (nlMin != nlMax) {
169 ++fail;
170 std::fprintf(stderr, "[rank %d] level COUNT differs across ranks: %d..%d\n", rank, nlMin,
171 nlMax);
172 } else {
173 std::vector<int> mine(3 * (std::size_t)nl), lo(3 * (std::size_t)nl),
174 hi(3 * (std::size_t)nl);
175 for (int L = 0; L < nl; ++L) {
176 mine[3 * (std::size_t)L] = tab[(std::size_t)L].x;
177 mine[3 * (std::size_t)L + 1] = tab[(std::size_t)L].y;
178 mine[3 * (std::size_t)L + 2] = tab[(std::size_t)L].z;
179 }
180 MPI_Allreduce(mine.data(), lo.data(), 3 * nl, MPI_INT, MPI_MIN, MPI_COMM_WORLD);
181 MPI_Allreduce(mine.data(), hi.data(), 3 * nl, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
182 for (int i = 0; i < 3 * nl; ++i)
183 if (lo[(std::size_t)i] != hi[(std::size_t)i]) {
184 ++fail;
185 std::fprintf(stderr, "[rank %d] level table entry %d differs across ranks\n", rank, i);
186 break;
187 }
188 }
189 if (rank == 0) {
190 std::printf(" level table (np=%d):", size);
191 for (const C3& rr : tab)
192 std::printf(" (%d,%d,%d)", rr.x, rr.y, rr.z);
193 std::printf("\n single-rank reference: ");
194 for (const C3& rr : refTab)
195 std::printf(" (%d,%d,%d)", rr.x, rr.y, rr.z);
196 std::printf("\n");
197 }
198 // The aspect rule's own signature, whatever the even-block gate does below it.
199 if (!(tab[0].x == 1 && tab[0].y == 2 && tab[0].z == 1)) {
200 ++fail;
201 std::fprintf(stderr, "[rank %d] level 0 ratio (%d,%d,%d), expected (1,2,1)\n", rank,
202 tab[0].x, tab[0].y, tab[0].z);
203 }
204 }
205
206 // 3. TRAP 6: no level telescoped — deferral is not blocking.
207 {
208 int tele = 0;
209 for (int L = 0; L < mg.nLevels(); ++L)
210 if (mg.level(L).tele)
211 ++tele;
212 int teleAll = 0;
213 MPI_Allreduce(&tele, &teleAll, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
214 if (teleAll != 0) {
215 ++fail;
216 std::fprintf(stderr,
217 "[rank %d] %d level(s) telescoped with the economic trigger off — an "
218 "aspect-DEFERRED axis was treated as BLOCKED (trap 6)\n",
219 rank, teleAll);
220 } else if (rank == 0)
221 std::printf(" trap 6: no telescope on the deferred hierarchy (min-extent trigger off)\n");
222 }
223
224 // 2. against the single-rank solve.
225 double xref = 0.0; // the reference solution's scale, for the RELATIVE floor below
226 for (std::size_t i = 0; i < hr.extent(0); ++i)
227 if (!(std::fabs(hr(i)) <= xref))
228 xref = std::fabs(hr(i));
229 auto blockDiff = [&](HostVec h, C3 ext, C3 og) {
230 double m = 0.0;
231 for (int z = G; z < ext.z - G; ++z)
232 for (int y = G; y < ext.y - G; ++y)
233 for (int x = G; x < ext.x - G; ++x) {
234 const int gx = x - G + og.x, gy = y - G + og.y, gz = z - G + og.z;
235 const double a = h((long)x + (long)y * ext.x + (long)z * (long)ext.x * ext.y);
236 const double rr =
237 hr((long)(gx + G) + (long)(gy + G) * re.x + (long)(gz + G) * (long)re.x * re.y);
238 const double d = std::fabs(a - rr);
239 if (!(d <= m))
240 m = d;
241 }
242 return m;
243 };
244 // np = 1 is BIT-EXACT (the distributed gating is byte-identical there). np > 1 is compared
245 // RELATIVE to the solution scale, at the MG-PCG's own floor: the driver stops on a RESIDUAL
246 // (rtol 1e-10 of |b|inf), and on a CUT-CELL operator a small-aperture row has a tiny effective
247 // eigenvalue, so the same residual leaves a much larger solution spread than the all-fluid
248 // hierarchy of test_cutcellmg_mpi does. Measured here (max|phi| = 14.67): 1.01e-07 /
249 // 2.97e-07 relative at np = 2 / 4, and a -DPECLET_FLOW_OPERATOR_DOUBLE build of the identical
250 // source moves it only 4x (to 2.46e-08 / 1.21e-07), so it is the stopping rule and NOT the
251 // float operator storage.
252 // The iteration count -- the decomposition-independence statement that actually has teeth --
253 // is gated to within 2 of the single-rank one.
254 const double tol = (size == 1) ? 0.0 : 1e-6 * xref;
255 const double d1 = blockDiff(hx, l0.ext, l0.og);
256 if (!(d1 <= tol)) {
257 ++fail;
258 std::fprintf(stderr,
259 "[rank %d] pass 1 max|distributed - single-rank| = %.3e (rel %.3e, tol %.3e)\n",
260 rank, d1, d1 / xref, tol);
261 } else if (rank == 0)
262 std::printf(
263 " pass 1: %d iters (single-rank %d), max|dist - single| = %.3e = %.3e relative "
264 "of max|phi| = %.4f (np=%d%s)\n",
265 it, refIt, d1, d1 / xref, xref, size, size == 1 ? ", BIT-EXACT" : "");
266 if (std::abs(it - refIt) > 2) {
267 ++fail;
268 std::fprintf(stderr, "[rank %d] pass 1 iterations %d vs single-rank %d (allowed +/-2)\n",
269 rank, it, refIt);
270 }
271 if (it >= 500 || refIt >= 500) {
272 ++fail;
273 std::fprintf(stderr, "[rank %d] pass 1 CAPPED (%d / %d iters)\n", rank, it, refIt);
274 }
275
276 // ---- the forced-'fp32' arm (doc/vof_projection_distributed_design.md §8 G-DD1 (d), WO-D2a) --
277 // pass 1's hierarchy under the FP32 V-cycle against the single-rank 'fp32' solve: np = 1
278 // memcmp-equal with equal iterations, np > 1 at pass 1's tolerance and iteration window.
279 {
280 CutcellMG r32;
281 r32.setMetric(hp);
282 r32.init(nx, ny, nz, NLEV);
284 CutcellMG m32;
285 m32.setMetric(hp);
286 m32.setTelescope(true);
288 m32.initMpi(nx, ny, nz, NLEV, MPI_COMM_WORLD);
290 int r32It = 0, it32 = 0;
291 bool threw = false;
292 HostVec h32, hx32;
293 try {
294 h32 = setupAndSolve(r32, re, ro, nx, ny, nz, /*collective=*/false, &r32It);
295 hx32 = setupAndSolve(m32, m32.level(0).ext, m32.level(0).og, nx, ny, nz,
296 /*collective=*/true, &it32);
297 } catch (const std::exception& ex) {
298 threw = true;
299 std::fprintf(stderr, "[rank %d] fp32 arm threw: %s\n", rank, ex.what());
300 }
301 if (threw || !r32.lastVcycleFp32() || !m32.lastVcycleFp32()) {
302 ++fail;
303 std::fprintf(stderr, "[rank %d] fp32 arm: the FP32 V-cycle did not run\n", rank);
304 } else {
305 const C3 e = m32.level(0).ext, og = m32.level(0).og;
306 double d = 0.0;
307 long bits = 0;
308 for (int z = G; z < e.z - G; ++z)
309 for (int y = G; y < e.y - G; ++y)
310 for (int x = G; x < e.x - G; ++x) {
311 const double a = hx32((long)x + (long)y * e.x + (long)z * (long)e.x * e.y);
312 const double rr = h32((long)(x - G + og.x + G) + (long)(y - G + og.y + G) * re.x +
313 (long)(z - G + og.z + G) * (long)re.x * re.y);
314 const double dd = std::fabs(a - rr);
315 if (!(dd <= d))
316 d = dd;
317 if (std::memcmp(&a, &rr, sizeof(double)) != 0)
318 ++bits;
319 }
320 const double tol = (size == 1) ? 0.0 : 1e-6 * xref;
321 const bool ok = size == 1 ? (bits == 0 && it32 == r32It)
322 : (d <= tol && std::abs(it32 - r32It) <= 2 && it32 < 500);
323 if (!ok) {
324 ++fail;
325 std::fprintf(stderr,
326 "[rank %d] fp32 arm: max|dist - single| = %.3e (%ld cells differ), %d iters "
327 "vs single-rank %d\n",
328 rank, d, bits, it32, r32It);
329 } else if (rank == 0)
330 std::printf(
331 " fp32 arm: %d iters (single-rank 'fp32' %d), max|dist - single| = %.3e "
332 "(np=%d%s)\n",
333 it32, r32It, d, size, size == 1 ? ", MEMCMP-EQUAL" : "");
334 }
335 }
336
337 // ---- PASS 2: a FORCED telescope at level 1 on the deferred hierarchy -----------------------
338 if (size > 1) {
339 CutcellMG mg2;
340 mg2.setMetric(hp);
341 mg2.setTelescope(true);
344 mg2.initMpi(nx, ny, nz, NLEV, MPI_COMM_WORLD);
345 CutcellMG::Level& m0 = mg2.level(0);
346 int it2 = 0;
347 auto hx2 = setupAndSolve(mg2, m0.ext, m0.og, nx, ny, nz, /*collective=*/true, &it2);
348 int forced = mg2.level(1).tele ? 1 : 0, forcedAll = 0;
349 MPI_Allreduce(&forced, &forcedAll, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
350 const double d2 = blockDiff(hx2, m0.ext, m0.og);
351 if (forcedAll != 1) {
352 ++fail;
353 std::fprintf(stderr, "[rank %d] pass 2: setTelescopeForceLevel(1) did not merge\n", rank);
354 }
355 if (!(d2 <= 1e-6 * xref)) {
356 ++fail;
357 std::fprintf(stderr, "[rank %d] pass 2 max|telescoped - single-rank| = %.3e (rel %.3e)\n",
358 rank, d2, d2 / xref);
359 } else if (rank == 0)
360 std::printf(
361 " pass 2 (telescope forced at level 1): %d iters, max|dist - single| = %.3e "
362 "= %.3e relative\n",
363 it2, d2, d2 / xref);
364 if (rank == 0 && mg2.level(0).ratio.x == 1 && mg2.level(0).ratio.y == 2)
365 std::printf(" pass 2: the aspect rule still owns level 0 across the merge\n");
366 }
367 }
368 int totalFail = 0;
369 MPI_Allreduce(&fail, &totalFail, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
370 if (rank == 0) {
371 if (totalFail == 0)
372 std::printf("OK (np=%d): the anisotropic CutcellMG hierarchy, distributed\n", size);
373 else
374 std::fprintf(stderr, "FAILED (np=%d): %d rank(s) failed\n", size, totalFail);
375 }
376 Kokkos::finalize();
377 MPI_Finalize();
378 return totalFail ? 1 : 0;
379}
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 setMetric(const double hp[3])
Per-axis cell spacings h_a' = h_a / hRef of the physical domain (doc/anisotropic_metric....
std::vector< C3 > levelRatios() const
The per-level coarsening ratio actually chosen (the level table of §5, gate §8.5).
void setOpenness(CCConst ox, CCConst oy, CCConst oz, double idx2, double idy2, double idz2)
void init(int nx, int ny, int nz, int nLevels)
void setVcyclePrecision(int mode)
flow — portable (Kokkos) cut-cell pressure-operator face openness from an SDF.
flow — portable (Kokkos) geometric multigrid for the cut-cell (variable-openness) pressure Poisson.
Kokkos::View< const double *, CCMem > CCConst
Kokkos::View< double *, CCMem > CCField
void buildOpenness(CCField ox, CCField oy, CCField oz, CCConst sdf, C3 ext, double dx, double dy, double dz, int order=1)
static constexpr int N
static constexpr int G
static constexpr double RSPH
static constexpr double LBOX
static HostVec setupAndSolve(CutcellMG &mg, C3 ext, C3 og, int nx, int ny, int nz, bool collective, int *iters)
static constexpr int NLEV
decltype(Kokkos::create_mirror_view(std::declval< CCField >())) HostVec
static double sdfAt(int gx, int gy, int gz, int nx, int ny, int nz, double dref)
void buildOpenness(CCField ox, CCField oy, CCField oz, CCConst sdf, C3 ext, double dx, double dy, double dz, int order=1)
static double mean(const std::vector< double > &a)
static double blockDiff(const std::vector< double > &local, const std::vector< double > &global, int ox, int oy, int oz, int lnx, int lny, int lnz)