Starting Julia with multiple threads on Mac M1

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

How can I setup JULIA_NUM_THREADS=6?

@carstenbauer in Julia REPL, I set ENV[“JULIA_NUM_THREADS”] = 6 and when I query nthreads(), the printed value is 1 instead of 6.

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.

@skleinbo where I can initialize command Run `export JULIA_NUM_THREADS=6? Do you mean I run command in REPL?

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”.

@carstenbauer I open terminal and type JULIA_NUM_THREADS=6 as can be seen in attach screenshot:


Then I open REPL and type
using Base.Threads
nthreads()
and get nthreads=1 instead of 6. I am still not sure how to set nthreads()=6?

Could you try export JULIA_NUM_THREADS=6?

$ export JULIA_NUM_THREADS=2
$ julia
julia> Threads.nthreads()
2

But a simpler solution may just be to use the -t flag while launching Julia.

To set the number of threads launched by the REPL in VSCode, set the appropriate option in the VSCode extension

image

1 Like

@skleinbo in terminal when I type JULIA_NUM_THREADS=6 julia, I get error (see screenshot):

1 Like

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.

1 Like

How do you start the REPL? To be clear, you should start it by running julia in the same terminal.

Also note that you should either do

export JULIA_NUM_THREADS=6
julia

or

JULIA_NUM_THREADS=6 julia

(in you screenshot you just have JULIA_NUM_THREADS=6)

@skleinbo Thanks for that. Julia was not in the path so I was getting error, now I added in path and can access julia via terminal.

@carstenbauer now I can set threads flag to 6 in terminal:

After this I open the jupyter nootbook and type Threads.nthreads() which give 1 instead of 6.

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.

  1. 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.

  2. Start jupyter via the terminal as follows: JULIA_NUM_THREADS=6 jupyter

  3. 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"))
5 Likes

@skleinbo @carstenbauer Thanks for helping me out! I am all set.

1 Like