ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
OpenMPExecutor.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------- File OpenMPExecutor.h --------------------------*/
3/*--------------------------------------------------------------------------*/
49/*--------------------------------------------------------------------------*/
50/*----------------------------- DEFINITIONS --------------------------------*/
51/*--------------------------------------------------------------------------*/
52
53#ifndef __ADS_OpenMPExecutor
54 #define __ADS_OpenMPExecutor
55/* self-identification: #endif constant */
56
57#if defined( _OPENMP )
58
59/*--------------------------------------------------------------------------*/
60/*------------------------------ INCLUDES ----------------------------------*/
61/*--------------------------------------------------------------------------*/
62
63#include "ADS/Contract.h"
65
66#include <omp.h>
67
68#include <algorithm>
69#include <atomic>
70#include <cstddef>
71#include <stdexcept>
72
73/*--------------------------------------------------------------------------*/
74/*--------------------------- NAMESPACE ------------------------------------*/
75/*--------------------------------------------------------------------------*/
76
77namespace ADS
78{
79
80/*--------------------------------------------------------------------------*/
81/*---------------------------- CLASS OpenMPLock ----------------------------*/
82/*--------------------------------------------------------------------------*/
83
86{
87 public:
92 OpenMPLock( void ) {
93#if defined( KMP_VERSION_MAJOR )
94 omp_init_lock_with_hint( &f_lock , omp_sync_hint_uncontended );
95#else
96 omp_init_lock( &f_lock );
97#endif
98 }
99
100 OpenMPLock( const OpenMPLock & ) = delete;
102 OpenMPLock & operator=( const OpenMPLock & ) = delete;
104 ~OpenMPLock() { omp_destroy_lock( &f_lock ); }
105
107 void lock( void ) {
108 omp_set_lock( &f_lock );
109 ( void )f_turn.load( std::memory_order_acquire );
110 }
111
112 void unlock( void ) {
113 f_turn.store( f_turn.load( std::memory_order_relaxed ) + 1 ,
114 std::memory_order_release );
115 omp_unset_lock( &f_lock );
116 }
117
118 bool try_lock( void ) {
119 if( omp_test_lock( &f_lock ) == 0 )
120 return( false );
121 ( void )f_turn.load( std::memory_order_acquire );
122 return( true );
123 }
124
125 private:
126 omp_lock_t f_lock;
132 std::atomic< unsigned > f_turn{ 0 };
133 }; // end( class OpenMPLock )
134
135/*--------------------------------------------------------------------------*/
136/*-------------------------- CLASS OpenMPExecutor --------------------------*/
137/*--------------------------------------------------------------------------*/
138
141{
142
143/*--------------------------------------------------------------------------*/
144 public:
148 explicit OpenMPExecutor( std::size_t p = 0 ) : f_p( p ) {
149 ADS_PRE( concurrency() >= 1 );
150 }
151
155 [[nodiscard]] std::size_t concurrency( void ) const {
156 if( f_p )
157 return( f_p );
158 const int c =
159 omp_in_parallel() ? omp_get_num_threads() : omp_get_max_threads();
160 return( std::size_t( c > 0 ? c : 1 ) );
161 }
162
179 template< class F >
180 void bulk( std::size_t n , F && f ) {
181 if( ( n <= 1 ) || ( concurrency() == 1 ) ) {
182 for( std::size_t i = 0 ; i < n ; ++i )
183 f( i );
184 return;
185 }
186 detail::FirstError error;
187#if _OPENMP >= 201511
188 if( omp_in_parallel() ) {
189 #pragma omp taskloop grainsize( 1 ) shared( error, f )
190 for( std::size_t i = 0 ; i < n ; ++i ) {
191 if( error.failed() )
192 continue;
193 try {
194 f( i );
195 }
196 catch( ... ) {
197 error.keep();
198 }
199 }
200 error.rethrow();
201 return;
202 }
203#endif
204 const long m = long( n );
205 #pragma omp parallel for schedule( dynamic, 1 ) \
206 num_threads( int( std::min( concurrency(), n ) ) )
207 for( long i = 0 ; i < m ; ++i ) {
208 if( error.failed() )
209 continue;
210 try {
211 f( std::size_t( i ) );
212 }
213 catch( ... ) {
214 error.keep();
215 }
216 }
217 error.rethrow();
218 }
219
221 template< class F , class G >
222 void invoke( F && f , G && g ) {
223 bulk( 2 , [ & ]( std::size_t i ) {
224 if( i == 0 )
225 f();
226 else
227 g();
228 } );
229 }
230
233
252 template< class F >
253 void run( std::size_t p , F && f ) {
254 ADS_PRE( p <= concurrency() );
255 detail::RunCaller caller;
256 if( p <= 1 ) {
257 if( p == 1 )
258 f( 0 );
259 return;
260 }
261 detail::FirstError error;
262 bool smaller = false;
263 #pragma omp parallel num_threads( int( p ) )
264 {
265 if( omp_get_num_threads() != int( p ) ) {
266 #pragma omp single
267 smaller = true;
268 }
269 else
270 try {
271 f( std::size_t( omp_get_thread_num() ) );
272 }
273 catch( ... ) {
274 error.keep();
275 }
276 }
277 if( smaller )
278 throw( std::runtime_error( "OpenMPExecutor::run: a team smaller than "
279 "the workers" ) );
280 error.rethrow();
281 }
282
283/*--------------------------------------------------------------------------*/
284 private:
285 std::size_t f_p;
286 }; // end( class OpenMPExecutor )
287
288/*--------------------------------------------------------------------------*/
289
290} // end( namespace ADS )
291
292#endif /* _OPENMP */
293
294/*--------------------------------------------------------------------------*/
295
296#endif /* OpenMPExecutor.h included */
297
298/*--------------------------------------------------------------------------*/
299/*----------------------- End File OpenMPExecutor.h ------------------------*/
300/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The layer of execution of the parallel algorithms of ADS, beyond the book: an executor runs the calls...
void run(std::size_t p, F &&f)
the calls of long-lived workers, at the same time on the threads of a region of OpenMP
Definition OpenMPExecutor.h:253
void invoke(F &&f, G &&g)
the calls f() and g() in parallel, by bulk() on 2 indices
Definition OpenMPExecutor.h:222
OpenMPLock lock_type
the lock of the data shared by the workers of run()
Definition OpenMPExecutor.h:232
std::size_t concurrency(void) const
the number of threads: , or, if , the threads of the team of the caller inside a region and omp_get_m...
Definition OpenMPExecutor.h:155
void bulk(std::size_t n, F &&f)
the calls on the threads
Definition OpenMPExecutor.h:180
OpenMPExecutor(std::size_t p=0)
the executor of threads of OpenMP (omp_get_max_threads() if , or the team of the caller inside a reg...
Definition OpenMPExecutor.h:148
the lock of OpenMP, omp_lock_t, as a mutex of the standard
Definition OpenMPExecutor.h:86
~OpenMPLock()
destroys the lock, which nobody holds
Definition OpenMPExecutor.h:104
OpenMPLock & operator=(const OpenMPLock &)=delete
no copy, as a mutex
OpenMPLock(const OpenMPLock &)=delete
no copy, as a mutex
bool try_lock(void)
takes the lock if it is free, and tells whether it did
Definition OpenMPExecutor.h:118
void lock(void)
takes the lock, waiting for it
Definition OpenMPExecutor.h:107
OpenMPLock(void)
an unlocked lock; on the runtime of LLVM a test-and-set one, since its default lock queues the thread...
Definition OpenMPExecutor.h:92
void unlock(void)
gives the lock back
Definition OpenMPExecutor.h:112
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
Definition ADT.h:96