Skip to content

Commit

Permalink
Merge 64b8a3b into 1d3eca1
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsamaroo committed Oct 23, 2020
2 parents 1d3eca1 + 64b8a3b commit ee02f80
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
25 changes: 25 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,31 @@ s = delayed(combine)(p, q, r)

@assert collect(s) == 16
```

The above computation can also be written in a more Julia-idiomatic syntax with `@par`:

```julia
p = @par add1(4)
q = @par add2(p)
r = @par add1(3)
s = @par combine(p, q, r)

@assert collect(s) == 16
```

or similarly:

```julia
s = @par begin
p = add1(4)
q = add2(p)
r = add1(3)
combine(p, q, r)
end

@assert collect(s) == 16
```

The connections between nodes `p`, `q`, `r` and `s` is represented by this dependency graph:

![graph](https://user-images.githubusercontent.com/25916/26920104-7b9b5fa4-4c55-11e7-97fb-fe5b9e73cae6.png)
Expand Down
37 changes: 37 additions & 0 deletions src/thunk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,43 @@ end

delayedmap(f, xs...) = map(delayed(f), xs...)

"""
@par f(args...) -> Thunk
Convenience macro to call `Dagger.delayed` on `f` with arguments `args`.
May also be called with a series of assignments like so:
```julia
x = @par begin
a = f(1,2)
b = g(a,3)
h(a,b)
end
```
`x` will hold the Thunk representing `h(a,b)`; additionally, `a` and `b`
will be defined in the same local scope and will be equally accessible
for later calls.
"""
macro par(ex)
_ex = _par(ex)
dump(_ex)
@show _ex
_ex
end
function _par(ex::Expr)
if ex.head == :call
f = ex.args[1]
args = ex.args[2:end]
# TODO: Support kwargs
return :(Dagger.delayed($(esc(f)))($(_par.(args)...)))
else
return Expr(ex.head, _par.(ex.args)...)
end
end
_par(ex::Symbol) = esc(ex)
_par(ex) = ex

persist!(t::Thunk) = (t.persist=true; t)
cache_result!(t::Thunk) = (t.cache=true; t)

Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ using Dagger

include("fakeproc.jl")

include("thunk.jl")
include("domain.jl")
include("array.jl")
include("scheduler.jl")
Expand Down
23 changes: 23 additions & 0 deletions test/thunk.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
@testset "@par" begin
@testset "per-call" begin
x = 2
a = @par x + x
@test a isa Dagger.Thunk
b = @par sum([x,1,2])
c = @par a * b
@test collect(c) == 20
end

@testset "block" begin
c = @par begin
x = 2
a = x + x
b = sum([x,1,2])
c = a * b
end
@test x isa Int
@test a isa Dagger.Thunk
@test c isa Dagger.Thunk
@test collect(c) == 20
end
end

0 comments on commit ee02f80

Please sign in to comment.