# Number of threads, puzzle

**URL:** https://discourse.julialang.org/t/number-of-threads-puzzle/74965
**Category:** General Usage
**Tags:** question, task, threads
**Created:** [January 21, 2022, 2:47am UTC](https://discourse.julialang.org/t/number-of-threads-puzzle/74965 "2022-01-21T02:47:50Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![HANHAOHAN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hanhaohan/32/36366_2.png) [@HANHAOHAN](https://discourse.julialang.org/u/HANHAOHAN)
#### Post date: [January 21, 2022, 2:47am UTC](https://discourse.julialang.org/t/number-of-threads-puzzle/74965/1 "2022-01-21T02:47:50Z")

</div>

# draft.jl

```Julia
c1 = Channel(100)
c2 = Channel(100)
c3 = Channel(100)

function task_1()
    while true
        wait(c1)
        take!(c1)
    end
end

function task_2()
    while true
        wait(c2)
        take!(c2)
    end
end

Threads.@spawn task_1()

Threads.@spawn task_2()

while true
    wait(c3)
    take!(c3)
end

```

On my 3 kernal 6 threads cpu, it will:  
julia -q -t 1 – draft.jl 6 threads  
julia -q -t 2 – draft.jl 8 threads  
julia -q -t 3 – draft.jl 9 threads  
julia -q -t 4 – draft.jl 10 threads

Why? Thanks!

---

<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: [January 21, 2022, 7:21am UTC](https://discourse.julialang.org/t/number-of-threads-puzzle/74965/2 "2022-01-21T07:21:51Z")

</div>

I’m sorry but I don’t understand. What do you mean by this?

> [@HANHAOHAN](#):
>
> On my 3 kernal 6 threads cpu, it will:  
> julia -q -t 1 – draft.jl 6 threads  
> julia -q -t 2 – draft.jl 8 threads  
> julia -q -t 3 – draft.jl 9 threads  
> julia -q -t 4 – draft.jl 10 threads

---

<div class="post-metadata">

### Author: ![HANHAOHAN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hanhaohan/32/36366_2.png) [@HANHAOHAN](https://discourse.julialang.org/u/HANHAOHAN)
#### Post date: [January 21, 2022, 7:49am UTC](https://discourse.julialang.org/t/number-of-threads-puzzle/74965/3 "2022-01-21T07:49:27Z")

</div>

Thanks for your reply!  
“julia -q -t 1 – draft.jl 6 threads” means there are “6 threads” in this new process, when it is started with “julia -q -t 1 – draft.jl”

---

<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: [January 21, 2022, 8:24am UTC](https://discourse.julialang.org/t/number-of-threads-puzzle/74965/4 "2022-01-21T08:24:45Z")

</div>

Well it’s not just the “Julia threads” that get started. There’s also (at least) a signal thread (see `signals-unix.c`) as well as multiple OpenBLAS threads (`libopenblas64_.so`).

I’d suggest to do the following. First, use the built-in `Threads.nthreads()` to see that the number of Julia threads matches your expectation. Second, set `OPENBLAS_NUM_THREADS=1` and rerun your original experiment to exclude the effect of OpenBLAS threads. Third, you may want something like done [here](https://discourse.julialang.org/t/thread-affinitization-pinning-julia-threads-to-cores/58069/7) and use `gdb` with a breakpoint at `pthread_create` to see which threads get started on a more fundamental level. Hope that helps.

**UPDATE:**

I just ran a simple experiment myself (with `OPENBLAS_NUM_THREADS=1`). I find

```julia
julia -t 1 # 2 threads
julia -t 2 # 3 threads
julia -t 3 # 4 threads
...

```

where I checked the number of threads with `htop`.

---

<div class="post-metadata">

### Author: ![HANHAOHAN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hanhaohan/32/36366_2.png) [@HANHAOHAN](https://discourse.julialang.org/u/HANHAOHAN)
#### Post date: [January 21, 2022, 10:12am UTC](https://discourse.julialang.org/t/number-of-threads-puzzle/74965/5 "2022-01-21T10:12:07Z")

</div>

Thank you very much!  
I checked with htop too.  
My julia version is “Version 1.6.4 (2021-11-19)”

I got the same result as yours, with `OPENBLAS_NUM_THREADS=1`.  
Thanks again. From you I learn to know that there is another thread with all Channels, for the first time.

And where to learn more about details like above of this new language? I read the [manual](https://docs.julialang.org/en/v1/), but miss it.

---

<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: [January 21, 2022, 11:38am UTC](https://discourse.julialang.org/t/number-of-threads-puzzle/74965/6 "2022-01-21T11:38:12Z")

</div>

> [@HANHAOHAN](#):
>
> And where to learn more about details like above of this new language?

These are internals and a user typically doesn’t have to care too much about an extra signal thread and such. (The number of Julia threads, i.e. what `Threads.nthreads()` gives, would have matched your expectation right away.)

However, where to find good information on this if one is interested / curious is a good question. Ideally in the developer section of the Julia docs, though I couldn’t find much on the unix signal thread in a quick search. Otherwise, as of right now, you either have to study the source code, debug with tools like `gdb` or, most importantly, ask questions here or on the Julia Slack I guess.

---

<div class="post-metadata">

### Author: ![HANHAOHAN](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hanhaohan/32/36366_2.png) [@HANHAOHAN](https://discourse.julialang.org/u/HANHAOHAN)
#### Post date: [January 21, 2022, 12:01pm UTC](https://discourse.julialang.org/t/number-of-threads-puzzle/74965/7 "2022-01-21T12:01:38Z")

</div>

Thanks a lot!

---

<div class="post-metadata">

### Author: ![j\_u](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_u/32/219081_2.png) [@j\_u](https://discourse.julialang.org/u/j_u)
#### Post date: [February 2, 2022, 2:28am UTC](https://discourse.julialang.org/t/number-of-threads-puzzle/74965/8 "2022-02-02T02:28:23Z")

</div>

> [@carstenbauer](#):
>
> However, where to find good information on this if one is interested / curious is a good question.

I am recalling that a few months ago in one of discourse topics I allowed myself to suggest that a blog post on the topic of practical intricacies of multiprocessing / multithreading and pinning Julia threads with a focus on BLAS libraries could be potentially useful. I will allow myself to renew this suggestion with a hope that you, maybe even in a collaboration of some other Julia discourse colleagues, could potentially find some time to gather all your notes and provide (especially less advanced Julia users) with clear and practical information on those topics - I believe this could be useful.

---

<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: [February 2, 2022, 6:10am UTC](https://discourse.julialang.org/t/number-of-threads-puzzle/74965/9 "2022-02-02T06:10:50Z")

</div>

Not really what you have in mind I guess (it’s not very comprehensive) but FWIW I created [Julia Threads + BLAS Threads · ThreadPinning.jl](https://carstenbauer.github.io/ThreadPinning.jl/dev/explanations/blas/) a few days ago. Maybe I or someone else finds the time to expand this further.

Also, at least the critical aspects of Julia BLAS interaction in case of multithreading probably needs to go into the Julia documentation itself. See @viralbshah 's comment on this [here](https://github.com/JuliaLinearAlgebra/MKL.jl/issues/106#issuecomment-1025796366) for example.

(I also tried to add a basic autochecking of MT settings feature to ThreadPinning.jl, see [Autochecking BLAS Thread Settings · ThreadPinning.jl](https://carstenbauer.github.io/ThreadPinning.jl/dev/examples/ex_blas/#ex_blas))

---

<div class="post-metadata">

### Author: ![j\_u](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_u/32/219081_2.png) [@j\_u](https://discourse.julialang.org/u/j_u)
#### Post date: [February 2, 2022, 10:06pm UTC](https://discourse.julialang.org/t/number-of-threads-puzzle/74965/10 "2022-02-02T22:06:50Z")

</div>

Thank you for pointing out to those new parts about BLAS Thread Settings at your GitHub repo/s. The first time I came by this problem was when trying to make AlphaZero.jl work on CPU only machines. I have tree hobby projects that I am working on exchangeably (currently focusing on the one that is not directly related to this particular topic) and coding is not my area of expertise, so I do not think I am in perfect position to make any contributions now. However, I fully agree with you regarding the Julia documentation about BLAS interactions and I still think that some blog post that summarize your and some other people findings on this subject would be an interesting read. 🙂
