#pragma omp parallel Preprocessor Directive (C Only)

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

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.

Syntax

#pragma omp parallel [clause[ clause] ...] 
<statement_block>

where clause is any of the following:

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 non-zero 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.
shared (list) Declares the scope of the 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 into the original value of the shared reduction variable.

Variables specified in the reduction clause:

  • must be of a type appropriate to the operator.
  • must be shared in the enclosing context.
  • must not be const-qualified.
  • must not have pointer type.

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

Nested parallel regions are always serialized.



Program Parallelization
Shared and Private Variables in a Parallel Environment


Control Parallel Processing with Pragmas


#pragma Preprocessor Directives for Parallel Processing
OpenMP Run-time Options for Parallel Processing
#pragma omp parallel sections Preprocessor Directive