parallel-loops-openmp
Table of Contents
Parallel loops (OpenMP)
The most common parallel pattern in C is a loop over independent array elements: each iteration writes to a different output location and doesn't read results that other iterations produce.
for (int i = 0; i < N; i++) a[i] = i * 0.5;
Parallel loops in OpenMP parallelise exactly this pattern. Adding #pragma omp parallel for above the loop splits the iteration range across all threads, each thread writing to its own slice of the array. No synchronisation is needed because the iterations are independent.
#pragma omp parallel for for (int i = 0; i < N; i++) { a[i] = i * 0.5; }
The directive requires the loop to have a countable iteration space and no data dependencies between iterations. Writing to a[i] must not depend on a[i-1] or any other a[j]. Loops with such dependencies require a different strategy (e.g. a scan) or explicit synchronisation.
parallel-loops-openmp.txt ยท Last modified: by 127.0.0.1
