xref: /petsc/src/sys/fileio/mpiuopen.c (revision 2205254efee3a00a594e5e2a3a70f74dcb40bc03)
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>
6 #include <stdarg.h>
7 #if defined(PETSC_HAVE_STDLIB_H)
8 #include <stdlib.h>
9 #endif
10 
11 #undef __FUNCT__
12 #define __FUNCT__ "PetscFOpen"
13 /*@C
14     PetscFOpen - Has the first process in the communicator open a file;
15     all others do nothing.
16 
17     Logically Collective on MPI_Comm
18 
19     Input Parameters:
20 +   comm - the communicator
21 .   name - the filename
22 -   mode - the mode for fopen(), usually "w"
23 
24     Output Parameter:
25 .   fp - the file pointer
26 
27     Level: developer
28 
29     Notes:
30        PETSC_NULL (0), "stderr" or "stdout" may be passed in as the filename
31 
32     Fortran Note:
33     This routine is not supported in Fortran.
34 
35     Concepts: opening ASCII file
36     Concepts: files^opening ASCII
37 
38 .seealso: PetscFClose(), PetscSynchronizedFGets(), PetscSynchronizedPrintf(), PetscSynchronizedFlush(),
39           PetscFPrintf()
40 @*/
41 PetscErrorCode  PetscFOpen(MPI_Comm comm,const char name[],const char mode[],FILE **fp)
42 {
43   PetscErrorCode ierr;
44   PetscMPIInt    rank;
45   FILE           *fd;
46   char           fname[PETSC_MAX_PATH_LEN],tname[PETSC_MAX_PATH_LEN];
47 
48   PetscFunctionBegin;
49   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
50   if (!rank) {
51     PetscBool isstdout,isstderr;
52     ierr = PetscStrcmp(name,"stdout",&isstdout);CHKERRQ(ierr);
53     ierr = PetscStrcmp(name,"stderr",&isstderr);CHKERRQ(ierr);
54     if (isstdout || !name) fd = PETSC_STDOUT;
55     else if (isstderr) fd = PETSC_STDERR;
56     else {
57       ierr = PetscStrreplace(PETSC_COMM_SELF,name,tname,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
58       ierr = PetscFixFilename(tname,fname);CHKERRQ(ierr);
59       ierr = PetscInfo1(0,"Opening file %s\n",fname);CHKERRQ(ierr);
60       fd   = fopen(fname,mode);
61       if (!fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to open file %s\n",fname);
62     }
63   } else fd = 0;
64   *fp = fd;
65   PetscFunctionReturn(0);
66 }
67 
68 #undef __FUNCT__
69 #define __FUNCT__ "PetscFClose"
70 /*@
71     PetscFClose - Has the first processor in the communicator close a
72     file; all others do nothing.
73 
74     Logically Collective on MPI_Comm
75 
76     Input Parameters:
77 +   comm - the communicator
78 -   fd - the file, opened with PetscFOpen()
79 
80    Level: developer
81 
82     Fortran Note:
83     This routine is not supported in Fortran.
84 
85     Concepts: files^closing ASCII
86     Concepts: closing file
87 
88 .seealso: PetscFOpen()
89 @*/
90 PetscErrorCode  PetscFClose(MPI_Comm comm,FILE *fd)
91 {
92   PetscErrorCode ierr;
93   PetscMPIInt    rank;
94   int            err;
95 
96   PetscFunctionBegin;
97   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
98   if (!rank && fd != PETSC_STDOUT && fd != PETSC_STDERR) {
99     err = fclose(fd);
100     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
101   }
102   PetscFunctionReturn(0);
103 }
104 
105 #if defined(PETSC_HAVE_POPEN)
106 
107 #undef __FUNCT__
108 #define __FUNCT__ "PetscPClose"
109 /*@C
110       PetscPClose - Closes (ends) a program on processor zero run with PetscPOpen()
111 
112      Collective on MPI_Comm, but only process 0 runs the command
113 
114    Input Parameters:
115 +   comm - MPI communicator, only processor zero runs the program
116 -   fp - the file pointer where program input or output may be read or PETSC_NULL if don't care
117 
118    Output Parameters:
119 .   rval - return value from pclose() or PETSC_NULL to raise an error on failure
120 
121    Level: intermediate
122 
123    Notes:
124        Does not work under Windows
125 
126 .seealso: PetscFOpen(), PetscFClose(), PetscPOpen()
127 
128 @*/
129 PetscErrorCode PetscPClose(MPI_Comm comm,FILE *fd,PetscInt *rval)
130 {
131   PetscErrorCode ierr;
132   PetscMPIInt    rank;
133   int            err;
134 
135   PetscFunctionBegin;
136   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
137   if (!rank) {
138     char buf[1024];
139     while (fgets(buf,1024,fd)) ; /* wait till it prints everything */
140     err = pclose(fd);
141     if (rval) *rval = err;
142     else if (err) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"pclose() failed with error code %D",err);
143   }
144   PetscFunctionReturn(0);
145 }
146 
147 
148 #undef __FUNCT__
149 #define __FUNCT__ "PetscPOpen"
150 /*@C
151       PetscPOpen - Runs a program on processor zero and sends either its input or output to
152           a file.
153 
154      Logically Collective on MPI_Comm, but only process 0 runs the command
155 
156    Input Parameters:
157 +   comm - MPI communicator, only processor zero runs the program
158 .   machine - machine to run command on or PETSC_NULL, or string with 0 in first location
159 .   program - name of program to run
160 -   mode - either r or w
161 
162    Output Parameter:
163 .   fp - the file pointer where program input or output may be read or PETSC_NULL if don't care
164 
165    Level: intermediate
166 
167    Notes:
168        Use PetscPClose() to close the file pointer when you are finished with it
169        Does not work under Windows
170 
171        The program string may contain ${DISPLAY}, ${HOMEDIRECTORY} or ${WORKINGDIRECTORY}; these
172     will be replaced with relevent values.
173 
174 .seealso: PetscFOpen(), PetscFClose(), PetscPClose()
175 
176 @*/
177 PetscErrorCode  PetscPOpen(MPI_Comm comm,const char machine[],const char program[],const char mode[],FILE **fp)
178 {
179   PetscErrorCode ierr;
180   PetscMPIInt    rank;
181   size_t         i,len,cnt;
182   char           commandt[PETSC_MAX_PATH_LEN],command[PETSC_MAX_PATH_LEN];
183   FILE           *fd;
184 
185   PetscFunctionBegin;
186   /* all processors have to do the string manipulation because PetscStrreplace() is a collective operation */
187   if (machine && machine[0]) {
188     ierr = PetscStrcpy(command,"ssh ");CHKERRQ(ierr);
189     ierr = PetscStrcat(command,machine);CHKERRQ(ierr);
190     ierr = PetscStrcat(command," \" export DISPLAY=${DISPLAY}; ");CHKERRQ(ierr);
191     /*
192         Copy program into command but protect the " with a \ in front of it
193     */
194     ierr = PetscStrlen(command,&cnt);CHKERRQ(ierr);
195     ierr = PetscStrlen(program,&len);CHKERRQ(ierr);
196     for (i=0; i<len; i++) {
197       if (program[i] == '\"') command[cnt++] = '\\';
198       command[cnt++] = program[i];
199     }
200     command[cnt] = 0;
201 
202     ierr = PetscStrcat(command,"\"");CHKERRQ(ierr);
203   } else {
204     ierr = PetscStrcpy(command,program);CHKERRQ(ierr);
205   }
206 
207   ierr = PetscStrreplace(comm,command,commandt,1024);CHKERRQ(ierr);
208 
209   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
210   if (!rank) {
211     ierr = PetscInfo1(0,"Running command :%s\n",commandt);CHKERRQ(ierr);
212     if (!(fd = popen(commandt,mode))) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Cannot run command %s",commandt);
213     if (fp) *fp = fd;
214   }
215   PetscFunctionReturn(0);
216 }
217 
218 #endif
219