diff --git a/gtsam/base/ConcurrentMap.h b/gtsam/base/ConcurrentMap.h new file mode 100644 index 000000000..aa0521b08 --- /dev/null +++ b/gtsam/base/ConcurrentMap.h @@ -0,0 +1,86 @@ +/* ---------------------------------------------------------------------------- + + * GTSAM Copyright 2010, Georgia Tech Research Corporation, + * Atlanta, Georgia 30332-0415 + * All Rights Reserved + * Authors: Frank Dellaert, et al. (see THANKS for the full author list) + + * See LICENSE for the license information + + * -------------------------------------------------------------------------- */ + +/** + * @file FastMap.h + * @brief A thin wrapper around std::map that uses boost's fast_pool_allocator. + * @author Richard Roberts + * @date Oct 17, 2010 + */ + +#pragma once + +#include +#undef min +#undef max +#undef ERROR + +#include +#include + +#include + +namespace gtsam { + +/** + * FastMap is a thin wrapper around std::map that uses the boost + * fast_pool_allocator instead of the default STL allocator. This is just a + * convenience to avoid having lengthy types in the code. Through timing, + * we've seen that the fast_pool_allocator can lead to speedups of several + * percent. + * @addtogroup base + */ +template +class ConcurrentMap : public tbb::concurrent_unordered_map { + +public: + + typedef tbb::concurrent_unordered_map Base; + + /** Default constructor */ + ConcurrentMap() {} + + /** Constructor from a range, passes through to base class */ + template + ConcurrentMap(INPUTITERATOR first, INPUTITERATOR last) : Base(first, last) {} + + /** Copy constructor from another FastMap */ + ConcurrentMap(const ConcurrentMap& x) : Base(x) {} + + /** Copy constructor from the base map class */ + ConcurrentMap(const Base& x) : Base(x) {} + + /** Handy 'exists' function */ + bool exists(const KEY& e) const { return this->count(e); } + +private: + /** Serialization function */ + friend class boost::serialization::access; + template + void save(Archive& ar, const unsigned int version) const + { + // Copy to an STL container and serialize that + FastVector > map(this->size()); + std::copy(this->begin(), this->end(), map.begin()); + ar & BOOST_SERIALIZATION_NVP(map); + } + template + void load(Archive& ar, const unsigned int version) + { + // Load into STL container and then fill our map + FastVector > map; + ar & BOOST_SERIALIZATION_NVP(map); + this->insert(map.begin(), map.end()); + } + BOOST_SERIALIZATION_SPLIT_MEMBER() +}; + +} diff --git a/gtsam/inference/BayesTree.h b/gtsam/inference/BayesTree.h index 3050d93dd..e88dd8ade 100644 --- a/gtsam/inference/BayesTree.h +++ b/gtsam/inference/BayesTree.h @@ -20,14 +20,10 @@ #pragma once #include -#include -#undef max -#undef min -#undef ERROR #include #include -#include +#include #include namespace gtsam { @@ -91,7 +87,7 @@ namespace gtsam { typedef FastList Cliques; /** Map from keys to Clique */ - typedef tbb::concurrent_unordered_map Nodes; + typedef ConcurrentMap Nodes; protected: diff --git a/gtsam/linear/HessianFactor.h b/gtsam/linear/HessianFactor.h index cebf22678..0cc1b2fdc 100644 --- a/gtsam/linear/HessianFactor.h +++ b/gtsam/linear/HessianFactor.h @@ -20,6 +20,7 @@ #include #include +#include #include #include @@ -55,7 +56,7 @@ namespace gtsam { : slot(_slot), dimension(_dimension) {} std::string toString() const; }; - class Scatter : public std::map, tbb::tbb_allocator > > { + class Scatter : public FastMap { public: Scatter() {} Scatter(const GaussianFactorGraph& gfg, boost::optional ordering = boost::none); diff --git a/gtsam/linear/VectorValues.h b/gtsam/linear/VectorValues.h index 65c385d11..192d66acc 100644 --- a/gtsam/linear/VectorValues.h +++ b/gtsam/linear/VectorValues.h @@ -18,17 +18,12 @@ #pragma once #include -#include +#include #include #include #include -#include -#undef min -#undef max -#undef ERROR - namespace gtsam { /** @@ -93,7 +88,7 @@ namespace gtsam { class GTSAM_EXPORT VectorValues { protected: typedef VectorValues This; - typedef tbb::concurrent_unordered_map Values; ///< Typedef for the collection of Vectors making up a VectorValues + typedef ConcurrentMap Values; ///< Typedef for the collection of Vectors making up a VectorValues Values values_; ///< Collection of Vectors making up this VectorValues public: