The C++17 standard introduced an execution policy parameter (e.g. std::execution::par_unseq
) which can be passed to some of the functions in the std
library to make them execute in parallel, e.g.:
std::copy(std::execution::par_unseq, obj1.begin(), obj1.end(), obj2.begin())
In other frameworks like OpenMP, it’s possible to set the maximum number of threads that it will use, e.g. #pragma omp parallel num_threads(<desired_numer>)
to set it locally within the section, or omp_set_num_threads(<desired_number>)
to set it within the calling scope.
I’m wondering how can this be achieved in standard C++ for the execution policies.
This is a good question. That said, unfortunately, I don't think it's possible. [execpol.general]/1 says:
This subclause describes classes that are execution policy types. An object of an execution policy type indicates the kinds of parallelism allowed in the execution of an algorithm and expresses the consequent requirements on the element access functions.
(emphasis mine)
Moreover, after that, the whole [execpol] is dealing about is_execution_policy
, (disambiguator) policy types, and the execution policy objects.
In other words, execution policies only bring the possibility of parallelism at the cost of constrained element access functions. It is not really specified how these policies are carried out. To me, it seems even less possible to control the details of parallelism, with the number of threads being an example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With