# Setting \`nthreads()\` using the .julia/config/startup.jl file

**URL:** <https://discourse.julialang.org/t/setting-nthreads-using-the-julia-config-startup-jl-file/35427>\
**Category:** General Usage\
**Created:** [March 2, 2020, 8:16pm UTC](https://discourse.julialang.org/t/setting-nthreads-using-the-julia-config-startup-jl-file/35427 "2020-03-02T20:16:49Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![oliver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oliver/32/19264_2.png) [@oliver](https://discourse.julialang.org/u/oliver)\
**Post date:** [March 2, 2020, 8:16pm UTC](https://discourse.julialang.org/t/setting-nthreads-using-the-julia-config-startup-jl-file/35427/1 "2020-03-02T20:16:49Z")

</div>

Why does setting `ENV[JULIA_CPU_THREADS]=5` and `ENV[JULIA_NUM_THREADS]=5` not affect `nprocs()` ? Here is what I get on startup:

```julia
julia> versioninfo()
Julia Version 1.3.1
Commit 2d5741174c (2019-12-30 21:36 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i5-9600K CPU @ 3.70GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, skylake)
Environment:
  JULIA_CPU_THREADS = 5
  JULIA_NUM_THREADS = 5
julia> nprocs()
1

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [March 2, 2020, 8:22pm UTC](https://discourse.julialang.org/t/setting-nthreads-using-the-julia-config-startup-jl-file/35427/2 "2020-03-02T20:22:55Z")

</div>

`JULIA_NUM_THREADS` controls `Threads.nthreads()`, `Distributed.nprocs()` is the number of slave processes (on possibly remote machines).

---

<div class="post-metadata">

**Author:** ![oliver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oliver/32/19264_2.png) [@oliver](https://discourse.julialang.org/u/oliver)\
**Post date:** [March 2, 2020, 8:28pm UTC](https://discourse.julialang.org/t/setting-nthreads-using-the-julia-config-startup-jl-file/35427/3 "2020-03-02T20:28:48Z")

</div>

```julia
julia> Threads.nthreads()
1

```

Still not clear since that information is not in the [documentation here](https://docs.julialang.org/en/v1/manual/environment-variables/#Parallelization-1). I was expecting that these values could be predefined prior to startup.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [March 2, 2020, 8:35pm UTC](https://discourse.julialang.org/t/setting-nthreads-using-the-julia-config-startup-jl-file/35427/4 "2020-03-02T20:35:52Z")

</div>

How did you set those environment variables? If you set them like `ENV[...] = ... ` in the REPL, that’s not going to affect the runtime since at the moment the value of those variables at startup determines the number of available threads. For now, this cannot be changed at runtime.

---

<div class="post-metadata">

**Author:** ![oliver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oliver/32/19264_2.png) [@oliver](https://discourse.julialang.org/u/oliver)\
**Post date:** [March 2, 2020, 8:37pm UTC](https://discourse.julialang.org/t/setting-nthreads-using-the-julia-config-startup-jl-file/35427/5 "2020-03-02T20:37:05Z")

</div>

I modified the `startup.jl` file prior to launching Julia in a Bash terminal.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [March 2, 2020, 8:38pm UTC](https://discourse.julialang.org/t/setting-nthreads-using-the-julia-config-startup-jl-file/35427/6 "2020-03-02T20:38:57Z")

</div>

`startup.jl` is a regular julia file like any other, it’s just run during startup and thus is runtime dependant. If you want to start julia with a greater number of threads you have to set that environment variable prior to any execution of julia code, e.g. via `export JULIA_NUM_THREADS=6` for your CPU in bash or by running julia like this:

```julia
> JULIA_NUM_THREADS=6 julia

```

if you only want to set it for the duration that process is alive.

---

<div class="post-metadata">

**Author:** ![oliver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oliver/32/19264_2.png) [@oliver](https://discourse.julialang.org/u/oliver)\
**Post date:** [March 2, 2020, 8:41pm UTC](https://discourse.julialang.org/t/setting-nthreads-using-the-julia-config-startup-jl-file/35427/7 "2020-03-02T20:41:36Z")

</div>

How would it be that such environment variable(s) are carried into `versioninfo()` on startup but not affecting my JIT settings?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [March 2, 2020, 8:43pm UTC](https://discourse.julialang.org/t/setting-nthreads-using-the-julia-config-startup-jl-file/35427/8 "2020-03-02T20:43:44Z")

</div>

`versioninfo()` reads whatever it displays at the time of calling it - you set the value in the `ENV` dictionary prior to running `versioninfo()` and thus it displays the new value. Setting that value will not spawn new threads at the moment, this is a known limitation since the `Threads` module is still a little bit in flux.

---

<div class="post-metadata">

**Author:** ![oliver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oliver/32/19264_2.png) [@oliver](https://discourse.julialang.org/u/oliver)\
**Post date:** [March 2, 2020, 8:45pm UTC](https://discourse.julialang.org/t/setting-nthreads-using-the-julia-config-startup-jl-file/35427/9 "2020-03-02T20:45:32Z")

</div>

```julia
oliver@debian:~$ JULIA_NUM_THREADS=4 julia
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.3.1 (2019-12-30)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |
julia> versioninfo()
Julia Version 1.3.1
Commit 2d5741174c (2019-12-30 21:36 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i5-9600K CPU @ 3.70GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, skylake)
Environment:
  JULIA_NUM_THREADS = 5
  JULIA_CPU_THREADS = 5
  JULIA_CMDSTAN_HOME = /home/oliver/Library/cmdstan-2.22.1
julia> Threads.nthreads()
4

```

Sure, I only ask because I’m confused, nothing world breaking. %-)

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 2, 2020, 8:48pm UTC](https://discourse.julialang.org/t/setting-nthreads-using-the-julia-config-startup-jl-file/35427/10 "2020-03-02T20:48:29Z")

</div>

> [@oliver](#):
>
> `versioninfo()` on startup

It did not happen “on startup”. In fact, nothing you’ve ever said in this thread happens “on startup”. And you can say the info is wrong since it’s not guarded against improperly setting of the environment variables.

> [@oliver](#):
>
> but not affecting my JIT settings?

And FWIW, it’s not a JIT setting. Again, nothing ever mentioned in this thread has anything to do witht JIT or the compiler…

---

<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:** [March 2, 2020, 8:52pm UTC](https://discourse.julialang.org/t/setting-nthreads-using-the-julia-config-startup-jl-file/35427/11 "2020-03-02T20:52:43Z")

</div>

xref: [julia - Setting nprocs() using the ./startup.jl file - Stack Overflow](https://stackoverflow.com/questions/60496064/setting-nprocs-using-the-startup-jl-file)

You already have an answer on stackoverflow. Please try to avoid cross posting on SO, Slack, and Discourse in the future. If you do it nonetheless, please cross reference the posts such that the discussions happening at those different places are linked.

---

<div class="post-metadata">

**Author:** ![oliver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oliver/32/19264_2.png) [@oliver](https://discourse.julialang.org/u/oliver)\
**Post date:** [March 2, 2020, 8:52pm UTC](https://discourse.julialang.org/t/setting-nthreads-using-the-julia-config-startup-jl-file/35427/12 "2020-03-02T20:52:50Z")

</div>

Thanks! I’d have to think about this a little more:

```julia
oliver@debian:~$ JULIA_NUM_THREADS=100 julia
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.3.1 (2019-12-30)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |
julia> versioninfo()
Julia Version 1.3.1
Commit 2d5741174c (2019-12-30 21:36 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i5-9600K CPU @ 3.70GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, skylake)
Environment:
  JULIA_NUM_THREADS = 5
  JULIA_CPU_THREADS = 5
julia> Threads.nthreads()
6

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 2, 2020, 8:59pm UTC](https://discourse.julialang.org/t/setting-nthreads-using-the-julia-config-startup-jl-file/35427/13 "2020-03-02T20:59:48Z")

</div>

Whatever you have in your startup is not helping or doing anything. Just get rid of it.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [March 2, 2020, 9:05pm UTC](https://discourse.julialang.org/t/setting-nthreads-using-the-julia-config-startup-jl-file/35427/14 "2020-03-02T21:05:13Z")

</div>

The [docs](https://docs.julialang.org/en/v1/manual/environment-variables/#JULIA_NUM_THREADS-1) can answer all of the questions in this thread.

---

<div class="post-metadata">

**Author:** ![oliver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oliver/32/19264_2.png) [@oliver](https://discourse.julialang.org/u/oliver)\
**Post date:** [March 2, 2020, 9:07pm UTC](https://discourse.julialang.org/t/setting-nthreads-using-the-julia-config-startup-jl-file/35427/15 "2020-03-02T21:07:24Z")

</div>

```julia
Environment:
...
  JULIA_CMDSTAN_HOME = /home/oliver/Library/cmdstan-2.22.1

```

In the `startup.jl` is both required and working as needed so I’ll keep that one at least. 🙂

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [March 2, 2020, 9:36pm UTC](https://discourse.julialang.org/t/setting-nthreads-using-the-julia-config-startup-jl-file/35427/16 "2020-03-02T21:36:06Z")

</div>

I think the reason your `versioninfo()` shows

```julia
Environment:
  JULIA_NUM_THREADS = 5
  JULIA_CPU_THREADS = 5

```

is just because you set those environment variables. They are meaningless. Here is mine:

```julia
julia> versioninfo()
Julia Version 1.3.1
Commit 2d5741174c (2019-12-30 21:36 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i7-6700 CPU @ 3.40GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, skylake)
Environment:
  JULIA_RUNATLIGHTSPEED = 1

```

---

<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:** [March 2, 2020, 9:36pm UTC](https://discourse.julialang.org/t/setting-nthreads-using-the-julia-config-startup-jl-file/35427/17 "2020-03-02T21:36:56Z")

</div>

Indeed. `versioninfo()` just displays all environmental variables starting with `JULIA_` (independent of whether they have a meaning or not).

---

<div class="post-metadata">

**Author:** ![oliver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oliver/32/19264_2.png) [@oliver](https://discourse.julialang.org/u/oliver)\
**Post date:** [March 3, 2020, 12:24am UTC](https://discourse.julialang.org/t/setting-nthreads-using-the-julia-config-startup-jl-file/35427/18 "2020-03-03T00:24:37Z")

</div>

I mean sure… `versioninfo()` is where the terminal options are going.

```julia
oliver@debian:~$ julia
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.3.1 (2019-12-30)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |
julia> versioninfo()
Julia Version 1.3.1
Commit 2d5741174c (2019-12-30 21:36 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i5-9600K CPU @ 3.70GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, skylake)
julia> Threads.nthreads()
1
julia> exit()
oliver@debian:~$ JULIA_NUM_THREADS=4 julia
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.3.1 (2019-12-30)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |
julia> versioninfo()
Julia Version 1.3.1
Commit 2d5741174c (2019-12-30 21:36 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i5-9600K CPU @ 3.70GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.1 (ORCJIT, skylake)
Environment:
  JULIA_NUM_THREADS = 4
julia> Threads.nthreads()
4

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 3, 2020, 12:44am UTC](https://discourse.julialang.org/t/setting-nthreads-using-the-julia-config-startup-jl-file/35427/19 "2020-03-03T00:44:16Z")

</div>

So do you have any more questions that needs to be clarified? It seems that you’ve got rid of the code that sets the environment variable in startup.jl that has no effect.

For the record, the new title,

> [Setting `nthreads()` using the .julia/config/startup.jl file](https://discourse.julialang.org/t/setting-nthreads-using-the-julia-config-startup-jl-file/35427)

is impossible right now. And it’s never going to be possible without the capability of adding thread at runtime. (Which in itself is a feature that is useful and will probably be implemented at some point)

---

<div class="post-metadata">

**Author:** ![oliver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oliver/32/19264_2.png) [@oliver](https://discourse.julialang.org/u/oliver)\
**Post date:** [March 3, 2020, 12:55am UTC](https://discourse.julialang.org/t/setting-nthreads-using-the-julia-config-startup-jl-file/35427/20 "2020-03-03T00:55:19Z")

</div>

How is it different to specify an environment variable in `startup.jl` than from the terminal?

[Next page](https://discourse.julialang.org/t/setting-nthreads-using-the-julia-config-startup-jl-file/35427.md?page=2)
