# Increading the number of threads of execution

**URL:** <https://discourse.julialang.org/t/increading-the-number-of-threads-of-execution/21925>\
**Category:** General Usage\
**Created:** [March 15, 2019, 7:31pm UTC](https://discourse.julialang.org/t/increading-the-number-of-threads-of-execution/21925 "2019-03-15T19:31:23Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)\
**Post date:** [March 15, 2019, 7:31pm UTC](https://discourse.julialang.org/t/increading-the-number-of-threads-of-execution/21925/1 "2019-03-15T19:31:23Z")

</div>

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  
**export JULIA\_NUM\_THREADS=4**  
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.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 15, 2019, 7:41pm UTC](https://discourse.julialang.org/t/increading-the-number-of-threads-of-execution/21925/2 "2019-03-15T19:41:20Z")

</div>

It works for me:

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

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

```

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?

---

<div class="post-metadata">

**Author:** ![iamsuddhasattwa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iamsuddhasattwa/32/7441_2.png) [@iamsuddhasattwa](https://discourse.julialang.org/u/iamsuddhasattwa)\
**Post date:** [March 15, 2019, 8:24pm UTC](https://discourse.julialang.org/t/increading-the-number-of-threads-of-execution/21925/3 "2019-03-15T20:24:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 15, 2019, 8:57pm UTC](https://discourse.julialang.org/t/increading-the-number-of-threads-of-execution/21925/4 "2019-03-15T20:57:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [March 15, 2019, 9:23pm UTC](https://discourse.julialang.org/t/increading-the-number-of-threads-of-execution/21925/5 "2019-03-15T21:23:44Z")

</div>

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

```nohighlight
$ JULIA_NUM_THREADS=4 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:

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

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [March 15, 2019, 9:55pm UTC](https://discourse.julialang.org/t/increading-the-number-of-threads-of-execution/21925/6 "2019-03-15T21:55:04Z")

</div>

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

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

```

Now:

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

julia> Threads.nthreads()
8

```

=)

---

<div class="post-metadata">

**Author:** ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)\
**Post date:** [March 15, 2019, 10:07pm UTC](https://discourse.julialang.org/t/increading-the-number-of-threads-of-execution/21925/7 "2019-03-15T22:07:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [March 15, 2019, 10:19pm UTC](https://discourse.julialang.org/t/increading-the-number-of-threads-of-execution/21925/8 "2019-03-15T22:19:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)\
**Post date:** [March 15, 2019, 10:25pm UTC](https://discourse.julialang.org/t/increading-the-number-of-threads-of-execution/21925/9 "2019-03-15T22:25:08Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [March 16, 2019, 7:58am UTC](https://discourse.julialang.org/t/increading-the-number-of-threads-of-execution/21925/10 "2019-03-16T07:58:20Z")

</div>

See also:  
[https://github.com/JuliaLang/julia/issues/26889](https://github.com/JuliaLang/julia/issues/26889)
