Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
amitmurthy committed Jan 21, 2017
1 parent 721a090 commit 2fed2d9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 44 deletions.
15 changes: 6 additions & 9 deletions base/multi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2289,9 +2289,8 @@ end
Clears global bindings in modules by initializing them to `nothing`.
`syms` should be of type `Symbol` or a collection of `Symbol`s . `pids` and `mod`
identify the processes and the module in which global variables
are to be reinitialized. Non-existing bindings for requested names are silently
ignored.
identify the processes and the module in which global variables are to be
reinitialized. Only those names found to be defined under `mod` are cleared.
An exception is raised if a global constant is requested to be cleared.
"""
Expand All @@ -2300,11 +2299,9 @@ function clear!(syms, pids=workers(); mod=Main)
@async remotecall_wait(clear_impl!, p, syms, mod)
end
end
clear!(sym::Symbol, pid::Int=myid(); mod=Main) = clear!([sym], [pid]; mod=mod)
clear!(syms::Vector{Symbol}, pid::Int=myid(); mod=Main) = clear!(sym, [pid]; mod=mod)
clear!(sym::Symbol, pids::Vector{Int}; mod=Main) = clear!([sym], pids; mod=mod)
clear!(sym::Symbol, pid::Int; mod=Main) = clear!([sym], [pid]; mod=mod)
clear!(sym::Symbol, pids=workers(); mod=Main) = clear!([sym], pids; mod=mod)
clear!(syms, pid::Int; mod=Main) = clear!(syms, [pid]; mod=mod)



clear_impl!(syms::Vector, mod::Module) = foreach(x->clear_impl!(x,mod), syms)
clear_impl!(syms, mod::Module) = foreach(x->clear_impl!(x,mod), syms)
clear_impl!(sym::Symbol, mod::Module) = isdefined(mod, sym) && eval(mod, :(global $sym = nothing))
16 changes: 8 additions & 8 deletions doc/src/manual/parallel-computing.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ Expressions executed remotely via `@spawn`, or closures specified for remote exe
a little differently compared to global bindings in other modules. Consider the following code
snippet:
```
```julia
A = rand(10,10)
remotecall_fetch(()->foo(A), 2)
```
Expand All @@ -258,13 +258,13 @@ with embedded global references (under `Main` module only) manage globals as fol

- New global bindings are created on destination workers if they are referenced as part of a remote call.

- Globals constants are declared as constants on remote nodes too.
- Global constants are declared as constants on remote nodes too.

- Globals are re-sent to a destination worker only in the context of a remote call, and then only
if its value has changed. Also, the cluster does not synchronize global bindings across nodes.
For example:

```
```julia
A = rand(10,10)
remotecall_fetch(()->foo(A), 2) # worker 2
A = rand(10,10)
Expand All @@ -281,20 +281,20 @@ on the master, no such action is taken on the workers as the bindings continue t
they are no longer required. This will release any memory associated with them as part of a regular garbage
collection cycle.

Thus programs should be careful referencing globals in remote calls. In fact, it is preferrable to avoid them
altogether if possible. If you must reference globals, consider using `let` blocks to localize globals.
Thus programs should be careful referencing globals in remote calls. In fact, it is preferable to avoid them
altogether if possible. If you must reference globals, consider using `let` blocks to localize global variables.

For example:

```
```julia
julia> A = rand(10,10);

julia> remotecall_fetch(()->A, 2);

julia> B = rand(10,10);

julia> let B = B
remotecall_fetch(()->B, 2);
remotecall_fetch(()->B, 2)
end;

julia> @spawnat 2 whos();
Expand All @@ -307,7 +307,7 @@ julia> From worker 2: A 800 bytes 10×10 Array
```

As can be seen, global variable `A` is defined on worker 2, but `B` is captured as a local variable
and hence a binding for `B` does not exist on 2.
and hence a binding for `B` does not exist on worker 2.


## Parallel Map and Loops
Expand Down
62 changes: 35 additions & 27 deletions test/parallel_exec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1422,36 +1422,44 @@ end
@test x == map(_->sin(2), 1:2)

# Testing clear!
foosym = rand()
barsym = rand()
bazsym = rand()

@test foosym == remotecall_fetch(()->foosym, id_other)
@test remotecall_fetch(isdefined, id_other, :foosym)
clear!(:foosym, id_other)
remote_foosym = remotecall_fetch(()->getfield(Main, :foosym), id_other)
@test remote_foosym === nothing
@test remote_foosym != foosym

for p in workers()
@test barsym == remotecall_fetch(()->barsym, p)
@test bazsym == remotecall_fetch(()->bazsym, p)
function setup_syms(n, pids)
syms = []
for i in 1:n
symstr = string("clrtest", randstring())
sym = Symbol(symstr)
eval(:(global $sym = rand()))
for p in pids
eval(:(@test $sym == remotecall_fetch(()->$sym, $p)))
eval(:(@test remotecall_fetch(isdefined, $p, Symbol($symstr))))
end
push!(syms, sym)
end
syms
end

@test remotecall_fetch(isdefined, p, :barsym)
@test remotecall_fetch(isdefined, p, :bazsym)
function test_clear(syms, pids)
for p in pids
for sym in syms
remote_val = remotecall_fetch(()->getfield(Main, sym), p)
@test remote_val === nothing
@test remote_val != getfield(Main, sym)
end
end
end

clear!([:barsym, :bazsym, :dontexistsym], workers())
syms = setup_syms(1, [id_other])
clear!(syms[1], id_other)
test_clear(syms, [id_other])

for p in workers()
remote_barsym = remotecall_fetch(()->getfield(Main, :barsym), p)
@test remote_barsym === nothing
@test remote_barsym != barsym
syms = setup_syms(1, workers())
clear!(syms[1], workers())
test_clear(syms, workers())

remote_bazsym = remotecall_fetch(()->getfield(Main, :bazsym), p)
@test remote_bazsym === nothing
@test remote_bazsym != bazsym
syms = setup_syms(3, [id_other])
clear!(syms, id_other)
test_clear(syms, [id_other])

syms = setup_syms(3, workers())
clear!(syms, workers())
test_clear(syms, workers())

@test remotecall_fetch(isdefined, p, :barsym)
@test remotecall_fetch(isdefined, p, :bazsym)
end

0 comments on commit 2fed2d9

Please sign in to comment.