Disable nested thread call

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

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

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

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 ?

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

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

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.

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

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

1 Like

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

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


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.