Skip to content

Commit

Permalink
Fix python interface with OpenMP
Browse files Browse the repository at this point in the history
  • Loading branch information
vigsterkr committed Jan 23, 2017
1 parent db05cba commit 2d92988
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 31 deletions.
60 changes: 29 additions & 31 deletions CMakeLists.txt
Expand Up @@ -979,38 +979,36 @@ IF (PythonModular)

SET(HAVE_PYTHON 1)

IF(PythonModular)
#custom swig flags for python modular interface
IF(${PYTHON_VERSION_MAJOR} VERSION_EQUAL 3)
SET(TARGET_SWIGFLAGS "-builtin\;-modern\;-modernargs\;-py3")
SET(PYTHON3 1)
ELSE()
SET(TARGET_SWIGFLAGS "-builtin\;-modern\;-modernargs")
ENDIF()
#custom swig flags for python modular interface
IF(${PYTHON_VERSION_MAJOR} VERSION_EQUAL 3)
SET(TARGET_SWIGFLAGS "-builtin\;-modern\;-modernargs\;-threads\;-py3")
SET(PYTHON3 1)
ELSE()
SET(TARGET_SWIGFLAGS "-builtin\;-modern\;-modernargs\;-threads")
ENDIF()

# SWIG was broken for combining -builtin and -modernargs
# from v3.0.0 and until 3.0.4. This bug was fixed in
# v3.0.5. Make CMake emit an error and fail to configure.
IF((NOT "${SWIG_VERSION}" VERSION_LESS "3.0.0") AND
("${SWIG_VERSION}" VERSION_LESS "3.0.5"))
MESSAGE(FATAL_ERROR
"This version of SWIG is broken for building Python_modular interface. Use SWIG < 3.0.0 or >= 3.0.5.")
ENDIF((NOT "${SWIG_VERSION}" VERSION_LESS "3.0.0") AND
("${SWIG_VERSION}" VERSION_LESS "3.0.5"))

# SWIG-generated Python-wrappers fail to build
# for Python >=3.5 with SWIG < 3.0.8. Make CMake
# emit an error and fail to configure.
IF ((NOT "${PYTHON_VERSION_STRING}" VERSION_LESS "3.5") AND
("${SWIG_VERSION}" VERSION_LESS "3.0.8"))
MESSAGE(FATAL_ERROR
"Building Python_modular interface for Python >= 3.5 requires SWIG >= 3.0.8.")
ENDIF ((NOT "${PYTHON_VERSION_STRING}" VERSION_LESS "3.5") AND
("${SWIG_VERSION}" VERSION_LESS "3.0.8"))

IF(EXISTS ${CMAKE_SOURCE_DIR}/src/interfaces/python_modular)
add_subdirectory(${CMAKE_SOURCE_DIR}/src/interfaces/python_modular)
ENDIF()
# SWIG was broken for combining -builtin and -modernargs
# from v3.0.0 and until 3.0.4. This bug was fixed in
# v3.0.5. Make CMake emit an error and fail to configure.
IF((NOT "${SWIG_VERSION}" VERSION_LESS "3.0.0") AND
("${SWIG_VERSION}" VERSION_LESS "3.0.5"))
MESSAGE(FATAL_ERROR
"This version of SWIG is broken for building Python_modular interface. Use SWIG < 3.0.0 or >= 3.0.5.")
ENDIF((NOT "${SWIG_VERSION}" VERSION_LESS "3.0.0") AND
("${SWIG_VERSION}" VERSION_LESS "3.0.5"))

# SWIG-generated Python-wrappers fail to build
# for Python >=3.5 with SWIG < 3.0.8. Make CMake
# emit an error and fail to configure.
IF ((NOT "${PYTHON_VERSION_STRING}" VERSION_LESS "3.5") AND
("${SWIG_VERSION}" VERSION_LESS "3.0.8"))
MESSAGE(FATAL_ERROR
"Building Python_modular interface for Python >= 3.5 requires SWIG >= 3.0.8.")
ENDIF ((NOT "${PYTHON_VERSION_STRING}" VERSION_LESS "3.5") AND
("${SWIG_VERSION}" VERSION_LESS "3.0.8"))

IF(EXISTS ${CMAKE_SOURCE_DIR}/src/interfaces/python_modular)
add_subdirectory(${CMAKE_SOURCE_DIR}/src/interfaces/python_modular)
ENDIF()
ENDIF()

Expand Down
10 changes: 10 additions & 0 deletions src/interfaces/python_modular/sg_print_functions.cpp
Expand Up @@ -14,15 +14,23 @@ void sg_global_print_message(FILE* target, const char* str)
void sg_global_print_warning(FILE* target, const char* str)
{
if (target==stdout)
{
PyGILState_STATE gil = PyGILState_Ensure();
PyErr_Warn(NULL, str);
PyGILState_Release(gil);
}
else
fprintf(target, "%s", str);
}

void sg_global_print_error(FILE* target, const char* str)
{
if (target==stdout)
{
PyGILState_STATE gil = PyGILState_Ensure();
PyErr_SetString(PyExc_RuntimeError, str);
PyGILState_Release(gil);
}
else
fprintf(target, "%s", str);
}
Expand All @@ -31,6 +39,7 @@ void sg_global_cancel_computations(bool &delayed, bool &immediately)
{
using namespace shogun;

PyGILState_STATE gil = PyGILState_Ensure();
if (PyErr_CheckSignals())
{
SG_SPRINT("\nImmediately return to matlab prompt / Prematurely finish computations / Do nothing (I/P/D)? ");
Expand All @@ -46,4 +55,5 @@ void sg_global_cancel_computations(bool &delayed, bool &immediately)
else
SG_SPRINT("\n");
}
PyGILState_Release(gil);
}

0 comments on commit 2d92988

Please sign in to comment.