# BoundsError with SharedArrays in remote node

**URL:** https://discourse.julialang.org/t/boundserror-with-sharedarrays-in-remote-node/51670
**Category:** Julia at Scale
**Created:** [December 11, 2020, 4:22pm UTC](https://discourse.julialang.org/t/boundserror-with-sharedarrays-in-remote-node/51670 "2020-12-11T16:22:17Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Noel\_Araujo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/noel_araujo/32/4862_2.png) [@Noel\_Araujo](https://discourse.julialang.org/u/Noel_Araujo)
#### Post date: [December 11, 2020, 4:22pm UTC](https://discourse.julialang.org/t/boundserror-with-sharedarrays-in-remote-node/51670/1 "2020-12-11T16:22:17Z")

</div>

I want to broadcast a matrix into some remote nodes to avoid communication.

My best guess is to use `SharedArrays`, but, when I try to copy the data on remote node, I got a error. Here an example:

```julia
args = (exeflags=`--threads 2`, topology=:master_worker, enable_threaded_blas=true)
using Distributed; addprocs(4; args...)
machine2 = [("pc2",2)]; addprocs(machine2; dir="/home/pc2", args...)
machine3 = [("pc3",4)]; addprocs(machine3; dir="/home/pc3", args...)
@everywhere using SharedArrays

N = 5
AA = rand(Int64,N,N)

arr_local = SharedArray{Int64, 2}((N,N), pids=[2,3,4,5])
arr_pc2 = SharedArray{Int64, 2}((N,N), pids=[6,7])
arr_pc3 = SharedArray{Int64, 2}((N,N), pids=[8,9,10,11])

arr_local .= AA ## ok
arr_pc2 .= AA ## problems

```

The error message:

```julia
julia> arr_pc2 .= AA
ERROR: BoundsError
Stacktrace:
 [1] _copyto_impl! at ./array.jl:333 [inlined]
 [2] copyto! at ./array.jl:326 [inlined]
 [3] copyto! at ./array.jl:350 [inlined]
 [4] copyto! at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/SharedArrays/src/SharedArrays.jl:587 [inlined]
 [5] copyto! at ./broadcast.jl:927 [inlined]
 [6] copyto! at ./broadcast.jl:886 [inlined]
 [7] materialize! at ./broadcast.jl:848 [inlined]
 [8] materialize!(::SharedArray{Int64,2}, ::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{2},Nothing,typeof(identity),Tuple{Array{Int64,2}}}) at ./broadcast.jl:845
 [9] top-level scope at REPL[17]:1

```

For sure I’m missing some basic concept of `SharedArrays`, because I cannot not even set a single value on remote node:

```julia
arr_local[1] # works 
arr_pc2[1] # more bound error
julia> arr_pc2[1]
ERROR: BoundsError: attempt to access 0×0 Array{Int64,2} at index [1]
Stacktrace:
 [1] getindex at ./array.jl:809 [inlined]
 [2] getindex(::SharedArray{Int64,2}, ::Int64) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/SharedArrays/src/SharedArrays.jl:508
 [3] top-level scope at REPL[24]:1

```

Any help ?

---

<div class="post-metadata">

### Author: ![Noel\_Araujo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/noel_araujo/32/4862_2.png) [@Noel\_Araujo](https://discourse.julialang.org/u/Noel_Araujo)
#### Post date: [December 17, 2020, 1:28am UTC](https://discourse.julialang.org/t/boundserror-with-sharedarrays-in-remote-node/51670/2 "2020-12-17T01:28:34Z")

</div>

I figure out after some free time + Google. The answer is [here](https://github.com/JuliaLang/julia/issues/28132).

I had to access the array id inside the remote machine with `remotecall_fetch`. Here’s the solution

```julia
(...)
arr_local .= AA ## ok
arr_pc2 .= AA ## problems

for i = 1:N^2
    remotecall_fetch(sharr->sharr.s[i] = AA[i], arr_pc2.pids[1], arr_pc2)
end
arr_pc2

```

Note about `arr_pc2.pids[1]`: To setup the process that will write the SharedArray, I just decided that it would be the first in the list, because I hope that at least one process exists.
