Add OpenMP* Support

To add OpenMP* support to your application, do the following:

  1. Add the appropriate OpenMP directives to your source code.

  2. Compile the application with the /Qopenmp (Windows*) or -qopenmp (Linux* and macOS*) option.

  3. For applications with large local or temporary arrays, you may need to increase the stack space available at runtime. In addition, you may need to increase the stack allocated to individual threads by using the OMP_STACKSIZE environment variable or by setting the corresponding library routines.

You can set other environment variables to control multi-threaded code execution.

OpenMP Directive Syntax

To add OpenMP support to your application, first add appropriate OpenMP directives to your source code.

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® Fortran Compiler.

The following syntax illustrates using the directives in your source.

Example

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

where:

The directives are interpreted as comments if you omit the /Qopenmp (Windows) or -qopenmp (Linux and macOS*) 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

Compile the Application

The /Qopenmp (Windows) or -qopenmp (Linux and macOS*) 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, or multi-core processor systems.

The /Qopenmp (Windows) or -qopenmp (Linux and macOS*) option works with both -O0 (Linux and macOS*) and /Od (Windows*) and with any optimization level of O1, O2 and O3.

Specifying -O0 (Linux and macOS*) or /Od (Windows) with the /Qopenmp (Windows) or -qopenmp (Linux and macOS*) option helps to debug OpenMP applications.

Compile your application using commands similar to those shown below:

Operating System

Syntax Example

Linux

ifort -qopenmp source_file

macOS*

ifort -qopenmp source_file

Windows

ifort /Qopenmp source_file

Assume that you compile the sample above, using commands similar to the following, where the c option instructs the compiler to compile the code without generating an executable:

Operating System

Extended Syntax Example

Linux

ifort -qopenmp -c parallel.f90

macOS*

ifort -qopenmp -c parallel.f90

Windows

ifort /Qopenmp /c parallel.f90

To build your application with target offload support (introduced since OpenMP 4.0) use compiler options to specify the target for which the regions marked with OpenMP "target" directives must be compiled. For example:

Operating System

Syntax Example

Linux

ifx -qopenmp -fopenmp-targets=spir64 offload.f90

macOS*

ifx -qopenmp -fopenmp-targets=spir64 offload.f90

Windows

ifx /Qopenmp /Qopenmp-targets:spir64 offload.f90

Refer to Get Started with OpenMP* Offload to GPU for the Intel® oneAPI DPC/C++ Compiler and Intel® Fortran Compiler for more information.

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 Also