←to practical programming

Exercise Multiprocessing

You probably need to
sudo apt install libomp-dev

Monte Carlo method

Estimate the number π using [this] Monte Carlo method:

  1. Draw a 1x1 square and inscribe a circle (or a quadrand) within your square;
  2. Uniformly scatter N points over the square;
  3. Count the number Nin points within the circle/quadrant;
  4. The number π is then approximately given as π = 4*Nin/N.

Exercises:

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