84#ifndef __ADS_FastFlowExecutor
85 #define __ADS_FastFlowExecutor
88#if defined( ADS_WITH_FASTFLOW )
95#if defined( FF_CONFIG_HPP ) && \
96 ( ! defined( NUM_CORES ) || ! defined( NUM_REAL_CORES ) || \
97 ( ! defined( ADS_FASTFLOW_MAPPING ) && \
98 ! defined( NO_DEFAULT_MAPPING ) ) )
102 #error "ADS/parallel/FastFlowExecutor.h included after a header of FastFlow"
109#if ! defined( ADS_FASTFLOW_MAPPING ) && ! defined( NO_DEFAULT_MAPPING )
110 #define NO_DEFAULT_MAPPING
123inline long fastFlowCores(
void )
125 static const long n =
126 long( std::max( std::thread::hardware_concurrency() , 1u ) );
134inline long fastFlowRealCores(
void )
136 static const long n = [] {
138#if defined( ADS_WITH_HWLOC )
139 const hwloc_topology_t t = hwlocTopology();
143 hwloc_bitmap_set( S.s ,
unsigned( i ) );
145 hwloc_get_next_obj_by_type( t , HWLOC_OBJ_CORE ,
nullptr ) ;
146 o ; o = hwloc_get_next_obj_by_type( t , HWLOC_OBJ_CORE , o ) )
147 if( hwloc_bitmap_intersects( o->cpuset , S.s ) )
151 return( c > 0 ? c : fastFlowCores() );
158#if ! defined( NUM_CORES )
159 #define NUM_CORES ::ADS::detail::fastFlowCores()
161#if ! defined( NUM_REAL_CORES )
162 #define NUM_REAL_CORES ::ADS::detail::fastFlowRealCores()
170#include <ff/parallel_for.hpp>
171#include <ff/spin-lock.hpp>
210 void unlock(
void ) { ff::spin_unlock( f_lock ); }
214 return( ! f_lock->test_and_set( std::memory_order_acquire ) );
236 : std::max< std::size_t >( std::thread::hardware_concurrency() ,
243 [[nodiscard]] std::size_t
concurrency(
void )
const {
return( f_p ); }
264 void bulk( std::size_t n , F && f ) {
265 if( ( n <= 1 ) || ( f_p == 1 ) || tl_inside ) {
266 for( std::size_t i = 0 ; i < n ; ++i )
270 std::lock_guard< std::mutex > g( f_lock );
271 detail::FirstError error;
273 const long m = long( std::max( n , f_p ) );
276 [ & ](
const long i ) {
277 if( ( std::size_t( i ) >= n ) || error.failed() )
281 f( std::size_t( i ) );
313 void run( std::size_t p , F && f ) {
315 ADS_PRE( ( p <= 1 ) || ! tl_inside );
316 detail::RunCaller caller;
322 std::lock_guard< std::mutex > g( f_lock );
323 detail::FirstError error;
324 workers().parallel_for(
325 0 ,
long( p ) , 1 , 1 ,
326 [ & ](
const long i ) {
329 f( std::size_t( i ) );
342 static inline thread_local bool tl_inside =
false;
345 ff::ParallelFor & loop(
void ) {
347 f_for = std::make_unique< ff::ParallelFor >(
long( f_p ) , f_spin ,
349 f_for->disableScheduler(
true );
356 ff::ParallelFor & workers(
void ) {
358 f_run = std::make_unique< ff::ParallelFor >(
long( f_p ) ,
false ,
false );
359 f_run->disableScheduler(
true );
361 if( f_for && f_spin )
362 f_for->threadPause();
368 std::unique_ptr< ff::ParallelFor > f_for;
369 std::unique_ptr< ff::ParallelFor > f_run;
The placement of the threads on the processors, beyond the book: a worker of a parallel algorithm pin...
The exponential backoff of a thread that waits for a word of memory to change, beyond the book: the -...
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...
the exponential backoff of a waiting thread: pauses at the -th call up to , then a yield to the syst...
Definition Backoff.h:70
std::size_t concurrency(void) const
the number of workers
Definition FastFlowExecutor.h:243
void run(std::size_t p, F &&f)
the calls of long-lived workers, at the same time on workers of FastFlow
Definition FastFlowExecutor.h:313
FastFlowExecutor(std::size_t p=0, bool spin=true)
the executor of workers of FastFlow (the hardware threads if ), which spin between the calls if spin
Definition FastFlowExecutor.h:234
void bulk(std::size_t n, F &&f)
the calls on the workers, in order on the calling thread if it is inside a call
Definition FastFlowExecutor.h:264
FastFlowLock lock_type
the lock of the data shared by the workers of run()
Definition FastFlowExecutor.h:293
the spin lock of FastFlow, ff::lock_t, as a mutex of the standard
Definition FastFlowExecutor.h:194
void unlock(void)
gives the lock back
Definition FastFlowExecutor.h:210
FastFlowLock & operator=(const FastFlowLock &)=delete
no copy, as a mutex
void lock(void)
takes the lock: a test-and-set of its flag, retried after the exponential backoff of Backoff until it...
Definition FastFlowExecutor.h:205
FastFlowLock(void)
an unlocked lock
Definition FastFlowExecutor.h:197
FastFlowLock(const FastFlowLock &)=delete
no copy, as a mutex
bool try_lock(void)
takes the lock if it is free, by one test-and-set of its flag (that of spin_lock()),...
Definition FastFlowExecutor.h:213
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
const std::vector< int > & affinityCpus(void)
the processors on which the process may run, in increasing order, as the first call found them: those...
Definition Affinity.h:127