OpenMP API Overview

Three Components:

The OpenMP API is comprised of three distinct components. As of version 4.0:

The application developer decides how to employ these components. In the simplest case, only a few of them are needed.

Implementations differ in their support of all API components. For example, an implementation may state that it supports nested parallelism, but the API makes it clear that may be limited to a single thread - the master thread. Not exactly what the developer might expect?

Compiler Directives:

Compiler directives appear as comments in your source code and are ignored by compilers unless you tell them otherwise - usually by specifying the appropriate compiler flag, as discussed in the Compiling section later.

OpenMP compiler directives are used for various purposes:

Compiler directives have the following syntax:

sentinel       directive-name      [clause, ...]

For example:

Fortran !$OMP PARALLEL DEFAULT(SHARED) PRIVATE(BETA,PI)
C/C++ #pragma omp parallel default(shared) private(beta,pi)

Compiler directives are covered in detail later.

Run-time Library Routines:

The OpenMP API includes an ever-growing number of run-time library routines.

These routines are used for a variety of purposes:

For C/C++, all of the run-time library routines are actual subroutines. For Fortran, some are actually functions, and some are subroutines. For example:

Fortran INTEGER FUNCTION OMP_GET_NUM_THREADS()
C/C++ #include <omp.h>
int omp_get_num_threads(void)

Note that for C/C++, you usually need to include the <omp.h> header file.

Fortran routines are not case sensitive, but C/C++ routines are.

The run-time library routines are briefly discussed as an overview in the Run-Time Library Routines section, and in more detail in Appendix A.

Environment Variables:

OpenMP provides several environment variables for controlling the execution of parallel code at run-time.

These environment variables can be used to control such things as:

Setting OpenMP environment variables is done the same way you set any other environment variables, and depends upon which shell you use. For example:

csh/tcsh setenv OMP_NUM_THREADS 8
sh/bash export OMP_NUM_THREADS=8

OpenMP environment variables are discussed in the Environment Variables section later.

Example OpenMP Code Structure:

Fortran - General Code Structure

       PROGRAM HELLO

       INTEGER VAR1, VAR2, VAR3

       Serial code 
             .
             .
             .

       Beginning of parallel section. Fork a team of threads. 
       Specify variable scoping 

!$OMP PARALLEL PRIVATE(VAR1, VAR2) SHARED(VAR3)

       Parallel section executed by all threads 
                 .
       Other OpenMP directives
                 .
       Run-time Library calls
                 .
       All threads join master thread and disband 

!$OMP END PARALLEL

       Resume serial code 
             .
             .
             .

       END

C / C++ - General Code Structure

#include <omp.h>

main ()  {

int var1, var2, var3;

Serial code 
      .
      .
      .

Beginning of parallel section. Fork a team of threads.
Specify variable scoping 

#pragma omp parallel private(var1, var2) shared(var3)
{

Parallel section executed by all threads 
                 .
Other OpenMP directives
                 .
Run-time Library calls
                 .
All threads join master thread and disband

 }  

Resume serial code 
      .
      .
      .

 }