| 1 |
/* |
| 2 |
Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho |
| 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 |
|
| 29 |
template< unsigned int WindowDimension , unsigned int IterationDimensions , unsigned int CurrentIteration > |
| 30 |
struct _Loop |
| 31 |
{ |
| 32 |
static_assert( IterationDimensions<=WindowDimension , "[ERROR] Iteration dimensions cannot excceed window dimension" ); |
| 33 |
static_assert( CurrentIteration>0 , "[ERROR] Current iteration cannot be zero" ); |
| 34 |
static_assert( CurrentIteration<=IterationDimensions , "[ERROR] Current iteration cannot exceed iteration dimensions" ); |
| 35 |
|
| 36 |
protected: |
| 37 |
static const int CurrentDimension = CurrentIteration + WindowDimension - IterationDimensions; |
| 38 |
friend struct Loop< WindowDimension , IterationDimensions >; |
| 39 |
friend struct _Loop< WindowDimension , IterationDimensions , CurrentIteration+1 >; |
| 40 |
|
| 41 |
|
| 42 |
/////////////////////////////// |
| 43 |
// Single-threaded execution // |
| 44 |
/////////////////////////////// |
| 45 |
template< typename UpdateFunction , typename ProcessFunction , class ... Windows > |
| 46 |
static void Run( int begin , int end , UpdateFunction& updateState , ProcessFunction& function , Windows ... w ) |
| 47 |
{ |
| 48 |
for( int i=begin ; i<end ; i++ ) |
| 49 |
{ |
| 50 |
updateState( WindowDimension - CurrentDimension , i ); |
| 51 |
if constexpr( CurrentIteration==1 ) function( w[i] ... ); |
| 52 |
else _Loop< WindowDimension , IterationDimensions , CurrentIteration-1 >::Run( begin , end , updateState , function , w[i] ... ); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
template< typename UpdateFunction , typename ProcessFunction , class ... Windows > |
| 57 |
static void Run( const int* begin , const int* end , UpdateFunction& updateState , ProcessFunction& function , Windows ... w ) |
| 58 |
{ |
| 59 |
for( int i=begin[0] ; i<end[0] ; i++ ) |
| 60 |
{ |
| 61 |
updateState( WindowDimension - CurrentDimension , i ); |
| 62 |
if constexpr( CurrentIteration==1 ) function( w[i] ... ); |
| 63 |
else _Loop< WindowDimension , IterationDimensions , CurrentIteration-1 >::Run( begin+1 , end+1 , updateState , function , w[i] ... ); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
template< unsigned int ... Begin , unsigned int ... End , typename UpdateFunction , typename ProcessFunction , class ... Windows > |
| 68 |
static void Run( ParameterPack::UIntPack< Begin ... > begin , ParameterPack::UIntPack< End ... > end , UpdateFunction& updateState , ProcessFunction& function , Windows ... w ) |
| 69 |
{ |
| 70 |
for( int i=ParameterPack::UIntPack< Begin ... >::First ; i<ParameterPack::UIntPack< End ... >::First ; i++ ) |
| 71 |
{ |
| 72 |
updateState( WindowDimension - CurrentDimension , i ); |
| 73 |
if constexpr( CurrentIteration==1 ) function( w[i] ... ); |
| 74 |
else _Loop< WindowDimension , IterationDimensions , CurrentIteration-1 >::Run( typename ParameterPack::UIntPack< Begin ... >::Rest() , typename ParameterPack::UIntPack< End ... >::Rest() , updateState , function , w[i] ... ); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
////////////////////////////// |
| 80 |
// Multi-threaded execution // |
| 81 |
////////////////////////////// |
| 82 |
template< typename UpdateFunction , typename ProcessFunction , class ... Windows > |
| 83 |
static void RunParallel( int begin , int end , UpdateFunction& updateState , ProcessFunction& function , Windows ... w ) |
| 84 |
{ |
| 85 |
ThreadPool::ParallelFor |
| 86 |
( |
| 87 |
begin , end , |
| 88 |
[&]( unsigned int thread , size_t i ) |
| 89 |
{ |
| 90 |
updateState( thread , WindowDimension - CurrentDimension , i ); |
| 91 |
if constexpr( CurrentIteration==1 ) function( thread , w[i] ... ); |
| 92 |
else _Loop< WindowDimension , IterationDimensions , CurrentIteration-1 >::RunThread( begin , end , thread , updateState , function , w[i] ... ); |
| 93 |
} |
| 94 |
); |
| 95 |
} |
| 96 |
|
| 97 |
template< typename UpdateFunction , typename ProcessFunction , class ... Windows > |
| 98 |
static void RunParallel( const int* begin , const int* end , UpdateFunction& updateState , ProcessFunction& function , Windows ... w ) |
| 99 |
{ |
| 100 |
ThreadPool::ParallelFor |
| 101 |
( |
| 102 |
begin[0] , end[0] , |
| 103 |
[&]( unsigned int thread , size_t i ) |
| 104 |
{ |
| 105 |
updateState( thread , WindowDimension - CurrentDimension , i ); |
| 106 |
if constexpr( CurrentIteration==1 ) function( thread , w[i] ... ); |
| 107 |
else _Loop< WindowDimension , IterationDimensions , CurrentIteration-1 >::RunThread( begin+1 , end+1 , thread , updateState , function , w[i] ... ); |
| 108 |
} |
| 109 |
); |
| 110 |
} |
| 111 |
|
| 112 |
template< unsigned int ... Begin , unsigned int ... End , typename UpdateFunction , typename ProcessFunction , class ... Windows > |
| 113 |
static void RunParallel( ParameterPack::UIntPack< Begin ... > begin , ParameterPack::UIntPack< End ... > end , UpdateFunction& updateState , ProcessFunction& function , Windows ... w ) |
| 114 |
{ |
| 115 |
ThreadPool::ParallelFor |
| 116 |
( |
| 117 |
ParameterPack::UIntPack< Begin ... >::First , ParameterPack::UIntPack< End ... >::First , |
| 118 |
[&]( unsigned int thread , size_t i ) |
| 119 |
{ |
| 120 |
updateState( thread , WindowDimension - CurrentDimension , i ); |
| 121 |
if constexpr( CurrentIteration==1 ) function( thread , w[i] ... ); |
| 122 |
else _Loop< WindowDimension , IterationDimensions , CurrentIteration-1 >::RunThread( typename ParameterPack::UIntPack< Begin ... >::Rest() , typename ParameterPack::UIntPack< End ... >::Rest() , thread , updateState , function , w[i] ... ); |
| 123 |
} |
| 124 |
); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
template< typename UpdateFunction , typename ProcessFunction , class ... Windows > |
| 129 |
static void RunThread( int begin , int end , unsigned int thread , UpdateFunction& updateState , ProcessFunction& function , Windows ... w ) |
| 130 |
{ |
| 131 |
for( int i=begin ; i<end ; i++ ) |
| 132 |
{ |
| 133 |
updateState( thread , WindowDimension - CurrentDimension , i ); |
| 134 |
if constexpr( CurrentIteration==1 ) function( thread , w[i] ... ); |
| 135 |
else _Loop< WindowDimension , IterationDimensions , CurrentIteration-1 >::RunThread( begin , end , thread , updateState , function , w[i] ... ); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
template< typename UpdateFunction , typename ProcessFunction , class ... Windows > |
| 140 |
static void RunThread( const int* begin , const int* end , unsigned int thread , UpdateFunction& updateState , ProcessFunction& function , Windows ... w ) |
| 141 |
{ |
| 142 |
for( int i=begin[0] ; i<end[0] ; i++ ) |
| 143 |
{ |
| 144 |
updateState( thread , WindowDimension - CurrentDimension , i ); |
| 145 |
if constexpr( CurrentIteration==1 ) function( thread , w[i] ... ); |
| 146 |
else _Loop< WindowDimension , IterationDimensions , CurrentIteration-1 >::RunThread( begin+1 , end+1 , thread , updateState , function , w[i] ... ); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
template< unsigned int ... Begin , unsigned int ... End , typename UpdateFunction , typename ProcessFunction , class ... Windows > |
| 151 |
static void RunThread( ParameterPack::UIntPack< Begin ... > begin , ParameterPack::UIntPack< End ... > end , unsigned int thread , UpdateFunction& updateState , ProcessFunction& function , Windows ... w ) |
| 152 |
{ |
| 153 |
for( int i=ParameterPack::UIntPack< Begin ... >::First ; i<ParameterPack::UIntPack< End ... >::First ; i++ ) |
| 154 |
{ |
| 155 |
updateState( thread , WindowDimension - CurrentDimension , i ); |
| 156 |
if constexpr( CurrentIteration==1 ) function( thread , w[i] ... ); |
| 157 |
else _Loop< WindowDimension , IterationDimensions , CurrentIteration-1 >::RunThread( typename ParameterPack::UIntPack< Begin ... >::Rest() , typename ParameterPack::UIntPack< End ... >::Rest() , thread , updateState , function , w[i] ... ); |
| 158 |
} |
| 159 |
} |
| 160 |
}; |