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