Sierra Toolkit  Version of the Day
AlgorithmRunner.hpp
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 #ifndef stk_algsup_AlgorithmRunner_hpp
10 #define stk_algsup_AlgorithmRunner_hpp
11 
12 #include <utility>
13 #include <stk_mesh/base/Types.hpp>
14 #include <stk_mesh/base/Bucket.hpp>
15 #include <stk_mesh/base/GetBuckets.hpp>
16 
17 namespace stk_classic {
18 
19 //----------------------------------------------------------------------
20 
21 class AlgorithmInterface ;
22 class AlgorithmRunnerInterface ;
23 
24 //----------------------------------------------------------------------
25 
27 AlgorithmRunnerInterface * algorithm_runner_non_thread();
28 
30 AlgorithmRunnerInterface * algorithm_runner_tpi( int nthreads );
31 
33 AlgorithmRunnerInterface * algorithm_runner_tbb( int nthreads );
34 
35 //----------------------------------------------------------------------
36 
37 class AlgorithmRunnerInterface {
38 public:
39  virtual ~AlgorithmRunnerInterface() {}
40 
56  template< class Algorithm >
57  void run_parts( const mesh::Selector & selector ,
58  const mesh::PartVector & union_parts ,
59  const std::vector< mesh::Bucket * > & buckets ,
60  const Algorithm & algorithm ) const ;
61 
76  template< class Algorithm >
77  void run( const mesh::Selector & selector ,
78  const mesh::PartVector & union_parts ,
79  const std::vector< mesh::Bucket * > & buckets ,
80  const Algorithm & algorithm ) const ;
81 
112  template< class Algorithm >
113  void run_parts( const mesh::Selector & selector ,
114  const mesh::PartVector & union_parts ,
115  const std::vector< mesh::Bucket * > & buckets ,
116  const Algorithm & algorithm ,
117  typename Algorithm::reduce_type * reduce_value ) const ;
118 
137  template< class Algorithm >
138  void run( const mesh::Selector & selector ,
139  const mesh::PartVector & union_parts ,
140  const std::vector< mesh::Bucket * > & buckets ,
141  const Algorithm & algorithm ,
142  typename Algorithm::reduce_type * reduce_value ) const ;
143 
144 private:
145  AlgorithmRunnerInterface ( const AlgorithmRunnerInterface & );
146  AlgorithmRunnerInterface & operator = ( const AlgorithmRunnerInterface & );
147 
148 protected:
149 
150  AlgorithmRunnerInterface() {}
151 
153  virtual void run_alg( const mesh::Selector & selector ,
154  const mesh::PartVector & union_parts ,
155  const std::vector< mesh::Bucket * > & buckets ,
156  const AlgorithmInterface & algorithm ,
157  void * reduce ) const = 0 ;
158 };
159 
160 //----------------------------------------------------------------------
161 
165 public:
166  const size_t m_maximum_entity_count ;
167  const size_t m_reduce_allocation_size ;
168 
169  virtual void init( void * out ) const = 0 ;
170 
171  virtual void join( void * inout , const void * in ) const = 0 ;
172 
173  virtual void apply( mesh::Bucket::iterator i ,
174  mesh::Bucket::iterator j ,
175  const mesh::PartVector & selected_parts ,
176  void * reduce_inout ) const = 0 ;
177 
178  virtual ~AlgorithmInterface();
179 
180  //void apply_one( const mesh::Selector & selector ,
181  // const mesh::Bucket & bucket ,
182  // void * reduce ) const ;
183 
184  void apply_one( const mesh::Selector & selector ,
185  const mesh::PartVector & union_part_vector ,
186  const mesh::Bucket & bucket ,
187  void * reduce ) const ;
188 
189 protected:
190 
191  explicit AlgorithmInterface( )
192  : m_maximum_entity_count( 0 ),
193  m_reduce_allocation_size( 0 ) {}
194 
195  AlgorithmInterface( size_t count )
196  : m_maximum_entity_count( count ),
197  m_reduce_allocation_size( 0 ) {}
198 
199  AlgorithmInterface( size_t count , size_t size )
200  : m_maximum_entity_count( count ),
201  m_reduce_allocation_size( size ) {}
202 
203 private:
205  AlgorithmInterface & operator = ( const AlgorithmInterface & );
206 };
207 
208 //----------------------------------------------------------------------
209 
210 namespace {
211 
212 template< class Algorithm >
213 class AlgorithmWrapper : public AlgorithmInterface {
214 private:
215  void init( void * ) const {}
216  void join( void * , const void * ) const {}
217 public:
218  const Algorithm & m_alg ;
219 
220  void apply( mesh::Bucket::iterator i ,
221  mesh::Bucket::iterator j ,
222  const mesh::PartVector & parts, void * reduce ) const
223  { m_alg.apply( i , j ); }
224 
225  explicit AlgorithmWrapper( const Algorithm & alg )
226  : AlgorithmInterface( alg.maximum_entity_count ), m_alg( alg ) {}
227 };
228 
229 template< class Algorithm >
230 class AlgorithmWrapperParts : public AlgorithmInterface {
231 private:
232  void init( void * ) const {}
233  void join( void * , const void * ) const {}
234 public:
235  const Algorithm & m_alg ;
236 
237  void apply( mesh::Bucket::iterator i ,
238  mesh::Bucket::iterator j ,
239  const mesh::PartVector & selected_parts , void * reduce ) const
240  { m_alg.apply( i , j , selected_parts ); }
241 
242  explicit AlgorithmWrapperParts( const Algorithm & alg )
243  : AlgorithmInterface( alg.maximum_entity_count ), m_alg( alg ) {}
244 };
245 
246 template< class Algorithm >
247 class AlgorithmWrapperReduce : public AlgorithmInterface {
248 public:
249  typedef typename Algorithm::reduce_type reduce_type ;
250 
251  const Algorithm & m_alg ;
252 
253  void init( void * reduce_out ) const
254  { m_alg.init( (reduce_type *) reduce_out ); }
255 
256  void join( void * reduce_inout , const void * reduce_in ) const
257  {
258  m_alg.join( (reduce_type *) reduce_inout ,
259  (const reduce_type *) reduce_in );
260  }
261 
262  void apply( mesh::Bucket::iterator i ,
263  mesh::Bucket::iterator j ,
264  const mesh::PartVector & parts, void * reduce_inout ) const
265  {
266  m_alg.apply( i , j , (reduce_type*) reduce_inout );
267  }
268 
269  explicit AlgorithmWrapperReduce( const Algorithm & alg )
270  : AlgorithmInterface( alg.maximum_entity_count ,
271  alg.reduce_count * sizeof( reduce_type ) ),
272  m_alg( alg ) {}
273 };
274 
275 template< class Algorithm >
276 class AlgorithmWrapperPartsReduce : public AlgorithmInterface {
277 public:
278  typedef typename Algorithm::reduce_type reduce_type ;
279 
280  const Algorithm & m_alg ;
281 
282  void init( void * reduce_out ) const
283  {
284  m_alg.init( (reduce_type *) reduce_out );
285  }
286 
287  void join( void * reduce_inout , const void * reduce_in ) const
288  {
289  m_alg.join( (reduce_type *) reduce_inout ,
290  (const reduce_type *) reduce_in );
291  }
292 
293  void apply( mesh::Bucket::iterator i ,
294  mesh::Bucket::iterator j ,
295  const mesh::PartVector & selected_parts ,
296  void * reduce_inout ) const
297  {
298  m_alg.apply( i , j , selected_parts , (reduce_type*) reduce_inout );
299  }
300 
301  explicit AlgorithmWrapperPartsReduce( const Algorithm & alg )
302  : AlgorithmInterface( alg.maximum_entity_count ,
303  alg.reduce_count * sizeof(reduce_type) ),
304  m_alg( alg ) {}
305 };
306 
307 }
308 
309 template< class Algorithm >
310 inline
311 void AlgorithmRunnerInterface::run(
312  const mesh::Selector & selector ,
313  const mesh::PartVector & union_parts ,
314  const std::vector< mesh::Bucket * > & buckets ,
315  const Algorithm & algorithm ,
316  typename Algorithm::reduce_type * reduce ) const
317 {
318  const AlgorithmWrapperReduce<Algorithm> wrap( algorithm );
319 
320  run_alg( selector , union_parts, buckets , wrap , reduce );
321 }
322 
323 template< class Algorithm >
324 inline
325 void AlgorithmRunnerInterface::run_parts(
326  const mesh::Selector & selector ,
327  const mesh::PartVector & union_parts ,
328  const std::vector< mesh::Bucket * > & buckets ,
329  const Algorithm & algorithm ,
330  typename Algorithm::reduce_type * reduce ) const
331 {
332  const AlgorithmWrapperPartsReduce<Algorithm> wrap( algorithm );
333 
334  run_alg( selector , union_parts, buckets , wrap , reduce );
335 }
336 
337 template< class Algorithm >
338 inline
339 void AlgorithmRunnerInterface::run(
340  const mesh::Selector & selector ,
341  const mesh::PartVector & union_parts ,
342  const std::vector< mesh::Bucket * > & buckets ,
343  const Algorithm & algorithm ) const
344 {
345  const AlgorithmWrapper<Algorithm> wrap( algorithm );
346 
347  run_alg( selector , union_parts, buckets , wrap , NULL );
348 }
349 
350 template< class Algorithm >
351 inline
352 void AlgorithmRunnerInterface::run_parts(
353  const mesh::Selector & selector ,
354  const mesh::PartVector & union_parts ,
355  const std::vector< mesh::Bucket * > & buckets ,
356  const Algorithm & algorithm ) const
357 {
358  const AlgorithmWrapperParts<Algorithm> wrap( algorithm );
359 
360  run_alg( selector , union_parts, buckets , wrap , NULL );
361 }
362 
363 //----------------------------------------------------------------------
364 
365 } //namespace stk_classic
366 
367 #endif
368 
AlgorithmRunnerInterface * algorithm_runner_tbb(int nthreads)
AlgorithmRunnerInterface * algorithm_runner_tpi(int nthreads)
AlgorithmRunnerInterface * algorithm_runner_non_thread()
This is a class for selecting buckets based on a set of meshparts and set logic.
Definition: Selector.hpp:112
Sierra Toolkit.
std::vector< Part *> PartVector
Collections of parts are frequently maintained as a vector of Part pointers.
Definition: Types.hpp:31
A container for the field data of a homogeneous collection of entities.
Definition: Bucket.hpp:94