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 */ 1095c0884eSLisandro Dalcin PETSC_INTERN 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 25d781fa04SBarry Smith PetscFormatConvertGetSize - Gets the length of a string needed to hold format converted with PetscFormatConvert() 26c9a19010SBarry Smith 27d781fa04SBarry Smith Input Parameter: 28d781fa04SBarry Smith . format - the PETSc format string 29c9a19010SBarry Smith 30d781fa04SBarry Smith Output Parameter: 31d781fa04SBarry Smith . size - the needed length of the new format 32c9a19010SBarry Smith 33c9a19010SBarry Smith Level: developer 34c9a19010SBarry Smith 35d781fa04SBarry Smith .seealso: PetscFormatConvert(), PetscVSNPrintf(), PetscVFPrintf() 36d781fa04SBarry Smith 37c9a19010SBarry Smith @*/ 38d781fa04SBarry Smith PetscErrorCode PetscFormatConvertGetSize(const char *format,size_t *size) 39d781fa04SBarry Smith { 40d781fa04SBarry Smith PetscInt i = 0; 41d781fa04SBarry Smith 42d781fa04SBarry Smith PetscFunctionBegin; 43d781fa04SBarry Smith *size = 0; 44d781fa04SBarry Smith while (format[i]) { 45d781fa04SBarry Smith if (format[i] == '%' && format[i+1] == '%') { 46d781fa04SBarry Smith i++; i++; *size += 2; 47d781fa04SBarry Smith } else if (format[i] == '%') { 48d781fa04SBarry Smith /* Find the letter */ 49d781fa04SBarry Smith for (; format[i] && format[i] <= '9'; i++,(*size += 1)); 50d781fa04SBarry Smith switch (format[i]) { 51d781fa04SBarry Smith case 'D': 52d781fa04SBarry Smith #if defined(PETSC_USE_64BIT_INDICES) 53d781fa04SBarry Smith *size += 2; 54d781fa04SBarry Smith #endif 55d781fa04SBarry Smith break; 56d781fa04SBarry Smith case 'g': 57d781fa04SBarry Smith *size += 4; 58d781fa04SBarry Smith break; 59d781fa04SBarry Smith default: 60d781fa04SBarry Smith break; 61d781fa04SBarry Smith } 62d781fa04SBarry Smith *size += 1; 63d781fa04SBarry Smith i++; 64d781fa04SBarry Smith } else { 65d781fa04SBarry Smith i++; 66d781fa04SBarry Smith *size += 1; 67d781fa04SBarry Smith } 68d781fa04SBarry Smith } 69d781fa04SBarry Smith *size += 1; /* space for NULL character */ 70d781fa04SBarry Smith PetscFunctionReturn(0); 71d781fa04SBarry Smith } 72d781fa04SBarry Smith 73d781fa04SBarry Smith /*@C 74d781fa04SBarry 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 75d781fa04SBarry Smith converts %g to [|%g|] so that PetscVSNPrintf() can easily insure all %g formatted numbers have a decimal point when printed. 76d781fa04SBarry Smith 77d781fa04SBarry Smith Input Parameters: 78d781fa04SBarry Smith + format - the PETSc format string 79d781fa04SBarry Smith . newformat - the location to put the new format 80d781fa04SBarry Smith - size - the length of newformat, you can use PetscFormatConvertGetSize() to compute the needed size 81d781fa04SBarry Smith 82d781fa04SBarry Smith Note: this exists so we can have the same code when PetscInt is either int or long long int 83d781fa04SBarry Smith 84d781fa04SBarry Smith Level: developer 85d781fa04SBarry Smith 86d781fa04SBarry Smith .seealso: PetscFormatConvertGetSize(), PetscVSNPrintf(), PetscVFPrintf() 87d781fa04SBarry Smith 88d781fa04SBarry Smith @*/ 89d781fa04SBarry Smith PetscErrorCode PetscFormatConvert(const char *format,char *newformat) 90e5c89e4eSSatish Balay { 91e5c89e4eSSatish Balay PetscInt i = 0, j = 0; 92e5c89e4eSSatish Balay 93eed5747fSBarry Smith PetscFunctionBegin; 94d781fa04SBarry 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 13914416c0eSBarry Smith #define PETSCDEFAULTBUFFERSIZE 8*1024 140d781fa04SBarry 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 15195452b02SPatrick Sanan Developer Notes: 15295452b02SPatrick Sanan this function may be called from an error handler, if an error occurs when it is called by the error handler than likely 153eed5747fSBarry Smith a recursion will occur and possible crash. 154c9a19010SBarry Smith 155c9a19010SBarry Smith Level: developer 156c9a19010SBarry Smith 157d781fa04SBarry Smith .seealso: PetscVSNPrintf(), PetscErrorPrintf(), PetscVPrintf() 158d781fa04SBarry Smith 159c9a19010SBarry Smith @*/ 1607087cfbeSBarry Smith PetscErrorCode PetscVSNPrintf(char *str,size_t len,const char *format,size_t *fullLength,va_list Argp) 161e5c89e4eSSatish Balay { 162d781fa04SBarry Smith char *newformat = NULL; 16314416c0eSBarry Smith char formatbuf[PETSCDEFAULTBUFFERSIZE]; 164d781fa04SBarry Smith size_t newLength; 1651a15ef5dSMatthew Knepley PetscErrorCode ierr; 16614416c0eSBarry Smith int flen; 167e5c89e4eSSatish Balay 168eed5747fSBarry Smith PetscFunctionBegin; 169d781fa04SBarry Smith ierr = PetscFormatConvertGetSize(format,&newLength);CHKERRQ(ierr); 17014416c0eSBarry Smith if (newLength < PETSCDEFAULTBUFFERSIZE) { 171e2135aedSMatthew Knepley newformat = formatbuf; 17214416c0eSBarry Smith newLength = PETSCDEFAULTBUFFERSIZE-1; 173e2135aedSMatthew Knepley } else { 174d781fa04SBarry Smith ierr = PetscMalloc1(newLength, &newformat);CHKERRQ(ierr); 175e2135aedSMatthew Knepley } 176d781fa04SBarry Smith ierr = PetscFormatConvert(format,newformat);CHKERRQ(ierr); 1777b9a2d1bSSatish Balay #if defined(PETSC_HAVE_VSNPRINTF) 178152b30f0SSatish Balay flen = vsnprintf(str,len,newformat,Argp); 179e5c89e4eSSatish Balay #else 18089b07760SSatish Balay #error "vsnprintf not found" 181e5c89e4eSSatish Balay #endif 18214416c0eSBarry Smith if (newLength > PETSCDEFAULTBUFFERSIZE-1) { 183e2135aedSMatthew Knepley ierr = PetscFree(newformat);CHKERRQ(ierr); 184e2135aedSMatthew Knepley } 1858627564fSBarry Smith { 1868627564fSBarry Smith PetscBool foundedot; 1878627564fSBarry Smith size_t cnt = 0,ncnt = 0,leng; 1888627564fSBarry Smith ierr = PetscStrlen(str,&leng);CHKERRQ(ierr); 18917ca8410SBarry Smith if (leng > 4) { 1908627564fSBarry Smith for (cnt=0; cnt<leng-4; cnt++) { 1918627564fSBarry Smith if (str[cnt] == '[' && str[cnt+1] == '|'){ 192c540d043SBarry Smith flen -= 4; 1938627564fSBarry Smith cnt++; cnt++; 1948627564fSBarry Smith foundedot = PETSC_FALSE; 1958627564fSBarry Smith for (; cnt<leng-1; cnt++) { 1968627564fSBarry Smith if (str[cnt] == '|' && str[cnt+1] == ']'){ 1978627564fSBarry Smith cnt++; 1988627564fSBarry Smith if (!foundedot) str[ncnt++] = '.'; 1998627564fSBarry Smith ncnt--; 2008627564fSBarry Smith break; 2018627564fSBarry Smith } else { 2028627564fSBarry Smith if (str[cnt] == 'e' || str[cnt] == '.') foundedot = PETSC_TRUE; 2038627564fSBarry Smith str[ncnt++] = str[cnt]; 2048627564fSBarry Smith } 2058627564fSBarry Smith } 2068627564fSBarry Smith } else { 2078627564fSBarry Smith str[ncnt] = str[cnt]; 2088627564fSBarry Smith } 2098627564fSBarry Smith ncnt++; 2108627564fSBarry Smith } 2118627564fSBarry Smith while (cnt < leng) { 2128627564fSBarry Smith str[ncnt] = str[cnt]; ncnt++; cnt++; 2138627564fSBarry Smith } 2148627564fSBarry Smith str[ncnt] = 0; 2158627564fSBarry Smith } 2168627564fSBarry Smith } 217748e1b9dSBarry Smith #if defined(PETSC_HAVE_WINDOWS_H) && !defined(PETSC_HAVE__SET_OUTPUT_FORMAT) 218e51f71cfSBarry Smith /* older Windows OS always produces e-+0np for floating point output; remove the extra 0 */ 219748e1b9dSBarry Smith { 220748e1b9dSBarry Smith size_t cnt = 0,ncnt = 0,leng; 221748e1b9dSBarry Smith ierr = PetscStrlen(str,&leng);CHKERRQ(ierr); 222748e1b9dSBarry Smith if (leng > 5) { 223748e1b9dSBarry Smith for (cnt=0; cnt<leng-4; cnt++) { 224e51f71cfSBarry 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') { 225748e1b9dSBarry Smith str[ncnt] = str[cnt]; ncnt++; cnt++; 226e51f71cfSBarry Smith str[ncnt] = str[cnt]; ncnt++; cnt++; cnt++; 227e51f71cfSBarry Smith str[ncnt] = str[cnt]; 228748e1b9dSBarry Smith } else { 229748e1b9dSBarry Smith str[ncnt] = str[cnt]; 230748e1b9dSBarry Smith } 231748e1b9dSBarry Smith ncnt++; 232748e1b9dSBarry Smith } 233748e1b9dSBarry Smith while (cnt < leng) { 234748e1b9dSBarry Smith str[ncnt] = str[cnt]; ncnt++; cnt++; 235748e1b9dSBarry Smith } 236748e1b9dSBarry Smith str[ncnt] = 0; 237748e1b9dSBarry Smith } 238748e1b9dSBarry Smith } 239748e1b9dSBarry Smith #endif 240c540d043SBarry Smith if (fullLength) *fullLength = 1 + (size_t) flen; 241eed5747fSBarry Smith PetscFunctionReturn(0); 242e5c89e4eSSatish Balay } 243e5c89e4eSSatish Balay 244c9a19010SBarry Smith /*@C 245c9a19010SBarry Smith PetscVFPrintf - All PETSc standard out and error messages are sent through this function; so, in theory, this can 246e5c89e4eSSatish Balay can be replaced with something that does not simply write to a file. 247e5c89e4eSSatish Balay 248c9a19010SBarry Smith To use, write your own function for example, 249c9a19010SBarry Smith $PetscErrorCode mypetscvfprintf(FILE *fd,const char format[],va_list Argp) 250c9a19010SBarry Smith ${ 251c9a19010SBarry Smith $ PetscErrorCode ierr; 252c9a19010SBarry Smith $ 253c9a19010SBarry Smith $ PetscFunctionBegin; 254c9a19010SBarry Smith $ if (fd != stdout && fd != stderr) { handle regular files 255c9a19010SBarry Smith $ ierr = PetscVFPrintfDefault(fd,format,Argp);CHKERR(ierr); 256c9a19010SBarry Smith $ } else { 257c9a19010SBarry Smith $ char buff[BIG]; 258c9a19010SBarry Smith $ size_t length; 259c9a19010SBarry Smith $ ierr = PetscVSNPrintf(buff,BIG,format,&length,Argp);CHKERRQ(ierr); 260c9a19010SBarry Smith $ now send buff to whatever stream or whatever you want 261c9a19010SBarry Smith $ } 262c9a19010SBarry Smith $ PetscFunctionReturn(0); 263c9a19010SBarry Smith $} 264c9a19010SBarry Smith then before the call to PetscInitialize() do the assignment 265c9a19010SBarry Smith $ PetscVFPrintf = mypetscvfprintf; 266c9a19010SBarry Smith 26795452b02SPatrick Sanan Notes: 26895452b02SPatrick Sanan For error messages this may be called by any process, for regular standard out it is 269e5c89e4eSSatish Balay called only by process 0 of a given communicator 270e5c89e4eSSatish Balay 27195452b02SPatrick Sanan Developer Notes: 27295452b02SPatrick Sanan this could be called by an error handler, if that happens then a recursion of the error handler may occur 273eed5747fSBarry Smith and a crash 274c9a19010SBarry Smith 275c9a19010SBarry Smith Level: developer 276c9a19010SBarry Smith 277c9a19010SBarry Smith .seealso: PetscVSNPrintf(), PetscErrorPrintf() 278c9a19010SBarry Smith 279c9a19010SBarry Smith @*/ 2807087cfbeSBarry Smith PetscErrorCode PetscVFPrintfDefault(FILE *fd,const char *format,va_list Argp) 281e5c89e4eSSatish Balay { 28214416c0eSBarry Smith char str[PETSCDEFAULTBUFFERSIZE]; 28314416c0eSBarry Smith char *buff = str; 28414416c0eSBarry Smith size_t fullLength; 285eed5747fSBarry Smith PetscErrorCode ierr; 2861531940fSBarry Smith #if defined(PETSC_HAVE_VA_COPY) 28714416c0eSBarry Smith va_list Argpcopy; 2881531940fSBarry Smith #endif 2891179db26SBarry Smith 290eed5747fSBarry Smith PetscFunctionBegin; 2911531940fSBarry Smith #if defined(PETSC_HAVE_VA_COPY) 29214416c0eSBarry Smith va_copy(Argpcopy,Argp); 2931531940fSBarry Smith #endif 29414416c0eSBarry Smith ierr = PetscVSNPrintf(str,sizeof(str),format,&fullLength,Argp);CHKERRQ(ierr); 29514416c0eSBarry Smith if (fullLength > sizeof(str)) { 29614416c0eSBarry Smith ierr = PetscMalloc1(fullLength,&buff);CHKERRQ(ierr); 2971531940fSBarry Smith #if defined(PETSC_HAVE_VA_COPY) 29814416c0eSBarry Smith ierr = PetscVSNPrintf(buff,fullLength,format,NULL,Argpcopy);CHKERRQ(ierr); 2991531940fSBarry Smith #else 3001531940fSBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"C89 does not support va_copy() hence cannot print long strings with PETSc printing routines"); 3011531940fSBarry Smith #endif 30214416c0eSBarry Smith } 30314416c0eSBarry Smith fprintf(fd,"%s",buff);CHKERRQ(ierr); 3045a058713SBarry Smith fflush(fd); 30514416c0eSBarry Smith if (buff != str) { 30614416c0eSBarry Smith ierr = PetscFree(buff);CHKERRQ(ierr); 30714416c0eSBarry Smith } 308eed5747fSBarry Smith PetscFunctionReturn(0); 309e5c89e4eSSatish Balay } 310e5c89e4eSSatish Balay 3115b5bc046SBarry Smith /*@C 3125b5bc046SBarry Smith PetscSNPrintf - Prints to a string of given length 3135b5bc046SBarry Smith 3145b5bc046SBarry Smith Not Collective 3155b5bc046SBarry Smith 3165b5bc046SBarry Smith Input Parameters: 3175b5bc046SBarry Smith + str - the string to print to 3185b5bc046SBarry Smith . len - the length of str 3195b5bc046SBarry Smith . format - the usual printf() format string 3205b5bc046SBarry Smith - any arguments 3215b5bc046SBarry Smith 3225b5bc046SBarry Smith Level: intermediate 3235b5bc046SBarry Smith 3245b5bc046SBarry Smith .seealso: PetscSynchronizedFlush(), PetscSynchronizedFPrintf(), PetscFPrintf(), PetscVSNPrintf(), 325d781fa04SBarry Smith PetscPrintf(), PetscViewerASCIIPrintf(), PetscViewerASCIISynchronizedPrintf(), PetscVFPrintf() 3265b5bc046SBarry Smith @*/ 3277087cfbeSBarry Smith PetscErrorCode PetscSNPrintf(char *str,size_t len,const char format[],...) 3285b5bc046SBarry Smith { 3295b5bc046SBarry Smith PetscErrorCode ierr; 330c9a19010SBarry Smith size_t fullLength; 3315b5bc046SBarry Smith va_list Argp; 3325b5bc046SBarry Smith 3335b5bc046SBarry Smith PetscFunctionBegin; 3345b5bc046SBarry Smith va_start(Argp,format); 3352d609e63SMatthew Knepley ierr = PetscVSNPrintf(str,len,format,&fullLength,Argp);CHKERRQ(ierr); 3365b5bc046SBarry Smith PetscFunctionReturn(0); 3375b5bc046SBarry Smith } 3385b5bc046SBarry Smith 339257d2499SJed Brown /*@C 340257d2499SJed Brown PetscSNPrintfCount - Prints to a string of given length, returns count 341257d2499SJed Brown 342257d2499SJed Brown Not Collective 343257d2499SJed Brown 344257d2499SJed Brown Input Parameters: 345257d2499SJed Brown + str - the string to print to 346257d2499SJed Brown . len - the length of str 347257d2499SJed Brown . format - the usual printf() format string 348257d2499SJed Brown - any arguments 349257d2499SJed Brown 350cb398dd3SBarry Smith Output Parameter: 351cb398dd3SBarry Smith . countused - number of characters used 352cb398dd3SBarry Smith 353257d2499SJed Brown Level: intermediate 354257d2499SJed Brown 355257d2499SJed Brown .seealso: PetscSynchronizedFlush(), PetscSynchronizedFPrintf(), PetscFPrintf(), PetscVSNPrintf(), 356d781fa04SBarry Smith PetscPrintf(), PetscViewerASCIIPrintf(), PetscViewerASCIISynchronizedPrintf(), PetscSNPrintf(), PetscVFPrintf() 357257d2499SJed Brown @*/ 358257d2499SJed Brown PetscErrorCode PetscSNPrintfCount(char *str,size_t len,const char format[],size_t *countused,...) 359257d2499SJed Brown { 360257d2499SJed Brown PetscErrorCode ierr; 361257d2499SJed Brown va_list Argp; 362257d2499SJed Brown 363257d2499SJed Brown PetscFunctionBegin; 364257d2499SJed Brown va_start(Argp,countused); 365257d2499SJed Brown ierr = PetscVSNPrintf(str,len,format,countused,Argp);CHKERRQ(ierr); 366257d2499SJed Brown PetscFunctionReturn(0); 367257d2499SJed Brown } 368257d2499SJed Brown 369e5c89e4eSSatish Balay /* ----------------------------------------------------------------------- */ 370e5c89e4eSSatish Balay 371d30b0576SJed Brown PrintfQueue petsc_printfqueue = 0,petsc_printfqueuebase = 0; 372d30b0576SJed Brown int petsc_printfqueuelength = 0; 373e5c89e4eSSatish Balay 374e5c89e4eSSatish Balay /*@C 375e5c89e4eSSatish Balay PetscSynchronizedPrintf - Prints synchronized output from several processors. 376e5c89e4eSSatish Balay Output of the first processor is followed by that of the second, etc. 377e5c89e4eSSatish Balay 378e5c89e4eSSatish Balay Not Collective 379e5c89e4eSSatish Balay 380e5c89e4eSSatish Balay Input Parameters: 381e5c89e4eSSatish Balay + comm - the communicator 382e5c89e4eSSatish Balay - format - the usual printf() format string 383e5c89e4eSSatish Balay 384e5c89e4eSSatish Balay Level: intermediate 385e5c89e4eSSatish Balay 386e5c89e4eSSatish Balay Notes: 3877889ec69SBarry Smith REQUIRES a call to PetscSynchronizedFlush() by all the processes after the completion of the calls to PetscSynchronizedPrintf() for the information 388e5c89e4eSSatish Balay from all the processors to be printed. 389e5c89e4eSSatish Balay 390e5c89e4eSSatish Balay Fortran Note: 391d60d70ccSBarry Smith The call sequence is PetscSynchronizedPrintf(MPI_Comm, character(*), PetscErrorCode ierr) from Fortran. 392e5c89e4eSSatish Balay That is, you can only pass a single character string from Fortran. 393e5c89e4eSSatish Balay 394e5c89e4eSSatish Balay .seealso: PetscSynchronizedFlush(), PetscSynchronizedFPrintf(), PetscFPrintf(), 395e5c89e4eSSatish Balay PetscPrintf(), PetscViewerASCIIPrintf(), PetscViewerASCIISynchronizedPrintf() 396e5c89e4eSSatish Balay @*/ 3977087cfbeSBarry Smith PetscErrorCode PetscSynchronizedPrintf(MPI_Comm comm,const char format[],...) 398e5c89e4eSSatish Balay { 399e5c89e4eSSatish Balay PetscErrorCode ierr; 400e5c89e4eSSatish Balay PetscMPIInt rank; 401e5c89e4eSSatish Balay 402e5c89e4eSSatish Balay PetscFunctionBegin; 4036180deefSBarry Smith if (comm == MPI_COMM_NULL) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Called with MPI_COMM_NULL, likely PetscObjectComm() failed"); 404e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 405e5c89e4eSSatish Balay 406e5c89e4eSSatish Balay /* First processor prints immediately to stdout */ 407e5c89e4eSSatish Balay if (!rank) { 408e5c89e4eSSatish Balay va_list Argp; 409e5c89e4eSSatish Balay va_start(Argp,format); 4101179db26SBarry Smith ierr = (*PetscVFPrintf)(PETSC_STDOUT,format,Argp);CHKERRQ(ierr); 411e5c89e4eSSatish Balay if (petsc_history) { 412cdc7d174SSatish Balay va_start(Argp,format); 4131179db26SBarry Smith ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr); 414e5c89e4eSSatish Balay } 415e5c89e4eSSatish Balay va_end(Argp); 416e5c89e4eSSatish Balay } else { /* other processors add to local queue */ 417e5c89e4eSSatish Balay va_list Argp; 418e5c89e4eSSatish Balay PrintfQueue next; 41914416c0eSBarry Smith size_t fullLength = PETSCDEFAULTBUFFERSIZE; 420e5c89e4eSSatish Balay 421b00a9115SJed Brown ierr = PetscNew(&next);CHKERRQ(ierr); 422a297a907SKarl Rupp if (petsc_printfqueue) { 423a297a907SKarl Rupp petsc_printfqueue->next = next; 424a297a907SKarl Rupp petsc_printfqueue = next; 425a297a907SKarl Rupp petsc_printfqueue->next = 0; 426a297a907SKarl Rupp } else petsc_printfqueuebase = petsc_printfqueue = next; 427d30b0576SJed Brown petsc_printfqueuelength++; 4282d609e63SMatthew Knepley next->size = -1; 42914416c0eSBarry Smith next->string = NULL; 4309ad23270SJed Brown while ((PetscInt)fullLength >= next->size) { 4312d609e63SMatthew Knepley next->size = fullLength+1; 43214416c0eSBarry Smith ierr = PetscFree(next->string);CHKERRQ(ierr); 433785e854fSJed Brown ierr = PetscMalloc1(next->size, &next->string);CHKERRQ(ierr); 434e5c89e4eSSatish Balay va_start(Argp,format); 435580bdb30SBarry Smith ierr = PetscArrayzero(next->string,next->size);CHKERRQ(ierr); 4362d609e63SMatthew Knepley ierr = PetscVSNPrintf(next->string,next->size,format, &fullLength,Argp);CHKERRQ(ierr); 437e5c89e4eSSatish Balay va_end(Argp); 438e5c89e4eSSatish Balay } 4392d609e63SMatthew Knepley } 440e5c89e4eSSatish Balay PetscFunctionReturn(0); 441e5c89e4eSSatish Balay } 442e5c89e4eSSatish Balay 443e5c89e4eSSatish Balay /*@C 444e5c89e4eSSatish Balay PetscSynchronizedFPrintf - Prints synchronized output to the specified file from 445e5c89e4eSSatish Balay several processors. Output of the first processor is followed by that of the 446e5c89e4eSSatish Balay second, etc. 447e5c89e4eSSatish Balay 448e5c89e4eSSatish Balay Not Collective 449e5c89e4eSSatish Balay 450e5c89e4eSSatish Balay Input Parameters: 451e5c89e4eSSatish Balay + comm - the communicator 452e5c89e4eSSatish Balay . fd - the file pointer 453e5c89e4eSSatish Balay - format - the usual printf() format string 454e5c89e4eSSatish Balay 455e5c89e4eSSatish Balay Level: intermediate 456e5c89e4eSSatish Balay 457e5c89e4eSSatish Balay Notes: 458e5c89e4eSSatish Balay REQUIRES a intervening call to PetscSynchronizedFlush() for the information 459e5c89e4eSSatish Balay from all the processors to be printed. 460e5c89e4eSSatish Balay 461e5c89e4eSSatish Balay .seealso: PetscSynchronizedPrintf(), PetscSynchronizedFlush(), PetscFPrintf(), 462e5c89e4eSSatish Balay PetscFOpen(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIPrintf() 463e5c89e4eSSatish Balay 464e5c89e4eSSatish Balay @*/ 4657087cfbeSBarry Smith PetscErrorCode PetscSynchronizedFPrintf(MPI_Comm comm,FILE *fp,const char format[],...) 466e5c89e4eSSatish Balay { 467e5c89e4eSSatish Balay PetscErrorCode ierr; 468e5c89e4eSSatish Balay PetscMPIInt rank; 469e5c89e4eSSatish Balay 470e5c89e4eSSatish Balay PetscFunctionBegin; 4716180deefSBarry Smith if (comm == MPI_COMM_NULL) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Called with MPI_COMM_NULL, likely PetscObjectComm() failed"); 472e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 473e5c89e4eSSatish Balay 474e5c89e4eSSatish Balay /* First processor prints immediately to fp */ 475e5c89e4eSSatish Balay if (!rank) { 476e5c89e4eSSatish Balay va_list Argp; 477e5c89e4eSSatish Balay va_start(Argp,format); 4781179db26SBarry Smith ierr = (*PetscVFPrintf)(fp,format,Argp);CHKERRQ(ierr); 479cdc7d174SSatish Balay if (petsc_history && (fp !=petsc_history)) { 480cdc7d174SSatish Balay va_start(Argp,format); 4811179db26SBarry Smith ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr); 482e5c89e4eSSatish Balay } 483e5c89e4eSSatish Balay va_end(Argp); 484e5c89e4eSSatish Balay } else { /* other processors add to local queue */ 485e5c89e4eSSatish Balay va_list Argp; 486e5c89e4eSSatish Balay PrintfQueue next; 48714416c0eSBarry Smith size_t fullLength = PETSCDEFAULTBUFFERSIZE; 48814416c0eSBarry Smith 489b00a9115SJed Brown ierr = PetscNew(&next);CHKERRQ(ierr); 490a297a907SKarl Rupp if (petsc_printfqueue) { 491a297a907SKarl Rupp petsc_printfqueue->next = next; 492a297a907SKarl Rupp petsc_printfqueue = next; 493a297a907SKarl Rupp petsc_printfqueue->next = 0; 494a297a907SKarl Rupp } else petsc_printfqueuebase = petsc_printfqueue = next; 495d30b0576SJed Brown petsc_printfqueuelength++; 4962d609e63SMatthew Knepley next->size = -1; 49714416c0eSBarry Smith next->string = NULL; 4989ad23270SJed Brown while ((PetscInt)fullLength >= next->size) { 4992d609e63SMatthew Knepley next->size = fullLength+1; 50014416c0eSBarry Smith ierr = PetscFree(next->string);CHKERRQ(ierr); 501785e854fSJed Brown ierr = PetscMalloc1(next->size, &next->string);CHKERRQ(ierr); 502e5c89e4eSSatish Balay va_start(Argp,format); 503580bdb30SBarry Smith ierr = PetscArrayzero(next->string,next->size);CHKERRQ(ierr); 5042d609e63SMatthew Knepley ierr = PetscVSNPrintf(next->string,next->size,format,&fullLength,Argp);CHKERRQ(ierr); 505e5c89e4eSSatish Balay va_end(Argp); 506e5c89e4eSSatish Balay } 5072d609e63SMatthew Knepley } 508e5c89e4eSSatish Balay PetscFunctionReturn(0); 509e5c89e4eSSatish Balay } 510e5c89e4eSSatish Balay 5110ec8b6e3SBarry Smith /*@C 512e5c89e4eSSatish Balay PetscSynchronizedFlush - Flushes to the screen output from all processors 5137889ec69SBarry Smith involved in previous PetscSynchronizedPrintf()/PetscSynchronizedFPrintf() calls. 514e5c89e4eSSatish Balay 515d083f849SBarry Smith Collective 516e5c89e4eSSatish Balay 517e5c89e4eSSatish Balay Input Parameters: 5180ec8b6e3SBarry Smith + comm - the communicator 5190ec8b6e3SBarry Smith - fd - the file pointer (valid on process 0 of the communicator) 520e5c89e4eSSatish Balay 521e5c89e4eSSatish Balay Level: intermediate 522e5c89e4eSSatish Balay 523e5c89e4eSSatish Balay Notes: 5247889ec69SBarry Smith If PetscSynchronizedPrintf() and/or PetscSynchronizedFPrintf() are called with 5257889ec69SBarry Smith different MPI communicators there must be an intervening call to PetscSynchronizedFlush() between the calls with different MPI communicators. 526e5c89e4eSSatish Balay 527e50bf69fSBarry Smith From Fortran pass PETSC_STDOUT if the flush is for standard out; otherwise pass a value obtained from PetscFOpen() 528e50bf69fSBarry Smith 529e5c89e4eSSatish Balay .seealso: PetscSynchronizedPrintf(), PetscFPrintf(), PetscPrintf(), PetscViewerASCIIPrintf(), 530e5c89e4eSSatish Balay PetscViewerASCIISynchronizedPrintf() 5310087d953SMatthew G. Knepley @*/ 5320ec8b6e3SBarry Smith PetscErrorCode PetscSynchronizedFlush(MPI_Comm comm,FILE *fd) 533e5c89e4eSSatish Balay { 534e5c89e4eSSatish Balay PetscErrorCode ierr; 53529a5cbdcSMatthew G. Knepley PetscMPIInt rank,size,tag,i,j,n = 0,dummy = 0; 5362d609e63SMatthew Knepley char *message; 537e5c89e4eSSatish Balay MPI_Status status; 538e5c89e4eSSatish Balay 539e5c89e4eSSatish Balay PetscFunctionBegin; 540e5c89e4eSSatish Balay ierr = PetscCommDuplicate(comm,&comm,&tag);CHKERRQ(ierr); 541e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 542e5c89e4eSSatish Balay ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 543e5c89e4eSSatish Balay 544e5c89e4eSSatish Balay /* First processor waits for messages from all other processors */ 545e5c89e4eSSatish Balay if (!rank) { 5460ec8b6e3SBarry Smith if (!fd) fd = PETSC_STDOUT; 547e5c89e4eSSatish Balay for (i=1; i<size; i++) { 5489f73f8ecSBarry Smith /* to prevent a flood of messages to process zero, request each message separately */ 5499f73f8ecSBarry Smith ierr = MPI_Send(&dummy,1,MPI_INT,i,tag,comm);CHKERRQ(ierr); 550e5c89e4eSSatish Balay ierr = MPI_Recv(&n,1,MPI_INT,i,tag,comm,&status);CHKERRQ(ierr); 551e5c89e4eSSatish Balay for (j=0; j<n; j++) { 55229a5cbdcSMatthew G. Knepley PetscMPIInt size = 0; 5532d609e63SMatthew Knepley 5542d609e63SMatthew Knepley ierr = MPI_Recv(&size,1,MPI_INT,i,tag,comm,&status);CHKERRQ(ierr); 555785e854fSJed Brown ierr = PetscMalloc1(size, &message);CHKERRQ(ierr); 5562d609e63SMatthew Knepley ierr = MPI_Recv(message,size,MPI_CHAR,i,tag,comm,&status);CHKERRQ(ierr); 5573bf036e2SBarry Smith ierr = PetscFPrintf(comm,fd,"%s",message);CHKERRQ(ierr); 5582d609e63SMatthew Knepley ierr = PetscFree(message);CHKERRQ(ierr); 559e5c89e4eSSatish Balay } 560e5c89e4eSSatish Balay } 561e5c89e4eSSatish Balay } else { /* other processors send queue to processor 0 */ 562d30b0576SJed Brown PrintfQueue next = petsc_printfqueuebase,previous; 563e5c89e4eSSatish Balay 564b3ef9d35SBarry Smith ierr = MPI_Recv(&dummy,1,MPI_INT,0,tag,comm,&status);CHKERRQ(ierr); 565d30b0576SJed Brown ierr = MPI_Send(&petsc_printfqueuelength,1,MPI_INT,0,tag,comm);CHKERRQ(ierr); 566d30b0576SJed Brown for (i=0; i<petsc_printfqueuelength; i++) { 5672d609e63SMatthew Knepley ierr = MPI_Send(&next->size,1,MPI_INT,0,tag,comm);CHKERRQ(ierr); 5682d609e63SMatthew Knepley ierr = MPI_Send(next->string,next->size,MPI_CHAR,0,tag,comm);CHKERRQ(ierr); 569e5c89e4eSSatish Balay previous = next; 570e5c89e4eSSatish Balay next = next->next; 5712d609e63SMatthew Knepley ierr = PetscFree(previous->string);CHKERRQ(ierr); 572e5c89e4eSSatish Balay ierr = PetscFree(previous);CHKERRQ(ierr); 573e5c89e4eSSatish Balay } 574d30b0576SJed Brown petsc_printfqueue = 0; 575d30b0576SJed Brown petsc_printfqueuelength = 0; 576e5c89e4eSSatish Balay } 577e5c89e4eSSatish Balay ierr = PetscCommDestroy(&comm);CHKERRQ(ierr); 578e5c89e4eSSatish Balay PetscFunctionReturn(0); 579e5c89e4eSSatish Balay } 580e5c89e4eSSatish Balay 581e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------------------*/ 582e5c89e4eSSatish Balay 583e5c89e4eSSatish Balay /*@C 584e5c89e4eSSatish Balay PetscFPrintf - Prints to a file, only from the first 585e5c89e4eSSatish Balay processor in the communicator. 586e5c89e4eSSatish Balay 587e5c89e4eSSatish Balay Not Collective 588e5c89e4eSSatish Balay 589e5c89e4eSSatish Balay Input Parameters: 590e5c89e4eSSatish Balay + comm - the communicator 591e5c89e4eSSatish Balay . fd - the file pointer 592e5c89e4eSSatish Balay - format - the usual printf() format string 593e5c89e4eSSatish Balay 594e5c89e4eSSatish Balay Level: intermediate 595e5c89e4eSSatish Balay 596e5c89e4eSSatish Balay Fortran Note: 597e5c89e4eSSatish Balay This routine is not supported in Fortran. 598e5c89e4eSSatish Balay 599e5c89e4eSSatish Balay 600e5c89e4eSSatish Balay .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(), 601e5c89e4eSSatish Balay PetscViewerASCIISynchronizedPrintf(), PetscSynchronizedFlush() 602e5c89e4eSSatish Balay @*/ 6037087cfbeSBarry Smith PetscErrorCode PetscFPrintf(MPI_Comm comm,FILE* fd,const char format[],...) 604e5c89e4eSSatish Balay { 605e5c89e4eSSatish Balay PetscErrorCode ierr; 606e5c89e4eSSatish Balay PetscMPIInt rank; 607e5c89e4eSSatish Balay 608e5c89e4eSSatish Balay PetscFunctionBegin; 6096180deefSBarry Smith if (comm == MPI_COMM_NULL) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Called with MPI_COMM_NULL, likely PetscObjectComm() failed"); 610e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 611e5c89e4eSSatish Balay if (!rank) { 612e5c89e4eSSatish Balay va_list Argp; 613e5c89e4eSSatish Balay va_start(Argp,format); 6141179db26SBarry Smith ierr = (*PetscVFPrintf)(fd,format,Argp);CHKERRQ(ierr); 615cdc7d174SSatish Balay if (petsc_history && (fd !=petsc_history)) { 616cdc7d174SSatish Balay va_start(Argp,format); 6171179db26SBarry Smith ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr); 618e5c89e4eSSatish Balay } 619e5c89e4eSSatish Balay va_end(Argp); 620e5c89e4eSSatish Balay } 621e5c89e4eSSatish Balay PetscFunctionReturn(0); 622e5c89e4eSSatish Balay } 623e5c89e4eSSatish Balay 624e5c89e4eSSatish Balay /*@C 625e5c89e4eSSatish Balay PetscPrintf - Prints to standard out, only from the first 626eed5747fSBarry Smith processor in the communicator. Calls from other processes are ignored. 627e5c89e4eSSatish Balay 628e5c89e4eSSatish Balay Not Collective 629e5c89e4eSSatish Balay 630e5c89e4eSSatish Balay Input Parameters: 631e5c89e4eSSatish Balay + comm - the communicator 632e5c89e4eSSatish Balay - format - the usual printf() format string 633e5c89e4eSSatish Balay 634e5c89e4eSSatish Balay Level: intermediate 635e5c89e4eSSatish Balay 636e5c89e4eSSatish Balay Fortran Note: 637d60d70ccSBarry Smith The call sequence is PetscPrintf(MPI_Comm, character(*), PetscErrorCode ierr) from Fortran. 638e5c89e4eSSatish Balay That is, you can only pass a single character string from Fortran. 639e5c89e4eSSatish Balay 640e5c89e4eSSatish Balay 641e5c89e4eSSatish Balay .seealso: PetscFPrintf(), PetscSynchronizedPrintf() 642e5c89e4eSSatish Balay @*/ 6437087cfbeSBarry Smith PetscErrorCode PetscPrintf(MPI_Comm comm,const char format[],...) 644e5c89e4eSSatish Balay { 645e5c89e4eSSatish Balay PetscErrorCode ierr; 646e5c89e4eSSatish Balay PetscMPIInt rank; 647e5c89e4eSSatish Balay 648e5c89e4eSSatish Balay PetscFunctionBegin; 6496180deefSBarry Smith if (comm == MPI_COMM_NULL) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Called with MPI_COMM_NULL, likely PetscObjectComm() failed"); 650e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 651e5c89e4eSSatish Balay if (!rank) { 652e5c89e4eSSatish Balay va_list Argp; 653e5c89e4eSSatish Balay va_start(Argp,format); 654eed5747fSBarry Smith ierr = (*PetscVFPrintf)(PETSC_STDOUT,format,Argp);CHKERRQ(ierr); 655e5c89e4eSSatish Balay if (petsc_history) { 656cdc7d174SSatish Balay va_start(Argp,format); 657eed5747fSBarry Smith ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr); 658e5c89e4eSSatish Balay } 659e5c89e4eSSatish Balay va_end(Argp); 660e5c89e4eSSatish Balay } 661e5c89e4eSSatish Balay PetscFunctionReturn(0); 662e5c89e4eSSatish Balay } 663e5c89e4eSSatish Balay 664e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------------------*/ 665c9a19010SBarry Smith /*@C 666c9a19010SBarry Smith PetscHelpPrintf - All PETSc help messages are passing through this function. You can change how help messages are printed by 667c9a19010SBarry Smith replacinng it with something that does not simply write to a stdout. 668c9a19010SBarry Smith 669c9a19010SBarry Smith To use, write your own function for example, 670c9a19010SBarry Smith $PetscErrorCode mypetschelpprintf(MPI_Comm comm,const char format[],....) 671c9a19010SBarry Smith ${ 672c9a19010SBarry Smith $ PetscFunctionReturn(0); 673c9a19010SBarry Smith $} 674c9a19010SBarry Smith then before the call to PetscInitialize() do the assignment 675c9a19010SBarry Smith $ PetscHelpPrintf = mypetschelpprintf; 676c9a19010SBarry Smith 677c9a19010SBarry Smith Note: the default routine used is called PetscHelpPrintfDefault(). 678c9a19010SBarry Smith 679c9a19010SBarry Smith Level: developer 680c9a19010SBarry Smith 681c9a19010SBarry Smith .seealso: PetscVSNPrintf(), PetscVFPrintf(), PetscErrorPrintf() 682c9a19010SBarry Smith @*/ 6837087cfbeSBarry Smith PetscErrorCode PetscHelpPrintfDefault(MPI_Comm comm,const char format[],...) 684e5c89e4eSSatish Balay { 685e5c89e4eSSatish Balay PetscErrorCode ierr; 686e5c89e4eSSatish Balay PetscMPIInt rank; 687e5c89e4eSSatish Balay 688e5c89e4eSSatish Balay PetscFunctionBegin; 6896180deefSBarry Smith if (comm == MPI_COMM_NULL) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Called with MPI_COMM_NULL, likely PetscObjectComm() failed"); 690e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 691e5c89e4eSSatish Balay if (!rank) { 692e5c89e4eSSatish Balay va_list Argp; 693e5c89e4eSSatish Balay va_start(Argp,format); 6941179db26SBarry Smith ierr = (*PetscVFPrintf)(PETSC_STDOUT,format,Argp);CHKERRQ(ierr); 695e5c89e4eSSatish Balay if (petsc_history) { 696cdc7d174SSatish Balay va_start(Argp,format); 6971179db26SBarry Smith ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr); 698e5c89e4eSSatish Balay } 699e5c89e4eSSatish Balay va_end(Argp); 700e5c89e4eSSatish Balay } 701e5c89e4eSSatish Balay PetscFunctionReturn(0); 702e5c89e4eSSatish Balay } 703e5c89e4eSSatish Balay 704e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------------------*/ 705e5c89e4eSSatish Balay 706e5c89e4eSSatish Balay 707e5c89e4eSSatish Balay /*@C 708e5c89e4eSSatish Balay PetscSynchronizedFGets - Several processors all get the same line from a file. 709e5c89e4eSSatish Balay 710d083f849SBarry Smith Collective 711e5c89e4eSSatish Balay 712e5c89e4eSSatish Balay Input Parameters: 713e5c89e4eSSatish Balay + comm - the communicator 714e5c89e4eSSatish Balay . fd - the file pointer 715e5c89e4eSSatish Balay - len - the length of the output buffer 716e5c89e4eSSatish Balay 717e5c89e4eSSatish Balay Output Parameter: 718e31d4fa4SJed Brown . string - the line read from the file, at end of file string[0] == 0 719e5c89e4eSSatish Balay 720e5c89e4eSSatish Balay Level: intermediate 721e5c89e4eSSatish Balay 722e5c89e4eSSatish Balay .seealso: PetscSynchronizedPrintf(), PetscSynchronizedFlush(), 723e5c89e4eSSatish Balay PetscFOpen(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIPrintf() 724e5c89e4eSSatish Balay 725e5c89e4eSSatish Balay @*/ 7267087cfbeSBarry Smith PetscErrorCode PetscSynchronizedFGets(MPI_Comm comm,FILE *fp,size_t len,char string[]) 727e5c89e4eSSatish Balay { 728e5c89e4eSSatish Balay PetscErrorCode ierr; 729e5c89e4eSSatish Balay PetscMPIInt rank; 730e5c89e4eSSatish Balay 731e5c89e4eSSatish Balay PetscFunctionBegin; 732e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 733e5c89e4eSSatish Balay 734e5c89e4eSSatish Balay if (!rank) { 735047b9c12SMatthew G Knepley char *ptr = fgets(string, len, fp); 736047b9c12SMatthew G Knepley 737047b9c12SMatthew G Knepley if (!ptr) { 738e31d4fa4SJed Brown string[0] = 0; 739e31d4fa4SJed Brown if (!feof(fp)) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_FILE_READ, "Error reading from file: %d", errno); 740047b9c12SMatthew G Knepley } 741e5c89e4eSSatish Balay } 742e5c89e4eSSatish Balay ierr = MPI_Bcast(string,len,MPI_BYTE,0,comm);CHKERRQ(ierr); 743e5c89e4eSSatish Balay PetscFunctionReturn(0); 744e5c89e4eSSatish Balay } 745238ccf28SShri Abhyankar 746*2475b7caSBarry Smith #if defined(PETSC_HAVE_CLOSURE) 747f1d7fe2eSBarry Smith int (^SwiftClosure)(const char*) = 0; 748f1d7fe2eSBarry Smith 749f1d7fe2eSBarry Smith PetscErrorCode PetscVFPrintfToString(FILE *fd,const char format[],va_list Argp) 750f1d7fe2eSBarry Smith { 751f1d7fe2eSBarry Smith PetscErrorCode ierr; 752f1d7fe2eSBarry Smith 753f1d7fe2eSBarry Smith PetscFunctionBegin; 754f1d7fe2eSBarry Smith if (fd != stdout && fd != stderr) { /* handle regular files */ 755f1d7fe2eSBarry Smith ierr = PetscVFPrintfDefault(fd,format,Argp);CHKERRQ(ierr); 756f1d7fe2eSBarry Smith } else { 75714416c0eSBarry Smith size_t length; 75814416c0eSBarry Smith char buff[PETSCDEFAULTBUFFERSIZE]; 759f1d7fe2eSBarry Smith 760*2475b7caSBarry Smith ierr = PetscVSNPrintf(buff,sizeof(buff),format,&length,Argp);CHKERRQ(ierr); 76114416c0eSBarry Smith ierr = SwiftClosure(buff);CHKERRQ(ierr); 762f1d7fe2eSBarry Smith } 763f1d7fe2eSBarry Smith PetscFunctionReturn(0); 764f1d7fe2eSBarry Smith } 765f1d7fe2eSBarry Smith 766f1d7fe2eSBarry Smith /* 767f1d7fe2eSBarry Smith Provide a Swift function that processes all the PETSc calls to PetscVFPrintf() 768f1d7fe2eSBarry Smith */ 769f1d7fe2eSBarry Smith PetscErrorCode PetscVFPrintfSetClosure(int (^closure)(const char*)) 770f1d7fe2eSBarry Smith { 771f1d7fe2eSBarry Smith PetscVFPrintf = PetscVFPrintfToString; 772f1d7fe2eSBarry Smith SwiftClosure = closure; 773f1d7fe2eSBarry Smith return 0; 774f1d7fe2eSBarry Smith } 775f1d7fe2eSBarry Smith #endif 776f1d7fe2eSBarry 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 8011b5687a1SBarry Smith PetscErrorCode PetscFormatRealArray(char buf[],size_t len,const char *fmt,PetscInt n,const PetscReal x[]) 8021b5687a1SBarry Smith { 8031b5687a1SBarry Smith PetscErrorCode ierr; 8041b5687a1SBarry Smith PetscInt i; 8051b5687a1SBarry Smith size_t left,count; 8061b5687a1SBarry Smith char *p; 8071b5687a1SBarry Smith 8081b5687a1SBarry Smith PetscFunctionBegin; 8091b5687a1SBarry Smith for (i=0,p=buf,left=len; i<n; i++) { 8101b5687a1SBarry Smith ierr = PetscSNPrintfCount(p,left,fmt,&count,(double)x[i]);CHKERRQ(ierr); 8111b5687a1SBarry Smith if (count >= left) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Insufficient space in buffer"); 8121b5687a1SBarry Smith left -= count; 8131b5687a1SBarry Smith p += count-1; 8141b5687a1SBarry Smith *p++ = ' '; 8151b5687a1SBarry Smith } 8161b5687a1SBarry Smith p[i ? 0 : -1] = 0; 8171b5687a1SBarry Smith PetscFunctionReturn(0); 8181b5687a1SBarry Smith } 819