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 <boost/algorithm/string/replace.hpp>
23 #include <boost/format.hpp>
24 
25 #include <cmath>
26 #include <cstddef>
27 #include <cassert>
28 #include <iomanip>
29 #include <iostream>
30 #include <map>
31 #include <stdexcept>
32 #include <utility>
33 
34 namespace gtsam {
35 namespace internal {
36 
37 GTSAM_EXPORT boost::shared_ptr<TimingOutline> gTimingRoot(
38  new TimingOutline("Total", getTicTocID("Total")));
39 GTSAM_EXPORT boost::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_USING_NEW_BOOST_TIMERS
60  timer_.stop();
61 #endif
62 }
63 
64 /* ************************************************************************* */
65 size_t TimingOutline::time() const {
66  size_t time = 0;
67  bool hasChildren = false;
68  for(const ChildMap::value_type& child: children_) {
69  time += child.second->time();
70  hasChildren = true;
71  }
72  if (hasChildren)
73  return time;
74  else
75  return t_;
76 }
77 
78 /* ************************************************************************* */
79 void TimingOutline::print(const std::string& outline) const {
80  std::string formattedLabel = label_;
81  boost::replace_all(formattedLabel, "_", " ");
82  std::cout << outline << "-" << formattedLabel << ": " << self() << " CPU ("
83  << n_ << " times, " << wall() << " wall, " << secs() << " children, min: "
84  << min() << " max: " << max() << ")\n";
85  // Order children
87  ChildOrder childOrder;
88  for(const ChildMap::value_type& child: children_) {
89  childOrder[child.second->myOrder_] = child.second;
90  }
91  // Print children
92  for(const ChildOrder::value_type& order_child: childOrder) {
93  std::string childOutline(outline);
94  childOutline += "| ";
95  order_child.second->print(childOutline);
96  }
97  std::cout.flush();
98 }
99 
100 void TimingOutline::print2(const std::string& outline,
101  const double parentTotal) const {
102 
103  const int w1 = 24, w2 = 2, w3 = 6, w4 = 8, precision = 2;
104  const double selfTotal = self(), selfMean = selfTotal / double(n_);
105  const double childTotal = secs();
106 
107  // compute standard deviation
108  const double selfStd = sqrt(t2_ / double(n_) - selfMean * selfMean);
109  const std::string label = outline + label_ + ": ";
110 
111  if (n_ == 0) {
112  std::cout << label << std::fixed << std::setprecision(precision)
113  << childTotal << " seconds" << std::endl;
114  } else {
115  std::cout << std::setw(w1 + outline.length()) << label;
116  std::cout << std::setiosflags(std::ios::right) << std::setw(w2) << n_
117  << " (times), " << std::setiosflags(std::ios::right) << std::fixed
118  << std::setw(w3) << std::setprecision(precision) << selfMean
119  << " (mean), " << std::setiosflags(std::ios::right) << std::fixed
120  << std::setw(w3) << std::setprecision(precision) << selfStd << " (std),"
121  << std::setiosflags(std::ios::right) << std::fixed << std::setw(w4)
122  << std::setprecision(precision) << selfTotal << " (total),";
123 
124  if (parentTotal > 0.0)
125  std::cout << std::setiosflags(std::ios::right) << std::fixed
126  << std::setw(w3) << std::setprecision(precision)
127  << 100.0 * selfTotal / parentTotal << " (%)";
128 
129  std::cout << std::endl;
130  }
131 
132  for(const ChildMap::value_type& child: children_) {
133  std::string childOutline(outline);
134  if (n_ == 0) {
135  child.second->print2(childOutline, childTotal);
136  } else {
137  childOutline += " ";
138  child.second->print2(childOutline, selfTotal);
139  }
140  }
141 }
142 
143 /* ************************************************************************* */
144 const boost::shared_ptr<TimingOutline>& TimingOutline::child(size_t child,
145  const std::string& label, const boost::weak_ptr<TimingOutline>& thisPtr) {
146  assert(thisPtr.lock().get() == this);
147  boost::shared_ptr<TimingOutline>& result = children_[child];
148  if (!result) {
149  // Create child if necessary
150  result.reset(new TimingOutline(label, child));
151  ++this->lastChildOrder_;
152  result->myOrder_ = this->lastChildOrder_;
153  result->parent_ = thisPtr;
154  }
155  return result;
156 }
157 
158 /* ************************************************************************* */
160 #ifdef GTSAM_USING_NEW_BOOST_TIMERS
161  assert(timer_.is_stopped());
162  timer_.start();
163 #else
164  assert(!timerActive_);
165  timer_.restart();
166  *timerActive_ = true;
167 #endif
168 
169 #ifdef GTSAM_USE_TBB
170  tbbTimer_ = tbb::tick_count::now();
171 #endif
172 }
173 
174 /* ************************************************************************* */
176 #ifdef GTSAM_USING_NEW_BOOST_TIMERS
177 
178  assert(!timer_.is_stopped());
179  timer_.stop();
180  size_t cpuTime = (timer_.elapsed().user + timer_.elapsed().system) / 1000;
181 # ifndef GTSAM_USE_TBB
182  size_t wallTime = timer_.elapsed().wall / 1000;
183 # endif
184 
185 #else
186 
187  assert(timerActive_);
188  double elapsed = timer_.elapsed();
189  size_t cpuTime = size_t(elapsed * 1000000.0);
190  *timerActive_ = false;
191 # ifndef GTSAM_USE_TBB
192  size_t wallTime = cpuTime;
193 # endif
194 
195 #endif
196 
197 #ifdef GTSAM_USE_TBB
198  size_t wallTime = size_t(
199  (tbb::tick_count::now() - tbbTimer_).seconds() * 1e6);
200 #endif
201 
202  add(cpuTime, wallTime);
203 }
204 
205 /* ************************************************************************* */
207  if (tIt_ > tMax_)
208  tMax_ = tIt_;
209  if (tMin_ == 0 || tIt_ < tMin_)
210  tMin_ = tIt_;
211  tIt_ = 0;
212  for(ChildMap::value_type& child: children_) {
213  child.second->finishedIteration();
214  }
215 }
216 
217 /* ************************************************************************* */
218 size_t getTicTocID(const char *descriptionC) {
219  const std::string description(descriptionC);
220  // Global (static) map from strings to ID numbers and current next ID number
221  static size_t nextId = 0;
223 
224  // Retrieve or add this string
226  description);
227  if (it == idMap.end()) {
228  it = idMap.insert(std::make_pair(description, nextId)).first;
229  ++nextId;
230  }
231 
232  // Return ID
233  return it->second;
234 }
235 
236 /* ************************************************************************* */
237 void tic(size_t id, const char *labelC) {
238  const std::string label(labelC);
239  boost::shared_ptr<TimingOutline> node = //
240  gCurrentTimer.lock()->child(id, label, gCurrentTimer);
241  gCurrentTimer = node;
242  node->tic();
243 }
244 
245 /* ************************************************************************* */
246 void toc(size_t id, const char *label) {
247  boost::shared_ptr<TimingOutline> current(gCurrentTimer.lock());
248  if (id != current->id_) {
249  gTimingRoot->print();
250  throw std::invalid_argument(
251  (boost::format(
252  "gtsam timing: Mismatched tic/toc: gttoc(\"%s\") called when last tic was \"%s\".")
253  % label % current->label_).str());
254  }
255  if (!current->parent_.lock()) {
256  gTimingRoot->print();
257  throw std::invalid_argument(
258  (boost::format(
259  "gtsam timing: Mismatched tic/toc: extra gttoc(\"%s\"), already at the root")
260  % label).str());
261  }
262  current->toc();
263  gCurrentTimer = current->parent_;
264 }
265 
266 } // namespace internal
267 } // namespace gtsam
double t2_
cache the t_i^2
Definition: timing.h:150
GTSAM_EXTERN_EXPORT boost::shared_ptr< TimingOutline > gTimingRoot
Definition: timing.h:219
GTSAM_EXPORT const boost::shared_ptr< TimingOutline > & child(size_t child, const std::string &label, const boost::weak_ptr< TimingOutline > &thisPtr)
Definition: timing.cpp:144
Global debugging flags.
GTSAM_EXPORT void finishedIteration()
Definition: timing.cpp:206
GTSAM_EXPORT void tic()
Definition: timing.cpp:159
GTSAM_EXTERN_EXPORT boost::weak_ptr< TimingOutline > gCurrentTimer
Definition: timing.h:220
double wall() const
wall time, in seconds
Definition: timing.h:181
EIGEN_DEVICE_FUNC const SqrtReturnType sqrt() const
gtsam::ValueWithDefault< bool, false > timerActive_
Definition: timing.h:168
double min() const
min time, in seconds
Definition: timing.h:182
GTSAM_EXPORT void print(const std::string &outline="") const
Definition: timing.cpp:79
GTSAM_EXPORT size_t time() const
time taken, including children
Definition: timing.cpp:65
Values result
double secs() const
time taken, in seconds, including children
Definition: timing.h:179
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:218
double max() const
max time, in seconds
Definition: timing.h:183
traits
Definition: chartTesting.h:28
cout precision(2)
GTSAM_EXPORT void print2(const std::string &outline="", const double parentTotal=-1.0) const
Definition: timing.cpp:100
ChildMap children_
subtrees
Definition: timing.h:162
Timing utilities.
GTSAM_EXPORT void toc()
Definition: timing.cpp:175


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:51:04