# Starting Julia with multiple threads on Mac M1

**URL:** <https://discourse.julialang.org/t/starting-julia-with-multiple-threads-on-mac-m1/92906>\
**Category:** New to Julia\
**Tags:** multithreading\
**Created:** [January 13, 2023, 5:55am UTC](https://discourse.julialang.org/t/starting-julia-with-multiple-threads-on-mac-m1/92906 "2023-01-13T05:55:52Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![sajjad\_khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sajjad_khan/32/45202_2.png) [@sajjad\_khan](https://discourse.julialang.org/u/sajjad_khan)\
**Post date:** [January 13, 2023, 5:55am UTC](https://discourse.julialang.org/t/starting-julia-with-multiple-threads-on-mac-m1/92906/1 "2023-01-13T05:55:52Z")

</div>

![Screen Shot 2023-01-12 at 10.47.26 PM](https://global.discourse-cdn.com/julialang/original/3X/e/6/e64e8b4ae72ba014479631bea1649873bbeafc6e.png)

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

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [January 13, 2023, 6:07am UTC](https://discourse.julialang.org/t/starting-julia-with-multiple-threads-on-mac-m1/92906/2 "2023-01-13T06:07:14Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![sajjad\_khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sajjad_khan/32/45202_2.png) [@sajjad\_khan](https://discourse.julialang.org/u/sajjad_khan)\
**Post date:** [January 13, 2023, 6:30am UTC](https://discourse.julialang.org/t/starting-julia-with-multiple-threads-on-mac-m1/92906/3 "2023-01-13T06:30:58Z")

</div>

> [@carstenbauer](#):
>
> JULIA\_NUM\_THREADS=6

How can I setup JULIA\_NUM\_THREADS=6?

---

<div class="post-metadata">

**Author:** ![sajjad\_khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sajjad_khan/32/45202_2.png) [@sajjad\_khan](https://discourse.julialang.org/u/sajjad_khan)\
**Post date:** [January 13, 2023, 6:40am UTC](https://discourse.julialang.org/t/starting-julia-with-multiple-threads-on-mac-m1/92906/4 "2023-01-13T06:40:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [January 13, 2023, 7:09am UTC](https://discourse.julialang.org/t/starting-julia-with-multiple-threads-on-mac-m1/92906/5 "2023-01-13T07:09:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sajjad\_khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sajjad_khan/32/45202_2.png) [@sajjad\_khan](https://discourse.julialang.org/u/sajjad_khan)\
**Post date:** [January 13, 2023, 7:13am UTC](https://discourse.julialang.org/t/starting-julia-with-multiple-threads-on-mac-m1/92906/6 "2023-01-13T07:13:50Z")

</div>

@skleinbo where I can initialize command Run `export JULIA\_NUM\_THREADS=6? Do you mean I run command in REPL?

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [January 13, 2023, 7:15am UTC](https://discourse.julialang.org/t/starting-julia-with-multiple-threads-on-mac-m1/92906/7 "2023-01-13T07:15:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sajjad\_khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sajjad_khan/32/45202_2.png) [@sajjad\_khan](https://discourse.julialang.org/u/sajjad_khan)\
**Post date:** [January 13, 2023, 7:38am UTC](https://discourse.julialang.org/t/starting-julia-with-multiple-threads-on-mac-m1/92906/8 "2023-01-13T07:38:01Z")

</div>

@carstenbauer I open terminal and type JULIA\_NUM\_THREADS=6 as can be seen in attach screenshot:

 ![Screen Shot 2023-01-13 at 12.34.17 AM](https://global.discourse-cdn.com/julialang/original/3X/f/d/fd5c53c73d20c7a621209a6ecb4dc7f96a59c6be.png)  
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?

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [January 13, 2023, 7:41am UTC](https://discourse.julialang.org/t/starting-julia-with-multiple-threads-on-mac-m1/92906/9 "2023-01-13T07:41:40Z")

</div>

Could you try `export JULIA_NUM_THREADS=6`?

```julia
$ 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.

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [January 13, 2023, 7:49am UTC](https://discourse.julialang.org/t/starting-julia-with-multiple-threads-on-mac-m1/92906/10 "2023-01-13T07:49:51Z")

</div>

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

 ![image](https://global.discourse-cdn.com/julialang/original/3X/7/7/774cd8ea83752f5039c8751bbcec6fb260cac69f.png)

![image](https://global.discourse-cdn.com/julialang/original/3X/e/7/e7b4fda2d0e5a50c167ee02593cfa59a91ae52f5.png)

---

<div class="post-metadata">

**Author:** ![sajjad\_khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sajjad_khan/32/45202_2.png) [@sajjad\_khan](https://discourse.julialang.org/u/sajjad_khan)\
**Post date:** [January 13, 2023, 7:53am UTC](https://discourse.julialang.org/t/starting-julia-with-multiple-threads-on-mac-m1/92906/11 "2023-01-13T07:53:56Z")

</div>

> [@skleinbo](#):
>
> JULIA\_NUM\_THREADS=6 julia

@skleinbo in terminal when I type JULIA\_NUM\_THREADS=6 julia, I get error (see screenshot):

 ![Screen Shot 2023-01-13 at 12.52.08 AM](https://global.discourse-cdn.com/julialang/original/3X/e/2/e2841753990260d55c14301a6606c6b6aaf31298.png)

---

<div class="post-metadata">

**Author:** ![skleinbo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skleinbo/32/36080_2.png) [@skleinbo](https://discourse.julialang.org/u/skleinbo)\
**Post date:** [January 13, 2023, 8:07am UTC](https://discourse.julialang.org/t/starting-julia-with-multiple-threads-on-mac-m1/92906/12 "2023-01-13T08:07:58Z")

</div>

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](https://julialang.org/downloads/platform/#macos)  
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](https://github.com/JuliaLang/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`.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [January 13, 2023, 8:08am UTC](https://discourse.julialang.org/t/starting-julia-with-multiple-threads-on-mac-m1/92906/13 "2023-01-13T08:08:30Z")

</div>

> [@sajjad\_khan](#):
>
> Then I open REPL and type

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

```julia
export JULIA_NUM_THREADS=6
julia

```

or

```julia
JULIA_NUM_THREADS=6 julia

```

(in you screenshot you just have `JULIA_NUM_THREADS=6`)

---

<div class="post-metadata">

**Author:** ![sajjad\_khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sajjad_khan/32/45202_2.png) [@sajjad\_khan](https://discourse.julialang.org/u/sajjad_khan)\
**Post date:** [January 13, 2023, 8:29am UTC](https://discourse.julialang.org/t/starting-julia-with-multiple-threads-on-mac-m1/92906/14 "2023-01-13T08:29:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sajjad\_khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sajjad_khan/32/45202_2.png) [@sajjad\_khan](https://discourse.julialang.org/u/sajjad_khan)\
**Post date:** [January 13, 2023, 8:32am UTC](https://discourse.julialang.org/t/starting-julia-with-multiple-threads-on-mac-m1/92906/15 "2023-01-13T08:32:53Z")

</div>

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

 ![Screen Shot 2023-01-13 at 1.28.33 AM](https://global.discourse-cdn.com/julialang/original/3X/4/b/4b8ffcab0e31a99f489905a0445b0cff55c7757d.png)

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

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [January 13, 2023, 8:43am UTC](https://discourse.julialang.org/t/starting-julia-with-multiple-threads-on-mac-m1/92906/16 "2023-01-13T08:43:43Z")

</div>

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](https://julialang.github.io/IJulia.jl/stable/manual/installation/#Installing-additional-Julia-kernels) that automatically sets `JULIA_NUM_THREADS` before it starts the kernel.

```julia
using IJulia
installkernel("Julia (6 threads)", env=Dict("JULIA_NUM_THREADS"=>"6"))

```

---

<div class="post-metadata">

**Author:** ![sajjad\_khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sajjad_khan/32/45202_2.png) [@sajjad\_khan](https://discourse.julialang.org/u/sajjad_khan)\
**Post date:** [January 13, 2023, 9:45am UTC](https://discourse.julialang.org/t/starting-julia-with-multiple-threads-on-mac-m1/92906/17 "2023-01-13T09:45:24Z")

</div>

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