Main MRPT website > C++ reference
MRPT logo
parallelization.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef __MRPT_PARALLELIZATION_H
29 #define __MRPT_PARALLELIZATION_H
30 
31 #include <mrpt/config.h>
32 
33 // This file declares helper structs for usage with TBB
34 // Refer to http://threadingbuildingblocks.org/
35 // (The following code blocks are based on OpenCV code - BSD license)
36 
37 #if MRPT_HAS_TBB
38  #include <tbb/tbb_stddef.h>
39  #if TBB_VERSION_MAJOR*100 + TBB_VERSION_MINOR >= 202
40  #include <tbb/tbb.h>
41  #include <tbb/task.h>
42  #undef min
43  #undef max
44  #else
45  #undef MRPT_HAS_TBB
46  #define MRPT_HAS_TBB 0
47  #endif
48 #endif
49 
50 
51 // Define a common interface so if we don't have TBB it falls back to a good-old for loop:
52 namespace mrpt
53 {
54  namespace system
55  {
56 #if MRPT_HAS_TBB
57  typedef tbb::blocked_range<int> BlockedRange;
58 
59  template<typename Body> static inline
60  void parallel_for( const BlockedRange& range, const Body& body )
61  {
62  tbb::parallel_for(range, body);
63  }
64 
65  template<typename Iterator, typename Body> static inline
66  void parallel_do( Iterator first, Iterator last, const Body& body )
67  {
68  tbb::parallel_do(first, last, body);
69  }
70 
71  typedef tbb::split Split;
72 
73  template<typename Body> static inline
74  void parallel_reduce( const BlockedRange& range, Body& body )
75  {
76  tbb::parallel_reduce(range, body);
77  }
78 
79  //typedef tbb::concurrent_vector<Rect> ConcurrentRectVector;
80 #else
81  // Emulate TBB-like classes which fall back to an old "for"
83  {
84  public:
85  BlockedRange() : _begin(0), _end(0), _grainsize(0) {}
86  BlockedRange(int b, int e, int g=1) : _begin(b), _end(e), _grainsize(g) {}
87  int begin() const { return _begin; }
88  int end() const { return _end; }
89  int grainsize() const { return _grainsize; }
90  protected:
92  };
93 
94  template<typename Body> static inline
95  void parallel_for( const BlockedRange& range, const Body& body )
96  {
97  body(range);
98  }
99  typedef std::vector<Rect> ConcurrentRectVector;
100 
101  template<typename Iterator, typename Body> static inline
102  void parallel_do( Iterator first, Iterator last, const Body& body )
103  {
104  for( ; first != last; ++first )
105  body(*first);
106  }
107 
108  class Split {};
109 
110  template<typename Body> static inline
111  void parallel_reduce( const BlockedRange& range, Body& body )
112  {
113  body(range);
114  }
115 
116 #endif // MRPT_HAS_TBB
117 
118  } // end NS
119 } // end NS
120 
121 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013