Main MRPT website > C++ reference
MRPT logo
parallelization.h
Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                       http://www.mrpt.org/                                |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 #ifndef  __MRPT_PARALLELIZATION_H
00029 #define  __MRPT_PARALLELIZATION_H
00030 
00031 #include <mrpt/config.h>
00032 
00033 // This file declares helper structs for usage with TBB
00034 //  Refer to http://threadingbuildingblocks.org/
00035 // (The following code blocks are based on OpenCV code - BSD license)
00036 
00037 #if MRPT_HAS_TBB
00038     #include <tbb/tbb_stddef.h>
00039     #if TBB_VERSION_MAJOR*100 + TBB_VERSION_MINOR >= 202
00040         #include <tbb/tbb.h>
00041         #include <tbb/task.h>
00042         #undef min
00043         #undef max
00044     #else
00045         #undef MRPT_HAS_TBB
00046         #define MRPT_HAS_TBB 0
00047     #endif
00048 #endif
00049 
00050 
00051 // Define a common interface so if we don't have TBB it falls back to a good-old for loop:
00052 namespace mrpt
00053 {
00054         namespace system
00055         {
00056 #if MRPT_HAS_TBB
00057         typedef tbb::blocked_range<int> BlockedRange;
00058 
00059         template<typename Body> static inline
00060         void parallel_for( const BlockedRange& range, const Body& body )
00061         {
00062             tbb::parallel_for(range, body);
00063         }
00064 
00065         template<typename Iterator, typename Body> static inline
00066         void parallel_do( Iterator first, Iterator last, const Body& body )
00067         {
00068             tbb::parallel_do(first, last, body);
00069         }
00070 
00071         typedef tbb::split Split;
00072 
00073         template<typename Body> static inline
00074         void parallel_reduce( const BlockedRange& range, Body& body )
00075         {
00076             tbb::parallel_reduce(range, body);
00077         }
00078 
00079         //typedef tbb::concurrent_vector<Rect> ConcurrentRectVector;
00080 #else
00081                 // Emulate TBB-like classes which fall back to an old "for"
00082         class BlockedRange
00083         {
00084         public:
00085             BlockedRange() : _begin(0), _end(0), _grainsize(0) {}
00086             BlockedRange(int b, int e, int g=1) : _begin(b), _end(e), _grainsize(g) {}
00087             int begin() const { return _begin; }
00088             int end() const { return _end; }
00089             int grainsize() const { return _grainsize; }
00090         protected:
00091             int _begin, _end, _grainsize;
00092         };
00093 
00094         template<typename Body> static inline
00095         void parallel_for( const BlockedRange& range, const Body& body )
00096         {
00097             body(range);
00098         }
00099         typedef std::vector<Rect> ConcurrentRectVector;
00100 
00101         template<typename Iterator, typename Body> static inline
00102         void parallel_do( Iterator first, Iterator last, const Body& body )
00103         {
00104             for( ; first != last; ++first )
00105                 body(*first);
00106         }
00107 
00108         class Split {};
00109 
00110         template<typename Body> static inline
00111         void parallel_reduce( const BlockedRange& range, Body& body )
00112         {
00113             body(range);
00114         }
00115 
00116 #endif // MRPT_HAS_TBB
00117 
00118     }  // end NS
00119 }  // end NS
00120 
00121 #endif



Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011