phdMesh  Version of the Day
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Groups
TPI.hpp
1 /*------------------------------------------------------------------------*/
2 /* TPI: Thread Pool Interface */
3 /* Copyright (2008) Sandia Corporation */
4 /* */
5 /* Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive */
6 /* license for use of this work by or on behalf of the U.S. Government. */
7 /* */
8 /* This library is free software; you can redistribute it and/or modify */
9 /* it under the terms of the GNU Lesser General Public License as */
10 /* published by the Free Software Foundation; either version 2.1 of the */
11 /* License, or (at your option) any later version. */
12 /* */
13 /* This library is distributed in the hope that it will be useful, */
14 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
15 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
16 /* Lesser General Public License for more details. */
17 /* */
18 /* You should have received a copy of the GNU Lesser General Public */
19 /* License along with this library; if not, write to the Free Software */
20 /* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 */
21 /* USA */
22 /*------------------------------------------------------------------------*/
27 #ifndef util_ThreadPool_hpp
28 #define util_ThreadPool_hpp
29 
30 #include <util/TPI.h>
31 
32 namespace TPI {
33 
34 typedef TPI_ThreadPool ThreadPool ;
35 
36 //----------------------------------------------------------------------
39 int Run( void (*func)(void*,ThreadPool), void * arg , int = 0 );
40 
43 template<class Worker>
44 int Run( Worker & worker , void (Worker::*method)(ThreadPool) , int = 0 );
45 
46 //----------------------------------------------------------------------
47 
48 inline
49 int Set_lock_size( int n ) { return TPI_Set_lock_size( n ); }
50 
51 inline
52 int Lock( ThreadPool pool , int n ) { return TPI_Lock( pool , n ); }
53 
54 inline
55 int Trylock( ThreadPool pool , int n ) { return TPI_Trylock( pool , n ); }
56 
57 inline
58 int Unlock( ThreadPool pool , int n ) { return TPI_Unlock( pool , n ); }
59 
66 class LockGuard {
67 private:
68  LockGuard();
69  LockGuard( const LockGuard & );
70  LockGuard & operator = ( const LockGuard & );
71  const ThreadPool m_pool ;
72  const int m_value ;
73  const int m_result ;
74 public:
75  operator int() const { return m_result ; }
76 
77  explicit LockGuard( ThreadPool pool , unsigned i_lock )
78  : m_pool( pool ), m_value( i_lock ), m_result( TPI_Lock(pool,i_lock) ) {}
79 
80  ~LockGuard() { TPI_Unlock( m_pool , m_value ); }
81 };
82 
83 //----------------------------------------------------------------------
84 
85 inline
86 int Rank( ThreadPool pool , int & rank , int & size )
87  { return TPI_Rank( pool , & rank , & size ); }
88 
89 inline
90 int Partition( int Rank , int Size , int N , int & I_local , int & N_local )
91  { return TPI_Partition( Rank , Size , N , & I_local , & N_local ); }
92 
93 //----------------------------------------------------------------------
94 
95 inline
96 int Init( int n ) { return TPI_Init( n ); }
97 
98 inline
99 int Finalize() { return TPI_Finalize(); }
100 
101 inline
102 int Size( int & number_allocated ) { return TPI_Size( & number_allocated ); }
103 
104 inline
105 int Concurrency() { return TPI_Concurrency(); }
106 
107 inline
108 double Walltime() { return TPI_Walltime(); }
109 
110 //----------------------------------------------------------------------
111 //----------------------------------------------------------------------
112 
113 namespace {
114 
115 template<class Worker>
116 class WorkerMethodHelper {
117 private:
118  WorkerMethodHelper();
119  WorkerMethodHelper( const WorkerMethodHelper & );
120  WorkerMethodHelper & operator = ( const WorkerMethodHelper & );
121 
122 public:
123 
124  typedef void (Worker::*Method)( ThreadPool );
125 
126  Worker & worker ;
127  Method method ;
128 
129  WorkerMethodHelper( Worker & w , Method m ) : worker(w), method(m) {}
130 
131  static void run( void * arg , ThreadPool pool )
132  {
133  try {
134  WorkerMethodHelper & wm = * reinterpret_cast<WorkerMethodHelper*>(arg);
135  (wm.worker.*wm.method)(pool);
136  } catch(...){}
137  }
138 };
139 
140 }
141 
142 //----------------------------------------------------------------------
143 //----------------------------------------------------------------------
144 
145 inline
146 int Run( void (*func)( void * , ThreadPool ) , void * arg , int n )
147 {
148  return TPI_Run( reinterpret_cast< TPI_parallel_subprogram >(func), arg , n );
149 }
150 
151 template<class Worker>
152 inline
153 int Run( Worker & worker, void (Worker::*method)(ThreadPool) , int n )
154 {
155  typedef WorkerMethodHelper<Worker> WM ;
156 
157  WM tmp( worker , method );
158 
159  return TPI_Run( reinterpret_cast<TPI_parallel_subprogram>(& WM::run),&tmp,n);
160 }
161 
162 //----------------------------------------------------------------------
163 //----------------------------------------------------------------------
164 
165 }
166 
167 #endif
168 
int Run(void(*func)(void *, ThreadPool), void *arg, int=0)
Definition: TPI.hpp:146