←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:

  1. inscribe a circle within your square;
  2. throw sufficiently large number N of darts;
  3. count the number Nin of darts within the circle;
  4. the number π is then approximately given as π = 4*Nin/N.

Exercises:

  1. (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.
  2. (30%) Do the same using OpemMP.
  3. (9%) Investigate the convergence of the result as function of N.
  4. (1%) Do the above exercises using one of the quasi-random (low-descrepancy) generators from GSL.