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 =100,200,2000,32768 and p=1,2,6,7. 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: omp_mc_integration.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 f(x)
* using the Monte Carlo method and n random points.
*
* Compile: g++-fopenmp omp_mc_integration.cpp -o mc_int
* Run: ./mc_int
*
* Algorithm:
*1. The total number of points 'n' is divided as evenly as possible among the threads.
*2. Each thread applies the mc_integration function to its local number of points,
* which gives the count of points below the curve f(x).
*3. All local counts are summed up using OpenMP's reduction clause to get a global count.
*4. The main thread performs the final calculation for the integral using the global count
* and prints the result.
*
* Note: f(x) is all hardwired.
*/
#include
#include
#include
#include
#include
using namespace std;
/* Get the input values */
void Get_input(long double* a_p, long double* b_p, long long* n_p);
/* Calculate count of points under the curve */
long long mc_integration(long double a, long double b, long long local_n, long long my_first_i, long long my_last_i, int seed);
/* Function we're integrating */
long double f(long double x);
int main(int argc, char* argv[]){
int thread_count;
long double lower_bound, upper_bound; // Lower and upper bounds for y values in Monte Carlo method
long double est_above_lower_bound, area_under_lower_bound, total_est; // Estimates for areas
double start_alg, finish_alg; // Start and finish times for main algorithm
double local_elapsed, max_elapsed; // Elapsed times for each process and maximum elapsed time
if (argc >1){
thread_count = std::atoi(argv[1]); // Assign the value to the original thread_count
omp_set_num_threads(thread_count);
}
// Get input values from user
Get_input(&a, &b, &n);
// Calculate width of interval and bounds for y values
width = b - a;
lower_bound = f(a)/2;
upper_bound = f(b);
// Initialize the total count
c =0;
// Start the timer
start_alg = omp_get_wtime();
#pragma omp parallel num_threads(thread_count) reduction(+:c)
{
// Start the parallel region
int my_rank = omp_get_thread_num(); // Get the rank of the current thread
int size = omp_get_num_threads(); // Get the total number of threads
// Calculate number of points for each thread
int div = n / size;
int rem = n % size;
long long my_first_i, my_last_i, local_n;
// Determine the range of points for each thread
if (my_rank < rem){
// If rank is less than remainder, thread gets n/p +1 points
my_first_i = my_rank *(div +1);
my_last_i = my_first_i + div;
local_n = my_last_i - my_first_i;
} else {
// If rank is greater than or equal to remainder, thread gets n/p points
my_first_i = my_rank * div + rem;
my_last_i = my_first_i + div;
local_n = my_last_i - my_first_i;
}
// Each thread performs Monte Carlo integration over its range of points
c += mc_integration(a, b, local_n, my_first_i, my_last_i, my_rank);
}
// Calculate elapsed time for main algorithm
finish_alg = omp_get_wtime();
local_elapsed = finish_alg - start_alg;
// Calculate and print the final estimate
est_above_lower_bound =(static_cast(c)/ static_cast(n))*(width *(upper_bound - lower_bound));
area_under_lower_bound = width * lower_bound;
total_est = est_above_lower_bound + area_under_lower_bound;
cout << "With n ="<< n <<" points, our estimate" << endl;
cout <<"of the integral from "<< a <<" to "<< b;
cout << setprecision(15);
cout <<"="<< total_est << endl;
cout << "The algorithm for estimating the integral took "<< local_elapsed <<" seconds to execute." << endl;
return

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!