Skip to content

Fix spdiagm with specified pairs #2784

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 24, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
57 changes: 49 additions & 8 deletions lib/cusparse/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -336,14 +336,55 @@ function SparseArrays.sparsevec(I::CuArray{Ti}, V::CuArray{Tv}, n::Integer) wher
CuSparseVector(I, V, n)
end

function SparseArrays.spdiagm(v::CuVector{Tv}) where {Tv}
nzVal = v
N = Int32(length(nzVal))

colPtr = CuArray(one(Int32):(N + one(Int32)))
rowVal = CuArray(one(Int32):N)
dims = (N, N)
CuSparseMatrixCSC(colPtr, rowVal, nzVal, dims)
SparseArrays.spdiagm(kv::Pair{<:Integer,<:CuVector}...) = _cuda_spdiagm(nothing, kv...)
SparseArrays.spdiagm(m::Integer, n::Integer, kv::Pair{<:Integer,<:CuVector}...) = _cuda_spdiagm((Int(m),Int(n)), kv...)
SparseArrays.spdiagm(v::CuVector) = _cuda_spdiagm(nothing, 0 => v)
SparseArrays.spdiagm(m::Integer, n::Integer, v::CuVector) = _cuda_spdiagm((Int(m), Int(n)), 0 => v)

function _cuda_spdiagm(size, kv::Pair{<:Integer, <:CuVector}...)
I, J, V, mmax, nmax = _cuda_spdiagm_internal(kv...)
mnmax = max(mmax, nmax)
m, n = something(size, (mnmax,mnmax))
(m ≥ mmax && n ≥ nmax) || throw(DimensionMismatch("invalid size=$size"))
return sparse(CuVector(I), CuVector(J), V, m, n)
end

function _cuda_spdiagm_internal(kv::Pair{<:Integer,<:CuVector{T}}...) where {T}
ncoeffs = 0
for p in kv
ncoeffs += SparseArrays._nnz(p.second)
end
I = Vector{T}(undef, ncoeffs)
J = Vector{T}(undef, ncoeffs)
V = CuArray{promote_type(map(x -> eltype(x.second), kv)...)}(undef, ncoeffs)
i = 0
m = 0
n = 0
for p in kv
k = p.first
v = p.second
if k < 0
row = -k
col = 0
elseif k > 0
row = 0
col = k
else
row = 0
col = 0
end
numel = SparseArrays._nnz(v)
r = 1+i:numel+i
I_r, J_r = SparseArrays._indices(v, row, col)
copyto!(view(I, r), I_r)
copyto!(view(J, r), J_r)
copyto!(view(V, r), v)
veclen = length(v)
m = max(m, row + veclen)
n = max(n, col + veclen)
i += numel
end
return I, J, V, m, n
end

LinearAlgebra.issymmetric(M::Union{CuSparseMatrixCSC,CuSparseMatrixCSR}) = size(M, 1) == size(M, 2) ? norm(M - transpose(M), Inf) == 0 : false
Expand Down
18 changes: 17 additions & 1 deletion test/libraries/cusparse/interfaces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,23 @@ end
cuda_vec = CuVector(ref_vec)

ref_spdiagm = spdiagm(ref_vec) # SparseArrays
cuda_spdiagm = spdiagm(cuda_vec) # CuSparseMatrixCSC
cuda_spdiagm = spdiagm(cuda_vec)

ref_cuda_sparse = CuSparseMatrixCSC(ref_spdiagm)

@test ref_cuda_sparse.rowVal == cuda_spdiagm.rowVal

@test ref_cuda_sparse.nzVal == cuda_spdiagm.nzVal

@test ref_cuda_sparse.colPtr == cuda_spdiagm.colPtr
end

@testset "spdiagm(2 => CuVector{$elty})" for elty in [Float32, Float64, ComplexF32, ComplexF64]
ref_vec = collect(elty, 100:121)
cuda_vec = CuVector(ref_vec)

ref_spdiagm = spdiagm(2 => ref_vec) # SparseArrays
cuda_spdiagm = spdiagm(2 => cuda_vec)

ref_cuda_sparse = CuSparseMatrixCSC(ref_spdiagm)

Expand Down