Skip to content

Commit cb98574

Browse files
committed
added: reduce TTFX of NonLinMPC and MovingHorizonEstimator constructors
I introduced a new function like wrapper, `NoTypeFunction`, to hide the concrete types of closures created for the evaluation of the objective function and its derivatives.
1 parent e7420db commit cb98574

3 files changed

Lines changed: 41 additions & 6 deletions

File tree

‎src/controller/nonlinmpc.jl‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -943,10 +943,20 @@ function get_nonlinobj_op(mpc::NonLinMPC, optim::JuMP.GenericModel{JNT}) where J
943943
return fill_diffstructure!(∇²J_arg, ∇²J, ∇²J_structure)
944944
end
945945
end
946-
if !isnothing(hess)
947-
@operator(optim, J_op, nZ̃, J_func, ∇J_func!, ∇²J_func!)
946+
J_op = if !isnothing(hess)
947+
JuMP.add_nonlinear_operator(
948+
optim, nZ̃,
949+
NoTypeFunction(J_func),
950+
NoTypeFunction(∇J_func!),
951+
NoTypeFunction(∇²J_func!);
952+
name = :J_op)
948953
else
949-
@operator(optim, J_op, nZ̃, J_func, ∇J_func!)
954+
JuMP.add_nonlinear_operator(
955+
optim,
956+
nZ̃,
957+
NoTypeFunction(J_func),
958+
NoTypeFunction(∇J_func!);
959+
name = :J_op)
950960
end
951961
return J_op
952962
end

‎src/estimator/mhe/construct.jl‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,10 +1551,20 @@ function get_nonlinobj_op(
15511551
update_objective!(J, ∇J, ∇²J, Z̃_J, Z̃_arg)
15521552
return fill_diffstructure!(∇²J_arg, ∇²J, ∇²J_structure)
15531553
end
1554-
if !isnothing(hess)
1555-
@operator(optim, J_op, nZ̃, J_func, ∇J_func!, ∇²J_func!)
1554+
J_op = if !isnothing(hess)
1555+
JuMP.add_nonlinear_operator(
1556+
optim, nZ̃,
1557+
NoTypeFunction(J_func),
1558+
NoTypeFunction(∇J_func!),
1559+
NoTypeFunction(∇²J_func!);
1560+
name = :J_op)
15561561
else
1557-
@operator(optim, J_op, nZ̃, J_func, ∇J_func!)
1562+
JuMP.add_nonlinear_operator(
1563+
optim,
1564+
nZ̃,
1565+
NoTypeFunction(J_func),
1566+
NoTypeFunction(∇J_func!);
1567+
name = :J_op)
15581568
end
15591569
return J_op
15601570
end

‎src/general.jl‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,21 @@ const ERROR_STATUSES = (
4949
JuMP.INVALID_OPTION, JuMP.INTERRUPTED, JuMP.OTHER_ERROR
5050
)
5151

52+
"""
53+
A callable wrapper that erases the concrete type of its function.
54+
55+
The `f` field is deliberately an abstract type (`Function`) to hide its concrete type. It
56+
means that calling this wrapper produces a dynamic dispatch, which is acceptable since MOI
57+
already does a dynamic dispatch when calling registered nonlinear operators. The goal is
58+
to hide the concrete type of `J_func`, `∇J_func` and `∇²J_func` closures at construction,
59+
since they are huge because of all the captured variables. Methods are slow to compile when
60+
their signature contains giant types. The constructor TTFX is thus reduced with this trick.
61+
"""
62+
struct NoTypeFunction <: Function
63+
f::Function
64+
end
65+
(obj::NoTypeFunction)(args...) = obj.f(args...)
66+
5267
"Verify that `optim` termination status is `OPTIMAL` or `LOCALLY_SOLVED`."
5368
function issolved(optim::JuMP.GenericModel)
5469
status = JuMP.termination_status(optim)

0 commit comments

Comments
 (0)