@@ -264,6 +264,7 @@ function forwarddiff_color_jacobian!(J::AbstractMatrix{<:Number},
264
264
chunksize = jac_cache. chunksize
265
265
color_i = 1
266
266
maxcolor = maximum (colorvec)
267
+
267
268
fill! (J, zero (eltype (J)))
268
269
269
270
if FiniteDiff. _use_findstructralnz (sparsity)
@@ -273,6 +274,9 @@ function forwarddiff_color_jacobian!(J::AbstractMatrix{<:Number},
273
274
cols_index = 1 : size (J,2 )
274
275
end
275
276
277
+ # fast path if J and sparsity are both SparseMatrixCSC and have the same sparsity pattern
278
+ sparseCSC_common_sparsity = FiniteDiff. _use_sparseCSC_common_sparsity (J, sparsity)
279
+
276
280
vecx = vec (x)
277
281
vect = vec (t)
278
282
vecfx= vec (fx)
@@ -287,9 +291,14 @@ function forwarddiff_color_jacobian!(J::AbstractMatrix{<:Number},
287
291
if ! (sparsity isa Nothing)
288
292
for j in 1 : chunksize
289
293
dx .= partials .(fx, j)
294
+
290
295
if ArrayInterface. fast_scalar_indexing (dx)
291
296
# dx is implicitly used in vecdx
292
- FiniteDiff. _colorediteration! (J,sparsity,rows_index,cols_index,vecdx,colorvec,color_i,ncols)
297
+ if sparseCSC_common_sparsity
298
+ FiniteDiff. _colorediteration! (J,vecdx,colorvec,color_i,ncols)
299
+ else
300
+ FiniteDiff. _colorediteration! (J,sparsity,rows_index,cols_index,vecdx,colorvec,color_i,ncols)
301
+ end
293
302
else
294
303
#=
295
304
J.nzval[rows_index] .+= (colorvec[cols_index] .== color_i) .* dx[rows_index]
@@ -316,3 +325,6 @@ function forwarddiff_color_jacobian!(J::AbstractMatrix{<:Number},
316
325
end
317
326
return J
318
327
end
328
+
329
+
330
+
0 commit comments