@@ -33,6 +33,30 @@ function recoupling_scalartype(::Type{A}, ::Type{Tₛ}) where {A, Tₛ <: Number
3333 return Tₛ <: Real ? real (T) : complex (T)
3434end
3535
36+ """
37+ SubblockGroup{T}
38+
39+ Small subblocks of equal size and strides, which are transformed by a single loop over all of them
40+ rather than by one `tensoradd!` call per subblock. Entry `k` of a subblock lies at `rel_dst[k]`
41+ and `rel_src[k]` relative to the offsets of the destination and source subblock, where the source
42+ strides are permuted such that both refer to the same entry. Term `n` then computes
43+ `dst[offset_dst[n] .+ rel_dst] = α * coeff[n] * src[offset_src[n] .+ rel_src] + b * dst[offset_dst[n] .+ rel_dst]`,
44+ with `b = β` for the first term into every destination subblock and `b = 1` for the groups of
45+ further recoupling terms, which have `accumulate` set and are applied after all others.
46+ The terms are sorted by `offset_dst`.
47+ """
48+ struct SubblockGroup{T}
49+ rel_dst:: Vector{Int}
50+ rel_src:: Vector{Int}
51+ offset_dst:: Vector{Int}
52+ offset_src:: Vector{Int}
53+ coeff:: Vector{T}
54+ accumulate:: Bool
55+ end
56+
57+ # subblocks up to this length are transformed in `SubblockGroup`s
58+ const SUBBLOCKGROUP_MAXLENGTH = 1024
59+
3660# (coefficient, destination position, source position)
3761const UniqueTransformerData{T} = Tuple{T, Int, Int}
3862
@@ -42,12 +66,16 @@ const UniqueTransformerData{T} = Tuple{T, Int, Int}
4266Tree transformation for `UniqueFusion` sectors, where every source subblock maps onto a single
4367destination subblock with a scalar coefficient, stored as `(coeff, idst, isrc)`. The subblock
4468structures of the destination and source spaces are kept alongside, such that the
45- [`StridedSubblocks`](@ref) of both tensors can be created without further lookups.
69+ [`StridedSubblocks`](@ref) of both tensors can be created without further lookups. The small
70+ subblocks are also collected in [`SubblockGroup`](@ref)s, and `large` holds the positions in
71+ `data` of the others.
4672"""
4773struct UniqueTreeTransformer{T, N} <: TreeTransformer
4874 data:: Vector{UniqueTransformerData{T}}
4975 structure_dst:: Vector{StridedStructure{N}}
5076 structure_src:: Vector{StridedStructure{N}}
77+ groups:: Vector{SubblockGroup{T}}
78+ large:: Vector{Int}
5179end
5280
5381# (recoupling matrix, destination positions, source positions)
@@ -60,12 +88,16 @@ Tree transformation for sectors with multiple fusion channels, where the subbloc
6088[`FusionTreeBlock`](@ref) map onto the subblocks of the transformed block through a recoupling
6189matrix, stored as `(U, inds_dst, inds_src)`, where `U[j, i]` maps source `i` onto destination `j`.
6290The subblock structures of the destination and source spaces are kept alongside, such that the
63- [`StridedSubblocks`](@ref) of both tensors can be created without further lookups.
91+ [`StridedSubblocks`](@ref) of both tensors can be created without further lookups. The blocks of
92+ small subblocks are also collected in [`SubblockGroup`](@ref)s, and `large` holds the positions in
93+ `data` of the others.
6494"""
6595struct GenericTreeTransformer{T, N} <: TreeTransformer
6696 data:: Vector{GenericTransformerData{T}}
6797 structure_dst:: Vector{StridedStructure{N}}
6898 structure_src:: Vector{StridedStructure{N}}
99+ groups:: Vector{SubblockGroup{T}}
100+ large:: Vector{Int}
69101end
70102
71103function UniqueTreeTransformer (:: Type{A} , transform, p, Vdst:: HomSpace , Vsrc:: HomSpace , conjsrc:: Bool ) where {A}
@@ -85,7 +117,8 @@ function UniqueTreeTransformer(::Type{A}, transform, p, Vdst::HomSpace, Vsrc::Ho
85117
86118 structure_dst = degeneracystructure (Vdst). subblockstructure
87119 structure_src = degeneracystructure (Vsrc). subblockstructure
88- transformer = UniqueTreeTransformer (data, structure_dst, structure_src)
120+ groups, large = subblockgroups (T, data, structure_dst, structure_src, linearize (p))
121+ transformer = UniqueTreeTransformer (data, structure_dst, structure_src, groups, large)
89122
90123 Δt = Base. time () - t₀
91124 @debug (lazy " Treetransformer for $Vsrc to $Vdst via $p" , conjsrc, nblocks = length (data), Δt)
@@ -137,7 +170,8 @@ function GenericTreeTransformer(::Type{A}, transform, p, Vdst::HomSpace, Vsrc::H
137170
138171 # sort by (approximate) weight to facilitate multi-threading strategies
139172 @timeit_debug GLOBAL_TIMER " bookkeeping: sort" Base. permute! (data, sortperm (weights; rev = true ))
140- transformer = GenericTreeTransformer {T, numind(Vdst)} (data, structure_dst, structure_src)
173+ groups, large = subblockgroups (T, data, structure_dst, structure_src, linearize (p))
174+ transformer = GenericTreeTransformer {T, numind(Vdst)} (data, structure_dst, structure_src, groups, large)
141175
142176 Δt = Base. time () - t₀
143177 @debug (
@@ -151,6 +185,59 @@ function GenericTreeTransformer(::Type{A}, transform, p, Vdst::HomSpace, Vsrc::H
151185 return transformer
152186end
153187
188+ """
189+ subblockgroups(T, data, structure_dst, structure_src, p) -> (groups, large)
190+
191+ Collect the terms of the transformer `data` that act on subblocks with at most
192+ `SUBBLOCKGROUP_MAXLENGTH` entries into [`SubblockGroup`](@ref)s by size and strides, with the
193+ accumulating groups last, and return them together with the positions in `data` of the other entries.
194+ """
195+ function subblockgroups (:: Type{T} , data, structure_dst:: Vector{StridedStructure{N}} , structure_src, p) where {T, N}
196+ terms = Tuple{T, Int, Int, Bool}[]
197+ large = Int[]
198+ for (n, (U, inds_dst, inds_src)) in enumerate (data)
199+ if prod (first (structure_dst[first (inds_dst)])) > SUBBLOCKGROUP_MAXLENGTH
200+ push! (large, n)
201+ else
202+ _push_terms! (terms, U, inds_dst, inds_src)
203+ end
204+ end
205+ sort! (terms; by = term -> structure_dst[term[2 ]][3 ])
206+
207+ # strides of dimensions of size 1 are irrelevant, and set to zero to obtain fewer groups
208+ groups = Dict{Tuple{Bool, NTuple{3 , NTuple{N, Int}}}, SubblockGroup{T}}()
209+ for (coeff, idst, isrc, accumulate) in terms
210+ sz, strides_dst, offset_dst = structure_dst[idst]
211+ _, strides_src, offset_src = structure_src[isrc]
212+ strides_dst = map ((n, s) -> n == 1 ? 0 : s, sz, strides_dst)
213+ strides_src′ = map ((n, s) -> n == 1 ? 0 : s, sz, TupleTools. getindices (strides_src, p))
214+ group = get! (groups, (accumulate, (sz, strides_dst, strides_src′))) do
215+ rel_dst, rel_src = Int[], Int[]
216+ for I in CartesianIndices (sz)
217+ push! (rel_dst, 1 + sum ((Tuple (I) .- 1 ) .* strides_dst; init = 0 ))
218+ push! (rel_src, 1 + sum ((Tuple (I) .- 1 ) .* strides_src′; init = 0 ))
219+ end
220+ return SubblockGroup (rel_dst, rel_src, Int[], Int[], T[], accumulate)
221+ end
222+ push! (group. offset_dst, offset_dst)
223+ push! (group. offset_src, offset_src)
224+ push! (group. coeff, coeff)
225+ end
226+ return sort! (collect (values (groups)); by = group -> group. accumulate), large
227+ end
228+
229+ _push_terms! (terms, coeff:: Number , idst:: Int , isrc:: Int ) = push! (terms, (coeff, idst, isrc, false ))
230+ function _push_terms! (terms, U:: AbstractMatrix , inds_dst, inds_src)
231+ for (i, idst) in enumerate (inds_dst)
232+ js = findall (! iszero, view (U, i, :))
233+ isempty (js) && push! (js, 1 ) # such that β is still applied
234+ for j in js
235+ push! (terms, (U[i, j], idst, inds_src[j], j != first (js)))
236+ end
237+ end
238+ return terms
239+ end
240+
154241"""
155242 buffersize(transformer::TreeTransformer) -> Int
156243
0 commit comments