Skip to content

Commit 6f30078

Browse files
vectorized partial decompression
add a comment to the very non-obvious vectorized code
1 parent 4944ad1 commit 6f30078

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/differentiation/compute_jacobian_ad.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ function forwarddiff_color_jacobian!(J::AbstractMatrix{<:Number},
9494
color = jac_cache.color
9595
color_i = 1
9696
chunksize = length(first(first(jac_cache.p)))
97+
fill!(J,zero(eltype(J)))
9798

9899
for i in 1:length(p)
99100
partial_i = p[i]
@@ -103,11 +104,20 @@ function forwarddiff_color_jacobian!(J::AbstractMatrix{<:Number},
103104
rows_index, cols_index, val = findnz(J)
104105
for j in 1:chunksize
105106
dx .= partials.(fx, j)
107+
#=
106108
for k in 1:length(cols_index)
107109
if color[cols_index[k]] == color_i
108110
J[rows_index[k], cols_index[k]] = dx[rows_index[k]]
109111
end
110112
end
113+
114+
or
115+
116+
J[rows_index, cols_index] .+= (color[cols_index] .== color_i) .* dx[rows_index]
117+
118+
+= means requires a zero'd out start
119+
=#
120+
@. setindex!((J,),getindex((J,),rows_index, cols_index) + (getindex((color,),cols_index) == color_i) * getindex((dx,),rows_index),rows_index, cols_index)
111121
color_i += 1
112122
(color_i > maximum(color)) && continue
113123
end

0 commit comments

Comments
 (0)