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