Skip to content

Commit

Permalink
fixup! Add at-par macro
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsamaroo committed Oct 23, 2020
1 parent 2cc2fde commit d64f22d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
13 changes: 13 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ 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
34 changes: 29 additions & 5 deletions src/thunk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,38 @@ 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)
@assert ex.head == :call "@par requires a function call as the argument"
f = ex.args[1]
args = ex.args[2:end]
# TODO: Support kwargs
:(Dagger.delayed(f)($(args...)))
_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
11 changes: 11 additions & 0 deletions test/thunk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,15 @@
b = @par sum([x,1,2])
c = @par a * b
@test collect(c) == 20

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

0 comments on commit d64f22d

Please sign in to comment.