@threads for only using master thread?

I just switched to 1.3 to try out implementing multithreaded algorithms. I tried a very simple loop, from the announcement post:

Threads.@threads for i = 1:10
   println(Threads.threadid(), " of ", Threads.nthreads())
end

However, this only printed 1 of 4 every time I ran it, no matter how many iterations. Clearly, nthreads() shows that I have 4 threads available - how do I make sure my loops use all of them?

Did you set JULIA_NUM_THREADS?

I’m using juno, which I thought automatically set it to the number of cores. Since nthreads() returns 4, doesn’t that mean that it has been properly set?

Yes, that’s should do it. It’s possible that the number of iterations is too low and there’s only one batch of work.

I’ll try to make a bigger case to see if it works.

In the meantime, its worth noting that the multithreading in general definitely works:

Threads.@spawn Threads.threadid()

fetch(ans)

gives me the expected various ids

EDIT: All of a sudden I tried this exact same code again and it worked fine. I have no idea why it wasn’t working to begin with.

Hello,
I am having the same problem… I am trying to use 12 threads to perform a task. I checked that 12 threads are available to Julia by running:
Threads.nthreads()
which gives me 12.

Then I test if multithreading works on a simple example:

z = zeros(10)
Threads.@threads for i = 1:10
           z[i] = Threads.threadid()
       end
z

But this results in an array of "1"s, so Julia always uses just one thread. According to https://docs.julialang.org/en/v1.0/manual/parallel-computing/#Multi-Threading-(Experimental)-1 Julia should distribute the task between the available threads…

I also tried to make z = zeros(100) but that doesn’t help.

Does somebody have an idea what could be wrong? It seems as if Julia still needs an extra “allowance” to use all the available threads.

Thank you!

Has you checked the JULIA_NUM_THREADS variable? I have just update that variable, and later I have run your code, obtaining different values in z array, one different for each thread.

1 Like

Yes, JULIA_NUM_THREADS variable was 12. I now solved the problem after restarting Julia 2 times from when it wasn’t working. Not sure what went wrong the first 2 times… Thank you for the reply!