Developer Guide and Reference

ID 767251
Date 10/31/2024
Public
Document Table of Contents

Add OpenMP* Support

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

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

  2. Add "USE omp_lib", which contains definitions of predefined OpenMP constants, kind type values, derived types, and interfaces for OpenMP runtime library routines.

  3. Compile the application with option -qopenmp (Linux*) or /Qopenmp (Windows*) to enable recognition of OpenMP parallel and loop transformation directives.

  4. Compile the application with options -qopenmp and -qopenmp-targets (Linux) or options /Qopenmp and /Qopenmp-targets (Windows) to enable recognition of offloading and data mapping directives to offload to a specified device.

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

To use directives in your source, use this syntax:

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

where:

  • <prefix> - Required for all OpenMP directives. For free format source input, the prefix is !$OMP only; for fixed format source input, the prefix is !$OMP, *$OMP, or C$OMP. Directives that are extensions to OpenMP have a free format prefix of !$OMPX and fixed format prefixes of !$OMX, C$OMX, or *$OMX.

  • <directive> - A valid OpenMP directive. Must immediately follow the prefix; for example: !$OMP PARALLEL.

  • [<clause>] - Optional. Clauses can be in any order and repeated as necessary, unless otherwise restricted.

  • <newline> - A required component of directive syntax. It precedes the structured block that is enclosed by this directive.

  • [,]: Optional. Commas between more than one <clause> are optional.

The directives are interpreted as comments if you omit the /Qopenmp (Windows) or -qopenmp (Linux) option.

The OpenMP constructs have one of the following syntax forms:

!$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:

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

Options -qopenmp (Linux) and /Qopenmp (Windows) enable 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 options -qopenmp with -qopenmp-targets (Linux) and the options /Qopenmp with /Qopenmp-targets (Windows) enable recognition of device targeting and data mapping directives and generation of code to map data to and from a specified device, and to generate target code for that device.

These options work with both -O0 (Linux) and /Od (Windows*) and with any optimization level of O1, O2 and O3.

Specifying -O0 (Linux) or /Od (Windows) with these options helps to debug OpenMP applications.

Compile your application using a command similar to one of the following:

Linux

ifx -qopenmp source_file

Windows

ifx /Qopenmp source_file

For example, to compile the previous code example without generating an executable, use the c option:

Linux

ifx -qopenmp -c parallel.f90

Windows

ifx /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:

Linux

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

Windows

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

For more information, see C/C++ or Fortran with OpenMP* Offload Programming Model.

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