FML
Loading...
Searching...
No Matches
Timings.h
Go to the documentation of this file.
1#ifndef TIMINGS_HEADER
2#define TIMINGS_HEADER
3
4#include <chrono>
5#include <iomanip>
6#include <iostream>
7#include <map>
8#include <mutex>
9#include <string>
10
11#ifdef USE_MPI
12#include <mpi.h>
13namespace FML {
14 // The MPI communicator used by the library (defined in Global.cpp)
15 extern MPI_Comm communicator;
16} // namespace FML
17#endif
18
19namespace FML {
20
21 /// This namespace contains useful things that don't fit anywhere else
22 namespace UTILS {
23
24 using TimePoint = std::chrono::time_point<std::chrono::steady_clock>;
25
26 /// Class for performing timings of the code using std::chrono
27 class Timings {
28 private:
29 // For keeping timings
30 std::map<std::string, TimePoint> timings;
31 std::map<std::string, double> elapsed_time_sec;
32 mutable std::mutex timings_mutex;
33
34 // Get the current time
35 TimePoint getTime() { return std::chrono::steady_clock::now(); }
36
37 // Convert an interval to seconds
38 double timeInSeconds(const TimePoint & time_start, const TimePoint & time_end) {
39 return std::chrono::duration_cast<std::chrono::duration<double>>(time_end - time_start).count();
40 }
41
42 public:
43 Timings() = default;
44
45 // Copies get the timings recorded so far (and their own lock)
46 Timings(const Timings & rhs) {
47 std::lock_guard<std::mutex> guard(rhs.timings_mutex);
48 timings = rhs.timings;
49 elapsed_time_sec = rhs.elapsed_time_sec;
50 }
51 Timings & operator=(const Timings & rhs) {
52 if (this != &rhs) {
53 std::scoped_lock guard(timings_mutex, rhs.timings_mutex);
54 timings = rhs.timings;
55 elapsed_time_sec = rhs.elapsed_time_sec;
56 }
57 return *this;
58 }
59
60 /// Output all the recorded timings
61 void PrintAllTimings() const {
62#ifdef USE_MPI
63 int ThisTask = 0;
64 int initialized = 0;
65 MPI_Initialized(&initialized);
66 if (initialized)
67 MPI_Comm_rank(FML::communicator, &ThisTask);
68 if (ThisTask > 0)
69 return;
70#endif
71 std::lock_guard<std::mutex> guard(timings_mutex);
72 std::cout << "\n";
73 std::cout << "============================================\n";
74 std::cout << "All the recorded timings for task 0:\n";
75 std::cout << "============================================\n";
76 for (const auto & t : elapsed_time_sec) {
77 std::cout << "Total elapsed time for [" << std::setw(35) << t.first << "]: " << std::setw(10)
78 << t.second << " sec\n";
79 }
80 std::cout << "============================================\n";
81 std::cout << "\n";
82 }
83
84 /// Start timing
85 /// @param[in] name The label to give to the timing
86 ///
87 void StartTiming(const char * name) { StartTiming(std::string(name)); }
88
89 /// Start timing
90 void StartTiming(std::string name) {
91 auto start_time = std::chrono::steady_clock::now();
92 std::lock_guard<std::mutex> guard(timings_mutex);
93
94 timings[name] = start_time;
95 elapsed_time_sec.try_emplace(name, 0.0);
96 }
97
98 /// End timing (the time since the last StartTiming with this name is added to its total; a second
99 /// EndTiming without a new StartTiming adds nothing and returns 0)
100 /// @param[in] name The label to give to the timing
101 /// @param[in] print Optional: print timing to screen right away
102 ///
103 double EndTiming(const char * name, bool print = false) { return EndTiming(std::string(name), print); }
104
105 /// End timing and print to screen if print = true
106 /// @param[in] name The label to give to the timing
107 /// @param[in] print Optional: print timing to screen right away
108 ///
109 double EndTiming(std::string name, bool print = false) {
110 auto end_time = std::chrono::steady_clock::now();
111 std::lock_guard<std::mutex> guard(timings_mutex);
112
113 double time_sec = 0.0;
114 double tot_time_sec = 0.0;
115
116 // Find the start time (and remove it: the timing is no longer running)
117 auto it = timings.find(name);
118 if (it == timings.end()) {
119 if (print)
120 std::cout << "Can't print elapsed time. No running timing [" << name << "]\n";
121 return 0.0;
122 }
123 time_sec = timeInSeconds(it->second, end_time);
124 timings.erase(it);
125
126 // Add to the total elapsed time
127 auto & total = elapsed_time_sec[name];
128 total += time_sec;
129 tot_time_sec = total;
130
131 if (print) {
132 if (tot_time_sec == time_sec) {
133 std::cout << "Elapsed time for [" << name << "]: " << time_sec << " sec\n";
134 } else {
135 std::cout << "Current elapsed time for [" << name << "]: " << time_sec
136 << " sec. Total: " << tot_time_sec << " sec\n";
137 }
138 }
139
140 return time_sec;
141 }
142
143 /// Total time recorded for a label (0 if it has never been ended)
144 double GetTotalTime(const std::string & name) const {
145 std::lock_guard<std::mutex> guard(timings_mutex);
146 auto it = elapsed_time_sec.find(name);
147 return it == elapsed_time_sec.end() ? 0.0 : it->second;
148 }
149
150 /// Print to screen total time for a given label
151 /// @param[in] name The label to print the time of
152 ///
153 void PrintTotalTime(const char * name) const { PrintTotalTime(std::string(name)); }
154
155 /// Print to screen total time for a given label
156 /// @param[in] name The label to print the time of
157 ///
158 void PrintTotalTime(std::string name) const {
159 std::lock_guard<std::mutex> guard(timings_mutex);
160 auto it = elapsed_time_sec.find(name);
161 if (it != elapsed_time_sec.end()) {
162 double time_sec = it->second;
163 std::cout << "Total elapsed time for [" << name << "]: " << time_sec << " sec\n";
164 }
165 }
166 };
167 } // namespace UTILS
168} // namespace FML
169#endif
std::string name
void print()
Definition Main.cpp:11
Class for performing timings of the code using std::chrono.
Definition Timings.h:27
void StartTiming(const char *name)
Start timing.
Definition Timings.h:87
void PrintTotalTime(std::string name) const
Print to screen total time for a given label.
Definition Timings.h:158
double GetTotalTime(const std::string &name) const
Total time recorded for a label (0 if it has never been ended)
Definition Timings.h:144
void PrintAllTimings() const
Output all the recorded timings.
Definition Timings.h:61
Timings(const Timings &rhs)
Definition Timings.h:46
double EndTiming(std::string name, bool print=false)
End timing and print to screen if print = true.
Definition Timings.h:109
void PrintTotalTime(const char *name) const
Print to screen total time for a given label.
Definition Timings.h:153
Timings & operator=(const Timings &rhs)
Definition Timings.h:51
void StartTiming(std::string name)
Start timing.
Definition Timings.h:90
double EndTiming(const char *name, bool print=false)
End timing (the time since the last StartTiming with this name is added to its total; a second EndTim...
Definition Timings.h:103
std::chrono::time_point< std::chrono::steady_clock > TimePoint
Definition Timings.h:24
int ThisTask
The MPI task number.
Definition Global.cpp:39
MPI_Comm communicator
The MPI communicator used for all communication in the library (fiducial MPI_COMM_WORLD).
Definition Global.cpp:26