Menu

(Solved) : Program 1 Interesting Way Calculating Pi Use Technique Known Monte Carlo Involves Randomiz Q44139671 . . .

Program-1. An interesting way of calculating pi is to use atechnique known as Monte Carlo, which involves randomization. Thistechnique works as follows. Suppose you have a circle inscribedwithin a square, as shown in the figure (assume that the radius ofthis circle is 1; thus we have a square of size 2×2).

First, generate a series of points as simple (x, y) coordinates.These points must fall within the Cartesian coordinates that boundthe square. Of the total number of random points that aregenerated, some will occur within the circle. Next, estimate  byperforming the following calculation:

pi = 4 x (number of points in circle) / (total number ofpoints)

Write a multi-threaded version of this algorithm that creates aseparate thread (the slave- thread) to generate a number of randompoints. The slave-thread will count the number of points that occurwithin the circle (the hit_count) and store that result in theglobal variable circle_count. When the slave-thread has exited, theparent thread (the master-thread) will calculate and output theestimated value of pi. It is worth experimenting with the number ofrandom points generated. As a general rules, the greater the numberof random points, the closer the approximation of pi.

Note: Program-1 contains only 2 threads; a master-thread and itssingle slave-thread.

Below, are codes for generating random numbers, as well as codesfor determining if the random (x, y) point occurs within thecircle.

/* Generates a double precision random number */ doublerandom_double()
{

return random() / ((double)RAND_MAX + 1); }

/* seed the random number generator */ srandom((unsigned)time(NULL));

/*generate random numbers between -1.0 and +1.0 (exclusive)*/ /*to obtain a random (x, y) point*/

x = random_double() * 2.0 – 1.0; y = random_double() * 2.0 -1.0;

/* is (x, y) point within the circle ? */ if ( sqrt(x*x + y*y)< 1.0 )

++hit_count; /* yes, (x, y) point is in circle */

  1. Program-2: Repeat Program-1 but instead of using a separatethread to generate random points, use OpenMP to parallelize thegeneration of points. Be careful not to place the calculation of in the parallel region, since you want to calculate pi onlyonce.

  2. Program-3. Program-1 asked you to design a multi-threadedprogram that estimated pi using the Monte Carlo technique. InProgram-1, you were asked to create a single slave-thread thatgenerated random points, storing the results in a global variablecircle_count. Once that slave-thread exited, the master-threadperformed the calculation that estimated the value of pi. ModifyProgram-1 so that you create several slave-threads, each of whichgenerates random points and determines if the points fall withinthe circle. Each slave- thread will have to update the global countof all points that fall within the circle. Protect against raceconditions on updates to the shared global variable by using mutexlocks.

    Note: each slave-thread must generate points_per_thread randompoints, which is the ratio of the total number of random points tothe number of slave-threads.

  3. Program-4. Program-2 asked you to design a program using OpenMPthat estimated pi using the Monte Carlo technique. Examine yoursolution to Program-2 looking for any possible race conditions. Ifyou identify a race condition, protect against it using thestrategy outline in the OpenMP section of Chapter-5 of thetextbook.

  

Let the constant NUMBER_OF_POINTS be the total number of random(x, y) points and the constant NUMBER_OF_SLAVES be the number ofslave-threads.

  1. Run Program-1 and Program-2 with NUMBER_OF_POINTS = 100, 1000,10000, 100000, 1000000, and return for each run both theirexecution times and their estimated pi values. Write a short reportin which you compare the running times and the estimated pi values,and provide meaningful comments (one or two paragraphs) on yourresults. The comments should address the running times obtained andrelate them to the concepts learned in class: (i) yourmulti-threaded Program-1 and Program-2 should run faster than anon-threaded program; (ii) Program-2 which uses OpenMP should runfaster than Program-1; and (iii) a larger value of NUMBER_OF_POINTSshould give a more accurate estimation of pi than a smallervalue.

  2. Run Program-3 and Program-4 with NUMBER_OF_POINTS = 1000000 andNUMBER_OF_SLAVES = 2, 20, 40, 80, 100, and return for each run boththeir execution times and their estimated pi values. Write a shortreport in which you compare the running times and the estimated pivalues, and provide meaningful comments (one or two paragraphs) onyour results. The comments should address the running timesobtained and relate them to the concepts learned in class: (i) yourmulti-threaded Program-3 and Program-4 should run faster thanProgram-1 and Program-2; (ii) Program-4 which uses OpenMP shouldrun faster than Program-3; (iii) a larger value of NUMBER_OF_SLAVESshould give a faster running time than a smaller value; and (iv)what can you say about the estimated pi values as NUMBER_OF_SLAVESchanges from 2 to 100?

Expert Answer


Answer to Program-1. An interesting way of calculating pi is to use a technique known as Monte Carlo, which involves randomization…

OR