Skip to content

Commit

Permalink
[Nonlinear] add timer to Evaluator for MOI.initialize (#2438)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Feb 20, 2024
1 parent 47b69c4 commit 2ea481b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Nonlinear/evaluator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ function Base.show(io::IO, evaluator::Evaluator)
end

function MOI.initialize(evaluator::Evaluator, features::Vector{Symbol})
start_time = time()
empty!(evaluator.ordered_constraints)
evaluator.eval_objective_timer = 0.0
evaluator.eval_objective_gradient_timer = 0.0
Expand All @@ -85,6 +86,7 @@ function MOI.initialize(evaluator::Evaluator, features::Vector{Symbol})
"following features are not supported: $features",
)
end
evaluator.initialize_timer = time() - start_time
return
end

Expand Down
2 changes: 2 additions & 0 deletions src/Nonlinear/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ mutable struct Evaluator{B} <: MOI.AbstractNLPEvaluator
# constraints without needing to query the full vector each time.
constraint_dual::Vector{Float64}
# Timers
initialize_timer::Float64
eval_objective_timer::Float64
eval_constraint_timer::Float64
eval_objective_gradient_timer::Float64
Expand All @@ -238,6 +239,7 @@ mutable struct Evaluator{B} <: MOI.AbstractNLPEvaluator
0.0,
0.0,
0.0,
0.0,
)
end
end
Expand Down
37 changes: 37 additions & 0 deletions test/Nonlinear/ReverseAD.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,43 @@ function test_constraint_gradient()
return
end

function test_timers()
x = MOI.VariableIndex(1)
model = Nonlinear.Model()
Nonlinear.set_objective(model, :(log($x)))
Nonlinear.add_constraint(model, :(sin($x)), MOI.LessThan(0.5))
evaluator = Nonlinear.Evaluator(model, Nonlinear.SparseReverseMode(), [x])
y = [1.2]
g = [NaN]
MOI.initialize(evaluator, [:Grad, :Jac, :Hess])
MOI.eval_objective(evaluator, y)
MOI.eval_constraint(evaluator, g, y)
MOI.eval_objective_gradient(evaluator, g, y)
MOI.eval_constraint_gradient(evaluator, g, y, 1)
MOI.eval_constraint_jacobian(evaluator, g, y)
MOI.eval_hessian_objective(evaluator, g, y)
MOI.eval_hessian_constraint(evaluator, g, y, 1)
MOI.eval_hessian_lagrangian(evaluator, g, y, 1.0, [1.0])
timers = [
evaluator.initialize_timer,
evaluator.eval_objective_timer,
evaluator.eval_constraint_timer,
evaluator.eval_objective_gradient_timer,
evaluator.eval_constraint_gradient_timer,
evaluator.eval_constraint_jacobian_timer,
evaluator.eval_hessian_objective_timer,
evaluator.eval_hessian_constraint_timer,
evaluator.eval_hessian_lagrangian_timer,
]
@test all(>=(0.0), timers)
if !Sys.iswindows()
# Windows times only in milliseconds, which can be too coarse to
# accurately measure and test.
@test sum(timers) > 0.0
end
return
end

end # module

TestReverseAD.runtests()

0 comments on commit 2ea481b

Please sign in to comment.