xref: /petsc/src/sys/fileio/smatlab.c (revision 2b8d69ca7ea5fe9190df62c1dce3bbd66fce84dd)
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 #if defined(PETSC_HAVE_UCBPS) && defined(PETSC_HAVE_POPEN)
42   /* check if MATLAB is not already running */
43   ierr = PetscPOpen(comm,machine,"/usr/ucb/ps -ugxww | grep matlab | grep -v grep","r",&fd);CHKERRQ(ierr);
44   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
45   if (!rank) found = fgets(buf,1024,fd);
46   ierr = MPI_Bcast(&found,1,MPI_CHAR,0,comm);CHKERRQ(ierr);
47   ierr = PetscPClose(comm,fd,NULL);CHKERRQ(ierr);
48   if (found) PetscFunctionReturn(0);
49 #endif
50 
51   if (script) {
52     /* the remote machine won't know about current directory, so add it to MATLAB path */
53     /* the extra \" are to protect possible () in the script command from the shell */
54     sprintf(command,"echo \"delete ${HOMEDIRECTORY}/matlab/startup.m ; path(path,'${WORKINGDIRECTORY}'); %s  \" > ${HOMEDIRECTORY}/matlab/startup.m",script);
55 #if defined(PETSC_HAVE_POPEN)
56     ierr = PetscPOpen(comm,machine,command,"r",&fd);CHKERRQ(ierr);
57     ierr = PetscPClose(comm,fd,NULL);CHKERRQ(ierr);
58 #endif
59   }
60 #if defined(PETSC_HAVE_POPEN)
61   ierr = PetscPOpen(comm,machine,"xterm -display ${DISPLAY} -e matlab -nosplash","r",fp);CHKERRQ(ierr);
62 #endif
63   PetscFunctionReturn(0);
64 }
65 
66