124#ifndef __ADS_Executor
125 #define __ADS_Executor
133#include "ADS/concurrent/impl/Spin.tpp"
145#include <type_traits>
161inline constexpr std::size_t
kGrain = 1024;
169 requires( E & e , std::size_t n , void ( &f )( std::size_t ) )
171 { e.concurrency() } -> std::convertible_to< std::size_t >;
185 void ( &f )( std::size_t ) ,
186 typename E::lock_type & l )
191 { l.try_lock() } -> std::convertible_to< bool >;
198inline thread_local bool tl_runCaller =
false;
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; }
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; }
236 [[nodiscard]]
static bool active(
void ) {
return( tl_inside ); }
239 static inline thread_local bool tl_inside =
false;
255 return( detail::tl_runCaller );
275 static void bulk( std::size_t n , F && f ) {
276 for( std::size_t i = 0 ; i < n ; ++i )
281 template<
class F ,
class G >
294 static void run( std::size_t p , F && f ) {
296 detail::RunCaller caller;
319template< Executor E ,
class F ,
class G >
340[[nodiscard]]
inline std::pair< std::size_t , std::size_t >
342 std::size_t blocks =
kBlocks );
366template< Executor E ,
class F >
369 std::size_t grain =
kGrain ,
370 std::size_t blocks =
kBlocks );
387template< Executor E ,
class F >
388void parallelFor( E & ex , std::size_t first , std::size_t last , F && f ,
414template< Executor E ,
class T ,
class F ,
class Op >
416 T
id , F && f , Op op ,
417 std::size_t grain =
kGrain ,
418 std::size_t blocks =
kBlocks );
465template<
Executor E , std::random_access_iterator I ,
466 std::random_access_iterator O ,
class T ,
class Op = std::plus<> >
485template<
Executor E , std::random_access_iterator I ,
486 std::random_access_iterator O ,
class T ,
class Op = std::plus<> >
498#include "ADS/parallel/impl/Executor.tpp"
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...