Starting from:

$30

Concurrency-Parallelism-Assignment 3 Solved

1        Introduction
 The “Monte Carlo Method” is a method of solving problems using statistics. Given the probability, p, that an event will occur in certain conditions, a computer can be used to generate those conditions repeatedly. The number of times the event occurs divided by the number of times the conditions are generated should be approximately equal to p.

Figure 1 shows a circle with radius r = 1 inscribed within a square.

The area of the circle is πr2 = π12 = π, and the area of the square is

(2r)2 = 22 = 4. The ratio of the area of the circle to the area of the Figura 1: A circle square is  within a square.

If we could compute ratio p, then we could multiple it by four to obtain the value π = p ×4. One particularly simple way to do this is to pick lattice points in the square and count how many of them lie inside the circle (see Figure2). Suppose for example that the points   are selected, then there are 812 points inside the circle and 212 points outside the circle and the percentage of points inside the circle is . Then the area of the circle is approximated with the following calculation: Area of circle = p×Area of square = p×4 =

 0.792195122×4 = 3,168780488.

2           Monte Carlo Method for π
Monte Carlo methods can be thought of as statistical simulation

methods that utilize a sequences of random numbers to perform the simulation. The name “Monte Carlo” was coined by Nicholas Constantine Metropolis (1915-1999) and inspired by Stanslaw Ulam (19091986), because of the similarity of statistical simulation to games of
Figura 2: Ratio between areas of circle and square.
Campus de Caparica

2829-516 CAPARICA | Portugal

T: +351 212 948 536

di.secretariado@fct.unl.pt

di.fct.unl.pt

chance, and because Monte Carlo is a center for gambling and games of chance. In a typical process one compute the number of points in a set A that lies inside box R. The ratio of the number of points that fall inside A to the total number of points tried is equal to the ratio of the two areas (or volume in 3 dimensions). The accuracy of the ratio p depends on the number of points used, with more points leading to a more accurate value.

 A simple Monte Carlo simulation to approximate the value of π could involve randomly selecting points   in the unit square and determining the ratio  where m is number of points that satisfy x2i + yi2 ≤ 1. In a typical simulation of sample size n = 1000 there are 787 points satisfying that equation. Using this data we obtain   and π = p ×4 = 0.787×4 = 3.148.

Every time a Monte Carlo simulation is made using the same sam-

ple size n it will come up with a slightly different value. The values

converge very slowly of the order O(n−1/2). This property is a consequence of the Central Limit Theorem.

You may find a web simulation of this method at https:// academo.org/demos/estimating-pi-monte-carlo/.

3           Lab Work

3.1         Study OpenMP
Carlo method.
                                                                                                                                                         Figura 3:     Monte

Start by studying OpenMP (https://www.openmp.org/resources/tutorials-articles/). I recommend you to study these slides https://www.openmp.org/wp-content/uploads/omp-hands-on-SC08.pdf up to page 20 and and try the proposed exercises (up to page 20).

You may also follow this tutorial in youtube https://www.youtube.com/playlist?list=PLLX-Q6B8xqZ8n8bwjGdzBJ25X2utwnoEG and/or study this book https://libgen.lc/ads.php?md5=D27D7EE55FA748118FEA185BC27D6FB5 Then step into the next section of this lab protocol.

3.2           OpenMP Parallel Version
Depart from the sequential version that you developed for the Lab of last week (P2). Now, use OpenMP to parallelize your program developing two versions:

1.    An OpenMP parallel version that has as few changes to the sequential version as possible; and

2.    An OpenMP parallel version that is optimized for speed.

2

The parallel program(s) must receive as command line arguments the number of simulations to be executed (i.e., the number of points to be generated) and how many parallel threads shall be executing. The second command line argument shall be optional and default to one.

Remember to keep on using GIT. :)

3.3          Experimental evaluation
Use the same command line flag that you use in the plain C version to make the program produce a simplified (CSV-like) output.

Reuse the bash script from last week to run multiple tests in your computer, with different values for iterations and processors. Also, remember to run each configuration at least three times.

3.4           To Think About...

The sequential version is faster, slower or identical to the parallel version with just one thread?

The parallel version with two thread is faster than with one? When you double the number of threads does it take half the time? More? Less?

Which of your implementations is faster? Java? C + pThreads? C + OpenMP? Why?

Final Note
If you search the web, it will be trivial to find an implementation of the Monte Carlo simulation to approximate the value of π, that you can easily adapt to your needs. But note that if you do that, you are cheating and you will not learn what you are supposed to learn in this lab class. Just be honest to yourself and make your own program. ;)

More products