🚀Go deeper — read the bookHigh-Performance Computing in C++— runnable code & full walkthrough →

hpc · high

OpenMP: shared-memory parallelism

OpenMP is the standard for shared-memory parallelism in C/C++: compiler pragmas fork a team of threads, split a loop's iterations across cores, and join at the end — no manual thread management. The classic bug is updating a shared accumulator inside the loop (sum += x), a data race that loses updates and gives a different wrong answer each run; the fix is reduction(+:sum), which gives each thread a private partial that the runtime combines once. Scheduling matters for load balance: schedule(static) splits equal contiguous chunks (best for uniform work) while schedule(dynamic) hands out chunks on demand to handle uneven iteration cost. Build with -fopenmp; the same source compiles serially without it.

🔑 Key line

OpenMP parallelizes shared-memory loops with one pragma; updating a shared accumulator is a data race — use reduction(+:sum) for private partials combined once, and switch from static to dynamic scheduling when iteration cost is uneven.

The code

#include <omp.h>
double sum = 0.0;
#pragma omp parallel for reduction(+:sum) // split the loop across threads
for (int i = 0; i < n; ++i)
sum += heavy(a[i]); // each thread owns a chunk
// schedule(static) : equal contiguous chunks (default, low overhead)
// schedule(dynamic) : grab work on demand -> good for uneven iterations
// compile with: g++ -O2 -fopenmp

What this lesson walks through

  1. 01One pragma turns a loop parallel
  2. 02The trap: a shared accumulator is a data race
  3. 03reduction: private partials, combined once
  4. 04Load balance: static vs dynamic scheduling

OpenMP parallelizes shared-memory code with compiler pragmas — no thread plumbing. '#pragma omp parallel for' splits the loop's iterations across a team of threads (one per core by default). The runtime forks the team, hands each thread a slice, and joins at the end of the loop.

See it animated — step by step, at your own pace

Unlock the full interactive walkthrough of OpenMP: shared-memory parallelism and 100+ animated C++ interview lessons.

← Previous
SIMD & Auto-Vectorization
Next →
Parallel STL (std::execution)