flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_vof_redistribute_mpi.cpp
Go to the documentation of this file.
1// flow — dynamic load balancing on a VoF run: `rebalance_by_weights` / `Solver::redistribute` with
2// a partition that actually MOVES.
3//
4// The defect this gates (found by WO-V9 item 4, fixed on branch `vof-rebalance`): everything the
5// solver grows AFTER construction — the FieldSet's own storage (`add`: the VoF colour mirror "C",
6// "kappa"/"kappa_branch", every closure target, every transported scalar), the member handles that
7// ALIAS those records, the lazily-allocated per-block scratch, and the block container's per-rank
8// box table — did not follow `allocateBlock` onto the new block. `redistribute` then memcpy'd the
9// NEW padded extent into the OLD allocation: an AddressSanitizer heap-buffer-overflow (a
10// 184320-byte write into a 115328-byte region) surfacing as `free(): invalid pointer` /
11// `malloc(): unsorted double linked list corrupted` on whichever ranks GREW.
12//
13// What is gated here, in order of what each can only catch:
14//
15// R1 THE MOVE IS EXACT DATA MOVEMENT. The global C, u, v, w, P and the transported scalar T are
16// gathered immediately before and immediately after the rebalance and must be BITWISE equal
17// (`!=`, not a tolerance) at every np — `redistributeGridFields` is pure data movement and
18// step 5's `setSolid` re-derives the geometry from the migrated SDF, so nothing about the
19// state may change. A stale view surviving the move shows up here as a differing cell long
20// before it corrupts the heap. The test FAILS if the partition did not actually move, so the
21// gate can never pass vacuously.
22//
23// R2 STEPPING AFTER THE MOVE IS STILL THE SAME SOLVER. The run continues to STEPS on the new
24// partition and is compared with a full-grid single-rank reference that never redistributed.
25// np = 1 is bitwise (one rank owns everything either way); np > 1 carries the pressure
26// driver's documented allreduce reduction-order floor.
27//
28// R3 THE BLOCK CONTAINER'S EXCHANGE TABLE FOLLOWS THE NEW DECOMPOSITION. Configuration 1 turns
29// on the per-bubble block container (rung W0). Its gather/scatter pieces are cut against the
30// owned box of EVERY rank; those boxes come from the `BlockDecomposer`, so a partition that
31// moves invalidates them. Gated by the same R1/R2 pair with the union colour and the
32// per-marker volumes carried through the move.
33//
34// Grid 24x24x48, two bubbles stacked along the long z axis (the aligned ORB's cut axis), all
35// fluid — the block container is all-fluid at rung W0.
36#include <mpi.h>
37
38#include <cmath>
39#include <cstdio>
40#include <cstdlib>
41#include <cstring>
42#include <Kokkos_Core.hpp>
43#include <vector>
44
45#include "flow_ibm.hpp"
46#include "peclet/core/common/types.hpp"
47#include "peclet/core/decomp/block_decomposer.hpp"
48
49using peclet::core::IVec;
50using peclet::core::decomp::BlockDecomposer;
53
54static constexpr int NX = 24, NY = 24, NZ = 48;
55static constexpr std::size_t GCELLS = (std::size_t)NX * NY * NZ;
56static constexpr double RHO_L = 10.0, RHO_G = 1.0, MU_L = 0.5, MU_G = 0.05, SIGMA = 4.0;
57static constexpr double RB = 4.5, GRAV = 2.0e-3;
58static const double CX[2] = {NX / 2.0 + 0.13, NX / 2.0 + 0.13};
59static const double CY[2] = {NY / 2.0 - 0.21, NY / 2.0 - 0.21};
60static const double CZ[2] = {14.0 + 0.31, 30.0 + 0.31};
61static const int ZSPLIT = 22; // clips the seed boxes so each marker gets exactly one sphere
62
63static double bubbleAt(int b, int x, int y, int z) {
64 const int NS = 12;
65 double acc = 0.0;
66 for (int j = 0; j < NS; ++j)
67 for (int i = 0; i < NS; ++i) {
68 const double px = x + (i + 0.5) / NS - CX[b], py = y + (j + 0.5) / NS - CY[b];
69 const double r2 = RB * RB - px * px - py * py;
70 if (r2 <= 0.0)
71 continue;
72 const double h = std::sqrt(r2);
73 const double lo = std::fmax(CZ[b] - h, (double)z), hi = std::fmin(CZ[b] + h, (double)z + 1);
74 if (hi > lo)
75 acc += hi - lo;
76 }
77 return acc / (NS * NS);
78}
79
80// The whole feature surface the defect lived on: VoF ("C" + the curvature mirrors), three property
81// closures (registry targets "rho"/"mu"/"force_z" resolved by NAME), a transported scalar (its own
82// registry buffer + its own per-block operator), and optionally the block container.
83// cfg 0: VoF + closures + scalar; 1: + the block container; 2: VoF + scalar + PHASE CHANGE with
84// the consistent-energy transport — the configuration that owns the g = 3 area drivers
85// (`pcAreaC_`/`pcAreaMc_`) and `vofEnergy_`, whose own `ready()`/`initialized()` predicates ask
86// "has init run", not "is it the right size".
87static void configure(IbmSolver& s, int ox, int oy, int oz, int lnx, int lny, int lnz, int cfg) {
88 s.setAdvection(false);
89 const bool blocks = (cfg == 1), pc = (cfg == 2);
90 s.setRho(RHO_L);
91 s.setMu(MU_L);
92 s.setDomainBc(4, 1, 0, 0, 0);
93 s.setDomainBc(5, 1, 0, 0, 0); // walls +-z (the CUT axis)
94 s.setPressureGeometry(std::vector<double>((std::size_t)lnx * lny * lnz, 10.0));
95 // Pure RB-GS / Chebyshev pressure: decomposition-agnostic, so a WEIGHTED partition is admissible
96 // (the geometric coarse levels assume clean coarsening — see test_redistribute_mpi).
97 s.setPressureLevels(1);
98 s.setPressureChebyshev(true, 400, 1e-14);
99 s.enableVof();
100 std::vector<double> c((std::size_t)lnx * lny * lnz, 0.0), t(c.size(), 0.0);
101 for (int z = 0; z < lnz; ++z)
102 for (int y = 0; y < lny; ++y)
103 for (int x = 0; x < lnx; ++x) {
104 const double v =
105 std::fmax(bubbleAt(0, x + ox, y + oy, z + oz), bubbleAt(1, x + ox, y + oy, z + oz));
106 const std::size_t i = (std::size_t)x + (std::size_t)y * lnx + (std::size_t)z * lnx * lny;
107 c[i] = v;
108 t[i] = 0.5 + 0.25 * std::sin(0.3 * (x + ox)) * std::cos(0.2 * (z + oz));
109 }
110 s.setVof(c);
111 s.setPropertyModel("rho", ClosureKind::LinearMix, "C", "", {RHO_L, RHO_G - RHO_L});
112 s.setPropertyModel("mu", ClosureKind::LinearMix, "C", "", {MU_L, MU_G - MU_L});
113 s.setPropertyModel("force_z", ClosureKind::LinearMix, "C", "",
114 {-RHO_L * GRAV, -(RHO_G - RHO_L) * GRAV});
115 if (!pc)
116 s.setSurfaceTension(SIGMA);
117 s.addScalar("T", 0.02, 1, 30);
118 s.setField("T", t);
119 if (pc) {
120 s.enablePhaseChange(RHO_G, RHO_L, 10.0);
121 s.setPhaseChangeThermal("T", 0.5, 0.02, 0.02, 0.0);
122 s.setPhaseChangeEnergy(1.0, 2.0);
123 }
124 if (blocks) {
125 std::vector<std::array<int, 6>> boxes;
126 for (int b = 0; b < 2; ++b) {
127 std::array<int, 6> q;
128 const double c3[3] = {CX[b], CY[b], CZ[b]};
129 for (int d = 0; d < 3; ++d) {
130 q[d] = (int)std::floor(c3[d] - RB) - 1;
131 q[3 + d] = (int)std::ceil(c3[d] + RB) + 1;
132 }
133 if (b == 0)
134 q[5] = ZSPLIT;
135 else
136 q[2] = ZSPLIT;
137 boxes.push_back(q);
138 }
139 s.enableVofBlocksFromField(boxes);
140 }
141 // cfg 2 has no surface tension, so `capillaryDt()` is infinite there: a fixed dt well inside the
142 // Weymouth-Yue cap instead.
143 s.setDt(pc ? 0.02 : 0.25 * s.capillaryDt());
144}
145
146static std::vector<double> gatherGlobal(const std::vector<double>& local, int ox, int oy, int oz,
147 int lnx, int lny, int lnz, int rank, int size) {
148 std::vector<double> global;
149 if (rank == 0)
150 global.assign(GCELLS, 0.0);
151 for (int r = 0; r < size; ++r) {
152 int meta[6] = {ox, oy, oz, lnx, lny, lnz};
153 if (r == 0) {
154 if (rank == 0)
155 for (int z = 0; z < lnz; ++z)
156 for (int y = 0; y < lny; ++y)
157 std::memcpy(&global[(std::size_t)ox + (std::size_t)(y + oy) * NX +
158 (std::size_t)(z + oz) * NX * NY],
159 &local[(std::size_t)y * lnx + (std::size_t)z * lnx * lny],
160 (std::size_t)lnx * sizeof(double));
161 continue;
162 }
163 if (rank == r) {
164 MPI_Send(meta, 6, MPI_INT, 0, 100 + r, MPI_COMM_WORLD);
165 MPI_Send(local.data(), (int)local.size(), MPI_DOUBLE, 0, 200 + r, MPI_COMM_WORLD);
166 } else if (rank == 0) {
167 MPI_Recv(meta, 6, MPI_INT, r, 100 + r, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
168 std::vector<double> buf((std::size_t)meta[3] * meta[4] * meta[5]);
169 MPI_Recv(buf.data(), (int)buf.size(), MPI_DOUBLE, r, 200 + r, MPI_COMM_WORLD,
170 MPI_STATUS_IGNORE);
171 for (int z = 0; z < meta[5]; ++z)
172 for (int y = 0; y < meta[4]; ++y)
173 std::memcpy(&global[(std::size_t)meta[0] + (std::size_t)(y + meta[1]) * NX +
174 (std::size_t)(z + meta[2]) * NX * NY],
175 &buf[(std::size_t)y * meta[3] + (std::size_t)z * meta[3] * meta[4]],
176 (std::size_t)meta[3] * sizeof(double));
177 }
178 }
179 return global;
180}
181
182// NaN-propagating (WO-R2): `std::fmax(m, NaN) == m`, so the obvious loop reports 0 for a field
183// that has gone entirely NaN.
184static double maxDiff(const std::vector<double>& a, const std::vector<double>& b) {
185 double m = 0;
186 for (std::size_t i = 0; i < b.size(); ++i) {
187 const double d = std::fabs(a[i] - b[i]);
188 if (!(d <= m))
189 m = d;
190 }
191 return m;
192}
193static long countBitDiff(const std::vector<double>& a, const std::vector<double>& b) {
194 long n = 0;
195 for (std::size_t i = 0; i < b.size(); ++i)
196 if (a[i] != b[i])
197 ++n;
198 return n;
199}
200
201int main(int argc, char** argv) {
202 MPI_Init(&argc, &argv);
203 Kokkos::initialize(argc, argv);
204 int fail = 0;
205 {
206 int rank = 0, size = 1;
207 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
208 MPI_Comm_size(MPI_COMM_WORLD, &size);
209 const int STEPS = (argc > 1) ? std::atoi(argv[1]) : 12;
210 const int MOVE_AT = STEPS / 2;
211
212 // D1 = the legacy aligned ORB (levels = 1, pinned); D2 = the WEIGHTED ORB the rebalance
213 // produces. The weight field is the interface-weighted one WO-V9 item 4 uses (1 + W*[mixed]),
214 // condensed to "1 + W where the two bubbles are" so the test does not depend on the transported
215 // state. D1 is pinned because the auto default balances 24x24x48 np 2 to exactly the weighted
216 // ORB's partition, so the partition would not move and nothing would be gated.
217 auto D1 = peclet::flow::CutcellMG::decomposition(static_cast<std::size_t>(size), NX, NY, NZ, 1);
218 std::vector<peclet::core::Real> w(GCELLS, 1.0);
219 for (int z = 0; z < NZ; ++z)
220 for (int y = 0; y < NY; ++y)
221 for (int x = 0; x < NX; ++x)
222 if (std::fmax(bubbleAt(0, x, y, z), bubbleAt(1, x, y, z)) > 1e-8)
223 w[(std::size_t)x + (std::size_t)y * NX + (std::size_t)z * NX * NY] = 24.0;
224 BlockDecomposer<3> D2((std::size_t)size, IVec<3>{NX, NY, NZ}, w);
225
226 auto blk = D1.block(rank);
227 const int ox = (int)blk.origin[0], oy = (int)blk.origin[1], oz = (int)blk.origin[2];
228 const int lnx = (int)blk.size[0], lny = (int)blk.size[1], lnz = (int)blk.size[2];
229 // The gate is vacuous unless the partition MOVES: the defect only fires on a rank whose block
230 // changed size (a growing rank overflows its old allocation).
231 int moved = 0;
232 for (int r = 0; r < size; ++r) {
233 const auto a = D1.block(r), b = D2.block(r);
234 for (int d = 0; d < 3; ++d)
235 if (a.origin[d] != b.origin[d] || a.size[d] != b.size[d])
236 moved = 1;
237 }
238 if (rank == 0) {
239 std::printf(
240 "VOF REDISTRIBUTE MPI np=%d grid %dx%dx%d block %dx%dx%d partition moves: %s\n", size,
241 NX, NY, NZ, lnx, lny, lnz, moved ? "YES" : "no");
242 for (int r = 0; r < size && size > 1; ++r) {
243 const auto a = D1.block(r), b = D2.block(r);
244 std::printf(" rank %d: %ldx%ldx%ld @ (%ld,%ld,%ld) -> %ldx%ldx%ld @ (%ld,%ld,%ld)\n",
245 r, (long)a.size[0], (long)a.size[1], (long)a.size[2], (long)a.origin[0],
246 (long)a.origin[1], (long)a.origin[2], (long)b.size[0], (long)b.size[1],
247 (long)b.size[2], (long)b.origin[0], (long)b.origin[1], (long)b.origin[2]);
248 }
249 }
250 if (size > 1 && !moved) {
251 if (rank == 0)
252 std::printf(" FAIL - the weighted ORB coincides with the default one; nothing is gated\n");
253 fail = 1;
254 }
255
256 static const char* CFGNAME[3] = {"VoF + closures + scalar",
257 "VoF + closures + scalar + the BLOCK container",
258 "VoF + scalar + PHASE CHANGE (consistent energy)"};
259 for (int cfg = 0; cfg < 3; ++cfg) {
260 const bool blocks = (cfg == 1);
261 if (rank == 0)
262 std::printf(" --- configuration: %s\n", CFGNAME[cfg]);
263 IbmSolver sd(lnx, lny, lnz);
264 sd.initMpi(D1, MPI_COMM_WORLD);
265 configure(sd, ox, oy, oz, lnx, lny, lnz, cfg);
266 for (int k = 0; k < MOVE_AT; ++k)
267 sd.step();
268
269 // ---- R1: the move is exact data movement -------------------------------------------
270 const char* fn[6] = {"C", "u", "v", "w", "P", "T"};
271 std::vector<double> pre[6], post[6];
272 auto snap = [&](std::vector<double>* out, int px, int py, int pz, int qx, int qy, int qz) {
273 out[0] = gatherGlobal(sd.getVof(), px, py, pz, qx, qy, qz, rank, size);
274 for (int c = 0; c < 3; ++c)
275 out[1 + c] = gatherGlobal(sd.getVelocity(c), px, py, pz, qx, qy, qz, rank, size);
276 out[4] = gatherGlobal(sd.getPressure(), px, py, pz, qx, qy, qz, rank, size);
277 out[5] = gatherGlobal(sd.getField("T"), px, py, pz, qx, qy, qz, rank, size);
278 };
279 snap(pre, ox, oy, oz, lnx, lny, lnz);
280 std::vector<double> volPre;
281 if (blocks) {
282 const auto st = sd.vofBlockStats();
283 std::vector<double> l(st.size(), 0.0);
284 volPre.assign(st.size(), 0.0);
285 for (std::size_t i = 0; i < st.size(); ++i)
286 l[i] = st[i].volume;
287 MPI_Allreduce(l.data(), volPre.data(), (int)l.size(), MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
288 }
289
290 sd.rebalanceByWeights(w); // <-- the call that corrupted the heap
291
292 const auto nb = D2.block(rank);
293 const int nox = (int)nb.origin[0], noy = (int)nb.origin[1], noz = (int)nb.origin[2];
294 const int nlx = (int)nb.size[0], nly = (int)nb.size[1], nlz = (int)nb.size[2];
295 snap(post, nox, noy, noz, nlx, nly, nlz);
296 if (rank == 0) {
297 std::printf(" R1 across the move (must be bitwise):");
298 for (int i = 0; i < 6; ++i) {
299 const long nd = countBitDiff(pre[i], post[i]);
300 std::printf(" %s %ld cells / %.2e", fn[i], nd, maxDiff(pre[i], post[i]));
301 if (nd != 0)
302 fail = 1;
303 }
304 std::printf("\n");
305 }
306 if (blocks) {
307 const auto st = sd.vofBlockStats();
308 std::vector<double> l(st.size(), 0.0), volPost(st.size(), 0.0);
309 for (std::size_t i = 0; i < st.size(); ++i)
310 l[i] = st[i].volume;
311 MPI_Allreduce(l.data(), volPost.data(), (int)l.size(), MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
312 if (rank == 0) {
313 double dm = 0.0;
314 for (std::size_t i = 0; i < volPost.size(); ++i)
315 dm = std::fmax(dm, std::fabs(volPost[i] - volPre[i]));
316 std::printf(" R3 marker volumes across the move: %zu markers, max|dV| = %.3e\n",
317 volPost.size(), dm);
318 if (!(dm <= 1e-12 * std::fmax(1.0, volPre.empty() ? 1.0 : volPre[0])))
319 fail = 1;
320 }
321 }
322
323 // ---- R2: the run continues on the new partition ------------------------------------
324 for (int k = MOVE_AT; k < STEPS; ++k)
325 sd.step();
326 std::vector<double> g[6];
327 snap(g, nox, noy, noz, nlx, nly, nlz);
328
329 // The CONTROL: the same distributed run that never rebalanced. It is what calibrates R2 —
330 // a fixed-sweep-count RB-GS (the scalar's implicit diffusion) is decomposition-dependent by
331 // construction, so an absolute tolerance here would either be vacuous or would measure the
332 // decomposition and blame the redistribute. Measured on this scene at np = 2: the scalar's
333 // own decomposition floor is 4.17e-05 with no rebalance at all and 4.20e-05 with one.
334 IbmSolver sc(lnx, lny, lnz);
335 sc.initMpi(D1, MPI_COMM_WORLD);
336 configure(sc, ox, oy, oz, lnx, lny, lnz, cfg);
337 for (int k = 0; k < STEPS; ++k)
338 sc.step();
339 std::vector<double> gc[6];
340 gc[0] = gatherGlobal(sc.getVof(), ox, oy, oz, lnx, lny, lnz, rank, size);
341 for (int c = 0; c < 3; ++c)
342 gc[1 + c] = gatherGlobal(sc.getVelocity(c), ox, oy, oz, lnx, lny, lnz, rank, size);
343 gc[4] = gatherGlobal(sc.getPressure(), ox, oy, oz, lnx, lny, lnz, rank, size);
344 gc[5] = gatherGlobal(sc.getField("T"), ox, oy, oz, lnx, lny, lnz, rank, size);
345
346 if (rank == 0) {
347 IbmSolver ref(NX, NY, NZ);
348 configure(ref, 0, 0, 0, NX, NY, NZ, cfg);
349 for (int k = 0; k < STEPS; ++k)
350 ref.step();
351 std::vector<double> r[6] = {ref.getVof(), ref.getVelocity(0), ref.getVelocity(1),
352 ref.getVelocity(2), ref.getPressure(), ref.getField("T")};
353 std::printf(
354 " R2 vs the single-rank reference after %d steps"
355 " [rebalanced | never-rebalanced control]:\n ",
356 STEPS);
357 for (int i = 0; i < 6; ++i) {
358 const double d = maxDiff(g[i], r[i]), dc = maxDiff(gc[i], r[i]);
359 // np = 1: the control IS the reference (one rank owns everything), so `bound` collapses
360 // to 1e-13 and the gate is bit-exactness. np > 1: the rebalanced run must sit at the
361 // control's own floor, not merely under some absolute number.
362 const double bound = std::fmax(1e-13, 4.0 * dc);
363 std::printf(" d%s %.2e|%.2e", fn[i], d, dc);
364 if (!(d <= bound))
365 fail = 1;
366 }
367 std::printf("\n");
368 }
369 }
370 }
371 int totalFail = 0;
372 MPI_Allreduce(&fail, &totalFail, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
373 int rank = 0;
374 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
375 int size = 1;
376 MPI_Comm_size(MPI_COMM_WORLD, &size);
377 if (rank == 0)
378 std::printf(totalFail == 0
379 ? "OK (np=%d): a VoF (+block) run survives a MOVING rebalance bit-exactly\n"
380 : "FAILED (np=%d)\n",
381 size);
382 Kokkos::finalize();
383 MPI_Finalize();
384 return totalFail == 0 ? 0 : 1;
385}
std::vector< double > getField(const std::string &name)
std::vector< double > getVof()
std::vector< double > getVelocity(int c)
std::vector< vof::VofBlockStats > vofBlockStats() const
std::vector< double > getPressure()
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
Solver< Staggered > IbmSolver
Definition post.py:1
static constexpr int NY
static constexpr int NX
static constexpr std::size_t GCELLS
static constexpr int NZ
static constexpr int STEPS
static constexpr double GRAV
static constexpr double SIGMA
static constexpr double CY
static constexpr double RHO_G
static constexpr double RHO_L
static constexpr double CZ
static constexpr double MU_G
static const int ZSPLIT
static constexpr double RB
static constexpr double MU_L
static const double CX[2]
static double maxDiff(const std::vector< double > &a, const std::vector< double > &b)
static std::vector< double > gatherGlobal(const std::vector< double > &local, int ox, int oy, int oz, int lnx, int lny, int lnz, int rank, int size)
static void configure(IbmSolver &s, int ox, int oy, int oz, int lnx, int lny, int lnz, int cfg)
static double bubbleAt(int b, int x, int y, int z)
static long countBitDiff(const std::vector< double > &a, const std::vector< double > &b)