Skip to content

Commit 4f12941

Browse files
committed
add remote macro for better Distributed compatibility
1 parent 5bd8cfd commit 4f12941

3 files changed

Lines changed: 51 additions & 2 deletions

File tree

src/DistributedData.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export save_at,
1919
dmap,
2020
dpmap,
2121
gather_array,
22-
tmp_symbol
22+
tmp_symbol,
23+
@remote
2324

2425
include("io.jl")
2526
export dstore, dload, dunlink

src/base.jl

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Get a value `val` from a remote `worker`; quoting of `val` works just as with
4646
`save_at`. Returns a future with the requested value.
4747
"""
4848
function get_from(worker, val; mod = Main)
49-
remotecall(() -> Base.eval(mod, :($val)), worker)
49+
remotecall(() -> Base.eval(mod, val), worker)
5050
end
5151

5252
"""
@@ -358,3 +358,41 @@ Decorate the symbol from `dInfo` with prefix and suffix.
358358
function tmp_symbol(dInfo::Dinfo; prefix = "", suffix = "_tmp")
359359
return tmp_symbol(dInfo.val, prefix = prefix, suffix = suffix)
360360
end
361+
362+
"""
363+
@remote module expr
364+
365+
A version of [`@remote`](@ref) that adds additional choice of the module for
366+
scope.
367+
"""
368+
macro remote(mod, x)
369+
:(Base.eval($mod, $(QuoteNode(x))))
370+
end
371+
372+
"""
373+
@remote expr
374+
375+
In a function that will get evaluated on a remote worker, this ensures the
376+
evaluation scope of the expression `expr` (usually a variable) is taken on
377+
the remote side, preventing namespace clash with the local session.
378+
379+
This is mainly useful for making the functions from `Distributed` package (such
380+
as `pmap` and `remotecall`) work with the data stored by `DistributedData`
381+
package.
382+
383+
Internally, this is handled by wrapping in `eval`.
384+
385+
# Example
386+
```
387+
julia> save_at(2, :x, 321)
388+
Future(2, 1, 162, nothing)
389+
390+
julia> let x=123
391+
remotecall_fetch(() -> x + (@remote x), 2)
392+
end
393+
444
394+
```
395+
"""
396+
macro remote(x)
397+
:(@remote Main $x)
398+
end

test/base.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@
129129
@test all([!isfile(f) for f in files])
130130
end
131131

132+
@testset "@remote macro" begin
133+
di = dtransform(:(), _ -> myid(), W, :test)
134+
135+
test = 333
136+
137+
for pid in W
138+
@test remotecall_fetch(() -> test + (@remote test), pid) == test + pid
139+
end
140+
end
141+
132142
rmprocs(W)
133143
W = nothing
134144

0 commit comments

Comments
 (0)