Skip to content

Commit b475dc7

Browse files
authored
Insert allocator checkpoints for @planar calls (#529)
1 parent a185415 commit b475dc7

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

‎src/planar/macros.jl‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ function planarparser(planarexpr, kwargs...)
4444
push!(parser.postprocessors, ex -> insertplanarallocator(ex, allocator))
4545
# the alloc/free calls are still `GlobalRef(TensorOperations, ...)`
4646
push!(parser.postprocessors, ex -> TO.insertallocator(ex, allocator))
47+
# add checkpoints around the block
48+
push!(parser.postprocessors, ex -> TO.insertcheckpoints(ex, allocator))
4749
break
4850
end
4951
end

‎test/tensors/planar.jl‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,34 @@ end
128128
@test occursin("DefaultBackend", string(ex))
129129
end
130130

131+
@testset "allocator is rewound" begin
132+
# A `BufferAllocator` hands out slices of a single buffer and reclaims them only
133+
# by rewinding its offset -- `tensorfree!` is a no-op for it. The temporaries a
134+
# block creates for intermediate results are released that way, so without a
135+
# checkpoint/reset pair around the block their space is never reclaimed. A buffer
136+
# that is not fully drained also never resizes itself, so it would stay pinned at
137+
# whatever size it first grew to, and every later temporary would fall back on the
138+
# garbage collector.
139+
for W in (ℂ^4, Vect[FermionParity](0 => 2, 1 => 2))
140+
A = rand(T, W ← W ⊗ W)
141+
B = rand(T, W ⊗ W ← W)
142+
@planar Cref[i; j] := A[i; k l] * τ[k l; m n] * B[m n; j]
143+
144+
# three tensors, so the first contraction is an intermediate temporary
145+
buffer = TensorOperations.BufferAllocator(; sizehint = 1 << 16)
146+
@planar allocator = buffer C[i; j] := A[i; k l] * τ[k l; m n] * B[m n; j]
147+
@test isempty(buffer)
148+
@test C ≈ Cref
149+
150+
# the result must not live in the buffer: the next block hands out the same
151+
# memory again, and `C` has to survive that
152+
@planar allocator = buffer C2[i; j] := A[i; k l] * τ[k l; m n] * B[m n; j]
153+
@test isempty(buffer)
154+
@test C ≈ Cref
155+
@test C2 ≈ Cref
156+
end
157+
end
158+
131159
@testset "contractcheck" begin
132160
V = ℂ^2
133161
A = rand(T, V ⊗ V ← V)

0 commit comments

Comments
 (0)