# Invoking "remote" variables in remote computations

**URL:** <https://discourse.julialang.org/t/invoking-remote-variables-in-remote-computations/40134>\
**Category:** General Usage\
**Created:** [May 25, 2020, 3:03pm UTC](https://discourse.julialang.org/t/invoking-remote-variables-in-remote-computations/40134 "2020-05-25T15:03:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![martincornejo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martincornejo/32/13436_2.png) [@martincornejo](https://discourse.julialang.org/u/martincornejo)\
**Post date:** [May 25, 2020, 3:03pm UTC](https://discourse.julialang.org/t/invoking-remote-variables-in-remote-computations/40134/1 "2020-05-25T15:03:23Z")

</div>

Let’s suppose in a distributed computing task, for each worker or process we’d like to compute some data (for example in the form of a matrix `A`) which might or might not be unique for that worker and will be useful later for further computations. `A` is relatively inexpensive to compute but expensive to send across processes, in that sense we want to store the data as a local variable in the remote worker.

To my understanding `@spawnat` and `remotecall` use the computing power of the workers, but without affecting their workspace. In `@spawnat workers()[1] A = rand(2,2)`, `A` is not stored in `workers()[1]`. To do so, the `@everywhere` macro is a candidate for the job.

```julia
@everywhere workers() A = rand(2,2)

```

Then problem I find then is how to invoke `A` for computations (say `A*b` with a local vector `b`), as `@spawnat` and `remotecall` read only local variables.

Using `@everywhere` let’s us invoke `A` into the computation

```julia
b = rand(2)
@everywhere workers() x = A*$b

```

But how should then the `x`’s be fetched back? I thought through a RemoteChannel, but I couldn’t set one up.

---

<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:** [May 26, 2020, 5:58am UTC](https://discourse.julialang.org/t/invoking-remote-variables-in-remote-computations/40134/2 "2020-05-26T05:58:43Z")

</div>

One way is to pass in a `RemoteChannel` reference to the workers, eg:

```julia
julia> r = RemoteChannel(()->Channel{Any}(nworkers()));

julia> @everywhere workers() function Atimesb(b,r)
           A = rand(2,2)
           x = A*b;
           put!(r,b)
       end

julia> @everywhere workers() Atimesb($b,$r)

# collect the results on the master
julia> result = [take!(r) for i=1:nworkers()]
2-element Array{Array{Float64,1},1}:
 [0.16141836526706888, 0.12093141352370562]
 [0.16141836526706888, 0.12093141352370562]

```
