ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
TBBExecutor.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File TBBExecutor.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
53/*--------------------------------------------------------------------------*/
54/*----------------------------- DEFINITIONS --------------------------------*/
55/*--------------------------------------------------------------------------*/
56
57#ifndef __ADS_TBBExecutor
58 #define __ADS_TBBExecutor
59/* self-identification: #endif constant */
60
61#if defined( ADS_WITH_TBB )
62
63/*--------------------------------------------------------------------------*/
64/*------------------------------ INCLUDES ----------------------------------*/
65/*--------------------------------------------------------------------------*/
66
67#include "ADS/Contract.h"
69
70#include <tbb/blocked_range.h>
71#include <tbb/parallel_for.h>
72#include <tbb/parallel_invoke.h>
73#include <tbb/partitioner.h>
74#include <tbb/spin_mutex.h>
75#include <tbb/task_arena.h>
76
77#include <algorithm>
78#include <cstddef>
79#include <optional>
80#include <thread>
81#include <utility>
82
83/*--------------------------------------------------------------------------*/
84/*--------------------------- NAMESPACE ------------------------------------*/
85/*--------------------------------------------------------------------------*/
86
87namespace ADS
88{
89
90/*--------------------------------------------------------------------------*/
91/*---------------------------- CLASS TBBExecutor ---------------------------*/
92/*--------------------------------------------------------------------------*/
93
96{
97
98/*--------------------------------------------------------------------------*/
99 public:
101 struct Current
102 { };
103
106 static constexpr Current current{};
107
111 explicit TBBExecutor( std::size_t p = 0 )
112 : f_p( p ? p
113 : std::max< std::size_t >( std::thread::hardware_concurrency() ,
114 1 ) ) ,
115 f_own( std::in_place , int( f_p ) ) {
116 ADS_PRE( f_p >= 1 );
117 }
118
121 explicit TBBExecutor( tbb::task_arena & arena )
122 : f_p( 0 ) , f_user( &arena ) { }
123
127 explicit TBBExecutor( Current ) : f_p( 0 ) { }
128
131 [[nodiscard]] std::size_t concurrency( void ) const {
132 if( f_p )
133 return( f_p );
134 const int c = f_user ? f_user->max_concurrency()
135 : tbb::this_task_arena::max_concurrency();
136 return( std::size_t( c > 0 ? c : 1 ) );
137 }
138
152 template< class F >
153 void bulk( std::size_t n , F && f ) {
154 if( ( n <= 1 ) || ( concurrency() == 1 ) ) {
155 for( std::size_t i = 0 ; i < n ; ++i )
156 f( i );
157 return;
158 }
159 execute( [ & ] {
160 tbb::parallel_for(
161 tbb::blocked_range< std::size_t >( 0 , n , 1 ) ,
162 [ & ]( const tbb::blocked_range< std::size_t > & r ) {
163 for( std::size_t i = r.begin() ; i < r.end() ; ++i )
164 f( i );
165 } ,
166 tbb::simple_partitioner() );
167 } );
168 }
169
172 template< class F , class G >
173 void invoke( F && f , G && g ) {
174 if( concurrency() == 1 ) {
175 f();
176 g();
177 return;
178 }
179 execute( [ & ] { tbb::parallel_invoke( f , g ); } );
180 }
181
183 using lock_type = tbb::spin_mutex;
184
203 template< class F >
204 void run( std::size_t p , F && f ) {
205 ADS_PRE( p <= concurrency() );
206 detail::RunCaller caller;
207 if( p <= 1 ) {
208 if( p == 1 )
209 f( 0 );
210 return;
211 }
212 detail::FirstError error;
213 execute( [ & ] {
214 tbb::parallel_for(
215 tbb::blocked_range< std::size_t >( 0 , p , 1 ) ,
216 [ & ]( const tbb::blocked_range< std::size_t > & r ) {
217 for( std::size_t i = r.begin() ; i < r.end() ; ++i )
218 tbb::this_task_arena::isolate( [ & ] {
219 try {
220 f( i );
221 }
222 catch( ... ) {
223 error.keep();
224 }
225 } );
226 } ,
227 tbb::simple_partitioner() );
228 } );
229 error.rethrow();
230 }
231
232/*--------------------------------------------------------------------------*/
233 private:
235 template< class B >
236 void execute( B && body ) {
237 if( f_user )
238 f_user->execute( body );
239 else if( f_own )
240 f_own->execute( body );
241 else
242 body();
243 }
244
245 std::size_t f_p;
246 std::optional< tbb::task_arena > f_own;
247 tbb::task_arena * f_user = nullptr;
248 }; // end( class TBBExecutor )
249
250/*--------------------------------------------------------------------------*/
251
252} // end( namespace ADS )
253
254#endif /* ADS_WITH_TBB */
255
256/*--------------------------------------------------------------------------*/
257
258#endif /* TBBExecutor.h included */
259
260/*--------------------------------------------------------------------------*/
261/*------------------------- End File TBBExecutor.h -------------------------*/
262/*--------------------------------------------------------------------------*/
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...
TBBExecutor(Current)
the executor on the arena of the thread that makes each call (the arena of a loop of oneTBB that call...
Definition TBBExecutor.h:127
void run(std::size_t p, F &&f)
the calls of long-lived workers, at the same time on threads of the arena
Definition TBBExecutor.h:204
void invoke(F &&f, G &&g)
the calls f() and g() in parallel, by the parallel_invoke of oneTBB in the arena
Definition TBBExecutor.h:173
void bulk(std::size_t n, F &&f)
the calls on the threads of the arena
Definition TBBExecutor.h:153
std::size_t concurrency(void) const
the number of threads of the arena: for its own, the concurrency of the arena of the program,...
Definition TBBExecutor.h:131
TBBExecutor(tbb::task_arena &arena)
the executor on the arena of the program, which it does not own and which must outlive it; its thread...
Definition TBBExecutor.h:121
TBBExecutor(std::size_t p=0)
the executor of an arena of its own, of threads (the hardware threads if )
Definition TBBExecutor.h:111
static constexpr Current current
the executor on the arena of the thread that makes each call [see TBBExecutor( Current )]
Definition TBBExecutor.h:106
tbb::spin_mutex lock_type
the lock of the data shared by the workers of run()
Definition TBBExecutor.h:183
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
Definition ADT.h:96
the tag of the executor on the arena of the calling thread
Definition TBBExecutor.h:102