ProgressLogging.jl icon indicating copy to clipboard operation
ProgressLogging.jl copied to clipboard

Incremental progress logging

Open pfitzseb opened this issue 3 years ago • 1 comments

Would be nice to support logging progress increments, so that e.g. something like

@withprogress name="threaded iteration" begin
    Threads.@threads for i in 1:12
        sleep(2)
        @logprogress increment = 1/12
    end
end

works.

pfitzseb avatar Sep 03 '21 12:09 pfitzseb

Here's what I do to make this work:

mutable struct Prog
    N::Int
    @atomic count::Int
end
Prog(N) = Prog(N, 0)
inc!(p::Prog) = @atomic p.count += 1
macro inc(p)
    quote
        local M = $inc!($p) # It seems to be important to separate this out from the `@logprogress` step since `@logprogress` can double-evaluate
	$ProgressLogging.@logprogress M / $p.N
    end |> esc
end

and then

using OhMyThreads: tforeach
p = Prog(N)
@withprogress tforeach(1:N) do i
    sleep(2)
    @inc p
end

MasonProtter avatar Feb 02 '24 13:02 MasonProtter