ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Executor.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File Executor.h -----------------------------*/
3/*--------------------------------------------------------------------------*/
120/*--------------------------------------------------------------------------*/
121/*----------------------------- DEFINITIONS --------------------------------*/
122/*--------------------------------------------------------------------------*/
123
124#ifndef __ADS_Executor
125 #define __ADS_Executor
126/* self-identification: #endif constant */
127
128/*--------------------------------------------------------------------------*/
129/*------------------------------ INCLUDES ----------------------------------*/
130/*--------------------------------------------------------------------------*/
131
132#include "ADS/Contract.h"
133#include "ADS/concurrent/impl/Spin.tpp"
134
135#include <algorithm>
136#include <atomic>
137#include <concepts>
138#include <cstddef>
139#include <cstdint>
140#include <exception>
141#include <iterator>
142#include <memory>
143#include <mutex>
144#include <optional>
145#include <type_traits>
146#include <utility>
147#include <vector>
148
149/*--------------------------------------------------------------------------*/
150/*--------------------------- NAMESPACE ------------------------------------*/
151/*--------------------------------------------------------------------------*/
152
153namespace ADS
154{
155
158inline constexpr std::size_t kBlocks = 64;
159
161inline constexpr std::size_t kGrain = 1024;
162
167template< class E >
168concept Executor =
169 requires( E & e , std::size_t n , void ( &f )( std::size_t ) )
170{
171 { e.concurrency() } -> std::convertible_to< std::size_t >;
172 e.bulk( n , f );
173 };
174
182template< class E >
184 Executor< E > && requires( E & e , std::size_t p ,
185 void ( &f )( std::size_t ) ,
186 typename E::lock_type & l )
187{
188 e.run( p , f );
189 l.lock();
190 l.unlock();
191 { l.try_lock() } -> std::convertible_to< bool >;
192 };
193
194namespace detail
195{
196
198inline thread_local bool tl_runCaller = false;
199
201class RunCaller
202{
203 public:
205 RunCaller( void ) : f_old( tl_runCaller ) { tl_runCaller = true; }
207 RunCaller( const RunCaller & ) = delete;
209 RunCaller & operator=( const RunCaller & ) = delete;
211 ~RunCaller() { tl_runCaller = f_old; }
212
213 private:
214 bool f_old;
215 };
216
222template< class T >
223class InsideCall
224{
225 public:
227 InsideCall( void ) : f_old( tl_inside ) { tl_inside = true; }
229 InsideCall( const InsideCall & ) = delete;
231 InsideCall & operator=( const InsideCall & ) = delete;
233 ~InsideCall() { tl_inside = f_old; }
234
236 [[nodiscard]] static bool active( void ) { return( tl_inside ); }
237
238 private:
239 static inline thread_local bool tl_inside = false;
240 bool f_old;
241 };
242
243} // end( namespace detail )
244
253[[nodiscard]] inline bool isRunCaller( void )
254{
255 return( detail::tl_runCaller );
256 }
257
258/*--------------------------------------------------------------------------*/
259/*---------------------------- CLASS Sequential ----------------------------*/
260/*--------------------------------------------------------------------------*/
261
265{
266 public:
268 [[nodiscard]] static constexpr std::size_t concurrency( void ) {
269 return( 1 );
270 }
271
274 template< class F >
275 static void bulk( std::size_t n , F && f ) {
276 for( std::size_t i = 0 ; i < n ; ++i )
277 f( i );
278 }
279
281 template< class F , class G >
282 static void invoke( F && f , G && g ) {
283 f();
284 g();
285 }
286
288 using lock_type = std::mutex;
289
293 template< class F >
294 static void run( std::size_t p , F && f ) {
295 ADS_PRE( p <= 1 );
296 detail::RunCaller caller;
297 if( p == 1 )
298 f( 0 );
299 }
300 }; // end( class Sequential )
301
302/*--------------------------------------------------------------------------*/
303/*------------------------------- PATTERNS ---------------------------------*/
304/*--------------------------------------------------------------------------*/
305
319template< Executor E , class F , class G >
320void parallelInvoke( E & ex , F && f , G && g );
321
340[[nodiscard]] inline std::pair< std::size_t , std::size_t >
341blocksOf( std::size_t first , std::size_t last , std::size_t grain = kGrain ,
342 std::size_t blocks = kBlocks );
343
366template< Executor E , class F >
367void parallelForBlocks( E & ex , std::size_t first , std::size_t last ,
368 F && f ,
369 std::size_t grain = kGrain ,
370 std::size_t blocks = kBlocks );
371
387template< Executor E , class F >
388void parallelFor( E & ex , std::size_t first , std::size_t last , F && f ,
389 std::size_t grain = kGrain , std::size_t blocks = kBlocks );
390
414template< Executor E , class T , class F , class Op >
415[[nodiscard]] T parallelReduce( E & ex , std::size_t first , std::size_t last ,
416 T id , F && f , Op op ,
417 std::size_t grain = kGrain ,
418 std::size_t blocks = kBlocks );
419
465template< Executor E , std::random_access_iterator I ,
466 std::random_access_iterator O , class T , class Op = std::plus<> >
467T parallelInclusiveScan( E & ex , I in , std::size_t n , O out , T init ,
468 Op op = Op() );
469
485template< Executor E , std::random_access_iterator I ,
486 std::random_access_iterator O , class T , class Op = std::plus<> >
487T parallelExclusiveScan( E & ex , I in , std::size_t n , O out , T init ,
488 Op op = Op() );
489
490/*--------------------------------------------------------------------------*/
491
492} // end( namespace ADS )
493
494/*--------------------------------------------------------------------------*/
495/*-------------------------- IMPLEMENTATION --------------------------------*/
496/*--------------------------------------------------------------------------*/
497
498#include "ADS/parallel/impl/Executor.tpp"
499
500/*--------------------------------------------------------------------------*/
501
502#endif /* Executor.h included */
503
504/*--------------------------------------------------------------------------*/
505/*------------------------- End File Executor.h ----------------------------*/
506/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
the executor of the calling thread alone, which makes the calls in their order
Definition Executor.h:265
static void run(std::size_t p, F &&f)
the call on the calling thread, the only worker, if (none if )
Definition Executor.h:294
static constexpr std::size_t concurrency(void)
the number of threads, 1
Definition Executor.h:268
std::mutex lock_type
the lock of the data shared by the workers
Definition Executor.h:288
static void invoke(F &&f, G &&g)
the call f() and then the call g(), on the calling thread
Definition Executor.h:282
static void bulk(std::size_t n, F &&f)
the calls , in this order, on the calling thread
Definition Executor.h:275
the executors, whose bulk( n , f ) runs on their threads and returns when they have returned,...
Definition Executor.h:168
the executors that also run long-lived workers: run( p , f ) runs , at the same time on distinct thr...
Definition Executor.h:183
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
void parallelFor(E &ex, std::size_t first, std::size_t last, F &&f, std::size_t grain=kGrain, std::size_t blocks=kBlocks)
the calls f( i ) for each , in parallel
constexpr std::size_t kGrain
the least number of indices of a block of the loops, by default
Definition Executor.h:161
T parallelReduce(E &ex, std::size_t first, std::size_t last, T id, F &&f, Op op, std::size_t grain=kGrain, std::size_t blocks=kBlocks)
the reduction of : the results of f( lo , hi ) on the blocks, combined by (op) in the order of the b...
T parallelExclusiveScan(E &ex, I in, std::size_t n, O out, T init, Op op=Op())
the exclusive prefixes of the items from in, written from out, which may be in; the sum of all of...
std::pair< std::size_t, std::size_t > blocksOf(std::size_t first, std::size_t last, std::size_t grain=kGrain, std::size_t blocks=kBlocks)
the blocks of , with at least indices each and at most of them: their number , and the size of all...
bool isRunCaller(void)
true if the running thread is the thread that called a run() of an executor (or a feedbackFarm() [see...
Definition Executor.h:253
constexpr std::size_t kBlocks
the largest number of blocks into which the loops and the reductions cut their indices,...
Definition Executor.h:158
void parallelInvoke(E &ex, F &&f, G &&g)
the calls f() and g() in parallel, returning when both have returned
void parallelForBlocks(E &ex, std::size_t first, std::size_t last, F &&f, std::size_t grain=kGrain, std::size_t blocks=kBlocks)
the calls f( lo , hi ) on the blocks of , in parallel
T parallelInclusiveScan(E &ex, I in, std::size_t n, O out, T init, Op op=Op())
the inclusive prefixes of the items from in, written from out, which may be in; the last prefix (i...
Definition ADT.h:96