I've been trying to isolate a defect I have in my code for the past couple 
days, and it seems like SharedArrays have troubles with deepcopying.

Looking at the source, this should be supported (Julia 0.4.2):

https://github.com/JuliaLang/julia/blob/v0.4.2/base/sharedarray.jl#L201

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 Base.show(io::IO, a::A)
    print(io,a.sa)
end

function foo(a::A)
    for i in 1:length(a.sa)
        a.sa[i] += 1
    end
end

There are a few ways I can break this. 

1. Calling @show on a deepcopy breaks when implemented like this.
instance = A(5)
@show instance
dc = deepcopy(instance); # Semi-colon necessary on REPL to prevent show 
being called
print(dc.sa) # Works fine
@show dc.sa # Works fine
@show dc # Seg fault if show is implemented

Very strangely, a switch to "println" instead of "print" in the show 
function and everything works fine.

2. Even more worrysome, accessing the array in a function (foo):
instance = A(5)
dc = deepcopy(instance); # Semi-colon necessary on REPL to prevent show 
being called
foo(instance) # Works fine on the original
for i in 1:length(dc.sa)
    dc.sa[i] += 1
end
dc.sa # The above worked, and everything prints out fine
foo(dc) #Seg fault

It turns out, the deepcopy is creating the SharedArray as an Array:

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}

Probably related? Is this a bug or am I doing something silly? Is anyone 
aware of a defect open for this or something similar, or should I open a 
new one?

Cheers,
Dan

Reply via email to