Skip to content
This repository was archived by the owner on Aug 22, 2025. It is now read-only.

Commit bfe0f0a

Browse files
committed
optimize for BBB matrix
1 parent c1729f2 commit bfe0f0a

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

src/differentiation/compute_jacobian_ad.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,24 +102,24 @@ function forwarddiff_color_jacobian!(J::AbstractMatrix{<:Number},
102102
color_i = 1
103103
fill!(J, zero(eltype(J)))
104104

105+
if DiffEqDiffTools._use_findstructralnz(sparsity)
106+
rows_index, cols_index = ArrayInterface.findstructralnz(sparsity)
107+
else
108+
rows_index = nothing
109+
cols_index = nothing
110+
end
111+
112+
ncols=size(J,2)
113+
105114
for i in eachindex(p)
106115
partial_i = p[i]
107116
t .= Dual{typeof(f)}.(x, partial_i)
108117
f(fx,t)
109-
if ArrayInterface.has_sparsestruct(sparsity)
110-
rows_index, cols_index = ArrayInterface.findstructralnz(sparsity)
118+
if !(sparsity isa Nothing)
111119
for j in 1:chunksize
112120
dx .= partials.(fx, j)
113121
if ArrayInterface.fast_scalar_indexing(dx)
114-
for k in 1:length(cols_index)
115-
if colorvec[cols_index[k]] == color_i
116-
if J isa SparseMatrixCSC
117-
J.nzval[k] = dx[rows_index[k]]
118-
else
119-
J[rows_index[k],cols_index[k]] = dx[rows_index[k]]
120-
end
121-
end
122-
end
122+
DiffEqDiffTools._colorediteration!(J,sparsity,rows_index,cols_index,dx,colorvec,color_i,ncols)
123123
else
124124
#=
125125
J.nzval[rows_index] .+= (colorvec[cols_index] .== color_i) .* dx[rows_index]

0 commit comments

Comments
 (0)