timing.cpp
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
19 #include <gtsam/base/debug.h>
20 #include <gtsam/base/timing.h>
21 
22 #include <cmath>
23 #include <cstddef>
24 #include <cassert>
25 #include <iomanip>
26 #include <iostream>
27 #include <map>
28 #include <stdexcept>
29 #include <utility>
30 
31 namespace gtsam {
32 namespace internal {
33 
34 // a static shared_ptr to TimingOutline with nullptr as the pointer
35 const static std::shared_ptr<TimingOutline> nullTimingOutline;
36 
37 GTSAM_EXPORT std::shared_ptr<TimingOutline> gTimingRoot(
38  new TimingOutline("Total", getTicTocID("Total")));
39 GTSAM_EXPORT std::weak_ptr<TimingOutline> gCurrentTimer(gTimingRoot);
40 
41 /* ************************************************************************* */
42 // Implementation of TimingOutline
43 /* ************************************************************************* */
44 
45 /* ************************************************************************* */
46 void TimingOutline::add(size_t usecs, size_t usecsWall) {
47  t_ += usecs;
48  tWall_ += usecsWall;
49  tIt_ += usecs;
50  double secs = (double(usecs) / 1000000.0);
51  t2_ += secs * secs;
52  ++n_;
53 }
54 
55 /* ************************************************************************* */
56 TimingOutline::TimingOutline(const std::string& label, size_t id) :
57  id_(id), t_(0), tWall_(0), t2_(0.0), tIt_(0), tMax_(0), tMin_(0), n_(0), myOrder_(
58  0), lastChildOrder_(0), label_(label) {
59 #ifdef GTSAM_USE_BOOST_FEATURES
60 #ifdef GTSAM_USING_NEW_BOOST_TIMERS
61  timer_.stop();
62 #endif
63 #endif
64 }
65 
66 /* ************************************************************************* */
67 size_t TimingOutline::time() const {
68 #ifdef GTSAM_USE_BOOST_FEATURES
69  size_t time = 0;
70  bool hasChildren = false;
71  for(const ChildMap::value_type& child: children_) {
72  time += child.second->time();
73  hasChildren = true;
74  }
75  if (hasChildren)
76  return time;
77  else
78  return t_;
79 #else
80  return 0;
81 #endif
82 }
83 
84 /* ************************************************************************* */
85 void TimingOutline::print(const std::string& outline) const {
86 #ifdef GTSAM_USE_BOOST_FEATURES
87  std::string formattedLabel = label_;
88  std::replace(formattedLabel.begin(), formattedLabel.end(), '_', ' ');
89  std::cout << outline << "-" << formattedLabel << ": " << self() << " CPU ("
90  << n_ << " times, " << wall() << " wall, " << secs() << " children, min: "
91  << min() << " max: " << max() << ")\n";
92  // Order children
94  ChildOrder childOrder;
95  for(const ChildMap::value_type& child: children_) {
96  childOrder[child.second->myOrder_] = child.second;
97  }
98  // Print children
99  for(const ChildOrder::value_type& order_child: childOrder) {
100  std::string childOutline(outline);
101  childOutline += "| ";
102  order_child.second->print(childOutline);
103  }
104  std::cout.flush();
105 #endif
106 }
107 
108 void TimingOutline::print2(const std::string& outline,
109  const double parentTotal) const {
110 #ifdef GTSAM_USE_BOOST_FEATURES
111  const int w1 = 24, w2 = 2, w3 = 6, w4 = 8, precision = 2;
112  const double selfTotal = self(), selfMean = selfTotal / double(n_);
113  const double childTotal = secs();
114 
115  // compute standard deviation
116  const double selfStd = sqrt(t2_ / double(n_) - selfMean * selfMean);
117  const std::string label = outline + label_ + ": ";
118 
119  if (n_ == 0) {
120  std::cout << label << std::fixed << std::setprecision(precision)
121  << childTotal << " seconds" << std::endl;
122  } else {
123  std::cout << std::setw(w1 + outline.length()) << label;
124  std::cout << std::setiosflags(std::ios::right) << std::setw(w2) << n_
125  << " (times), " << std::setiosflags(std::ios::right) << std::fixed
126  << std::setw(w3) << std::setprecision(precision) << selfMean
127  << " (mean), " << std::setiosflags(std::ios::right) << std::fixed
128  << std::setw(w3) << std::setprecision(precision) << selfStd << " (std),"
129  << std::setiosflags(std::ios::right) << std::fixed << std::setw(w4)
130  << std::setprecision(precision) << selfTotal << " (total),";
131 
132  if (parentTotal > 0.0)
133  std::cout << std::setiosflags(std::ios::right) << std::fixed
134  << std::setw(w3) << std::setprecision(precision)
135  << 100.0 * selfTotal / parentTotal << " (%)";
136 
137  std::cout << std::endl;
138  }
139 
140  for(const ChildMap::value_type& child: children_) {
141  std::string childOutline(outline);
142  if (n_ == 0) {
143  child.second->print2(childOutline, childTotal);
144  } else {
145  childOutline += " ";
146  child.second->print2(childOutline, selfTotal);
147  }
148  }
149 #endif
150 }
151 
152 /* ************************************************************************* */
153 const std::shared_ptr<TimingOutline>& TimingOutline::child(size_t child,
154  const std::string& label, const std::weak_ptr<TimingOutline>& thisPtr) {
155 #ifdef GTSAM_USE_BOOST_FEATURES
156  assert(thisPtr.lock().get() == this);
157  std::shared_ptr<TimingOutline>& result = children_[child];
158  if (!result) {
159  // Create child if necessary
160  result.reset(new TimingOutline(label, child));
161  ++this->lastChildOrder_;
162  result->myOrder_ = this->lastChildOrder_;
163  result->parent_ = thisPtr;
164  }
165  return result;
166 #else
167  return nullTimingOutline;
168 #endif
169 }
170 
171 /* ************************************************************************* */
173 // Disable this entire function if we are not using boost
174 #ifdef GTSAM_USE_BOOST_FEATURES
175 #ifdef GTSAM_USING_NEW_BOOST_TIMERS
176  assert(timer_.is_stopped());
177  timer_.start();
178 #else
179  assert(!timerActive_);
180  timer_.restart();
181  *timerActive_ = true;
182 #endif
183 
184 #ifdef GTSAM_USE_TBB
185  tbbTimer_ = tbb::tick_count::now();
186 #endif
187 #endif
188 }
189 
190 /* ************************************************************************* */
192 // Disable this entire function if we are not using boost
193 #ifdef GTSAM_USE_BOOST_FEATURES
194 
195 #ifdef GTSAM_USING_NEW_BOOST_TIMERS
196 
197  assert(!timer_.is_stopped());
198  timer_.stop();
199  size_t cpuTime = (timer_.elapsed().user + timer_.elapsed().system) / 1000;
200 # ifndef GTSAM_USE_TBB
201  size_t wallTime = timer_.elapsed().wall / 1000;
202 # endif
203 
204 #else
205 
206  assert(timerActive_);
207  double elapsed = timer_.elapsed();
208  size_t cpuTime = size_t(elapsed * 1000000.0);
209  *timerActive_ = false;
210 # ifndef GTSAM_USE_TBB
211  size_t wallTime = cpuTime;
212 # endif
213 
214 #endif
215 
216 #ifdef GTSAM_USE_TBB
217  size_t wallTime = size_t(
218  (tbb::tick_count::now() - tbbTimer_).seconds() * 1e6);
219 #endif
220 
221  add(cpuTime, wallTime);
222 #endif
223 }
224 
225 /* ************************************************************************* */
227 #ifdef GTSAM_USE_BOOST_FEATURES
228  if (tIt_ > tMax_)
229  tMax_ = tIt_;
230  if (tMin_ == 0 || tIt_ < tMin_)
231  tMin_ = tIt_;
232  tIt_ = 0;
233  for(ChildMap::value_type& child: children_) {
234  child.second->finishedIteration();
235  }
236 #endif
237 }
238 
239 /* ************************************************************************* */
240 size_t getTicTocID(const char *descriptionC) {
241 // disable anything which refers to TimingOutline as well, for good measure
242 #ifdef GTSAM_USE_BOOST_FEATURES
243  const std::string description(descriptionC);
244  // Global (static) map from strings to ID numbers and current next ID number
245  static size_t nextId = 0;
247 
248  // Retrieve or add this string
249  auto it = idMap.find(description);
250  if (it == idMap.end()) {
251  it = idMap.insert({description, nextId}).first;
252  ++nextId;
253  }
254 
255  // Return ID
256  return it->second;
257 #else
258  return 0;
259 #endif
260 }
261 
262 /* ************************************************************************* */
263 void tic(size_t id, const char *labelC) {
264 // disable anything which refers to TimingOutline as well, for good measure
265 #ifdef GTSAM_USE_BOOST_FEATURES
266  const std::string label(labelC);
267  std::shared_ptr<TimingOutline> node = //
268  gCurrentTimer.lock()->child(id, label, gCurrentTimer);
269  gCurrentTimer = node;
270  node->tic();
271 #endif
272 }
273 
274 /* ************************************************************************* */
275 void toc(size_t id, const char *labelC) {
276 // disable anything which refers to TimingOutline as well, for good measure
277 #ifdef GTSAM_USE_BOOST_FEATURES
278  const std::string label(labelC);
279  std::shared_ptr<TimingOutline> current(gCurrentTimer.lock());
280  if (id != current->id_) {
281  gTimingRoot->print();
282  throw std::invalid_argument(
283  "gtsam timing: Mismatched tic/toc: gttoc(\"" + label +
284  "\") called when last tic was \"" + current->label_ + "\".");
285  }
286  if (!current->parent_.lock()) {
287  gTimingRoot->print();
288  throw std::invalid_argument(
289  "gtsam timing: Mismatched tic/toc: extra gttoc(\"" + label +
290  "\"), already at the root");
291  }
292  current->toc();
293  gCurrentTimer = current->parent_;
294 #endif
295 }
296 
297 } // namespace internal
298 } // namespace gtsam
double max() const
max time, in seconds
Definition: timing.h:197
double t2_
cache the
Definition: timing.h:153
GTSAM_EXPORT size_t time() const
time taken, including children
Definition: timing.cpp:67
double secs() const
time taken, in seconds, including children
Definition: timing.h:185
Global debugging flags.
GTSAM_EXPORT void finishedIteration()
Definition: timing.cpp:226
static const std::shared_ptr< TimingOutline > nullTimingOutline
Definition: timing.cpp:35
GTSAM_EXPORT void tic()
Definition: timing.cpp:172
double min() const
min time, in seconds
Definition: timing.h:196
GTSAM_EXPORT void print2(const std::string &outline="", const double parentTotal=-1.0) const
Definition: timing.cpp:108
Values result
double wall() const
wall time, in seconds
Definition: timing.h:195
GTSAM_EXPORT const std::shared_ptr< TimingOutline > & child(size_t child, const std::string &label, const std::weak_ptr< TimingOutline > &thisPtr)
Definition: timing.cpp:153
GTSAM_EXTERN_EXPORT std::shared_ptr< TimingOutline > gTimingRoot
Definition: timing.h:234
GTSAM_EXPORT TimingOutline(const std::string &label, size_t myId)
Constructor.
Definition: timing.cpp:56
void add(size_t usecs, size_t usecsWall)
Definition: timing.cpp:46
static char right
double elapsed
size_t getTicTocID(const char *descriptionC)
Definition: timing.cpp:240
traits
Definition: chartTesting.h:28
GTSAM_EXTERN_EXPORT std::weak_ptr< TimingOutline > gCurrentTimer
Definition: timing.h:235
cout precision(2)
Jet< T, N > sqrt(const Jet< T, N > &f)
Definition: jet.h:418
ChildMap children_
subtrees
Definition: timing.h:165
Timing utilities.
GTSAM_EXPORT void toc()
Definition: timing.cpp:191
GTSAM_EXPORT void print(const std::string &outline="") const
Definition: timing.cpp:85


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:40:20