Skip to content
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

Sort SparseCSC elements function and speed up sparse matrix multiplication #8316

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion base/linalg.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module LinAlg

importall Base
import Base: USE_BLAS64, size, copy, copy_transpose!, power_by_squaring, print_matrix, transpose!
import Base: USE_BLAS64, size, copy, copy_transpose!, power_by_squaring, print_matrix, transpose!, sortCSC!

export
# Modules
Expand Down
6 changes: 3 additions & 3 deletions base/linalg/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ function *{Tv,Ti}(A::SparseMatrixCSC{Tv,Ti}, B::SparseMatrixCSC{Tv,Ti})
deleteat!(nzvalC, colptrC[end]:length(nzvalC))

# The Gustavson algorithm does not guarantee the product to have sorted row indices.
Cunsorted = SparseMatrixCSC(mA, nB, colptrC, rowvalC, nzvalC)
Ct = Cunsorted.'
Ctt = Base.SparseMatrix.transpose!(Ct, SparseMatrixCSC(mA, nB, colptrC, rowvalC, nzvalC))
C = SparseMatrixCSC(mA, nB, colptrC, rowvalC, nzvalC)
sortCSC!(C)
return C
end

## solvers
Expand Down
2 changes: 1 addition & 1 deletion base/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Base.NonTupleType, Base.float, Base.Order, Base.Sort.Forward
export SparseMatrixCSC,
blkdiag, dense, diag, diagm, droptol!, dropzeros!, etree, full,
getindex, ishermitian, issparse, issym, istril, istriu, nnz,
setindex!, sparse, sparsevec, spdiagm, speye, spones,
setindex!, sortCSC!, sparse, sparsevec, spdiagm, speye, spones,
sprand, sprandbool, sprandn, spzeros, symperm, trace, tril, tril!,
triu, triu!, nonzeros

Expand Down
40 changes: 40 additions & 0 deletions base/sparse/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,46 @@ function full{Tv}(S::SparseMatrixCSC{Tv})
return A
end

function sortCSC!{Tv,Ti}(A::SparseMatrixCSC{Tv,Ti})
m, n = size(A)

colptr = A.colptr; rowval = A.rowval; nzval = A.nzval

# Crude estimate for a preallocation
rows = min(int(((A.colptr[end] - 1)/A.n)*2), m)
row = Array(Ti, rows)
val = Array(Tv, rows)

for i = 1:n
@inbounds col_start = colptr[i]
@inbounds col_end = (colptr[i+1] - 1)

numrows = int((col_end - col_start) + 1)
if numrows > rows
rows = numrows
row = Array(Ti, numrows)
val = Array(Tv, numrows)
end

jj = 1
@simd for j = col_start:col_end
@inbounds row[jj] = rowval[j]
@inbounds val[jj] = nzval[j]
jj += 1
end

index = sortperm(row[1:numrows])

jj = 1;
@simd for j = col_start:col_end
@inbounds rowval[j] = row[index[jj]]
@inbounds nzval[j] = val[index[jj]]
jj += 1
end
end
end


float(S::SparseMatrixCSC) = SparseMatrixCSC(S.m, S.n, copy(S.colptr), copy(S.rowval), float(copy(S.nzval)))

complex(S::SparseMatrixCSC) = SparseMatrixCSC(S.m, S.n, copy(S.colptr), copy(S.rowval), complex(copy(S.nzval)))
Expand Down