Description
Category: OpenMPI
Objective:
Use OpenMPI to estimate the value of a definite integral using the Monte Carlo method.
How to use Monte Carlo simulation to estimate an integral:
The Monte Carlo technique takes advantage of a theorem in probability that is whimsically called the Law of the Unconscious Statistician. The theorem is basically the chain rule for integrals. If ππ is a continuous random variable and ππ = ππ(ππ) is a random variable that is created by a continuous transformation (g) of ππ, then the expected value of ππ is given by the following convolution:
πΈπΈ[ππ] = πΈπΈ[ππ(ππ)] = ππ(π₯π₯)ππππ(π₯π₯)πππ₯π₯
where ππππ is the probability density function for ππ.
To apply the theorem, choose ππ to be a uniform random variable on the interval (ππ, ππ). The density function of ππ is therefore ππππ(π₯π₯) = 1/(ππ β ππ) if π₯π₯ is in (ππ, ππ) and 0 otherwise. Rearranging the equation gives
ππ
ππ(π₯π₯)πππ₯π₯ = (ππ β ππ) β πΈπΈ[ππ(ππ)]
ππ
Consequently, to estimate the integral of a continuous function g on the interval (ππ, ππ), you need to estimate the expected value πΈπΈ[ππ(ππ)], where ππ ~ ππ(ππ, ππ). To do this, generate a uniform random sample in (ππ, ππ), evaluate g on each point in the sample, and take the arithmetic mean of those values. In symbols,
ππππ
ππ(π₯π₯)πππ₯π₯ β (ππ β ππ) ππ(π₯π₯ππ)
ππ ππ ππ=1
where the π₯π₯ππ are independent random uniform variates on (ππ, ππ).
Method:
Description:
Write a C++ application that uses OpenMPI to estimate the following two definite integrals
1) πππ₯π₯ which should be equal to 1/3 to test your results
2) πππ₯π₯
You application should take two command line arguments (-P and -N):
-P 1 this can be 1 or 2, and indicates which integral listed above to estimate.
-N 1000000 this is the number of random samples to generate in total and needs to be distributed across the available processors.
Your program should use MPI’s broadcast communication methods to distribute random runs among processors and then gather the results for the final calculation of the integral.
Sample Run:
ο srun ./a.out -P 1 -N 10000000
ο The estimate for integral 1 is 0.33333321 ο Bye!
Turn-In Instructions
Zip up your file(s) into Lab6.zip and upload this zip file on the assignment section of Canvas.
Grading Rubric:
AUTOMATIC GRADING POINT DEDUCTIONS PER PROBLEM:
Element Percentage Deduction Details
Does Not Compile 40% Code does not compile on PACE-ICE!
Does Not Match Output Up to 90% The code compiles but does not produce correct outputs.
Clear Self-Documenting Coding Styles Up to 25% This can include incorrect indentation, using unclear variable names, unclear/missing comments, or compiling with warnings. (See Appendix A)
LATE POLICY
Element Percentage Deduction Details
Appendix A: Coding Standards
Indentation:
When using if/for/while statements, make sure you indent 4 spaces for the content inside those. Also make sure that you use spaces to make the code more readable. For example:
for (int i; i < 10; i++)
{ j = j + i;
}
If you have nested statements, you should use multiple indentions. Each { should be on its own line (like the for loop) If you have else or else if statements after your if statement, they should be on their own line.
for (int i; i < 10; i++)
{
if (i < 5) {
counter++; k -= i; } else
{ k +=1; } j += i;
}
Camel Case:
This naming convention has the first letter of the variable be lower case, and the first letter in each new word be capitalized (e.g. firstSecondThird).
This applies for functions and member functions as well!
The main exception to this is class names, where the first letter should also be capitalized.
Variable and Function Names:
Your variable and function names should be clear about what that variable or function represents. Do not use one letter variables, but use abbreviations when it is appropriate (for example: βimag” instead of
βimaginaryβ). The more descriptive your variable and function names are, the more readable your code will be. This is the idea behind self-documenting code.
File Headers:
Every file should have the following header at the top
/*
Author: your name
Class: ECE4122 or ECE6122 (section)
Description:
What is the purpose of this file?
*/
Code Comments:
1. Every function must have a comment section describing the purpose of the function, the input and output parameters, the return value (if any).
2. Every class must have a comment section to describe the purpose of the class.
3. Comments need to be placed inside of functions/loops to assist in the understanding of the flow of the code.



