24#include <Kokkos_Core.hpp>
32long gGlobal = 0, gAlloc = 0, gDealloc = 0;
33std::map<std::string, long> gCombined;
35std::map<std::string, long> gAllocBy;
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;
45void onBeginFence(
const char* name,
const uint32_t devid, uint64_t* handle) {
51 if (std::strstr(name,
"Profile Tool Fence") !=
nullptr)
53 const auto id = Kokkos::Tools::Experimental::identifier_from_devid(devid);
56 if (isDevice(devid) &&
id.instance_id == 0 &&
id.device_id == 0)
58 if (std::strstr(name,
"fence after copying values back") !=
nullptr)
59 ++gCombined[gReduce.empty() ? std::string(
"(no parallel_reduce)") : gReduce];
61void onEndFence(uint64_t) {}
62void onBeginReduce(
const char* name,
const uint32_t, uint64_t* kid) {
67void onEndReduce(uint64_t) {}
68bool deviceSpace(
const Kokkos::Tools::SpaceHandle h) {
69 return std::strcmp(
h.name,
"Host") != 0;
71void onAlloc(
const Kokkos::Tools::SpaceHandle h,
const char* label,
const void*,
const uint64_t) {
72 if (gCount && deviceSpace(h)) {
75 if (std::strstr(label,
"TeamScratchMemory") ==
nullptr)
77 ++gAllocBy[std::string(
h.name) +
":" + label];
80void onDealloc(
const Kokkos::Tools::SpaceHandle h,
const char*,
const void*,
const uint64_t) {
81 if (gCount && deviceSpace(h))
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;
99constexpr int NX = 32,
NY = 24,
NZ = 16;
102void setup(
Stag& s,
bool autoGuess) {
103 const double rl = 1.0, rg = 0.02, ml = 0.01, mg = 2
e-4,
g = 2
e-3,
R = 4.0;
104 s.setAdvection(
false);
107 s.setDomainBc(2, 1, 0.0, 0.0, 0.0);
108 s.setDomainBc(3, 1, 0.0, 0.0, 0.0);
109 s.setPressureGeometry(std::vector<double>((std::size_t)
NX *
NY *
NZ, 10.0));
111 s.setVof(std::vector<double>((std::size_t)
NX *
NY *
NZ, 0.0));
114 s.setSurfaceTension(0.05);
115 s.setPressurePcg(
true, 800, 1e-8);
116 s.setPressureBottomMode(1);
118 s.setPressureVcyclePrecision(0);
119 s.setPressureInitialGuess(2, 2);
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;
126 {(rl - rav) * g, (rg - rl) *
g});
127 s.setSuperficialVelocity(
true, 0, 0.0);
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;
141int budget(
bool autoGuess) {
142 const char*
tag = autoGuess ?
"auto+projected" :
"fp64+zero";
143 gGlobal = gAlloc = gDealloc = 0;
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);
164 const double perG = (double)gGlobal / steps;
165 const double perA = (double)gAlloc / steps, perD = (
double)gDealloc /
steps;
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)
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");
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();
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 (> "
187 tag, perG, kGlobalBudget, (
double)comb / steps, perA, perD, kAllocBudget, pre);
188 if (perG > kGlobalBudget || perA > kAllocBudget || perD > kAllocBudget || pre <= 0)
190 std::printf(
"device_sync_budget [%s]: %s\n", tag, rc ?
"FAIL" :
"PASS");
196 const int steps = 10;
197 std::vector<std::vector<double>>
st[2];
199 for (
int on = 0; on < 2; ++on) {
202 s.setPressurePrefactorHoist(on == 1);
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();
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) {
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");
219 std::printf(
"prefactor_hoist_bitwise: prefactors off %ld (== 0), on %ld (> 0): %s\n", pre[0],
220 pre[1], rc ?
"FAIL" :
"PASS");
231 double global, combined, alloc, dealloc;
234SyncCounts vofMomCounts(
const char* scheme) {
235 gGlobal = gAlloc = gDealloc = 0;
239 const double ratio = 1
e3,
U[3] = {1.0, 0.6, -0.4};
241 s.setAdvection(
false);
245 s.setPressureGeometry(std::vector<double>((std::size_t)n * n * n, 10.0));
246 s.setPressureChebyshev(
true, 300, 1e-13);
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));
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) {
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);
284 for (
const auto& [label, k] : gCombined)
286 return {(double)gGlobal / steps, (
double)comb /
steps, (double)gAlloc / steps,
287 (
double)gDealloc /
steps};
291 const SyncCounts up = vofMomCounts(
"upwind");
292 const SyncCounts
ph = vofMomCounts(
"muscl_phase");
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 / "
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");
305int main(
int argc,
char** argv) {
306 Kokkos::initialize(argc, argv);
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");
320 rc = budget(
false) | budget(
true);
flow — host-facing Kokkos IBM Navier-Stokes solver (drop-in flow-style API).
advance(n=128, warm=5, steps=5)
peclet::flow::Solver< peclet::flow::Staggered > Stag
static constexpr double U
static constexpr double R
static bool same(double a, double b)