How to setup multiple threads in Mac M1. The versioninfo() prints 6 virtual cores and when I set
using Base.Threads
Threads.nthreads()=6
and query # of set threads using nthreads(), I get 6 but when I want to use those threads using @threads for i = 1:6
println(“Hello from Thread:”, Threads.threadid())
end
I get:
Hello from Thread:1
Hello from Thread:1
Hello from Thread:1
Hello from Thread:1
Hello from Thread:1
Hello from Thread:1
whereas I was expecting to get Hello from Thread:(thread # changes, say 1,4,2,5,3,6). Any quick fix to this? Thanks
You need to specify the desired number of threads when starting Julia, e.g. via julia -t 6, or by setting the environment variable JULIA_NUM_THREADS=6.
Specifically, you don’t want to redefine Threads.nthreads()=6
Otherwise, this just works for me (started Julia with 6 threads):
julia> Threads.nthreads()
6
julia> @threads for i in 1:nthreads()
println("Hello from thread ", threadid())
end
Hello from thread 2
Hello from thread 6
Hello from thread 3
Hello from thread 5
Hello from thread 4
Hello from thread 1
From the terminal run JULIA_NUM_THREADS=6 julia. This will only apply to that Julia process. If you want every Julia process launched from that terminal to run with six threads, run export JULIA_NUM_THREADS=6 and then julia.
If you want to launch every Julia session always with six threads, add export JULIA_NUM_THREADS=6 to ~/.bashrc. This should also work if you launch Julia from Applications on MacOS.
The environment variable must be set before you start Julia. As @skleinbo pointed out, you can use export to set the variable in the terminal session (which you then use to start julia). Alternatively, you can also use JULIA_NUM_THREADS=6 julia to set the variable only for this single call. But note that you can also just use julia -t 6 to start the Julia REPL with 6 threads.
To permanently set the number of threads, you can put the export JULIA_NUM_THREADS=6 into you .bashrc or .zshrc depending on which shell you’re using. This way, you’ll also get 6 Julia threads when starting Julia by “clicking on the Julia application”.
That probably means the Julia executable is not in your path. The VSCode extension is clever enough to look for it under /Applications/Julia-...; that’s why the built-in REPL works.
A procedure to add it to the path is described well here Platform Specific Instructions for Official Binaries
Note however that you will need to repeat this procedure when you update to Julia 1.9 some time in the future. I personally am a fan up juliaup which manages multiple Julia versions and keeps them up to date, instead of installing them by hand into /Applications/
By the way, since you are using zsh and not bash, you would need to add export JULIA_NUM_THREADS=6 to ~/.zshrc, not ~/.bashrc.
If you set an environment variable via export in the terminal, it will only be set within this session. That is, if you open jupyter in a different terminal or by “clicking on the jupyter application”, the environment variable won’t be set there.
There are three ways around it.
As I mentioned before, just put export JULIA_NUM_THREADS=6 into your ~/.bashrc or ~/.zshrc (depending on your shell, see echo $SHELL). This will “globally” set the environment variable such that it should be respected by all applications (including jupyter and julia itself) irrespective of how you start them.
Start jupyter via the terminal as follows: JULIA_NUM_THREADS=6 jupyter
Create an IJulia kernel that automatically sets JULIA_NUM_THREADS before it starts the kernel.
using IJulia
installkernel("Julia (6 threads)", env=Dict("JULIA_NUM_THREADS"=>"6"))