Actual source code: mpiuopen.c

petsc-3.4.4 2014-03-13
  1: #define PETSC_DESIRE_FEATURE_TEST_MACROS /* for popen */
  2: /*
  3:       Some PETSc utilites routines to add simple parallel IO capability
  4: */
  5: #include <petscsys.h>

  7: #include <errno.h>

 11: /*@C
 12:     PetscFOpen - Has the first process in the communicator open a file;
 13:     all others do nothing.

 15:     Logically Collective on MPI_Comm

 17:     Input Parameters:
 18: +   comm - the communicator
 19: .   name - the filename
 20: -   mode - the mode for fopen(), usually "w"

 22:     Output Parameter:
 23: .   fp - the file pointer

 25:     Level: developer

 27:     Notes:
 28:        NULL (0), "stderr" or "stdout" may be passed in as the filename

 30:     Fortran Note:
 31:     This routine is not supported in Fortran.

 33:     Concepts: opening ASCII file
 34:     Concepts: files^opening ASCII

 36: .seealso: PetscFClose(), PetscSynchronizedFGets(), PetscSynchronizedPrintf(), PetscSynchronizedFlush(),
 37:           PetscFPrintf()
 38: @*/
 39: PetscErrorCode  PetscFOpen(MPI_Comm comm,const char name[],const char mode[],FILE **fp)
 40: {
 42:   PetscMPIInt    rank;
 43:   FILE           *fd;
 44:   char           fname[PETSC_MAX_PATH_LEN],tname[PETSC_MAX_PATH_LEN];

 47:   MPI_Comm_rank(comm,&rank);
 48:   if (!rank) {
 49:     PetscBool isstdout,isstderr;
 50:     PetscStrcmp(name,"stdout",&isstdout);
 51:     PetscStrcmp(name,"stderr",&isstderr);
 52:     if (isstdout || !name) fd = PETSC_STDOUT;
 53:     else if (isstderr) fd = PETSC_STDERR;
 54:     else {
 55:       PetscStrreplace(PETSC_COMM_SELF,name,tname,PETSC_MAX_PATH_LEN);
 56:       PetscFixFilename(tname,fname);
 57:       PetscInfo1(0,"Opening file %s\n",fname);
 58:       fd   = fopen(fname,mode);
 59:       if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to open file %s\n",fname);
 60:     }
 61:   } else fd = 0;
 62:   *fp = fd;
 63:   return(0);
 64: }

 68: /*@
 69:     PetscFClose - Has the first processor in the communicator close a
 70:     file; all others do nothing.

 72:     Logically Collective on MPI_Comm

 74:     Input Parameters:
 75: +   comm - the communicator
 76: -   fd - the file, opened with PetscFOpen()

 78:    Level: developer

 80:     Fortran Note:
 81:     This routine is not supported in Fortran.

 83:     Concepts: files^closing ASCII
 84:     Concepts: closing file

 86: .seealso: PetscFOpen()
 87: @*/
 88: PetscErrorCode  PetscFClose(MPI_Comm comm,FILE *fd)
 89: {
 91:   PetscMPIInt    rank;
 92:   int            err;

 95:   MPI_Comm_rank(comm,&rank);
 96:   if (!rank && fd != PETSC_STDOUT && fd != PETSC_STDERR) {
 97:     err = fclose(fd);
 98:     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
 99:   }
100:   return(0);
101: }

103: #if defined(PETSC_HAVE_POPEN)

107: /*@C
108:       PetscPClose - Closes (ends) a program on processor zero run with PetscPOpen()

110:      Collective on MPI_Comm, but only process 0 runs the command

112:    Input Parameters:
113: +   comm - MPI communicator, only processor zero runs the program
114: -   fp - the file pointer where program input or output may be read or NULL if don't care

116:    Output Parameters:
117: .   rval - return value from pclose() or NULL to raise an error on failure

119:    Level: intermediate

121:    Notes:
122:        Does not work under Windows

124: .seealso: PetscFOpen(), PetscFClose(), PetscPOpen()

126: @*/
127: PetscErrorCode PetscPClose(MPI_Comm comm,FILE *fd,PetscInt *rval)
128: {
130:   PetscMPIInt    rank;
131:   int            err;

134:   MPI_Comm_rank(comm,&rank);
135:   if (!rank) {
136:     char buf[1024];
137:     while (fgets(buf,1024,fd)) ; /* wait till it prints everything */
138:     err = pclose(fd);
139:     if (rval) *rval = err;
140:     else if (err) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_SYS,"pclose() failed with error code %d, errno %d",err,errno);
141:   }
142:   return(0);
143: }


148: /*@C
149:       PetscPOpen - Runs a program on processor zero and sends either its input or output to
150:           a file.

152:      Logically Collective on MPI_Comm, but only process 0 runs the command

154:    Input Parameters:
155: +   comm - MPI communicator, only processor zero runs the program
156: .   machine - machine to run command on or NULL, or string with 0 in first location
157: .   program - name of program to run
158: -   mode - either r or w

160:    Output Parameter:
161: .   fp - the file pointer where program input or output may be read or NULL if don't care

163:    Level: intermediate

165:    Notes:
166:        Use PetscPClose() to close the file pointer when you are finished with it
167:        Does not work under Windows

169:        The program string may contain ${DISPLAY}, ${HOMEDIRECTORY} or ${WORKINGDIRECTORY}; these
170:     will be replaced with relevent values.

172: .seealso: PetscFOpen(), PetscFClose(), PetscPClose()

174: @*/
175: PetscErrorCode  PetscPOpen(MPI_Comm comm,const char machine[],const char program[],const char mode[],FILE **fp)
176: {
178:   PetscMPIInt    rank;
179:   size_t         i,len,cnt;
180:   char           commandt[PETSC_MAX_PATH_LEN],command[PETSC_MAX_PATH_LEN];
181:   FILE           *fd;

184:   /* all processors have to do the string manipulation because PetscStrreplace() is a collective operation */
185:   if (machine && machine[0]) {
186:     PetscStrcpy(command,"ssh ");
187:     PetscStrcat(command,machine);
188:     PetscStrcat(command," \" export DISPLAY=${DISPLAY}; ");
189:     /*
190:         Copy program into command but protect the " with a \ in front of it
191:     */
192:     PetscStrlen(command,&cnt);
193:     PetscStrlen(program,&len);
194:     for (i=0; i<len; i++) {
195:       if (program[i] == '\"') command[cnt++] = '\\';
196:       command[cnt++] = program[i];
197:     }
198:     command[cnt] = 0;

200:     PetscStrcat(command,"\"");
201:   } else {
202:     PetscStrcpy(command,program);
203:   }

205:   PetscStrreplace(comm,command,commandt,1024);

207:   MPI_Comm_rank(comm,&rank);
208:   if (!rank) {
209:     PetscInfo1(0,"Running command :%s\n",commandt);
210:     if (!(fd = popen(commandt,mode))) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot run command %s",commandt);
211:     if (fp) *fp = fd;
212:   }
213:   return(0);
214: }

216: #endif