1af0996ceSBarry Smith #include <petsc/private/petscimpl.h> /*I "petscsys.h" I*/ 2c4aff060SBarry Smith 3c4aff060SBarry Smith /* ---------------------------------------------------------------- */ 4c4aff060SBarry Smith 5c4aff060SBarry Smith #if !defined(PETSC_PYTHON_EXE) 6c4aff060SBarry Smith #define PETSC_PYTHON_EXE "python" 7c4aff060SBarry Smith #endif 8c4aff060SBarry Smith 9c4aff060SBarry Smith static PetscErrorCode PetscPythonFindExecutable(char pythonexe[PETSC_MAX_PATH_LEN]) 10c4aff060SBarry Smith { 11ace3abfcSBarry Smith PetscBool flag; 12c4aff060SBarry Smith PetscErrorCode ierr; 136e111a19SKarl Rupp 14c4aff060SBarry Smith PetscFunctionBegin; 15c4aff060SBarry Smith /* get the path for the Python interpreter executable */ 16c4aff060SBarry Smith ierr = PetscStrncpy(pythonexe,PETSC_PYTHON_EXE,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); 17c5929fdfSBarry Smith ierr = PetscOptionsGetString(NULL,NULL,"-python",pythonexe,PETSC_MAX_PATH_LEN,&flag);CHKERRQ(ierr); 18c4aff060SBarry Smith if (!flag || pythonexe[0]==0) { 19c4aff060SBarry Smith ierr = PetscStrncpy(pythonexe,PETSC_PYTHON_EXE,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); 20c4aff060SBarry Smith } 21c4aff060SBarry Smith PetscFunctionReturn(0); 22c4aff060SBarry Smith } 23c4aff060SBarry Smith 24*412d63f8SBarry Smith static PetscErrorCode PetscPythonFindLibrary(const char pythonexe[PETSC_MAX_PATH_LEN],char pythonlib[PETSC_MAX_PATH_LEN]) 25c4aff060SBarry Smith { 26*412d63f8SBarry Smith const char cmdline[] = "-c 'from distutils import sysconfig; print(sysconfig.get_config_var('LIBPYTHON'))"; 27*412d63f8SBarry Smith char command[PETSC_MAX_PATH_LEN+1+sizeof(cmdline)+1],*eol; 28c4aff060SBarry Smith FILE *fp = NULL; 29ace3abfcSBarry Smith PetscBool found = PETSC_FALSE; 30c4aff060SBarry Smith PetscErrorCode ierr; 31c4aff060SBarry Smith 326e111a19SKarl Rupp PetscFunctionBegin; 33c4aff060SBarry Smith #if defined(PETSC_PYTHON_LIB) 34c4aff060SBarry Smith ierr = PetscStrcpy(pythonlib,PETSC_PYTHON_LIB);CHKERRQ(ierr); 35c4aff060SBarry Smith PetscFunctionReturn(0); 36c4aff060SBarry Smith #endif 37c4aff060SBarry Smith 38c4aff060SBarry Smith /* call Python to find out the name of the Python dynamic library */ 39c4aff060SBarry Smith ierr = PetscStrncpy(command,pythonexe,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); 40c4aff060SBarry Smith ierr = PetscStrcat(command," ");CHKERRQ(ierr); 41c4aff060SBarry Smith ierr = PetscStrcat(command,cmdline);CHKERRQ(ierr); 42c4aff060SBarry Smith #if defined(PETSC_HAVE_POPEN) 430298fd71SBarry Smith ierr = PetscPOpen(PETSC_COMM_SELF,NULL,command,"r",&fp);CHKERRQ(ierr); 44*412d63f8SBarry Smith if (!fgets(pythonlib,PETSC_MAX_PATH_LEN,fp)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Python: bad output from executable: %s",pythonexe); 45016831caSBarry Smith ierr = PetscPClose(PETSC_COMM_SELF,fp);CHKERRQ(ierr); 46c4aff060SBarry Smith #else 47e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,1,"Python: Aborted due to missing popen()"); 48c4aff060SBarry Smith #endif 49c4aff060SBarry Smith /* remove newlines */ 50*412d63f8SBarry Smith ierr = PetscStrchr(pythonlib,'\n',&eol);CHKERRQ(ierr); 51c4aff060SBarry Smith if (eol) eol[0] = 0; 52*412d63f8SBarry Smith ierr = PetscTestFile(pythonlib,'r',&found);CHKERRQ(ierr); 53*412d63f8SBarry Smith ierr = PetscInfo2(0,"Python library %s found %d\n",pythonlib,found);CHKERRQ(ierr); 54c4aff060SBarry Smith PetscFunctionReturn(0); 55c4aff060SBarry Smith } 56c4aff060SBarry Smith 57c4aff060SBarry Smith /* ---------------------------------------------------------------- */ 58c4aff060SBarry Smith 59c4aff060SBarry Smith typedef struct _Py_object_t PyObject; /* fake definition */ 60c4aff060SBarry Smith 61e0ab9aedSLisandro Dalcin static PyObject* Py_None = 0; 62e0ab9aedSLisandro Dalcin 639ac80d5eSLisandro Dalcin static const char* (*Py_GetVersion)(void); 649ac80d5eSLisandro Dalcin 65c4aff060SBarry Smith static int (*Py_IsInitialized)(void); 66c4aff060SBarry Smith static void (*Py_InitializeEx)(int); 67c4aff060SBarry Smith static void (*Py_Finalize)(void); 68c4aff060SBarry Smith 69c4aff060SBarry Smith static void (*PySys_SetArgv)(int,char**); 702f2e82b0SLisandro Dalcin static PyObject* (*PySys_GetObject)(const char*); 712f2e82b0SLisandro Dalcin static PyObject* (*PyObject_CallMethod)(PyObject*,const char*, const char*, ...); 72c4aff060SBarry Smith static PyObject* (*PyImport_ImportModule)(const char*); 73c4aff060SBarry Smith 74c4aff060SBarry Smith static void (*Py_IncRef)(PyObject*); 75c4aff060SBarry Smith static void (*Py_DecRef)(PyObject*); 76c4aff060SBarry Smith 77c4aff060SBarry Smith static void (*PyErr_Clear)(void); 78c4aff060SBarry Smith static PyObject* (*PyErr_Occurred)(void); 79e0ab9aedSLisandro Dalcin static void (*PyErr_Fetch)(PyObject**,PyObject**,PyObject**); 80e0ab9aedSLisandro Dalcin static void (*PyErr_NormalizeException)(PyObject**,PyObject**, PyObject**); 81e0ab9aedSLisandro Dalcin static void (*PyErr_Display)(PyObject*,PyObject*,PyObject*); 82e0ab9aedSLisandro Dalcin static void (*PyErr_Restore)(PyObject*,PyObject*,PyObject*); 83c4aff060SBarry Smith 84c4aff060SBarry Smith 85c4aff060SBarry Smith #define PetscDLPyLibOpen(libname) \ 86d44a1e48SBarry Smith PetscDLLibraryAppend(PETSC_COMM_SELF,&PetscDLLibrariesLoaded,libname) 87c4aff060SBarry Smith #define PetscDLPyLibSym(symbol, value) \ 880298fd71SBarry Smith PetscDLLibrarySym(PETSC_COMM_SELF,&PetscDLLibrariesLoaded,NULL,symbol,(void**)value) 89c4aff060SBarry Smith #define PetscDLPyLibClose(comm) \ 90c4aff060SBarry Smith do { } while (0) 91c4aff060SBarry Smith 92c4aff060SBarry Smith static PetscErrorCode PetscPythonLoadLibrary(const char pythonlib[]) 93c4aff060SBarry Smith { 94c4aff060SBarry Smith PetscErrorCode ierr; 95c4aff060SBarry Smith 966e111a19SKarl Rupp PetscFunctionBegin; 97c4aff060SBarry Smith /* open the Python dynamic library */ 98c4aff060SBarry Smith ierr = PetscDLPyLibOpen(pythonlib);CHKERRQ(ierr); 99c4aff060SBarry Smith ierr = PetscInfo1(0,"Python: loaded dynamic library %s\n", pythonlib);CHKERRQ(ierr); 100c4aff060SBarry Smith /* look required symbols from the Python C-API */ 101e0ab9aedSLisandro Dalcin ierr = PetscDLPyLibSym("_Py_NoneStruct" , &Py_None );CHKERRQ(ierr); 1029ac80d5eSLisandro Dalcin ierr = PetscDLPyLibSym("Py_GetVersion" , &Py_GetVersion );CHKERRQ(ierr); 103c4aff060SBarry Smith ierr = PetscDLPyLibSym("Py_IsInitialized" , &Py_IsInitialized );CHKERRQ(ierr); 104c4aff060SBarry Smith ierr = PetscDLPyLibSym("Py_InitializeEx" , &Py_InitializeEx );CHKERRQ(ierr); 105c4aff060SBarry Smith ierr = PetscDLPyLibSym("Py_Finalize" , &Py_Finalize );CHKERRQ(ierr); 1062f2e82b0SLisandro Dalcin ierr = PetscDLPyLibSym("PySys_GetObject" , &PySys_GetObject );CHKERRQ(ierr); 107c4aff060SBarry Smith ierr = PetscDLPyLibSym("PySys_SetArgv" , &PySys_SetArgv );CHKERRQ(ierr); 1082f2e82b0SLisandro Dalcin ierr = PetscDLPyLibSym("PyObject_CallMethod" , &PyObject_CallMethod );CHKERRQ(ierr); 109c4aff060SBarry Smith ierr = PetscDLPyLibSym("PyImport_ImportModule" , &PyImport_ImportModule );CHKERRQ(ierr); 110c4aff060SBarry Smith ierr = PetscDLPyLibSym("Py_IncRef" , &Py_IncRef );CHKERRQ(ierr); 111c4aff060SBarry Smith ierr = PetscDLPyLibSym("Py_DecRef" , &Py_DecRef );CHKERRQ(ierr); 112c4aff060SBarry Smith ierr = PetscDLPyLibSym("PyErr_Clear" , &PyErr_Clear );CHKERRQ(ierr); 113c4aff060SBarry Smith ierr = PetscDLPyLibSym("PyErr_Occurred" , &PyErr_Occurred );CHKERRQ(ierr); 114e0ab9aedSLisandro Dalcin ierr = PetscDLPyLibSym("PyErr_Fetch" , &PyErr_Fetch );CHKERRQ(ierr); 115e0ab9aedSLisandro Dalcin ierr = PetscDLPyLibSym("PyErr_NormalizeException", &PyErr_NormalizeException);CHKERRQ(ierr); 116e0ab9aedSLisandro Dalcin ierr = PetscDLPyLibSym("PyErr_Display", &PyErr_Display );CHKERRQ(ierr); 117e0ab9aedSLisandro Dalcin ierr = PetscDLPyLibSym("PyErr_Restore", &PyErr_Restore );CHKERRQ(ierr); 118c4aff060SBarry Smith /* XXX TODO: check that ALL symbols were there !!! */ 119e0ab9aedSLisandro Dalcin if (!Py_None) SETERRQ(PETSC_COMM_SELF,1,"Python: failed to load symbols from dynamic library"); 1209ac80d5eSLisandro Dalcin if (!Py_GetVersion) SETERRQ(PETSC_COMM_SELF,1,"Python: failed to load symbols from dynamic library"); 121c1235816SBarry Smith if (!Py_IsInitialized) SETERRQ(PETSC_COMM_SELF,1,"Python: failed to load symbols from dynamic library"); 122c1235816SBarry Smith if (!Py_InitializeEx) SETERRQ(PETSC_COMM_SELF,1,"Python: failed to load symbols from dynamic library"); 123c1235816SBarry Smith if (!Py_Finalize) SETERRQ(PETSC_COMM_SELF,1,"Python: failed to load symbols from dynamic library"); 124c4aff060SBarry Smith ierr = PetscInfo(0,"Python: all required symbols loaded from Python dynamic library\n");CHKERRQ(ierr); 125c4aff060SBarry Smith PetscFunctionReturn(0); 126c4aff060SBarry Smith } 127c4aff060SBarry Smith 128c4aff060SBarry Smith /* ---------------------------------------------------------------- */ 129c4aff060SBarry Smith 130c4aff060SBarry Smith static char PetscPythonExe[PETSC_MAX_PATH_LEN] = { 0 }; 131c4aff060SBarry Smith static char PetscPythonLib[PETSC_MAX_PATH_LEN] = { 0 }; 132ace3abfcSBarry Smith static PetscBool PetscBeganPython = PETSC_FALSE; 133c4aff060SBarry Smith 134c4aff060SBarry Smith /*@C 135c4aff060SBarry Smith PetscPythonFinalize - Finalize Python. 136c4aff060SBarry Smith 137c4aff060SBarry Smith Level: intermediate 138c4aff060SBarry Smith 139c4aff060SBarry Smith .keywords: Python 140c4aff060SBarry Smith @*/ 1417087cfbeSBarry Smith PetscErrorCode PetscPythonFinalize(void) 142c4aff060SBarry Smith { 143c4aff060SBarry Smith PetscFunctionBegin; 144c4aff060SBarry Smith if (PetscBeganPython) { if (Py_IsInitialized()) Py_Finalize(); } 145c4aff060SBarry Smith PetscBeganPython = PETSC_FALSE; 146c4aff060SBarry Smith PetscFunctionReturn(0); 147c4aff060SBarry Smith } 148c4aff060SBarry Smith 149c4aff060SBarry Smith /*@C 150c4aff060SBarry Smith PetscPythonInitialize - Initialize Python and import petsc4py. 151c4aff060SBarry Smith 152c4aff060SBarry Smith Input Parameter: 1530298fd71SBarry Smith + pyexe - path to the Python interpreter executable, or NULL. 1540298fd71SBarry Smith - pylib - full path to the Python dynamic library, or NULL. 155c4aff060SBarry Smith 156c4aff060SBarry Smith Level: intermediate 157c4aff060SBarry Smith 158c4aff060SBarry Smith .keywords: Python 159c4aff060SBarry Smith 160c4aff060SBarry Smith @*/ 1617087cfbeSBarry Smith PetscErrorCode PetscPythonInitialize(const char pyexe[],const char pylib[]) 162c4aff060SBarry Smith { 163c4aff060SBarry Smith PyObject *module = 0; 164c4aff060SBarry Smith PetscErrorCode ierr; 1656e111a19SKarl Rupp 166c4aff060SBarry Smith PetscFunctionBegin; 167c4aff060SBarry Smith if (PetscBeganPython) PetscFunctionReturn(0); 168c4aff060SBarry Smith /* Python executable */ 169c4aff060SBarry Smith if (pyexe && pyexe[0] != 0) { 170c4aff060SBarry Smith ierr = PetscStrncpy(PetscPythonExe,pyexe,sizeof(PetscPythonExe));CHKERRQ(ierr); 171c4aff060SBarry Smith } else { 172c4aff060SBarry Smith ierr = PetscPythonFindExecutable(PetscPythonExe);CHKERRQ(ierr); 173c4aff060SBarry Smith } 174c4aff060SBarry Smith /* Python dynamic library */ 175c4aff060SBarry Smith if (pylib && pylib[0] != 0) { 176c4aff060SBarry Smith ierr = PetscStrncpy(PetscPythonLib,pylib,sizeof(PetscPythonLib));CHKERRQ(ierr); 177c4aff060SBarry Smith } else { 178c4aff060SBarry Smith ierr = PetscPythonFindLibrary(PetscPythonExe,PetscPythonLib);CHKERRQ(ierr); 179c4aff060SBarry Smith } 180c4aff060SBarry Smith /* dynamically load Python library */ 181c4aff060SBarry Smith ierr = PetscPythonLoadLibrary(PetscPythonLib);CHKERRQ(ierr); 182c4aff060SBarry Smith /* initialize Python */ 183c4aff060SBarry Smith PetscBeganPython = PETSC_FALSE; 184c4aff060SBarry Smith if (!Py_IsInitialized()) { 1852f2e82b0SLisandro Dalcin static PetscBool registered = PETSC_FALSE; 1862f2e82b0SLisandro Dalcin const char *py_version; 1872f2e82b0SLisandro Dalcin PyObject *sys_path; 1882f2e82b0SLisandro Dalcin char path[PETSC_MAX_PATH_LEN] = { 0 }; 189a297a907SKarl Rupp 1902f2e82b0SLisandro Dalcin /* initialize Python */ 1912f2e82b0SLisandro Dalcin Py_InitializeEx(0); /* 0: do not install signal handlers */ 1922f2e82b0SLisandro Dalcin /* build 'sys.argv' list */ 1932f2e82b0SLisandro Dalcin py_version = Py_GetVersion(); 1949ac80d5eSLisandro Dalcin if (py_version[0] == '2') { 1952f2e82b0SLisandro Dalcin int argc = 0; char **argv = 0; 196c4aff060SBarry Smith ierr = PetscGetArgs(&argc,&argv);CHKERRQ(ierr); 1972f2e82b0SLisandro Dalcin PySys_SetArgv(argc,argv); 1982f2e82b0SLisandro Dalcin } 1992f2e82b0SLisandro Dalcin if (py_version[0] == '3') { 2002f2e82b0SLisandro Dalcin /* XXX 'argv' is type 'wchar_t**' */ 2010298fd71SBarry Smith PySys_SetArgv(0,NULL); 2022f2e82b0SLisandro Dalcin } 2032f2e82b0SLisandro Dalcin /* add PETSC_LIB_DIR in front of 'sys.path' */ 2042f2e82b0SLisandro Dalcin sys_path = PySys_GetObject("path"); 2052f2e82b0SLisandro Dalcin if (sys_path) { 2062f2e82b0SLisandro Dalcin ierr = PetscStrreplace(PETSC_COMM_SELF,"${PETSC_LIB_DIR}",path,sizeof(path));CHKERRQ(ierr); 2072f2e82b0SLisandro Dalcin Py_DecRef(PyObject_CallMethod(sys_path,"insert","is",(int)0,(char*)path)); 2089ac80d5eSLisandro Dalcin } 209c4aff060SBarry Smith /* register finalizer */ 210c4aff060SBarry Smith if (!registered) { 211c4aff060SBarry Smith ierr = PetscRegisterFinalize(PetscPythonFinalize);CHKERRQ(ierr); 212a297a907SKarl Rupp 213c4aff060SBarry Smith registered = PETSC_TRUE; 214c4aff060SBarry Smith } 215c4aff060SBarry Smith PetscBeganPython = PETSC_TRUE; 216c4aff060SBarry Smith } 217c4aff060SBarry Smith /* import 'petsc4py.PETSc' module */ 218c4aff060SBarry Smith module = PyImport_ImportModule("petsc4py.PETSc"); 219c4aff060SBarry Smith if (module) { 220c4aff060SBarry Smith ierr = PetscInfo(0,"Python: successfully imported module 'petsc4py.PETSc'\n");CHKERRQ(ierr); 221a297a907SKarl Rupp 222c4aff060SBarry Smith Py_DecRef(module); module = 0; 223c4aff060SBarry Smith } else { 224e0ab9aedSLisandro Dalcin PetscPythonPrintError(); 225e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Python: could not import module 'petsc4py.PETSc', perhaps your PYTHONPATH does not contain it\n"); 226c4aff060SBarry Smith } 227c4aff060SBarry Smith PetscFunctionReturn(0); 228c4aff060SBarry Smith } 229c4aff060SBarry Smith 230e0ab9aedSLisandro Dalcin /*@C 231e0ab9aedSLisandro Dalcin PetscPythonPrintError - Print Python errors. 232e0ab9aedSLisandro Dalcin 233e0ab9aedSLisandro Dalcin Level: developer 234e0ab9aedSLisandro Dalcin 235e0ab9aedSLisandro Dalcin .keywords: Python 236e0ab9aedSLisandro Dalcin 237e0ab9aedSLisandro Dalcin @*/ 2387087cfbeSBarry Smith PetscErrorCode PetscPythonPrintError(void) 239e0ab9aedSLisandro Dalcin { 240e0ab9aedSLisandro Dalcin PyObject *exc=0, *val=0, *tb=0; 2416e111a19SKarl Rupp 242e0ab9aedSLisandro Dalcin PetscFunctionBegin; 243e0ab9aedSLisandro Dalcin if (!PetscBeganPython) PetscFunctionReturn(0); 244e0ab9aedSLisandro Dalcin if (!PyErr_Occurred()) PetscFunctionReturn(0); 245e0ab9aedSLisandro Dalcin PyErr_Fetch(&exc,&val,&tb); 246e0ab9aedSLisandro Dalcin PyErr_NormalizeException(&exc,&val,&tb); 247e0ab9aedSLisandro Dalcin PyErr_Display(exc ? exc : Py_None, 248e0ab9aedSLisandro Dalcin val ? val : Py_None, 249e0ab9aedSLisandro Dalcin tb ? tb : Py_None); 250e0ab9aedSLisandro Dalcin PyErr_Restore(exc,val,tb); 251e0ab9aedSLisandro Dalcin PetscFunctionReturn(0); 252e0ab9aedSLisandro Dalcin } 253e0ab9aedSLisandro Dalcin 254c4aff060SBarry Smith /* ---------------------------------------------------------------- */ 2555180491cSLisandro Dalcin 2568cc058d9SJed Brown PETSC_EXTERN PetscErrorCode (*PetscPythonMonitorSet_C)(PetscObject,const char[]); 2570298fd71SBarry Smith PetscErrorCode (*PetscPythonMonitorSet_C)(PetscObject,const char[]) = NULL; 2585180491cSLisandro Dalcin 2595180491cSLisandro Dalcin /*@C 2605180491cSLisandro Dalcin PetscPythonMonitorSet - Set Python monitor 2615180491cSLisandro Dalcin 2625180491cSLisandro Dalcin Level: developer 2635180491cSLisandro Dalcin 2645180491cSLisandro Dalcin .keywords: Python 2655180491cSLisandro Dalcin 2665180491cSLisandro Dalcin @*/ 2675180491cSLisandro Dalcin PetscErrorCode PetscPythonMonitorSet(PetscObject obj, const char url[]) 2685180491cSLisandro Dalcin { 2695180491cSLisandro Dalcin PetscErrorCode ierr; 2706e111a19SKarl Rupp 2715180491cSLisandro Dalcin PetscFunctionBegin; 2725180491cSLisandro Dalcin PetscValidHeader(obj,1); 2735180491cSLisandro Dalcin PetscValidCharPointer(url,2); 2746c4ed002SBarry Smith if (!PetscPythonMonitorSet_C) { 2750298fd71SBarry Smith ierr = PetscPythonInitialize(NULL,NULL);CHKERRQ(ierr); 2766c4ed002SBarry Smith if (!PetscPythonMonitorSet_C) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"Couldn't initialize Python support for monitors"); 2775180491cSLisandro Dalcin } 2785180491cSLisandro Dalcin ierr = PetscPythonMonitorSet_C(obj,url);CHKERRQ(ierr); 2795180491cSLisandro Dalcin PetscFunctionReturn(0); 2805180491cSLisandro Dalcin } 2815180491cSLisandro Dalcin 2825180491cSLisandro Dalcin /* ---------------------------------------------------------------- */ 283