-
Notifications
You must be signed in to change notification settings - Fork 14
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
Remove allocations in callbacks #230
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
611f4a2
update interface between sparse KKT systems and callbacks
frapac 0803480
add PrimalVector type to store primal information
frapac b4c0eeb
fix tests
frapac 5e769b1
Merge remote-tracking branch 'origin/master' into fp/alloc_callbacks
frapac dbef23c
fix tests on GPU
frapac 22cea3f
Merge branch 'master' into fp/alloc_callbacks
sshin23 bf9bd28
Merge remote-tracking branch 'origin/master' into fp/alloc_callbacks
sshin23 ab6886f
merged with master & improvment
sshin23 861b8d0
address comments
frapac 3f10339
typo fix
sshin23 77cf8b1
merge
sshin23 ba3544c
typo fix
sshin23 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
scale = is_resto ? zero(T) : get_minimize(nlp) ? one(T) : -one(T)