Sierra Toolkit Version of the Day
AlgorithmRunner.hpp
00001 /*------------------------------------------------------------------------*/
00002 /*                 Copyright 2010 Sandia Corporation.                     */
00003 /*  Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive   */
00004 /*  license for use of this work by or on behalf of the U.S. Government.  */
00005 /*  Export of this program may require a license from the                 */
00006 /*  United States Government.                                             */
00007 /*------------------------------------------------------------------------*/
00008 
00009 #ifndef stk_algsup_AlgorithmRunner_hpp
00010 #define stk_algsup_AlgorithmRunner_hpp
00011 
00012 #include <utility>
00013 #include <stk_mesh/base/Types.hpp>
00014 #include <stk_mesh/base/Bucket.hpp>
00015 #include <stk_mesh/base/GetBuckets.hpp>
00016 
00017 namespace stk {
00018 
00019 //----------------------------------------------------------------------
00020 
00021 class AlgorithmInterface ;
00022 class AlgorithmRunnerInterface ;
00023 
00024 //----------------------------------------------------------------------
00025 
00027 AlgorithmRunnerInterface * algorithm_runner_non_thread();
00028 
00030 AlgorithmRunnerInterface * algorithm_runner_tpi( int nthreads );
00031 
00033 AlgorithmRunnerInterface * algorithm_runner_tbb( int nthreads );
00034 
00035 //----------------------------------------------------------------------
00036 
00037 class AlgorithmRunnerInterface {
00038 public:
00039   virtual ~AlgorithmRunnerInterface() {}
00040 
00056   template< class Algorithm >
00057   void run_parts( const mesh::Selector                & selector ,
00058                   const mesh::PartVector              & union_parts ,
00059                   const std::vector< mesh::Bucket * > & buckets ,
00060                   const Algorithm                     & algorithm ) const ;
00061 
00076   template< class Algorithm >
00077   void run( const mesh::Selector                & selector ,
00078             const mesh::PartVector              & union_parts ,
00079             const std::vector< mesh::Bucket * > & buckets ,
00080             const Algorithm                     & algorithm ) const ;
00081 
00112   template< class Algorithm >
00113   void run_parts( const mesh::Selector                & selector ,
00114                   const mesh::PartVector              & union_parts ,
00115                   const std::vector< mesh::Bucket * > & buckets ,
00116                   const Algorithm                     & algorithm ,
00117                   typename Algorithm::reduce_type     * reduce_value ) const ;
00118 
00137   template< class Algorithm >
00138   void run( const mesh::Selector                & selector ,
00139             const mesh::PartVector              & union_parts ,
00140             const std::vector< mesh::Bucket * > & buckets ,
00141             const Algorithm                     & algorithm ,
00142             typename Algorithm::reduce_type     * reduce_value ) const ;
00143 
00144 private:
00145   AlgorithmRunnerInterface ( const AlgorithmRunnerInterface & );
00146   AlgorithmRunnerInterface & operator = ( const AlgorithmRunnerInterface & );
00147 
00148 protected:
00149 
00150   AlgorithmRunnerInterface() {}
00151 
00153   virtual void run_alg( const mesh::Selector          & selector ,
00154                         const mesh::PartVector & union_parts ,
00155                         const std::vector< mesh::Bucket * > & buckets ,
00156                         const AlgorithmInterface            & algorithm ,
00157                         void * reduce ) const = 0 ;
00158 };
00159 
00160 //----------------------------------------------------------------------
00161 
00164 class AlgorithmInterface {
00165 public:
00166   const size_t m_maximum_entity_count ;
00167   const size_t m_reduce_allocation_size ;
00168 
00169   virtual void init( void * out ) const = 0 ;
00170 
00171   virtual void join( void * inout , const void * in ) const = 0 ;
00172 
00173   virtual void apply( mesh::Bucket::iterator i ,
00174                       mesh::Bucket::iterator j ,
00175                       const mesh::PartVector & selected_parts ,
00176                       void * reduce_inout ) const = 0 ;
00177 
00178   virtual ~AlgorithmInterface();
00179 
00180   //void apply_one( const mesh::Selector          & selector ,
00181   //                const mesh::Bucket                  & bucket ,
00182   //                void                                * reduce ) const ;
00183 
00184   void apply_one( const mesh::Selector   & selector ,
00185                   const mesh::PartVector & union_part_vector ,
00186                   const mesh::Bucket     & bucket ,
00187                   void                   * reduce ) const ;
00188 
00189 protected:
00190 
00191   explicit AlgorithmInterface( )
00192     : m_maximum_entity_count( 0 ),
00193       m_reduce_allocation_size( 0 ) {}
00194 
00195   AlgorithmInterface( size_t count )
00196     : m_maximum_entity_count( count ),
00197       m_reduce_allocation_size( 0 ) {}
00198 
00199   AlgorithmInterface( size_t count , size_t size )
00200     : m_maximum_entity_count( count ),
00201       m_reduce_allocation_size( size ) {}
00202 
00203 private:
00204   AlgorithmInterface( const AlgorithmInterface & );
00205   AlgorithmInterface & operator = ( const AlgorithmInterface & );
00206 };
00207 
00208 //----------------------------------------------------------------------
00209 
00210 namespace {
00211 
00212 template< class Algorithm >
00213 class AlgorithmWrapper : public AlgorithmInterface {
00214 private:
00215   void init( void * ) const {}
00216   void join( void * , const void * ) const {}
00217 public:
00218   const Algorithm & m_alg ;
00219 
00220   void apply( mesh::Bucket::iterator i ,
00221               mesh::Bucket::iterator j ,
00222               const mesh::PartVector & parts, void * reduce ) const
00223   { m_alg.apply( i , j ); }
00224 
00225   explicit AlgorithmWrapper( const Algorithm & alg )
00226     : AlgorithmInterface( alg.maximum_entity_count ), m_alg( alg ) {}
00227 };
00228 
00229 template< class Algorithm >
00230 class AlgorithmWrapperParts : public AlgorithmInterface {
00231 private:
00232   void init( void * ) const {}
00233   void join( void * , const void * ) const {}
00234 public:
00235   const Algorithm & m_alg ;
00236 
00237   void apply( mesh::Bucket::iterator i ,
00238               mesh::Bucket::iterator j ,
00239               const mesh::PartVector & selected_parts , void * reduce ) const
00240   { m_alg.apply( i , j , selected_parts ); }
00241 
00242   explicit AlgorithmWrapperParts( const Algorithm & alg )
00243     : AlgorithmInterface( alg.maximum_entity_count ), m_alg( alg ) {}
00244 };
00245 
00246 template< class Algorithm >
00247 class AlgorithmWrapperReduce : public AlgorithmInterface {
00248 public:
00249   typedef typename Algorithm::reduce_type reduce_type ;
00250 
00251   const Algorithm & m_alg ;
00252 
00253   void init( void * reduce_out ) const
00254   { m_alg.init( (reduce_type *) reduce_out ); }
00255 
00256   void join( void * reduce_inout , const void * reduce_in ) const
00257   {
00258     m_alg.join( (reduce_type *) reduce_inout ,
00259                 (const reduce_type *) reduce_in );
00260   }
00261 
00262   void apply( mesh::Bucket::iterator i ,
00263               mesh::Bucket::iterator j ,
00264               const mesh::PartVector & parts, void * reduce_inout ) const
00265   {
00266     m_alg.apply( i , j , (reduce_type*) reduce_inout );
00267   }
00268 
00269   explicit AlgorithmWrapperReduce( const Algorithm & alg )
00270     : AlgorithmInterface( alg.maximum_entity_count ,
00271                           alg.reduce_count * sizeof( reduce_type ) ),
00272           m_alg( alg ) {}
00273 };
00274 
00275 template< class Algorithm >
00276 class AlgorithmWrapperPartsReduce : public AlgorithmInterface {
00277 public:
00278   typedef typename Algorithm::reduce_type reduce_type ;
00279 
00280   const Algorithm & m_alg ;
00281 
00282   void init( void * reduce_out ) const
00283   {
00284     m_alg.init( (reduce_type *) reduce_out );
00285   }
00286 
00287   void join( void * reduce_inout , const void * reduce_in ) const
00288   {
00289     m_alg.join( (reduce_type *) reduce_inout ,
00290                 (const reduce_type *) reduce_in );
00291   }
00292 
00293   void apply( mesh::Bucket::iterator i ,
00294               mesh::Bucket::iterator j ,
00295               const mesh::PartVector & selected_parts ,
00296               void * reduce_inout ) const
00297   {
00298     m_alg.apply( i , j , selected_parts , (reduce_type*) reduce_inout );
00299   }
00300 
00301   explicit AlgorithmWrapperPartsReduce( const Algorithm & alg )
00302     : AlgorithmInterface( alg.maximum_entity_count ,
00303                           alg.reduce_count * sizeof(reduce_type) ),
00304       m_alg( alg ) {}
00305 };
00306 
00307 }
00308 
00309 template< class Algorithm >
00310 inline
00311 void AlgorithmRunnerInterface::run(
00312   const mesh::Selector                & selector ,
00313   const mesh::PartVector              & union_parts ,
00314   const std::vector< mesh::Bucket * > & buckets ,
00315   const Algorithm                     & algorithm ,
00316   typename Algorithm::reduce_type     * reduce ) const
00317 {
00318   const AlgorithmWrapperReduce<Algorithm> wrap( algorithm );
00319 
00320   run_alg( selector , union_parts, buckets , wrap , reduce );
00321 }
00322 
00323 template< class Algorithm >
00324 inline
00325 void AlgorithmRunnerInterface::run_parts(
00326   const mesh::Selector                & selector ,
00327   const mesh::PartVector              & union_parts ,
00328   const std::vector< mesh::Bucket * > & buckets ,
00329   const Algorithm                     & algorithm ,
00330   typename Algorithm::reduce_type     * reduce ) const
00331 {
00332   const AlgorithmWrapperPartsReduce<Algorithm> wrap( algorithm );
00333 
00334   run_alg( selector , union_parts, buckets , wrap , reduce );
00335 }
00336 
00337 template< class Algorithm >
00338 inline
00339 void AlgorithmRunnerInterface::run(
00340   const mesh::Selector                & selector ,
00341   const mesh::PartVector              & union_parts ,
00342   const std::vector< mesh::Bucket * > & buckets ,
00343   const Algorithm                     & algorithm ) const
00344 {
00345   const AlgorithmWrapper<Algorithm> wrap( algorithm );
00346 
00347   run_alg( selector , union_parts, buckets , wrap , NULL );
00348 }
00349 
00350 template< class Algorithm >
00351 inline
00352 void AlgorithmRunnerInterface::run_parts(
00353   const mesh::Selector                & selector ,
00354   const mesh::PartVector              & union_parts ,
00355   const std::vector< mesh::Bucket * > & buckets ,
00356   const Algorithm                     & algorithm ) const
00357 {
00358   const AlgorithmWrapperParts<Algorithm> wrap( algorithm );
00359 
00360   run_alg( selector , union_parts, buckets , wrap , NULL );
00361 }
00362 
00363 //----------------------------------------------------------------------
00364 
00365 } //namespace stk
00366 
00367 #endif
00368 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends