# Transfer elements of an array to a shared array

**URL:** <https://discourse.julialang.org/t/transfer-elements-of-an-array-to-a-shared-array/87746>\
**Category:** General Usage\
**Tags:** sharedarrays\
**Created:** [September 24, 2022, 3:14pm UTC](https://discourse.julialang.org/t/transfer-elements-of-an-array-to-a-shared-array/87746 "2022-09-24T15:14:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jisutich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jisutich/32/33342_2.png) [@jisutich](https://discourse.julialang.org/u/jisutich)\
**Post date:** [September 24, 2022, 3:14pm UTC](https://discourse.julialang.org/t/transfer-elements-of-an-array-to-a-shared-array/87746/1 "2022-09-24T15:14:18Z")

</div>

Hi,

Suppose I have a `SharedArray` and now I have a normal vector which contains some values. I want to update the elements of my Shared Array by this vector. How to do it? Basically I have sample code

```julia
using Distributed
addprocs(4);

@everywhere using LinearAlgebra
@everywhere using Random
@everywhere using SharedArrays
@everywhere function pool_check(q::SharedArray,Np)
       pool_t = zeros(Np)
       for i =1:Np
             z1= q[rand(1:length(q))]
             z2= q[rand(1:length(q))]
             z = z1+z2
            pool_t[i] = z
       end
       return pool_t
end

q = SharedArray{Float64}(100);
q .= 0.5;
ret = pmap(Np->pool_check(q,Np),fill(10^4,4));

q_new = reduce(vcat,[ret[i] for i = 1:4])

```

Now I want to use elements of `q_new` to replace elements of `q`. But when I try `q = q_new`, it is no longer a shared array. How to avoid this problem?

Thanks

---

<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:** [September 26, 2022, 10:21am UTC](https://discourse.julialang.org/t/transfer-elements-of-an-array-to-a-shared-array/87746/2 "2022-09-26T10:21:50Z")

</div>

When I try your code I get:

```julia
julia> q
100-element SharedVector{Float64}:
...
julia> q_new = reduce(vcat,[ret[i] for i = 1:4])
40000-element Vector{Float64}:
...

```

So, it’s not clear what you mean by `replace elements of q`.

I expected that `q_new` is of same size as `q`. In this case the answer would just be:

```julia
q .= q_new

```

But, of course, this errors as the dimensions are different.

The following demonstrates how it replaces values:

```julia
julia> q .= q_new[1:100]
100-element SharedVector{Float64}:
 1.0
 1.0
 1.0
...

```

---

<div class="post-metadata">

**Author:** ![jisutich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jisutich/32/33342_2.png) [@jisutich](https://discourse.julialang.org/u/jisutich)\
**Post date:** [September 27, 2022, 6:10pm UTC](https://discourse.julialang.org/t/transfer-elements-of-an-array-to-a-shared-array/87746/3 "2022-09-27T18:10:44Z")

</div>

Oh it was my fault. There is a typo in the code, it should be

```julia
ret = pmap(Np->pool_check(q,Np),fill(25,4));

```

So `q` and `q_new` do have same dimensions. My previous problem was that when I tried `q= q_new`, `q` is no longer a Shared Array. I guess `q .= q_new[1:end]` may work.

---

<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:** [September 27, 2022, 6:20pm UTC](https://discourse.julialang.org/t/transfer-elements-of-an-array-to-a-shared-array/87746/4 "2022-09-27T18:20:15Z")

</div>

When the dimensions are equal just

```julia
q .= q_new

```

---

<div class="post-metadata">

**Author:** ![jisutich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jisutich/32/33342_2.png) [@jisutich](https://discourse.julialang.org/u/jisutich)\
**Post date:** [September 27, 2022, 6:44pm UTC](https://discourse.julialang.org/t/transfer-elements-of-an-array-to-a-shared-array/87746/6 "2022-09-27T18:44:56Z")

</div>

I just find `q .= q_new` may not be okay

 ![image](https://global.discourse-cdn.com/julialang/original/3X/f/e/fe0d0228616c851d2cc80f3651cf3f6b927d80d3.jpeg)

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 27, 2022, 6:59pm UTC](https://discourse.julialang.org/t/transfer-elements-of-an-array-to-a-shared-array/87746/7 "2022-09-27T18:59:58Z")

</div>

If they have the same number of elements but different shapes, then you can do

```julia
q .= q_new[:] # allocates
q .= @view q_new[:] # surprisingly, allocates
q .= vec(q_new) # also allocates, surprisingly
copyto!(q, q_new) # this does not allocate, fortunately

for i in eachindex(q, q_new)
    q[i] = q_new[i] # also no allocations
end

```

I guess `copyto!(q, q_new)` is the way to go, but I wonder why options 2 and 3 allocate.

> [@jisutich](#):
>
> `z1= q[rand(1:length(q))]`

BTW, this could just be

```julia
z1 = rand(q) # q[rand(1:end)] also works

```

---

<div class="post-metadata">

**Author:** ![jisutich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jisutich/32/33342_2.png) [@jisutich](https://discourse.julialang.org/u/jisutich)\
**Post date:** [September 27, 2022, 7:32pm UTC](https://discourse.julialang.org/t/transfer-elements-of-an-array-to-a-shared-array/87746/8 "2022-09-27T19:32:44Z")

</div>

Yes, they work. I am surprised that `copyto!` doesn’t allocate. This may be helpful considering the large pool size I use in my practical calculation. Thanks.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 27, 2022, 7:35pm UTC](https://discourse.julialang.org/t/transfer-elements-of-an-array-to-a-shared-array/87746/9 "2022-09-27T19:35:05Z")

</div>

It’s completely natural that `copyto!` doesn’t allocate, it just copies over the elements. The surprise is that `q .= @view q_new[:]` and `q .= vec(q_new)` do.
