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.

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

use omp_lib

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.

Example

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

where:

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

The following example demonstrates one way of using an OpenMP directive 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;}

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, or multi-core processor systems.

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:

Operating System

Description

Linux and Mac OS X

icc -openmp source_file

Windows

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

Operating System

Example

Linux and Mac OS X

icc -openmp -c parallel.cpp

Windows

icl /Qopenmp /c parallel.cpp

The compiler might return a message similar to the following:

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. See the OpenMP Environment Variables.


Submit feedback on this help topic

Copyright © 1996-2011, Intel Corporation. All rights reserved.