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 ptrdiff_t DenseIndex;
61 
62  /* ************************************************************************* */
67  template<typename TEST_TYPE, typename BASIC_TYPE, typename AS_NON_CONST,
68  typename AS_CONST>
69  struct const_selector {
70  };
71 
73  template<typename BASIC_TYPE, typename AS_NON_CONST, typename AS_CONST>
74  struct const_selector<BASIC_TYPE, BASIC_TYPE, AS_NON_CONST, AS_CONST> {
75  typedef AS_NON_CONST type;
76  };
77 
79  template<typename BASIC_TYPE, typename AS_NON_CONST, typename AS_CONST>
80  struct const_selector<const BASIC_TYPE, BASIC_TYPE, AS_NON_CONST, AS_CONST> {
81  typedef AS_CONST type;
82  };
83 
84  /* ************************************************************************* */
90  template<typename T, T defaultValue>
92  T value;
93 
95  ValueWithDefault() : value(defaultValue) {}
96 
98  ValueWithDefault(const T& _value) : value(_value) {}
99 
101  T& operator*() { return value; }
102 
104  const T& operator*() const { return value; }
105 
107  operator T() const { return value; }
108  };
109 
110  /* ************************************************************************* */
113  template<typename T>
115  T element_;
116  public:
117  typedef T value_type;
118  typedef const T* const_iterator;
119  typedef T* iterator;
120  ListOfOneContainer(const T& element) : element_(element) {}
121  const T* begin() const { return &element_; }
122  const T* end() const { return &element_ + 1; }
123  T* begin() { return &element_; }
124  T* end() { return &element_ + 1; }
125  size_t size() const { return 1; }
126  };
127 
128  BOOST_CONCEPT_ASSERT((boost::RandomAccessRangeConcept<ListOfOneContainer<int> >));
129 
131  template<typename T>
132  ListOfOneContainer<T> ListOfOne(const T& element) {
133  return ListOfOneContainer<T>(element);
134  }
135 
136  /* ************************************************************************* */
137 #ifdef __clang__
138 # pragma clang diagnostic push
139 # pragma clang diagnostic ignored "-Wunused-private-field" // Clang complains that previousOpenMPThreads is unused in the #else case below
140 #endif
141 
146  {
147  int previousOpenMPThreads;
148 
149  public:
150 #if defined GTSAM_USE_TBB && defined GTSAM_USE_EIGEN_MKL_OPENMP
152  previousOpenMPThreads(omp_get_num_threads())
153  {
154  omp_set_num_threads(omp_get_num_procs() / 4);
155  }
156 
158  {
159  omp_set_num_threads(previousOpenMPThreads);
160  }
161 #else
162  TbbOpenMPMixedScope() : previousOpenMPThreads(-1) {}
163  ~TbbOpenMPMixedScope() {}
164 #endif
165  };
166 
167 #ifdef __clang__
168 # pragma clang diagnostic pop
169 #endif
170 
171 }
172 
173 /* ************************************************************************* */
176 #ifdef NDEBUG
177 #define assert_throw(CONDITION, EXCEPTION) ((void)0)
178 #else
179 #define assert_throw(CONDITION, EXCEPTION) \
180  if (!(CONDITION)) { \
181  throw (EXCEPTION); \
182  }
183 #endif
184 
185 #ifdef _MSC_VER
186 
187 // Define some common g++ functions and macros we use that MSVC does not have
188 
189 #if (_MSC_VER < 1800)
190 
191 #include <boost/math/special_functions/fpclassify.hpp>
192 namespace std {
193  template<typename T> inline int isfinite(T a) {
194  return (int)boost::math::isfinite(a); }
195  template<typename T> inline int isnan(T a) {
196  return (int)boost::math::isnan(a); }
197  template<typename T> inline int isinf(T a) {
198  return (int)boost::math::isinf(a); }
199 }
200 
201 #endif
202 
203 #include <boost/math/constants/constants.hpp>
204 #ifndef M_PI
205 #define M_PI (boost::math::constants::pi<double>())
206 #endif
207 #ifndef M_PI_2
208 #define M_PI_2 (boost::math::constants::pi<double>() / 2.0)
209 #endif
210 #ifndef M_PI_4
211 #define M_PI_4 (boost::math::constants::pi<double>() / 4.0)
212 #endif
213 
214 #endif
215 
216 #ifdef min
217 #undef min
218 #endif
219 
220 #ifdef max
221 #undef max
222 #endif
223 
224 #ifdef ERROR
225 #undef ERROR
226 #endif
A helper class that behaves as a container with one element, and works with boost::range.
Definition: types.h:114
const T & operator*() const
Operator to access the value.
Definition: types.h:104
ValueWithDefault(const T &_value)
Initialize to the given value.
Definition: types.h:98
STL namespace.
ptrdiff_t DenseIndex
The index type for Eigen objects.
Definition: types.h:60
ListOfOneContainer< T > ListOfOne(const T &element)
Factory function for ListOfOneContainer to enable ListOfOne(e) syntax.
Definition: types.h:132
Helper struct that encapsulates a value with a default, this is just used as a member object so you d...
Definition: types.h:91
ValueWithDefault()
Default constructor, initialize to default value supplied in template argument.
Definition: types.h:95
T & operator*()
Operator to access the value.
Definition: types.h:101
Helper class that uses templates to select between two types based on whether TEST_TYPE is const or n...
Definition: types.h:69
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:57
An object whose scope defines a block where TBB and OpenMP parallelism are mixed. ...
Definition: types.h:145
Global functions in a separate testing namespace.
Definition: chartTesting.h:28