49#include <Kokkos_Core.hpp>
60constexpr bool kHost = std::is_same_v<CCMem, Kokkos::HostSpace>;
63 auto h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), f);
64 return std::vector<double>(
h.data(),
h.data() +
h.extent(0));
66std::vector<double> toHostOp(
FPV f) {
67 auto h = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), f);
68 std::vector<double>
v(
h.extent(0));
69 for (std::size_t i = 0;
i <
v.size(); ++
i)
75 auto h = Kokkos::create_mirror_view(f);
76 for (std::size_t i = 0;
i <
v.size(); ++
i)
78 Kokkos::deep_copy(f, h);
82bool sameBits(
const V& a,
const V& b) {
83 auto ha = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), a);
84 auto hb = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), b);
85 return ha.span() ==
hb.span() &&
86 std::memcmp(
ha.data(),
hb.data(),
ha.span() *
sizeof(
typename V::value_type)) == 0;
92 C3 e{0, 0, 0}, n{0, 0, 0};
94 std::vector<double> AC, AX, AY, AZ;
95 std::vector<int> comp;
97 std::vector<double> d, s;
98 long idx(
int x,
int y,
int z)
const {
99 return (
long)(
x + 1) + (
long)(
y + 1) * e.x + (
long)(
z + 1) * e.x * e.y;
102 bool face(
const int c[3],
int f6,
int nb[3],
double& af)
const {
103 const int a = f6 >> 1,
side = f6 & 1, nn[3] = {n.x, n.y, n.z};
107 int v =
c[
a] + (
side ? 1 : -1);
108 if (v < 0 || v >= nn[a]) {
111 v = (
v + nn[
a]) % nn[a];
114 const long i = idx(c[0], c[1], c[2]);
115 const long st = (
a == 0) ? 1 : (
a == 1 ? e.
x : (long)e.
x * e.
y);
116 const std::vector<double>&
F = (
a == 0) ? AX : (
a == 1 ? AY : AZ);
121 void forCells(Fn fn)
const {
122 for (
int z = 0;
z < n.z; ++
z)
123 for (
int y = 0;
y < n.y; ++
y)
124 for (
int x = 0;
x < n.x; ++
x) {
125 const int c[3] = {
x,
y,
z};
130 std::vector<long> parent(AC.size());
131 for (std::size_t i = 0;
i < parent.size(); ++
i)
133 auto find = [&](
long a) {
134 while (parent[a] != a)
135 a = parent[
a] = parent[parent[
a]];
138 forCells([&](
const int c[3],
long i) {
139 if (!(AC[i] > 1e-30))
141 for (
int f6 = 0; f6 < 6; ++f6) {
144 if (face(c, f6, nb, af) && af < 0.0) {
145 const long j = idx(nb[0], nb[1], nb[2]);
147 parent[find(i)] = find(j);
151 comp.assign(AC.size(), -1);
152 std::vector<int> id(AC.size(), -1);
154 forCells([&](
const int*,
long i) {
155 if (!(AC[i] > 1e-30))
157 const long r = find(i);
162 d.assign(AC.size(), 0.0);
163 s.assign(AC.size(), 0.0);
164 forCells([&](
const int c[3],
long i) {
166 for (
int f6 = 0; f6 < 6; ++f6) {
169 if (face(c, f6, nb, af))
173 s[
i] = (comp[
i] >= 0 && d[
i] > 0.0) ? 1.0 / std::sqrt(d[i]) : 0.0;
177 double applyResummed(
const std::vector<double>& x,
const int c[3],
long i)
const {
178 double v = d[
i] *
x[
i];
179 for (
int f6 = 0; f6 < 6; ++f6) {
182 if (face(c, f6, nb, af))
183 v += af *
x[idx(nb[0], nb[1], nb[2])];
188 double applyStored(
const std::vector<double>& x,
const int c[3],
long i)
const {
189 const int nn[3] = {n.x, n.y, n.z};
190 auto at = [&](
int dx,
int dy,
int dz) {
191 const int q[3] = {(
c[0] +
dx + nn[0]) % nn[0], (c[1] + dy + nn[1]) % nn[1],
192 (
c[2] +
dz + nn[2]) % nn[2]};
193 return x[idx(q[0], q[1], q[2])];
195 const long sy = e.x,
sz = (long)e.x * e.y;
196 return AC[
i] *
x[
i] + AX[
i + 1] * at(1, 0, 0) + AX[
i] * at(-1, 0, 0) +
197 AY[
i + sy] * at(0, 1, 0) + AY[
i] * at(0, -1, 0) + AZ[
i +
sz] * at(0, 0, 1) +
198 AZ[
i] * at(0, 0, -1);
201 void means(
const std::vector<double>& x,
double& meanMax,
double& xmax,
double& xsolid)
const {
202 std::vector<long double> sum(nc, 0.0L);
203 std::vector<long> cnt(nc, 0);
204 xmax = xsolid = meanMax = 0.0;
205 forCells([&](
const int*,
long i) {
207 sum[comp[
i]] += (
long double)x[i];
209 xmax = std::max(xmax, std::fabs(x[i]));
211 xsolid = std::max(xsolid, std::fabs(x[i]));
214 for (
int c = 0;
c < nc; ++
c)
215 meanMax = std::max(meanMax, (
double)std::fabs(sum[c] / (
long double)cnt[c]));
221std::vector<double> compatibleRhs(
const HostBottom& H,
unsigned seed) {
222 std::vector<double>
v(
H.AC.size(), 0.0);
223 std::mt19937
rng(seed);
224 std::uniform_real_distribution<double>
U(-1.0, 1.0);
225 std::vector<long double> sum(
H.nc, 0.0L);
226 std::vector<long> cnt(
H.nc, 0);
227 H.forCells([&](
const int*,
long i) {
228 const double r =
U(rng);
229 if (
H.comp[i] >= 0) {
235 H.forCells([&](
const int*,
long i) {
237 v[
i] = (double)((
long double)v[i] - sum[
H.comp[i]] / (
long double)cnt[
H.comp[i]]);
242double residualResummed(
const HostBottom& H,
const std::vector<double>& x,
243 const std::vector<double>& r) {
244 double rn = 0.0, r0 = 0.0;
245 H.forCells([&](
const int c[3],
long i) {
248 r0 = std::max(r0, std::fabs(r[i]));
249 rn = std::max(rn, std::fabs(
H.applyResummed(x, c, i) - r[i]));
257std::vector<double> denseReference(
const HostBottom& H,
const BottomPlanes& pl,
258 const std::vector<double>& r) {
259 const int nn = pl.
P * pl.
b;
260 auto plane = [&](
const int c[3]) {
return c[pl.
s]; };
261 auto fidx = [&](
const int c[3]) {
return c[pl.
s] * pl.
b +
c[pl.
a0] + pl.
n[pl.
a0] *
c[pl.
a1]; };
262 std::vector<int>
kc(
H.nc, -1),
mc(
H.nc, 0);
263 H.forCells([&](
const int c[3],
long i) {
265 kc[
H.comp[
i]] = std::max(kc[
H.comp[i]], plane(c));
267 H.forCells([&](
const int c[3],
long i) {
268 if (
H.comp[i] >= 0 && plane(c) == kc[
H.comp[i]])
271 std::vector<double>
A((std::size_t)nn * nn, 0.0), rhs(nn, 0.0);
272 std::vector<long> ext(nn);
273 H.forCells([&](
const int c[3],
long i) {
274 const int fi = fidx(c);
276 rhs[fi] =
H.s[
i] *
r[
i];
277 A[(std::size_t)fi * nn + fi] += 1.0;
278 for (
int f6 = 0; f6 < 6; ++f6) {
281 if (
H.face(c, f6, nb, af)) {
282 const long j =
H.idx(nb[0], nb[1], nb[2]);
283 A[(std::size_t)fi * nn + fidx(nb)] +=
H.s[
i] * af *
H.s[
j];
287 for (
int fi = 0; fi < nn; ++fi)
288 for (
int fj = 0; fj < nn; ++fj) {
289 const long i = ext[fi],
j = ext[fj];
290 const int ci =
H.comp[
i];
291 if (ci >= 0 &&
H.comp[j] == ci && fi / pl.
b == kc[ci] && fj / pl.
b == kc[ci])
292 A[(std::size_t)fi * nn + fj] += 1.0 / mc[ci];
294 for (
int j = 0;
j < nn; ++
j) {
295 double p =
A[(std::size_t)j * nn + j];
296 for (
int m = 0;
m <
j; ++
m)
297 p -= A[(std::size_t)
j * nn +
m] *
A[(std::size_t)j * nn + m];
298 const double ljj = std::sqrt(p);
299 A[(std::size_t)j * nn + j] = ljj;
300 for (
int i = j + 1;
i < nn; ++
i) {
301 double v =
A[(std::size_t)i * nn + j];
302 for (
int m = 0;
m <
j; ++
m)
303 v -= A[(std::size_t)
i * nn +
m] *
A[(std::size_t)j * nn + m];
304 A[(std::size_t)i * nn + j] = v / ljj;
307 for (
int i = 0;
i < nn; ++
i) {
309 for (
int m = 0;
m <
i; ++
m)
310 v -= A[(std::size_t)
i * nn +
m] * rhs[
m];
311 rhs[
i] =
v /
A[(std::size_t)i * nn + i];
313 for (
int i = nn - 1;
i >= 0; --
i) {
315 for (
int m = i + 1;
m < nn; ++
m)
316 v -= A[(std::size_t)
m * nn +
i] * rhs[
m];
317 rhs[
i] =
v /
A[(std::size_t)i * nn + i];
319 std::vector<double>
x(
H.AC.size(), 0.0);
320 for (
int fi = 0; fi < nn; ++fi)
321 x[ext[fi]] = (
H.comp[ext[fi]] >= 0) ?
H.s[ext[fi]] * rhs[fi] : 0.0;
322 std::vector<long double> sum(
H.nc, 0.0L);
323 std::vector<long> cnt(
H.nc, 0);
324 H.forCells([&](
const int*,
long i) {
325 if (
H.comp[i] >= 0) {
326 sum[
H.comp[
i]] +=
x[
i];
330 H.forCells([&](
const int*,
long i) {
332 x[
i] = (double)((
long double)x[i] - sum[
H.comp[i]] / (
long double)cnt[
H.comp[i]]);
345 const int nx =
cs.nx,
ny =
cs.ny, nz =
cs.nz;
348 mg.
init(nx, ny, nz, 2);
350 const C3 e{nx + 2,
ny + 2, nz + 2};
351 const std::size_t
n = (std::size_t)
e.x *
e.y *
e.z;
352 std::vector<double> hx(n), hy(n), hz(n);
353 const double lr = std::log(50.0),
k = 2.0 * M_PI;
354 for (
int z = 0;
z <
e.z; ++
z)
355 for (
int y = 0;
y <
e.y; ++
y)
356 for (
int x = 0;
x <
e.x; ++
x) {
357 const std::size_t
i =
x + (std::size_t)y *
e.x + (std::size_t)
z *
e.x *
e.y;
358 const double s = 0.5 + 0.5 * std::sin(k * x / nx) * std::cos(k * (y + 0.5 * z) / ny);
359 const double t = 0.5 + 0.5 * std::cos(k * (x + z) / nz);
360 hx[
i] = std::exp(lr * s) / 50.0;
361 hy[
i] = std::exp(lr * t) / 50.0;
362 hz[
i] = std::exp(lr * (1.0 - s)) / 50.0;
363 const int ix =
x - 1, iy =
y - 1, iz =
z - 1;
365 if (ix == 0 || ix == 16 || ix == 6 || ix == 8)
367 if (ix == 6 || ix == 7)
371 const bool inX = ix >= 20 && ix < 24, inY = iy >= 10 && iy < 12, inZ = iz >= 6 && iz < 8;
372 if ((ix == 20 || ix == 24) && inY && inZ)
374 if ((iy == 10 || iy == 12) && inX && inZ)
376 if ((iz == 6 || iz == 8) && inX && inY)
387 for (
int f = 0;
f < 6; ++
f)
389 H.AC = toHostOp(
bt.AC);
390 H.AX = toHostOp(
bt.AFX);
391 H.AY = toHostOp(
bt.AFY);
392 H.AZ = toHostOp(
bt.AFZ);
397 "[%s] bottom %dx%dx%d: slow axis %d, P = %d, b = %d, border %d; %d fluid component(s) "
403 printf(
"[%s] FAIL: no plane ordering, or the device labels disagree\n",
cs.name);
406 const std::vector<double>
r = compatibleRhs(H, 7);
414 const double res = residualResummed(H, x, r);
416 H.means(x, mm, xm, xs);
417 const bool ok =
res <= 1
e-12 &&
mm <= 1
e-15 * xm && xs == 0.0;
419 "[%s] U1 FP64 M: residual %.3e, max|component mean| %.2e max|x|, max|x| solid %.1e -> "
421 cs.name, res, mm / xm, xs, ok ?
"ok" :
"FAIL");
424 std::vector<double> zf;
430 const double res = residualResummed(H, zf, r);
431 const bool ok =
res <= 1
e-3;
432 printf(
"[%s] U2 FP32 M: residual %.3e -> %s\n",
cs.name, res, ok ?
"ok" :
"FAIL");
436 std::vector<int> Ts =
437 kHost ? std::vector<int>{1, 2, 4, 8} : std::vector<int>{32, 64, 128, 256, 0};
442 std::vector<int> used;
448 if ((T != 0 && (Tf != T || Ta != T))) {
449 printf(
"[%s] U3 team size %d not available (factor %d, solve %d): skipped\n",
cs.name, T,
456 Kokkos::deep_copy(zref, z);
463 printf(
"[%s] U3 team size %d differs from %d\n",
cs.name, Ta, Tref);
466 ok =
ok && used.size() >= 2;
467 printf(
"[%s] U3 team sizes",
cs.name);
470 printf(
": factor + M %s -> %s\n", ok ?
"bitwise identical" :
"DIFFER (or < 2 sizes)",
476 std::vector<int> used;
477 auto agree = [&](
const auto&
A,
const auto&
B) {
481 for (
int T : {1, 2, 4}) {
483 int Th = 0,
Tt = 0, Th64 = 0, Tt64 = 0;
488 if (Th != T || Tt != T || Th64 != T || Tt64 != T)
494 printf(
"[%s] U8 team size %d, first pivot floor %.0e: host schedule differs\n",
cs.name,
499 ok =
ok && used.size() >= 2;
501 "[%s] U8 host schedule vs team algorithm (FP32 + FP64, with and without a restart), "
506 printf(
": %s -> %s\n", ok ?
"bitwise identical" :
"DIFFER (or < 2 sizes)", ok ?
"ok" :
"FAIL");
510 std::vector<double> hr(
bt.n, 0.0);
512 std::uniform_real_distribution<double>
U(-1.0, 1.0);
513 H.forCells([&](
const int*,
long i) { hr[
i] =
U(rng); });
517 std::vector<int> used;
518 auto check = [&](
const auto&
D,
double tau,
bool noMean) {
521 for (
int T : {1, 2, 4}) {
526 std::find(used.begin(), used.end(), T) == used.end())
530 printf(
"[%s] U9 lane M (team %d) differs from team M (team %d), floor %.0e, noMean %d\n",
531 cs.name, T, To, tau, noMean ? 1 : 0);
539 auto st = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
D.stat);
541 printf(
"[%s] U9 factor floor %.0e: ok %d, restarts %d (unexpected)\n",
cs.name, tau,
st(0),
545 for (
bool noMean : {
true,
false}) {
546 check(D, tau, noMean);
547 check(D64, tau, noMean);
550 ok =
ok && used.size() >= 2;
552 "[%s] U9 host lane M vs team M (FP32 + FP64, both shift paths, +/- mean removal), %d "
553 "comparisons, team sizes",
557 printf(
": %s -> %s\n", ok ?
"bitwise identical" :
"DIFFER (or < 2 sizes)", ok ?
"ok" :
"FAIL");
561 std::vector<double>
hb(
bt.n, 0.0);
563 std::uniform_real_distribution<double>
U(-1.0, 1.0);
564 H.forCells([&](
const int*,
long i) {
hb[
i] =
U(rng); });
567 int itRef = -1, Tref = -1;
569 std::vector<int> used;
570 for (
int T : {1, 2, 4, 8}) {
571 Kokkos::deep_copy(
bt.rhs, b);
575 printf(
"[%s] U3 FCG team size %d not available (ran %d): skipped\n",
cs.name, T, Tu);
580 Kokkos::deep_copy(xref,
bt.x);
587 printf(
"[%s] U3 FCG team size %d differs from %d (iterations %d vs %d)\n",
cs.name, T, Tref,
592 printf(
"[%s] U3 FCG team sizes",
cs.name);
595 printf(
": the whole solve (%d iterations) %s -> %s\n", itRef,
596 ok ?
"bitwise identical" :
"DIFFERS (or < 2 sizes)", ok ?
"ok" :
"FAIL");
599 auto fcg = [&](
const char*
tag,
int itMax,
int restartsWant) {
600 std::vector<double>
hb(
bt.n, 0.0);
601 std::mt19937
rng(11);
602 std::uniform_real_distribution<double>
U(-1.0, 1.0);
603 H.forCells([&](
const int*,
long i) {
hb[
i] =
U(rng); });
606 Kokkos::deep_copy(
bt.rhs, b);
612 std::vector<long double> bsum(
H.nc, 0.0L);
613 std::vector<long> bcnt(
H.nc, 0);
614 H.forCells([&](
const int*,
long i) {
615 if (
H.comp[i] >= 0) {
616 bsum[
H.comp[
i]] +=
hb[
i];
620 double r0 = 0.0, rn = 0.0;
621 H.forCells([&](
const int c[3],
long i) {
625 (double)((
long double)hb[i] - bsum[
H.comp[i]] / (
long double)bcnt[
H.comp[i]]);
626 r0 = std::max(r0, std::fabs(bp));
627 rn = std::max(rn, std::fabs(bp -
H.applyStored(x, c, i)));
630 H.means(x, mm, xm, xs);
631 const bool ok =
it >= 1 &&
it <= itMax && !flag && restarts == restartsWant &&
632 rn <= 10.0 *
kBottomTau * r0 && xs == 0.0 &&
mm <= 4
e-16 * xm;
634 "[%s] %s FCG: %d iteration(s), restarts %d, flag %d, true residual %.3e r0, max|x| "
635 "solid %.1e, max|component mean| %.2e max|x| -> %s\n",
636 tag,
cs.name, it, restarts, flag ? 1 : 0, rn / r0, xs, mm / xm, ok ?
"ok" :
"FAIL");
639 fails += fcg(
"U4", 3, 0);
646 auto hA = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(),
bt.AFX);
647 const long i0 = 1 + (long)1 *
bt.ext.x + (
long)1 *
bt.ext.x *
bt.ext.y;
649 while (!(hA(i) < 0) && i < (
long)
bt.n - 1)
651 const auto keep = hA(i);
652 hA(i) = std::numeric_limits<MReal>::quiet_NaN();
653 Kokkos::deep_copy(
bt.AFX, hA);
655 Kokkos::deep_copy(
bt.rhs, rd);
661 xmax = std::max(xmax, std::fabs(v));
662 const bool ok = flag && xmax == 0.0;
663 printf(
"[%s] U6 NaN coefficient: flag %d, max|x| %.1e -> %s\n",
cs.name, flag ? 1 : 0, xmax,
667 Kokkos::deep_copy(
bt.AFX, hA);
670 if (
bt.inner.x == 16 &&
bt.inner.y == 12 &&
bt.inner.z == 8) {
671 const std::vector<double>
xr = denseReference(H, pl, r);
672 double dm = 0.0,
am = 0.0;
673 H.forCells([&](
const int*,
long i) {
674 am = std::max(am, std::fabs(xr[i]));
675 dm = std::max(dm, std::fabs(zf[i] - xr[i]));
677 const bool ok =
dm <= 1
e-3 *
am;
678 printf(
"[%s] U7 FP32 M vs dense FP64 A'^-1: max diff %.3e max|x_ref| -> %s\n",
cs.name, dm / am,
687int main(
int argc,
char** argv) {
688 Kokkos::initialize(argc, argv);
691 const Case cases[] = {
692 {
"periodic", {0, 0, 0, 0, 0, 0}, 32, 24, 16,
false,
false},
693 {
"periodic-P2", {0, 0, 0, 0, 0, 0}, 4, 4, 4,
false,
false},
694 {
"walls-y", {0, 0, 1, 1, 0, 0}, 32, 24, 16,
false,
false},
695 {
"walls-z", {0, 0, 0, 0, 1, 1}, 32, 24, 16,
false,
false},
696 {
"solids", {0, 0, 0, 0, 0, 0}, 32, 24, 16,
true,
false},
697 {
"solids-walls-y", {0, 0, 1, 1, 0, 0}, 32, 24, 16,
true,
false},
698 {
"pocket", {0, 0, 0, 0, 0, 0}, 32, 24, 16,
false,
true},
700 for (
const Case& c : cases)
704 printf(
fails ?
"bottom_direct: FAIL (%d)\n" :
"bottom_direct: PASS\n",
fails);
705 return fails ? 1 : 0;
const BottomPlanes & directPlanes() const
void directMarkStaleForTest()
int directSolveTeamForTest(int team, int *Tused=nullptr)
int bottomComponents() const
const char * directBottomIneligible() const
void directPivotTolForTest(double t)
void setOpenness(CCConst ox, CCConst oy, CCConst oz, double idx2, double idy2, double idz2)
void bottomForceForTest()
void setBoundaryConditions(const int bc[6])
int directApplyForTest(const BottomDirect< FR > &D, CCField z, CCField r, int team, bool noMean=false, bool teamAlgorithm=false)
void setAgglomerationMode(int mode)
int directRestarts() const
void init(int nx, int ny, int nz, int nLevels)
BottomDirect< FR > directFactorForTest(int team, double tauPiv0=kBottomPivotTol, int *Tused=nullptr, bool teamAlgorithm=false)
flow — portable (Kokkos) geometric multigrid for the cut-cell (variable-openness) pressure Poisson.
Kokkos::View< const double *, CCMem > CCConst
constexpr double kBottomTau
Kokkos::View< double *, CCMem > CCField
Kokkos::View< MReal *, CCMem > FPV
constexpr double kBottomPivotTol
CCField toDevice(const std::vector< double > &v, const char *name)
std::vector< typename V::non_const_value_type > toHost(const V &f)
static int runCase(const Config &c, const peclet::core::decomp::BlockDecomposer< 3 > &dec, int rank, int size)
static void check(bool ok, const std::string &what)
static bool sameBits(const std::vector< double > &a, const std::vector< double > &b)
static constexpr double F
static constexpr double U
static bool same(double a, double b)
static void build(IbmSolver &s, std::size_t cells)