# Is there any overhead through too many threads?

**URL:** <https://discourse.julialang.org/t/is-there-any-overhead-through-too-many-threads/46559>\
**Category:** Performance\
**Created:** [September 13, 2020, 7:16pm UTC](https://discourse.julialang.org/t/is-there-any-overhead-through-too-many-threads/46559 "2020-09-13T19:16:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [September 13, 2020, 7:16pm UTC](https://discourse.julialang.org/t/is-there-any-overhead-through-too-many-threads/46559/1 "2020-09-13T19:16:31Z")

</div>

Hi everyone,  
sorry if this has already been answered somewhere, but my quick search didn’t really give me an answer.

I was wondering if there is any possible performance regression when setting `JULIA_NUM_THREADS` larger than the actual available threads. On my laptop there’s no issue, as Julia automatically sets it too the maximum threads available, even if I set the environment variable too large. For the cluster I’m using I allocate some of the threads and set the number accordingly. The issue is that the number of allocated threads is not the same for Intel and AMD cores, i.e. when allocating 16 “CPUs” through the LSF manager I get 16 threads on AMD and 32 on Intel. Should I just always set `JULIA_NUM_THREADS` to 32 and it doesn’t matter for the AMD cores, or are there any caveats? I don’t know prior to the run on which node the job will run.

Thanks.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [September 13, 2020, 7:21pm UTC](https://discourse.julialang.org/t/is-there-any-overhead-through-too-many-threads/46559/2 "2020-09-13T19:21:21Z")

</div>

The number of threads in Julia will be capped to the actual number of threads:

```bash
% JULIA_NUM_THREADS=100 julia -e 'using Base.Threads; @show Threads.nthreads()'
Threads.nthreads() = 8

```

Note that you may experience some degradation of performance if you hyperthread, but that depends on your actual workload

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 13, 2020, 7:38pm UTC](https://discourse.julialang.org/t/is-there-any-overhead-through-too-many-threads/46559/3 "2020-09-13T19:38:49Z")

</div>

> [@giordano](#):
>
> The number of threads in Julia will be capped to the actual number of threads:

I think this is changed for 1.6.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [September 13, 2020, 7:44pm UTC](https://discourse.julialang.org/t/is-there-any-overhead-through-too-many-threads/46559/4 "2020-09-13T19:44:53Z")

</div>

No mention in [`NEWS.md`](https://github.com/JuliaLang/julia/blob/72854dc6e2874638cdd4f578bcfebc162ec347b5/NEWS.md)? 😞

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [September 13, 2020, 8:07pm UTC](https://discourse.julialang.org/t/is-there-any-overhead-through-too-many-threads/46559/5 "2020-09-13T20:07:52Z")

</div>

Okay I haven’t checked this, but how does Julia know how many threads I have allocated on a node with more threads available to others?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 13, 2020, 8:18pm UTC](https://discourse.julialang.org/t/is-there-any-overhead-through-too-many-threads/46559/6 "2020-09-13T20:18:18Z")

</div>

[https://github.com/JuliaLang/julia/pull/36778](https://github.com/JuliaLang/julia/pull/36778)

Added a label to it.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [September 13, 2020, 9:11pm UTC](https://discourse.julialang.org/t/is-there-any-overhead-through-too-many-threads/46559/7 "2020-09-13T21:11:17Z")

</div>

I’m not sure I understand the question: don’t you launch separate processes on each node?

---

<div class="post-metadata">

**Author:** ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)\
**Post date:** [September 13, 2020, 9:42pm UTC](https://discourse.julialang.org/t/is-there-any-overhead-through-too-many-threads/46559/8 "2020-09-13T21:42:39Z")

</div>

Yes I do. The question is the following: Should I set my `JULIA_NUM_THREADS` to the number of possible hyperthreads with an Intel processor, or should I try to get the right number for the AMD processors? Would there be a noticable difference between 2x too many threads? Maybe this makes more sense if I highlight also that these number of threads are not all the threads available on one node, just the ones I am able to use.
