collapse-openmp
**This is an old revision of the document!**
Table of Contents
Collapse (OpenMP)
Collapse is an OpenMP clause that merges multiple perfectly-nested loops into a single flat iteration space before distributing work across threads. When a loop nest has too few outer iterations to keep all threads busy, distributing only the outer loop leaves most threads idle. collapse(N) multiplies the available parallelism by treating the combined iteration space as one loop. “Perfectly nested” means no statements appear between the loop headers.
#pragma omp parallel for collapse(2) for (int i = 0; i < M; i++) { for (int j = 0; j < N; j++) { a[i][j] = b[i][j] * c[i][j]; } }
Without collapse(2), a 4-thread run on a loop with M = 2 would use at most 2 threads. With it, the iteration space is M * N, and all 4 threads stay busy. The combined iteration space is split and scheduled according to the schedule clause as usual.
collapse-openmp.1781163760.txt.gz · Last modified: by 127.0.0.1
