gtsam  4.0.0
gtsam
types.h
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 
20 #pragma once
21 
22 #include <gtsam/dllexport.h>
23 #include <boost/concept/assert.hpp>
24 #include <boost/range/concepts.hpp>
25 #include <gtsam/config.h> // for GTSAM_USE_TBB
26 
27 #include <cstddef>
28 #include <cstdint>
29 
30 #ifdef GTSAM_USE_TBB
31 #include <tbb/task_scheduler_init.h>
32 #include <tbb/tbb_exception.h>
33 #include <tbb/scalable_allocator.h>
34 #endif
35 
36 #ifdef GTSAM_USE_EIGEN_MKL_OPENMP
37 #include <omp.h>
38 #endif
39 
40 #ifdef __clang__
41 # define CLANG_DIAGNOSTIC_PUSH_IGNORE(diag) \
42  _Pragma("clang diagnostic push") \
43  _Pragma("clang diagnostic ignored \"" diag "\"")
44 #else
45 # define CLANG_DIAGNOSTIC_PUSH_IGNORE(diag)
46 #endif
47 
48 #ifdef __clang__
49 # define CLANG_DIAGNOSTIC_POP() _Pragma("clang diagnostic pop")
50 #else
51 # define CLANG_DIAGNOSTIC_POP()
52 #endif
53 
54 namespace gtsam {
55 
57  typedef std::uint64_t Key;
58 
60  typedef std::uint64_t FactorIndex;
61 
63  typedef ptrdiff_t DenseIndex;
64 
65  /* ************************************************************************* */
70  template<typename TEST_TYPE, typename BASIC_TYPE, typename AS_NON_CONST,
71  typename AS_CONST>
72  struct const_selector {
73  };
74 
76  template<typename BASIC_TYPE, typename AS_NON_CONST, typename AS_CONST>
77  struct const_selector<BASIC_TYPE, BASIC_TYPE, AS_NON_CONST, AS_CONST> {
78  typedef AS_NON_CONST type;
79  };
80 
82  template<typename BASIC_TYPE, typename AS_NON_CONST, typename AS_CONST>
83  struct const_selector<const BASIC_TYPE, BASIC_TYPE, AS_NON_CONST, AS_CONST> {
84  typedef AS_CONST type;
85  };
86 
87  /* ************************************************************************* */
93  template<typename T, T defaultValue>
95  T value;
96 
98  ValueWithDefault() : value(defaultValue) {}
99 
101  ValueWithDefault(const T& _value) : value(_value) {}
102 
104  T& operator*() { return value; }
105 
107  const T& operator*() const { return value; }
108 
110  operator T() const { return value; }
111  };
112 
113  /* ************************************************************************* */
116  template<typename T>
118  T element_;
119  public:
120  typedef T value_type;
121  typedef const T* const_iterator;
122  typedef T* iterator;
123  ListOfOneContainer(const T& element) : element_(element) {}
124  const T* begin() const { return &element_; }
125  const T* end() const { return &element_ + 1; }
126  T* begin() { return &element_; }
127  T* end() { return &element_ + 1; }
128  size_t size() const { return 1; }
129  };
130 
131  BOOST_CONCEPT_ASSERT((boost::RandomAccessRangeConcept<ListOfOneContainer<int> >));
132 
134  template<typename T>
135  ListOfOneContainer<T> ListOfOne(const T& element) {
136  return ListOfOneContainer<T>(element);
137  }
138 
139  /* ************************************************************************* */
140 #ifdef __clang__
141 # pragma clang diagnostic push
142 # pragma clang diagnostic ignored "-Wunused-private-field" // Clang complains that previousOpenMPThreads is unused in the #else case below
143 #endif
144 
149  {
150  int previousOpenMPThreads;
151 
152  public:
153 #if defined GTSAM_USE_TBB && defined GTSAM_USE_EIGEN_MKL_OPENMP
155  previousOpenMPThreads(omp_get_num_threads())
156  {
157  omp_set_num_threads(omp_get_num_procs() / 4);
158  }
159 
161  {
162  omp_set_num_threads(previousOpenMPThreads);
163  }
164 #else
165  TbbOpenMPMixedScope() : previousOpenMPThreads(-1) {}
166  ~TbbOpenMPMixedScope() {}
167 #endif
168  };
169 
170 #ifdef __clang__
171 # pragma clang diagnostic pop
172 #endif
173 
174 }
175 
176 /* ************************************************************************* */
179 #ifdef NDEBUG
180 #define assert_throw(CONDITION, EXCEPTION) ((void)0)
181 #else
182 #define assert_throw(CONDITION, EXCEPTION) \
183  if (!(CONDITION)) { \
184  throw (EXCEPTION); \
185  }
186 #endif
187 
188 #ifdef _MSC_VER
189 
190 // Define some common g++ functions and macros we use that MSVC does not have
191 
192 #if (_MSC_VER < 1800)
193 
194 #include <boost/math/special_functions/fpclassify.hpp>
195 namespace std {
196  template<typename T> inline int isfinite(T a) {
197  return (int)boost::math::isfinite(a); }
198  template<typename T> inline int isnan(T a) {
199  return (int)boost::math::isnan(a); }
200  template<typename T> inline int isinf(T a) {
201  return (int)boost::math::isinf(a); }
202 }
203 
204 #endif
205 
206 #include <boost/math/constants/constants.hpp>
207 #ifndef M_PI
208 #define M_PI (boost::math::constants::pi<double>())
209 #endif
210 #ifndef M_PI_2
211 #define M_PI_2 (boost::math::constants::pi<double>() / 2.0)
212 #endif
213 #ifndef M_PI_4
214 #define M_PI_4 (boost::math::constants::pi<double>() / 4.0)
215 #endif
216 
217 #endif
218 
219 #ifdef min
220 #undef min
221 #endif
222 
223 #ifdef max
224 #undef max
225 #endif
226 
227 #ifdef ERROR
228 #undef ERROR
229 #endif
ptrdiff_t DenseIndex
The index type for Eigen objects.
Definition: types.h:63
ListOfOneContainer< T > ListOfOne(const T &element)
Factory function for ListOfOneContainer to enable ListOfOne(e) syntax.
Definition: types.h:135
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:57
Helper struct that encapsulates a value with a default, this is just used as a member object so you d...
Definition: types.h:94
ValueWithDefault(const T &_value)
Initialize to the given value.
Definition: types.h:101
std::uint64_t FactorIndex
Integer nonlinear factor index type.
Definition: types.h:60
Helper class that uses templates to select between two types based on whether TEST_TYPE is const or n...
Definition: types.h:72
T & operator *()
Operator to access the value.
Definition: types.h:104
An object whose scope defines a block where TBB and OpenMP parallelism are mixed.
Definition: types.h:148
A helper class that behaves as a container with one element, and works with boost::range.
Definition: types.h:117
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
ValueWithDefault()
Default constructor, initialize to default value supplied in template argument.
Definition: types.h:98