peclet-dem 1.1.0
Performance-portable XPBD Discrete Element Method (Kokkos + ArborX)
Loading...
Searching...
No Matches
ktimer.cpp
Go to the documentation of this file.
1// Minimal Kokkos Tools kernel timer: accumulates wall time per kernel name, prints on finalize
2// (rank 0 only if OMPI_COMM_WORLD_RANK is set). For the session's WO-11 cost attribution.
3#include <chrono>
4#include <cstdint>
5#include <cstdio>
6#include <cstdlib>
7#include <map>
8#include <string>
9#include <vector>
10#include <algorithm>
11namespace {
12using clk = std::chrono::steady_clock;
13struct Rec { double t = 0; long n = 0; };
14std::map<std::string, Rec> recs;
15struct Fr { std::string n; clk::time_point t; double cov = 0; };
16std::vector<Fr> stack;
17clk::time_point dcT;
18std::uint64_t nextId = 0;
19std::map<std::uint64_t, std::pair<std::string, clk::time_point>> open;
20void begin(const char* name, std::uint64_t* id) { *id = nextId++; open[*id] = {name, clk::now()}; }
21void end(std::uint64_t id) {
22 auto it = open.find(id); if (it == open.end()) return;
23 auto& r = recs[it->second.first];
24 const double d = std::chrono::duration<double>(clk::now() - it->second.second).count();
25 r.t += d; ++r.n; open.erase(it); if (!stack.empty()) stack.back().cov += d;
26}
27}
28extern "C" {
29void kokkosp_init_library(const int, const std::uint64_t, const std::uint32_t, void*) {}
31 const char* r = std::getenv("OMPI_COMM_WORLD_RANK");
32 if (r && std::atoi(r) != 0) return;
33 std::vector<std::pair<std::string, Rec>> v(recs.begin(), recs.end());
34 std::sort(v.begin(), v.end(), [](auto& a, auto& b) { return a.second.t > b.second.t; });
35 double tot = 0; for (auto& x : v) tot += x.second.t;
36 std::fprintf(stderr, "KTIMER total_kernel_s=%.4f\n", tot);
37 int k = 0;
38 for (auto& x : v) { if (k++ >= 400) break; std::fprintf(stderr, "KT %9.4f s %8ld %s\n", x.second.t, x.second.n, x.first.c_str()); }
39}
40void kokkosp_begin_parallel_for(const char* n, const std::uint32_t, std::uint64_t* id) { begin(n, id); }
41void kokkosp_end_parallel_for(const std::uint64_t id) { end(id); }
42void kokkosp_begin_parallel_reduce(const char* n, const std::uint32_t, std::uint64_t* id) { begin(n, id); }
43void kokkosp_end_parallel_reduce(const std::uint64_t id) { end(id); }
44void kokkosp_begin_parallel_scan(const char* n, const std::uint32_t, std::uint64_t* id) { begin(n, id); }
45void kokkosp_end_parallel_scan(const std::uint64_t id) { end(id); }
46void kokkosp_push_profile_region(const char* n) { stack.push_back({std::string("REGION ") + n, clk::now(), 0}); }
48 if (stack.empty()) return;
49 const double d = std::chrono::duration<double>(clk::now() - stack.back().t).count();
50 auto& r = recs[stack.back().n]; r.t += d; ++r.n;
51 auto& x = recs["XHOST " + stack.back().n.substr(7)]; x.t += d - stack.back().cov; ++x.n;
52 stack.pop_back(); if (!stack.empty()) stack.back().cov += d;
53}
54void kokkosp_begin_deep_copy(void*, const char*, const void*, void*, const char*, const void*, const std::uint64_t) { dcT = clk::now(); }
55void kokkosp_end_deep_copy() { const double d = std::chrono::duration<double>(clk::now() - dcT).count(); auto& r = recs["deep_copy(all)"]; r.t += d; ++r.n; if (!stack.empty()) stack.back().cov += d; }
56}
void kokkosp_end_parallel_for(const std::uint64_t id)
Definition ktimer.cpp:41
void kokkosp_begin_parallel_reduce(const char *n, const std::uint32_t, std::uint64_t *id)
Definition ktimer.cpp:42
void kokkosp_init_library(const int, const std::uint64_t, const std::uint32_t, void *)
Definition ktimer.cpp:29
void kokkosp_end_deep_copy()
Definition ktimer.cpp:55
void kokkosp_begin_deep_copy(void *, const char *, const void *, void *, const char *, const void *, const std::uint64_t)
Definition ktimer.cpp:54
void kokkosp_end_parallel_scan(const std::uint64_t id)
Definition ktimer.cpp:45
void kokkosp_begin_parallel_for(const char *n, const std::uint32_t, std::uint64_t *id)
Definition ktimer.cpp:40
void kokkosp_end_parallel_reduce(const std::uint64_t id)
Definition ktimer.cpp:43
void kokkosp_pop_profile_region()
Definition ktimer.cpp:47
void kokkosp_finalize_library()
Definition ktimer.cpp:30
void kokkosp_begin_parallel_scan(const char *n, const std::uint32_t, std::uint64_t *id)
Definition ktimer.cpp:44
void kokkosp_push_profile_region(const char *n)
Definition ktimer.cpp:46