Skip to content

Commit

Permalink
fix @showprogress @distributed (timholy#295)
Browse files Browse the repository at this point in the history
* fix macro

* add test in global scope

* don't forget to close while take!(ch)
  • Loading branch information
MarcMush authored Jan 31, 2024
1 parent 65d049e commit 807496a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 22 deletions.
35 changes: 13 additions & 22 deletions src/ProgressMeter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -796,47 +796,38 @@ function showprogressdistributed(args...)
r = loop.args[1].args[2]
body = loop.args[2]

setup = quote
n = length($(esc(r)))
p = Progress(n, $(showprogress_process_args(progressargs)...))
ch = RemoteChannel(() -> Channel{Bool}(n))
end

if na == 1
# would be nice to do this with @sync @distributed but @sync is broken
# https://github.com/JuliaLang/julia/issues/28979
compute = quote
display = @async let i = 0
while i < n
take!(ch)
next!(p)
i += 1
end
end
@distributed for $(esc(var)) = $(esc(r))
waiting = @distributed for $(esc(var)) = $(esc(r))
$(esc(body))
put!(ch, true)
end
wait(waiting)
nothing
end
else
compute = quote
display = @async while take!(ch) next!(p) end
results = @distributed $(esc(reducer)) for $(esc(var)) = $(esc(r))
@distributed $(esc(reducer)) for $(esc(var)) = $(esc(r))
x = $(esc(body))
put!(ch, true)
x
end
put!(ch, false)
results
end
end

quote
$setup
results = $compute
wait(display)
results
let n = length($(esc(r)))
p = Progress(n, $(showprogress_process_args(progressargs)...))
ch = RemoteChannel(() -> Channel{Bool}(n))

@async while take!(ch) next!(p) end
results = $compute
put!(ch, false)
finish!(p)
results
end
end
end

Expand Down
35 changes: 35 additions & 0 deletions test/test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,41 @@ end
println("Testing @showprogress macro on distributed for loop without reducer")
testfunc16(3000, 0.01, 0.001)

function testfunc16cb(N, dt, tsleep)
ProgressMeter.@showprogress dt=dt @distributed for i in N
if rand() < 0.7
sleep(tsleep)
end
200 < i < 400 && continue
i > 1500 && break
i ^ 2
end
end

println("Testing @showprogress macro on distributed for loop with continue")
testfunc16cb(1:1000, 0.01, 0.002)

println("Testing @showprogress macro on distributed for loop with break")
testfunc16cb(1000:2000, 0.01, 0.003)


println("testing `@showprogress @distributed` in global scope")
@showprogress @distributed for i in 1:10
sleep(0.1)
i^2
end

println("testing `@showprogress @distributed (+)` in global scope")
# https://github.com/timholy/ProgressMeter.jl/issues/243
result = @showprogress @distributed (+) for i in 1:10
sleep(0.1)
i^2
end
@test result == sum(abs2, 1:10)




function testfunc17()
n = 30
p = ProgressMeter.Progress(n, start=15)
Expand Down

0 comments on commit 807496a

Please sign in to comment.