Skip to content

Commit 3f69cc9

Browse files
authored
Make sure DiagonalTensorMaps play well with allocators (#624)
1 parent 9458d76 commit 3f69cc9

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

‎src/tensors/diagonal.jl‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,12 @@ function TO.tensoralloc(
306306
V = only(domain(structure))
307307
dim = reduceddim(V)
308308
data = TO.tensoralloc(M, dim, istemp, allocator)
309-
return DiagonalTensorMap{T, S, M}(data, V)
309+
return DiagonalTensorMap{T, S, typeof(data)}(data, V)
310+
end
311+
312+
function TO.tensorfree!(t::DiagonalTensorMap, allocator = TO.DefaultAllocator())
313+
TO.tensorfree!(t.data, allocator)
314+
return nothing
310315
end
311316

312317
# Linear Algebra and factorizations

‎test/tensors/diagonal.jl‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Test, TestExtras
22
using TensorKit
3+
using TensorOperations
34

45
diagspacelist = (
56
(ℂ^4)',
@@ -254,3 +255,20 @@ diagspacelist = (
254255
end
255256
TensorKit.empty_globalcaches!()
256257
end
258+
259+
@testset "DiagonalTensor temporaries with $(nameof(typeof(allocator)))" for allocator in
260+
(TensorOperations.DefaultAllocator(), TensorOperations.ManualAllocator(), TensorOperations.BufferAllocator())
261+
for V in diagspacelist, T in (Float64, ComplexF64)
262+
BraidingStyle(sectortype(V)) isa SymmetricBraiding || continue
263+
d = tensoralloc(DiagonalTensorMap{T, spacetype(V), Vector{T}}, V ← V, Val(true), allocator)
264+
@test d isa DiagonalTensorMap{T}
265+
@test space(d) == (V ← V)
266+
tensorfree!(d, allocator)
267+
268+
D1, D2, D3 = DiagonalTensorMap(rand(T, reduceddim(V)), V),
269+
DiagonalTensorMap(rand(T, reduceddim(V)), V), DiagonalTensorMap(rand(T, reduceddim(V)), V)
270+
@tensor C[a; b] := D1[a; c] * D2[c; d] * D3[d; b]
271+
@tensor allocator = allocator C′[a; b] := D1[a; c] * D2[c; d] * D3[d; b]
272+
@test C′ ≈ C
273+
end
274+
end

0 commit comments

Comments
 (0)