Skip to content

Commit 467009c

Browse files
committed
actually avoid making allocations for inexact jacobian
1 parent 7efded5 commit 467009c

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

Project.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
99
ConcreteStructs = "2569d6c7-a4a2-43d3-a901-331e8e4be471"
1010
DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e"
1111
EnumX = "4e289a0a-7415-4d19-859d-a7e5c4648b56"
12+
FastBroadcast = "7034ab61-46d4-4ed7-9d0f-46aef9175898"
1213
FiniteDiff = "6a86dc24-6348-571c-b903-95158fe2bd41"
1314
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
1415
LineSearches = "d3d80556-e9d4-5f37-9878-2ab0fcc64255"
@@ -35,9 +36,8 @@ NonlinearSolveFastLevenbergMarquardtExt = "FastLevenbergMarquardt"
3536
NonlinearSolveLeastSquaresOptimExt = "LeastSquaresOptim"
3637

3738
[compat]
38-
BandedMatrices = "1"
3939
ADTypes = "0.2"
40-
ArrayInterface = "6.0.24, 7"
40+
BandedMatrices = "1"
4141
ConcreteStructs = "0.2"
4242
DiffEqBase = "6.130"
4343
EnumX = "1"
@@ -63,6 +63,7 @@ julia = "1.9"
6363
[extras]
6464
BandedMatrices = "aae01518-5342-5314-be14-df237901396f"
6565
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
66+
DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e"
6667
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9"
6768
FastLevenbergMarquardt = "7a0df574-e128-4d35-8cbd-3d84502bf7ce"
6869
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
@@ -79,7 +80,6 @@ StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
7980
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
8081
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
8182
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
82-
DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e"
8383

8484
[targets]
8585
test = ["Enzyme", "BenchmarkTools", "SafeTestsets", "Pkg", "Test", "ForwardDiff", "StaticArrays", "Symbolics", "LinearSolve", "Random", "LinearAlgebra", "Zygote", "SparseDiffTools", "NonlinearProblemLibrary", "LeastSquaresOptim", "FastLevenbergMarquardt", "NaNMath", "BandedMatrices", "DiffEqBase"]

src/NonlinearSolve.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ import PrecompileTools
99

1010
PrecompileTools.@recompile_invalidations begin
1111
using DiffEqBase, LinearAlgebra, LinearSolve, SparseArrays, SparseDiffTools
12+
using FastBroadcast: @.., True, False
1213
import ArrayInterface: restructure
1314

1415
import ADTypes: AbstractFiniteDifferencesMode
1516
import ArrayInterface: undefmatrix,
16-
matrix_colors, parameterless_type, ismutable, issingular
17+
matrix_colors, parameterless_type, ismutable, issingular,fast_scalar_indexing
1718
import ConcreteStructs: @concrete
1819
import EnumX: @enumx
1920
import ForwardDiff

src/pseudotransient.jl

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,19 @@ end
114114
function perform_step!(cache::PseudoTransientCache{true})
115115
@unpack u, u_prev, fu1, f, p, alg, J, linsolve, du, alpha, tc_storage = cache
116116
jacobian!!(J, cache)
117+
inv_alpha = inv(alpha)
118+
117119
if J isa SciMLBase.AbstractSciMLOperator
118-
J = J - (1 / alpha) * I
120+
J = J - inv_alpha * I
119121
else
120-
J .= J - (1 / alpha) * I
122+
idxs = diagind(J)
123+
if fast_scalar_indexing(J)
124+
@inbounds for i in axes(J, 1)
125+
J[i, i] = J[i, i] - inv_alpha
126+
end
127+
else
128+
@.. broadcast=false @view(J[idxs])=@view(J[idxs]) - inv_alpha
129+
end
121130
end
122131

123132
termination_condition = cache.termination_condition(tc_storage)
@@ -151,8 +160,9 @@ function perform_step!(cache::PseudoTransientCache{false})
151160
termination_condition = cache.termination_condition(tc_storage)
152161

153162
cache.J = jacobian!!(cache.J, cache)
163+
inv_alpha = inv(alpha)
154164

155-
cache.J = cache.J - (1 / alpha) * I
165+
cache.J = cache.J - inv_alpha * I
156166
# u = u - J \ fu
157167
if linsolve === nothing
158168
cache.du = fu1 / cache.J

0 commit comments

Comments
 (0)