Actual source code: smatlab.c

      1: #define PETSC_DLL

     3:  #include petsc.h
     4:  #include petscsys.h

      8: /*@C
      9:     PetscStartMatlab - starts up Matlab with a Matlab script

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

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

     18:     Output Parameter:
     19: .     fp - a file pointer returned from PetscPOpen()

     21:     Level: intermediate

     23:     Notes:
     24:      This overwrites your matlab/startup.m file

     26:      The script must be in your Matlab path or current directory

     28:      Assumes that all machines share a common file system

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

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

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

     69:   return(0);
     70: }