Skip to content

Treat PtrArray as CPU storage - #629

Merged
lkdvos merged 1 commit into
mainfrom
lb/ptrarray_cpustorage
Nov 19, 2026
Merged

lkdvos merged 1 commit into
mainfrom
lb/ptrarray_cpustorage

Conversation

@leburgel

@leburgel leburgel commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Temporaries from TensorOperations' ManualAllocator are PtrArrays, which are not in CPUStorage = Union{Array, Memory}. Their recoupling workspace in fusion-tree transformations with real coefficients and complex data (e.g. SU(2), ComplexF64) therefore misses the BLAS.gemm! method of _recouple!, and mul! on the reshaped view of the reinterpreted buffer falls back to LinearAlgebra._generic_matmatmul!.

This adds PtrArray to CPUStorage. Its only use on main is that _recouple! method, whose body only reinterprets the buffer and passes pointers to BLAS. PtrArrays.jl becomes a direct dependency, with the compat bound of TensorOperations, which already depends on it. The grouped subblock kernel of #597 dispatches on the same CPUStorage, so once rebased it also applies to PtrArray-backed tensors. Measured on #597's head with PtrArray added there (an SU(2) PEPS CTMRG corner contraction, D = 7, χ = 49): ManualAllocator 0.085 s → 0.043 s with the _recouple! method alone, 0.010 s with both, against 0.012 s for a BufferAllocator.

Found in QuantumKitHub/PEPSKit.jl#482, where using a ManualAllocator gave a significant slowdown due to the BLAS.gemm! miss in the recouplings.

Reproducer
using TensorKit, TensorOperations
V = SU2Space(0 => 4, 1 // 2 => 4, 1 => 3, 3 // 2 => 2)
t = randn(ComplexF64, V ⊗ V ⊗ V ← V ⊗ V)
p = ((3, 1, 5), (2, 4))
ref = permute(t, p)
for allocator in (TensorOperations.DefaultAllocator(), TensorOperations.ManualAllocator())
    times = map(_ -> @elapsed(permute(t, p; allocator)), 1:2)
    println(rpad(nameof(typeof(allocator)), 17), round.(times; digits = 3), " s, ≈ default: ", permute(t, p; allocator) ≈ ref)
end

main:

DefaultAllocator [0.02, 0.009] s, ≈ default: true
ManualAllocator  [8.684, 0.029] s, ≈ default: true

This PR:

DefaultAllocator [0.021, 0.009] s, ≈ default: true
ManualAllocator  [5.52, 0.006] s, ≈ default: true

Julia 1.12.7, TensorOperations 5.8.4, 1 Julia thread, 8 BLAS threads; the first call includes compilation.

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
src/TensorKit.jl 17.24% <ø> (ø)
src/tensors/treetransformers.jl 96.66% <ø> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lkdvos
lkdvos merged commit 148bc26 into main Nov 19, 2026
43 of 45 checks passed
@lkdvos
lkdvos deleted the lb/ptrarray_cpustorage branch November 19, 2026 19:11
Comment thread src/TensorKit.jl

using Dictionaries: Dictionaries, Dictionary, Indices, gettoken, gettokenvalue
using OhMyThreads
using PtrArrays: PtrArray

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we not have used

using TensorOperations.PtrArrays: PtrArray

in order not to have to have PtrArrays as explicit dependency and maintain its compat bounds. Or is this a bad coding pattern?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think that is not the best code pattern, although I do think we have done this in other parts of our codebase as well. Ultimately dependabot will handle the compat entries so it might be fine for now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants