Fixed serialization problems with tbb data structures

release/4.3a0
Richard Roberts 2013-08-16 04:10:29 +00:00
parent 6d4c101d86
commit e2733d9899
4 changed files with 92 additions and 14 deletions

View File

@ -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 <tbb/concurrent_unordered_map.h>
#undef min
#undef max
#undef ERROR
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/split_member.hpp>
#include <gtsam/base/FastVector.h>
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<typename KEY, typename VALUE>
class ConcurrentMap : public tbb::concurrent_unordered_map<KEY, VALUE> {
public:
typedef tbb::concurrent_unordered_map<KEY, VALUE> Base;
/** Default constructor */
ConcurrentMap() {}
/** Constructor from a range, passes through to base class */
template<typename INPUTITERATOR>
ConcurrentMap(INPUTITERATOR first, INPUTITERATOR last) : Base(first, last) {}
/** Copy constructor from another FastMap */
ConcurrentMap(const ConcurrentMap<KEY,VALUE>& 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<class Archive>
void save(Archive& ar, const unsigned int version) const
{
// Copy to an STL container and serialize that
FastVector<std::pair<KEY, VALUE> > map(this->size());
std::copy(this->begin(), this->end(), map.begin());
ar & BOOST_SERIALIZATION_NVP(map);
}
template<class Archive>
void load(Archive& ar, const unsigned int version)
{
// Load into STL container and then fill our map
FastVector<std::pair<KEY, VALUE> > map;
ar & BOOST_SERIALIZATION_NVP(map);
this->insert(map.begin(), map.end());
}
BOOST_SERIALIZATION_SPLIT_MEMBER()
};
}

View File

@ -20,14 +20,10 @@
#pragma once
#include <string>
#include <tbb/tbb.h>
#undef max
#undef min
#undef ERROR
#include <gtsam/base/types.h>
#include <gtsam/base/FastList.h>
#include <gtsam/base/FastMap.h>
#include <gtsam/base/ConcurrentMap.h>
#include <gtsam/base/FastVector.h>
namespace gtsam {
@ -91,7 +87,7 @@ namespace gtsam {
typedef FastList<sharedClique> Cliques;
/** Map from keys to Clique */
typedef tbb::concurrent_unordered_map<Key, sharedClique> Nodes;
typedef ConcurrentMap<Key, sharedClique> Nodes;
protected:

View File

@ -20,6 +20,7 @@
#include <gtsam/base/SymmetricBlockMatrix.h>
#include <gtsam/base/FastVector.h>
#include <gtsam/base/FastMap.h>
#include <gtsam/linear/GaussianFactor.h>
#include <boost/make_shared.hpp>
@ -55,7 +56,7 @@ namespace gtsam {
: slot(_slot), dimension(_dimension) {}
std::string toString() const;
};
class Scatter : public std::map<Key, SlotEntry, std::less<Key>, tbb::tbb_allocator<std::pair<const Key, SlotEntry> > > {
class Scatter : public FastMap<Key, SlotEntry> {
public:
Scatter() {}
Scatter(const GaussianFactorGraph& gfg, boost::optional<const Ordering&> ordering = boost::none);

View File

@ -18,17 +18,12 @@
#pragma once
#include <gtsam/base/Vector.h>
#include <gtsam/base/FastMap.h>
#include <gtsam/base/ConcurrentMap.h>
#include <gtsam/base/FastVector.h>
#include <gtsam/global_includes.h>
#include <boost/shared_ptr.hpp>
#include <tbb/concurrent_unordered_map.h>
#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<Key, Vector> Values; ///< Typedef for the collection of Vectors making up a VectorValues
typedef ConcurrentMap<Key, Vector> Values; ///< Typedef for the collection of Vectors making up a VectorValues
Values values_; ///< Collection of Vectors making up this VectorValues
public: