flow 1.4.0
Kokkos cut-cell IBM incompressible Navier-Stokes solver + pnm pore extraction
Loading...
Searching...
No Matches
test_device_sync_budget.cpp
Go to the documentation of this file.
1// G-SYNC (doc/vof_gpu_coarse_tail_design.md §5.5, WO-S4): the device step's host-synchronization
2// budget. A small single-rank block-VoF bubble in a walled box (the bubble column's configuration:
3// variable density by a linear closure, CSF, MG-PCG, superficial-velocity shift) runs 10 steps
4// while in-process Kokkos Tools callbacks count, per step,
5// - GLOBAL fences on the device (Kokkos::fence(), exec-less deep_copy: each one drains every
6// stream, and the GPU then idles until the host relaunches),
7// - combined-reducer fences ("fence after copying values back": a parallel_reduce with >= 2
8// results reduces through host memory and fences twice, even into device Views), each
9// attributed to the parallel_reduce label it belongs to, and
10// - device-space allocations / deallocations (with cudaMallocAsync off a cudaMalloc/cudaFree
11// raises no fence event, so S4(e) "no per-step View" is only visible here).
12// Two configurations: 'fp64' V-cycles + zero guess, and 'auto' V-cycles + the projected guess (the
13// WO-S2 sites: the FP32 exit's single-result mean, the wBad read, the guess packet). Both must
14// launch the side-instance bottom prefactor of WO-X (diagnostics counter > 0).
15// `test_device_sync_budget hoist` instead runs the 'auto' + projected configuration twice, the
16// WO-X hoist off (the lazy factor in the bottom solve, coefficients in project()) and on, and
17// compares the state byte for byte (§5.6: the hoisted factor is bitwise the lazy one).
18// Host backends have no device to drain: the test skips there (exit 77).
19#include <array>
20#include <cmath>
21#include <cstdint>
22#include <cstdio>
23#include <cstring>
24#include <Kokkos_Core.hpp>
25#include <map>
26#include <string>
27#include <vector>
28
29#include "flow_ibm.hpp"
30
31namespace {
32long gGlobal = 0, gAlloc = 0, gDealloc = 0;
33std::map<std::string, long> gCombined; // combined-reducer fences by parallel_reduce label
34std::string gReduce; // the label of the last parallel_reduce begun
35std::map<std::string, long> gAllocBy; // device allocations by View label
36bool gCount = false;
37
38bool isDevice(uint32_t devid) {
39 const auto id = Kokkos::Tools::Experimental::identifier_from_devid(devid);
40 return id.type != Kokkos::Tools::Experimental::DeviceType::Serial &&
41 id.type != Kokkos::Tools::Experimental::DeviceType::OpenMP &&
42 id.type != Kokkos::Tools::Experimental::DeviceType::Threads &&
43 id.type != Kokkos::Tools::Experimental::DeviceType::HPX;
44}
45void onBeginFence(const char* name, const uint32_t devid, uint64_t* handle) {
46 *handle = 0;
47 if (!gCount)
48 return;
49 // A tool with a parallel_reduce callback makes Kokkos fence globally around each kernel callback
50 // (ToolRequirements::requires_global_fencing): those fences are the probe's, not the step's.
51 if (std::strstr(name, "Profile Tool Fence") != nullptr)
52 return;
53 const auto id = Kokkos::Tools::Experimental::identifier_from_devid(devid);
54 // A global fence reaches the callback once per enabled space with instance bits 0 (and device
55 // bits 0): count the device space's only.
56 if (isDevice(devid) && id.instance_id == 0 && id.device_id == 0)
57 ++gGlobal;
58 if (std::strstr(name, "fence after copying values back") != nullptr)
59 ++gCombined[gReduce.empty() ? std::string("(no parallel_reduce)") : gReduce];
60}
61void onEndFence(uint64_t) {}
62void onBeginReduce(const char* name, const uint32_t, uint64_t* kid) {
63 *kid = 0;
64 gReduce = name;
65}
66// Not cleared at the end: Kokkos fences "after copying values back" once the end callback ran.
67void onEndReduce(uint64_t) {}
68bool deviceSpace(const Kokkos::Tools::SpaceHandle h) {
69 return std::strcmp(h.name, "Host") != 0;
70}
71void onAlloc(const Kokkos::Tools::SpaceHandle h, const char* label, const void*, const uint64_t) {
72 if (gCount && deviceSpace(h)) {
73 // Kokkos 5.1.1 allocates a 0-byte team scratch per TeamPolicy launch (Cuda_Instance.cpp:426):
74 // Kokkos-internal and iteration-dependent, reported but not pinned.
75 if (std::strstr(label, "TeamScratchMemory") == nullptr)
76 ++gAlloc;
77 ++gAllocBy[std::string(h.name) + ":" + label];
78 }
79}
80void onDealloc(const Kokkos::Tools::SpaceHandle h, const char*, const void*, const uint64_t) {
81 if (gCount && deviceSpace(h))
82 ++gDealloc;
83}
84
85// The measured post-S4b budget (both configurations; the bubble column's fencebt census agrees):
86// global fences 0/step -- the block container's reads #1 and #2 are instance-scoped (WO-S4b);
87// combined-reducer fences: none from any label but the allowlist below, all outside the MG files
88// (vmg_resid_max, the velocity MG's stop test, once per sweep check, so iteration-dependent;
89// rho_minmax; overlap_census). §5.5 asks for 0; these three are a separate package (Q-6, the
90// 2026-10-19 coordinator ruling). A label 'peclet::flow::mg*' is never allowed.
91// device allocations / deallocations per step: 0 -- no per-step View (S4(e)); Kokkos' own
92// per-launch team scratch (below) is listed but not counted (measured 15 and 14.4/step).
93constexpr double kGlobalBudget = 0.0;
94const char* const kCombinedAllow[] = {"peclet::flow::vmg_resid_max", "rho_minmax",
95 "vof::block::overlap_census"};
96constexpr double kAllocBudget = 0.0;
97
99constexpr int NX = 32, NY = 24, NZ = 16;
100
101// The bubble: `autoGuess` selects 'auto' V-cycles + the projected guess (else 'fp64' + zero).
102void setup(Stag& s, bool autoGuess) {
103 const double rl = 1.0, rg = 0.02, ml = 0.01, mg = 2e-4, g = 2e-3, R = 4.0;
104 s.setAdvection(false);
105 s.setRho(rl);
106 s.setMu(ml);
107 s.setDomainBc(2, 1, 0.0, 0.0, 0.0); // -y wall
108 s.setDomainBc(3, 1, 0.0, 0.0, 0.0); // +y wall
109 s.setPressureGeometry(std::vector<double>((std::size_t)NX * NY * NZ, 10.0));
110 s.enableVof();
111 s.setVof(std::vector<double>((std::size_t)NX * NY * NZ, 0.0));
112 s.setPropertyModel("rho", peclet::flow::ClosureKind::LinearMix, "C", "", {rl, rg - rl});
113 s.setPropertyModel("mu", peclet::flow::ClosureKind::LinearMix, "C", "", {ml, mg - ml});
114 s.setSurfaceTension(0.05);
115 s.setPressurePcg(true, 800, 1e-8);
116 s.setPressureBottomMode(1); // agglomerated: the direct bottom (the bubble column's), so X fires
117 if (autoGuess) {
118 s.setPressureVcyclePrecision(0); // 'auto'
119 s.setPressureInitialGuess(2, 2); // 'projected', history 2
120 }
121 s.enableVofBlocks({{{NX / 2.0, NY / 2.0, NZ / 2.0, R}}});
122 s.enableVofBlockCsf();
123 const double alpha = (4.0 / 3.0) * M_PI * R * R * R / ((double)NX * NY * NZ);
124 const double rav = rl + (rg - rl) * alpha;
125 s.setPropertyModel("force_x", peclet::flow::ClosureKind::LinearMix, "C", "",
126 {(rl - rav) * g, (rg - rl) * g});
127 s.setSuperficialVelocity(true, 0, 0.0);
128}
129
130void advance(Stag& s, int n0, int n1, int countFrom) {
131 for (int n = n0; n < n1; ++n) {
132 const auto L = s.vofStepLimits();
133 s.setDt(0.25 * std::fmin(L.cflDt, L.capillaryDt));
134 gCount = n >= countFrom;
135 s.step();
136 gCount = false;
137 }
138}
139
140// One configuration's budget. Returns 0 (pass) or 1.
141int budget(bool autoGuess) {
142 const char* tag = autoGuess ? "auto+projected" : "fp64+zero";
143 gGlobal = gAlloc = gDealloc = 0;
144 gCombined.clear();
145 gAllocBy.clear();
146 Stag s(NX, NY, NZ);
147 setup(s, autoGuess);
148 namespace KT = Kokkos::Tools::Experimental;
149 KT::set_begin_fence_callback(onBeginFence);
150 KT::set_end_fence_callback(onEndFence);
151 KT::set_begin_parallel_reduce_callback(onBeginReduce);
152 KT::set_end_parallel_reduce_callback(onEndReduce);
153 KT::set_allocate_data_callback(onAlloc);
154 KT::set_deallocate_data_callback(onDealloc);
155 const int warm = 2, steps = 8;
156 advance(s, 0, warm + steps, warm);
157 KT::set_begin_fence_callback(nullptr);
158 KT::set_end_fence_callback(nullptr);
159 KT::set_begin_parallel_reduce_callback(nullptr);
160 KT::set_end_parallel_reduce_callback(nullptr);
161 KT::set_allocate_data_callback(nullptr);
162 KT::set_deallocate_data_callback(nullptr);
163 int rc = 0;
164 const double perG = (double)gGlobal / steps;
165 const double perA = (double)gAlloc / steps, perD = (double)gDealloc / steps;
166 long comb = 0;
167 for (const auto& [label, n] : gCombined) {
168 bool allowed = false;
169 for (const char* a : kCombinedAllow)
170 allowed = allowed || label == a;
171 if (label.rfind("peclet::flow::mg", 0) == 0)
172 allowed = false;
173 comb += n;
174 std::printf("device_sync_budget [%s]: combined-reducer fences/step %.2f from '%s'%s\n", tag,
175 (double)n / steps, label.c_str(), allowed ? "" : " <-- NOT ALLOWED");
176 if (!allowed)
177 rc = 1;
178 }
179 for (const auto& [label, n] : gAllocBy)
180 std::printf("device_sync_budget [%s]: device allocations/step %.2f of '%s'\n", tag,
181 (double)n / steps, label.c_str());
182 const long pre = s.pressurePrefactorCount();
183 std::printf(
184 "device_sync_budget [%s]: global fences/step %.2f (budget %.1f), combined %.2f (allowlist "
185 "only), device View alloc/dealloc per step %.2f/%.2f (budget %.1f), bottom prefactors %ld (> "
186 "0)\n",
187 tag, perG, kGlobalBudget, (double)comb / steps, perA, perD, kAllocBudget, pre);
188 if (perG > kGlobalBudget || perA > kAllocBudget || perD > kAllocBudget || pre <= 0)
189 rc = 1;
190 std::printf("device_sync_budget [%s]: %s\n", tag, rc ? "FAIL" : "PASS");
191 return rc;
192}
193
194// WO-X hoist off vs on, 'auto' + projected: the state after 10 steps byte for byte.
195int hoistBitwise() {
196 const int steps = 10;
197 std::vector<std::vector<double>> st[2];
198 long pre[2];
199 for (int on = 0; on < 2; ++on) {
200 Stag s(NX, NY, NZ);
201 setup(s, true);
202 s.setPressurePrefactorHoist(on == 1);
203 advance(s, 0, steps, steps);
204 for (int c = 0; c < 3; ++c)
205 st[on].push_back(s.getVelocity(c));
206 st[on].push_back(s.getPressure());
207 st[on].push_back(s.getVof());
208 pre[on] = s.pressurePrefactorCount();
209 }
210 int rc = (pre[0] == 0 && pre[1] > 0) ? 0 : 1;
211 const char* names[] = {"u", "v", "w", "p", "C"};
212 for (std::size_t k = 0; k < st[0].size(); ++k) {
213 const bool same =
214 st[0][k].size() == st[1][k].size() &&
215 std::memcmp(st[0][k].data(), st[1][k].data(), st[0][k].size() * sizeof(double)) == 0;
216 std::printf("prefactor_hoist_bitwise: %s %s\n", names[k], same ? "identical" : "DIFFERS");
217 rc |= same ? 0 : 1;
218 }
219 std::printf("prefactor_hoist_bitwise: prefactors off %ld (== 0), on %ld (> 0): %s\n", pre[0],
220 pre[1], rc ? "FAIL" : "PASS");
221 return rc;
222}
223// G-M2-10 (doc/vof_momentum_second_order.md §9): the device synchronization of a VoF-momentum step,
224// 'muscl_phase' against 'upwind'. A 24^3 periodic box, a tilted sharp plane at ratio 1e3, a UNIFORM
225// velocity (the identity keeps both schemes' fields bitwise equal, so the pressure solve sees the
226// same input and its iteration count cannot differ), 2 warm + 8 counted steps. Global fences,
227// combined-reducer fences and device View allocations / deallocations per step must be EQUAL: the
228// capped kernels add no fence and no View (the ghost-flag kernel has no fence of its own, the
229// 'capped' count rides the clamp reduction).
230struct SyncCounts {
231 double global, combined, alloc, dealloc;
232};
233
234SyncCounts vofMomCounts(const char* scheme) {
235 gGlobal = gAlloc = gDealloc = 0;
236 gCombined.clear();
237 gAllocBy.clear();
238 const int n = 24;
239 const double ratio = 1e3, U[3] = {1.0, 0.6, -0.4};
240 Stag s(n, n, n);
241 s.setAdvection(false);
242 s.setRho(ratio);
243 s.setMu(0.0);
244 s.setDt(0.2);
245 s.setPressureGeometry(std::vector<double>((std::size_t)n * n * n, 10.0));
246 s.setPressureChebyshev(true, 300, 1e-13);
247 s.enableVof();
248 std::vector<double> c((std::size_t)n * n * n);
249 const double m[3] = {0.6 / 1.72456, -0.5 / 1.72456, 0.62456 / 1.72456};
250 const double alpha = 0.5 * (m[0] + m[1] + m[2]) * n;
251 for (int z = 0; z < n; ++z)
252 for (int y = 0; y < n; ++y)
253 for (int x = 0; x < n; ++x)
254 c[(std::size_t)x + (std::size_t)n * (y + (std::size_t)n * z)] =
255 peclet::flow::vof::plicVolume(m[0], m[1], m[2],
256 alpha - (m[0] * x + m[1] * y + m[2] * z));
257 s.setVof(c);
258 s.setPropertyModel("rho", peclet::flow::ClosureKind::LinearMix, "C", "", {1.0, ratio - 1.0});
259 s.enableVofMomentum(1.0, ratio);
260 s.setVofMomentumScheme(scheme);
261 const std::size_t nc = (std::size_t)n * n * n;
262 s.uploadVelocity(std::vector<double>(nc, U[0]), std::vector<double>(nc, U[1]),
263 std::vector<double>(nc, U[2]));
264 namespace KT = Kokkos::Tools::Experimental;
265 KT::set_begin_fence_callback(onBeginFence);
266 KT::set_end_fence_callback(onEndFence);
267 KT::set_begin_parallel_reduce_callback(onBeginReduce);
268 KT::set_end_parallel_reduce_callback(onEndReduce);
269 KT::set_allocate_data_callback(onAlloc);
270 KT::set_deallocate_data_callback(onDealloc);
271 const int warm = 2, steps = 8;
272 for (int k = 0; k < warm + steps; ++k) {
273 gCount = k >= warm;
274 s.step();
275 gCount = false;
276 }
277 KT::set_begin_fence_callback(nullptr);
278 KT::set_end_fence_callback(nullptr);
279 KT::set_begin_parallel_reduce_callback(nullptr);
280 KT::set_end_parallel_reduce_callback(nullptr);
281 KT::set_allocate_data_callback(nullptr);
282 KT::set_deallocate_data_callback(nullptr);
283 long comb = 0;
284 for (const auto& [label, k] : gCombined)
285 comb += k;
286 return {(double)gGlobal / steps, (double)comb / steps, (double)gAlloc / steps,
287 (double)gDealloc / steps};
288}
289
290int vofMomBudget() {
291 const SyncCounts up = vofMomCounts("upwind");
292 const SyncCounts ph = vofMomCounts("muscl_phase");
293 std::printf(
294 "vofmom_sync_budget: per step, upwind vs muscl_phase: global fences %.2f / %.2f, "
295 "combined-reducer fences %.2f / %.2f, device View alloc %.2f / %.2f, dealloc %.2f / "
296 "%.2f\n",
297 up.global, ph.global, up.combined, ph.combined, up.alloc, ph.alloc, up.dealloc, ph.dealloc);
298 const bool ok = up.global == ph.global && up.combined == ph.combined && up.alloc == ph.alloc &&
299 up.dealloc == ph.dealloc;
300 std::printf("vofmom_sync_budget: %s\n", ok ? "PASS" : "FAIL");
301 return ok ? 0 : 1;
302}
303} // namespace
304
305int main(int argc, char** argv) {
306 Kokkos::initialize(argc, argv);
307 int rc = 0;
308 {
309 using MemSpace = Kokkos::DefaultExecutionSpace::memory_space;
310 const bool hoist = argc > 1 && std::strcmp(argv[argc - 1], "hoist") == 0;
311 const bool vofmom = argc > 1 && std::strcmp(argv[argc - 1], "vofmom") == 0;
312 if (std::is_same_v<MemSpace, Kokkos::HostSpace>) {
313 std::printf("device_sync_budget: host backend, nothing to measure -- SKIP\n");
314 rc = 77;
315 } else if (vofmom) {
316 rc = vofMomBudget();
317 } else if (hoist) {
318 rc = hoistBitwise();
319 } else {
320 rc = budget(false) | budget(true);
321 }
322 }
323 Kokkos::finalize();
324 return rc;
325}
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
y
Definition cmp.py:24
advance(n=128, warm=5, steps=5)
Definition gperf.py:30
int m
Definition post.py:35
tuple rc
Definition post.py:37
static constexpr int NY
static constexpr int NX
static constexpr int NZ
peclet::flow::Solver< peclet::flow::Staggered > Stag
static constexpr double U
static constexpr double R
static bool same(double a, double b)