Intel® C++ Compiler 16.0 User and Reference Guide

omp for

Specifies a parallel loop. Each iteration of the loop is executed by one of the threads in the team.

Syntax

#pragma omp for [clause, clause, ...]

for loop

Arguments

clause

Can be zero or more of the following clauses:

collapse(n)

Specifies how many nested loops of a for loop associated with the OpenMP* for construct should be collapsed into a single loop for running in parallel.

firstprivate( list )

Provides a superset of the functionality provided by the private clause. Each private data object is initialized with the value of the original object.

lastprivate( list )

Provides a superset of the functionality provided by the private clause. When exiting the region, the original object is updated with the value of the private copy from the last sequential iteration of the associated loop, or the lexically last section construct.

nowait

Indicates that an implementation may omit the barrier at the end of the worksharing region.

ordered

private( list )

Declares variables to be private to each thread in a team.

reduction( operator: list )

Performs a reduction on scalar variables.

schedule (type[, chunk])

Specifies how iterations of the for loop are divided among the threads of the team.

  • collapse()

  • firstprivate(list)

  • lastprivate(list)

  • nowait

  • ordered

  • private(list)

  • reduction(operator : list)

  • schedule (type[, chunk])

for loop

Must be in the following form:

for (init-expr; test-expr; incr-expr)

structured-block

Description

The for loop that follows this pragma is executed in parallel by the currently active team of threads.

Example: Calculating the value of pi by executing the for loop in parallel

#include <omp.h> 
static long num_steps = 1000000000; 
double step; 
 
int main () { 
    double pi, sum = 0.0; 
    step = 1.0/(double) num_steps; 
 
    #pragma omp parallel { 
        int i; 
 
        #pragma omp for reduction(+:sum) 
        double x; 
        for (i=1; i<= num_steps; i++) { 
            x = (i-0.5)*step; 
            sum = sum + 4.0/(1.0+x*x); 
        }
    }
 
    pi = step * sum;  
    printf("The computed value of Pi is: %1.10lf\n", pi); 
}