ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
FastFlowExecutor.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------ File FastFlowExecutor.h -------------------------*/
3/*--------------------------------------------------------------------------*/
80/*--------------------------------------------------------------------------*/
81/*----------------------------- DEFINITIONS --------------------------------*/
82/*--------------------------------------------------------------------------*/
83
84#ifndef __ADS_FastFlowExecutor
85 #define __ADS_FastFlowExecutor
86/* self-identification: #endif constant */
87
88#if defined( ADS_WITH_FASTFLOW )
89
90// the macros below reach FastFlow only if none of its headers came first:
91// ff/config.hpp reads NUM_CORES and NUM_REAL_CORES at its first inclusion,
92// and the headers that read NO_DEFAULT_MAPPING include it before; with
93// FF_CONFIG_HPP defined they are read already, and the executor would pin
94// its threads and count the cores by a shell at each ParallelFor
95#if defined( FF_CONFIG_HPP ) && \
96 ( ! defined( NUM_CORES ) || ! defined( NUM_REAL_CORES ) || \
97 ( ! defined( ADS_FASTFLOW_MAPPING ) && \
98 ! defined( NO_DEFAULT_MAPPING ) ) )
99// a program that includes the executor after a header of FastFlow is
100// stopped: it includes it before any <ff/...> header, or defines NUM_CORES,
101// NUM_REAL_CORES and NO_DEFAULT_MAPPING before them
102 #error "ADS/parallel/FastFlowExecutor.h included after a header of FastFlow"
103#endif
104
105/*--------------------------------------------------------------------------*/
106/*------------------------------ INCLUDES ----------------------------------*/
107/*--------------------------------------------------------------------------*/
108
109#if ! defined( ADS_FASTFLOW_MAPPING ) && ! defined( NO_DEFAULT_MAPPING )
110 #define NO_DEFAULT_MAPPING
111#endif
112
114
115#include <algorithm>
116#include <thread>
117
118namespace ADS::detail
119{
120
123inline long fastFlowCores( void )
124{
125 static const long n =
126 long( std::max( std::thread::hardware_concurrency() , 1u ) );
127 return( n );
128 }
129
134inline long fastFlowRealCores( void )
135{
136 static const long n = [] {
137 long c = 0;
138#if defined( ADS_WITH_HWLOC )
139 const hwloc_topology_t t = hwlocTopology();
140 HwlocSet S;
141 if( t && S.s ) {
142 for( int i : affinityCpus() )
143 hwloc_bitmap_set( S.s , unsigned( i ) );
144 for( hwloc_obj_t o =
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 ) )
148 ++c;
149 }
150#endif
151 return( c > 0 ? c : fastFlowCores() );
152 }();
153 return( n );
154 }
155
156} // end( namespace ADS::detail )
157
158#if ! defined( NUM_CORES )
159 #define NUM_CORES ::ADS::detail::fastFlowCores()
160#endif
161#if ! defined( NUM_REAL_CORES )
162 #define NUM_REAL_CORES ::ADS::detail::fastFlowRealCores()
163#endif
164
165#include "ADS/Contract.h"
168
169#include <ff/ff.hpp>
170#include <ff/parallel_for.hpp>
171#include <ff/spin-lock.hpp>
172
173#include <algorithm>
174#include <atomic>
175#include <cstddef>
176#include <exception>
177#include <memory>
178#include <mutex>
179#include <thread>
180
181/*--------------------------------------------------------------------------*/
182/*--------------------------- NAMESPACE ------------------------------------*/
183/*--------------------------------------------------------------------------*/
184
185namespace ADS
186{
187
188/*--------------------------------------------------------------------------*/
189/*--------------------------- CLASS FastFlowLock ---------------------------*/
190/*--------------------------------------------------------------------------*/
191
194{
195 public:
197 FastFlowLock( void ) { ff::init_unlocked( f_lock ); }
199 FastFlowLock( const FastFlowLock & ) = delete;
201 FastFlowLock & operator=( const FastFlowLock & ) = delete;
202
205 void lock( void ) {
206 for( Backoff b ; ! try_lock() ; )
207 b.pause();
208 }
209
210 void unlock( void ) { ff::spin_unlock( f_lock ); }
213 bool try_lock( void ) {
214 return( ! f_lock->test_and_set( std::memory_order_acquire ) );
215 }
216
217 private:
218 ff::lock_t f_lock;
219 }; // end( class FastFlowLock )
220
221/*--------------------------------------------------------------------------*/
222/*------------------------- CLASS FastFlowExecutor -------------------------*/
223/*--------------------------------------------------------------------------*/
224
227{
228
229/*--------------------------------------------------------------------------*/
230 public:
234 explicit FastFlowExecutor( std::size_t p = 0 , bool spin = true )
235 : f_p( p ? p
236 : std::max< std::size_t >( std::thread::hardware_concurrency() ,
237 1 ) ) ,
238 f_spin( spin ) {
239 ADS_PRE( f_p >= 1 );
240 }
241
243 [[nodiscard]] std::size_t concurrency( void ) const { return( f_p ); }
244
263 template< class F >
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 )
267 f( i );
268 return;
269 }
270 std::lock_guard< std::mutex > g( f_lock );
271 detail::FirstError error;
272 // at least one index per worker, those from n doing nothing
273 const long m = long( std::max( n , f_p ) );
274 loop().parallel_for(
275 0 , m , 1 , 1 ,
276 [ & ]( const long i ) {
277 if( ( std::size_t( i ) >= n ) || error.failed() )
278 return;
279 tl_inside = true;
280 try {
281 f( std::size_t( i ) );
282 }
283 catch( ... ) {
284 error.keep();
285 }
286 tl_inside = false;
287 } ,
288 long( f_p ) );
289 error.rethrow();
290 }
291
294
312 template< class F >
313 void run( std::size_t p , F && f ) {
314 ADS_PRE( p <= f_p );
315 ADS_PRE( ( p <= 1 ) || ! tl_inside );
316 detail::RunCaller caller;
317 if( p <= 1 ) {
318 if( p == 1 )
319 f( 0 );
320 return;
321 }
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 ) {
327 tl_inside = true;
328 try {
329 f( std::size_t( i ) );
330 }
331 catch( ... ) {
332 error.keep();
333 }
334 tl_inside = false;
335 } ,
336 long( p ) );
337 error.rethrow();
338 }
339
340/*--------------------------------------------------------------------------*/
341 private:
342 static inline thread_local bool tl_inside = false;
343
345 ff::ParallelFor & loop( void ) {
346 if( ! f_for ) {
347 f_for = std::make_unique< ff::ParallelFor >( long( f_p ) , f_spin ,
348 f_spin );
349 f_for->disableScheduler( true );
350 }
351 return( *f_for );
352 }
353
356 ff::ParallelFor & workers( void ) {
357 if( ! f_run ) {
358 f_run = std::make_unique< ff::ParallelFor >( long( f_p ) , false , false );
359 f_run->disableScheduler( true );
360 }
361 if( f_for && f_spin )
362 f_for->threadPause();
363 return( *f_run );
364 }
365
366 std::size_t f_p;
367 bool f_spin;
368 std::unique_ptr< ff::ParallelFor > f_for;
369 std::unique_ptr< ff::ParallelFor > f_run;
370 std::mutex f_lock;
371 }; // end( class FastFlowExecutor )
372
373/*--------------------------------------------------------------------------*/
374
375} // end( namespace ADS )
376
377#endif /* ADS_WITH_FASTFLOW */
378
379/*--------------------------------------------------------------------------*/
380
381#endif /* FastFlowExecutor.h included */
382
383/*--------------------------------------------------------------------------*/
384/*---------------------- End File FastFlowExecutor.h -----------------------*/
385/*--------------------------------------------------------------------------*/
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
Definition ADT.h:96