1 #define PETSC_DLL 2 3 #include "petscsys.h" 4 5 #undef __FUNCT__ 6 #define __FUNCT__ "PetscStartMatlab" 7 /*@C 8 PetscStartMatlab - starts up Matlab with a Matlab script 9 10 Logically Collective on MPI_Comm, but only processor zero in the communicator does anything 11 12 Input Parameters: 13 + comm - MPI communicator 14 . machine - optional machine to run Matlab on 15 - script - name of script (without the .m) 16 17 Output Parameter: 18 . fp - a file pointer returned from PetscPOpen() 19 20 Level: intermediate 21 22 Notes: 23 This overwrites your matlab/startup.m file 24 25 The script must be in your Matlab path or current directory 26 27 Assumes that all machines share a common file system 28 29 .seealso: PetscPOpen(), PetscPClose() 30 @*/ 31 PetscErrorCode PETSC_DLLEXPORT PetscStartMatlab(MPI_Comm comm,const char machine[],const char script[],FILE **fp) 32 { 33 PetscErrorCode ierr; 34 FILE *fd; 35 char command[512]; 36 #if defined(PETSC_HAVE_UCBPS) && defined(PETSC_HAVE_POPEN) 37 char buf[1024],*found; 38 PetscMPIInt rank; 39 #endif 40 41 PetscFunctionBegin; 42 43 #if defined(PETSC_HAVE_UCBPS) && defined(PETSC_HAVE_POPEN) 44 /* check if Matlab is not already running */ 45 ierr = PetscPOpen(comm,machine,"/usr/ucb/ps -ugxww | grep matlab | grep -v grep","r",&fd);CHKERRQ(ierr); 46 ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 47 if (!rank) { 48 found = fgets(buf,1024,fd); 49 } 50 ierr = MPI_Bcast(&found,1,MPI_CHAR,0,comm);CHKERRQ(ierr); 51 ierr = PetscPClose(comm,fd);CHKERRQ(ierr); 52 if (found) PetscFunctionReturn(0); 53 #endif 54 55 if (script) { 56 /* the remote machine won't know about current directory, so add it to Matlab path */ 57 /* the extra \" are to protect possible () in the script command from the shell */ 58 sprintf(command,"echo \"delete ${HOMEDIRECTORY}/matlab/startup.m ; path(path,'${WORKINGDIRECTORY}'); %s \" > ${HOMEDIRECTORY}/matlab/startup.m",script); 59 #if defined(PETSC_HAVE_POPEN) 60 ierr = PetscPOpen(comm,machine,command,"r",&fd);CHKERRQ(ierr); 61 ierr = PetscPClose(comm,fd);CHKERRQ(ierr); 62 #endif 63 } 64 #if defined(PETSC_HAVE_POPEN) 65 ierr = PetscPOpen(comm,machine,"xterm -display ${DISPLAY} -e matlab -nosplash","r",fp);CHKERRQ(ierr); 66 #endif 67 68 PetscFunctionReturn(0); 69 } 70 71