From 30ed04c5a3bc025170ef15aa0b20175d124a591c Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Fri, 21 Aug 2015 13:38:16 -0400 Subject: [PATCH] fix #8207, `@parallel` evaluates its range twice --- base/multi.jl | 23 +++++++++++------------ test/parallel.jl | 8 ++++++++ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/base/multi.jl b/base/multi.jl index 59dd6aae995b2..58004f45d21ef 100644 --- a/base/multi.jl +++ b/base/multi.jl @@ -1491,34 +1491,29 @@ function pfor(f, N::Int) [@spawn f(first(c), last(c)) for c in splitrange(N, nworkers())] end -function make_preduce_body(reducer, var, body, ran) - localize_vars( +function make_preduce_body(reducer, var, body, R) quote function (lo::Int, hi::Int) - R = $(esc(ran)) - $(esc(var)) = R[lo] + $(esc(var)) = ($R)[lo] ac = $(esc(body)) if lo != hi - for $(esc(var)) in R[(lo+1):hi] + for $(esc(var)) in ($R)[(lo+1):hi] ac = ($(esc(reducer)))(ac, $(esc(body))) end end ac end end - ) end -function make_pfor_body(var, body, ran) - localize_vars( +function make_pfor_body(var, body, R) quote function (lo::Int, hi::Int) - for $(esc(var)) in ($(esc(ran)))[lo:hi] + for $(esc(var)) in ($R)[lo:hi] $(esc(body)) end end end - ) end macro parallel(args...) @@ -1537,16 +1532,20 @@ macro parallel(args...) var = loop.args[1].args[1] r = loop.args[1].args[2] body = loop.args[2] + localize_vars( if na==1 quote - pfor($(make_pfor_body(var, body, r)), length($(esc(r)))) + therange = $(esc(r)) + pfor($(make_pfor_body(var, body, :therange)), length(therange)) end else quote + therange = $(esc(r)) preduce($(esc(reducer)), - $(make_preduce_body(reducer, var, body, r)), length($(esc(r)))) + $(make_preduce_body(reducer, var, body, :therange)), length(therange)) end end + ) end diff --git a/test/parallel.jl b/test/parallel.jl index 777c2c415c071..8d316ba1a83cd 100644 --- a/test/parallel.jl +++ b/test/parallel.jl @@ -506,3 +506,11 @@ let t = @task 42 schedule(t, ErrorException(""), error=true) @test_throws ErrorException wait(t) end + +# issue #8207 +let A = Any[] + @parallel (+) for i in (push!(A,1); 1:2) + i + end + @test length(A) == 1 +end