|
| 1 | +# on-device sparse array functionality |
| 2 | + |
| 3 | +using SparseArrays |
| 4 | + |
| 5 | +# NOTE: this functionality is currently very bare-bones, only defining the array types |
| 6 | +# without any device-compatible sparse array functionality |
| 7 | + |
| 8 | + |
| 9 | +# core types |
| 10 | + |
| 11 | +export CuSparseDeviceVector, CuSparseDeviceMatrixCSC, CuSparseDeviceMatrixCSR, |
| 12 | + CuSparseDeviceMatrixBSR, CuSparseDeviceMatrixCOO |
| 13 | + |
| 14 | +mutable struct CuSparseDeviceVector{Tv,Ti} <: AbstractSparseVector{Tv,Ti} |
| 15 | + iPtr::CuDeviceVector{Ti, AS.Global} |
| 16 | + nzVal::CuDeviceVector{Tv, AS.Global} |
| 17 | + dims::NTuple{2,Int} |
| 18 | + nnz::Int |
| 19 | +end |
| 20 | + |
| 21 | +Base.length(g::CuSparseDeviceVector) = prod(g.dims) |
| 22 | +Base.size(g::CuSparseDeviceVector) = g.dims |
| 23 | +Base.ndims(g::CuSparseDeviceVector) = 1 |
| 24 | + |
| 25 | +mutable struct CuSparseDeviceMatrixCSC{Tv,Ti} <: AbstractSparseMatrix{Tv,Ti} |
| 26 | + colPtr::CuDeviceVector{Ti, AS.Global} |
| 27 | + rowVal::CuDeviceVector{Ti, AS.Global} |
| 28 | + nzVal::CuDeviceVector{Tv, AS.Global} |
| 29 | + dims::NTuple{2,Int} |
| 30 | + nnz::Int |
| 31 | +end |
| 32 | + |
| 33 | +Base.length(g::CuSparseDeviceMatrixCSC) = prod(g.dims) |
| 34 | +Base.size(g::CuSparseDeviceMatrixCSC) = g.dims |
| 35 | +Base.ndims(g::CuSparseDeviceMatrixCSC) = 2 |
| 36 | + |
| 37 | +struct CuSparseDeviceMatrixCSR{Tv,Ti} <: AbstractSparseMatrix{Tv,Ti} |
| 38 | + rowPtr::CuDeviceVector{Ti, AS.Global} |
| 39 | + colVal::CuDeviceVector{Ti, AS.Global} |
| 40 | + nzVal::CuDeviceVector{Tv, AS.Global} |
| 41 | + dims::NTuple{2, Int} |
| 42 | + nnz::Int |
| 43 | +end |
| 44 | + |
| 45 | +Base.length(g::CuSparseDeviceMatrixCSR) = prod(g.dims) |
| 46 | +Base.size(g::CuSparseDeviceMatrixCSR) = g.dims |
| 47 | +Base.ndims(g::CuSparseDeviceMatrixCSR) = 2 |
| 48 | + |
| 49 | +mutable struct CuSparseDeviceMatrixBSR{Tv,Ti} <: AbstractSparseMatrix{Tv,Ti} |
| 50 | + rowPtr::CuDeviceVector{Ti} |
| 51 | + colVal::CuDeviceVector{Ti} |
| 52 | + nzVal::CuDeviceVector{Tv} |
| 53 | + dims::NTuple{2,Int} |
| 54 | + blockDim::Int |
| 55 | + dir::Char |
| 56 | + nnz::Int |
| 57 | +end |
| 58 | + |
| 59 | +Base.length(g::CuSparseDeviceMatrixBSR) = prod(g.dims) |
| 60 | +Base.size(g::CuSparseDeviceMatrixBSR) = g.dims |
| 61 | +Base.ndims(g::CuSparseDeviceMatrixBSR) = 2 |
| 62 | + |
| 63 | +struct CuSparseDeviceMatrixCOO{Tv,Ti} <: AbstractSparseMatrix{Tv,Ti} |
| 64 | + rowInd::CuDeviceVector{Ti} |
| 65 | + colInd::CuDeviceVector{Ti} |
| 66 | + nzVal::CuDeviceVector{Tv} |
| 67 | + dims::NTuple{2,Int} |
| 68 | + nnz::Int |
| 69 | +end |
| 70 | + |
| 71 | +Base.length(g::CuSparseDeviceMatrixCOO) = prod(g.dims) |
| 72 | +Base.size(g::CuSparseDeviceMatrixCOO) = g.dims |
| 73 | +Base.ndims(g::CuSparseDeviceMatrixCOO) = 2 |
| 74 | + |
| 75 | + |
| 76 | +# input/output |
| 77 | + |
| 78 | +function Base.show(io::IO, ::MIME"text/plain", A::CuSparseDeviceVector) |
| 79 | + println(io, "$(length(A))-element device sparse vector at:") |
| 80 | + println(io, " iPtr: $(A.iPtr)") |
| 81 | + print(io, " nzVal: $(A.nzVal)") |
| 82 | +end |
| 83 | + |
| 84 | +function Base.show(io::IO, ::MIME"text/plain", A::CuSparseDeviceMatrixCSR) |
| 85 | + println(io, "$(length(A))-element device sparse matrix CSR at:") |
| 86 | + println(io, " rowPtr: $(A.rowPtr)") |
| 87 | + println(io, " colVal: $(A.colVal)") |
| 88 | + print(io, " nzVal: $(A.nzVal)") |
| 89 | +end |
| 90 | + |
| 91 | +function Base.show(io::IO, ::MIME"text/plain", A::CuSparseDeviceMatrixCSC) |
| 92 | + println(io, "$(length(A))-element device sparse matrix CSC at:") |
| 93 | + println(io, " colPtr: $(A.colPtr)") |
| 94 | + println(io, " rowVal: $(A.rowVal)") |
| 95 | + print(io, " nzVal: $(A.nzVal)") |
| 96 | +end |
| 97 | + |
| 98 | +function Base.show(io::IO, ::MIME"text/plain", A::CuSparseDeviceMatrixBSR) |
| 99 | + println(io, "$(length(A))-element device sparse matrix BSR at:") |
| 100 | + println(io, " rowPtr: $(A.rowPtr)") |
| 101 | + println(io, " colVal: $(A.colVal)") |
| 102 | + print(io, " nzVal: $(A.nzVal)") |
| 103 | +end |
| 104 | + |
| 105 | +function Base.show(io::IO, ::MIME"text/plain", A::CuSparseDeviceMatrixCOO) |
| 106 | + println(io, "$(length(A))-element device sparse matrix COO at:") |
| 107 | + println(io, " rowPtr: $(A.rowPtr)") |
| 108 | + println(io, " colInd: $(A.colInd)") |
| 109 | + print(io, " nzVal: $(A.nzVal)") |
| 110 | +end |
0 commit comments