From 2d9298839bf40349d9c9444eb95f2fd13a7d889d Mon Sep 17 00:00:00 2001 From: Viktor Gal Date: Mon, 23 Jan 2017 16:50:10 +0800 Subject: [PATCH] Fix python interface with OpenMP --- CMakeLists.txt | 60 +++++++++---------- .../python_modular/sg_print_functions.cpp | 10 ++++ 2 files changed, 39 insertions(+), 31 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 974818b8caa..ae11ba219e4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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() diff --git a/src/interfaces/python_modular/sg_print_functions.cpp b/src/interfaces/python_modular/sg_print_functions.cpp index 42fb0a66670..4b86b8d8a5c 100644 --- a/src/interfaces/python_modular/sg_print_functions.cpp +++ b/src/interfaces/python_modular/sg_print_functions.cpp @@ -14,7 +14,11 @@ 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); } @@ -22,7 +26,11 @@ void sg_global_print_warning(FILE* target, const char* 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); } @@ -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)? "); @@ -46,4 +55,5 @@ void sg_global_cancel_computations(bool &delayed, bool &immediately) else SG_SPRINT("\n"); } + PyGILState_Release(gil); }