Skip to content

Add dispatches to access device matrix data via SparseArrays interface #2738

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
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions lib/cusparse/device.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ end
Base.length(g::CuSparseDeviceMatrixCSC) = prod(g.dims)
Base.size(g::CuSparseDeviceMatrixCSC) = g.dims
SparseArrays.nnz(g::CuSparseDeviceMatrixCSC) = g.nnz
SparseArrays.rowvals(g::CuSparseDeviceMatrixCSC) = g.rowVal
SparseArrays.getcolptr(g::CuSparseDeviceMatrixCSC) = g.colPtr
SparseArrays.getnzval(g::CuSparseDeviceMatrixCSC) = g.nzVal
SparseArrays.nzrange(g::CuSparseDeviceMatrixCSC, col::Integer) = SparseArrays.getcolptr(g)[col]:(SparseArrays.getcolptr(g)[col+1]-1)

struct CuSparseDeviceMatrixCSR{Tv,Ti,A} <: AbstractSparseMatrix{Tv,Ti}
rowPtr::CuDeviceVector{Ti, A}
Expand All @@ -46,6 +50,7 @@ end
Base.length(g::CuSparseDeviceMatrixCSR) = prod(g.dims)
Base.size(g::CuSparseDeviceMatrixCSR) = g.dims
SparseArrays.nnz(g::CuSparseDeviceMatrixCSR) = g.nnz
SparseArrays.getnzval(g::CuSparseDeviceMatrixCSR) = g.nzVal

struct CuSparseDeviceMatrixBSR{Tv,Ti,A} <: AbstractSparseMatrix{Tv,Ti}
rowPtr::CuDeviceVector{Ti, A}
Expand All @@ -60,6 +65,7 @@ end
Base.length(g::CuSparseDeviceMatrixBSR) = prod(g.dims)
Base.size(g::CuSparseDeviceMatrixBSR) = g.dims
SparseArrays.nnz(g::CuSparseDeviceMatrixBSR) = g.nnz
SparseArrays.getnzval(g::CuSparseDeviceMatrixBSR) = g.nzVal

struct CuSparseDeviceMatrixCOO{Tv,Ti,A} <: AbstractSparseMatrix{Tv,Ti}
rowInd::CuDeviceVector{Ti, A}
Expand All @@ -72,6 +78,7 @@ end
Base.length(g::CuSparseDeviceMatrixCOO) = prod(g.dims)
Base.size(g::CuSparseDeviceMatrixCOO) = g.dims
SparseArrays.nnz(g::CuSparseDeviceMatrixCOO) = g.nnz
SparseArrays.getnzval(g::CuSparseDeviceMatrixCOO) = g.nzVal

struct CuSparseDeviceArrayCSR{Tv, Ti, N, M, A} <: AbstractSparseArray{Tv, Ti, N}
rowPtr::CuDeviceArray{Ti, M, A}
Expand All @@ -89,6 +96,7 @@ end
Base.length(g::CuSparseDeviceArrayCSR) = prod(g.dims)
Base.size(g::CuSparseDeviceArrayCSR) = g.dims
SparseArrays.nnz(g::CuSparseDeviceArrayCSR) = g.nnz
SparseArrays.getnzval(g::CuSparseDeviceArrayCSR) = g.nzVal

# input/output

Expand Down
20 changes: 10 additions & 10 deletions test/libraries/cusparse/device.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ using CUDA.CUSPARSE: CuSparseDeviceVector, CuSparseDeviceMatrixCSC, CuSparseDevi
CuSparseDeviceMatrixBSR, CuSparseDeviceMatrixCOO

@testset "cudaconvert" begin
@test isbitstype(CuSparseDeviceVector{Float32, Cint, CUDA.AS.Global})
@test isbitstype(CuSparseDeviceMatrixCSC{Float32, Cint, CUDA.AS.Global})
@test isbitstype(CuSparseDeviceMatrixCSR{Float32, Cint, CUDA.AS.Global})
@test isbitstype(CuSparseDeviceMatrixBSR{Float32, Cint, CUDA.AS.Global})
@test isbitstype(CuSparseDeviceMatrixCOO{Float32, Cint, CUDA.AS.Global})
@test isbitstype(CuSparseDeviceVector{Float32, Cint, AS.Global})
@test isbitstype(CuSparseDeviceMatrixCSC{Float32, Cint, AS.Global})
@test isbitstype(CuSparseDeviceMatrixCSR{Float32, Cint, AS.Global})
@test isbitstype(CuSparseDeviceMatrixBSR{Float32, Cint, AS.Global})
@test isbitstype(CuSparseDeviceMatrixCOO{Float32, Cint, AS.Global})

V = sprand(10, 0.5)
cuV = CuSparseVector(V)
@test cudaconvert(cuV) isa CuSparseDeviceVector{Float64, Cint, 1}
@test cudaconvert(cuV) isa CuSparseDeviceVector{Float64, Cint, AS.Global}

A = sprand(10, 10, 0.5)
cuA = CuSparseMatrixCSC(A)
@test cudaconvert(cuA) isa CuSparseDeviceMatrixCSC{Float64, Cint, 1}
@test cudaconvert(cuA) isa CuSparseDeviceMatrixCSC{Float64, Cint, AS.Global}

cuA = CuSparseMatrixCSR(A)
@test cudaconvert(cuA) isa CuSparseDeviceMatrixCSR{Float64, Cint, 1}
@test cudaconvert(cuA) isa CuSparseDeviceMatrixCSR{Float64, Cint, AS.Global}

cuA = CuSparseMatrixCOO(A)
@test cudaconvert(cuA) isa CuSparseDeviceMatrixCOO{Float64, Cint, 1}
@test cudaconvert(cuA) isa CuSparseDeviceMatrixCOO{Float64, Cint, AS.Global}

cuA = CuSparseMatrixBSR(A, 2)
@test cudaconvert(cuA) isa CuSparseDeviceMatrixBSR
@test cudaconvert(cuA) isa CuSparseDeviceMatrixBSR{Float64, Cint, AS.Global}
end