Skip to content

Commit cbc716a

Browse files
lkdvosclaude
andcommitted
Track temporaries with an istemp flag on SparseBlockTensorMap
`TO.tensoralloc` returns an empty sparse tensor that remembers whether it is a temporary. Blocks added later are requested from the allocator with the matching `istemp`, and `tensorfree!` (and `zerovector!`) only release blocks of temporaries, so `tensorfree!` never receives arrays that were not allocated as temporaries. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 1638bf4 commit cbc716a

4 files changed

Lines changed: 40 additions & 24 deletions

File tree

‎src/tensors/sparseblocktensor.jl‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,30 @@
22
struct SparseBlockTensorMap{TT<:AbstractTensorMap{E,S,N₁,N₂}} <: AbstractBlockTensorMap{E,S,N₁,N₂}
33
44
Sparse `SparseBlockTensorMap` type that stores tensors of type `TT` in a sparse dictionary.
5+
`istemp` marks temporaries of TensorOperations, whose blocks are obtained from and released
6+
to an allocator.
57
"""
68
struct SparseBlockTensorMap{TT <: AbstractTensorMap, E, S, N₁, N₂, N} <:
79
AbstractBlockTensorMap{E, S, N₁, N₂}
810
data::Dict{CartesianIndex{N}, TT}
911
space::TensorMapSumSpace{S, N₁, N₂}
12+
istemp::Bool
1013

1114
# uninitialized constructor
1215
function SparseBlockTensorMap{TT, E, S, N₁, N₂, N}(
13-
::UndefBlocksInitializer, space::TensorMapSumSpace{S, N₁, N₂}
16+
::UndefBlocksInitializer, space::TensorMapSumSpace{S, N₁, N₂}; istemp::Bool = false
1417
) where {E, S, N₁, N₂, N, TT <: AbstractTensorMap{E, S, N₁, N₂}}
1518
@assert N₁ + N₂ == N "SparseBlockTensorMap: data has wrong number of dimensions"
1619
data = Dict{CartesianIndex{N}, TT}()
17-
return new{TT, E, S, N₁, N₂, N}(data, space)
20+
return new{TT, E, S, N₁, N₂, N}(data, space, istemp)
1821
end
1922

2023
# constructor from data
2124
function SparseBlockTensorMap{TT, E, S, N₁, N₂, N}(
2225
data::Dict{CartesianIndex{N}, TT}, space::TensorMapSumSpace{S, N₁, N₂}
2326
) where {E, S, N₁, N₂, N, TT <: AbstractTensorMap{E, S, N₁, N₂}}
2427
@assert N₁ + N₂ == N "SparseBlockTensorMap: data has wrong number of dimensions"
25-
return new{TT, E, S, N₁, N₂, N}(data, space)
28+
return new{TT, E, S, N₁, N₂, N}(data, space, false)
2629
end
2730
end
2831

@@ -34,10 +37,11 @@ end
3437

3538
# uninitialized constructor
3639
function SparseBlockTensorMap{TT}(
37-
::Union{UndefBlocksInitializer, UndefInitializer}, space::TensorMapSumSpace{S, N₁, N₂}
40+
::Union{UndefBlocksInitializer, UndefInitializer}, space::TensorMapSumSpace{S, N₁, N₂};
41+
kwargs...
3842
) where {E, S, N₁, N₂, TT <: AbstractTensorMap{E, S, N₁, N₂}}
3943
N = N₁ + N₂
40-
return SparseBlockTensorMap{TT, E, S, N₁, N₂, N}(undef_blocks, space)
44+
return SparseBlockTensorMap{TT, E, S, N₁, N₂, N}(undef_blocks, space; kwargs...)
4145
end
4246

4347
# constructor from data
@@ -194,9 +198,15 @@ end
194198
t::SparseBlockTensorMap, I::CartesianIndex, ::TO.DefaultAllocator
195199
) = getindex!(t, I)
196200
@propagate_inbounds function getindex!(t::SparseBlockTensorMap, I::CartesianIndex, allocator)
197-
haskey(t, I) && return t[I]
198-
tI = TO.tensoralloc(eltype(t), eachspace(t)[I], Val(false), allocator)
199-
return t[I] = zerovector!(tI)
201+
return haskey(t, I) ? t[I] : zerovector!(allocblock!(t, I, allocator))
202+
end
203+
function allocblock!(t::SparseBlockTensorMap, I::CartesianIndex, allocator)
204+
V = eachspace(t)[I]
205+
return t[I] = if t.istemp
206+
TO.tensoralloc(eltype(t), V, Val(true), allocator)
207+
else
208+
TO.tensoralloc(eltype(t), V, Val(false), allocator)
209+
end
200210
end
201211

202212
# Show

‎src/tensors/tensoroperations.jl‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,25 +101,29 @@ function TO.tensoralloc(
101101
::Type{BT}, structure::TensorMapSumSpace, istemp::Val, allocator = TO.DefaultAllocator()
102102
) where {BT <: AbstractBlockTensorMap}
103103
C = BT(undef_blocks, structure)
104-
issparse(C) && return C # don't fill up sparse blocks
105104
blockallocator(V) = TO.tensoralloc(eltype(C), V, istemp, allocator)
106105
map!(blockallocator, parent(C), eachspace(C))
107106
return C
108107
end
109108

110-
# sparse results allocate exactly the blocks that will be written, so `tensorfree!` is consistent
109+
# sparse tensors start out empty, and only remember whether their blocks are temporaries
110+
function TO.tensoralloc(
111+
::Type{BT}, structure::TensorMapSumSpace, ::Val{istemp},
112+
allocator = TO.DefaultAllocator()
113+
) where {BT <: SparseBlockTensorMap, istemp}
114+
return BT(undef_blocks, structure; istemp)
115+
end
116+
117+
# sparse results allocate the blocks that will be written up front
111118
const BlockOrAdjoint = Union{AbstractBlockTensorMap, AdjointBlockTensorMap}
112119

113120
_tensoralloc(ttype, structure, keys, istemp::Val, allocator) =
114121
TO.tensoralloc(ttype, structure, istemp, allocator)
115122
function _tensoralloc(
116123
ttype::Type{<:SparseBlockTensorMap}, structure, keys, istemp::Val, allocator
117124
)
118-
C = ttype(undef_blocks, structure)
119-
Vs = eachspace(C)
120-
for I in keys
121-
haskey(C, I) || (C[I] = TO.tensoralloc(eltype(C), Vs[I], istemp, allocator))
122-
end
125+
C = TO.tensoralloc(ttype, structure, istemp, allocator)
126+
foreach(I -> haskey(C, I) || allocblock!(C, I, allocator), keys)
123127
return C
124128
end
125129

@@ -201,7 +205,7 @@ function TO.tensorfree!(t::BlockTensorMap, allocator = TO.DefaultAllocator())
201205
return nothing
202206
end
203207
function TO.tensorfree!(t::SparseBlockTensorMap, allocator = TO.DefaultAllocator())
204-
foreach(Base.Fix2(TO.tensorfree!, allocator), nonzero_values(t))
208+
t.istemp && foreach(Base.Fix2(TO.tensorfree!, allocator), nonzero_values(t))
205209
return nothing
206210
end
207211

‎src/tensors/vectorinterface.jl‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# zerovector
22
# ----------
33
VI.zerovector!(t::AbstractBlockTensorMap) = (zerovector!(parent(t)); t)
4-
VI.zerovector!(t::SparseBlockTensorMap) = (empty!(t.data); t)
4+
# blocks of temporaries belong to an allocator, and are kept until `tensorfree!`
5+
function VI.zerovector!(t::SparseBlockTensorMap)
6+
t.istemp ? foreach(zerovector!, nonzero_values(t)) : empty!(t.data)
7+
return t
8+
end
59

610
# scale
711
# -----

‎test/linalg/tensoroperations.jl‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,17 +154,15 @@ end
154154
##
155155

156156
struct TrackingAllocator
157-
live::Set{UInt} # handed out and not freed yet
158-
temps::Set{UInt} # handed out as temporaries
159-
foreign::Base.RefValue{Int} # freed arrays that were not handed out by the allocator
157+
live::Set{UInt} # temporaries that were handed out and not freed yet
158+
foreign::Base.RefValue{Int} # freed arrays that were not handed out as temporaries
160159
end
161-
TrackingAllocator() = TrackingAllocator(Set{UInt}(), Set{UInt}(), Ref(0))
160+
TrackingAllocator() = TrackingAllocator(Set{UInt}(), Ref(0))
162161
function TensorOperations.tensoralloc(
163162
::Type{A}, structure, ::Val{istemp}, allocator::TrackingAllocator
164163
) where {A <: AbstractArray, istemp}
165164
x = fill!(TensorOperations.tensoralloc(A, structure, Val(false)), NaN)
166-
push!(allocator.live, objectid(x))
167-
istemp && push!(allocator.temps, objectid(x))
165+
istemp && push!(allocator.live, objectid(x))
168166
return x
169167
end
170168
function TensorOperations.tensorfree!(x::AbstractArray, allocator::TrackingAllocator)
@@ -179,7 +177,7 @@ function check_allocator(f)
179177
allocator = TrackingAllocator()
180178
@test f(allocator) ≈ f(TensorOperations.DefaultAllocator())
181179
@test allocator.foreign[] == 0
182-
return @test isdisjoint(allocator.live, allocator.temps)
180+
return @test isempty(allocator.live)
183181
end
184182

185183
@testset "allocator only frees what it allocated ($(sectortype(a)))" for (a, b, c) in (

0 commit comments

Comments
 (0)