Skip to content

Commit

Permalink
Fix chunk size picking for StaticArrays
Browse files Browse the repository at this point in the history
  • Loading branch information
avik-pal committed Nov 27, 2023
1 parent 98e6af3 commit 8324047
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
14 changes: 12 additions & 2 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,23 @@ __standard_tag(::Nothing, x) = ForwardDiff.Tag(SimpleNonlinearSolveTag(), eltype
__standard_tag(tag::ForwardDiff.Tag, _) = tag
__standard_tag(tag, x) = ForwardDiff.Tag(tag, eltype(x))

Check warning on line 40 in src/utils.jl

View check run for this annotation

Codecov / codecov/patch

src/utils.jl#L38-L40

Added lines #L38 - L40 were not covered by tests

__pick_forwarddiff_chunk(x) = ForwardDiff.Chunk(length(x))
function __pick_forwarddiff_chunk(x::StaticArray)
L = prod(Size(x))
if L ForwardDiff.DEFAULT_CHUNK_THRESHOLD
return ForwardDiff.Chunk{L}()

Check warning on line 46 in src/utils.jl

View check run for this annotation

Codecov / codecov/patch

src/utils.jl#L42-L46

Added lines #L42 - L46 were not covered by tests
else
return ForwardDiff.Chunk{ForwardDiff.DEFAULT_CHUNK_THRESHOLD}()

Check warning on line 48 in src/utils.jl

View check run for this annotation

Codecov / codecov/patch

src/utils.jl#L48

Added line #L48 was not covered by tests
end
end

function __get_jacobian_config(ad::AutoForwardDiff{CS}, f, x) where {CS}
ck = (CS === nothing || CS 0) ? ForwardDiff.Chunk(length(x)) : ForwardDiff.Chunk{CS}()
ck = (CS === nothing || CS 0) ? __pick_forwarddiff_chunk(x) : ForwardDiff.Chunk{CS}()
tag = __standard_tag(ad.tag, x)
return ForwardDiff.JacobianConfig(f, x, ck, tag)

Check warning on line 55 in src/utils.jl

View check run for this annotation

Codecov / codecov/patch

src/utils.jl#L52-L55

Added lines #L52 - L55 were not covered by tests
end
function __get_jacobian_config(ad::AutoForwardDiff{CS}, f!, y, x) where {CS}
ck = (CS === nothing || CS 0) ? ForwardDiff.Chunk(length(x)) : ForwardDiff.Chunk{CS}()
ck = (CS === nothing || CS 0) ? __pick_forwarddiff_chunk(x) : ForwardDiff.Chunk{CS}()
tag = __standard_tag(ad.tag, x)
return ForwardDiff.JacobianConfig(f!, y, x, ck, tag)

Check warning on line 60 in src/utils.jl

View check run for this annotation

Codecov / codecov/patch

src/utils.jl#L57-L60

Added lines #L57 - L60 were not covered by tests
end
Expand Down
8 changes: 3 additions & 5 deletions test/basictests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -258,11 +258,9 @@ end
# --- Allocation Checks ---

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

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

0 comments on commit 8324047

Please sign in to comment.