How to start multiple threads in Julia?

Viewed 626

I use Julia1.7 under Windows 10 and to start multiple threads according to the documentation I should do:

$ julia --threads 4

Where should I enter such command? When I start Julia the environment looks like: enter image description here

It does not have the $ sign.

2 Answers

You can't set the number of threads in an already running Julia session. As the comments suggests, you should pass the --threads flag to julia when starting, i.e. the command is entered in your shell (if you're on Windows most likely PowerShell, or the new Windows Terminal, or cmd).

If you are using either of the VSCode Julia extension or Juno (the older, Atom based Julia IDE) there are settings which allow you to choose the number of threads. A screenshot from VSCode settings:

enter image description here

Alternatively, threads can be set in settings.json, where an entry like

"julia.NumThreads": "auto"

should be added.

When using Jupyter notebooks through IJulia, you can register a kernel with multiple threads by doing

julia> using IJulia

julia> installkernel("Julia (4 threads)", env = Dict("JULIA_NUM_THREADS" => "4"))

When working in Pluto notebooks, you can set the number of threads when starting the notebook like so:

Pluto.run(threads=16)

You can also use the shell variable JULIA_NUM_THREADS, and set it equal to the number of threads that you want.

Related