28#include <Kokkos_Core.hpp>
42void check(
bool ok,
const std::string& what) {
43 std::printf(
" %-90s %s\n", what.c_str(), ok ?
"PASS" :
"FAIL");
48std::string num(
double v) {
50 std::snprintf(b,
sizeof b,
"%.3e", v);
54constexpr double kBoundSmoothed = 1e-14, kBoundAgglomerated = 1e-8;
55constexpr int kCap = 500;
56constexpr double kRtol = 1e-10;
60 unsigned long s = (
unsigned long)key * 6364136223846793005ul + 1442695040888963407ul;
62 s *= 0xff51afd7ed558ccdul;
64 return (
double)(s >> 11) / (
double)(1ul << 52) - 1.0;
70 auto h = Kokkos::create_mirror_view(f);
71 auto ac = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), l.
AC);
72 for (std::size_t i = 0; i < h.extent(0); ++i)
74 for (
int z = 0; z < l.
inner.
z; ++z)
75 for (
int y = 0; y < l.
inner.
y; ++y)
76 for (
int x = 0; x < l.
inner.
x; ++x) {
78 (long)(x + l.g) + (long)(y + l.g) * l.ext.
x + (long)(z + l.g) * (long)l.ext.
x * l.ext.y;
79 h(i) = (double)ac(i) > 1e-30 ?
val(seed * 7919L + x + 1000L * y + 1000000L * z) : 0.0;
81 Kokkos::deep_copy(f, h);
87 const std::size_t n = mg.
level(0).
n;
88 CCField s0(
"s0", n), s1(
"s1", n), s2(
"s2", n), s3(
"s3", n), s4(
"s4", n), s5(
"s5", n);
89 mg.
solvePCG(s0, s1, s2, s3, s4, s5, 0, 1e-8, 2, 2, 12);
95 CCField x = randomVector(mg, 11), y = randomVector(mg, 12), Mx(
"Mx", l.
n), My(
"My", l.
n);
103 const double a = mg.
dot(l, x, My), b = mg.
dot(l, y, Mx);
104 return std::abs(a - b) / (std::sqrt(mg.
dot(l, x, x)) * std::sqrt(mg.
dot(l, My, My)));
109 const std::size_t n = l.
n;
110 std::vector<double> hb =
rhsOf(P);
111 auto ac = Kokkos::create_mirror_view_and_copy(Kokkos::HostSpace(), l.
AC);
112 for (std::size_t i = 0; i < n; ++i)
113 if (!((
double)ac(i) > 1e-30))
115 CCField b =
toDevice(hb,
"b"), x(
"x", n), r(
"r", n), p(
"p", n), z(
"z", n), Ap(
"Ap", n);
116 return mg.
solvePCG(b, x, r, p, z, Ap, kCap, kRtol, 2, 2, 12);
119Problem thinProblem(
bool walls) {
120 const int w = walls ? 1 : 0;
121 const int bc[6] = {0, 0, 0, 0, w, w};
122 auto rho = [](
double x,
double y,
double z) {
123 const double dx = x - 24, dy = y - 40, dz = (z - 2.0) * 3.0;
124 return dx * dx + dy * dy + dz * dz < 144.0 ? 1e2 : 1.0;
127 walls ?
"thinW" :
"slabP", 64, 64, 4, 5, bc, [](
double,
double,
double) {
return 1.0; }, rho);
129Problem blobProblem(
bool walls,
int levels) {
130 const int w = walls ? 1 : 0;
131 const int bc[6] = {w, w, w, w, w, w};
132 auto rho = [](
double x,
double y,
double z) {
133 const double dx = x - 20, dy = y - 36, dz = z - 28;
134 return dx * dx + dy * dy + dz * dz < 100.0 ? 1e2 : 1.0;
137 std::string(walls ?
"iso64w" :
"iso64p") +
" D" + std::to_string(levels), 64, 64, 64, levels,
138 bc, [](
double,
double,
double) {
return 1.0; }, rho);
141 const int bc[6] = {1, 1, 1, 1, 0, 0};
142 auto one = [](double, double, double) {
return 1.0; };
143 return makeProblem(
"scalar", 16, 16, 4, 0, bc, one, one);
146 const int bc[6] = {2, 3, 1, 1, 1, 1};
147 auto one = [](double, double, double) {
return 1.0; };
148 return makeProblem(
"channel", 32, 16, 16, 4, bc, one, one);
152 std::string s = std::to_string(mg.
nLevels()) +
" levels, bottom ";
154 s += std::to_string(b.inner.x) +
"x" + std::to_string(b.inner.y) +
"x" +
155 std::to_string(b.inner.z) + (mg.
agglomerateBottom() ?
" agglomerated" :
" smoothed");
160void symCase(
const Problem& P,
bool lr,
bool agglom) {
166 const std::string tag = P.name + (lr ?
"" :
" LR off");
167 std::printf(
"%s: %s\n", tag.c_str(), hierarchy(mg).c_str());
169 const double aOff = asym(mg,
false);
171 const double aOn = asym(mg,
false);
172 std::printf(
" a: trilinear/forward %s (recorded), symmetric %s\n", num(aOff).c_str(),
175 tag +
": the bottom is " + (agglom ?
"agglomerated" :
"smoothed"));
176 const double bound = agglom ? kBoundAgglomerated : kBoundSmoothed;
177 check(aOn <= bound, tag +
": a " + num(aOn) +
" <= " + num(bound));
181void guardCase(
const Problem& P,
bool lr,
int bound) {
185 const std::string tag = P.name + (lr ?
"" :
" LR off");
187 const int off = pcgIters(mg, P);
189 const int on = pcgIters(mg, P);
191 "%s: production PCG rhsOf rtol 1e-10: trilinear/forward %d (recorded), symmetric %d\n",
192 tag.c_str(), off, on);
193 check(on <= bound, tag +
": PCG " + std::to_string(on) +
" <= " + std::to_string(bound));
200void fp32Case(
const Problem& P) {
205 std::printf(
"%s FP32: not eligible (%s) -- n/a\n", P.name.c_str(), why);
209 const double a = asym(mg,
true);
210 const int it = pcgIters(mg, P);
213 std::printf(
"%s FP32 V-cycle (forced 'fp32', symmetric): a %s (recorded %s); PCG %d%s\n",
214 P.name.c_str(), num(a).c_str(), num(aRec).c_str(), it,
217 check(a <= 100.0 * aRec, P.name +
" FP32: a " + num(a) +
" <= 100 x " + num(aRec));
218 check(it <= 18 && !mg.
lastSolveFailed(), P.name +
" FP32: PCG " + std::to_string(it) +
" <= 18");
223int main(
int argc,
char** argv) {
224 Kokkos::initialize(argc, argv);
226 const Problem thinW = thinProblem(
true), slabP = thinProblem(
false);
227 symCase(thinW,
true,
false);
228 symCase(thinW,
false,
false);
229 symCase(slabP,
true,
false);
230 symCase(scalarProblem(),
true,
false);
231 symCase(blobProblem(
false, 5),
true,
false);
232 symCase(blobProblem(
true, 5),
true,
false);
234 symCase(channelProblem(),
true,
false);
235 symCase(blobProblem(
false, 4),
true,
true);
237 guardCase(thinW,
true, 16);
238 guardCase(thinW,
false, 16);
239 guardCase(slabP,
true, 14);
242 std::printf(
"mg_vcycle_symmetry: %s (%d failure%s)\n",
fails ?
"FAIL" :
"PASS",
fails,
243 fails == 1 ?
"" :
"s");
244 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 precondVcycleFp32(CCField zz, CCField rr, int e, int k, CCField p)
const char * fp32VcycleIneligible(bool krylov) const
void setSymmetricVcycle(bool on)
A3 (doc/mg_depth_policy.md Amendment A3, WO-S1): the symmetric pressure V-cycle – piecewise-constant ...
double maxabs(Level &lv, CCField a)
void precondVcycle(CCField zz, CCField rr)
double dot(Level &lv, CCField a, CCField b)
bool lastVcycleFp32() const
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...
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,...
Kokkos::View< double *, CCMem > CCField
std::vector< double > rhsOf(const Problem &P)
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)
static double val(long gi)
static void check(bool ok, const std::string &what)