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