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: {
 30:   FILE *fd;
 31:   char  command[512];
 32: #if defined(PETSC_HAVE_UCBPS) && defined(PETSC_HAVE_POPEN)
 33:   char        buf[1024], *found;
 34:   PetscMPIInt rank;
 35: #endif

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

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