Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print timer of SummaryCallback in finalizer #79

Merged
merged 5 commits into from
Dec 19, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions src/callbacks_step/summary.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
"""
SummaryCallback(io::IO = stdout)

Create and return a callback that prints a human-readable summary of the simulation setup at the
beginning of a simulation and then resets the timer. When the returned callback is executed
directly, the current timer values are shown.
Create and return a callback that resets the timer at the beginning of
a simulation and prints the timer values at the end of the simulation.
"""
struct SummaryCallback
io::IO
Expand All @@ -12,12 +11,17 @@ struct SummaryCallback
function initialize(cb, u, t, integrator)
initialize_summary_callback(cb, u, t, integrator)
end
# At the end of the simulation, the timer is printed
function finalize(cb, u, t, integrator)
finalize_summary_callback(cb, u, t, integrator)
end
summary_callback = new(io)
# SummaryCallback is called at end of simulation
condition = (u, t, integrator) -> isfinished(integrator)
# SummaryCallback is never called during the simulation
condition = (u, t, integrator) -> false
DiscreteCallback(condition, summary_callback,
save_positions = (false, false),
initialize = initialize)
initialize = initialize,
finalize = finalize)
end
end

Expand All @@ -32,22 +36,14 @@ function initialize_summary_callback(cb::DiscreteCallback, u, t, integrator)
return nothing
end

function (cb::SummaryCallback)(integrator)
u_modified!(integrator, false)
cb()
end
# the summary callback does nothing when called accidentally
(cb::SummaryCallback)(integrator) = u_modified!(integrator, false)

function (summary_callback::SummaryCallback)()
io = summary_callback.io
function finalize_summary_callback(cb::DiscreteCallback, u, t, integrator)
io = cb.affect!.io
TimerOutputs.complement!(timer())
print_timer(io, timer(), title = "DispersiveSWE",
allocations = true, linechars = :unicode, compact = false)
println(io)
return nothing
end

# Allow calling the callback explicitly
function (cb::DiscreteCallback{Condition, Affect!})() where {Condition,
Affect! <: SummaryCallback}
cb.affect!()
end
Loading