9#ifndef PECLET_FLOW_MAC_CUTCELL_HPP
10#define PECLET_FLOW_MAC_CUTCELL_HPP
13#include <Kokkos_Core.hpp>
14#include <Kokkos_MathematicalFunctions.hpp>
24using CCExec = Kokkos::DefaultExecutionSpace;
25using CCMem = CCExec::memory_space;
26using CCField = Kokkos::View<double*, CCMem>;
27using CCConst = Kokkos::View<const double*, CCMem>;
60KOKKOS_INLINE_FUNCTION
double ccFractionCore(
double sd,
double sxp,
double sxm,
double syp,
61 double sym,
double szp,
double szm,
int type,
62 double dx,
double dy,
double dz) {
65 const double gx = (sxp - sxm) / (2.0 * dx), gy = (syp - sym) / (2.0 * dy),
66 gz = (szp - szm) / (2.0 * dz);
67 double gmag = Kokkos::sqrt(gx * gx + gy * gy + gz * gz);
70 const double nx = gx / gmag, ny = gy / gmag, nz = gz / gmag;
71 double denom = (type == 1) ? (Kokkos::fabs(ny) * dy + Kokkos::fabs(nz) * dz)
72 : (type == 2) ? (Kokkos::fabs(nx) * dx + Kokkos::fabs(nz) * dz)
73 : (Kokkos::fabs(nx) * dx + Kokkos::fabs(ny) * dy);
76 double frac = 0.5 + sd / denom;
85 const double fx = Kokkos::floor(x), fy = Kokkos::floor(y), fz = Kokkos::floor(z);
86 const double wx = x - fx, wy = y - fy, wz = z - fz;
87 int x0 = (int)fx, y0 = (
int)fy, z0 = (int)fz;
88 auto cl = [](
int v,
int n) {
return v < 0 ? 0 : (v >= n ? n - 1 : v); };
89 const int x1 = cl(x0 + 1, ext.
x), y1 = cl(y0 + 1, ext.
y), z1 = cl(z0 + 1, ext.
z);
93 const long sy = ext.
x, sz =
static_cast<long>(ext.
x) * ext.
y;
94 auto F = [&](
int xx,
int yy,
int zz) {
95 return sdf(
static_cast<long>(xx) +
static_cast<long>(yy) * sy +
static_cast<long>(zz) * sz);
97 const double c00 =
F(x0, y0, z0) * (1 - wx) +
F(x1, y0, z0) * wx;
98 const double c10 =
F(x0, y1, z0) * (1 - wx) +
F(x1, y1, z0) * wx;
99 const double c01 =
F(x0, y0, z1) * (1 - wx) +
F(x1, y0, z1) * wx;
100 const double c11 =
F(x0, y1, z1) * (1 - wx) +
F(x1, y1, z1) * wx;
101 const double c0 = c00 * (1 - wy) + c10 * wy, c1 = c01 * (1 - wy) + c11 * wy;
102 return c0 * (1 - wz) + c1 * wz;
106 int type,
double dx,
double dy,
double dz) {
107 const double sd =
ccSampleExt(sdf, ext, fx, fy, fz);
110 const double e = 1.0;
112 sd,
ccSampleExt(sdf, ext, fx + e, fy, fz),
ccSampleExt(sdf, ext, fx - e, fy, fz),
113 ccSampleExt(sdf, ext, fx, fy + e, fz),
ccSampleExt(sdf, ext, fx, fy - e, fz),
114 ccSampleExt(sdf, ext, fx, fy, fz + e),
ccSampleExt(sdf, ext, fx, fy, fz - e), type, dx, dy,
121KOKKOS_INLINE_FUNCTION
double ccTriFrac(
double a,
double b,
double c) {
122 const bool pa = a >= 0.0, pb = b >= 0.0, pc = c >= 0.0;
123 const int np = (pa ? 1 : 0) + (pb ? 1 : 0) + (pc ? 1 : 0);
143 const double den = (x - y) * (x - z);
144 return den > 1e-300 ? (x * x) / den : 1.0;
160 const double den = (x - y) * (x - z);
161 return 1.0 - (den > 1e-300 ? (x * x) / den : 1.0);
178 const double e = 0.5;
179 double t1x = 0, t1y = 0, t1z = 0, t2x = 0, t2y = 0, t2z = 0;
183 }
else if (type == 2) {
190 const double ccg =
ccSampleExt(sdf, ext, fx, fy, fz);
198 const double c00 =
ccSampleExt(sdf, ext, fx - t1x - t2x, fy - t1y - t2y, fz - t1z - t2z);
199 const double c10 =
ccSampleExt(sdf, ext, fx + t1x - t2x, fy + t1y - t2y, fz + t1z - t2z);
200 const double c11 =
ccSampleExt(sdf, ext, fx + t1x + t2x, fy + t1y + t2y, fz + t1z + t2z);
201 const double c01 =
ccSampleExt(sdf, ext, fx - t1x + t2x, fy - t1y + t2y, fz - t1z + t2z);
202 const double cc = ccg;
207 if (frac > 1.0 - 1e-12)
216 double dy,
double dz,
int order = 1) {
220 Kokkos::parallel_for(
221 "peclet::flow::cc_open_ms", MD(space, {0, 0, 0}, {ext.
x, ext.
y, ext.
z}),
222 KOKKOS_LAMBDA(
int lx,
int ly,
int lz) {
223 const long i =
static_cast<long>(lx) +
static_cast<long>(ly) * ext.
x +
224 static_cast<long>(lz) *
static_cast<long>(ext.
x) * ext.
y;
231 Kokkos::parallel_for(
232 "peclet::flow::cc_open", MD(space, {0, 0, 0}, {ext.
x, ext.
y, ext.
z}),
233 KOKKOS_LAMBDA(
int lx,
int ly,
int lz) {
234 const long i =
static_cast<long>(lx) +
static_cast<long>(ly) * ext.
x +
235 static_cast<long>(lz) *
static_cast<long>(ext.
x) * ext.
y;
236 ox(i) =
ccFaceOpen(sdf, ext, lx - 0.5, ly, lz, 1, dx, dy, dz);
237 oy(i) =
ccFaceOpen(sdf, ext, lx, ly - 0.5, lz, 2, dx, dy, dz);
238 oz(i) =
ccFaceOpen(sdf, ext, lx, ly, lz - 0.5, 3, dx, dy, dz);
251 double dy,
double dz,
int order = 1) {
253 const int dims[3] = {ext.
x, ext.
y, ext.
z};
254 const long st[3] = {1, (long)ext.
x, (
long)ext.
x * ext.
y};
255 const int b = (a + 1) % 3, c = (a + 2) % 3;
256 const long sa = st[a], sb = st[b], sc = st[c];
257 const int bf = dims[a] - g;
258 const int type = a + 1;
260 Kokkos::parallel_for(
261 "peclet::flow::cc_open_high_face", MD(space, {0, 0}, {dims[b], dims[c]}),
262 KOKKOS_LAMBDA(
int p0,
int p1) {
264 q[a] = (double)bf - 0.5;
267 const long i = (long)p0 * sb + (
long)p1 * sc + (long)bf * sa;
268 oa(i) = (order >= 2) ?
ccFaceOpenMS(sdf, ext, q[0], q[1], q[2], type)
269 :
ccFaceOpen(sdf, ext, q[0], q[1], q[2], type, dx, dy, dz);
293 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>)
309 Kokkos::Profiling::pushRegion(region);
314 : on_(
Kokkos::Tools::profileLibraryLoaded()) {
316 Kokkos::Profiling::pushRegion(std::string(launch) + suffix);
320 Kokkos::Profiling::popRegion();
346#define PECLET_FLOW_OMP_SIMD _Pragma("omp simd")
348#define PECLET_FLOW_OMP_SIMD
351template <
bool Simd,
class F>
355 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>) {
356 const int ny = hi.
y - lo.y, nz = hi.
z - lo.z;
357 if (ny <= 0 || nz <= 0 || hi.
x <= lo.x)
359 const long rows = (long)ny * nz;
360 auto row = [=](
long r) {
361 const int y = lo.y + (int)(r % ny), z = lo.z + (int)(r / ny);
362 if constexpr (Simd) {
364 for (
int x = lo.x; x < hi.
x; ++x)
367 for (
int x = lo.x; x < hi.
x; ++x)
373 for (
long r = 0; r < rows; ++r)
377 Kokkos::parallel_for(name, Kokkos::RangePolicy<CCExec>(space, 0, rows), row);
379 Kokkos::parallel_for(name, MD(space, {lo.x, lo.y, lo.z}, {hi.
x, hi.
y, hi.
z}), f);
409 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>) {
410 if (rhi.
x <= rlo.
x || rhi.
y <= rlo.
y || rhi.
z <= rlo.
z)
412 auto clip = [](
int v,
int lo,
int hi) {
return v < lo ? lo : (v > hi ? hi : v); };
413 const C3 sl{clip(slo.
x, rlo.
x, rhi.
x), clip(slo.
y, rlo.
y, rhi.
y), clip(slo.
z, rlo.
z, rhi.
z)};
414 const C3 sh{clip(shi.
x, rlo.
x, rhi.
x), clip(shi.
y, rlo.
y, rhi.
y), clip(shi.
z, rlo.
z, rhi.
z)};
415 const bool skip = sl.x < sh.x && sl.y < sh.y && sl.z < sh.z;
416 const int nx = rhi.
x - rlo.
x, ny = rhi.
y - rlo.
y, nz = rhi.
z - rlo.
z;
419 const long rows = (long)ny * nz;
420 auto row = [=](
long t) {
run(rlo.
y + (
int)(t % ny), rlo.
z + (
int)(t / ny), rlo.
x, rhi.
x); };
423 for (
long t = 0; t < rows; ++t)
427 Kokkos::parallel_for(name, Kokkos::RangePolicy<CCExec>(space, 0, rows), row);
430 const int nzLo = sl.z - rlo.
z, nzHi = rhi.
z - sh.z, nzS = sh.z - sl.z;
431 const int nyLo = sl.y - rlo.
y, nyHi = rhi.
y - sh.y, nyS = sh.y - sl.y;
432 const int nxLo = sl.x - rlo.
x, nxHi = rhi.
x - sh.x;
433 const long nA = (long)(nzLo + nzHi) * ny;
434 const long nB = (long)nzS * (nyLo + nyHi);
435 const long nC = (nxLo + nxHi > 0) ? (
long)nzS * nyS : 0;
436 const long tasks = nA + nB + nC;
437 const long cells = (nA + nB) * nx + nC * (nxLo + nxHi);
438 const long runs = nC * ((nxLo > 0) + (nxHi > 0));
441 auto task = [=](
long t) {
443 const int zz = (int)(t / ny);
444 run(rlo.
y + (
int)(t % ny), zz < nzLo ? rlo.
z + zz : sh.z + (zz - nzLo), rlo.
x, rhi.
x);
449 const int nyb = nyLo + nyHi, yy = (int)(t % nyb);
450 run(yy < nyLo ? rlo.
y + yy : sh.y + (yy - nyLo), sl.z + (
int)(t / nyb), rlo.
x, rhi.
x);
454 const int ly = sl.y + (int)(t % nyS), lz = sl.z + (int)(t / nyS);
456 run(ly, lz, rlo.
x, sl.x);
458 run(ly, lz, sh.x, rhi.
x);
462 for (
long t = 0; t < tasks; ++t)
466 Kokkos::parallel_for(name, Kokkos::RangePolicy<CCExec>(space, 0, tasks), task);
468 (void)name, (
void)rlo, (void)rhi, (
void)slo, (void)shi, (
void)
run;
486 w.xm = ((m & 1) && lx == g) ? i + (
long)(n.x - 1) : i - 1;
487 w.xp = ((m & 1) && lx == g + n.x - 1) ? i - (
long)(n.x - 1) : i + 1;
488 w.ym = ((m & 2) && ly == g) ? i + (
long)(n.y - 1) * sy : i - sy;
489 w.yp = ((m & 2) && ly == g + n.y - 1) ? i - (
long)(n.y - 1) * sy : i + sy;
490 w.zm = ((m & 4) && lz == g) ? i + (
long)(n.z - 1) * sz : i - sz;
491 w.zp = ((m & 4) && lz == g + n.z - 1) ? i - (
long)(n.z - 1) * sz : i + sz;
499KOKKOS_INLINE_FUNCTION
void ccMaskRow(
int ly,
int lz,
int lx0,
int x1,
int step,
C3 e,
C3 n,
int g,
500 int m,
const Cell& cell) {
503 const long sy = e.x, sz = (long)e.x * e.y;
504 const long base = (long)ly * sy + (
long)lz * sz;
505 const long dym = ((m & 2) && ly == g) ? (
long)(n.y - 1) * sy : -sy;
506 const long dyp = ((m & 2) && ly == g + n.y - 1) ? -(
long)(n.y - 1) * sy : sy;
507 const long dzm = ((m & 4) && lz == g) ? (
long)(n.z - 1) * sz : -sz;
508 const long dzp = ((m & 4) && lz == g + n.z - 1) ? -(
long)(n.z - 1) * sz : sz;
509 auto scalar = [&](
int lx) {
510 const long i = (long)lx + base;
512 cell(i, w.xp, w.xm, w.yp, w.ym, w.zp, w.zm);
514 const int lxLast = g + n.x - 1;
515 int lo = lx0, hi = lx0 + ((x1 - 1 - lx0) / step) * step;
516 if ((m & 1) && lo == g) {
520 const bool peelLast = (m & 1) && hi == lxLast && hi >= lo;
524 for (
int lx = lo; lx <= hi; lx += step) {
525 const long i = (long)lx + base;
526 cell(i, i + 1, i - 1, i + dyp, i + dym, i + dzp, i + dzm);
541template <
class R,
bool = Kokkos::is_reducer_v<std::decay_t<R>>>
543 using type =
typename std::decay_t<R>::value_type;
547 using D = std::decay_t<R>;
548 template <
class T,
bool = Kokkos::is_view_v<T>>
554 using type =
typename T::non_const_value_type;
559template <
class F,
class...
R>
563 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>) {
564 const int ny = hi.
y - lo.y > 0 ? hi.
y - lo.y : 0, nz = hi.
z - lo.z > 0 ? hi.
z - lo.z : 0;
565 const long rows = hi.
x > lo.x ? (long)ny * nz : 0;
566 Kokkos::parallel_reduce(
567 name, Kokkos::RangePolicy<CCExec>(space, 0, rows),
569 const int y = lo.y + (int)(r % ny), z = lo.z + (int)(r / ny);
570 for (
int x = lo.x; x < hi.
x; ++x)
573 std::forward<R>(reducers)...);
575 Kokkos::parallel_reduce(name, MD(space, {lo.x, lo.y, lo.z}, {hi.
x, hi.
y, hi.
z}), f,
576 std::forward<R>(reducers)...);
590template <
class R,
bool = Kokkos::is_reducer_v<R>>
594template <
class T,
class S>
598 T
v[4] = {T(0), T(0), T(0), T(0)};
599 T
row()
const {
return (
v[0] +
v[1]) + (
v[2] +
v[3]); }
601template <
class F,
class... T>
602inline void laneRow(
const F& f,
int x0,
int x1,
int y,
int z, T&... acc) {
603 std::tuple<Lanes4<T>...> lanes;
607 for (; x + 3 < x1; x += 4) {
608 f(x, y, z, l.
v[0]...);
609 f(x + 1, y, z, l.
v[1]...);
610 f(x + 2, y, z, l.
v[2]...);
611 f(x + 3, y, z, l.
v[3]...);
613 for (
int k = 0; x < x1; ++x, ++k)
614 f(x, y, z, l.
v[k]...);
615 ((acc += l.
row()), ...);
620template <
class F,
class...
R>
623 "ccReduce3Lanes: sum reductions only (max/min keep ccReduce3)");
624 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>) {
626 const int ny = hi.
y - lo.y > 0 ? hi.
y - lo.y : 0, nz = hi.
z - lo.z > 0 ? hi.
z - lo.z : 0;
627 const long rows = hi.
x > lo.x ? (long)ny * nz : 0;
628 Kokkos::parallel_reduce(
629 name, Kokkos::RangePolicy<CCExec>(space, 0, rows),
631 const int y = lo.y + (int)(r % ny), z = lo.z + (int)(r / ny);
634 std::forward<R>(reducers)...);
636 ccReduce3(name, lo, hi, f, std::forward<R>(reducers)...);
HostSerialRegion & operator=(const HostSerialRegion &)=delete
HostSerialRegion(const char *launch, const char *suffix)
HostSerialRegion(const HostSerialRegion &)=delete
HostSerialRegion(const char *region)
#define PECLET_FLOW_OMP_SIMD
void ccRows3(const char *name, C3 lo, C3 hi, F f)
void laneRow(const F &f, int x0, int x1, int y, int z, T &... acc)
Kokkos::DefaultExecutionSpace CCExec
void ccForRows3(const char *name, C3 lo, C3 hi, F f)
CcNbrs ccMaskNbrs(int lx, int ly, int lz, C3 n, int g, long i, long sy, long sz, int m)
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< const double *, CCMem > CCConst
double ccFaceOpen(CCConst sdf, C3 ext, double fx, double fy, double fz, int type, double dx, double dy, double dz)
void ccReduce3(const char *name, C3 lo, C3 hi, F f, R &&... reducers)
constexpr long kHostSerialCellCutoff
double ccFaceOpenMS(CCConst sdf, C3 ext, double fx, double fy, double fz, int type)
double ccFractionCore(double sd, double sxp, double sxm, double syp, double sym, double szp, double szm, int type, double dx, double dy, double dz)
double ccTriFrac(double a, double b, double c)
void ccBoxShellRuns(const char *name, C3 rlo, C3 rhi, C3 slo, C3 shi, Run run)
void buildOpennessHighFace(CCField oa, CCConst sdf, C3 ext, int g, int a, double dx, double dy, double dz, int order=1)
constexpr long kShellRunCells
CCExec::memory_space CCMem
Kokkos::View< double *, CCMem > CCField
void ccFor3(const char *name, C3 lo, C3 hi, F f)
void ccMaskRow(int ly, int lz, int lx0, int x1, int step, C3 e, C3 n, int g, int m, const Cell &cell)
void ccReduce3Lanes(const char *name, C3 lo, C3 hi, F f, R &&... reducers)
double ccSampleExt(CCConst sdf, C3 ext, double x, double y, double z)
void buildOpenness(CCField ox, CCField oy, CCField oz, CCConst sdf, C3 ext, double dx, double dy, double dz, int order=1)
bool hostRunSerial(long cells)
Kokkos::MDRangePolicy< Exec, Kokkos::Rank< 2, Kokkos::Iterate::Left, Kokkos::Iterate::Left > > MDRange2
2-D range, first index fastest (both the tile order and the order within a tile).
flow — the multidimensional iteration policies: iteration order follows storage.
typename T::non_const_value_type type
typename Pick< D >::type type
typename std::decay_t< R >::value_type type
static double value(int gx, int gy, int gz)
static constexpr double F
static constexpr double R