File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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)
You can’t perform that action at this time.
0 commit comments