Skip to content
This repository was archived by the owner on May 15, 2025. It is now read-only.

Commit 8324047

Browse files
committed
Fix chunk size picking for StaticArrays
1 parent 98e6af3 commit 8324047

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

src/utils.jl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,23 @@ __standard_tag(::Nothing, x) = ForwardDiff.Tag(SimpleNonlinearSolveTag(), eltype
3939
__standard_tag(tag::ForwardDiff.Tag, _) = tag
4040
__standard_tag(tag, x) = ForwardDiff.Tag(tag, eltype(x))
4141

42+
__pick_forwarddiff_chunk(x) = ForwardDiff.Chunk(length(x))
43+
function __pick_forwarddiff_chunk(x::StaticArray)
44+
L = prod(Size(x))
45+
if L ForwardDiff.DEFAULT_CHUNK_THRESHOLD
46+
return ForwardDiff.Chunk{L}()
47+
else
48+
return ForwardDiff.Chunk{ForwardDiff.DEFAULT_CHUNK_THRESHOLD}()
49+
end
50+
end
51+
4252
function __get_jacobian_config(ad::AutoForwardDiff{CS}, f, x) where {CS}
43-
ck = (CS === nothing || CS 0) ? ForwardDiff.Chunk(length(x)) : ForwardDiff.Chunk{CS}()
53+
ck = (CS === nothing || CS 0) ? __pick_forwarddiff_chunk(x) : ForwardDiff.Chunk{CS}()
4454
tag = __standard_tag(ad.tag, x)
4555
return ForwardDiff.JacobianConfig(f, x, ck, tag)
4656
end
4757
function __get_jacobian_config(ad::AutoForwardDiff{CS}, f!, y, x) where {CS}
48-
ck = (CS === nothing || CS 0) ? ForwardDiff.Chunk(length(x)) : ForwardDiff.Chunk{CS}()
58+
ck = (CS === nothing || CS 0) ? __pick_forwarddiff_chunk(x) : ForwardDiff.Chunk{CS}()
4959
tag = __standard_tag(ad.tag, x)
5060
return ForwardDiff.JacobianConfig(f!, y, x, ck, tag)
5161
end

test/basictests.jl

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -258,11 +258,9 @@ end
258258
# --- Allocation Checks ---
259259

260260
## SimpleDFSane needs to allocate a history vector
261-
@testset "Allocation Checks: $(_nameof(alg))" for alg in (
262-
SimpleNewtonRaphson(; autodiff = AutoForwardDiff(; chunksize = 2)),
263-
SimpleHalley(; autodiff = AutoForwardDiff(; chunksize = 2)),
264-
SimpleBroyden(), SimpleKlement(), SimpleLimitedMemoryBroyden(),
265-
SimpleTrustRegion(; autodiff = AutoForwardDiff(; chunksize = 2)))
261+
@testset "Allocation Checks: $(_nameof(alg))" for alg in ( SimpleNewtonRaphson(),
262+
SimpleHalley(), SimpleBroyden(), SimpleKlement(), SimpleLimitedMemoryBroyden(),
263+
SimpleTrustRegion())
266264
@check_allocs nlsolve(prob, alg) = DiffEqBase.__solve(prob, alg; abstol = 1e-9)
267265

268266
nlprob_scalar = NonlinearProblem{false}(quadratic_f, 1.0, 2.0)

0 commit comments

Comments
 (0)