Closed
Description
Copied largely from a topic I started on julia-users: https://groups.google.com/forum/#!topic/julia-users/Cg6K3-mpAig
I've created a small stripped down version of the problem I am encountering.
type A
sa::SharedArray{Float64,1}
end
function A(n::Int)
sa = SharedArray(Float64, n, init = S -> S[Base.localindexes(S)] = 0)
return A(sa)
end
function foo(a::A)
for i in 1:length(a.sa)
a.sa[i] += 1
end
end
Executing the following on this type will break it:
julia> instance = A(5)
[0.0,0.0,0.0,0.0,0.0]
julia> dc = deepcopy(instance); # Semi-colon necessary on REPL to prevent show being called
julia> foo(instance) # Works fine on the original
julia> for i in 1:length(dc.sa)
dc.sa[i] += 1
end
julia> dc.sa # The above worked, and everything prints out fine on the REPL
5-element Array{Float64,1}:
1.0
1.0
1.0
1.0
1.0
julia> foo(dc) # Seg fault
Turns out, the deepcopy is creating the SharedArray as an Array which is causing the issue:
julia> instance = A(5)
[0.0,0.0,0.0,0.0,0.0]
julia> dc = deepcopy(instance);
julia> typeof(instance)
A
julia> typeof(instance.sa)
SharedArray{Float64,1}
julia> typeof(dc)
A
julia> typeof(dc.sa)
Array{Float64,1}
This all seems to be caused by the similar
function, which is returning an array. I have successfully prevented the seg fault by introducing this function:
Base.similar(S::SharedArray) = SharedArray(eltype(S), size(S), pids=procs(S))
For which I will create a pull request. I would love to hear comments, alternative approaches, or suggestions that might make the PR more robust, since this will be my first attempt dabbling in the base language.