# L-3 MCS 572 Fri 30 Aug 2024 : estimatepidp.jl # Runs a Monte Carlo simulation with multiprocessing, # using the Distributed module of Julia. # Launch this script as julia -p estimatepidp.jl, # or as time julia --procs=16 estimatepidp.jl to get also # the wall clock time, where p is the total number # of workers, the number of processes is then p+1. using Distributed, Statistics @everywhere function estimatepi(n) # # Runs a simple Monte Carlo method # to estimate pi with n samples. # count = 0 for i=1:n x = rand() y = rand() count += (x^2 + y^2) <= 1 end return 4*count/n end parallelpi(N) = mean(pmap(n->estimatepi(n), [N/nworkers() for i=1:nworkers()])); np = nprocs() nw = nworkers() println("number of processes : $np") println("number of workers : $nw") timestart = time() estpi = parallelpi(10_000_000_000) elapsed = time() - timestart println("The estimate for Pi : $estpi") println("The elapsed time : $elapsed seconds")