|
| 1 | +# define the `@timeit_debug` switch explicitly (instead of letting the first macro |
| 2 | +# expansion do it) so that ordinary code can branch on it, see `timers_enabled` |
| 3 | +timeit_debug_enabled() = false |
| 4 | + |
| 5 | +""" |
| 6 | + TensorKit.GLOBAL_TIMER |
| 7 | +
|
| 8 | +The global `TimerOutput` object into which all timer sections of TensorKit accumulate. |
| 9 | +See [`enable_timers!`](@ref) for how to activate them, [`print_timers`](@ref) for |
| 10 | +displaying the resulting call tree and [`timer_summary`](@ref) for aggregating it into |
| 11 | +per-category totals. |
| 12 | +""" |
| 13 | +const GLOBAL_TIMER = TimerOutput("TensorKit") |
| 14 | + |
| 15 | +""" |
| 16 | + TensorKit.timer() -> TimerOutputs.TimerOutput |
| 17 | +
|
| 18 | +Return the global timer object [`GLOBAL_TIMER`](@ref) into which all TensorKit timer |
| 19 | +sections accumulate. |
| 20 | +""" |
| 21 | +timer() = GLOBAL_TIMER |
| 22 | + |
| 23 | +""" |
| 24 | + TensorKit.timers_enabled() -> Bool |
| 25 | +
|
| 26 | +Return whether the `@timeit_debug` timer sections of TensorKit are currently compiled in, |
| 27 | +i.e. whether [`enable_timers!`](@ref) has been called. |
| 28 | +
|
| 29 | +This is used internally to force serial execution of parallel regions while timing, since |
| 30 | +a `TimerOutput` may only be manipulated from a single task. When timers are disabled this |
| 31 | +check const-folds to `false`, so it has no runtime cost. |
| 32 | +""" |
| 33 | +timers_enabled() = timeit_debug_enabled() |
| 34 | + |
| 35 | +""" |
| 36 | + TensorKit.enable_timers!() |
| 37 | +
|
| 38 | +Enable all timer sections of TensorKit (including its submodules), which accumulate |
| 39 | +timings of the internal kernels into [`GLOBAL_TIMER`](@ref). Undone by |
| 40 | +[`disable_timers!`](@ref). |
| 41 | +
|
| 42 | +!!! warning |
| 43 | + Enabling or disabling timers redefines internal functions and therefore triggers |
| 44 | + recompilation of the instrumented methods on first use. This is a debug-session |
| 45 | + operation, not a runtime switch. |
| 46 | +
|
| 47 | +!!! warning |
| 48 | + While timers are enabled, TensorKit-internal task parallelism is disabled (threaded |
| 49 | + regions run serially), so multi-threaded speedups are not measurable. Additionally, |
| 50 | + TensorKit functions should not be called concurrently from multiple user tasks while |
| 51 | + timing, as the timer object is not thread-safe. |
| 52 | +
|
| 53 | +Note that each timer section adds an overhead of roughly 100-200 ns per entry, which can |
| 54 | +distort measurements of very small workloads. For GPU tensors, timings only reflect |
| 55 | +host-side dispatch of asynchronous kernels unless the workload is explicitly synchronized. |
| 56 | +""" |
| 57 | +function enable_timers!() |
| 58 | + TimerOutputs.enable_debug_timings(TensorKit) |
| 59 | + return nothing |
| 60 | +end |
| 61 | + |
| 62 | +""" |
| 63 | + TensorKit.disable_timers!() |
| 64 | +
|
| 65 | +Disable all timer sections of TensorKit again; the inverse of [`enable_timers!`](@ref). |
| 66 | +Also triggers recompilation of the instrumented methods on first use. |
| 67 | +""" |
| 68 | +function disable_timers!() |
| 69 | + TimerOutputs.disable_debug_timings(TensorKit) |
| 70 | + return nothing |
| 71 | +end |
| 72 | + |
| 73 | +""" |
| 74 | + TensorKit.reset_timers!() |
| 75 | +
|
| 76 | +Reset the accumulated timings in [`GLOBAL_TIMER`](@ref). |
| 77 | +""" |
| 78 | +function reset_timers!() |
| 79 | + TimerOutputs.reset_timer!(GLOBAL_TIMER) |
| 80 | + return nothing |
| 81 | +end |
| 82 | + |
| 83 | +""" |
| 84 | + TensorKit.print_timers(io::IO = stdout; kwargs...) |
| 85 | +
|
| 86 | +Print the accumulated timings in [`GLOBAL_TIMER`](@ref) as a nested table. Keyword |
| 87 | +arguments are forwarded to `TimerOutputs.print_timer`. |
| 88 | +""" |
| 89 | +print_timers(io::IO = stdout; kwargs...) = TimerOutputs.print_timer(io, GLOBAL_TIMER; kwargs...) |
| 90 | + |
| 91 | +const TIMER_CATEGORIES = (:symmetry, :bookkeeping, :alloc, :dense, :other) |
| 92 | + |
| 93 | +# "symmetry: recoupling" -> :symmetry; unprefixed or unknown prefix -> nothing |
| 94 | +function _timer_category(label::String) |
| 95 | + i = findfirst(':', label) |
| 96 | + i === nothing && return nothing |
| 97 | + prefix = Symbol(label[1:prevind(label, i)]) |
| 98 | + return prefix in TIMER_CATEGORIES ? prefix : nothing |
| 99 | +end |
| 100 | + |
| 101 | +# category of the miss-path (construction) section of an `@cached` function |
| 102 | +function _cached_category(fname::Symbol) |
| 103 | + return fname in (:fsbraid, :fstranspose, :treebraider, :treetransposer) ? |
| 104 | + "symmetry" : "bookkeeping" |
| 105 | +end |
| 106 | + |
| 107 | +const TimerSummary = Dict{Symbol, @NamedTuple{time::Int64, allocated::Int64, ncalls::Int64}} |
| 108 | + |
| 109 | +""" |
| 110 | + TensorKit.timer_summary([io::IO]; to = GLOBAL_TIMER) |
| 111 | + -> Dict{Symbol, @NamedTuple{time::Int64, allocated::Int64, ncalls::Int64}} |
| 112 | +
|
| 113 | +Aggregate the timer tree into per-category totals (time in ns, allocated bytes, number of |
| 114 | +section entries) for the categories `$(TIMER_CATEGORIES)`. |
| 115 | +
|
| 116 | +Each section's *exclusive* time (its own time minus that of its timed children) is |
| 117 | +attributed to the category given by its label prefix or, for unprefixed labels, to the |
| 118 | +category of the nearest categorized ancestor (`:other` at the root). Every nanosecond is |
| 119 | +thus counted exactly once and the totals sum to the total measured time. |
| 120 | +
|
| 121 | +When `io` is given (default `stdout`), a small table is printed; pass `nothing` to skip |
| 122 | +printing and only return the totals. |
| 123 | +""" |
| 124 | +function timer_summary(io::Union{IO, Nothing} = stdout; to::TimerOutput = GLOBAL_TIMER) |
| 125 | + totals = TimerSummary(c => (time = 0, allocated = 0, ncalls = 0) for c in TIMER_CATEGORIES) |
| 126 | + for child in to.root.children |
| 127 | + _accumulate_summary!(totals, child, :other) |
| 128 | + end |
| 129 | + io === nothing || _print_timer_summary(io, totals) |
| 130 | + return totals |
| 131 | +end |
| 132 | + |
| 133 | +function _accumulate_summary!(totals::TimerSummary, s, inherited::Symbol) |
| 134 | + cat = something(_timer_category(s.name), inherited) |
| 135 | + t = TimerOutputs.time(s) |
| 136 | + b = TimerOutputs.allocated(s) |
| 137 | + for child in s.children |
| 138 | + t -= TimerOutputs.time(child) |
| 139 | + b -= TimerOutputs.allocated(child) |
| 140 | + _accumulate_summary!(totals, child, cat) |
| 141 | + end |
| 142 | + old = totals[cat] |
| 143 | + totals[cat] = ( |
| 144 | + time = old.time + max(t, 0), allocated = old.allocated + max(b, 0), |
| 145 | + ncalls = old.ncalls + TimerOutputs.ncalls(s), |
| 146 | + ) |
| 147 | + return nothing |
| 148 | +end |
| 149 | + |
| 150 | +function _print_timer_summary(io::IO, totals::TimerSummary) |
| 151 | + total_time = sum(x -> x.time, values(totals)) |
| 152 | + println(io, "TensorKit timer summary:") |
| 153 | + for cat in TIMER_CATEGORIES |
| 154 | + (; time, allocated, ncalls) = totals[cat] |
| 155 | + percentage = total_time == 0 ? 0.0 : 100 * time / total_time |
| 156 | + @printf( |
| 157 | + io, "%12s: %s (%5.1f%%) %s %d sections\n", |
| 158 | + cat, TimerOutputs.prettytime(time), percentage, |
| 159 | + TimerOutputs.prettymemory(allocated), ncalls |
| 160 | + ) |
| 161 | + end |
| 162 | + return nothing |
| 163 | +end |
0 commit comments