Skip to content

Support kron between any Diagonal and dense GPU arrays - #890

Open
albertomercurio wants to merge 1 commit into
JuliaGPU:mainfrom
albertomercurio:kron-diagonal-any
Open

albertomercurio wants to merge 1 commit into
JuliaGPU:mainfrom
albertomercurio:kron-diagonal-any

Conversation

@albertomercurio

Copy link
Copy Markdown
Contributor

kron between a Diagonal and a dense GPU array only had a GPU method when the diagonal was itself a GPU vector and the other argument a plain GPU matrix. Other combinations fell back to LinearAlgebra's generic method and hit scalar indexing, for example:

using CUDA, FillArrays
kron(Eye{Float64}(3), CUDA.rand(4, 4))       # Diagonal of Ones
kron(transpose(CUDA.rand(4, 4)), Eye{Float64}(3))
kron(Diagonal(CUDA.rand(3)), CUDA.rand(4))   # vector

This PR:

  • Diagonal: accepts any Diagonal whose diagonal can be passed to a kernel. That is a GPU vector as before, but also an isbits lazy vector, such as a range or the Ones and Fill of FillArrays.jl, without depending on FillArrays. A Diagonal of a CPU Vector now throws the back-end's "cannot use a CPU array in a kernel" error instead of scalar indexing.
  • Dense argument: accepts GPU vectors and Transpose/Adjoint wrappers, like the dense-dense methods already do.
  • Cleanup: shortens the two kernels with fldmod1, and adds a message to the DimensionMismatch.

Tests: the Diagonal-dense tests now cover vectors, transpose/adjoint, and a range diagonal, which adapt keeps on the host. They also check type stability with @inferred.

Verified:

  • linalg/kron passes for JLArray and Array.
  • The linalg/kron test suite and FillArrays.Eye pass with CuArray.
  • With GPUArrays, LinearAlgebra, FillArrays and CUDA loaded, kron has no method ambiguities.

Motivation: QuantumToolbox.jl builds super-operators as kron(Eye(n), A) and kron(transpose(A), Eye(n)), so dense GPU operators could not be used in its master equation solvers.

🤖 Generated with Claude Code

The diagonal can now be any vector usable in a kernel, not only a GPU
vector: e.g., ranges or the `Ones` of FillArrays' `Eye`. The dense argument
can also be a vector, or a `Transpose`/`Adjoint` of a GPU array.

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

This branch has not been deployed

No deployments
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.

1 participant