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 */ 1602c9f0b5SLisandro Dalcin FILE *PETSC_STDOUT = NULL; 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 */ 2202c9f0b5SLisandro Dalcin FILE *PETSC_STDERR = NULL; 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 { 40*3ca90d2dSJacob Faibussowitsch size_t sz = 0; 41d781fa04SBarry Smith PetscInt i = 0; 42d781fa04SBarry Smith 43d781fa04SBarry Smith PetscFunctionBegin; 44*3ca90d2dSJacob Faibussowitsch PetscValidCharPointer(format,1); 45*3ca90d2dSJacob Faibussowitsch PetscValidPointer(size,2); 46d781fa04SBarry Smith while (format[i]) { 47*3ca90d2dSJacob Faibussowitsch if (format[i] == '%') { 48*3ca90d2dSJacob Faibussowitsch if (format[i+1] == '%') { 49*3ca90d2dSJacob Faibussowitsch i += 2; 50*3ca90d2dSJacob Faibussowitsch sz += 2; 51*3ca90d2dSJacob Faibussowitsch continue; 52*3ca90d2dSJacob Faibussowitsch } 53d781fa04SBarry Smith /* Find the letter */ 54*3ca90d2dSJacob Faibussowitsch while (format[i] && (format[i] <= '9')) {++i;++sz;} 55d781fa04SBarry Smith switch (format[i]) { 56*3ca90d2dSJacob Faibussowitsch #if PetscDefined(USE_64BIT_INDICES) 57d781fa04SBarry Smith case 'D': 58*3ca90d2dSJacob Faibussowitsch sz += 2; 59*3ca90d2dSJacob Faibussowitsch break; 60d781fa04SBarry Smith #endif 61d781fa04SBarry Smith case 'g': 62*3ca90d2dSJacob Faibussowitsch sz += 4; 63d781fa04SBarry Smith default: 64d781fa04SBarry Smith break; 65d781fa04SBarry Smith } 66d781fa04SBarry Smith } 67*3ca90d2dSJacob Faibussowitsch ++i; 68*3ca90d2dSJacob Faibussowitsch ++sz; 69d781fa04SBarry Smith } 70*3ca90d2dSJacob Faibussowitsch *size = sz+1; /* space for NULL character */ 71d781fa04SBarry Smith PetscFunctionReturn(0); 72d781fa04SBarry Smith } 73d781fa04SBarry Smith 74d781fa04SBarry Smith /*@C 75d781fa04SBarry 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 76d781fa04SBarry Smith converts %g to [|%g|] so that PetscVSNPrintf() can easily insure all %g formatted numbers have a decimal point when printed. 77d781fa04SBarry Smith 78d781fa04SBarry Smith Input Parameters: 79d781fa04SBarry Smith + format - the PETSc format string 80d781fa04SBarry Smith . newformat - the location to put the new format 81d781fa04SBarry Smith - size - the length of newformat, you can use PetscFormatConvertGetSize() to compute the needed size 82d781fa04SBarry Smith 83d781fa04SBarry Smith Note: this exists so we can have the same code when PetscInt is either int or long long int 84d781fa04SBarry Smith 85d781fa04SBarry Smith Level: developer 86d781fa04SBarry Smith 87d781fa04SBarry Smith .seealso: PetscFormatConvertGetSize(), PetscVSNPrintf(), PetscVFPrintf() 88d781fa04SBarry Smith 89d781fa04SBarry Smith @*/ 90d781fa04SBarry Smith PetscErrorCode PetscFormatConvert(const char *format,char *newformat) 91e5c89e4eSSatish Balay { 92e5c89e4eSSatish Balay PetscInt i = 0, j = 0; 93e5c89e4eSSatish Balay 94eed5747fSBarry Smith PetscFunctionBegin; 95d781fa04SBarry Smith while (format[i]) { 962a1ad9caSBarry Smith if (format[i] == '%' && format[i+1] == '%') { 972a1ad9caSBarry Smith newformat[j++] = format[i++]; 982a1ad9caSBarry Smith newformat[j++] = format[i++]; 992a1ad9caSBarry Smith } else if (format[i] == '%') { 1008627564fSBarry Smith if (format[i+1] == 'g') { 1018627564fSBarry Smith newformat[j++] = '['; 1028627564fSBarry Smith newformat[j++] = '|'; 1038627564fSBarry Smith } 1047bc47156SJose Roman /* Find the letter */ 1057bc47156SJose Roman for (; format[i] && format[i] <= '9'; i++) newformat[j++] = format[i]; 1067bc47156SJose Roman switch (format[i]) { 1077bc47156SJose Roman case 'D': 1086de02169SBarry Smith #if !defined(PETSC_USE_64BIT_INDICES) 109e5c89e4eSSatish Balay newformat[j++] = 'd'; 110e5c89e4eSSatish Balay #else 111e5c89e4eSSatish Balay newformat[j++] = 'l'; 112e5c89e4eSSatish Balay newformat[j++] = 'l'; 113e5c89e4eSSatish Balay newformat[j++] = 'd'; 114e5c89e4eSSatish Balay #endif 1157bc47156SJose Roman break; 1168627564fSBarry Smith case 'g': 1178627564fSBarry Smith newformat[j++] = format[i]; 1188627564fSBarry Smith if (format[i-1] == '%') { 1198627564fSBarry Smith newformat[j++] = '|'; 1208627564fSBarry Smith newformat[j++] = ']'; 1218627564fSBarry Smith } 1228627564fSBarry Smith break; 1237bc47156SJose Roman case 'G': 124145505e7SDominic Meiser SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"%%G format is no longer supported, use %%g and cast the argument to double"); 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"); 1277bc47156SJose Roman default: 1287bc47156SJose Roman newformat[j++] = format[i]; 1297bc47156SJose Roman break; 1307bc47156SJose Roman } 1317bc47156SJose Roman i++; 132a297a907SKarl Rupp } else newformat[j++] = format[i++]; 133e5c89e4eSSatish Balay } 134e5c89e4eSSatish Balay newformat[j] = 0; 135eed5747fSBarry Smith PetscFunctionReturn(0); 136e5c89e4eSSatish Balay } 137e5c89e4eSSatish Balay 13814416c0eSBarry Smith #define PETSCDEFAULTBUFFERSIZE 8*1024 139d781fa04SBarry Smith 140c9a19010SBarry Smith /*@C 141c9a19010SBarry Smith PetscVSNPrintf - The PETSc version of vsnprintf(). Converts a PETSc format string into a standard C format string and then puts all the 142c9a19010SBarry Smith function arguments into a string using the format statement. 143c9a19010SBarry Smith 144c9a19010SBarry Smith Input Parameters: 145c9a19010SBarry Smith + str - location to put result 146c9a19010SBarry Smith . len - the amount of space in str 147c9a19010SBarry Smith + format - the PETSc format string 148c9a19010SBarry Smith - fullLength - the amount of space in str actually used. 149c9a19010SBarry Smith 15095452b02SPatrick Sanan Developer Notes: 15195452b02SPatrick Sanan 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 156d781fa04SBarry Smith .seealso: PetscVSNPrintf(), PetscErrorPrintf(), PetscVPrintf() 157d781fa04SBarry Smith 158c9a19010SBarry Smith @*/ 1597087cfbeSBarry Smith PetscErrorCode PetscVSNPrintf(char *str,size_t len,const char *format,size_t *fullLength,va_list Argp) 160e5c89e4eSSatish Balay { 161d781fa04SBarry Smith char *newformat = NULL; 16214416c0eSBarry Smith char formatbuf[PETSCDEFAULTBUFFERSIZE]; 163d781fa04SBarry Smith size_t newLength; 1641a15ef5dSMatthew Knepley PetscErrorCode ierr; 16514416c0eSBarry Smith int flen; 166e5c89e4eSSatish Balay 167eed5747fSBarry Smith PetscFunctionBegin; 168d781fa04SBarry Smith ierr = PetscFormatConvertGetSize(format,&newLength);CHKERRQ(ierr); 16914416c0eSBarry Smith if (newLength < PETSCDEFAULTBUFFERSIZE) { 170e2135aedSMatthew Knepley newformat = formatbuf; 17114416c0eSBarry Smith newLength = PETSCDEFAULTBUFFERSIZE-1; 172e2135aedSMatthew Knepley } else { 173d781fa04SBarry Smith ierr = PetscMalloc1(newLength, &newformat);CHKERRQ(ierr); 174e2135aedSMatthew Knepley } 175d781fa04SBarry Smith ierr = PetscFormatConvert(format,newformat);CHKERRQ(ierr); 1767b9a2d1bSSatish Balay #if defined(PETSC_HAVE_VSNPRINTF) 177152b30f0SSatish Balay flen = vsnprintf(str,len,newformat,Argp); 178e5c89e4eSSatish Balay #else 17989b07760SSatish Balay #error "vsnprintf not found" 180e5c89e4eSSatish Balay #endif 18114416c0eSBarry Smith if (newLength > PETSCDEFAULTBUFFERSIZE-1) { 182e2135aedSMatthew Knepley ierr = PetscFree(newformat);CHKERRQ(ierr); 183e2135aedSMatthew Knepley } 1848627564fSBarry Smith { 1858627564fSBarry Smith PetscBool foundedot; 1868627564fSBarry Smith size_t cnt = 0,ncnt = 0,leng; 1878627564fSBarry Smith ierr = PetscStrlen(str,&leng);CHKERRQ(ierr); 18817ca8410SBarry Smith if (leng > 4) { 1898627564fSBarry Smith for (cnt=0; cnt<leng-4; cnt++) { 1908627564fSBarry Smith if (str[cnt] == '[' && str[cnt+1] == '|') { 191c540d043SBarry Smith flen -= 4; 1928627564fSBarry Smith cnt++; cnt++; 1938627564fSBarry Smith foundedot = PETSC_FALSE; 1948627564fSBarry Smith for (; cnt<leng-1; cnt++) { 1958627564fSBarry Smith if (str[cnt] == '|' && str[cnt+1] == ']') { 1968627564fSBarry Smith cnt++; 1978627564fSBarry Smith if (!foundedot) str[ncnt++] = '.'; 1988627564fSBarry Smith ncnt--; 1998627564fSBarry Smith break; 2008627564fSBarry Smith } else { 2018627564fSBarry Smith if (str[cnt] == 'e' || str[cnt] == '.') foundedot = PETSC_TRUE; 2028627564fSBarry Smith str[ncnt++] = str[cnt]; 2038627564fSBarry Smith } 2048627564fSBarry Smith } 2058627564fSBarry Smith } else { 2068627564fSBarry Smith str[ncnt] = str[cnt]; 2078627564fSBarry Smith } 2088627564fSBarry Smith ncnt++; 2098627564fSBarry Smith } 2108627564fSBarry Smith while (cnt < leng) { 2118627564fSBarry Smith str[ncnt] = str[cnt]; ncnt++; cnt++; 2128627564fSBarry Smith } 2138627564fSBarry Smith str[ncnt] = 0; 2148627564fSBarry Smith } 2158627564fSBarry Smith } 216748e1b9dSBarry Smith #if defined(PETSC_HAVE_WINDOWS_H) && !defined(PETSC_HAVE__SET_OUTPUT_FORMAT) 217e51f71cfSBarry Smith /* older Windows OS always produces e-+0np for floating point output; remove the extra 0 */ 218748e1b9dSBarry Smith { 219748e1b9dSBarry Smith size_t cnt = 0,ncnt = 0,leng; 220748e1b9dSBarry Smith ierr = PetscStrlen(str,&leng);CHKERRQ(ierr); 221748e1b9dSBarry Smith if (leng > 5) { 222748e1b9dSBarry Smith for (cnt=0; cnt<leng-4; cnt++) { 223e51f71cfSBarry 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') { 224748e1b9dSBarry Smith str[ncnt] = str[cnt]; ncnt++; cnt++; 225e51f71cfSBarry Smith str[ncnt] = str[cnt]; ncnt++; cnt++; cnt++; 226e51f71cfSBarry Smith str[ncnt] = str[cnt]; 227748e1b9dSBarry Smith } else { 228748e1b9dSBarry Smith str[ncnt] = str[cnt]; 229748e1b9dSBarry Smith } 230748e1b9dSBarry Smith ncnt++; 231748e1b9dSBarry Smith } 232748e1b9dSBarry Smith while (cnt < leng) { 233748e1b9dSBarry Smith str[ncnt] = str[cnt]; ncnt++; cnt++; 234748e1b9dSBarry Smith } 235748e1b9dSBarry Smith str[ncnt] = 0; 236748e1b9dSBarry Smith } 237748e1b9dSBarry Smith } 238748e1b9dSBarry Smith #endif 239c540d043SBarry Smith if (fullLength) *fullLength = 1 + (size_t) flen; 240eed5747fSBarry Smith PetscFunctionReturn(0); 241e5c89e4eSSatish Balay } 242e5c89e4eSSatish Balay 243c9a19010SBarry Smith /*@C 244c9a19010SBarry Smith PetscVFPrintf - All PETSc standard out and error messages are sent through this function; so, in theory, this can 245e5c89e4eSSatish Balay can be replaced with something that does not simply write to a file. 246e5c89e4eSSatish Balay 247c9a19010SBarry Smith To use, write your own function for example, 248c9a19010SBarry Smith $PetscErrorCode mypetscvfprintf(FILE *fd,const char format[],va_list Argp) 249c9a19010SBarry Smith ${ 250c9a19010SBarry Smith $ PetscErrorCode ierr; 251c9a19010SBarry Smith $ 252c9a19010SBarry Smith $ PetscFunctionBegin; 253c9a19010SBarry Smith $ if (fd != stdout && fd != stderr) { handle regular files 254c9a19010SBarry Smith $ ierr = PetscVFPrintfDefault(fd,format,Argp);CHKERR(ierr); 255c9a19010SBarry Smith $ } else { 256c9a19010SBarry Smith $ char buff[BIG]; 257c9a19010SBarry Smith $ size_t length; 258c9a19010SBarry Smith $ ierr = PetscVSNPrintf(buff,BIG,format,&length,Argp);CHKERRQ(ierr); 259c9a19010SBarry Smith $ now send buff to whatever stream or whatever you want 260c9a19010SBarry Smith $ } 261c9a19010SBarry Smith $ PetscFunctionReturn(0); 262c9a19010SBarry Smith $} 263c9a19010SBarry Smith then before the call to PetscInitialize() do the assignment 264c9a19010SBarry Smith $ PetscVFPrintf = mypetscvfprintf; 265c9a19010SBarry Smith 26695452b02SPatrick Sanan Notes: 26795452b02SPatrick Sanan For error messages this may be called by any process, for regular standard out it is 268e5c89e4eSSatish Balay called only by process 0 of a given communicator 269e5c89e4eSSatish Balay 27095452b02SPatrick Sanan Developer Notes: 27195452b02SPatrick Sanan this could be called by an error handler, if that happens then a recursion of the error handler may occur 272eed5747fSBarry Smith and a crash 273c9a19010SBarry Smith 274c9a19010SBarry Smith Level: developer 275c9a19010SBarry Smith 276c9a19010SBarry Smith .seealso: PetscVSNPrintf(), PetscErrorPrintf() 277c9a19010SBarry Smith 278c9a19010SBarry Smith @*/ 2797087cfbeSBarry Smith PetscErrorCode PetscVFPrintfDefault(FILE *fd,const char *format,va_list Argp) 280e5c89e4eSSatish Balay { 28114416c0eSBarry Smith char str[PETSCDEFAULTBUFFERSIZE]; 28214416c0eSBarry Smith char *buff = str; 28314416c0eSBarry Smith size_t fullLength; 284eed5747fSBarry Smith PetscErrorCode ierr; 2851531940fSBarry Smith #if defined(PETSC_HAVE_VA_COPY) 28614416c0eSBarry Smith va_list Argpcopy; 2871531940fSBarry Smith #endif 2881179db26SBarry Smith 289eed5747fSBarry Smith PetscFunctionBegin; 2901531940fSBarry Smith #if defined(PETSC_HAVE_VA_COPY) 29114416c0eSBarry Smith va_copy(Argpcopy,Argp); 2921531940fSBarry Smith #endif 29314416c0eSBarry Smith ierr = PetscVSNPrintf(str,sizeof(str),format,&fullLength,Argp);CHKERRQ(ierr); 29414416c0eSBarry Smith if (fullLength > sizeof(str)) { 29514416c0eSBarry Smith ierr = PetscMalloc1(fullLength,&buff);CHKERRQ(ierr); 2961531940fSBarry Smith #if defined(PETSC_HAVE_VA_COPY) 29714416c0eSBarry Smith ierr = PetscVSNPrintf(buff,fullLength,format,NULL,Argpcopy);CHKERRQ(ierr); 2981531940fSBarry Smith #else 2991531940fSBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB,"C89 does not support va_copy() hence cannot print long strings with PETSc printing routines"); 3001531940fSBarry Smith #endif 30114416c0eSBarry Smith } 3022f613bf5SBarry Smith fprintf(fd,"%s",buff); 3035a058713SBarry Smith fflush(fd); 30414416c0eSBarry Smith if (buff != str) { 30514416c0eSBarry Smith ierr = PetscFree(buff);CHKERRQ(ierr); 30614416c0eSBarry Smith } 307eed5747fSBarry Smith PetscFunctionReturn(0); 308e5c89e4eSSatish Balay } 309e5c89e4eSSatish Balay 3105b5bc046SBarry Smith /*@C 3115b5bc046SBarry Smith PetscSNPrintf - Prints to a string of given length 3125b5bc046SBarry Smith 3135b5bc046SBarry Smith Not Collective 3145b5bc046SBarry Smith 3155b5bc046SBarry Smith Input Parameters: 3165b5bc046SBarry Smith + str - the string to print to 3175b5bc046SBarry Smith . len - the length of str 3185b5bc046SBarry Smith . format - the usual printf() format string 31910699b91SBarry Smith - ... - any arguments that are to be printed, each much have an appropriate symbol in the format argument 3205b5bc046SBarry Smith 3215b5bc046SBarry Smith Level: intermediate 3225b5bc046SBarry Smith 3235b5bc046SBarry Smith .seealso: PetscSynchronizedFlush(), PetscSynchronizedFPrintf(), PetscFPrintf(), PetscVSNPrintf(), 324d781fa04SBarry Smith PetscPrintf(), PetscViewerASCIIPrintf(), PetscViewerASCIISynchronizedPrintf(), PetscVFPrintf() 3255b5bc046SBarry Smith @*/ 3267087cfbeSBarry Smith PetscErrorCode PetscSNPrintf(char *str,size_t len,const char format[],...) 3275b5bc046SBarry Smith { 3285b5bc046SBarry Smith PetscErrorCode ierr; 329c9a19010SBarry Smith size_t fullLength; 3305b5bc046SBarry Smith va_list Argp; 3315b5bc046SBarry Smith 3325b5bc046SBarry Smith PetscFunctionBegin; 3335b5bc046SBarry Smith va_start(Argp,format); 3342d609e63SMatthew Knepley ierr = PetscVSNPrintf(str,len,format,&fullLength,Argp);CHKERRQ(ierr); 3355b5bc046SBarry Smith PetscFunctionReturn(0); 3365b5bc046SBarry Smith } 3375b5bc046SBarry Smith 338257d2499SJed Brown /*@C 339257d2499SJed Brown PetscSNPrintfCount - Prints to a string of given length, returns count 340257d2499SJed Brown 341257d2499SJed Brown Not Collective 342257d2499SJed Brown 343257d2499SJed Brown Input Parameters: 344257d2499SJed Brown + str - the string to print to 345257d2499SJed Brown . len - the length of str 346257d2499SJed Brown . format - the usual printf() format string 34710699b91SBarry Smith - ... - any arguments that are to be printed, each much have an appropriate symbol in the format argument 348257d2499SJed Brown 349cb398dd3SBarry Smith Output Parameter: 350cb398dd3SBarry Smith . countused - number of characters used 351cb398dd3SBarry Smith 352257d2499SJed Brown Level: intermediate 353257d2499SJed Brown 354257d2499SJed Brown .seealso: PetscSynchronizedFlush(), PetscSynchronizedFPrintf(), PetscFPrintf(), PetscVSNPrintf(), 355d781fa04SBarry Smith PetscPrintf(), PetscViewerASCIIPrintf(), PetscViewerASCIISynchronizedPrintf(), PetscSNPrintf(), PetscVFPrintf() 356257d2499SJed Brown @*/ 357257d2499SJed Brown PetscErrorCode PetscSNPrintfCount(char *str,size_t len,const char format[],size_t *countused,...) 358257d2499SJed Brown { 359257d2499SJed Brown PetscErrorCode ierr; 360257d2499SJed Brown va_list Argp; 361257d2499SJed Brown 362257d2499SJed Brown PetscFunctionBegin; 363257d2499SJed Brown va_start(Argp,countused); 364257d2499SJed Brown ierr = PetscVSNPrintf(str,len,format,countused,Argp);CHKERRQ(ierr); 365257d2499SJed Brown PetscFunctionReturn(0); 366257d2499SJed Brown } 367257d2499SJed Brown 368e5c89e4eSSatish Balay /* ----------------------------------------------------------------------- */ 369e5c89e4eSSatish Balay 37002c9f0b5SLisandro Dalcin PrintfQueue petsc_printfqueue = NULL,petsc_printfqueuebase = NULL; 371d30b0576SJed Brown int petsc_printfqueuelength = 0; 372e5c89e4eSSatish Balay 373e5c89e4eSSatish Balay /*@C 374e5c89e4eSSatish Balay PetscSynchronizedPrintf - Prints synchronized output from several processors. 375e5c89e4eSSatish Balay Output of the first processor is followed by that of the second, etc. 376e5c89e4eSSatish Balay 377e5c89e4eSSatish Balay Not Collective 378e5c89e4eSSatish Balay 379e5c89e4eSSatish Balay Input Parameters: 380e5c89e4eSSatish Balay + comm - the communicator 381e5c89e4eSSatish Balay - format - the usual printf() format string 382e5c89e4eSSatish Balay 383e5c89e4eSSatish Balay Level: intermediate 384e5c89e4eSSatish Balay 385e5c89e4eSSatish Balay Notes: 3867889ec69SBarry Smith REQUIRES a call to PetscSynchronizedFlush() by all the processes after the completion of the calls to PetscSynchronizedPrintf() for the information 387e5c89e4eSSatish Balay from all the processors to be printed. 388e5c89e4eSSatish Balay 389e5c89e4eSSatish Balay Fortran Note: 390d60d70ccSBarry Smith The call sequence is PetscSynchronizedPrintf(MPI_Comm, character(*), PetscErrorCode ierr) from Fortran. 391e5c89e4eSSatish Balay That is, you can only pass a single character string from Fortran. 392e5c89e4eSSatish Balay 393e5c89e4eSSatish Balay .seealso: PetscSynchronizedFlush(), PetscSynchronizedFPrintf(), PetscFPrintf(), 394e5c89e4eSSatish Balay PetscPrintf(), PetscViewerASCIIPrintf(), PetscViewerASCIISynchronizedPrintf() 395e5c89e4eSSatish Balay @*/ 3967087cfbeSBarry Smith PetscErrorCode PetscSynchronizedPrintf(MPI_Comm comm,const char format[],...) 397e5c89e4eSSatish Balay { 398e5c89e4eSSatish Balay PetscErrorCode ierr; 399e5c89e4eSSatish Balay PetscMPIInt rank; 400e5c89e4eSSatish Balay 401e5c89e4eSSatish Balay PetscFunctionBegin; 4026180deefSBarry Smith if (comm == MPI_COMM_NULL) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Called with MPI_COMM_NULL, likely PetscObjectComm() failed"); 403ffc4695bSBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr); 404e5c89e4eSSatish Balay 405e5c89e4eSSatish Balay /* First processor prints immediately to stdout */ 406dd400576SPatrick Sanan if (rank == 0) { 407e5c89e4eSSatish Balay va_list Argp; 408e5c89e4eSSatish Balay va_start(Argp,format); 4091179db26SBarry Smith ierr = (*PetscVFPrintf)(PETSC_STDOUT,format,Argp);CHKERRQ(ierr); 410e5c89e4eSSatish Balay if (petsc_history) { 411cdc7d174SSatish Balay va_start(Argp,format); 4121179db26SBarry Smith ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr); 413e5c89e4eSSatish Balay } 414e5c89e4eSSatish Balay va_end(Argp); 415e5c89e4eSSatish Balay } else { /* other processors add to local queue */ 416e5c89e4eSSatish Balay va_list Argp; 417e5c89e4eSSatish Balay PrintfQueue next; 41814416c0eSBarry Smith size_t fullLength = PETSCDEFAULTBUFFERSIZE; 419e5c89e4eSSatish Balay 420b00a9115SJed Brown ierr = PetscNew(&next);CHKERRQ(ierr); 421a297a907SKarl Rupp if (petsc_printfqueue) { 422a297a907SKarl Rupp petsc_printfqueue->next = next; 423a297a907SKarl Rupp petsc_printfqueue = next; 42402c9f0b5SLisandro Dalcin petsc_printfqueue->next = NULL; 425a297a907SKarl Rupp } else petsc_printfqueuebase = petsc_printfqueue = next; 426d30b0576SJed Brown petsc_printfqueuelength++; 4272d609e63SMatthew Knepley next->size = -1; 42814416c0eSBarry Smith next->string = NULL; 4299ad23270SJed Brown while ((PetscInt)fullLength >= next->size) { 4302d609e63SMatthew Knepley next->size = fullLength+1; 43114416c0eSBarry Smith ierr = PetscFree(next->string);CHKERRQ(ierr); 432785e854fSJed Brown ierr = PetscMalloc1(next->size, &next->string);CHKERRQ(ierr); 433e5c89e4eSSatish Balay va_start(Argp,format); 434580bdb30SBarry Smith ierr = PetscArrayzero(next->string,next->size);CHKERRQ(ierr); 4352d609e63SMatthew Knepley ierr = PetscVSNPrintf(next->string,next->size,format, &fullLength,Argp);CHKERRQ(ierr); 436e5c89e4eSSatish Balay va_end(Argp); 437e5c89e4eSSatish Balay } 4382d609e63SMatthew Knepley } 439e5c89e4eSSatish Balay PetscFunctionReturn(0); 440e5c89e4eSSatish Balay } 441e5c89e4eSSatish Balay 442e5c89e4eSSatish Balay /*@C 443e5c89e4eSSatish Balay PetscSynchronizedFPrintf - Prints synchronized output to the specified file from 444e5c89e4eSSatish Balay several processors. Output of the first processor is followed by that of the 445e5c89e4eSSatish Balay second, etc. 446e5c89e4eSSatish Balay 447e5c89e4eSSatish Balay Not Collective 448e5c89e4eSSatish Balay 449e5c89e4eSSatish Balay Input Parameters: 450e5c89e4eSSatish Balay + comm - the communicator 451e5c89e4eSSatish Balay . fd - the file pointer 452e5c89e4eSSatish Balay - format - the usual printf() format string 453e5c89e4eSSatish Balay 454e5c89e4eSSatish Balay Level: intermediate 455e5c89e4eSSatish Balay 456e5c89e4eSSatish Balay Notes: 457e5c89e4eSSatish Balay REQUIRES a intervening call to PetscSynchronizedFlush() for the information 458e5c89e4eSSatish Balay from all the processors to be printed. 459e5c89e4eSSatish Balay 460e5c89e4eSSatish Balay .seealso: PetscSynchronizedPrintf(), PetscSynchronizedFlush(), PetscFPrintf(), 461e5c89e4eSSatish Balay PetscFOpen(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIPrintf() 462e5c89e4eSSatish Balay 463e5c89e4eSSatish Balay @*/ 4647087cfbeSBarry Smith PetscErrorCode PetscSynchronizedFPrintf(MPI_Comm comm,FILE *fp,const char format[],...) 465e5c89e4eSSatish Balay { 466e5c89e4eSSatish Balay PetscErrorCode ierr; 467e5c89e4eSSatish Balay PetscMPIInt rank; 468e5c89e4eSSatish Balay 469e5c89e4eSSatish Balay PetscFunctionBegin; 4706180deefSBarry Smith if (comm == MPI_COMM_NULL) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Called with MPI_COMM_NULL, likely PetscObjectComm() failed"); 471ffc4695bSBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr); 472e5c89e4eSSatish Balay 473e5c89e4eSSatish Balay /* First processor prints immediately to fp */ 474dd400576SPatrick Sanan if (rank == 0) { 475e5c89e4eSSatish Balay va_list Argp; 476e5c89e4eSSatish Balay va_start(Argp,format); 4771179db26SBarry Smith ierr = (*PetscVFPrintf)(fp,format,Argp);CHKERRQ(ierr); 478cdc7d174SSatish Balay if (petsc_history && (fp !=petsc_history)) { 479cdc7d174SSatish Balay va_start(Argp,format); 4801179db26SBarry Smith ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr); 481e5c89e4eSSatish Balay } 482e5c89e4eSSatish Balay va_end(Argp); 483e5c89e4eSSatish Balay } else { /* other processors add to local queue */ 484e5c89e4eSSatish Balay va_list Argp; 485e5c89e4eSSatish Balay PrintfQueue next; 48614416c0eSBarry Smith size_t fullLength = PETSCDEFAULTBUFFERSIZE; 48714416c0eSBarry Smith 488b00a9115SJed Brown ierr = PetscNew(&next);CHKERRQ(ierr); 489a297a907SKarl Rupp if (petsc_printfqueue) { 490a297a907SKarl Rupp petsc_printfqueue->next = next; 491a297a907SKarl Rupp petsc_printfqueue = next; 49202c9f0b5SLisandro Dalcin petsc_printfqueue->next = NULL; 493a297a907SKarl Rupp } else petsc_printfqueuebase = petsc_printfqueue = next; 494d30b0576SJed Brown petsc_printfqueuelength++; 4952d609e63SMatthew Knepley next->size = -1; 49614416c0eSBarry Smith next->string = NULL; 4979ad23270SJed Brown while ((PetscInt)fullLength >= next->size) { 4982d609e63SMatthew Knepley next->size = fullLength+1; 49914416c0eSBarry Smith ierr = PetscFree(next->string);CHKERRQ(ierr); 500785e854fSJed Brown ierr = PetscMalloc1(next->size, &next->string);CHKERRQ(ierr); 501e5c89e4eSSatish Balay va_start(Argp,format); 502580bdb30SBarry Smith ierr = PetscArrayzero(next->string,next->size);CHKERRQ(ierr); 5032d609e63SMatthew Knepley ierr = PetscVSNPrintf(next->string,next->size,format,&fullLength,Argp);CHKERRQ(ierr); 504e5c89e4eSSatish Balay va_end(Argp); 505e5c89e4eSSatish Balay } 5062d609e63SMatthew Knepley } 507e5c89e4eSSatish Balay PetscFunctionReturn(0); 508e5c89e4eSSatish Balay } 509e5c89e4eSSatish Balay 5100ec8b6e3SBarry Smith /*@C 511e5c89e4eSSatish Balay PetscSynchronizedFlush - Flushes to the screen output from all processors 5127889ec69SBarry Smith involved in previous PetscSynchronizedPrintf()/PetscSynchronizedFPrintf() calls. 513e5c89e4eSSatish Balay 514d083f849SBarry Smith Collective 515e5c89e4eSSatish Balay 516e5c89e4eSSatish Balay Input Parameters: 5170ec8b6e3SBarry Smith + comm - the communicator 5180ec8b6e3SBarry Smith - fd - the file pointer (valid on process 0 of the communicator) 519e5c89e4eSSatish Balay 520e5c89e4eSSatish Balay Level: intermediate 521e5c89e4eSSatish Balay 522e5c89e4eSSatish Balay Notes: 5237889ec69SBarry Smith If PetscSynchronizedPrintf() and/or PetscSynchronizedFPrintf() are called with 5247889ec69SBarry Smith different MPI communicators there must be an intervening call to PetscSynchronizedFlush() between the calls with different MPI communicators. 525e5c89e4eSSatish Balay 526e50bf69fSBarry Smith From Fortran pass PETSC_STDOUT if the flush is for standard out; otherwise pass a value obtained from PetscFOpen() 527e50bf69fSBarry Smith 528e5c89e4eSSatish Balay .seealso: PetscSynchronizedPrintf(), PetscFPrintf(), PetscPrintf(), PetscViewerASCIIPrintf(), 529e5c89e4eSSatish Balay PetscViewerASCIISynchronizedPrintf() 5300087d953SMatthew G. Knepley @*/ 5310ec8b6e3SBarry Smith PetscErrorCode PetscSynchronizedFlush(MPI_Comm comm,FILE *fd) 532e5c89e4eSSatish Balay { 533e5c89e4eSSatish Balay PetscErrorCode ierr; 53429a5cbdcSMatthew G. Knepley PetscMPIInt rank,size,tag,i,j,n = 0,dummy = 0; 5352d609e63SMatthew Knepley char *message; 536e5c89e4eSSatish Balay MPI_Status status; 537e5c89e4eSSatish Balay 538e5c89e4eSSatish Balay PetscFunctionBegin; 539e5c89e4eSSatish Balay ierr = PetscCommDuplicate(comm,&comm,&tag);CHKERRQ(ierr); 540ffc4695bSBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr); 541ffc4695bSBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRMPI(ierr); 542e5c89e4eSSatish Balay 543e5c89e4eSSatish Balay /* First processor waits for messages from all other processors */ 544dd400576SPatrick Sanan if (rank == 0) { 5450ec8b6e3SBarry Smith if (!fd) fd = PETSC_STDOUT; 546e5c89e4eSSatish Balay for (i=1; i<size; i++) { 5479f73f8ecSBarry Smith /* to prevent a flood of messages to process zero, request each message separately */ 548ffc4695bSBarry Smith ierr = MPI_Send(&dummy,1,MPI_INT,i,tag,comm);CHKERRMPI(ierr); 549ffc4695bSBarry Smith ierr = MPI_Recv(&n,1,MPI_INT,i,tag,comm,&status);CHKERRMPI(ierr); 550e5c89e4eSSatish Balay for (j=0; j<n; j++) { 55129a5cbdcSMatthew G. Knepley PetscMPIInt size = 0; 5522d609e63SMatthew Knepley 553ffc4695bSBarry Smith ierr = MPI_Recv(&size,1,MPI_INT,i,tag,comm,&status);CHKERRMPI(ierr); 554785e854fSJed Brown ierr = PetscMalloc1(size, &message);CHKERRQ(ierr); 555ffc4695bSBarry Smith ierr = MPI_Recv(message,size,MPI_CHAR,i,tag,comm,&status);CHKERRMPI(ierr); 5563bf036e2SBarry Smith ierr = PetscFPrintf(comm,fd,"%s",message);CHKERRQ(ierr); 5572d609e63SMatthew Knepley ierr = PetscFree(message);CHKERRQ(ierr); 558e5c89e4eSSatish Balay } 559e5c89e4eSSatish Balay } 560e5c89e4eSSatish Balay } else { /* other processors send queue to processor 0 */ 561d30b0576SJed Brown PrintfQueue next = petsc_printfqueuebase,previous; 562e5c89e4eSSatish Balay 563ffc4695bSBarry Smith ierr = MPI_Recv(&dummy,1,MPI_INT,0,tag,comm,&status);CHKERRMPI(ierr); 564ffc4695bSBarry Smith ierr = MPI_Send(&petsc_printfqueuelength,1,MPI_INT,0,tag,comm);CHKERRMPI(ierr); 565d30b0576SJed Brown for (i=0; i<petsc_printfqueuelength; i++) { 56655b25c41SPierre Jolivet ierr = MPI_Send(&next->size,1,MPI_INT,0,tag,comm);CHKERRMPI(ierr); 56755b25c41SPierre Jolivet ierr = MPI_Send(next->string,next->size,MPI_CHAR,0,tag,comm);CHKERRMPI(ierr); 568e5c89e4eSSatish Balay previous = next; 569e5c89e4eSSatish Balay next = next->next; 5702d609e63SMatthew Knepley ierr = PetscFree(previous->string);CHKERRQ(ierr); 571e5c89e4eSSatish Balay ierr = PetscFree(previous);CHKERRQ(ierr); 572e5c89e4eSSatish Balay } 57302c9f0b5SLisandro Dalcin petsc_printfqueue = NULL; 574d30b0576SJed Brown petsc_printfqueuelength = 0; 575e5c89e4eSSatish Balay } 576e5c89e4eSSatish Balay ierr = PetscCommDestroy(&comm);CHKERRQ(ierr); 577e5c89e4eSSatish Balay PetscFunctionReturn(0); 578e5c89e4eSSatish Balay } 579e5c89e4eSSatish Balay 580e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------------------*/ 581e5c89e4eSSatish Balay 582e5c89e4eSSatish Balay /*@C 583e5c89e4eSSatish Balay PetscFPrintf - Prints to a file, only from the first 584e5c89e4eSSatish Balay processor in the communicator. 585e5c89e4eSSatish Balay 586e5c89e4eSSatish Balay Not Collective 587e5c89e4eSSatish Balay 588e5c89e4eSSatish Balay Input Parameters: 589e5c89e4eSSatish Balay + comm - the communicator 590e5c89e4eSSatish Balay . fd - the file pointer 591e5c89e4eSSatish Balay - format - the usual printf() format string 592e5c89e4eSSatish Balay 593e5c89e4eSSatish Balay Level: intermediate 594e5c89e4eSSatish Balay 595e5c89e4eSSatish Balay Fortran Note: 596e5c89e4eSSatish Balay This routine is not supported in Fortran. 597e5c89e4eSSatish Balay 598e5c89e4eSSatish Balay .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(), 599e5c89e4eSSatish Balay PetscViewerASCIISynchronizedPrintf(), PetscSynchronizedFlush() 600e5c89e4eSSatish Balay @*/ 6017087cfbeSBarry Smith PetscErrorCode PetscFPrintf(MPI_Comm comm,FILE* fd,const char format[],...) 602e5c89e4eSSatish Balay { 603e5c89e4eSSatish Balay PetscErrorCode ierr; 604e5c89e4eSSatish Balay PetscMPIInt rank; 605e5c89e4eSSatish Balay 606e5c89e4eSSatish Balay PetscFunctionBegin; 6076180deefSBarry Smith if (comm == MPI_COMM_NULL) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Called with MPI_COMM_NULL, likely PetscObjectComm() failed"); 608ffc4695bSBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr); 609dd400576SPatrick Sanan if (rank == 0) { 610e5c89e4eSSatish Balay va_list Argp; 611e5c89e4eSSatish Balay va_start(Argp,format); 6121179db26SBarry Smith ierr = (*PetscVFPrintf)(fd,format,Argp);CHKERRQ(ierr); 613cdc7d174SSatish Balay if (petsc_history && (fd !=petsc_history)) { 614cdc7d174SSatish Balay va_start(Argp,format); 6151179db26SBarry Smith ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr); 616e5c89e4eSSatish Balay } 617e5c89e4eSSatish Balay va_end(Argp); 618e5c89e4eSSatish Balay } 619e5c89e4eSSatish Balay PetscFunctionReturn(0); 620e5c89e4eSSatish Balay } 621e5c89e4eSSatish Balay 622e5c89e4eSSatish Balay /*@C 623e5c89e4eSSatish Balay PetscPrintf - Prints to standard out, only from the first 624eed5747fSBarry Smith processor in the communicator. Calls from other processes are ignored. 625e5c89e4eSSatish Balay 626e5c89e4eSSatish Balay Not Collective 627e5c89e4eSSatish Balay 628e5c89e4eSSatish Balay Input Parameters: 629e5c89e4eSSatish Balay + comm - the communicator 630e5c89e4eSSatish Balay - format - the usual printf() format string 631e5c89e4eSSatish Balay 632e5c89e4eSSatish Balay Level: intermediate 633e5c89e4eSSatish Balay 634b2706f25SRichard Tran Mills Notes: 635b2706f25SRichard Tran Mills PetscPrintf() supports some format specifiers that are unique to PETSc. 636b2706f25SRichard Tran Mills See the manual page for PetscFormatConvert() for details. 637b2706f25SRichard Tran Mills 638e5c89e4eSSatish Balay Fortran Note: 639d60d70ccSBarry Smith The call sequence is PetscPrintf(MPI_Comm, character(*), PetscErrorCode ierr) from Fortran. 640e5c89e4eSSatish Balay That is, you can only pass a single character string from Fortran. 641e5c89e4eSSatish Balay 642b2706f25SRichard Tran Mills .seealso: PetscFPrintf(), PetscSynchronizedPrintf(), PetscFormatConvert() 643e5c89e4eSSatish Balay @*/ 6447087cfbeSBarry Smith PetscErrorCode PetscPrintf(MPI_Comm comm,const char format[],...) 645e5c89e4eSSatish Balay { 646e5c89e4eSSatish Balay PetscErrorCode ierr; 647e5c89e4eSSatish Balay PetscMPIInt rank; 648e5c89e4eSSatish Balay 649e5c89e4eSSatish Balay PetscFunctionBegin; 6506180deefSBarry Smith if (comm == MPI_COMM_NULL) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Called with MPI_COMM_NULL, likely PetscObjectComm() failed"); 651ffc4695bSBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr); 652dd400576SPatrick Sanan if (rank == 0) { 653e5c89e4eSSatish Balay va_list Argp; 654e5c89e4eSSatish Balay va_start(Argp,format); 655eed5747fSBarry Smith ierr = (*PetscVFPrintf)(PETSC_STDOUT,format,Argp);CHKERRQ(ierr); 656e5c89e4eSSatish Balay if (petsc_history) { 657cdc7d174SSatish Balay va_start(Argp,format); 658eed5747fSBarry Smith ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr); 659e5c89e4eSSatish Balay } 660e5c89e4eSSatish Balay va_end(Argp); 661e5c89e4eSSatish Balay } 662e5c89e4eSSatish Balay PetscFunctionReturn(0); 663e5c89e4eSSatish Balay } 664e5c89e4eSSatish Balay 6657087cfbeSBarry Smith PetscErrorCode PetscHelpPrintfDefault(MPI_Comm comm,const char format[],...) 666e5c89e4eSSatish Balay { 667e5c89e4eSSatish Balay PetscErrorCode ierr; 668e5c89e4eSSatish Balay PetscMPIInt rank; 669e5c89e4eSSatish Balay 670e5c89e4eSSatish Balay PetscFunctionBegin; 6716180deefSBarry Smith if (comm == MPI_COMM_NULL) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Called with MPI_COMM_NULL, likely PetscObjectComm() failed"); 672ffc4695bSBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr); 673dd400576SPatrick Sanan if (rank == 0) { 674e5c89e4eSSatish Balay va_list Argp; 675e5c89e4eSSatish Balay va_start(Argp,format); 6761179db26SBarry Smith ierr = (*PetscVFPrintf)(PETSC_STDOUT,format,Argp);CHKERRQ(ierr); 677e5c89e4eSSatish Balay if (petsc_history) { 678cdc7d174SSatish Balay va_start(Argp,format); 6791179db26SBarry Smith ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr); 680e5c89e4eSSatish Balay } 681e5c89e4eSSatish Balay va_end(Argp); 682e5c89e4eSSatish Balay } 683e5c89e4eSSatish Balay PetscFunctionReturn(0); 684e5c89e4eSSatish Balay } 685e5c89e4eSSatish Balay 686e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------------------*/ 687e5c89e4eSSatish Balay 688e5c89e4eSSatish Balay /*@C 689e5c89e4eSSatish Balay PetscSynchronizedFGets - Several processors all get the same line from a file. 690e5c89e4eSSatish Balay 691d083f849SBarry Smith Collective 692e5c89e4eSSatish Balay 693e5c89e4eSSatish Balay Input Parameters: 694e5c89e4eSSatish Balay + comm - the communicator 695e5c89e4eSSatish Balay . fd - the file pointer 696e5c89e4eSSatish Balay - len - the length of the output buffer 697e5c89e4eSSatish Balay 698e5c89e4eSSatish Balay Output Parameter: 699e31d4fa4SJed Brown . string - the line read from the file, at end of file string[0] == 0 700e5c89e4eSSatish Balay 701e5c89e4eSSatish Balay Level: intermediate 702e5c89e4eSSatish Balay 703e5c89e4eSSatish Balay .seealso: PetscSynchronizedPrintf(), PetscSynchronizedFlush(), 704e5c89e4eSSatish Balay PetscFOpen(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIPrintf() 705e5c89e4eSSatish Balay 706e5c89e4eSSatish Balay @*/ 7077087cfbeSBarry Smith PetscErrorCode PetscSynchronizedFGets(MPI_Comm comm,FILE *fp,size_t len,char string[]) 708e5c89e4eSSatish Balay { 709e5c89e4eSSatish Balay PetscErrorCode ierr; 710e5c89e4eSSatish Balay PetscMPIInt rank; 711e5c89e4eSSatish Balay 712e5c89e4eSSatish Balay PetscFunctionBegin; 713ffc4695bSBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRMPI(ierr); 714e5c89e4eSSatish Balay 715dd400576SPatrick Sanan if (rank == 0) { 716047b9c12SMatthew G Knepley char *ptr = fgets(string, len, fp); 717047b9c12SMatthew G Knepley 718047b9c12SMatthew G Knepley if (!ptr) { 719e31d4fa4SJed Brown string[0] = 0; 720e31d4fa4SJed Brown if (!feof(fp)) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_FILE_READ, "Error reading from file: %d", errno); 721047b9c12SMatthew G Knepley } 722e5c89e4eSSatish Balay } 723ffc4695bSBarry Smith ierr = MPI_Bcast(string,len,MPI_BYTE,0,comm);CHKERRMPI(ierr); 724e5c89e4eSSatish Balay PetscFunctionReturn(0); 725e5c89e4eSSatish Balay } 726238ccf28SShri Abhyankar 7272475b7caSBarry Smith #if defined(PETSC_HAVE_CLOSURE) 728f1d7fe2eSBarry Smith int (^SwiftClosure)(const char*) = 0; 729f1d7fe2eSBarry Smith 730f1d7fe2eSBarry Smith PetscErrorCode PetscVFPrintfToString(FILE *fd,const char format[],va_list Argp) 731f1d7fe2eSBarry Smith { 732f1d7fe2eSBarry Smith PetscErrorCode ierr; 733f1d7fe2eSBarry Smith 734f1d7fe2eSBarry Smith PetscFunctionBegin; 735f1d7fe2eSBarry Smith if (fd != stdout && fd != stderr) { /* handle regular files */ 736f1d7fe2eSBarry Smith ierr = PetscVFPrintfDefault(fd,format,Argp);CHKERRQ(ierr); 737f1d7fe2eSBarry Smith } else { 73814416c0eSBarry Smith size_t length; 73914416c0eSBarry Smith char buff[PETSCDEFAULTBUFFERSIZE]; 740f1d7fe2eSBarry Smith 7412475b7caSBarry Smith ierr = PetscVSNPrintf(buff,sizeof(buff),format,&length,Argp);CHKERRQ(ierr); 74214416c0eSBarry Smith ierr = SwiftClosure(buff);CHKERRQ(ierr); 743f1d7fe2eSBarry Smith } 744f1d7fe2eSBarry Smith PetscFunctionReturn(0); 745f1d7fe2eSBarry Smith } 746f1d7fe2eSBarry Smith 747f1d7fe2eSBarry Smith /* 748f1d7fe2eSBarry Smith Provide a Swift function that processes all the PETSc calls to PetscVFPrintf() 749f1d7fe2eSBarry Smith */ 750f1d7fe2eSBarry Smith PetscErrorCode PetscVFPrintfSetClosure(int (^closure)(const char*)) 751f1d7fe2eSBarry Smith { 752f1d7fe2eSBarry Smith PetscVFPrintf = PetscVFPrintfToString; 753f1d7fe2eSBarry Smith SwiftClosure = closure; 754f1d7fe2eSBarry Smith return 0; 755f1d7fe2eSBarry Smith } 756f1d7fe2eSBarry Smith #endif 757f1d7fe2eSBarry Smith 7588c74ee41SBarry Smith /*@C 7598c74ee41SBarry Smith PetscFormatStrip - Takes a PETSc format string and removes all numerical modifiers to % operations 7608c74ee41SBarry Smith 7618c74ee41SBarry Smith Input Parameters: 7628c74ee41SBarry Smith . format - the PETSc format string 7638c74ee41SBarry Smith 7648c74ee41SBarry Smith Level: developer 7658c74ee41SBarry Smith 7668c74ee41SBarry Smith @*/ 7678c74ee41SBarry Smith PetscErrorCode PetscFormatStrip(char *format) 7688c74ee41SBarry Smith { 7698c74ee41SBarry Smith size_t loc1 = 0, loc2 = 0; 7708c74ee41SBarry Smith 7718c74ee41SBarry Smith PetscFunctionBegin; 7728c74ee41SBarry Smith while (format[loc2]) { 7738c74ee41SBarry Smith if (format[loc2] == '%') { 7748c74ee41SBarry Smith format[loc1++] = format[loc2++]; 7758c74ee41SBarry Smith while (format[loc2] && ((format[loc2] >= '0' && format[loc2] <= '9') || format[loc2] == '.')) loc2++; 7768c74ee41SBarry Smith } 7778c74ee41SBarry Smith format[loc1++] = format[loc2++]; 7788c74ee41SBarry Smith } 7798c74ee41SBarry Smith PetscFunctionReturn(0); 7808c74ee41SBarry Smith } 7818c74ee41SBarry Smith 7821b5687a1SBarry Smith PetscErrorCode PetscFormatRealArray(char buf[],size_t len,const char *fmt,PetscInt n,const PetscReal x[]) 7831b5687a1SBarry Smith { 7841b5687a1SBarry Smith PetscErrorCode ierr; 7851b5687a1SBarry Smith PetscInt i; 7861b5687a1SBarry Smith size_t left,count; 7871b5687a1SBarry Smith char *p; 7881b5687a1SBarry Smith 7891b5687a1SBarry Smith PetscFunctionBegin; 7901b5687a1SBarry Smith for (i=0,p=buf,left=len; i<n; i++) { 7911b5687a1SBarry Smith ierr = PetscSNPrintfCount(p,left,fmt,&count,(double)x[i]);CHKERRQ(ierr); 7921b5687a1SBarry Smith if (count >= left) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Insufficient space in buffer"); 7931b5687a1SBarry Smith left -= count; 7941b5687a1SBarry Smith p += count-1; 7951b5687a1SBarry Smith *p++ = ' '; 7961b5687a1SBarry Smith } 7971b5687a1SBarry Smith p[i ? 0 : -1] = 0; 7981b5687a1SBarry Smith PetscFunctionReturn(0); 7991b5687a1SBarry Smith } 800