Question: Parallelizing MC Integration with OpenMP problem: I need to evaluate how well my parallel version of the code performs for accuracy and timing. I chose
Parallelizing MC Integration with OpenMP problem: I need to evaluate how well my parallel version of the code performs for accuracy and timing. I chose n and p Then use the result to answer questions like:How does varying the number of random points affect the accuracy, in general? Does varying the number of threads affect the accuracy of the algorithm? If so describe in general how it does so Is your program scalable? If so is it strongly scalable or weakly scalable? Justify your answer with examples from your data. If you have a better version of code that would help solve the problem then please use it or you can use what I have:
File: ompmcintegration.cpp
Purpose: Use OpenMP to implement a parallel version of the Monte Carlo
method for estimating the integral of a function. This version
uses OpenMP's reduction clause to compute the global sum.
Input: The endpoints of the interval of integration and the number
of random points
Output: Estimate of the integral from a to b of fx
using the Monte Carlo method and n random points.
Compile: gfopenmp ompmcintegration.cpp o mcint
Run: mcint
Algorithm:
The total number of points n is divided as evenly as possible among the threads.
Each thread applies the mcintegration function to its local number of points,
which gives the count of points below the curve fx
All local counts are summed up using OpenMP's reduction clause to get a global count.
The main thread performs the final calculation for the integral using the global count
and prints the result.
Note: fx is all hardwired.
#include
#include
#include
#include
#include
using namespace std;
Get the input values
void Getinputlong double ap long double bp long long np;
Calculate count of points under the curve
long long mcintegrationlong double a long double b long long localn long long myfirsti long long mylasti int seed;
Function we're integrating
long double flong double x;
int mainint argc, char argv
int threadcount;
long double lowerbound, upperbound; Lower and upper bounds for y values in Monte Carlo method
long double estabovelowerbound, areaunderlowerbound, totalest; Estimates for areas
double startalg, finishalg; Start and finish times for main algorithm
double localelapsed, maxelapsed; Elapsed times for each process and maximum elapsed time
if argc
threadcount std::atoiargv; Assign the value to the original threadcount
ompsetnumthreadsthreadcount;
Get input values from user
Getinput&a &b &n;
Calculate width of interval and bounds for y values
width b a;
lowerbound fa;
upperbound fb;
Initialize the total count
c ;
Start the timer
startalg ompgetwtime;
#pragma omp parallel numthreadsthreadcount reduction:c
Start the parallel region
int myrank ompgetthreadnum; Get the rank of the current thread
int size ompgetnumthreads; Get the total number of threads
Calculate number of points for each thread
int div n size;
int rem n size;
long long myfirsti mylasti localn;
Determine the range of points for each thread
if myrank rem
If rank is less than remainder, thread gets np points
myfirsti myrank div ;
mylasti myfirsti div;
localn mylasti myfirsti;
else
If rank is greater than or equal to remainder, thread gets np points
myfirsti myrank div rem;
mylasti myfirsti div;
localn mylasti myfirsti;
Each thread performs Monte Carlo integration over its range of points
c mcintegrationa b localn myfirsti mylasti myrank;
Calculate elapsed time for main algorithm
finishalg ompgetwtime;
localelapsed finishalg startalg;
Calculate and print the final estimate
estabovelowerbound staticcastc staticcastnwidth upperbound lowerbound;
areaunderlowerbound width lowerbound;
totalest estabovelowerbound areaunderlowerbound;
cout "With n n points, our estimate" endl;
cout of the integral from a to b;
cout setprecision;
cout totalest endl;
cout "The algorithm for estimating the integral took localelapsed seconds to execute." endl;
return
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
