9.4. Parallel region construct

The following directive defines a parallel region, which is a region of the program that is to be executed by multiple threads in parallel. This is the fundamental construct that starts parallel execution:

#pragma omp parallel [clause[ [, ]clause] ...] new-line
       structured-block

The clause is one of the following:

  • if(scalar-expression)

  • private(variable-list)

  • firstprivate(variable-list)

  • default(shared | none)

  • shared(variable-list)

  • copyin(variable-list)

  • reduction(operator: variable-list)

  • num_threads(integer-expression)

For a detailed description of the clauses, see 9.9, “Data-sharing attribute clauses” on page 355.

When a thread encounters a parallel construct, a team of threads is created if one of the following cases is true:

  • No if clause is present.

  • The if expression evaluates to a nonzero value.

This thread becomes the master thread of the team, with a thread number of 0, and all threads in the team, including the master thread, execute the region in parallel. If the value of the if expression is zero, the region is serialized.

The number of threads in a parallel region is determined by the following factors, in order of precedence:

  1. Use of the omp_set_num_threads() library function.[3]

    [3] See 9.10.1, “Execution environment functions” on page 364.

  2. Setting of the OMP_NUM_THREADS environment variable.[4]

    [4] See 9.11.2, “OMP_NUM_THREADS” on page 374.

  3. Implementation default.[5]

    [5] On AIX, it is equivalent to the number of equipped processors.

The sample code in Example 9-1 illustrates the parallel region execution. In this program, every thread executes and prints the “Hello World.” enclosed in the parallel region.

Example 9-1. omp_parallel.c
#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("Before forking a parallel region.
");

    /* Fork a team of threads giving them their own copies of variables. */
    #pragma omp parallel
    {
        printf("Hello World.
");
    } /* All threads join master thread and terminate. */

    printf("Exiting the program... Bye.
");
}

To compile this code, run:

					$ cc_r -qsmp=omp omp_parallel.c

Note

  • Use cc_r when compiling C programs with OpenMP directives. Use xlC_r when compiling C++ programs with OpenMP directives.

  • The -qsmp=omp compiler option should be specified when compiling C and C++ programs with OpenMP directives. If not specified, a stub library (/usr/lib/libxlomp_ser.a), which does not create any threads, will be in-lined instead of the actual OpenMP library (/usr/lib/libxlsmp.a).


Before running this program, set the environment variable OMP_NUM_THREADS in order to set the number of threads that you want to execute in parallel. For example,

$ export OMP_NUM_THREADS=3 a.out

The output of the program is as follows:

Before forking a parallel region
Hello World.
Hello World.
Hello World.
Exiting the program... Bye.

As instructed by the environment variable, the program forked three threads and executed the parallel region construct.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset