#pragma omp for Preprocessor Directive (C Only)

The omp for directive instructs the compiler to distribute loop iterations within the team of threads that encounters this work-sharing construct.

Syntax

#pragma omp for [clause[ clause] ...] 
<for_loop>

where clause is any of the following:

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 as if there was an implied declaration within the statement block. Data variables in list are separated by commas.
lastprivate (list) Declares the scope of the data variables in list to be private to each thread. The final value of each variable in list, if assigned, will be the value assigned to that variable in the last iteration. Variables not assigned a value will have an indeterminate value. Data variables in list are separated by commas.
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.
ordered Specify this clause if an ordered construct is present within the dynamic extent of the omp for directive.
schedule (type) Specifies how iterations of the for loop are divided among available threads. Acceptable values for type are:
dynamic
dynamic,n
If n is not specified, iterations of a loop are divided into chunks of size ceiling(number_of_iterations/number_of_threads).

If n is specified, all chunks are set to size n. n must be an integral assignment expression of value 1 or greater.

Chunks are dynamically assigned to threads on a first-come, first-serve basis as threads become available. This continues until all work is completed.

guided
guided,n
Chunks are made progressively smaller until the default minimum chunk size is reached. The first chunk is of size ceiling(number_of_iterations/number_of_threads). Remaining chunks are of size ceiling(number_of_iterations_remaining/number_of_threads).

If n is specified, the minimum chunk size is set to n. n must be an integral assignment expression of value 1 or greater.

If n is not specified, a default value of 1 is assumed.

Chunks are assigned to threads on a first-come, first-serve basis as threads become available. This continues until all work is completed.

runtime
Scheduling policy is determined at run-time. Use the OMP_SCHEDULE environment variable to set the scheduling type and chunk size.
static
Iterations of a loop are divided into chunks of size ceiling(number_of_iterations/number_of_threads). Each thread is assigned a separate chunk.

This scheduling policy is also known as block scheduling.

static,n
Iterations of a loop are divided into chunks of size n. Each chunk is assigned to a thread in round-robin fashion.

n must be an integral assignment expression of value 1 or greater.

This scheduling policy is also known as block cyclic scheduling.

static,1
Iterations of a loop are divided into chunks of size 1. Each chunk is assigned to a thread in round-robin fashion.

This scheduling policy is also known as cyclic scheduling.

nowait Use this clause to avoid the implied barrier at the end of the for directive. This is useful if you have multiple independent work-sharing sections or iterative loops within a given parallel region. Only one nowait clause can appear on a given for directive.

and where for_loop is a for loop construct with the following canonical shape:

for (init_expr; exit_cond; incr_expr)
 statement

where:

init_expr
takes form:
iv = b
integer-type iv = b
exit_cond
takes form:
iv <= ub
iv <  ub
iv >= ub
iv >  ub
incr_expr
takes form:
++iv
iv++
--iv
iv--
iv += incr 
iv -= incr
iv = iv + incr
iv = incr + iv
iv = iv - incr

 

iv
Iteration variable. The iteration variable must be a signed integer not modified anywhere within the for loop. It is implicitly made private for the duration of the for operation. If not specified as lastprivate, the iteration variable will have an indeterminate value after the operation completes..
b, ub, incr
Loop invariant signed integer expressions. No synchronization is performed when evaluating these expressions and evaluated side effects may result in indeterminate values..

 

Notes
Program sections using the omp for pragma must be able to produce a correct result regardless of which thread executes a particular iteration. Similarly, program correctness must not rely on using a particular scheduling algorithm.

The for loop iteration variable is implicitly made private in scope for the duration of loop execution. This variable must not be modified within the body of the for loop. The value of the increment variable is indeterminate unless the variable is specified as having a data scope of lastprivate.

An implicit barrier exists at the end of the for loop unless the nowait clause is specified.

Restrictions are:



Program Parallelization
Shared and Private Variables in a Parallel Environment


Control Parallel Processing with Pragmas


#pragma Preprocessor Directives for Parallel Processing
#pragma ordered Preprocessor Directive
OpenMP Run-time Options for Parallel Processing