Skip to content
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

Catch [CTRL+C] and gracefully exit #349

Merged
merged 6 commits into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/MOI_callbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,20 @@ function Base.unsafe_convert(::Type{Ptr{Cvoid}}, x::_CallbackUserData)
end

function _gurobi_callback_wrapper(
::Ptr{Cvoid},
p_model::Ptr{Cvoid},
cb_data::Ptr{Cvoid},
cb_where::Cint,
p_user_data::Ptr{Cvoid}
)
user_data = unsafe_pointer_to_objref(p_user_data)::_CallbackUserData
user_data.callback(CallbackData(user_data.model, cb_data), cb_where)
try
user_data.callback(CallbackData(user_data.model, cb_data), cb_where)
catch ex
GRBterminate(p_model)
if !(ex isa InterruptException)
rethrow(ex)
end
end
return Cint(0)
end

Expand Down
22 changes: 20 additions & 2 deletions src/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2306,10 +2306,28 @@ function MOI.optimize!(model::Optimizer)
if _check_moi_callback_validity(model)
MOI.set(model, CallbackFunction(), _default_moi_callback(model))
model.has_generic_callback = false
end

elseif !model.has_generic_callback
# From the docstring of disable_sigint, "External functions that do not
# call julia code or julia runtime automatically disable sigint during
# their execution." We don't want this though! We want to be able to
# SIGINT Gurobi, and then catch it as an interrupt. As a hack, until
# Julia introduces an interruptible ccall --- which it likely won't
# https://github.com/JuliaLang/julia/issues/2622 --- set a null
# callback.
MOI.set(model, CallbackFunction(), (x, y) -> nothing)
end

# Catch [CTRL+C], even when Julia is run from a script not in interactive
# mode. If `true`, then a script would call `atexit` without throwing the
# `InterruptException`. `false` is the default in interactive mode.
#
# TODO(odow): Julia 1.5 exposes `Base.exit_on_sigint(::Bool)`.
ccall(:jl_exit_on_sigint, Cvoid, (Cint,), false)
ret = GRBoptimize(model)
_check_ret(model, ret)
if !isinteractive()
ccall(:jl_exit_on_sigint, Cvoid, (Cint,), true)
end

# Post-optimize caching to speed up the checks in VariablePrimal and
# ConstraintDual.
Expand Down
61 changes: 61 additions & 0 deletions test/MOI/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,67 @@ function test_Attributes()
@test MOI.get(model, MOI.SimplexIterations()) == 0
end

function test_GRBterminate()
model = Gurobi.Optimizer(GRB_ENV)
MOI.set(model, MOI.Silent(), true)
x = MOI.add_variable(model)
MOI.set(model, Gurobi.CallbackFunction(), (cb_data, cb_where) -> begin
GRBterminate(model)
end)
MOI.optimize!(model)
@test MOI.get(model, MOI.TerminationStatus()) == MOI.INTERRUPTED
end

"""
test_InterruptException()

This test simulates an InterruptException being thrown. It is a little
complicated due to the delayed handling of GRBterminate, which _schedules_ a
request for termination, rather than terminating immediately. This means Gurobi
may continue to call the callback after the interruption.

First, we must ensure that InterruptException() is only thrown once. Double
interrupting would interrupt our handling of the first interrupt!

Second, if the model is too simplisitic, Gurobi may be able to prove optimality
after we have interrupted, but before it has decided to actually exit the solve.
"""
function test_InterruptException()
model = Gurobi.Optimizer(GRB_ENV)
MOI.set(model, MOI.Silent(), true)
x = MOI.add_variable(model)
MOI.add_constraint(model, MOI.SingleVariable(x), MOI.Integer())
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
MOI.set(
model,
MOI.ObjectiveFunction{MOI.SingleVariable}(),
MOI.SingleVariable(x),
)
MOI.set(model, MOI.RawParameter("LazyConstraints"), 1)
interrupt_thrown = false
i = 0.0
MOI.set(model, Gurobi.CallbackFunction(), (cb_data, cb_where) -> begin
if cb_where != Gurobi.GRB_CB_MIPSOL
return
end
MOI.submit(
model,
MOI.LazyConstraint(cb_data),
MOI.ScalarAffineFunction{Float64}(
[MOI.ScalarAffineTerm(1.0, x)], 0.0
),
MOI.GreaterThan{Float64}(i)
)
i += 1
if !interrupt_thrown
interrupt_thrown = true
throw(InterruptException())
end
end)
MOI.optimize!(model)
@test MOI.get(model, MOI.TerminationStatus()) == MOI.INTERRUPTED
end

function test_indicator_name()
MOI.empty!(OPTIMIZER)
x = MOI.add_variables(model, 2)
Expand Down