←PPNM
Exercise Multiprocessing
Monte Carlo method
Suppose you can throw darts, one after one, into a 1x1 square target
with such a bad precision that your darts cover the square uniformly.
Then you can calculate the number π in the following way:
- inscribe a circle within your square;
- throw sufficiently large number N of darts;
- count the number Nin of darts within the circle;
- the number π is then approximately given as
π = 4*Nin/N.
Exercises:
- (60%) Build a multiprocessing program with pthreads that implements
the above algorithm. Hint: you should not use the
rand
random number generator because it is not necessarily thread-safe:
read man rand_r
and use rand_r
instead.
- (30%) Do the same using OpemMP.
- (9%) Investigate the convergence of the result as function of N.
- (1%) Do the above exercises using one of the quasi-random
(low-descrepancy) generators from GSL.