flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_mg_fp32_vcycle.cpp
Go to the documentation of this file.
1
57#include <algorithm>
58#include <cmath>
59#include <cstdio>
60#include <cstdlib>
61#include <cstring>
62#include <Kokkos_Core.hpp>
63#include <random>
64#include <stdexcept>
65#include <string>
66#include <vector>
67
68#include "mac_cutcell_mg.hpp"
69#include "mg_fp32_problems.hpp"
70
71using namespace peclet::flow;
72using namespace peclet_flow_fp32_problems;
73
74namespace {
75
76// A level's FP32 data on the host, with the wrap neighbours of ccWrapNbrs.
77struct HostLevel {
78 C3 e{0, 0, 0}, n{0, 0, 0};
79 int g = 1;
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) {
84 e = lv.ext;
85 n = lv.inner;
86 g = lv.g;
87 WX = toHost(lv.WX);
88 WY = toHost(lv.WY);
89 WZ = toHost(lv.WZ);
90 ox = toHost(lv.ox);
91 oy = toHost(lv.oy);
92 oz = toHost(lv.oz);
93 auto ac = toHost(lv.AC);
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));
98 }
99 template <class Fn>
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));
106 }
107 }
108 // (W v)_i = sum_f w_f (v_i - v_j), long double
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;
111 long double s = 0;
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;
118 });
119 return s;
120 }
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];
124 }
125};
126
127std::string num(double v) {
128 char b[32];
129 std::snprintf(b, sizeof b, "%.3g", v);
130 return b;
131}
132int fails = 0;
133void check(bool ok, const std::string& what) {
134 std::printf(" %-72s %s\n", what.c_str(), ok ? "PASS" : "FAIL");
135 if (!ok)
136 ++fails;
137}
138
139// (b), (c) and the rebuild identity on every non-bottom level of P.
140void dataChecks(const Problem& P) {
141 CutcellMG mg;
143 const int nl = mg.nLevels();
144 check(mg.fp32VcycleIneligible(true) == nullptr, P.name + ": eligible");
145 std::vector<std::vector<float>> fused;
146 for (int L = 0; L + 1 < nl; ++L) {
147 CutcellMG::Level& lv = mg.level(L);
148 HostLevel h(lv);
149 char tag[96];
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);
151 // (c) the rounding of each face weight, and the decoupled sets
152 double worst = 0.0;
153 bool sign = true;
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) {
158 if (t[a] != 0.0)
159 worst = std::max(worst, std::fabs((double)w[a] / t[a] - 1.0));
160 else if (w[a] != 0.0f)
161 worst = 1.0;
162 if (w[a] < 0.0f)
163 sign = false;
164 }
165 }
166 long n32 = 0, n64 = 0, h32 = 0, h64 = 0;
167 mg.fp32DecoupledCounts(L, n32, n64);
168 h.forInner([&](int, int, int, long i, const CcNbrs&) {
169 h32 += h.Dsum(i) == 0.0f ? 1 : 0;
170 h64 += h.AC[i] < 1e-30 ? 1 : 0;
171 });
172 check(mg.fp32WeightsOk(L) && sign && worst <= 0x1p-24,
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));
177 // (b) symmetry of the stored-weight flux form
178 std::mt19937 gen(11 + L);
179 std::uniform_real_distribution<double> uni(-1.0, 1.0);
180 double maxD = 0.0;
181 h.forInner(
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&) {
188 u[i] = uni(gen);
189 v[i] = uni(gen);
190 nu += (long double)u[i] * u[i];
191 nv += (long double)v[i] * v[i];
192 });
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)));
195 }
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());
201 }
202 // the setter's rebuild from the stored openness == the fused build, bitwise
204 bool same = true;
205 for (int L = 0; L + 1 < nl; ++L) {
206 CutcellMG::Level& lv = mg.level(L);
207 std::vector<float> w = toHost(lv.WX), wy = toHost(lv.WY), wz = toHost(lv.WZ);
208 w.insert(w.end(), wy.begin(), wy.end());
209 w.insert(w.end(), wz.begin(), wz.end());
210 same = same && mg.fp32WeightsOk(L) && w.size() == fused[L].size() &&
211 std::memcmp(w.data(), fused[L].data(), w.size() * sizeof(float)) == 0;
212 }
213 check(same, P.name + ": the setter's rebuild == the fused build, bitwise");
214 // 'fp64': no FP32 data on a fresh hierarchy
215 CutcellMG m64;
217 bool none = true;
218 for (int L = 0; L < m64.nLevels(); ++L)
219 none = none && m64.level(L).WX.extent(0) == 0 && !m64.fp32WeightsOk(L);
220 check(none, P.name + ": 'fp64' allocates no FP32 data");
221}
222
223constexpr bool kHost = std::is_same_v<CCMem, Kokkos::HostSpace>;
224
225// a deterministic pseudo-random field on the inner cells of level lv (0 in the ghosts)
226template <class T>
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); });
232 return v;
233}
234template <class T>
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)
239 h(i) = v[i];
240 Kokkos::deep_copy(f, h);
241 return f;
242}
243// The scalar plain-float reference of §4.4.3 (one colour pass / the residual), written out.
244struct FloatRef {
245 const HostLevel& 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]);
251 acc = acc + h.WY[i] * (xi - x[w.ym]);
252 acc = acc + h.WY[i + sy] * (xi - x[w.yp]);
253 acc = acc + h.WZ[i] * (xi - x[w.zm]);
254 acc = acc + h.WZ[i + sz] * (xi - x[w.zp]);
255 return acc;
256 }
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)
260 return;
261 const float d = h.Dsum(i);
262 if (d == 0.0f)
263 return;
264 x[i] = x[i] + (b[i] - q(x, i, w)) / d;
265 });
266 }
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); });
270 }
271};
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]));
277 }
278 return s > 0 ? m / s : m;
279}
280
281// G-D1 (a), (e) on every non-bottom level of P.
282void kernelChecks(const Problem& P) {
283 CutcellMG mg;
285 for (int L = 0; L + 1 < mg.nLevels(); ++L) {
286 CutcellMG::Level& lv = mg.level(L);
287 HostLevel h(lv);
288 char tag[96];
289 std::snprintf(tag, sizeof tag, "%s L%d", P.name.c_str(), L);
290 // (a) x = c 1, b = 0 -> r = 0 exactly
291 bool zero = true;
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);
296 residualFp32Wrap(lv.resf, VConst(lv.xf), VConst(lv.rhsf), VConst(lv.WX), VConst(lv.WY),
297 VConst(lv.WZ), lv.ext, lv.inner, lv.g);
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; });
300 }
301 check(zero, std::string(tag) + ": (a) the FP32 residual of c * 1 is exactly 0");
302 // (e) one colour pass of each colour, and the residual, against the float reference
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());
307 double dev = 0.0;
308 bool bits = true;
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));
316 }
317 std::vector<float> r(x.size(), 0.0f);
318 ref.residual(r, x, b);
319 Kokkos::deep_copy(xd, upload(x, "x2"));
320 residualFp32Wrap(rd, VConst(xd), VConst(bd), VConst(lv.WX), VConst(lv.WY), VConst(lv.WZ),
321 lv.ext, lv.inner, lv.g);
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));
325 if (kHost)
326 check(bits,
327 std::string(tag) + ": (e) smoother passes + residual == float reference, bitwise");
328 else
329 check(dev <= 1e-5, std::string(tag) + ": (e) device vs float reference, max rel " + num(dev));
330 }
331}
332
333// G-D2 (a), (d) and the FP32 / FP64 difference on level 0 of P (B = one FP32 V-cycle, z = B r).
334void preconditionerChecks(const Problem& P, bool nonlinearity) {
335 CutcellMG mg;
337 CutcellMG::Level& l0 = mg.level(0);
338 const std::size_t n = l0.n;
339 { // the driver's smoothing schedule (2, 2, 12): a zero-iteration solve sets it
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);
342 }
343 HostLevel h(l0);
344 auto B = [&](const std::vector<double>& rv) {
345 CCField r = toDevice(rv, "r"), z("z", n);
346 mg.removeMean(l0, r);
347 mg.precondVcycleFp32(z, r, std::ilogb(mg.maxabs(l0, r)), -1, CCField());
348 return std::make_pair(toHost(z), toHost(r));
349 };
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);
353 bool cov = true;
354 for (int k : {-40, 0, 37}) {
355 std::vector<double> rk(r1);
356 for (double& v : rk)
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);
361 });
362 }
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) {
365 long double s2 = 0;
366 h.forInner([&](int, int, int, long i, const CcNbrs&) {
367 const long double v = f(i);
368 s2 += v * v;
369 });
370 return (double)std::sqrt(s2);
371 };
372 if (nonlinearity) {
373 std::vector<double> r12(r1);
374 for (std::size_t i = 0; i < r12.size(); ++i)
375 r12[i] += r2[i];
376 const auto z2 = B(r2), z12 = B(r12);
377 const double nl =
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));
381 }
382 // for the record: the same r through today's FP64 V-cycle
384 CCField r = toDevice(r1, "r"), z("z", n);
385 mg.removeMean(l0, r);
386 mg.precondVcycle(z, r);
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]; });
390 // review finding 4: the guard against vcycleFp32Impl drifting from vcycleImpl (5-7e-8 measured)
391 check(d <= 1e-6, P.name + ": |B32 r - B64 r| / |B64 r| = " + num(d) + " <= 1e-6");
392}
393
394// A PCG solve of P from x = 0 on a fresh hierarchy; mode as given.
395struct Solve {
396 int it = -1;
397 bool fp32 = false;
398 std::vector<double> x;
399};
400Solve solveOn(CutcellMG& mg, const Problem& P) {
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);
404 Solve out;
405 out.it = mg.solvePCG(b, x, r, p, z, Ap, 200, 1e-8, 2, 2, 12);
406 out.fp32 = mg.lastVcycleFp32();
407 out.x = toHost(x);
408 return out;
409}
410// A PCG solve of P from the projected guess (E, §5.3) on the span of v1 and v2 (history 2: the
411// solve's x holds v2 = phi^{n-1} on entry, as projectSolve hands it over).
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),
417 Ap("Ap", n);
418 mg.setProjectedGuess(toDevice(v1, "v1"), 2);
419 Solve out;
420 out.it = mg.solvePCG(b, x, r, p, z, Ap, 200, 1e-8, 2, 2, 12);
422 out.fp32 = mg.lastVcycleFp32();
423 out.x = toHost(x);
424 return out;
425}
426Solve solve(const Problem& P, int mode) {
427 CutcellMG mg;
428 setUp(mg, P, mode);
429 return solveOn(mg, P);
430}
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;
434}
435void setUpExact(CutcellMG& mg, const Problem& P, int mode, bool exact) {
436 mg.init(P.nx, P.ny, P.nz, P.levels);
438 mg.setExactResidual(exact);
439 mg.setVcyclePrecision(mode);
440 mg.setOpenness(CCConst(toDevice(P.ox, "ox")), CCConst(toDevice(P.oy, "oy")),
441 CCConst(toDevice(P.oz, "oz")), 1.0, 1.0, 1.0);
442}
443
444// One single-rank solve for G-D3 (driver 0 = PCG, 1 = FCG): iterations, failure, the FP32 flag,
445// the exit's true relative residual max|P(b - A x)| / max|P b|, and the health ratios.
446struct KrylovRun {
447 int it = 0;
448 bool failed = false, fp32 = false;
449 double trueRel = 0.0, healthMedian = 0.0;
450};
451KrylovRun krylov(const Problem& P, int mode, int driver, double rtol, int maxit) {
452 CutcellMG mg;
453 setUp(mg, P, mode);
454 mg.setHealthTrace(true);
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),
463 Ap("Ap", n);
464 CutcellMG::Level& l0 = mg.level(0);
465 mg.removeMean(l0, b); // the projection's compatible rhs
466 KrylovRun out;
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);
469 out.failed = mg.lastSolveFailed();
470 out.fp32 = mg.lastVcycleFp32();
471 // the true residual of the returned x, mean-projected
472 mg.matvecOverlap(l0, Ap, x);
473 CCField t("t", n);
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)
478 th(i) -= ah(i);
479 Kokkos::deep_copy(t, th);
480 mg.removeMean(l0, t);
481 out.trueRel = mg.maxabs(l0, t) / mg.maxabs(l0, b);
482 std::vector<double> h = mg.healthLog();
483 if (!h.empty()) {
484 std::sort(h.begin(), h.end());
485 out.healthMedian = h[h.size() / 2];
486 }
487 return out;
488}
489
490void solverChecks(const Problem& P, bool fcgMustConverge = false) {
491 for (int driver : {0, 1}) {
492 const std::string tag = P.name + (driver == 0 ? " PCG" : " FCG");
493 const KrylovRun a64 = krylov(P, CutcellMG::kVcycleFp64, driver, 1e-8, 200),
494 a32 = krylov(P, CutcellMG::kVcycleFp32, driver, 1e-8, 200);
495 const int slack = std::max(1, (int)std::ceil(0.05 * a64.it));
496 // (a), (c), (d) compare against an FP64 solve that converged for real (cyl / pack at 1e4 cap
497 // or diverge in FP64 too: the coarsening limit of CLAUDE.md "Pressure solve")
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");
502 if (!valid) {
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")
507 .c_str());
508 } else {
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)");
518 }
519 const KrylovRun f64 = krylov(P, CutcellMG::kVcycleFp64, driver, 1e-14, 300),
520 f32 = krylov(P, CutcellMG::kVcycleFp32, driver, 1e-14, 300);
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) + ")");
524 }
525}
526
527void eligibilityChecks() {
528 auto ones = [](double, double, double) { return 1.0; };
529 auto uni = [](double, double, double) { return 1.0; };
530 // outflow
531 const int bcOut[6] = {0, 0, 0, 0, 1, 3};
532 Problem out = makeProblem("outflow", 16, 16, 16, 3, bcOut, ones, uni);
533 // odd inner dimension on a non-bottom level (18 -> 9 on level 1 of 3)
534 const int bcPer[6] = {0, 0, 0, 0, 0, 0};
535 Problem odd = makeProblem("odd", 24, 20, 18, 3, bcPer, ones, uni);
536 // a single level
537 Problem one = makeProblem("one-level", 16, 16, 16, 1, bcPer, ones, uni);
538 // a face weight below 1e-30 (one face of the periodic box)
539 Problem tiny = makeProblem("tiny-face", 16, 16, 16, 3, bcPer, ones, uni);
540 tiny.ox[tiny.idx(5, 5, 5)] = 1e-35;
541 struct Case {
542 const Problem* P;
543 const char* want;
544 } cases[] = {{&out, "outflow"}, {&odd, "odd"}, {&one, "single level"}, {&tiny, "face weight"}};
545 for (const Case& c : cases) {
546 CutcellMG mg;
548 const char* why = mg.fp32VcycleIneligible(true);
549 check(why && std::string(why).find(c.want) != std::string::npos,
550 c.P->name + ": (d) ineligible: " + (why ? why : "(eligible!)"));
551 // review finding 5: 'fp32' builds FP32 data only where the configuration can be eligible
552 // (the face-range case can: its weights are built, the check fails)
553 bool none = true;
554 for (int L = 0; L < mg.nLevels(); ++L)
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 "
559 "be eligible"));
560 }
561 { // the overlay (test hook) and a non-Krylov caller, on an otherwise eligible problem
562 Problem ok = makeProblem("eligible", 16, 16, 16, 3, bcPer, ones, uni);
563 CutcellMG mg;
565 check(mg.fp32VcycleIneligible(true) == nullptr, "eligible 16^3: eligible");
566 const char* why = mg.fp32VcycleIneligible(false);
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!)"));
569 mg.debugSetOverlaySolve(true);
570 why = mg.fp32VcycleIneligible(true);
571 check(why && std::string(why).find("overlay") != std::string::npos,
572 std::string("eligible 16^3 + overlay: (d) ") + (why ? why : "(eligible!)"));
573 mg.debugSetOverlaySolve(false);
574 // the mean-removal scope is a build-time condition: 'all' marks the weights stale, 'fine'
575 // rebuilds them from the stored openness at once
576 mg.setMeanRemovalScope(true);
577 why = mg.fp32VcycleIneligible(true);
578 check(why && std::string(why).find("mean-removal") != std::string::npos,
579 std::string("eligible 16^3, scope 'all': (d) ") + (why ? why : "(eligible!)"));
580 mg.setMeanRemovalScope(false);
581 check(mg.fp32VcycleIneligible(true) == nullptr && mg.fp32WeightsOk(0),
582 "eligible 16^3, scope back to 'fine': eligible, weights rebuilt");
583 }
584 // (f) the default is 'auto' (WO-F, USER 2026-10-19; 'fp64' under the 2026-10-15 opt-in ruling):
585 // the default solve is bitwise the explicit 'auto' solve; on a host backend 'auto' is 'fp64'
586 // (WO-A) and builds no FP32 data, on a device backend it is 'fp32' where eligible; 'fp32' on the
587 // ineligible solvable configurations raises at the solve, naming why
588 {
589 Problem ok = makeProblem("eligible", 16, 16, 16, 3, bcPer, ones, uni);
590 CutcellMG dflt;
591 check(dflt.vcyclePrecision() == CutcellMG::kVcycleAuto, "(f) the default mode is 'auto'");
592 dflt.init(ok.nx, ok.ny, ok.nz, ok.levels);
593 dflt.setBoundaryConditions(ok.bc);
594 dflt.setOpenness(CCConst(toDevice(ok.ox, "ox")), CCConst(toDevice(ok.oy, "oy")),
595 CCConst(toDevice(ok.oz, "oz")), 1.0, 1.0, 1.0);
596 bool none = true;
597 for (int L = 0; L < dflt.nLevels(); ++L)
598 none = none && dflt.level(L).WX.extent(0) == 0;
599 const Solve a = solveOn(dflt, ok);
600 const Solve f = solve(ok, CutcellMG::kVcycleFp32), b = solve(ok, CutcellMG::kVcycleAuto),
602 check(sameSolve(a, b),
603 "eligible 16^3: (f) the default solve == the explicit 'auto' solve "
604 "bitwise, it " +
605 std::to_string(a.it) + " / " + std::to_string(b.it));
606 check(CutcellMG::kHostMemory ? (none && !a.fp32) : (!none && a.fp32),
607 std::string("eligible 16^3: (f) the default ") +
608 (CutcellMG::kHostMemory ? "builds no FP32 data, FP64 reported (host)"
609 : "builds FP32 data, FP32 reported (device)"));
610 if (CutcellMG::kHostMemory) // WO-A: 'auto' is FP32 on device backends only
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) +
614 ")");
615 else
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) +
618 ", FP32 reported");
619 }
620 // (f) 'auto' where ineligible is bitwise to 'fp64' (no raise)
621 for (const Problem* P : {&out, &odd, &tiny}) {
622 const Solve a = solve(*P, CutcellMG::kVcycleFp64), b = solve(*P, CutcellMG::kVcycleAuto);
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) +
625 ", FP64 reported");
626 }
627 { // ... also through the D5 deferral (exact outer: the FP64 V-cycle data rebuilt on demand)
628 CutcellMG a, b;
629 setUpExact(a, tiny, CutcellMG::kVcycleFp64, true);
630 setUpExact(b, tiny, CutcellMG::kVcycleAuto, true);
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");
635 }
636 for (const Problem* P : {&out, &odd}) {
637 std::string what;
638 try {
640 } catch (const std::runtime_error& e) {
641 what = e.what();
642 }
643 check(what.find("not eligible here") != std::string::npos,
644 P->name + ": (f) 'fp32' raises at the solve: " + (what.empty() ? "(no raise!)" : what));
645 }
646}
647
648// WO-D5 (§4.4.7): with the FP32 V-cycle and an exact outer the non-bottom levels' FP64 V-cycle
649// data are not allocated (level 0 keeps x and AC; the bottom is whole); they are built on demand,
650// bitwise as the eager build, by a run-time switch to 'fp64' and by a non-exact apply.
651// The FP64 V-cycle data are absent exactly as §4.4.7 lists (true) or all present (false; level
652// 0's rhs only with l0rhs: the A5 V-cycle runs on the caller's vectors and never allocates it)?
653bool fp64Deferred(CutcellMG& mg, bool deferred, bool l0rhs = true) {
654 bool ok = true;
655 const int nl = mg.nLevels();
656 for (int L = 0; L < nl; ++L) {
657 CutcellMG::Level& v = mg.level(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;
665 }
666 return ok;
667}
668// The MG level arrays in bytes per fine cell (FP64 data, openness, FP32 data).
669double mgBytesPerCell(CutcellMG& mg) {
670 double b = 0;
671 for (int L = 0; L < mg.nLevels(); ++L) {
672 CutcellMG::Level& v = mg.level(L);
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));
679 }
680 return b / (double)mg.level(0).n;
681}
682void memoryChecks() {
683 const Problem P = columnProblem();
684 CutcellMG ref64;
685 setUpExact(ref64, P, CutcellMG::kVcycleFp64, true);
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);
689 CutcellMG mg;
690 setUpExact(mg, P, CutcellMG::kVcycleFp32, true);
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) +
695 " B/cell ('fp32')")
696 .c_str(),
697 "(info)");
698 const Solve a = solveOn(mg, P);
699 check(a.fp32 && fp64Deferred(mg, true), "D5 column: the FP32 solve allocates no FP64 data");
700 // run-time switch to 'fp64': built on demand, bitwise a hierarchy that was 'fp64' throughout
702 const Solve b = solveOn(mg, P);
703 check(sameSolve(b, s64) && fp64Deferred(mg, false, /*l0rhs=*/false),
704 "D5 column: switched to 'fp64' == 'fp64' throughout, bitwise, it " + std::to_string(b.it) +
705 " / " + std::to_string(s64.it));
706 // and back to 'fp32': released again, bitwise the first FP32 solve
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");
711 // a non-exact level-0 apply on the deferred hierarchy builds them: bitwise the eager build
712 CutcellMG eager;
713 setUpExact(eager, P, CutcellMG::kVcycleFp32, false);
714 check(fp64Deferred(eager, false), "D5 column, 'fp32' non-exact: every FP64 array allocated");
715 const Solve e = solveOn(eager, P);
716 CutcellMG lazy;
717 setUpExact(lazy, P, CutcellMG::kVcycleFp32, true);
718 lazy.setExactResidual(false);
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");
722 // review E0/X1 finding 1: the same switch, then a projected-guess solve FIRST -- its fused host
723 // guess matvec (matvecGuessDots) is the first reader of level 0's band and must build it
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] + 1e-3 * std::cos(0.23 * (double)i);
727 v2[i] = 0.9 * v2[i] + 1e-3 * std::sin(0.37 * (double)i);
728 }
729 CutcellMG eagerG, lazyG;
730 setUpExact(eagerG, P, CutcellMG::kVcycleFp32, false);
731 setUpExact(lazyG, P, CutcellMG::kVcycleFp32, true);
732 lazyG.setExactResidual(false);
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, "
736 "bitwise, it " +
737 std::to_string(gl.it) + " / " + std::to_string(ge.it) + " (cold " +
738 std::to_string(e.it) + ")");
739}
740
741// WO-D5 memory report (not a check; `test_mg_fp32_vcycle --memory N L`): the MG level arrays in
742// bytes per fine cell on an N^3 periodic all-fluid box with L levels -- 'fp64'; 'fp32' with the
743// FP64 V-cycle data built (a non-exact outer: the WO-D3 layout); 'fp32' deferred (§4.4.7).
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};
747 const Problem P = makeProblem("box", n, n, n, levels, bcPer, ones, ones);
748 const struct {
749 const char* what;
750 int mode;
751 bool exact;
752 } rows[] = {{"'fp64'", CutcellMG::kVcycleFp64, true},
753 {"'fp32', FP64 V-cycle data built (the WO-D3 layout)", CutcellMG::kVcycleFp32, false},
754 {"'fp32', FP64 V-cycle data deferred (WO-D5)", CutcellMG::kVcycleFp32, true}};
755 for (const auto& r : rows) {
756 CutcellMG mg;
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,
759 mgBytesPerCell(mg));
760 }
761}
762
763// G-K1 (doc/vof_projection_distributed_design.md §8, WO-D1): on every non-bottom level, with the
764// ghosts filled by fillWrap, one box pass (plain offsets, the distributed launch form) equals the
765// in-kernel wrap pass on the inner cells, bitwise -- the smoother (each colour) and the residual --
766// and the interior + shell split equals one full-box pass.
767void boxKernelChecks(const Problem& P) {
768 CutcellMG mg;
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) {
773 CutcellMG::Level& lv = mg.level(L);
774 const C3 e = lv.ext, og = CutcellMG::parityOg(lv);
775 const int g = lv.g;
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));
784 }
785 auto up = [&](const std::vector<float>& h, const char* name) {
786 VField f(name, n);
787 auto m = Kokkos::create_mirror_view(f);
788 for (std::size_t i = 0; i < n; ++i)
789 m(i) = h[i];
790 Kokkos::deep_copy(f, m);
791 return f;
792 };
793 VField b = up(hb, "b");
794 const VConst W[3] = {VConst(lv.WX), VConst(lv.WY), VConst(lv.WZ)};
795 const C3 lo{g, g, g}, hi{e.x - g, e.y - g, e.z - g};
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};
798 auto innerSame = [&](VField a, VField c) {
799 const auto ha = toHost(a), hc = toHost(c);
800 long bad = 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;
806 }
807 return bad;
808 };
809 long bad = 0;
810 for (int color : {0, 1}) {
811 VField xw = up(hx, "xw"), xb = up(hx, "xb"), xs = up(hx, "xs");
812 mg.fillWrap(lv, xb);
813 mg.fillWrap(lv, xs);
814 cutcellSmoothColorFp32Wrap(xw, VConst(b), W[0], W[1], W[2], e, lv.inner, og, g, color);
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,
817 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);
820 }
821 VField x = up(hx, "x"), rw("rw", n), rb("rb", n), rs("rs", n);
822 residualFp32Wrap(rw, VConst(x), VConst(b), W[0], W[1], W[2], e, lv.inner, g);
823 mg.fillWrap(lv, x);
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)");
831 }
832}
833
834} // namespace
835
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]));
840 } else {
841 std::printf("mg_fp32_vcycle (doc/vof_projection_cost_design.md §4, G-D1)\n");
842 for (const Problem& P : {columnProblem(), cylProblem(), packProblem(), boxProblem()})
843 boxKernelChecks(P); // G-K1 (doc/vof_projection_distributed_design.md §8, WO-D1)
844 for (const Problem& P : {columnProblem(), cylProblem(), packProblem()}) {
845 dataChecks(P);
846 kernelChecks(P);
847 preconditionerChecks(P, P.name == "column");
848 solverChecks(P);
849 }
850 {
851 const Problem S = slabProblem();
852 preconditionerChecks(S, false);
853 solverChecks(S, /*fcgMustConverge=*/true);
854 }
855 eligibilityChecks();
856 memoryChecks();
857 std::printf("%s (%d failure%s)\n", fails ? "FAIL" : "PASS", fails, fails == 1 ? "" : "s");
858 }
859 Kokkos::finalize();
860 return fails ? 1 : 0;
861}
static C3 parityOg(const Level &lv)
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 precondVcycleFp32(CCField zz, CCField rr, int e, int k, CCField p)
void matvecOverlap(Level &l0, CCField y, CCField v)
const std::vector< double > & healthLog() const
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 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,...
y
Definition cmp.py:24
list bottom
Definition mgd_case.py:454
constexpr bool kHost
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)
int m
Definition post.py:35
B
Definition post.py:34
t
Definition post.py:21
one(n, rho_ratio, mu_ratio, stress, steps, adv, dtf, grav=False, sigma="none")
worst
Definition stab5.py:6
P
Definition stab5.py:16
exact
Definition summ.py:10
dict rows
Definition summarize.py:38
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 int fails
static bool same(double a, double b)