Skip to content

Commit

Permalink
Merge pull request #1886 from michaelbynum/release_gil
Browse files Browse the repository at this point in the history
Release GIL and Run in Background Thread
  • Loading branch information
jajhall authored Sep 7, 2024
2 parents 2d8d8af + ec57e11 commit f7be435
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
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

0 comments on commit f7be435

Please sign in to comment.