As of this writing, Julia supports three types of concurrency:

  1. Coroutines
  2. Multi-Threading
  3. Multi-Core or Distributed Processing

This post will explore multicore parallelization in Julia

Using multiple cores in julia

If more than one cores are to be used in julia, it must be specified, either when starting julia, using -p <n_cpus, for example

julia -p 8 # to use 8 cores

or by adding processors in an interactive session

using Distributed
addprocs(8)

By adding 8 processors, the number of processors available to Julia will be 9, including the main process. When starting julia with -p option, the module Distributed is loaded implicitly.

Example: Monte Carlo estimation of \(\pi\)

Serial and parallel implementations

Defining the serial versions

function estimate_pi(n=1000)
    points_inside = 0
    for i=1:n
        x = rand()
        y = rand()
        points_inside += Int(sqrt((x - 0.5)^2 + (y-0.5)^2) <= (1/2))
    end
    4* points_inside / n
end
## estimate_pi (generic function with 2 methods)

function estimate_pi_2(n=1000)
    points_inside = 0
    for i=1:n
        points_inside += Int(sqrt((rand() - 0.5)^2 + (rand()-0.5)^2) <= (1/2))
    end
    4* points_inside / n
end
## estimate_pi_2 (generic function with 2 methods)

Defining the parallel (distributed) versions

using Distributed
addprocs(4)
## 4-element Array{Int64,1}:
##  2
##  3
##  4
##  5

function pestimate_pi(n=1000)
    points_inside = 0
    points_inside = @distributed (+) for i=1:n
        x = rand()
        y = rand()
        Int(sqrt((x - 0.5)^2 + (y-0.5)^2) <= (1/2))
    end
    4* points_inside / n
end
## pestimate_pi (generic function with 2 methods)

function pestimate_pi_2(n=1000)
    points_inside = 0
    points_inside = @distributed (+) for i=1:n
        Int(sqrt((rand() - 0.5)^2 + (rand()-0.5)^2) <= (1/2))
    end
    4* points_inside / n
end
## pestimate_pi_2 (generic function with 2 methods)

Making sure that the functions are correctly implemented

estimate_pi(2000)
## 3.124
estimate_pi_2(2000)
## 3.144
pestimate_pi(2000)
## 3.142
pestimate_pi_2(2000)
## 3.128

Timing

Checking the number of workers available

workers()
## 4-element Array{Int64,1}:
##  2
##  3
##  4
##  5
@time estimate_pi(500000000)
##   2.184667 seconds (5 allocations: 176 bytes)
## 3.14159684
@time estimate_pi_2(500000000)
##   2.182281 seconds (5 allocations: 176 bytes)
## 3.141576544
@time pestimate_pi(500000000)
##   1.275921 seconds (1.35 k allocations: 92.555 KiB)
## 3.141555336
@time pestimate_pi_2(500000000)
##   1.241780 seconds (959 allocations: 65.945 KiB)
## 3.141478528