ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
StdParExecutor.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------- File StdParExecutor.h --------------------------*/
3/*--------------------------------------------------------------------------*/
46/*--------------------------------------------------------------------------*/
47/*----------------------------- DEFINITIONS --------------------------------*/
48/*--------------------------------------------------------------------------*/
49
50#ifndef __ADS_StdParExecutor
51 #define __ADS_StdParExecutor
52/* self-identification: #endif constant */
53
54#include <version>
55
56#if defined( __cpp_lib_parallel_algorithm ) || defined( ADS_WITH_STDPAR )
57
58/*--------------------------------------------------------------------------*/
59/*------------------------------ INCLUDES ----------------------------------*/
60/*--------------------------------------------------------------------------*/
61
62#include "ADS/Contract.h"
64
65#include <algorithm>
66#include <compare>
67#include <cstddef>
68#include <execution>
69#include <iterator>
70#include <thread>
71#include <type_traits>
72
73/*--------------------------------------------------------------------------*/
74/*--------------------------- NAMESPACE ------------------------------------*/
75/*--------------------------------------------------------------------------*/
76
77namespace ADS
78{
79
80namespace detail
81{
82
84struct IndexIterator
85{
86 using iterator_category = std::random_access_iterator_tag;
87 using value_type = std::size_t;
88 using difference_type = std::ptrdiff_t;
89 using pointer = const std::size_t *;
90 using reference = std::size_t;
91
92 std::size_t i = 0;
93
94 std::size_t operator*() const { return( i ); }
95 std::size_t operator[]( difference_type k ) const {
96 return( i + std::size_t( k ) );
97 }
98 IndexIterator & operator++() {
99 ++i;
100 return( *this );
101 }
102 IndexIterator operator++( int ) {
103 IndexIterator t = *this;
104 ++i;
105 return( t );
106 }
107 IndexIterator & operator--() {
108 --i;
109 return( *this );
110 }
111 IndexIterator operator--( int ) {
112 IndexIterator t = *this;
113 --i;
114 return( t );
115 }
116 IndexIterator & operator+=( difference_type k ) {
117 i += std::size_t( k );
118 return( *this );
119 }
120 IndexIterator & operator-=( difference_type k ) {
121 i -= std::size_t( k );
122 return( *this );
123 }
124 friend IndexIterator operator+( IndexIterator a , difference_type k ) {
125 return( a += k );
126 }
127 friend IndexIterator operator+( difference_type k , IndexIterator a ) {
128 return( a += k );
129 }
130 friend IndexIterator operator-( IndexIterator a , difference_type k ) {
131 return( a -= k );
132 }
133 friend difference_type operator-( IndexIterator a , IndexIterator b ) {
134 return( difference_type( a.i ) - difference_type( b.i ) );
135 }
136 friend bool operator==( IndexIterator , IndexIterator ) = default;
137 friend auto operator<=>( IndexIterator , IndexIterator ) = default;
138 };
139
140} // end( namespace detail )
141
142/*--------------------------------------------------------------------------*/
143/*------------------------- CLASS StdParExecutor ---------------------------*/
144/*--------------------------------------------------------------------------*/
145
148template< class Policy = std::execution::parallel_policy >
149 requires(
150 std::is_execution_policy_v< Policy > &&
151 ! std::is_same_v< Policy , std::execution::parallel_unsequenced_policy > )
153{
154
155/*--------------------------------------------------------------------------*/
156 public:
159 explicit StdParExecutor( std::size_t p = 0 )
160 : f_p( p ? p
161 : std::max< std::size_t >( std::thread::hardware_concurrency() ,
162 1 ) ) { }
163
165 [[nodiscard]] std::size_t concurrency( void ) const { return( f_p ); }
166
182 template< class F >
183 void bulk( std::size_t n , F && f ) {
184 if( ( n <= 1 ) || ( f_p == 1 ) ) {
185 for( std::size_t i = 0 ; i < n ; ++i )
186 f( i );
187 return;
188 }
189 detail::FirstError error;
190 // a policy by name: the parallel algorithms of libstdc++ 13 do not take
191 // a temporary one
192 Policy policy{};
193 std::for_each( policy , detail::IndexIterator{ 0 } ,
194 detail::IndexIterator{ n } , [ & ]( std::size_t i ) {
195 if( error.failed() )
196 return;
197 try {
198 f( i );
199 }
200 catch( ... ) {
201 error.keep();
202 }
203 } );
204 error.rethrow();
205 }
206
207/*--------------------------------------------------------------------------*/
208 private:
209 std::size_t f_p;
210 }; // end( class StdParExecutor )
211
212/*--------------------------------------------------------------------------*/
213
214} // end( namespace ADS )
215
216#endif /* __cpp_lib_parallel_algorithm */
217
218/*--------------------------------------------------------------------------*/
219
220#endif /* StdParExecutor.h included */
221
222/*--------------------------------------------------------------------------*/
223/*----------------------- End File StdParExecutor.h ------------------------*/
224/*--------------------------------------------------------------------------*/
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...
void bulk(std::size_t n, F &&f)
the calls by the std::for_each of the policy
Definition StdParExecutor.h:183
StdParExecutor(std::size_t p=0)
the executor of the policy, which tells the algorithms of ADS that it has threads (the hardware thre...
Definition StdParExecutor.h:159
std::size_t concurrency(void) const
the number of threads given to the constructor
Definition StdParExecutor.h:165
Definition ADT.h:96