Skip to content

Commit c6f9e78

Browse files
committed
Refactor logging, throttle per-chain updates
1 parent cefafb0 commit c6f9e78

File tree

2 files changed

+91
-107
lines changed

2 files changed

+91
-107
lines changed

src/logging.jl

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,19 @@ Create a new logger for progress logging.
1414
struct CreateNewProgressBar{S<:AbstractString} <: AbstractProgressKwarg
1515
name::S
1616
uuid::UUIDs.UUID
17-
1817
function CreateNewProgressBar(name::AbstractString)
19-
return new{typeof{name}}(name, UUIDs.uuid4())
18+
return new{typeof(name)}(name, UUIDs.uuid4())
2019
end
2120
end
2221
function init_progress(p::CreateNewProgressBar)
23-
if hasprogresslevel(Logging.current_logger())
24-
ProgressLogging.@withprogress $(exprs...)
25-
else
26-
$with_progresslogger($Base.@__MODULE__, $Logging.current_logger()) do
27-
$ProgressLogging.@withprogress $(exprs...)
28-
end
29-
end
3022
ProgressLogging.@logprogress p.name nothing _id = p.uuid
3123
end
32-
function update_progress(p::CreateNewProgressBar, progress_frac, ::Bool)
24+
function update_progress(p::CreateNewProgressBar, progress_frac)
3325
ProgressLogging.@logprogress p.name progress_frac _id = p.uuid
3426
end
35-
finish_progress(::CreateNewProgressBar) = ProgressLogging.@logprogress "done"
27+
function finish_progress(p::CreateNewProgressBar)
28+
ProgressLogging.@logprogress p.name "done" _id = p.uuid
29+
end
3630

3731
"""
3832
NoLogging
@@ -41,7 +35,7 @@ Do not log progress at all.
4135
"""
4236
struct NoLogging <: AbstractProgressKwarg end
4337
init_progress(::NoLogging) = nothing
44-
update_progress(::NoLogging, ::Any, ::Bool) = nothing
38+
update_progress(::NoLogging, ::Any) = nothing
4539
finish_progress(::NoLogging) = nothing
4640

4741
"""
@@ -57,8 +51,21 @@ struct ExistingProgressBar{S<:AbstractString} <: AbstractProgressKwarg
5751
name::S
5852
uuid::UUIDs.UUID
5953
end
60-
init_progress(::ExistingProgressBar) = nothing
61-
function update_progress(p::ExistingProgressBar, progress_frac, ::Bool)
54+
function init_progress(p::ExistingProgressBar)
55+
# Hacky code to reset the start timer if called from a multi-chain sampling
56+
# process. We need this because the progress bar is constructed in the
57+
# multi-chain method, i.e. if we don't do this the progress bar shows the
58+
# time elapsed since _all_ sampling began, not since the current chain
59+
# started.
60+
try
61+
bartrees = Logging.current_logger().loggers[1].logger.bartrees
62+
bar = TerminalLoggers.findbar(bartrees, p.uuid).data
63+
bar.tfirst = time()
64+
catch
65+
end
66+
ProgressLogging.@logprogress p.name nothing _id = p.uuid
67+
end
68+
function update_progress(p::ExistingProgressBar, progress_frac)
6269
ProgressLogging.@logprogress p.name progress_frac _id = p.uuid
6370
end
6471
function finish_progress(p::ExistingProgressBar)
@@ -71,39 +78,32 @@ end
7178
Use a `Channel` to log progress. This is used for 'reporting' progress back
7279
to the main thread or worker when using `progress=:overall` with MCMCThreads or
7380
MCMCDistributed.
81+
82+
n_updates is the number of updates that each child thread is expected to report
83+
back to the main thread.
7484
"""
7585
struct ChannelProgress{T<:Union{Channel{Bool},Distributed.RemoteChannel{Channel{Bool}}}} <:
7686
AbstractProgressKwarg
7787
channel::T
88+
n_updates::Int
7889
end
7990
init_progress(::ChannelProgress) = nothing
80-
function update_progress(p::ChannelProgress, ::Any, update_channel::Bool)
81-
return update_channel && put!(p.channel, true)
82-
end
91+
update_progress(p::ChannelProgress, ::Any) = put!(p.channel, true)
8392
# Note: We don't want to `put!(p.channel, false)`, because that would stop the
8493
# channel from being used for further updates e.g. from other chains.
8594
finish_progress(::ChannelProgress) = nothing
8695

87-
# avoid creating a progress bar with @withprogress if progress logging is disabled
88-
# and add a custom progress logger if the current logger does not seem to be able to handle
89-
# progress logs
90-
macro ifwithprogresslogger(cond, exprs...)
96+
# Add a custom progress logger if the current logger does not seem to be able to handle
97+
# progress logs.
98+
macro withprogresslogger(expr)
9199
return esc(
92100
quote
93-
if $cond
94-
# Create a new logger
95-
if $hasprogresslevel($Logging.current_logger())
96-
$ProgressLogging.@withprogress $(exprs...)
97-
else
98-
$with_progresslogger($Base.@__MODULE__, $Logging.current_logger()) do
99-
$ProgressLogging.@withprogress $(exprs...)
100-
end
101+
if !($hasprogresslevel($Logging.current_logger()))
102+
$with_progresslogger($Base.@__MODULE__, $Logging.current_logger()) do
103+
$(expr)
101104
end
102105
else
103-
# Don't create a new logger, either because progress logging
104-
# was disabled, or because it's otherwise being manually
105-
# managed.
106-
$(exprs[end])
106+
$(expr)
107107
end
108108
end,
109109
)

0 commit comments

Comments
 (0)