Refactored and renamed some internals

release/4.3a0
dellaert 2015-06-14 16:05:39 -07:00
parent 15966a65f2
commit 75e072396c
2 changed files with 53 additions and 44 deletions

View File

@ -35,9 +35,9 @@
namespace gtsam { namespace gtsam {
namespace internal { namespace internal {
GTSAM_EXPORT boost::shared_ptr<TimingOutline> timingRoot( GTSAM_EXPORT boost::shared_ptr<TimingOutline> gTimingRoot(
new TimingOutline("Total", getTicTocID("Total"))); new TimingOutline("Total", getTicTocID("Total")));
GTSAM_EXPORT boost::weak_ptr<TimingOutline> timingCurrent(timingRoot); GTSAM_EXPORT boost::weak_ptr<TimingOutline> gCurrentTimer(gTimingRoot);
/* ************************************************************************* */ /* ************************************************************************* */
// Implementation of TimingOutline // Implementation of TimingOutline
@ -54,8 +54,8 @@ void TimingOutline::add(size_t usecs, size_t usecsWall) {
} }
/* ************************************************************************* */ /* ************************************************************************* */
TimingOutline::TimingOutline(const std::string& label, size_t myId) : TimingOutline::TimingOutline(const std::string& label, size_t id) :
myId_(myId), t_(0), tWall_(0), t2_(0.0), tIt_(0), tMax_(0), tMin_(0), n_(0), myOrder_( id_(id), t_(0), tWall_(0), t2_(0.0), tIt_(0), tMax_(0), tMin_(0), n_(0), myOrder_(
0), lastChildOrder_(0), label_(label) { 0), lastChildOrder_(0), label_(label) {
#ifdef GTSAM_USING_NEW_BOOST_TIMERS #ifdef GTSAM_USING_NEW_BOOST_TIMERS
timer_.stop(); timer_.stop();
@ -157,7 +157,7 @@ const boost::shared_ptr<TimingOutline>& TimingOutline::child(size_t child,
} }
/* ************************************************************************* */ /* ************************************************************************* */
void TimingOutline::ticInternal() { void TimingOutline::tic() {
#ifdef GTSAM_USING_NEW_BOOST_TIMERS #ifdef GTSAM_USING_NEW_BOOST_TIMERS
assert(timer_.is_stopped()); assert(timer_.is_stopped());
timer_.start(); timer_.start();
@ -173,7 +173,7 @@ void TimingOutline::ticInternal() {
} }
/* ************************************************************************* */ /* ************************************************************************* */
void TimingOutline::tocInternal() { void TimingOutline::toc() {
#ifdef GTSAM_USING_NEW_BOOST_TIMERS #ifdef GTSAM_USING_NEW_BOOST_TIMERS
assert(!timer_.is_stopped()); assert(!timer_.is_stopped());
@ -216,7 +216,6 @@ void TimingOutline::finishedIteration() {
} }
/* ************************************************************************* */ /* ************************************************************************* */
// Generate or retrieve a unique global ID number that will be used to look up tic_/toc statements
size_t getTicTocID(const char *descriptionC) { size_t getTicTocID(const char *descriptionC) {
const std::string description(descriptionC); const std::string description(descriptionC);
// Global (static) map from strings to ID numbers and current next ID number // Global (static) map from strings to ID numbers and current next ID number
@ -236,37 +235,33 @@ size_t getTicTocID(const char *descriptionC) {
} }
/* ************************************************************************* */ /* ************************************************************************* */
void ticInternal(size_t id, const char *labelC) { void tic(size_t id, const char *labelC) {
const std::string label(labelC); const std::string label(labelC);
if (ISDEBUG("timing-verbose"))
std::cout << "gttic_(" << id << ", " << label << ")" << std::endl;
boost::shared_ptr<TimingOutline> node = // boost::shared_ptr<TimingOutline> node = //
timingCurrent.lock()->child(id, label, timingCurrent); gCurrentTimer.lock()->child(id, label, gCurrentTimer);
timingCurrent = node; gCurrentTimer = node;
node->ticInternal(); node->tic();
} }
/* ************************************************************************* */ /* ************************************************************************* */
void tocInternal(size_t id, const char *label) { void toc(size_t id, const char *label) {
if (ISDEBUG("timing-verbose")) boost::shared_ptr<TimingOutline> current(gCurrentTimer.lock());
std::cout << "gttoc(" << id << ", " << label << ")" << std::endl; if (id != current->id_) {
boost::shared_ptr<TimingOutline> current(timingCurrent.lock()); gTimingRoot->print();
if (id != current->myId_) {
timingRoot->print();
throw std::invalid_argument( throw std::invalid_argument(
(boost::format( (boost::format(
"gtsam timing: Mismatched tic/toc: gttoc(\"%s\") called when last tic was \"%s\".") "gtsam timing: Mismatched tic/toc: gttoc(\"%s\") called when last tic was \"%s\".")
% label % current->label_).str()); % label % current->label_).str());
} }
if (!current->parent_.lock()) { if (!current->parent_.lock()) {
timingRoot->print(); gTimingRoot->print();
throw std::invalid_argument( throw std::invalid_argument(
(boost::format( (boost::format(
"gtsam timing: Mismatched tic/toc: extra gttoc(\"%s\"), already at the root") "gtsam timing: Mismatched tic/toc: extra gttoc(\"%s\"), already at the root")
% label).str()); % label).str());
} }
current->tocInternal(); current->toc();
timingCurrent = current->parent_; gCurrentTimer = current->parent_;
} }
} // namespace internal } // namespace internal

View File

@ -128,16 +128,21 @@
namespace gtsam { namespace gtsam {
namespace internal { namespace internal {
// Generate/retrieve a unique global ID number that will be used to look up tic/toc statements
GTSAM_EXPORT size_t getTicTocID(const char *description); GTSAM_EXPORT size_t getTicTocID(const char *description);
GTSAM_EXPORT void ticInternal(size_t id, const char *label);
GTSAM_EXPORT void tocInternal(size_t id, const char *label); // Create new TimingOutline child for gCurrentTimer, make it gCurrentTimer, and call tic method
GTSAM_EXPORT void tic(size_t id, const char *label);
// Call toc on gCurrentTimer and then set gCurrentTimer to the parent of gCurrentTimer
GTSAM_EXPORT void toc(size_t id, const char *label);
/** /**
* Timing Entry, arranged in a tree * Timing Entry, arranged in a tree
*/ */
class GTSAM_EXPORT TimingOutline { class GTSAM_EXPORT TimingOutline {
protected: protected:
size_t myId_; size_t id_;
size_t t_; size_t t_;
size_t tWall_; size_t tWall_;
double t2_ ; ///< cache the \sum t_i^2 double t2_ ; ///< cache the \sum t_i^2
@ -179,29 +184,38 @@ namespace gtsam {
void print2(const std::string& outline = "", const double parentTotal = -1.0) const; void print2(const std::string& outline = "", const double parentTotal = -1.0) const;
const boost::shared_ptr<TimingOutline>& const boost::shared_ptr<TimingOutline>&
child(size_t child, const std::string& label, const boost::weak_ptr<TimingOutline>& thisPtr); child(size_t child, const std::string& label, const boost::weak_ptr<TimingOutline>& thisPtr);
void ticInternal(); void tic();
void tocInternal(); void toc();
void finishedIteration(); void finishedIteration();
GTSAM_EXPORT friend void tocInternal(size_t id, const char *label); GTSAM_EXPORT friend void toc(size_t id, const char *label);
}; // \TimingOutline }; // \TimingOutline
/** /**
* No documentation * Small class that calls internal::tic at construction, and internol::toc when destroyed
*/ */
class AutoTicToc { class AutoTicToc {
private: private:
size_t id_; size_t id_;
const char* label_; const char* label_;
bool isSet_; bool isSet_;
public: public:
AutoTicToc(size_t id, const char* label) : id_(id), label_(label), isSet_(true) { ticInternal(id_, label_); } AutoTicToc(size_t id, const char* label)
void stop() { tocInternal(id_, label_); isSet_ = false; } : id_(id), label_(label), isSet_(true) {
~AutoTicToc() { if(isSet_) stop(); } tic(id_, label_);
}
void stop() {
toc(id_, label_);
isSet_ = false;
}
~AutoTicToc() {
if (isSet_) stop();
}
}; };
GTSAM_EXTERN_EXPORT boost::shared_ptr<TimingOutline> timingRoot; GTSAM_EXTERN_EXPORT boost::shared_ptr<TimingOutline> gTimingRoot;
GTSAM_EXTERN_EXPORT boost::weak_ptr<TimingOutline> timingCurrent; GTSAM_EXTERN_EXPORT boost::weak_ptr<TimingOutline> gCurrentTimer;
} }
// Tic and toc functions that are always active (whether or not ENABLE_TIMING is defined) // Tic and toc functions that are always active (whether or not ENABLE_TIMING is defined)
@ -213,7 +227,7 @@ namespace gtsam {
// tic // tic
#define gttic_(label) \ #define gttic_(label) \
static const size_t label##_id_tic = ::gtsam::internal::getTicTocID(#label); \ static const size_t label##_id_tic = ::gtsam::internal::getTicTocID(#label); \
::gtsam::internal::AutoTicToc label##_obj = ::gtsam::internal::AutoTicToc(label##_id_tic, #label) ::gtsam::internal::AutoTicToc label##_obj(label##_id_tic, #label)
// toc // toc
#define gttoc_(label) \ #define gttoc_(label) \
@ -231,26 +245,26 @@ namespace gtsam {
// indicate iteration is finished // indicate iteration is finished
inline void tictoc_finishedIteration_() { inline void tictoc_finishedIteration_() {
::gtsam::internal::timingRoot->finishedIteration(); } ::gtsam::internal::gTimingRoot->finishedIteration(); }
// print // print
inline void tictoc_print_() { inline void tictoc_print_() {
::gtsam::internal::timingRoot->print(); } ::gtsam::internal::gTimingRoot->print(); }
// print mean and standard deviation // print mean and standard deviation
inline void tictoc_print2_() { inline void tictoc_print2_() {
::gtsam::internal::timingRoot->print2(); } ::gtsam::internal::gTimingRoot->print2(); }
// get a node by label and assign it to variable // get a node by label and assign it to variable
#define tictoc_getNode(variable, label) \ #define tictoc_getNode(variable, label) \
static const size_t label##_id_getnode = ::gtsam::internal::getTicTocID(#label); \ static const size_t label##_id_getnode = ::gtsam::internal::getTicTocID(#label); \
const boost::shared_ptr<const ::gtsam::internal::TimingOutline> variable = \ const boost::shared_ptr<const ::gtsam::internal::TimingOutline> variable = \
::gtsam::internal::timingCurrent.lock()->child(label##_id_getnode, #label, ::gtsam::internal::timingCurrent); ::gtsam::internal::gCurrentTimer.lock()->child(label##_id_getnode, #label, ::gtsam::internal::gCurrentTimer);
// reset // reset
inline void tictoc_reset_() { inline void tictoc_reset_() {
::gtsam::internal::timingRoot.reset(new ::gtsam::internal::TimingOutline("Total", ::gtsam::internal::getTicTocID("Total"))); ::gtsam::internal::gTimingRoot.reset(new ::gtsam::internal::TimingOutline("Total", ::gtsam::internal::getTicTocID("Total")));
::gtsam::internal::timingCurrent = ::gtsam::internal::timingRoot; } ::gtsam::internal::gCurrentTimer = ::gtsam::internal::gTimingRoot; }
#ifdef ENABLE_TIMING #ifdef ENABLE_TIMING
#define gttic(label) gttic_(label) #define gttic(label) gttic_(label)