Skip to content

Commit be403f3

Browse files
authored
Merge pull request #372 from JuliaParallel/jps/at-spawn-broadcast
at-spawn: Add support for broadcasting
2 parents 54629f9 + f3986fc commit be403f3

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/thunk.jl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,20 @@ macro spawn(exs...)
367367
_par(ex; lazy=false, opts=opts)
368368
end
369369

370+
struct ExpandedBroadcast{F} end
371+
(eb::ExpandedBroadcast{F})(args...) where F =
372+
Base.materialize(Base.broadcasted(F, args...))
373+
replace_broadcast(ex) = ex
374+
function replace_broadcast(fn::Symbol)
375+
if startswith(string(fn), '.')
376+
return :($ExpandedBroadcast{$(Symbol(string(fn)[2:end]))}())
377+
end
378+
return fn
379+
end
380+
370381
function _par(ex::Expr; lazy=true, recur=true, opts=())
371382
if ex.head == :call && recur
372-
f = ex.args[1]
383+
f = replace_broadcast(ex.args[1])
373384
args = ex.args[2:end]
374385
opts = esc.(opts)
375386
if lazy

test/thunk.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ end
6161
@test fetch(c) == 20
6262
end
6363
@test Dagger.Sch.EAGER_CONTEXT[] isa Context
64+
@testset "broadcast" begin
65+
A, B = rand(4), rand(4)
66+
@test fetch(@spawn A .+ B) A .+ B
67+
@test fetch(@spawn A .* B) A .* B
68+
end
6469
@testset "waiting" begin
6570
a = @spawn sleep(1)
6671
@test !isready(a)

0 commit comments

Comments
 (0)