Sierra Toolkit Version of the Day
AlgorithmRunnerTPI.cpp
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 
00010 
00011 
00012 #include <stk_algsup/AlgorithmRunner.hpp>
00013 
00014 #ifdef STK_HAVE_TPI
00015 
00016 #include <TPI.h>
00017 
00018 #include <stdexcept>
00019 
00020 #include <stk_mesh/base/BulkData.hpp>
00021 #include <stk_mesh/base/Bucket.hpp>
00022 #include <stk_mesh/base/GetBuckets.hpp>
00023 
00024 namespace stk {
00025 namespace {
00026 
00027 //----------------------------------------------------------------------
00028 
00029 struct RunTPI { 
00030   const mesh::Selector             & selector ;
00031   const mesh::PartVector           & union_parts ;
00032   const std::vector<mesh::Bucket*> & buckets ; 
00033   const AlgorithmInterface         & alg ;
00034 
00035   RunTPI( const mesh::Selector             & arg_selector ,
00036           const mesh::PartVector           & arg_union_parts ,
00037           const std::vector<mesh::Bucket*> & arg_buckets ,
00038           const AlgorithmInterface         & arg_alg )
00039     : selector( arg_selector ), 
00040       union_parts(arg_union_parts), 
00041       buckets( arg_buckets ), 
00042       alg( arg_alg ) 
00043   {}
00044  
00045   ~RunTPI() 
00046   {}
00047 };   
00048 
00049 extern "C" {
00050  
00051 static void RunTPI_join( TPI_Work * work , const void * reduce )
00052 {  
00053   const RunTPI & myself = * ((const RunTPI *) work->info );
00054 
00055   myself.alg.join( work->reduce , reduce );
00056 }
00057 
00058 static void RunTPI_init( TPI_Work * work )
00059 {
00060   const RunTPI & myself = * ((const RunTPI *) work->info );
00061 
00062   myself.alg.init( work->reduce );
00063 }
00064    
00065 static void RunTPI_apply( TPI_Work * work )
00066 {
00067   const RunTPI & myself = * ((const RunTPI *) work->info );
00068 
00069   myself.alg.apply_one( myself.selector ,
00070                         myself.union_parts ,
00071                         * myself.buckets[ work->rank ] ,
00072                         work->reduce 
00073                         );
00074 }
00075 
00076 }
00077  
00078 class AlgorithmRunnerTPI : public AlgorithmRunnerInterface {
00079 public:
00080 
00081   void run_alg( const mesh::Selector                & selector ,
00082                 const mesh::PartVector              & union_parts ,
00083                 const std::vector< mesh::Bucket * > & buckets ,
00084                 const AlgorithmInterface            & alg ,
00085                 void                                * reduce ) const ;
00086 
00087   AlgorithmRunnerTPI( int nthreads ) : result( 0 <= TPI_Init( nthreads ) ) {}
00088 
00089   const bool result ;
00090 };
00091 
00092 void AlgorithmRunnerTPI::run_alg(
00093   const mesh::Selector                & selector ,
00094   const mesh::PartVector              & union_parts ,
00095   const std::vector< mesh::Bucket * > & buckets ,
00096   const AlgorithmInterface            & alg ,
00097   void                                * reduce ) const
00098 {
00099   if ( reduce && ! alg.m_reduce_allocation_size ) {
00100     std::string msg("AlgorithmRunnerTPI: ERROR reduce value with zero size");
00101     throw std::invalid_argument(msg);
00102   }
00103  
00104   if ( ! buckets.empty() ) {
00105  
00106     RunTPI tmp( selector, union_parts, buckets , alg );
00107  
00108     if ( reduce ) {
00109       TPI_Run_reduce( RunTPI_apply , & tmp ,
00110                       buckets.size() ,
00111                       RunTPI_join ,
00112                       RunTPI_init ,
00113                       alg.m_reduce_allocation_size ,
00114                       reduce );
00115     }
00116     else {
00117       TPI_Run( RunTPI_apply , & tmp , buckets.size() , 0 );
00118     }
00119   }
00120 }
00121  
00122 } // namespace
00123 
00124 //----------------------------------------------------------------------
00125 
00126 AlgorithmRunnerInterface * algorithm_runner_tpi( int nthreads )
00127 {
00128   static AlgorithmRunnerTPI runner( nthreads );
00129 
00130   return runner.result ? & runner : NULL ;
00131 }
00132 
00133 } // namespace stk
00134 
00135 #else
00136 
00137 namespace stk {
00138 
00139 AlgorithmRunnerInterface * algorithm_runner_tpi( int nthreads )
00140 {
00141   return NULL ;
00142 }
00143 
00144 } // namespace stk
00145 
00146 #endif
00147 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends