Sierra Toolkit  Version of the Day
AlgorithmRunnerTPI.cpp
1 /*------------------------------------------------------------------------*/
2 /* Copyright 2010 Sandia Corporation. */
3 /* Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive */
4 /* license for use of this work by or on behalf of the U.S. Government. */
5 /* Export of this program may require a license from the */
6 /* United States Government. */
7 /*------------------------------------------------------------------------*/
8 
9 
10 
11 
12 #include <stk_algsup/AlgorithmRunner.hpp>
13 
14 #ifdef STK_HAVE_TPI
15 
16 #include <TPI.h>
17 
18 #include <stdexcept>
19 
20 #include <stk_mesh/base/BulkData.hpp>
21 #include <stk_mesh/base/Bucket.hpp>
22 #include <stk_mesh/base/GetBuckets.hpp>
23 
24 namespace stk_classic {
25 namespace {
26 
27 //----------------------------------------------------------------------
28 
29 struct RunTPI {
30  const mesh::Selector & selector ;
31  const mesh::PartVector & union_parts ;
32  const std::vector<mesh::Bucket*> & buckets ;
33  const AlgorithmInterface & alg ;
34 
35  RunTPI( const mesh::Selector & arg_selector ,
36  const mesh::PartVector & arg_union_parts ,
37  const std::vector<mesh::Bucket*> & arg_buckets ,
38  const AlgorithmInterface & arg_alg )
39  : selector( arg_selector ),
40  union_parts(arg_union_parts),
41  buckets( arg_buckets ),
42  alg( arg_alg )
43  {}
44 
45  ~RunTPI()
46  {}
47 };
48 
49 extern "C" {
50 
51 static void RunTPI_join( TPI_Work * work , const void * reduce )
52 {
53  const RunTPI & myself = * ((const RunTPI *) work->info );
54 
55  myself.alg.join( work->reduce , reduce );
56 }
57 
58 static void RunTPI_init( TPI_Work * work )
59 {
60  const RunTPI & myself = * ((const RunTPI *) work->info );
61 
62  myself.alg.init( work->reduce );
63 }
64 
65 static void RunTPI_apply( TPI_Work * work )
66 {
67  const RunTPI & myself = * ((const RunTPI *) work->info );
68 
69  myself.alg.apply_one( myself.selector ,
70  myself.union_parts ,
71  * myself.buckets[ work->rank ] ,
72  work->reduce
73  );
74 }
75 
76 }
77 
78 class AlgorithmRunnerTPI : public AlgorithmRunnerInterface {
79 public:
80 
81  void run_alg( const mesh::Selector & selector ,
82  const mesh::PartVector & union_parts ,
83  const std::vector< mesh::Bucket * > & buckets ,
84  const AlgorithmInterface & alg ,
85  void * reduce ) const ;
86 
87  AlgorithmRunnerTPI( int nthreads ) : result( 0 <= TPI_Init( nthreads ) ) {}
88 
89  const bool result ;
90 };
91 
92 void AlgorithmRunnerTPI::run_alg(
93  const mesh::Selector & selector ,
94  const mesh::PartVector & union_parts ,
95  const std::vector< mesh::Bucket * > & buckets ,
96  const AlgorithmInterface & alg ,
97  void * reduce ) const
98 {
99  if ( reduce && ! alg.m_reduce_allocation_size ) {
100  std::string msg("AlgorithmRunnerTPI: ERROR reduce value with zero size");
101  throw std::invalid_argument(msg);
102  }
103 
104  if ( ! buckets.empty() ) {
105 
106  RunTPI tmp( selector, union_parts, buckets , alg );
107 
108  if ( reduce ) {
109  TPI_Run_reduce( RunTPI_apply , & tmp ,
110  buckets.size() ,
111  RunTPI_join ,
112  RunTPI_init ,
113  alg.m_reduce_allocation_size ,
114  reduce );
115  }
116  else {
117  TPI_Run( RunTPI_apply , & tmp , buckets.size() , 0 );
118  }
119  }
120 }
121 
122 } // namespace
123 
124 //----------------------------------------------------------------------
125 
126 AlgorithmRunnerInterface * algorithm_runner_tpi( int nthreads )
127 {
128  static AlgorithmRunnerTPI runner( nthreads );
129 
130  return runner.result ? & runner : NULL ;
131 }
132 
133 } // namespace stk_classic
134 
135 #else
136 
137 namespace stk_classic {
138 
139 AlgorithmRunnerInterface * algorithm_runner_tpi( int nthreads )
140 {
141  return NULL ;
142 }
143 
144 } // namespace stk_classic
145 
146 #endif
147 
AlgorithmRunnerInterface * algorithm_runner_tpi(int nthreads)
Sierra Toolkit.
std::vector< Part *> PartVector
Collections of parts are frequently maintained as a vector of Part pointers.
Definition: Types.hpp:31