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
Show file tree
Hide file tree
Changes from 2 commits
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 @@
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 @@
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)

Check warning on line 40 in src/callbacks_step/summary.jl

View check run for this annotation

Codecov / codecov/patch

src/callbacks_step/summary.jl#L40

Added line #L40 was not covered by tests

function (summary_callback::SummaryCallback)()
io = summary_callback.io
function finalize_summary_callback(cb::DiscreteCallback, u, t, integrator)
io = cb.affect!.io

Check warning on line 43 in src/callbacks_step/summary.jl

View check run for this annotation

Codecov / codecov/patch

src/callbacks_step/summary.jl#L42-L43

Added lines #L42 - L43 were not covered by tests
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
1 change: 0 additions & 1 deletion test/test_unit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,6 @@ using SparseArrays: sparse, SparseMatrixCSC
summary_callback = SummaryCallback()
@test_nowarn print(summary_callback)
@test_nowarn display(summary_callback)
@test_nowarn summary_callback()
end

@testset "util" begin
Expand Down