C++ - Parallel Algorithms & Execution Policies

Overview

Estimated time: 50–70 minutes

Speed up algorithms using execution policies: sequential, parallel, and parallel+vectorized. Learn constraints and safety rules.

Learning Objectives

  • Use std::execution::par and par_unseq with standard algorithms.
  • Understand when operations are safe under parallel policies.

Prerequisites

Examples

#include 
#include 
#include 
#include 
#include 
int main(){
  std::vector v(1'000'000, 1);
  auto sum = std::reduce(std::execution::par, v.begin(), v.end());
  std::cout << sum << "\n";
}
#include 
#include 
#include 
#include 
int main(){
  std::vector v(1000000);
  std::transform(std::execution::par_unseq, v.begin(), v.end(), v.begin(), [](double x){ return std::sin(x); });
}

Common Pitfalls

  • Algorithms must not introduce data races; lambdas must be side-effect free or properly synchronized.
  • par_unseq assumes no data dependencies and can reorder/vectorize operations; ensure it’s safe.

Checks for Understanding

  1. What’s the difference between par and par_unseq?
  2. Why should functions be pure under parallel policies?
Show answers
  1. par runs in parallel across threads; par_unseq may also vectorize and reorder.
  2. To avoid data races and ensure determinism unless non-determinism is acceptable.

Exercises

  1. Use std::transform_reduce with execution::par to compute dot product of two large vectors.
  2. Benchmark seq vs par on a CPU-bound transformation.