24 using TimePoint = std::chrono::time_point<std::chrono::steady_clock>;
30 std::map<std::string, TimePoint> timings;
31 std::map<std::string, double> elapsed_time_sec;
32 mutable std::mutex timings_mutex;
35 TimePoint getTime() {
return std::chrono::steady_clock::now(); }
39 return std::chrono::duration_cast<std::chrono::duration<double>>(time_end - time_start).count();
47 std::lock_guard<std::mutex> guard(rhs.timings_mutex);
48 timings = rhs.timings;
49 elapsed_time_sec = rhs.elapsed_time_sec;
53 std::scoped_lock guard(timings_mutex, rhs.timings_mutex);
54 timings = rhs.timings;
55 elapsed_time_sec = rhs.elapsed_time_sec;
65 MPI_Initialized(&initialized);
71 std::lock_guard<std::mutex> guard(timings_mutex);
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";
80 std::cout <<
"============================================\n";
91 auto start_time = std::chrono::steady_clock::now();
92 std::lock_guard<std::mutex> guard(timings_mutex);
94 timings[
name] = start_time;
95 elapsed_time_sec.try_emplace(
name, 0.0);
110 auto end_time = std::chrono::steady_clock::now();
111 std::lock_guard<std::mutex> guard(timings_mutex);
113 double time_sec = 0.0;
114 double tot_time_sec = 0.0;
117 auto it = timings.find(
name);
118 if (it == timings.end()) {
120 std::cout <<
"Can't print elapsed time. No running timing [" <<
name <<
"]\n";
123 time_sec = timeInSeconds(it->second, end_time);
127 auto & total = elapsed_time_sec[
name];
129 tot_time_sec = total;
132 if (tot_time_sec == time_sec) {
133 std::cout <<
"Elapsed time for [" <<
name <<
"]: " << time_sec <<
" sec\n";
135 std::cout <<
"Current elapsed time for [" <<
name <<
"]: " << time_sec
136 <<
" sec. Total: " << tot_time_sec <<
" sec\n";
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;
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";
Class for performing timings of the code using std::chrono.
void StartTiming(const char *name)
Start timing.
void PrintTotalTime(std::string name) const
Print to screen total time for a given label.
double GetTotalTime(const std::string &name) const
Total time recorded for a label (0 if it has never been ended)
void PrintAllTimings() const
Output all the recorded timings.
Timings(const Timings &rhs)
double EndTiming(std::string name, bool print=false)
End timing and print to screen if print = true.
void PrintTotalTime(const char *name) const
Print to screen total time for a given label.
Timings & operator=(const Timings &rhs)
void StartTiming(std::string name)
Start timing.
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...
std::chrono::time_point< std::chrono::steady_clock > TimePoint
int ThisTask
The MPI task number.
MPI_Comm communicator
The MPI communicator used for all communication in the library (fiducial MPI_COMM_WORLD).