User and Reference Guide for the Intel® C++ Compiler 14.0

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()

  • 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); 
}

Submit feedback on this help topic