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

Release GIL and Run in Background Thread #1886

Merged
merged 4 commits into from
Sep 7, 2024
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: 10 additions & 1 deletion src/highs_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,15 @@ std::tuple<HighsStatus, int> highs_getRowByName(Highs* h,
}


HighsStatus highs_run(Highs* h)
{
py::gil_scoped_release release;
HighsStatus status = h->run();
py::gil_scoped_acquire();
return status;
}


PYBIND11_MODULE(_core, m) {
// enum classes
py::enum_<ObjSense>(m, "ObjSense")
Expand Down Expand Up @@ -874,7 +883,7 @@ PYBIND11_MODULE(_core, m) {
.def("writeBasis", &Highs::writeBasis)
.def("postsolve", &highs_postsolve)
.def("postsolve", &highs_mipPostsolve)
.def("run", &Highs::run)
.def("run", &highs_run)
.def("feasibilityRelaxation",
[](Highs& self, double global_lower_penalty, double global_upper_penalty, double global_rhs_penalty,
py::object local_lower_penalty, py::object local_upper_penalty, py::object local_rhs_penalty) {
Expand Down
25 changes: 21 additions & 4 deletions src/highspy/highs.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@
from itertools import groupby, product
from operator import itemgetter
from decimal import Decimal
from threading import Thread


class _ThreadingResult:
def __init__(self):
self.out = None


class Highs(_Highs):
"""HiGHS solver interface"""
Expand All @@ -47,6 +54,12 @@ def __init__(self):
def silent(self):
"""Disables solver output to the console."""
super().setOptionValue("output_flag", False)

def _run(self, res):
res.out = super().run()

def run(self):
return self.solve()

# solve
def solve(self):
Expand All @@ -55,11 +68,15 @@ def solve(self):
Returns:
A HighsStatus object containing the solve status.
"""
return super().run()
res = _ThreadingResult()
t = Thread(target=self._run, args=(res,))
t.start()
t.join()
return res.out

def optimize(self):
"""Alias for the solve method."""
return super().run()
return self.solve()

# reset the objective and sense, then solve
def minimize(self, obj=None):
Expand Down Expand Up @@ -91,7 +108,7 @@ def minimize(self, obj=None):
super().changeObjectiveOffset(obj.constant)

super().changeObjectiveSense(ObjSense.kMinimize)
return super().run()
return self.solve()

# reset the objective and sense, then solve
def maximize(self, obj=None):
Expand Down Expand Up @@ -123,7 +140,7 @@ def maximize(self, obj=None):
super().changeObjectiveOffset(obj.constant)

super().changeObjectiveSense(ObjSense.kMaximize)
return super().run()
return self.solve()

def internal_get_value(self, var_index_collection, col_value):
"""Internal method to get the value of a variable in the solution. Could be value or dual."""
Expand Down
Loading