Skip to content

Commit bebe0d6

Browse files
lkdvosclaude
andcommitted
Recouple complex data with real coefficients in a real gemm on all storage
Reinterpreting GPU storage as real yields native GPU arrays, such that the real and imaginary parts of complex data can be recoupled with real coefficients in a single `mul!`, which dispatches to the vendor BLAS. This adds a generic `_recouple!` method for complex `DenseVector` buffers with a real recoupling matrix, keeping the direct `BLAS.gemm!` call only for CPU storage, where views of reinterpreted arrays are not `StridedMatrix`. The CPU rule of `recoupling_scalartype`, keeping real coefficients real, is now used for all storage. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 4a0e238 commit bebe0d6

2 files changed

Lines changed: 13 additions & 12 deletions

File tree

‎src/tensors/indexmanipulations.jl‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -744,8 +744,15 @@ end
744744
# computes `buffer_dst = buffer_src * transpose(U)`, where both are column-major views into `buffer`
745745
_recouple!(buffer, buffer_dst, buffer_src, U) =
746746
(mul!(buffer_dst, buffer_src, transpose(StridedView(U))); nothing)
747-
# real coefficients acting on complex data: recouple the real and imaginary parts in a single real gemm,
748-
# called directly since views of reinterpreted arrays are not `StridedMatrix` and `mul!` would not use BLAS
747+
# real coefficients acting on complex data: recouple the real and imaginary parts in a single real gemm
748+
function _recouple!(
749+
buffer::DenseVector{Complex{R}}, buffer_dst::StridedView, buffer_src::StridedView, U::AbstractMatrix{R}
750+
) where {R <: LinearAlgebra.BlasReal}
751+
rbuffer = reinterpret(R, buffer)
752+
mul!(_realview(rbuffer, buffer_dst), _realview(rbuffer, buffer_src), transpose(U))
753+
return nothing
754+
end
755+
# on the CPU, views of reinterpreted arrays are not `StridedMatrix`, so `mul!` would not use BLAS
749756
function _recouple!(
750757
buffer::CPUStorage{Complex{R}}, buffer_dst::StridedView, buffer_src::StridedView, U::Matrix{R}
751758
) where {R <: LinearAlgebra.BlasReal}

‎src/tensors/treetransformers.jl‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,14 @@ end
2323
recoupling_scalartype(A::Type{<:AbstractVector}, Tₛ::Type{<:Number}) -> Type{<:Number}
2424
2525
Scalar type used to store the recoupling coefficients with sector scalar type `Tₛ` in the
26-
transformers for destination tensors with storagetype `A`. For CPU storage with BLAS scalars, this
27-
is the precision of the storage, where real coefficients are kept real also for complex storage,
28-
such that they can be applied to the real and imaginary parts at once. For other storage with BLAS
29-
scalars, this is the scalar type of the storage.
26+
transformers for destination tensors with storagetype `A`. For storage with BLAS scalars, this is
27+
the precision of the storage, where real coefficients are kept real also for complex storage, such
28+
that they can be applied to the real and imaginary parts at once.
3029
"""
31-
function recoupling_scalartype(::Type{A}, ::Type{Tₛ}) where {A <: CPUStorage, Tₛ <: Number}
32-
T = eltype(A)
33-
T <: BlasFloat || return Tₛ
34-
return Tₛ <: Real ? real(T) : complex(T)
35-
end
3630
function recoupling_scalartype(::Type{A}, ::Type{Tₛ}) where {A, Tₛ <: Number}
3731
T = eltype(A)
3832
T <: BlasFloat || return Tₛ
39-
return Tₛ <: Real ? T : complex(T)
33+
return Tₛ <: Real ? real(T) : complex(T)
4034
end
4135

4236
# storagetypes are vector types, so the recoupling matrices are stored as reshaped vectors

0 commit comments

Comments
 (0)