#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#pragmaomp 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 main 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(reduction-identifier:list)
Performs a reduction on each data variable in list using the specified reduction-identifier. 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.Scalar variables and array sections are supported in list. Items in list are separated by commas. reduction-identifier is either an C onlyidentifierC only, C++ onlyid-expressionC++ only, or one of the following operators that are implicitly declared: +, -, *, &, |, ^, &&, and ||. If you use a reduction-identifier that is not implicitly declared, you must use the omp declare reduction directive to declare the reduction-identifier beforehand.
The following table lists each reduction-identifier that is implicitly declared at every scope for arithmetic types and its semantic initializer value. The actual initializer value is that value as expressed in the data type of the reduction list item.
Table 1. Implicitly declared C/C++ reduction-identifiers
Identifier Initializer Combiner
+ omp_priv=0 omp_out+=omp_in
* omp_priv=1 omp_out*=omp_in
- omp_priv=0 omp_out-=omp_in
& omp_priv=~0 omp_out&=omp_in
| omp_priv=0 omp_out|=omp_in
^ omp_priv=0 omp_out^=omp_in
&& omp_priv=1 omp_out=omp_in&&omp_out
|| omp_priv=0 omp_out=omp_in||omp_out
max omp_priv=Least representable number in the reduction list item type

omp_out=omp_in>omp_out?

omp_in:omp_out

min omp_priv=Largest representable number in the reduction list item type

omp_out=omp_in<omp_out?

omp_in:omp_out

omp_in and omp_out correspond to two identifiers that refer to storage of the type of the list item. omp_out holds the final value of the combiner operation. Any reduction-identifier that is defined with the omp declare reduction directive is also valid. In that case, the initializer and combiner of the reduction-identifier are specified by the initializer-clause and the combiner in the omp declare reduction directive.
Restrictions:
  • A list item that appears in a reduction clause of a worksharing construct must be shared in the parallel regions to which any of the worksharing regions arising from the worksharing construct bind.
  • A list item that appears in a reduction clause of the innermost enclosing worksharing or parallel construct may not be accessed in an explicit task.
  • Any number of reduction clauses can be specified on the directive, but a list item can appear only once in the reduction clauses for that directive.
  • For a reduction-identifier declared with the omp declare reduction construct, the directive must appear before its use in a reduction clause.
  • If a list item is an array section, it must specify contiguous storage and it cannot be a zero-length array section.
  • If a list item is an array section, accesses to the elements of the array outside the specified array section are not allowed.
  • The type of a list item that appears in a reduction clause must be valid for the reduction-identifier. C onlyFor a max or min reduction in C, the type of the list item must be an allowed arithmetic data type: char, int, float, double, or _Bool, possibly modified with long, short, signed, or unsigned.C only C++ onlyFor a max or min reduction in C++, the type of the list item must be an allowed arithmetic data type: char, wchar_t, int, float, double, or bool, possibly modified with long, short, signed, or unsigned.C++ only
  • A list item that appears in a reduction clause must not be const-qualified.
  • If a list item in a reduction clause on a worksharing construct has a reference type then it must bind to the same object for all threads of the team.
  • The reduction-identifier for any list item must be unambiguous and accessible.
proc_bind(master | close | spread)
Specifies a policy for assigning threads to places within the current place partition. At most one proc_bind clause can be specified on the parallel directive. If the OMP_PROC_BIND environment variable is not set to FALSE, the proc_bind clause overrides the first element in the OMP_PROC_BIND environment variable. If the OMP_PROC_BIND environment variable is set to FALSE, the proc_bind clause has no effect.

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.