Intel® oneAPI DPC++/C++ Compiler Developer Guide and Reference

ID 767253
Date 11/07/2023
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

Add OpenMP* Support

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

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

  2. Compile the application with the /Qopenmp (Windows*) or -qopenmp (Linux*) 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 Pragma Syntax

To add OpenMP support to your application, first declare the OpenMP header and then add appropriate OpenMP pragmas to your source code.

To declare the OpenMP header, add the following in your code:

#include <omp.h>

OpenMP pragmas 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® oneAPI DPC++/C++ Compiler.

To use pragmas in your source, use this syntax:

<prefix> <pragma> [<clause>, ...] <newline>

where:

  • <prefix> - Required for all OpenMP pragmas. The prefix must be #pragma omp.

  • <pragma> - A valid OpenMP pragma. Must immediately follow the prefix.

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

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

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

The following example demonstrates one way of using an OpenMP pragma to parallelize a loop:

#include <omp.h> 
void simple_omp(int *a){
  int i;
  #pragma omp parallel for
  for (i=0; i<1024; i++)
    a[i] = i*2; 
}

Compile the Application

The /Qopenmp (Windows) or -qopenmp (Linux) option enables the parallelizer to generate multi-threaded code based on the OpenMP pragmas 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) option works with both -O0 (Linux) and /Od (Windows*) and with any optimization level of O1, O2 and O3.

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

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

Linux

icpx -qopenmp source_file

Windows

icx /Qopenmp source_file

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

Linux

icpx -qopenmp -c parallel.cpp

Windows

icx /Qopenmp /c parallel.c

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" pragmas must be compiled. For example:

Linux

icpx -qopenmp -fopenmp-targets=spir64 offload.cpp

Windows

icx /Qopenmp /Qopenmp-targets=spir64 offload.c

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