From ee295722bac6d72377f1fdc19f450cf8571e5aea Mon Sep 17 00:00:00 2001 From: leburgel Date: Sun, 22 Nov 2026 10:59:46 +0100 Subject: [PATCH] Transform the fusion tree pairs of a UniqueTreeTransformer on several tasks The pairs are split into one equal range per task, on up to get_num_manipulation_threads() tasks with taskforeach, as the fusion tree blocks of a GenericTreeTransformer already are. Every task gets at least 1024 pairs, so small transformers stay serial. Co-Authored-By: Claude Opus 5.5 --- docs/src/Changelog.md | 1 + src/tensors/treetransformers.jl | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/docs/src/Changelog.md b/docs/src/Changelog.md index 152b50d33..c49aead0f 100644 --- a/docs/src/Changelog.md +++ b/docs/src/Changelog.md @@ -49,6 +49,7 @@ When releasing a new version, move the "Unreleased" changes to a new version sec - `taskforeach` with a single worker is a plain loop without an atomic counter, which lowers the cost per subblock of single-threaded index manipulations and planar traces - Tree transformers for sectors with multiple fusion channels share equal recoupling matrices, which only depend on the sectors, instead of storing one copy per transformer - The cached braids and transposes of fusion tree blocks keep their recoupling matrices in the scalar type of the tree transformers that use them, instead of a copy in that type for every transformer +- The fusion tree pairs of a `UniqueTreeTransformer` are transformed on up to `get_num_manipulation_threads()` tasks, as the fusion tree blocks of a `GenericTreeTransformer` already were, which speeds up the construction of the index manipulations of abelian tensors with many fusion tree pairs ## [0.17.3](https://github.com/QuantumKitHub/TensorKit.jl/compare/v0.17.2...v0.17.3) - 2026-10-30 diff --git a/src/tensors/treetransformers.jl b/src/tensors/treetransformers.jl index 37d9df181..967007835 100644 --- a/src/tensors/treetransformers.jl +++ b/src/tensors/treetransformers.jl @@ -37,6 +37,9 @@ end # (coefficient, destination position, source position) const UniqueTransformerData{T} = Tuple{T, Int, Int} +# the minimal number of fusion tree pairs per task of `UniqueTreeTransformer` +const UNIQUE_TRANSFORM_MINPAIRS = 1024 + """ UniqueTreeTransformer{T, N} <: TreeTransformer @@ -97,10 +100,18 @@ function UniqueTreeTransformer(::Type{A}, transform, p, Vdst::HomSpace, Vsrc::Ho T = recoupling_scalartype(A, sectorscalartype(sectortype(Vdst))) data = Vector{UniqueTransformerData{T}}(undef, length(src_trees)) - @timeit_debug GLOBAL_TIMER "symmetry: tree transform" for (isrc, (f₁, f₂)) in enumerate(src_trees) - f_dst, coeff = transform(conjsrc ? (f₂, f₁) : (f₁, f₂)) - _, (_, idst) = gettoken(dst_trees, f_dst) - data[isrc] = (convert(T, coeff), idst, isrc) + # the pairs are split into one equal range per task, with enough pairs in every range + ntasks = clamp(length(data) ÷ UNIQUE_TRANSFORM_MINPAIRS, 1, get_num_manipulation_threads()) + chunks = Iterators.partition(eachindex(data), max(cld(length(data), ntasks), 1)) + @timeit_debug GLOBAL_TIMER "symmetry: tree transform" begin + taskforeach(chunks, ntasks) do isrcs + for isrc in isrcs + f₁, f₂ = gettokenvalue(src_trees, isrc) + f_dst, coeff = transform(conjsrc ? (f₂, f₁) : (f₁, f₂)) + _, (_, idst) = gettoken(dst_trees, f_dst) + data[isrc] = (convert(T, coeff), idst, isrc) + end + end end structure_dst = degeneracystructure(Vdst).subblockstructure