flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
mg_fp32_vcycle.hpp
Go to the documentation of this file.
1// D -- the FP32 V-cycle preconditioner's kernels (doc/vof_projection_cost_design.md §4.4.3,
2// §4.4.4 items 1-4). Included by mac_cutcell_mg.hpp after VReal, ccWrapNbrs, ccWrapRowPeeled and
3// the FP64 transfer kernels; CutcellMG drives them (precondVcycleFp32). The FP64 bodies are not
4// touched: every FP32 body is new and is used only on an eligible solve (§4.4.1).
5//
6// Storage per level: the FP32 face weights WX, WY, WZ (w_f = fl32(t_f) > 0, each face stored once
7// and read by both of its cells) and the FP32 iterates xf, rhsf, resf. The diagonal is never
8// stored in any precision: D_i is summed from the six weights the update reads anyway, and the
9// arithmetic is the flux/correction form, so x = c * 1 gives r = 0 exactly (§4.2 P1, P5). Every
10// literal in an FP32 body carries `f`; no double variable enters an FP32 body.
11#pragma once
12
13namespace peclet::flow {
14
15using VField = Kokkos::View<VReal*, CCMem>;
16using VConst = Kokkos::View<const VReal*, CCMem>;
17
18// §4.4.3: D_i = ((((WX(i+sx) + WX(i)) + WY(i+sy)) + WY(i)) + WZ(i+sz)) + WZ(i), FP32, this order.
19template <class WV>
20KOKKOS_INLINE_FUNCTION VReal fp32Diag(const WV& WX, const WV& WY, const WV& WZ, long i, long sx,
21 long sy, long sz) {
22 return ((((WX(i + sx) + WX(i)) + WY(i + sy)) + WY(i)) + WZ(i + sz)) + WZ(i);
23}
24// §4.4.3: q_i = sum_f w_f (x_i - x_j(f)), left to right in the stated face order.
25template <class XV, class WV>
26KOKKOS_INLINE_FUNCTION VReal fp32Flux(const XV& x, const WV& WX, const WV& WY, const WV& WZ, long i,
27 long sx, long sy, long sz, long xp, long xm, long yp, long ym,
28 long zp, long zm) {
29 const VReal xi = x(i);
30 return WX(i) * (xi - x(xm)) + WX(i + sx) * (xi - x(xp)) + WY(i) * (xi - x(ym)) +
31 WY(i + sy) * (xi - x(yp)) + WZ(i) * (xi - x(zm)) + WZ(i + sz) * (xi - x(zp));
32}
33// §4.4.3 smoother cell: x_i = x_i + (b_i - q_i) / D_i on a cell with D_i != 0 (else skipped).
34template <class XV, class BV, class WV>
35KOKKOS_INLINE_FUNCTION void fp32SmoothCell(const XV& x, const BV& b, const WV& WX, const WV& WY,
36 const WV& WZ, long i, long sx, long sy, long sz, long xp,
37 long xm, long yp, long ym, long zp, long zm) {
38 const VReal d = fp32Diag(WX, WY, WZ, i, sx, sy, sz);
39 if (d == 0.0f)
40 return; // decoupled (the FP64 AC < 1e-30 set, §4.4.1 (5)): x stays as it is
41 const VReal q = fp32Flux(x, WX, WY, WZ, i, sx, sy, sz, xp, xm, yp, ym, zp, zm);
42 x(i) = x(i) + (b(i) - q) / d;
43}
44// §4.4.3 residual cell: r_i = b_i - q_i.
45template <class RV, class XV, class BV, class WV>
46KOKKOS_INLINE_FUNCTION void fp32ResidualCell(const RV& r, const XV& x, const BV& b, const WV& WX,
47 const WV& WY, const WV& WZ, long i, long sx, long sy,
48 long sz, long xp, long xm, long yp, long ym, long zp,
49 long zm) {
50 r(i) = b(i) - fp32Flux(x, WX, WY, WZ, i, sx, sy, sz, xp, xm, yp, ym, zp, zm);
51}
52
53// One red-black colour pass of the FP32 smoother with the A3 wrapped neighbour reads (every inner
54// dimension even; cutcellSmoothColorFaceWrap's launch forms: host pencils with the row ends
55// peeled, device MDRange).
57 C3 n, C3 og, int g, int color) {
58 CCExec space;
59 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>) {
60 const int nyi = e.y - 2 * g, nzi = e.z - 2 * g;
61 const long cells = (long)nyi * nzi * (e.x - 2 * g);
62 auto pencil = KOKKOS_LAMBDA(long t) {
63 const int ly = g + (int)(t % nyi), lz = g + (int)(t / nyi);
64 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
65 const int P = (color + og.x + og.y + ly + og.z + lz) & 1;
66 ccWrapRowPeeled(ly, lz, e, n, g, g + ((P ^ (g & 1)) & 1), 2,
67 [&](long i, long xp, long xm, long yp, long ym, long zp, long zm) {
68 fp32SmoothCell(x, b, WX, WY, WZ, i, sx, sy, sz, xp, xm, yp, ym, zp, zm);
69 });
70 };
71 if (hostRunSerial(cells)) {
72 const HostSerialRegion serialRegion("peclet::flow::cc_smooth_f32@serial"); // WO-T
73 for (long t = 0; t < (long)nyi * nzi; ++t)
74 pencil(t);
75 return;
76 }
77 Kokkos::parallel_for("peclet::flow::cc_smooth_f32",
78 Kokkos::RangePolicy<CCExec>(space, 0, (long)nyi * nzi), pencil);
79 return;
80 }
81 using MD = MDRange3<CCExec>;
82 Kokkos::parallel_for(
83 "peclet::flow::cc_smooth_f32", MD(space, {g, g, g}, {e.x - g, e.y - g, e.z - g}),
84 KOKKOS_LAMBDA(int lx, int ly, int lz) {
85 if (((og.x + lx + og.y + ly + og.z + lz) & 1) != color)
86 return;
87 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
88 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
89 const CcNbrs w = ccWrapNbrs(lx, ly, lz, n, g, i, sy, sz);
90 fp32SmoothCell(x, b, WX, WY, WZ, i, sx, sy, sz, w.xp, w.xm, w.yp, w.ym, w.zp, w.zm);
91 });
92}
93// The FP32 residual r = b - W x over the inner cells with the wrapped reads of x.
94inline void residualFp32Wrap(VField r, VConst x, VConst b, VConst WX, VConst WY, VConst WZ, C3 e,
95 C3 n, int g) {
96 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>) {
97 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
98 ccWrapForPeeled("peclet::flow::cc_residual_f32", e, n, g,
99 [=](long i, long xp, long xm, long yp, long ym, long zp, long zm) {
100 fp32ResidualCell(r, x, b, WX, WY, WZ, i, sx, sy, sz, xp, xm, yp, ym, zp, zm);
101 });
102 return;
103 }
104 ccFor3(
105 "peclet::flow::cc_residual_f32", C3{g, g, g}, C3{e.x - g, e.y - g, e.z - g},
106 KOKKOS_LAMBDA(int lx, int ly, int lz) {
107 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
108 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
109 const CcNbrs w = ccWrapNbrs(lx, ly, lz, n, g, i, sy, sz);
110 fp32ResidualCell(r, x, b, WX, WY, WZ, i, sx, sy, sz, w.xp, w.xm, w.yp, w.ym, w.zp, w.zm);
111 });
112}
113
114// §4.4.4 item 1, the level-0 ENTRY (replaces the z = 0 memset and the first pre-smooth colour
115// pass): rhsf = fl32(rr * sigma) on every inner cell (sigma = 2^-e: exact); on the cells of
116// `color` with D_i != 0 (when `smoothFirst`) xf = rhsf / D_i, on every other inner cell xf = 0.
117inline void entryFp32(VField xf, VField rhsf, CCConst rr, VConst WX, VConst WY, VConst WZ,
118 double sigma, C3 e, C3 og, int g, int color, bool smoothFirst) {
119 ccFor3(
120 "peclet::flow::mg_entry_f32", C3{g, g, g}, C3{e.x - g, e.y - g, e.z - g},
121 KOKKOS_LAMBDA(int lx, int ly, int lz) {
122 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
123 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
124 const VReal b = (VReal)(rr(i) * sigma);
125 rhsf(i) = b;
126 VReal x0 = 0.0f;
127 if (smoothFirst && ((og.x + lx + og.y + ly + og.z + lz) & 1) == color) {
128 const VReal d = fp32Diag(WX, WY, WZ, i, sx, sy, sz);
129 if (d != 0.0f)
130 x0 = b / d;
131 }
132 xf(i) = x0;
133 });
134}
135
136// §4.4.4 item 2, restriction FP32 -> FP32 that zeroes the coarse iterate on the same cells (the
137// FP32 restrictAvgZeroX): the children summed in FP32 in restrictAvgCell's order, times the
138// inverse child count (a power of two: 0.125f at ratio 2 on every axis).
139inline void restrictFp32ZeroX(VField coarse, VField coarseX, VConst fine, C3 cext, C3 fext, int gc,
140 int gf, C3 cinner, C3 ratio) {
141 const VReal inv = (VReal)(1.0 / (double)(ratio.x * ratio.y * ratio.z));
142 ccFor3(
143 "peclet::flow::restrict_f32", C3{0, 0, 0}, C3{cinner.x, cinner.y, cinner.z},
144 KOKKOS_LAMBDA(int icx, int icy, int icz) {
145 const long fsy = fext.x, fsz = (long)fext.x * fext.y;
146 VReal s = 0.0f;
147 for (int dz = 0; dz < ratio.z; ++dz)
148 for (int dy = 0; dy < ratio.y; ++dy)
149 for (int dx = 0; dx < ratio.x; ++dx) {
150 const int fx = ratio.x * icx + dx + gf, fy = ratio.y * icy + dy + gf,
151 fz = ratio.z * icz + dz + gf;
152 s += fine((long)fx + (long)fy * fsy + (long)fz * fsz);
153 }
154 const long ci =
155 (long)(icx + gc) + (long)(icy + gc) * cext.x + (long)(icz + gc) * (long)cext.x * cext.y;
156 coarse(ci) = s * inv;
157 coarseX(ci) = 0.0f;
158 });
159}
160// §4.4.4 item 3, the bottom interface down: restriction FP32 -> FP64, the children summed in
161// double from (double)fine, times the double inverse count, into the bottom's rhs; zeroes the
162// bottom's x on the same cells.
163inline void restrictFp32ToDZeroX(CCField coarse, CCField coarseX, VConst fine, C3 cext, C3 fext,
164 int gc, int gf, C3 cinner, C3 ratio) {
165 const double inv = 1.0 / (double)(ratio.x * ratio.y * ratio.z);
166 ccFor3(
167 "peclet::flow::restrict_f32d", C3{0, 0, 0}, C3{cinner.x, cinner.y, cinner.z},
168 KOKKOS_LAMBDA(int icx, int icy, int icz) {
169 const long fsy = fext.x, fsz = (long)fext.x * fext.y;
170 double s = 0.0;
171 for (int dz = 0; dz < ratio.z; ++dz)
172 for (int dy = 0; dy < ratio.y; ++dy)
173 for (int dx = 0; dx < ratio.x; ++dx) {
174 const int fx = ratio.x * icx + dx + gf, fy = ratio.y * icy + dy + gf,
175 fz = ratio.z * icz + dz + gf;
176 s += (double)fine((long)fx + (long)fy * fsy + (long)fz * fsz);
177 }
178 const long ci =
179 (long)(icx + gc) + (long)(icy + gc) * cext.x + (long)(icz + gc) * (long)cext.x * cext.y;
180 coarse(ci) = s * inv;
181 coarseX(ci) = 0.0;
182 });
183}
184
185// The coarse sample of prolongAddCell (§14 H-2's integer form): lower coarse index and weight.
186KOKKOS_INLINE_FUNCTION int fp32ProlongLow(int ratio, int i, int gc) {
187 return (ratio == 2) ? (i >> 1) + gc - 1 + (i & 1) : i + gc;
188}
189// §4.4.4 item 2, prolongation FP32 <- FP32 (added to fine): prolongAddCell's trilinear form with
190// the weights 0.25f / 0.75f and (1.0f - w) written as a subtraction.
191inline void prolongFp32(VField fine, VConst coarse, C3 fext, C3 cext, int gf, int gc, C3 finner,
192 C3 ratio) {
193 ccFor3(
194 "peclet::flow::prolong_f32", C3{0, 0, 0}, C3{finner.x, finner.y, finner.z},
195 KOKKOS_LAMBDA(int ifx, int ify, int ifz) {
196 const int x0 = fp32ProlongLow(ratio.x, ifx, gc), y0 = fp32ProlongLow(ratio.y, ify, gc),
197 z0 = fp32ProlongLow(ratio.z, ifz, gc);
198 const VReal wx = (ratio.x == 2) ? ((ifx & 1) ? 0.25f : 0.75f) : 0.0f;
199 const VReal wy = (ratio.y == 2) ? ((ify & 1) ? 0.25f : 0.75f) : 0.0f;
200 const VReal wz = (ratio.z == 2) ? ((ifz & 1) ? 0.25f : 0.75f) : 0.0f;
201 const long sy = cext.x, sz = (long)cext.x * cext.y;
202 auto C = [&](int xx, int yy, int zz) {
203 return coarse((long)xx + (long)yy * sy + (long)zz * sz);
204 };
205 const VReal c00 = C(x0, y0, z0) * (1.0f - wx) + C(x0 + 1, y0, z0) * wx;
206 const VReal c10 = C(x0, y0 + 1, z0) * (1.0f - wx) + C(x0 + 1, y0 + 1, z0) * wx;
207 const VReal c01 = C(x0, y0, z0 + 1) * (1.0f - wx) + C(x0 + 1, y0, z0 + 1) * wx;
208 const VReal c11 = C(x0, y0 + 1, z0 + 1) * (1.0f - wx) + C(x0 + 1, y0 + 1, z0 + 1) * wx;
209 const VReal c0 = c00 * (1.0f - wy) + c10 * wy, c1 = c01 * (1.0f - wy) + c11 * wy;
210 const long fi =
211 (long)(ifx + gf) + (long)(ify + gf) * fext.x + (long)(ifz + gf) * (long)fext.x * fext.y;
212 fine(fi) = fine(fi) + (c0 * (1.0f - wz) + c1 * wz);
213 });
214}
215// §4.4.4 item 3, the bottom interface up: prolongation FP32 <- FP64, the interpolation in double
216// (prolongAddCell's expression), xf = fl32((double)xf + interp).
217inline void prolongFp32FromD(VField fine, CCConst coarse, C3 fext, C3 cext, int gf, int gc,
218 C3 finner, C3 ratio) {
219 ccFor3(
220 "peclet::flow::prolong_f32d", C3{0, 0, 0}, C3{finner.x, finner.y, finner.z},
221 KOKKOS_LAMBDA(int ifx, int ify, int ifz) {
222 const int x0 = fp32ProlongLow(ratio.x, ifx, gc), y0 = fp32ProlongLow(ratio.y, ify, gc),
223 z0 = fp32ProlongLow(ratio.z, ifz, gc);
224 const double wx = (ratio.x == 2) ? ((ifx & 1) ? 0.25 : 0.75) : 0.0;
225 const double wy = (ratio.y == 2) ? ((ify & 1) ? 0.25 : 0.75) : 0.0;
226 const double wz = (ratio.z == 2) ? ((ifz & 1) ? 0.25 : 0.75) : 0.0;
227 const long sy = cext.x, sz = (long)cext.x * cext.y;
228 auto C = [&](int xx, int yy, int zz) {
229 return coarse((long)xx + (long)yy * sy + (long)zz * sz);
230 };
231 const double c00 = C(x0, y0, z0) * (1 - wx) + C(x0 + 1, y0, z0) * wx;
232 const double c10 = C(x0, y0 + 1, z0) * (1 - wx) + C(x0 + 1, y0 + 1, z0) * wx;
233 const double c01 = C(x0, y0, z0 + 1) * (1 - wx) + C(x0 + 1, y0, z0 + 1) * wx;
234 const double c11 = C(x0, y0 + 1, z0 + 1) * (1 - wx) + C(x0 + 1, y0 + 1, z0 + 1) * wx;
235 const double c0 = c00 * (1 - wy) + c10 * wy, c1 = c01 * (1 - wy) + c11 * wy;
236 const long fi =
237 (long)(ifx + gf) + (long)(ify + gf) * fext.x + (long)(ifz + gf) * (long)fext.x * fext.y;
238 fine(fi) = (VReal)((double)fine(fi) + (c0 * (1 - wz) + c1 * wz));
239 });
240}
241// doc/mg_depth_policy.md A3.3 item 1, the piecewise-constant prolongation P = (prod ratio) R^T on
242// the FP32 data (prolongConstAddCell's parent index): FP32 <- FP32, fine = fine + coarse in FP32.
243KOKKOS_INLINE_FUNCTION int fp32ProlongParent(int ratio, int i, int gc) {
244 return ((ratio == 2) ? (i >> 1) : i) + gc;
245}
246inline void prolongConstFp32(VField fine, VConst coarse, C3 fext, C3 cext, int gf, int gc,
247 C3 finner, C3 ratio) {
248 ccFor3(
249 "peclet::flow::prolong_const_f32", C3{0, 0, 0}, C3{finner.x, finner.y, finner.z},
250 KOKKOS_LAMBDA(int ifx, int ify, int ifz) {
251 const long ci = (long)fp32ProlongParent(ratio.x, ifx, gc) +
252 (long)fp32ProlongParent(ratio.y, ify, gc) * cext.x +
253 (long)fp32ProlongParent(ratio.z, ifz, gc) * (long)cext.x * cext.y;
254 const long fi =
255 (long)(ifx + gf) + (long)(ify + gf) * fext.x + (long)(ifz + gf) * (long)fext.x * fext.y;
256 fine(fi) = fine(fi) + coarse(ci);
257 });
258}
259// ... and the bottom interface up, FP32 <- FP64: xf = fl32((double)xf + coarse).
260inline void prolongConstFp32FromD(VField fine, CCConst coarse, C3 fext, C3 cext, int gf, int gc,
261 C3 finner, C3 ratio) {
262 ccFor3(
263 "peclet::flow::prolong_const_f32d", C3{0, 0, 0}, C3{finner.x, finner.y, finner.z},
264 KOKKOS_LAMBDA(int ifx, int ify, int ifz) {
265 const long ci = (long)fp32ProlongParent(ratio.x, ifx, gc) +
266 (long)fp32ProlongParent(ratio.y, ify, gc) * cext.x +
267 (long)fp32ProlongParent(ratio.z, ifz, gc) * (long)cext.x * cext.y;
268 const long fi =
269 (long)(ifx + gf) + (long)(ify + gf) * fext.x + (long)(ifz + gf) * (long)fext.x * fext.y;
270 fine(fi) = (VReal)((double)fine(fi) + coarse(ci));
271 });
272}
273
274// --- Distributed launch forms (doc/vof_projection_distributed_design.md §5.3, WO-D1) -----------
275// The distributed FP32 V-cycle's box kernels: the launch forms of the FP64 box kernels
276// (cutcellSmoothColorBoxFace, residualCutcellBoxFace) around the single-rank FP32 cell bodies,
277// reading the neighbours at plain offsets i +- 1, i +- sy, i +- sz (the ghosts hold what the
278// in-kernel wrap reads: gate G-K1). Cells in [lo, hi) minus the skip box [exLo, exHi).
279
280// One red-black colour pass of the FP32 smoother over a box (cutcellSmoothColorBoxFace's forms:
281// host pencils, device MDRange; its parity expression).
282// K3 (doc/vof_distributed_host_comm_design.md §5.2): `wrapMask` != 0 reads x across the level's
283// rank-local periodic axes in-kernel (ccMaskNbrs; g = the level's ghost width), the launch forms
284// as below; wrapMask = 0 (the default) is today's code.
286 C3 og, int color, C3 lo, C3 hi, C3 exLo, C3 exHi,
287 int wrapMask = 0, int g = 1) {
288 if (hi.x <= lo.x || hi.y <= lo.y || hi.z <= lo.z)
289 return;
290 CCExec space;
291 if (wrapMask != 0) {
292 const C3 n{e.x - 2 * g, e.y - 2 * g, e.z - 2 * g};
293 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>) {
294 const int nyi = hi.y - lo.y, nzi = hi.z - lo.z;
295 const long cells = (long)nyi * nzi * (hi.x - lo.x);
296 auto pencil = KOKKOS_LAMBDA(long t) {
297 const int ly = lo.y + (int)(t % nyi), lz = lo.z + (int)(t / nyi);
298 const bool yzSkip = ly >= exLo.y && ly < exHi.y && lz >= exLo.z && lz < exHi.z;
299 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
300 const int P = (color + og.x + og.y + ly + og.z + lz) & 1;
301 for (int lx = lo.x + ((P ^ (lo.x & 1)) & 1); lx < hi.x; lx += 2) {
302 if (yzSkip && lx >= exLo.x && lx < exHi.x)
303 continue; // inside the skip box (already swept by the interior pass)
304 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
305 const CcNbrs w = ccMaskNbrs(lx, ly, lz, n, g, i, sy, sz, wrapMask);
306 fp32SmoothCell(x, b, WX, WY, WZ, i, sx, sy, sz, w.xp, w.xm, w.yp, w.ym, w.zp, w.zm);
307 }
308 };
309 if (hostRunSerial(cells)) {
310 const HostSerialRegion serialRegion("peclet::flow::cc_smooth_box_f32@serial"); // WO-T
311 for (long t = 0; t < (long)nyi * nzi; ++t)
312 pencil(t);
313 return;
314 }
315 Kokkos::parallel_for("peclet::flow::cc_smooth_box_f32",
316 Kokkos::RangePolicy<CCExec>(space, 0, (long)nyi * nzi), pencil);
317 return;
318 }
319 using MD = MDRange3<CCExec>;
320 Kokkos::parallel_for(
321 "peclet::flow::cc_smooth_box_f32", MD(space, {lo.x, lo.y, lo.z}, {hi.x, hi.y, hi.z}),
322 KOKKOS_LAMBDA(int lx, int ly, int lz) {
323 if (lx >= exLo.x && lx < exHi.x && ly >= exLo.y && ly < exHi.y && lz >= exLo.z &&
324 lz < exHi.z)
325 return;
326 if (((og.x + lx + og.y + ly + og.z + lz) & 1) != color)
327 return;
328 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
329 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
330 const CcNbrs w = ccMaskNbrs(lx, ly, lz, n, g, i, sy, sz, wrapMask);
331 fp32SmoothCell(x, b, WX, WY, WZ, i, sx, sy, sz, w.xp, w.xm, w.yp, w.ym, w.zp, w.zm);
332 });
333 return;
334 }
335 if constexpr (std::is_same_v<typename CCExec::memory_space, Kokkos::HostSpace>) {
336 const int nyi = hi.y - lo.y, nzi = hi.z - lo.z;
337 const long cells = (long)nyi * nzi * (hi.x - lo.x);
338 auto pencil = KOKKOS_LAMBDA(long t) {
339 const int ly = lo.y + (int)(t % nyi), lz = lo.z + (int)(t / nyi);
340 const bool yzSkip = ly >= exLo.y && ly < exHi.y && lz >= exLo.z && lz < exHi.z;
341 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
342 const int P = (color + og.x + og.y + ly + og.z + lz) & 1;
343 for (int lx = lo.x + ((P ^ (lo.x & 1)) & 1); lx < hi.x; lx += 2) {
344 if (yzSkip && lx >= exLo.x && lx < exHi.x)
345 continue; // inside the skip box (already swept by the interior pass)
346 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
347 fp32SmoothCell(x, b, WX, WY, WZ, i, sx, sy, sz, i + sx, i - sx, i + sy, i - sy, i + sz,
348 i - sz);
349 }
350 };
351 if (hostRunSerial(cells)) { // coarse MG level: the fork/join costs more than the sweep
352 const HostSerialRegion serialRegion("peclet::flow::cc_smooth_box_f32@serial"); // WO-T
353 for (long t = 0; t < (long)nyi * nzi; ++t)
354 pencil(t);
355 return;
356 }
357 Kokkos::parallel_for("peclet::flow::cc_smooth_box_f32",
358 Kokkos::RangePolicy<CCExec>(space, 0, (long)nyi * nzi), pencil);
359 return;
360 }
361 using MD = MDRange3<CCExec>;
362 Kokkos::parallel_for(
363 "peclet::flow::cc_smooth_box_f32", MD(space, {lo.x, lo.y, lo.z}, {hi.x, hi.y, hi.z}),
364 KOKKOS_LAMBDA(int lx, int ly, int lz) {
365 if (lx >= exLo.x && lx < exHi.x && ly >= exLo.y && ly < exHi.y && lz >= exLo.z &&
366 lz < exHi.z)
367 return; // inside the skip box (already swept by the interior pass)
368 if (((og.x + lx + og.y + ly + og.z + lz) & 1) != color)
369 return;
370 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
371 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
372 fp32SmoothCell(x, b, WX, WY, WZ, i, sx, sy, sz, i + sx, i - sx, i + sy, i - sy, i + sz,
373 i - sz);
374 });
375}
376// The FP32 residual r = b - W x over a box (residualCutcellBoxFace's form).
377inline void residualFp32Box(VField r, VConst x, VConst b, VConst WX, VConst WY, VConst WZ, C3 e,
378 C3 lo, C3 hi, C3 exLo, C3 exHi, int wrapMask = 0, int g = 1) {
379 if (hi.x <= lo.x || hi.y <= lo.y || hi.z <= lo.z)
380 return;
381 if (wrapMask != 0) { // K3 (§5.2): see cutcellSmoothColorFp32Box
382 const C3 n{e.x - 2 * g, e.y - 2 * g, e.z - 2 * g};
383 ccFor3(
384 "peclet::flow::cc_residual_box_f32", lo, hi, KOKKOS_LAMBDA(int lx, int ly, int lz) {
385 if (lx >= exLo.x && lx < exHi.x && ly >= exLo.y && ly < exHi.y && lz >= exLo.z &&
386 lz < exHi.z)
387 return;
388 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
389 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
390 const CcNbrs w = ccMaskNbrs(lx, ly, lz, n, g, i, sy, sz, wrapMask);
391 fp32ResidualCell(r, x, b, WX, WY, WZ, i, sx, sy, sz, w.xp, w.xm, w.yp, w.ym, w.zp, w.zm);
392 });
393 return;
394 }
395 ccFor3(
396 "peclet::flow::cc_residual_box_f32", lo, hi, KOKKOS_LAMBDA(int lx, int ly, int lz) {
397 if (lx >= exLo.x && lx < exHi.x && ly >= exLo.y && ly < exHi.y && lz >= exLo.z &&
398 lz < exHi.z)
399 return; // inside the skip box (already done by the interior pass)
400 const long sx = 1, sy = e.x, sz = (long)e.x * e.y;
401 const long i = (long)lx + (long)ly * sy + (long)lz * sz;
402 fp32ResidualCell(r, x, b, WX, WY, WZ, i, sx, sy, sz, i + sx, i - sx, i + sy, i - sy, i + sz,
403 i - sz);
404 });
405}
406// The telescope transition's conversions (§5.4, WO-D2b1): d = (double)f over the full extent ...
407inline void fp32ToDouble(CCField d, VConst f, C3 e) {
408 const long n = (long)e.x * e.y * (long)e.z;
409 Kokkos::parallel_for(
410 "peclet::flow::f32_to_f64", Kokkos::RangePolicy<CCExec>(CCExec(), 0, n),
411 KOKKOS_LAMBDA(long i) { d(i) = (double)f(i); });
412}
413// ... and f = fl32((double)f + d) on the inner cells (prolongFp32FromD's rounding, §4.4.4 item 3).
414inline void fp32AddDouble(VField f, CCConst d, C3 e, int g) {
415 ccFor3(
416 "peclet::flow::f32_add_f64", C3{g, g, g}, C3{e.x - g, e.y - g, e.z - g},
417 KOKKOS_LAMBDA(int lx, int ly, int lz) {
418 const long i = (long)lx + (long)ly * e.x + (long)lz * (long)e.x * e.y;
419 // PRECISION-EXEMPT: FP32 V-cycle preconditioner (doc/vof_projection_distributed_design.md)
420 f(i) = (VReal)((double)f(i) + d(i));
421 });
422}
423
424} // namespace peclet::flow
Kokkos::DefaultExecutionSpace CCExec
void cutcellSmoothColorFp32Box(VField x, VConst b, VConst WX, VConst WY, VConst WZ, C3 e, C3 og, int color, C3 lo, C3 hi, C3 exLo, C3 exHi, int wrapMask=0, int g=1)
int fp32ProlongParent(int ratio, int i, int gc)
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).
Definition policy.hpp:36
void prolongFp32FromD(VField fine, CCConst coarse, C3 fext, C3 cext, int gf, int gc, C3 finner, C3 ratio)
Kokkos::View< const double *, CCMem > CCConst
void residualFp32Box(VField r, VConst x, VConst b, VConst WX, VConst WY, VConst WZ, C3 e, C3 lo, C3 hi, C3 exLo, C3 exHi, int wrapMask=0, int g=1)
void fp32ResidualCell(const RV &r, const XV &x, const BV &b, const WV &WX, const WV &WY, const WV &WZ, long i, long sx, long sy, long sz, long xp, long xm, long yp, long ym, long zp, long zm)
void residualFp32Wrap(VField r, VConst x, VConst b, VConst WX, VConst WY, VConst WZ, C3 e, C3 n, int g)
void restrictFp32ToDZeroX(CCField coarse, CCField coarseX, VConst fine, C3 cext, C3 fext, int gc, int gf, C3 cinner, C3 ratio)
VReal fp32Diag(const WV &WX, const WV &WY, const WV &WZ, long i, long sx, long sy, long sz)
void restrictFp32ZeroX(VField coarse, VField coarseX, VConst fine, C3 cext, C3 fext, int gc, int gf, C3 cinner, C3 ratio)
void prolongFp32(VField fine, VConst coarse, C3 fext, C3 cext, int gf, int gc, C3 finner, C3 ratio)
void fp32AddDouble(VField f, CCConst d, C3 e, int g)
void ccWrapForPeeled(const char *name, C3 e, C3 n, int g, const Cell &cell)
void fp32ToDouble(CCField d, VConst f, C3 e)
Kokkos::View< VReal *, CCMem > VField
VReal fp32Flux(const XV &x, const WV &WX, const WV &WY, const WV &WZ, long i, long sx, long sy, long sz, long xp, long xm, long yp, long ym, long zp, long zm)
CcNbrs ccWrapNbrs(int lx, int ly, int lz, C3 n, int g, long i, long sy, long sz)
Kokkos::View< double *, CCMem > CCField
void ccFor3(const char *name, C3 lo, C3 hi, F f)
void fp32SmoothCell(const XV &x, const BV &b, const WV &WX, const WV &WY, const WV &WZ, long i, long sx, long sy, long sz, long xp, long xm, long yp, long ym, long zp, long zm)
int fp32ProlongLow(int ratio, int i, int gc)
void ccWrapRowPeeled(int ly, int lz, C3 e, C3 n, int g, int lx0, int step, const Cell &cell)
void entryFp32(VField xf, VField rhsf, CCConst rr, VConst WX, VConst WY, VConst WZ, double sigma, C3 e, C3 og, int g, int color, bool smoothFirst)
void cutcellSmoothColorFp32Wrap(VField x, VConst b, VConst WX, VConst WY, VConst WZ, C3 e, C3 n, C3 og, int g, int color)
bool hostRunSerial(long cells)
void prolongConstFp32(VField fine, VConst coarse, C3 fext, C3 cext, int gf, int gc, C3 finner, C3 ratio)
void prolongConstFp32FromD(VField fine, CCConst coarse, C3 fext, C3 cext, int gf, int gc, C3 finner, C3 ratio)
Kokkos::View< const VReal *, CCMem > VConst