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

improve performance for sorting columns in sparse matrix (thereby improving sparse matmul) #29682

Merged
merged 1 commit into from
Oct 19, 2018
Merged
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
40 changes: 25 additions & 15 deletions stdlib/SparseArrays/src/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3401,37 +3401,47 @@ function sortSparseMatrixCSC!(A::SparseMatrixCSC{Tv,Ti}; sortindices::Symbol = :
row = zeros(Ti, m)
val = zeros(Tv, m)

for i = 1:n
@inbounds col_start = colptr[i]
@inbounds col_end = (colptr[i+1] - 1)
perm = Base.Perm(Base.ord(isless, identity, false, Base.Order.Forward), row)

numrows = col_end - col_start + 1
@inbounds for i = 1:n
nzr = nzrange(A, i)
numrows = length(nzr)
if numrows <= 1
continue
elseif numrows == 2
f = col_start
f = first(nzr)
s = f+1
if rowval[f] > rowval[s]
@inbounds rowval[f], rowval[s] = rowval[s], rowval[f]
@inbounds nzval[f], nzval[s] = nzval[s], nzval[f]
rowval[f], rowval[s] = rowval[s], rowval[f]
nzval[f], nzval[s] = nzval[s], nzval[f]
end
continue
end
resize!(row, numrows)
resize!(index, numrows)

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

sortperm!(unsafe_wrap(Vector{Ti}, pointer(index), numrows),
unsafe_wrap(Vector{Ti}, pointer(row), numrows))
if numrows <= 16
alg = Base.Sort.InsertionSort
else
alg = Base.Sort.QuickSort
end

# Reset permutation
index .= 1:numrows

sort!(index, alg, perm)

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