Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++17: how to control number of threads in execution policy?

Tags:

c++

c++17

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.

like image 238
anymous.asker Avatar asked May 01 '19 19:05

anymous.asker


1 Answers

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.

like image 70
L. F. Avatar answered Nov 17 '22 16:11

L. F.