openmp OpenMP reductions Approximation of PI using reductions based on #pragma atomic

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

h = 1.0 / n;
#pragma omp parallel for private(x) shared(n, h, area) 
for (i = 1; i <= n; i++)
{
  x = h * (i - 0.5);
  #pragma atomic
  area += (4.0 / (1.0 + x*x));
}
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. We can use the #pragma atomic in here because the given operation (+=) can be done atomically, which simplifies the readability compared to the usage of the #pragma omp critical.



Got any openmp Question?