Optimized Belief Propagation (CPU and GPU)
DetailedTimings.h
Go to the documentation of this file.
1 /*
2 Copyright (C) 2024 Scott Grauer-Gray
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
28 #ifndef DETAILEDTIMINGS_H_
29 #define DETAILEDTIMINGS_H_
30 
31 #include <vector>
32 #include <algorithm>
33 #include <unordered_map>
34 #include <iterator>
35 #include <map>
36 #include <iostream>
37 #include <chrono>
38 #include <ranges>
39 #include "RunEval/RunData.h"
41 
49 template <typename T>
50 requires std::is_enum_v<T>
52 public:
59  explicit DetailedTimings(
60  const std::unordered_map<T, std::string_view>& timing_segment_names);
61 
65  void ResetTiming() { segment_timings_.clear(); }
66 
72  void AddToCurrentTimings(const DetailedTimings& in_detailed_timings);
73 
80  void AddTiming(
81  const T timing_segment,
82  const std::chrono::duration<double>& segment_time)
83  {
84  segment_timings_[timing_segment].push_back(segment_time);
85  }
86 
94  std::chrono::duration<double> MedianTiming(const T run_segment_index) const;
95 
101  RunData AsRunData() const;
102 
103 private:
104  std::map<T, std::vector<std::chrono::duration<double>>> segment_timings_;
105  const std::unordered_map<T, std::string_view> timing_seg_to_str_;
106 };
107 
108 //initialize each timing segment
109 template <typename T>
110 requires std::is_enum_v<T>
112  const std::unordered_map<T, std::string_view>& timing_segment_names) :
113  timing_seg_to_str_{timing_segment_names}
114 {
115  std::ranges::transform(
116  timing_seg_to_str_,
117  std::inserter(segment_timings_, segment_timings_.end()),
118  [](const auto& segment) ->
119  std::pair<T, std::vector<std::chrono::duration<double>>> {
120  return {segment.first, std::vector<std::chrono::duration<double>>()};
121  });
122 }
123 
124 //add instance of DetailedTimings to current DetailedTimings
125 template <typename T>
126 requires std::is_enum_v<T>
128  const DetailedTimings& in_detailed_timings)
129 {
130  std::ranges::for_each(in_detailed_timings.segment_timings_,
131  [this](const auto& curr_segment_timings) {
132  const auto& [segment, segment_time_durations] = curr_segment_timings;
133  if (auto iter = this->segment_timings_.find(segment);
134  iter != this->segment_timings_.cend())
135  {
136  iter->second.insert(iter->second.cend(),
137  segment_time_durations.cbegin(),
138  segment_time_durations.cend());
139  }
140  else {
141  this->segment_timings_.insert({segment, segment_time_durations});
142  }
143  });
144 }
145 
146 //get median timing for a specified segment that may have been run multiple
147 //times
148 template <typename T>
149 requires std::is_enum_v<T>
150 std::chrono::duration<double> DetailedTimings<T>::MedianTiming(
151  const T run_segment_index) const
152 {
153  if (segment_timings_.at(run_segment_index).size() > 0) {
154  std::vector<std::chrono::duration<double>> segment_timing_vect_copy(
155  segment_timings_.at(run_segment_index));
156  //get median timing across runs
157  std::ranges::nth_element(
158  segment_timing_vect_copy,
159  segment_timing_vect_copy.begin() + segment_timing_vect_copy.size() / 2);
160  return (segment_timing_vect_copy[segment_timing_vect_copy.size() / 2]);
161  }
162  else {
163  return std::chrono::duration<double>();
164  }
165 }
166 
167 //return current timing data as a RunData object for output
168 template <typename T>
169 requires std::is_enum_v<T>
171  RunData timings_run_data;
172  std::ranges::for_each(segment_timings_,
173  [this, &timings_run_data](auto current_timing) {
174  //generate header for timing data
175  const std::string header =
176  std::string(timing_seg_to_str_.at(current_timing.first)) + " " +
177  std::string(run_eval::kMedianOfTestRunsDesc);
178  //get median timing across runs
179  std::ranges::nth_element(
180  current_timing.second,
181  current_timing.second.begin() + current_timing.second.size() / 2);
182  if (current_timing.second.size() > 0) {
183  timings_run_data.AddDataWHeader(
184  header,
185  current_timing.second[current_timing.second.size() / 2].count());
186  }
187  });
188  return timings_run_data;
189 }
190 
191 #endif /* DETAILEDTIMINGS_H_ */
Declares class to store headers with data corresponding to current program run and evaluation.
Contains namespace with enums and constants for implementation run evaluation.
Class to store timings of one or more segments taken during the run(s) of an implementation or across...
std::chrono::duration< double > MedianTiming(const T run_segment_index) const
Get median timing for a specified segment that may have been run multiple times.
void ResetTiming()
Reset all timings.
DetailedTimings(const std::unordered_map< T, std::string_view > &timing_segment_names)
Construct a new DetailedTimings object where each timing segment is initialized.
void AddToCurrentTimings(const DetailedTimings &in_detailed_timings)
Add instance of DetailedTimings to current DetailedTimings.
RunData AsRunData() const
Return current timing data as a RunData object for evaluation.
void AddTiming(const T timing_segment, const std::chrono::duration< double > &segment_time)
Add timing by segment index.
Class to store headers with data corresponding to current program run and evaluation.
Definition: RunData.h:42
void AddDataWHeader(const std::string &header, const std::string &data)
Add string data with header describing added data.
Definition: RunData.cpp:49
constexpr std::string_view kMedianOfTestRunsDesc
Constant to describing timing as median across evaluation runs.