Skip to content

Commit 0ab79d4

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 0ab79d4

2 files changed

Lines changed: 17 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: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,26 @@
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
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+
_copy_subblocks_kernel!(subblocks(tdst), tsrc, offsets)
9+
return tdst
10+
end
1511

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₂])
12+
function _copy_subblocks_kernel!(dstblocks, tsrc, offsets)
13+
for (k, v) in nonzero_pairs(tsrc), ((f₁, f₂), src) in subblocks(v)
14+
ranges = map(offsets, (f₁.uncoupled..., f₂.uncoupled...), Tuple(k)) do o, c, kᵢ
15+
return (o[c][kᵢ] + 1):o[c][kᵢ + 1]
2116
end
17+
copy!(dstblocks[(f₁, f₂)][ranges...], src)
2218
end
23-
return tdst
19+
return nothing
20+
end
21+
22+
# for every sector `c` of `V`, the cumulative dimensions of `c` over the summands of `V`
23+
function _sumspace_offsets(V)
24+
return SectorDict{sectortype(V), Vector{Int}}(c => cumsum(vcat(0, [dim(Vᵢ, c) for Vᵢ in V])) for c in sectors(V))
2425
end
2526

2627
function Base.convert(::Type{TensorMap}, t::AbstractBlockTensorMap)

0 commit comments

Comments
 (0)