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