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