In defining a function, I created a SharedArray and use @parallel to fill each of its elements. At last, I want to return the mean of these elements by calling mean
on the SharedArray. However, it doesn’t seem to work as expected. Here’s a MWE:
function foo(m::Int64)
z = SharedArray(Float64, m)
@parallel for i = 1:m
z[i] = 1.0
end
mean(z)
end
On my Mac, calling foo(10)
for the first time gives result 0.0, whereas 0.7 afterwards.
If the function returns the SharedArray instead, the output is correct:
function bar(m::Int64)
z = SharedArray(Float64, m)
@parallel for i = 1:m
z[i] = 1.0
end
z
end
Calling bar(10)
gives the expected result.
To add even more confusion, z = bar(10); mean(z)
and mean(bar(10))
give different results. The former gives what is expected, whereas the latter behaves as foo(10)
, i.e. returning 0.0 for the first call and 0.7 afterwords.
I’m totally confused.