Block-Structured AMR Software Framework
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 
18 namespace amrex {
19 
20 struct MemStat
21 {
22  Long nalloc = 0;
23  Long nfree = 0;
24  Long currentmem = 0;
25  double avgmem = 0.;
26  Long maxmem = 0;
27 };
28 
31 {
32 public:
33  explicit TinyProfiler (std::string funcname) noexcept;
34  TinyProfiler (std::string funcname, bool start_) noexcept;
35  explicit TinyProfiler (const char* funcname) noexcept;
36  TinyProfiler (const char* funcname, bool start_) noexcept;
37  ~TinyProfiler ();
38 
39  TinyProfiler (TinyProfiler const&) = delete;
40  TinyProfiler (TinyProfiler &&) = delete;
43 
44  void start () noexcept;
45  void stop () noexcept;
46 
47  void memory_start () const noexcept;
48  void memory_stop () const noexcept;
49 
50  static MemStat* memory_alloc (std::size_t nbytes,
51  std::map<std::string, MemStat>& memstats) noexcept;
52  static void memory_free (std::size_t nbytes, MemStat* stat) noexcept;
53 
54  static void Initialize () noexcept;
55  static void Finalize (bool bFlushing = false) noexcept;
56 
57  static void MemoryInitialize () noexcept;
58  static void MemoryFinalize (bool bFlushing = false) noexcept;
59 
60  static bool RegisterArena (const std::string& memory_name,
61  std::map<std::string, MemStat>& memstats) noexcept;
62 
63  static void DeregisterArena (std::map<std::string, MemStat>& memstats) noexcept;
64 
65  static void StartRegion (std::string regname) noexcept;
66  static void StopRegion (const std::string& regname) noexcept;
67 
68  static void PrintCallStack (std::ostream& os);
69 
70 private:
71  struct Stats
72  {
73  Stats () noexcept = default;
74  int depth{0};
75  Long n{0L};
76  double dtin{0.0};
77  double dtex{0.0};
78  };
79 
81  struct ProcStats
82  {
83 
85  Long navg{0L}, nmax{0L};
87  double dtinavg{0.0}, dtinmax{0.0};
89  double dtexavg{0.0}, dtexmax{0.0};
90  bool do_print{true};
91  std::string fname;
92  static bool compex (const ProcStats& lhs, const ProcStats& rhs) {
93  return lhs.dtexmax > rhs.dtexmax;
94  }
95  static bool compin (const ProcStats& lhs, const ProcStats& rhs) {
96  return lhs.dtinmax > rhs.dtinmax;
97  }
98  };
99 
101  {
102  Long nalloc = 0;
103  Long nfree = 0;
105  Long avgmem_avg = 0;
106  Long avgmem_max = 0;
108  Long maxmem_avg = 0;
109  Long maxmem_max = 0;
110  std::string fname;
111  static bool compmem (const MemProcStats& lhs, const MemProcStats& rhs) {
112  return lhs.maxmem_max > rhs.maxmem_max;
113  }
114  };
115 
116  std::string fname;
117  bool in_parallel_region = false;
118  int global_depth = -1;
119  std::vector<Stats*> stats;
120 
121  static std::deque<const TinyProfiler*> mem_stack;
122 
123 #ifdef AMREX_USE_OMP
124  struct aligned_deque {
125  // align thread-private data to cache lines to prevent false sharing
126  alignas(64) std::deque<const TinyProfiler*> deque;
127  };
128 
129  static std::vector<aligned_deque> mem_stack_thread_private;
130 #endif
131  static std::vector<std::map<std::string, MemStat>*> all_memstats;
132  static std::vector<std::string> all_memnames;
133 
134  static std::vector<std::string> regionstack;
135  static std::deque<std::tuple<double,double,std::string*> > ttstack;
136  static std::map<std::string,std::map<std::string, Stats> > statsmap;
137  static double t_init;
139  static int n_print_tabs;
140  static int verbose;
141  static double print_threshold;
142  static bool enabled;
143  static bool memprof_enabled;
144  static std::string output_file;
145 
146  static std::string const& get_output_file ();
147  static void PrintStats (std::map<std::string,Stats>& regstats, double dt_max,
148  std::ostream* os);
149  static void PrintMemStats (std::map<std::string, MemStat>& memstats,
150  std::string const& memname, double dt_max,
151  double t_final, std::ostream* os);
152 };
153 
155 {
156 public:
157  explicit TinyProfileRegion (std::string a_regname) noexcept;
158  explicit TinyProfileRegion (const char* a_regname) noexcept;
164 private:
165  std::string regname;
167 };
168 
169 }
170 #endif
Definition: AMReX_TinyProfiler.H:155
~TinyProfileRegion()
Definition: AMReX_TinyProfiler.cpp:943
TinyProfileRegion(TinyProfileRegion const &)=delete
TinyProfileRegion(std::string a_regname) noexcept
Definition: AMReX_TinyProfiler.cpp:927
TinyProfileRegion & operator=(TinyProfileRegion const &)=delete
TinyProfileRegion(TinyProfileRegion &&)=delete
std::string regname
Definition: AMReX_TinyProfiler.H:165
TinyProfiler tprof
Definition: AMReX_TinyProfiler.H:166
A simple profiler that returns basic performance information (e.g. min, max, and average running time...
Definition: AMReX_TinyProfiler.H:31
static bool memprof_enabled
Definition: AMReX_TinyProfiler.H:143
static std::vector< std::string > regionstack
Definition: AMReX_TinyProfiler.H:134
static bool RegisterArena(const std::string &memory_name, std::map< std::string, MemStat > &memstats) noexcept
Definition: AMReX_TinyProfiler.cpp:495
static void MemoryInitialize() noexcept
Definition: AMReX_TinyProfiler.cpp:335
static bool device_synchronize_around_region
Definition: AMReX_TinyProfiler.H:138
static std::deque< const TinyProfiler * > mem_stack
Definition: AMReX_TinyProfiler.H:121
static std::string const & get_output_file()
Definition: AMReX_TinyProfiler.cpp:961
static void PrintMemStats(std::map< std::string, MemStat > &memstats, std::string const &memname, double dt_max, double t_final, std::ostream *os)
Definition: AMReX_TinyProfiler.cpp:735
TinyProfiler & operator=(TinyProfiler const &)=delete
static void Finalize(bool bFlushing=false) noexcept
Definition: AMReX_TinyProfiler.cpp:354
static void MemoryFinalize(bool bFlushing=false) noexcept
Definition: AMReX_TinyProfiler.cpp:447
static bool enabled
Definition: AMReX_TinyProfiler.H:142
static int verbose
Definition: AMReX_TinyProfiler.H:140
static std::vector< std::string > all_memnames
Definition: AMReX_TinyProfiler.H:132
static double print_threshold
Definition: AMReX_TinyProfiler.H:141
std::vector< Stats * > stats
Definition: AMReX_TinyProfiler.H:119
static std::deque< std::tuple< double, double, std::string * > > ttstack
Definition: AMReX_TinyProfiler.H:135
TinyProfiler(TinyProfiler const &)=delete
static void PrintStats(std::map< std::string, Stats > &regstats, double dt_max, std::ostream *os)
Definition: AMReX_TinyProfiler.cpp:521
static MemStat * memory_alloc(std::size_t nbytes, std::map< std::string, MemStat > &memstats) noexcept
Definition: AMReX_TinyProfiler.cpp:267
void stop() noexcept
Definition: AMReX_TinyProfiler.cpp:155
std::string fname
Definition: AMReX_TinyProfiler.H:116
static void PrintCallStack(std::ostream &os)
Definition: AMReX_TinyProfiler.cpp:950
TinyProfiler(std::string funcname) noexcept
Definition: AMReX_TinyProfiler.cpp:64
bool in_parallel_region
Definition: AMReX_TinyProfiler.H:117
void start() noexcept
Definition: AMReX_TinyProfiler.cpp:94
static double t_init
Definition: AMReX_TinyProfiler.H:137
void memory_start() const noexcept
Definition: AMReX_TinyProfiler.cpp:228
void memory_stop() const noexcept
Definition: AMReX_TinyProfiler.cpp:245
static void StartRegion(std::string regname) noexcept
Definition: AMReX_TinyProfiler.cpp:908
static std::map< std::string, std::map< std::string, Stats > > statsmap
Definition: AMReX_TinyProfiler.H:136
~TinyProfiler()
Definition: AMReX_TinyProfiler.cpp:88
static void StopRegion(const std::string &regname) noexcept
Definition: AMReX_TinyProfiler.cpp:918
static void memory_free(std::size_t nbytes, MemStat *stat) noexcept
Definition: AMReX_TinyProfiler.cpp:296
static std::vector< std::map< std::string, MemStat > * > all_memstats
Definition: AMReX_TinyProfiler.H:131
static void Initialize() noexcept
Definition: AMReX_TinyProfiler.cpp:311
int global_depth
Definition: AMReX_TinyProfiler.H:118
TinyProfiler(TinyProfiler &&)=delete
static std::string output_file
Definition: AMReX_TinyProfiler.H:144
static int n_print_tabs
Definition: AMReX_TinyProfiler.H:139
static void DeregisterArena(std::map< std::string, MemStat > &memstats) noexcept
Definition: AMReX_TinyProfiler.cpp:506
@ max
Definition: AMReX_ParallelReduce.H:17
Definition: AMReX_Amr.cpp:49
Definition: AMReX_Arena.H:12
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
Definition: AMReX_TinyProfiler.H:101
static bool compmem(const MemProcStats &lhs, const MemProcStats &rhs)
Definition: AMReX_TinyProfiler.H:111
Long avgmem_avg
Definition: AMReX_TinyProfiler.H:105
Long maxmem_avg
Definition: AMReX_TinyProfiler.H:108
Long nalloc
Definition: AMReX_TinyProfiler.H:102
Long avgmem_max
Definition: AMReX_TinyProfiler.H:106
Long maxmem_min
Definition: AMReX_TinyProfiler.H:107
Long nfree
Definition: AMReX_TinyProfiler.H:103
Long maxmem_max
Definition: AMReX_TinyProfiler.H:109
Long avgmem_min
Definition: AMReX_TinyProfiler.H:104
std::string fname
Definition: AMReX_TinyProfiler.H:110
stats across processes
Definition: AMReX_TinyProfiler.H:82
double dtexmin
Definition: AMReX_TinyProfiler.H:88
Long navg
Definition: AMReX_TinyProfiler.H:85
double dtexavg
Definition: AMReX_TinyProfiler.H:89
std::string fname
Definition: AMReX_TinyProfiler.H:91
static bool compin(const ProcStats &lhs, const ProcStats &rhs)
Definition: AMReX_TinyProfiler.H:95
Long nmin
Definition: AMReX_TinyProfiler.H:84
Long nmax
Definition: AMReX_TinyProfiler.H:85
double dtexmax
Definition: AMReX_TinyProfiler.H:89
bool do_print
Definition: AMReX_TinyProfiler.H:90
double dtinavg
Definition: AMReX_TinyProfiler.H:87
double dtinmax
Definition: AMReX_TinyProfiler.H:87
double dtinmin
Definition: AMReX_TinyProfiler.H:86
static bool compex(const ProcStats &lhs, const ProcStats &rhs)
Definition: AMReX_TinyProfiler.H:92
Definition: AMReX_TinyProfiler.H:72
Stats() noexcept=default