# How to create SharedArrays on other workers

**URL:** <https://discourse.julialang.org/t/how-to-create-sharedarrays-on-other-workers/33619>\
**Category:** Performance\
**Created:** [January 21, 2020, 12:28pm UTC](https://discourse.julialang.org/t/how-to-create-sharedarrays-on-other-workers/33619 "2020-01-21T12:28:31Z")\
**Posts on this page:** 10\
**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 21, 2020, 12:28pm UTC](https://discourse.julialang.org/t/how-to-create-sharedarrays-on-other-workers/33619/1 "2020-01-21T12:28:31Z")

</div>

I am trying to create a SharedArray on a different worker in julia 1.3.0.  
Consider the following code snippet:

```julia
using Distributed
addprocs(1)

using SharedArrays

f = @spawnat 2 SharedVector{Float64}(10)
fetch(f)

```

If I try to execute it, I get an error

> ERROR: On worker 2:  
> UndefVarError: SharedVector not defined

What am I doing wrong? Is it possible to create SharedArrays on other workers at all?

The reason I am trying to do it in the first place is the following.  
For my parallel computation, I reserve several SharedArrays from the master process and then pass them to the workers. If I run this computation on a NUMA machine, there will be a problem for the workers which live on the NUMA node different from the one occupied by the master process (but may be I am wrong). I think that the problem can be solved if the Arrays backing the SharedArrays are created on the respective workers.

---

<div class="post-metadata">

**Author:** ![dmolina](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmolina/32/5246_2.png) [@dmolina](https://discourse.julialang.org/u/dmolina)\
**Post date:** [January 21, 2020, 1:35pm UTC](https://discourse.julialang.org/t/how-to-create-sharedarrays-on-other-workers/33619/2 "2020-01-21T13:35:42Z")

</div>

Hello, @Gregstrq.

I think you have to create the SharedArray first, and later you can work with it with the different process. It has not sense to try to create the shared vector only in one process. A shared vector is a vector whose values are distributed between several processes, see [Parallel Computing · The Julia Language](https://docs.julialang.org/en/v1/manual/parallel-computing/)

Take in account, anyway, that the processing time must be expensive enough to compensate the distribution, if not, it is better to use threads.

---

<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 21, 2020, 1:51pm UTC](https://discourse.julialang.org/t/how-to-create-sharedarrays-on-other-workers/33619/3 "2020-01-21T13:51:53Z")

</div>

Check this thread:

> [@Should a view of a SharedArray be also shared between workers?](https://discourse.julialang.org/t/should-a-view-of-a-sharedarray-be-also-shared-between-workers/33580):
>
> 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 impli…

---

<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 21, 2020, 2:11pm UTC](https://discourse.julialang.org/t/how-to-create-sharedarrays-on-other-workers/33619/4 "2020-01-21T14:11:47Z")

</div>

In that thread, the function was not defined on the other worker.  
Here, `using SharedArrays` should have defined the necessary structures on all of the workers.

---

<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 21, 2020, 2:13pm UTC](https://discourse.julialang.org/t/how-to-create-sharedarrays-on-other-workers/33619/5 "2020-01-21T14:13:09Z")

</div>

I don’t think so.  
Here and there it should be

```julia
@everywhere using SharedArrays

```

---

<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 21, 2020, 2:25pm UTC](https://discourse.julialang.org/t/how-to-create-sharedarrays-on-other-workers/33619/6 "2020-01-21T14:25:56Z")

</div>

> I think you have to create the SharedArray first, and later you can work with it with the different process. It has not sense to try to create the shared vector only in one process. A shared vector is a vector whose values are distributed between several processes, see [Parallel Computing · The Julia Language](https://docs.julialang.org/en/v1/manual/parallel-computing/)

I meant that I construct SharedArray-s on master process, but they are indeed shared across all workers.  
My problem is with memory allocation: on machine with non-uniform memory access (NUMA) the cores have more fast access to some parts of RAM and more slow access to other parts of RAM.  
I am not sure, but I think that when you create a SharedArray on some process, the memory is allocated in the region of RAM corresponding to the cpu this process runs on. If you try to use this SharedArray from the process which runs on the other cpu node, the acess to the underlying memory would be slower.

---

<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 21, 2020, 2:35pm UTC](https://discourse.julialang.org/t/how-to-create-sharedarrays-on-other-workers/33619/7 "2020-01-21T14:35:36Z")

</div>

> I don’t think so.  
> Here and there it should be
> 
> ```julia
> @everywhere using SharedArrays
> 
> ```

My bad. You were right. `using` loads the package but doesn’t bring it into scope on other workers.  
I should have checked the docs more thoroughly.

---

<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 21, 2020, 2:41pm UTC](https://discourse.julialang.org/t/how-to-create-sharedarrays-on-other-workers/33619/8 "2020-01-21T14:41:18Z")

</div>

If someone else encounters a similar problem, I should also point out that the alternative to

```julia
@everywhere using SharedArrays

```

is to use

```julia
f = @spawnat 2 SharedArrays.SharedVector{Float64}(10)

```

---

<div class="post-metadata">

**Author:** ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)\
**Post date:** [January 21, 2020, 6:12pm UTC](https://discourse.julialang.org/t/how-to-create-sharedarrays-on-other-workers/33619/9 "2020-01-21T18:12:41Z")

</div>

I may be talking out of turn here. On a large NUMA machine I have lot of experience in using cpusets and corresponding memory sets (768 core SGI UV 1000)  
These days you would be using cgroups for this.  
Which then leads to containerisarion.  
So I think the answer to what you are wanting to accomplish is to run Julia processes in containers, which then have isolated memory spaces, and the containers should have the memory ‘pinned’ to to particular cpus they are allocated to.

---

<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:** [February 3, 2020, 5:48pm UTC](https://discourse.julialang.org/t/how-to-create-sharedarrays-on-other-workers/33619/10 "2020-02-03T17:48:05Z")

</div>

Do you know of a good guide about using Julia with cgroups?
