Skip to content

Commit bfdf4fc

Browse files
lkdvosclaude
andcommitted
Index the destination once in _copy_subblocks!
`_copy_subblocks!` looped over the fusion-tree blocks of the destination and read every nonzero block of the source with `v[f₁, f₂]`, which goes through TensorKit's lock-protected `degeneracystructure` cache on each call: (#fusion trees) × (#nonzero blocks) cache hits. It now iterates the source blocks with `subblocks(v)` and indexes a single `subblocks(tdst)` iterator by fusion-tree pair, so the structure is looked up once per tensor. The block ranges come from per-leg cumulative dimensions computed once, instead of a `blockedrange` per subblock. Bump version to v0.3.20. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent e4ee03c commit bfdf4fc

2 files changed

Lines changed: 13 additions & 16 deletions

File tree

‎Project.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "BlockTensorKit"
22
uuid = "5f87ffc2-9cf1-4a46-8172-465d160bd8cd"
3-
version = "0.3.19"
3+
version = "0.3.20"
44
authors = ["Lukas Devos <ldevos98@gmail.com> and contributors"]
55

66
[deps]

‎src/tensors/abstractblocktensor/conversion.jl‎

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,24 @@
22
# ----------
33

44
function _copy_subblocks!(tdst, tsrc)
5-
S = spacetype(tsrc)
65
N₁, N₂ = numout(tsrc), numin(tsrc)
7-
for ((f₁, f₂), arr) in subblocks(tdst)
8-
blockax = ntuple(N₁ + N₂) do i
9-
return if i <= N₁
10-
blockedrange(map(Base.Fix2(dim, f₁.uncoupled[i]), space(tsrc, i)))
11-
else
12-
blockedrange(map(Base.Fix2(dim, f₂.uncoupled[i - N₁]), space(tsrc, i)'))
13-
end
14-
end
15-
16-
for (k, v) in nonzero_pairs(tsrc)
17-
indices = getindex.(blockax, Block.(Tuple(k)))
18-
arr_slice = arr[indices...]
19-
# need to check for empty since fusion tree pair might not be present
20-
isempty(arr_slice) || copy!(arr_slice, v[f₁, f₂])
6+
offsets = ntuple(i -> _sumspace_offsets(i <= N₁ ? codomain(tsrc)[i] : domain(tsrc)[i - N₁]), N₁ + N₂)
7+
# a single `SubblockIterator` holds the structure: `tdst[f₁, f₂]` would look it up in TensorKit's locked cache every time
8+
dstblocks = subblocks(tdst)
9+
for (k, v) in nonzero_pairs(tsrc), ((f₁, f₂), src) in subblocks(v)
10+
ranges = map(offsets, (f₁.uncoupled..., f₂.uncoupled...), Tuple(k)) do o, c, kᵢ
11+
return (o[c][kᵢ] + 1):o[c][kᵢ + 1]
2112
end
13+
copy!(dstblocks[(f₁, f₂)][ranges...], src)
2214
end
2315
return tdst
2416
end
2517

18+
# for every sector `c` of `V`, the cumulative dimensions of `c` over the summands of `V`
19+
function _sumspace_offsets(V)
20+
return SectorDict{sectortype(V), Vector{Int}}(c => cumsum(vcat(0, [dim(Vᵢ, c) for Vᵢ in V])) for c in sectors(V))
21+
end
22+
2623
function Base.convert(::Type{TensorMap}, t::AbstractBlockTensorMap)
2724
S = spacetype(t)
2825
N₁, N₂ = numout(t), numin(t)

0 commit comments

Comments
 (0)