00001 #ifndef PARALLEL_REDUCE_H 00002 #define PARALLEL_REDUCE_H 00003 00004 #include "parallel_common.h" 00005 00006 namespace parallel_ode 00007 { 00008 00009 namespace ReduceTypes 00010 { 00012 enum ReduceType { REDUCE_NONE, 00013 REDUCE_SEQUENTIAL, 00014 REDUCE_STRIDED, 00015 REDUCE_COMPACT, 00016 DEFAULT_REDUCE_TYPE = REDUCE_STRIDED }; 00017 } 00018 typedef ReduceTypes::ReduceType ReduceType; 00019 00021 00025 class ReduceStrategy 00026 { 00027 public: 00028 00032 ReduceStrategy( ) 00033 : bodyAlignment_( ParallelOptions::DEFAULTALIGN ), 00034 bodySize_( 0 ), 00035 bodySizeWithReduction_(0), 00036 bodyStride_(0), 00037 bodyOffsetStride_(0), 00038 bClearReduceBuffers_(true) { } 00039 00043 virtual ~ReduceStrategy( ) { } 00044 00052 virtual void initialize( int bodySize, int maxBodyRepetitionCount, const IntVector& batchRepetitionCount ); 00053 00059 virtual ReduceType getType( ) const { return ReduceTypes::REDUCE_NONE; } 00060 00066 inline void setBodyAlignment( int bodyAlignment ) { bodyAlignment_ = bodyAlignment; } 00067 00073 inline void setClearReduceBuffers( bool clearBuffers ) { bClearReduceBuffers_ = clearBuffers; } 00074 00083 inline int getFinalIndex( int baseBodyIndex, int bodyOffset ) { return baseBodyIndex + (bodyOffset * bodyOffsetStride_); } 00084 00085 inline int getBodyAlignment( ) const { return bodyAlignment_; } 00086 inline int getBodySize( ) const { return bodySize_; } 00087 inline int getBodySizeWithReduction( ) const { return bodySizeWithReduction_; } 00088 inline int getBodyStride( ) const { return bodyStride_; } 00089 inline int getBodyOffsetStride( ) const { return bodyOffsetStride_; } 00090 00091 inline bool clearReduceBuffers( ) { return bClearReduceBuffers_; } 00092 00093 inline void print( ) { 00094 printf("BSize,BSizeReduce,BStride,BOffsetStride = %d, %d, %d, %d\n", bodySize_, bodySizeWithReduction_, bodyStride_, bodyOffsetStride_); 00095 } 00096 00097 protected: 00098 inline void setBodySize( int bodySize ) { bodySize_ = bodySize; } 00099 inline void setBodySizeWithReduction( int bodySize ) { bodySizeWithReduction_ = bodySize; } 00100 inline void setBodyStride( int bodyStride ) { bodyStride_ = bodyStride; } 00101 inline void setBodyOffsetStride( int bodyStride ) { bodyOffsetStride_ = bodyStride; } 00102 00103 private: 00104 00105 int bodyAlignment_; 00106 int bodySize_; 00107 int bodySizeWithReduction_; 00108 int bodyStride_; 00109 int bodyOffsetStride_; 00111 bool bClearReduceBuffers_; 00113 }; 00114 00116 00123 class SequentialReduceStrategy : public ReduceStrategy 00124 { 00125 public: 00126 00127 SequentialReduceStrategy( ) : ReduceStrategy( ) { } 00128 virtual ~SequentialReduceStrategy( ) { } 00129 00130 virtual void initialize( int bodySize, int maxBodyRepetitionCount, const IntVector& batchRepetitionCount ); 00131 virtual ReduceType getType( ) const { return ReduceTypes::REDUCE_SEQUENTIAL; } 00132 }; 00133 00135 00142 class StridedReduceStrategy : public ReduceStrategy 00143 { 00144 00145 public: 00146 00147 StridedReduceStrategy( ) : ReduceStrategy( ) { } 00148 virtual ~StridedReduceStrategy( ) { } 00149 00150 virtual void initialize( int bodySize, int maxBodyRepetitionCount, const IntVector& batchRepetitionCount ); 00151 virtual ReduceType getType( ) const { return ReduceTypes::REDUCE_STRIDED; } 00152 }; 00153 00155 00162 class CompactReduceStrategy : public ReduceStrategy 00163 { 00164 00165 public: 00166 00167 CompactReduceStrategy( ) : ReduceStrategy( ) { } 00168 virtual ~CompactReduceStrategy( ) { } 00169 00170 virtual void initialize( int bodySize, int maxBodyRepetitionCount, const IntVector& batchRepetitionCount ); 00171 virtual ReduceType getType( ) const { return ReduceTypes::REDUCE_COMPACT; } 00172 }; 00173 00175 00179 class ReduceStrategyFactory 00180 { 00181 public: 00191 static ReduceStrategy* create( ReduceType reduceType, bool bClearBuffers = true, int bodyAlignment = ParallelOptions::DEFAULTALIGN ) 00192 { 00193 ReduceStrategy* newStrategy = NULL; 00194 00195 switch( reduceType ) 00196 { 00197 case ReduceTypes::REDUCE_SEQUENTIAL: 00198 newStrategy = new SequentialReduceStrategy( ); 00199 break; 00200 case ReduceTypes::REDUCE_STRIDED: 00201 newStrategy = new StridedReduceStrategy( ); 00202 break; 00203 case ReduceTypes::REDUCE_COMPACT: 00204 newStrategy = new CompactReduceStrategy( ); 00205 break; 00206 case ReduceTypes::REDUCE_NONE: 00207 default: 00208 newStrategy = new ReduceStrategy( ); 00209 break; 00210 } 00211 00212 if( newStrategy ) { 00213 newStrategy->setBodyAlignment( bodyAlignment ); 00214 newStrategy->setClearReduceBuffers( bClearBuffers ); 00215 } 00216 00217 return newStrategy; 00218 } 00219 }; 00220 00221 } 00222 00223 #endif