Advertisement
Guest User

Untitled

a guest
Nov 12th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. /*
  2.  
  3. This program will numerically compute the integral of
  4.  
  5. 4/(1+x*x)
  6.  
  7. from 0 to 1. The value of this integral is pi -- which
  8. is great since it gives us an easy way to check the answer.
  9.  
  10. This version of the program uses a divide and concquer algorithm
  11. and recurrsion.
  12.  
  13. History: Written by Tim Mattson, 10/2013
  14.  
  15. */
  16. #include <omp.h>
  17. #include <stdio.h>
  18. static long num_steps = 1024*1024*1024;
  19. #define MIN_BLK 1024*1024*256
  20.  
  21. double pi_comp(int Nstart,int Nfinish,double step)
  22. { int i,iblk;
  23. double x, sum = 0.0,sum1, sum2;
  24. if (Nfinish-Nstart < MIN_BLK){
  25. for (i=Nstart;i< Nfinish; i++){
  26. x = (i+0.5)*step;
  27. sum = sum + 4.0/(1.0+x*x);
  28. }
  29. }
  30. else{
  31. iblk = Nfinish-Nstart;
  32. sum1 = pi_comp(Nstart, Nfinish-iblk/2,step);
  33. sum2 = pi_comp(Nfinish-iblk/2, Nfinish, step);
  34. sum = sum1 + sum2;
  35. }return sum;
  36. }
  37. int main ()
  38. {
  39. int i;
  40. double step, pi, sum;
  41. double init_time, final_time;
  42. step = 1.0/(double) num_steps;
  43.  
  44. init_time = omp_get_wtime();
  45. sum = pi_comp(0,num_steps,step);
  46. pi = step * sum;
  47. final_time = omp_get_wtime() - init_time;
  48. printf(" for %ld steps pi = %f in %f secs\n",num_steps,pi,final_time);
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement