Skip to content

Commit 1984b60

Browse files
authored
Merge pull request #204 from JuliaSymbolics/s/par
Par for parallelism
2 parents aa41078 + 0ec7af3 commit 1984b60

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/code.jl

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ module Code
33
using StaticArrays, LabelledArrays, SparseArrays
44

55
export toexpr, Assignment, (), Let, Func, DestructuredArgs, LiteralExpr,
6-
SetArray, MakeArray, MakeSparseArray, MakeTuple, AtIndex
6+
SetArray, MakeArray, MakeSparseArray, MakeTuple, AtIndex,
7+
Par, Multithreaded
78

89
import ..SymbolicUtils
910
import SymbolicUtils: @matchable, Sym, Term, istree, operation, arguments
@@ -461,6 +462,24 @@ function toexpr(a::MakeTuple, st)
461462
:(($(toexpr.(a.elems, (st,))...),))
462463
end
463464

465+
struct Multithreaded end
466+
"""
467+
Par(exprs, reduce)
468+
469+
"""
470+
struct Par{Typ}
471+
exprs::Vector
472+
combine
473+
end
474+
475+
function toexpr(p::Par{Multithreaded}, st)
476+
spawns = map(p.exprs) do thunk
477+
:(Base.Threads.@spawn $(toexpr(thunk, st)))
478+
end
479+
quote
480+
$(toexpr(p.combine, st))(map(fetch, ($(spawns...),))...)
481+
end
482+
end
464483

465484
"""
466485
LiteralExpr(ex)

test/code.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,10 @@ test_repr(a, b) = @test repr(Base.remove_linenums!(a)) == repr(Base.remove_linen
121121

122122
test_repr(toexpr(MakeSparseArray(spvec)),
123123
:(SparseVector(10, $(spvec.nzind), [a])))
124-
125124
test_repr(toexpr(MakeTuple((a, b, a+b))),
126125
:((a,b,$(+)(a,b))))
126+
127+
@test Par{Multithreaded}([1,2],vcat)|>toexpr|>eval == [1,2]
128+
@test @elapsed(Par{Multithreaded}([:(sleep(.3)),:(sleep(.6))],vcat)|>toexpr|>eval) < 0.8
127129
end
128130

0 commit comments

Comments
 (0)