Skip to content

Commit

Permalink
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 1d3eca1 commit 2cc2fde
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
12 changes: 12 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ 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
```

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
13 changes: 13 additions & 0 deletions src/thunk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ end

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

"""
@par f(args...) -> Thunk
Convenience macro to call `Dagger.delayed` on `f` with arguments `args`.
"""
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...)))
end

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
8 changes: 8 additions & 0 deletions test/thunk.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@testset "@par" 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

0 comments on commit 2cc2fde

Please sign in to comment.