62#include <Kokkos_Core.hpp>
78 C3 e{0, 0, 0}, n{0, 0, 0};
80 std::vector<float> WX, WY, WZ;
81 std::vector<double> ox, oy, oz, AC;
82 double gf[3] = {1, 1, 1};
83 explicit HostLevel(CutcellMG::Level& lv) {
94 AC.assign(ac.begin(), ac.end());
95 gf[0] = 1.0 * (1.0 / (double)(
lv.cfac.x *
lv.cfac.x));
96 gf[1] = 1.0 * (1.0 / (double)(
lv.cfac.y *
lv.cfac.y));
97 gf[2] = 1.0 * (1.0 / (double)(
lv.cfac.z *
lv.cfac.z));
100 void forInner(Fn fn)
const {
101 for (
int lz = g; lz < e.z - g; ++lz)
102 for (
int ly = g; ly < e.y - g; ++ly)
103 for (
int lx = g; lx < e.x - g; ++lx) {
104 const long i = (long)lx + (
long)ly * e.x + (long)lz * (
long)e.x * e.y;
105 fn(lx, ly, lz, i,
ccWrapNbrs(lx, ly, lz, n, g, i, e.x, (
long)e.x * e.y));
109 long double applyDot(
const std::vector<double>& u,
const std::vector<double>& v)
const {
110 const long sy = e.x,
sz = (long)e.x * e.y;
112 forInner([&](
int,
int,
int,
long i,
const CcNbrs& w) {
113 const long double q =
114 (
long double)WX[i] * (v[i] - v[
w.xm]) + (
long double)WX[i + 1] * (v[i] - v[
w.xp]) +
115 (
long double)WY[i] * (v[i] - v[
w.ym]) + (
long double)WY[i + sy] * (v[i] - v[
w.yp]) +
116 (
long double)WZ[i] * (v[i] - v[
w.zm]) + (
long double)WZ[i + sz] * (v[i] - v[
w.zp]);
117 s += (
long double)u[i] * q;
121 float Dsum(
long i)
const {
122 const long sy = e.x,
sz = (long)e.x * e.y;
123 return ((((WX[i + 1] + WX[i]) + WY[i + sy]) + WY[i]) + WZ[i + sz]) + WZ[
i];
127std::string
num(
double v) {
129 std::snprintf(b,
sizeof b,
"%.3g", v);
133void check(
bool ok,
const std::string& what) {
134 std::printf(
" %-72s %s\n",
what.c_str(), ok ?
"PASS" :
"FAIL");
140void dataChecks(
const Problem& P) {
145 std::vector<std::vector<float>> fused;
146 for (
int L = 0;
L + 1 < nl; ++
L) {
150 std::snprintf(tag,
sizeof tag,
"%s L%d %dx%dx%d",
P.name.c_str(), L,
h.n.x,
h.n.y,
h.n.z);
154 for (std::size_t i = 0;
i <
h.WX.size(); ++
i) {
155 const double t[3] = {
h.ox[
i] *
h.gf[0],
h.oy[
i] *
h.gf[1],
h.oz[
i] *
h.gf[2]};
156 const float w[3] = {
h.WX[
i],
h.WY[
i],
h.WZ[
i]};
157 for (
int a = 0;
a < 3; ++
a) {
159 worst = std::max(worst, std::fabs((
double)w[a] / t[a] - 1.0));
160 else if (w[a] != 0.0f)
166 long n32 = 0, n64 = 0, h32 = 0, h64 = 0;
168 h.forInner([&](
int,
int,
int,
long i,
const CcNbrs&) {
169 h32 +=
h.Dsum(i) == 0.0f ? 1 : 0;
170 h64 +=
h.AC[
i] < 1
e-30 ? 1 : 0;
173 std::string(tag) +
": (c) max|w/t - 1| = " +
num(worst) +
", w >= 0");
174 check(n32 == n64 && h32 == n32 && h64 == n64, std::string(tag) +
": (c) decoupled sets " +
175 std::to_string(n32) +
176 " == " + std::to_string(n64));
178 std::mt19937 gen(11 + L);
179 std::uniform_real_distribution<double> uni(-1.0, 1.0);
182 [&](
int,
int,
int,
long i,
const CcNbrs&) { maxD = std::max(maxD, (
double)
h.Dsum(i)); });
183 double worstSym = 0.0;
184 for (
int k = 0;
k < 10; ++
k) {
185 std::vector<double>
u(
h.WX.size(), 0.0),
v(
h.WX.size(), 0.0);
186 long double nu = 0, nv = 0;
187 h.forInner([&](
int,
int,
int,
long i,
const CcNbrs&) {
190 nu += (
long double)u[i] * u[i];
191 nv += (
long double)v[i] * v[i];
193 const long double d = std::fabs(
h.applyDot(u, v) -
h.applyDot(v, u));
194 worstSym = std::max(worstSym, (
double)(d / (std::sqrt(nu) * std::sqrt(nv) * maxD)));
196 check(worstSym <= 1e-6,
197 std::string(tag) +
": (b) |u'Wv - v'Wu| / (|u||v| max D) = " +
num(worstSym));
198 fused.push_back(
h.WX);
199 fused.back().insert(fused.back().end(),
h.WY.begin(),
h.WY.end());
200 fused.back().insert(fused.back().end(),
h.WZ.begin(),
h.WZ.end());
205 for (
int L = 0;
L + 1 < nl; ++
L) {
208 w.insert(
w.end(), wy.begin(), wy.end());
209 w.insert(
w.end(), wz.begin(), wz.end());
211 std::memcmp(
w.data(), fused[L].data(),
w.size() *
sizeof(
float)) == 0;
213 check(
same,
P.name +
": the setter's rebuild == the fused build, bitwise");
220 check(none,
P.name +
": 'fp64' allocates no FP32 data");
223constexpr bool kHost = std::is_same_v<CCMem, Kokkos::HostSpace>;
227std::vector<T> randomInner(
const HostLevel& h,
unsigned seed,
double lo,
double hi) {
228 std::mt19937 gen(seed);
229 std::uniform_real_distribution<double> uni(lo, hi);
230 std::vector<T>
v((std::size_t)
h.e.x *
h.e.y *
h.e.z, (T)0);
231 h.forInner([&](
int,
int,
int,
long i,
const CcNbrs&) {
v[
i] = (
T)uni(gen); });
235Kokkos::View<T*, CCMem> upload(
const std::vector<T>& v,
const char* name) {
236 Kokkos::View<T*, CCMem>
f(name,
v.size());
237 auto h = Kokkos::create_mirror_view(f);
238 for (std::size_t i = 0;
i <
v.size(); ++
i)
240 Kokkos::deep_copy(f, h);
246 float q(
const std::vector<float>& x,
long i,
const CcNbrs& w)
const {
247 const long sy = h.e.x,
sz = (long)h.e.x * h.e.y;
248 const float xi =
x[
i];
249 float acc = h.WX[
i] * (xi -
x[
w.xm]);
250 acc =
acc + h.WX[
i + 1] * (xi -
x[
w.xp]);
252 acc =
acc + h.WY[
i + sy] * (xi -
x[
w.yp]);
257 void pass(std::vector<float>& x,
const std::vector<float>& b,
int color)
const {
258 h.forInner([&](
int lx,
int ly,
int lz,
long i,
const CcNbrs& w) {
259 if (((lx + ly + lz) & 1) != color)
261 const float d = h.Dsum(i);
264 x[
i] =
x[
i] + (
b[
i] -
q(x, i, w)) / d;
267 void residual(std::vector<float>& r,
const std::vector<float>& x,
268 const std::vector<float>& b)
const {
269 h.forInner([&](
int,
int,
int,
long i,
const CcNbrs& w) {
r[
i] =
b[
i] -
q(x, i, w); });
272double maxRel(
const std::vector<float>& a,
const std::vector<float>& b) {
273 double m = 0.0,
s = 0.0;
274 for (std::size_t i = 0;
i <
a.size(); ++
i) {
275 m = std::max(m, (
double)std::fabs(a[i] - b[i]));
276 s = std::max(s, (
double)std::fabs(b[i]));
278 return s > 0 ?
m /
s :
m;
282void kernelChecks(
const Problem& P) {
285 for (
int L = 0;
L + 1 < mg.
nLevels(); ++
L) {
289 std::snprintf(tag,
sizeof tag,
"%s L%d",
P.name.c_str(), L);
292 for (
double c : {1.0, -3.7, 1e5, std::ldexp(1.0, -60)}) {
293 Kokkos::deep_copy(
lv.xf, (
VReal)c);
294 Kokkos::deep_copy(
lv.rhsf, 0.0f);
295 Kokkos::deep_copy(
lv.resf, 1.0f);
298 const std::vector<float>
r =
toHost(
lv.resf);
299 h.forInner([&](
int,
int,
int,
long i,
const CcNbrs&) {
zero =
zero &&
r[
i] == 0.0f; });
301 check(zero, std::string(tag) +
": (a) the FP32 residual of c * 1 is exactly 0");
303 const FloatRef
ref{
h};
304 std::vector<float>
x = randomInner<float>(h, 3 + L, -1.0, 1.0),
305 b = randomInner<float>(h, 5 + L, -1.0, 1.0);
306 VField xd = upload(x,
"x"), bd = upload(b,
"b"), rd(
"r",
x.size());
309 for (
int color : {0, 1}) {
310 ref.pass(x, b, color);
312 lv.ext,
lv.inner,
C3{0, 0, 0},
lv.g, color);
313 const std::vector<float> got =
toHost(xd);
314 bits = bits && std::memcmp(got.data(),
x.data(),
x.size() *
sizeof(
float)) == 0;
315 dev = std::max(dev, maxRel(got, x));
317 std::vector<float>
r(
x.size(), 0.0f);
318 ref.residual(r, x, b);
319 Kokkos::deep_copy(xd, upload(x,
"x2"));
322 const std::vector<float> gotr =
toHost(rd);
323 bits = bits && std::memcmp(gotr.data(),
r.data(),
r.size() *
sizeof(
float)) == 0;
324 dev = std::max(dev, maxRel(gotr, r));
327 std::string(tag) +
": (e) smoother passes + residual == float reference, bitwise");
329 check(dev <= 1e-5, std::string(tag) +
": (e) device vs float reference, max rel " +
num(dev));
334void preconditionerChecks(
const Problem& P,
bool nonlinearity) {
338 const std::size_t
n = l0.
n;
340 CCField s0(
"s0", n),
s1(
"s1", n),
s2(
"s2", n),
s3(
"s3", n),
s4(
"s4", n),
s5(
"s5", n);
341 mg.
solvePCG(s0, s1, s2, s3, s4, s5, 0, 1e-8, 2, 2, 12);
344 auto B = [&](
const std::vector<double>& rv) {
350 const std::vector<double>
r1 = randomInner<double>(h, 21, -1.0, 1.0),
351 r2 = randomInner<double>(h, 22, -1.0, 1.0);
352 const auto z1 =
B(r1);
354 for (
int k : {-40, 0, 37}) {
355 std::vector<double> rk(r1);
357 v = std::ldexp(v, k);
358 const auto zk =
B(rk);
359 h.forInner([&](
int,
int,
int,
long i,
const CcNbrs&) {
360 cov = cov && zk.first[
i] == std::ldexp(z1.first[i], k);
363 check(cov,
P.name +
": G-D2 (a) B(2^k r) == 2^k B(r) bitwise, k = -40, 0, 37");
364 auto norm = [&](
auto f) {
366 h.forInner([&](
int,
int,
int,
long i,
const CcNbrs&) {
367 const long double v =
f(i);
370 return (
double)std::sqrt(s2);
373 std::vector<double> r12(r1);
374 for (std::size_t i = 0;
i < r12.size(); ++
i)
376 const auto z2 =
B(r2), z12 =
B(r12);
378 norm([&](
long i) {
return (
long double)z12.first[
i] - z1.first[
i] - z2.first[
i]; }) /
379 norm([&](
long i) {
return (
long double)z1.first[
i]; });
380 check(nl <= 1e-4,
P.name +
": G-D2 (d) |B(r1+r2) - B r1 - B r2| / |B r1| = " +
num(nl));
387 const std::vector<double> z64 =
toHost(z);
388 const double d = norm([&](
long i) {
return (
long double)z1.first[
i] - z64[
i]; }) /
389 norm([&](
long i) {
return (
long double)z64[
i]; });
391 check(d <= 1e-6,
P.name +
": |B32 r - B64 r| / |B64 r| = " +
num(d) +
" <= 1e-6");
398 std::vector<double>
x;
401 const std::vector<double>
hb =
rhsOf(P);
402 const std::size_t
n =
hb.size();
403 CCField b =
toDevice(hb,
"b"),
x(
"x", n),
r(
"r", n),
p(
"p", n),
z(
"z", n), Ap(
"Ap", n);
405 out.it = mg.
solvePCG(b, x, r, p, z, Ap, 200, 1e-8, 2, 2, 12);
412Solve solveGuessOn(
CutcellMG& mg,
const Problem& P,
const std::vector<double>& v1,
413 const std::vector<double>& v2) {
414 const std::vector<double>
hb =
rhsOf(P);
415 const std::size_t
n =
hb.size();
416 CCField b =
toDevice(hb,
"b"),
x =
toDevice(v2,
"x"),
r(
"r", n),
p(
"p", n),
z(
"z", n),
420 out.it = mg.
solvePCG(b, x, r, p, z, Ap, 200, 1e-8, 2, 2, 12);
429 return solveOn(mg, P);
431bool sameSolve(
const Solve& a,
const Solve& b) {
432 return a.it ==
b.it &&
a.fp32 ==
b.fp32 &&
a.x.size() ==
b.x.size() &&
433 std::memcmp(
a.x.data(),
b.x.data(),
a.x.size() *
sizeof(
double)) == 0;
436 mg.
init(
P.nx,
P.ny,
P.nz,
P.levels);
448 bool failed =
false, fp32 =
false;
449 double trueRel = 0.0, healthMedian = 0.0;
451KrylovRun krylov(
const Problem& P,
int mode,
int driver,
double rtol,
int maxit) {
455 const C3 e =
P.ext();
456 const std::size_t
n = (std::size_t)
e.x *
e.y *
e.z;
457 std::vector<double>
hb(n, 0.0);
458 for (
int z = 0;
z <
P.nz; ++
z)
459 for (
int y = 0;
y <
P.ny; ++
y)
460 for (
int x = 0;
x <
P.nx; ++
x)
461 hb[
P.idx(x, y, z)] = std::sin(0.3 * x + 0.1) * std::cos(0.2 * y) + 0.1 * std::sin(0.5 * z);
462 CCField b =
toDevice(hb,
"b"),
x(
"x", n),
r(
"r", n),
p(
"p", n), zz(
"z", n), zp(
"zp", n),
467 out.it =
driver == 0 ? mg.
solvePCG(b, x, r, p, zz, Ap, maxit, rtol, 2, 2, 12)
468 : mg.
solveFCG(b, x, r, p, zz, zp, Ap, maxit, rtol, 2, 2, 12);
474 Kokkos::deep_copy(t, b);
475 auto th = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), t);
476 auto ah = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), Ap);
477 for (std::size_t i = 0;
i <
n; ++
i)
479 Kokkos::deep_copy(t, th);
484 std::sort(
h.begin(),
h.end());
485 out.healthMedian =
h[
h.size() / 2];
491 for (
int driver : {0, 1}) {
492 const std::string
tag =
P.name + (
driver == 0 ?
" PCG" :
" FCG");
495 const int slack = std::max(1, (
int)std::ceil(0.05 * a64.it));
498 const bool valid = a64.it < 200 && a64.trueRel <= 1.5e-8;
499 if (fcgMustConverge && driver == 1)
500 check(valid, tag +
": fp64 converges (it " + std::to_string(a64.it) +
", true " +
501 num(a64.trueRel) +
"), so (a)-(e) run here");
503 std::printf(
" %-72s (info)\n",
504 (tag +
": fp64 does not converge (it " + std::to_string(a64.it) +
", true " +
505 num(a64.trueRel) +
"); fp32 it " + std::to_string(a32.it) +
", true " +
506 num(a32.trueRel) +
": (a), (c), (d), (e) n/a")
509 check(a32.fp32 && !a64.fp32 && a32.it <= a64.it + slack,
510 tag +
": G-D3 (a) iterations fp32 " + std::to_string(a32.it) +
" vs fp64 " +
511 std::to_string(a64.it));
512 check(a32.trueRel <= 1.5e-8, tag +
": G-D3 (c) exit max|P(b - Ax)|/max|Pb| = " +
513 num(a32.trueRel) +
" (fp64 " +
num(a64.trueRel) +
")");
514 check(!a32.failed && a32.it < 200, tag +
": G-D3 (d) no failure, not capped");
515 check(a32.healthMedian <= std::fmax(1.1 * a64.healthMedian, 1e-2),
516 tag +
": G-D3 (e) health median fp32 " +
num(a32.healthMedian) +
" <= max(1.1x fp64 " +
517 num(a64.healthMedian) +
", 1e-2)");
521 check(f32.trueRel <= 2.0 * f64.trueRel,
522 tag +
": G-D3 (b) floor fp32 " +
num(f32.trueRel) +
" vs fp64 " +
num(f64.trueRel) +
523 " (it " + std::to_string(f32.it) +
" / " + std::to_string(f64.it) +
")");
527void eligibilityChecks() {
528 auto ones = [](double, double, double) {
return 1.0; };
529 auto uni = [](double, double, double) {
return 1.0; };
531 const int bcOut[6] = {0, 0, 0, 0, 1, 3};
534 const int bcPer[6] = {0, 0, 0, 0, 0, 0};
540 tiny.
ox[tiny.
idx(5, 5, 5)] = 1
e-35;
544 }
cases[] = {{&
out,
"outflow"}, {&odd,
"odd"}, {&
one,
"single level"}, {&tiny,
"face weight"}};
545 for (
const Case& c : cases) {
549 check(why && std::string(why).find(
c.want) != std::string::npos,
550 c.P->name +
": (d) ineligible: " + (why ? why :
"(eligible!)"));
555 none = none && mg.
level(L).
WX.extent(0) == 0;
556 const bool can =
c.P == &tiny;
557 check(none != can,
c.P->name + (can ?
": FP32 data built (the range check decides)"
558 :
": no FP32 data built on a configuration that cannot "
567 check(why && std::string(why).find(
"PCG or FCG") != std::string::npos,
568 std::string(
"eligible 16^3, another driver: (d) ") + (why ? why :
"(eligible!)"));
571 check(why && std::string(why).find(
"overlay") != std::string::npos,
572 std::string(
"eligible 16^3 + overlay: (d) ") + (why ? why :
"(eligible!)"));
578 check(why && std::string(why).find(
"mean-removal") != std::string::npos,
579 std::string(
"eligible 16^3, scope 'all': (d) ") + (why ? why :
"(eligible!)"));
582 "eligible 16^3, scope back to 'fine': eligible, weights rebuilt");
598 none = none && dflt.
level(L).
WX.extent(0) == 0;
599 const Solve
a = solveOn(dflt, ok);
602 check(sameSolve(a, b),
603 "eligible 16^3: (f) the default solve == the explicit 'auto' solve "
605 std::to_string(
a.it) +
" / " + std::to_string(
b.it));
607 std::string(
"eligible 16^3: (f) the default ") +
609 :
"builds FP32 data, FP32 reported (device)"));
611 check(
f.fp32 && !
b.fp32 && sameSolve(d, b),
612 "eligible 16^3: (f) host 'auto' == 'fp64' bitwise, it " + std::to_string(
a.it) +
" / " +
613 std::to_string(
b.it) +
", FP64 reported ('fp32' runs: it " + std::to_string(
f.it) +
616 check(
f.fp32 && sameSolve(f, b),
"eligible 16^3: (f) 'auto' == 'fp32' bitwise, it " +
617 std::to_string(
f.it) +
" / " + std::to_string(
b.it) +
621 for (
const Problem* P : {&
out, &odd, &tiny}) {
623 check(!
a.fp32 && sameSolve(a, b),
P->name +
": (f) 'auto' == 'fp64' bitwise, it " +
624 std::to_string(
a.it) +
" / " + std::to_string(
b.it) +
631 const Solve sa = solveOn(a, tiny), sb = solveOn(b, tiny);
632 check(!sa.fp32 && sameSolve(sa, sb),
633 "tiny-face, exact outer: (f) 'auto' == 'fp64' bitwise, it " + std::to_string(sa.it) +
634 " / " + std::to_string(sb.it) +
", FP64 reported");
640 }
catch (
const std::runtime_error& e) {
643 check(
what.find(
"not eligible here") != std::string::npos,
644 P->name +
": (f) 'fp32' raises at the solve: " + (
what.empty() ?
"(no raise!)" : what));
653bool fp64Deferred(
CutcellMG& mg,
bool deferred,
bool l0rhs =
true) {
656 for (
int L = 0;
L < nl; ++
L) {
658 const bool bottom =
L + 1 == nl;
659 const auto has = [&
v](
const auto&
f) {
return f.extent(0) ==
v.n; };
660 const bool dead = deferred && !
bottom;
661 ok =
ok && has(
v.ox) && has(
v.oy) && has(
v.oz);
662 ok =
ok && has(
v.x) == (
L == 0 || !dead) && has(
v.AC) == (
L == 0 || !dead);
663 ok =
ok && has(
v.rhs) == (!dead && (
L > 0 || l0rhs)) && has(
v.res) == !dead &&
664 has(
v.AFX) == !dead && has(
v.AFY) == !dead && has(
v.AFZ) == !dead;
673 b += 8.0 * (double)(
v.x.extent(0) +
v.rhs.extent(0) +
v.res.extent(0) +
v.ox.extent(0) +
674 v.oy.extent(0) +
v.oz.extent(0));
675 b += (double)
sizeof(FPV::value_type) *
676 (double)(
v.AC.extent(0) +
v.AFX.extent(0) +
v.AFY.extent(0) +
v.AFZ.extent(0));
677 b += 4.0 * (double)(
v.WX.extent(0) +
v.WY.extent(0) +
v.WZ.extent(0) +
v.xf.extent(0) +
678 v.rhsf.extent(0) +
v.resf.extent(0));
680 return b / (double)mg.
level(0).
n;
686 check(fp64Deferred(ref64,
false),
"D5 column, 'fp64' exact: every FP64 array allocated");
687 const double b64 = mgBytesPerCell(ref64);
688 const Solve s64 = solveOn(ref64, P);
691 check(fp64Deferred(mg,
true),
"D5 column, 'fp32' exact: the §4.4.7 arrays are not allocated");
692 const double b32 = mgBytesPerCell(mg);
693 std::printf(
" %-72s %s\n",
694 (
"D5 column: MG level arrays " +
num(b64) +
" B/cell ('fp64') -> " +
num(b32) +
698 const Solve
a = solveOn(mg, P);
699 check(
a.fp32 && fp64Deferred(mg,
true),
"D5 column: the FP32 solve allocates no FP64 data");
702 const Solve
b = solveOn(mg, P);
703 check(sameSolve(b, s64) && fp64Deferred(mg,
false,
false),
704 "D5 column: switched to 'fp64' == 'fp64' throughout, bitwise, it " + std::to_string(
b.it) +
705 " / " + std::to_string(s64.it));
708 const Solve
c = solveOn(mg, P);
709 check(sameSolve(c, a) && fp64Deferred(mg,
true),
710 "D5 column: back to 'fp32' releases them, bitwise the first FP32 solve");
714 check(fp64Deferred(eager,
false),
"D5 column, 'fp32' non-exact: every FP64 array allocated");
715 const Solve
e = solveOn(eager, P);
719 const Solve l = solveOn(lazy, P);
720 check(sameSolve(l, e) && fp64Deferred(lazy,
false),
721 "D5 column: exact off after a deferred build == the eager build, bitwise");
724 std::vector<double> v1 =
e.x, v2 =
e.x;
725 for (std::size_t i = 0;
i < v2.size(); ++
i) {
726 v1[
i] = 0.97 * v1[
i] + 1
e-3 * std::cos(0.23 * (
double)i);
727 v2[
i] = 0.9 * v2[
i] + 1
e-3 * std::sin(0.37 * (
double)i);
733 const Solve ge = solveGuessOn(eagerG, P, v1, v2), gl = solveGuessOn(lazyG, P, v1, v2);
734 check(sameSolve(gl, ge) && ge.it <
e.it,
735 "D5 x E column: exact off after a deferred build, projected guess == the eager build, "
737 std::to_string(gl.it) +
" / " + std::to_string(ge.it) +
" (cold " +
738 std::to_string(
e.it) +
")");
744void memoryReport(
int n,
int levels) {
745 auto ones = [](double, double, double) {
return 1.0; };
746 const int bcPer[6] = {0, 0, 0, 0, 0, 0};
755 for (
const auto& r : rows) {
757 setUpExact(mg, P,
r.mode,
r.exact);
758 std::printf(
"D5 memory %d^3 box, %d levels: %s: %.1f B/cell\n", n, mg.
nLevels(),
r.what,
767void boxKernelChecks(
const Problem& P) {
770 std::mt19937 gen(11);
771 std::uniform_real_distribution<double> uni(-1.0, 1.0);
772 for (
int L = 0;
L + 1 < mg.
nLevels(); ++
L) {
776 const std::size_t
n =
lv.n;
777 std::vector<float> hx(n, 0.0f),
hb(n, 0.0f);
778 for (
int z = g;
z <
e.z -
g; ++
z)
779 for (
int y = g;
y <
e.y -
g; ++
y)
780 for (
int x = g;
x <
e.x -
g; ++
x) {
781 const long i = (long)x + (
long)
y *
e.x + (long)z * (
long)
e.x *
e.y;
782 hx[
i] =
static_cast<float>(uni(gen));
783 hb[
i] =
static_cast<float>(uni(gen));
785 auto up = [&](
const std::vector<float>&
h,
const char* name) {
787 auto m = Kokkos::create_mirror_view(f);
788 for (std::size_t i = 0;
i <
n; ++
i)
790 Kokkos::deep_copy(f, m);
796 const C3 ilo{
g + 1,
g + 1,
g + 1}, ihi{
e.x -
g - 1,
e.y -
g - 1,
e.z -
g - 1};
797 const C3 none{0, 0, 0};
801 for (
int z = g;
z <
e.z -
g; ++
z)
802 for (
int y = g;
y <
e.y -
g; ++
y)
803 for (
int x = g;
x <
e.x -
g; ++
x) {
804 const long i = (long)x + (
long)
y *
e.x + (long)z * (
long)
e.x *
e.y;
805 bad += std::memcmp(&ha[i], &hc[i],
sizeof(
float)) != 0;
810 for (
int color : {0, 1}) {
811 VField xw = up(hx,
"xw"), xb = up(hx,
"xb"), xs = up(hx,
"xs");
815 cutcellSmoothColorFp32Box(xb,
VConst(b), W[0], W[1], W[2], e, og, color, lo, hi, none, none);
816 cutcellSmoothColorFp32Box(xs,
VConst(b), W[0], W[1], W[2], e, og, color, ilo, ihi, none,
818 cutcellSmoothColorFp32Box(xs,
VConst(b), W[0], W[1], W[2], e, og, color, lo, hi, ilo, ihi);
819 bad += innerSame(xw, xb) + innerSame(xb, xs);
821 VField x = up(hx,
"x"), rw(
"rw", n),
rb(
"rb", n), rs(
"rs", n);
824 residualFp32Box(rb,
VConst(x),
VConst(b), W[0], W[1], W[2], e, lo, hi, none, none);
825 residualFp32Box(rs,
VConst(x),
VConst(b), W[0], W[1], W[2], e, ilo, ihi, none, none);
826 residualFp32Box(rs,
VConst(x),
VConst(b), W[0], W[1], W[2], e, lo, hi, ilo, ihi);
827 bad += innerSame(rw, rb) + innerSame(rb, rs);
828 check(bad == 0,
P.name +
" L" + std::to_string(L) +
829 ": G-K1 box == wrap (smoother both colours, residual), split == full (" +
830 std::to_string(bad) +
" cells differ)");
836int main(
int argc,
char** argv) {
837 Kokkos::initialize(argc, argv);
838 if (argc >= 4 && std::string(argv[1]) ==
"--memory") {
839 memoryReport(std::atoi(argv[2]), std::atoi(argv[3]));
841 std::printf(
"mg_fp32_vcycle (doc/vof_projection_cost_design.md §4, G-D1)\n");
847 preconditionerChecks(P, P.name ==
"column");
852 preconditionerChecks(S,
false);
857 std::printf(
"%s (%d failure%s)\n",
fails ?
"FAIL" :
"PASS",
fails,
fails == 1 ?
"" :
"s");
860 return fails ? 1 : 0;
static C3 parityOg(const Level &lv)
static constexpr bool kHostMemory
void removeMean(Level &lv, CCField f, bool stopGuard=false)
void clearProjectedGuess()
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 precondVcycleFp32(CCField zz, CCField rr, int e, int k, CCField p)
void matvecOverlap(Level &l0, CCField y, CCField v)
int vcyclePrecision() const
const std::vector< double > & healthLog() const
void debugSetOverlaySolve(bool on)
const char * fp32VcycleIneligible(bool krylov) const
void setOpenness(CCConst ox, CCConst oy, CCConst oz, double idx2, double idy2, double idz2)
void setProjectedGuess(CCField v1, int hist)
void setBoundaryConditions(const int bc[6])
double maxabs(Level &lv, CCField a)
void fp32DecoupledCounts(int L, long &n32, long &n64)
void precondVcycle(CCField zz, CCField rr)
void setHealthTrace(bool on)
bool lastVcycleFp32() const
void setExactResidual(bool on)
The exact double flux-form level-0 apply in the residual and the Krylov matvec.
void init(int nx, int ny, int nz, int nLevels)
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})
void setMeanRemovalScope(bool all)
bool fp32WeightsOk(int L) const
void fillWrap(Level &lv, V f)
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,...
std::string num(double v)
void zero(const ScalarVec &y)
void cutcellSmoothColorFp32Box(VField x, VConst b, VConst WX, VConst WY, VConst WZ, C3 e, C3 og, int color, C3 lo, C3 hi, C3 exLo, C3 exHi, int wrapMask=0, int g=1)
Kokkos::View< const double *, CCMem > CCConst
void residualFp32Box(VField r, VConst x, VConst b, VConst WX, VConst WY, VConst WZ, C3 e, C3 lo, C3 hi, C3 exLo, C3 exHi, int wrapMask=0, int g=1)
void residualFp32Wrap(VField r, VConst x, VConst b, VConst WX, VConst WY, VConst WZ, C3 e, C3 n, int g)
Kokkos::View< VReal *, CCMem > VField
CcNbrs ccWrapNbrs(int lx, int ly, int lz, C3 n, int g, long i, long sy, long sz)
Kokkos::View< double *, CCMem > CCField
void cutcellSmoothColorFp32Wrap(VField x, VConst b, VConst WX, VConst WY, VConst WZ, C3 e, C3 n, C3 og, int g, int color)
Kokkos::View< const VReal *, CCMem > VConst
std::vector< double > rhsOf(const Problem &P)
Problem slabProblem(double ratio=1e3)
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")
solve(N, kind, mu=0.1, F=1e-3, dt=80.0, warm_tol=1e-7, tail=40, max_steps=4000)
Kokkos::View< VReal *, CCMem > WX
long idx(int x, int y, int z) const
static void check(bool ok, const std::string &what)
static void solverChecks()
static bool same(double a, double b)