Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Segmentation fault after deep-copying SharedArray nested in a type #14459

Closed
DanielArndt opened this issue Dec 21, 2015 · 4 comments
Closed

Segmentation fault after deep-copying SharedArray nested in a type #14459

DanielArndt opened this issue Dec 21, 2015 · 4 comments
Labels
domain:parallelism Parallel or distributed computation

Comments

@DanielArndt
Copy link
Contributor

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.

DanielArndt added a commit to DanielArndt/julia that referenced this issue Dec 21, 2015
- Previous behaviour, creating an Array, is incorrect behaviour, and
  caused segfaults
- Fixes JuliaLang#14459
@DanielArndt DanielArndt changed the title Segmentation fault after deep-copying SharedArray in a type Segmentation fault after deep-copying SharedArray nested in a type Dec 21, 2015
@DanielArndt
Copy link
Contributor Author

I guess it makes more sense to change similar(S::SharedArray, T, dims::Dims) and modify the others to use that...

@tkelman
Copy link
Contributor

tkelman commented Dec 21, 2015

ref #12964, which I was not a fan of.

@DanielArndt
Copy link
Contributor Author

Great, thanks for the context. I'll hold off working on this then since I imagine some debate needs to be had.

@kshyatt kshyatt added the domain:parallelism Parallel or distributed computation label Dec 22, 2015
@KristofferC
Copy link
Sponsor Member

Fixed with the change to SharedArray indexing I presume.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:parallelism Parallel or distributed computation
Projects
None yet
Development

No branches or pull requests

4 participants