Skip to content

Commit ec902eb

Browse files
lkdvosclaude
andcommitted
address review comments
- drop the redundant `permute!` section and label `braid!` as `permute!/braid!` - time the trivial-sector fast paths in `braid!`/`transpose!` as `dense: tensoradd` - rename `dense: lapack` to `dense: MatrixAlgebraKit` - move the `dense: trace` section inside the loop in `planartrace!` so the untimed `planar_trace` coefficients are not attributed to `dense` - move `_cached_category` next to the `@cached` macro; clarify the `timeit_debug_enabled` comment and `timers_enabled` docstring Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent f6e4e81 commit ec902eb

7 files changed

Lines changed: 31 additions & 24 deletions

File tree

‎docs/src/man/profiling.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ TensorKit.timer_summary() # symmetry / bookkeeping / alloc / dense / other
3333
TensorKit.disable_timers!()
3434
```
3535

36-
[`TensorKit.print_timers`](@ref) displays the accumulated timings as a nested call tree, with sections for the top-level operations (`"permute!"`, `"contract!"`, `"svd_compact!"`, ...) and nested sections labeled by their category prefix (`"symmetry: ..."`, `"bookkeeping: ..."`, `"alloc: ..."`, `"dense: ..."`).
36+
[`TensorKit.print_timers`](@ref) displays the accumulated timings as a nested call tree, with sections for the top-level operations (`"permute!/braid!"`, `"contract!"`, `"svd_compact!"`, ...) and nested sections labeled by their category prefix (`"symmetry: ..."`, `"bookkeeping: ..."`, `"alloc: ..."`, `"dense: ..."`).
3737
[`TensorKit.timer_summary`](@ref) aggregates the *exclusive* time of each section (its own time minus that of its timed children) into per-category totals, such that every nanosecond is counted exactly once and the totals sum to the total measured time.
3838

3939
A few caveats to keep in mind:

‎src/auxiliary/caches.jl‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ function CacheStyle(args...)
4848
return GlobalLRUCache()
4949
end
5050

51+
# category of the miss-path (construction) timer section of an `@cached` function
52+
function _cached_category(fname::Symbol)
53+
return fname in (:fsbraid, :fstranspose, :treebraider, :treetransposer) ?
54+
"symmetry" : "bookkeeping"
55+
end
56+
5157
macro cached(ex)
5258
Meta.isexpr(ex, :function) ||
5359
error("cached macro can only be used on function definitions")

‎src/auxiliary/timers.jl‎

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
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`
1+
# This is the switch that every `@timeit_debug` section in this module checks, and that
2+
# `TimerOutputs.enable_debug_timings(TensorKit)` redefines to `true` (recompiling all
3+
# instrumented methods). TimerOutputs would define it automatically on the first
4+
# `@timeit_debug` expansion; defining it explicitly here lets ordinary code branch on it
5+
# as well, see `timers_enabled`.
36
timeit_debug_enabled() = false
47

58
"""
@@ -26,9 +29,11 @@ timer() = GLOBAL_TIMER
2629
Return whether the `@timeit_debug` timer sections of TensorKit are currently compiled in,
2730
i.e. whether [`enable_timers!`](@ref) has been called.
2831
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+
This is a documented alias for `timeit_debug_enabled`, the switch that is redefined by
33+
`TimerOutputs.enable_debug_timings`. It is used internally to force serial execution of
34+
parallel regions while timing, since a `TimerOutput` may only be manipulated from a single
35+
task. When timers are disabled this check const-folds to `false`, so it has no runtime
36+
cost.
3237
"""
3338
timers_enabled() = timeit_debug_enabled()
3439

@@ -98,12 +103,6 @@ function _timer_category(label::String)
98103
return prefix in TIMER_CATEGORIES ? prefix : nothing
99104
end
100105

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-
107106
const TimerSummary = Dict{Symbol, @NamedTuple{time::Int64, allocated::Int64, ncalls::Int64}}
108107

109108
"""

‎src/factorizations/matrixalgebrakit.jl‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ for f! in (
4242
$(f! in (:eig_full!, :eigh_full!) && :(LinearAlgebra.checksquare(t)))
4343
@timeit_debug GLOBAL_TIMER $(string(f!)) begin
4444
foreachblock(t, F...) do _, (tblock, Fblocks...)
45-
@timeit_debug GLOBAL_TIMER "dense: lapack" begin
45+
@timeit_debug GLOBAL_TIMER "dense: MatrixAlgebraKit" begin
4646
Fblocks′ = $f!(tblock, Fblocks, alg)
4747
# deal with the case where the output is not in-place
4848
for (b′, b) in zip(Fblocks′, Fblocks)
@@ -67,7 +67,7 @@ for f! in (
6767
$(f! in (:eig_vals!, :eigh_vals!, :project_hermitian!, :project_antihermitian!, :exponential!) && :(LinearAlgebra.checksquare(t)))
6868
@timeit_debug GLOBAL_TIMER $(string(f!)) begin
6969
foreachblock(t, N) do _, (tblock, Nblock)
70-
@timeit_debug GLOBAL_TIMER "dense: lapack" begin
70+
@timeit_debug GLOBAL_TIMER "dense: MatrixAlgebraKit" begin
7171
Nblock′ = $f!(tblock, Nblock, alg)
7272
# deal with the case where the output is not the same as the input
7373
Nblock === Nblock′ || copy!(Nblock, Nblock′)
@@ -84,7 +84,7 @@ function MAK.exponential!((τ, t)::Tuple{E, T}, N, alg::AbstractAlgorithm) where
8484
LinearAlgebra.checksquare(t)
8585
@timeit_debug GLOBAL_TIMER "exponential!" begin
8686
foreachblock(t, N) do _, (tblock, Nblock)
87-
@timeit_debug GLOBAL_TIMER "dense: lapack" begin
87+
@timeit_debug GLOBAL_TIMER "dense: MatrixAlgebraKit" begin
8888
Nblock′ = exponential!((τ, tblock), Nblock, alg)
8989
# deal with the case where the output is not the same as the input
9090
Nblock === Nblock′ || copy!(Nblock, Nblock′)

‎src/planar/planaroperations.jl‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ function planartrace!(
9898
rmul!(C, β)
9999
end
100100
β′ = One()
101-
@timeit_debug GLOBAL_TIMER "dense: trace" for (f₁, f₂) in fusiontrees(A)
101+
for (f₁, f₂) in fusiontrees(A)
102102
for ((f₁′, f₂′), coeff) in planar_trace((f₁, f₂), (p₁, p₂), (q₁, q₂))
103-
TO.tensortrace!(
103+
@timeit_debug GLOBAL_TIMER "dense: trace" TO.tensortrace!(
104104
C[f₁′, f₂′],
105105
A[f₁, f₂], (p₁, p₂), (q₁, q₂), false,
106106
α * coeff, β′,

‎src/tensors/indexmanipulations.jl‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,7 @@ See also [`permute`](@ref) for creating a new tensor.
221221
)
222222
@boundscheck spacecheck_transform(permute, tdst, tsrc, p)
223223
levels = ntuple(identity, numind(tsrc))
224-
return @timeit_debug GLOBAL_TIMER "permute!" @inbounds braid!(
225-
tdst, tsrc, p, levels, α, β, backend, allocator
226-
)
224+
return @inbounds braid!(tdst, tsrc, p, levels, α, β, backend, allocator)
227225
end
228226

229227
"""
@@ -309,9 +307,11 @@ See also [`braid`](@ref) for creating a new tensor.
309307
backend::AbstractBackend = TO.DefaultBackend(), allocator = TO.DefaultAllocator()
310308
)
311309
@boundscheck spacecheck_transform(braid, tdst, tsrc, p, levels)
312-
@timeit_debug GLOBAL_TIMER "braid!" begin
310+
@timeit_debug GLOBAL_TIMER "permute!/braid!" begin
313311
if has_array_view(tdst) && has_array_view(tsrc)
314-
TO.tensoradd!(tdst[], tsrc[], p, false, α, β, backend, allocator)
312+
@timeit_debug GLOBAL_TIMER "dense: tensoradd" TO.tensoradd!(
313+
tdst[], tsrc[], p, false, α, β, backend, allocator
314+
)
315315
return tdst
316316
end
317317
levels1 = TupleTools.getindices(levels, codomainind(tsrc))
@@ -389,7 +389,9 @@ end
389389
@boundscheck spacecheck_transform(transpose, tdst, tsrc, p)
390390
@timeit_debug GLOBAL_TIMER "transpose!" begin
391391
if has_array_view(tdst) && has_array_view(tsrc)
392-
TO.tensoradd!(tdst[], tsrc[], p, false, α, β, backend, allocator)
392+
@timeit_debug GLOBAL_TIMER "dense: tensoradd" TO.tensoradd!(
393+
tdst[], tsrc[], p, false, α, β, backend, allocator
394+
)
393395
return tdst
394396
end
395397
transformer = treetransposer(tdst, tsrc, p)

‎test/other/timers.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ end
6060
svd_compact(t)
6161

6262
names = [child.name for child in TensorKit.timer().root.children]
63-
@test "permute!" in names
63+
@test "permute!/braid!" in names
6464
@test "contract!" in names
6565
# also verifies that `enable_timers!` reached the Factorizations submodule
6666
@test "svd_compact!" in names

0 commit comments

Comments
 (0)