t1nman

omp_pi

May 25th, 2012
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <omp.h>
  4.  
  5. long long num_steps = 1000000000;
  6. double step;
  7.  
  8. int main(int argc, char* argv[])
  9. {
  10.     double start, stop;
  11.     double x, pi, sum=0.0;
  12.     int i;
  13.     step = 1./(double)num_steps;
  14.     start = omp_get_wtime();
  15.  
  16.     omp_set_num_threads(5);
  17.  
  18. #pragma omp parallel
  19.     {
  20.    
  21. #pragma omp for reduction(+:sum) private(x) schedule(guided)
  22.         for (i=0; i<num_steps; i++)
  23.         {
  24.             x = (i + .5)*step;
  25.             sum = sum + 4.0/(1.+ x*x);
  26.         }
  27.                                
  28.     }
  29.        
  30.     pi = sum*step;
  31.     stop = omp_get_wtime();
  32.  
  33.     printf("The value of PI is %15.12f\n",pi);
  34.     printf("The time to calculate PI was %.2lf seconds\n",stop - start);
  35.  
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment