Actual source code: smatlab.c

petsc-3.3-p7 2013-05-11
  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


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

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

 67:   return(0);
 68: }