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 <algorithm>
23 #include <cassert>
24 #include <cmath>
25 #include <cstddef>
26 #include <iomanip>
27 #include <iostream>
28 #include <map>
29 #include <stdexcept>
30 #include <utility>
31 
32 namespace gtsam {
33 namespace internal {
34 
36 
37 // a static shared_ptr to TimingOutline with nullptr as the pointer
38 const static std::shared_ptr<TimingOutline> nullTimingOutline;
39 
40 GTSAM_EXPORT std::shared_ptr<TimingOutline> gTimingRoot(
41  new TimingOutline("Total", getTicTocID("Total")));
42 GTSAM_EXPORT std::weak_ptr<TimingOutline> gCurrentTimer(gTimingRoot);
43 
44 /* ************************************************************************* */
45 // Implementation of TimingOutline
46 /* ************************************************************************* */
47 
48 /* ************************************************************************* */
49 void TimingOutline::add(size_t usecs, size_t usecsWall) {
50  t_ += usecs;
51  tWall_ += usecsWall;
52  tIt_ += usecs;
53  double secs = (double(usecs) / 1000000.0);
54  t2_ += secs * secs;
55  ++n_;
56 }
57 
58 /* ************************************************************************* */
59 TimingOutline::TimingOutline(const std::string& label, size_t id) :
60  id_(id), t_(0), tWall_(0), t2_(0.0), tIt_(0), tMax_(0), tMin_(0), n_(0), myOrder_(
61  0), lastChildOrder_(0), label_(label) {
62 #if GTSAM_USE_BOOST_FEATURES
63 #ifdef GTSAM_USING_NEW_BOOST_TIMERS
64  timer_.stop();
65 #endif
66 #endif
67 }
68 
69 /* ************************************************************************* */
70 size_t TimingOutline::time() const {
71 #if GTSAM_USE_BOOST_FEATURES
72  size_t time = 0;
73  bool hasChildren = false;
74  for(const ChildMap::value_type& child: children_) {
75  time += child.second->time();
76  hasChildren = true;
77  }
78  if (hasChildren)
79  return time;
80  else
81  return t_;
82 #else
83  return 0;
84 #endif
85 }
86 
87 /* ************************************************************************* */
88 void TimingOutline::print(const std::string& outline) const {
89 #if GTSAM_USE_BOOST_FEATURES
90  std::string formattedLabel = label_;
91  std::replace(formattedLabel.begin(), formattedLabel.end(), '_', ' ');
92  std::cout << outline << "-" << formattedLabel << ": " << self() << " CPU ("
93  << n_ << " times, " << wall() << " wall, " << secs() << " children, min: "
94  << min() << " max: " << max() << ")\n";
95  // Order children
96  ChildOrder childOrder;
97  for(const ChildMap::value_type& child: children_) {
98  childOrder[child.second->myOrder_] = child.second;
99  }
100  // Print children
101  for(const ChildOrder::value_type& order_child: childOrder) {
102  std::string childOutline(outline);
103  childOutline += "| ";
104  order_child.second->print(childOutline);
105  }
106  std::cout.flush();
107 #endif
108 }
109 
110 /* ************************************************************************* */
111 void TimingOutline::printCsvHeader(bool addLineBreak) const {
112 #if GTSAM_USE_BOOST_FEATURES
113  // Order is (CPU time, number of times, wall time, time + children in seconds,
114  // min time, max time)
115  std::cout << label_ + " cpu time (s)" << "," << label_ + " #calls" << ","
116  << label_ + " wall time(s)" << "," << label_ + " subtree time (s)"
117  << "," << label_ + " min time (s)" << "," << label_ + "max time(s)"
118  << ",";
119  // Order children
120  ChildOrder childOrder;
121  for (const ChildMap::value_type& child : children_) {
122  childOrder[child.second->myOrder_] = child.second;
123  }
124  // Print children
125  for (const ChildOrder::value_type& order_child : childOrder) {
126  order_child.second->printCsvHeader();
127  }
128  if (addLineBreak) {
129  std::cout << std::endl;
130  }
131  std::cout.flush();
132 #endif
133 }
134 
135 /* ************************************************************************* */
136 void TimingOutline::printCsv(bool addLineBreak) const {
137 #if GTSAM_USE_BOOST_FEATURES
138  // Order is (CPU time, number of times, wall time, time + children in seconds,
139  // min time, max time)
140  std::cout << self() << "," << n_ << "," << wall() << "," << secs() << ","
141  << min() << "," << max() << ",";
142  // Order children
143  ChildOrder childOrder;
144  for (const ChildMap::value_type& child : children_) {
145  childOrder[child.second->myOrder_] = child.second;
146  }
147  // Print children
148  for (const ChildOrder::value_type& order_child : childOrder) {
149  order_child.second->printCsv(false);
150  }
151  if (addLineBreak) {
152  std::cout << std::endl;
153  }
154  std::cout.flush();
155 #endif
156 }
157 
158 void TimingOutline::print2(const std::string& outline,
159  const double parentTotal) const {
160 #if GTSAM_USE_BOOST_FEATURES
161  const int w1 = 24, w2 = 2, w3 = 6, w4 = 8, precision = 2;
162  const double selfTotal = self(), selfMean = selfTotal / double(n_);
163  const double childTotal = secs();
164 
165  // compute standard deviation
166  const double selfStd = sqrt(t2_ / double(n_) - selfMean * selfMean);
167  const std::string label = outline + label_ + ": ";
168 
169  if (n_ == 0) {
170  std::cout << label << std::fixed << std::setprecision(precision)
171  << childTotal << " seconds" << std::endl;
172  } else {
173  std::cout << std::setw(w1 + outline.length()) << label;
174  std::cout << std::setiosflags(std::ios::right) << std::setw(w2) << n_
175  << " (times), " << std::setiosflags(std::ios::right) << std::fixed
176  << std::setw(w3) << std::setprecision(precision) << selfMean
177  << " (mean), " << std::setiosflags(std::ios::right) << std::fixed
178  << std::setw(w3) << std::setprecision(precision) << selfStd << " (std),"
179  << std::setiosflags(std::ios::right) << std::fixed << std::setw(w4)
180  << std::setprecision(precision) << selfTotal << " (total),";
181 
182  if (parentTotal > 0.0)
183  std::cout << std::setiosflags(std::ios::right) << std::fixed
184  << std::setw(w3) << std::setprecision(precision)
185  << 100.0 * selfTotal / parentTotal << " (%)";
186 
187  std::cout << std::endl;
188  }
189 
190  for(const ChildMap::value_type& child: children_) {
191  std::string childOutline(outline);
192  if (n_ == 0) {
193  child.second->print2(childOutline, childTotal);
194  } else {
195  childOutline += " ";
196  child.second->print2(childOutline, selfTotal);
197  }
198  }
199 #endif
200 }
201 
202 /* ************************************************************************* */
203 const std::shared_ptr<TimingOutline>& TimingOutline::child(size_t child,
204  const std::string& label, const std::weak_ptr<TimingOutline>& thisPtr) {
205 #if GTSAM_USE_BOOST_FEATURES
206  assert(thisPtr.lock().get() == this);
207  std::shared_ptr<TimingOutline>& result = children_[child];
208  if (!result) {
209  // Create child if necessary
210  result.reset(new TimingOutline(label, child));
211  ++this->lastChildOrder_;
212  result->myOrder_ = this->lastChildOrder_;
213  result->parent_ = thisPtr;
214  }
215  return result;
216 #else
217  return nullTimingOutline;
218 #endif
219 }
220 
221 /* ************************************************************************* */
223 // Disable this entire function if we are not using boost
224 #if GTSAM_USE_BOOST_FEATURES
225 #ifdef GTSAM_USING_NEW_BOOST_TIMERS
226  assert(timer_.is_stopped());
227  timer_.start();
228 #else
229  assert(!timerActive_);
230  timer_.restart();
231  *timerActive_ = true;
232 #endif
233 
234 #ifdef GTSAM_USE_TBB
235  tbbTimer_ = tbb::tick_count::now();
236 #endif
237 #endif
238 }
239 
240 /* ************************************************************************* */
242 // Disable this entire function if we are not using boost
243 #if GTSAM_USE_BOOST_FEATURES
244 
245 #ifdef GTSAM_USING_NEW_BOOST_TIMERS
246 
247  assert(!timer_.is_stopped());
248  timer_.stop();
249  size_t cpuTime = (timer_.elapsed().user + timer_.elapsed().system) / 1000;
250 # ifndef GTSAM_USE_TBB
251  size_t wallTime = timer_.elapsed().wall / 1000;
252 # endif
253 
254 #else
255 
256  assert(timerActive_);
257  double elapsed = timer_.elapsed();
258  size_t cpuTime = size_t(elapsed * 1000000.0);
259  *timerActive_ = false;
260 # ifndef GTSAM_USE_TBB
261  size_t wallTime = cpuTime;
262 # endif
263 
264 #endif
265 
266 #ifdef GTSAM_USE_TBB
267  size_t wallTime = size_t(
268  (tbb::tick_count::now() - tbbTimer_).seconds() * 1e6);
269 #endif
270 
271  add(cpuTime, wallTime);
272 #endif
273 }
274 
275 /* ************************************************************************* */
277 #if GTSAM_USE_BOOST_FEATURES
278  if (tIt_ > tMax_)
279  tMax_ = tIt_;
280  if (tMin_ == 0 || tIt_ < tMin_)
281  tMin_ = tIt_;
282  tIt_ = 0;
283  for(ChildMap::value_type& child: children_) {
284  child.second->finishedIteration();
285  }
286 #endif
287 }
288 
289 /* ************************************************************************* */
290 size_t getTicTocID(const char *descriptionC) {
291 // disable anything which refers to TimingOutline as well, for good measure
292 #if GTSAM_USE_BOOST_FEATURES
293  const std::string description(descriptionC);
294  // Global (static) map from strings to ID numbers and current next ID number
295  static size_t nextId = 0;
297 
298  // Retrieve or add this string
299  auto it = idMap.find(description);
300  if (it == idMap.end()) {
301  it = idMap.insert({description, nextId}).first;
302  ++nextId;
303  }
304 
305  // Return ID
306  return it->second;
307 #else
308  return 0;
309 #endif
310 }
311 
312 /* ************************************************************************* */
313 void tic(size_t id, const char *labelC) {
314 // disable anything which refers to TimingOutline as well, for good measure
315 #if GTSAM_USE_BOOST_FEATURES
316  const std::string label(labelC);
317  std::shared_ptr<TimingOutline> node = //
318  gCurrentTimer.lock()->child(id, label, gCurrentTimer);
319  gCurrentTimer = node;
320  node->tic();
321 #endif
322 }
323 
324 /* ************************************************************************* */
325 void toc(size_t id, const char *labelC) {
326 // disable anything which refers to TimingOutline as well, for good measure
327 #if GTSAM_USE_BOOST_FEATURES
328  const std::string label(labelC);
329  std::shared_ptr<TimingOutline> current(gCurrentTimer.lock());
330  if (id != current->id_) {
331  gTimingRoot->print();
332  throw std::invalid_argument(
333  "gtsam timing: Mismatched tic/toc: gttoc(\"" + label +
334  "\") called when last tic was \"" + current->label_ + "\".");
335  }
336  if (!current->parent_.lock()) {
337  gTimingRoot->print();
338  throw std::invalid_argument(
339  "gtsam timing: Mismatched tic/toc: extra gttoc(\"" + label +
340  "\"), already at the root");
341  }
342  current->toc();
343  gCurrentTimer = current->parent_;
344 #endif
345 }
346 
347 } // namespace internal
348 } // namespace gtsam
timing.h
Timing utilities.
gtsam::internal::TimingOutline::time
GTSAM_EXPORT size_t time() const
time taken, including children
Definition: timing.cpp:70
gtsam::internal::toc
void toc(size_t id, const char *labelC)
Definition: timing.cpp:325
benchmark.elapsed
elapsed
Definition: benchmark.py:86
gtsam::internal::TimingOutline::finishedIteration
GTSAM_EXPORT void finishedIteration()
Definition: timing.cpp:276
gtsam::internal::TimingOutline::lastChildOrder_
size_t lastChildOrder_
Definition: timing.h:159
gtsam::FastMap< size_t, std::shared_ptr< TimingOutline > >
setup.description
description
Definition: gtsam/3rdparty/GeographicLib/python/setup.py:45
gtsam::internal::TimingOutline::printCsvHeader
GTSAM_EXPORT void printCsvHeader(bool addLineBreak=false) const
Print the CSV header. Order is (CPU time, number of times, wall time, time + children in seconds,...
Definition: timing.cpp:111
gtsam::internal::TimingOutline::print
GTSAM_EXPORT void print(const std::string &outline="") const
Definition: timing.cpp:88
gtsam::internal::tic
void tic(size_t id, const char *labelC)
Definition: timing.cpp:313
result
Values result
Definition: OdometryOptimize.cpp:8
gtsam::internal::TimingOutline::tIt_
size_t tIt_
Definition: timing.h:154
gtsam::internal::getTicTocID
size_t getTicTocID(const char *descriptionC)
Definition: timing.cpp:290
gtsam::internal::TimingOutline::tic
GTSAM_EXPORT void tic()
Definition: timing.cpp:222
gtsam::internal::TimingOutline::TimingOutline
GTSAM_EXPORT TimingOutline(const std::string &label, size_t myId)
Constructor.
Definition: timing.cpp:59
id
static const Similarity3 id
Definition: testSimilarity3.cpp:44
gtsam::internal::gCurrentTimer
GTSAM_EXTERN_EXPORT std::weak_ptr< TimingOutline > gCurrentTimer
Definition: timing.h:258
gtsam::internal::TimingOutline::tWall_
size_t tWall_
Definition: timing.h:152
gtsam::internal::TimingOutline::label_
std::string label_
Definition: timing.h:160
gtsam::internal::TimingOutline::t_
size_t t_
Definition: timing.h:151
gtsam::internal::TimingOutline::toc
GTSAM_EXPORT void toc()
Definition: timing.cpp:241
gtsam::internal::TimingOutline::print2
GTSAM_EXPORT void print2(const std::string &outline="", const double parentTotal=-1.0) const
Definition: timing.cpp:158
size_t
std::size_t size_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:509
gtsam::internal::TimingOutline::tMin_
size_t tMin_
Definition: timing.h:156
gtsam::internal::TimingOutline::printCsv
GTSAM_EXPORT void printCsv(bool addLineBreak=false) const
Print the times recursively from parent to child in CSV format. For each timing node,...
Definition: timing.cpp:136
gtsam::internal::gTimingRoot
GTSAM_EXTERN_EXPORT std::shared_ptr< TimingOutline > gTimingRoot
Definition: timing.h:257
gtsam::internal::TimingOutline::add
void add(size_t usecs, size_t usecsWall)
Definition: timing.cpp:49
right
static char right
Definition: blas_interface.hh:61
gtsam
traits
Definition: SFMdata.h:40
precision
cout precision(2)
gtsam::internal::TimingOutline::tMax_
size_t tMax_
Definition: timing.h:155
gtsam::internal::nullTimingOutline
const static std::shared_ptr< TimingOutline > nullTimingOutline
Definition: timing.cpp:38
gtsam::internal::TimingOutline::children_
ChildMap children_
subtrees
Definition: timing.h:165
min
#define min(a, b)
Definition: datatypes.h:19
internal
Definition: BandTriangularSolver.h:13
gtsam::internal::TimingOutline
Definition: timing.h:148
gtsam::internal::TimingOutline::n_
size_t n_
Definition: timing.h:157
max
#define max(a, b)
Definition: datatypes.h:20
gtsam::internal::TimingOutline::child
const GTSAM_EXPORT std::shared_ptr< TimingOutline > & child(size_t child, const std::string &label, const std::weak_ptr< TimingOutline > &thisPtr)
Definition: timing.cpp:203
ceres::sqrt
Jet< T, N > sqrt(const Jet< T, N > &f)
Definition: jet.h:418
gtsam::internal::TimingOutline::t2_
double t2_
cache the
Definition: timing.h:153
debug.h
Global debugging flags.


gtsam
Author(s):
autogenerated on Wed Mar 19 2025 03:08:33