# Memory allocation and SharedArrays

**URL:** <https://discourse.julialang.org/t/memory-allocation-and-sharedarrays/27272>\
**Category:** Julia at Scale\
**Tags:** question\
**Created:** [August 7, 2019, 3:28pm UTC](https://discourse.julialang.org/t/memory-allocation-and-sharedarrays/27272 "2019-08-07T15:28:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![HugoLhuillier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hugolhuillier/32/976_2.png) [@HugoLhuillier](https://discourse.julialang.org/u/HugoLhuillier)\
**Post date:** [August 7, 2019, 3:28pm UTC](https://discourse.julialang.org/t/memory-allocation-and-sharedarrays/27272/1 "2019-08-07T15:28:17Z")

</div>

Hi all,

I am new to parallel computing and I had a question regarding memory allocation. Is it possible to update an array in place while doing parallel computing? I tried using `SharedArrays` and `DistributedArrays` without success.

As an example,

```julia
# initializes the array to be updated in place 
T = SharedArray{Float64,2}(10, 10, pids = workers());
for (i, ind) in enumerate(CartesianIndices(T))
    T[ind] = i 
end # works as intended 
# reset all entries to zero
T .= 0.0;
@async @distributed for (i, ind) in enumerate(CartesianIndices(T))
    T[ind] = i 
end # T is only filled with zeros

```

The question [has already been asked on this forum](https://discourse.julialang.org/t/pmap-with-in-place-functions/10523) but I could not find a definitive answer.

Thank you very much in advance!

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [August 7, 2019, 4:04pm UTC](https://discourse.julialang.org/t/memory-allocation-and-sharedarrays/27272/2 "2019-08-07T16:04:02Z")

</div>

Trying to get an MWE this seems to work:

```julia
julia> using Distributed, SharedArrays

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

julia> T = SharedArray{Float64, 2}(10,10, pids = workers());

julia> @sync @distributed for i in 1:100   
          T[i] = i
       end
Task (done) @0x0000000011642850

julia> T
10×10 SharedArray{Float64,2}:
  1.0 11.0 21.0 31.0 41.0 51.0 61.0 71.0 81.0 91.0
  2.0 12.0 22.0 32.0 42.0 52.0 62.0 72.0 82.0 92.0
  3.0 13.0 23.0 33.0 43.0 53.0 63.0 73.0 83.0 93.0
  4.0 14.0 24.0 34.0 44.0 54.0 64.0 74.0 84.0 94.0
  5.0 15.0 25.0 35.0 45.0 55.0 65.0 75.0 85.0 95.0
  6.0 16.0 26.0 36.0 46.0 56.0 66.0 76.0 86.0 96.0
  7.0 17.0 27.0 37.0 47.0 57.0 67.0 77.0 87.0 97.0
  8.0 18.0 28.0 38.0 48.0 58.0 68.0 78.0 88.0 98.0
  9.0 19.0 29.0 39.0 49.0 59.0 69.0 79.0 89.0 99.0
 10.0 20.0 30.0 40.0 50.0 60.0 70.0 80.0 90.0 100.0
```

---

<div class="post-metadata">

**Author:** ![HugoLhuillier](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hugolhuillier/32/976_2.png) [@HugoLhuillier](https://discourse.julialang.org/u/HugoLhuillier)\
**Post date:** [August 7, 2019, 4:42pm UTC](https://discourse.julialang.org/t/memory-allocation-and-sharedarrays/27272/3 "2019-08-07T16:42:25Z")

</div>

That is indeed working! Surprising difference…

---

<div class="post-metadata">

**Author:** ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)\
**Post date:** [August 7, 2019, 10:43pm UTC](https://discourse.julialang.org/t/memory-allocation-and-sharedarrays/27272/4 "2019-08-07T22:43:10Z")

</div>

You should `wait()` on that `@async` block to ensure it runs, and doesn’t throw any errors (which you won’t find out about until you `wait()` on it, anyway).
