openmp OpenMP reductions Approximation of PI using reductions based on #pragma omp critical


Example

double area;
double h = 1.0 / n;
#pragma omp parallel for shared(n, h, area) 
for (i = 1; i <= n; i++)
{
  double x = h * (i - 0.5);
  #pragma omp critical
  {
    area += (4.0 / (1.0 + x*x));
  }
}
double pi = h * area;

In this example, each threads execute a subset of the iteration count and they accumulate atomically into the shared variable area, which ensures that there are no lost updates.