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

**URL:** <https://discourse.julialang.org/t/why-does-similar-sharedarray-create-an-array/6673>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 25, 2017, 7:44am UTC](https://discourse.julialang.org/t/why-does-similar-sharedarray-create-an-array/6673 "2017-10-25T07:44:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ranocha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ranocha/32/35588_2.png) [@ranocha](https://discourse.julialang.org/u/ranocha)\
**Post date:** [October 25, 2017, 7:44am UTC](https://discourse.julialang.org/t/why-does-similar-sharedarray-create-an-array/6673/1 "2017-10-25T07:44:13Z")

</div>

Consider the following example:

```julia
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 `SharedArray`s in [https://github.com/JuliaLang/julia/blob/0d7248e2ff65bd6886ba3f003bf5aeab929edab5/base/sharedarray.jl#L538](https://github.com/JuliaLang/julia/blob/0d7248e2ff65bd6886ba3f003bf5aeab929edab5/base/sharedarray.jl#L538), giving

```julia
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
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`, `SharedArray`s cannot be used in DifferentialEquations.jl (OrdinaryDiffEq.jl) up to now.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [October 25, 2017, 7:53am UTC](https://discourse.julialang.org/t/why-does-similar-sharedarray-create-an-array/6673/2 "2017-10-25T07:53:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ranocha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ranocha/32/35588_2.png) [@ranocha](https://discourse.julialang.org/u/ranocha)\
**Post date:** [October 25, 2017, 8:48am UTC](https://discourse.julialang.org/t/why-does-similar-sharedarray-create-an-array/6673/3 "2017-10-25T08:48:45Z")

</div>

@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](https://github.com/JuliaLang/julia/issues/22218). I’m still confused by

```julia
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))`?
