Skip to content

Commit f689f04

Browse files
Implementation of spdiagm for CUSPARSE (#2458)
Co-authored-by: Alexis Montoison <35051714+amontoison@users.noreply.github.com>
1 parent b661096 commit f689f04

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

lib/cusparse/array.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,16 @@ function SparseArrays.sparsevec(I::CuArray{Ti}, V::CuArray{Tv}, n::Integer) wher
322322
CuSparseVector(I, V, n)
323323
end
324324

325+
function SparseArrays.spdiagm(v::CuVector{Tv}) where {Tv}
326+
nzVal = v
327+
N = Int32(length(nzVal))
328+
329+
colPtr = CuArray(one(Int32):(N + one(Int32)))
330+
rowVal = CuArray(one(Int32):N)
331+
dims = (N, N)
332+
CuSparseMatrixCSC(colPtr, rowVal, nzVal, dims)
333+
end
334+
325335
LinearAlgebra.issymmetric(M::Union{CuSparseMatrixCSC,CuSparseMatrixCSR}) = size(M, 1) == size(M, 2) ? norm(M - transpose(M), Inf) == 0 : false
326336
LinearAlgebra.ishermitian(M::Union{CuSparseMatrixCSC,CuSparseMatrixCSR}) = size(M, 1) == size(M, 2) ? norm(M - adjoint(M), Inf) == 0 : false
327337
LinearAlgebra.issymmetric(M::Symmetric{CuSparseMatrixCSC}) = true

test/libraries/cusparse/interfaces.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,3 +538,21 @@ using LinearAlgebra, SparseArrays
538538
end
539539
end
540540
end
541+
542+
@testset "SparseArrays" begin
543+
@testset "spdiagm(CuVector{$elty})" for elty in [Float32, Float64, ComplexF32, ComplexF64]
544+
ref_vec = collect(elty, 100:121)
545+
cuda_vec = CuVector(ref_vec)
546+
547+
ref_spdiagm = spdiagm(ref_vec) # SparseArrays
548+
cuda_spdiagm = spdiagm(cuda_vec) # CuSparseMatrixCSC
549+
550+
ref_cuda_sparse = CuSparseMatrixCSC(ref_spdiagm)
551+
552+
@test ref_cuda_sparse.rowVal == cuda_spdiagm.rowVal
553+
554+
@test ref_cuda_sparse.nzVal == cuda_spdiagm.nzVal
555+
556+
@test ref_cuda_sparse.colPtr == cuda_spdiagm.colPtr
557+
end
558+
end

0 commit comments

Comments
 (0)