/********************************************************** ** trapezoid_OpenMP.cpp parallelized wiht OpenMP ** *********************************************************** ** Integrate x^2 - 2x + 7 [1,10] using trapezoidal rule ** *********************************************************** ** Drive the number of subintervals into range where the ** ** accumulated round-off error swamps the approximation ** ** Time the whole process and report elapsed wall clock ** ** time in anticipation of parallelizing the code. ** **********************************************************/ #include #include #include #include #include const int MAX = (int) 1e9; //maximum number of subintervals const float X_MIN = 1.0; const float X_MAX = 10.0; //limits of our integration float f(float x) { //function to integrate return static_cast((x-2.0)*x+7.0); //we'll use x^2 -2x + 7 } float integralF(float x) {//the definite integral of f(x) return static_cast(((((1.0/3.0)*x)-1.0)*x+7.0)*x); } //The workhorse function: trapezoidal rule integration float trap(float xLo, float xHi, int numIntervals) { float area; //area under the curve (the integral) float width; //width of each trapezoid float x; //our points of evaluation float sum; //summing up the f(x)'s sum = 0.0; //init our summing var width = (xHi-xLo)/numIntervals; //compute width of each trap #pragma omp parallel for default(none) \ shared(numIntervals,xLo,width) \ private(x) \ reduction(+:sum) for(int i=1; i