flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
symconv.cpp
Go to the documentation of this file.
1// symconv.cpp: gate G-SYMCONV of doc/mg_depth_policy.md Amendment A3 (A3.5, WO-S2) -- the
2// production solvers (CutcellMG::solvePCG / solveFCG, schedule (2, 2, 12), rtol 1e-10, zero guess,
3// rhsOf with closed cells zero, cap 500) on the rows of A3's transfer-pair matrix, with the
4// symmetric V-cycle switch (setSymmetricVcycle) off and on, on one hierarchy. Host, single rank.
5// The problem presets are those of the matrix tool
6// (bubble_column_perf/gpu_diag/mgd/a3/symdiag.cpp). Usage: symconv <case> <levels> [LR 0|1, default
7// 1]. One line per case:
8// SYMCONV <case> D<levels> LR<lr> levels <n> bottom <dims> <smoothed|agglomerated> |
9// off PCG <it>[ capped] FCG <it>[ capped] | on PCG <it>[ capped] FCG <it>[ capped]
10// Build: build_diag.sh (gpu_diag/mgd/wo7d) against the tree's host libpeclet_flow_solver_mpi.a.
11#include <mpi.h>
12
13#include <array>
14#include <cmath>
15#include <cstdio>
16#include <Kokkos_Core.hpp>
17#include <string>
18#include <vector>
19
20#include "mac_cutcell_mg.hpp"
21#include "mg_fp32_problems.hpp"
22
23using namespace peclet::flow;
24using namespace peclet_flow_fp32_problems;
26
27static double val(long gi) {
28 unsigned long s = (unsigned long)gi * 6364136223846793005ul + 1442695040888963407ul;
29 s ^= s >> 33;
30 s *= 0xff51afd7ed558ccdul;
31 s ^= s >> 33;
32 return (double)(s >> 11) / (double)(1ul << 52) - 1.0;
33}
34
35static Problem preset(const std::string& nm, int levels) { // symdiag.cpp's presets
36 auto one = [](double, double, double) { return 1.0; };
37 if (nm == "thinW" || nm == "slabP") {
38 int bc[6] = {0, 0, 0, 0, nm == "thinW" ? 1 : 0, nm == "thinW" ? 1 : 0};
39 auto rho = [](double x, double y, double z) {
40 const double dx = x - 24, dy = y - 40, dz = (z - 2.0) * 3.0;
41 return dx * dx + dy * dy + dz * dz < 144.0 ? 1e2 : 1.0;
42 };
43 return makeProblem(nm, 64, 64, 4, levels, bc, one, rho);
44 }
45 if (nm == "iso64p" || nm == "iso64w" || nm == "iso64c4") {
46 const int w = nm == "iso64w" ? 1 : 0;
47 int bc[6] = {w, w, w, w, w, w};
48 const double ratio = nm == "iso64c4" ? 1e4 : 1e2;
49 auto rho = [ratio](double x, double y, double z) {
50 const double dx = x - 20, dy = y - 36, dz = z - 28;
51 return dx * dx + dy * dy + dz * dz < 100.0 ? ratio : 1.0;
52 };
53 return makeProblem(nm, 64, 64, 64, levels, bc, one, rho);
54 }
55 if (nm == "uni128" || nm == "uni256" || nm == "uni256w") {
56 const int N = nm == "uni128" ? 128 : 256, w = nm == "uni256w" ? 1 : 0;
57 int bc[6] = {w, w, w, w, w, w};
58 return makeProblem(nm, N, N, N, levels, bc, one, one);
59 }
60 if (nm == "cyl64") {
61 int bc[6] = {0, 0, 0, 0, 0, 0};
62 auto open = [](double x, double, double z) {
63 return clamp01(0.5 + std::sqrt((x - 32) * (x - 32) + (z - 32) * (z - 32)) - 16.0);
64 };
65 return makeProblem(nm, 64, 64, 64, levels, bc, open, one);
66 }
67 if (nm == "iso64cut") {
68 int bc[6] = {0, 0, 0, 0, 0, 0};
69 auto open = [](double x, double y, double z) {
70 const double dx = x - 32, dy = y - 30, dz = z - 34;
71 return dx * dx + dy * dy + dz * dz < 18.0 * 18.0 ? 0.0 : 1.0;
72 };
73 return makeProblem(nm, 64, 64, 64, levels, bc, open, one);
74 }
75 const bool m10 = nm == "m10"; // else iso128
76 const int nz = m10 ? 25 : 128;
77 int bc[6] = {0, 0, 0, 0, 0, 0};
78 std::vector<std::array<double, 4>> B;
79 for (int k = 0; k < (m10 ? 12 : 40); ++k)
80 B.push_back({val(100 + k) * 64 + 64, val(200 + k) * 64 + 64, (val(300 + k) * 0.5 + 0.5) * nz,
81 m10 ? 5.0 : 7.0});
82 auto rho = [B](double x, double y, double z) {
83 for (const auto& b : B) {
84 const double dx = x - b[0], dy = y - b[1], dz = z - b[2];
85 if (dx * dx + dy * dy + dz * dz < b[3] * b[3])
86 return 1e-3;
87 }
88 return 1.0;
89 };
90 return makeProblem(nm, 128, 128, nz, levels, bc, one, rho);
91}
92
93int main(int argc, char** argv) {
94 MPI_Init(&argc, &argv);
95 Kokkos::initialize(argc, argv);
96 {
97 const std::string nm = argc > 1 ? argv[1] : "thinW";
98 const int levels = argc > 2 ? std::atoi(argv[2]) : 0;
99 const int lr = argc > 3 ? std::atoi(argv[3]) : 1;
100 const int bot = argc > 4 ? std::atoi(argv[4]) : 12;
101 const int diag = argc > 5 ? std::atoi(argv[5]) : 0;
102 const Problem P = preset(nm, levels);
103 CutcellMG mg;
105 mg.setLineRelaxation(lr != 0);
106 Level& l0 = mg.level(0);
107 const std::size_t n = l0.n;
108 std::vector<double> hb = rhsOf(P);
109 auto ac = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), l0.AC);
110 for (std::size_t i = 0; i < n; ++i)
111 if (!((double)ac(i) > 1e-30))
112 hb[i] = 0.0;
113 constexpr int kCap = 500;
114 std::string out;
115 for (int sym : {0, 1}) {
116 mg.setSymmetricVcycle(sym != 0);
117 CCField b = toDevice(hb, "b"), x("x", n), r("r", n), p("p", n), z("z", n), zp("zp", n),
118 Ap("Ap", n);
119 const int ip = mg.solvePCG(b, x, r, p, z, Ap, kCap, 1e-10, 2, 2, bot);
120 const bool fp = mg.lastSolveFailed() || ip >= kCap;
121 Kokkos::deep_copy(x, 0.0);
122 const int ifc = mg.solveFCG(b, x, r, p, z, zp, Ap, kCap, 1e-10, 2, 2, bot);
123 const bool ff = mg.lastSolveFailed() || ifc >= kCap;
124 char buf[160];
125 std::snprintf(buf, sizeof buf, " | %s PCG %d%s FCG %d%s", sym ? "on" : "off", ip,
126 fp ? " capped" : "", ifc, ff ? " capped" : "");
127 out += buf;
128 }
129 { // the asymmetry a of the symmetric V-cycle (ctest mg_vcycle_symmetry's measure)
130 mg.setSymmetricVcycle(true);
131 CCField x("x", n), y("y", n), Mx("Mx", n), My("My", n);
132 for (int k = 0; k < 2; ++k) {
133 auto h = Kokkos::create_mirror_view(k ? y : x);
134 for (std::size_t i = 0; i < n; ++i)
135 h(i) = 0.0;
136 for (int zz = 0; zz < l0.inner.z; ++zz)
137 for (int yy = 0; yy < l0.inner.y; ++yy)
138 for (int xx = 0; xx < l0.inner.x; ++xx) {
139 const long i = (long)(xx + l0.g) + (long)(yy + l0.g) * l0.ext.x +
140 (long)(zz + l0.g) * (long)l0.ext.x * l0.ext.y;
141 h(i) = (double)ac(i) > 1e-30 ? val((11 + k) * 7919L + xx + 1000L * yy + 1000000L * zz)
142 : 0.0;
143 }
144 Kokkos::deep_copy(k ? y : x, h);
145 mg.removeMean(l0, k ? y : x);
146 }
147 mg.precondVcycle(Mx, x);
148 mg.precondVcycle(My, y);
149 const double a1 = mg.dot(l0, x, My), b1 = mg.dot(l0, y, Mx);
150 char buf[64];
151 std::snprintf(
152 buf, sizeof buf, " | a(on) %.3e",
153 std::abs(a1 - b1) / (std::sqrt(mg.dot(l0, x, x)) * std::sqrt(mg.dot(l0, My, My))));
154 out += buf;
155 }
156 if (diag) { // FR vs PR with the same (symmetric) preconditioner
157 mg.setSymmetricVcycle(true);
158 std::vector<std::vector<double>> hist(2), rel(2);
159 for (int pr = 0; pr < 2; ++pr) {
160 CCField x("x", n), r = toDevice(hb, "r"), z("z", n), zo("zo", n), p("p", n), Ap("Ap", n);
161 mg.removeMean(l0, r);
162 const double r0 = mg.maxabs(l0, r);
163 mg.precondVcycle(z, r);
164 Kokkos::deep_copy(p, z);
165 double rz = mg.dot(l0, r, z);
166 for (int k = 0; k < kCap; ++k) {
167 mg.matvecOverlap(l0, Ap, p);
168 const double alpha = rz / mg.dot(l0, p, Ap);
169 mg.axpy(x, alpha, p);
170 mg.axpy(r, -alpha, Ap);
171 mg.removeMean(l0, r);
172 const double rn = mg.maxabs(l0, r) / r0;
173 hist[pr].push_back(rn);
174 if (rn < 1e-10)
175 break;
176 Kokkos::deep_copy(zo, z);
177 mg.precondVcycle(z, r);
178 const double rzn = mg.dot(l0, r, z), rzo = mg.dot(l0, r, zo);
179 const double bFR = rzn / rz, bPR = (rzn - rzo) / rz;
180 rel[pr].push_back((bPR - bFR) / bFR);
181 Kokkos::deep_copy(Ap, z);
182 mg.axpy(Ap, pr ? bPR : bFR, p);
183 Kokkos::deep_copy(p, Ap);
184 rz = rzn;
185 }
186 }
187 int sep = -1;
188 const std::size_t m = std::min(hist[0].size(), hist[1].size());
189 for (std::size_t k = 0; k < m && sep < 0; ++k)
190 if (std::abs(hist[0][k] / hist[1][k] - 1.0) > 0.01)
191 sep = (int)k + 1;
192 std::printf(
193 "DIAG %s D%d bottom %d: FR its %zu, PR its %zu, first >1%% residual split at it %d\n",
194 nm.c_str(), levels, bot, hist[0].size(), hist[1].size(), sep);
195 for (std::size_t k = 0; k < std::max(hist[0].size(), hist[1].size()); ++k)
196 std::printf("DIAG it %3zu |r|/r0 FR %.3e PR %.3e | (bPR-bFR)/bFR on FR %+.3e\n", k + 1,
197 k < hist[0].size() ? hist[0][k] : 0.0, k < hist[1].size() ? hist[1][k] : 0.0,
198 k < rel[0].size() ? rel[0][k] : 0.0);
199 }
200 const Level& bl = mg.level(mg.nLevels() - 1);
201 std::printf("SYMCONV %s D%d LR%d bot %d levels %d bottom %dx%dx%d %s%s\n", nm.c_str(), levels,
202 lr, bot, mg.nLevels(), bl.inner.x, bl.inner.y, bl.inner.z,
203 mg.agglomerateBottom() ? "agglomerated" : "smoothed", out.c_str());
204 }
205 Kokkos::finalize();
206 MPI_Finalize();
207 return 0;
208}
void axpy(CCField y, double a, CCField x)
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 matvecOverlap(Level &l0, CCField y, CCField v)
void setSymmetricVcycle(bool on)
A3 (doc/mg_depth_policy.md Amendment A3, WO-S1): the symmetric pressure V-cycle – piecewise-constant ...
double maxabs(Level &lv, CCField a)
void precondVcycle(CCField zz, CCField rr)
double dot(Level &lv, CCField a, CCField b)
void setLineRelaxation(bool on)
A2.4 (doc/mg_depth_policy.md, WO-7): zebra line relaxation along the blocked strong axis on the trigg...
int solveFCG(CCField b, CCField x, CCField r, CCField p, CCField z, CCField zp, 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})
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,...
Kokkos::View< double *, CCMem > CCField
std::vector< double > rhsOf(const Problem &P)
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)
static Problem preset(const std::string &nm, int levels)
Definition symconv.cpp:35
static double val(long gi)
Definition symconv.cpp:27
static constexpr int N