24#include <Kokkos_Core.hpp>
38void check(
bool ok,
const std::string& what) {
39 std::printf(
" %-78s %s\n",
what.c_str(), ok ?
"PASS" :
"FAIL");
44std::string
num(
double v) {
46 std::snprintf(b,
sizeof b,
"%.3g", v);
50constexpr int NX = 32,
NY = 32;
51constexpr int SX = 5, SY = 7;
53Problem makeLineProblem(
bool walls,
int NZ) {
55 P.name = walls ?
"z walls" :
"z periodic";
61 P.bc[4] =
P.bc[5] = 1;
63 const std::size_t
n = (std::size_t)
e.x *
e.y *
e.z;
67 std::mt19937
rng(20261026u);
68 std::uniform_real_distribution<double> open(0.05, 1.0), u01(0.0, 1.0);
69 auto face = [&] {
return u01(rng) < 0.05 ? 0.0 : open(rng); };
70 for (
int z = 0;
z <
NZ; ++
z)
71 for (
int y = 0;
y <
NY; ++
y)
72 for (
int x = 0;
x <
NX; ++
x) {
73 const long i =
P.idx(x, y, z);
78 for (
int z = 0;
z <
NZ; ++
z) {
79 P.ox[
P.idx(
SX, SY, z)] = 0.0;
80 P.ox[
P.idx((
SX + 1) %
NX, SY, z)] = 0.0;
81 P.oy[
P.idx(
SX, SY, z)] = 0.0;
82 P.oy[
P.idx(
SX, (SY + 1) %
NY, z)] = 0.0;
88bool denseSolve(std::vector<long double> M, std::vector<long double> r,
int m,
89 std::vector<long double>& d) {
90 for (
int c = 0;
c <
m; ++
c) {
92 for (
int k = c + 1;
k <
m; ++
k)
93 if (std::fabs(M[k * m + c]) > std::fabs(M[p * m + c]))
95 if (M[p * m + c] == 0.0L)
98 for (
int j = 0;
j <
m; ++
j)
99 std::swap(M[c * m + j], M[p * m + j]);
100 std::swap(r[c], r[p]);
102 for (
int k = c + 1;
k <
m; ++
k) {
103 const long double f =
M[
k *
m +
c] /
M[
c *
m +
c];
104 for (
int j = c;
j <
m; ++
j)
105 M[k * m + j] -= f * M[c * m + j];
110 for (
int k = m - 1;
k >= 0; --
k) {
111 long double s =
r[
k];
112 for (
int j = k + 1;
j <
m; ++
j)
113 s -= M[k * m + j] * d[j];
123 std::vector<long double> AC, AF[3];
124 long idx(
int lx,
int ly,
int lz)
const {
return (
long)lx + (long)ly * sy + (
long)lz * sz; }
125 bool decoupled(
long i,
bool fp32)
const {
return fp32 ? AC[
i] == 0.0L : AC[
i] < 1e-30L; }
132double comparePass(
const HostOp& H,
const std::vector<XT>& b,
const std::vector<XT>& x0,
133 const std::vector<XT>& x1,
bool periodic,
bool fp32,
int color,
bool& otherSame,
134 bool& finite,
int& nCols) {
135 const C3 e =
H.e,
n =
H.n;
136 const int g = 1,
m =
n.z;
142 for (std::size_t i = 0;
i < x1.size(); ++
i)
143 finite = finite && std::isfinite((
double)x1[i]);
144 for (
int ly = g; ly <
e.y -
g; ++ly)
145 for (
int lx = g; lx <
e.x -
g; ++lx) {
146 if (((lx + ly) & 1) != color) {
147 for (
int lz = g; lz <
e.z -
g; ++lz) {
148 const long i =
H.idx(lx, ly, lz);
149 otherSame = otherSame && std::memcmp(&x0[i], &x1[i],
sizeof(XT)) == 0;
153 if (lx - g ==
SX && ly - g == SY)
155 std::vector<long double>
M((std::size_t)m * m, 0.0L),
r(m, 0.0L),
d;
156 for (
int k = 0;
k <
m; ++
k) {
157 const int lz =
g +
k;
158 const long i =
H.idx(lx, ly, lz);
159 if (
H.decoupled(i, fp32)) {
166 Ax =
H.AC[
i] *
x0[
i] +
H.AF[0][
i] *
x0[
w.xm] +
H.AF[0][
i + 1] *
x0[
w.xp] +
167 H.AF[1][
i] *
x0[
w.ym] +
H.AF[1][
i +
H.sy] *
x0[
w.yp] +
H.AF[2][
i] *
x0[
w.zm] +
168 H.AF[2][
i +
t] *
x0[
w.zp];
170 Ax =
H.AC[
i] *
x0[
i] +
H.AF[0][
i + 1] *
x0[
w.xp] +
H.AF[0][
i] *
x0[
w.xm] +
171 H.AF[1][
i +
H.sy] *
x0[
w.yp] +
H.AF[1][
i] *
x0[
w.ym] +
H.AF[2][
i +
t] *
x0[
w.zp] +
172 H.AF[2][
i] *
x0[
w.zm];
174 r[
k] = (
long double)b[i] - Ax;
176 const long double lo =
H.AF[2][
i], up =
H.AF[2][
i +
t];
182 M[
k *
m +
k + 1] += up;
186 if (!denseSolve(M, r, m, d)) {
190 long double dmax = 0.0L, emax = 0.0L;
191 for (
int k = 0;
k <
m; ++
k) {
192 const long i =
H.idx(lx, ly, g + k);
193 const long double got = (
long double)x1[i] - (
long double)
x0[
i];
194 dmax = std::max(dmax, std::fabs(d[k]));
195 emax = std::max(emax, std::fabs(got - d[k]));
199 worst = std::max(worst, (
double)(emax / dmax));
204void runCase(
bool walls,
int nz) {
205 const Problem P = makeLineProblem(walls, nz);
206 std::printf(
"%s (32x32x%d, s = z)\n",
P.name.c_str(), nz);
208 mg.
init(
P.nx,
P.ny,
P.nz,
P.levels);
215 const std::size_t
n =
lv.n;
218 if (
lv.rhs.extent(0) != n)
228 H.sz = (long)
e.x *
e.y;
232 H.AC.assign(ac.begin(), ac.end());
233 H.AF[0].assign(
fx.begin(),
fx.end());
234 H.AF[1].assign(fy.begin(), fy.end());
235 H.AF[2].assign(fz.begin(), fz.end());
238 for (
int ly = 1; ly <
e.y - 1; ++ly)
239 for (
int lx = 1; lx <
e.x - 1; ++lx)
240 zero = zero && fz[
H.idx(lx, ly, 1)] == 0 && fz[
H.idx(lx, ly,
e.z - 1)] == 0;
241 check(zero,
"FP64 wall faces: AF_z = 0 on both wall planes");
245 std::mt19937
rng(7u);
246 std::uniform_real_distribution<double>
U(-1.0, 1.0);
248 for (
int color = 0; color < 2; ++color) {
249 std::vector<double> hx(n, 0.0),
hb(n, 0.0);
250 for (std::size_t i = 0;
i <
n; ++
i) {
254 Kokkos::deep_copy(
lv.x, Kokkos::View<double*, Kokkos::HostSpace>(hx.data(), n));
255 Kokkos::deep_copy(
lv.rhs, Kokkos::View<double*, Kokkos::HostSpace>(
hb.data(), n));
260 std::vector<double>
x0 = hx;
261 for (
int lz = 0; lz <
e.z; ++lz)
262 for (
int ly = 0; ly <
e.y; ++ly)
263 for (
int lx = 0; lx <
e.x; ++lx) {
265 lx >= 1 && lx <
e.x - 1 && ly >= 1 && ly <
e.y - 1 && lz >= 1 && lz <
e.z - 1;
267 x0[
H.idx(lx, ly, lz)] = x1[
H.idx(lx, ly, lz)];
271 const double err = comparePass(H, hb, x0, x1, periodic,
false, color,
same, finite, cols);
272 check(err <= 1e-12,
"FP64 colour " + std::to_string(color) +
": " + std::to_string(cols) +
273 " columns = dense solve, max rel err " +
num(err) +
" <= 1e-12");
274 check(
same,
"FP64 colour " + std::to_string(color) +
": the other colour bitwise unchanged");
275 check(finite,
"FP64 colour " + std::to_string(color) +
": all finite (sealed column included)");
280 std::vector<VReal> W[3];
281 for (
int a = 0;
a < 3; ++
a) {
283 for (std::size_t i = 0;
i <
n; ++
i)
284 W[a][i] =
static_cast<VReal>(-(
double)
H.AF[a][i]);
286 auto toDevF = [&](
const std::vector<VReal>&
v) {
288 auto h = Kokkos::create_mirror_view(f);
289 for (std::size_t i = 0;
i <
v.size(); ++
i)
291 Kokkos::deep_copy(f, h);
294 lv.WX = toDevF(W[0]);
295 lv.WY = toDevF(W[1]);
296 lv.WZ = toDevF(W[2]);
304 for (
int ly = 1; ly <
e.y - 1; ++ly)
305 for (
int lx = 1; lx <
e.x - 1; ++lx)
306 zero = zero && W[2][
H.idx(lx, ly, 1)] == 0.0f && W[2][
H.idx(lx, ly,
e.z - 1)] == 0.0f;
307 check(zero,
"FP32 wall weights: W_z = 0 on both wall planes");
310 for (
int lz = 0; lz <
e.z; ++lz)
311 for (
int ly = 0; ly <
e.y; ++ly)
312 for (
int lx = 0; lx <
e.x; ++lx) {
313 const long i =
H.idx(lx, ly, lz);
315 lx >= 1 && lx <
e.x - 1 && ly >= 1 && ly <
e.y - 1 && lz >= 1 && lz <
e.z - 1;
318 inner ? ((((W[0][
i + 1] + W[0][
i]) + W[1][
i +
H.sy]) + W[1][
i]) + W[2][
i +
H.sz]) +
321 H32.AC[
i] = (
long double)D;
322 for (
int a = 0;
a < 3; ++
a)
323 H32.AF[a][i] = -(
long double)W[
a][
i];
325 for (
int color = 0; color < 2; ++color) {
326 std::vector<VReal> hx(n, 0.0f),
hb(n, 0.0f);
327 for (std::size_t i = 0;
i <
n; ++
i) {
328 hx[
i] =
static_cast<VReal>(
U(rng));
331 Kokkos::deep_copy(
lv.xf, Kokkos::View<VReal*, Kokkos::HostSpace>(hx.data(), n));
332 Kokkos::deep_copy(
lv.rhsf, Kokkos::View<VReal*, Kokkos::HostSpace>(
hb.data(), n));
338 const double err = comparePass(H32, hb, hx, x1, periodic,
true, color,
same, finite, cols);
339 check(err <= 1e-4,
"FP32 colour " + std::to_string(color) +
": " + std::to_string(cols) +
340 " columns = dense solve, max rel err " +
num(err) +
" <= 1e-4");
341 check(
same,
"FP32 colour " + std::to_string(color) +
": the other colour bitwise unchanged");
343 "FP32 colour " + std::to_string(color) +
": all finite (sealed column included)");
345 for (
const VReal c : {1.0f, -3.7f, 1e5f}) {
346 std::vector<VReal> hx(n, c),
hb(n, 0.0f);
347 Kokkos::deep_copy(
lv.xf, Kokkos::View<VReal*, Kokkos::HostSpace>(hx.data(), n));
348 Kokkos::deep_copy(
lv.rhsf, Kokkos::View<VReal*, Kokkos::HostSpace>(
hb.data(), n));
353 const bool same = std::memcmp(x1.data(), hx.data(), n *
sizeof(
VReal)) == 0;
354 check(
same,
"FP32 x = " +
num(c) +
" * 1, b = 0: both colours bitwise unchanged");
361int main(
int argc,
char** argv) {
362 Kokkos::initialize(argc, argv);
364 for (
int nz : {25, 2, 3}) {
370 std::printf(
"mg_line_relax: %s (%d failure%s)\n",
fails ?
"FAIL" :
"PASS",
fails,
371 fails == 1 ?
"" :
"s");
372 return fails ? 1 : 0;
void lineSmoothColorFp32(Level &lv, int s, int color)
void setOpenness(CCConst ox, CCConst oy, CCConst oz, double idx2, double idy2, double idz2)
void lineSmoothColor(Level &lv, int s, int color)
void setBoundaryConditions(const int bc[6])
bool linePeriodic(int s) const
void init(int nx, int ny, int nz, int nLevels)
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 finite(const char *who, const std::string &name, double v, const char *dim)
void zero(const ScalarVec &y)
Kokkos::View< const double *, CCMem > CCConst
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
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 constexpr double U
static bool same(double a, double b)