flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_bottom_direct_mpi.cpp
Go to the documentation of this file.
1// flow — the distributed host 'direct' pressure bottom (doc/vof_step_performance_design.md §14.3
2// H-6): every rank holding the bottom solves the allgathered GLOBAL bottom gb_ redundantly.
3//
4// Why this test exists (WO-H6 review, findings 1 and 2): a wrong gb_ (a bad global-id map, gather
5// order or boundary fill) only weakens the preconditioner, and the outer PCG still converges to
6// rtol, so a tolerance comparison against a reference cannot see it. ITERATION IDENTITY can: the
7// direct bottom and GraphAMG both solve the same global bottom to their tolerances, so their
8// preconditioners agree to the bottom's round-off and the outer PCG takes the same number of
9// iterations every step. Gates, on a walled (+-y), cut-cell (a sphere's fractional openness),
10// variable-coefficient operator rebuilt every "step" (a dense blob moving through the domain):
11//
12// A in place: no telescope, the bottom on every rank;
13// B a FORCED TELESCOPE at level 1: the bottom lives on the group roots only (bt.comm = roots,
14// the roots' og / gdim), the other ranks idle below the telescope point;
15// C a REPARTITION stage (a weighted level-0 ORB with an odd root split + setRepartition, the
16// test_telescope_mpi case D knob);
17// in each: explicit 'direct' (it raises when not taken) vs explicit 'algebraic', pressure
18// iterations identical EVERY step and the final state's max|x_d - x_a| / max|x_a| <= 1e-12; B and
19// C also assert that the bottom actually sits below a telescope / repartition point (fewer ranks
20// hold it than run) and is agglomerated (A..C: 'direct' did not raise).
21// H review note 1: an explicit 'direct' that is INELIGIBLE under a telescope (a 16^3 bottom: no
22// axis gives planes of at most 192 cells) raises on EVERY rank -- a rank idling below the
23// telescope point used to block in the transition scatter while the roots raised.
24//
25// Host backends only: the device keeps GraphAMG distributed (§13.9 Q-D7; 'direct' raises there by
26// design), so on a device build the test reports SKIP (exit 77). np = 2 and 4.
27#include <mpi.h>
28
29#include <algorithm>
30#include <cmath>
31#include <cstdio>
32#include <Kokkos_Core.hpp>
33#include <stdexcept>
34#include <string>
35#include <vector>
36
37#include "mac_cutcell_mg.hpp"
38#include "peclet/core/common/types.hpp"
39#include "peclet/core/decomp/block_decomposer.hpp"
40
41using peclet::core::Index;
42using peclet::core::IVec;
47
48static constexpr int G = 1;
49static constexpr int STEPS = 6;
50
51static int wrap(int a, int n) {
52 return ((a % n) + n) % n;
53}
54
55// Face coefficient (x-face of cell (gx, gy, gz) sits at gx - 1/2): the sphere's open fraction
56// times 1 / rho of a dense blob moving along z with the step; 1 everywhere on a wall-free axis.
57struct Scene {
58 IVec<3> gs;
59 int step = 0;
60 double open(double x, double y, double z) const {
61 const double cx = 0.5 * gs[0], cy = 0.5 * gs[1], cz = 0.5 * gs[2], R = 0.22 * gs[1];
62 const double phi =
63 std::sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy) + (z - cz) * (z - cz)) - R;
64 return std::clamp(phi + 0.5, 0.0, 1.0);
65 }
66 double coef(double x, double y, double z) const {
67 const double bz = std::fmod(0.2 * gs[2] + 3.0 * step, (double)gs[2]);
68 const double w = 0.12 * gs[0], dx = x - 0.3 * gs[0], dy = y - 0.35 * gs[1], dz = z - bz;
69 return 1.0 / (1.0 + 9.0 * std::exp(-(dx * dx + dy * dy + dz * dz) / (w * w)));
70 }
71 double face(int a, double x, double y, double z) const { // cell-centre coordinates
72 if (a == 0)
73 x -= 0.5;
74 if (a == 1)
75 y -= 0.5;
76 if (a == 2)
77 z -= 0.5;
78 return open(x, y, z) * coef(x, y, z);
79 }
80};
81
82// A cell with an open face (the operator's row is not empty).
83static bool fluidCell(const Scene& sc, double cx, double cy, double cz) {
84 return sc.open(cx - 0.5, cy, cz) + sc.open(cx + 0.5, cy, cz) + sc.open(cx, cy - 0.5, cz) +
85 sc.open(cx, cy + 0.5, cz) + sc.open(cx, cy, cz - 0.5) + sc.open(cx, cy, cz + 0.5) >
86 0.0;
87}
88
89// The rhs is zero in solid cells (all six faces closed) and has zero sum over the fluid cells,
90// so the singular walled / cut-cell problem is compatible (one fluid component).
91static void fillScene(CCField ox, CCField oy, CCField oz, CCField b, C3 ext, C3 og,
92 const Scene& sc) {
93 auto hx = Kokkos::create_mirror_view(ox), hy = Kokkos::create_mirror_view(oy),
94 hz = Kokkos::create_mirror_view(oz), hb = Kokkos::create_mirror_view(b);
95 const IVec<3> gs = sc.gs;
96 double loc[2] = {0.0, 0.0}, glb[2];
97 for (int pass = 0; pass < 2; ++pass) {
98 for (int z = 0; z < ext.z; ++z)
99 for (int y = 0; y < ext.y; ++y)
100 for (int x = 0; x < ext.x; ++x) {
101 const long i = (long)x + (long)y * ext.x + (long)z * (long)ext.x * ext.y;
102 // periodic x, z (wrapped ghosts); walls on y (the boundary fill closes the wall faces)
103 const int gx = wrap(x - G + og.x, (int)gs[0]), gy = y - G + og.y,
104 gz = wrap(z - G + og.z, (int)gs[2]);
105 const double cx = gx + 0.5, cy = gy + 0.5, cz = gz + 0.5;
106 const bool inner =
107 x >= G && x < ext.x - G && y >= G && y < ext.y - G && z >= G && z < ext.z - G;
108 const bool fluid = fluidCell(sc, cx, cy, cz);
109 const double f = std::sin(2.0 * M_PI * gx / gs[0]) * std::cos(M_PI * (gy + 0.5) / gs[1]) +
110 0.5 * std::sin(2.0 * M_PI * (gz + 0.3 * sc.step) / gs[2]) +
111 0.1 * (gy + 0.5) / gs[1];
112 if (pass == 0) {
113 hx(i) = sc.face(0, cx, cy, cz);
114 hy(i) = sc.face(1, cx, cy, cz);
115 hz(i) = sc.face(2, cx, cy, cz);
116 if (inner && fluid) {
117 loc[0] += f;
118 loc[1] += 1.0;
119 }
120 } else {
121 hb(i) = (inner && fluid) ? f - glb[0] / glb[1] : 0.0;
122 }
123 }
124 if (pass == 0)
125 MPI_Allreduce(loc, glb, 2, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
126 }
127 Kokkos::deep_copy(ox, hx);
128 Kokkos::deep_copy(oy, hy);
129 Kokkos::deep_copy(oz, hz);
130 Kokkos::deep_copy(b, hb);
131}
132
133struct Run {
134 std::vector<int> iters;
135 Kokkos::View<double*, Kokkos::HostSpace> x;
136 int teleCount = 0, repCount = 0, localLevels = 0;
137 C3 coarsest{}, ext{}, og{};
138};
139
140static void configure(CutcellMG& mg, int force, bool repart) {
141 const int bc[6] = {0, 0, 1, 1, 0, 0}; // walls on -y / +y (hasBC_), periodic x and z
143 mg.setAgglomerationMode(-1); // 'auto' agglomeration (set_pressure_bottom_extent's path)
144 mg.setTelescope(force >= 0 || repart); // a Repartition stage is a telescope point
145 mg.setTelescopeForceLevel(force);
146 mg.setRepartition(repart);
147}
148
149static Run solveSteps(IVec<3> gs, int nlev, int force, bool repart,
150 peclet::core::decomp::BlockDecomposer<3>* dec0, int engine) {
151 CutcellMG mg;
152 configure(mg, force, repart);
153 mg.setBottomSolver(engine);
154 mg.initMpi(gs[0], gs[1], gs[2], nlev, MPI_COMM_WORLD, dec0);
155 CutcellMG::Level& l0 = mg.level(0);
156 const C3 ext = l0.ext, og = l0.og;
157 const std::size_t n = (std::size_t)ext.x * ext.y * ext.z;
158 CCField ox("ox", n), oy("oy", n), oz("oz", n), b("b", n), x("x", n), r("r", n), p("p", n),
159 z("z", n), Ap("Ap", n);
160 Run res;
161 Scene sc{gs, 0};
162 for (int s = 0; s < STEPS; ++s) {
163 sc.step = s;
164 fillScene(ox, oy, oz, b, ext, og, sc);
165 mg.setOpenness(CCConst(ox), CCConst(oy), CCConst(oz), 1.0, 1.0, 1.0); // every step
166 Kokkos::deep_copy(x, 0.0);
167 res.iters.push_back(mg.solvePCG(b, x, r, p, z, Ap, /*maxit=*/300, /*rtol=*/1e-10, 2, 2, 8));
168 }
169 res.ext = ext;
170 res.og = og;
171 res.x = Kokkos::create_mirror_view(x);
172 Kokkos::deep_copy(res.x, x);
173 res.teleCount = mg.telescopeCount();
174 res.repCount = mg.repartitionCount();
175 res.localLevels = mg.localLevels();
176 res.coarsest = mg.coarsestGlobal();
177 return res;
178}
179
180int main(int argc, char** argv) {
181 MPI_Init(&argc, &argv);
182 Kokkos::initialize(argc, argv);
183 int rank = 0, size = 1;
184 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
185 MPI_Comm_size(MPI_COMM_WORLD, &size);
186 int fail = 0;
187 constexpr bool host = std::is_same_v<peclet::flow::CCExec::memory_space, Kokkos::HostSpace>;
188 if (!host) {
189 if (rank == 0)
190 std::printf("SKIP: the distributed 'direct' bottom is host-only (device keeps GraphAMG)\n");
191 Kokkos::finalize();
192 MPI_Finalize();
193 return 77;
194 }
195 {
196 struct Case {
197 const char* name;
198 IVec<3> gs;
199 int nlev, force;
200 bool repart, weighted;
201 };
202 const Case cases[] = {
203 {"A in place 64x48x32", {64, 48, 32}, 4, -1, false, false},
204 {"B forced telescope@1 64x48x32", {64, 48, 32}, 4, 1, false, false},
205 {"C repartition 48x32x32 (weighted, odd root)", {48, 32, 32}, 4, -1, true, true},
206 };
207 for (const Case& c : cases) {
208 peclet::core::decomp::BlockDecomposer<3>* dec0 = nullptr;
209 std::vector<double> w;
210 if (c.weighted) { // test_telescope_mpi case D: a slab x < 3 weighs 20x -> odd root split
211 w.resize((std::size_t)c.gs[0] * c.gs[1] * c.gs[2]);
212 for (Index zz = 0; zz < c.gs[2]; ++zz)
213 for (Index yy = 0; yy < c.gs[1]; ++yy)
214 for (Index xx = 0; xx < c.gs[0]; ++xx)
215 w[(std::size_t)(xx + yy * c.gs[0] + zz * c.gs[0] * c.gs[1])] = xx < 3 ? 20.0 : 1.0;
216 dec0 = new peclet::core::decomp::BlockDecomposer<3>((std::size_t)size, c.gs, w);
217 }
218 Run rd, ra;
219 std::string err;
220 try {
221 rd = solveSteps(c.gs, c.nlev, c.force, c.repart, dec0, CutcellMG::kBottomDirect);
222 ra = solveSteps(c.gs, c.nlev, c.force, c.repart, dec0, CutcellMG::kBottomAlgebraic);
223 } catch (const std::exception& e) {
224 err = e.what();
225 }
226 delete dec0;
227 int bad = err.empty() ? 0 : 1, anyBad = 0;
228 MPI_Allreduce(&bad, &anyBad, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
229 if (anyBad) {
230 if (rank == 0)
231 std::printf(" %-44s np=%d FAIL: %s\n", c.name, size,
232 err.empty() ? "raised on another rank" : err.c_str());
233 ++fail;
234 continue;
235 }
236 // the bottom's placement: ranks holding every level hold the bottom
237 int maxLv = 0, nBottom = 0;
238 MPI_Allreduce(&rd.localLevels, &maxLv, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
239 int mine = rd.localLevels == maxLv ? 1 : 0;
240 MPI_Allreduce(&mine, &nBottom, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
241 int stages[2] = {rd.teleCount, rd.repCount};
242 MPI_Bcast(stages, 2, MPI_INT, 0, MPI_COMM_WORLD); // rank 0 always holds every level
243 int cg[3] = {rd.coarsest.x, rd.coarsest.y, rd.coarsest.z};
244 MPI_Bcast(cg, 3, MPI_INT, 0, MPI_COMM_WORLD);
245 // over this rank's FLUID inner cells: a solid cell's row is empty, so its x is whatever the
246 // preconditioner left there (the two bottoms treat a solid bottom cell differently) and is no
247 // part of the solution; ghosts are not either.
248 const C3 e = rd.ext, o = rd.og;
249 const Scene sc{c.gs, STEPS - 1};
250 double loc[2] = {0.0, 0.0}, glb[2];
251 for (int zz = G; zz < e.z - G; ++zz)
252 for (int yy = G; yy < e.y - G; ++yy)
253 for (int xx = G; xx < e.x - G; ++xx) {
254 if (!fluidCell(sc, xx - G + o.x + 0.5, yy - G + o.y + 0.5, zz - G + o.z + 0.5))
255 continue;
256 const long i = (long)xx + (long)yy * e.x + (long)zz * (long)e.x * e.y;
257 loc[0] = std::max(loc[0], std::fabs(rd.x(i) - ra.x(i)));
258 loc[1] = std::max(loc[1], std::fabs(ra.x(i)));
259 }
260 MPI_Allreduce(loc, glb, 2, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
261 const double rel = glb[0] / (glb[1] + 1e-300);
262 const bool sameIt = rd.iters == ra.iters;
263 bool placed = true;
264 if (c.force >= 0)
265 placed = stages[0] >= 1 && nBottom < size;
266 if (c.repart)
267 placed = stages[1] >= 1; // the bottom's blocks are a proportional ORB's, not dec0's
268 const bool ok = sameIt && rel <= 1e-12 && placed;
269 if (!ok)
270 ++fail;
271 if (rank == 0) {
272 std::printf(
273 " %-44s np=%d bottom %dx%dx%d on %d/%d rank(s), %d telescope / %d repartition "
274 "stage(s); iters direct/algebraic:",
275 c.name, size, cg[0], cg[1], cg[2], nBottom, size, stages[0], stages[1]);
276 for (std::size_t s = 0; s < rd.iters.size(); ++s)
277 std::printf(" %d/%d", rd.iters[s], ra.iters[s]);
278 std::printf(" rel %.3e %s%s\n", rel, ok ? "ok" : "FAIL",
279 placed ? "" : " (the bottom is not below a telescope / repartition point)");
280 }
281 }
282
283 // H: an ineligible explicit 'direct' under a telescope raises on every rank (no hang).
284 {
285 const IVec<3> gs{64, 64, 64}; // 3 levels -> a 16^3 bottom: planes of 256 > 192 cells
286 std::string err;
287 int lvls = 0;
288 try {
289 Run r = solveSteps(gs, 3, 1, false, nullptr, CutcellMG::kBottomDirect);
290 lvls = r.localLevels;
291 } catch (const std::runtime_error& e) {
292 err = e.what();
293 }
294 int raised = (err.find("not eligible here") != std::string::npos &&
295 err.find("192 cells") != std::string::npos)
296 ? 1
297 : 0,
298 nRaised = 0;
299 MPI_Allreduce(&raised, &nRaised, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
300 const bool ok = nRaised == size;
301 if (!ok)
302 ++fail;
303 if (rank == 0)
304 std::printf(" %-44s np=%d raised on %d/%d ranks%s %s\n",
305 "H ineligible 'direct' under a telescope", size, nRaised, size,
306 err.empty() ? "" : (" (\"" + err + "\")").c_str(), ok ? "ok" : "FAIL");
307 (void)lvls;
308 }
309 }
310 int totalFail = 0;
311 MPI_Allreduce(&fail, &totalFail, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
312 if (rank == 0) {
313 if (totalFail == 0)
314 std::printf("OK (np=%d): distributed 'direct' bottom == 'algebraic' (iterations, 1e-12)\n",
315 size);
316 else
317 std::fprintf(stderr, "FAILED (np=%d)\n", size);
318 }
319 Kokkos::finalize();
320 MPI_Finalize();
321 return totalFail == 0 ? 0 : 1;
322}
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 setRepartition(bool on)
Repartition stages for a weighted level-0 decomposition (see repartition_); before initMpi.
void setAgglomerationMode(int mode)
flow — portable (Kokkos) geometric multigrid for the cut-cell (variable-openness) pressure Poisson.
Kokkos::View< const double *, CCMem > CCConst
Kokkos::View< double *, CCMem > CCField
Kokkos::View< double *, Kokkos::HostSpace > x
std::vector< int > iters
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 Run solveSteps(IVec< 3 > gs, int nlev, int force, bool repart, peclet::core::decomp::BlockDecomposer< 3 > *dec0, int engine)
static int wrap(int a, int n)
static void configure(CutcellMG &mg, int force, bool repart)
static void fillScene(CCField ox, CCField oy, CCField oz, CCField b, C3 ext, C3 og, const Scene &sc)
static constexpr int G
static bool fluidCell(const Scene &sc, double cx, double cy, double cz)
static void cg(CCField x, CCConst b, OpV AC, OpV AW, OpV AE, OpV AS, OpV AN, OpV AB, OpV AT, C3 e, Exch &&exchange, const Reduce &red, long gCells, int iters)
static constexpr double R