# Should a view of a SharedArray be also shared between workers?

**URL:** <https://discourse.julialang.org/t/should-a-view-of-a-sharedarray-be-also-shared-between-workers/33580>\
**Category:** Internals & Design\
**Created:** [January 20, 2020, 1:04pm UTC](https://discourse.julialang.org/t/should-a-view-of-a-sharedarray-be-also-shared-between-workers/33580 "2020-01-20T13:04:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gregstrq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregstrq/32/20620_2.png) [@Gregstrq](https://discourse.julialang.org/u/Gregstrq)\
**Post date:** [January 20, 2020, 1:04pm UTC](https://discourse.julialang.org/t/should-a-view-of-a-sharedarray-be-also-shared-between-workers/33580/1 "2020-01-20T13:04:02Z")

</div>

Consider the following simple example, where we try to fill a view of a SharedArray on a worker (I’ve tested it on julia 1.1.0):

```julia
using Distributed
w = addprocs(1)[]

using SharedArrays

function idid!(v)
    for i=1:length(v)
        v[i] = i
    end
end

sha = SharedArray{Float64}(10)
shav = @view sha[1:5]

@spawnat w idid!(shav)

sha

```

The result that we get is:

> sha  
> 10-element SharedArray{Float64,1}:  
> 0.0  
> 0.0  
> 0.0  
> 0.0  
> 0.0  
> 0.0  
> 0.0  
> 0.0  
> 0.0  
> 0.0

As we see, nothing happens, which implies that when a view of a SharedArray is passed to another worker, the underlying storage get somehow copied.

Is it an intended behaviour?  
I would rather think that it is more logical to have a view of a SharedArray be also shared.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [January 20, 2020, 1:19pm UTC](https://discourse.julialang.org/t/should-a-view-of-a-sharedarray-be-also-shared-between-workers/33580/2 "2020-01-20T13:19:59Z")

</div>

```julia
using Distributed
w = addprocs(1)[]

@everywhere using SharedArrays

@everywhere function idid!(v)
    for i=1:length(v)
        v[i] = i
    end
end

@everywhere sha = SharedArray{Float64}(10)
@everywhere shav = @view sha[1:5]

f = @spawnat w idid!(shav)
fetch(f)

sha

```

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [January 20, 2020, 1:21pm UTC](https://discourse.julialang.org/t/should-a-view-of-a-sharedarray-be-also-shared-between-workers/33580/3 "2020-01-20T13:21:28Z")

</div>

You’re being fooled by the fact that you haven’t `wait`ed for the result of the `@spawnat`; if you do that, you get this:

```julia
ERROR: On worker 2:
UndefVarError: #idid! not defined
deserialize_datatype at /home/tim/src/julia-1/usr/share/julia/stdlib/v1.3/Serialization/src/Serialization.jl:1193
...

```

Put an `@everywhere` in front of the definition of `idid!` and wait on the result, and you should see it working as expected.

---

<div class="post-metadata">

**Author:** ![Gregstrq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gregstrq/32/20620_2.png) [@Gregstrq](https://discourse.julialang.org/u/Gregstrq)\
**Post date:** [January 20, 2020, 1:23pm UTC](https://discourse.julialang.org/t/should-a-view-of-a-sharedarray-be-also-shared-between-workers/33580/4 "2020-01-20T13:23:15Z")

</div>

Thank you.
