| 1 |
/* |
| 2 |
Copyright (c) 2017, Michael Kazhdan |
| 3 |
All rights reserved. |
| 4 |
|
| 5 |
Redistribution and use in source and binary forms, with or without modification, |
| 6 |
are permitted provided that the following conditions are met: |
| 7 |
|
| 8 |
Redistributions of source code must retain the above copyright notice, this list of |
| 9 |
conditions and the following disclaimer. Redistributions in binary form must reproduce |
| 10 |
the above copyright notice, this list of conditions and the following disclaimer |
| 11 |
in the documentation and/or other materials provided with the distribution. |
| 12 |
|
| 13 |
Neither the name of the Johns Hopkins University nor the names of its contributors |
| 14 |
may be used to endorse or promote products derived from this software without specific |
| 15 |
prior written permission. |
| 16 |
|
| 17 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 18 |
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES |
| 19 |
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
| 20 |
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 |
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 22 |
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 |
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 25 |
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
| 26 |
DAMAGE. |
| 27 |
*/ |
| 28 |
#ifndef MULTI_THREADING_INCLUDED |
| 29 |
#define MULTI_THREADING_INCLUDED |
| 30 |
|
| 31 |
#include <thread> |
| 32 |
#include <vector> |
| 33 |
#include <atomic> |
| 34 |
#include <functional> |
| 35 |
#include <future> |
| 36 |
#ifdef _OPENMP |
| 37 |
#include <omp.h> |
| 38 |
#endif // _OPENMP |
| 39 |
|
| 40 |
namespace PoissonRecon |
| 41 |
{ |
| 42 |
struct ThreadPool |
| 43 |
{ |
| 44 |
enum ParallelType |
| 45 |
{ |
| 46 |
#ifdef _OPENMP |
| 47 |
OPEN_MP , |
| 48 |
#endif // _OPENMP |
| 49 |
ASYNC , |
| 50 |
NONE |
| 51 |
}; |
| 52 |
static const std::vector< std::string > ParallelNames; |
| 53 |
|
| 54 |
enum ScheduleType |
| 55 |
{ |
| 56 |
STATIC , |
| 57 |
DYNAMIC |
| 58 |
}; |
| 59 |
static const std::vector< std::string > ScheduleNames; |
| 60 |
|
| 61 |
static unsigned int NumThreads( void ){ return _NumThreads; } |
| 62 |
static void SetNumThreads( unsigned int numThreads ){ _NumThreads = numThreads; } |
| 63 |
static ParallelType ParallelizationType; |
| 64 |
static size_t ChunkSize; |
| 65 |
static ScheduleType Schedule; |
| 66 |
|
| 67 |
template< typename Function , typename ... Functions > |
| 68 |
static void ParallelSections( const Function &function , const Functions & ... functions ) |
| 69 |
{ |
| 70 |
std::vector< std::future< void > > futures; |
| 71 |
if constexpr( sizeof ... (Functions) ) |
| 72 |
{ |
| 73 |
futures.reserve( sizeof...(Functions) ); |
| 74 |
_ParallelSections( futures , functions... ); |
| 75 |
} |
| 76 |
function(); |
| 77 |
for( unsigned int i=0 ; i<futures.size() ; i++ ) futures[i].get(); |
| 78 |
} |
| 79 |
|
| 80 |
template< typename Function , typename ... Functions > |
| 81 |
static void ParallelSections( const Function &&function , const Functions && ... functions ) |
| 82 |
{ |
| 83 |
std::vector< std::future< void > > futures; |
| 84 |
if constexpr( sizeof ... (Functions) ) |
| 85 |
{ |
| 86 |
futures.reserve( sizeof...(Functions) ); |
| 87 |
_ParallelSections( futures , std::move(functions)... ); |
| 88 |
} |
| 89 |
function(); |
| 90 |
for( unsigned int i=0 ; i<futures.size() ; i++ ) futures[i].get(); |
| 91 |
} |
| 92 |
|
| 93 |
static void ParallelFor( size_t begin , size_t end , const std::function< void ( unsigned int , size_t ) > &iterationFunction , unsigned int numThreads=_NumThreads , ParallelType pType=ParallelizationType , ScheduleType schedule=Schedule , size_t chunkSize=ChunkSize ) |
| 94 |
{ |
| 95 |
if( begin>=end ) return; |
| 96 |
size_t range = end - begin; |
| 97 |
size_t chunks = ( range + chunkSize - 1 ) / chunkSize; |
| 98 |
std::atomic< size_t > index; |
| 99 |
index.store( 0 ); |
| 100 |
|
| 101 |
// If the computation is serial, go ahead and run it |
| 102 |
if( pType==ParallelType::NONE || numThreads<=1 ) |
| 103 |
{ |
| 104 |
for( size_t i=begin ; i<end ; i++ ) iterationFunction( 0 , i ); |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
// If the chunkSize is too large to satisfy all the threads, lower it |
| 109 |
if( range<=chunkSize*(numThreads-1) ) |
| 110 |
{ |
| 111 |
chunkSize = ( range + numThreads - 1 ) / numThreads; |
| 112 |
chunks = numThreads = (unsigned int)( ( range + chunkSize - 1 ) / chunkSize ); |
| 113 |
} |
| 114 |
|
| 115 |
std::function< void (unsigned int , size_t ) > _ChunkFunction = [ &iterationFunction , begin , end , chunkSize ]( unsigned int thread , size_t chunk ) |
| 116 |
{ |
| 117 |
const size_t _begin = begin + chunkSize*chunk; |
| 118 |
const size_t _end = std::min< size_t >( end , _begin+chunkSize ); |
| 119 |
for( size_t i=_begin ; i<_end ; i++ ) iterationFunction( thread , i ); |
| 120 |
}; |
| 121 |
std::function< void (unsigned int ) > _StaticThreadFunction = [ &_ChunkFunction , chunks , numThreads ]( unsigned int thread ) |
| 122 |
{ |
| 123 |
for( size_t chunk=thread ; chunk<chunks ; chunk+=numThreads ) _ChunkFunction( thread , chunk ); |
| 124 |
}; |
| 125 |
|
| 126 |
std::function< void (unsigned int ) > _DynamicThreadFunction = [ &_ChunkFunction , chunks , &index ]( unsigned int thread ) |
| 127 |
{ |
| 128 |
size_t chunk; |
| 129 |
while( ( chunk=index.fetch_add(1) )<chunks ) _ChunkFunction( thread , chunk ); |
| 130 |
}; |
| 131 |
|
| 132 |
std::function< void (unsigned int ) > ThreadFunction; |
| 133 |
if ( schedule==ScheduleType::STATIC ) ThreadFunction = _StaticThreadFunction; |
| 134 |
else if( schedule==ScheduleType::DYNAMIC ) ThreadFunction = _DynamicThreadFunction; |
| 135 |
|
| 136 |
if( false ){} |
| 137 |
#ifdef _OPENMP |
| 138 |
else if( pType==ParallelType::OPEN_MP ) |
| 139 |
{ |
| 140 |
// NOLINTBEGIN(bugprone-branch-clone): the branches differ only |
| 141 |
// in their OpenMP schedule pragma, which clang-tidy ignores. |
| 142 |
if( schedule==ScheduleType::STATIC ) |
| 143 |
#pragma omp parallel for num_threads( numThreads ) schedule( static , 1 ) |
| 144 |
for( int c=0 ; c<chunks ; c++ ) _ChunkFunction( omp_get_thread_num() , c ); |
| 145 |
else if( schedule==ScheduleType::DYNAMIC ) |
| 146 |
#pragma omp parallel for num_threads( numThreads ) schedule( dynamic , 1 ) |
| 147 |
for( int c=0 ; c<chunks ; c++ ) _ChunkFunction( omp_get_thread_num() , c ); |
| 148 |
// NOLINTEND(bugprone-branch-clone) |
| 149 |
} |
| 150 |
#endif // _OPENMP |
| 151 |
else if( pType==ParallelType::ASYNC ) |
| 152 |
{ |
| 153 |
static std::vector< std::future< void > > futures; |
| 154 |
futures.resize( numThreads-1 ); |
| 155 |
for( unsigned int t=1 ; t<numThreads ; t++ ) futures[t-1] = std::async( std::launch::async , ThreadFunction , t ); |
| 156 |
ThreadFunction( 0 ); |
| 157 |
for( unsigned int t=1 ; t<numThreads ; t++ ) futures[t-1].get(); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
private: |
| 162 |
static unsigned int _NumThreads; |
| 163 |
|
| 164 |
template< typename Function , typename ... Functions > |
| 165 |
static void _ParallelSections( std::vector< std::future< void > > &futures , const Function &function , const Functions & ... functions ) |
| 166 |
{ |
| 167 |
futures.push_back( std::async( std::launch::async , function ) ); |
| 168 |
if constexpr( sizeof...(Functions) ) _ParallelSections( futures , functions... ); |
| 169 |
} |
| 170 |
|
| 171 |
template< typename Function , typename ... Functions > |
| 172 |
static void _ParallelSections( std::vector< std::future< void > > &futures , const Function &&function , const Functions && ... functions ) |
| 173 |
{ |
| 174 |
futures.push_back( std::async( std::launch::async , function ) ); |
| 175 |
if constexpr( sizeof...(Functions) ) _ParallelSections( futures , std::move(functions)... ); |
| 176 |
} |
| 177 |
}; |
| 178 |
|
| 179 |
inline ThreadPool::ParallelType ThreadPool::ParallelizationType = ThreadPool::ParallelType::NONE; |
| 180 |
inline unsigned int ThreadPool::_NumThreads = std::thread::hardware_concurrency(); |
| 181 |
inline ThreadPool::ScheduleType ThreadPool::Schedule = ThreadPool::DYNAMIC; |
| 182 |
inline size_t ThreadPool::ChunkSize = 128; |
| 183 |
|
| 184 |
const inline std::vector< std::string > ThreadPool::ParallelNames = |
| 185 |
{ |
| 186 |
#ifdef _OPENMP |
| 187 |
"open mp" , |
| 188 |
#endif // _OPENMP |
| 189 |
"async" , |
| 190 |
"none" |
| 191 |
}; |
| 192 |
const inline std::vector< std::string > ThreadPool::ScheduleNames = { "static" , "dynamic" }; |
| 193 |
} |
| 194 |
#endif // MULTI_THREADING_INCLUDED |