Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TBB deprecation fix (partial) #209

Merged
merged 11 commits into from
May 6, 2020
16 changes: 4 additions & 12 deletions gtsam/base/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,23 @@
#include <gtsam/base/debug.h>
#include <gtsam/config.h> // for GTSAM_USE_TBB

#ifdef GTSAM_USE_TBB
#include <tbb/mutex.h>
#endif
#include <mutex> // std::mutex, std::unique_lock

namespace gtsam {

GTSAM_EXPORT FastMap<std::string, ValueWithDefault<bool, false> > debugFlags;

#ifdef GTSAM_USE_TBB
tbb::mutex debugFlagsMutex;
#endif
std::mutex debugFlagsMutex;

/* ************************************************************************* */
bool guardedIsDebug(const std::string& s) {
#ifdef GTSAM_USE_TBB
tbb::mutex::scoped_lock lock(debugFlagsMutex);
#endif
std::unique_lock<std::mutex> lock(debugFlagsMutex);
return gtsam::debugFlags[s];
}

/* ************************************************************************* */
void guardedSetDebug(const std::string& s, const bool v) {
#ifdef GTSAM_USE_TBB
tbb::mutex::scoped_lock lock(debugFlagsMutex);
#endif
std::unique_lock<std::mutex> lock(debugFlagsMutex);
gtsam::debugFlags[s] = v;
}

Expand Down
1 change: 0 additions & 1 deletion gtsam/base/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include <cstdint>

#ifdef GTSAM_USE_TBB
#include <tbb/task_scheduler_init.h>
#include <tbb/tbb_exception.h>
#include <tbb/scalable_allocator.h>
#endif
Expand Down
4 changes: 1 addition & 3 deletions gtsam/geometry/Unit3.cpp
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,10 @@ static Point3 CalculateBestAxis(const Point3& n) {

/* ************************************************************************* */
const Matrix32& Unit3::basis(OptionalJacobian<6, 2> H) const {
#ifdef GTSAM_USE_TBB
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would consider leaving these #ifdef GTSAM_USE_TBB guards in place. They have the benefit of making single-threaded builds (without TBB) completely mutex-free.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, I see, yeah that would be useful. But I don't like keeping the TBB name since it is not a TBB function/class. Maybe renaming the guard to GTSAM_MULTITHREADED would be more accurate?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it would be more accurate, but let's keep that change until TBB is not the only way to do multi-threading. A change like this has the potential to break some setups of users in ways we can't anticipate. Better to do this as part of a version bump with release docs - I propose this (awesome!) PR should simply addresses the warnings.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I'll add the guards back, but I really hope we don't forget. Maybe we can keep an issue and PR open specifically for this until we decide to add it in.

// NOTE(hayk): At some point it seemed like this reproducably resulted in
// deadlock. However, I don't know why and I can no longer reproduce it.
// It either was a red herring or there is still a latent bug left to debug.
tbb::mutex::scoped_lock lock(B_mutex_);
#endif
std::unique_lock<std::mutex> lock(B_mutex_);

const bool cachedBasis = static_cast<bool>(B_);
const bool cachedJacobian = static_cast<bool>(H_B_);
Expand Down
8 changes: 2 additions & 6 deletions gtsam/geometry/Unit3.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@

#include <string>

#ifdef GTSAM_USE_TBB
#include <tbb/mutex.h>
#endif
#include <mutex> // std::mutex

namespace gtsam {

Expand All @@ -47,9 +45,7 @@ class Unit3 {
mutable boost::optional<Matrix32> B_; ///< Cached basis
mutable boost::optional<Matrix62> H_B_; ///< Cached basis derivative

#ifdef GTSAM_USE_TBB
mutable tbb::mutex B_mutex_; ///< Mutex to protect the cached basis.
#endif
mutable std::mutex B_mutex_; ///< Mutex to protect the cached basis.

public:

Expand Down