Quantcast
Channel: Intel® C++-Compiler
Viewing all articles
Browse latest Browse all 1665

Adding OpenMP* Support to your Application

$
0
0

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 [Q]openmp option.

  3. For applications with large local or temporary arrays, you may need to increase the stack space available at run-time. In addition, you may need to increase the stack allocated to individual threads by using the KMP_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® C++ Compiler.

The following syntax illustrates using the pragmas in your source.

Example

<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 which is enclosed by this pragma.

The pragmas are interpreted as comments if you omit the [Q]openmp option.

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

Example

#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 [Q]openmp 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 [Q]openmp option works with both -O0 (Linux* and OS X*) and /Od (Windows*) and with any optimization level of O1, O2 and O3.

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

Compile your application using commands similar to those shown below:

Operating System

Syntax Example

Linux*

icc -openmp source_file

OS X*

icl -openmp source_file

icc -openmp source_file

Windows*

icl /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*

icc -openmp -c parallel.cpp

OS X*

icc -openmp -c parallel.cpp

Windows*

icl /Qopenmp /c parallel.cpp

The compiler might return a message similar to the following:

Example

parallel.cpp(20) : (col. 3) 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.

Englisch

Viewing all articles
Browse latest Browse all 1665

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>