# SharedArray undefined on worker

**URL:** <https://discourse.julialang.org/t/sharedarray-undefined-on-worker/36926>\
**Category:** General Usage\
**Created:** [April 2, 2020, 11:04pm UTC](https://discourse.julialang.org/t/sharedarray-undefined-on-worker/36926 "2020-04-02T23:04:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_McVay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_mcvay/32/4307_2.png) [@Paul\_McVay](https://discourse.julialang.org/u/Paul_McVay)\
**Post date:** [April 2, 2020, 11:04pm UTC](https://discourse.julialang.org/t/sharedarray-undefined-on-worker/36926/1 "2020-04-02T23:04:29Z")

</div>

I have what I thought was a really basic usage of SharedArrays. But it says the array is undefined on the worker processes and I have no idea why it would be. I have the following code in a file “test.jl”. I run it with `julia -p 4 test.jl`

```julia
using SharedArrays

const arr = SharedArray{Int64, 2}((5,5))

for i in 1:5
    arr[i, :] = fill(i, 5)
end

@everywhere function test()
    return arr[myid(), :]
end 

remotecall_fetch(test, 2)

```

it exits with

```julia
ERROR: LoadError: On worker 2:
UndefVarError: arr not defined

```

---

<div class="post-metadata">

**Author:** ![k945](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/k945/32/8906_2.png) [@k945](https://discourse.julialang.org/u/k945)\
**Post date:** [April 3, 2020, 1:12am UTC](https://discourse.julialang.org/t/sharedarray-undefined-on-worker/36926/2 "2020-04-03T01:12:08Z")

</div>

You need to define and load “arr” and SharedArrays on all processes.

```julia
@everywhere using SharedArrays
@everywhere arr = SharedArray{Int64, 2}((5,5))

```

---

<div class="post-metadata">

**Author:** ![Paul\_McVay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_mcvay/32/4307_2.png) [@Paul\_McVay](https://discourse.julialang.org/u/Paul_McVay)\
**Post date:** [April 3, 2020, 1:35am UTC](https://discourse.julialang.org/t/sharedarray-undefined-on-worker/36926/3 "2020-04-03T01:35:59Z")

</div>

No, that creates 5 different `arr` instead of one array that is shared. You can run this if you don’t believe me

```julia
@everywhere using SharedArrays
@everywhere arr = SharedArray{Int64, 2}((5,5))

@everywhere function test()
    arr[myid(), :] = fill(myid(), 5)
end 
println(arr)
test()
println(arr)
remotecall_fetch(test, 3)
println(arr)

```

If it were shared, the last print of `arr` should be `[1 1 1 1 1; 0 0 0 0 0; 3 3 3 3 3; 0 0 0 0 0; 0 0 0 0 0]` but its not

---

<div class="post-metadata">

**Author:** ![k945](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/k945/32/8906_2.png) [@k945](https://discourse.julialang.org/u/k945)\
**Post date:** [April 3, 2020, 2:42am UTC](https://discourse.julialang.org/t/sharedarray-undefined-on-worker/36926/4 "2020-04-03T02:42:24Z")

</div>

My mistake. Thanks for pointing that out.

Add `@distributed` to your loop

So using your original code

```julia
arr = SharedArray{Int64, 2}((5,5))

@distributed for i in 1:5
    arr[i, :] = fill(i, 5)
end

@everywhere function test()
    return arr[myid(), :]
end 
remotecall_fetch(test, 2)

```

Should return what you expect

---

<div class="post-metadata">

**Author:** ![Paul\_McVay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_mcvay/32/4307_2.png) [@Paul\_McVay](https://discourse.julialang.org/u/Paul_McVay)\
**Post date:** [April 3, 2020, 3:12am UTC](https://discourse.julialang.org/t/sharedarray-undefined-on-worker/36926/5 "2020-04-03T03:12:06Z")

</div>

I don’t think that’s right. I think the `@distributed` macro just creates copies similar to how `@spawnat` does. You can take a look at the documentation here [Parallel Computing · The Julia Language](https://docs.julialang.org/en/v1/manual/parallel-computing/#Data-Movement-1). It talks about implicit data movement.

If for example, you add `arr[2,2] = 10` after the distributed loop but before the test function, the remote call and fetch still only outputs `[2, 2, 2, 2, 2]`. Additionally, if you run your code with a regular array instead of a shared array (see below), it works the same which is evidence for the implicit data movement.

```julia
arr = Array{Int64, 2}(undef, 5,5)

@distributed for i in 1:5
    arr[i, :] = fill(i, 5)
end
arr[2,2] = 10
@everywhere function test()
    return arr[myid(), :]
end 
println(remotecall_fetch(test, 2))

```

---

<div class="post-metadata">

**Author:** ![k945](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/k945/32/8906_2.png) [@k945](https://discourse.julialang.org/u/k945)\
**Post date:** [April 3, 2020, 9:12am UTC](https://discourse.julialang.org/t/sharedarray-undefined-on-worker/36926/6 "2020-04-03T09:12:56Z")

</div>

My understanding is that you [avoid implicit data movement](https://docs.julialang.org/en/v1/manual/parallel-computing/#Parallel-Map-and-Loops-1) if you loop through a SharedArray in this way. But I may have to spend a little bit more time reading the documentation.

With that said, This should be a posted as a new question; my differ based on whether I run the code as a script or via the REPL.

Via REPL (using Julia V1.3.0) I get [2,10,2,2,2] when I run the following

```julia
julia> using Distributed,SharedArrays

julia> addprocs(3)
3-element Array{Int64,1}:
 2
 3
 4

julia> @everywhere function test()
           return arr[myid(), :]
       end

julia> arr = SharedArray{Int64}(5, 5)
5×5 SharedArray{Int64,2}:
 0 0 0 0 0
 0 0 0 0 0
 0 0 0 0 0
 0 0 0 0 0
 0 0 0 0 0

julia> @distributed for i = 1:5
           
           arr[i, :] = fill(i, 5)
       end
Task (runnable) @0x00007f0856f57d00

julia> arr[2,2] = 10
10

julia> println(remotecall_fetch(test, 2))
[2, 10, 2, 2, 2]

```

however, running this as a script returns [2,2,2,2,2].

---

<div class="post-metadata">

**Author:** ![Paul\_McVay](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_mcvay/32/4307_2.png) [@Paul\_McVay](https://discourse.julialang.org/u/Paul_McVay)\
**Post date:** [April 3, 2020, 2:52pm UTC](https://discourse.julialang.org/t/sharedarray-undefined-on-worker/36926/7 "2020-04-03T14:52:23Z")

</div>

That’s very weird. Going back to my original example, it appears to work if you add a `fetchfrom` the process of interest first. I might submit an issue on github since it doesn’t make sense.

```julia
@everywhere using SharedArrays

const arr = SharedArray{Int64, 2}((5,5))

for i in 1:5
    arr[i, :] = fill(i, 5)
end

@everywhere function test()
    arr[myid(), myid()] = 10
    return arr[myid(), :]
end 

println(remotecall_fetch(test, 2)) # doesn't work
@fetchfrom 2 arr
println(remotecall_fetch(test, 2)) # works now
println(remotecall_fetch(test, 3)) # still doesn't work on this worker though
println(arr)

```

---

<div class="post-metadata">

**Author:** ![jwar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jwar/32/22763_2.png) [@jwar](https://discourse.julialang.org/u/jwar)\
**Post date:** [March 10, 2021, 3:57pm UTC](https://discourse.julialang.org/t/sharedarray-undefined-on-worker/36926/9 "2021-03-10T15:57:38Z")

</div>

Hi!

There’s also the possibility to just broadcast the declaration, e.g.,

```
for w in workers()
	@spawnat(w,arr)
end

```
