54#ifndef OPENSWMM_ENGINE_2D_GPU_KOKKOS_PERF_COUNTERS_HPP
55#define OPENSWMM_ENGINE_2D_GPU_KOKKOS_PERF_COUNTERS_HPP
57#include <Kokkos_Core.hpp>
118using clock = std::chrono::steady_clock;
124inline thread_local std::vector<clock::time_point>
fence_t0;
139inline void begin_dispatch(
const char* name, std::uint32_t , std::uint64_t* kID) {
144 const auto it = g.label_id.find(name);
146 if (it == g.label_id.end()) {
147 id = g.kernels.size();
148 g.label_id.emplace(name,
id);
149 g.kernels.push_back({name, 0, 0.0});
171 const double dt = std::chrono::duration<double>(clock::now() -
dispatch_t0.back()).count();
173 if (kID < g.kernels.size()) {
175 g.kernels[kID].sec += dt;
187inline void begin_fence(
const char* , std::uint32_t , std::uint64_t* handle) {
208 g.sec_fence += std::chrono::duration<double>(clock::now() -
fence_t0.back()).count();
224inline void begin_deep_copy(Kokkos_Profiling_SpaceHandle dst,
const char*,
const void*,
225 Kokkos_Profiling_SpaceHandle src,
const char*,
const void*,
226 std::uint64_t size) {
231 const bool dst_host = std::strncmp(dst.name,
"Host", 4) == 0;
232 const bool src_host = std::strncmp(src.name,
"Host", 4) == 0;
233 if (src_host && !dst_host)
235 else if (dst_host && !src_host)
238 g.bytes_same += size;
253 static const bool done = [] {
256 if (std::getenv(
"KOKKOS_TOOLS_LIBS") || std::getenv(
"KOKKOS_PROFILE_LIBRARY")) {
257 std::fprintf(stderr,
"[PERF-2D-KOKKOS] external Kokkos tool loaded; in-process "
258 "counters disabled for this run.\n");
261 namespace KTE = Kokkos::Tools::Experimental;
319 const double adv =
static_cast<double>(g.n_advance);
321 "[PERF-2D-KOKKOS] advances=%ld launches=%ld launches_per_advance=%.1f "
322 "fences=%ld fence_s=%.4f deep_copies=%ld "
323 "h2d_bytes=%llu d2h_bytes=%llu samespace_bytes=%llu "
324 "h2d_bytes_per_advance=%.0f d2h_bytes_per_advance=%.0f\n",
325 g.n_advance, g.n_launch, g.n_launch / adv, g.n_fence, g.sec_fence, g.n_deep_copy,
326 g.bytes_h2d, g.bytes_d2h, g.bytes_same, g.bytes_h2d / adv, g.bytes_d2h / adv);
327 std::vector<const KernelEntry*> order;
328 order.reserve(g.kernels.size());
329 for (
const auto& k : g.kernels)
331 std::sort(order.begin(), order.end(),
334 std::fprintf(stderr,
"[PERF-2D-KOKKOS-KERNEL] name=%s n=%ld s=%.4f per_advance=%.1f\n",
335 k->label.c_str(), k->n, k->sec, k->n / adv);
Performance timing and phase accumulators for engine execution.
~Scope()
Restore this thread's previous counter attribution on scope exit.
Definition KokkosPerfCounters.hpp:109
Scope(const Scope &)=delete
Scope(Counters &counters)
Attribute this thread's nested Kokkos dispatches to one solver counter set.
Definition KokkosPerfCounters.hpp:103
bool enabled() noexcept
Read the process-cached profiling environment gate.
Definition PerfTimers.hpp:238
Definition KokkosPerfCounters.hpp:116
std::chrono::steady_clock clock
Definition KokkosPerfCounters.hpp:118
void end_dispatch(std::uint64_t kID)
Finish a paired host-side dispatch timing record.
Definition KokkosPerfCounters.hpp:165
thread_local std::vector< clock::time_point > dispatch_t0
Definition KokkosPerfCounters.hpp:123
thread_local std::vector< clock::time_point > fence_t0
Definition KokkosPerfCounters.hpp:124
void end_fence(std::uint64_t)
Accumulate elapsed host wall for a paired Kokkos fence.
Definition KokkosPerfCounters.hpp:202
void begin_deep_copy(Kokkos_Profiling_SpaceHandle dst, const char *, const void *, Kokkos_Profiling_SpaceHandle src, const char *, const void *, std::uint64_t size)
Count a Kokkos deep-copy dispatch and its logical direction/bytes.
Definition KokkosPerfCounters.hpp:224
void begin_dispatch(const char *name, std::uint32_t, std::uint64_t *kID)
Begin one paired host-dispatch timing record.
Definition KokkosPerfCounters.hpp:139
void begin_fence(const char *, std::uint32_t, std::uint64_t *handle)
Start timing a Kokkos fence callback pair.
Definition KokkosPerfCounters.hpp:187
Definition KokkosPerfCounters.hpp:71
thread_local Counters * active
Definition KokkosPerfCounters.hpp:92
void dump()
One [PERF-2D-KOKKOS] summary line plus one line per kernel label, sorted by host-side dispatch wall d...
Definition KokkosPerfCounters.hpp:313
void reset()
Replace active solver counters with empty defaults.
Definition KokkosPerfCounters.hpp:283
void install()
Registers the counting callbacks once per process. No-op unless OPENSWMM_PERF is set; defers to an ex...
Definition KokkosPerfCounters.hpp:252
void count_advance()
Count one managed solver advance when profiling is enabled.
Definition KokkosPerfCounters.hpp:295
Definition KokkosPerfCounters.hpp:79
std::map< std::string, std::uint64_t > label_id
Definition KokkosPerfCounters.hpp:87
long n_advance
Definition KokkosPerfCounters.hpp:80
unsigned long long bytes_same
Definition KokkosPerfCounters.hpp:85
long n_deep_copy
Definition KokkosPerfCounters.hpp:84
long n_launch
parallel_for + reduce + scan
Definition KokkosPerfCounters.hpp:81
unsigned long long bytes_d2h
Definition KokkosPerfCounters.hpp:85
std::vector< KernelEntry > kernels
indexed by kernel id
Definition KokkosPerfCounters.hpp:86
double sec_fence
Definition KokkosPerfCounters.hpp:83
long n_fence
Definition KokkosPerfCounters.hpp:82
unsigned long long bytes_h2d
Definition KokkosPerfCounters.hpp:85
Definition KokkosPerfCounters.hpp:73
std::string label
Definition KokkosPerfCounters.hpp:74
double sec
host-side dispatch wall (== execution on OMP)
Definition KokkosPerfCounters.hpp:76
long n
launches
Definition KokkosPerfCounters.hpp:75