Skip to content

Commit 4da2430

Browse files
lkdvosclaude
andcommitted
Speed up block write-back, norm, ==, and tr for AbstractBlockTensorMap
- add copy_blocks! (blockwise dense -> BlockMatrix copy, avoids scalar indexing) and use it for all dense write-backs - truncate_domain!/truncate_codomain!: do not copy the destination that is fully overwritten - norm: one AbstractBlockTensorMap method, no temporary; also pass p to the inner norms - add == and tr for AbstractBlockTensorMap over nonzero keys - fix undefined S in real/imag error message Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 9207116 commit 4da2430

6 files changed

Lines changed: 121 additions & 11 deletions

File tree

‎src/auxiliary/blockarrays.jl‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ function copy_dense!(Adense, A)
1616
return Adense
1717
end
1818

19+
function copy_blocks!(A::BlockMatrix, Adense)
20+
for block_index in Iterators.product(blockaxes(A)...)
21+
a = view(A, block_index...)
22+
isempty(a) && continue
23+
indices = getindex.(axes(A), block_index)
24+
a .= view(Adense, indices...)
25+
end
26+
return A
27+
end
28+
copy_blocks!(A, Adense) = copyto!(A, Adense)
29+
1930
const BlockBlasMat{T <: MAK.BlasFloat} = BlockMatrix{T}
2031

2132
function MAK.zero!(A::BlockBlasMat)

‎src/linalg/factorizations.jl‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ for f! in (
2828
Fblocks′ = MAK.$f!(copy_dense!(similar_dense(tblock), tblock), alg)
2929
# deal with the case where the output is not in-place
3030
for (b′, b) in zip(Fblocks′, Fblocks)
31-
b === b′ || copyto!(b, b′)
31+
b === b′ || copy_blocks!(b, b′)
3232
end
3333
return nothing
3434
end
@@ -46,7 +46,7 @@ for f! in (
4646
TensorKit.foreachblock(t, N) do _, (tblock, Nblock)
4747
Nblock′ = MAK.$f!(copy_dense!(similar_dense(tblock), tblock), alg)
4848
# deal with the case where the output is not the same as the input
49-
Nblock === Nblock′ || copyto!(Nblock, Nblock′)
49+
Nblock === Nblock′ || copy_blocks!(Nblock, Nblock′)
5050
return nothing
5151
end
5252
return N
@@ -186,25 +186,25 @@ end
186186
function TensorKit.Factorizations.truncate_domain!(tdst::AbstractBlockTensorMap, tsrc::AbstractBlockTensorMap, inds)
187187
TensorKit.foreachblock(tdst, tsrc) do c, (dst_block, src_block)
188188
I = get(inds, c, nothing)
189-
dst_dense = copy_dense!(similar_dense(dst_block), dst_block)
189+
dst_dense = similar_dense(dst_block)
190190
src_dense = copy_dense!(similar_dense(src_block), src_block)
191191
@assert !isnothing(I)
192192
@views dst_dense .= src_dense[:, I]
193193
# deal with the case where the output is not in-place
194-
dst_dense === dst_block || copyto!(dst_block, dst_dense)
194+
dst_dense === dst_block || copy_blocks!(dst_block, dst_dense)
195195
return nothing
196196
end
197197
return tdst
198198
end
199199
function TensorKit.Factorizations.truncate_codomain!(tdst::AbstractBlockTensorMap, tsrc::AbstractBlockTensorMap, inds)
200200
TensorKit.foreachblock(tdst, tsrc) do c, (dst_block, src_block)
201201
I = get(inds, c, nothing)
202-
dst_dense = copy_dense!(similar_dense(dst_block), dst_block)
202+
dst_dense = similar_dense(dst_block)
203203
src_dense = copy_dense!(similar_dense(src_block), src_block)
204204
@assert !isnothing(I)
205205
@views dst_dense .= src_dense[I, :]
206206
# deal with the case where the output is not in-place
207-
dst_dense === dst_block || copyto!(dst_block, dst_dense)
207+
dst_dense === dst_block || copy_blocks!(dst_block, dst_dense)
208208
return nothing
209209
end
210210
return tdst

‎src/linalg/linalg.jl‎

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,34 @@ for (TC, TA, TB) in Iterators.product(
157157
end
158158
end
159159

160-
function LinearAlgebra.norm(tA::BlockTensorMap, p::Real = 2)
160+
function LinearAlgebra.norm(tA::AbstractBlockTensorMap, p::Real = 2)
161161
vals = nonzero_values(tA)
162162
isempty(vals) && return norm(zero(scalartype(tA)), p)
163-
return LinearAlgebra.norm(norm.(vals), p)
163+
return LinearAlgebra.norm(Iterators.map(Base.Fix2(norm, p), vals), p)
164+
end
165+
166+
function Base.:(==)(t1::AbstractBlockTensorMap, t2::AbstractBlockTensorMap)
167+
space(t1) == space(t2) || return false
168+
for I in nonzero_keys(t1)
169+
v1 = t1[I]
170+
(haskey(t2, I) ? v1 == t2[I] : iszero(norm(v1))) || return false
171+
end
172+
if issparse(t1)
173+
for I in nonzero_keys(t2)
174+
haskey(t1, I) || iszero(norm(t2[I])) || return false
175+
end
176+
end
177+
return true
178+
end
179+
180+
function LinearAlgebra.tr(t::AbstractBlockTensorMap)
181+
domain(t) == codomain(t) ||
182+
throw(SpaceMismatch("Trace of a tensor only exist when domain == codomain"))
183+
s = zero(scalartype(t)) * zero(TK.dimscalartype(sectortype(t)))
184+
for I in nonzero_keys(t)
185+
TT.getindices(I.I, codomainind(t)) == TT.getindices(I.I, domainind(t)) && (s += tr(t[I]))
186+
end
187+
return s
164188
end
165189

166190
for f in (:real, :imag)
@@ -173,7 +197,7 @@ for f in (:real, :imag)
173197
end
174198
return t′
175199
else
176-
msg = "`$f` has not been implemented for `BlockTensorMap{$(S)}`."
200+
msg = "`$f` has not been implemented for `BlockTensorMap{$(spacetype(t))}`."
177201
throw(ArgumentError(msg))
178202
end
179203
end
@@ -208,7 +232,7 @@ function LinearAlgebra.lmul!(D::DiagonalTensorMap, t::AbstractBlockTensorMap)
208232
domain(D) == codomain(t) || throw(SpaceMismatch())
209233
TensorKit.foreachblock(t, D) do c, (tblock, Dblock)
210234
tblock′ = lmul!(Dblock, copy_dense!(similar_dense(tblock), tblock))
211-
tblock === tblock′ || copyto!(tblock, tblock′)
235+
tblock === tblock′ || copy_blocks!(tblock, tblock′)
212236
return tblock
213237
end
214238
return t
@@ -218,7 +242,7 @@ function LinearAlgebra.rmul!(t::AbstractBlockTensorMap, D::DiagonalTensorMap)
218242
codomain(D) == domain(t) || throw(SpaceMismatch())
219243
TensorKit.foreachblock(t, D) do c, (tblock, Dblock)
220244
tblock′ = rmul!(copy_dense!(similar_dense(tblock), tblock), Dblock)
221-
tblock === tblock′ || copyto!(tblock, tblock′)
245+
tblock === tblock′ || copy_blocks!(tblock, tblock′)
222246
return tblock
223247
end
224248
return t

‎test/abstracttensor/blocktensor.jl‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,34 @@ end
170170
end
171171
end
172172

173+
@testset "norm, ==, tr: test via conversion" begin
174+
W = V1 ⊗ V2 ← V1 ⊗ V2
175+
for T in (Float64, ComplexF64)
176+
t = rand(T, W)
177+
t′ = convert(TensorMap, t)
178+
for p in (1, 2, 3, Inf)
179+
@test norm(t, p) ≈ norm(t′, p)
180+
end
181+
@test tr(t) ≈ tr(t′)
182+
@test t == copy(t)
183+
ts, td = SparseBlockTensorMap(t), BlockTensorMap(t)
184+
@test t == ts == td
185+
@test td == ts
186+
@test td != 2 * ts
187+
@test ts != 2 * td
188+
@test norm(ts, 1) ≈ norm(td, 1)
189+
@test t != 2 * t
190+
t2 = copy(t)
191+
t2[1] = zero(t2[1]) + t2[1]
192+
@test t2 == t
193+
@test (t2 + t) != t
194+
@test t != rand(T, V1 ⊗ V2 ← V1)
195+
@test iszero(norm(spzeros(T, W)))
196+
@test spzeros(T, W) == zero(t)
197+
@test iszero(tr(spzeros(T, W)))
198+
end
199+
end
200+
173201
@testset "Basic linear algebra: test via conversion" begin
174202
W = V1 ⊗ V2 ⊗ V3 ← V4 ⊗ V5
175203
for T in (Float32, ComplexF64)

‎test/abstracttensor/sparseblocktensor.jl‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,34 @@ end
145145
end
146146
end
147147

148+
@testset "norm, ==, tr: test via conversion" begin
149+
W = V1 ⊗ V2 ← V1 ⊗ V2
150+
for T in (Float64, ComplexF64)
151+
t = sprand(T, W, 0.5)
152+
t′ = convert(TensorMap, t)
153+
for p in (1, 2, 3, Inf)
154+
@test norm(t, p) ≈ norm(t′, p)
155+
end
156+
@test tr(t) ≈ tr(t′)
157+
@test t == copy(t)
158+
ts, td = SparseBlockTensorMap(t), BlockTensorMap(t)
159+
@test t == ts == td
160+
@test td == ts
161+
@test td != 2 * ts
162+
@test ts != 2 * td
163+
@test norm(ts, 1) ≈ norm(td, 1)
164+
@test t != 2 * t
165+
t2 = copy(t)
166+
t2[1] = zero(t2[1]) + t2[1]
167+
@test t2 == t
168+
@test (t2 + t) != t
169+
@test t != rand(T, V1 ⊗ V2 ← V1)
170+
@test iszero(norm(spzeros(T, W)))
171+
@test spzeros(T, W) == zero(t)
172+
@test iszero(tr(spzeros(T, W)))
173+
end
174+
end
175+
148176
@testset "Basic linear algebra: test via conversion" begin
149177
W = V1 ⊗ V2 ⊗ V3 ← V4 ⊗ V5
150178
for T in (Float32, ComplexF64)

‎test/linalg/factorizations.jl‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,3 +526,22 @@ end
526526
# end
527527
# end
528528
# end
529+
530+
@testset "In-place write-back into block storage" begin
531+
V1 = SumSpace(ℂ^2, ℂ^2)
532+
V2 = SumSpace(ℂ^3, ℂ^1)
533+
D = DiagonalTensorMap(rand(dim(V1)), V1)
534+
D′ = DiagonalTensorMap(D.data, ℂ^4)
535+
t = rand(V1 ← V1 ⊗ V2)
536+
t0 = TensorMap(t)
537+
@test TensorMap(lmul!(D, t)) ≈ D′ * t0
538+
t = rand(V1 ⊗ V2 ← V1)
539+
t0 = TensorMap(t)
540+
@test TensorMap(rmul!(t, D)) ≈ t0 * D′
541+
542+
t = rand(V1 ← V1 ⊗ V2)
543+
b = block(t, first(blocksectors(t)))
544+
d = BlockTensorKit.copy_dense!(BlockTensorKit.similar_dense(b), b) .* 2
545+
BlockTensorKit.copy_blocks!(b, d)
546+
@test BlockTensorKit.copy_dense!(BlockTensorKit.similar_dense(b), b) == d
547+
end

0 commit comments

Comments
 (0)