OpenMP is a portable thread-based programming model, whereby a single thread of execution (referred to as the initial thread) can spawn (or fork) additional threads to perform work in parallel. Ideally each thread of execution is mapped onto an individual processing core for maximum performance. Each thread (within a thread team) is uniquely identified by its thread ID and individual threads can communicate with each other through shared memory resources.
OpenMP is not a programming language but rather is a specification that enables shared-memory parallelism, in base languages such as Fortran and C, through the following components:
- Compiler Directives
- Runtime Library Routines
- Environment Variables
Figure 1 shows a typical OpenMP thread-based execution whereby the initial thread creates a parallel region to perform some work in parallel. Within the parallel region a team of n+1 threads are forked (spawned) and assigned individual tasks to complete. After the final task is complete, the threads are synchronised (joined) and serial execution continues with the initial thread.
The most fundamental OpenMP operations can be classified into three (3) groups:
- Invoking a Parallel Region
!$omp parallel private(var1,var2,...),shared(var1,var2,...)
... code block ...
!$omp end parallel
#pragma omp parallel private(var1,var2,...),shared(var1,var2,...)
{
... code block ...
}
Variables that are in scope at the invocation of a parallel region must be declared either private or shared within the ensuing parallel region. If a variable is designated as private, then all threads within the parallel region will create an individual instance of the variable, and can modify it without any conflict from other threads. A variable that is designated as shared remains shared among all threads, and needs to be accessed carefully to avoid race conditions.
- Querying Thread ID and Team Size (within the current parallel region)
integer :: id, threads
id=OMP_GET_THREAD_NUM() ! returns ID of this thread
threads=OMP_GET_NUM_THREADS() ! returns the number of threads in parallel region
int id, threads;
id=OMP_GET_THREAD_NUM(); // returns ID of this thread
threads=OMP_GET_NUM_THREADS(); // returns the number of threads in parallel region
- Loop-level Parallelism (splitting loop iterations over threads)
!$omp parallel do private(var1,...),shared(var1,...)
... do loop ...
!$omp end parallel do
#pragma omp parallel for private(var1,...),shared(var1,...)
... for loop ...
For more detailed information on the OpenMP standard, download and review the latest OpenMP
specification.