Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Squeezing out a few allocations #18

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/AutoDiff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ function _solve_jacobian_θ(mcp::MixedComplementarityProblems.PrimalDualMCP, sol
(; x, y, s, ϵ) = solution

∇F_z = let
∇F = MixedComplementarityProblems.get_result_buffer(mcp.∇F_z!)
∇F = mcp.∇F_z!.result_buffer
mcp.∇F_z!(∇F, x, y, s; θ, ϵ)
∇F
end

∇F_θ = let
∇F = MixedComplementarityProblems.get_result_buffer(mcp.∇F_θ!)
∇F = mcp.∇F_θ!.result_buffer
mcp.∇F_θ!(∇F, x, y, s; θ, ϵ)
∇F
end
Expand Down
14 changes: 7 additions & 7 deletions src/solver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function solve(
max_outer_iters = 50,
)
# Set up common memory.
∇F = get_result_buffer(mcp.∇F_z!)
∇F = mcp.∇F_z!.result_buffer
F = zeros(mcp.unconstrained_dimension + 2mcp.constrained_dimension)
δz = zeros(mcp.unconstrained_dimension + 2mcp.constrained_dimension)
δx = @view δz[1:(mcp.unconstrained_dimension)]
Expand All @@ -52,7 +52,7 @@ function solve(
# TODO! Can add some adaptive regularization.
mcp.F!(F, x, y, s; θ, ϵ)
mcp.∇F_z!(∇F, x, y, s; θ, ϵ)
δz .= -∇F \ F
δz .= (∇F \ F) .* -1

# Fraction to the boundary linesearch.
α_s = fraction_to_the_boundary_linesearch(s, δs; tol)
Expand All @@ -65,11 +65,11 @@ function solve(
end

# Update variables accordingly.
x += α_s * δx
s += α_s * δs
y += α_y * δy
@. x += α_s * δx
@. s += α_s * δs
@. y += α_y * δy

kkt_error = maximum(abs.(F))
kkt_error = norm(F, Inf)
inner_iters += 1
end

Expand All @@ -89,7 +89,7 @@ end
"""
function fraction_to_the_boundary_linesearch(v, δ; τ = 0.995, decay = 0.5, tol = 1e-4)
α = 1.0
while any(v + α * δ .< (1 - τ) * v)
while any(@. v + α * δ < (1 - τ) * v)
if α < tol
return NaN
end
Expand Down
Loading