#pragma omp parallel

Purpose

The omp parallel directive explicitly instructs the compiler to parallelize the chosen block of code.

Syntax

Read syntax diagramSkip visual syntax diagram
                            .-,------.   
                            V        |   
>>-#--pragma--omp parallel----clause-+-------------------------><

Parameters

clause is any of the following clauses:
if (exp)
When the if argument is specified, the program code executes in parallel only if the scalar expression represented by exp evaluates to a nonzero value at run time. Only one if clause can be specified.
private (list)
Declares the scope of the data variables in list to be private to each thread. Data variables in list are separated by commas.
firstprivate (list)
Declares the scope of the data variables in list to be private to each thread. Each new private object is initialized with the value of the original variable as if there was an implied declaration within the statement block. Data variables in list are separated by commas.
num_threads (int_exp)
The value of int_exp is an integer expression that specifies the number of threads to use for the parallel region. If dynamic adjustment of the number of threads is also enabled, then int_exp specifies the maximum number of threads to be used.
shared (list)
Declares the scope of the comma-separated data variables in list to be shared across all threads.
default (shared | none)
Defines the default data scope of variables in each thread. Only one default clause can be specified on an omp parallel directive.

Specifying default(shared) is equivalent to stating each variable in a shared(list) clause.

Specifying default(none) requires that each data variable visible to the parallelized statement block must be explcitly listed in a data scope clause, with the exception of those variables that are:

  • const-qualified,
  • specified in an enclosed data scope attribute clause, or,
  • used as a loop control variable referenced only by a corresponding omp for or omp parallel for directive.
copyin (list)
For each data variable specified in list, the value of the data variable in the master thread is copied to the thread-private copies at the beginning of the parallel region. Data variables in list are separated by commas.

Each data variable specified in the copyin clause must be a threadprivate variable.

reduction (operator: list)
Performs a reduction on all scalar variables in list using the specified operator. Reduction variables in list are separated by commas.
A private copy of each variable in list is created for each thread. At the end of the statement block, the final values of all private copies of the reduction variable are combined in a manner appropriate to the operator, and the result is placed back in the original value of the shared reduction variable. For example, when the max operator is specified, the original reduction variable value combines with the final values of the private copies by using the following expression:
original_reduction_variable = original_reduction_variable < private_copy ?
private_copy : original_reduction_variable; 
For variables specified in the reduction clause, they must satisfy the following conditions:
  • Must be of a type appropriate to the operator. If the max or min operator is specified, the variables must be one of the following types with or without long, short, signed, or unsigned:
    • _Bool
    • char
    • int
    • float
    • double
  • Must be shared in the enclosing context.
  • Must not be const-qualified.
  • Must not have pointer type.

Usage

When a parallel region is encountered, a logical team of threads is formed. Each thread in the team executes all statements within a parallel region except for work-sharing constructs. Work within work-sharing constructs is distributed among the threads in a team.

Loop iterations must be independent before the loop can be parallelized. An implied barrier exists at the end of a parallelized statement block.

By default, nested parallel regions are serialized.