# L-11 MCS 507 Fri 15 Sep 2023 : 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 -p N estimatepidp.jl to get also # the wall clock time, where N is the total number # of workers, the number of processes is then N+1. using Distributed, Statistics # @everywhere function estimatepi(n) # # Runs a simple Monte Carlo method # to estimate pi with n samples. @everywhere function estimatepi(n) 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")