|
2 | 2 |
|
3 | 3 | ## API functions |
4 | 4 |
|
| 5 | +function sparsetodense(A::Union{ROCSparseMatrixCSC{T},ROCSparseMatrixCSR{T},ROCSparseMatrixCOO{T}}, index::SparseChar, |
| 6 | + algo::rocsparse_sparse_to_dense_alg=rocsparse_sparse_to_dense_alg_default) where {T} |
| 7 | + m,n = size(A) |
| 8 | + B = ROCMatrix{T}(undef, m, n) |
| 9 | + desc_sparse = ROCSparseMatrixDescriptor(A, index) |
| 10 | + desc_dense = ROCDenseMatrixDescriptor(B) |
| 11 | + |
| 12 | + function bufferSize() |
| 13 | + out = Ref{Csize_t}() |
| 14 | + rocsparse_sparse_to_dense(handle(), desc_sparse, desc_dense, algo, out, C_NULL) |
| 15 | + return out[] |
| 16 | + end |
| 17 | + |
| 18 | + buffer_size = Ref{Csize_t}() |
| 19 | + with_workspace(bufferSize) do buffer |
| 20 | + buffer_size[] = sizeof(buffer) |
| 21 | + rocsparse_sparse_to_dense(handle(), desc_sparse, desc_dense, algo, buffer_size, buffer) |
| 22 | + end |
| 23 | + return B |
| 24 | +end |
| 25 | + |
| 26 | +function densetosparse(A::ROCMatrix{T}, fmt::Symbol, index::SparseChar, |
| 27 | + algo::rocsparse_dense_to_sparse_alg=rocsparse_dense_to_sparse_alg_default) where {T} |
| 28 | + m,n = size(A) |
| 29 | + local rowPtr, colPtr, desc_sparse, B |
| 30 | + if fmt == :coo |
| 31 | + desc_sparse = ROCSparseMatrixDescriptor(ROCSparseMatrixCOO, T, Cint, m, n, index) |
| 32 | + elseif fmt == :csr |
| 33 | + rowPtr = ROCVector{Cint}(undef, m+1) |
| 34 | + desc_sparse = ROCSparseMatrixDescriptor(ROCSparseMatrixCSR, rowPtr, T, Cint, m, n, index) |
| 35 | + elseif fmt == :csc |
| 36 | + colPtr = ROCVector{Cint}(undef, n+1) |
| 37 | + desc_sparse = ROCSparseMatrixDescriptor(ROCSparseMatrixCSC, colPtr, T, Cint, m, n, index) |
| 38 | + else |
| 39 | + error("Format :$fmt not available, use :csc, :csr or :coo.") |
| 40 | + end |
| 41 | + desc_dense = ROCDenseMatrixDescriptor(A) |
| 42 | + |
| 43 | + function bufferSize() |
| 44 | + out = Ref{Csize_t}() |
| 45 | + rocsparse_dense_to_sparse(handle(), desc_dense, desc_sparse, algo, out, C_NULL) |
| 46 | + return out[] |
| 47 | + end |
| 48 | + |
| 49 | + buffer_size = Ref{Csize_t}() |
| 50 | + with_workspace(bufferSize) do buffer |
| 51 | + buffer_size[] = sizeof(buffer) |
| 52 | + # Analysis |
| 53 | + rocsparse_dense_to_sparse(handle(), desc_dense, desc_sparse, algo, C_NULL, buffer) |
| 54 | + nnzB = Ref{Int64}() |
| 55 | + rocsparse_spmat_get_size(desc_sparse, Ref{Int64}(), Ref{Int64}(), nnzB) |
| 56 | + if fmt == :coo |
| 57 | + rowInd = ROCVector{Cint}(undef, nnzB[]) |
| 58 | + colInd = ROCVector{Cint}(undef, nnzB[]) |
| 59 | + nzVal = ROCVector{T}(undef, nnzB[]) |
| 60 | + B = ROCSparseMatrixCOO{T, Cint}(rowInd, colInd, nzVal, (m,n)) |
| 61 | + rocsparse_coo_set_pointers(desc_sparse, B.rowInd, B.colInd, B.nzVal) |
| 62 | + elseif fmt == :csr |
| 63 | + colVal = ROCVector{Cint}(undef, nnzB[]) |
| 64 | + nzVal = ROCVector{T}(undef, nnzB[]) |
| 65 | + B = ROCSparseMatrixCSR{T, Cint}(rowPtr, colVal, nzVal, (m,n)) |
| 66 | + rocsparse_csr_set_pointers(desc_sparse, B.rowPtr, B.colVal, B.nzVal) |
| 67 | + elseif fmt == :csc |
| 68 | + rowVal = ROCVector{Cint}(undef, nnzB[]) |
| 69 | + nzVal = ROCVector{T}(undef, nnzB[]) |
| 70 | + B = ROCSparseMatrixCSC{T, Cint}(colPtr, rowVal, nzVal, (m,n)) |
| 71 | + rocsparse_csc_set_pointers(desc_sparse, B.colPtr, B.rowVal, B.nzVal) |
| 72 | + else |
| 73 | + error("Format :$fmt not available, use :csc, :csr or :coo.") |
| 74 | + end |
| 75 | + rocsparse_dense_to_sparse(handle(), desc_dense, desc_sparse, algo, buffer_size, buffer) |
| 76 | + end |
| 77 | + return B |
| 78 | +end |
| 79 | + |
5 | 80 | function gather!(X::ROCSparseVector, Y::ROCVector, index::SparseChar) |
6 | 81 | descX = ROCSparseVectorDescriptor(X, index) |
7 | 82 | descY = ROCDenseVectorDescriptor(Y) |
8 | 83 | rocsparse_gather(handle(), descY, descX) |
9 | 84 | X |
10 | 85 | end |
11 | 86 |
|
| 87 | +function scatter!(Y::ROCVector, X::ROCSparseVector, index::SparseChar) |
| 88 | + descX = ROCSparseVectorDescriptor(X, index) |
| 89 | + descY = ROCDenseVectorDescriptor(Y) |
| 90 | + rocsparse_scatter(handle(), descX, descY) |
| 91 | + return Y |
| 92 | +end |
| 93 | + |
| 94 | +function axpby!(alpha::Number, X::ROCSparseVector{T}, beta::Number, Y::ROCVector{T}, index::SparseChar) where {T} |
| 95 | + descX = ROCSparseVectorDescriptor(X, index) |
| 96 | + descY = ROCDenseVectorDescriptor(Y) |
| 97 | + rocsparse_axpby(handle(), Ref{T}(alpha), descX, Ref{T}(beta), descY) |
| 98 | + return Y |
| 99 | +end |
| 100 | + |
| 101 | +function rot!(X::ROCSparseVector{T}, Y::ROCVector{T}, c::Number, s::Number, index::SparseChar) where {T} |
| 102 | + descX = ROCSparseVectorDescriptor(X, index) |
| 103 | + descY = ROCDenseVectorDescriptor(Y) |
| 104 | + rocsparse_rot(handle(), Ref{T}(c), Ref{T}(s), descX, descY) |
| 105 | + return X, Y |
| 106 | +end |
| 107 | + |
| 108 | +function vv!(transx::SparseChar, X::ROCSparseVector{T}, Y::DenseROCVector{T}, index::SparseChar) where {T} |
| 109 | + descX = ROCSparseVectorDescriptor(X, index) |
| 110 | + descY = ROCDenseVectorDescriptor(Y) |
| 111 | + result = Ref{T}() |
| 112 | + |
| 113 | + function bufferSize() |
| 114 | + out = Ref{Csize_t}() |
| 115 | + rocsparse_spvv(handle(), transx, descX, descY, result, T, out, C_NULL) |
| 116 | + return out[] |
| 117 | + end |
| 118 | + |
| 119 | + buffer_size = Ref{Csize_t}() |
| 120 | + with_workspace(bufferSize) do buffer |
| 121 | + buffer_size[] = sizeof(buffer) |
| 122 | + rocsparse_spvv(handle(), transx, descX, descY, result, T, buffer_size, buffer) |
| 123 | + end |
| 124 | + return result[] |
| 125 | +end |
| 126 | + |
12 | 127 | function mv!( |
13 | 128 | transa::SparseChar, alpha::Number, A::Union{ROCSparseMatrixCSR{T}, ROCSparseMatrixCSC{T}, ROCSparseMatrixCOO{T}}, |
14 | 129 | X::DenseROCVector{T}, beta::Number, Y::DenseROCVector{T}, index::SparseChar, |
|
0 commit comments