# Allocation of distributed arrays to workers

**URL:** <https://discourse.julialang.org/t/allocation-of-distributed-arrays-to-workers/37721>\
**Category:** General Usage\
**Tags:** parallel\
**Created:** [April 16, 2020, 11:46pm UTC](https://discourse.julialang.org/t/allocation-of-distributed-arrays-to-workers/37721 "2020-04-16T23:46:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Diego](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@Diego](https://discourse.julialang.org/u/Diego)\
**Post date:** [April 16, 2020, 11:46pm UTC](https://discourse.julialang.org/t/allocation-of-distributed-arrays-to-workers/37721/1 "2020-04-16T23:46:22Z")

</div>

By default when creating a distributed array each worker is assigned consecutive fields.

So for a 10 element array, worker 1 would control 1:5 and worker 2 6:10.

¿Is there a way of defining the DArrays in a way that worker 1 takes care of the odd elements and worker 2 of the even elements?

Thank you!

---

<div class="post-metadata">

**Author:** ![eaubanel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eaubanel/32/13327_2.png) [@eaubanel](https://discourse.julialang.org/u/eaubanel)\
**Post date:** [April 17, 2020, 2:33pm UTC](https://discourse.julialang.org/t/allocation-of-distributed-arrays-to-workers/37721/2 "2020-04-17T14:33:17Z")

</div>

You can do this with SharedArrays:

```julia
julia> using Distributed
julia> addprocs(2)
julia> @everywhere using SharedArrays
julia> S = SharedArray{Int,1}(10, init = S -> S[indexpids(S):2:length(S)] = repeat([myid()], length(indexpids(S):2:length(S))))
10-element SharedArray{Int64,1}:
 2
 3
 2
 3
 2
 3
 2
 3
 2
 3

```

In other words, you can have each worker access particular elements based on their id (returned by `myid()`)

---

<div class="post-metadata">

**Author:** ![Diego](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@Diego](https://discourse.julialang.org/u/Diego)\
**Post date:** [April 17, 2020, 2:58pm UTC](https://discourse.julialang.org/t/allocation-of-distributed-arrays-to-workers/37721/3 "2020-04-17T14:58:52Z")

</div>

> [@Diego](#):
>
> By default when creating a distributed array each worker is assigned consecutive fields.

I would rather use DistributedArrays due to performance, SharedArrays make a copy to hard disk, while DistributedArrays work from memory.

---

<div class="post-metadata">

**Author:** ![eaubanel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eaubanel/32/13327_2.png) [@eaubanel](https://discourse.julialang.org/u/eaubanel)\
**Post date:** [April 17, 2020, 5:21pm UTC](https://discourse.julialang.org/t/allocation-of-distributed-arrays-to-workers/37721/4 "2020-04-17T17:21:45Z")

</div>

Really? If you don’t create a SharedArray backed by a file (via SharedArray(file, type, dims) ), then I don’t see why the disk would be used. I’ve had good results with SharedArrays, in my limited experience.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [April 20, 2020, 5:50am UTC](https://discourse.julialang.org/t/allocation-of-distributed-arrays-to-workers/37721/5 "2020-04-20T05:50:00Z")

</div>

In any case the question might be pertinent for HPC environments where users aren’t using shared memory

---

<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:** [April 20, 2020, 9:34am UTC](https://discourse.julialang.org/t/allocation-of-distributed-arrays-to-workers/37721/6 "2020-04-20T09:34:52Z")

</div>

Idiot reply from me. Is there a way to reshape the original array such that the even and odd elements appear to be contiguous columns?

I guess I am asking this in a blue sky fashion in general for arrays, which might be interesting.
