-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove allocations in callbacks (#230)
* update interface between sparse KKT systems and callbacks * add PrimalVector type to store primal information - split slack from variable explicitly - remove all allocations in callbacks * fix tests * fix tests on GPU * address comments * typo fix * typo fix Co-authored-by: Sungho Shin <sshin@anl.gov>
- Loading branch information
Showing
12 changed files
with
511 additions
and
262 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,128 +1,137 @@ | ||
function eval_f_wrapper(solver::MadNLPSolver, x::Vector{T}) where T | ||
function eval_f_wrapper(solver::MadNLPSolver, x::PrimalVector{T}) where T | ||
nlp = solver.nlp | ||
cnt = solver.cnt | ||
@trace(solver.logger,"Evaluating objective.") | ||
x_nlpmodel = _madnlp_unsafe_wrap(x, get_nvar(nlp)) | ||
cnt.eval_function_time += @elapsed obj_val = (get_minimize(nlp) ? 1. : -1.) * obj(nlp,x_nlpmodel) | ||
cnt.obj_cnt+=1 | ||
cnt.obj_cnt==1 && (is_valid(obj_val) || throw(InvalidNumberException(:obj))) | ||
return obj_val*solver.obj_scale[] | ||
cnt.eval_function_time += @elapsed begin | ||
sense = (get_minimize(nlp) ? one(T) : -one(T)) | ||
obj_val = sense * obj(nlp, variable(x)) | ||
end | ||
cnt.obj_cnt += 1 | ||
if cnt.obj_cnt == 1 && !is_valid(obj_val) | ||
throw(InvalidNumberException(:obj)) | ||
end | ||
return obj_val * solver.obj_scale[] | ||
end | ||
|
||
function eval_grad_f_wrapper!(solver::MadNLPSolver, f::Vector{T},x::Vector{T}) where T | ||
function eval_grad_f_wrapper!(solver::MadNLPSolver, f::PrimalVector{T}, x::PrimalVector{T}) where T | ||
nlp = solver.nlp | ||
cnt = solver.cnt | ||
@trace(solver.logger,"Evaluating objective gradient.") | ||
obj_scaling = solver.obj_scale[] * (get_minimize(nlp) ? one(T) : -one(T)) | ||
x_nlpmodel = _madnlp_unsafe_wrap(x, get_nvar(nlp)) | ||
f_nlpmodel = _madnlp_unsafe_wrap(f, get_nvar(nlp)) | ||
cnt.eval_function_time += @elapsed grad!( | ||
nlp, | ||
x_nlpmodel, | ||
f_nlpmodel | ||
variable(x), | ||
variable(f), | ||
) | ||
_scal!(obj_scaling, f) | ||
_scal!(obj_scaling, full(f)) | ||
cnt.obj_grad_cnt+=1 | ||
cnt.obj_grad_cnt==1 && (is_valid(f) || throw(InvalidNumberException(:grad))) | ||
if cnt.obj_grad_cnt == 1 && !is_valid(full(f)) | ||
throw(InvalidNumberException(:grad)) | ||
end | ||
return f | ||
end | ||
|
||
function eval_cons_wrapper!(solver::MadNLPSolver, c::Vector{T},x::Vector{T}) where T | ||
function eval_cons_wrapper!(solver::MadNLPSolver, c::Vector{T}, x::PrimalVector{T}) where T | ||
nlp = solver.nlp | ||
cnt = solver.cnt | ||
@trace(solver.logger, "Evaluating constraints.") | ||
x_nlpmodel = _madnlp_unsafe_wrap(x, get_nvar(nlp)) | ||
c_nlpmodel = _madnlp_unsafe_wrap(c, get_ncon(nlp)) | ||
cnt.eval_function_time += @elapsed cons!( | ||
nlp, | ||
x_nlpmodel, | ||
c_nlpmodel | ||
variable(x), | ||
c, | ||
) | ||
view(c,solver.ind_ineq).-=view(x,get_nvar(nlp)+1:solver.n) | ||
view(c,solver.ind_ineq) .-= slack(x) | ||
c .-= solver.rhs | ||
c .*= solver.con_scale | ||
cnt.con_cnt+=1 | ||
cnt.con_cnt==2 && (is_valid(c) || throw(InvalidNumberException(:cons))) | ||
if cnt.con_cnt == 1 && !is_valid(c) | ||
throw(InvalidNumberException(:cons)) | ||
end | ||
return c | ||
end | ||
|
||
function eval_jac_wrapper!(solver::MadNLPSolver, kkt::AbstractKKTSystem, x::Vector{T}) where T | ||
function eval_jac_wrapper!(solver::MadNLPSolver, kkt::AbstractKKTSystem, x::PrimalVector{T}) where T | ||
nlp = solver.nlp | ||
cnt = solver.cnt | ||
ns = length(solver.ind_ineq) | ||
@trace(solver.logger, "Evaluating constraint Jacobian.") | ||
jac = get_jacobian(kkt) | ||
x_nlpmodel = _madnlp_unsafe_wrap(x, get_nvar(nlp)) | ||
jac_nlpmodel = _madnlp_unsafe_wrap(jac, get_nnzj(nlp.meta)) | ||
cnt.eval_function_time += @elapsed jac_coord!( | ||
nlp, | ||
x_nlpmodel, | ||
jac_nlpmodel | ||
variable(x), | ||
jac, | ||
) | ||
compress_jacobian!(kkt) | ||
cnt.con_jac_cnt+=1 | ||
cnt.con_jac_cnt==1 && (is_valid(jac) || throw(InvalidNumberException(:jac))) | ||
cnt.con_jac_cnt += 1 | ||
if cnt.con_jac_cnt == 1 && !is_valid(jac) | ||
throw(InvalidNumberException(:jac)) | ||
end | ||
@trace(solver.logger,"Constraint jacobian evaluation started.") | ||
return jac | ||
end | ||
|
||
function eval_lag_hess_wrapper!(solver::MadNLPSolver, kkt::AbstractKKTSystem, x::Vector{T},l::Vector{T};is_resto=false) where T | ||
function eval_lag_hess_wrapper!(solver::MadNLPSolver, kkt::AbstractKKTSystem, x::PrimalVector{T},l::Vector{T};is_resto=false) where T | ||
nlp = solver.nlp | ||
cnt = solver.cnt | ||
@trace(solver.logger,"Evaluating Lagrangian Hessian.") | ||
dual(solver._w1) .= l.*solver.con_scale | ||
dual(solver._w1) .= l .* solver.con_scale | ||
hess = get_hessian(kkt) | ||
x_nlpmodel = _madnlp_unsafe_wrap(x, get_nvar(nlp)) | ||
hess_nlpmodel = _madnlp_unsafe_wrap(hess, get_nnzh(nlp.meta)) | ||
scale = (get_minimize(nlp) ? one(T) : -one(T)) | ||
scale *= (is_resto ? zero(T) : solver.obj_scale[]) | ||
cnt.eval_function_time += @elapsed hess_coord!( | ||
nlp, | ||
x_nlpmodel, | ||
variable(x), | ||
dual(solver._w1), | ||
hess_nlpmodel; | ||
obj_weight = (get_minimize(nlp) ? 1. : -1.) * (is_resto ? 0.0 : solver.obj_scale[]) | ||
hess; | ||
obj_weight = scale, | ||
) | ||
compress_hessian!(kkt) | ||
cnt.lag_hess_cnt+=1 | ||
cnt.lag_hess_cnt==1 && (is_valid(hess) || throw(InvalidNumberException(:hess))) | ||
cnt.lag_hess_cnt += 1 | ||
if cnt.lag_hess_cnt == 1 && !is_valid(hess) | ||
throw(InvalidNumberException(:hess)) | ||
end | ||
return hess | ||
end | ||
|
||
function eval_jac_wrapper!(solver::MadNLPSolver, kkt::AbstractDenseKKTSystem, x::Vector{T}) where T | ||
function eval_jac_wrapper!(solver::MadNLPSolver, kkt::AbstractDenseKKTSystem, x::PrimalVector{T}) where T | ||
nlp = solver.nlp | ||
cnt = solver.cnt | ||
ns = length(solver.ind_ineq) | ||
@trace(solver.logger, "Evaluating constraint Jacobian.") | ||
jac = get_jacobian(kkt) | ||
x_nlpmodel = _madnlp_unsafe_wrap(x, get_nvar(nlp)) | ||
cnt.eval_function_time += @elapsed jac_dense!( | ||
nlp, | ||
x_nlpmodel, | ||
jac | ||
variable(x), | ||
jac, | ||
) | ||
compress_jacobian!(kkt) | ||
cnt.con_jac_cnt+=1 | ||
cnt.con_jac_cnt==1 && (is_valid(jac) || throw(InvalidNumberException(:jac))) | ||
if cnt.con_jac_cnt == 1 && !is_valid(jac) | ||
throw(InvalidNumberException(:jac)) | ||
end | ||
@trace(solver.logger,"Constraint jacobian evaluation started.") | ||
return jac | ||
end | ||
|
||
function eval_lag_hess_wrapper!(solver::MadNLPSolver, kkt::AbstractDenseKKTSystem, x::Vector{T},l::Vector{T};is_resto=false) where T | ||
function eval_lag_hess_wrapper!(solver::MadNLPSolver, kkt::AbstractDenseKKTSystem, x::PrimalVector{T},l::Vector{T};is_resto=false) where T | ||
nlp = solver.nlp | ||
cnt = solver.cnt | ||
@trace(solver.logger,"Evaluating Lagrangian Hessian.") | ||
dual(solver._w1) .= l.*solver.con_scale | ||
dual(solver._w1) .= l .* solver.con_scale | ||
hess = get_hessian(kkt) | ||
x_nlpmodel = _madnlp_unsafe_wrap(x, get_nvar(nlp)) | ||
scale = is_resto ? zero(T) : get_minimize(nlp) ? solver.obj_scale[] : -solver.obj_scale[] | ||
cnt.eval_function_time += @elapsed hess_dense!( | ||
nlp, | ||
x_nlpmodel, | ||
variable(x), | ||
dual(solver._w1), | ||
hess; | ||
obj_weight = (get_minimize(nlp) ? 1. : -1.) * (is_resto ? 0.0 : solver.obj_scale[]) | ||
obj_weight = scale, | ||
) | ||
compress_hessian!(kkt) | ||
cnt.lag_hess_cnt+=1 | ||
cnt.lag_hess_cnt==1 && (is_valid(hess) || throw(InvalidNumberException(:hess))) | ||
if cnt.lag_hess_cnt == 1 && !is_valid(hess) | ||
throw(InvalidNumberException(:hess)) | ||
end | ||
return hess | ||
end | ||
|
Oops, something went wrong.