1e5c89e4eSSatish Balay /* 2e5c89e4eSSatish Balay Utilites routines to add simple ASCII IO capability. 3e5c89e4eSSatish Balay */ 4c6db04a5SJed Brown #include <../src/sys/fileio/mprint.h> 579c0e996SJed Brown #include <errno.h> 6e5c89e4eSSatish Balay /* 7e5c89e4eSSatish Balay If petsc_history is on, then all Petsc*Printf() results are saved 8e5c89e4eSSatish Balay if the appropriate (usually .petschistory) file. 9e5c89e4eSSatish Balay */ 10e5c89e4eSSatish Balay extern FILE *petsc_history; 11e5c89e4eSSatish Balay /* 12e5c89e4eSSatish Balay Allows one to overwrite where standard out is sent. For example 135106ddf5SBarry Smith PETSC_STDOUT = fopen("/dev/ttyXX","w") will cause all standard out 14e5c89e4eSSatish Balay writes to go to terminal XX; assuming you have write permission there 15e5c89e4eSSatish Balay */ 16e5c89e4eSSatish Balay FILE *PETSC_STDOUT = 0; 17ae9b4142SLisandro Dalcin /* 18ae9b4142SLisandro Dalcin Allows one to overwrite where standard error is sent. For example 19ae9b4142SLisandro Dalcin PETSC_STDERR = fopen("/dev/ttyXX","w") will cause all standard error 20ae9b4142SLisandro Dalcin writes to go to terminal XX; assuming you have write permission there 21ae9b4142SLisandro Dalcin */ 22ae9b4142SLisandro Dalcin FILE *PETSC_STDERR = 0; 23b13499bfSbcordonn 24c9a19010SBarry Smith /*@C 25*d781fa04SBarry Smith PetscFormatConvertGetSize - Gets the length of a string needed to hold format converted with PetscFormatConvert() 26c9a19010SBarry Smith 27*d781fa04SBarry Smith Input Parameter: 28*d781fa04SBarry Smith . format - the PETSc format string 29c9a19010SBarry Smith 30*d781fa04SBarry Smith Output Parameter: 31*d781fa04SBarry Smith . size - the needed length of the new format 32c9a19010SBarry Smith 33c9a19010SBarry Smith Level: developer 34c9a19010SBarry Smith 35*d781fa04SBarry Smith .seealso: PetscFormatConvert(), PetscVSNPrintf(), PetscVFPrintf() 36*d781fa04SBarry Smith 37c9a19010SBarry Smith @*/ 38*d781fa04SBarry Smith PetscErrorCode PetscFormatConvertGetSize(const char *format,size_t *size) 39*d781fa04SBarry Smith { 40*d781fa04SBarry Smith PetscInt i = 0; 41*d781fa04SBarry Smith 42*d781fa04SBarry Smith PetscFunctionBegin; 43*d781fa04SBarry Smith *size = 0; 44*d781fa04SBarry Smith while (format[i]) { 45*d781fa04SBarry Smith if (format[i] == '%' && format[i+1] == '%') { 46*d781fa04SBarry Smith i++; i++; *size += 2; 47*d781fa04SBarry Smith } else if (format[i] == '%') { 48*d781fa04SBarry Smith /* Find the letter */ 49*d781fa04SBarry Smith for (; format[i] && format[i] <= '9'; i++,(*size += 1)); 50*d781fa04SBarry Smith switch (format[i]) { 51*d781fa04SBarry Smith case 'D': 52*d781fa04SBarry Smith #if defined(PETSC_USE_64BIT_INDICES) 53*d781fa04SBarry Smith *size += 2; 54*d781fa04SBarry Smith #endif 55*d781fa04SBarry Smith break; 56*d781fa04SBarry Smith case 'g': 57*d781fa04SBarry Smith *size += 4; 58*d781fa04SBarry Smith break; 59*d781fa04SBarry Smith default: 60*d781fa04SBarry Smith break; 61*d781fa04SBarry Smith } 62*d781fa04SBarry Smith *size += 1; 63*d781fa04SBarry Smith i++; 64*d781fa04SBarry Smith } else { 65*d781fa04SBarry Smith i++; 66*d781fa04SBarry Smith *size += 1; 67*d781fa04SBarry Smith } 68*d781fa04SBarry Smith } 69*d781fa04SBarry Smith *size += 1; /* space for NULL character */ 70*d781fa04SBarry Smith PetscFunctionReturn(0); 71*d781fa04SBarry Smith } 72*d781fa04SBarry Smith 73*d781fa04SBarry Smith /*@C 74*d781fa04SBarry Smith PetscFormatConvert - Takes a PETSc format string and converts the %D to %d for 32 bit PETSc indices and %lld for 64 bit PETSc indices. Also 75*d781fa04SBarry Smith converts %g to [|%g|] so that PetscVSNPrintf() can easily insure all %g formatted numbers have a decimal point when printed. 76*d781fa04SBarry Smith 77*d781fa04SBarry Smith Input Parameters: 78*d781fa04SBarry Smith + format - the PETSc format string 79*d781fa04SBarry Smith . newformat - the location to put the new format 80*d781fa04SBarry Smith - size - the length of newformat, you can use PetscFormatConvertGetSize() to compute the needed size 81*d781fa04SBarry Smith 82*d781fa04SBarry Smith Note: this exists so we can have the same code when PetscInt is either int or long long int 83*d781fa04SBarry Smith 84*d781fa04SBarry Smith Level: developer 85*d781fa04SBarry Smith 86*d781fa04SBarry Smith .seealso: PetscFormatConvertGetSize(), PetscVSNPrintf(), PetscVFPrintf() 87*d781fa04SBarry Smith 88*d781fa04SBarry Smith @*/ 89*d781fa04SBarry Smith PetscErrorCode PetscFormatConvert(const char *format,char *newformat) 90e5c89e4eSSatish Balay { 91e5c89e4eSSatish Balay PetscInt i = 0, j = 0; 92e5c89e4eSSatish Balay 93eed5747fSBarry Smith PetscFunctionBegin; 94*d781fa04SBarry Smith while (format[i]) { 952a1ad9caSBarry Smith if (format[i] == '%' && format[i+1] == '%') { 962a1ad9caSBarry Smith newformat[j++] = format[i++]; 972a1ad9caSBarry Smith newformat[j++] = format[i++]; 982a1ad9caSBarry Smith } else if (format[i] == '%') { 998627564fSBarry Smith if (format[i+1] == 'g') { 1008627564fSBarry Smith newformat[j++] = '['; 1018627564fSBarry Smith newformat[j++] = '|'; 1028627564fSBarry Smith } 1037bc47156SJose Roman /* Find the letter */ 1047bc47156SJose Roman for (; format[i] && format[i] <= '9'; i++) newformat[j++] = format[i]; 1057bc47156SJose Roman switch (format[i]) { 1067bc47156SJose Roman case 'D': 1076de02169SBarry Smith #if !defined(PETSC_USE_64BIT_INDICES) 108e5c89e4eSSatish Balay newformat[j++] = 'd'; 109e5c89e4eSSatish Balay #else 110e5c89e4eSSatish Balay newformat[j++] = 'l'; 111e5c89e4eSSatish Balay newformat[j++] = 'l'; 112e5c89e4eSSatish Balay newformat[j++] = 'd'; 113e5c89e4eSSatish Balay #endif 1147bc47156SJose Roman break; 1158627564fSBarry Smith case 'g': 1168627564fSBarry Smith newformat[j++] = format[i]; 1178627564fSBarry Smith if (format[i-1] == '%') { 1188627564fSBarry Smith newformat[j++] = '|'; 1198627564fSBarry Smith newformat[j++] = ']'; 1208627564fSBarry Smith } 1218627564fSBarry Smith break; 1227bc47156SJose Roman case 'G': 123145505e7SDominic Meiser SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"%%G format is no longer supported, use %%g and cast the argument to double"); 1247bc47156SJose Roman break; 125121a09c4SJose Roman case 'F': 126145505e7SDominic Meiser SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"%%F format is no longer supported, use %%f and cast the argument to double"); 127121a09c4SJose Roman break; 1287bc47156SJose Roman default: 1297bc47156SJose Roman newformat[j++] = format[i]; 1307bc47156SJose Roman break; 1317bc47156SJose Roman } 1327bc47156SJose Roman i++; 133a297a907SKarl Rupp } else newformat[j++] = format[i++]; 134e5c89e4eSSatish Balay } 135e5c89e4eSSatish Balay newformat[j] = 0; 136eed5747fSBarry Smith PetscFunctionReturn(0); 137e5c89e4eSSatish Balay } 138e5c89e4eSSatish Balay 139*d781fa04SBarry Smith #define PETSCVSNPRINTFDEFAULTFORMATBUFFERSIZE 8*1024 140*d781fa04SBarry Smith 141c9a19010SBarry Smith /*@C 142c9a19010SBarry Smith PetscVSNPrintf - The PETSc version of vsnprintf(). Converts a PETSc format string into a standard C format string and then puts all the 143c9a19010SBarry Smith function arguments into a string using the format statement. 144c9a19010SBarry Smith 145c9a19010SBarry Smith Input Parameters: 146c9a19010SBarry Smith + str - location to put result 147c9a19010SBarry Smith . len - the amount of space in str 148c9a19010SBarry Smith + format - the PETSc format string 149c9a19010SBarry Smith - fullLength - the amount of space in str actually used. 150c9a19010SBarry Smith 151eed5747fSBarry Smith Developer Notes: this function may be called from an error handler, if an error occurs when it is called by the error handler than likely 152eed5747fSBarry Smith a recursion will occur and possible crash. 153c9a19010SBarry Smith 154c9a19010SBarry Smith Level: developer 155c9a19010SBarry Smith 156*d781fa04SBarry Smith .seealso: PetscVSNPrintf(), PetscErrorPrintf(), PetscVPrintf() 157*d781fa04SBarry Smith 158c9a19010SBarry Smith @*/ 1597087cfbeSBarry Smith PetscErrorCode PetscVSNPrintf(char *str,size_t len,const char *format,size_t *fullLength,va_list Argp) 160e5c89e4eSSatish Balay { 161*d781fa04SBarry Smith char *newformat = NULL; 162*d781fa04SBarry Smith char formatbuf[PETSCVSNPRINTFDEFAULTFORMATBUFFERSIZE]; 163*d781fa04SBarry Smith size_t newLength; 1641a15ef5dSMatthew Knepley PetscErrorCode ierr; 165e5c89e4eSSatish Balay 166eed5747fSBarry Smith PetscFunctionBegin; 167*d781fa04SBarry Smith ierr = PetscFormatConvertGetSize(format,&newLength);CHKERRQ(ierr); 168*d781fa04SBarry Smith if (newLength < PETSCVSNPRINTFDEFAULTFORMATBUFFERSIZE) { 169e2135aedSMatthew Knepley newformat = formatbuf; 170*d781fa04SBarry Smith newLength = PETSCVSNPRINTFDEFAULTFORMATBUFFERSIZE-1; 171e2135aedSMatthew Knepley } else { 172*d781fa04SBarry Smith ierr = PetscMalloc1(newLength, &newformat);CHKERRQ(ierr); 173e2135aedSMatthew Knepley } 174*d781fa04SBarry Smith ierr = PetscFormatConvert(format,newformat);CHKERRQ(ierr); 1755a058713SBarry Smith #if defined(PETSC_HAVE_VSNPRINTF_CHAR) 176748e1b9dSBarry Smith (void) vsnprintf(str,len,newformat,(char*)Argp); 17789b07760SSatish Balay #elif defined(PETSC_HAVE_VSNPRINTF) 178748e1b9dSBarry Smith (void) vsnprintf(str,len,newformat,Argp); 179141c0d65SSatish Balay #elif defined(PETSC_HAVE__VSNPRINTF) 180748e1b9dSBarry Smith (void) _vsnprintf(str,len,newformat,Argp); 181e5c89e4eSSatish Balay #else 18289b07760SSatish Balay #error "vsnprintf not found" 183e5c89e4eSSatish Balay #endif 184*d781fa04SBarry Smith if (newLength > PETSCVSNPRINTFDEFAULTFORMATBUFFERSIZE-1) { 185e2135aedSMatthew Knepley ierr = PetscFree(newformat);CHKERRQ(ierr); 186e2135aedSMatthew Knepley } 1878627564fSBarry Smith { 1888627564fSBarry Smith PetscBool foundedot; 1898627564fSBarry Smith size_t cnt = 0,ncnt = 0,leng; 1908627564fSBarry Smith ierr = PetscStrlen(str,&leng);CHKERRQ(ierr); 19117ca8410SBarry Smith if (leng > 4) { 1928627564fSBarry Smith for (cnt=0; cnt<leng-4; cnt++) { 1938627564fSBarry Smith if (str[cnt] == '[' && str[cnt+1] == '|'){ 1948627564fSBarry Smith cnt++; cnt++; 1958627564fSBarry Smith foundedot = PETSC_FALSE; 1968627564fSBarry Smith for (; cnt<leng-1; cnt++) { 1978627564fSBarry Smith if (str[cnt] == '|' && str[cnt+1] == ']'){ 1988627564fSBarry Smith cnt++; 1998627564fSBarry Smith if (!foundedot) str[ncnt++] = '.'; 2008627564fSBarry Smith ncnt--; 2018627564fSBarry Smith break; 2028627564fSBarry Smith } else { 2038627564fSBarry Smith if (str[cnt] == 'e' || str[cnt] == '.') foundedot = PETSC_TRUE; 2048627564fSBarry Smith str[ncnt++] = str[cnt]; 2058627564fSBarry Smith } 2068627564fSBarry Smith } 2078627564fSBarry Smith } else { 2088627564fSBarry Smith str[ncnt] = str[cnt]; 2098627564fSBarry Smith } 2108627564fSBarry Smith ncnt++; 2118627564fSBarry Smith } 2128627564fSBarry Smith while (cnt < leng) { 2138627564fSBarry Smith str[ncnt] = str[cnt]; ncnt++; cnt++; 2148627564fSBarry Smith } 2158627564fSBarry Smith str[ncnt] = 0; 2168627564fSBarry Smith } 2178627564fSBarry Smith } 218748e1b9dSBarry Smith #if defined(PETSC_HAVE_WINDOWS_H) && !defined(PETSC_HAVE__SET_OUTPUT_FORMAT) 219e51f71cfSBarry Smith /* older Windows OS always produces e-+0np for floating point output; remove the extra 0 */ 220748e1b9dSBarry Smith { 221748e1b9dSBarry Smith size_t cnt = 0,ncnt = 0,leng; 222748e1b9dSBarry Smith ierr = PetscStrlen(str,&leng);CHKERRQ(ierr); 223748e1b9dSBarry Smith if (leng > 5) { 224748e1b9dSBarry Smith for (cnt=0; cnt<leng-4; cnt++) { 225e51f71cfSBarry Smith if (str[cnt] == 'e' && (str[cnt+1] == '-' || str[cnt+1] == '+') && str[cnt+2] == '0' && str[cnt+3] >= '0' && str[cnt+3] <= '9' && str[cnt+4] >= '0' && str[cnt+4] <= '9') { 226748e1b9dSBarry Smith str[ncnt] = str[cnt]; ncnt++; cnt++; 227e51f71cfSBarry Smith str[ncnt] = str[cnt]; ncnt++; cnt++; cnt++; 228e51f71cfSBarry Smith str[ncnt] = str[cnt]; 229748e1b9dSBarry Smith } else { 230748e1b9dSBarry Smith str[ncnt] = str[cnt]; 231748e1b9dSBarry Smith } 232748e1b9dSBarry Smith ncnt++; 233748e1b9dSBarry Smith } 234748e1b9dSBarry Smith while (cnt < leng) { 235748e1b9dSBarry Smith str[ncnt] = str[cnt]; ncnt++; cnt++; 236748e1b9dSBarry Smith } 237748e1b9dSBarry Smith str[ncnt] = 0; 238748e1b9dSBarry Smith } 239748e1b9dSBarry Smith } 240748e1b9dSBarry Smith #endif 241748e1b9dSBarry Smith if (fullLength) { 242748e1b9dSBarry Smith ierr = PetscStrlen(str,fullLength);CHKERRQ(ierr); 243748e1b9dSBarry Smith } 244eed5747fSBarry Smith PetscFunctionReturn(0); 245e5c89e4eSSatish Balay } 246e5c89e4eSSatish Balay 247c9a19010SBarry Smith /*@C 248c9a19010SBarry Smith PetscVFPrintf - All PETSc standard out and error messages are sent through this function; so, in theory, this can 249e5c89e4eSSatish Balay can be replaced with something that does not simply write to a file. 250e5c89e4eSSatish Balay 251c9a19010SBarry Smith To use, write your own function for example, 252c9a19010SBarry Smith $PetscErrorCode mypetscvfprintf(FILE *fd,const char format[],va_list Argp) 253c9a19010SBarry Smith ${ 254c9a19010SBarry Smith $ PetscErrorCode ierr; 255c9a19010SBarry Smith $ 256c9a19010SBarry Smith $ PetscFunctionBegin; 257c9a19010SBarry Smith $ if (fd != stdout && fd != stderr) { handle regular files 258c9a19010SBarry Smith $ ierr = PetscVFPrintfDefault(fd,format,Argp);CHKERR(ierr); 259c9a19010SBarry Smith $ } else { 260c9a19010SBarry Smith $ char buff[BIG]; 261c9a19010SBarry Smith $ size_t length; 262c9a19010SBarry Smith $ ierr = PetscVSNPrintf(buff,BIG,format,&length,Argp);CHKERRQ(ierr); 263c9a19010SBarry Smith $ now send buff to whatever stream or whatever you want 264c9a19010SBarry Smith $ } 265c9a19010SBarry Smith $ PetscFunctionReturn(0); 266c9a19010SBarry Smith $} 267c9a19010SBarry Smith then before the call to PetscInitialize() do the assignment 268c9a19010SBarry Smith $ PetscVFPrintf = mypetscvfprintf; 269c9a19010SBarry Smith 270c9a19010SBarry Smith Notes: For error messages this may be called by any process, for regular standard out it is 271e5c89e4eSSatish Balay called only by process 0 of a given communicator 272e5c89e4eSSatish Balay 273eed5747fSBarry Smith Developer Notes: this could be called by an error handler, if that happens then a recursion of the error handler may occur 274eed5747fSBarry Smith and a crash 275c9a19010SBarry Smith 276c9a19010SBarry Smith Level: developer 277c9a19010SBarry Smith 278c9a19010SBarry Smith .seealso: PetscVSNPrintf(), PetscErrorPrintf() 279c9a19010SBarry Smith 280c9a19010SBarry Smith @*/ 2817087cfbeSBarry Smith PetscErrorCode PetscVFPrintfDefault(FILE *fd,const char *format,va_list Argp) 282e5c89e4eSSatish Balay { 283748e1b9dSBarry Smith char str[8*1024]; 284eed5747fSBarry Smith PetscErrorCode ierr; 2851179db26SBarry Smith 286eed5747fSBarry Smith PetscFunctionBegin; 287748e1b9dSBarry Smith ierr = PetscVSNPrintf(str,sizeof(str),format,NULL,Argp);CHKERRQ(ierr); 288748e1b9dSBarry Smith fprintf(fd,"%s",str);CHKERRQ(ierr); 2895a058713SBarry Smith fflush(fd); 290eed5747fSBarry Smith PetscFunctionReturn(0); 291e5c89e4eSSatish Balay } 292e5c89e4eSSatish Balay 2935b5bc046SBarry Smith /*@C 2945b5bc046SBarry Smith PetscSNPrintf - Prints to a string of given length 2955b5bc046SBarry Smith 2965b5bc046SBarry Smith Not Collective 2975b5bc046SBarry Smith 2985b5bc046SBarry Smith Input Parameters: 2995b5bc046SBarry Smith + str - the string to print to 3005b5bc046SBarry Smith . len - the length of str 3015b5bc046SBarry Smith . format - the usual printf() format string 3025b5bc046SBarry Smith - any arguments 3035b5bc046SBarry Smith 3045b5bc046SBarry Smith Level: intermediate 3055b5bc046SBarry Smith 3065b5bc046SBarry Smith .seealso: PetscSynchronizedFlush(), PetscSynchronizedFPrintf(), PetscFPrintf(), PetscVSNPrintf(), 307*d781fa04SBarry Smith PetscPrintf(), PetscViewerASCIIPrintf(), PetscViewerASCIISynchronizedPrintf(), PetscVFPrintf() 3085b5bc046SBarry Smith @*/ 3097087cfbeSBarry Smith PetscErrorCode PetscSNPrintf(char *str,size_t len,const char format[],...) 3105b5bc046SBarry Smith { 3115b5bc046SBarry Smith PetscErrorCode ierr; 312c9a19010SBarry Smith size_t fullLength; 3135b5bc046SBarry Smith va_list Argp; 3145b5bc046SBarry Smith 3155b5bc046SBarry Smith PetscFunctionBegin; 3165b5bc046SBarry Smith va_start(Argp,format); 3172d609e63SMatthew Knepley ierr = PetscVSNPrintf(str,len,format,&fullLength,Argp);CHKERRQ(ierr); 3185b5bc046SBarry Smith PetscFunctionReturn(0); 3195b5bc046SBarry Smith } 3205b5bc046SBarry Smith 321257d2499SJed Brown /*@C 322257d2499SJed Brown PetscSNPrintfCount - Prints to a string of given length, returns count 323257d2499SJed Brown 324257d2499SJed Brown Not Collective 325257d2499SJed Brown 326257d2499SJed Brown Input Parameters: 327257d2499SJed Brown + str - the string to print to 328257d2499SJed Brown . len - the length of str 329257d2499SJed Brown . format - the usual printf() format string 330257d2499SJed Brown . countused - number of characters used 331257d2499SJed Brown - any arguments 332257d2499SJed Brown 333257d2499SJed Brown Level: intermediate 334257d2499SJed Brown 335257d2499SJed Brown .seealso: PetscSynchronizedFlush(), PetscSynchronizedFPrintf(), PetscFPrintf(), PetscVSNPrintf(), 336*d781fa04SBarry Smith PetscPrintf(), PetscViewerASCIIPrintf(), PetscViewerASCIISynchronizedPrintf(), PetscSNPrintf(), PetscVFPrintf() 337257d2499SJed Brown @*/ 338257d2499SJed Brown PetscErrorCode PetscSNPrintfCount(char *str,size_t len,const char format[],size_t *countused,...) 339257d2499SJed Brown { 340257d2499SJed Brown PetscErrorCode ierr; 341257d2499SJed Brown va_list Argp; 342257d2499SJed Brown 343257d2499SJed Brown PetscFunctionBegin; 344257d2499SJed Brown va_start(Argp,countused); 345257d2499SJed Brown ierr = PetscVSNPrintf(str,len,format,countused,Argp);CHKERRQ(ierr); 346257d2499SJed Brown PetscFunctionReturn(0); 347257d2499SJed Brown } 348257d2499SJed Brown 349e5c89e4eSSatish Balay /* ----------------------------------------------------------------------- */ 350e5c89e4eSSatish Balay 351d30b0576SJed Brown PrintfQueue petsc_printfqueue = 0,petsc_printfqueuebase = 0; 352d30b0576SJed Brown int petsc_printfqueuelength = 0; 353e5c89e4eSSatish Balay 354e5c89e4eSSatish Balay /*@C 355e5c89e4eSSatish Balay PetscSynchronizedPrintf - Prints synchronized output from several processors. 356e5c89e4eSSatish Balay Output of the first processor is followed by that of the second, etc. 357e5c89e4eSSatish Balay 358e5c89e4eSSatish Balay Not Collective 359e5c89e4eSSatish Balay 360e5c89e4eSSatish Balay Input Parameters: 361e5c89e4eSSatish Balay + comm - the communicator 362e5c89e4eSSatish Balay - format - the usual printf() format string 363e5c89e4eSSatish Balay 364e5c89e4eSSatish Balay Level: intermediate 365e5c89e4eSSatish Balay 366e5c89e4eSSatish Balay Notes: 3677889ec69SBarry Smith REQUIRES a call to PetscSynchronizedFlush() by all the processes after the completion of the calls to PetscSynchronizedPrintf() for the information 368e5c89e4eSSatish Balay from all the processors to be printed. 369e5c89e4eSSatish Balay 370e5c89e4eSSatish Balay Fortran Note: 371d60d70ccSBarry Smith The call sequence is PetscSynchronizedPrintf(MPI_Comm, character(*), PetscErrorCode ierr) from Fortran. 372e5c89e4eSSatish Balay That is, you can only pass a single character string from Fortran. 373e5c89e4eSSatish Balay 374e5c89e4eSSatish Balay .seealso: PetscSynchronizedFlush(), PetscSynchronizedFPrintf(), PetscFPrintf(), 375e5c89e4eSSatish Balay PetscPrintf(), PetscViewerASCIIPrintf(), PetscViewerASCIISynchronizedPrintf() 376e5c89e4eSSatish Balay @*/ 3777087cfbeSBarry Smith PetscErrorCode PetscSynchronizedPrintf(MPI_Comm comm,const char format[],...) 378e5c89e4eSSatish Balay { 379e5c89e4eSSatish Balay PetscErrorCode ierr; 380e5c89e4eSSatish Balay PetscMPIInt rank; 381e5c89e4eSSatish Balay 382e5c89e4eSSatish Balay PetscFunctionBegin; 3836180deefSBarry Smith if (comm == MPI_COMM_NULL) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Called with MPI_COMM_NULL, likely PetscObjectComm() failed"); 384e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 385e5c89e4eSSatish Balay 386e5c89e4eSSatish Balay /* First processor prints immediately to stdout */ 387e5c89e4eSSatish Balay if (!rank) { 388e5c89e4eSSatish Balay va_list Argp; 389e5c89e4eSSatish Balay va_start(Argp,format); 3901179db26SBarry Smith ierr = (*PetscVFPrintf)(PETSC_STDOUT,format,Argp);CHKERRQ(ierr); 391e5c89e4eSSatish Balay if (petsc_history) { 392cdc7d174SSatish Balay va_start(Argp,format); 3931179db26SBarry Smith ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr); 394e5c89e4eSSatish Balay } 395e5c89e4eSSatish Balay va_end(Argp); 396e5c89e4eSSatish Balay } else { /* other processors add to local queue */ 397e5c89e4eSSatish Balay va_list Argp; 398e5c89e4eSSatish Balay PrintfQueue next; 399c9a19010SBarry Smith size_t fullLength = 8191; 400e5c89e4eSSatish Balay 401b00a9115SJed Brown ierr = PetscNew(&next);CHKERRQ(ierr); 402a297a907SKarl Rupp if (petsc_printfqueue) { 403a297a907SKarl Rupp petsc_printfqueue->next = next; 404a297a907SKarl Rupp petsc_printfqueue = next; 405a297a907SKarl Rupp petsc_printfqueue->next = 0; 406a297a907SKarl Rupp } else petsc_printfqueuebase = petsc_printfqueue = next; 407d30b0576SJed Brown petsc_printfqueuelength++; 4082d609e63SMatthew Knepley next->size = -1; 4099ad23270SJed Brown while ((PetscInt)fullLength >= next->size) { 4102d609e63SMatthew Knepley next->size = fullLength+1; 411a297a907SKarl Rupp 412785e854fSJed Brown ierr = PetscMalloc1(next->size, &next->string);CHKERRQ(ierr); 413e5c89e4eSSatish Balay va_start(Argp,format); 4142d609e63SMatthew Knepley ierr = PetscMemzero(next->string,next->size);CHKERRQ(ierr); 4152d609e63SMatthew Knepley ierr = PetscVSNPrintf(next->string,next->size,format, &fullLength,Argp);CHKERRQ(ierr); 416e5c89e4eSSatish Balay va_end(Argp); 417e5c89e4eSSatish Balay } 4182d609e63SMatthew Knepley } 419e5c89e4eSSatish Balay PetscFunctionReturn(0); 420e5c89e4eSSatish Balay } 421e5c89e4eSSatish Balay 422e5c89e4eSSatish Balay /*@C 423e5c89e4eSSatish Balay PetscSynchronizedFPrintf - Prints synchronized output to the specified file from 424e5c89e4eSSatish Balay several processors. Output of the first processor is followed by that of the 425e5c89e4eSSatish Balay second, etc. 426e5c89e4eSSatish Balay 427e5c89e4eSSatish Balay Not Collective 428e5c89e4eSSatish Balay 429e5c89e4eSSatish Balay Input Parameters: 430e5c89e4eSSatish Balay + comm - the communicator 431e5c89e4eSSatish Balay . fd - the file pointer 432e5c89e4eSSatish Balay - format - the usual printf() format string 433e5c89e4eSSatish Balay 434e5c89e4eSSatish Balay Level: intermediate 435e5c89e4eSSatish Balay 436e5c89e4eSSatish Balay Notes: 437e5c89e4eSSatish Balay REQUIRES a intervening call to PetscSynchronizedFlush() for the information 438e5c89e4eSSatish Balay from all the processors to be printed. 439e5c89e4eSSatish Balay 440e5c89e4eSSatish Balay .seealso: PetscSynchronizedPrintf(), PetscSynchronizedFlush(), PetscFPrintf(), 441e5c89e4eSSatish Balay PetscFOpen(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIPrintf() 442e5c89e4eSSatish Balay 443e5c89e4eSSatish Balay @*/ 4447087cfbeSBarry Smith PetscErrorCode PetscSynchronizedFPrintf(MPI_Comm comm,FILE *fp,const char format[],...) 445e5c89e4eSSatish Balay { 446e5c89e4eSSatish Balay PetscErrorCode ierr; 447e5c89e4eSSatish Balay PetscMPIInt rank; 448e5c89e4eSSatish Balay 449e5c89e4eSSatish Balay PetscFunctionBegin; 4506180deefSBarry Smith if (comm == MPI_COMM_NULL) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Called with MPI_COMM_NULL, likely PetscObjectComm() failed"); 451e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 452e5c89e4eSSatish Balay 453e5c89e4eSSatish Balay /* First processor prints immediately to fp */ 454e5c89e4eSSatish Balay if (!rank) { 455e5c89e4eSSatish Balay va_list Argp; 456e5c89e4eSSatish Balay va_start(Argp,format); 4571179db26SBarry Smith ierr = (*PetscVFPrintf)(fp,format,Argp);CHKERRQ(ierr); 458cdc7d174SSatish Balay if (petsc_history && (fp !=petsc_history)) { 459cdc7d174SSatish Balay va_start(Argp,format); 4601179db26SBarry Smith ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr); 461e5c89e4eSSatish Balay } 462e5c89e4eSSatish Balay va_end(Argp); 463e5c89e4eSSatish Balay } else { /* other processors add to local queue */ 464e5c89e4eSSatish Balay va_list Argp; 465e5c89e4eSSatish Balay PrintfQueue next; 466c9a19010SBarry Smith size_t fullLength = 8191; 467b00a9115SJed Brown ierr = PetscNew(&next);CHKERRQ(ierr); 468a297a907SKarl Rupp if (petsc_printfqueue) { 469a297a907SKarl Rupp petsc_printfqueue->next = next; 470a297a907SKarl Rupp petsc_printfqueue = next; 471a297a907SKarl Rupp petsc_printfqueue->next = 0; 472a297a907SKarl Rupp } else petsc_printfqueuebase = petsc_printfqueue = next; 473d30b0576SJed Brown petsc_printfqueuelength++; 4742d609e63SMatthew Knepley next->size = -1; 4759ad23270SJed Brown while ((PetscInt)fullLength >= next->size) { 4762d609e63SMatthew Knepley next->size = fullLength+1; 477785e854fSJed Brown ierr = PetscMalloc1(next->size, &next->string);CHKERRQ(ierr); 478e5c89e4eSSatish Balay va_start(Argp,format); 4792d609e63SMatthew Knepley ierr = PetscMemzero(next->string,next->size);CHKERRQ(ierr); 4802d609e63SMatthew Knepley ierr = PetscVSNPrintf(next->string,next->size,format,&fullLength,Argp);CHKERRQ(ierr); 481e5c89e4eSSatish Balay va_end(Argp); 482e5c89e4eSSatish Balay } 4832d609e63SMatthew Knepley } 484e5c89e4eSSatish Balay PetscFunctionReturn(0); 485e5c89e4eSSatish Balay } 486e5c89e4eSSatish Balay 4870ec8b6e3SBarry Smith /*@C 488e5c89e4eSSatish Balay PetscSynchronizedFlush - Flushes to the screen output from all processors 4897889ec69SBarry Smith involved in previous PetscSynchronizedPrintf()/PetscSynchronizedFPrintf() calls. 490e5c89e4eSSatish Balay 491e5c89e4eSSatish Balay Collective on MPI_Comm 492e5c89e4eSSatish Balay 493e5c89e4eSSatish Balay Input Parameters: 4940ec8b6e3SBarry Smith + comm - the communicator 4950ec8b6e3SBarry Smith - fd - the file pointer (valid on process 0 of the communicator) 496e5c89e4eSSatish Balay 497e5c89e4eSSatish Balay Level: intermediate 498e5c89e4eSSatish Balay 499e5c89e4eSSatish Balay Notes: 5007889ec69SBarry Smith If PetscSynchronizedPrintf() and/or PetscSynchronizedFPrintf() are called with 5017889ec69SBarry Smith different MPI communicators there must be an intervening call to PetscSynchronizedFlush() between the calls with different MPI communicators. 502e5c89e4eSSatish Balay 503e50bf69fSBarry Smith From Fortran pass PETSC_STDOUT if the flush is for standard out; otherwise pass a value obtained from PetscFOpen() 504e50bf69fSBarry Smith 505e5c89e4eSSatish Balay .seealso: PetscSynchronizedPrintf(), PetscFPrintf(), PetscPrintf(), PetscViewerASCIIPrintf(), 506e5c89e4eSSatish Balay PetscViewerASCIISynchronizedPrintf() 5070087d953SMatthew G. Knepley @*/ 5080ec8b6e3SBarry Smith PetscErrorCode PetscSynchronizedFlush(MPI_Comm comm,FILE *fd) 509e5c89e4eSSatish Balay { 510e5c89e4eSSatish Balay PetscErrorCode ierr; 51129a5cbdcSMatthew G. Knepley PetscMPIInt rank,size,tag,i,j,n = 0,dummy = 0; 5122d609e63SMatthew Knepley char *message; 513e5c89e4eSSatish Balay MPI_Status status; 514e5c89e4eSSatish Balay 515e5c89e4eSSatish Balay PetscFunctionBegin; 516e5c89e4eSSatish Balay ierr = PetscCommDuplicate(comm,&comm,&tag);CHKERRQ(ierr); 517e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 518e5c89e4eSSatish Balay ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 519e5c89e4eSSatish Balay 520e5c89e4eSSatish Balay /* First processor waits for messages from all other processors */ 521e5c89e4eSSatish Balay if (!rank) { 5220ec8b6e3SBarry Smith if (!fd) fd = PETSC_STDOUT; 523e5c89e4eSSatish Balay for (i=1; i<size; i++) { 5249f73f8ecSBarry Smith /* to prevent a flood of messages to process zero, request each message separately */ 5259f73f8ecSBarry Smith ierr = MPI_Send(&dummy,1,MPI_INT,i,tag,comm);CHKERRQ(ierr); 526e5c89e4eSSatish Balay ierr = MPI_Recv(&n,1,MPI_INT,i,tag,comm,&status);CHKERRQ(ierr); 527e5c89e4eSSatish Balay for (j=0; j<n; j++) { 52829a5cbdcSMatthew G. Knepley PetscMPIInt size = 0; 5292d609e63SMatthew Knepley 5302d609e63SMatthew Knepley ierr = MPI_Recv(&size,1,MPI_INT,i,tag,comm,&status);CHKERRQ(ierr); 531785e854fSJed Brown ierr = PetscMalloc1(size, &message);CHKERRQ(ierr); 5322d609e63SMatthew Knepley ierr = MPI_Recv(message,size,MPI_CHAR,i,tag,comm,&status);CHKERRQ(ierr); 5333bf036e2SBarry Smith ierr = PetscFPrintf(comm,fd,"%s",message);CHKERRQ(ierr); 5342d609e63SMatthew Knepley ierr = PetscFree(message);CHKERRQ(ierr); 535e5c89e4eSSatish Balay } 536e5c89e4eSSatish Balay } 537e5c89e4eSSatish Balay } else { /* other processors send queue to processor 0 */ 538d30b0576SJed Brown PrintfQueue next = petsc_printfqueuebase,previous; 539e5c89e4eSSatish Balay 540b3ef9d35SBarry Smith ierr = MPI_Recv(&dummy,1,MPI_INT,0,tag,comm,&status);CHKERRQ(ierr); 541d30b0576SJed Brown ierr = MPI_Send(&petsc_printfqueuelength,1,MPI_INT,0,tag,comm);CHKERRQ(ierr); 542d30b0576SJed Brown for (i=0; i<petsc_printfqueuelength; i++) { 5432d609e63SMatthew Knepley ierr = MPI_Send(&next->size,1,MPI_INT,0,tag,comm);CHKERRQ(ierr); 5442d609e63SMatthew Knepley ierr = MPI_Send(next->string,next->size,MPI_CHAR,0,tag,comm);CHKERRQ(ierr); 545e5c89e4eSSatish Balay previous = next; 546e5c89e4eSSatish Balay next = next->next; 5472d609e63SMatthew Knepley ierr = PetscFree(previous->string);CHKERRQ(ierr); 548e5c89e4eSSatish Balay ierr = PetscFree(previous);CHKERRQ(ierr); 549e5c89e4eSSatish Balay } 550d30b0576SJed Brown petsc_printfqueue = 0; 551d30b0576SJed Brown petsc_printfqueuelength = 0; 552e5c89e4eSSatish Balay } 553e5c89e4eSSatish Balay ierr = PetscCommDestroy(&comm);CHKERRQ(ierr); 554e5c89e4eSSatish Balay PetscFunctionReturn(0); 555e5c89e4eSSatish Balay } 556e5c89e4eSSatish Balay 557e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------------------*/ 558e5c89e4eSSatish Balay 559e5c89e4eSSatish Balay /*@C 560e5c89e4eSSatish Balay PetscFPrintf - Prints to a file, only from the first 561e5c89e4eSSatish Balay processor in the communicator. 562e5c89e4eSSatish Balay 563e5c89e4eSSatish Balay Not Collective 564e5c89e4eSSatish Balay 565e5c89e4eSSatish Balay Input Parameters: 566e5c89e4eSSatish Balay + comm - the communicator 567e5c89e4eSSatish Balay . fd - the file pointer 568e5c89e4eSSatish Balay - format - the usual printf() format string 569e5c89e4eSSatish Balay 570e5c89e4eSSatish Balay Level: intermediate 571e5c89e4eSSatish Balay 572e5c89e4eSSatish Balay Fortran Note: 573e5c89e4eSSatish Balay This routine is not supported in Fortran. 574e5c89e4eSSatish Balay 575e5c89e4eSSatish Balay Concepts: printing^in parallel 576e5c89e4eSSatish Balay Concepts: printf^in parallel 577e5c89e4eSSatish Balay 578e5c89e4eSSatish Balay .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(), 579e5c89e4eSSatish Balay PetscViewerASCIISynchronizedPrintf(), PetscSynchronizedFlush() 580e5c89e4eSSatish Balay @*/ 5817087cfbeSBarry Smith PetscErrorCode PetscFPrintf(MPI_Comm comm,FILE* fd,const char format[],...) 582e5c89e4eSSatish Balay { 583e5c89e4eSSatish Balay PetscErrorCode ierr; 584e5c89e4eSSatish Balay PetscMPIInt rank; 585e5c89e4eSSatish Balay 586e5c89e4eSSatish Balay PetscFunctionBegin; 5876180deefSBarry Smith if (comm == MPI_COMM_NULL) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Called with MPI_COMM_NULL, likely PetscObjectComm() failed"); 588e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 589e5c89e4eSSatish Balay if (!rank) { 590e5c89e4eSSatish Balay va_list Argp; 591e5c89e4eSSatish Balay va_start(Argp,format); 5921179db26SBarry Smith ierr = (*PetscVFPrintf)(fd,format,Argp);CHKERRQ(ierr); 593cdc7d174SSatish Balay if (petsc_history && (fd !=petsc_history)) { 594cdc7d174SSatish Balay va_start(Argp,format); 5951179db26SBarry Smith ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr); 596e5c89e4eSSatish Balay } 597e5c89e4eSSatish Balay va_end(Argp); 598e5c89e4eSSatish Balay } 599e5c89e4eSSatish Balay PetscFunctionReturn(0); 600e5c89e4eSSatish Balay } 601e5c89e4eSSatish Balay 602e5c89e4eSSatish Balay /*@C 603e5c89e4eSSatish Balay PetscPrintf - Prints to standard out, only from the first 604eed5747fSBarry Smith processor in the communicator. Calls from other processes are ignored. 605e5c89e4eSSatish Balay 606e5c89e4eSSatish Balay Not Collective 607e5c89e4eSSatish Balay 608e5c89e4eSSatish Balay Input Parameters: 609e5c89e4eSSatish Balay + comm - the communicator 610e5c89e4eSSatish Balay - format - the usual printf() format string 611e5c89e4eSSatish Balay 612e5c89e4eSSatish Balay Level: intermediate 613e5c89e4eSSatish Balay 614e5c89e4eSSatish Balay Fortran Note: 615d60d70ccSBarry Smith The call sequence is PetscPrintf(MPI_Comm, character(*), PetscErrorCode ierr) from Fortran. 616e5c89e4eSSatish Balay That is, you can only pass a single character string from Fortran. 617e5c89e4eSSatish Balay 618e5c89e4eSSatish Balay Concepts: printing^in parallel 619e5c89e4eSSatish Balay Concepts: printf^in parallel 620e5c89e4eSSatish Balay 621e5c89e4eSSatish Balay .seealso: PetscFPrintf(), PetscSynchronizedPrintf() 622e5c89e4eSSatish Balay @*/ 6237087cfbeSBarry Smith PetscErrorCode PetscPrintf(MPI_Comm comm,const char format[],...) 624e5c89e4eSSatish Balay { 625e5c89e4eSSatish Balay PetscErrorCode ierr; 626e5c89e4eSSatish Balay PetscMPIInt rank; 627e5c89e4eSSatish Balay 628e5c89e4eSSatish Balay PetscFunctionBegin; 6296180deefSBarry Smith if (comm == MPI_COMM_NULL) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Called with MPI_COMM_NULL, likely PetscObjectComm() failed"); 630e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 631e5c89e4eSSatish Balay if (!rank) { 632e5c89e4eSSatish Balay va_list Argp; 633e5c89e4eSSatish Balay va_start(Argp,format); 634eed5747fSBarry Smith ierr = (*PetscVFPrintf)(PETSC_STDOUT,format,Argp);CHKERRQ(ierr); 635e5c89e4eSSatish Balay if (petsc_history) { 636cdc7d174SSatish Balay va_start(Argp,format); 637eed5747fSBarry Smith ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr); 638e5c89e4eSSatish Balay } 639e5c89e4eSSatish Balay va_end(Argp); 640e5c89e4eSSatish Balay } 641e5c89e4eSSatish Balay PetscFunctionReturn(0); 642e5c89e4eSSatish Balay } 643e5c89e4eSSatish Balay 644e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------------------*/ 645c9a19010SBarry Smith /*@C 646c9a19010SBarry Smith PetscHelpPrintf - All PETSc help messages are passing through this function. You can change how help messages are printed by 647c9a19010SBarry Smith replacinng it with something that does not simply write to a stdout. 648c9a19010SBarry Smith 649c9a19010SBarry Smith To use, write your own function for example, 650c9a19010SBarry Smith $PetscErrorCode mypetschelpprintf(MPI_Comm comm,const char format[],....) 651c9a19010SBarry Smith ${ 652c9a19010SBarry Smith $ PetscFunctionReturn(0); 653c9a19010SBarry Smith $} 654c9a19010SBarry Smith then before the call to PetscInitialize() do the assignment 655c9a19010SBarry Smith $ PetscHelpPrintf = mypetschelpprintf; 656c9a19010SBarry Smith 657c9a19010SBarry Smith Note: the default routine used is called PetscHelpPrintfDefault(). 658c9a19010SBarry Smith 659c9a19010SBarry Smith Level: developer 660c9a19010SBarry Smith 661c9a19010SBarry Smith .seealso: PetscVSNPrintf(), PetscVFPrintf(), PetscErrorPrintf() 662c9a19010SBarry Smith @*/ 6637087cfbeSBarry Smith PetscErrorCode PetscHelpPrintfDefault(MPI_Comm comm,const char format[],...) 664e5c89e4eSSatish Balay { 665e5c89e4eSSatish Balay PetscErrorCode ierr; 666e5c89e4eSSatish Balay PetscMPIInt rank; 667e5c89e4eSSatish Balay 668e5c89e4eSSatish Balay PetscFunctionBegin; 6696180deefSBarry Smith if (comm == MPI_COMM_NULL) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Called with MPI_COMM_NULL, likely PetscObjectComm() failed"); 670e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 671e5c89e4eSSatish Balay if (!rank) { 672e5c89e4eSSatish Balay va_list Argp; 673e5c89e4eSSatish Balay va_start(Argp,format); 6741179db26SBarry Smith ierr = (*PetscVFPrintf)(PETSC_STDOUT,format,Argp);CHKERRQ(ierr); 675e5c89e4eSSatish Balay if (petsc_history) { 676cdc7d174SSatish Balay va_start(Argp,format); 6771179db26SBarry Smith ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr); 678e5c89e4eSSatish Balay } 679e5c89e4eSSatish Balay va_end(Argp); 680e5c89e4eSSatish Balay } 681e5c89e4eSSatish Balay PetscFunctionReturn(0); 682e5c89e4eSSatish Balay } 683e5c89e4eSSatish Balay 684e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------------------*/ 685e5c89e4eSSatish Balay 686e5c89e4eSSatish Balay 687e5c89e4eSSatish Balay /*@C 688e5c89e4eSSatish Balay PetscSynchronizedFGets - Several processors all get the same line from a file. 689e5c89e4eSSatish Balay 690e5c89e4eSSatish Balay Collective on MPI_Comm 691e5c89e4eSSatish Balay 692e5c89e4eSSatish Balay Input Parameters: 693e5c89e4eSSatish Balay + comm - the communicator 694e5c89e4eSSatish Balay . fd - the file pointer 695e5c89e4eSSatish Balay - len - the length of the output buffer 696e5c89e4eSSatish Balay 697e5c89e4eSSatish Balay Output Parameter: 698e31d4fa4SJed Brown . string - the line read from the file, at end of file string[0] == 0 699e5c89e4eSSatish Balay 700e5c89e4eSSatish Balay Level: intermediate 701e5c89e4eSSatish Balay 702e5c89e4eSSatish Balay .seealso: PetscSynchronizedPrintf(), PetscSynchronizedFlush(), 703e5c89e4eSSatish Balay PetscFOpen(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIPrintf() 704e5c89e4eSSatish Balay 705e5c89e4eSSatish Balay @*/ 7067087cfbeSBarry Smith PetscErrorCode PetscSynchronizedFGets(MPI_Comm comm,FILE *fp,size_t len,char string[]) 707e5c89e4eSSatish Balay { 708e5c89e4eSSatish Balay PetscErrorCode ierr; 709e5c89e4eSSatish Balay PetscMPIInt rank; 710e5c89e4eSSatish Balay 711e5c89e4eSSatish Balay PetscFunctionBegin; 712e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 713e5c89e4eSSatish Balay 714e5c89e4eSSatish Balay if (!rank) { 715047b9c12SMatthew G Knepley char *ptr = fgets(string, len, fp); 716047b9c12SMatthew G Knepley 717047b9c12SMatthew G Knepley if (!ptr) { 718e31d4fa4SJed Brown string[0] = 0; 719e31d4fa4SJed Brown if (!feof(fp)) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_FILE_READ, "Error reading from file: %d", errno); 720047b9c12SMatthew G Knepley } 721e5c89e4eSSatish Balay } 722e5c89e4eSSatish Balay ierr = MPI_Bcast(string,len,MPI_BYTE,0,comm);CHKERRQ(ierr); 723e5c89e4eSSatish Balay PetscFunctionReturn(0); 724e5c89e4eSSatish Balay } 725238ccf28SShri Abhyankar 726f1d7fe2eSBarry Smith #if defined(PETSC_HAVE_CLOSURES) 727f1d7fe2eSBarry Smith int (^SwiftClosure)(const char*) = 0; 728f1d7fe2eSBarry Smith 729f1d7fe2eSBarry Smith PetscErrorCode PetscVFPrintfToString(FILE *fd,const char format[],va_list Argp) 730f1d7fe2eSBarry Smith { 731f1d7fe2eSBarry Smith PetscErrorCode ierr; 732f1d7fe2eSBarry Smith 733f1d7fe2eSBarry Smith PetscFunctionBegin; 734f1d7fe2eSBarry Smith if (fd != stdout && fd != stderr) { /* handle regular files */ 735f1d7fe2eSBarry Smith ierr = PetscVFPrintfDefault(fd,format,Argp);CHKERRQ(ierr); 736f1d7fe2eSBarry Smith } else { 737f1d7fe2eSBarry Smith size_t len=8*1024,length; 738f1d7fe2eSBarry Smith char buf[len]; 739f1d7fe2eSBarry Smith 740f1d7fe2eSBarry Smith ierr = PetscVSNPrintf(buf,len,format,&length,Argp);CHKERRQ(ierr); 741f1d7fe2eSBarry Smith ierr = SwiftClosure(buf);CHKERRQ(ierr); 742f1d7fe2eSBarry Smith } 743f1d7fe2eSBarry Smith PetscFunctionReturn(0); 744f1d7fe2eSBarry Smith } 745f1d7fe2eSBarry Smith 746f1d7fe2eSBarry Smith /* 747f1d7fe2eSBarry Smith Provide a Swift function that processes all the PETSc calls to PetscVFPrintf() 748f1d7fe2eSBarry Smith */ 749f1d7fe2eSBarry Smith PetscErrorCode PetscVFPrintfSetClosure(int (^closure)(const char*)) 750f1d7fe2eSBarry Smith { 751f1d7fe2eSBarry Smith PetscVFPrintf = PetscVFPrintfToString; 752f1d7fe2eSBarry Smith SwiftClosure = closure; 753f1d7fe2eSBarry Smith return 0; 754f1d7fe2eSBarry Smith } 755f1d7fe2eSBarry Smith #endif 756f1d7fe2eSBarry Smith 757238ccf28SShri Abhyankar #if defined(PETSC_HAVE_MATLAB_ENGINE) 758c6db04a5SJed Brown #include <mex.h> 7597087cfbeSBarry Smith PetscErrorCode PetscVFPrintf_Matlab(FILE *fd,const char format[],va_list Argp) 760238ccf28SShri Abhyankar { 761238ccf28SShri Abhyankar PetscErrorCode ierr; 762238ccf28SShri Abhyankar 763238ccf28SShri Abhyankar PetscFunctionBegin; 764238ccf28SShri Abhyankar if (fd != stdout && fd != stderr) { /* handle regular files */ 765238ccf28SShri Abhyankar ierr = PetscVFPrintfDefault(fd,format,Argp);CHKERRQ(ierr); 766238ccf28SShri Abhyankar } else { 767238ccf28SShri Abhyankar size_t len=8*1024,length; 768238ccf28SShri Abhyankar char buf[len]; 769238ccf28SShri Abhyankar 770238ccf28SShri Abhyankar ierr = PetscVSNPrintf(buf,len,format,&length,Argp);CHKERRQ(ierr); 771df413903SBarry Smith mexPrintf("%s",buf); 772238ccf28SShri Abhyankar } 773238ccf28SShri Abhyankar PetscFunctionReturn(0); 774238ccf28SShri Abhyankar } 775238ccf28SShri Abhyankar #endif 7766fc7ef2bSBarry Smith 7778c74ee41SBarry Smith /*@C 7788c74ee41SBarry Smith PetscFormatStrip - Takes a PETSc format string and removes all numerical modifiers to % operations 7798c74ee41SBarry Smith 7808c74ee41SBarry Smith Input Parameters: 7818c74ee41SBarry Smith . format - the PETSc format string 7828c74ee41SBarry Smith 7838c74ee41SBarry Smith Level: developer 7848c74ee41SBarry Smith 7858c74ee41SBarry Smith @*/ 7868c74ee41SBarry Smith PetscErrorCode PetscFormatStrip(char *format) 7878c74ee41SBarry Smith { 7888c74ee41SBarry Smith size_t loc1 = 0, loc2 = 0; 7898c74ee41SBarry Smith 7908c74ee41SBarry Smith PetscFunctionBegin; 7918c74ee41SBarry Smith while (format[loc2]) { 7928c74ee41SBarry Smith if (format[loc2] == '%') { 7938c74ee41SBarry Smith format[loc1++] = format[loc2++]; 7948c74ee41SBarry Smith while (format[loc2] && ((format[loc2] >= '0' && format[loc2] <= '9') || format[loc2] == '.')) loc2++; 7958c74ee41SBarry Smith } 7968c74ee41SBarry Smith format[loc1++] = format[loc2++]; 7978c74ee41SBarry Smith } 7988c74ee41SBarry Smith PetscFunctionReturn(0); 7998c74ee41SBarry Smith } 8008c74ee41SBarry Smith 801