# How to access the localpart of a distributed array?

**URL:** https://discourse.julialang.org/t/how-to-access-the-localpart-of-a-distributed-array/6711
**Category:** Julia at Scale
**Created:** [October 26, 2017, 9:44pm UTC](https://discourse.julialang.org/t/how-to-access-the-localpart-of-a-distributed-array/6711 "2017-10-26T21:44:37Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [October 26, 2017, 9:44pm UTC](https://discourse.julialang.org/t/how-to-access-the-localpart-of-a-distributed-array/6711/1 "2017-10-26T21:44:37Z")

</div>

I was trying to learn [distributed array](https://github.com/JuliaParallel/DistributedArrays.jl/blob/master/README.md). And I can distribute the arrays using `distribute`, however I can’t work out how to get the localparts of arrays.

As a MWE here what I am trying to do

```julia
addprocs()
@everywhere using DistributedArrays

a = rand(1:5, 1_000_000)
b= rand(1_000_000)

@time da= distribute(a)
@time db= distribute(a)

sum(da) #these work fine
sum(db)

# I want to something like
@everywhere res = localpart(da) .* localpart(db)

```

But it says `da` and `db` are not defined on the workers, which makes sense, so how can define `da` and `db` in the workers?

My actual use cases uses much more complicated algorithms, but I would lilke to work with two or more distributed vectors.

Also just realised, wouldn’t it cause an issue where the vectors are distributed differently on different workers? I need finer control over how I distribute the vectors too then.

---

<div class="post-metadata">

### Author: ![raminammour](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raminammour/32/13572_2.png) [@raminammour](https://discourse.julialang.org/u/raminammour)
#### Post date: [October 26, 2017, 10:41pm UTC](https://discourse.julialang.org/t/how-to-access-the-localpart-of-a-distributed-array/6711/2 "2017-10-26T22:41:24Z")

</div>

Two things:

1- if you just do `res=da.*db`, then `res` is a distributed array with its localparts corresponding to what I think you want.  
2- the message that you are seeing has to do with `@everywhere`. from the help:

```julia
@everywhere bar=1

```

will define Main.bar on all processes.

Unlike `@spawn` and `@spawnat`,` @everywhere` does not capture any local variables. Prefixing `@everywhere` with `@eval` allows us to broadcast local variables using interpolation :

```julia
  foo = 1
  @eval @everywhere bar=$foo

```

So if you really wanted `res` to be the same variable name everywhere, this will do:

```julia
@eval @everywhere res=localpart($da).*localpart($db)

```

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [October 27, 2017, 9:15pm UTC](https://discourse.julialang.org/t/how-to-access-the-localpart-of-a-distributed-array/6711/3 "2017-10-27T21:15:42Z")

</div>

The reason why distributed arrays are useful is because we can parallelize operations? Does `res=da.*db` auto-parallelize? The solution on how to do this is not clear.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [October 27, 2017, 9:58pm UTC](https://discourse.julialang.org/t/how-to-access-the-localpart-of-a-distributed-array/6711/4 "2017-10-27T21:58:09Z")

</div>

> [@xiaodai](#):
>
> The reason why distributed array are useful is because we can parallelize operation? Does res=da.\*db auto-parallelize? The solution on how to do this is not clear.

Yes and yes, but it needs help.

> <https://github.com/JuliaParallel/DistributedArrays.jl/blob/7d213890aec31fef29364a4dc2a60cffb0faaf5c/src/mapreduce.jl#L23>

---

<div class="post-metadata">

### Author: ![raminammour](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raminammour/32/13572_2.png) [@raminammour](https://discourse.julialang.org/u/raminammour)
#### Post date: [October 27, 2017, 10:21pm UTC](https://discourse.julialang.org/t/how-to-access-the-localpart-of-a-distributed-array/6711/5 "2017-10-27T22:21:20Z")

</div>

Yes, as Chris said. Not everything is implemented or optimal but for simple operations it is.  
The easiest way to check

```julia
@which da.*db

```

If it is not implemented, falls back on AbstractArray implementation (which can be slow).

In this case you will see that it is implemented and does what you want, efficiently.

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [October 27, 2017, 10:22pm UTC](https://discourse.julialang.org/t/how-to-access-the-localpart-of-a-distributed-array/6711/6 "2017-10-27T22:22:46Z")

</div>

Actually my use-case was alot more complicated than `.*`. I guess I have to learn alot more about distributed arrays before I can do what I want with it.

---

<div class="post-metadata">

### Author: ![raminammour](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raminammour/32/13572_2.png) [@raminammour](https://discourse.julialang.org/u/raminammour)
#### Post date: [October 27, 2017, 11:57pm UTC](https://discourse.julialang.org/t/how-to-access-the-localpart-of-a-distributed-array/6711/7 "2017-10-27T23:57:55Z")

</div>

A more general want to code with them is as follows:

```julia
da=DArray(...)
for ip in procs(da)
   @spawnat ip begin
        Do things with local parts or access other parts (entail communication)
End 
End

```

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [October 27, 2017, 11:59pm UTC](https://discourse.julialang.org/t/how-to-access-the-localpart-of-a-distributed-array/6711/8 "2017-10-27T23:59:28Z")

</div>

> [@raminammour](#):
>
> @spawnat ip begin  
> Do things with local parts or access other parts (entail communication)

How do I refer to `da` in each of the subprocesses?

---

<div class="post-metadata">

### Author: ![raminammour](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raminammour/32/13572_2.png) [@raminammour](https://discourse.julialang.org/u/raminammour)
#### Post date: [October 28, 2017, 5:37am UTC](https://discourse.julialang.org/t/how-to-access-the-localpart-of-a-distributed-array/6711/9 "2017-10-28T05:37:40Z")

</div>

If you use the spawnat macro, then just `da`.

`@spawnat procs(da)[2] sum(localpart(da))`

Would work, for example. `@spawnat` interpolates, unlike `@everywhere`.
