Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <omp.h>
- using namespace std;
- double IntegralPI = 0.0;
- double func(double x1, double x2){
- return (4.0/(1+x1*x1)+4.0/(1+x2*x2))*(x1-x2)/2;
- }
- void sum(double s){
- IntegralPI += s;
- }
- int main(){
- double a = 0.0;
- double b = 1.0;
- const int n = 1000000;
- double x[n];
- x[0] = a;
- //double IntegralPI = 0.0;
- //double integralBuffer = 0.0;
- for(int i = 0; i < n-1; i++){
- x[i+1] = x[i]+(b-a)/n;
- }
- double t1 = omp_get_wtime();
- #pragma omp parallel for reduction(+: IntegralPI)
- for(int i = 0; i < n-1; i++){
- IntegralPI += func(x[i+1],x[i]);
- }
- IntegralPI += func(x[n-1],x[n-2]);
- double t2 = omp_get_wtime();
- cout << IntegralPI << " Working time: " << t2-t1 << endl;
- IntegralPI = 0;
- double t3 = omp_get_wtime();
- for(int i = 0; i < n-1; i++){
- IntegralPI += func(x[i+1],x[i]);
- }
- IntegralPI += func(x[n-1],x[n-2]);
- double t4 = omp_get_wtime();
- cout<< IntegralPI << " Working time: " << t4-t3 << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement