Skip to content
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
5 changes: 5 additions & 0 deletions src/pyscipopt/scip.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -1822,6 +1822,11 @@ cdef extern from "blockmemshell/memory.h":
void BMScheckEmptyMemory()
long long BMSgetMemoryUsed()

cdef extern from "scip/scip_mem.h":
SCIP_Longint SCIPgetMemUsed(SCIP* scip)
SCIP_Longint SCIPgetMemTotal(SCIP* scip)
SCIP_Longint SCIPgetMemExternEstim(SCIP* scip)

cdef extern from "scip/scip_expr.h":
SCIP_RETCODE SCIPcreateExpr(SCIP* scip,
SCIP_EXPR** expr,
Expand Down
35 changes: 35 additions & 0 deletions src/pyscipopt/scip.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@
if rc == SCIP_OKAY:
pass
elif rc == SCIP_ERROR:
raise Exception('SCIP: unspecified error!')

Check failure on line 319 in src/pyscipopt/scip.pxi

View workflow job for this annotation

GitHub Actions / test-coverage (3.11)

SCIP: unspecified error!
elif rc == SCIP_NOMEMORY:
raise MemoryError('SCIP: insufficient memory error!')
elif rc == SCIP_READERROR:
Expand All @@ -335,7 +335,7 @@
raise Exception('SCIP: method cannot be called at this time'
+ ' in solution process!')
elif rc == SCIP_INVALIDDATA:
raise Exception('SCIP: error in input data!')

Check failure on line 338 in src/pyscipopt/scip.pxi

View workflow job for this annotation

GitHub Actions / test-coverage (3.11)

SCIP: error in input data!
elif rc == SCIP_INVALIDRESULT:
raise Exception('SCIP: method returned an invalid result code!')
elif rc == SCIP_PLUGINNOTFOUND:
Expand Down Expand Up @@ -11467,6 +11467,41 @@

locale.setlocale(locale.LC_NUMERIC,user_locale)

def getMemUsed(self):
"""
Gets the total number of bytes used in block and buffer memory.

Returns
-------
int

"""
return SCIPgetMemUsed(self._scip)

def getMemTotal(self):
"""
Gets the total number of bytes in block and buffer memory
(i.e., total allocated, including unused).

Returns
-------
int

"""
return SCIPgetMemTotal(self._scip)

def getMemExternEstim(self):
"""
Gets the estimated number of bytes used by external software,
e.g., the LP solver.

Returns
-------
int

"""
return SCIPgetMemExternEstim(self._scip)

def getNLPs(self):
"""
Gets total number of LPs solved so far.
Expand Down
3 changes: 3 additions & 0 deletions src/pyscipopt/scip.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,9 @@ class Model:
def getNLPIterations(self) -> Incomplete: ...
def getNLPRows(self) -> Incomplete: ...
def getNLPs(self) -> Incomplete: ...
def getMemUsed(self) -> int: ...
def getMemTotal(self) -> int: ...
def getMemExternEstim(self) -> int: ...
def getNLeaves(self) -> Incomplete: ...
def getNLimSolsFound(self) -> Incomplete: ...
def getNNlRows(self) -> Incomplete: ...
Expand Down
20 changes: 20 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,3 +642,23 @@ def test_getSolVal():
m.getVal("not_a_var")
with pytest.raises(TypeError):
m.getSolVal(sol, "not_a_var")


def test_memory_methods():
m = Model()

# Memory values should be non-negative even on an empty model
assert m.getMemUsed() >= 0
assert m.getMemTotal() >= 0
assert m.getMemExternEstim() >= 0

# Total allocated should be at least as much as actively used
assert m.getMemTotal() >= m.getMemUsed()

# After adding variables and solving, memory usage should increase
x = m.addVar("x", vtype="C", obj=1.0)
m.addCons(x >= 0)
m.optimize()

assert m.getMemUsed() > 0
assert m.getMemTotal() > 0