Practice Multi-threading

Last updated: January 12, 2023

Launching Julia on multiple threads

To use Julia on multiple threads, you need to set the JULIA_NUM_THREADS environment variable.

This can be done by running (in the terminal, not in Julia):

$ export JULIA_NUM_THREADS=n      # n is the number of threads you want to use

Or by launching Julia with (again, in the terminal):

$ JULIA_NUM_THREADS=n julia

For example, to launch Julia on 4 threads, you can run:

$ JULIA_NUM_THREADS=4 julia

Note 1:

The Juno IDE automatically launches Julia on all your available threads.

Note 2:

Windows users can also set this environment variable with CMD with:

set JULIA_NUM_THREADS=n

or with Powershell with:

$env:JULIA_NUM_THREADS=n

How do you know how many threads are available on your machine?

A lot of laptops around have 4 threads (2 CPU, each with 2 threads). Utilities such as htop allow you to visualize the working threads.

Linux

From a terminal:

$ nproc

Windows

Go to System Information . This will show you the System Summary in which you can see a description of your Processor .

MacOS

From Terminal:

sysctl -n hw.ncpu

Using multiple threads

Multi-threading is supported by the Base.Threads interface (part of the Base module of Julia) which contains a number of functions and macros. Below are the most important ones.

How do you know how many threads you are using in your Julia session?

To see how many threads you are using, you can run (in the Julia REPL):

julia> Threads.nthreads()

Note: if you could not determine how many threads you have on your machine with the methods above, you can try to launch Julia on a number of threads larger than what you can possibly have (e.g. 10) and run this command. Julia will be launched on all your available threads and this command will tell you how many that is.

To get the ID of the current thread, you can run:

julia> Threads.threadid()

For loop on multiple threads

This is done thanks to the Threads.@threads macro:

julia> Threads.@threads for i = 1:10
           println("i = $i on thread $(Threads.threadid())")
       end

Multi-threading outside of the context of loops

Here, you want to use the Threads.@spawn macro. This feature is currently experimental and not documented in the Julia manual, but an example is given in this Julia blog post.

Comments & questions