| Both sides previous revisionPrevious revision | |
| openmp [June 11, 2026 at 11:03] – Ivan Janevski | openmp [June 11, 2026 at 11:03] (current) – Ivan Janevski |
|---|
| **OpenMP** is a shared-memory parallelism API for C, C++, and Fortran. | **OpenMP** is a shared-memory parallelism API for C, C++, and Fortran. |
| |
| If you have a loop that takes too long and you want it to use all the cores on your machine instead of just one, OpenMP is usually the shortest path there. It works via compiler directives (`#pragma omp` in C/C++), a small runtime library (`libomp`), and a set of environment variables. In contrast to distributed-memory models like [[mpi|MPI]], where each process has its own memory, OpenMP threads all share the same address space — you get parallelism without touching your data layout. | If you have a loop that takes too long and you want it to use all the cores on your machine instead of just one, OpenMP is usually the shortest path there. It works via compiler directives (`#pragma omp` in C/C++), a small runtime library (`libomp`), and a set of environment variables. In contrast to distributed-memory models like [[mpi|MPI]], where each process has its own memory, OpenMP threads all share the same address space. You get parallelism without touching your data layout. |
| |
| The execution model is **fork-join**: the program starts as a single thread. When it hits a `#pragma omp parallel` block, it forks into a team of worker threads that all execute the block concurrently, then join back into one thread at the closing brace. You do not write any thread creation, management, or teardown code — the compiler inserts all of that. If the compiler does not support OpenMP, it silently ignores all `#pragma omp` directives and the program runs serially, which is useful for debugging: drop `-fopenmp` from the compile command and you get a clean serial build without changing a line of source. | The execution model is **fork-join**: the program starts as a single thread. When it hits a `#pragma omp parallel` block, it forks into a team of worker threads that all execute the block concurrently, then join back into one thread at the closing brace. You do not write any thread creation, management, or teardown code — the compiler inserts all of that. If the compiler does not support OpenMP, it silently ignores all `#pragma omp` directives and the program runs serially, which is useful for debugging: drop `-fopenmp` from the compile command and you get a clean serial build without changing a line of source. |