Skip to content

Commit d29659e

Browse files
authored
feat: fast prep modification for in-place FiniteDiff (#734)
1 parent e541190 commit d29659e

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

DifferentiationInterface/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DifferentiationInterface"
22
uuid = "a0c0ee7d-e4b9-4e03-894e-1c5f64a51d63"
33
authors = ["Guillaume Dalle", "Adrian Hill"]
4-
version = "0.6.43"
4+
version = "0.6.44"
55

66
[deps]
77
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"

DifferentiationInterface/ext/DifferentiationInterfaceFiniteDiffExt/twoarg.jl

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,26 @@ function DI.prepare_derivative(
166166
return FiniteDiffTwoArgDerivativePrep(cache, relstep, absstep, dir)
167167
end
168168

169+
function DI.prepare!_derivative(
170+
f!,
171+
y,
172+
old_prep::FiniteDiffTwoArgDerivativePrep,
173+
backend::AutoFiniteDiff,
174+
x,
175+
contexts::Vararg{DI.Context,C},
176+
) where {C}
177+
if y isa Vector
178+
(; cache) = old_prep
179+
cache.fx isa Union{Number,Nothing} || resize!(cache.fx, length(y))
180+
cache.c1 isa Union{Number,Nothing} || resize!(cache.c1, length(y))
181+
cache.c2 isa Union{Number,Nothing} || resize!(cache.c2, length(y))
182+
cache.c3 isa Union{Number,Nothing} || resize!(cache.c3, length(y))
183+
return old_prep
184+
else
185+
return DI.prepare_derivative(f!, y, backend, x, contexts...)
186+
end
187+
end
188+
169189
function DI.value_and_derivative(
170190
f!,
171191
y,
@@ -257,6 +277,28 @@ function DI.prepare_jacobian(
257277
return FiniteDiffTwoArgJacobianPrep(cache, relstep, absstep, dir)
258278
end
259279

280+
function DI.prepare!_jacobian(
281+
f!,
282+
y,
283+
old_prep::FiniteDiffTwoArgJacobianPrep,
284+
backend::AutoFiniteDiff,
285+
x,
286+
contexts::Vararg{DI.Context,C},
287+
) where {C}
288+
if x isa Vector && y isa Vector
289+
(; cache) = old_prep
290+
cache.x1 isa Union{Number,Nothing} || resize!(cache.x1, length(x))
291+
cache.x2 isa Union{Number,Nothing} || resize!(cache.x2, length(x))
292+
cache.fx isa Union{Number,Nothing} || resize!(cache.fx, length(y))
293+
cache.fx1 isa Union{Number,Nothing} || resize!(cache.fx1, length(y))
294+
cache.colorvec = 1:length(x)
295+
cache.sparsity = nothing
296+
return old_prep
297+
else
298+
return DI.prepare_jacobian(f!, y, backend, x, contexts...)
299+
end
300+
end
301+
260302
function DI.value_and_jacobian(
261303
f!,
262304
y,

0 commit comments

Comments
 (0)