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 24354e5084SJose Roman /* 25354e5084SJose Roman Return the maximum expected new size of the format 26354e5084SJose Roman */ 27354e5084SJose Roman #define PETSC_MAX_LENGTH_FORMAT(l) (l+l/8) 28354e5084SJose Roman 29e5c89e4eSSatish Balay #undef __FUNCT__ 30e5c89e4eSSatish Balay #define __FUNCT__ "PetscFormatConvert" 31c9a19010SBarry Smith /*@C 32c9a19010SBarry Smith PetscFormatConvert - Takes a PETSc format string and converts it to a reqular C format string 33c9a19010SBarry Smith 34c9a19010SBarry Smith Input Parameters: 35c9a19010SBarry Smith + format - the PETSc format string 36c9a19010SBarry Smith . newformat - the location to put the standard C format string values 37c9a19010SBarry Smith - size - the length of newformat 38c9a19010SBarry Smith 39eed5747fSBarry Smith Note: this exists so we can have the same code when PetscInt is either int or long long and PetscScalar is either __float128, double, or float 40c9a19010SBarry Smith 41c9a19010SBarry Smith Level: developer 42c9a19010SBarry Smith 43c9a19010SBarry Smith @*/ 447087cfbeSBarry Smith PetscErrorCode PetscFormatConvert(const char *format,char *newformat,size_t size) 45e5c89e4eSSatish Balay { 46e5c89e4eSSatish Balay PetscInt i = 0,j = 0; 47e5c89e4eSSatish Balay 48eed5747fSBarry Smith PetscFunctionBegin; 497bc47156SJose Roman while (format[i] && j < (PetscInt)size-1) { 507bc47156SJose Roman if (format[i] == '%' && format[i+1] != '%') { 517bc47156SJose Roman /* Find the letter */ 527bc47156SJose Roman for (; format[i] && format[i] <= '9'; i++) newformat[j++] = format[i]; 537bc47156SJose Roman switch (format[i]) { 547bc47156SJose Roman case 'D': 556de02169SBarry Smith #if !defined(PETSC_USE_64BIT_INDICES) 56e5c89e4eSSatish Balay newformat[j++] = 'd'; 57e5c89e4eSSatish Balay #else 58e5c89e4eSSatish Balay newformat[j++] = 'l'; 59e5c89e4eSSatish Balay newformat[j++] = 'l'; 60e5c89e4eSSatish Balay newformat[j++] = 'd'; 61e5c89e4eSSatish Balay #endif 627bc47156SJose Roman break; 637bc47156SJose Roman case 'G': 64ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_DOUBLE) || defined(PETSC_USE_REAL_SINGLE) 65a83599f4SBarry Smith newformat[j++] = 'g'; 66ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL___FLOAT128) 67d9822059SBarry Smith newformat[j++] = 'Q'; 6857ddf7daSJose Roman newformat[j++] = 'g'; 69a83599f4SBarry Smith #endif 707bc47156SJose Roman break; 71121a09c4SJose Roman case 'F': 72121a09c4SJose Roman #if defined(PETSC_USE_REAL_DOUBLE) || defined(PETSC_USE_REAL_SINGLE) 73121a09c4SJose Roman newformat[j++] = 'f'; 74121a09c4SJose Roman #elif defined(PETSC_USE_REAL_LONG_DOUBLE) 75121a09c4SJose Roman newformat[j++] = 'L'; 76121a09c4SJose Roman newformat[j++] = 'f'; 77121a09c4SJose Roman #elif defined(PETSC_USE_REAL___FLOAT128) 78121a09c4SJose Roman newformat[j++] = 'Q'; 79121a09c4SJose Roman newformat[j++] = 'f'; 80121a09c4SJose Roman #endif 81121a09c4SJose Roman break; 827bc47156SJose Roman default: 837bc47156SJose Roman newformat[j++] = format[i]; 847bc47156SJose Roman break; 857bc47156SJose Roman } 867bc47156SJose Roman i++; 87a297a907SKarl Rupp } else newformat[j++] = format[i++]; 88e5c89e4eSSatish Balay } 89e5c89e4eSSatish Balay newformat[j] = 0; 90eed5747fSBarry Smith PetscFunctionReturn(0); 91e5c89e4eSSatish Balay } 92e5c89e4eSSatish Balay 93e5c89e4eSSatish Balay #undef __FUNCT__ 94e5c89e4eSSatish Balay #define __FUNCT__ "PetscVSNPrintf" 95c9a19010SBarry Smith /*@C 96c9a19010SBarry Smith PetscVSNPrintf - The PETSc version of vsnprintf(). Converts a PETSc format string into a standard C format string and then puts all the 97c9a19010SBarry Smith function arguments into a string using the format statement. 98c9a19010SBarry Smith 99c9a19010SBarry Smith Input Parameters: 100c9a19010SBarry Smith + str - location to put result 101c9a19010SBarry Smith . len - the amount of space in str 102c9a19010SBarry Smith + format - the PETSc format string 103c9a19010SBarry Smith - fullLength - the amount of space in str actually used. 104c9a19010SBarry Smith 105eed5747fSBarry 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 106eed5747fSBarry Smith a recursion will occur and possible crash. 107c9a19010SBarry Smith 108c9a19010SBarry Smith Level: developer 109c9a19010SBarry Smith 110c9a19010SBarry Smith @*/ 1117087cfbeSBarry Smith PetscErrorCode PetscVSNPrintf(char *str,size_t len,const char *format,size_t *fullLength,va_list Argp) 112e5c89e4eSSatish Balay { 113e2135aedSMatthew Knepley char *newformat; 114e2135aedSMatthew Knepley char formatbuf[8*1024]; 1151a96acb8SJed Brown size_t oldLength,length; 1161a96acb8SJed Brown int fullLengthInt; 1171a15ef5dSMatthew Knepley PetscErrorCode ierr; 118e5c89e4eSSatish Balay 119eed5747fSBarry Smith PetscFunctionBegin; 120e2135aedSMatthew Knepley ierr = PetscStrlen(format, &oldLength);CHKERRQ(ierr); 121e2135aedSMatthew Knepley if (oldLength < 8*1024) { 122e2135aedSMatthew Knepley newformat = formatbuf; 123354e5084SJose Roman oldLength = 8*1024-1; 124e2135aedSMatthew Knepley } else { 125354e5084SJose Roman oldLength = PETSC_MAX_LENGTH_FORMAT(oldLength); 126354e5084SJose Roman ierr = PetscMalloc(oldLength * sizeof(char), &newformat);CHKERRQ(ierr); 127e2135aedSMatthew Knepley } 128354e5084SJose Roman PetscFormatConvert(format,newformat,oldLength); 1291a15ef5dSMatthew Knepley ierr = PetscStrlen(newformat, &length);CHKERRQ(ierr); 1302d609e63SMatthew Knepley #if 0 131a297a907SKarl Rupp if (length > len) newformat[len] = '\0'; 1322d609e63SMatthew Knepley #endif 1335a058713SBarry Smith #if defined(PETSC_HAVE_VSNPRINTF_CHAR) 13442c7c0bfSJed Brown fullLengthInt = vsnprintf(str,len,newformat,(char*)Argp); 13589b07760SSatish Balay #elif defined(PETSC_HAVE_VSNPRINTF) 13642c7c0bfSJed Brown fullLengthInt = vsnprintf(str,len,newformat,Argp); 137141c0d65SSatish Balay #elif defined(PETSC_HAVE__VSNPRINTF) 13842c7c0bfSJed Brown fullLengthInt = _vsnprintf(str,len,newformat,Argp); 139e5c89e4eSSatish Balay #else 14089b07760SSatish Balay #error "vsnprintf not found" 141e5c89e4eSSatish Balay #endif 14242c7c0bfSJed Brown if (fullLengthInt < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"vsnprintf() failed"); 1438c74ee41SBarry Smith if (fullLength) *fullLength = (size_t)fullLengthInt; 144e2135aedSMatthew Knepley if (oldLength >= 8*1024) { 145e2135aedSMatthew Knepley ierr = PetscFree(newformat);CHKERRQ(ierr); 146e2135aedSMatthew Knepley } 147eed5747fSBarry Smith PetscFunctionReturn(0); 148e5c89e4eSSatish Balay } 149e5c89e4eSSatish Balay 150e5c89e4eSSatish Balay #undef __FUNCT__ 151c9a19010SBarry Smith #define __FUNCT__ "PetscVFPrintfDefault" 152c9a19010SBarry Smith /*@C 153c9a19010SBarry Smith PetscVFPrintf - All PETSc standard out and error messages are sent through this function; so, in theory, this can 154e5c89e4eSSatish Balay can be replaced with something that does not simply write to a file. 155e5c89e4eSSatish Balay 156c9a19010SBarry Smith To use, write your own function for example, 157c9a19010SBarry Smith $PetscErrorCode mypetscvfprintf(FILE *fd,const char format[],va_list Argp) 158c9a19010SBarry Smith ${ 159c9a19010SBarry Smith $ PetscErrorCode ierr; 160c9a19010SBarry Smith $ 161c9a19010SBarry Smith $ PetscFunctionBegin; 162c9a19010SBarry Smith $ if (fd != stdout && fd != stderr) { handle regular files 163c9a19010SBarry Smith $ ierr = PetscVFPrintfDefault(fd,format,Argp);CHKERR(ierr); 164c9a19010SBarry Smith $ } else { 165c9a19010SBarry Smith $ char buff[BIG]; 166c9a19010SBarry Smith $ size_t length; 167c9a19010SBarry Smith $ ierr = PetscVSNPrintf(buff,BIG,format,&length,Argp);CHKERRQ(ierr); 168c9a19010SBarry Smith $ now send buff to whatever stream or whatever you want 169c9a19010SBarry Smith $ } 170c9a19010SBarry Smith $ PetscFunctionReturn(0); 171c9a19010SBarry Smith $} 172c9a19010SBarry Smith then before the call to PetscInitialize() do the assignment 173c9a19010SBarry Smith $ PetscVFPrintf = mypetscvfprintf; 174c9a19010SBarry Smith 175c9a19010SBarry Smith Notes: For error messages this may be called by any process, for regular standard out it is 176e5c89e4eSSatish Balay called only by process 0 of a given communicator 177e5c89e4eSSatish Balay 178eed5747fSBarry Smith Developer Notes: this could be called by an error handler, if that happens then a recursion of the error handler may occur 179eed5747fSBarry Smith and a crash 180c9a19010SBarry Smith 181c9a19010SBarry Smith Level: developer 182c9a19010SBarry Smith 183c9a19010SBarry Smith .seealso: PetscVSNPrintf(), PetscErrorPrintf() 184c9a19010SBarry Smith 185c9a19010SBarry Smith @*/ 1867087cfbeSBarry Smith PetscErrorCode PetscVFPrintfDefault(FILE *fd,const char *format,va_list Argp) 187e5c89e4eSSatish Balay { 188e2135aedSMatthew Knepley char *newformat; 189e2135aedSMatthew Knepley char formatbuf[8*1024]; 190e2135aedSMatthew Knepley size_t oldLength; 191eed5747fSBarry Smith PetscErrorCode ierr; 1921179db26SBarry Smith 193eed5747fSBarry Smith PetscFunctionBegin; 194eed5747fSBarry Smith ierr = PetscStrlen(format, &oldLength);CHKERRQ(ierr); 195e2135aedSMatthew Knepley if (oldLength < 8*1024) { 196e2135aedSMatthew Knepley newformat = formatbuf; 197354e5084SJose Roman oldLength = 8*1024-1; 198e2135aedSMatthew Knepley } else { 199354e5084SJose Roman oldLength = PETSC_MAX_LENGTH_FORMAT(oldLength); 200eed5747fSBarry Smith ierr = PetscMalloc(oldLength * sizeof(char), &newformat);CHKERRQ(ierr); 201e2135aedSMatthew Knepley } 202eed5747fSBarry Smith ierr = PetscFormatConvert(format,newformat,oldLength);CHKERRQ(ierr); 203d8c6e182Sbcordonn 2045a058713SBarry Smith #if defined(PETSC_HAVE_VFPRINTF_CHAR) 205e5c89e4eSSatish Balay vfprintf(fd,newformat,(char*)Argp); 206e5c89e4eSSatish Balay #else 207e5c89e4eSSatish Balay vfprintf(fd,newformat,Argp); 208e5c89e4eSSatish Balay #endif 2095a058713SBarry Smith fflush(fd); 210e2135aedSMatthew Knepley if (oldLength >= 8*1024) { 211eed5747fSBarry Smith ierr = PetscFree(newformat);CHKERRQ(ierr); 212e2135aedSMatthew Knepley } 213eed5747fSBarry Smith PetscFunctionReturn(0); 214e5c89e4eSSatish Balay } 215e5c89e4eSSatish Balay 2165b5bc046SBarry Smith #undef __FUNCT__ 2175b5bc046SBarry Smith #define __FUNCT__ "PetscSNPrintf" 2185b5bc046SBarry Smith /*@C 2195b5bc046SBarry Smith PetscSNPrintf - Prints to a string of given length 2205b5bc046SBarry Smith 2215b5bc046SBarry Smith Not Collective 2225b5bc046SBarry Smith 2235b5bc046SBarry Smith Input Parameters: 2245b5bc046SBarry Smith + str - the string to print to 2255b5bc046SBarry Smith . len - the length of str 2265b5bc046SBarry Smith . format - the usual printf() format string 2275b5bc046SBarry Smith - any arguments 2285b5bc046SBarry Smith 2295b5bc046SBarry Smith Level: intermediate 2305b5bc046SBarry Smith 2315b5bc046SBarry Smith .seealso: PetscSynchronizedFlush(), PetscSynchronizedFPrintf(), PetscFPrintf(), PetscVSNPrintf(), 2325b5bc046SBarry Smith PetscPrintf(), PetscViewerASCIIPrintf(), PetscViewerASCIISynchronizedPrintf() 2335b5bc046SBarry Smith @*/ 2347087cfbeSBarry Smith PetscErrorCode PetscSNPrintf(char *str,size_t len,const char format[],...) 2355b5bc046SBarry Smith { 2365b5bc046SBarry Smith PetscErrorCode ierr; 237c9a19010SBarry Smith size_t fullLength; 2385b5bc046SBarry Smith va_list Argp; 2395b5bc046SBarry Smith 2405b5bc046SBarry Smith PetscFunctionBegin; 2415b5bc046SBarry Smith va_start(Argp,format); 2422d609e63SMatthew Knepley ierr = PetscVSNPrintf(str,len,format,&fullLength,Argp);CHKERRQ(ierr); 2435b5bc046SBarry Smith PetscFunctionReturn(0); 2445b5bc046SBarry Smith } 2455b5bc046SBarry Smith 246257d2499SJed Brown #undef __FUNCT__ 247257d2499SJed Brown #define __FUNCT__ "PetscSNPrintfCount" 248257d2499SJed Brown /*@C 249257d2499SJed Brown PetscSNPrintfCount - Prints to a string of given length, returns count 250257d2499SJed Brown 251257d2499SJed Brown Not Collective 252257d2499SJed Brown 253257d2499SJed Brown Input Parameters: 254257d2499SJed Brown + str - the string to print to 255257d2499SJed Brown . len - the length of str 256257d2499SJed Brown . format - the usual printf() format string 257257d2499SJed Brown . countused - number of characters used 258257d2499SJed Brown - any arguments 259257d2499SJed Brown 260257d2499SJed Brown Level: intermediate 261257d2499SJed Brown 262257d2499SJed Brown .seealso: PetscSynchronizedFlush(), PetscSynchronizedFPrintf(), PetscFPrintf(), PetscVSNPrintf(), 263257d2499SJed Brown PetscPrintf(), PetscViewerASCIIPrintf(), PetscViewerASCIISynchronizedPrintf(), PetscSNPrintf() 264257d2499SJed Brown @*/ 265257d2499SJed Brown PetscErrorCode PetscSNPrintfCount(char *str,size_t len,const char format[],size_t *countused,...) 266257d2499SJed Brown { 267257d2499SJed Brown PetscErrorCode ierr; 268257d2499SJed Brown va_list Argp; 269257d2499SJed Brown 270257d2499SJed Brown PetscFunctionBegin; 271257d2499SJed Brown va_start(Argp,countused); 272257d2499SJed Brown ierr = PetscVSNPrintf(str,len,format,countused,Argp);CHKERRQ(ierr); 273257d2499SJed Brown PetscFunctionReturn(0); 274257d2499SJed Brown } 275257d2499SJed Brown 276e5c89e4eSSatish Balay /* ----------------------------------------------------------------------- */ 277e5c89e4eSSatish Balay 278d30b0576SJed Brown PrintfQueue petsc_printfqueue = 0,petsc_printfqueuebase = 0; 279d30b0576SJed Brown int petsc_printfqueuelength = 0; 280*0298fd71SBarry Smith FILE *petsc_printfqueuefile = NULL; 281e5c89e4eSSatish Balay 282e5c89e4eSSatish Balay #undef __FUNCT__ 283e5c89e4eSSatish Balay #define __FUNCT__ "PetscSynchronizedPrintf" 284e5c89e4eSSatish Balay /*@C 285e5c89e4eSSatish Balay PetscSynchronizedPrintf - Prints synchronized output from several processors. 286e5c89e4eSSatish Balay Output of the first processor is followed by that of the second, etc. 287e5c89e4eSSatish Balay 288e5c89e4eSSatish Balay Not Collective 289e5c89e4eSSatish Balay 290e5c89e4eSSatish Balay Input Parameters: 291e5c89e4eSSatish Balay + comm - the communicator 292e5c89e4eSSatish Balay - format - the usual printf() format string 293e5c89e4eSSatish Balay 294e5c89e4eSSatish Balay Level: intermediate 295e5c89e4eSSatish Balay 296e5c89e4eSSatish Balay Notes: 297e5c89e4eSSatish Balay REQUIRES a intervening call to PetscSynchronizedFlush() for the information 298e5c89e4eSSatish Balay from all the processors to be printed. 299e5c89e4eSSatish Balay 300e5c89e4eSSatish Balay Fortran Note: 301d60d70ccSBarry Smith The call sequence is PetscSynchronizedPrintf(MPI_Comm, character(*), PetscErrorCode ierr) from Fortran. 302e5c89e4eSSatish Balay That is, you can only pass a single character string from Fortran. 303e5c89e4eSSatish Balay 304e5c89e4eSSatish Balay .seealso: PetscSynchronizedFlush(), PetscSynchronizedFPrintf(), PetscFPrintf(), 305e5c89e4eSSatish Balay PetscPrintf(), PetscViewerASCIIPrintf(), PetscViewerASCIISynchronizedPrintf() 306e5c89e4eSSatish Balay @*/ 3077087cfbeSBarry Smith PetscErrorCode PetscSynchronizedPrintf(MPI_Comm comm,const char format[],...) 308e5c89e4eSSatish Balay { 309e5c89e4eSSatish Balay PetscErrorCode ierr; 310e5c89e4eSSatish Balay PetscMPIInt rank; 311e5c89e4eSSatish Balay 312e5c89e4eSSatish Balay PetscFunctionBegin; 313e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 314e5c89e4eSSatish Balay 315e5c89e4eSSatish Balay /* First processor prints immediately to stdout */ 316e5c89e4eSSatish Balay if (!rank) { 317e5c89e4eSSatish Balay va_list Argp; 318e5c89e4eSSatish Balay va_start(Argp,format); 3191179db26SBarry Smith ierr = (*PetscVFPrintf)(PETSC_STDOUT,format,Argp);CHKERRQ(ierr); 320e5c89e4eSSatish Balay if (petsc_history) { 321cdc7d174SSatish Balay va_start(Argp,format); 3221179db26SBarry Smith ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr); 323e5c89e4eSSatish Balay } 324e5c89e4eSSatish Balay va_end(Argp); 325e5c89e4eSSatish Balay } else { /* other processors add to local queue */ 326e5c89e4eSSatish Balay va_list Argp; 327e5c89e4eSSatish Balay PrintfQueue next; 328c9a19010SBarry Smith size_t fullLength = 8191; 329e5c89e4eSSatish Balay 330e5c89e4eSSatish Balay ierr = PetscNew(struct _PrintfQueue,&next);CHKERRQ(ierr); 331a297a907SKarl Rupp if (petsc_printfqueue) { 332a297a907SKarl Rupp petsc_printfqueue->next = next; 333a297a907SKarl Rupp petsc_printfqueue = next; 334a297a907SKarl Rupp petsc_printfqueue->next = 0; 335a297a907SKarl Rupp } else petsc_printfqueuebase = petsc_printfqueue = next; 336d30b0576SJed Brown petsc_printfqueuelength++; 3372d609e63SMatthew Knepley next->size = -1; 3389ad23270SJed Brown while ((PetscInt)fullLength >= next->size) { 3392d609e63SMatthew Knepley next->size = fullLength+1; 340a297a907SKarl Rupp 3412d609e63SMatthew Knepley ierr = PetscMalloc(next->size * sizeof(char), &next->string);CHKERRQ(ierr); 342e5c89e4eSSatish Balay va_start(Argp,format); 3432d609e63SMatthew Knepley ierr = PetscMemzero(next->string,next->size);CHKERRQ(ierr); 3442d609e63SMatthew Knepley ierr = PetscVSNPrintf(next->string,next->size,format, &fullLength,Argp);CHKERRQ(ierr); 345e5c89e4eSSatish Balay va_end(Argp); 346e5c89e4eSSatish Balay } 3472d609e63SMatthew Knepley } 348e5c89e4eSSatish Balay PetscFunctionReturn(0); 349e5c89e4eSSatish Balay } 350e5c89e4eSSatish Balay 351e5c89e4eSSatish Balay #undef __FUNCT__ 352e5c89e4eSSatish Balay #define __FUNCT__ "PetscSynchronizedFPrintf" 353e5c89e4eSSatish Balay /*@C 354e5c89e4eSSatish Balay PetscSynchronizedFPrintf - Prints synchronized output to the specified file from 355e5c89e4eSSatish Balay several processors. Output of the first processor is followed by that of the 356e5c89e4eSSatish Balay second, etc. 357e5c89e4eSSatish Balay 358e5c89e4eSSatish Balay Not Collective 359e5c89e4eSSatish Balay 360e5c89e4eSSatish Balay Input Parameters: 361e5c89e4eSSatish Balay + comm - the communicator 362e5c89e4eSSatish Balay . fd - the file pointer 363e5c89e4eSSatish Balay - format - the usual printf() format string 364e5c89e4eSSatish Balay 365e5c89e4eSSatish Balay Level: intermediate 366e5c89e4eSSatish Balay 367e5c89e4eSSatish Balay Notes: 368e5c89e4eSSatish Balay REQUIRES a intervening call to PetscSynchronizedFlush() for the information 369e5c89e4eSSatish Balay from all the processors to be printed. 370e5c89e4eSSatish Balay 371e5c89e4eSSatish Balay .seealso: PetscSynchronizedPrintf(), PetscSynchronizedFlush(), PetscFPrintf(), 372e5c89e4eSSatish Balay PetscFOpen(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIPrintf() 373e5c89e4eSSatish Balay 374e5c89e4eSSatish Balay @*/ 3757087cfbeSBarry Smith PetscErrorCode PetscSynchronizedFPrintf(MPI_Comm comm,FILE *fp,const char format[],...) 376e5c89e4eSSatish Balay { 377e5c89e4eSSatish Balay PetscErrorCode ierr; 378e5c89e4eSSatish Balay PetscMPIInt rank; 379e5c89e4eSSatish Balay 380e5c89e4eSSatish Balay PetscFunctionBegin; 381e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 382e5c89e4eSSatish Balay 383e5c89e4eSSatish Balay /* First processor prints immediately to fp */ 384e5c89e4eSSatish Balay if (!rank) { 385e5c89e4eSSatish Balay va_list Argp; 386e5c89e4eSSatish Balay va_start(Argp,format); 3871179db26SBarry Smith ierr = (*PetscVFPrintf)(fp,format,Argp);CHKERRQ(ierr); 388a297a907SKarl Rupp 389d30b0576SJed Brown petsc_printfqueuefile = fp; 390cdc7d174SSatish Balay if (petsc_history && (fp !=petsc_history)) { 391cdc7d174SSatish Balay va_start(Argp,format); 3921179db26SBarry Smith ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr); 393e5c89e4eSSatish Balay } 394e5c89e4eSSatish Balay va_end(Argp); 395e5c89e4eSSatish Balay } else { /* other processors add to local queue */ 396e5c89e4eSSatish Balay va_list Argp; 397e5c89e4eSSatish Balay PrintfQueue next; 398c9a19010SBarry Smith size_t fullLength = 8191; 399e5c89e4eSSatish Balay ierr = PetscNew(struct _PrintfQueue,&next);CHKERRQ(ierr); 400a297a907SKarl Rupp if (petsc_printfqueue) { 401a297a907SKarl Rupp petsc_printfqueue->next = next; 402a297a907SKarl Rupp petsc_printfqueue = next; 403a297a907SKarl Rupp petsc_printfqueue->next = 0; 404a297a907SKarl Rupp } else petsc_printfqueuebase = petsc_printfqueue = next; 405d30b0576SJed Brown petsc_printfqueuelength++; 4062d609e63SMatthew Knepley next->size = -1; 4079ad23270SJed Brown while ((PetscInt)fullLength >= next->size) { 4082d609e63SMatthew Knepley next->size = fullLength+1; 4092d609e63SMatthew Knepley ierr = PetscMalloc(next->size * sizeof(char), &next->string);CHKERRQ(ierr); 410e5c89e4eSSatish Balay va_start(Argp,format); 4112d609e63SMatthew Knepley ierr = PetscMemzero(next->string,next->size);CHKERRQ(ierr); 4122d609e63SMatthew Knepley ierr = PetscVSNPrintf(next->string,next->size,format,&fullLength,Argp);CHKERRQ(ierr); 413e5c89e4eSSatish Balay va_end(Argp); 414e5c89e4eSSatish Balay } 4152d609e63SMatthew Knepley } 416e5c89e4eSSatish Balay PetscFunctionReturn(0); 417e5c89e4eSSatish Balay } 418e5c89e4eSSatish Balay 419e5c89e4eSSatish Balay #undef __FUNCT__ 420e5c89e4eSSatish Balay #define __FUNCT__ "PetscSynchronizedFlush" 421e30d2299SSatish Balay /*@ 422e5c89e4eSSatish Balay PetscSynchronizedFlush - Flushes to the screen output from all processors 423e5c89e4eSSatish Balay involved in previous PetscSynchronizedPrintf() calls. 424e5c89e4eSSatish Balay 425e5c89e4eSSatish Balay Collective on MPI_Comm 426e5c89e4eSSatish Balay 427e5c89e4eSSatish Balay Input Parameters: 428e5c89e4eSSatish Balay . comm - the communicator 429e5c89e4eSSatish Balay 430e5c89e4eSSatish Balay Level: intermediate 431e5c89e4eSSatish Balay 432e5c89e4eSSatish Balay Notes: 433e5c89e4eSSatish Balay Usage of PetscSynchronizedPrintf() and PetscSynchronizedFPrintf() with 434e5c89e4eSSatish Balay different MPI communicators REQUIRES an intervening call to PetscSynchronizedFlush(). 435e5c89e4eSSatish Balay 436e5c89e4eSSatish Balay .seealso: PetscSynchronizedPrintf(), PetscFPrintf(), PetscPrintf(), PetscViewerASCIIPrintf(), 437e5c89e4eSSatish Balay PetscViewerASCIISynchronizedPrintf() 438e5c89e4eSSatish Balay @*/ 4397087cfbeSBarry Smith PetscErrorCode PetscSynchronizedFlush(MPI_Comm comm) 440e5c89e4eSSatish Balay { 441e5c89e4eSSatish Balay PetscErrorCode ierr; 442bf3b0749SBarry Smith PetscMPIInt rank,size,tag,i,j,n,dummy = 0; 4432d609e63SMatthew Knepley char *message; 444e5c89e4eSSatish Balay MPI_Status status; 445e5c89e4eSSatish Balay FILE *fd; 446e5c89e4eSSatish Balay 447e5c89e4eSSatish Balay PetscFunctionBegin; 448e5c89e4eSSatish Balay ierr = PetscCommDuplicate(comm,&comm,&tag);CHKERRQ(ierr); 449e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 450e5c89e4eSSatish Balay ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 451e5c89e4eSSatish Balay 452e5c89e4eSSatish Balay /* First processor waits for messages from all other processors */ 453e5c89e4eSSatish Balay if (!rank) { 454a297a907SKarl Rupp if (petsc_printfqueuefile) fd = petsc_printfqueuefile; 455a297a907SKarl Rupp else fd = PETSC_STDOUT; 456e5c89e4eSSatish Balay for (i=1; i<size; i++) { 4579f73f8ecSBarry Smith /* to prevent a flood of messages to process zero, request each message separately */ 4589f73f8ecSBarry Smith ierr = MPI_Send(&dummy,1,MPI_INT,i,tag,comm);CHKERRQ(ierr); 459e5c89e4eSSatish Balay ierr = MPI_Recv(&n,1,MPI_INT,i,tag,comm,&status);CHKERRQ(ierr); 460e5c89e4eSSatish Balay for (j=0; j<n; j++) { 4619f73f8ecSBarry Smith PetscMPIInt size; 4622d609e63SMatthew Knepley 4632d609e63SMatthew Knepley ierr = MPI_Recv(&size,1,MPI_INT,i,tag,comm,&status);CHKERRQ(ierr); 4642d609e63SMatthew Knepley ierr = PetscMalloc(size * sizeof(char), &message);CHKERRQ(ierr); 4652d609e63SMatthew Knepley ierr = MPI_Recv(message,size,MPI_CHAR,i,tag,comm,&status);CHKERRQ(ierr); 4663bf036e2SBarry Smith ierr = PetscFPrintf(comm,fd,"%s",message);CHKERRQ(ierr); 4672d609e63SMatthew Knepley ierr = PetscFree(message);CHKERRQ(ierr); 468e5c89e4eSSatish Balay } 469e5c89e4eSSatish Balay } 470*0298fd71SBarry Smith petsc_printfqueuefile = NULL; 471e5c89e4eSSatish Balay } else { /* other processors send queue to processor 0 */ 472d30b0576SJed Brown PrintfQueue next = petsc_printfqueuebase,previous; 473e5c89e4eSSatish Balay 474b3ef9d35SBarry Smith ierr = MPI_Recv(&dummy,1,MPI_INT,0,tag,comm,&status);CHKERRQ(ierr); 475d30b0576SJed Brown ierr = MPI_Send(&petsc_printfqueuelength,1,MPI_INT,0,tag,comm);CHKERRQ(ierr); 476d30b0576SJed Brown for (i=0; i<petsc_printfqueuelength; i++) { 4772d609e63SMatthew Knepley ierr = MPI_Send(&next->size,1,MPI_INT,0,tag,comm);CHKERRQ(ierr); 4782d609e63SMatthew Knepley ierr = MPI_Send(next->string,next->size,MPI_CHAR,0,tag,comm);CHKERRQ(ierr); 479e5c89e4eSSatish Balay previous = next; 480e5c89e4eSSatish Balay next = next->next; 4812d609e63SMatthew Knepley ierr = PetscFree(previous->string);CHKERRQ(ierr); 482e5c89e4eSSatish Balay ierr = PetscFree(previous);CHKERRQ(ierr); 483e5c89e4eSSatish Balay } 484d30b0576SJed Brown petsc_printfqueue = 0; 485d30b0576SJed Brown petsc_printfqueuelength = 0; 486e5c89e4eSSatish Balay } 487e5c89e4eSSatish Balay ierr = PetscCommDestroy(&comm);CHKERRQ(ierr); 488e5c89e4eSSatish Balay PetscFunctionReturn(0); 489e5c89e4eSSatish Balay } 490e5c89e4eSSatish Balay 491e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------------------*/ 492e5c89e4eSSatish Balay 493e5c89e4eSSatish Balay #undef __FUNCT__ 494e5c89e4eSSatish Balay #define __FUNCT__ "PetscFPrintf" 495e5c89e4eSSatish Balay /*@C 496e5c89e4eSSatish Balay PetscFPrintf - Prints to a file, only from the first 497e5c89e4eSSatish Balay processor in the communicator. 498e5c89e4eSSatish Balay 499e5c89e4eSSatish Balay Not Collective 500e5c89e4eSSatish Balay 501e5c89e4eSSatish Balay Input Parameters: 502e5c89e4eSSatish Balay + comm - the communicator 503e5c89e4eSSatish Balay . fd - the file pointer 504e5c89e4eSSatish Balay - format - the usual printf() format string 505e5c89e4eSSatish Balay 506e5c89e4eSSatish Balay Level: intermediate 507e5c89e4eSSatish Balay 508e5c89e4eSSatish Balay Fortran Note: 509e5c89e4eSSatish Balay This routine is not supported in Fortran. 510e5c89e4eSSatish Balay 511e5c89e4eSSatish Balay Concepts: printing^in parallel 512e5c89e4eSSatish Balay Concepts: printf^in parallel 513e5c89e4eSSatish Balay 514e5c89e4eSSatish Balay .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(), 515e5c89e4eSSatish Balay PetscViewerASCIISynchronizedPrintf(), PetscSynchronizedFlush() 516e5c89e4eSSatish Balay @*/ 5177087cfbeSBarry Smith PetscErrorCode PetscFPrintf(MPI_Comm comm,FILE* fd,const char format[],...) 518e5c89e4eSSatish Balay { 519e5c89e4eSSatish Balay PetscErrorCode ierr; 520e5c89e4eSSatish Balay PetscMPIInt rank; 521e5c89e4eSSatish Balay 522e5c89e4eSSatish Balay PetscFunctionBegin; 523e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 524e5c89e4eSSatish Balay if (!rank) { 525e5c89e4eSSatish Balay va_list Argp; 526e5c89e4eSSatish Balay va_start(Argp,format); 5271179db26SBarry Smith ierr = (*PetscVFPrintf)(fd,format,Argp);CHKERRQ(ierr); 528cdc7d174SSatish Balay if (petsc_history && (fd !=petsc_history)) { 529cdc7d174SSatish Balay va_start(Argp,format); 5301179db26SBarry Smith ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr); 531e5c89e4eSSatish Balay } 532e5c89e4eSSatish Balay va_end(Argp); 533e5c89e4eSSatish Balay } 534e5c89e4eSSatish Balay PetscFunctionReturn(0); 535e5c89e4eSSatish Balay } 536e5c89e4eSSatish Balay 537e5c89e4eSSatish Balay #undef __FUNCT__ 538e5c89e4eSSatish Balay #define __FUNCT__ "PetscPrintf" 539e5c89e4eSSatish Balay /*@C 540e5c89e4eSSatish Balay PetscPrintf - Prints to standard out, only from the first 541eed5747fSBarry Smith processor in the communicator. Calls from other processes are ignored. 542e5c89e4eSSatish Balay 543e5c89e4eSSatish Balay Not Collective 544e5c89e4eSSatish Balay 545e5c89e4eSSatish Balay Input Parameters: 546e5c89e4eSSatish Balay + comm - the communicator 547e5c89e4eSSatish Balay - format - the usual printf() format string 548e5c89e4eSSatish Balay 549e5c89e4eSSatish Balay Level: intermediate 550e5c89e4eSSatish Balay 551e5c89e4eSSatish Balay Fortran Note: 552d60d70ccSBarry Smith The call sequence is PetscPrintf(MPI_Comm, character(*), PetscErrorCode ierr) from Fortran. 553e5c89e4eSSatish Balay That is, you can only pass a single character string from Fortran. 554e5c89e4eSSatish Balay 555e5c89e4eSSatish Balay Concepts: printing^in parallel 556e5c89e4eSSatish Balay Concepts: printf^in parallel 557e5c89e4eSSatish Balay 558e5c89e4eSSatish Balay .seealso: PetscFPrintf(), PetscSynchronizedPrintf() 559e5c89e4eSSatish Balay @*/ 5607087cfbeSBarry Smith PetscErrorCode PetscPrintf(MPI_Comm comm,const char format[],...) 561e5c89e4eSSatish Balay { 562e5c89e4eSSatish Balay PetscErrorCode ierr; 563e5c89e4eSSatish Balay PetscMPIInt rank; 564e5c89e4eSSatish Balay 565e5c89e4eSSatish Balay PetscFunctionBegin; 566e5c89e4eSSatish Balay if (!comm) comm = PETSC_COMM_WORLD; 567e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 568e5c89e4eSSatish Balay if (!rank) { 569e5c89e4eSSatish Balay va_list Argp; 570e5c89e4eSSatish Balay va_start(Argp,format); 571eed5747fSBarry Smith ierr = (*PetscVFPrintf)(PETSC_STDOUT,format,Argp);CHKERRQ(ierr); 572e5c89e4eSSatish Balay if (petsc_history) { 573cdc7d174SSatish Balay va_start(Argp,format); 574eed5747fSBarry Smith ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr); 575e5c89e4eSSatish Balay } 576e5c89e4eSSatish Balay va_end(Argp); 577e5c89e4eSSatish Balay } 578e5c89e4eSSatish Balay PetscFunctionReturn(0); 579e5c89e4eSSatish Balay } 580e5c89e4eSSatish Balay 581e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------------------*/ 582e5c89e4eSSatish Balay #undef __FUNCT__ 583e5c89e4eSSatish Balay #define __FUNCT__ "PetscHelpPrintfDefault" 584c9a19010SBarry Smith /*@C 585c9a19010SBarry Smith PetscHelpPrintf - All PETSc help messages are passing through this function. You can change how help messages are printed by 586c9a19010SBarry Smith replacinng it with something that does not simply write to a stdout. 587c9a19010SBarry Smith 588c9a19010SBarry Smith To use, write your own function for example, 589c9a19010SBarry Smith $PetscErrorCode mypetschelpprintf(MPI_Comm comm,const char format[],....) 590c9a19010SBarry Smith ${ 591c9a19010SBarry Smith $ PetscFunctionReturn(0); 592c9a19010SBarry Smith $} 593c9a19010SBarry Smith then before the call to PetscInitialize() do the assignment 594c9a19010SBarry Smith $ PetscHelpPrintf = mypetschelpprintf; 595c9a19010SBarry Smith 596c9a19010SBarry Smith Note: the default routine used is called PetscHelpPrintfDefault(). 597c9a19010SBarry Smith 598c9a19010SBarry Smith Level: developer 599c9a19010SBarry Smith 600c9a19010SBarry Smith .seealso: PetscVSNPrintf(), PetscVFPrintf(), PetscErrorPrintf() 601c9a19010SBarry Smith @*/ 6027087cfbeSBarry Smith PetscErrorCode PetscHelpPrintfDefault(MPI_Comm comm,const char format[],...) 603e5c89e4eSSatish Balay { 604e5c89e4eSSatish Balay PetscErrorCode ierr; 605e5c89e4eSSatish Balay PetscMPIInt rank; 606e5c89e4eSSatish Balay 607e5c89e4eSSatish Balay PetscFunctionBegin; 608e5c89e4eSSatish Balay if (!comm) comm = PETSC_COMM_WORLD; 609e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 610e5c89e4eSSatish Balay if (!rank) { 611e5c89e4eSSatish Balay va_list Argp; 612e5c89e4eSSatish Balay va_start(Argp,format); 6131179db26SBarry Smith ierr = (*PetscVFPrintf)(PETSC_STDOUT,format,Argp);CHKERRQ(ierr); 614e5c89e4eSSatish Balay if (petsc_history) { 615cdc7d174SSatish Balay va_start(Argp,format); 6161179db26SBarry Smith ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr); 617e5c89e4eSSatish Balay } 618e5c89e4eSSatish Balay va_end(Argp); 619e5c89e4eSSatish Balay } 620e5c89e4eSSatish Balay PetscFunctionReturn(0); 621e5c89e4eSSatish Balay } 622e5c89e4eSSatish Balay 623e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------------------*/ 624e5c89e4eSSatish Balay 625e5c89e4eSSatish Balay 626e5c89e4eSSatish Balay #undef __FUNCT__ 627e5c89e4eSSatish Balay #define __FUNCT__ "PetscSynchronizedFGets" 628e5c89e4eSSatish Balay /*@C 629e5c89e4eSSatish Balay PetscSynchronizedFGets - Several processors all get the same line from a file. 630e5c89e4eSSatish Balay 631e5c89e4eSSatish Balay Collective on MPI_Comm 632e5c89e4eSSatish Balay 633e5c89e4eSSatish Balay Input Parameters: 634e5c89e4eSSatish Balay + comm - the communicator 635e5c89e4eSSatish Balay . fd - the file pointer 636e5c89e4eSSatish Balay - len - the length of the output buffer 637e5c89e4eSSatish Balay 638e5c89e4eSSatish Balay Output Parameter: 639e5c89e4eSSatish Balay . string - the line read from the file 640e5c89e4eSSatish Balay 641e5c89e4eSSatish Balay Level: intermediate 642e5c89e4eSSatish Balay 643e5c89e4eSSatish Balay .seealso: PetscSynchronizedPrintf(), PetscSynchronizedFlush(), 644e5c89e4eSSatish Balay PetscFOpen(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIPrintf() 645e5c89e4eSSatish Balay 646e5c89e4eSSatish Balay @*/ 6477087cfbeSBarry Smith PetscErrorCode PetscSynchronizedFGets(MPI_Comm comm,FILE *fp,size_t len,char string[]) 648e5c89e4eSSatish Balay { 649e5c89e4eSSatish Balay PetscErrorCode ierr; 650e5c89e4eSSatish Balay PetscMPIInt rank; 651e5c89e4eSSatish Balay 652e5c89e4eSSatish Balay PetscFunctionBegin; 653e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 654e5c89e4eSSatish Balay 655e5c89e4eSSatish Balay if (!rank) { 656047b9c12SMatthew G Knepley char *ptr = fgets(string, len, fp); 657047b9c12SMatthew G Knepley 658047b9c12SMatthew G Knepley if (!ptr) { 659a297a907SKarl Rupp if (feof(fp)) len = 0; 660a297a907SKarl Rupp else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_FILE_READ, "Error reading from file: %d", errno); 661047b9c12SMatthew G Knepley } 662e5c89e4eSSatish Balay } 663e5c89e4eSSatish Balay ierr = MPI_Bcast(string,len,MPI_BYTE,0,comm);CHKERRQ(ierr); 664e5c89e4eSSatish Balay PetscFunctionReturn(0); 665e5c89e4eSSatish Balay } 666238ccf28SShri Abhyankar 667238ccf28SShri Abhyankar #if defined(PETSC_HAVE_MATLAB_ENGINE) 668c6db04a5SJed Brown #include <mex.h> 669238ccf28SShri Abhyankar #undef __FUNCT__ 670238ccf28SShri Abhyankar #define __FUNCT__ "PetscVFPrintf_Matlab" 6717087cfbeSBarry Smith PetscErrorCode PetscVFPrintf_Matlab(FILE *fd,const char format[],va_list Argp) 672238ccf28SShri Abhyankar { 673238ccf28SShri Abhyankar PetscErrorCode ierr; 674238ccf28SShri Abhyankar 675238ccf28SShri Abhyankar PetscFunctionBegin; 676238ccf28SShri Abhyankar if (fd != stdout && fd != stderr) { /* handle regular files */ 677238ccf28SShri Abhyankar ierr = PetscVFPrintfDefault(fd,format,Argp);CHKERRQ(ierr); 678238ccf28SShri Abhyankar } else { 679238ccf28SShri Abhyankar size_t len=8*1024,length; 680238ccf28SShri Abhyankar char buf[len]; 681238ccf28SShri Abhyankar 682238ccf28SShri Abhyankar ierr = PetscVSNPrintf(buf,len,format,&length,Argp);CHKERRQ(ierr); 683df413903SBarry Smith mexPrintf("%s",buf); 684238ccf28SShri Abhyankar } 685238ccf28SShri Abhyankar PetscFunctionReturn(0); 686238ccf28SShri Abhyankar } 687238ccf28SShri Abhyankar #endif 6886fc7ef2bSBarry Smith 6896fc7ef2bSBarry Smith #undef __FUNCT__ 6908c74ee41SBarry Smith #define __FUNCT__ "PetscFormatStrip" 6918c74ee41SBarry Smith /*@C 6928c74ee41SBarry Smith PetscFormatStrip - Takes a PETSc format string and removes all numerical modifiers to % operations 6938c74ee41SBarry Smith 6948c74ee41SBarry Smith Input Parameters: 6958c74ee41SBarry Smith . format - the PETSc format string 6968c74ee41SBarry Smith 6978c74ee41SBarry Smith Level: developer 6988c74ee41SBarry Smith 6998c74ee41SBarry Smith @*/ 7008c74ee41SBarry Smith PetscErrorCode PetscFormatStrip(char *format) 7018c74ee41SBarry Smith { 7028c74ee41SBarry Smith size_t loc1 = 0, loc2 = 0; 7038c74ee41SBarry Smith 7048c74ee41SBarry Smith PetscFunctionBegin; 7058c74ee41SBarry Smith while (format[loc2]) { 7068c74ee41SBarry Smith if (format[loc2] == '%') { 7078c74ee41SBarry Smith format[loc1++] = format[loc2++]; 7088c74ee41SBarry Smith while (format[loc2] && ((format[loc2] >= '0' && format[loc2] <= '9') || format[loc2] == '.')) loc2++; 7098c74ee41SBarry Smith } 7108c74ee41SBarry Smith format[loc1++] = format[loc2++]; 7118c74ee41SBarry Smith } 7128c74ee41SBarry Smith PetscFunctionReturn(0); 7138c74ee41SBarry Smith } 7148c74ee41SBarry Smith 715