Skip to content

Commit

Permalink
Merge pull request #393 from SciML/ap/tstable_findmin
Browse files Browse the repository at this point in the history
Make `__findmin` type stable
  • Loading branch information
avik-pal authored Mar 23, 2024
2 parents 3925219 + e6ff3aa commit 28edcd4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "NonlinearSolve"
uuid = "8913a72c-1f9b-4ce2-8d82-65094dcecaec"
authors = ["SciML"]
version = "3.8.1"
version = "3.8.2"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand Down
27 changes: 20 additions & 7 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,29 @@ LazyArrays.applied_axes(::typeof(__zero), x) = axes(x)
@inline __is_complex(::Type{Complex}) = true
@inline __is_complex(::Type{T}) where {T} = false

@inline __findmin_caches(f, caches) = __findmin(f get_fu, caches)
@inline __findmin_caches(f::F, caches) where {F} = __findmin(f get_fu, caches)
# FIXME: DEFAULT_NORM makes an Array of NaNs not a NaN (atleast according to `isnan`)
@inline __findmin(::typeof(DEFAULT_NORM), x) = __findmin(Base.Fix1(maximum, abs), x)
@inline function __findmin(f, x)
@generated function __findmin(f::F, x) where {F}
# JET shows dynamic dispatch if this is not written as a generated function
if F === typeof(DEFAULT_NORM)
return :(return __findmin_impl(Base.Fix1(maximum, abs), x))
end
return :(return __findmin_impl(f, x))
end
@inline @views function __findmin_impl(f::F, x) where {F}
idx = findfirst(Base.Fix2(!==, nothing), x)
# This is an internal function so we assume that inputs are consistent and there is
# atleast one non-`nothing` value
fx_idx = f(x[idx])
idx == length(x) && return fx_idx, idx
fmin = @closure xᵢ -> begin
xᵢ === nothing && return Inf
xᵢ === nothing && return oftype(fx_idx, Inf)
fx = f(xᵢ)
return ifelse(isnan(fx), Inf, fx)
return ifelse(isnan(fx), oftype(fx, Inf), fx)
end
return findmin(fmin, x)
x_min, x_min_idx = findmin(fmin, x[(idx + 1):length(x)])
x_min < fx_idx && return x_min, x_min_idx + idx
return fx_idx, idx
end

@inline __can_setindex(x) = can_setindex(x)
Expand All @@ -130,7 +143,7 @@ Statistics from the nonlinear equation solver about the solution process.
- nf: Number of function evaluations.
- njacs: Number of Jacobians created during the solve.
- nfactors: Number of factorzations of the jacobian required for the solve.
- nsolve: Number of linear solves `W\b` required for the solve.
- nsolve: Number of linear solves `W \\ b` required for the solve.
- nsteps: Total number of iterations for the nonlinear solver.
"""
struct ImmutableNLStats
Expand Down

2 comments on commit 28edcd4

@avik-pal
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/103503

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v3.8.2 -m "<description of version>" 28edcd4901aca7e2155c6ed1e95dfcbc232f81cc
git push origin v3.8.2

Please sign in to comment.