Reputation: 83
I'm trying parallelise some bits of a code but I do not understand why the following functions main1() and main2() give different results using Julia's multi-threading:
a = rand(4,4);b = rand(4,4);c = rand(4,4);d = rand(4,4)
function main1(a,b,c,d)
L = zeros(2,2,16)
FF = zeros(2,2,16)
FT = zeros(2,2,16)
F = Array{Float32}(undef,2,2)
# L = Array{Array{Float32, 1}, 4}
for i = 1:4
for j = 1:4
ic = i + j*(i-1)
F[1,1] = a[i,j]
F[1,2] = b[i,j]
F[2,1] = c[i,j]
F[2,2] = d[i,j]
L[:,:,ic] .= F * F'
FF[:,:,ic] .= F
FT[:,:,ic] .= F'
end
end
return L,FF,FT
end
function main2(a,b,c,d)
L = zeros(2,2,16)
FF = zeros(2,2,16)
FT = zeros(2,2,16)
F = Array{Float32}(undef,2,2)
# L = Array{Array{Float32, 1}, 4}
Threads.@threads for i = 1:4
Threads.@threads for j = 1:4
ic = i + j*(i-1)
F[1,1] = a[i,j]
F[1,2] = b[i,j]
F[2,1] = c[i,j]
F[2,2] = d[i,j]
L[:,:,ic] .= F * F'
FF[:,:,ic] .= F
FT[:,:,ic] .= F'
end
end
return L,FF,FT
end
How could the parallelisation of main1() be properly fixed?
Upvotes: 2
Views: 174
Reputation: 42194
You cannot nest @threads
loops so normally you should do:
Threads.@threads for u in vec(CartesianIndices((4,4)))
i,j = u.I
# your code goes here
end
However, in your code you get the same ic
value for different pair of values of (i,j)
. In the main1
you are overwriting the same parts of L
, FF
, FT
many times which is an obvious bug. Multi-threading will change the order the data is overwritten so it will yields different results. In conclusion, first fix main1
and than parallelize it.
Upvotes: 1