$24
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:
1The 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 .
( , )
The density function of is
( )=1/( − )
.
To apply the theorem, choose
to be a uniform random variable on the interval
therefore
if is in ( , ) and 0 otherwise. Rearranging the equation gives
� () =(−)∙[()]
uniform random sample in
[()]
~(,)
, you
,
Consequently, to estimate the integral of a continuous function g on the interval
need to estimate the expected value
, where
. To do this,
generate a
( , )
evaluate g on each point in the sample, and take the
arithmetic mean of those
values. In symbols,
( , )
1
� () ≈(−)∙
� ( )
=1
where the are independent random uniform variates on ( , ).
1 Estimate an integral by using Monte Carlo simulation
Method:
Description:
Write a C++ application that uses OpenMPI to estimate the following two definite integrals ∫01 2
1) ∫01 − 2 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:
If a student’s program runs correctly and produces the desired output, the student has the potential to get a 100 on his or her homework; however, TA’s will look through your code for other elements needed to meet the lab requirements. The table below shows typical deductions that could occur.
AUTOMATIC GRADING POINT DEDUCTIONS PER PROBLEM:
Element
Percentage
Details
Deduction
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
Up to 25%
This can include incorrect indentation, using unclear variable names,
Coding Styles
unclear/missing comments, or compiling with warnings. (See
Appendix A)
LATE POLICY
Element
Percentage Deduction
Details
Late Deduction Function
score – 0.5 * H
H = number of hours (ceiling function) passed
deadline
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)
Last Date Modified: date
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.