Skip to content

Commit 2ea481b

Browse files
authored
[Nonlinear] add timer to Evaluator for MOI.initialize (#2438)
1 parent 47b69c4 commit 2ea481b

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

src/Nonlinear/evaluator.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ function Base.show(io::IO, evaluator::Evaluator)
6464
end
6565

6666
function MOI.initialize(evaluator::Evaluator, features::Vector{Symbol})
67+
start_time = time()
6768
empty!(evaluator.ordered_constraints)
6869
evaluator.eval_objective_timer = 0.0
6970
evaluator.eval_objective_gradient_timer = 0.0
@@ -85,6 +86,7 @@ function MOI.initialize(evaluator::Evaluator, features::Vector{Symbol})
8586
"following features are not supported: $features",
8687
)
8788
end
89+
evaluator.initialize_timer = time() - start_time
8890
return
8991
end
9092

src/Nonlinear/types.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ mutable struct Evaluator{B} <: MOI.AbstractNLPEvaluator
212212
# constraints without needing to query the full vector each time.
213213
constraint_dual::Vector{Float64}
214214
# Timers
215+
initialize_timer::Float64
215216
eval_objective_timer::Float64
216217
eval_constraint_timer::Float64
217218
eval_objective_gradient_timer::Float64
@@ -238,6 +239,7 @@ mutable struct Evaluator{B} <: MOI.AbstractNLPEvaluator
238239
0.0,
239240
0.0,
240241
0.0,
242+
0.0,
241243
)
242244
end
243245
end

test/Nonlinear/ReverseAD.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,43 @@ function test_constraint_gradient()
10511051
return
10521052
end
10531053

1054+
function test_timers()
1055+
x = MOI.VariableIndex(1)
1056+
model = Nonlinear.Model()
1057+
Nonlinear.set_objective(model, :(log($x)))
1058+
Nonlinear.add_constraint(model, :(sin($x)), MOI.LessThan(0.5))
1059+
evaluator = Nonlinear.Evaluator(model, Nonlinear.SparseReverseMode(), [x])
1060+
y = [1.2]
1061+
g = [NaN]
1062+
MOI.initialize(evaluator, [:Grad, :Jac, :Hess])
1063+
MOI.eval_objective(evaluator, y)
1064+
MOI.eval_constraint(evaluator, g, y)
1065+
MOI.eval_objective_gradient(evaluator, g, y)
1066+
MOI.eval_constraint_gradient(evaluator, g, y, 1)
1067+
MOI.eval_constraint_jacobian(evaluator, g, y)
1068+
MOI.eval_hessian_objective(evaluator, g, y)
1069+
MOI.eval_hessian_constraint(evaluator, g, y, 1)
1070+
MOI.eval_hessian_lagrangian(evaluator, g, y, 1.0, [1.0])
1071+
timers = [
1072+
evaluator.initialize_timer,
1073+
evaluator.eval_objective_timer,
1074+
evaluator.eval_constraint_timer,
1075+
evaluator.eval_objective_gradient_timer,
1076+
evaluator.eval_constraint_gradient_timer,
1077+
evaluator.eval_constraint_jacobian_timer,
1078+
evaluator.eval_hessian_objective_timer,
1079+
evaluator.eval_hessian_constraint_timer,
1080+
evaluator.eval_hessian_lagrangian_timer,
1081+
]
1082+
@test all(>=(0.0), timers)
1083+
if !Sys.iswindows()
1084+
# Windows times only in milliseconds, which can be too coarse to
1085+
# accurately measure and test.
1086+
@test sum(timers) > 0.0
1087+
end
1088+
return
1089+
end
1090+
10541091
end # module
10551092

10561093
TestReverseAD.runtests()

0 commit comments

Comments
 (0)