flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_mg_vcycle_symmetry_mpi.cpp
Go to the documentation of this file.
1
19#include <mpi.h>
20
21#include <cmath>
22#include <cstdio>
23#include <cstring>
24#include <Kokkos_Core.hpp>
25#include <string>
26#include <vector>
27
28#include "mac_cutcell_mg.hpp"
29#include "mg_fp32_problems.hpp"
30
31using namespace peclet::flow;
32using namespace peclet_flow_fp32_problems;
34
35namespace {
36
37int rank = 0, size = 1, fails = 0;
38void check(bool ok, const std::string& what) {
39 int bad = ok ? 0 : 1, any = 0;
40 MPI_Allreduce(&bad, &any, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
41 if (rank == 0)
42 std::printf(" %-96s %s\n", what.c_str(), any ? "FAIL" : "PASS");
43 std::fflush(stdout);
44 if (any)
45 ++fails;
46}
47std::string num(double v) {
48 char b[32];
49 std::snprintf(b, sizeof b, "%.3e", v);
50 return b;
51}
52
53constexpr double kBoundSmoothed = 1e-14, kBoundAgglomerated = 1e-8;
54
55double val(long key) { // as ctest mg_vcycle_symmetry
56 unsigned long s = (unsigned long)key * 6364136223846793005ul + 1442695040888963407ul;
57 s ^= s >> 33;
58 s *= 0xff51afd7ed558ccdul;
59 s ^= s >> 33;
60 return (double)(s >> 11) / (double)(1ul << 52) - 1.0;
61}
62
63// test_mg_fp32_vcycle_mpi.cpp's setUpDist: this rank's block of the global openness.
64void setUpDist(CutcellMG& mg, const Problem& P) {
65 mg.setBoundaryConditions(P.bc); // before initMpi (the per-level ghost width reads it)
66 mg.initMpi(P.nx, P.ny, P.nz, P.levels, MPI_COMM_WORLD);
68 const Level& l0 = mg.level(0);
69 const C3 e = l0.ext, og = l0.og;
70 const int g = l0.g;
71 const std::size_t n = (std::size_t)e.x * e.y * e.z;
72 std::vector<double> ox(n, 0.0), oy(n, 0.0), oz(n, 0.0);
73 for (int z = g; z < e.z - g; ++z)
74 for (int y = g; y < e.y - g; ++y)
75 for (int x = g; x < e.x - g; ++x) {
76 const long li = (long)x + (long)y * e.x + (long)z * (long)e.x * e.y;
77 const long gi = P.idx(x - g + og.x, y - g + og.y, z - g + og.z);
78 ox[li] = P.ox[gi];
79 oy[li] = P.oy[gi];
80 oz[li] = P.oz[gi];
81 }
82 mg.setOpenness(CCConst(toDevice(ox, "ox")), CCConst(toDevice(oy, "oy")),
83 CCConst(toDevice(oz, "oz")), 1.0, 1.0, 1.0);
84}
85// Level 0's inner cells: f(global inner x, y, z, local index).
86template <class F>
87void forInner(const Level& l0, F f) {
88 const C3 e = l0.ext;
89 const int g = l0.g;
90 for (int z = g; z < e.z - g; ++z)
91 for (int y = g; y < e.y - g; ++y)
92 for (int x = g; x < e.x - g; ++x)
93 f(x - g + l0.og.x, y - g + l0.og.y, z - g + l0.og.z,
94 (long)x + (long)y * e.x + (long)z * (long)e.x * e.y);
95}
96// The random vector of ctest mg_vcycle_symmetry on this rank's block (closed cells zero),
97// mean-free.
98CCField randomVector(CutcellMG& mg, int seed) {
99 Level& l = mg.level(0);
100 CCField f("v", l.n);
101 auto h = Kokkos::create_mirror_view(f);
102 auto ac = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), l.AC);
103 for (std::size_t i = 0; i < h.extent(0); ++i)
104 h(i) = 0.0;
105 forInner(l, [&](int x, int y, int z, long i) {
106 h(i) = (double)ac(i) > 1e-30 ? val(seed * 7919L + x + 1000L * y + 1000000L * z) : 0.0;
107 });
108 Kokkos::deep_copy(f, h);
109 mg.removeMean(l, f);
110 return f;
111}
112void setSchedule(CutcellMG& mg) { // the drivers' (2, 2, 12)
113 const std::size_t n = mg.level(0).n;
114 CCField s0("s0", n), s1("s1", n), s2("s2", n), s3("s3", n), s4("s4", n), s5("s5", n);
115 mg.solvePCG(s0, s1, s2, s3, s4, s5, 0, 1e-8, 2, 2, 12);
116}
117double asym(CutcellMG& mg) {
118 Level& l = mg.level(0);
119 setSchedule(mg);
120 CCField x = randomVector(mg, 11), y = randomVector(mg, 12), Mx("Mx", l.n), My("My", l.n);
121 mg.precondVcycle(Mx, x);
122 mg.precondVcycle(My, y);
123 const double a = mg.dot(l, x, My), b = mg.dot(l, y, Mx);
124 return std::abs(a - b) / (std::sqrt(mg.dot(l, x, x)) * std::sqrt(mg.dot(l, My, My)));
125}
126// One symmetric V-cycle z = M r, r = randomVector(seed 5), as a host array of level 0.
127std::vector<double> oneVcycle(CutcellMG& mg) {
128 Level& l = mg.level(0);
129 setSchedule(mg);
130 CCField r = randomVector(mg, 5), z("z", l.n);
131 mg.precondVcycle(z, r);
132 return toHost(z);
133}
134
135Problem thinProblem(bool walls) { // as ctest mg_vcycle_symmetry
136 const int w = walls ? 1 : 0;
137 const int bc[6] = {0, 0, 0, 0, w, w};
138 auto rho = [](double x, double y, double z) {
139 const double dx = x - 24, dy = y - 40, dz = (z - 2.0) * 3.0;
140 return dx * dx + dy * dy + dz * dz < 144.0 ? 1e2 : 1.0;
141 };
142 return makeProblem(
143 walls ? "thinW" : "slabP", 64, 64, 4, 5, bc, [](double, double, double) { return 1.0; }, rho);
144}
145Problem blobProblem(bool walls, int levels) {
146 const int w = walls ? 1 : 0;
147 const int bc[6] = {w, w, w, w, w, w};
148 auto rho = [](double x, double y, double z) {
149 const double dx = x - 20, dy = y - 36, dz = z - 28;
150 return dx * dx + dy * dy + dz * dz < 100.0 ? 1e2 : 1.0;
151 };
152 return makeProblem(
153 std::string(walls ? "iso64w" : "iso64p") + " D" + std::to_string(levels), 64, 64, 64, levels,
154 bc, [](double, double, double) { return 1.0; }, rho);
155}
156Problem scalarProblem() {
157 const int bc[6] = {1, 1, 1, 1, 0, 0};
158 auto one = [](double, double, double) { return 1.0; };
159 return makeProblem("scalar", 16, 16, 4, 0, bc, one, one);
160}
161Problem channelProblem() {
162 const int bc[6] = {2, 3, 1, 1, 1, 1};
163 auto one = [](double, double, double) { return 1.0; };
164 return makeProblem("channel", 32, 16, 16, 4, bc, one, one);
165}
166
167void symCase(const Problem& P, bool lr, bool agglom) {
168 const std::string tag = P.name + (lr ? "" : " LR off") + " np" + std::to_string(size);
169 CutcellMG mg;
170 setUpDist(mg, P);
171 mg.setLineRelaxation(lr);
172 if (agglom)
174 mg.setSymmetricVcycle(true);
175 if (rank == 0)
176 std::printf("%s: %d levels, bottom %s\n", tag.c_str(), mg.nLevels(),
177 mg.agglomerateBottom() ? "agglomerated" : "smoothed");
178 // A distributed device bottom is GraphAMG, the single-rank one the direct factor: on a device
179 // backend the agglomerated case is recorded only (G-SYM gates it on host).
180 const bool gated = !(agglom && !CutcellMG::kHostMemory);
181 if (size == 1 && gated) { // np 1 init_mpi == single rank, one V-cycle, memcmp on the inner cells
184 ref.setLineRelaxation(lr);
185 if (agglom)
186 ref.setAgglomerationMode(1);
187 ref.setSymmetricVcycle(true);
188 const std::vector<double> zd = oneVcycle(mg), zs = oneVcycle(ref);
189 const Level& l0 = mg.level(0);
190 long bits = 0;
191 forInner(l0, [&](int x, int y, int z, long i) {
192 if (std::memcmp(&zd[i], &zs[P.idx(x, y, z)], sizeof(double)) != 0)
193 ++bits;
194 });
195 check(bits == 0, tag + ": init_mpi V-cycle memcmp-equal to single rank (" +
196 std::to_string(bits) + " cells differ)");
197 }
198 const double a = asym(mg);
199 const double bound = agglom ? kBoundAgglomerated : kBoundSmoothed;
200 if (rank == 0)
201 std::printf(" a %s (bound %s)\n", num(a).c_str(), num(bound).c_str());
202 if (!gated) {
203 if (rank == 0)
204 std::printf(" %s: a %s recorded (distributed device bottom, not gated)\n", tag.c_str(),
205 num(a).c_str());
206 return;
207 }
208 check(a <= bound, tag + ": a " + num(a) + " <= " + num(bound));
209}
210
211} // namespace
212
213int main(int argc, char** argv) {
214 MPI_Init(&argc, &argv);
215 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
216 MPI_Comm_size(MPI_COMM_WORLD, &size);
217 Kokkos::initialize(argc, argv);
218 {
219 const Problem thinW = thinProblem(true), slabP = thinProblem(false);
220 symCase(thinW, true, false);
221 symCase(thinW, false, false);
222 symCase(slabP, true, false);
223 symCase(scalarProblem(), true, false);
224 symCase(blobProblem(false, 5), true, false);
225 symCase(blobProblem(true, 5), true, false);
226 symCase(cylProblem(), true, false);
227 symCase(channelProblem(), true, false);
228 symCase(blobProblem(false, 4), true, true);
229 }
230 Kokkos::finalize();
231 if (rank == 0)
232 std::printf("mg_vcycle_symmetry_mpi np%d: %s (%d failure%s)\n", size, fails ? "FAIL" : "PASS",
233 fails, fails == 1 ? "" : "s");
234 MPI_Finalize();
235 return fails ? 1 : 0;
236}
static constexpr bool kHostMemory
void removeMean(Level &lv, CCField f, bool stopGuard=false)
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 setSymmetricVcycle(bool on)
A3 (doc/mg_depth_policy.md Amendment A3, WO-S1): the symmetric pressure V-cycle – piecewise-constant ...
void setOpenness(CCConst ox, CCConst oy, CCConst oz, double idx2, double idy2, double idz2)
void setBoundaryConditions(const int bc[6])
void precondVcycle(CCField zz, CCField rr)
double dot(Level &lv, CCField a, CCField b)
void setAgglomerationMode(int mode)
void setLineRelaxation(bool on)
A2.4 (doc/mg_depth_policy.md, WO-7): zebra line relaxation along the blocked strong axis on the trigg...
void setVcyclePrecision(int mode)
flow — portable (Kokkos) geometric multigrid for the cut-cell (variable-openness) pressure Poisson.
The FP32 V-cycle test problems (column, cyl, slab, pack) and their right-hand side,...
y
Definition cmp.py:24
Kokkos::View< const double *, CCMem > CCConst
Kokkos::View< double *, CCMem > CCField
CCField toDevice(const std::vector< double > &v, const char *name)
void setUp(CutcellMG &mg, const Problem &P, int mode)
Problem makeProblem(const std::string &name, int nx, int ny, int nz, int levels, const int bc[6], Open open, Rho rho)
std::vector< typename V::non_const_value_type > toHost(const V &f)
one(n, rho_ratio, mu_ratio, stress, steps, adv, dtf, grav=False, sigma="none")
P
Definition stab5.py:16
static double val(long gi)
Definition symconv.cpp:27
static void check(bool ok, const std::string &what)
static constexpr double F
static int fails