←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:
- Draw a 1x1 square and inscribe a circle (or a quadrand)
within your square;
- Uniformly scatter N points over the square;
- Count the number Nin points within the circle/quadrant;
- The number π is then approximately given as
π = 4*Nin/N.
Exercises:
- (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.
- (Optional) Do the same using OpemMP.
- (Optional) Investigate the convergence of the result as function of N (probably 1/√N).
- (Optional) Do the above exercises using one of the quasi-random
(low-descrepancy) generators from GSL.