# Disable nested thread call

**URL:** <https://discourse.julialang.org/t/disable-nested-thread-call/25818>\
**Category:** General Usage\
**Created:** [June 28, 2019, 6:37pm UTC](https://discourse.julialang.org/t/disable-nested-thread-call/25818 "2019-06-28T18:37:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)\
**Post date:** [June 28, 2019, 6:37pm UTC](https://discourse.julialang.org/t/disable-nested-thread-call/25818/1 "2019-06-28T18:37:07Z")

</div>

I’m wondering if it is possible to disable nested threaded call? e.g

```julia
function foo()
    Threads.@threads for k in 1:120
       # do something
     end
end

function baz()
    Threads.@threads for k in 1:120
      foo()
     end
end  

```

Is there a way that can disable the threading call in foo without changing `foo`’s code? (suppose `foo` is written by someone else, and `baz` is my code) something like `@inbounds` ?

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [June 28, 2019, 6:49pm UTC](https://discourse.julialang.org/t/disable-nested-thread-call/25818/2 "2019-06-28T18:49:01Z")

</div>

I suspect the answer is “no, what are you really trying to do?”

This is feeling like an “XY” problem: [https://mywiki.wooledge.org/XyProblem](https://mywiki.wooledge.org/XyProblem)

---

<div class="post-metadata">

**Author:** ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)\
**Post date:** [June 28, 2019, 6:54pm UTC](https://discourse.julialang.org/t/disable-nested-thread-call/25818/3 "2019-06-28T18:54:02Z")

</div>

I don’t think so, this can be a common requirement, e.g there’s `BLAS.set_num_threads` to set the number of threads BLAS can use, and then you can put a matrix prod reduction (which is a long loop of matrices) to your own thread which is usually more efficient since the thread call happens in outer loop now.

I’m trying to use batched matrix multiplication which is

```julia
C[:, :, k] = A[:, :, k] * B[:, :, k]

```

there is a thread call on the `k` loop, but now I have another batch dimension which cannot easily be reshaped to the first batch dimension, thus I’d like to spawn the threads at the outer loop instead and disable the inner one.

---

<div class="post-metadata">

**Author:** ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)\
**Post date:** [June 28, 2019, 7:02pm UTC](https://discourse.julialang.org/t/disable-nested-thread-call/25818/4 "2019-06-28T19:02:44Z")

</div>

I remember there was an issue about dynamic thread call which might be related, but I can’t find it anymore…

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 28, 2019, 7:27pm UTC](https://discourse.julialang.org/t/disable-nested-thread-call/25818/5 "2019-06-28T19:27:36Z")

</div>

If you are worrying about performance, then it’s taken care of.

---

<div class="post-metadata">

**Author:** ![Roger-luo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roger-luo/32/3399_2.png) [@Roger-luo](https://discourse.julialang.org/u/Roger-luo)\
**Post date:** [June 28, 2019, 7:31pm UTC](https://discourse.julialang.org/t/disable-nested-thread-call/25818/6 "2019-06-28T19:31:46Z")

</div>

Oh I just checked the source code, seems this will be auto disabled in nested thread call

```julia
        # Hack to make nested threaded loops kinda work
        if threadid() != 1 || in_threaded_loop[]
            # We are in a nested threaded loop
            Base.invokelatest(threadsfor_fun, true)
        else

```

thanks!

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 28, 2019, 7:40pm UTC](https://discourse.julialang.org/t/disable-nested-thread-call/25818/7 "2019-06-28T19:40:26Z")

</div>

Yes that’s what I mean by the performance is taken care of. I did not say it’ll be automatically done since this is not guaranteed to be the solution. We are free to change the implementation to not do this.
