Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
profiler.hpp
Go to the documentation of this file.
1// ----------------------------------------------------------------------
2//
3// Copyright (C) 2024 - 2026 by the Vulpes authors.
4// This file is part of Vulpes.
5//
6// Vulpes is free software; you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// Vulpes is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// General Public License for more details.
15//
16// You should have received a copy of the GNU General Public License
17// along with Vulpes. If not, see <http://www.gnu.org/licenses/>.
18//
19// ----------------------------------------------------------------------
20
21// Author: Matteo Caldana <matteo.caldana@polimi.it>
22
23#ifndef VULPES_UTILS_PROFILER_HPP_
24#define VULPES_UTILS_PROFILER_HPP_
25
26#include <algorithm>
27#include <chrono>
28#include <cstddef>
29#include <iomanip>
30#include <iostream>
31#include <string>
32#include <unordered_map>
33#include <utility>
34#include <vector>
35
36#include <nlohmann/json.hpp>
38// NOLINTBEGIN
44#define PROFILE(func) \
45 do { \
46 Profiler::start(#func); \
47 func; \
48 Profiler::stop(#func); \
49 } while (0)
50
56#define PROFILE_INTERVAL(func) \
57 do { \
58 Profiler::startInterval(#func); \
59 func; \
60 Profiler::stopInterval(#func); \
61 } while (0)
62// NOLINTEND
63
73class Profiler {
74 public:
80 static void setEnabled(bool enabled) { s_enabled = enabled; }
81
82#ifndef NPROFILE
84 static void start(const char* label) {
85 if (s_enabled) {
86 start(std::string(label));
87 }
88 }
89
90 static void stop(const char* label) {
91 if (s_enabled) {
92 stop(std::string(label));
93 }
94 }
95
96 static void startInterval(const char* label) {
97 if (s_enabled) {
98 startInterval(std::string(label));
99 }
100 }
101
102 static void stopInterval(const char* label) {
103 if (s_enabled) {
104 stopInterval(std::string(label));
105 }
106 }
107
116 static void start(const std::string& label);
117
126 static void startInterval(const std::string& label);
127
135 static void stop(const std::string& label);
136
143 static void stopInterval(const std::string& label);
144
151 static void print();
152
156 static void clear();
157
174 static nlohmann::json dumpJson();
175
179 static size_t getDuration(const std::string& label);
180
181#else
182 // If NPROFILE is defined, these functions do nothing; the const char*
183 // overloads keep a literal label from building a std::string
184 static void start(const char*) {}
185 static void stop(const char*) {}
186 static void startInterval(const char*) {}
187 static void stopInterval(const char*) {}
188 static void start(const std::string&) {}
189 static void stop(const std::string&) {}
190 static void startInterval(const std::string&) {}
191 static void stopInterval(const std::string&) {}
192 static void print() {}
193 static void clear() {}
194 static nlohmann::json dumpJson() { return {}; }
195 static size_t getDuration(const std::string&) { return -1; };
196
197#endif
198
199 inline static bool s_enabled = true;
200
201 using time_point = std::chrono::high_resolution_clock::time_point;
202 using TimeCounter = std::pair<std::size_t, std::size_t>;
205 static std::unordered_map<std::string, TimeCounter> time_map;
207 static std::unordered_map<std::string, time_point> start_times;
208
210 static std::unordered_map<std::string, std::vector<time_point>>
213 static std::unordered_map<std::string, std::vector<time_point>>
215};
216
217#endif // VULPES_UTILS_PROFILER_HPP_
Collects high-resolution timing data for labeled code sections.
Definition profiler.hpp:73
static bool s_enabled
Definition profiler.hpp:199
std::pair< std::size_t, std::size_t > TimeCounter
Definition profiler.hpp:202
static void startInterval(const char *label)
startInterval(label) when recording is on.
Definition profiler.hpp:96
static size_t getDuration(const std::string &label)
Get the time elapsed for a certain label.
Definition profiler.cpp:187
static std::unordered_map< std::string, std::vector< time_point > > start_time_intervals
Interval start timestamps recorded per label via startInterval().
Definition profiler.hpp:48
static void stop(const char *label)
stop(label) when recording is on.
Definition profiler.hpp:90
static void clear()
Remove all recorded timing data.
Definition profiler.cpp:153
static std::unordered_map< std::string, time_point > start_times
Pending start time recorded per label via start(), consumed by stop().
Definition profiler.hpp:39
static void stopInterval(const char *label)
stopInterval(label) when recording is on.
Definition profiler.hpp:102
static nlohmann::json dumpJson()
Export collected profile data as a JSON object including interval timestamps.
Definition profiler.cpp:159
static std::unordered_map< std::string, TimeCounter > time_map
Definition profiler.hpp:37
static std::unordered_map< std::string, std::vector< time_point > > stop_time_intervals
Interval end timestamps recorded per label via stopInterval().
Definition profiler.hpp:44
static void print()
Print accumulated timings for all labels in milliseconds.
Definition profiler.cpp:91
static void setEnabled(bool enabled)
Turn recording on or off (on by default), outside timed regions: a stop() without its start() records...
Definition profiler.hpp:80
static void start(const char *label)
start(label) when recording is on.
Definition profiler.hpp:84
std::chrono::high_resolution_clock::time_point time_point
Definition profiler.hpp:201