Skip to content

Commit 96670eb

Browse files
author
Stuart Daines
committed
sparse fast path only if J, sparsity share same sparsity pattern
Simplifies fast path logic. All other cases (J is not a SparseMatrixCSC, or J has a different sparsity pattern) use previous slow path.
1 parent c31eec3 commit 96670eb

File tree

2 files changed

+6
-16
lines changed

2 files changed

+6
-16
lines changed

src/iteration_utils.jl

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,10 @@ end
3232
_use_findstructralnz(sparsity) = ArrayInterface.has_sparsestruct(sparsity)
3333
_use_findstructralnz(::SparseMatrixCSC) = false
3434

35-
# test if J, sparsity are both SparseMatrixCSC and have the same size storage arrays,
36-
# if so, update J so they can share the same sparsity pattern
37-
_use_sparseCSC_common_sparsity!(J, sparsity) = false
38-
function _use_sparseCSC_common_sparsity!(J::SparseMatrixCSC, sparsity::SparseMatrixCSC)
39-
common_sparsity = (length(J.colptr) == length(sparsity.colptr) &&
40-
length(J.nzval) == length(sparsity.nzval))
41-
42-
if common_sparsity
43-
J.colptr .= sparsity.colptr
44-
J.rowval .= sparsity.rowval
45-
end
46-
47-
return common_sparsity
48-
end
35+
# test if J, sparsity are both SparseMatrixCSC and have the same sparsity pattern of stored values
36+
_use_sparseCSC_common_sparsity(J, sparsity) = false
37+
_use_sparseCSC_common_sparsity(J::SparseMatrixCSC, sparsity::SparseMatrixCSC) =
38+
((J.colptr == sparsity.colptr) && (J.rowval == sparsity.rowval))
4939

5040
function __init__()
5141
@require BlockBandedMatrices="ffab5731-97b5-5995-9138-79e8c1846df0" begin

src/jacobians.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,8 @@ function finite_difference_jacobian!(
348348
fill!(J,false)
349349
end
350350

351-
# fast path if J and sparsity are both SparseMatrixCSC and have the same number of columns and stored values
352-
sparseCSC_common_sparsity = _use_sparseCSC_common_sparsity!(J, sparsity)
351+
# fast path if J and sparsity are both SparseMatrixCSC and have the same sparsity pattern
352+
sparseCSC_common_sparsity = _use_sparseCSC_common_sparsity(J, sparsity)
353353

354354
if fdtype == Val(:forward)
355355
vfx1 = _vec(fx1)

0 commit comments

Comments
 (0)