Actual source code: smatlab.c

petsc-3.7.3 2016-08-01
Report Typos and Errors
  2: #include <petscsys.h>

  6: /*@C
  7:     PetscStartMatlab - starts up MATLAB with a MATLAB script

  9:     Logically Collective on MPI_Comm, but only processor zero in the communicator does anything

 11:     Input Parameters:
 12: +     comm - MPI communicator
 13: .     machine - optional machine to run MATLAB on
 14: -     script - name of script (without the .m)

 16:     Output Parameter:
 17: .     fp - a file pointer returned from PetscPOpen()

 19:     Level: intermediate

 21:     Notes:
 22:      This overwrites your matlab/startup.m file

 24:      The script must be in your MATLAB path or current directory

 26:      Assumes that all machines share a common file system

 28: .seealso: PetscPOpen(), PetscPClose()
 29: @*/
 30: PetscErrorCode  PetscStartMatlab(MPI_Comm comm,const char machine[],const char script[],FILE **fp)
 31: {
 33:   FILE           *fd;
 34:   char           command[512];
 35: #if defined(PETSC_HAVE_UCBPS) && defined(PETSC_HAVE_POPEN)
 36:   char           buf[1024],*found;
 37:   PetscMPIInt    rank;
 38: #endif

 41: #if defined(PETSC_HAVE_UCBPS) && defined(PETSC_HAVE_POPEN)
 42:   /* check if MATLAB is not already running */
 43:   PetscPOpen(comm,machine,"/usr/ucb/ps -ugxww | grep matlab | grep -v grep","r",&fd);
 44:   MPI_Comm_rank(comm,&rank);
 45:   if (!rank) found = fgets(buf,1024,fd);
 46:   MPI_Bcast(&found,1,MPI_CHAR,0,comm);
 47:   PetscPClose(comm,fd,NULL);
 48:   if (found) return(0);
 49: #endif

 51:   if (script) {
 52:     /* the remote machine won't know about current directory, so add it to MATLAB path */
 53:     /* the extra \" are to protect possible () in the script command from the shell */
 54:     sprintf(command,"echo \"delete ${HOMEDIRECTORY}/matlab/startup.m ; path(path,'${WORKINGDIRECTORY}'); %s  \" > ${HOMEDIRECTORY}/matlab/startup.m",script);
 55: #if defined(PETSC_HAVE_POPEN)
 56:     PetscPOpen(comm,machine,command,"r",&fd);
 57:     PetscPClose(comm,fd,NULL);
 58: #endif
 59:   }
 60: #if defined(PETSC_HAVE_POPEN)
 61:   PetscPOpen(comm,machine,"xterm -display ${DISPLAY} -e matlab -nosplash","r",fp);
 62: #endif
 63:   return(0);
 64: }