Skip to content

Commit

Permalink
Merge pull request #3449 from amitmurthy/amitm/fetchfrom
Browse files Browse the repository at this point in the history
Implemented @fetch and @fetchfrom, the fetch versions of @Spawn and @spa...
  • Loading branch information
JeffBezanson committed Jun 20, 2013
2 parents 5b7df8e + ec4e5e2 commit 4f05da7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
2 changes: 2 additions & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,8 @@ export
@spawn,
@spawnlocal, # deprecated
@spawnat,
@fetch,
@fetchfrom,
@everywhere,
@parallel,
@gensym,
Expand Down
28 changes: 21 additions & 7 deletions base/multi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1072,8 +1072,6 @@ function sync_add(r)
r
end

spawnat(p, thunk) = sync_add(remotecall(p, thunk))

let nextidx = 1
global chooseproc
function chooseproc(thunk::Function)
Expand Down Expand Up @@ -1102,13 +1100,34 @@ let nextidx = 1
end
end

spawnat(p, thunk) = sync_add(remotecall(p, thunk))

spawn_somewhere(thunk) = spawnat(chooseproc(thunk),thunk)

macro spawn(expr)
expr = localize_vars(:(()->($expr)), false)
:(spawn_somewhere($(esc(expr))))
end

macro spawnat(p, expr)
expr = localize_vars(:(()->($expr)), false)
:(spawnat($(esc(p)), $(esc(expr))))
end

macro fetch(expr)
expr = localize_vars(:(()->($expr)), false)
quote
thunk = $(esc(expr))
remotecall_fetch(chooseproc(thunk), thunk)
end
end

macro fetchfrom(p, expr)
expr = localize_vars(:(()->($expr)), false)
:(remotecall_fetch($(esc(p)), $(esc(expr))))
end


function spawnlocal(thunk)
rr = RemoteRef(myid())
sync_add(rr)
Expand All @@ -1132,11 +1151,6 @@ macro spawnlocal(expr)
:(@async $(esc(expr)))
end

macro spawnat(p, expr)
expr = localize_vars(:(()->($expr)), false)
:(spawnat($(esc(p)), $(esc(expr))))
end

function at_each(f, args...)
for w in PGRP.workers
sync_add(remotecall(w.id, f, args...))
Expand Down
2 changes: 2 additions & 0 deletions test/parallel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ id_me = myid()
id_other = filter(x -> x != id_me, procs())[rand(1:(nprocs()-1))]

@test fetch(@spawnat id_other myid()) == id_other
@test @fetchfrom id_other begin myid() end == id_other
@fetch begin myid() end

d = drand((200,200), [id_me, id_other])
s = convert(Array, d[1:150, 1:150])
Expand Down

0 comments on commit 4f05da7

Please sign in to comment.