Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <omp.h>
  3.  
  4. using namespace std;
  5.  
  6. double IntegralPI = 0.0;
  7. double func(double x1, double x2){
  8. return (4.0/(1+x1*x1)+4.0/(1+x2*x2))*(x1-x2)/2;
  9. }
  10. void sum(double s){
  11. IntegralPI += s;
  12. }
  13.  
  14. int main(){
  15. double a = 0.0;
  16. double b = 1.0;
  17. const int n = 1000000;
  18. double x[n];
  19. x[0] = a;
  20. //double IntegralPI = 0.0;
  21. //double integralBuffer = 0.0;
  22. for(int i = 0; i < n-1; i++){
  23. x[i+1] = x[i]+(b-a)/n;
  24. }
  25. double t1 = omp_get_wtime();
  26. #pragma omp parallel for reduction(+: IntegralPI)
  27. for(int i = 0; i < n-1; i++){
  28. IntegralPI += func(x[i+1],x[i]);
  29. }
  30. IntegralPI += func(x[n-1],x[n-2]);
  31. double t2 = omp_get_wtime();
  32. cout << IntegralPI << " Working time: " << t2-t1 << endl;
  33. IntegralPI = 0;
  34. double t3 = omp_get_wtime();
  35. for(int i = 0; i < n-1; i++){
  36. IntegralPI += func(x[i+1],x[i]);
  37. }
  38. IntegralPI += func(x[n-1],x[n-2]);
  39. double t4 = omp_get_wtime();
  40. cout<< IntegralPI << " Working time: " << t4-t3 << endl;
  41. return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement