flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_mg_depth_policy_mpi.cpp
Go to the documentation of this file.
1// doc/mg_depth_policy.md WO-1: the AUTO pressure-MG depth under MPI (CutcellMG::initMpi with
2// nLevels == 0), hierarchy only (no operator, no solve).
3// T1 telescoping ON (the default): the resolved depth D and every level's GLOBAL dims equal the
4// single-rank init's (rank- and backend-independence of the hierarchy, §2), for 192^3,
5// 256x192x128 and 2048x64x64; predict(levels=0, telescope=true) equals the built plan.
6// T2 predict(levels=0, np=1) for the two §4.4 rows too large to build in a test (512x512x256,
7// 1024x1024x512) and for the T1 grids against the single-rank init.
8// T3 telescoping OFF: D is where in-place coarsening stops, so a blocked case resolves to fewer
9// levels than single rank (40^3 on the coarse-first level-0 partition with decomposition
10// levels 2 at np 8: blocks odd below the 10^3 level, D 3 < 4 -- the aligned default ORB
11// nests for the full depth of every grid searched, so it cannot show it); at every np D <=
12// single-rank D; predict(levels=0, telescope=false) equals the built plan.
13#include <mpi.h>
14
15#include <cstdio>
16#include <Kokkos_Core.hpp>
17#include <vector>
18
19#include "mac_cutcell_mg.hpp"
20
21using namespace peclet::flow;
22
23namespace {
24
25int g_fail = 0;
26int g_rank = 0;
27
28void expect(bool ok, const char* fmt, int a = 0, int b = 0, int c = 0, int d = 0) {
29 if (!ok)
30 ++g_fail;
31 if (g_rank == 0) {
32 std::printf("%s ", ok ? "PASS" : "FAIL");
33 std::printf(fmt, a, b, c, d);
34 std::printf("\n");
35 std::fflush(stdout);
36 }
37}
38
39bool same(C3 a, C3 b) {
40 return a.x == b.x && a.y == b.y && a.z == b.z;
41}
42
43// Single-rank reference: the per-level global dims of CutcellMG::init(G, 0), mode auto.
44std::vector<C3> singleRankDims(C3 G) {
45 std::vector<C3> d;
46 if (g_rank == 0) {
48 ref.setAgglomerationMode(-1);
49 ref.init(G.x, G.y, G.z, 0);
50 C3 g = G;
51 for (const C3& r : ref.levelRatios()) {
52 d.push_back(g);
53 g = C3{g.x / r.x, g.y / r.y, g.z / r.z};
54 }
55 }
56 int n = (int)d.size();
57 MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
58 d.resize(n);
59 MPI_Bcast(d.data(), 3 * n, MPI_INT, 0, MPI_COMM_WORLD);
60 return d;
61}
62
63// The built plan's global dims on rank 0 (the root of every stage holds every level), broadcast.
64std::vector<CutcellMG::PlanRow> builtOnRoot(const CutcellMG& mg) {
65 std::vector<CutcellMG::PlanRow> p = mg.builtPlan();
66 int n = (int)p.size();
67 MPI_Bcast(&n, 1, MPI_INT, 0, MPI_COMM_WORLD);
68 p.resize(n);
69 std::vector<int> buf(5 * n);
70 if (g_rank == 0)
71 for (int L = 0; L < n; ++L) {
72 buf[5 * L + 0] = p[L].global.x;
73 buf[5 * L + 1] = p[L].global.y;
74 buf[5 * L + 2] = p[L].global.z;
75 buf[5 * L + 3] = p[L].ranks;
76 buf[5 * L + 4] = p[L].tele ? 1 : 0;
77 }
78 MPI_Bcast(buf.data(), 5 * n, MPI_INT, 0, MPI_COMM_WORLD);
79 for (int L = 0; L < n; ++L) {
80 p[L].global = C3{buf[5 * L + 0], buf[5 * L + 1], buf[5 * L + 2]};
81 p[L].ranks = buf[5 * L + 3];
82 p[L].tele = buf[5 * L + 4] != 0;
83 }
84 return p;
85}
86
87bool planEqual(const std::vector<CutcellMG::PlanRow>& a, const std::vector<CutcellMG::PlanRow>& b) {
88 if (a.size() != b.size())
89 return false;
90 for (std::size_t L = 0; L < a.size(); ++L)
91 if (!same(a[L].global, b[L].global) || a[L].ranks != b[L].ranks || a[L].tele != b[L].tele)
92 return false;
93 return true;
94}
95
96void printPlan(const char* tag, const std::vector<CutcellMG::PlanRow>& p) {
97 if (g_rank != 0)
98 return;
99 std::printf(" %s:", tag);
100 for (const auto& r : p)
101 std::printf(" %dx%dx%d[%d%s]", r.global.x, r.global.y, r.global.z, r.ranks, r.tele ? "T" : "");
102 std::printf("\n");
103}
104
105} // namespace
106
107int main(int argc, char** argv) {
108 MPI_Init(&argc, &argv);
109 Kokkos::initialize(argc, argv);
110 int size = 1;
111 MPI_Comm_rank(MPI_COMM_WORLD, &g_rank);
112 MPI_Comm_size(MPI_COMM_WORLD, &size);
113 {
114 // T1 + T2 (np = 1 part): telescoping ON.
115 const C3 grids[] = {{192, 192, 192}, {256, 192, 128}, {2048, 64, 64}};
116 for (const C3& G : grids) {
117 const std::vector<C3> ref = singleRankDims(G);
118 CutcellMG mg;
120 mg.setTelescope(true);
121 mg.initMpi(G.x, G.y, G.z, 0, MPI_COMM_WORLD);
122 const auto built = builtOnRoot(mg);
123 printPlan("built", built);
124 bool dimsOk = built.size() == ref.size();
125 for (std::size_t L = 0; dimsOk && L < ref.size(); ++L)
126 dimsOk = same(built[L].global, ref[L]);
127 expect(dimsOk, "T1 %dx%dx%d telescope ON: D %d == single-rank D, every level's global dims",
128 G.x, G.y, G.z, (int)built.size());
129 const auto pred = CutcellMG::predict(G.x, G.y, G.z, size, 0, true);
130 printPlan("predict", pred);
131 expect(planEqual(pred, built), "T1 %dx%dx%d predict(levels=0, telescope) == built (D %d)",
132 G.x, G.y, G.z, (int)pred.size());
133 const auto p1 = CutcellMG::predict(G.x, G.y, G.z, 1, 0, true);
134 bool p1Ok = p1.size() == ref.size();
135 for (std::size_t L = 0; p1Ok && L < ref.size(); ++L)
136 p1Ok = same(p1[L].global, ref[L]);
137 expect(p1Ok, "T2 %dx%dx%d predict(levels=0, np=1) == single-rank init", G.x, G.y, G.z);
138 }
139 // T2: the rows too large to build here (§4.4): D and the bottom from predict alone.
140 struct Big {
141 C3 G;
142 int D;
143 C3 bottom;
144 };
145 const Big bigs[] = {{{512, 512, 256}, 6, {16, 16, 8}}, {{1024, 1024, 512}, 7, {16, 16, 8}}};
146 for (const Big& b : bigs) {
147 const auto p = CutcellMG::predict(b.G.x, b.G.y, b.G.z, 1, 0, true);
148 expect((int)p.size() == b.D && same(p.back().global, b.bottom),
149 "T2 %dx%dx%d predict(levels=0): D %d, bottom 16x16x8", b.G.x, b.G.y, b.G.z,
150 (int)p.size());
151 // The same global per-level dims at the rank counts these grids are run at (np 4: 2x2 at
152 // 256^3/rank; np 32), telescoping ON: the depth does not depend on np.
153 for (const int np : {4, 32}) {
154 const auto q = CutcellMG::predict(b.G.x, b.G.y, b.G.z, np, 0, true);
155 bool qOk = q.size() == p.size();
156 for (std::size_t L = 0; qOk && L < p.size(); ++L)
157 qOk = same(q[L].global, p[L].global);
158 expect(qOk, "T2 %dx%dx%d predict(levels=0, np=%d): every level's global dims == np 1's",
159 b.G.x, b.G.y, b.G.z, np);
160 }
161 }
162 // T3: telescoping OFF.
163 {
164 const C3 G{40, 40, 40};
165 const std::vector<C3> ref = singleRankDims(G); // 40 20 10 5: D 4
166 CutcellMG mg;
168 mg.setTelescope(false);
169 const auto dec0 = CutcellMG::decomposition((std::size_t)size, G.x, G.y, G.z, 2);
170 mg.initMpi(G.x, G.y, G.z, 0, MPI_COMM_WORLD, &dec0);
171 const auto built = builtOnRoot(mg);
172 printPlan("built (telescope OFF)", built);
173 expect(built.size() <= ref.size(), "T3 40^3 telescope OFF np %d: D %d <= single-rank D %d",
174 size, (int)built.size(), (int)ref.size());
175 if (size == 8)
176 expect(built.size() < ref.size(), "T3 40^3 telescope OFF np 8: blocked, D %d < %d",
177 (int)built.size(), (int)ref.size());
178 const auto pred = CutcellMG::predict(G.x, G.y, G.z, size, 0, false, 4, 2);
179 expect(planEqual(pred, built), "T3 40^3 predict(levels=0, telescope=false) == built (D %d)",
180 (int)pred.size());
181 }
182 }
183 Kokkos::finalize();
184 if (g_rank == 0)
185 std::printf("mg_depth_policy_mpi np %d: %s (%d failures)\n", size, g_fail ? "FAIL" : "PASS",
186 g_fail);
187 int f = g_fail, all = 0;
188 MPI_Allreduce(&f, &all, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
189 MPI_Finalize();
190 return all ? 1 : 0;
191}
void setAgglomerationMode(int mode)
flow — portable (Kokkos) geometric multigrid for the cut-cell (variable-openness) pressure Poisson.
static constexpr int G
static std::string fmt(const char *f, double a, long b=0)
static bool same(double a, double b)