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

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

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

julia> Threads.nthreads()
1

Still not clear since that information is not in the documentation here. I was expecting that these values could be predefined prior to startup.

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.

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

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_NUM_THREADS=6 julia

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

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

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.

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. %-)

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.

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

xref: julia - Setting nprocs() using the ./startup.jl file - Stack Overflow

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.

1 Like

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

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

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

1 Like

The docs can answer all of the questions in this thread.

1 Like
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. :slight_smile:

I think the reason your versioninfo() shows

Environment:
  JULIA_NUM_THREADS = 5
  JULIA_CPU_THREADS = 5

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

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

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

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

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

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

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)

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