# Modifying arrays by views on workers

**URL:** <https://discourse.julialang.org/t/modifying-arrays-by-views-on-workers/45198>\
**Category:** Julia at Scale\
**Created:** [August 19, 2020, 8:47am UTC](https://discourse.julialang.org/t/modifying-arrays-by-views-on-workers/45198 "2020-08-19T08:47:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tansongchen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tansongchen/32/17310_2.png) [@tansongchen](https://discourse.julialang.org/u/tansongchen)\
**Post date:** [August 19, 2020, 8:47am UTC](https://discourse.julialang.org/t/modifying-arrays-by-views-on-workers/45198/1 "2020-08-19T08:47:29Z")

</div>

Julia has been good in my parallel computing works, however I encounter an issue recently. Let’s say I have an array `a` and I would like a `@view` as a shortcut to modify or refer to part of `a`, so I make a data structure about this, e.g.

```julia
using Distributed

@everywhere struct MyArray
    a::Vector
    b::SubArray
    MyArray(a::Vector) = new(a, @view a[1:2])
end

```

Now I would like to create an array, launch a parallel computing on some workers with this array and calculate some stuff:

```julia

m = MyArray([1., 2., 3., 4.])

future = @spawnat 2 (m.b .= 0; sum(m.a))
remoteResult = fetch(future)
localResult = (m.b .= 0; sum(m.a))
println(remoteResult, localResult)

```

But when I run these code I get `remoteResult = 10.0` and `localResult = 7.0`. It seems that the relation between a `SubArray` and an `Array` is removed after they are transferred to a worker.

I would like to know whether (1) this is an inconsistency in Julia `Distributed`, or (2) I am using parallel features improperly? If (2), what should I do instead?

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [August 19, 2020, 9:26am UTC](https://discourse.julialang.org/t/modifying-arrays-by-views-on-workers/45198/2 "2020-08-19T09:26:22Z")

</div>

I don’t have the answer but conceptually, modifying a portion of an array on different workers and expecting that the local array will be modified by what the remote machine did doesn’t make sense to me.

what you should do instead is split up your work and return the sub-result from each worker and then reduce the various results together at the end. if the result is a sum, do a sum of the first half on one machine and the sum of the second half on the other… then when the result comes back… add them together

---

<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:** [August 19, 2020, 11:22am UTC](https://discourse.julialang.org/t/modifying-arrays-by-views-on-workers/45198/3 "2020-08-19T11:22:29Z")

</div>

It appears that views do not get propagated to workers.

```julia
julia> m = MyArray([1., 2., 3., 4.])
MyArray([1.0, 2.0, 3.0, 4.0], [1.0, 2.0])

julia> @fetchfrom 2 m
MyArray([1.0, 2.0, 3.0, 4.0], [0.0, 0.0])

```

This may be because the array `m.a` gets copied when it is passed to a worker, and the view on the worker has no reference to the copy.

```julia
julia> m.a === @fetchfrom 2 m.a
false

julia> @fetchfrom 2 parent(m.b)
2-element Array{Float64,1}:
 0.0
 0.0

```

One way around this would be to initiate the view on the worker.

```julia
julia> remoteResult = @fetchfrom 2 (p = MyArray(m.a); p.b .= 0; sum(p.a))
7.0

```

---

<div class="post-metadata">

**Author:** ![tansongchen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tansongchen/32/17310_2.png) [@tansongchen](https://discourse.julialang.org/u/tansongchen)\
**Post date:** [August 20, 2020, 1:51am UTC](https://discourse.julialang.org/t/modifying-arrays-by-views-on-workers/45198/4 "2020-08-20T01:51:21Z")

</div>

I certainly do not expect some modification happened remotely should affect a local array; what is surprising here is: when I copy both an array and its view to a remote machine, the relation between them seems broken.

---

<div class="post-metadata">

**Author:** ![tansongchen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tansongchen/32/17310_2.png) [@tansongchen](https://discourse.julialang.org/u/tansongchen)\
**Post date:** [August 20, 2020, 1:54am UTC](https://discourse.julialang.org/t/modifying-arrays-by-views-on-workers/45198/5 "2020-08-20T01:54:17Z")

</div>

This workaround seems perfect. The rule of thumb here is going to be: create and use a view on the same machine.

Although I think Julia stdlib should have implemented this in a more consistent way…
