27 #ifndef util_ThreadPool_hpp
28 #define util_ThreadPool_hpp
34 typedef TPI_ThreadPool ThreadPool ;
39 int Run(
void (*func)(
void*,ThreadPool),
void * arg ,
int = 0 );
43 template<
class Worker>
44 int Run( Worker & worker ,
void (Worker::*method)(ThreadPool) ,
int = 0 );
49 int Set_lock_size(
int n ) {
return TPI_Set_lock_size( n ); }
52 int Lock( ThreadPool pool ,
int n ) {
return TPI_Lock( pool , n ); }
55 int Trylock( ThreadPool pool ,
int n ) {
return TPI_Trylock( pool , n ); }
58 int Unlock( ThreadPool pool ,
int n ) {
return TPI_Unlock( pool , n ); }
71 const ThreadPool m_pool ;
75 operator int()
const {
return m_result ; }
77 explicit LockGuard( ThreadPool pool ,
unsigned i_lock )
78 : m_pool( pool ), m_value( i_lock ), m_result( TPI_Lock(pool,i_lock) ) {}
80 ~
LockGuard() { TPI_Unlock( m_pool , m_value ); }
86 int Rank( ThreadPool pool ,
int & rank ,
int & size )
87 {
return TPI_Rank( pool , & rank , & size ); }
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 ); }
96 int Init(
int n ) {
return TPI_Init( n ); }
99 int Finalize() {
return TPI_Finalize(); }
102 int Size(
int & number_allocated ) {
return TPI_Size( & number_allocated ); }
105 int Concurrency() {
return TPI_Concurrency(); }
108 double Walltime() {
return TPI_Walltime(); }
115 template<
class Worker>
116 class WorkerMethodHelper {
118 WorkerMethodHelper();
119 WorkerMethodHelper(
const WorkerMethodHelper & );
120 WorkerMethodHelper & operator = (
const WorkerMethodHelper & );
124 typedef void (Worker::*Method)( ThreadPool );
129 WorkerMethodHelper( Worker & w , Method m ) : worker(w), method(m) {}
131 static void run(
void * arg , ThreadPool pool )
134 WorkerMethodHelper & wm = *
reinterpret_cast<WorkerMethodHelper*
>(arg);
135 (wm.worker.*wm.method)(pool);
146 int Run(
void (*func)(
void * , ThreadPool ) ,
void * arg ,
int n )
148 return TPI_Run( reinterpret_cast< TPI_parallel_subprogram >(func), arg , n );
151 template<
class Worker>
153 int Run( Worker & worker,
void (Worker::*method)(ThreadPool) ,
int n )
155 typedef WorkerMethodHelper<Worker> WM ;
157 WM tmp( worker , method );
159 return TPI_Run( reinterpret_cast<TPI_parallel_subprogram>(& WM::run),&tmp,n);
int Run(void(*func)(void *, ThreadPool), void *arg, int=0)