b2api
B2000++ API Reference Manual, VERSION 4.6
 
Loading...
Searching...
No Matches
b2timing.H
1//------------------------------------------------------------------------
2// b2timing.H --
3//
4// written by Harald Klimach <harald.klimach@dlr.de>
5//
6// (c) 2024 Deutsches Zentrum für Luft- und Raumfahrt (DLR) e.V.
7// Linder Höhe, 51147 Köln
8//
9// All Rights Reserved. Proprietary source code. The contents of
10// this file may not be disclosed to third parties, copied or
11// duplicated in any form, in whole or in part, without the prior
12// written permission of SMR or DLR.
13//
14// This timing infrastructure provides a map of timers that are uniquely
15// identified by strings.
16// After the creation of a timing object in that map of timers it can
17// be started and stopped during program execution.
18// There is no relation established between the timers and the regions
19// they time may overlap or not. If you put timers into the code to
20// assess the running times of different code segments you need to
21// take care of those yourself.
22// Timing information will be put into CSV files with one file per MPI
23// rank. This can be controlled via the -timer-maxrank option. By default
24// only the root process will write out its timing information.
25//
26// This is a convenience solution, as there are some problems with tracing
27// OpenMP and TBB paralle programs.
28//
29//------------------------------------------------------------------------
30
31#ifndef B2TIMING_H_
32#define B2TIMING_H_
33
34#include <chrono>
35#include <iostream>
36#include <map>
37#include <string>
38
39using namespace std::chrono_literals;
40
41namespace b2000 {
42
67class Timing {
68public:
69 Timing() {}
70
76 void start();
77
85 void stop();
86
89 double avg_duration() const;
90
93 size_t count() const;
94
96 double duration() const;
97
102 void print(std::ostream& out) const;
103
104private:
105 std::chrono::time_point<std::chrono::high_resolution_clock> start_;
106 std::chrono::duration<double> duration_{0s};
107 bool active_{false};
108 size_t count_{0};
109
110}; // class Timing
111
112std::ostream& operator<<(std::ostream& out, const Timing& tim);
113
115using timer_map_t = std::map<std::string, Timing>;
116
131extern timer_map_t timers;
132
139inline timer_map_t::iterator obtain_timer(std::string name) {
140 return timers
141 .emplace(std::piecewise_construct, std::forward_as_tuple(name), std::forward_as_tuple())
142 .first;
143}
144
150void print_timers(std::string filename);
151
153std::string timing_fn(std::string logdir, int rank);
154
155} // namespace b2000
156
157#endif // B2TIMING_H_
Definition b2timing.H:67
void print(std::ostream &out) const
Definition b2timing.C:56
double avg_duration() const
Definition b2timing.C:48
void stop()
Definition b2timing.C:35
void start()
Definition b2timing.C:28
size_t count() const
Definition b2timing.C:46
double duration() const
Get the overall duration of this timing recorded so far.
Definition b2timing.C:44
Contains the base classes for implementing Finite Elements.
Definition b2boundary_condition.H:32
std::map< std::string, Timing > timer_map_t
A global map of timers to hold all the timings in the application.
Definition b2timing.H:115
void print_timers(std::string filename)
Definition b2timing.C:65
timer_map_t timers
Definition b2timing.C:26
timer_map_t::iterator obtain_timer(std::string name)
Definition b2timing.H:139
std::string timing_fn(std::string logdir, int rank)
Construct a filename for timer output.
Definition b2timing.C:80