|
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, out, C_NULL) |
| 15 | + return out[] |
| 16 | + end |
| 17 | + with_workspace(bufferSize) do buffer |
| 18 | + buffer_size = sizeof(buffer) |
| 19 | + rocsparse_sparse_to_dense(handle(), desc_sparse, desc_dense, algo, buffer_size, buffer) |
| 20 | + end |
| 21 | + return B |
| 22 | +end |
| 23 | + |
| 24 | +function densetosparse(A::ROCMatrix{T}, fmt::Symbol, index::SparseChar, |
| 25 | + algo::rocsparse_dense_to_sparse_alg=rocsparse_dense_to_sparse_alg_default) where {T} |
| 26 | + m,n = size(A) |
| 27 | + local rowPtr, colPtr, desc_sparse, B |
| 28 | + if fmt == :coo |
| 29 | + desc_sparse = ROCSparseMatrixDescriptor(ROCSparseMatrixCOO, T, Cint, m, n, index) |
| 30 | + elseif fmt == :csr |
| 31 | + rowPtr = ROCVector{Cint}(undef, m+1) |
| 32 | + desc_sparse = ROCSparseMatrixDescriptor(ROCSparseMatrixCSR, rowPtr, T, Cint, m, n, index) |
| 33 | + elseif fmt == :csc |
| 34 | + colPtr = ROCVector{Cint}(undef, n+1) |
| 35 | + desc_sparse = ROCSparseMatrixDescriptor(ROCSparseMatrixCSC, colPtr, T, Cint, m, n, index) |
| 36 | + else |
| 37 | + error("Format :$fmt not available, use :csc, :csr or :coo.") |
| 38 | + end |
| 39 | + desc_dense = ROCDenseMatrixDescriptor(A) |
| 40 | + |
| 41 | + function bufferSize() |
| 42 | + out = Ref{Csize_t}() |
| 43 | + rocsparse_dense_to_sparse(handle(), desc_dense, desc_sparse, algo, out, C_NULL) |
| 44 | + return out[] |
| 45 | + end |
| 46 | + with_workspace(bufferSize) do buffer |
| 47 | + buffer_size = sizeof(buffer) |
| 48 | + # Analysis |
| 49 | + rocsparse_dense_to_sparse(handle(), desc_dense, desc_sparse, algo, C_NULL, buffer) |
| 50 | + nnzB = Ref{Int64}() |
| 51 | + rocsparse_spmat_get_size(desc_sparse, Ref{Int64}(), Ref{Int64}(), nnzB) |
| 52 | + if fmt == :coo |
| 53 | + rowInd = ROCVector{Cint}(undef, nnzB[]) |
| 54 | + colInd = ROCVector{Cint}(undef, nnzB[]) |
| 55 | + nzVal = ROCVector{T}(undef, nnzB[]) |
| 56 | + B = ROCSparseMatrixCOO{T, Cint}(rowInd, colInd, nzVal, (m,n)) |
| 57 | + rocsparse_coo_set_pointers(desc_sparse, B.rowInd, B.colInd, B.nzVal) |
| 58 | + elseif fmt == :csr |
| 59 | + colVal = ROCVector{Cint}(undef, nnzB[]) |
| 60 | + nzVal = ROCVector{T}(undef, nnzB[]) |
| 61 | + B = ROCSparseMatrixCSR{T, Cint}(rowPtr, colVal, nzVal, (m,n)) |
| 62 | + rocsparse_csr_set_pointers(desc_sparse, B.rowPtr, B.colVal, B.nzVal) |
| 63 | + elseif fmt == :csc |
| 64 | + rowVal = ROCVector{Cint}(undef, nnzB[]) |
| 65 | + nzVal = ROCVector{T}(undef, nnzB[]) |
| 66 | + B = ROCSparseMatrixCSC{T, Cint}(colPtr, rowVal, nzVal, (m,n)) |
| 67 | + rocsparse_csc_set_pointers(desc_sparse, B.colPtr, B.rowVal, B.nzVal) |
| 68 | + else |
| 69 | + error("Format :$fmt not available, use :csc, :csr or :coo.") |
| 70 | + end |
| 71 | + rocsparse_dense_to_sparse(handle(), desc_dense, desc_sparse, algo, buffer_size, buffer) |
| 72 | + end |
| 73 | + return B |
| 74 | +end |
| 75 | + |
5 | 76 | function gather!(X::ROCSparseVector, Y::ROCVector, index::SparseChar) |
6 | 77 | descX = ROCSparseVectorDescriptor(X, index) |
7 | 78 | descY = ROCDenseVectorDescriptor(Y) |
|
0 commit comments