flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
block_batch.hpp
Go to the documentation of this file.
1
21#ifndef PECLET_FLOW_VOF_BLOCK_BATCH_HPP
22#define PECLET_FLOW_VOF_BLOCK_BATCH_HPP
23
24#include <Kokkos_Core.hpp>
25
26#include "mac_stencils.hpp" // peclet::flow::SExec, SField, I3, L3
27#include "vof/advect_wy.hpp"
28#include "vof/curvature_field.hpp" // VofRawField
29
30namespace peclet::flow::vof {
31
33struct VofRawMask {
34 const unsigned char* p;
35 KOKKOS_INLINE_FUNCTION unsigned char operator()(long i) const { return p[i]; }
36};
37
41 double *c, *mx, *my, *mz, *al, *fl;
42 double* u[3];
43 unsigned char* cc;
44 const unsigned char* outside;
45 I3 e, n, o;
46 int g;
47 double dth;
48 double weps;
50 bool span[3];
51 unsigned char bcOwn[6];
52};
53
55inline constexpr int kVofBlockBatch = 16;
56
67
69KOKKOS_INLINE_FUNCTION int vofJobOf(const long* off, long t) {
70 int b = 0;
71 while (t >= off[b + 1])
72 ++b;
73 return b;
74}
75
77template <class F>
78inline long vofSetOffsets(VofBlockTable& T, F size) {
79 T.off[0] = 0;
80 for (int k = 0; k < T.nj; ++k)
81 T.off[k + 1] = T.off[k] + size(T.job[k]);
82 for (int k = T.nj; k < kVofBlockBatch; ++k)
83 T.off[k + 1] = T.off[T.nj];
84 return T.off[T.nj];
85}
86
87inline long vofJobLen(const VofBlockJob& J) {
88 return static_cast<long>(J.e.x) * J.e.y * J.e.z;
89}
90KOKKOS_INLINE_FUNCTION long vofJobInner(const VofBlockJob& J) {
91 return static_cast<long>(J.n.x) * J.n.y * J.n.z;
92}
93
94inline long vofJobGrown1(const VofBlockJob& J) {
95 return static_cast<long>(J.n.x + 2) * (J.n.y + 2) * (J.n.z + 2);
96}
97
98// ---- H-4: the host launch forms (`doc/vof_step_performance_design.md` §14.3) --------------------
99//
100// On a host backend GCC compiles a static `RangePolicy` to `schedule(static)`: one contiguous block
101// of the flat index per thread. The device forms above therefore serialise on a host -- a list
102// kernel launched over the upper bound has each block's active entries at the head of its range,
103// on one thread -- and every region kernel pays 64-bit div/mod per cell. The launchers below are
104// the host branches; each batched launcher selects them at compile time on its execution space's
105// memory space (as `ccdetail::ccRows3` does), so a device build never instantiates them and its
106// launches are untouched. Every host form visits the same cells with the same per-cell body, and
107// every body writes only its own cell(s) or adds integers atomically, so the state is bitwise the
108// device form's on the same host.
109
111template <class Exec>
112inline constexpr bool kVofHostExec = std::is_same_v<typename Exec::memory_space, Kokkos::HostSpace>;
113
116 long o[kVofBlockBatch + 1];
117};
118
123template <class Exec, class Cnt, class F>
124inline void vofHostListFor(const char* name, int nj, Cnt cnt, F body) {
125 Exec().fence(); // the counts were written by an earlier launch
127 C.o[0] = 0;
128 for (int k = 0; k < nj; ++k) {
129 const long n = cnt(k);
130 C.o[k + 1] = C.o[k] + (n > 0 ? n : 0);
131 }
132 for (int k = nj; k < kVofBlockBatch; ++k)
133 C.o[k + 1] = C.o[nj];
134 Kokkos::RangePolicy<Exec, Kokkos::Schedule<Kokkos::Dynamic>> pol(Exec(), 0, C.o[nj]);
135 pol.set_chunk_size(16);
136 Kokkos::parallel_for(name, pol, [=](const long t) {
137 const int k = vofJobOf(C.o, t);
138 body(k, t - C.o[k]);
139 });
140}
141
143template <class Tab>
144inline auto vofListCount(const Tab& T, LField start, LField end) {
145 return [=](int k) { return end(T.base + k) - start(T.base + k); };
146}
147
148// The host x loops of the region kernels carry `omp simd` where `ccFor3`'s contract holds (an
149// iteration writes only its own cell and reads no cell another iteration of the row writes); with
150// the host's -ffp-contract=off that is bit-identical to the scalar loop. Same definition as
151// `mac_cutcell.hpp`'s.
152#ifndef PECLET_FLOW_OMP_SIMD
153#if defined(_OPENMP)
154#define PECLET_FLOW_OMP_SIMD _Pragma("omp simd")
155#else
156#define PECLET_FLOW_OMP_SIMD
157#endif
158#endif
159
163};
164
168template <class Exec, class Rows, class F>
169inline void vofHostRowFor(const char* name, int nj, Rows rows, F row) {
171 R.off[0] = 0;
172 for (int k = 0; k < nj; ++k)
173 R.off[k + 1] = R.off[k] + rows(k);
174 for (int k = nj; k < kVofBlockBatch; ++k)
175 R.off[k + 1] = R.off[nj];
176 Kokkos::parallel_for(name, Kokkos::RangePolicy<Exec>(Exec(), 0, R.off[nj]), [=](const long t) {
177 const int k = vofJobOf(R.off, t);
178 row(k, t - R.off[k]);
179 });
180}
181
186template <class Exec, class Rows, class RowOf, class Hit>
187inline void vofHostRowScan(const char* name, int nj, int base, Rows rows, RowOf rowOf, Hit hit,
188 LField list, LField start, LField end) {
190 R.off[0] = 0;
191 for (int k = 0; k < nj; ++k)
192 R.off[k + 1] = R.off[k] + rows(k);
193 for (int k = nj; k < kVofBlockBatch; ++k)
194 R.off[k + 1] = R.off[nj];
195 Kokkos::parallel_scan(name, Kokkos::RangePolicy<Exec>(Exec(), 0, R.off[nj]),
196 [=](const long t, long& upd, const bool final) {
197 const int k = vofJobOf(R.off, t);
198 const long r = t - R.off[k];
199 int x0, x1;
200 const long i0 = rowOf(k, r, x0, x1);
201 long q = upd;
202 if (final) {
203 if (r == 0)
204 start(base + k) = q;
205 for (int x = x0; x < x1; ++x)
206 if (hit(k, i0 + x))
207 list(q++) = i0 + x;
208 if (t + 1 == R.off[k + 1])
209 end(base + k) = q;
210 } else {
211 for (int x = x0; x < x1; ++x)
212 if (hit(k, i0 + x))
213 ++q;
214 }
215 upd = q;
216 });
217}
218
219// ---- stage 1: the Courant numbers ---------------------------------------------------------------
220
225inline void vofBatchCfl(const VofBlockTable& T, SField out) {
226 using Team = Kokkos::TeamPolicy<SExec>;
227 Kokkos::parallel_for(
228 "vof::block::batch_cfl", Team(SExec(), T.nj, Kokkos::AUTO),
229 KOKKOS_LAMBDA(const typename Team::member_type& tm) {
230 const int k = tm.league_rank();
231 const VofBlockJob& J = T.job[k];
232 const I3 e = J.e, n = J.n;
233 const int g = J.g;
234 const long sx = 1, sy = e.x, sz = static_cast<long>(e.x) * e.y;
235 const long region = static_cast<long>(n.x) * n.y * n.z;
236 const VofRawField c{J.c}, u{J.u[0]}, v{J.u[1]}, w{J.u[2]};
237 const double weps = J.weps;
238 const bool band0 = J.interfaceCfl;
239 double m = 0.0;
240 Kokkos::parallel_reduce(
241 Kokkos::TeamThreadRange(tm, region),
242 [&](const long r, double& acc) {
243 const int x = static_cast<int>(r % n.x) + g;
244 const int y = static_cast<int>((r / n.x) % n.y) + g;
245 const int z = static_cast<int>(r / (static_cast<long>(n.x) * n.y)) + g;
246 const long i = L3(x, y, z, e);
247 if (!band0) { // WyAdvector::maxCourant
248 acc = Kokkos::fmax(acc, Kokkos::fabs(u(i)));
249 acc = Kokkos::fmax(acc, Kokkos::fabs(v(i)));
250 acc = Kokkos::fmax(acc, Kokkos::fabs(w(i)));
251 return;
252 }
253 // WyAdvector::maxCourantInterface
254 const double ci = c(i);
255 const bool band =
256 wyIsMixed(ci, weps) || wyColourJump(c(i - sx), ci, weps) ||
257 wyColourJump(c(i + sx), ci, weps) || wyColourJump(c(i - sy), ci, weps) ||
258 wyColourJump(c(i + sy), ci, weps) || wyColourJump(c(i - sz), ci, weps) ||
259 wyColourJump(c(i + sz), ci, weps);
260 if (!band)
261 return;
262 acc = Kokkos::fmax(acc, Kokkos::fabs(u(i)));
263 acc = Kokkos::fmax(acc, Kokkos::fabs(u(i - sx)));
264 acc = Kokkos::fmax(acc, Kokkos::fabs(v(i)));
265 acc = Kokkos::fmax(acc, Kokkos::fabs(v(i - sy)));
266 acc = Kokkos::fmax(acc, Kokkos::fabs(w(i)));
267 acc = Kokkos::fmax(acc, Kokkos::fabs(w(i - sz)));
268 },
269 Kokkos::Max<double>(m));
270 Kokkos::single(Kokkos::PerTeam(tm), [&]() {
271 // the two return expressions of maxCourant / maxCourantInterface
272 out(T.base + k) = band0 ? Kokkos::fmax(m, 0.0) * J.dth : m * J.dth;
273 });
274 });
275}
276
277// ---- stage 2: the frozen dilation flag ----------------------------------------------------------
278
281template <class Exec = SExec>
282inline void vofBatchFreeze(const VofBlockTable& T) {
283 if constexpr (kVofHostExec<Exec>) {
285 "vof::block::batch_freeze", T.nj,
286 [&](const int k) { return static_cast<long>(T.job[k].e.y) * T.job[k].e.z; },
287 [=](const int k, const long r) {
288 const VofBlockJob& J = T.job[k];
289 const long i0 = r * J.e.x; // extended row r = (r % e.y, r / e.y)
290 const double* c = J.c;
291 unsigned char* cc = J.cc;
292 PECLET_FLOW_OMP_SIMD
293 for (int x = 0; x < J.e.x; ++x)
294 cc[i0 + x] = c[i0 + x] > 0.5 ? 1u : 0u;
295 });
296 return;
297 }
298 Kokkos::parallel_for(
299 "vof::block::batch_freeze", Kokkos::RangePolicy<SExec>(SExec(), 0, T.off[T.nj]),
300 KOKKOS_LAMBDA(const long t) {
301 const int k = vofJobOf(T.off, t);
302 const VofBlockJob& J = T.job[k];
303 const long i = t - T.off[k];
304 J.cc[i] = J.c[i] > 0.5 ? 1u : 0u;
305 });
306}
307
308// ---- stage 3a/b: the worklist and the PLIC reconstruction --------------------------------------
309
314template <class Exec = SExec>
315inline void vofBatchWorklist(const VofBlockTable& T, LField list, LField start, LField end) {
316 if constexpr (kVofHostExec<Exec>) { // H-4(d): the grown-by-one rows
318 "vof::block::batch_worklist", T.nj, T.base,
319 [&](const int k) { return static_cast<long>(T.job[k].n.y + 2) * (T.job[k].n.z + 2); },
320 [=](const int k, const long r, int& x0, int& x1) {
321 const VofBlockJob& J = T.job[k];
322 const int ry = J.n.y + 2, g = J.g;
323 x0 = g - 1;
324 x1 = g + J.n.x + 1;
325 return L3(0, g - 1 + static_cast<int>(r % ry), g - 1 + static_cast<int>(r / ry), J.e);
326 },
327 [=](const int k, const long i) { return wyIsMixed(T.job[k].c[i], T.job[k].weps); }, list,
328 start, end);
329 return;
330 }
331 Kokkos::parallel_scan(
332 "vof::block::batch_worklist", Kokkos::RangePolicy<SExec>(SExec(), 0, T.off[T.nj]),
333 KOKKOS_LAMBDA(const long t, long& upd, const bool final) {
334 const int k = vofJobOf(T.off, t);
335 const VofBlockJob& J = T.job[k];
336 const long r = t - T.off[k];
337 const int rx = J.n.x + 2, ry = J.n.y + 2;
338 const int ix = static_cast<int>(r % rx);
339 const int iy = static_cast<int>((r / rx) % ry);
340 const int iz = static_cast<int>(r / (static_cast<long>(rx) * ry));
341 const int g = J.g;
342 const long i = L3(g - 1 + ix, g - 1 + iy, g - 1 + iz, J.e);
343 const bool mixed = wyIsMixed(J.c[i], J.weps);
344 if (final) {
345 if (r == 0)
346 start(T.base + k) = upd;
347 if (mixed)
348 list(upd) = i;
349 if (t + 1 == T.off[k + 1])
350 end(T.base + k) = upd + (mixed ? 1 : 0);
351 }
352 if (mixed)
353 ++upd;
354 });
355}
356
359template <class Exec = SExec>
360inline void vofBatchPlic(const VofBlockTable& T, LField list, LField start, LField end) {
361 if constexpr (kVofHostExec<Exec>) {
362 vofHostListFor<Exec>("vof::block::batch_plic", T.nj, vofListCount(T, start, end),
363 [=](const int k, const long t) {
364 const long q = start(T.base + k) + t;
365 const VofBlockJob& J = T.job[k];
366 const long sy = J.e.x, sz = static_cast<long>(J.e.x) * J.e.y;
367 wyReconstructCell(VofRawField{J.c}, list(q), sy, sz, VofRawField{J.mx},
369 VofRawField{J.al});
370 });
371 return;
372 }
373 Kokkos::parallel_for(
374 "vof::block::batch_plic", Kokkos::RangePolicy<SExec>(SExec(), 0, T.off[T.nj]),
375 KOKKOS_LAMBDA(const long t) {
376 const int k = vofJobOf(T.off, t);
377 const long s0 = start(T.base + k);
378 const long q = s0 + (t - T.off[k]);
379 if (q >= end(T.base + k))
380 return;
381 const VofBlockJob& J = T.job[k];
382 const long sy = J.e.x, sz = static_cast<long>(J.e.x) * J.e.y;
383 wyReconstructCell(VofRawField{J.c}, list(q), sy, sz, VofRawField{J.mx}, VofRawField{J.my},
385 });
386}
387
388// ---- stage 3c/d: fluxes and the sweep update ----------------------------------------------------
389
391inline long vofJobFaces(const VofBlockJob& J, int d) {
392 return static_cast<long>(J.n.x + (d == 0)) * (J.n.y + (d == 1)) * (J.n.z + (d == 2));
393}
394
396KOKKOS_INLINE_FUNCTION void vofBatchFluxCell(const VofBlockJob& J, int d, long p) {
397 const I3 e = J.e;
398 const long sd = d == 0 ? 1 : (d == 1 ? static_cast<long>(e.x) : static_cast<long>(e.x) * e.y);
399 const VofRawField c{J.c}, mx{J.mx}, my{J.my}, mz{J.mz}, al{J.al}, u{J.u[d]};
400 if (J.outside != nullptr) {
401 J.fl[p] =
402 wyFaceFluxBc(u(p) * J.dth, p, sd, d, c, mx, my, mz, al, VofRawMask{J.outside}, J.weps);
403 return;
404 }
405 J.fl[p] = wyFaceFlux(u(p) * J.dth, p, sd, d, c, mx, my, mz, al, J.weps);
406}
407
411template <class Exec = SExec>
412inline void vofBatchFlux(const VofBlockTable& T, int d) {
413 if constexpr (kVofHostExec<Exec>) {
415 "vof::block::batch_flux", T.nj,
416 [&](const int k) {
417 const VofBlockJob& J = T.job[k];
418 return static_cast<long>(J.n.y + (d == 1)) * (J.n.z + (d == 2));
419 },
420 [=](const int k, const long r) {
421 const VofBlockJob& J = T.job[k];
422 const int ay = J.n.y + (d == 1), g = J.g;
423 const int y = static_cast<int>(r % ay) + g - (d == 1);
424 const int z = static_cast<int>(r / ay) + g - (d == 2);
425 const long i0 = L3(0, y, z, J.e);
426 const int x0 = g - (d == 0), x1 = g + J.n.x;
427 PECLET_FLOW_OMP_SIMD
428 for (int x = x0; x < x1; ++x)
429 vofBatchFluxCell(J, d, i0 + x);
430 });
431 return;
432 }
433 Kokkos::parallel_for(
434 "vof::block::batch_flux", Kokkos::RangePolicy<SExec>(SExec(), 0, T.off[T.nj]),
435 KOKKOS_LAMBDA(const long t) {
436 const int k = vofJobOf(T.off, t);
437 const VofBlockJob& J = T.job[k];
438 const long r = t - T.off[k];
439 const int ax = J.n.x + (d == 0), ay = J.n.y + (d == 1);
440 const int g = J.g;
441 const int x = static_cast<int>(r % ax) + g - (d == 0);
442 const int y = static_cast<int>((r / ax) % ay) + g - (d == 1);
443 const int z = static_cast<int>(r / (static_cast<long>(ax) * ay)) + g - (d == 2);
444 vofBatchFluxCell(J, d, L3(x, y, z, J.e));
445 });
446}
447
453inline void vofBatchBcLedger(const VofBlockTable& T, int d, SField out, int stride, int slot0) {
454 Kokkos::parallel_for(
455 "vof::block::batch_bcvol", Kokkos::RangePolicy<SExec>(SExec(), 0, 2 * T.nj),
456 KOKKOS_LAMBDA(const long t) {
457 const int k = static_cast<int>(t / 2), s = static_cast<int>(t % 2);
458 const VofBlockJob& J = T.job[k];
459 const int f = 2 * d + s;
460 if (J.outside == nullptr || !J.bcOwn[f])
461 return;
462 const I3 e = J.e, n = J.n;
463 const int g = J.g;
464 const int b = (d + 1) % 3, c2 = (d + 2) % 3;
465 const long st[3] = {1, e.x, static_cast<long>(e.x) * e.y};
466 const long sd = st[d], sb = st[b], sc = st[c2];
467 const int nn[3] = {n.x, n.y, n.z};
468 const int fa = (s == 0) ? (g - 1) : (g + nn[d] - 1);
469 double acc = 0.0;
470 for (int p1 = g; p1 < g + nn[c2]; ++p1)
471 for (int p0 = g; p0 < g + nn[b]; ++p0)
472 acc += J.fl[static_cast<long>(p0) * sb + static_cast<long>(p1) * sc +
473 static_cast<long>(fa) * sd];
474 out(static_cast<long>(stride) * (T.base + k) + slot0 + f) += (s == 0) ? acc : -acc;
475 });
476}
477
479KOKKOS_INLINE_FUNCTION void vofBatchUpdateCell(const VofBlockJob& J, int d, long i) {
480 const I3 e = J.e;
481 const long sd = d == 0 ? 1 : (d == 1 ? static_cast<long>(e.x) : static_cast<long>(e.x) * e.y);
482 const double dth = J.dth;
483 const VofRawField c{J.c}, fl{J.fl}, u{J.u[d]};
484 const unsigned char* cc = J.cc;
485 // The dilation term must scale the SAME uf by the SAME dt/h as the flux, or the exact
486 // cancellation in full cells (advect_wy.hpp header) is lost to rounding.
487 const double aP = u(i) * dth, aM = u(i - sd) * dth;
488 const double dil = cc[i] ? (aP - aM) : 0.0;
489 c(i) = c(i) + (fl(i - sd) - fl(i)) + dil;
490}
491
494template <class Exec = SExec>
495inline void vofBatchUpdate(const VofBlockTable& T, int d) {
496 if constexpr (kVofHostExec<Exec>) {
498 "vof::block::batch_update", T.nj,
499 [&](const int k) { return static_cast<long>(T.job[k].n.y) * T.job[k].n.z; },
500 [=](const int k, const long r) {
501 const VofBlockJob& J = T.job[k];
502 const int g = J.g;
503 const long i0 =
504 L3(0, static_cast<int>(r % J.n.y) + g, static_cast<int>(r / J.n.y) + g, J.e);
505 PECLET_FLOW_OMP_SIMD
506 for (int x = g; x < g + J.n.x; ++x)
507 vofBatchUpdateCell(J, d, i0 + x);
508 });
509 return;
510 }
511 Kokkos::parallel_for(
512 "vof::block::batch_update", Kokkos::RangePolicy<SExec>(SExec(), 0, T.off[T.nj]),
513 KOKKOS_LAMBDA(const long t) {
514 const int k = vofJobOf(T.off, t);
515 const VofBlockJob& J = T.job[k];
516 const long r = t - T.off[k];
517 const I3 e = J.e, n = J.n;
518 const int g = J.g;
519 const int x = static_cast<int>(r % n.x) + g;
520 const int y = static_cast<int>((r / n.x) % n.y) + g;
521 const int z = static_cast<int>(r / (static_cast<long>(n.x) * n.y)) + g;
522 vofBatchUpdateCell(J, d, L3(x, y, z, e));
523 });
524}
525
526// ---- stage 3e: the block ghost policy (`VofBlockSet::fillBlockGhosts`), pass by pass ------------
527
531template <class Exec = SExec>
532inline void vofBatchGhostZero(const VofBlockTable& T) {
533 if constexpr (kVofHostExec<Exec>) {
535 "vof::block::batch_ghost_zero", T.nj,
536 [&](const int k) {
537 const VofBlockJob& J = T.job[k];
538 return static_cast<long>(J.e.z - J.n.z) * J.e.y +
539 static_cast<long>(J.n.z) * (J.e.y - J.n.y) + static_cast<long>(J.n.z) * J.n.y;
540 },
541 [=](const int k, const long r) {
542 const VofBlockJob& J = T.job[k];
543 const I3 e = J.e, n = J.n;
544 const int g = J.g;
545 const long rZ = static_cast<long>(e.z - n.z) * e.y,
546 rY = static_cast<long>(n.z) * (e.y - n.y);
547 double* c = J.c;
548 if (r < rZ + rY) { // a whole ghost row
549 int y, z;
550 if (r < rZ) { // z-ghost plane row
551 const int l = static_cast<int>(r / e.y);
552 y = static_cast<int>(r - static_cast<long>(l) * e.y);
553 z = l < g ? l : n.z + l;
554 } else { // y-ghost row of the inner z range
555 const long t2 = r - rZ;
556 const int zi = static_cast<int>(t2 / (e.y - n.y));
557 const int l = static_cast<int>(t2 - static_cast<long>(zi) * (e.y - n.y));
558 y = l < g ? l : n.y + l;
559 z = g + zi;
560 }
561 const long i0 = L3(0, y, z, e);
562 PECLET_FLOW_OMP_SIMD
563 for (int x = 0; x < e.x; ++x)
564 c[i0 + x] = 0.0;
565 return;
566 }
567 const long t3 = r - rZ - rY; // the x-ghost runs of inner row (yi, zi)
568 const int zi = static_cast<int>(t3 / n.y),
569 yi = static_cast<int>(t3 - static_cast<long>(zi) * n.y);
570 const long i0 = L3(0, g + yi, g + zi, e);
571 for (int x = 0; x < g; ++x)
572 c[i0 + x] = 0.0;
573 for (int x = g + n.x; x < e.x; ++x)
574 c[i0 + x] = 0.0;
575 });
576 return;
577 }
578 Kokkos::parallel_for(
579 "vof::block::batch_ghost_zero", Kokkos::RangePolicy<SExec>(SExec(), 0, T.off[T.nj]),
580 KOKKOS_LAMBDA(const long t) {
581 const int k = vofJobOf(T.off, t);
582 const VofBlockJob& J = T.job[k];
583 const long i = t - T.off[k];
584 const I3 e = J.e, n = J.n;
585 const int g = J.g;
586 const int x = static_cast<int>(i % e.x);
587 const int y = static_cast<int>((i / e.x) % e.y);
588 const int z = static_cast<int>(i / (static_cast<long>(e.x) * e.y));
589 if (x >= g && x < g + n.x && y >= g && y < g + n.y && z >= g && z < g + n.z)
590 return;
591 J.c[i] = 0.0;
592 });
593}
594
600template <class Exec = SExec>
601inline void vofBatchPeriodic(const VofBlockTable& T, int a) {
602 if constexpr (kVofHostExec<Exec>) {
604 "vof::block::batch_periodic", T.nj,
605 [&](const int k) {
606 const I3 e = T.job[k].e;
607 const int g = T.job[k].g;
608 return a == 0 ? static_cast<long>(e.y) * e.z
609 : (a == 1 ? 2L * g * e.z : static_cast<long>(e.y) * 2 * g);
610 },
611 [=](const int k, const long r) {
612 const VofBlockJob& J = T.job[k];
613 const I3 e = J.e;
614 const int g = J.g;
615 double* f = J.c;
616 if (a == 0) { // extended row r: the g x-ghosts at each end
617 const int N = e.x - 2 * g;
618 const long base = r * e.x;
619 for (int gl = 0; gl < g; ++gl) {
620 f[base + gl] = f[base + gl + N];
621 f[base + g + N + gl] = f[base + g + gl];
622 }
623 return;
624 }
625 long dst, src;
626 if (a == 1) { // ghost row (l, z): y = l (low) or g + N + (l - g) (high)
627 const int N = e.y - 2 * g;
628 const int z = static_cast<int>(r / (2 * g)), l = static_cast<int>(r % (2 * g));
629 const int y = l < g ? l : N + l, sy = l < g ? l + N : l; // high: y - N = g + (l - g)
630 dst = L3(0, y, z, e);
631 src = L3(0, sy, z, e);
632 } else { // ghost row (y, l)
633 const int N = e.z - 2 * g;
634 const int l = static_cast<int>(r / e.y), y = static_cast<int>(r % e.y);
635 const int z = l < g ? l : N + l, sz = l < g ? l + N : l;
636 dst = L3(0, y, z, e);
637 src = L3(0, y, sz, e);
638 }
640 for (int x = 0; x < e.x; ++x)
641 f[dst + x] = f[src + x];
642 });
643 return;
644 }
645 Kokkos::parallel_for(
646 "vof::block::batch_periodic", Kokkos::RangePolicy<SExec>(SExec(), 0, T.off[T.nj]),
647 KOKKOS_LAMBDA(const long t) {
648 const int k = vofJobOf(T.off, t);
649 const VofBlockJob& J = T.job[k];
650 const long r = t - T.off[k];
651 const I3 e = J.e;
652 const int g = J.g;
653 const long st[3] = {1, e.x, static_cast<long>(e.x) * e.y};
654 const int dims[3] = {e.x, e.y, e.z};
655 const int b = (a + 1) % 3, c = (a + 2) % 3;
656 const long sa = st[a], sb = st[b], sc = st[c];
657 const int N = dims[a] - 2 * g;
658 const int p0 = static_cast<int>(r % dims[b]);
659 const int p1 = static_cast<int>(r / dims[b]);
660 const VofRawField f{J.c};
661 const long base = static_cast<long>(p0) * sb + static_cast<long>(p1) * sc;
662 for (int gl = 0; gl < g; ++gl) {
663 f(base + static_cast<long>(gl) * sa) = f(base + static_cast<long>(gl + N) * sa);
664 f(base + static_cast<long>(g + N + gl) * sa) = f(base + static_cast<long>(g + gl) * sa);
665 }
666 });
667}
668
670KOKKOS_INLINE_FUNCTION void vofBatchClampCell(const VofBlockJob& J, int x, int y, int z, int q0,
671 int q1, int q2, bool p0, bool p1, bool p2) {
672 const I3 e = J.e, o = J.o;
673 const int g = J.g;
674 const int gx = x - g + o.x, gy = y - g + o.y, gz = z - g + o.z;
675 const int cx = p0 ? gx : (gx < 0 ? 0 : (gx >= q0 ? q0 - 1 : gx));
676 const int cy = p1 ? gy : (gy < 0 ? 0 : (gy >= q1 ? q1 - 1 : gy));
677 const int cz = p2 ? gz : (gz < 0 ? 0 : (gz >= q2 ? q2 - 1 : gz));
678 if (cx != gx || cy != gy || cz != gz)
679 J.c[L3(x, y, z, e)] = J.c[L3(cx - o.x + g, cy - o.y + g, cz - o.z + g, e)];
680}
681
686template <class Exec = SExec>
687inline void vofBatchClamp(const VofBlockTable& T, I3 gs, bool px, bool py, bool pz) {
688 const bool p0 = px, p1 = py, p2 = pz;
689 const int q0 = gs.x, q1 = gs.y, q2 = gs.z;
690 if constexpr (kVofHostExec<Exec>) {
692 "vof::block::batch_clamp", T.nj,
693 [&](const int k) { return static_cast<long>(T.job[k].e.y) * T.job[k].e.z; },
694 [=](const int k, const long r) {
695 const VofBlockJob& J = T.job[k];
696 const I3 e = J.e, o = J.o;
697 const int g = J.g;
698 const int y = static_cast<int>(r % e.y), z = static_cast<int>(r / e.y);
699 const int gy = y - g + o.y, gz = z - g + o.z, gx0 = o.x - g, gx1 = o.x - g + e.x;
700 if ((p1 || (gy >= 0 && gy < q1)) && (p2 || (gz >= 0 && gz < q2)) &&
701 (p0 || (gx0 >= 0 && gx1 <= q0)))
702 return; // inside on every non-periodic axis: nothing in this row writes
703 for (int x = 0; x < e.x; ++x)
704 vofBatchClampCell(J, x, y, z, q0, q1, q2, p0, p1, p2);
705 });
706 return;
707 }
708 Kokkos::parallel_for(
709 "vof::block::batch_clamp", Kokkos::RangePolicy<SExec>(SExec(), 0, T.off[T.nj]),
710 KOKKOS_LAMBDA(const long t) {
711 const int k = vofJobOf(T.off, t);
712 const VofBlockJob& J = T.job[k];
713 const long i = t - T.off[k];
714 const I3 e = J.e;
715 const int x = static_cast<int>(i % e.x);
716 const int y = static_cast<int>((i / e.x) % e.y);
717 const int z = static_cast<int>(i / (static_cast<long>(e.x) * e.y));
718 vofBatchClampCell(J, x, y, z, q0, q1, q2, p0, p1, p2);
719 });
720}
721
722// ---- stage 4: marker debris and sub-wispEps residue (`VofBlockSet::debrisPassBatchImpl`) --------
723
727template <class SF>
728KOKKOS_INLINE_FUNCTION bool vofDebrisCell(const SF& c, long i, long sy, long sz, double ieps,
729 double cfull) {
730 if (!vofIsInterface(c(i), ieps))
731 return false;
732 for (int oz = -2; oz <= 2; ++oz)
733 for (int oy = -2; oy <= 2; ++oy)
734 for (int ox = -2; ox <= 2; ++ox)
735 if (c(i + ox + oy * sy + oz * sz) >= cfull)
736 return false; // attached: a cell at least half full of this marker within two
737 return true;
738}
739
741template <class SF>
742KOKKOS_INLINE_FUNCTION bool vofAttachedCell(const SF& c, long i, long sy, long sz, double ieps,
743 double cfull) {
744 if (!vofIsInterface(c(i), ieps))
745 return false;
746 bool attached = false;
747 for (int oz = -2; oz <= 2 && !attached; ++oz)
748 for (int oy = -2; oy <= 2 && !attached; ++oy)
749 for (int ox = -2; ox <= 2 && !attached; ++ox)
750 attached = c(i + ox + oy * sy + oz * sz) >= cfull;
751 return attached;
752}
753
755KOKKOS_INLINE_FUNCTION bool vofResidueCell(double ci, double weps) {
756 return ci != 0.0 && Kokkos::fabs(ci) <= weps;
757}
758
762template <class Exec = SExec>
763inline void vofBatchMark(const VofBlockTable& T, int kind, double ieps, double cfull, double weps,
764 LField list, LField start, LField end) {
765 if constexpr (kVofHostExec<Exec>) { // H-4(d): the inner rows
767 "vof::block::batch_debris_mark", T.nj, T.base,
768 [&](const int k) { return static_cast<long>(T.job[k].n.y) * T.job[k].n.z; },
769 [=](const int k, const long r, int& x0, int& x1) {
770 const VofBlockJob& J = T.job[k];
771 const int g = J.g;
772 x0 = g;
773 x1 = g + J.n.x;
774 return L3(0, g + static_cast<int>(r % J.n.y), g + static_cast<int>(r / J.n.y), J.e);
775 },
776 [=](const int k, const long i) {
777 const VofBlockJob& J = T.job[k];
778 const long sy = J.e.x, sz = static_cast<long>(J.e.x) * J.e.y;
779 const VofRawField c{J.c};
780 return kind == 0 ? vofDebrisCell(c, i, sy, sz, ieps, cfull)
781 : kind == 1 ? vofResidueCell(c(i), weps)
782 : vofAttachedCell(c, i, sy, sz, ieps, cfull);
783 },
784 list, start, end);
785 return;
786 }
787 Kokkos::parallel_scan(
788 "vof::block::batch_debris_mark", Kokkos::RangePolicy<SExec>(SExec(), 0, T.off[T.nj]),
789 KOKKOS_LAMBDA(const long t, long& upd, const bool final) {
790 const int k = vofJobOf(T.off, t);
791 const VofBlockJob& J = T.job[k];
792 const long r = t - T.off[k];
793 const int nx = J.n.x, ny = J.n.y, g = J.g;
794 const int ix = static_cast<int>(r % nx);
795 const int iy = static_cast<int>((r / nx) % ny);
796 const int iz = static_cast<int>(r / (static_cast<long>(nx) * ny));
797 const long i = L3(g + ix, g + iy, g + iz, J.e);
798 const long sy = J.e.x, sz = static_cast<long>(J.e.x) * J.e.y;
799 const VofRawField c{J.c};
800 const bool hit = kind == 0 ? vofDebrisCell(c, i, sy, sz, ieps, cfull)
801 : kind == 1 ? vofResidueCell(c(i), weps)
802 : vofAttachedCell(c, i, sy, sz, ieps, cfull);
803 if (final) {
804 if (r == 0)
805 start(T.base + k) = upd;
806 if (hit)
807 list(upd) = i;
808 if (t + 1 == T.off[k + 1])
809 end(T.base + k) = upd + (hit ? 1 : 0);
810 }
811 if (hit)
812 ++upd;
813 });
814}
815
829
833 double vaMin;
834};
835
838inline void vofBatchDebrisSums(const VofBlockTable& T, VofDebrisFlags F, LField listD, LField listR,
839 LField listA, LField sD0, LField eD0, LField sR0, LField eR0,
840 LField sA0, LField eA0, SField out, int stride, int slot0) {
841 Kokkos::parallel_for(
842 "vof::block::batch_debris_sums", Kokkos::RangePolicy<SExec>(SExec(), 0, T.nj),
843 KOKKOS_LAMBDA(const long q) {
844 const int j = T.base + static_cast<int>(q);
845 const VofRawField c{T.job[q].c};
846 const long nD = F.doDebris ? eD0(j) - sD0(j) : 0;
847 const long nR = F.doResidue ? eR0(j) - sR0(j) : 0;
848 const bool removeD = F.doDebris && F.remove && nD > 0;
849 const bool pending = removeD || nR > 0;
850 const long ob = static_cast<long>(stride) * j + slot0;
851 double sD = 0.0, sR = 0.0, W = 0.0, VA = 0.0, dV = 0.0;
852 const long d0 = F.doDebris ? sD0(j) : 0;
853 for (long t = 0; t < nD; ++t)
854 sD += c(listD(d0 + t));
855 double acted = 0.0;
856 if (pending) {
857 const long r0 = F.doResidue ? sR0(j) : 0, a0 = sA0(j), nA = eA0(j) - sA0(j);
858 for (long t = 0; t < nR; ++t)
859 sR += c(listR(r0 + t));
860 for (long t = 0; t < nA; ++t) {
861 const double ca = c(listA(a0 + t));
862 W += ca * (1.0 - ca);
863 VA += ca;
864 }
865 acted = (VA < F.vaMin || !(W > 0.0)) ? 0.0 : 1.0;
866 // the removed volume, in the fixed order D (if removing) then R -- the serial order
867 const long nDr = removeD ? nD : 0;
868 for (long t = 0; t < nDr; ++t)
869 dV += c(listD(d0 + t));
870 for (long t = 0; t < nR; ++t)
871 dV += c(listR(r0 + t));
872 }
873 out(ob + kDbSumD) = sD;
874 out(ob + kDbSumR) = sR;
875 out(ob + kDbW) = W;
876 out(ob + kDbVA) = VA;
877 out(ob + kDbDV) = dV;
878 out(ob + kDbActed) = acted;
879 out(ob + kDbND) = static_cast<double>(nD);
880 out(ob + kDbNR) = static_cast<double>(nR);
881 out(ob + kDbPending) = pending ? 1.0 : 0.0;
882 });
883}
884
887KOKKOS_INLINE_FUNCTION long vofDebrisActCount(const VofDebrisFlags& F, LField sA0, LField eA0,
888 SField out, long ob, int j) {
889 if (out(ob + kDbActed) == 0.0)
890 return 0;
891 const long nD = static_cast<long>(out(ob + kDbND)), nR = static_cast<long>(out(ob + kDbNR));
892 const bool removeD = F.doDebris && F.remove && nD > 0;
893 const long nDr = removeD ? nD : 0, nA = eA0(j) - sA0(j);
894 return nDr + nR + nA;
895}
896
899KOKKOS_INLINE_FUNCTION void vofDebrisActEntry(const VofBlockTable& T, const VofDebrisFlags& F,
900 int k, long t, LField listD, LField listR,
901 LField listA, LField sD0, LField sR0, LField sA0,
902 LField eA0, SField out, int stride, int slot0,
903 SField ex, Kokkos::View<int*, SMem> capf) {
904 const int j = T.base + k;
905 const long ob = static_cast<long>(stride) * j + slot0;
906 if (out(ob + kDbActed) == 0.0)
907 return;
908 const long nD = static_cast<long>(out(ob + kDbND)), nR = static_cast<long>(out(ob + kDbNR));
909 const bool removeD = F.doDebris && F.remove && nD > 0;
910 const long nDr = removeD ? nD : 0, nA = eA0(j) - sA0(j);
911 const long nZ = nDr + nR, nTot = nZ + nA;
912 if (t >= nTot)
913 return;
914 const VofRawField c{T.job[k].c};
915 if (t < nDr) {
916 c(listD(sD0(j) + t)) = 0.0;
917 return;
918 }
919 if (t < nZ) {
920 c(listR(sR0(j) + t - nDr)) = 0.0;
921 return;
922 }
923 const double dV = out(ob + kDbDV), W = out(ob + kDbW);
924 const long ta = t - nZ;
925 const long i = listA(sA0(j) + ta);
926 const double ca = c(i);
927 const double d = dV * (ca * (1.0 - ca)) / W;
928 double cn = ca + d;
929 double e = 0.0;
930 if (cn > 1.0) {
931 e = cn - 1.0;
932 cn = 1.0;
933 Kokkos::atomic_max(&capf(j), 1);
934 }
935 ex(T.off[k] + ta) = e;
936 c(i) = cn;
937}
938
943template <class Exec = SExec>
945 LField listR, LField listA, LField sD0, LField sR0, LField sA0,
946 LField eA0, SField out, int stride, int slot0, SField ex,
947 Kokkos::View<int*, SMem> capf) {
948 if constexpr (kVofHostExec<Exec>) {
950 "vof::block::batch_debris_write", T.nj,
951 [&](const int k) {
952 const int j = T.base + k;
953 return vofDebrisActCount(F, sA0, eA0, out, static_cast<long>(stride) * j + slot0, j);
954 },
955 [=](const int k, const long t) {
956 vofDebrisActEntry(T, F, k, t, listD, listR, listA, sD0, sR0, sA0, eA0, out, stride, slot0,
957 ex, capf);
958 });
959 return;
960 }
961 Kokkos::parallel_for(
962 "vof::block::batch_debris_write", Kokkos::RangePolicy<SExec>(SExec(), 0, T.off[T.nj]),
963 KOKKOS_LAMBDA(const long tt) {
964 const int k = vofJobOf(T.off, tt);
965 vofDebrisActEntry(T, F, k, tt - T.off[k], listD, listR, listA, sD0, sR0, sA0, eA0, out,
966 stride, slot0, ex, capf);
967 });
968}
969
972inline void vofBatchDebrisLost(const VofBlockTable& T, LField sA0, LField eA0, SField out,
973 int stride, int slot0, SField ex, Kokkos::View<int*, SMem> capf) {
974 Kokkos::parallel_for(
975 "vof::block::batch_debris_lost", Kokkos::RangePolicy<SExec>(SExec(), 0, T.nj),
976 KOKKOS_LAMBDA(const long q) {
977 const int j = T.base + static_cast<int>(q);
978 const long ob = static_cast<long>(stride) * j + slot0;
979 if (out(ob + kDbActed) == 0.0)
980 return;
981 double lost = 0.0;
982 if (capf(j) > 0) {
983 const long nA = eA0(j) - sA0(j), e0 = T.off[q];
984 double a = 0.0; // list order, as the serial loop accumulated it
985 for (long t = 0; t < nA; ++t)
986 a += ex(e0 + t);
987 lost = a;
988 }
989 out(ob + kDbDV) = lost;
990 });
991}
992
993// ---- the tiled job reductions (device): bubble boxes and statistics -----------------------------
994
1001inline constexpr long kVofTile = 512;
1002
1003inline long vofTiles(const VofBlockJob& J) {
1004 return (vofJobInner(J) + kVofTile - 1) / kVofTile;
1005}
1006
1008inline void vofEnsureTileScratch(SField& tile, long len) {
1009 if (static_cast<long>(tile.extent(0)) < len)
1010 tile = SField(Kokkos::view_alloc(Kokkos::WithoutInitializing, "vof::block::batch_tile"), len);
1011}
1012
1013// ---- stage 5: the bubble boxes (`VofBlockSet::bubbleBox`) ------------------------------------
1014
1027template <class Exec = SExec>
1028inline void vofBatchBox(const VofBlockTable& T, double eps, SField out, int stride, int slot0,
1029 SField& tile) {
1030 if constexpr (kVofHostExec<Exec>) {
1031 int box[6 * kVofBlockBatch];
1032 for (int k = 0; k < T.nj; ++k)
1033 for (int a = 0; a < 6; ++a)
1034 box[6 * k + a] =
1035 a < 3 ? Kokkos::reduction_identity<int>::min() : Kokkos::reduction_identity<int>::max();
1036 int* bx = box;
1038 "vof::block::batch_bbox", T.nj,
1039 [&](const int k) { return static_cast<long>(T.job[k].n.y) * T.job[k].n.z; },
1040 [=](const int k, const long r) {
1041 const VofBlockJob& J = T.job[k];
1042 const I3 e = J.e, n = J.n;
1043 const int g = J.g;
1044 const int py = static_cast<int>(r % n.y), pz = static_cast<int>(r / n.y);
1045 const long i0 = L3(g, py + g, pz + g, e);
1046 const VofRawField c{J.c};
1047 int lo = -1, hi = -1;
1048 for (int px = 0; px < n.x; ++px)
1049 if (Kokkos::fabs(c(i0 + px)) > eps) {
1050 if (lo < 0)
1051 lo = px;
1052 hi = px;
1053 }
1054 if (lo < 0)
1055 return; // no cell of the row is in the box
1056 int* s = bx + 6 * k;
1057 Kokkos::atomic_min(&s[0], lo);
1058 Kokkos::atomic_min(&s[1], py);
1059 Kokkos::atomic_min(&s[2], pz);
1060 Kokkos::atomic_max(&s[3], hi);
1061 Kokkos::atomic_max(&s[4], py);
1062 Kokkos::atomic_max(&s[5], pz);
1063 });
1064 Exec().fence();
1065 for (int k = 0; k < T.nj; ++k) {
1066 const long ob = static_cast<long>(stride) * (T.base + k) + slot0;
1067 for (int a = 0; a < 6; ++a)
1068 out(ob + a) = static_cast<double>(box[6 * k + a]);
1069 }
1070 return;
1071 }
1072 VofBlockTable R = T; // the flat index is the tile
1073 const long nt = vofSetOffsets(R, vofTiles);
1074 vofEnsureTileScratch(tile, 6 * nt);
1075 SField tl = tile;
1076 using Team = Kokkos::TeamPolicy<SExec>;
1077 Kokkos::parallel_for(
1078 "vof::block::batch_bbox", Team(SExec(), nt, Kokkos::AUTO),
1079 KOKKOS_LAMBDA(const typename Team::member_type& tm) {
1080 const long t = tm.league_rank();
1081 const int k = vofJobOf(R.off, t);
1082 const VofBlockJob& J = R.job[k];
1083 const I3 e = J.e, n = J.n;
1084 const int g = J.g;
1085 const long region = static_cast<long>(n.x) * n.y * n.z;
1086 const long r0 = (t - R.off[k]) * kVofTile;
1087 const long r1 = r0 + kVofTile < region ? r0 + kVofTile : region;
1088 const VofRawField c{J.c};
1089 int v[6];
1090 for (int a = 0; a < 6; ++a) {
1091 int r = 0;
1092 auto body = [&](const long q, int& acc) {
1093 const int px = static_cast<int>(q % n.x);
1094 const int py = static_cast<int>((q / n.x) % n.y);
1095 const int pz = static_cast<int>(q / (static_cast<long>(n.x) * n.y));
1096 if (!(Kokkos::fabs(c(L3(px + g, py + g, pz + g, e))) > eps))
1097 return;
1098 const int p = (a % 3 == 0) ? px : (a % 3 == 1 ? py : pz);
1099 if (a < 3)
1100 acc = p < acc ? p : acc;
1101 else
1102 acc = p > acc ? p : acc;
1103 };
1104 if (a < 3)
1105 Kokkos::parallel_reduce(Kokkos::TeamThreadRange(tm, r0, r1), body, Kokkos::Min<int>(r));
1106 else
1107 Kokkos::parallel_reduce(Kokkos::TeamThreadRange(tm, r0, r1), body, Kokkos::Max<int>(r));
1108 v[a] = r;
1109 }
1110 Kokkos::single(Kokkos::PerTeam(tm), [&]() {
1111 for (int a = 0; a < 6; ++a)
1112 tl(6 * t + a) = static_cast<double>(v[a]);
1113 });
1114 });
1115 Kokkos::parallel_for(
1116 "vof::block::batch_bbox_combine", Kokkos::RangePolicy<SExec>(SExec(), 0, R.nj),
1117 KOKKOS_LAMBDA(const long q) {
1118 const int k = static_cast<int>(q);
1119 double v[6]; // the reducers' identities: an empty job reads hi < lo, as before
1120 for (int a = 0; a < 6; ++a)
1121 v[a] = static_cast<double>(a < 3 ? Kokkos::reduction_identity<int>::min()
1122 : Kokkos::reduction_identity<int>::max());
1123 for (long t = R.off[k]; t < R.off[k + 1]; ++t)
1124 for (int a = 0; a < 6; ++a) {
1125 const double w = tl(6 * t + a);
1126 if (a < 3 ? w < v[a] : w > v[a])
1127 v[a] = w;
1128 }
1129 const long ob = static_cast<long>(stride) * (R.base + k) + slot0;
1130 for (int a = 0; a < 6; ++a)
1131 out(ob + a) = v[a];
1132 });
1133}
1134
1135// ---- stage 6: the curvature cascade and the CSF face force (`VofBlockSet::computeCsf`) ---------
1136
1142 double *c, *mx, *my, *mz, *al, *kap, *br;
1143 int* slot;
1144 double* f[3];
1146 int g;
1147 double ieps, mtol, ptW, peps, dW, cmin, km;
1149 VofMetric gm;
1150};
1151
1159
1160template <class F>
1161inline long vofSetOffsets(VofCurvTable& T, F size) {
1162 T.off[0] = 0;
1163 for (int k = 0; k < T.nj; ++k)
1164 T.off[k + 1] = T.off[k] + size(T.job[k]);
1165 for (int k = T.nj; k < kVofBlockBatch; ++k)
1166 T.off[k + 1] = T.off[T.nj];
1167 return T.off[T.nj];
1168}
1169inline long vofCurvInner(const VofCurvJob& J) {
1170 return static_cast<long>(J.n.x) * J.n.y * J.n.z;
1171}
1172
1173inline long vofCurvGrown(const VofCurvJob& J) {
1174 return static_cast<long>(J.n.x + 2 * kPvHalf) * (J.n.y + 2 * kPvHalf) * (J.n.z + 2 * kPvHalf);
1175}
1176
1178KOKKOS_INLINE_FUNCTION long vofCurvCell(const VofCurvJob& J, long r, bool grown) {
1179 const int gr = grown ? kPvHalf : 0;
1180 const int rx = J.n.x + 2 * gr, ry = J.n.y + 2 * gr;
1181 const int ix = static_cast<int>(r % rx);
1182 const int iy = static_cast<int>((r / rx) % ry);
1183 const int iz = static_cast<int>(r / (static_cast<long>(rx) * ry));
1184 return L3(J.g - gr + ix, J.g - gr + iy, J.g - gr + iz, J.e);
1185}
1186
1188inline long vofCurvRows(const VofCurvJob& J, bool grown) {
1189 const int gr = grown ? kPvHalf : 0;
1190 return static_cast<long>(J.n.y + 2 * gr) * (J.n.z + 2 * gr);
1191}
1192
1194inline long vofCurvRow(const VofCurvJob& J, long r, bool grown, int& x0, int& x1) {
1195 const int gr = grown ? kPvHalf : 0;
1196 const int ry = J.n.y + 2 * gr;
1197 x0 = J.g - gr;
1198 x1 = J.g + J.n.x + gr;
1199 return L3(0, J.g - gr + static_cast<int>(r % ry), J.g - gr + static_cast<int>(r / ry), J.e);
1200}
1201
1204template <class Exec = SExec>
1205inline void vofCurvCompact(const VofCurvTable& T, bool grown, LField list, LField start,
1206 LField end) {
1207 if constexpr (kVofHostExec<Exec>) { // H-4(d): the grown or inner rows
1209 "vof::block::batch_curv_compact", T.nj, T.base,
1210 [&](const int k) { return vofCurvRows(T.job[k], grown); },
1211 [=](const int k, const long r, int& x0, int& x1) {
1212 return vofCurvRow(T.job[k], r, grown, x0, x1);
1213 },
1214 [=](const int k, const long i) { return vofIsInterface(T.job[k].c[i], T.job[k].ieps); },
1215 list, start, end);
1216 return;
1217 }
1218 Kokkos::parallel_scan(
1219 "vof::block::batch_curv_compact", Kokkos::RangePolicy<SExec>(SExec(), 0, T.off[T.nj]),
1220 KOKKOS_LAMBDA(const long t, long& upd, const bool final) {
1221 const int k = vofJobOf(T.off, t);
1222 const VofCurvJob& J = T.job[k];
1223 const long r = t - T.off[k];
1224 const long i = vofCurvCell(J, r, grown);
1225 const bool hit = vofIsInterface(J.c[i], J.ieps);
1226 if (final) {
1227 if (r == 0)
1228 start(T.base + k) = upd;
1229 if (hit)
1230 list(upd) = i;
1231 if (t + 1 == T.off[k + 1])
1232 end(T.base + k) = upd + (hit ? 1 : 0);
1233 }
1234 if (hit)
1235 ++upd;
1236 });
1237}
1238
1241template <class Exec = SExec>
1242inline void vofCurvPlanesZero(const VofCurvTable& T) {
1243 if constexpr (kVofHostExec<Exec>) {
1245 "vof::block::batch_curv_planes_zero", T.nj,
1246 [&](const int k) { return vofCurvRows(T.job[k], true); },
1247 [=](const int k, const long r) {
1248 const VofCurvJob& J = T.job[k];
1249 int x0, x1;
1250 const long i0 = vofCurvRow(J, r, true, x0, x1);
1251 PECLET_FLOW_OMP_SIMD
1252 for (int x = x0; x < x1; ++x) {
1253 J.mx[i0 + x] = 0.0;
1254 J.my[i0 + x] = 0.0;
1255 J.mz[i0 + x] = 0.0;
1256 J.al[i0 + x] = 0.0;
1257 J.slot[i0 + x] = -1;
1258 }
1259 });
1260 return;
1261 }
1262 Kokkos::parallel_for(
1263 "vof::block::batch_curv_planes_zero", Kokkos::RangePolicy<SExec>(SExec(), 0, T.off[T.nj]),
1264 KOKKOS_LAMBDA(const long t) {
1265 const int k = vofJobOf(T.off, t);
1266 const VofCurvJob& J = T.job[k];
1267 const long i = vofCurvCell(J, t - T.off[k], true);
1268 J.mx[i] = 0.0;
1269 J.my[i] = 0.0;
1270 J.mz[i] = 0.0;
1271 J.al[i] = 0.0;
1272 J.slot[i] = -1;
1273 });
1274}
1275
1277KOKKOS_INLINE_FUNCTION void vofCurvPlaneEntry(const VofCurvTable& T, int k, long q, LField list) {
1278 const VofCurvJob& J = T.job[k];
1279 const long sy = J.e.x, sz = static_cast<long>(J.e.x) * J.e.y;
1280 const long i = list(q);
1282 VofRawField{J.mz}, VofRawField{J.al});
1283 curvMomentsBuild(J.mx[i], J.my[i], J.mz[i], J.al[i], J.gm, T.cache[q]);
1284 J.slot[i] = static_cast<int>(q);
1285}
1286
1290template <class Exec = SExec>
1291inline void vofCurvPlanesList(const VofCurvTable& T, LField list, LField start, LField end) {
1292 if constexpr (kVofHostExec<Exec>) {
1294 "vof::block::batch_curv_planes", T.nj, vofListCount(T, start, end),
1295 [=](const int k, const long t) { vofCurvPlaneEntry(T, k, start(T.base + k) + t, list); });
1296 return;
1297 }
1298 Kokkos::parallel_for(
1299 "vof::block::batch_curv_planes", Kokkos::RangePolicy<SExec>(SExec(), 0, T.off[T.nj]),
1300 KOKKOS_LAMBDA(const long t) {
1301 const int k = vofJobOf(T.off, t);
1302 const long q = start(T.base + k) + (t - T.off[k]);
1303 if (q >= end(T.base + k))
1304 return;
1305 vofCurvPlaneEntry(T, k, q, list);
1306 });
1307}
1308
1311template <class Exec = SExec>
1312inline void vofCurvHfReset(const VofCurvTable& T) {
1313 if constexpr (kVofHostExec<Exec>) {
1315 "vof::block::batch_curv_hf_reset", T.nj,
1316 [&](const int k) { return vofCurvRows(T.job[k], false); },
1317 [=](const int k, const long r) {
1318 const VofCurvJob& J = T.job[k];
1319 int x0, x1;
1320 const long i0 = vofCurvRow(J, r, false, x0, x1);
1321 PECLET_FLOW_OMP_SIMD
1322 for (int x = x0; x < x1; ++x) {
1323 J.kap[i0 + x] = 0.0;
1324 J.br[i0 + x] = static_cast<double>(kCurvNone);
1325 }
1326 });
1327 return;
1328 }
1329 Kokkos::parallel_for(
1330 "vof::block::batch_curv_hf_reset", Kokkos::RangePolicy<SExec>(SExec(), 0, T.off[T.nj]),
1331 KOKKOS_LAMBDA(const long t) {
1332 const int k = vofJobOf(T.off, t);
1333 const VofCurvJob& J = T.job[k];
1334 const long i = vofCurvCell(J, t - T.off[k], false);
1335 J.kap[i] = 0.0;
1336 J.br[i] = static_cast<double>(kCurvNone);
1337 });
1338}
1339
1352inline constexpr int kVofTier3MaxTeams = 1 << 16;
1353inline void vofCurvFallbackTeams(const VofCurvTable& T, LField list, LField start, LField end) {
1354 using Policy = Kokkos::TeamPolicy<SExec>;
1355 constexpr int side = 2 * kPvHalf + 1, nk = side * side * side;
1356 const int league =
1357 std::max(1, std::min(kVofTier3MaxTeams, static_cast<int>(SExec().concurrency() / kVofWarp)));
1358 Policy pol(SExec(), league, kVofWarp);
1359 pol.set_scratch_size(0, Kokkos::PerTeam(VofPvTeamScratch::shmem(nk)));
1360 Kokkos::parallel_for(
1361 "vof::block::batch_curv_pv_team", pol, KOKKOS_LAMBDA(const typename Policy::member_type& tm) {
1362 const VofPvTeamScratch sc(tm, nk);
1363 long cum[kVofBlockBatch + 1]; // device prefix of the jobs' entry counts
1364 cum[0] = 0;
1365 for (int k = 0; k < T.nj; ++k)
1366 cum[k + 1] = cum[k] + (end(T.base + k) - start(T.base + k));
1367 for (long g = tm.league_rank(); g < cum[T.nj]; g += tm.league_size()) {
1368 int k = 0;
1369 while (g >= cum[k + 1])
1370 ++k;
1371 const VofCurvJob& J = T.job[k];
1372 const long i = list(start(T.base + k) + (g - cum[k]));
1373 const long sy = J.e.x, sz = static_cast<long>(J.e.x) * J.e.y;
1374 curvFallbackTeam(tm, sc, i, VofPvCached{J.slot, T.cache}, VofRawField{J.kap},
1375 VofRawField{J.br}, sy, sz, kPvHalf, J.dW, J.cmin, J.gm);
1376 tm.team_barrier(); // the single lane is done with the scratch before the next entry's
1377 // scan
1378 }
1379 });
1380}
1381
1385KOKKOS_INLINE_FUNCTION void vofCurvListEntry(const VofCurvTable& T, int pass, int k, long q,
1386 LField list, LField cnt) {
1387 const VofCurvJob& J = T.job[k];
1388 const long i = list(q);
1389 const long sy = J.e.x, sz = static_cast<long>(J.e.x) * J.e.y;
1390 const VofRawField c{J.c}, mx{J.mx}, my{J.my}, mz{J.mz}, al{J.al}, kap{J.kap}, br{J.br};
1391 const bool all = pass == 4;
1392 if (pass == 0 || all) {
1393 curvHeightCell(i, c, mx, my, mz, al, kap, br, 1L, sy, sz, J.mtol, J.ptW, J.ieps, J.forceFb,
1394 J.oneDir, J.useFit, J.gm, J.peps);
1395 if (!all)
1396 return;
1397 }
1398 if (pass == 1 || all) {
1399 curvFallbackCell(i, VofPvCached{J.slot, T.cache}, kap, br, sy, sz, kPvHalf, J.dW, J.cmin, J.gm);
1400 if (!all)
1401 return;
1402 }
1403 long* ct = &cnt(8 * (T.base + k));
1404 if (pass == 2 || all) {
1405 const double km = J.km;
1406 // km <= 0: this cascade's clip is off (VofCurvature::clipPass returns 0).
1407 // `!(|k| <= km)` rather than `|k| > km`: a NaN curvature is clipped (to +km, the sign of
1408 // a NaN is meaningless) and counted, never silently kept.
1409 if (km > 0.0 && csfKappaDefined(br(i)) && !(Kokkos::fabs(kap(i)) <= km)) {
1410 kap(i) = (kap(i) == kap(i)) ? Kokkos::copysign(km, kap(i)) : km;
1411 Kokkos::atomic_add(&ct[7], 1L);
1412 }
1413 if (!all)
1414 return;
1415 }
1416 const int b = static_cast<int>(br(i));
1417 if (b == kCurvNone)
1418 return;
1419 Kokkos::atomic_add(&ct[0], 1L);
1420 if (b == kCurvHf)
1421 Kokkos::atomic_add(&ct[1], 1L);
1422 else if (b == kCurvHfMixed)
1423 Kokkos::atomic_add(&ct[2], 1L);
1424 else if (b == kCurvHfFit)
1425 Kokkos::atomic_add(&ct[3], 1L);
1426 else if (b == kCurvPv)
1427 Kokkos::atomic_add(&ct[4], 1L);
1428 else if (b == kCurvPvReduced)
1429 Kokkos::atomic_add(&ct[5], 1L);
1430 else
1431 Kokkos::atomic_add(&ct[6], 1L);
1432}
1433
1439template <class Exec = SExec>
1440inline void vofCurvListPass(const VofCurvTable& T, int pass, LField list, LField start, LField end,
1441 LField cnt) {
1442 if constexpr (kVofHostExec<Exec>) { // tier 3 included: the host keeps the loop
1443 vofHostListFor<Exec>("vof::block::batch_curv_list", T.nj, vofListCount(T, start, end),
1444 [=](const int k, const long t) {
1445 vofCurvListEntry(T, pass, k, start(T.base + k) + t, list, cnt);
1446 });
1447 return;
1448 }
1449 if constexpr (!Kokkos::SpaceAccessibility<Kokkos::HostSpace, SField::memory_space>::accessible) {
1450 if (pass == 1) { // tier 3 on a device: persistent warp-teams (C3); the host keeps the loop
1451 vofCurvFallbackTeams(T, list, start, end);
1452 return;
1453 }
1454 }
1455 Kokkos::parallel_for(
1456 "vof::block::batch_curv_list", Kokkos::RangePolicy<SExec>(SExec(), 0, T.off[T.nj]),
1457 KOKKOS_LAMBDA(const long t) {
1458 const int k = vofJobOf(T.off, t);
1459 const long q = start(T.base + k) + (t - T.off[k]);
1460 if (q >= end(T.base + k))
1461 return;
1462 vofCurvListEntry(T, pass, k, q, list, cnt);
1463 });
1464}
1465
1472template <class Exec = SExec>
1473inline void vofCurvListPasses(const VofCurvTable& T, bool anyClip, LField list, LField start,
1474 LField end, LField cnt) {
1475 if constexpr (kVofHostExec<Exec>) {
1476 vofCurvListPass<Exec>(T, 4, list, start, end, cnt);
1477 return;
1478 }
1479 vofCurvListPass<Exec>(T, 0, list, start, end, cnt); // tiers 1-2
1480 vofCurvListPass<Exec>(T, 1, list, start, end, cnt); // tier 3
1481 if (anyClip)
1482 vofCurvListPass<Exec>(T, 2, list, start, end, cnt); // the admissibility clip
1483 vofCurvListPass<Exec>(T, 3, list, start, end, cnt); // the census
1484}
1485
1488KOKKOS_INLINE_FUNCTION void vofCsfForceCell(const VofCurvJob& J, long i, double sig, double w0,
1489 double w1, double w2) {
1490 const long sy = J.e.x, sz = static_cast<long>(J.e.x) * J.e.y;
1491 const VofRawField cv{J.c}, kp{J.kap}, kb{J.br};
1492 for (int cc = 0; cc < 3; ++cc) {
1493 const double wc = cc == 0 ? w0 : (cc == 1 ? w1 : w2);
1494 const long strd = (cc == 0) ? 1 : (cc == 1 ? sy : sz);
1495 const double dC = cv(i) - cv(i - strd);
1496 double f = 0.0;
1497 if (dC != 0.0) {
1498 double kf = 0.0;
1499 vof::csfFaceCurvature(kp(i - strd), kb(i - strd), kp(i), kb(i), kf);
1500 f = vof::csfFaceForce(sig, kf, dC, wc);
1501 }
1502 J.f[cc][i] = f;
1503 }
1504}
1505
1509template <class Exec = SExec>
1510inline void vofCsfForceBatch(const VofCurvTable& T, double sig, double w0, double w1, double w2) {
1511 if constexpr (kVofHostExec<Exec>) {
1513 "vof::block::batch_csf_force", T.nj,
1514 [&](const int k) { return vofCurvRows(T.job[k], false); },
1515 [=](const int k, const long r) {
1516 const VofCurvJob& J = T.job[k];
1517 int x0, x1;
1518 const long i0 = vofCurvRow(J, r, false, x0, x1);
1519 PECLET_FLOW_OMP_SIMD
1520 for (int x = x0; x < x1; ++x)
1521 vofCsfForceCell(J, i0 + x, sig, w0, w1, w2);
1522 });
1523 return;
1524 }
1525 Kokkos::parallel_for(
1526 "vof::block::batch_csf_force", Kokkos::RangePolicy<SExec>(SExec(), 0, T.off[T.nj]),
1527 KOKKOS_LAMBDA(const long t) {
1528 const int k = vofJobOf(T.off, t);
1529 vofCsfForceCell(T.job[k], vofCurvCell(T.job[k], t - T.off[k], false), sig, w0, w1, w2);
1530 });
1531}
1532
1533// ---- C2: the block statistics (§5.10) ----------------------------------------------------------
1534
1536template <int N>
1537struct VofSumN {
1538 double v[N];
1539 KOKKOS_INLINE_FUNCTION VofSumN() {
1540 for (int i = 0; i < N; ++i)
1541 v[i] = 0.0;
1542 }
1543 KOKKOS_INLINE_FUNCTION VofSumN& operator+=(const VofSumN& o) {
1544 for (int i = 0; i < N; ++i)
1545 v[i] += o.v[i];
1546 return *this;
1547 }
1548};
1549
1550} // namespace peclet::flow::vof
1551
1552namespace Kokkos {
1553template <int N>
1554struct reduction_identity<peclet::flow::vof::VofSumN<N>> {
1555 KOKKOS_FORCEINLINE_FUNCTION static peclet::flow::vof::VofSumN<N> sum() {
1557 }
1558};
1559} // namespace Kokkos
1560
1561namespace peclet::flow::vof {
1562
1566 double c[kVofBlockBatch][3];
1567 unsigned char valid[kVofBlockBatch];
1568};
1569
1571inline constexpr int kVofStats = 14;
1572
1574KOKKOS_INLINE_FUNCTION void vofStatsM1Cell(const VofBlockJob& J, long r, double hh, VofSumN<4>& a) {
1575 const I3 e = J.e, n = J.n, o = J.o;
1576 const int g = J.g;
1577 const VofRawField c{J.c};
1578 const int x = static_cast<int>(r % n.x) + g;
1579 const int y = static_cast<int>((r / n.x) % n.y) + g;
1580 const int z = static_cast<int>(r / (static_cast<long>(n.x) * n.y)) + g;
1581 const double q = c(L3(x, y, z, e));
1582 a.v[0] += q;
1583 a.v[1] += q * (x - g + o.x + 0.5) * hh;
1584 a.v[2] += q * (y - g + o.y + 0.5) * hh;
1585 a.v[3] += q * (z - g + o.z + 0.5) * hh;
1586}
1587
1589KOKKOS_INLINE_FUNCTION long vofStatsCellIndex(const VofBlockJob& J, long r) {
1590 const I3 n = J.n;
1591 const int g = J.g;
1592 const int x = static_cast<int>(r % n.x) + g;
1593 const int y = static_cast<int>((r / n.x) % n.y) + g;
1594 const int z = static_cast<int>(r / (static_cast<long>(n.x) * n.y)) + g;
1595 return L3(x, y, z, J.e);
1596}
1597
1599KOKKOS_INLINE_FUNCTION bool vofStatsMixed(double q, double aeps) {
1600 return q > aeps && q < 1.0 - aeps;
1601}
1602
1605KOKKOS_INLINE_FUNCTION double vofStatsMomentsCell(const VofBlockJob& J, long r, double hh,
1606 double cx, double cy, double cz, VofSumN<7>& a) {
1607 const I3 e = J.e, n = J.n, o = J.o;
1608 const int g = J.g;
1609 const VofRawField c{J.c};
1610 const int x = static_cast<int>(r % n.x) + g;
1611 const int y = static_cast<int>((r / n.x) % n.y) + g;
1612 const int z = static_cast<int>(r / (static_cast<long>(n.x) * n.y)) + g;
1613 const double q = c(L3(x, y, z, e));
1614 const double px = (x - g + o.x + 0.5) * hh - cx, py = (y - g + o.y + 0.5) * hh - cy,
1615 pz = (z - g + o.z + 0.5) * hh - cz;
1616 a.v[0] += q * px * px;
1617 a.v[1] += q * py * py;
1618 a.v[2] += q * pz * pz;
1619 a.v[3] += q * px * py;
1620 a.v[4] += q * px * pz;
1621 a.v[5] += q * py * pz;
1622 return q;
1623}
1624
1627KOKKOS_INLINE_FUNCTION double vofStatsAreaCell(const VofBlockJob& J, long i, double q) {
1628 const long sy = J.e.x, sz = static_cast<long>(J.e.x) * J.e.y;
1629 const VofRawField c{J.c};
1630 double st[27];
1631 for (int dz = -1; dz <= 1; ++dz)
1632 for (int dy = -1; dy <= 1; ++dy)
1633 for (int dx = -1; dx <= 1; ++dx)
1634 st[vof::plicSt(dx + 1, dy + 1, dz + 1)] = c(i + dx + dy * sy + dz * sz);
1635 double m[3];
1636 vof::mycNormal(st, m);
1637 const double al = vof::plicAlpha(m[0], m[1], m[2], q);
1638 double pv[8][3], ctr[3], ar = 0.0;
1639 const int nv = vof::plicPolygon(m[0], m[1], m[2], al, pv);
1640 vof::polygonAreaCentroid(pv, nv, ctr, ar);
1641 return ar;
1642}
1643
1646KOKKOS_INLINE_FUNCTION void vofStatsM2Cell(const VofBlockJob& J, long r, double hh, double aeps,
1647 double cx, double cy, double cz, VofSumN<7>& a) {
1648 const double q = vofStatsMomentsCell(J, r, hh, cx, cy, cz, a);
1649 if (!vofStatsMixed(q, aeps)) // interfaceArea: mixed cells only
1650 return;
1651 a.v[6] += vofStatsAreaCell(J, vofStatsCellIndex(J, r), q);
1652}
1653
1656KOKKOS_INLINE_FUNCTION void vofStatsWrite(double* q0, double v, const double nc[3],
1657 const VofSumN<7>& m2, const VofStatsPrev& P, int k,
1658 double dt) {
1659 q0[0] = v;
1660 if (v <= 0.0) {
1661 for (int d = 1; d < kVofStats; ++d)
1662 q0[d] = 0.0;
1663 return;
1664 }
1665 for (int d = 0; d < 3; ++d) {
1666 q0[1 + d] = nc[d];
1667 q0[4 + d] = (P.valid[k] && dt > 0.0) ? (nc[d] - P.c[k][d]) / dt : 0.0;
1668 }
1669 q0[7] = m2.v[6];
1670 const double iv = 1.0 / v;
1671 for (int d = 0; d < 6; ++d)
1672 q0[8 + d] = m2.v[d] * iv;
1673}
1674
1677KOKKOS_INLINE_FUNCTION void vofStatsCombine1(const long* off, const SField& tl, int k,
1678 VofSumN<4>& m1) {
1679 for (long t = off[k]; t < off[k + 1]; ++t)
1680 for (int d = 0; d < 4; ++d)
1681 m1.v[d] += tl(4 * t + d);
1682}
1683
1699inline void vofBatchStats(const VofBlockTable& T, const VofStatsPrev& P, double hh, double aeps,
1700 double dt, SField out, long off, SField& tile) {
1701 using Team = Kokkos::TeamPolicy<SExec>;
1702 if constexpr (kVofHostExec<SExec>) {
1703 Kokkos::parallel_for(
1704 "vof::block::batch_stats", Team(SExec(), T.nj, Kokkos::AUTO),
1705 KOKKOS_LAMBDA(const typename Team::member_type& tm) {
1706 const int k = tm.league_rank();
1707 const VofBlockJob& J = T.job[k];
1708 const long region = vofJobInner(J);
1709 double* q0 = out.data() + off + static_cast<long>(kVofStats) * (T.base + k);
1710 VofSumN<4> m1;
1711 Kokkos::parallel_reduce(
1712 Kokkos::TeamThreadRange(tm, region),
1713 [&](const long r, VofSumN<4>& a) { vofStatsM1Cell(J, r, hh, a); },
1714 Kokkos::Sum<VofSumN<4>>(m1));
1715 const double v = m1.v[0];
1716 VofSumN<7> m2;
1717 const double nc[3] = {v > 0.0 ? m1.v[1] / v : 0.0, v > 0.0 ? m1.v[2] / v : 0.0,
1718 v > 0.0 ? m1.v[3] / v : 0.0};
1719 if (v > 0.0)
1720 Kokkos::parallel_reduce(
1721 Kokkos::TeamThreadRange(tm, region),
1722 [&](const long r, VofSumN<7>& a) {
1723 vofStatsM2Cell(J, r, hh, aeps, nc[0], nc[1], nc[2], a);
1724 },
1725 Kokkos::Sum<VofSumN<7>>(m2));
1726 Kokkos::single(Kokkos::PerTeam(tm), [&]() { vofStatsWrite(q0, v, nc, m2, P, k, dt); });
1727 });
1728 return;
1729 }
1730 VofBlockTable R = T; // the flat index is the tile
1731 const long nt = vofSetOffsets(R, vofTiles);
1732 vofEnsureTileScratch(tile, 11 * nt);
1733 SField tl = tile;
1734 const long t2 = 4 * nt; // the second-moment partials follow the first-moment ones
1735 Kokkos::parallel_for(
1736 "vof::block::batch_stats_m1", Team(SExec(), nt, Kokkos::AUTO),
1737 KOKKOS_LAMBDA(const typename Team::member_type& tm) {
1738 const long t = tm.league_rank();
1739 const int k = vofJobOf(R.off, t);
1740 const VofBlockJob& J = R.job[k];
1741 const long region = vofJobInner(J), r0 = (t - R.off[k]) * kVofTile;
1742 const long r1 = r0 + kVofTile < region ? r0 + kVofTile : region;
1743 VofSumN<4> m1;
1744 Kokkos::parallel_reduce(
1745 Kokkos::TeamThreadRange(tm, r0, r1),
1746 [&](const long r, VofSumN<4>& a) { vofStatsM1Cell(J, r, hh, a); },
1747 Kokkos::Sum<VofSumN<4>>(m1));
1748 Kokkos::single(Kokkos::PerTeam(tm), [&]() {
1749 for (int d = 0; d < 4; ++d)
1750 tl(4 * t + d) = m1.v[d];
1751 });
1752 });
1753 // The area's cell body is long and only the mixed cells (a few per cent of a box) take it: each
1754 // team first lists its tile's mixed cells in team scratch (a team scan, ascending order), then
1755 // reduces the area over the dense list, so a warp's lanes all do PLIC work.
1756 const auto shm = Kokkos::PerTeam(static_cast<int>(kVofTile * sizeof(int)));
1757 Kokkos::parallel_for(
1758 "vof::block::batch_stats_m2", Team(SExec(), nt, Kokkos::AUTO).set_scratch_size(0, shm),
1759 KOKKOS_LAMBDA(const typename Team::member_type& tm) {
1760 const long t = tm.league_rank();
1761 const int k = vofJobOf(R.off, t);
1762 const VofBlockJob& J = R.job[k];
1763 VofSumN<4> m1;
1764 vofStatsCombine1(R.off, tl, k, m1);
1765 const double v = m1.v[0];
1766 if (v <= 0.0) // the empty marker: no second moments
1767 return;
1768 const double cx = m1.v[1] / v, cy = m1.v[2] / v, cz = m1.v[3] / v;
1769 const long region = vofJobInner(J), r0 = (t - R.off[k]) * kVofTile;
1770 const long r1 = r0 + kVofTile < region ? r0 + kVofTile : region;
1771 const VofRawField c{J.c};
1772 VofSumN<7> m2;
1773 Kokkos::parallel_reduce(
1774 Kokkos::TeamThreadRange(tm, r0, r1),
1775 [&](const long r, VofSumN<7>& a) { vofStatsMomentsCell(J, r, hh, cx, cy, cz, a); },
1776 Kokkos::Sum<VofSumN<7>>(m2));
1777 int* list = static_cast<int*>(tm.team_shmem().get_shmem(kVofTile * sizeof(int)));
1778 int nm = 0;
1779 Kokkos::parallel_scan(
1780 Kokkos::TeamThreadRange(tm, r0, r1),
1781 [&](const long r, int& u, const bool fin) {
1782 if (!vofStatsMixed(c(vofStatsCellIndex(J, r)), aeps))
1783 return;
1784 if (fin)
1785 list[u] = static_cast<int>(r - r0);
1786 ++u;
1787 },
1788 nm);
1789 tm.team_barrier();
1790 double ar = 0.0;
1791 Kokkos::parallel_reduce(
1792 Kokkos::TeamThreadRange(tm, nm),
1793 [&](const int j, double& s) {
1794 const long i = vofStatsCellIndex(J, r0 + list[j]);
1795 s += vofStatsAreaCell(J, i, c(i));
1796 },
1797 ar);
1798 Kokkos::single(Kokkos::PerTeam(tm), [&]() {
1799 for (int d = 0; d < 6; ++d)
1800 tl(t2 + 7 * t + d) = m2.v[d];
1801 tl(t2 + 7 * t + 6) = ar;
1802 });
1803 });
1804 Kokkos::parallel_for(
1805 "vof::block::batch_stats_combine", Kokkos::RangePolicy<SExec>(SExec(), 0, R.nj),
1806 KOKKOS_LAMBDA(const long q) {
1807 const int k = static_cast<int>(q);
1808 VofSumN<4> m1;
1809 vofStatsCombine1(R.off, tl, k, m1);
1810 const double v = m1.v[0];
1811 VofSumN<7> m2;
1812 double nc[3] = {0.0, 0.0, 0.0};
1813 if (v > 0.0) {
1814 for (int d = 0; d < 3; ++d)
1815 nc[d] = m1.v[1 + d] / v;
1816 for (long t = R.off[k]; t < R.off[k + 1]; ++t)
1817 for (int d = 0; d < 7; ++d)
1818 m2.v[d] += tl(t2 + 7 * t + d);
1819 }
1820 vofStatsWrite(out.data() + off + static_cast<long>(kVofStats) * (R.base + k), v, nc, m2, P,
1821 k, dt);
1822 });
1823}
1824
1825} // namespace peclet::flow::vof
1826
1827#endif // PECLET_FLOW_VOF_BLOCK_BATCH_HPP
flow — VoF rung V1: Weymouth & Yue (2010) directionally-split, exactly volume-conserving geometric ad...
flow — VoF rung V3 (WO-O): the view-level driver for the curvature cascade.
#define PECLET_FLOW_OMP_SIMD
flow — portable (Kokkos) MAC stencil operators: Red-Black Gauss-Seidel smoothers + divergence.
int vofJobOf(const long *off, long t)
Job of flat index t (off[0] <= t < off[nj]): a walk over at most 16 offsets.
constexpr bool kVofHostExec
The host branches select on this (§14.2).
void vofBatchBox(const VofBlockTable &T, double eps, SField out, int stride, int slot0, SField &tile)
The tight box of |C| > eps over each job's inner region, in the block's LOCAL inner frame (bubbleBox'...
long vofJobGrown1(const VofBlockJob &J)
The inner region grown by one cell per side: WyAdvector::reconstructImpl's range.
void vofCurvCompact(const VofCurvTable &T, bool grown, LField list, LField start, LField end)
VofCurvature::compact: the interfacial cells of the grown (grown) or inner region,...
bool vofDebrisCell(const SF &c, long i, long sy, long sz, double ieps, double cfull)
The debris predicate of doc/vof_overlap_design.md §11 on cell i: interfacial, and NO cell of the mark...
void vofBatchFlux(const VofBlockTable &T, int d)
WyAdvector::computeFluxesImpl (uncut): one flux per d-face, wyFaceFlux (or, for a job carrying the WO...
void vofCurvListPasses(const VofCurvTable &T, bool anyClip, LField list, LField start, LField end, LField cnt)
The whole cascade list stage of a chunk: tiers 1-2, tier 3, the clip (only if some job clips),...
double vofStatsAreaCell(const VofBlockJob &J, long i, double q)
The PLIC interface area of the mixed cell at extended index i (colour q) of job J (VofBlockSet::inter...
void vofBatchCfl(const VofBlockTable &T, SField out)
One team per job: WyAdvector::maxCourant or maxCourantInterface (per the job's interfaceCfl),...
bool csfKappaDefined(double branch)
Is the curvature of a cell carrying branch code branch usable?
void vofCsfForceCell(const VofCurvJob &J, long i, double sig, double w0, double w1, double w2)
The three CSF face-force components at the low faces of inner cell i of job J (the per-cell body of v...
long vofSetOffsets(VofBlockTable &T, F size)
Fill T.off from a per-job range size.
constexpr int kVofWarp
The team size of a device tier-3 launch: one warp / wavefront.
bool vofIsInterface(double c, double eps)
wyIsMixed with a wisp threshold: the cell carries an interface only while eps < C < 1 - eps.
void vofBatchClamp(const VofBlockTable &T, I3 gs, bool px, bool py, bool pz)
Pass 3 (colour_field.hpp::clampFill, skip = 0): the part of each extended block outside a NON-periodi...
void vofEnsureTileScratch(SField &tile, long len)
Grow-only scratch of the tiled reductions: at least len doubles.
void vofBatchPeriodic(const VofBlockTable &T, int a)
Pass 2 on axis a (colour_field.hpp::periodicFill, one axis): only the jobs whose block spans the peri...
double wyFaceFluxBc(double a, long p, long sd, int dir, const SF &c, const SF &mx, const SF &my, const SF &mz, const SF &alpha, const MF &outside, double eps=0.0)
WO-R sibling of wyFaceFlux for a face whose DONOR lies outside the global domain.
void vofBatchGhostZero(const VofBlockTable &T)
Pass 1: every ghost cell of every job -> 0.
constexpr int kVofStats
Doubles per job in the statistics packet: volume, centroid[3], velocity[3], area, moment[6].
constexpr int kVofBlockBatch
Jobs per launch; a container with more master blocks runs each stage in chunks.
void vofCurvFallbackTeams(const VofCurvTable &T, LField list, LField start, LField end)
VofDebrisSlot
The per-job slots of the debris packet (out(stride * (base + k) + slot)).
@ kDbND
debris count
@ kDbW
sum_A C(1 - C)
@ kDbActed
1 when the return ran (pending and VA >= kDebrisMinAttached and W > 0)
@ kDbSumR
sum_R C in list order (signed residue)
@ kDbNR
residue count
@ kDbPending
1 when there was something to remove (removeD or nR > 0)
@ kDbSumD
sum_D C in list order (the removed / census debris volume)
@ kDbDV
the volume to return (D if removing, then R); replaced by lost once acted
void vofBatchDebrisSums(const VofBlockTable &T, VofDebrisFlags F, LField listD, LField listR, LField listA, LField sD0, LField eD0, LField sR0, LField eR0, LField sA0, LField eA0, SField out, int stride, int slot0)
Step 2 of the pass: one thread per job, every sum in list order (the per-block kernel's single thread...
long vofStatsCellIndex(const VofBlockJob &J, long r)
Flat inner index of inner cell r (x-fastest) of job J, in its extended block.
void vofStatsWrite(double *q0, double v, const double nc[3], const VofSumN< 7 > &m2, const VofStatsPrev &P, int k, double dt)
measure()'s packet of job k from its sums: volume v, centroid nc, the second moments and the area m2 ...
void wyReconstructCell(const SF &c, long i, long sy, long sz, SF mx, SF my, SF mz, SF alpha)
PLIC reconstruction of one cell: MYC normal from the 3^3 colour stencil + the analytic plane offset.
long vofJobFaces(const VofBlockJob &J, int d)
A sweep's face range of one job: the inner region with one extra layer on the low side of d.
void vofStatsM1Cell(const VofBlockJob &J, long r, double hh, VofSumN< 4 > &a)
measure()'s first-moment terms of inner cell r (x-fastest flat index) of job J.
void vofCurvHfReset(const VofCurvTable &T)
heightPass (worklist mode), part 1: every inner cell to kappa = 0, branch = kCurvNone.
bool vofStatsMixed(double q, double aeps)
interfaceArea's cell test: a mixed cell, aeps < C < 1 - aeps.
long vofJobInner(const VofBlockJob &J)
void vofCurvPlanesList(const VofCurvTable &T, LField list, LField start, LField end)
Part 2: wyReconstructCell over the grown interfacial list (upper bound: the grown region),...
void vofHostRowScan(const char *name, int nj, int base, Rows rows, RowOf rowOf, Hit hit, LField list, LField start, LField end)
H-4(d): a host compaction scan over ROWS.
void vofBatchMark(const VofBlockTable &T, int kind, double ieps, double cfull, double weps, LField list, LField start, LField end)
One mark scan over the concatenated inner regions (T.off = vofJobInner), in each block's inner-box in...
void vofBatchFluxCell(const VofBlockJob &J, int d, long p)
The flux of d-face p of job J (the per-cell body of vofBatchFlux, both launch forms).
void vofCsfForceBatch(const VofCurvTable &T, double sig, double w0, double w1, double w2)
VofBlockSet::buildCsfForce for every job, the three components per cell: the V4 balanced-force CSF at...
Kokkos::View< long *, SMem > LField
Definition advect_wy.hpp:90
void vofCurvListEntry(const VofCurvTable &T, int pass, int k, long q, LField list, LField cnt)
Entry q (an absolute list position) of job k of a cascade list pass (see below): the body shared by t...
bool wyColourJump(double a, double b, double eps)
The interface-BAND jump test of maxCourantInterface: two neighbouring cells carry a DIFFERENT colour.
void vofBatchPlic(const VofBlockTable &T, LField list, LField start, LField end)
wyReconstructCell over each job's compacted list: launched over the upper bound (the job's region),...
bool wyIsMixed(double c)
The mixed-cell predicate.
Definition advect_wy.hpp:95
void vofCurvPlanesZero(const VofCurvTable &T)
reconstructPlanes (worklist mode), part 1: zero the four plane fields and clear the slot map (design ...
void vofBatchDebrisLost(const VofBlockTable &T, LField sA0, LField eA0, SField out, int stride, int slot0, SField ex, Kokkos::View< int *, SMem > capf)
lost: the capped excess of an acted job summed in list order on one thread (only where a cap fired,...
void vofBatchBcLedger(const VofBlockTable &T, int d, SField out, int stride, int slot0)
The WO-R boundary ledger (WyAdvector::accumulateBcFaceVolume) of the jobs carrying the mask: one thre...
long vofJobLen(const VofBlockJob &J)
long vofCurvCell(const VofCurvJob &J, long r, bool grown)
Cell r of job J's grown (grown) or inner region, in the region's x-fastest order.
void curvFallbackCell(long i, const Acc &acc, SF kap, SF br, long sy, long sz, int gr, double dW, double cmin, VofMetric g)
Tier 3 for one target, one thread (host): the frame from the target's entry (pvFrameMoments: the phys...
long vofCurvRow(const VofCurvJob &J, long r, bool grown, int &x0, int &x1)
The first cell of row r of job J's grown or inner region, and the region's x range.
auto vofListCount(const Tab &T, LField start, LField end)
The [start, end) count of job k of a list launch.
bool vofAttachedCell(const SF &c, long i, long sy, long sz, double ieps, double cfull)
The ATTACHED predicate (§5.3 step 2): interfacial with a cell at or above cfull within two.
long vofCurvRows(const VofCurvJob &J, bool grown)
Rows of job J's grown (grown) or inner region, for the host row forms (H-4b).
long vofTiles(const VofBlockJob &J)
void curvHeightCell(long i, SF c, SF mx, SF my, SF mz, SF al, SF kap, SF br, long s0, long s1, long s2, double mtol, double ptW, double ieps, bool forceFb, bool oneDir, bool useFit, VofMetric g, double peps)
WO-V9 — the per-cell bodies of the two cascade tiers, lifted VERBATIM out of the kernels they used to...
void vofHostRowFor(const char *name, int nj, Rows rows, F row)
H-4(b): a host region kernel over ROWS – rows(k) x-runs for job k, row(k, r) handling job k's local r...
void vofStatsCombine1(const long *off, const SField &tl, int k, VofSumN< 4 > &m1)
Job k's first moments from its tile partials (tl(4 t + {0..3}), tiles [off[k], off[k + 1])),...
PECLET_VOF_DEVICE_NOINLINE void curvMomentsBuild(double mx, double my, double mz, double alpha, const VofMetric &g, PvMoments &P)
pvMomentsBuild, the one call every planes pass and the dense accessor make (out of line on a device,...
constexpr long kVofTile
Inner cells per tile of the tiled device reductions (vofBatchBox, vofBatchStats): job k's inner regio...
void vofBatchClampCell(const VofBlockJob &J, int x, int y, int z, int q0, int q1, int q2, bool p0, bool p1, bool p2)
The clamp of extended cell (x, y, z) of job J (the per-cell body of vofBatchClamp).
void vofBatchWorklist(const VofBlockTable &T, LField list, LField start, LField end)
WyAdvector::reconstructImpl's worklist scan, over the concatenated grown-by-one regions (T....
long vofCurvInner(const VofCurvJob &J)
double wyFaceFlux(double a, long p, long sd, int dir, const SF &c, const SF &mx, const SF &my, const SF &mz, const SF &alpha, double eps=0.0)
Signed Eulerian donor-cell flux through the dir-face between cell p and cell p + sd,...
PECLET_VOF_DEVICE_NOINLINE void curvFallbackTeam(const Member &tm, const VofPvTeamScratch &sc, long i, const Acc &acc, SF kap, SF br, long sy, long sz, int gr, double dW, double cmin, VofMetric g)
curvFallbackCell for one target on a whole team (design G §5.4; the team shape of C3,...
void vofBatchStats(const VofBlockTable &T, const VofStatsPrev &P, double hh, double aeps, double dt, SField out, long off, SField &tile)
VofBlockSet::measure for every job: the first moments, then the central second moments and the PLIC i...
void vofCurvPlaneEntry(const VofCurvTable &T, int k, long q, LField list)
Entry q (an absolute grown-list position) of job k of the planes pass (below).
double csfFaceForce(double sigma, double kf, double dC, double hGrad)
The balanced-force CSF contribution to the momentum RHS at one staggered velocity unknown,...
void vofBatchFreeze(const VofBlockTable &T)
WyAdvector::freezeDilationFlag over every job's extended block.
void vofBatchUpdate(const VofBlockTable &T, int d)
WyAdvector::applySweepImpl (uncut) over the inner regions (T.off = vofJobInner); the expression verba...
bool vofResidueCell(double ci, double weps)
The residue predicate (§13): 0 < |C| <= wispEps.
void vofDebrisActEntry(const VofBlockTable &T, const VofDebrisFlags &F, int k, long t, LField listD, LField listR, LField listA, LField sD0, LField sR0, LField sA0, LField eA0, SField out, int stride, int slot0, SField ex, Kokkos::View< int *, SMem > capf)
Act entry t of job k (the per-block write kernel's expressions verbatim); t past the job's count,...
void vofCurvListPass(const VofCurvTable &T, int pass, LField list, LField start, LField end, LField cnt)
The per-list-cell passes of the cascade over the inner interfacial list (upper bound: the inner regio...
double vofStatsMomentsCell(const VofBlockJob &J, long r, double hh, double cx, double cy, double cz, VofSumN< 7 > &a)
measure()'s central second-moment terms about (cx, cy, cz) of inner cell r of job J (a....
bool csfFaceCurvature(double k0, double b0, double k1, double b1, double &kf)
Face curvature for the balanced-force CSF, from the two cells sharing the face.
void vofHostListFor(const char *name, int nj, Cnt cnt, F body)
H-4(a): a host list kernel over the EXACT entry counts, cnt(k) (read directly: the count Views are Ho...
long vofDebrisActCount(const VofDebrisFlags &F, LField sA0, LField eA0, SField out, long ob, int j)
The number of act entries of job j (its D removed, then R, then A), 0 when it did not act: the per-bl...
constexpr int kVofTier3MaxTeams
Tier 3 of the batched cascade on a DEVICE (C3, doc/vof_step_performance_design.md §4....
long vofCurvGrown(const VofCurvJob &J)
The inner region grown by kPvHalf: every cell VofCurvature::reconstructPlanes writes.
void vofBatchUpdateCell(const VofBlockJob &J, int d, long i)
The sweep update of inner cell i of job J (the per-cell body of vofBatchUpdate).
void vofBatchDebrisWrite(const VofBlockTable &T, VofDebrisFlags F, LField listD, LField listR, LField listA, LField sD0, LField sR0, LField sA0, LField eA0, SField out, int stride, int slot0, SField ex, Kokkos::View< int *, SMem > capf)
Step 4, the act: removed cells -> 0, attached C += dV C(1-C)/W capped at 1 (the per-block write kerne...
void vofStatsM2Cell(const VofBlockJob &J, long r, double hh, double aeps, double cx, double cy, double cz, VofSumN< 7 > &a)
measure()'s central second-moment terms about (cx, cy, cz) and the PLIC interface area of inner cell ...
Kokkos::View< double *, SMem > SField
long L3(int x, int y, int z, I3 e)
Kokkos::DefaultExecutionSpace SExec
Kokkos::DefaultExecutionSpace Exec
One master block's share of a batched container launch.
unsigned char * cc
the frozen dilation flag
double dth
dt / h of this block's advector
double weps
the advector's wisp tolerance
double * u[3]
face velocities uf, vf, wf
bool interfaceCfl
interfaceLocalCfl: maxCourantInterface, else maxCourant
bool span[3]
the block spans this globally periodic axis (wrap in-block)
I3 o
extended extent, inner counts, global index of inner (0,0,0)
const unsigned char * outside
the WO-R out-of-domain mask; null when there is none
unsigned char bcOwn[6]
WO-R: the global domain faces this block owns.
The job table of one launch, by value.
long off[kVofBlockBatch+1]
VofBlockJob job[kVofBlockBatch]
The exact offset table of a host list launch: job k's entries are [o[k], o[k + 1]).
One master block's cascade (VofCurvature, worklist mode) for the batched launches: its planes,...
double * f[3]
the block's CSF face force, per component
int * slot
the block's slot map (design G §5.2): absolute cListG_ positions
VofCurvJob job[kVofBlockBatch]
PvMoments * cache
the chunk's moment cache, indexed like the grown list (design G §5.2)
Flags of the pass, uniform over the blocks.
The cache-backed accessor: slot[j] >= 0 iff cell j of the grown region is interfacial,...
static std::size_t shmem(int nk)
Per-team level-0 scratch bytes for a stencil of nk offsets.
A raw-pointer field accessor with the View<double*> call syntax, for kernels that walk SEVERAL blocks...
Read-only raw accessor of an unsigned-char field (the WO-R out-of-domain mask).
const unsigned char * p
unsigned char operator()(long i) const
The row offsets of a host region launch: job k's rows are [off[k], off[k + 1]).
long off[kVofBlockBatch+1]
What measure() reads besides the colour: each job's previous centroid and whether it is valid (the ho...
double c[kVofBlockBatch][3]
unsigned char valid[kVofBlockBatch]
N double sums in one team reduction (Kokkos::Sum<VofSumN<N>>).
VofSumN & operator+=(const VofSumN &o)
static constexpr int N
static constexpr double F
static constexpr double R
Kokkos::View< double *, SMem > SField
long L3(int x, int y, int z, I3 e)
Kokkos::DefaultExecutionSpace SExec