Why does `similar(::SharedArray)` create an `Array`?

question

#1

Consider the following example:

julia> u0 = SharedArray{Float64}(2,3)
2×3 SharedArray{Float64,2}:
 0.0  0.0  0.0
 0.0  0.0  0.0

julia> u1 = copy(u0)
2×3 Array{Float64,2}:
 0.0  0.0  0.0
 0.0  0.0  0.0

I would have expected u1::typeof(u0). This behaviour is due to the definition of similar for SharedArrays in https://github.com/JuliaLang/julia/blob/0d7248e2ff65bd6886ba3f003bf5aeab929edab5/base/sharedarray.jl#L538, giving

julia> u0 = SharedArray{Float64}(2,3)
2×3 SharedArray{Float64,2}:
 0.0  0.0  0.0
 0.0  0.0  0.0

julia> similar(u0)
2×3 Array{Float64,2}:
 6.92442e-310  6.92442e-310  6.92442e-310
 6.92442e-310  6.92442e-310  6.92442e-310

Here, I would have expected similar(u0)::typeof(u0).

I’m using julia v0.6.1

julia> versioninfo()
Julia Version 0.6.1-pre.1
Commit 4b967d4a9d* (2017-08-21 21:11 UTC)
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: Intel(R) Core(TM) i5-4590S CPU @ 3.00GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, haswell)

I would like to understand the reasoning for this implementation. Could someone please explain me the reason for this behaviour?
Since copy(u0) is not of the same type as u0, SharedArrays cannot be used in DifferentialEquations.jl (OrdinaryDiffEq.jl) up to now.


#2

Some discussion: https://github.com/JuliaLang/julia/pull/12964, see also https://github.com/JuliaLang/julia/pull/23747


#3

@fredrikekre Thank you very much for your fast reply.

At least, the behaviour of similar seems to be discussed controversially, see also https://github.com/JuliaLang/julia/issues/22218. I’m still confused by

ulia> u0 = SharedArray{Float64}(2,3)
2×3 SharedArray{Float64,2}:
 0.0  0.0  0.0
 0.0  0.0  0.0

julia> copy(u0)
2×3 Array{Float64,2}:
 0.0  0.0  0.0
 0.0  0.0  0.0

julia> deepcopy(u0)
2×3 SharedArray{Float64,2}:
 0.0  0.0  0.0
 0.0  0.0  0.0

What is the recommended way to copy a SharedArray and get a SharedArray? Is it deepcopy? Or something like SharedArray(copy(::SharedArray))?