-
Notifications
You must be signed in to change notification settings - Fork 3
Add timer()
and @trixi_timeit
#25
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
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f42854f
Add `timer()` and `@trixi_timeit`
efaulhaber 2ac2256
Add tests
efaulhaber e4c912d
Add tests for enabling and disabling timings
efaulhaber 5739347
Increment version number
efaulhaber e790d4c
Implement suggestions
efaulhaber c81f5ce
Fix tests
efaulhaber 4207794
Update src/trixi_timeit.jl
efaulhaber 525eaa8
Update src/trixi_timeit.jl
efaulhaber 6dbd20b
Update src/trixi_timeit.jl
efaulhaber e2894f3
Update src/trixi_timeit.jl
efaulhaber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Enable debug timings `@trixi_timeit timer() "name" stuff...`. | ||
# This allows us to disable timings completely by executing | ||
# `TimerOutputs.disable_debug_timings(TrixiBase)` | ||
# and to enable them again by executing | ||
# `TimerOutputs.enable_debug_timings(TrixiBase)` | ||
timeit_debug_enabled() = true | ||
|
||
""" | ||
disable_debug_timings() | ||
|
||
Disable all `@trixi_timeit` and `@timeit_debug` timings. | ||
The timings should be optimized away, allowing for truly zero-overhead. | ||
Enable timings again with [`enable_debug_timings`](@ref). | ||
|
||
See also [`enable_debug_timings`](@ref), [`@trixi_timeit`](@ref). | ||
""" | ||
disable_debug_timings() = TimerOutputs.disable_debug_timings(@__MODULE__) | ||
|
||
""" | ||
enable_debug_timings() | ||
|
||
Enable all `@trixi_timeit` and `@timeit_debug` timings. | ||
efaulhaber marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
See also [`disable_debug_timings`](@ref), [`@trixi_timeit`](@ref). | ||
""" | ||
enable_debug_timings() = TimerOutputs.enable_debug_timings(@__MODULE__) | ||
|
||
# Store main timer for global timing of functions. | ||
# Always call timer() to hide implementation details. | ||
const main_timer = TimerOutput() | ||
sloede marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
""" | ||
timer() | ||
|
||
Main timer for global timing. | ||
efaulhaber marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
timer() = main_timer | ||
|
||
""" | ||
@trixi_timeit timer() "some label" expression | ||
|
||
Basically the same as a special case of `@timeit_debug` from | ||
[TimerOutputs.jl](https://github.com/KristofferC/TimerOutputs.jl), | ||
but without `try ... finally ... end` block. Thus, it's not exception-safe, | ||
but it also avoids some related performance problems. Since we do not use | ||
exception handling in Trixi, that's not really an issue. | ||
efaulhaber marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
All `@trixi_timeit` timings can be disabled with [`disable_debug_timings`](@ref). | ||
The timings should then be optimized away, allowing for truly zero-overhead. | ||
|
||
See also [`disable_debug_timings`](@ref), [`enable_debug_timings`](@ref). | ||
""" | ||
macro trixi_timeit(timer_output, label, expr) | ||
timeit_block = quote | ||
if timeit_debug_enabled() | ||
local to = $(esc(timer_output)) | ||
local enabled = to.enabled | ||
if enabled | ||
local accumulated_data = $(TimerOutputs.push!)(to, $(esc(label))) | ||
end | ||
local b0 = $(TimerOutputs.gc_bytes)() | ||
local t0 = $(TimerOutputs.time_ns)() | ||
end | ||
local val = $(esc(expr)) | ||
if timeit_debug_enabled() && enabled | ||
$(TimerOutputs.do_accumulate!)(accumulated_data, t0, b0) | ||
$(TimerOutputs.pop!)(to) | ||
end | ||
val | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
@testset verbose=true "Timers" begin | ||
@testset verbose=true "`timer()`" begin | ||
@test (@inferred timer()) isa TrixiBase.TimerOutput | ||
|
||
# Test empty timer output | ||
TrixiBase.TimerOutputs.reset_timer!(timer()) | ||
|
||
timer_output = """ | ||
──────────────────────────────────────────────────────────────────── | ||
Time Allocations | ||
─────────────────────── ──────────────────────── | ||
Tot / % measured: 91.5s / 0.0% 5.43MiB / 0.0% | ||
|
||
Section ncalls time %tot avg alloc %tot avg | ||
──────────────────────────────────────────────────────────────────── | ||
──────────────────────────────────────────────────────────────────── | ||
""" | ||
# Remove "Tot / % measured" line and trailing white spaces | ||
expected = replace(timer_output, r"Tot / % measured: .*" => "", | ||
r"\s+\n" => "\n") | ||
actual = replace(repr(timer()) * "\n", r"Tot / % measured: .*" => "", | ||
r"\s+\n" => "\n") | ||
|
||
# Compare against empty timer output | ||
@test actual == expected | ||
end | ||
|
||
@testset verbose=true "`@trixi_timeit`" begin | ||
# Start with empty timer output | ||
TrixiBase.TimerOutputs.reset_timer!(timer()) | ||
|
||
# Add timer entry with 2 calls | ||
@trixi_timeit timer() "test timer" sin(0.0) | ||
@trixi_timeit timer() "test timer" sin(0.0) | ||
|
||
timer_output = """ | ||
─────────────────────────────────────────────────────────────────────── | ||
Time Allocations | ||
─────────────────────── ──────────────────────── | ||
Tot / % measured: 61.4ms / 99.2% 5.60MiB / 99.6% | ||
|
||
Section ncalls time %tot avg alloc %tot avg | ||
─────────────────────────────────────────────────────────────────────── | ||
test timer 2 60.9ms 100.0% 60.9ms 5.57MiB 100.0% 5.57MiB | ||
─────────────────────────────────────────────────────────────────────── | ||
""" | ||
# Remove "Tot / % measured" line and trailing white spaces and replace | ||
# the "test timer" line (but don't remove it, we want to check that it's there). | ||
expected = replace(timer_output, r"Tot / % measured: .*" => "", | ||
r"\s+\n" => "\n", | ||
r"test timer 2 .*B\n" => "test timer 2") | ||
actual = replace(repr(timer()) * "\n", r"Tot / % measured: .*" => "", | ||
r"\s+\n" => "\n", | ||
r"test timer 2 .*B\n" => "test timer 2") | ||
|
||
# Compare against empty timer output | ||
@test actual == expected | ||
end | ||
|
||
@testset verbose=true "disable and enable timings" begin | ||
# Start with empty timer output | ||
TrixiBase.TimerOutputs.reset_timer!(timer()) | ||
|
||
# Disable timings | ||
disable_debug_timings() | ||
|
||
# These two timings should be disabled | ||
@trixi_timeit timer() "test timer" sin(0.0) | ||
@trixi_timeit timer() "test timer" sin(0.0) | ||
|
||
# Disable timings | ||
enable_debug_timings() | ||
|
||
# This timing should be counted | ||
@trixi_timeit timer() "test timer 2" sin(0.0) | ||
|
||
println(timer()) | ||
|
||
timer_output = """ | ||
───────────────────────────────────────────────────────────────────────── | ||
Time Allocations | ||
─────────────────────── ──────────────────────── | ||
Tot / % measured: 23.7ms / 0.0% 1.00MiB / 0.0% | ||
|
||
Section ncalls time %tot avg alloc %tot avg | ||
───────────────────────────────────────────────────────────────────────── | ||
test timer 2 1 875ns 100.0% 875ns 48.0B 100.0% 48.0B | ||
───────────────────────────────────────────────────────────────────────── | ||
""" | ||
# Remove "Tot / % measured" line and trailing white spaces and replace | ||
# the "test timer" line (but don't remove it, we want to check that it's there). | ||
expected = replace(timer_output, r"Tot / % measured: .*" => "", | ||
r"\s+\n" => "\n", | ||
r"test timer 2 1 .*B\n" => "test timer 2 1") | ||
actual = replace(repr(timer()) * "\n", r"Tot / % measured: .*" => "", | ||
r"\s+\n" => "\n", | ||
r"test timer 2 1 .*B\n" => "test timer 2 1") | ||
|
||
# Compare against empty timer output | ||
@test actual == expected | ||
end | ||
end; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.