Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Getting Started with OpenMP


The Basics of Compilation

Using the OpenMP pragmas requires an OpenMP-compatible compiler and thread-safe libraries. A perfect choice is the Intel C++ Compiler version 7.0 or newer. (The Intel Fortran compiler also supports OpenMP.) Adding the following command-line option to the compiler instructs it to pay attention to the OpenMP pragmas and to insert threads.

If you omit /Qopenmp on the command-line, the compiler will ignore OpenMP pragmas, providing a very simple way to generate a single-threaded version without changing any source code. The Intel C++ Compiler supports the OpenMP 2.0 specification. Be sure to browse the release notes and compatibility information supplied with the Intel C++ Compiler for the latest updates. The complete OpenMP specification is available from www.openmp.org.For conditional compilation, the compiler defines _OPENMP. If needed, this define can be tested as shown below.

 

#ifdef _OPENMP
        fn();
    #endif<
  

The general form of all OpenMP pragmas is:

If the line does not start with pragma omp it is not an OpenMP pragma. The full list of pragmas is located in the specification found at www.openmp.org.The C-runtime thread-safe libraries are selected using the /MD or /MDd (for debug) command-line compiler options. These options are selected, when using Microsoft Visual C++, in the Code Generation Category for C/C++ Project Settings by selecting either Multithreaded DLL or Debug Multithreaded DLL.

A Few Simple Examples

The following examples illustrate how simple OpenMP is to use. In common practice, additional issues need to be addressed, but these are great to get you going.

  • Problem 1: The following loop clips an array to the range 0...255. You need to thread it using an OpenMP pragma.

    Solution: Simply insert the following pragma immediately before the loop.

  • Problem 2: The following loop generates a table of square roots for the numbers 0...100. You need to thread it using OpenMP.

    Solution: The loop variable is not a signed integer, so that needs to be changed and the pragma needs to be added.

Avoiding Data Dependencies and Race Conditions

When a loop meets all five loop restrictions and the compiler threads the loop, it may still not work correctly due to the existence of data dependencies. Data dependencies exist when different iterations of a loop-more specifically a loop iteration that is executed on a different thread-reads or writes shared memory. Consider the following example that calculates factorials.


// Do NOT do this. It will fail due to data dependencies.
    // Each loop iteration writes a value that a different
    iteration reads.
    #pragma omp parallel for
    for (i=2; i < 10; i++)
    {
       factorial[i] = i * factorial[i-1];
    }

The compiler will thread this loop, but it will fail because at least one of the loop iterations is data-dependent upon a different iteration. This situation is referred to as a race condition. Race conditions can only occur when using shared resources (like memory) and parallel execution. To address this problem either rewrite the loop or pick a different algorithm, one that does not contain the race condition.Race conditions are tricky to detect because, in a given instance, the variables might "win the race" in the order tha t happens to make the program function correctly. Just because a program works once doesn't mean that it will always work. Testing your program on various machines, some with Hyper-Threading Technology and some with multiple physical processors, is a good starting point. Tools such as the Intel Thread Checker can also help, as can a sharp eye. Traditional debuggers are useless for detecting race conditions because they cause one thread to stop the "race" while the other threads continue to significantly change the runtime behavior

Managing Shared and Private Data

Nearly every loop (at least if it's a useful one) reads or writes memory, and it's the programmer's job to tell the compiler which pieces of memory should be shared among the threads and which pieces should be kept private. When memory is identified as shared, all threads access the exact same memory location. When memory is identified as private, however, a separate copy of the variable is made for each thread to access in private. When the loop ends, these private copies are destroyed. By default, all variables are shared except for the loop variable, which is private. Memory can be declared as private in the following two ways.

  • Declare the variable inside the loop-really inside the parallel OpenMP directive-without the static keyword.
  • Specify the private clause on an OpenMP directive.

The following loop fails to function correctly because the variable temp is shared. It needs to be private.


// WRONG. Fails due to shared memory.

    // Variable temp is shared among all threads, so while one thread
    // is reading variable temp another thread might be writing to it

    #pragma omp parallel for
    for (i=0; i < 100; i++)
    {
       temp = array[i];
       array[i] = do_something(temp);
    }

The following two examples both declare the variable temp as private memory, which fixes the problem.


// This works. The variable temp is now private
    #pragma omp parallel for
    for (i=0; i < 100; i++)
    {
       int temp; // variables declared within a
    parallel construct
        // are, by definition, private
       temp = array[i];
       array[i] = do_something(temp);
    }
    // This also works. The variable temp is declared private
    #pragma omp parallel for private(temp)
    for (i=0; i < 100; i++)
    {
       temp = array[i];
       array[i] = do_something(temp);
    }

Every time you instruct OpenMP to parallelize a loop, you should carefully examine all memory references, including the references made by called functions. Variables declared within a parallel construct are defined as private except when they are declared with the static declarator, because static variables are not allocated on the stack.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.