Parallel computing in Julia - running a simple for-loop on multiple cores
Just to add the example to the answer of Chris. Since the release of julia 1.3 you do this easily with Threads.@threads
Threads.@threads for N in 1:5:20
println("The number of this iteration is $N")
end
Here you are running only one julia session with multiple threads instead of using Distributed
where you run multiple julia sessions.
See, e.g. multithreading blog post for more information.
Distributed Processing
Start julia with e.g. julia -p 4
if you want to use 4 cpus (or use the function addprocs(4)
). In Julia 1.x, you make a parallel loop as following:
using Distributed
@distributed for N in 1:5:20
println("The N of this iteration in $N")
end
Note that every process have its own variables per default. For any serious work, have a look at the manual https://docs.julialang.org/en/v1.4/manual/parallel-computing/, in particular the section about SharedArrays.
Another option for distributed computing are the function pmap
or the package MPI.jl
.
Threads
Since Julia 1.3, you can also use Threads as noted by wueli.
Start julia with e.g. julia -t 4
to use 4 threads. Alternatively you can or set the environment variable JULIA_NUM_THREADS
before starting julia.
For example Linux/Mac OS:
export JULIA_NUM_THREADS=4
In windows, you can use set JULIA_NUM_THREADS 4
in the cmd prompt.
Then in julia:
Threads.@threads for N = 1::20
println("N = $N (thread $(Threads.threadid()) of out $(Threads.nthreads()))")
end
All CPUs are assumed to have access to shared memory in the examples above (e.g. "OpenMP style" parallelism) which is the common case for multi-core CPUs.