OpenMP
By OpenMP Architecture Review Board
OpenMP is an API specification for shared-memory parallel programming in C, C++, and Fortran, implemented as a set of compiler directives, runtime library routines, and environment variables that let a programmer parallelize loops and code…
Definition
OpenMP is an API specification for shared-memory parallel programming in C, C++, and Fortran, implemented as a set of compiler directives, runtime library routines, and environment variables that let a programmer parallelize loops and code regions with minimal changes to sequential source code. It is maintained by the OpenMP Architecture Review Board, a consortium of hardware vendors, compiler makers, and research institutions. OpenMP is one of the most widely used tools for multithreaded parallelism on multicore CPUs in scientific and high-performance computing.
Overview
OpenMP addresses the problem that writing correct, efficient multithreaded code by hand, using low-level thread creation, locking, and synchronization primitives, is tedious and error-prone, especially for the common case of parallelizing loops that operate independently over array elements. Rather than requiring a programmer to restructure a program around explicit thread management, OpenMP lets them annotate existing sequential code with directives (pragmas in C/C++, comments in Fortran) that tell a compliant compiler how to split the work across multiple threads automatically. Mechanically, a developer marks a code region, most commonly a for-loop, with a directive such as `#pragma omp parallel for`, and the compiler generates code that spawns a team of threads, divides the loop's iterations among them according to a scheduling policy, executes them concurrently on separate CPU cores, and synchronizes the threads at the end of the region. Additional directives and clauses handle more complex needs: declaring which variables are shared versus private to each thread, reducing partial results into a single value, protecting critical sections from concurrent access, and controlling how iterations are chunked and assigned to threads. Because parallelism is expressed at the compiler-directive level rather than through manual thread APIs, a program can typically be compiled either with OpenMP support enabled or disabled, falling back to sequential execution. OpenMP sits alongside message-passing approaches like MPI as one of the two dominant parallel programming models in high-performance computing, but the two target different problems: OpenMP parallelizes work across cores that share a single machine's memory, while MPI coordinates separate processes, potentially across many machines, that do not share memory and must communicate explicitly. The two are frequently combined, using MPI to distribute work across a cluster and OpenMP to parallelize within each node. Compared to POSIX Threads, OpenMP operates at a much higher level of abstraction, trading some fine-grained control for dramatically simpler code. In practice, OpenMP is used heavily in scientific and engineering simulation codes, numerical libraries, and any CPU-bound application with loops that can be safely executed out of order, such as matrix operations, image processing kernels, and physics simulations. Most major compilers, including GCC, Clang, and the Intel and Microsoft compilers, support it natively, which has made it a low-friction way to add multicore parallelism to existing C/C++/Fortran codebases without a rewrite. The trade-offs are that OpenMP's shared-memory model does not extend across machine boundaries the way MPI does, incorrect use of shared and private variable clauses can silently introduce race conditions that are hard to debug, and performance gains are capped by the number of cores on a single node and by how much of the code parallelizes cleanly (per Amdahl's Law). Code with heavy data dependencies between iterations often cannot be parallelized this way at all, limiting OpenMP's benefit to workloads with genuinely independent units of work.
Specification
- Compiler directives (pragmas) that parallelize loops with minimal code changes
- Shared-memory threading model for multicore CPU parallelism
- Supports C, C++, and Fortran across major compilers
- Clauses for shared/private variables, reductions, and critical sections
- Compatible fallback to sequential execution when disabled
- Often combined with MPI for hybrid cluster-and-node parallelism
- Maintained by a vendor and research consortium standards body