29#include <Kokkos_Core.hpp>
40using FV = Kokkos::View<VReal*, pf::CCMem>;
41using FVC = Kokkos::View<const VReal*, pf::CCMem>;
45 pf::I3 e{
N + 2 * g,
N + 2 * g,
N + 2 * g}, og{0, 0, 0};
46 const std::size_t n = (std::size_t)e.x * e.y * e.z;
48 Kokkos::deep_copy(phi, 1.0);
49 Kokkos::deep_copy(d, 0.5);
52 for (
int i = 0; i < 10; ++i)
57 for (
int rep = 0; rep < 5; ++rep) {
59 for (
int i = 0; i < K; ++i)
62 double ms = t.seconds() * 1e3;
66 const double ns_per_sweep = best * 1e6 / K;
67 const double cells = (double)
N *
N *
N;
68 const double ns_per_cell = ns_per_sweep / cells;
70 cells * 64.0 / (ns_per_sweep);
71 std::printf(
"KOKKOS N=%d sweeps=%d : %.3f ns/sweep, %.4f ns/cell, %.1f GB/s (exec %s)\n",
N, K,
72 ns_per_sweep, ns_per_cell, gbps, Kokkos::DefaultExecutionSpace::name());
76constexpr bool kHost = std::is_same_v<pf::CCMem, Kokkos::HostSpace>;
80template <
class Exec = pf::CCExec>
83 if constexpr (std::is_same_v<typename Exec::memory_space, Kokkos::HostSpace>) {
84 const int nyi = e.y - 2 * g, nzi = e.z - 2 * g;
85 auto pencil = [=](
long t) {
86 const int ly = g + (int)(t % nyi), lz = g + (int)(t / nyi);
87 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
88 const int P = (color + og.x + og.y + ly + og.z + lz) & 1;
89 const int lx0 = g + ((P ^ (g & 1)) & 1), lxLast = g + n.x - 1;
90 const long dym = (ly == g) ? (
long)(n.y - 1) * sy : -sy;
91 const long dyp = (ly == g + n.y - 1) ? -(
long)(n.y - 1) * sy : sy;
92 const long dzm = (lz == g) ? (
long)(n.z - 1) * sz : -sz;
93 const long dzp = (lz == g + n.z - 1) ? -(
long)(n.z - 1) * sz : sz;
94 const long row = (long)ly * sy + (
long)lz * sz;
95 auto scalar = [&](
int lx) {
96 const long i = (long)lx + row;
98 pf::cutcellSmoothFaceCell(phi, b,
AC, AFX, AFY, AFZ, i, sx, sy, sz, w.xp, w.xm, w.yp, w.ym,
106 const bool lastIsColour = ((lxLast - lx0) & 1) == 0;
107 const int hi = lastIsColour ? lxLast : lxLast + 1;
109 for (
int lx = lo; lx < hi; lx += 2) {
110 const long i = (long)lx + row;
111 pf::cutcellSmoothFaceCell(phi, b,
AC, AFX, AFY, AFZ, i, sx, sy, sz, i + 1, i - 1, i + dyp,
112 i + dym, i + dzp, i + dzm);
114 if (lastIsColour && lxLast > lx0)
117 Kokkos::parallel_for(
"bench::smooth_peeled", Kokkos::RangePolicy<Exec>(0, (
long)nyi * nzi),
123template <
class Exec = pf::CCExec>
126 if constexpr (std::is_same_v<typename Exec::memory_space, Kokkos::HostSpace>) {
127 for (
int c = 0; c < 2; ++c) {
128 pf::cutcellSmoothColorFaceWrap(xa, b,
AC, AFX, AFY, AFZ, e, n, og, g, c);
129 smoothPeeledFp64(xb, b,
AC, AFX, AFY, AFZ, e, n, og, g, c);
132 for (
long i = 0; i < (long)xa.extent(0); ++i)
133 ndiff += std::memcmp(&xa(i), &xb(i),
sizeof(
double)) != 0;
134 std::printf(
"P0 (b) peeled vs (a) production after 2 passes: %ld differing cells -> %s\n",
135 ndiff, ndiff == 0 ?
"BITWISE" :
"DIFFERS");
140template <
class XV,
class BV,
class WV>
141KOKKOS_INLINE_FUNCTION
void smoothLiteCell(
const XV& x,
const BV& b,
const WV& WX,
const WV& WY,
142 const WV& WZ,
long i,
long sx,
long sy,
long sz,
144 const double D = (((((double)WX(i + sx) + (double)WX(i)) + (double)WY(i + sy)) + (double)WY(i)) +
145 (double)WZ(i + sz)) +
149 const double xi = x(i);
150 const double q = (double)WX(i) * (xi - x(w.xm)) + (
double)WX(i + sx) * (xi - x(w.xp)) +
151 (
double)WY(i) * (xi - x(w.ym)) + (
double)WY(i + sy) * (xi - x(w.yp)) +
152 (
double)WZ(i) * (xi - x(w.zm)) + (
double)WZ(i + sz) * (xi - x(w.zp));
153 x(i) = xi + (b(i) - q) / D;
157template <
class XV,
class BV,
class WV>
158KOKKOS_INLINE_FUNCTION
void smoothFp32Cell(
const XV& x,
const BV& b,
const WV& WX,
const WV& WY,
159 const WV& WZ,
long i,
long sx,
long sy,
long sz,
161 const VReal D = ((((WX(i + sx) + WX(i)) + WY(i + sy)) + WY(i)) + WZ(i + sz)) + WZ(i);
164 const VReal xi = x(i);
165 const VReal q = WX(i) * (xi - x(w.xm)) + WX(i + sx) * (xi - x(w.xp)) + WY(i) * (xi - x(w.ym)) +
166 WY(i + sy) * (xi - x(w.yp)) + WZ(i) * (xi - x(w.zm)) +
167 WZ(i + sz) * (xi - x(w.zp));
168 x(i) = xi + (b(i) - q) / D;
171template <
bool Fp32,
class XV,
class BV,
class WV>
172KOKKOS_INLINE_FUNCTION
void fluxCell(
const XV& x,
const BV& b,
const WV& WX,
const WV& WY,
173 const WV& WZ,
long i,
long sx,
long sy,
long sz,
183template <
bool Fp32,
class XV,
class BV>
188 const int nyi = e.y - 2 * g, nzi = e.z - 2 * g;
189 auto pencil = KOKKOS_LAMBDA(
long t) {
190 const int ly = g + (int)(t % nyi), lz = g + (int)(t / nyi);
191 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
192 const int P = (color + og.x + og.y + ly + og.z + lz) & 1;
194 for (
int lx = g + ((P ^ (g & 1)) & 1); lx < e.x - g; lx += 2) {
195 const long i = (long)lx + (
long)ly * sy + (long)lz * sz;
200 Kokkos::parallel_for(
"bench::smooth_flux",
201 Kokkos::RangePolicy<pf::CCExec>(space, 0, (
long)nyi * nzi), pencil);
204 Kokkos::parallel_for(
205 "bench::smooth_flux", MD(space, {g, g, g}, {e.x - g, e.y - g, e.z - g}),
206 KOKKOS_LAMBDA(
int lx,
int ly,
int lz) {
207 if (((og.x + lx + og.y + ly + og.z + lz) & 1) != color)
209 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
210 const long i = (long)lx + (
long)ly * sy + (long)lz * sz;
218 FILE* f = std::fopen(
"/proc/loadavg",
"r");
221 if (!std::fgets(buf,
sizeof buf, f))
225 std::printf(
"P0 %s loadavg %s", tag, buf[0] ? buf :
"?\n");
232 std::sort(v.begin(), v.end());
233 return {v.front(), v[v.size() / 2], v.back()};
238 std::printf(
"P0 exec %s concurrency %d rounds %d passes/round %d\n", pf::CCExec::name(), conc,
243 for (
long len : {(long)conc, 6144L}) {
244 std::vector<double> us;
246 for (
int r = 0; r < rounds; ++r) {
249 for (
int k = 0; k < K; ++k) {
250 Kokkos::parallel_for(
"bench::empty", Kokkos::RangePolicy<pf::CCExec>(0, len),
251 KOKKOS_LAMBDA(
long){});
254 us.push_back(t.seconds() * 1e6 / K);
257 std::printf(
"P0 t_L empty parallel_for len %ld: min %.2f med %.2f max %.2f us/launch\n", len,
263 const pf::C3 n{128, 96, 64}, e{n.x + 2, n.y + 2, n.z + 2}, og{0, 0, 0};
264 const long ne = (long)e.x * e.y * e.z;
265 std::vector<double> rho(ne), ox(ne), oy(ne), oz(ne), bh(ne, 0.0), xh(ne, 0.0);
266 auto wrap = [](
int l,
int nn) {
return ((l - 1) % nn + nn) % nn; };
268 unsigned long long s = 12345;
269 auto urand = [&s]() {
270 s = s * 6364136223846793005ULL + 1442695040888963407ULL;
271 return (
double)(s >> 11) * (1.0 / 9007199254740992.0);
273 std::vector<double> cx, cy, cz;
274 for (
int k = 0; k < 24; ++k) {
275 cx.push_back(
urand() * n.x);
276 cy.push_back(8 +
urand() * (n.y - 16));
277 cz.push_back(
urand() * n.z);
279 for (
int lz = 0; lz < e.z; ++lz)
280 for (
int ly = 0; ly < e.y; ++ly)
281 for (
int lx = 0; lx < e.x; ++lx) {
282 const double X =
wrap(lx, n.x) + 0.5, Y = std::clamp(ly - 1, 0, n.y - 1) + 0.5,
283 Z =
wrap(lz, n.z) + 0.5;
285 for (
int k = 0; k < 24; ++k) {
286 double dx = std::fabs(X - cx[k]), dz = std::fabs(Z - cz[k]);
287 dx = std::min(dx, n.x - dx);
288 dz = std::min(dz, n.z - dz);
289 const double r = std::sqrt(dx * dx + (Y - cy[k]) * (Y - cy[k]) + dz * dz);
290 C = std::max(C, std::clamp(6.5 - r, 0.0, 1.0));
292 rho[(long)lx + (
long)ly * e.x + (long)lz * e.x * e.y] = 1.0 - C + 0.02 * C;
294 const long sy = e.x, sz = (long)e.x * e.y;
295 for (
int lz = 1; lz < e.z; ++lz)
296 for (
int ly = 1; ly < e.y; ++ly)
297 for (
int lx = 1; lx < e.x; ++lx) {
298 const long i = (long)lx + ly * sy + lz * sz;
299 ox[i] = 1.0 / (0.5 * (rho[i] + rho[i - 1]));
300 oy[i] = (ly == 1 || ly == n.y + 1) ? 0.0 : 1.0 / (0.5 * (rho[i] + rho[i - sy]));
301 oz[i] = 1.0 / (0.5 * (rho[i] + rho[i - sz]));
303 for (
int lz = 1; lz <= n.z; ++lz)
304 for (
int ly = 1; ly <= n.y; ++ly)
305 for (
int lx = 1; lx <= n.x; ++lx) {
306 const long i = (long)lx + ly * sy + lz * sz;
307 bh[i] =
urand() - 0.5;
308 xh[i] =
urand() - 0.5;
310 auto up = [ne](
const std::vector<double>& h,
const char* nm) {
312 auto m = Kokkos::create_mirror_view(d);
313 for (
long i = 0; i < ne; ++i)
315 Kokkos::deep_copy(d, m);
318 pf::CCField dox = up(ox,
"ox"), doy = up(oy,
"oy"), doz = up(oz,
"oz"), b = up(bh,
"b"),
320 pf::FPV AC(
"AC", ne), AFX(
"AFX", ne), AFY(
"AFY", ne), AFZ(
"AFZ", ne);
323 FV WX(
"WX", ne), WY(
"WY", ne), WZ(
"WZ", ne), xf(
"xf", ne), bf(
"bf", ne);
324 Kokkos::parallel_for(
325 "bench::fp32_data", Kokkos::RangePolicy<pf::CCExec>(0, ne), KOKKOS_LAMBDA(
long i) {
326 WX(i) =
static_cast<VReal>(dox(i) * 1.0);
327 WY(i) =
static_cast<VReal>(doy(i) * 1.0);
328 WZ(i) =
static_cast<VReal>(doz(i) * 1.0);
329 xf(i) =
static_cast<VReal>(x0(i));
330 bf(i) =
static_cast<VReal>(b(i));
332 pf::CCField xa(
"xa", ne), xb(
"xb", ne), xc(
"xc", ne);
333 Kokkos::deep_copy(xa, x0);
334 Kokkos::deep_copy(xb, x0);
335 Kokkos::deep_copy(xc, x0);
342 const char* names[4] = {
"(a) fp64 wrap (production)",
"(b) fp64 peeled",
"(c) D-lite fp32 faces",
344 std::vector<double> ms[4];
345 auto pass = [&](
int v,
int c) {
362 for (
int v = 0; v < 4; ++v)
363 for (
int k = 0; k < 4; ++k)
367 for (
int r = 0; r < rounds; ++r)
368 for (
int v = 0; v < 4; ++v) {
369 if (!
kHost && v == 1)
373 for (
int k = 0; k < passes; ++k)
376 ms[v].push_back(t.seconds() * 1e3 / passes);
378 const double upd = 0.5 * (double)n.x * n.y * n.z;
380 for (
int v = 0; v < 4; ++v) {
382 std::printf(
"P0 pass %-28s n/a (host-only variant)\n", names[v]);
389 "P0 pass %-28s min %.4f med %.4f max %.4f ms/pass | %.3f ns/updated cell | (a)/this %.3f\n",
390 names[v], st.mn, st.med, st.mx, st.mn * 1e6 / upd, amin / st.mn);
397int runC0(
int rounds,
int launches) {
399 std::printf(
"C0 exec %s concurrency %d rounds %d launches/round %d\n", pf::CCExec::name(), conc,
403 const pf::C3 n{128, 96, 64}, e{n.x + 2 * g, n.y + 2 * g, n.z + 2 * g};
404 const long N = (long)e.x * e.y * e.z;
407 auto hr = Kokkos::create_mirror_view(r), hz = Kokkos::create_mirror_view(z);
408 auto ha = Kokkos::create_mirror_view(ac);
410 for (
long i = 0; i <
N; ++i) {
411 s = s * 1664525u + 1013904223u;
412 hr(i) = (double)(s >> 8) / 16777216.0 - 0.5;
413 s = s * 1664525u + 1013904223u;
414 hz(i) = (double)(s >> 8) / 16777216.0 - 0.5;
417 Kokkos::deep_copy(r, hr);
418 Kokkos::deep_copy(z, hz);
419 Kokkos::deep_copy(ac, ha);
420 const pf::C3 lo{g, g, g}, hi{e.x - g, e.y - g, e.z - g};
421 auto all = KOKKOS_LAMBDA(
int x,
int y,
int zc,
double& acc) {
422 const long i = (long)x + (
long)y * e.x + (long)zc * (
long)e.x * e.y;
425 auto msk = KOKKOS_LAMBDA(
int x,
int y,
int zc,
double& acc) {
426 const long i = (long)x + (
long)y * e.x + (long)zc * (
long)e.x * e.y;
430 const char* names[4] = {
"all-fluid B2 (ccReduce3)",
"all-fluid 4-lane",
"masked B2 (ccReduce3)",
432 std::vector<double> us[4];
433 double val[4] = {0, 0, 0, 0};
434 for (
int rd = 0; rd < rounds; ++rd)
435 for (
int v = 0; v < 4; ++v) {
438 for (
int k = 0; k < launches; ++k) {
451 us[v].push_back(t.seconds() * 1e6 / launches);
453 for (
int v = 0; v < 4; ++v) {
456 std::printf(
"C0 %-26s min %.2f med %.2f max %.2f us/launch | this/B2 (min) %.3f | sum %.17g\n",
457 names[v], st.mn, st.med, st.mx, st.mn / s0.mn,
val[v]);
459 std::printf(
"C0 rel diff lanes vs B2: all %.3e masked %.3e\n",
460 std::fabs(
val[1] -
val[0]) / std::fabs(
val[0]),
461 std::fabs(
val[3] -
val[2]) / std::fabs(
val[2]));
468int main(
int argc,
char** argv) {
469 Kokkos::initialize(argc, argv);
471 if (argc > 1 && std::strcmp(argv[1],
"c0") == 0) {
472 rc =
p0bench::runC0(argc > 2 ? std::atoi(argv[2]) : 7, argc > 3 ? std::atoi(argv[3]) : 200);
473 }
else if (argc > 1 && std::strcmp(argv[1],
"p0") == 0) {
474 rc =
p0bench::runP0(argc > 2 ? std::atoi(argv[2]) : 5, argc > 3 ? std::atoi(argv[3]) : 40);
477 (argc > 2) ? std::atoi(argv[2]) : 200);
#define PECLET_FLOW_OMP_SIMD
flow — portable (Kokkos) geometric multigrid for the cut-cell (variable-openness) pressure Poisson.
flow — portable (Kokkos) MAC stencil operators: Red-Black Gauss-Seidel smoothers + divergence.
int runRbgs(int N, int K)
void fluxCell(const XV &x, const BV &b, const WV &WX, const WV &WY, const WV &WZ, long i, long sx, long sy, long sz, const pf::CcNbrs &w)
void smoothLiteCell(const XV &x, const BV &b, const WV &WX, const WV &WY, const WV &WZ, long i, long sx, long sy, long sz, const pf::CcNbrs &w)
void checkPeeled(pf::CCField xa, pf::CCField xb, pf::CCConst b, pf::FPC AC, pf::FPC AFX, pf::FPC AFY, pf::FPC AFZ, pf::C3 e, pf::C3 n, pf::C3 og, int g)
int runP0(int rounds, int passes)
void smoothFluxPass(XV x, BV b, FVC WX, FVC WY, FVC WZ, pf::C3 e, pf::C3 n, pf::C3 og, int g, int color)
void loadavg(const char *tag)
int runC0(int rounds, int launches)
Kokkos::View< VReal *, pf::CCMem > FV
Stat stats(std::vector< double > v)
void smoothFp32Cell(const XV &x, const BV &b, const WV &WX, const WV &WY, const WV &WZ, long i, long sx, long sy, long sz, const pf::CcNbrs &w)
void smoothPeeledFp64(pf::CCField phi, pf::CCConst b, pf::FPC AC, pf::FPC AFX, pf::FPC AFY, pf::FPC AFZ, pf::C3 e, pf::C3 n, pf::C3 og, int g, int color)
Kokkos::View< const VReal *, pf::CCMem > FVC
Kokkos::DefaultExecutionSpace CCExec
Kokkos::MDRangePolicy< Exec, Kokkos::Rank< 3, Kokkos::Iterate::Left, Kokkos::Iterate::Left > > MDRange3
3-D range, x (the first index) fastest (both the tile order and the order within a tile).
Kokkos::View< double *, SMem > SField
void cutcellSmoothFaceCell(const PV &phi, const BV &b, const OpV &AC, const OpV &AFX, const OpV &AFY, const OpV &AFZ, long i, long sx, long sy, long sz, long xp, long xm, long yp, long ym, long zp, long zm)
Kokkos::View< const double *, CCMem > CCConst
void ccReduce3(const char *name, C3 lo, C3 hi, F f, R &&... reducers)
Kokkos::View< const MReal *, CCMem > FPC
void buildCutcellOpFace(OpV AC, OpV AFX, OpV AFY, OpV AFZ, CCConst ox, CCConst oy, CCConst oz, C3 e, int gb, double gfx, double gfy, double gfz)
CcNbrs ccWrapNbrs(int lx, int ly, int lz, C3 n, int g, long i, long sy, long sz)
Kokkos::View< double *, CCMem > CCField
Kokkos::View< MReal *, CCMem > FPV
void ccReduce3Lanes(const char *name, C3 lo, C3 hi, F f, R &&... reducers)
void cutcellSmoothColorFaceWrap(CCField phi, CCConst b, FPC AC, FPC AFX, FPC AFY, FPC AFZ, C3 e, C3 n, C3 og, int g, int color)
Kokkos::View< const double *, SMem > SConst
void poisSweep(SField phi, SConst d, I3 e, I3 og, int g)
static double val(long gi)
static int wrap(int a, int n)
static constexpr double AC
static double urand(int id, int gx, int gy, int gz)