flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_ca_odd_periodic_mpi.cpp
Go to the documentation of this file.
1// Communication-avoiding (CA) red-black pairs across a periodic axis of ODD global extent.
2//
3// Both CA pairs (CutcellMG::smooth on the g = 2 coarse levels, IbmSolver's momentum sweep) sweep
4// the 1-deep ghost ring in colour 0 with the ring slot's OWN parity. Across a periodic wrap of odd
5// global extent the slot (z = -1, the image of z = nz - 1) has the opposite parity of its image, so
6// the ring would be swept in the wrong colour; CA is therefore off on such a level / velocity
7// decomposition and the per-colour exchange runs instead (mac_cutcell_mg.hpp, flow_ibm_mpi.hpp).
8// Grid 32 x 32 x 25, periodic on every axis, so z is odd at every MG level.
9// (a) CutcellMG MG-PCG, a fixed 6 iterations, CA on and off: np 1 memcmp == single rank; at any
10// np CA on memcmp == CA off; np > 1 within test_cutcellmg_mpi's 1e-8 of single rank.
11// (b) IbmSolver Stokes through a sphere (cut-cell IBM, the CA momentum path), 4 steps, CA
12// both / momentum / pressure / off: np 1 memcmp == single rank (u, v, w, p); at any np each
13// arm memcmp == off; np > 1 velocity within test_sdflow_mpi's relative 2e-5 of single rank.
14// Before the fix np 1 differed from single rank in (a) (CA on) and in (b) (both, momentum).
15#include <mpi.h>
16
17#include <algorithm>
18#include <cmath>
19#include <cstdio>
20#include <cstring>
21#include <Kokkos_Core.hpp>
22#include <string>
23#include <type_traits>
24#include <vector>
25
26#include "flow_ibm.hpp"
27#include "mac_cutcell_mg.hpp"
28#include "peclet/core/common/types.hpp"
29#include "peclet/core/decomp/block_decomposer.hpp"
30
31using peclet::core::IVec;
37
38static constexpr int NX = 32, NY = 32, NZ = 25;
39static int gRank = 0, gSize = 1, gFail = 0;
40
41static void check(bool ok, const std::string& what) {
42 int all = 0, v = ok ? 1 : 0;
43 MPI_Allreduce(&v, &all, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD);
44 if (gRank == 0)
45 std::printf(" %-78s %s\n", what.c_str(), all ? "ok" : "FAIL");
46 if (!all)
47 ++gFail;
48}
49
50// This rank's inner-block values (x-fastest) against a global (single-rank) array: max|a - ref|
51// and the memcmp-unequal count, both reduced over the ranks.
52struct Diff {
53 double maxAbs = 0.0, refMax = 0.0;
54 long bits = 0;
55};
56static Diff compareBlock(const std::vector<double>& a, const std::vector<double>& ref, int ox,
57 int oy, int oz, int lnx, int lny, int lnz) {
58 Diff d;
59 for (int z = 0; z < lnz; ++z)
60 for (int y = 0; y < lny; ++y)
61 for (int x = 0; x < lnx; ++x) {
62 const double u = a[(std::size_t)x + (std::size_t)y * lnx + (std::size_t)z * lnx * lny];
63 const double r = ref[(std::size_t)(x + ox) + (std::size_t)(y + oy) * NX +
64 (std::size_t)(z + oz) * NX * NY];
65 d.maxAbs = std::max(d.maxAbs, std::fabs(u - r));
66 d.refMax = std::max(d.refMax, std::fabs(r));
67 if (std::memcmp(&u, &r, sizeof(double)) != 0)
68 ++d.bits;
69 }
70 MPI_Allreduce(MPI_IN_PLACE, &d.maxAbs, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
71 MPI_Allreduce(MPI_IN_PLACE, &d.refMax, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
72 MPI_Allreduce(MPI_IN_PLACE, &d.bits, 1, MPI_LONG, MPI_SUM, MPI_COMM_WORLD);
73 return d;
74}
75
76static bool sameBits(const std::vector<double>& a, const std::vector<double>& b) {
77 int eq = a.size() == b.size() && std::memcmp(a.data(), b.data(), a.size() * sizeof(double)) == 0;
78 MPI_Allreduce(MPI_IN_PLACE, &eq, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD);
79 return eq != 0;
80}
81
82static std::string fmt(const char* f, double a, long b = 0) {
83 char buf[96];
84 std::snprintf(buf, sizeof buf, f, a, b);
85 return buf;
86}
87
88// ---------------------------------------------------------------------------------------------
89// (a) CutcellMG: periodic all-fluid Poisson, mean-free source, 6 MG-PCG iterations, 4 levels.
90static double source(int gx, int gy, int gz) {
91 return std::sin(2.0 * M_PI * gx / NX) +
92 std::cos(4.0 * M_PI * gy / NY) * std::sin(2.0 * M_PI * gz / NZ);
93}
94
95// Solve on `mg` (level 0 of ghost width g); returns the inner block, x-fastest.
96static std::vector<double> mgSolve(CutcellMG& mg) {
97 const CutcellMG::Level& l0 = mg.level(0);
98 const C3 e = l0.ext, og = l0.og;
99 const int g = l0.g;
100 const std::size_t n = (std::size_t)e.x * e.y * e.z;
101 CCField ox("ox", n), oy("oy", n), oz("oz", n);
102 Kokkos::deep_copy(ox, 1.0);
103 Kokkos::deep_copy(oy, 1.0);
104 Kokkos::deep_copy(oz, 1.0);
105 mg.setOpenness(CCConst(ox), CCConst(oy), CCConst(oz), 1.0, 1.0, 1.0);
106 CCField b("b", n), x("x", n), r("r", n), p("p", n), z("z", n), Ap("Ap", n);
107 auto hb = Kokkos::create_mirror_view(b);
108 for (std::size_t c = 0; c < n; ++c)
109 hb(c) = 0.0;
110 for (int k = g; k < e.z - g; ++k)
111 for (int j = g; j < e.y - g; ++j)
112 for (int i = g; i < e.x - g; ++i)
113 hb((long)i + (long)j * e.x + (long)k * e.x * e.y) =
114 source(i - g + og.x, j - g + og.y, k - g + og.z);
115 Kokkos::deep_copy(b, hb);
116 mg.solvePCG(b, x, r, p, z, Ap, /*maxit=*/6, /*rtol=*/1e-30, 2, 2, 8);
117 auto hx = Kokkos::create_mirror_view(x);
118 Kokkos::deep_copy(hx, x);
119 std::vector<double> out;
120 for (int k = g; k < e.z - g; ++k)
121 for (int j = g; j < e.y - g; ++j)
122 for (int i = g; i < e.x - g; ++i)
123 out.push_back(hx((long)i + (long)j * e.x + (long)k * e.x * e.y));
124 return out;
125}
126
127static void mgChecks() {
128 constexpr int NLEV = 4;
129 CutcellMG ref;
130 ref.init(NX, NY, NZ, NLEV);
131 const std::vector<double> xs = mgSolve(ref);
132 std::vector<double> xd[2];
133 int lnx = 0, lny = 0, lnz = 0, ox = 0, oy = 0, oz = 0;
134 const int modes[2] = {peclet::flow::kCaMg, 0};
135 for (int k = 0; k < 2; ++k) {
136 CutcellMG mg;
137 mg.setCommAvoiding(modes[k]);
138 mg.initMpi(NX, NY, NZ, NLEV, MPI_COMM_WORLD);
139 if (k == 0) {
140 int caLevels = 0;
141 for (int L = 0; L < mg.nLevels(); ++L)
142 caLevels += mg.level(L).caOk ? 1 : 0;
143 check(caLevels == 0, fmt("(a) no CA level on the odd periodic grid (%g of them)", caLevels));
144 }
145 xd[k] = mgSolve(mg);
146 const CutcellMG::Level& l0 = mg.level(0);
147 lnx = l0.inner.x;
148 lny = l0.inner.y;
149 lnz = l0.inner.z;
150 ox = l0.og.x; // the inner origin, global
151 oy = l0.og.y; // the inner origin, global
152 oz = l0.og.z; // the inner origin, global
153 }
154 check(sameBits(xd[0], xd[1]), "(a) MG-PCG x: CA on memcmp == CA off");
155 for (int k = 0; k < 2; ++k) {
156 const Diff d = compareBlock(xd[k], xs, ox, oy, oz, lnx, lny, lnz);
157 const std::string t = std::string("(a) MG-PCG x, CA ") + (k == 0 ? "on" : "off");
158 if (gSize == 1)
159 check(d.bits == 0, t + fmt(": np 1 memcmp == single rank (max|d| %.3e, %ld cells differ)",
160 d.maxAbs, d.bits));
161 else
162 check(d.maxAbs <= 1e-8, t + fmt(": max|d| vs single rank %.3e <= 1e-8", d.maxAbs));
163 }
164}
165
166// ---------------------------------------------------------------------------------------------
167// (b) IbmSolver: Stokes through one periodic sphere, cut-cell pressure, 4 steps.
168static std::vector<double> sphereSdf() {
169 std::vector<double> sdf((std::size_t)NX * NY * NZ);
170 const double R = 0.3 * NZ, c[3] = {0.5 * NX, 0.5 * NY, 0.5 * NZ};
171 const int n[3] = {NX, NY, NZ};
172 for (int z = 0; z < NZ; ++z)
173 for (int y = 0; y < NY; ++y)
174 for (int x = 0; x < NX; ++x) {
175 double d2 = 0.0;
176 const int p[3] = {x, y, z};
177 for (int a = 0; a < 3; ++a) {
178 const double d = p[a] - c[a];
179 const double w = d - n[a] * std::round(d / n[a]);
180 d2 += w * w;
181 }
182 sdf[(std::size_t)x + (std::size_t)y * NX + (std::size_t)z * NX * NY] = std::sqrt(d2) - R;
183 }
184 return sdf;
185}
186
187static void configure(IbmSolver& s) {
188 s.setRho(1.0);
189 s.setMu(0.1);
190 s.setDt(5.0);
191 s.setBodyForce(1e-3, 2e-4, 0);
192 s.setAdvection(false);
193 s.setVelocityIterations(40);
194 s.setPressureLevels(4);
195 s.setPressurePcg(true, 200, 1e-9);
196}
197
198struct Fields {
199 std::vector<double> f[4]; // u, v, w, p (inner block, x-fastest)
200};
201static Fields fieldsOf(IbmSolver& s) {
202 Fields r;
203 for (int c = 0; c < 3; ++c)
204 r.f[c] = s.getVelocity(c);
205 r.f[3] = s.getPressure();
206 return r;
207}
208
209static void solverChecks() {
210 constexpr int STEPS = 4;
211 const std::vector<double> gsdf = sphereSdf();
212 IbmSolver ref(NX, NY, NZ);
213 configure(ref);
214 // The distributed solve runs the 'direct' bottom on a host backend and GraphAMG on a device,
215 // where the single-rank solver would take the device 'direct' bottom (8.3e-10 in p at np 1);
216 // the np = 1 bit-exact gate pins the reference to the engine the distributed path runs.
217 ref.setPressureBottomSolver(
218 std::is_same_v<peclet::flow::CCExec::memory_space, Kokkos::HostSpace> ? 0 : 2);
219 ref.setSolid(gsdf, /*cutcell_pressure=*/true);
220 for (int it = 0; it < STEPS; ++it)
221 ref.step();
222 const Fields fs = fieldsOf(ref);
223
224 const peclet::core::decomp::BlockDecomposer<3> dec =
225 CutcellMG::decomposition(static_cast<std::size_t>(gSize), NX, NY, NZ);
226 const auto blk = dec.block(gRank);
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 std::vector<double> lsdf((std::size_t)lnx * lny * lnz);
230 for (int z = 0; z < lnz; ++z)
231 for (int y = 0; y < lny; ++y)
232 for (int x = 0; x < lnx; ++x)
233 lsdf[(std::size_t)x + (std::size_t)y * lnx + (std::size_t)z * lnx * lny] =
234 gsdf[(std::size_t)(x + ox) + (std::size_t)(y + oy) * NX +
235 (std::size_t)(z + oz) * NX * NY];
236
238 const char* names[4] = {"both", "momentum", "pressure", "off"};
239 Fields fd[4];
240 for (int k = 0; k < 4; ++k) {
241 IbmSolver s(lnx, lny, lnz);
242 s.setCommAvoiding(modes[k]);
243 s.initMpi(NX, NY, NZ, MPI_COMM_WORLD);
244 configure(s);
245 s.setSolid(lsdf, true);
246 for (int it = 0; it < STEPS; ++it)
247 s.step();
248 fd[k] = fieldsOf(s);
249 }
250 for (int k = 0; k < 4; ++k) {
251 const std::string t = std::string("(b) CA ") + names[k];
252 if (k < 3) {
253 bool eq = true;
254 for (int c = 0; c < 4; ++c)
255 eq = sameBits(fd[k].f[c], fd[3].f[c]) && eq;
256 check(eq, t + ": u, v, w, p memcmp == CA off");
257 }
258 if (gSize == 1) {
259 long bits = 0;
260 double mx = 0.0;
261 for (int c = 0; c < 4; ++c) {
262 const Diff d = compareBlock(fd[k].f[c], fs.f[c], ox, oy, oz, lnx, lny, lnz);
263 bits += d.bits;
264 mx = std::max(mx, d.maxAbs);
265 }
266 check(bits == 0,
267 t + fmt(": np 1 u, v, w, p memcmp == single rank (max|d| %.3e, %ld differ)", mx, bits));
268 } else {
269 double rel = 0.0;
270 for (int c = 0; c < 3; ++c) {
271 const Diff d = compareBlock(fd[k].f[c], fs.f[c], ox, oy, oz, lnx, lny, lnz);
272 rel = std::max(rel, d.maxAbs / (d.refMax + 1e-300));
273 }
274 if (gRank == 0)
275 std::printf(" %s: max over u, v, w of max|d| / max|ref| = %.3e\n", names[k], rel);
276 check(rel <= 2e-5, t + fmt(": velocity vs single rank, relative %.3e <= 2e-5", rel));
277 }
278 }
279}
280
281int main(int argc, char** argv) {
282 MPI_Init(&argc, &argv);
283 Kokkos::initialize(argc, argv);
284 {
285 MPI_Comm_rank(MPI_COMM_WORLD, &gRank);
286 MPI_Comm_size(MPI_COMM_WORLD, &gSize);
287 if (gRank == 0)
288 std::printf("CA red-black pairs on a %dx%dx%d periodic grid (odd z), np %d\n", NX, NY, NZ,
289 gSize);
290 mgChecks();
291 solverChecks();
292 }
293 if (gRank == 0) {
294 if (gFail == 0)
295 std::printf("OK (np=%d): odd periodic extent -- CA off, np 1 == single rank\n", gSize);
296 else
297 std::fprintf(stderr, "FAILED (np=%d): %d checks\n", gSize, gFail);
298 }
299 Kokkos::finalize();
300 MPI_Finalize();
301 return gFail == 0 ? 0 : 1;
302}
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 setCommAvoiding(int mask)
Communication-avoiding smoothing mask (kCaMomentum | kCaMg); only kCaMg is read here.
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
flow — portable (Kokkos) geometric multigrid for the cut-cell (variable-openness) pressure Poisson.
Kokkos::View< const double *, CCMem > CCConst
Kokkos::View< double *, CCMem > CCField
Solver< Staggered > IbmSolver
static constexpr int NY
static constexpr int NX
static constexpr int NZ
std::vector< double > f[4]
static constexpr int STEPS
static int gFail
static Diff compareBlock(const std::vector< double > &a, const std::vector< double > &ref, int ox, int oy, int oz, int lnx, int lny, int lnz)
static std::vector< double > mgSolve(CutcellMG &mg)
static int gRank
static Fields fieldsOf(IbmSolver &s)
static void check(bool ok, const std::string &what)
static void solverChecks()
static int gSize
static bool sameBits(const std::vector< double > &a, const std::vector< double > &b)
static std::vector< double > sphereSdf()
static double source(int gx, int gy, int gz)
static std::string fmt(const char *f, double a, long b=0)
static void configure(IbmSolver &s)
static void mgChecks()
static constexpr int NLEV
static constexpr double R