21#include <Kokkos_Core.hpp>
28#include "peclet/core/common/types.hpp"
29#include "peclet/core/decomp/block_decomposer.hpp"
31using peclet::core::IVec;
38static constexpr int NX = 32,
NY = 32,
NZ = 25;
41static void check(
bool ok,
const std::string& what) {
42 int all = 0, v = ok ? 1 : 0;
43 MPI_Allreduce(&v, &all, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD);
45 std::printf(
" %-78s %s\n", what.c_str(), all ?
"ok" :
"FAIL");
56static Diff compareBlock(
const std::vector<double>& a,
const std::vector<double>& ref,
int ox,
57 int oy,
int oz,
int lnx,
int lny,
int lnz) {
59 for (
int z = 0; z < lnz; ++z)
60 for (
int y = 0; y < lny; ++y)
61 for (
int x = 0; x < lnx; ++x) {
62 const double u = a[(std::size_t)x + (std::size_t)y * lnx + (std::size_t)z * lnx * lny];
63 const double r = ref[(std::size_t)(x + ox) + (std::size_t)(y + oy) *
NX +
64 (std::size_t)(z + oz) *
NX *
NY];
65 d.maxAbs = std::max(d.maxAbs, std::fabs(u - r));
66 d.refMax = std::max(d.refMax, std::fabs(r));
67 if (std::memcmp(&u, &r,
sizeof(
double)) != 0)
70 MPI_Allreduce(MPI_IN_PLACE, &d.maxAbs, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
71 MPI_Allreduce(MPI_IN_PLACE, &d.refMax, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
72 MPI_Allreduce(MPI_IN_PLACE, &d.bits, 1, MPI_LONG, MPI_SUM, MPI_COMM_WORLD);
76static bool sameBits(
const std::vector<double>& a,
const std::vector<double>& b) {
77 int eq = a.size() == b.size() && std::memcmp(a.data(), b.data(), a.size() *
sizeof(
double)) == 0;
78 MPI_Allreduce(MPI_IN_PLACE, &eq, 1, MPI_INT, MPI_MIN, MPI_COMM_WORLD);
82static std::string
fmt(
const char* f,
double a,
long b = 0) {
84 std::snprintf(buf,
sizeof buf, f, a, b);
90static double source(
int gx,
int gy,
int gz) {
91 return std::sin(2.0 * M_PI * gx /
NX) +
92 std::cos(4.0 * M_PI * gy /
NY) * std::sin(2.0 * M_PI * gz /
NZ);
98 const C3 e = l0.
ext, og = l0.
og;
100 const std::size_t n = (std::size_t)e.x * e.y * e.z;
101 CCField ox(
"ox", n), oy(
"oy", n), oz(
"oz", n);
102 Kokkos::deep_copy(ox, 1.0);
103 Kokkos::deep_copy(oy, 1.0);
104 Kokkos::deep_copy(oz, 1.0);
106 CCField b(
"b", n), x(
"x", n), r(
"r", n), p(
"p", n), z(
"z", n), Ap(
"Ap", n);
107 auto hb = Kokkos::create_mirror_view(b);
108 for (std::size_t c = 0; c < n; ++c)
110 for (
int k = g; k < e.z - g; ++k)
111 for (
int j = g; j < e.y - g; ++j)
112 for (
int i = g; i < e.x - g; ++i)
113 hb((
long)i + (
long)j * e.x + (
long)k * e.x * e.y) =
114 source(i - g + og.x, j - g + og.y, k - g + og.z);
115 Kokkos::deep_copy(b, hb);
116 mg.
solvePCG(b, x, r, p, z, Ap, 6, 1e-30, 2, 2, 8);
117 auto hx = Kokkos::create_mirror_view(x);
118 Kokkos::deep_copy(hx, x);
119 std::vector<double> out;
120 for (
int k = g; k < e.z - g; ++k)
121 for (
int j = g; j < e.y - g; ++j)
122 for (
int i = g; i < e.x - g; ++i)
123 out.push_back(hx((
long)i + (
long)j * e.x + (
long)k * e.x * e.y));
128 constexpr int NLEV = 4;
131 const std::vector<double> xs =
mgSolve(ref);
132 std::vector<double> xd[2];
133 int lnx = 0, lny = 0, lnz = 0, ox = 0, oy = 0, oz = 0;
135 for (
int k = 0; k < 2; ++k) {
141 for (
int L = 0; L < mg.
nLevels(); ++L)
143 check(caLevels == 0,
fmt(
"(a) no CA level on the odd periodic grid (%g of them)", caLevels));
154 check(
sameBits(xd[0], xd[1]),
"(a) MG-PCG x: CA on memcmp == CA off");
155 for (
int k = 0; k < 2; ++k) {
157 const std::string t = std::string(
"(a) MG-PCG x, CA ") + (k == 0 ?
"on" :
"off");
159 check(d.bits == 0, t +
fmt(
": np 1 memcmp == single rank (max|d| %.3e, %ld cells differ)",
162 check(d.maxAbs <= 1e-8, t +
fmt(
": max|d| vs single rank %.3e <= 1e-8", d.maxAbs));
169 std::vector<double> sdf((std::size_t)
NX *
NY *
NZ);
170 const double R = 0.3 *
NZ, c[3] = {0.5 *
NX, 0.5 *
NY, 0.5 *
NZ};
171 const int n[3] = {
NX,
NY,
NZ};
172 for (
int z = 0; z <
NZ; ++z)
173 for (
int y = 0; y <
NY; ++y)
174 for (
int x = 0; x <
NX; ++x) {
176 const int p[3] = {x, y, z};
177 for (
int a = 0; a < 3; ++a) {
178 const double d = p[a] - c[a];
179 const double w = d - n[a] * std::round(d / n[a]);
182 sdf[(std::size_t)x + (std::size_t)y *
NX + (std::size_t)z *
NX *
NY] = std::sqrt(d2) -
R;
191 s.setBodyForce(1e-3, 2e-4, 0);
192 s.setAdvection(
false);
193 s.setVelocityIterations(40);
194 s.setPressureLevels(4);
195 s.setPressurePcg(
true, 200, 1e-9);
199 std::vector<double>
f[4];
203 for (
int c = 0; c < 3; ++c)
204 r.f[c] = s.getVelocity(c);
205 r.f[3] = s.getPressure();
210 constexpr int STEPS = 4;
211 const std::vector<double> gsdf =
sphereSdf();
217 ref.setPressureBottomSolver(
218 std::is_same_v<peclet::flow::CCExec::memory_space, Kokkos::HostSpace> ? 0 : 2);
219 ref.setSolid(gsdf,
true);
220 for (
int it = 0; it <
STEPS; ++it)
224 const peclet::core::decomp::BlockDecomposer<3> dec =
225 CutcellMG::decomposition(
static_cast<std::size_t
>(
gSize),
NX,
NY,
NZ);
226 const auto blk = dec.block(
gRank);
227 const int ox = (int)blk.origin[0], oy = (
int)blk.origin[1], oz = (int)blk.origin[2];
228 const int lnx = (int)blk.size[0], lny = (
int)blk.size[1], lnz = (int)blk.size[2];
229 std::vector<double> lsdf((std::size_t)lnx * lny * lnz);
230 for (
int z = 0; z < lnz; ++z)
231 for (
int y = 0; y < lny; ++y)
232 for (
int x = 0; x < lnx; ++x)
233 lsdf[(std::size_t)x + (std::size_t)y * lnx + (std::size_t)z * lnx * lny] =
234 gsdf[(std::size_t)(x + ox) + (std::size_t)(y + oy) *
NX +
235 (std::size_t)(z + oz) *
NX *
NY];
238 const char* names[4] = {
"both",
"momentum",
"pressure",
"off"};
240 for (
int k = 0; k < 4; ++k) {
242 s.setCommAvoiding(modes[k]);
243 s.initMpi(
NX,
NY,
NZ, MPI_COMM_WORLD);
245 s.setSolid(lsdf,
true);
246 for (
int it = 0; it <
STEPS; ++it)
250 for (
int k = 0; k < 4; ++k) {
251 const std::string t = std::string(
"(b) CA ") + names[k];
254 for (
int c = 0; c < 4; ++c)
255 eq =
sameBits(fd[k].f[c], fd[3].f[c]) && eq;
256 check(eq, t +
": u, v, w, p memcmp == CA off");
261 for (
int c = 0; c < 4; ++c) {
262 const Diff d =
compareBlock(fd[k].f[c], fs.f[c], ox, oy, oz, lnx, lny, lnz);
264 mx = std::max(mx, d.maxAbs);
267 t +
fmt(
": np 1 u, v, w, p memcmp == single rank (max|d| %.3e, %ld differ)", mx, bits));
270 for (
int c = 0; c < 3; ++c) {
271 const Diff d =
compareBlock(fd[k].f[c], fs.f[c], ox, oy, oz, lnx, lny, lnz);
272 rel = std::max(rel, d.maxAbs / (d.refMax + 1e-300));
275 std::printf(
" %s: max over u, v, w of max|d| / max|ref| = %.3e\n", names[k], rel);
276 check(rel <= 2e-5, t +
fmt(
": velocity vs single rank, relative %.3e <= 2e-5", rel));
281int main(
int argc,
char** argv) {
282 MPI_Init(&argc, &argv);
283 Kokkos::initialize(argc, argv);
285 MPI_Comm_rank(MPI_COMM_WORLD, &
gRank);
286 MPI_Comm_size(MPI_COMM_WORLD, &
gSize);
288 std::printf(
"CA red-black pairs on a %dx%dx%d periodic grid (odd z), np %d\n",
NX,
NY,
NZ,
295 std::printf(
"OK (np=%d): odd periodic extent -- CA off, np 1 == single rank\n",
gSize);
297 std::fprintf(stderr,
"FAILED (np=%d): %d checks\n",
gSize,
gFail);
301 return gFail == 0 ? 0 : 1;
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 setOpenness(CCConst ox, CCConst oy, CCConst oz, double idx2, double idy2, double idz2)
void setCommAvoiding(int mask)
Communication-avoiding smoothing mask (kCaMomentum | kCaMg); only kCaMg is read here.
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
flow — portable (Kokkos) geometric multigrid for the cut-cell (variable-openness) pressure Poisson.
Kokkos::View< const double *, CCMem > CCConst
Kokkos::View< double *, CCMem > CCField
Solver< Staggered > IbmSolver
std::vector< double > f[4]
static constexpr int STEPS
static Diff compareBlock(const std::vector< double > &a, const std::vector< double > &ref, int ox, int oy, int oz, int lnx, int lny, int lnz)
static std::vector< double > mgSolve(CutcellMG &mg)
static Fields fieldsOf(IbmSolver &s)
static void check(bool ok, const std::string &what)
static void solverChecks()
static bool sameBits(const std::vector< double > &a, const std::vector< double > &b)
static std::vector< double > sphereSdf()
static double source(int gx, int gy, int gz)
static std::string fmt(const char *f, double a, long b=0)
static void configure(IbmSolver &s)
static constexpr int NLEV
static constexpr double R