ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
TaskflowExecutor.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------ File TaskflowExecutor.h -------------------------*/
3/*--------------------------------------------------------------------------*/
40/*--------------------------------------------------------------------------*/
41/*----------------------------- DEFINITIONS --------------------------------*/
42/*--------------------------------------------------------------------------*/
43
44#ifndef __ADS_TaskflowExecutor
45 #define __ADS_TaskflowExecutor
46/* self-identification: #endif constant */
47
48#if defined( ADS_WITH_TASKFLOW ) || __has_include( <taskflow/taskflow.hpp> )
49
50/*--------------------------------------------------------------------------*/
51/*------------------------------ INCLUDES ----------------------------------*/
52/*--------------------------------------------------------------------------*/
53
54#include "ADS/Contract.h"
56
57#include <taskflow/algorithm/for_each.hpp>
58#include <taskflow/taskflow.hpp>
59
60#include <cstddef>
61
62/*--------------------------------------------------------------------------*/
63/*--------------------------- NAMESPACE ------------------------------------*/
64/*--------------------------------------------------------------------------*/
65
66namespace ADS
67{
68
69/*--------------------------------------------------------------------------*/
70/*------------------------ CLASS TaskflowExecutor --------------------------*/
71/*--------------------------------------------------------------------------*/
72
75{
76
77/*--------------------------------------------------------------------------*/
78 public:
81 explicit TaskflowExecutor( tf::Executor & e ) : f_e( &e ) { }
82
84 [[nodiscard]] std::size_t concurrency( void ) const {
85 const std::size_t w = f_e->num_workers();
86 return( w ? w : 1 );
87 }
88
106 template< class F >
107 void bulk( std::size_t n , F && f ) {
108 if( ( n <= 1 ) || ( concurrency() == 1 ) ) {
109 for( std::size_t i = 0 ; i < n ; ++i )
110 f( i );
111 return;
112 }
113 detail::FirstError error;
114 tf::Taskflow tf;
115 tf.for_each_index(
116 std::size_t( 0 ) , n , std::size_t( 1 ) ,
117 [ & ]( std::size_t i ) {
118 if( error.failed() )
119 return;
120 try {
121 f( i );
122 }
123 catch( ... ) {
124 error.keep();
125 }
126 } ,
127 tf::DynamicPartitioner( 1 ) );
128 if( f_e->this_worker_id() >= 0 )
129 f_e->corun( tf );
130 else
131 f_e->run( tf ).wait();
132 error.rethrow();
133 }
134
135/*--------------------------------------------------------------------------*/
136 private:
137 tf::Executor * f_e;
138 }; // end( class TaskflowExecutor )
139
140/*--------------------------------------------------------------------------*/
141
142} // end( namespace ADS )
143
144#endif /* ADS_WITH_TASKFLOW */
145
146/*--------------------------------------------------------------------------*/
147
148#endif /* TaskflowExecutor.h included */
149
150/*--------------------------------------------------------------------------*/
151/*---------------------- End File TaskflowExecutor.h -----------------------*/
152/*--------------------------------------------------------------------------*/
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...
std::size_t concurrency(void) const
the number of the workers of Taskflow
Definition TaskflowExecutor.h:84
TaskflowExecutor(tf::Executor &e)
the executor on the executor of Taskflow , which it does not own and which must outlive it
Definition TaskflowExecutor.h:81
void bulk(std::size_t n, F &&f)
the calls by a graph of Taskflow, co-run from a worker of Taskflow
Definition TaskflowExecutor.h:107
Definition ADT.h:96