CRC Wiki
CRC Wiki
Log in

OpenMP

From CRC Wiki

General Description

OpenMP is an Application Program Interface (API), jointly defined by a group of major computer hardware and software vendors. OpenMP provides a portable, scalable model for developers of shared memory parallel applications. The API supports C/C++ and Fortran on a wide variety of architectures.

OpenMP Is: 

    * An Application Program Interface (API) that may be used to explicitly direct multi-threaded, shared memory parallelism
    * Comprised of three primary API components:
          o Compiler Directives
          o Runtime Library Routines
          o Environment Variables 
    * Portable:
          o The API is specified for C/C++ and Fortran
          o Most major platforms have been implemented including Unix/Linux platforms and Windows NT 
    * Standardized:
          o Jointly defined and endorsed by a group of major computer hardware and software vendors 
    * What does OpenMP stand for?
          o Short version: Open Multi-Processing
          o Long version: Open specifications for Multi-Processing via collaborative work between interested parties from the hardware and software industry, government and academia. 

Basic Usage

Example OpenMP Code Structure:

Fortran:

       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 
             .
             .
             .

       All threads join master thread and disband 

!$OMP END PARALLEL

       Resume serial code 
             .
             .
             .

       END


C/C++:

      #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 
              .
              .
              .

        All threads join master thread and disband 

        }  

      Resume serial code 
            .
            .
            .

      }

Useful Options

Set # of threads for OpenMP:

setenv OMP_NUM_THREADS 8

in csh,

export OMP_NUM_THREADS=8

in bash.
 
Compiler	                        Compiler Options	Default behavior for # of threads (OMP_NUM_THREADS not set)

GNU (gcc, g++, gfortran)	        -fopenmp        	as many threads as available cores
Intel (icc ifort)	                -openmp	                as many threads as available cores
Portland Group (pgcc,pgCC,pgf77,pgf90)	-mp	                one thread

Further Information

See the official website: OpenMP