09 OpenMP Intro
09 OpenMP Intro
/
Processes and threads
Serial
region
Serial Serial
region region
Parallel processes Parallel region Parallel region
Process Thread
Independent execution units A single process may contain multiple
Have their own state information and threads
own memory address space Have their own state information, but
share the same memory address space
/
Processes and threads
Serial
region
Serial Serial
region region
Parallel processes Parallel region Parallel region
Process Thread
Long-lived: spawned when parallel Short-lived: created when entering a
program started, killed when program parallel region, destroyed (joined)
is finished when region ends
Explicit communication between Communication through shared
processes memory
/
OpenMP
/
What is OpenMP?
/
Why would you want to learn OpenMP?
/
Three components of OpenMP
/
OpenMP directives
Fortran:
!$omp directive [clauses]
/
Compiling an OpenMP program
Compilers that support OpenMP usually require an option that enables the feature
GNU: -fopenmp
Intel: -qopenmp
Cray: -h omp
OpenMP enabled by default, -h noomp disables
PGI: -mp[=nonuma,align,allcores,bind]
Without these options a serial version is compiled!
/
Parallel construct
Fortran:
!$omp parallel [clauses]
structured block
!$omp end parallel
Each thread executes the same code within the parallel region
OpenMP provides several constructs for controlling work distribution
Loop construct
Single/Master construct
Sections construct
Task construct
Workshare construct (Fortran only)
Thread id can be queried and used for distributing work manually (similar to MPI
rank)
/
Loop construct
!$omp do [clauses]
...
!$omp end do
in C/C++ limited only to "canonical" for-loops. Iterator base loops are also possible in
C++
Construct must reside inside a parallel region
Combined construct with omp parallel:
#pragma omp parallel for / !$omp parallel do
/
Loop construct
!$omp parallel
!$omp do
do i = 1, n
z(i) = x(i) + y(i)
end do
!$omp end do
!$omp end parallel
/
Summary