Intel® C++ Compiler 16.0 User and Reference Guide
Specifies a parallel loop. Each iteration of the loop is executed by one of the threads in the team.
#pragma omp for [clause, clause, ...] |
for loop
clause |
Can be zero or more of the following clauses:
|
||||||||||||||||
for loop |
Must be in the following form: for (init-expr; test-expr; incr-expr) structured-block |
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);
} |