Increading the number of threads of execution

Hi, I am trying to learn multi-threaded programming on Julia, and I am trying to increase the number of threads to 4 (since my computer has 4 cores), by typing
on my terminal. I am on CentOs (linux) and my Lulia version is 1.0.0. This however, does not seem to have any effect, as when I do
julia> Threads.nthreads()
It shows that the number of threads is till 1.

I would appreciate any help on help me overcome this basic obstacle.

Also, I am surprised that none of the documentations address this issue. For a language designed for computational math, it is surprising that it lacks this basic feature of setting the number of threads from within a julia program.

It works for me:

rdeits@laptop-rdeits01:~$ julia
<banner omitted>
julia> Threads.nthreads()

rdeits@laptop-rdeits01:~$ export JULIA_NUM_THREADS=4
rdeits@laptop-rdeits01:~$ julia
<banner omitted> 
julia> Threads.nthreads()

Note that (as with any environment variable), you need to do the export and then launch Julia from the same terminal session (in that order). Are you sure you’re doing that?


Thank you. I did that and it worked.
I also noticed that if I relaunch julia after a reboot, the number of threads is back to 1.
This means that julia has to be reconfigured every time it is run. I was hoping for a way to detect the number N of processors during runtime, and set the number of threads to N from with the program.
That way, the code / program would be more portable.

Yes, that’s expected. export sets an environment variable for the duration of your current terminal session (this is not a Julia-specific issue, but simply how your shell works). The typical approach if you want to make such a configuration change permanent is to add the export command to your ~/.bashrc file.

As for detecting and changing the number of threads while the program is running, I agree that such a thing would be pretty cool, but I’m not sure it’s currently possible.


Instead of depending upon your shell to remember your JULIA_NUM_THREADS environment variable, you can just set it when you invoke Julia:


Now, that’s still a shell-specific syntax. In certain situations that expect a single command (and arguments) this won’t work. I imagine CentOS includes the env command, which can do this entire thing through as arguments to a single program:

$ env JULIA_NUM_THREADS=4 julia

You can even set this up as an alias for julia itself so that every time you type julia you get 4 threads set automatically.


A bit of a hack … Put this at the top of your startup.jl:

if Threads.nthreads() != 8
    atexit(() -> withenv(() -> run(`julia`), "JULIA_NUM_THREADS" => 8))


$ julia
   _       _ _(_)_     |  Documentation:
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.2.0-DEV.94 (2019-01-04)
 _/ |\__'_|_|_|\__'_|  |  Commit fb47697195 (70 days old master)
|__/                   |

julia> Threads.nthreads()



This is definitely not a good idea, because now any flags passed to the first julia won’t get passed along to the second one (although I’m not 100% sure of that because I haven’t tested it).


I know… it’s not a serious solution to the problem, it was meant as an amusing hack.

1 Like

I could tell, but I just wanted to make sure anyone who hadn’t also thought through the implications was aware of that as well :slight_smile:

1 Like

See also: