24#include <Kokkos_Core.hpp>
38void check(
bool ok,
const std::string& what) {
39 int bad =
ok ? 0 : 1, any = 0;
40 MPI_Allreduce(&bad, &any, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
42 std::printf(
" %-96s %s\n",
what.c_str(), any ?
"FAIL" :
"PASS");
47std::string
num(
double v) {
49 std::snprintf(b,
sizeof b,
"%.3e", v);
53constexpr double kBoundSmoothed = 1
e-14, kBoundAgglomerated = 1
e-8;
56 unsigned long s = (
unsigned long)key * 6364136223846793005ul + 1442695040888963407ul;
58 s *= 0xff51afd7ed558ccdul;
60 return (
double)(
s >> 11) / (
double)(1ul << 52) - 1.0;
66 mg.initMpi(
P.nx,
P.ny,
P.nz,
P.levels, MPI_COMM_WORLD);
69 const C3 e = l0.ext,
og = l0.
og;
71 const std::size_t
n = (std::size_t)
e.x *
e.y *
e.z;
72 std::vector<double>
ox(n, 0.0), oy(n, 0.0),
oz(n, 0.0);
73 for (
int z = g;
z <
e.z -
g; ++
z)
74 for (
int y = g;
y <
e.y -
g; ++
y)
75 for (
int x = g;
x <
e.x -
g; ++
x) {
76 const long li = (long)x + (
long)
y *
e.x + (long)z * (
long)
e.x *
e.y;
77 const long gi =
P.idx(x - g +
og.x, y - g +
og.y, z - g +
og.z);
87void forInner(
const Level& l0,
F f) {
90 for (
int z = g;
z <
e.z -
g; ++
z)
91 for (
int y = g;
y <
e.y -
g; ++
y)
92 for (
int x = g;
x <
e.x -
g; ++
x)
93 f(x - g + l0.
og.
x, y - g + l0.
og.
y, z - g + l0.
og.
z,
94 (
long)x + (
long)y *
e.x + (
long)z * (
long)
e.x *
e.y);
101 auto h = Kokkos::create_mirror_view(f);
102 auto ac = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), l.
AC);
103 for (std::size_t i = 0;
i <
h.extent(0); ++
i)
105 forInner(l, [&](
int x,
int y,
int z,
long i) {
106 h(i) = (double)ac(i) > 1
e-30 ?
val(seed * 7919L + x + 1000L * y + 1000000L * z) : 0.0;
108 Kokkos::deep_copy(f, h);
113 const std::size_t
n = mg.
level(0).
n;
114 CCField s0(
"s0", n),
s1(
"s1", n),
s2(
"s2", n),
s3(
"s3", n),
s4(
"s4", n),
s5(
"s5", n);
115 mg.
solvePCG(s0, s1, s2, s3, s4, s5, 0, 1e-8, 2, 2, 12);
120 CCField x = randomVector(mg, 11),
y = randomVector(mg, 12), Mx(
"Mx", l.
n), My(
"My", l.
n);
123 const double a = mg.
dot(l, x, My),
b = mg.
dot(l, y, Mx);
124 return std::abs(a - b) / (std::sqrt(mg.
dot(l, x, x)) * std::sqrt(mg.
dot(l, My, My)));
127std::vector<double> oneVcycle(
CutcellMG& mg) {
135Problem thinProblem(
bool walls) {
136 const int w = walls ? 1 : 0;
137 const int bc[6] = {0, 0, 0, 0,
w,
w};
138 auto rho = [](
double x,
double y,
double z) {
139 const double dx =
x - 24,
dy =
y - 40,
dz = (
z - 2.0) * 3.0;
143 walls ?
"thinW" :
"slabP", 64, 64, 4, 5, bc, [](
double,
double,
double) {
return 1.0; },
rho);
145Problem blobProblem(
bool walls,
int levels) {
146 const int w = walls ? 1 : 0;
147 const int bc[6] = {
w,
w,
w,
w,
w,
w};
148 auto rho = [](
double x,
double y,
double z) {
149 const double dx =
x - 20,
dy =
y - 36,
dz =
z - 28;
153 std::string(walls ?
"iso64w" :
"iso64p") +
" D" + std::to_string(levels), 64, 64, 64, levels,
154 bc, [](
double,
double,
double) {
return 1.0; },
rho);
157 const int bc[6] = {1, 1, 1, 1, 0, 0};
158 auto one = [](double, double, double) {
return 1.0; };
159 return makeProblem(
"scalar", 16, 16, 4, 0, bc, one, one);
162 const int bc[6] = {2, 3, 1, 1, 1, 1};
163 auto one = [](double, double, double) {
return 1.0; };
164 return makeProblem(
"channel", 32, 16, 16, 4, bc, one, one);
167void symCase(
const Problem& P,
bool lr,
bool agglom) {
168 const std::string
tag =
P.name + (
lr ?
"" :
" LR off") +
" np" + std::to_string(size);
176 std::printf(
"%s: %d levels, bottom %s\n",
tag.c_str(), mg.
nLevels(),
181 if (size == 1 && gated) {
184 ref.setLineRelaxation(lr);
186 ref.setAgglomerationMode(1);
187 ref.setSymmetricVcycle(
true);
188 const std::vector<double> zd = oneVcycle(mg), zs = oneVcycle(ref);
191 forInner(l0, [&](
int x,
int y,
int z,
long i) {
192 if (std::memcmp(&zd[i], &zs[
P.idx(x, y, z)],
sizeof(
double)) != 0)
195 check(bits == 0, tag +
": init_mpi V-cycle memcmp-equal to single rank (" +
196 std::to_string(bits) +
" cells differ)");
198 const double a = asym(mg);
199 const double bound = agglom ? kBoundAgglomerated : kBoundSmoothed;
201 std::printf(
" a %s (bound %s)\n",
num(a).c_str(),
num(bound).c_str());
204 std::printf(
" %s: a %s recorded (distributed device bottom, not gated)\n",
tag.c_str(),
208 check(a <= bound, tag +
": a " +
num(a) +
" <= " +
num(bound));
213int main(
int argc,
char** argv) {
214 MPI_Init(&argc, &argv);
215 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
216 MPI_Comm_size(MPI_COMM_WORLD, &size);
217 Kokkos::initialize(argc, argv);
219 const Problem thinW = thinProblem(
true), slabP = thinProblem(
false);
220 symCase(thinW,
true,
false);
221 symCase(thinW,
false,
false);
222 symCase(slabP,
true,
false);
223 symCase(scalarProblem(),
true,
false);
224 symCase(blobProblem(
false, 5),
true,
false);
225 symCase(blobProblem(
true, 5),
true,
false);
227 symCase(channelProblem(),
true,
false);
228 symCase(blobProblem(
false, 4),
true,
true);
232 std::printf(
"mg_vcycle_symmetry_mpi np%d: %s (%d failure%s)\n", size,
fails ?
"FAIL" :
"PASS",
235 return fails ? 1 : 0;
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 setSymmetricVcycle(bool on)
A3 (doc/mg_depth_policy.md Amendment A3, WO-S1): the symmetric pressure V-cycle – piecewise-constant ...
void setOpenness(CCConst ox, CCConst oy, CCConst oz, double idx2, double idy2, double idz2)
void setBoundaryConditions(const int bc[6])
void precondVcycle(CCField zz, CCField rr)
double dot(Level &lv, CCField a, CCField b)
void setAgglomerationMode(int mode)
void setLineRelaxation(bool on)
A2.4 (doc/mg_depth_policy.md, WO-7): zebra line relaxation along the blocked strong axis on the trigg...
void setVcyclePrecision(int mode)
bool agglomerateBottom() const
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)
Kokkos::View< const double *, CCMem > CCConst
Kokkos::View< double *, CCMem > CCField
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")
static double val(long gi)
static void check(bool ok, const std::string &what)
static constexpr double F