cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
TimeKeeper.cpp
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2020-2024 Laurent Forthomme
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <cmath>
20#include <sstream>
21
22#include "CepGen/Utils/String.h"
24
25namespace cepgen {
26 namespace utils {
28 monitors_.clear();
29 tmr_.reset();
30 }
31
32 TimeKeeper& TimeKeeper::tick(const std::string& func, double time) {
33 monitors_[func].emplace_back(time > 0. ? time : tmr_.elapsed());
34 return *this;
35 }
36
37 std::string TimeKeeper::summary() const {
38 if (monitors_.empty())
39 return std::string();
40
41 //--- a bit of arithmetics
42
43 struct Monitor {
44 std::string name;
45 size_t size;
46 double total, mean, rms;
47 bool operator<(const Monitor& oth) const { return total < oth.total; }
48 };
49
50 std::vector<Monitor> mons;
51 double total_time = 0.;
52 for (const auto& mon : monitors_) {
53 const auto& tm = mon.second;
54 const double total = tm.empty() ? -1. : std::accumulate(tm.begin(), tm.end(), 0.);
55 const double mean = total / double(tm.size());
56 const double rms = std::sqrt(
57 std::fabs(std::inner_product(tm.begin(), tm.end(), tm.begin(), 0.) / double(tm.size()) - mean * mean));
58 mons.emplace_back(Monitor{mon.first, tm.size(), total, mean, rms});
59 total_time += total;
60 }
61
62 //--- sort by total clock time (desc.)
63
64 std::sort(mons.rbegin(), mons.rend());
65
66 //--- displaying the various probes
67
68 static const double s_to_ms = 1.e3;
69 std::ostringstream oss;
70 oss << utils::format("%2s | %-100s | %12s\t%10s\t%5s", "#", "Caller", "Total (ms)", "Average (ms)", "RMS (ms)");
71 for (const auto& mon : mons)
72 oss << utils::format("\n%10u | %-100s | %12.6f\t%10e\t%5.3e",
73 mon.size,
74 mon.name.c_str(),
75 mon.total * s_to_ms,
76 mon.mean * s_to_ms,
77 mon.rms * s_to_ms);
78 oss << "\nTotal time: " << total_time << ".";
79 return oss.str();
80 }
81
82 TimeKeeper::Ticker::Ticker(TimeKeeper* tk, const std::string& name) : tk_(tk), name_(name) {}
83
85 if (tk_)
86 tk_->tick(name_, tmr_.elapsed());
87 }
88 } // namespace utils
89} // namespace cepgen
Ticker(TimeKeeper *, const std::string &)
Build a named and scoped time ticker.
~Ticker()
Ticker destructor to store the timing information to the parent timekeeper.
Collection of clocks to benchmark execution blocks.
Definition TimeKeeper.h:35
TimeKeeper & tick(const std::string &func, double time=-1.)
Count the time for one monitor.
std::string summary() const
Write a summary of all monitors.
void clear()
Reset all counters and the timer.
double elapsed() const
Time elapsed since the last reset call (or class construction)
Definition Timer.h:37
void reset()
Reset the clock counter.
Definition Timer.h:42
std::string format(const std::string &fmt, Args... args)
Format a string using a printf style format descriptor.
Definition String.h:61
Common namespace for this Monte Carlo generator.