Using OpenMP*

Using OpenMP* in your application requires several steps. To use OpenMP, you must do the following:

  1. Add OpenMP directives to your application source code.

  2. Compile the application with -openmp (Linux* and Mac OS* X) or /Qopenmp (Windows*) option.

Additionally, you can set environment variables for the multi-threaded code execution.

Add OpenMP Support to the Application

Add the OpenMP API routine declarations to your application by adding a statement similar to the following in your code:

Example

include 'omp_lib.h'

or (depending on the Fortran language level)

use omp_lib

See the samples files mentioned in OpenMP* Sample for the example syntax needed to include the runtime libraries.

OpenMP Directive Syntax

OpenMP directives use a specific format and syntax. Intel Extension Routines to OpenMP* describes the OpenMP extensions to the specification that have been added to the Intel® compiler.

The following syntax illustrates using the directives in your source.

Syntax

<prefix> <directive> [<clause>[[,]<clause>...]]

where:

The directives are interpreted as comments if you omit the -openmp (Linux and Mac OS X) or /Qopenmp (Windows*) option.

The OpenMP constructs defining a parallel region have one of the following syntax forms:

Example

!$OMP <directive>

  <structured block of code>

!$OMP END <directive>

or

!$OMP <directive>

  <structured block of code>

or

!$OMP <directive>

The following example demonstrates one way of using an OpenMP directive to parallelize a loop.

Example

subroutine simple_omp(a, N)

  use omp_lib

  integer :: N, a(N)

!$OMP PARALLEL DO

  do i = 1, N

    a(i) = i*2

  end do

end subroutine simple_omp

See OpenMP* Examples for more examples on using directives in specific circumstances.

Compile the Application

The -openmp (Linux* and Mac OS* X) or /Qopenmp (Windows*) option enables the parallelizer to generate multi-threaded code based on the OpenMP directives in the source. The code can be executed in parallel on single processor, multi-processor, and multi-core processor systems.

Note

IA-64 Architecture: Specifying this option implies -opt-mem-bandwith1 (Linux and Mac OS X) or /Qopt-mem-bandwidth1 (Windows).

The openmp option works with both -O0 (Linux and Mac OS X) and /Od (Windows) and with any optimization level of -O1, -O2 and -O3 (Linux and Mac OS X) or /O1, /O2 and /O3 (Windows).

Specifying -O0 (Linux and Mac OS X) or /Od (Windows) with the OpenMP option helps to debug OpenMP applications.

Compile your application using commands similar to those shown below:

Platform

Description

Linux and Mac OS X

ifort -openmp source_file

Windows

ifort /Qopenmp source_file

Assume that you compile the sample above, using the commands similar to the following, where -c (Linux and Mac OS X) or /c (Windows) instructs the compiler to compile the code without generating an executable:

Platform

Example

Linux and Mac OS X

ifort -openmp -c parallel.f90

Windows

ifort /Qopenmp /c parallel.f90

The compiler might return a message similar to the following:

parallel.f90(20) : (col. 6) remark: OpenMP DEFINED LOOP WAS PARALLELIZED.

Configure the OpenMP Environment

Before you run the multi-threaded code, you can set the number of desired threads using the OpenMP environment variable, OMP_NUM_THREADS. See the OpenMP Environment Variables.