h = 1.0 / n;
#pragma omp parallel for private(x) shared(n, h) reduction(+:area)
for (i = 1; i <= n; i++)
{
x = h * (i - 0.5);
area += (4.0 / (1.0 + x*x));
}
pi = h * area;
In this example, each threads execute a subset of the iteration count. Each thread has its local private copy of area
and at the end of the parallel region they all apply the addition operation (+
) so as to generate the final value for area
.