4

I've been getting into standard algorithms and noticed that you can specify an execution policy.

The documentation found here lists 4 policies:

std::execution::sequenced_policy  
std::execution::parallel_policy 
std::execution::parallel_unsequenced_policy  
std::execution::unsequenced_policy

I understand the difference between parallel policies and sequenced ones, but I'm not sure why you'd also need unsequenced and unsequenced parallel?

From what I know you can't guarantee thread execution order anyway so wouldn't that make the parallel and parallel unsequenced policies identical?

Why add a normal unsequenced policy when if you can't parallelise the policy automatically falls back to a sequential one?

What am I missing?

Tony Tannous
  • 14,154
  • 10
  • 50
  • 86
Edward
  • 468
  • 4
  • 18

1 Answers1

7

Table Visualization

-------------------------------------------------------------------------
|   Type                  |     Vectorization     |     Parallelization |
|------------------------------------------------------------------------                
| Sequenced               |           X           |           X         | 
| Unsequenced             |           V           |           X         |
| Parallel                |           X           |           V         |
| Parallel & unsequenced  |           V           |           V         |
-------------------------------------------------------------------------

From latest C++20 working draft n4849

20.18 Execution policies
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.

using namespace std;
vector<int> v = /* ... */;

// standard sequential sort
sort(v.begin(), v.end());

// explicitly sequential sort
sort(execution::seq, v.begin(), v.end());

// permitting parallel execution
sort(execution::par, v.begin(), v.end());

// permitting vectorization as well
sort(execution::par_unseq, v.begin(), v.end());

§ 20.18.3 Execution policy type trait

  • 20.18.4 Sequenced execution policy [execpol.seq]

    1. The class execution::sequenced_policy is an execution policy type used as a unique type to disambiguate parallel algorithm overloading and require that a parallel algorithm’s execution may not be parallelized.

  • 20.18.5 Parallel execution policy [execpol.par]

    1. The class execution::parallel_policy is an execution policy type used as a unique type to disambiguate parallel algorithm overloading and indicate that a parallel algorithm’s execution may be parallelized.

  • 20.18.6 Parallel and unsequenced execution policy [execpol.parunseq]

    1. The class execution::parallel_unsequenced_policy is an execution policy type used as a unique type to disambiguate parallel algorithm overloading and indicate that a parallel algorithm’s execution may be parallelized and vectorized.

  • 20.18.7 Unsequenced execution policy [execpol.unseq]

    1. The class unsequenced_policy is an execution policy type used as a unique type to disambiguate parallel algorithm overloading and indicate that a parallel algorithm’s execution may be vectorized, e.g., executed on a single thread using instructions that operate on multiple data items.

Further reading

Tony Tannous
  • 14,154
  • 10
  • 50
  • 86