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

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):

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.

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
1 Like

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

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.

3 Likes

Thank you.