53#ifndef __ADS_OpenMPExecutor
54 #define __ADS_OpenMPExecutor
93#if defined( KMP_VERSION_MAJOR )
94 omp_init_lock_with_hint( &f_lock , omp_sync_hint_uncontended );
96 omp_init_lock( &f_lock );
108 omp_set_lock( &f_lock );
109 ( void )f_turn.load( std::memory_order_acquire );
113 f_turn.store( f_turn.load( std::memory_order_relaxed ) + 1 ,
114 std::memory_order_release );
115 omp_unset_lock( &f_lock );
119 if( omp_test_lock( &f_lock ) == 0 )
121 ( void )f_turn.load( std::memory_order_acquire );
132 std::atomic< unsigned > f_turn{ 0 };
159 omp_in_parallel() ? omp_get_num_threads() : omp_get_max_threads();
160 return( std::size_t( c > 0 ? c : 1 ) );
180 void bulk( std::size_t n , F && f ) {
182 for( std::size_t i = 0 ; i < n ; ++i )
186 detail::FirstError error;
188 if( omp_in_parallel() ) {
189 #pragma omp taskloop grainsize( 1 ) shared( error, f )
190 for( std::size_t i = 0 ; i < n ; ++i ) {
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 ) {
211 f( std::size_t( i ) );
221 template<
class F ,
class G >
223 bulk( 2 , [ & ]( std::size_t i ) {
253 void run( std::size_t p , F && f ) {
255 detail::RunCaller caller;
261 detail::FirstError error;
262 bool smaller =
false;
263 #pragma omp parallel num_threads( int( p ) )
265 if( omp_get_num_threads() !=
int( p ) ) {
271 f( std::size_t( omp_get_thread_num() ) );
278 throw( std::runtime_error(
"OpenMPExecutor::run: a team smaller than "
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