/*@C PetscStartMatlab - starts up MATLAB with a MATLAB script Logically Collective on MPI_Comm, but only processor zero in the communicator does anything Input Parameters: + comm - MPI communicator . machine - optional machine to run MATLAB on - script - name of script (without the .m) Output Parameter: . fp - a file pointer returned from PetscPOpen() Level: intermediate Notes: This overwrites your matlab/startup.m file The script must be in your MATLAB path or current directory Assumes that all machines share a common file system .seealso: PetscPOpen(), PetscPClose() @*/ PetscErrorCode PetscStartMatlab(MPI_Comm comm,const char machine[],const char script[],FILE **fp) { PetscErrorCode ierr; FILE *fd; char command[512]; #if defined(PETSC_HAVE_UCBPS) && defined(PETSC_HAVE_POPEN) char buf[1024],*found; PetscMPIInt rank; #endif PetscFunctionBegin; #if defined(PETSC_HAVE_UCBPS) && defined(PETSC_HAVE_POPEN) /* check if MATLAB is not already running */ ierr = PetscPOpen(comm,machine,"/usr/ucb/ps -ugxww | grep matlab | grep -v grep","r",&fd);CHKERRQ(ierr); ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); if (!rank) found = fgets(buf,1024,fd); ierr = MPI_Bcast(&found,1,MPI_CHAR,0,comm);CHKERRQ(ierr); ierr = PetscPClose(comm,fd,NULL);CHKERRQ(ierr); if (found) PetscFunctionReturn(0); #endif if (script) { /* the remote machine won't know about current directory, so add it to MATLAB path */ /* the extra \" are to protect possible () in the script command from the shell */ sprintf(command,"echo \"delete ${HOMEDIRECTORY}/matlab/startup.m ; path(path,'${WORKINGDIRECTORY}'); %s \" > ${HOMEDIRECTORY}/matlab/startup.m",script); #if defined(PETSC_HAVE_POPEN) ierr = PetscPOpen(comm,machine,command,"r",&fd);CHKERRQ(ierr); ierr = PetscPClose(comm,fd,NULL);CHKERRQ(ierr); #endif } #if defined(PETSC_HAVE_POPEN) ierr = PetscPOpen(comm,machine,"xterm -display ${DISPLAY} -e matlab -nosplash","r",fp);CHKERRQ(ierr); #endif PetscFunctionReturn(0); }
PetscErrorCode PetscLs(MPI_Comm comm,const char libname[],char found[],size_t tlen,PetscBool *flg) { PetscErrorCode ierr; size_t len; char *f,program[PETSC_MAX_PATH_LEN]; FILE *fp; PetscFunctionBegin; ierr = PetscStrcpy(program,"ls ");CHKERRQ(ierr); ierr = PetscStrcat(program,libname);CHKERRQ(ierr); #if defined(PETSC_HAVE_POPEN) ierr = PetscPOpen(comm,PETSC_NULL,program,"r",&fp);CHKERRQ(ierr); #else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine"); #endif f = fgets(found,tlen,fp); if (f) *flg = PETSC_TRUE; else *flg = PETSC_FALSE; while (f) { ierr = PetscStrlen(found,&len);CHKERRQ(ierr); f = fgets(found+len,tlen-len,fp); } if (*flg) {ierr = PetscInfo2(0,"ls on %s gives \n%s\n",libname,found);CHKERRQ(ierr);} #if defined(PETSC_HAVE_POPEN) ierr = PetscPClose(comm,fp);CHKERRQ(ierr); #else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine"); #endif PetscFunctionReturn(0); }
static PetscErrorCode PetscViewerFileClose_ASCII(PetscViewer viewer) { PetscErrorCode ierr; PetscMPIInt rank; PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data; int err; PetscFunctionBegin; ierr = MPI_Comm_rank(((PetscObject)viewer)->comm,&rank);CHKERRQ(ierr); if (!rank && vascii->fd != stderr && vascii->fd != PETSC_STDOUT) { if (vascii->fd && vascii->closefile) { err = fclose(vascii->fd); if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file"); } if (vascii->storecompressed) { char par[PETSC_MAX_PATH_LEN],buf[PETSC_MAX_PATH_LEN]; FILE *fp; ierr = PetscStrcpy(par,"gzip ");CHKERRQ(ierr); ierr = PetscStrcat(par,vascii->filename);CHKERRQ(ierr); #if defined(PETSC_HAVE_POPEN) ierr = PetscPOpen(PETSC_COMM_SELF,PETSC_NULL,par,"r",&fp);CHKERRQ(ierr); if (fgets(buf,1024,fp)) { SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error from compression command %s\n%s",par,buf); } ierr = PetscPClose(PETSC_COMM_SELF,fp);CHKERRQ(ierr); #else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine"); #endif } } ierr = PetscFree(vascii->filename);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode PetscDrawDestroy_X(PetscDraw draw) { PetscDraw_X *win = (PetscDraw_X*)draw->data; PetscErrorCode ierr; #if defined(PETSC_HAVE_POPEN) char command[PETSC_MAX_PATH_LEN]; PetscMPIInt rank; FILE *fd; #endif PetscFunctionBegin; ierr = PetscDrawSynchronizedClear(draw);CHKERRQ(ierr); #if defined(PETSC_HAVE_POPEN) ierr = MPI_Comm_rank(((PetscObject)draw)->comm,&rank);CHKERRQ(ierr); if (draw->savefilename && !rank && draw->savefilemovie) { ierr = PetscSNPrintf(command,PETSC_MAX_PATH_LEN,"ffmpeg -i %s_%%d.Gif %s.m4v",draw->savefilename,draw->savefilename);CHKERRQ(ierr); ierr = PetscPOpen(((PetscObject)draw)->comm,PETSC_NULL,command,"r",&fd);CHKERRQ(ierr); ierr = PetscPClose(((PetscObject)draw)->comm,fd,PETSC_NULL);CHKERRQ(ierr); } #endif XFreeGC(win->disp,win->gc.set); XCloseDisplay(win->disp); ierr = PetscDrawDestroy(&draw->popup);CHKERRQ(ierr); ierr = PetscFree(win->font);CHKERRQ(ierr); ierr = PetscFree(win);CHKERRQ(ierr); PetscFunctionReturn(0); }
static PetscErrorCode PetscViewerFileClose_Binary(PetscViewer v) { PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data; PetscErrorCode ierr; PetscMPIInt rank; int err; PetscFunctionBegin; ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)v),&rank);CHKERRQ(ierr); if ((!rank || vbinary->btype == FILE_MODE_READ) && vbinary->fdes) { close(vbinary->fdes); if (!rank && vbinary->storecompressed) { char par[PETSC_MAX_PATH_LEN],buf[PETSC_MAX_PATH_LEN]; FILE *fp; /* compress the file */ ierr = PetscStrcpy(par,"gzip -f ");CHKERRQ(ierr); ierr = PetscStrcat(par,vbinary->filename);CHKERRQ(ierr); #if defined(PETSC_HAVE_POPEN) ierr = PetscPOpen(PETSC_COMM_SELF,NULL,par,"r",&fp);CHKERRQ(ierr); if (fgets(buf,1024,fp)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error from command %s\n%s",par,buf); ierr = PetscPClose(PETSC_COMM_SELF,fp,NULL);CHKERRQ(ierr); #else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine"); #endif } } if (vbinary->fdes_info) { err = fclose(vbinary->fdes_info); if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file"); } ierr = PetscFree(vbinary->filename);CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@C PetscEmacsClientErrorHandler - Error handler that uses the emacsclient program to load the file where the error occured. Then calls the "previous" error handler. Not Collective Input Parameters: + comm - communicator over which error occured . line - the line number of the error (indicated by __LINE__) . func - the function where error is detected (indicated by __FUNCT__) . file - the file in which the error was detected (indicated by __FILE__) . dir - the directory of the file (indicated by __SDIR__) . mess - an error text string, usually just printed to the screen . n - the generic error number . p - specific error number - ctx - error handler context Options Database Key: . -on_error_emacs <machinename> Level: developer Notes: You must put (server-start) in your .emacs file for the emacsclient software to work Most users need not directly employ this routine and the other error handlers, but can instead use the simplified interface SETERRQ, which has the calling sequence $ SETERRQ(PETSC_COMM_SELF,number,p,mess) Notes for experienced users: Use PetscPushErrorHandler() to set the desired error handler. Developer Note: Since this is an error handler it cannot call CHKERRQ(); thus we just return if an error is detected. Concepts: emacs^going to on error Concepts: error handler^going to line in emacs .seealso: PetscPushErrorHandler(), PetscAttachDebuggerErrorHandler(), PetscAbortErrorHandler() @*/ PetscErrorCode PetscEmacsClientErrorHandler(MPI_Comm comm,int line,const char *fun,const char *file,const char *dir,PetscErrorCode n,PetscErrorType p,const char *mess,void *ctx) { PetscErrorCode ierr; char command[PETSC_MAX_PATH_LEN]; const char *pdir; FILE *fp; PetscInt rval; PetscFunctionBegin; ierr = PetscGetPetscDir(&pdir);if (ierr) PetscFunctionReturn(ierr); sprintf(command,"cd %s; emacsclient --no-wait +%d %s%s\n",pdir,line,dir,file); #if defined(PETSC_HAVE_POPEN) ierr = PetscPOpen(MPI_COMM_WORLD,(char*)ctx,command,"r",&fp);if (ierr) PetscFunctionReturn(ierr); ierr = PetscPClose(MPI_COMM_WORLD,fp,&rval);if (ierr) PetscFunctionReturn(ierr); #else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine"); #endif ierr = PetscPopErrorHandler();if (ierr) PetscFunctionReturn(ierr); /* remove this handler from the stack of handlers */ if (!eh) { ierr = PetscTraceBackErrorHandler(comm,line,fun,file,dir,n,p,mess,0);if (ierr) PetscFunctionReturn(ierr); } else { ierr = (*eh->handler)(comm,line,fun,file,dir,n,p,mess,eh->ctx);if (ierr) PetscFunctionReturn(ierr); } PetscFunctionReturn(ierr); }
/* PFStringCreateFunction - Creates a function from a string Collective over PF Input Parameters: + pf - the function object - string - the string that defines the function Output Parameter: . f - the function pointer. .seealso: PFSetFromOptions() */ PetscErrorCode PETSCVEC_DLLEXPORT PFStringCreateFunction(PF pf,char *string,void **f) { #if defined(PETSC_USE_DYNAMIC_LIBRARIES) PetscErrorCode ierr; char task[1024],tmp[256],lib[PETSC_MAX_PATH_LEN],username[64]; FILE *fd; PetscTruth tmpshared,wdshared,keeptmpfiles = PETSC_FALSE; MPI_Comm comm; #endif PetscFunctionBegin; #if defined(PETSC_USE_DYNAMIC_LIBRARIES) ierr = PetscStrfree(pf->data);CHKERRQ(ierr); ierr = PetscStrallocpy(string,(char**)&pf->data);CHKERRQ(ierr); /* create the new C function and compile it */ ierr = PetscSharedTmp(((PetscObject)pf)->comm,&tmpshared);CHKERRQ(ierr); ierr = PetscSharedWorkingDirectory(((PetscObject)pf)->comm,&wdshared);CHKERRQ(ierr); if (tmpshared) { /* do it in /tmp since everyone has one */ ierr = PetscGetTmp(((PetscObject)pf)->comm,tmp,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); comm = ((PetscObject)pf)->comm; } else if (!wdshared) { /* each one does in private /tmp */ ierr = PetscGetTmp(((PetscObject)pf)->comm,tmp,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); comm = PETSC_COMM_SELF; } else { /* do it in current directory */ ierr = PetscStrcpy(tmp,".");CHKERRQ(ierr); comm = ((PetscObject)pf)->comm; } ierr = PetscOptionsGetTruth(((PetscObject)pf)->prefix,"-pf_string_keep_files",&keeptmpfiles,PETSC_NULL);CHKERRQ(ierr); if (keeptmpfiles) { sprintf(task,"cd %s ; mkdir ${USERNAME} ; cd ${USERNAME} ; \\cp -f ${PETSC_DIR}/src/pf/impls/string/makefile ./makefile ; ke MIN=%d NOUT=%d petscdlib STRINGFUNCTION=\"%s\" ; sync\n",tmp,(int)pf->dimin,(int)pf->dimout,string); } else { sprintf(task,"cd %s ; mkdir ${USERNAME} ;cd ${USERNAME} ; \\cp -f ${PETSC_DIR}/src/pf/impls/string/makefile ./makefile ; make MIN=%d NOUT=%d -f makefile petscdlib STRINGFUNCTION=\"%s\" ; \\rm -f makefile petscdlib.c libpetscdlib.a ; sync\n",tmp,(int)pf->dimin,(int)pf->dimout,string); } #if defined(PETSC_HAVE_POPEN) ierr = PetscPOpen(comm,PETSC_NULL,task,"r",&fd);CHKERRQ(ierr); ierr = PetscPClose(comm,fd);CHKERRQ(ierr); #else SETERRQ(PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine"); #endif ierr = MPI_Barrier(comm);CHKERRQ(ierr); /* load the apply function from the dynamic library */ ierr = PetscGetUserName(username,64);CHKERRQ(ierr); sprintf(lib,"%s/%s/libpetscdlib",tmp,username); ierr = PetscDLLibrarySym(comm,PETSC_NULL,lib,"PFApply_String",f);CHKERRQ(ierr); #endif PetscFunctionReturn(0); }
static PetscErrorCode PetscPythonFindLibrary(char pythonexe[PETSC_MAX_PATH_LEN],char pythonlib[PETSC_MAX_PATH_LEN]) { const char cmdline[] = "-c 'import sys; print(sys.exec_prefix); print(sys.version[:3])'"; char command[PETSC_MAX_PATH_LEN+1+sizeof(cmdline)+1]; char prefix[PETSC_MAX_PATH_LEN],version[8],sep[2]={PETSC_DIR_SEPARATOR, 0},*eol; FILE *fp = NULL; char path[PETSC_MAX_PATH_LEN+1]; PetscBool found = PETSC_FALSE; PetscErrorCode ierr; PetscFunctionBegin; #if defined(PETSC_PYTHON_LIB) ierr = PetscStrcpy(pythonlib,PETSC_PYTHON_LIB);CHKERRQ(ierr); PetscFunctionReturn(0); #endif /* call Python to find out the name of the Python dynamic library */ ierr = PetscStrncpy(command,pythonexe,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); ierr = PetscStrcat(command," ");CHKERRQ(ierr); ierr = PetscStrcat(command,cmdline);CHKERRQ(ierr); #if defined(PETSC_HAVE_POPEN) ierr = PetscPOpen(PETSC_COMM_SELF,NULL,command,"r",&fp);CHKERRQ(ierr); if (!fgets(prefix,sizeof(prefix),fp)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Python: bad output from executable: %s",pythonexe); if (!fgets(version,sizeof(version),fp)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Python: bad output from executable: %s",pythonexe); ierr = PetscPClose(PETSC_COMM_SELF,fp,NULL);CHKERRQ(ierr); #else SETERRQ(PETSC_COMM_SELF,1,"Python: Aborted due to missing popen()"); #endif /* remove newlines */ ierr = PetscStrchr(prefix,'\n',&eol);CHKERRQ(ierr); if (eol) eol[0] = 0; ierr = PetscStrchr(version,'\n',&eol);CHKERRQ(ierr); if (eol) eol[0] = 0; /* test for $prefix/lib64/libpythonX.X[.so]*/ ierr = PetscStrcpy(pythonlib,prefix);CHKERRQ(ierr); ierr = PetscStrcat(pythonlib,sep);CHKERRQ(ierr); ierr = PetscStrcat(pythonlib,"lib64");CHKERRQ(ierr); ierr = PetscTestDirectory(pythonlib,'r',&found);CHKERRQ(ierr); if (found) { ierr = PetscStrcat(pythonlib,sep);CHKERRQ(ierr); ierr = PetscStrcat(pythonlib,"libpython");CHKERRQ(ierr); ierr = PetscStrcat(pythonlib,version);CHKERRQ(ierr); ierr = PetscDLLibraryRetrieve(PETSC_COMM_SELF,pythonlib,path,PETSC_MAX_PATH_LEN,&found);CHKERRQ(ierr); if (found) PetscFunctionReturn(0); } /* test for $prefix/lib/libpythonX.X[.so]*/ ierr = PetscStrcpy(pythonlib,prefix);CHKERRQ(ierr); ierr = PetscStrcat(pythonlib,sep);CHKERRQ(ierr); ierr = PetscStrcat(pythonlib,"lib");CHKERRQ(ierr); ierr = PetscTestDirectory(pythonlib,'r',&found);CHKERRQ(ierr); if (found) { ierr = PetscStrcat(pythonlib,sep);CHKERRQ(ierr); ierr = PetscStrcat(pythonlib,"libpython");CHKERRQ(ierr); ierr = PetscStrcat(pythonlib,version);CHKERRQ(ierr); ierr = PetscDLLibraryRetrieve(PETSC_COMM_SELF,pythonlib,path,PETSC_MAX_PATH_LEN,&found);CHKERRQ(ierr); if (found) PetscFunctionReturn(0); } /* nothing good found */ ierr = PetscMemzero(pythonlib,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); ierr = PetscInfo(0,"Python dynamic library not found\n");CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@C PetscFileRetrieve - Obtains a library from a URL or compressed and copies into local disk space as uncompressed. Collective on MPI_Comm Input Parameter: + comm - processors accessing the library . libname - name of library, including entire URL (with or without .gz) - llen - length of llibname Output Parameter: + llibname - name of local copy of library - found - if found and retrieved the file Level: developer @*/ PetscErrorCode PetscFileRetrieve(MPI_Comm comm,const char libname[],char llibname[],size_t llen,PetscBool *found) { char buf[1024],tmpdir[PETSC_MAX_PATH_LEN],urlget[PETSC_MAX_PATH_LEN],*par; const char *pdir; FILE *fp; PetscErrorCode ierr; int i; PetscMPIInt rank; size_t len = 0; PetscBool flg1,flg2,flg3,sharedtmp,exists; #if defined(PETSC_HAVE_POPEN) int rval; #endif PetscFunctionBegin; *found = PETSC_FALSE; /* if file does not have an ftp:// or http:// or .gz then need not process file */ ierr = PetscStrstr(libname,".gz",&par);CHKERRQ(ierr); if (par) {ierr = PetscStrlen(par,&len);CHKERRQ(ierr);} ierr = PetscStrncmp(libname,"ftp://",6,&flg1);CHKERRQ(ierr); ierr = PetscStrncmp(libname,"http://",7,&flg2);CHKERRQ(ierr); ierr = PetscStrncmp(libname,"file://",7,&flg3);CHKERRQ(ierr); if (!flg1 && !flg2 && !flg3 && (!par || len != 3)) { ierr = PetscStrncpy(llibname,libname,llen);CHKERRQ(ierr); ierr = PetscTestFile(libname,'r',found);CHKERRQ(ierr); if (*found) { ierr = PetscInfo1(NULL,"Found file %s\n",libname);CHKERRQ(ierr); } else { ierr = PetscInfo1(NULL,"Did not find file %s\n",libname);CHKERRQ(ierr); } PetscFunctionReturn(0); } if (par && len == 3){ size_t llen; ierr = PetscStrlen(libname,&llen);CHKERRQ(ierr); ierr = PetscStrncpy(llibname,libname,llen);CHKERRQ(ierr); llibname[llen-len] = 0; ierr = PetscTestFile(llibname,'r',found);CHKERRQ(ierr); if (*found) { ierr = PetscInfo1(NULL,"Found uncompressed version of file %s\n",llibname);CHKERRQ(ierr); PetscFunctionReturn(0); } else { ierr = PetscInfo1(NULL,"Did not find uncompressed version of file %s\n",libname);CHKERRQ(ierr); } } /* Determine if all processors share a common /tmp */ ierr = PetscSharedTmp(comm,&sharedtmp);CHKERRQ(ierr); ierr = PetscOptionsGetenv(comm,"PETSC_TMP",tmpdir,PETSC_MAX_PATH_LEN,&flg1);CHKERRQ(ierr); ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); if (!rank || !sharedtmp) { /* Construct the script to get URL file */ ierr = PetscGetPetscDir(&pdir);CHKERRQ(ierr); ierr = PetscStrcpy(urlget,pdir);CHKERRQ(ierr); ierr = PetscStrcat(urlget,"/bin/urlget");CHKERRQ(ierr); ierr = PetscTestFile(urlget,'r',&exists);CHKERRQ(ierr); if (!exists) { ierr = PetscTestFile("urlget",'r',&exists);CHKERRQ(ierr); if (!exists) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Cannot locate PETSc script urlget in %s or current directory",urlget); ierr = PetscStrcpy(urlget,"urlget");CHKERRQ(ierr); } ierr = PetscStrcat(urlget," ");CHKERRQ(ierr); /* are we using an alternative /tmp? */ if (flg1) { ierr = PetscStrcat(urlget,"-tmp ");CHKERRQ(ierr); ierr = PetscStrcat(urlget,tmpdir);CHKERRQ(ierr); ierr = PetscStrcat(urlget," ");CHKERRQ(ierr); } ierr = PetscStrcat(urlget,libname);CHKERRQ(ierr); ierr = PetscStrcat(urlget," 2>&1 ");CHKERRQ(ierr); #if defined(PETSC_HAVE_POPEN) ierr = PetscPOpen(PETSC_COMM_SELF,NULL,urlget,"r",&fp);CHKERRQ(ierr); #else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine"); #endif if (!fgets(buf,1024,fp)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"No output from ${PETSC_DIR}/bin/urlget in getting file %s",libname); ierr = PetscInfo1(0,"Message back from urlget: %s\n",buf);CHKERRQ(ierr); ierr = PetscStrncmp(buf,"Error",5,&flg1);CHKERRQ(ierr); ierr = PetscStrncmp(buf,"Traceback",9,&flg2);CHKERRQ(ierr); #if defined(PETSC_HAVE_POPEN) ierr = PetscPClose(PETSC_COMM_SELF,fp,&rval);CHKERRQ(ierr); #endif if (flg1 || flg2) *found = PETSC_FALSE; else { *found = PETSC_TRUE; /* Check for \n and make it 0 */ for (i=0; i<1024; i++) { if (buf[i] == '\n') { buf[i] = 0; break; } } ierr = PetscStrncpy(llibname,buf,llen);CHKERRQ(ierr); } } if (sharedtmp) { /* send library name to all processors */ ierr = MPI_Bcast(found,1,MPIU_BOOL,0,comm);CHKERRQ(ierr); if (*found) { ierr = MPI_Bcast(llibname,llen,MPI_CHAR,0,comm);CHKERRQ(ierr); ierr = MPI_Bcast(found,1,MPIU_BOOL,0,comm);CHKERRQ(ierr); } } PetscFunctionReturn(0); }
PetscErrorCode waitForSignal(PetscInt waitTime) { PetscErrorCode ierr; FILE *fd1; PetscViewer fd; PetscInt iDone=0; PetscInt zero=0; PetscInt one=1; PetscInt fp; PetscInt numsignalfiles = 2; PetscTruth flg; if (firstTime) { ierr = PetscPrintf(PETSC_COMM_WORLD,"Initializing signaling module ...\n");CHKERRQ(ierr); ierr = PetscOptionsGetString(PETSC_NULL,"-signalscript",externalSignalScriptName,PETSC_MAX_PATH_LEN-1,&useExternalSignalScript);CHKERRQ(ierr); ierr = PetscOptionsHasName(PETSC_NULL,"-signalfiles",&useSignalFiles);CHKERRQ(ierr); if ((useExternalSignalScript) && (useSignalFiles)) { SETERRQ(1,"Cannot specify both an external signal script and signal files!"); } if ((!useExternalSignalScript) && (!useSignalFiles)) { SETERRQ(1,"Must specify an external signal script OR signal files!"); } if (useExternalSignalScript) { ierr = PetscPrintf(PETSC_COMM_WORLD,"External signaling script has been specified: %s\n",externalSignalScriptName);CHKERRQ(ierr); } if (useSignalFiles) { ierr = PetscOptionsGetStringArray(PETSC_NULL,"-signalfiles",signalfiles,&numsignalfiles,&flg);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"Signal files have been specified: %s, %s\n",signalfiles[0],signalfiles[1]);CHKERRQ(ierr); ierr = PetscOptionsGetInt(PETSC_NULL,"-signalwaittime",&signalWaitTime,&flg);CHKERRQ(ierr); if (flg) { ierr = PetscPrintf(PETSC_COMM_WORLD,"Signal wait time of %d seconds has been specified\n",signalWaitTime);CHKERRQ(ierr); } else { signalWaitTime = -1; } } firstTime = PETSC_FALSE; } else { if (useExternalSignalScript) { ierr = PetscPOpen(PETSC_COMM_WORLD,PETSC_NULL,externalSignalScriptName,"r",&fd1);CHKERRQ(ierr); ierr = PetscPClose(PETSC_COMM_WORLD,fd1);CHKERRQ(ierr); } else { if (signalWaitTime>0) waitTime = signalWaitTime; /* overwrite with runtime option */ /* overwrite external signal file */ ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,signalfiles[1],FILE_MODE_WRITE,&fd);CHKERRQ(ierr); ierr = PetscViewerBinaryGetDescriptor(fd,&fp);CHKERRQ(ierr); ierr = PetscBinarySynchronizedWrite(PETSC_COMM_WORLD,fp,&zero,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); ierr = PetscViewerDestroy(fd);CHKERRQ(ierr); /* send "ready" signal */ ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD,signalfiles[0],&fd);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(fd,"%d\n",one);CHKERRQ(ierr); ierr = PetscViewerDestroy(fd);CHKERRQ(ierr); /* wait for external signal */ while (iDone==0) { ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,signalfiles[1],FILE_MODE_READ,&fd);CHKERRQ(ierr); ierr = PetscViewerBinaryGetDescriptor(fd,&fp);CHKERRQ(ierr); ierr = PetscBinarySynchronizedRead(PETSC_COMM_WORLD,fp,&iDone,1,PETSC_INT);CHKERRQ(ierr); ierr = PetscViewerDestroy(fd);CHKERRQ(ierr); PetscSleep(waitTime); } /* send "busy" signal */ ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD,signalfiles[0],&fd);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(fd,"%d\n",zero);CHKERRQ(ierr); ierr = PetscViewerDestroy(fd);CHKERRQ(ierr); } } return 0; }