openmp
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| openmp [June 11, 2026 at 09:35] – Ivan Janevski | openmp [August 22, 2026 at 15:22] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| # OpenMP | # OpenMP | ||
| - | **OpenMP** is a shared-memory parallelism API for C, C++, and Fortran. It lets a single program exploit multiple CPU cores by distributing work across a team of threads that all share the same address space. This is in contrast to distributed-memory models like [[mpi|MPI]], | ||
| - | The execution model is **fork-join**: the program starts as a single master thread. When it hits a `#pragma omp parallel` block, it forks into a team of worker threads; all threads execute the block concurrently, | + | **OpenMP** is a shared-memory parallelism API for C, C++, and Fortran. Add a `#pragma omp` directive before a loop or block to parallelize |
| - | ```c | + | The execution model is **fork-join**: |
| - | #pragma omp parallel | + | |
| - | { | + | |
| - | int tid = omp_get_thread_num(); | + | |
| - | int nthreads = omp_get_num_threads(); | + | |
| - | printf(" | + | |
| - | } | + | |
| - | ``` | + | |
| - | + | ||
| - | The number of threads | + | |
| - | + | ||
| - | Adding more threads does not always mean proportionally faster code. [[amdahls-law|Amdahl' | + | |
| - | + | ||
| - | ## Concepts | + | |
| - | + | ||
| - | 1. [[data-sharing-openmp|Data sharing]] | + | |
| - | 2. [[parallel-loops-openmp|Parallel loops]] | + | |
| - | 3. [[collapse-openmp|Collapse]] | + | |
| - | 4. [[reduction-openmp|Reduction]] | + | |
| - | 5. [[scheduling-openmp|Scheduling]] | + | |
| - | 6. [[simd-openmp|SIMD]] | + | |
| - | 7. [[tasks-openmp|Tasks]] | + | |
| - | 8. [[single-openmp|Single]] | + | |
| - | 9. [[master-openmp|Master]] | + | |
| - | 10. [[sections-openmp|Sections]] | + | |
| - | 11. [[barrier-openmp|Barrier]] | + | |
| - | 12. [[nowait-openmp|Nowait]] | + | |
| - | 13. [[critical-sections-openmp|Critical sections]] | + | |
| - | 14. [[atomic-openmp|Atomic]] | + | |
| - | 15. [[flush-openmp|Flush]] | + | |
| - | 16. [[false-sharing-openmp|False sharing]] | + | |
| - | 17. [[thread-affinity-openmp|Thread affinity]] | + | |
| - | 18. [[time-measurement-openmp|Time measurement]] | + | |
| - | + | ||
| - | ## Overview | + | |
| - | + | ||
| - | ### Directives | + | |
| ```c | ```c | ||
| - | #pragma omp parallel | + | // compile: gcc -O2 -fopenmp -o sum sum.c |
| - | #pragma omp parallel for // distribute loop iterations across the team | + | // run: OMP_NUM_THREADS=4 ./sum |
| - | #pragma omp parallel for reduction(+:s) // loop with a parallel reduction | + | // description: |
| - | #pragma omp parallel sections | + | |
| - | #pragma omp section | + | |
| - | #pragma omp single | + | |
| - | #pragma | + | |
| - | #pragma omp task // package work for any idle thread to execute | + | |
| - | #pragma omp taskwait | + | |
| - | #pragma omp barrier | + | |
| - | #pragma omp critical | + | |
| - | #pragma omp atomic | + | |
| - | #pragma omp simd // assert the loop is safe to vectorise | + | |
| - | #pragma omp flush // enforce memory visibility across threads | + | |
| - | ``` | + | |
| - | ### Functions | + | #include < |
| + | #include < | ||
| - | ```c | + | int main(void) { |
| - | omp_get_thread_num() // ID of the calling thread (0 … N-1) | + | long n = 1000000000L, |
| - | omp_get_num_threads() // number of threads in the current team | + | |
| - | omp_get_max_threads() // threads that would be used if a parallel region started now | + | # |
| - | omp_set_num_threads(n) // set the default thread count at runtime | + | |
| - | omp_get_num_procs() | + | sum += i; |
| - | omp_get_wtime() | + | |
| - | omp_in_parallel() | + | |
| + | } | ||
| ``` | ``` | ||
| - | ### Environment variables | + | On a 4-core machine this runs roughly 4× faster with `-fopenmp` than without. [[amdahls-law|Amdahl' |
| - | ^ Variable ^ Default ^ Description ^ | + | ## Concepts |
| - | | `OMP_NUM_THREADS` | core count | Number of threads to use in each parallel region | | + | |
| - | | `OMP_SCHEDULE` | `static` | Default schedule kind and optional chunk size, e.g. `dynamic,4` | | + | |
| - | | `OMP_PROC_BIND` | `false` | Thread-to-core affinity policy: `close`, `spread`, or `master` | | + | |
| - | | `OMP_PLACES` | (unset) | Placement units for affinity: `cores`, `threads`, or `sockets` | | + | |
| - | | `OMP_MAX_ACTIVE_LEVELS` | `1` | Maximum nesting depth of simultaneously active parallel regions | | + | |
| - | | `OMP_DISPLAY_ENV` | `false` | Print OpenMP version and active settings at startup: `TRUE` or `VERBOSE` | | + | |
| + | 1. [[openmp-data-sharing|Data sharing]] | ||
| + | 2. [[openmp-parallel-loops|Parallel loops]] | ||
| + | 3. [[openmp-collapse|Collapse]] | ||
| + | 4. [[openmp-reduction|Reduction]] | ||
| + | 5. [[openmp-scheduling|Scheduling]] | ||
| + | 6. [[openmp-simd|SIMD]] | ||
| + | 7. [[openmp-tasks|Tasks]] | ||
| + | 8. [[openmp-single|Single]] | ||
| + | 9. [[openmp-master|Master]] | ||
| + | 10. [[openmp-sections|Sections]] | ||
| + | 11. [[openmp-barrier|Barrier]] | ||
| + | 12. [[openmp-nowait|Nowait]] | ||
| + | 13. [[openmp-critical-sections|Critical sections]] | ||
| + | 14. [[openmp-atomic|Atomic]] | ||
| + | 15. [[openmp-flush|Flush]] | ||
| + | 16. [[openmp-false-sharing|False sharing]] | ||
| + | 17. [[openmp-thread-affinity|Thread affinity]] | ||
| + | 18. [[openmp-time-measurement|Time measurement]] | ||
| + | 19. [[openmp-overview|Overview]] | ||
| - | ## Info | ||
| - | *This article is synthetic* | ||
| - | Up: [[home]] | ||
openmp.1781170537.md.gz · Last modified: by Ivan Janevski
