Skip to content

Commit cbce39f

Browse files
authored
Get rid of inefficient findnz (#111)
* Get rid of inefficient `findnz` * Fix
1 parent e26edfe commit cbce39f

2 files changed

Lines changed: 30 additions & 22 deletions

File tree

src/SparseMatrixColorings.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ using SparseArrays:
3535
SparseMatrixCSC,
3636
dropzeros,
3737
dropzeros!,
38-
findnz,
3938
nnz,
4039
nonzeros,
4140
nzrange,

src/result.jl

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,15 @@ end
106106
function ColumnColoringResult(S::SparseMatrixCSC, color::Vector{Int})
107107
group = group_by_color(color)
108108
n = size(S, 1)
109-
I, J, _ = findnz(S)
109+
rv = rowvals(S)
110110
compressed_indices = zeros(Int, nnz(S))
111-
for k in eachindex(I, J, compressed_indices)
112-
i, j = I[k], J[k]
113-
c = color[j]
114-
# A[i, j] = B[i, c]
115-
compressed_indices[k] = (c - 1) * n + i
111+
for j in axes(S, 2)
112+
for k in nzrange(S, j)
113+
i = rv[k]
114+
c = color[j]
115+
# A[i, j] = B[i, c]
116+
compressed_indices[k] = (c - 1) * n + i
117+
end
116118
end
117119
return ColumnColoringResult(S, color, group, compressed_indices)
118120
end
@@ -144,13 +146,15 @@ function RowColoringResult(S::SparseMatrixCSC, color::Vector{Int})
144146
Sᵀ = sparse(transpose(S))
145147
group = group_by_color(color)
146148
C = length(group) # ncolors
147-
I, J, _ = findnz(S)
149+
rv = rowvals(S)
148150
compressed_indices = zeros(Int, nnz(S))
149-
for k in eachindex(I, J, compressed_indices)
150-
i, j = I[k], J[k]
151-
c = color[i]
152-
# A[i, j] = B[c, j]
153-
compressed_indices[k] = (j - 1) * C + c
151+
for j in axes(S, 2)
152+
for k in nzrange(S, j)
153+
i = rv[k]
154+
c = color[i]
155+
# A[i, j] = B[c, j]
156+
compressed_indices[k] = (j - 1) * C + c
157+
end
154158
end
155159
return RowColoringResult(S, Sᵀ, color, group, compressed_indices)
156160
end
@@ -181,13 +185,15 @@ end
181185
function StarSetColoringResult(S::SparseMatrixCSC, color::Vector{Int}, star_set::StarSet)
182186
group = group_by_color(color)
183187
n = size(S, 1)
184-
I, J, _ = findnz(S)
188+
rv = rowvals(S)
185189
compressed_indices = zeros(Int, nnz(S))
186-
for k in eachindex(I, J, compressed_indices)
187-
i, j = I[k], J[k]
188-
l, c = symmetric_coefficient(i, j, color, star_set)
189-
# A[i, j] = B[l, c]
190-
compressed_indices[k] = (c - 1) * n + l
190+
for j in axes(S, 2)
191+
for k in nzrange(S, j)
192+
i = rv[k]
193+
l, c = symmetric_coefficient(i, j, color, star_set)
194+
# A[i, j] = B[l, c]
195+
compressed_indices[k] = (c - 1) * n + l
196+
end
191197
end
192198
return StarSetColoringResult(S, color, group, star_set, compressed_indices)
193199
end
@@ -370,15 +376,18 @@ function LinearSystemColoringResult(
370376
) where {R}
371377
group = group_by_color(color)
372378
C = length(group) # ncolors
379+
rv = rowvals(S)
373380

374381
# build T such that T * strict_upper_nonzeros(A) = B
375382
# and solve a linear least-squares problem
376383
# only consider the strict upper triangle of A because of symmetry
377384
n = checksquare(S)
378385
strict_upper_nonzero_inds = Tuple{Int,Int}[]
379-
I, J, _ = findnz(S)
380-
for (i, j) in zip(I, J)
381-
(i < j) && push!(strict_upper_nonzero_inds, (i, j))
386+
for j in axes(S, 2)
387+
for k in nzrange(S, j)
388+
i = rv[k]
389+
(i < j) && push!(strict_upper_nonzero_inds, (i, j))
390+
end
382391
end
383392

384393
T = spzeros(float(R), n * C, length(strict_upper_nonzero_inds))

0 commit comments

Comments
 (0)