I am trying to use multiple threads to speed up my code. But it is very confusing when I use the Threads.@threads in my code. Here is one simple example of the code:
Threads.@threads for i in 1:3
hits = Set{Int}()
b = arr_A[i, :]
k = arr_B[i, :]
t0 = norm(b/k)
t = 0.0
println(i)
while true
y = b + (t0 + t)*k
push!(hits, y)
t += 1
end
println(hits)
end
I have tested the code with and without using Threads.@threads, the results in hits are totally different. I know the index of hits could be different, but the elements in each index should be the same. Here I attached one of my results based on my original code:
Without using Threads.@threads:
With using Threads.@threads:
I am wondering why this difference happen? Any suggestions would be very helpful
Are you referencing global variables? Then you might have a data race.
I don’t think it is a global vairable that I am referencing. All the variables are defined within the loop, except the arrays, arr_A and arr_B, which was read from a datafile.
Could you provide a reproducible example? It’s hard to see what’s going on here, for instance the while
loop in your example seems to run forever (if I understand correctly?)
My bad, it should have one condition statement within the while loop. I am trying to provide one simple example, but it looks like that this simple example works well.
using Base.Threads
arr_A=rand(10,3)
arr_B=rand(10,3)
#Threads.@threads for i in 1:3
for i in 1:3
hits = Set{Float64}()
a = arr_A[i,:]
k = arr_B[i,:]
t0 = arr_A[i]./arr_B[i]
t = 0.0
while t<1
y = a + (t0 + t)*k
push!(hits, y[1])
t += 0.05
end
end
But in the original code, it doesn’t. I think the error is from the while loop. My understanding is that if not using multithreads, the code will run the loop i, but after using multithreads, the for loop run different i simultaneously, but when going to the while loop, things are changed, and the threads seems to become one serial calculations from different threads, at least in my side, I can see the parameter t is always increasing, regardless of the value of i. So my question is that could we also parallel the while loop and run each i. Thanks
After clarifying my question, I think it is not related to the inner loop or outer loop. But it is related to the variable within one loop. Here is one simple example:
Threads.@spawn while true
a = f(t)
if (a > something)
break
end
t += 0.05
end
My understanding when using Threads.@spawn for the while loop, it will distribute different threadid() to a=f(t), and each threadid() doesn’t wait for each other, until the loop is end. In this case, some threadid() may have satisfy the if statement, so that the loop is ended. This could make issues. So if my above understanding is correct, my question is that how could I wait until all the threads finish the calculation of a=f(t), and then go to the if statement? Thanks
If I understand your question correctly, the right documentation is here: Threads.@spawn
: all it takes is a wait
statement. Does that help?
Also please remember to format your code: Please read: make it easier to help you