# SharedArrays and process affinity

**URL:** https://discourse.julialang.org/t/sharedarrays-and-process-affinity/55684
**Category:** General Usage
**Tags:** question, cluster, sharedarrays
**Created:** [February 20, 2021, 4:22pm UTC](https://discourse.julialang.org/t/sharedarrays-and-process-affinity/55684 "2021-02-20T16:22:16Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Chong\_Wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chong_wang/32/20307_2.png) [@Chong\_Wang](https://discourse.julialang.org/u/Chong_Wang)
#### Post date: [February 20, 2021, 4:22pm UTC](https://discourse.julialang.org/t/sharedarrays-and-process-affinity/55684/1 "2021-02-20T16:22:16Z")

</div>

I am trying to pin my julia workers to specific CPU cores with ClusterManagers.jl. However, it seems `LocalAffinityManager` does not work well with `SharedArrays`.

```julia
using Distributed, SharedArrays, ClusterManagers

addprocs(LocalAffinityManager(;affinities=collect(0:63)))

results = [SharedArray{Float64}(1024) for _ in 1:nworkers()]

@everywhere function do_something!(result)
    result[:] += rand(1024)
end

for i in 1:nworkers()
    remotecall_fetch(do_something!, i, results[i])
end

```

The above script results in

```julia
ERROR: LoadError: On worker 2:
BoundsError: attempt to access 0-element Array{Float64,1} at index [1]
getindex at ./array.jl:809 [inlined]
getindex at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/SharedArrays/src/SharedArrays.jl:508 [inlined]
macro expansion at ./multidimensional.jl:772 [inlined]
macro expansion at ./cartesian.jl:64 [inlined]
macro expansion at ./multidimensional.jl:767 [inlined]
_unsafe_getindex! at ./multidimensional.jl:762 [inlined]
_unsafe_getindex at ./multidimensional.jl:757
_getindex at ./multidimensional.jl:743 [inlined]
getindex at ./abstractarray.jl:1060 [inlined]
do_something! at /ocean/projects/phy200062p/mistguy/temp/parallel/test.jl:9
#106 at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/Distributed/src/process_messages.jl:294
run_work_thunk at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/Distributed/src/process_messages.jl:79
macro expansion at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/Distributed/src/process_messages.jl:294 [inlined]
#105 at ./task.jl:356
Stacktrace:
 [1] #remotecall_fetch#143 at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/Distributed/src/remotecall.jl:394 [inlined]
 [2] remotecall_fetch(::Function, ::Distributed.Worker, ::SharedArray{Float64,1}) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/Distributed/src/remotecall.jl:386
 [3] remotecall_fetch(::Function, ::Int64, ::SharedArray{Float64,1}; kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/Distributed/src/remotecall.jl:421
 [4] remotecall_fetch(::Function, ::Int64, ::SharedArray{Float64,1}) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.5/Distributed/src/remotecall.jl:421
 [5] top-level scope at /ocean/projects/phy200062p/mistguy/temp/parallel/test.jl:13
 [6] include(::String) at ./client.jl:457
 [7] top-level scope at REPL[1]:1
in expression starting at /ocean/projects/phy200062p/mistguy/temp/parallel/test.jl:12

```

If I do not use `LocalAffinityManager` and simply write `addprocs(64)`, everything is fine.

So is this expected? If so, what prevents `SharedArrays` to work with `taskset`, which `LocalAffinityManager` uses?

---

<div class="post-metadata">

### Author: ![Chong\_Wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chong_wang/32/20307_2.png) [@Chong\_Wang](https://discourse.julialang.org/u/Chong_Wang)
#### Post date: [February 23, 2021, 4:44pm UTC](https://discourse.julialang.org/t/sharedarrays-and-process-affinity/55684/2 "2021-02-23T16:44:35Z")

</div>

Could it be caused by SharedArray only accessible from certain cores?

---

<div class="post-metadata">

### Author: ![runapp](https://avatars.discourse-cdn.com/v4/letter/r/6f9a4e/32.png) [@runapp](https://discourse.julialang.org/u/runapp)
#### Post date: [February 24, 2021, 4:45pm UTC](https://discourse.julialang.org/t/sharedarrays-and-process-affinity/55684/3 "2021-02-24T16:45:24Z")

</div>

Does this error occurs only on system with more than 64 cores?

---

<div class="post-metadata">

### Author: ![Chong\_Wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chong_wang/32/20307_2.png) [@Chong\_Wang](https://discourse.julialang.org/u/Chong_Wang)
#### Post date: [February 24, 2021, 10:50pm UTC](https://discourse.julialang.org/t/sharedarrays-and-process-affinity/55684/4 "2021-02-24T22:50:57Z")

</div>

No, it can be reproduced with `addprocs(LocalAffinityManager(;affinities=collect(0:1)))` on a 24-core machine.
