/*@C PetscFOpen - Has the first process in the communicator open a file; all others do nothing. Logically Collective on MPI_Comm Input Parameters: + comm - the communicator . name - the filename - mode - the mode for fopen(), usually "w" Output Parameter: . fp - the file pointer Level: developer Notes: NULL (0), "stderr" or "stdout" may be passed in as the filename Fortran Note: This routine is not supported in Fortran. Concepts: opening ASCII file Concepts: files^opening ASCII .seealso: PetscFClose(), PetscSynchronizedFGets(), PetscSynchronizedPrintf(), PetscSynchronizedFlush(), PetscFPrintf() @*/ PetscErrorCode PetscFOpen(MPI_Comm comm,const char name[],const char mode[],FILE **fp) { PetscErrorCode ierr; PetscMPIInt rank; FILE *fd; char fname[PETSC_MAX_PATH_LEN],tname[PETSC_MAX_PATH_LEN]; PetscFunctionBegin; ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); if (!rank) { PetscBool isstdout,isstderr; ierr = PetscStrcmp(name,"stdout",&isstdout);CHKERRQ(ierr); ierr = PetscStrcmp(name,"stderr",&isstderr);CHKERRQ(ierr); if (isstdout || !name) fd = PETSC_STDOUT; else if (isstderr) fd = PETSC_STDERR; else { PetscBool devnull; ierr = PetscStrreplace(PETSC_COMM_SELF,name,tname,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); ierr = PetscFixFilename(tname,fname);CHKERRQ(ierr); ierr = PetscStrbeginswith(fname,"/dev/null",&devnull);CHKERRQ(ierr); if (devnull) { ierr = PetscStrcpy(fname,"/dev/null");CHKERRQ(ierr); } ierr = PetscInfo1(0,"Opening file %s\n",fname);CHKERRQ(ierr); fd = fopen(fname,mode); if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to open file %s\n",fname); } } else fd = 0; *fp = fd; PetscFunctionReturn(0); }
/*@C PetscPOpen - Runs a program on processor zero and sends either its input or output to a file. Logically Collective on MPI_Comm, but only process 0 runs the command Input Parameters: + comm - MPI communicator, only processor zero runs the program . machine - machine to run command on or NULL, or string with 0 in first location . program - name of program to run - mode - either r or w Output Parameter: . fp - the file pointer where program input or output may be read or NULL if don't care Level: intermediate Notes: Use PetscPClose() to close the file pointer when you are finished with it Does not work under Windows If machine is not provided will use the value set with PetsPOpenSetMachine() if that was provided, otherwise will use the machine running node zero of the communicator The program string may contain ${DISPLAY}, ${HOMEDIRECTORY} or ${WORKINGDIRECTORY}; these will be replaced with relevent values. .seealso: PetscFOpen(), PetscFClose(), PetscPClose(), PetscPOpenSetMachine() @*/ PetscErrorCode PetscPOpen(MPI_Comm comm,const char machine[],const char program[],const char mode[],FILE **fp) { PetscErrorCode ierr; PetscMPIInt rank; size_t i,len,cnt; char commandt[PETSC_MAX_PATH_LEN],command[PETSC_MAX_PATH_LEN]; FILE *fd; PetscFunctionBegin; /* all processors have to do the string manipulation because PetscStrreplace() is a collective operation */ if (PetscPOpenMachine[0] || (machine && machine[0])) { ierr = PetscStrcpy(command,"ssh ");CHKERRQ(ierr); if (PetscPOpenMachine[0]) { ierr = PetscStrcat(command,PetscPOpenMachine);CHKERRQ(ierr); } else { ierr = PetscStrcat(command,machine);CHKERRQ(ierr); } ierr = PetscStrcat(command," \" export DISPLAY=${DISPLAY}; ");CHKERRQ(ierr); /* Copy program into command but protect the " with a \ in front of it */ ierr = PetscStrlen(command,&cnt);CHKERRQ(ierr); ierr = PetscStrlen(program,&len);CHKERRQ(ierr); for (i=0; i<len; i++) { if (program[i] == '\"') command[cnt++] = '\\'; command[cnt++] = program[i]; } command[cnt] = 0; ierr = PetscStrcat(command,"\"");CHKERRQ(ierr); } else { ierr = PetscStrcpy(command,program);CHKERRQ(ierr); } ierr = PetscStrreplace(comm,command,commandt,1024);CHKERRQ(ierr); ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); if (!rank) { ierr = PetscInfo1(0,"Running command :%s\n",commandt);CHKERRQ(ierr); if (!(fd = popen(commandt,mode))) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot run command %s",commandt); if (fp) *fp = fd; } PetscFunctionReturn(0); }
/*@C PetscPythonInitialize - Initialize Python and import petsc4py. Input Parameter: + pyexe - path to the Python interpreter executable, or NULL. - pylib - full path to the Python dynamic library, or NULL. Level: intermediate .keywords: Python @*/ PetscErrorCode PetscPythonInitialize(const char pyexe[],const char pylib[]) { PyObject *module = 0; PetscErrorCode ierr; PetscFunctionBegin; if (PetscBeganPython) PetscFunctionReturn(0); /* Python executable */ if (pyexe && pyexe[0] != 0) { ierr = PetscStrncpy(PetscPythonExe,pyexe,sizeof(PetscPythonExe));CHKERRQ(ierr); } else { ierr = PetscPythonFindExecutable(PetscPythonExe);CHKERRQ(ierr); } /* Python dynamic library */ if (pylib && pylib[0] != 0) { ierr = PetscStrncpy(PetscPythonLib,pylib,sizeof(PetscPythonLib));CHKERRQ(ierr); } else { ierr = PetscPythonFindLibrary(PetscPythonExe,PetscPythonLib);CHKERRQ(ierr); } /* dynamically load Python library */ ierr = PetscPythonLoadLibrary(PetscPythonLib);CHKERRQ(ierr); /* initialize Python */ PetscBeganPython = PETSC_FALSE; if (!Py_IsInitialized()) { static PetscBool registered = PETSC_FALSE; const char *py_version; PyObject *sys_path; char path[PETSC_MAX_PATH_LEN] = { 0 }; /* initialize Python */ Py_InitializeEx(0); /* 0: do not install signal handlers */ /* build 'sys.argv' list */ py_version = Py_GetVersion(); if (py_version[0] == '2') { int argc = 0; char **argv = 0; ierr = PetscGetArgs(&argc,&argv);CHKERRQ(ierr); PySys_SetArgv(argc,argv); } if (py_version[0] == '3') { /* XXX 'argv' is type 'wchar_t**' */ PySys_SetArgv(0,NULL); } /* add PETSC_LIB_DIR in front of 'sys.path' */ sys_path = PySys_GetObject("path"); if (sys_path) { ierr = PetscStrreplace(PETSC_COMM_SELF,"${PETSC_LIB_DIR}",path,sizeof(path));CHKERRQ(ierr); Py_DecRef(PyObject_CallMethod(sys_path,"insert","is",(int)0,(char*)path)); } /* register finalizer */ if (!registered) { ierr = PetscRegisterFinalize(PetscPythonFinalize);CHKERRQ(ierr); registered = PETSC_TRUE; } PetscBeganPython = PETSC_TRUE; } /* import 'petsc4py.PETSc' module */ module = PyImport_ImportModule("petsc4py.PETSc"); if (module) { ierr = PetscInfo(0,"Python: successfully imported module 'petsc4py.PETSc'\n");CHKERRQ(ierr); Py_DecRef(module); module = 0; } else { PetscPythonPrintError(); SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Python: could not import module 'petsc4py.PETSc', perhaps your PYTHONPATH does not contain it\n"); } PetscFunctionReturn(0); }