delpi  0.0.1
DElta-complete LP solver
Loading...
Searching...
No Matches
Stats.cpp
1
6#include "delpi/util/Stats.h"
7
8#include <fmt/core.h>
9
10#include <chrono>
11#include <ostream>
12#include <string>
13#include <utility>
14
15#define DELPI_STATS_FMT "{:<35} @ {:<26} = {:>15} sec"
16#define DELPI_ITERATION_STATS_FMT "{:<35} @ {:<26} = {:>15}"
17
18namespace delpi {
19
20Stats::Stats(const bool enabled, std::string class_name, std::string operations_name)
21 : timer_{}, enabled_{enabled}, class_name_{std::move(class_name)}, operations_name_{std::move(operations_name)} {}
22
23std::string Stats::ToSegmentString() const {
24 return fmt::format(DELPI_STATS_FMT, operations_name_, class_name_, timer_.seconds());
25}
26std::string Stats::ToString() const { return Stats::ToSegmentString(); }
27
28Stats &Stats::operator+=(const Stats &other) {
29 if (class_name_.empty() && !other.class_name_.empty()) class_name_ = other.class_name_;
30 if (operations_name_.empty() && !other.operations_name_.empty()) operations_name_ = other.operations_name_;
31 timer_ += other.timer_;
32 return *this;
33}
34Stats Stats::operator+(const Stats &other) const {
35 Stats result{*this};
36 result += other;
37 return result;
38}
39
41 if (enabled_) std::atomic_fetch_add_explicit(&iterations_, 1, std::memory_order_relaxed);
42}
43
45 return fmt::format(DELPI_ITERATION_STATS_FMT, iterations_name_, class_name_, iterations_.load());
46}
48
49IterationStats::IterationStats(bool enabled, std::string class_name, std::string operations_name,
50 std::string iterations_name)
51 : Stats(enabled, std::move(class_name), std::move(operations_name)),
52 iterations_{0},
53 iterations_name_{std::move(iterations_name)} {}
55 : Stats(other), iterations_{other.iterations_.load()}, iterations_name_{other.iterations_name_} {}
56
57void IterationStats::operator++() { Increase(); }
58void IterationStats::operator++(int) { Increase(); }
59IterationStats &IterationStats::operator=(const IterationStats &other) {
60 if (this != &other) {
61 Stats::operator=(other);
62 iterations_ = other.iterations_.load();
63 iterations_name_ = other.iterations_name_;
64 }
65 return *this;
66}
67IterationStats &IterationStats::operator+=(const IterationStats &other) {
68 Stats::operator+=(other);
69 if (iterations_name_.empty() && !other.iterations_name_.empty()) iterations_name_ = other.iterations_name_;
70 std::atomic_fetch_add_explicit(&iterations_, other.iterations_.load(), std::memory_order_relaxed);
71 return *this;
72}
73IterationStats IterationStats::operator+(const IterationStats &other) const {
74 IterationStats result{*this};
75 result += other;
76 return result;
77}
78
79std::ostream &operator<<(std::ostream &os, const Stats &stats) { return os << stats.ToString(); }
80std::ostream &operator<<(std::ostream &os, const IterationStats &stats) { return os << stats.ToString(); }
81
82} // namespace delpi
Dataclass collecting statistics about some operation or process.
Definition Stats.h:71
IterationStats(bool enabled, std::string class_name, std::string name_time="Time spent in Operations", std::string iterations_name="Total # of Iterations")
Construct an IterationStats object.
Definition Stats.cpp:49
std::string ToString() const override
Convert the current state of the object to a formatted string.
Definition Stats.cpp:47
std::atomic< unsigned int > iterations_
Atomic counter for the total number of iterations.
Definition Stats.h:73
std::string iterations_name_
Name to give to the iteration operation.
Definition Stats.h:74
void Increase()
Increase the iteration counter by one.
Definition Stats.cpp:40
std::string ToSegmentString() const override
Convert the current state of the object to a formatted string, only including the specific part the S...
Definition Stats.cpp:44
Dataclass collecting statistics about some operation or process.
Definition Stats.h:22
virtual std::string ToString() const
Convert the current state of the object to a formatted string.
Definition Stats.cpp:26
std::string class_name_
Name of the class running the operation the Stats object is collecting statistics for.
Definition Stats.h:28
Stats(bool enabled, std::string class_name, std::string name_time="Time spent in Operations")
Construct a Stats object.
Definition Stats.cpp:20
virtual std::string ToSegmentString() const
Convert the current state of the object to a formatted string, only including the specific part the S...
Definition Stats.cpp:23
Timer timer_
Timer object to measure the cumulative time spent in the operation.
Definition Stats.h:24
bool enabled_
Flag to enable/disable the collection of statistics.
Definition Stats.h:27
std::string operations_name_
Name of the operation the Stats object is collecting statistics for.
Definition Stats.h:29
Global namespace for the delpi library.