fork download
  1. #include <stdio.h>
  2. #include <omp.h>
  3.  
  4. int main() {
  5. long num_steps = 1000000; // Number of intervals
  6. double step = 1.0 / (double)num_steps;
  7. double pi = 0.0;
  8.  
  9. #pragma omp parallel
  10. {
  11. double sum = 0.0;
  12. int i;
  13. #pragma omp for
  14. for (i = 0; i < num_steps; i++) {
  15. double x = (i + 0.5) * step;
  16. sum += 4.0 / (1.0 + x * x);
  17. }
  18.  
  19. // Use a critical section to update the global variable pi
  20. #pragma omp critical
  21. {
  22. pi += sum * step;
  23. }
  24. }
  25.  
  26. printf("Calculated value of PI: %.15f\n", pi);
  27. return 0;
  28. }
Success #stdin #stdout 0.01s 5276KB
stdin
Standard input is empty
stdout
Calculated value of PI: 3.141592653589764