Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_TinyProfiler.H
Go to the documentation of this file.
1#ifndef AMREX_TINY_PROFILER_H_
2#define AMREX_TINY_PROFILER_H_
3#include <AMReX_Config.H>
4
5#include <AMReX_INT.H>
6#include <AMReX_REAL.H>
7
8#include <array>
9#include <deque>
10#include <iosfwd>
11#include <limits>
12#include <map>
13#include <string>
14#include <tuple>
15#include <utility>
16#include <vector>
17
18namespace amrex {
19
20struct MemStat
21{
23 Long nfree = 0;
25 double avgmem = 0.;
27};
28
30class TinyProfiler
31{
32public:
33 explicit TinyProfiler (std::string funcname) noexcept;
34 TinyProfiler (std::string funcname, bool start_) noexcept;
35 TinyProfiler (std::string funcname, bool start_, bool gpu_sync_) noexcept;
36 explicit TinyProfiler (const char* funcname) noexcept;
37 TinyProfiler (const char* funcname, bool start_) noexcept;
38 TinyProfiler (const char* funcname, bool start_, bool gpu_sync_) noexcept;
40
41 TinyProfiler (TinyProfiler const&) = delete;
45
46 void start ();
47 void stop ();
48
49 void memory_start () const noexcept;
50 void memory_stop () const noexcept;
51
52 static MemStat* memory_alloc (std::size_t nbytes,
53 std::map<std::string, MemStat>& memstats) noexcept;
54 static void memory_free (std::size_t nbytes, MemStat* stat) noexcept;
55
56 static void Initialize ();
57 static void Finalize (bool bFlushing = false);
58
59 static void MemoryInitialize ();
60 static void MemoryFinalize (bool bFlushing = false);
61
62 static bool RegisterArena (const std::string& memory_name,
63 std::map<std::string, MemStat>& memstats) noexcept;
64
65 static void DeregisterArena (std::map<std::string, MemStat>& memstats) noexcept;
66
67 static void StartRegion (std::string regname) noexcept;
68 static void StartRegion (std::string regname, bool gpu_sync) noexcept;
69 static void StopRegion (const std::string& regname) noexcept;
70
71 static void PrintCallStack (std::ostream& os);
72
84 [[nodiscard]] static const char* CurrentName () noexcept;
85
86 static void PrintMemoryUsage (std::ostream* os, bool only_local) noexcept;
87
88private:
89 struct Stats
90 {
91 Stats () noexcept = default;
92 int depth{0};
93 Long n{0L};
94 double dtin{0.0};
95 double dtex{0.0};
96 };
97
98 struct TimerKey
99 {
100 std::string name;
101 bool gpu_sync = false;
102
103 bool operator< (TimerKey const& rhs) const noexcept
104 {
105 int const c = name.compare(rhs.name);
106 return c < 0 || (c == 0 && gpu_sync < rhs.gpu_sync);
107 }
108 };
109
110 using RegionStats = std::map<TimerKey, Stats>;
111 using StatsMap = std::map<std::string, RegionStats>;
112
114 struct ProcStats
115 {
116
117 Long nmin{std::numeric_limits<Long>::max()};
118 Long navg{0L}, nmax{0L};
119 double dtinmin{std::numeric_limits<double>::max()};
120 double dtinavg{0.0}, dtinmax{0.0};
121 double dtexmin{std::numeric_limits<double>::max()};
122 double dtexavg{0.0}, dtexmax{0.0};
123 bool do_print{true};
124 std::string fname;
125 static bool compex (const ProcStats& lhs, const ProcStats& rhs) {
126 return lhs.dtexmax > rhs.dtexmax;
127 }
128 static bool compin (const ProcStats& lhs, const ProcStats& rhs) {
129 return lhs.dtinmax > rhs.dtinmax;
130 }
131 };
132
133 struct MemProcStats
134 {
135 Long nalloc = 0;
136 Long nfree = 0;
137 Long curmem_max = 0;
138 Long avgmem_min = std::numeric_limits<Long>::max();
139 Long avgmem_avg = 0;
140 Long avgmem_max = 0;
141 Long maxmem_min = std::numeric_limits<Long>::max();
142 Long maxmem_avg = 0;
143 Long maxmem_max = 0;
144 std::string fname;
145 static bool compmem (const MemProcStats& lhs, const MemProcStats& rhs) {
146 return lhs.maxmem_max > rhs.maxmem_max;
147 }
148 };
149
151 TimerKey key;
152 [[maybe_unused]] bool gpu_sync = false; // only used on GPU builds
153 bool in_parallel_region = false;
154 int global_depth = -1;
155 std::vector<Stats*> stats;
156 std::vector<Stats*> selective_stats;
158 const char* prev_name = nullptr;
159
160 static std::deque<const TinyProfiler*> mem_stack;
161
162#ifdef AMREX_USE_OMP
163 struct aligned_deque {
164 // align thread-private data to cache lines to prevent false sharing
165 alignas(64) std::deque<const TinyProfiler*> deque;
166 };
167
168 static std::vector<aligned_deque> mem_stack_thread_private;
169#endif
170 static std::vector<std::map<std::string, MemStat>*> all_memstats;
171 static std::vector<std::string> all_memnames;
172
173 static std::vector<std::string> regionstack;
174 static std::vector<std::string> selective_regionstack;
175 static std::vector<std::tuple<std::string,bool,bool> > regionstartstack;
177 static std::deque<std::tuple<double,double,std::string*,double> > ttstack;
178 static StatsMap statsmap;
179 static StatsMap selective_statsmap;
180 static bool selective_reporting;
181 static double t_init;
182 static double t_memory_init;
183 static bool device_synchronize_around_region;
184 static int n_print_tabs;
185 static int verbose;
186 static double print_threshold;
187 static bool enabled;
188 static bool memprof_enabled;
189 static std::string output_file;
190
191 static std::string const& get_output_file ();
192 static void PrintStats (RegionStats& regstats, double dt_max,
193 std::ostream* os);
194 static void PrintMemStats (std::map<std::string, MemStat>& memstats,
195 std::string const& memname, double dt_max,
196 double t_final, std::ostream* os, bool only_local);
197};
198
200{
201public:
202 explicit TinyProfileRegion (std::string a_regname) noexcept;
203 explicit TinyProfileRegion (const char* a_regname) noexcept;
204 TinyProfileRegion (std::string a_regname, bool gpu_sync) noexcept;
205 TinyProfileRegion (const char* a_regname, bool gpu_sync) noexcept;
211private:
212 std::string regname;
213 TinyProfiler tprof;
214};
215
216}
217#endif
~TinyProfileRegion()
Definition AMReX_TinyProfiler.cpp:1138
TinyProfileRegion & operator=(TinyProfileRegion const &)=delete
TinyProfileRegion(TinyProfileRegion const &)=delete
TinyProfileRegion(std::string a_regname) noexcept
Definition AMReX_TinyProfiler.cpp:1106
TinyProfileRegion(TinyProfileRegion &&)=delete
A simple profiler that returns basic performance information (e.g. min, max, and average running time...
Definition AMReX_BLProfiler.H:636
static void Initialize()
Definition AMReX_TinyProfiler.cpp:413
static bool RegisterArena(const std::string &memory_name, std::map< std::string, MemStat > &memstats) noexcept
Definition AMReX_TinyProfiler.cpp:593
static void PrintMemoryUsage(std::ostream *os, bool only_local) noexcept
Definition AMReX_TinyProfiler.cpp:1164
void start()
Definition AMReX_TinyProfiler.cpp:149
TinyProfiler(TinyProfiler const &)=delete
TinyProfiler & operator=(TinyProfiler const &)=delete
static MemStat * memory_alloc(std::size_t nbytes, std::map< std::string, MemStat > &memstats) noexcept
Definition AMReX_TinyProfiler.cpp:369
static void PrintCallStack(std::ostream &os)
Definition AMReX_TinyProfiler.cpp:1145
TinyProfiler(std::string funcname) noexcept
Definition AMReX_TinyProfiler.cpp:107
void stop()
Definition AMReX_TinyProfiler.cpp:240
static void MemoryInitialize()
Definition AMReX_TinyProfiler.cpp:447
static void Finalize(bool bFlushing=false)
Definition AMReX_TinyProfiler.cpp:468
void memory_start() const noexcept
Definition AMReX_TinyProfiler.cpp:330
void memory_stop() const noexcept
Definition AMReX_TinyProfiler.cpp:347
static void StartRegion(std::string regname) noexcept
Definition AMReX_TinyProfiler.cpp:1059
~TinyProfiler()
Definition AMReX_TinyProfiler.cpp:143
static void StopRegion(const std::string &regname) noexcept
Definition AMReX_TinyProfiler.cpp:1086
static void memory_free(std::size_t nbytes, MemStat *stat) noexcept
Definition AMReX_TinyProfiler.cpp:398
static void MemoryFinalize(bool bFlushing=false)
Definition AMReX_TinyProfiler.cpp:564
TinyProfiler(TinyProfiler &&)=delete
static void DeregisterArena(std::map< std::string, MemStat > &memstats) noexcept
Definition AMReX_TinyProfiler.cpp:604
static const char * CurrentName() noexcept
Without the tiny profiler, there is no active profiler.
Definition AMReX_BLProfiler.H:639
amrex_long Long
Definition AMReX_INT.H:30
Definition AMReX_Amr.cpp:50
Definition AMReX_Arena.H:21
Long currentmem
amount of currently used memory in bytes
Definition AMReX_TinyProfiler.H:24
Long maxmem
running maximum of currentmem
Definition AMReX_TinyProfiler.H:26
Long nalloc
number of allocations
Definition AMReX_TinyProfiler.H:22
Long nfree
number of frees
Definition AMReX_TinyProfiler.H:23
double avgmem
memory used (bytes) times time in use (seconds)
Definition AMReX_TinyProfiler.H:25