Actual source code: smatlab.c

      2: #include <petscsys.h>

      4: /*@C
      5:     PetscStartMatlab - starts up MATLAB with a MATLAB script

      7:     Logically Collective, but only processor zero in the communicator does anything

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

     14:     Output Parameter:
     15: .     fp - a file pointer returned from PetscPOpen()

     17:     Level: intermediate

     19:     Notes:
     20:      This overwrites your matlab/startup.m file

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

     24:      Assumes that all machines share a common file system

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

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

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