I'm preparing a small presentation in Ipython where I want to show how easy it is to do parallel operation in Julia.
It's basically a Monte Carlo Pi calculation described here
The problem is that I can't make it work in parallel inside an IPython (Jupyter) Notebook, it only uses one.
I started Julia as:
julia -p 4
If I define the functions inside the REPL and run it there it works ok.
@everywhere function compute_pi(N::Int) """ Compute pi with a Monte Carlo simulation of N darts thrown in [-1,1]^2 Returns estimate of pi """ n_landed_in_circle = 0 for i = 1:N x = rand() * 2 - 1 # uniformly distributed number on x-axis y = rand() * 2 - 1 # uniformly distributed number on y-axis r2 = x*x + y*y # radius squared, in radial coordinates if r2 < 1.0 n_landed_in_circle += 1 end end return n_landed_in_circle / N * 4.0 end
function parallel_pi_computation(N::Int; ncores::Int=4) """ Compute pi in parallel, over ncores cores, with a Monte Carlo simulation throwing N total darts """ # compute sum of pi's estimated among all cores in parallel sum_of_pis = @parallel (+) for i=1:ncores compute_pi(int(N/)) end return sum_of_pis / ncores # average value end
julia> @time parallel_pi_computation(int(1e9)) elapsed time: 2.702617652 seconds (93400 bytes allocated) 3.1416044160000003
But when I do:
using IJulia notebook()
And try to do the same thing inside the Notebook it only uses 1 core:
In : @time parallel_pi_computation(int(10e8)) elapsed time: 10.277870808 seconds (219188 bytes allocated) Out: 3.141679988
So, why isnt Jupyter using all the cores? What can I do to make it work?