Skip to content

Commit af18c81

Browse files
Merge pull request #61 from huanglangwen/optimize
Optimize for BandedBlockBanded Matrix
2 parents c1729f2 + 67880b0 commit af18c81

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
1717
VertexSafeGraphs = "19fa3120-7c27-5ec5-8db8-b0b0aa330d6f"
1818

1919
[compat]
20-
ArrayInterface = "1.1"
20+
ArrayInterface = ">= 1.1"
21+
DiffEqDiffTools = ">= 1.3"
2122
julia = "1"
2223

2324
[extras]

src/differentiation/compute_jacobian_ad.jl

Lines changed: 13 additions & 13 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]
@@ -134,12 +134,12 @@ function forwarddiff_color_jacobian!(J::AbstractMatrix{<:Number},
134134
end
135135
end
136136
color_i += 1
137-
(color_i > maximum(colorvec)) && continue
137+
(color_i > maximum(colorvec)) && return
138138
end
139139
else
140140
for j in 1:chunksize
141141
col_index = (i-1)*chunksize + j
142-
(col_index > maximum(colorvec)) && continue
142+
(col_index > maximum(colorvec)) && return
143143
J[:, col_index] .= partials.(fx, j)
144144
end
145145
end

test/test_ad.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ using SparseDiffTools
22
using ForwardDiff: Dual, jacobian
33
using SparseArrays, Test
44
using LinearAlgebra
5+
using BlockBandedMatrices
56

67
fcalls = 0
78
function f(dx,x)
@@ -61,3 +62,22 @@ forwarddiff_color_jacobian!(_denseJ1, f, x, jac_cache)
6162
_Jt = similar(Tridiagonal(J))
6263
forwarddiff_color_jacobian!(_Jt, f, x, colorvec = repeat(1:3,10), sparsity = _Jt)
6364
@test _Jt J
65+
66+
#https://github.com/JuliaDiffEq/DiffEqDiffTools.jl/issues/67#issuecomment-516871956
67+
function f(out, x)
68+
x = reshape(x, 100, 100)
69+
out = reshape(out, 100, 100)
70+
for i in 1:100
71+
for j in 1:100
72+
out[i, j] = x[i, j] + x[max(i -1, 1), j] + x[min(i+1, size(x, 1)), j] + x[i, max(j-1, 1)] + x[i, min(j+1, size(x, 2))]
73+
end
74+
end
75+
return vec(out)
76+
end
77+
x = rand(10000)
78+
J = BandedBlockBandedMatrix(Ones(10000, 10000), (fill(100, 100), fill(100, 100)), (1, 1), (1, 1))
79+
Jsparse = sparse(J)
80+
colors = matrix_colors(J)
81+
forwarddiff_color_jacobian!(J, f, x, colorvec=colors)
82+
forwarddiff_color_jacobian!(Jsparse, f, x, colorvec=colors)
83+
@test J Jsparse

0 commit comments

Comments
 (0)