Site Tools


openmp

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
openmp [June 11, 2026 at 09:14] – external edit 127.0.0.1openmp [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]], where each process has its own memory. OpenMP is implemented via compiler directives (`#pragma omp` in C/C++), a small runtime library (`libomp`), and a set of environment variables. 
  
-The execution model is **fork-join**: the program starts as a single master thread. When it hits a `#pragma omp parallel` blockit forks into a team of worker threads; all threads execute the block concurrently, then join back into one thread at the closing braceThe compiler inserts the thread creationsynchronization, and teardown code. The programmer only writes directives. If the compiler does not support OpenMP, it silently ignores all `#pragma ompdirectives and the program runs serially, which is a useful property during debugging.+**OpenMP** is a shared-memory parallelism API for C, C++, and Fortran. Add a `#pragma omp` directive before a loop or block to parallelize it across available cores. The compiler handles thread creation and synchronization; if it doesn't support OpenMP, directives are silently ignored and the program runs serially, making debugging easy. 
 + 
 +The execution model is **fork-join**: a single thread forks into worker team at `#pragma omp parallel`, they execute concurrently, then join back. Thread count defaults to the number of logical coresconfigurable via `OMP_NUM_THREADS=N` at runtime or `omp_set_num_threads(n)` in code. Compile with `-fopenmp` and include `<omp.h>` for the `omp_*` runtime functions.
  
 ```c ```c
-#pragma omp parallel +// compile: gcc -O2 -fopenmp -o sum sum.c 
-+// run: OMP_NUM_THREADS=4 ./sum 
-    int tid omp_get_thread_num(); +// description: parallel reduction using a single pragma 
-    int nthreads = omp_get_num_threads(); + 
-    printf("thread %d of %d\n", tidnthreads);+#include <omp.h> 
 +#include <stdio.h> 
 + 
 +int main(void) 
 +    long n = 1000000000L, sum = 0; 
 +    double t omp_get_wtime(); 
 +    #pragma omp parallel for reduction(+:sum) 
 +    for (long i = 0; i < n; i++) 
 +        sum += i
 +    printf("sum=%ld  time=%.2fs\n", sumomp_get_wtime(- t); 
 +    return 0;
 } }
 ``` ```
  
-The number of threads defaults to the number of logical CPU cores. It can be overridden with the `OMP_NUM_THREADS` environment variable or the `num_threads(N)` clause on the pragma. Output order is non-deterministic — threads are scheduled by the OS. Compile with `-fopenmp` and include `<omp.h>` to use the `omp_*` runtime functions. +On a 4-core machine this runs roughly 4× faster with `-fopenmp` than without. [[amdahls-law|Amdahl's law]] limits speedup when part of the program is inherently serialreducing overhead like false sharing and load imbalance matters more than just adding threads.
- +
-Adding more threads does not always mean proportionally faster code. [[amdahls-law|Amdahl's law]] states that if a fraction $s$ of the program is inherently serial, the maximum possible speedup is $1/s$ regardless of how many threads are used. A loop that accounts for 80% of runtime can at best give 5× speedup no matter how many cores are available. This makes reducing the serial fraction more impactful than simply increasing thread count. Eliminating overhead sources like false sharingload imbalance, and unnecessary synchronisation compounds that further.+
  
 ## Concepts ## Concepts
  
- 1. [[data-sharing-openmp|Data sharing]] + 1. [[openmp-data-sharing|Data sharing]] 
- 2. [[parallel-loops-openmp|Parallel loops]] + 2. [[openmp-parallel-loops|Parallel loops]] 
- 3. [[collapse-openmp|Collapse]] + 3. [[openmp-collapse|Collapse]] 
- 4. [[reduction-openmp|Reduction]] + 4. [[openmp-reduction|Reduction]] 
- 5. [[scheduling-openmp|Scheduling]] + 5. [[openmp-scheduling|Scheduling]] 
- 6. [[simd-openmp|SIMD]] + 6. [[openmp-simd|SIMD]] 
- 7. [[tasks-openmp|Tasks]] + 7. [[openmp-tasks|Tasks]] 
- 8. [[single-openmp|Single]] + 8. [[openmp-single|Single]] 
- 9. [[master-openmp|Master]] + 9. [[openmp-master|Master]] 
- 10. [[sections-openmp|Sections]] + 10. [[openmp-sections|Sections]] 
- 11. [[barrier-openmp|Barrier]] + 11. [[openmp-barrier|Barrier]] 
- 12. [[nowait-openmp|Nowait]] + 12. [[openmp-nowait|Nowait]] 
- 13. [[critical-sections-openmp|Critical sections]] + 13. [[openmp-critical-sections|Critical sections]] 
- 14. [[atomic-openmp|Atomic]] + 14. [[openmp-atomic|Atomic]] 
- 15. [[flush-openmp|Flush]] + 15. [[openmp-flush|Flush]] 
- 16. [[false-sharing-openmp|False sharing]] + 16. [[openmp-false-sharing|False sharing]] 
- 17. [[thread-affinity-openmp|Thread affinity]] + 17. [[openmp-thread-affinity|Thread affinity]] 
- 18. [[time-measurement-openmp|Time measurement]] + 18. [[openmp-time-measurement|Time measurement]] 
- + 19[[openmp-overview|Overview]]
-## Overview +
- +
-### Directives +
- +
-```c +
-#pragma omp parallel                         // fork a team of threads; join at closing brace +
-#pragma omp parallel for                     // distribute loop iterations across the team +
-#pragma omp parallel for reduction(+:s)     // loop with a parallel reduction +
-#pragma omp parallel sections                // distribute independent blocks across the team +
-#pragma omp section                          // one block inside a sections region +
-#pragma omp single                           // one thread runs the block; others wait at end +
-#pragma omp master                           // thread 0 only; no implicit barrier +
-#pragma omp task                             // package work for any idle thread to execute +
-#pragma omp taskwait                         // wait for all child tasks to finish +
-#pragma omp barrier                          // all threads wait until every thread arrives +
-#pragma omp critical                         // mutual exclusion — one thread at a time +
-#pragma omp atomic                           // single hardware-atomic read-modify-write +
-#pragma omp simd                             // assert the loop is safe to vectorise +
-#pragma omp flush                            // enforce memory visibility across threads +
-``` +
- +
-### Functions +
- +
-```c +
-omp_get_thread_num()      // ID of the calling thread (0 … N-1) +
-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()       // number of logical processors available to the program +
-omp_get_wtime()           // wall-clock time in seconds; use for timing parallel regions +
-omp_in_parallel()         // 1 if called from inside a parallel region, 0 otherwise +
-``` +
- +
-### Environment variables +
- +
-^ Variable ^ Default ^ Description ^ +
-| `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` |+
  
openmp.1781169250.md.gz · Last modified: by 127.0.0.1