Skip to content

Commit 0b36555

Browse files
committed
Interface sparsetodense and densetosparse
1 parent 586d952 commit 0b36555

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

src/sparse/generic.jl

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,77 @@
22

33
## API functions
44

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+
576
function gather!(X::ROCSparseVector, Y::ROCVector, index::SparseChar)
677
descX = ROCSparseVectorDescriptor(X, index)
778
descY = ROCDenseVectorDescriptor(Y)

test/rocsparse/generic.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,28 @@
1+
fmt = Dict(ROCSparseMatrixCSC => :csc,
2+
ROCSparseMatrixCSR => :csr,
3+
ROCSparseMatrixCOO => :coo)
4+
5+
for SparseMatrixType in [ROCSparseMatrixCSC, ROCSparseMatrixCSR, ROCSparseMatrixCOO]
6+
@testset "$SparseMatrixType -- densetosparse algo=$algo" for algo in [rocSPARSE.rocsparse_dense_to_sparse_alg_default]
7+
@testset "densetosparse $T" for T in [Float32, Float64, ComplexF32, ComplexF64]
8+
A_sparse = sprand(T, 10, 20, 0.5)
9+
A_dense = Matrix{T}(A_sparse)
10+
dA_dense = ROCMatrix{T}(A_dense)
11+
dA_sparse = rocSPARSE.densetosparse(dA_dense, fmt[SparseMatrixType], 'O', algo)
12+
@test A_sparse collect(dA_sparse)
13+
end
14+
end
15+
@testset "$SparseMatrixType -- sparsetodense algo=$algo" for algo in [rocSPARSE.rocsparse_sparse_to_dense_alg_default]
16+
@testset "sparsetodense $T" for T in [Float32, Float64, ComplexF32, ComplexF64]
17+
A_dense = rand(T, 10, 20)
18+
A_sparse = sparse(A_dense)
19+
dA_sparse = SparseMatrixType(A_sparse)
20+
dA_dense = rocSPARSE.sparsetodense(dA_sparse, 'O', algo)
21+
@test A_dense collect(dA_dense)
22+
end
23+
end
24+
end
25+
126
@testset "gather! $T" for T in [Float32, Float64, ComplexF32, ComplexF64]
227
X = sprand(T, 20, 0.5)
328
dX = ROCSparseVector{T}(X)

0 commit comments

Comments
 (0)