Skip to content

Commit 543953d

Browse files
committed
clean cache separation
1 parent 0fac8ac commit 543953d

9 files changed

Lines changed: 135 additions & 107 deletions

File tree

‎docs/src/lib/tensors.md‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ In `TensorMap` instances, all data is gathered in a single `AbstractVector`, whi
9797

9898
To obtain information about the structure of the data, you can use:
9999
```@docs
100-
fusionblockstructure(::AbstractTensorMap)
101100
dim(::AbstractTensorMap)
102101
blocksectors(::AbstractTensorMap)
103102
hasblock(::AbstractTensorMap, ::Sector)

‎ext/TensorKitMooncakeExt/utility.jl‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ end
6262
Mooncake.tangent_type(::Type{<:VectorSpace}) = Mooncake.NoTangent
6363
Mooncake.tangent_type(::Type{<:HomSpace}) = Mooncake.NoTangent
6464

65-
@zero_derivative DefaultCtx Tuple{typeof(TensorKit.fusionblockstructure), Any}
65+
@zero_derivative DefaultCtx Tuple{typeof(TensorKit.sectorstructure), Any}
66+
@zero_derivative DefaultCtx Tuple{typeof(TensorKit.degeneracystructure), Any}
6667

6768
@zero_derivative DefaultCtx Tuple{typeof(TensorKit.select), HomSpace, Index2Tuple}
6869
@zero_derivative DefaultCtx Tuple{typeof(TensorKit.flip), HomSpace, Any}

‎src/spaces/homspace.jl‎

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,16 @@ function Base.show(io::IO, W::HomSpace)
6969
)
7070
end
7171

72-
"""
73-
blocksectors(W::HomSpace)
72+
@doc """
73+
blocksectors(W::HomSpace) -> Indices{I}
7474
75-
Return an iterator over the different unique coupled sector labels, i.e. the intersection
76-
of the different fusion outputs that can be obtained by fusing the sectors present in the
77-
domain, as well as from the codomain.
75+
Return an `Indices` of all coupled sectors for `W`. The result is cached based on the
76+
sector structure of `W` (ignoring degeneracy dimensions).
7877
79-
See also [`hasblock`](@ref).
80-
"""
81-
function blocksectors(W::HomSpace)
78+
See also [`hasblock`](@ref), [`blockstructure`](@ref).
79+
""" blocksectors(::HomSpace)
80+
81+
function _blocksectors(W::HomSpace)
8282
sectortype(W) === Trivial &&
8383
return OneOrNoneIterator(dim(domain(W)) != 0 && dim(codomain(W)) != 0, Trivial())
8484

@@ -109,23 +109,15 @@ See also [`blocksectors`](@ref).
109109
"""
110110
hasblock(W::HomSpace, c::Sector) = hasblock(codomain(W), c) && hasblock(domain(W), c)
111111

112-
"""
113-
dim(W::HomSpace)
112+
@doc """
113+
dim(W::HomSpace) -> Int
114114
115115
Return the total dimension of a `HomSpace`, i.e. the number of linearly independent
116116
morphisms that can be constructed within this space.
117-
"""
118-
function dim(W::HomSpace)
119-
d = 0
120-
for c in blocksectors(W)
121-
d += blockdim(codomain(W), c) * blockdim(domain(W), c)
122-
end
123-
return d
124-
end
117+
""" dim(::HomSpace)
125118

126119
dims(W::HomSpace) = (dims(codomain(W))..., dims(domain(W))...)
127120

128-
129121
# Operations on HomSpaces
130122
# -----------------------
131123
"""

‎src/tensors/abstracttensor.jl‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -321,21 +321,13 @@ numind(t::AbstractTensorMap) = numind(typeof(t))
321321

322322
# tensor characteristics: data structure and properties
323323
#------------------------------------------------------
324-
"""
325-
fusionblockstructure(t::AbstractTensorMap) -> TensorStructure
326-
327-
Return the necessary structure information to decompose a tensor in blocks labeled by
328-
coupled sectors and in subblocks labeled by a splitting-fusion tree couple.
329-
"""
330-
fusionblockstructure(t::AbstractTensorMap) = fusionblockstructure(space(t))
331-
332324
"""
333325
dim(t::AbstractTensorMap) -> Int
334326
335327
The total number of free parameters of a tensor, discounting the entries that are fixed by
336328
symmetry. This is also the dimension of the `HomSpace` on which the `TensorMap` is defined.
337329
"""
338-
dim(t::AbstractTensorMap) = fusionblockstructure(t).totaldim
330+
dim(t::AbstractTensorMap) = dim(space(t))
339331

340332
dims(t::AbstractTensorMap) = dims(space(t))
341333

@@ -344,7 +336,7 @@ dims(t::AbstractTensorMap) = dims(space(t))
344336
345337
Return an iterator over all coupled sectors of a tensor.
346338
"""
347-
blocksectors(t::AbstractTensorMap) = keys(fusionblockstructure(t).blockstructure)
339+
blocksectors(t::AbstractTensorMap) = blocksectors(space(t))
348340

349341
"""
350342
hasblock(t::AbstractTensorMap, c::Sector) -> Bool

‎src/tensors/braidingtensor.jl‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,7 @@ function block(b::BraidingTensor, s::Sector)
143143
return data
144144
end
145145

146-
structure = fusionblockstructure(b)
147-
base_offset = first(structure.blockstructure[s][2]) - 1
146+
base_offset = first(blockstructure(b)[s][2]) - 1
148147

149148
for ((f1, f2), (sz, str, off)) in pairs(fusiontreestructure(space(b)))
150149
if (f1.uncoupled != reverse(f2.uncoupled)) || !(f1.coupled == f2.coupled == s)

‎src/tensors/tensor.jl‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ struct TensorMap{T, S <: IndexSpace, N₁, N₂, A <: DenseVector{T}} <: Abstrac
1515
function TensorMap{T, S, N₁, N₂, A}(
1616
::UndefInitializer, space::TensorMapSpace{S, N₁, N₂}
1717
) where {T, S <: IndexSpace, N₁, N₂, A <: DenseVector{T}}
18-
d = fusionblockstructure(space).totaldim
19-
data = A(undef, d)
18+
data = A(undef, dim(space))
2019
if !isbitstype(T)
2120
zerovector!(data)
2221
end
@@ -31,8 +30,7 @@ struct TensorMap{T, S <: IndexSpace, N₁, N₂, A <: DenseVector{T}} <: Abstrac
3130
I = sectortype(S)
3231
T <: Real && !(sectorscalartype(I) <: Real) &&
3332
@warn("Tensors with real data might be incompatible with sector type $I", maxlog = 1)
34-
d = fusionblockstructure(space).totaldim
35-
length(data) == d || throw(DimensionMismatch("invalid length of data"))
33+
length(data) == dim(space) || throw(DimensionMismatch("invalid length of data"))
3634
return new{T, S, N₁, N₂, A}(data, space)
3735
end
3836
end
@@ -453,7 +451,7 @@ end
453451
#-------------------------------------------------
454452
block(t::TensorMap, c::Sector) = blocks(t)[c]
455453

456-
blocks(t::TensorMap) = BlockIterator(t, fusionblockstructure(t).blockstructure)
454+
blocks(t::TensorMap) = BlockIterator(t, blockstructure(space(t)))
457455

458456
function blocktype(::Type{TT}) where {TT <: TensorMap}
459457
A = storagetype(TT)
@@ -462,24 +460,26 @@ function blocktype(::Type{TT}) where {TT <: TensorMap}
462460
end
463461

464462
function Base.iterate(iter::BlockIterator{<:TensorMap}, state...)
465-
next = iterate(iter.structure, state...)
463+
next = iterate(pairs(iter.structure), state...)
466464
isnothing(next) && return next
467465
(c, (sz, r)), newstate = next
468466
return c => reshape(view(iter.t.data, r), sz), newstate
469467
end
470468

471469
function Base.getindex(iter::BlockIterator{<:TensorMap}, c::Sector)
472470
sectortype(iter.t) === typeof(c) || throw(SectorMismatch())
473-
(d₁, d₂), r = get(iter.structure, c) do
474-
# is s is not a key, at least one of the two dimensions will be zero:
471+
found, token = gettoken(iter.structure, c)
472+
if found
473+
(d₁, d₂), r = gettokenvalue(iter.structure, token)
474+
return reshape(view(iter.t.data, r), (d₁, d₂))
475+
else
476+
# if c is not a key, at least one of the two dimensions will be zero:
475477
# it then does not matter where exactly we construct a view in `t.data`,
476478
# as it will have length zero anyway
477-
d₁′ = blockdim(codomain(iter.t), c)
478-
d₂′ = blockdim(domain(iter.t), c)
479-
l = d₁′ * d₂′
480-
return (d₁′, d₂′), 1:l
479+
d₁ = blockdim(codomain(iter.t), c)
480+
d₂ = blockdim(domain(iter.t), c)
481+
return reshape(view(iter.t.data, 1:(d₁ * d₂)), (d₁, d₂))
481482
end
482-
return reshape(view(iter.t.data, r), (d₁, d₂))
483483
end
484484

485485
# Getting and setting the data at the subblock level

‎src/tensors/tensoroperations.jl‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ function TO.tensoralloc(
99
::Type{TT}, structure::TensorMapSpace, istemp::Val, allocator = TO.DefaultAllocator()
1010
) where {TT <: AbstractTensorMap}
1111
A = storagetype(TT)
12-
dim = fusionblockstructure(structure).totaldim
13-
data = TO.tensoralloc(A, dim, istemp, allocator)
12+
data = TO.tensoralloc(A, dim(structure), istemp, allocator)
1413
TT′ = tensormaptype(spacetype(structure), numout(structure), numin(structure), typeof(data))
1514
return TT′(data, structure)
1615
end

0 commit comments

Comments
 (0)