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++; 87e5c89e4eSSatish Balay } else { 88e5c89e4eSSatish Balay newformat[j++] = format[i++]; 89e5c89e4eSSatish Balay } 90e5c89e4eSSatish Balay } 91e5c89e4eSSatish Balay newformat[j] = 0; 92eed5747fSBarry Smith PetscFunctionReturn(0); 93e5c89e4eSSatish Balay } 94e5c89e4eSSatish Balay 95e5c89e4eSSatish Balay #undef __FUNCT__ 96e5c89e4eSSatish Balay #define __FUNCT__ "PetscVSNPrintf" 97c9a19010SBarry Smith /*@C 98c9a19010SBarry Smith PetscVSNPrintf - The PETSc version of vsnprintf(). Converts a PETSc format string into a standard C format string and then puts all the 99c9a19010SBarry Smith function arguments into a string using the format statement. 100c9a19010SBarry Smith 101c9a19010SBarry Smith Input Parameters: 102c9a19010SBarry Smith + str - location to put result 103c9a19010SBarry Smith . len - the amount of space in str 104c9a19010SBarry Smith + format - the PETSc format string 105c9a19010SBarry Smith - fullLength - the amount of space in str actually used. 106c9a19010SBarry Smith 107eed5747fSBarry 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 108eed5747fSBarry Smith a recursion will occur and possible crash. 109c9a19010SBarry Smith 110c9a19010SBarry Smith Level: developer 111c9a19010SBarry Smith 112c9a19010SBarry Smith @*/ 1137087cfbeSBarry Smith PetscErrorCode PetscVSNPrintf(char *str,size_t len,const char *format,size_t *fullLength,va_list Argp) 114e5c89e4eSSatish Balay { 115e2135aedSMatthew Knepley char *newformat; 116e2135aedSMatthew Knepley char formatbuf[8*1024]; 1171a96acb8SJed Brown size_t oldLength,length; 1181a96acb8SJed Brown int fullLengthInt; 1191a15ef5dSMatthew Knepley PetscErrorCode ierr; 120e5c89e4eSSatish Balay 121eed5747fSBarry Smith PetscFunctionBegin; 122e2135aedSMatthew Knepley ierr = PetscStrlen(format, &oldLength);CHKERRQ(ierr); 123e2135aedSMatthew Knepley if (oldLength < 8*1024) { 124e2135aedSMatthew Knepley newformat = formatbuf; 125354e5084SJose Roman oldLength = 8*1024-1; 126e2135aedSMatthew Knepley } else { 127354e5084SJose Roman oldLength = PETSC_MAX_LENGTH_FORMAT(oldLength); 128354e5084SJose Roman ierr = PetscMalloc(oldLength * sizeof(char), &newformat);CHKERRQ(ierr); 129e2135aedSMatthew Knepley } 130354e5084SJose Roman PetscFormatConvert(format,newformat,oldLength); 1311a15ef5dSMatthew Knepley ierr = PetscStrlen(newformat, &length);CHKERRQ(ierr); 1322d609e63SMatthew Knepley #if 0 1331a15ef5dSMatthew Knepley if (length > len) { 1341a15ef5dSMatthew Knepley newformat[len] = '\0'; 1351a15ef5dSMatthew Knepley } 1362d609e63SMatthew Knepley #endif 1375a058713SBarry Smith #if defined(PETSC_HAVE_VSNPRINTF_CHAR) 13842c7c0bfSJed Brown fullLengthInt = vsnprintf(str,len,newformat,(char *)Argp); 13989b07760SSatish Balay #elif defined(PETSC_HAVE_VSNPRINTF) 14042c7c0bfSJed Brown fullLengthInt = vsnprintf(str,len,newformat,Argp); 141141c0d65SSatish Balay #elif defined(PETSC_HAVE__VSNPRINTF) 14242c7c0bfSJed Brown fullLengthInt = _vsnprintf(str,len,newformat,Argp); 143e5c89e4eSSatish Balay #else 14489b07760SSatish Balay #error "vsnprintf not found" 145e5c89e4eSSatish Balay #endif 14642c7c0bfSJed Brown if (fullLengthInt < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"vsnprintf() failed"); 1478c74ee41SBarry Smith if (fullLength) *fullLength = (size_t)fullLengthInt; 148e2135aedSMatthew Knepley if (oldLength >= 8*1024) { 149e2135aedSMatthew Knepley ierr = PetscFree(newformat);CHKERRQ(ierr); 150e2135aedSMatthew Knepley } 151eed5747fSBarry Smith PetscFunctionReturn(0); 152e5c89e4eSSatish Balay } 153e5c89e4eSSatish Balay 154e5c89e4eSSatish Balay #undef __FUNCT__ 155c9a19010SBarry Smith #define __FUNCT__ "PetscVFPrintfDefault" 156c9a19010SBarry Smith /*@C 157c9a19010SBarry Smith PetscVFPrintf - All PETSc standard out and error messages are sent through this function; so, in theory, this can 158e5c89e4eSSatish Balay can be replaced with something that does not simply write to a file. 159e5c89e4eSSatish Balay 160c9a19010SBarry Smith To use, write your own function for example, 161c9a19010SBarry Smith $PetscErrorCode mypetscvfprintf(FILE *fd,const char format[],va_list Argp) 162c9a19010SBarry Smith ${ 163c9a19010SBarry Smith $ PetscErrorCode ierr; 164c9a19010SBarry Smith $ 165c9a19010SBarry Smith $ PetscFunctionBegin; 166c9a19010SBarry Smith $ if (fd != stdout && fd != stderr) { handle regular files 167c9a19010SBarry Smith $ ierr = PetscVFPrintfDefault(fd,format,Argp);CHKERR(ierr); 168c9a19010SBarry Smith $ } else { 169c9a19010SBarry Smith $ char buff[BIG]; 170c9a19010SBarry Smith $ size_t length; 171c9a19010SBarry Smith $ ierr = PetscVSNPrintf(buff,BIG,format,&length,Argp);CHKERRQ(ierr); 172c9a19010SBarry Smith $ now send buff to whatever stream or whatever you want 173c9a19010SBarry Smith $ } 174c9a19010SBarry Smith $ PetscFunctionReturn(0); 175c9a19010SBarry Smith $} 176c9a19010SBarry Smith then before the call to PetscInitialize() do the assignment 177c9a19010SBarry Smith $ PetscVFPrintf = mypetscvfprintf; 178c9a19010SBarry Smith 179c9a19010SBarry Smith Notes: For error messages this may be called by any process, for regular standard out it is 180e5c89e4eSSatish Balay called only by process 0 of a given communicator 181e5c89e4eSSatish Balay 182eed5747fSBarry Smith Developer Notes: this could be called by an error handler, if that happens then a recursion of the error handler may occur 183eed5747fSBarry Smith and a crash 184c9a19010SBarry Smith 185c9a19010SBarry Smith Level: developer 186c9a19010SBarry Smith 187c9a19010SBarry Smith .seealso: PetscVSNPrintf(), PetscErrorPrintf() 188c9a19010SBarry Smith 189c9a19010SBarry Smith @*/ 1907087cfbeSBarry Smith PetscErrorCode PetscVFPrintfDefault(FILE *fd,const char *format,va_list Argp) 191e5c89e4eSSatish Balay { 192e2135aedSMatthew Knepley char *newformat; 193e2135aedSMatthew Knepley char formatbuf[8*1024]; 194e2135aedSMatthew Knepley size_t oldLength; 195eed5747fSBarry Smith PetscErrorCode ierr; 1961179db26SBarry Smith 197eed5747fSBarry Smith PetscFunctionBegin; 198eed5747fSBarry Smith ierr = PetscStrlen(format, &oldLength);CHKERRQ(ierr); 199e2135aedSMatthew Knepley if (oldLength < 8*1024) { 200e2135aedSMatthew Knepley newformat = formatbuf; 201354e5084SJose Roman oldLength = 8*1024-1; 202e2135aedSMatthew Knepley } else { 203354e5084SJose Roman oldLength = PETSC_MAX_LENGTH_FORMAT(oldLength); 204eed5747fSBarry Smith ierr = PetscMalloc(oldLength * sizeof(char), &newformat);CHKERRQ(ierr); 205e2135aedSMatthew Knepley } 206eed5747fSBarry Smith ierr = PetscFormatConvert(format,newformat,oldLength);CHKERRQ(ierr); 207d8c6e182Sbcordonn 2085a058713SBarry Smith #if defined(PETSC_HAVE_VFPRINTF_CHAR) 209e5c89e4eSSatish Balay vfprintf(fd,newformat,(char *)Argp); 210e5c89e4eSSatish Balay #else 211e5c89e4eSSatish Balay vfprintf(fd,newformat,Argp); 212e5c89e4eSSatish Balay #endif 2135a058713SBarry Smith fflush(fd); 214e2135aedSMatthew Knepley if (oldLength >= 8*1024) { 215eed5747fSBarry Smith ierr = PetscFree(newformat);CHKERRQ(ierr); 216e2135aedSMatthew Knepley } 217eed5747fSBarry Smith PetscFunctionReturn(0); 218e5c89e4eSSatish Balay } 219e5c89e4eSSatish Balay 2205b5bc046SBarry Smith #undef __FUNCT__ 2215b5bc046SBarry Smith #define __FUNCT__ "PetscSNPrintf" 2225b5bc046SBarry Smith /*@C 2235b5bc046SBarry Smith PetscSNPrintf - Prints to a string of given length 2245b5bc046SBarry Smith 2255b5bc046SBarry Smith Not Collective 2265b5bc046SBarry Smith 2275b5bc046SBarry Smith Input Parameters: 2285b5bc046SBarry Smith + str - the string to print to 2295b5bc046SBarry Smith . len - the length of str 2305b5bc046SBarry Smith . format - the usual printf() format string 2315b5bc046SBarry Smith - any arguments 2325b5bc046SBarry Smith 2335b5bc046SBarry Smith Level: intermediate 2345b5bc046SBarry Smith 2355b5bc046SBarry Smith .seealso: PetscSynchronizedFlush(), PetscSynchronizedFPrintf(), PetscFPrintf(), PetscVSNPrintf(), 2365b5bc046SBarry Smith PetscPrintf(), PetscViewerASCIIPrintf(), PetscViewerASCIISynchronizedPrintf() 2375b5bc046SBarry Smith @*/ 2387087cfbeSBarry Smith PetscErrorCode PetscSNPrintf(char *str,size_t len,const char format[],...) 2395b5bc046SBarry Smith { 2405b5bc046SBarry Smith PetscErrorCode ierr; 241c9a19010SBarry Smith size_t fullLength; 2425b5bc046SBarry Smith va_list Argp; 2435b5bc046SBarry Smith 2445b5bc046SBarry Smith PetscFunctionBegin; 2455b5bc046SBarry Smith va_start(Argp,format); 2462d609e63SMatthew Knepley ierr = PetscVSNPrintf(str,len,format,&fullLength,Argp);CHKERRQ(ierr); 2475b5bc046SBarry Smith PetscFunctionReturn(0); 2485b5bc046SBarry Smith } 2495b5bc046SBarry Smith 250257d2499SJed Brown #undef __FUNCT__ 251257d2499SJed Brown #define __FUNCT__ "PetscSNPrintfCount" 252257d2499SJed Brown /*@C 253257d2499SJed Brown PetscSNPrintfCount - Prints to a string of given length, returns count 254257d2499SJed Brown 255257d2499SJed Brown Not Collective 256257d2499SJed Brown 257257d2499SJed Brown Input Parameters: 258257d2499SJed Brown + str - the string to print to 259257d2499SJed Brown . len - the length of str 260257d2499SJed Brown . format - the usual printf() format string 261257d2499SJed Brown . countused - number of characters used 262257d2499SJed Brown - any arguments 263257d2499SJed Brown 264257d2499SJed Brown Level: intermediate 265257d2499SJed Brown 266257d2499SJed Brown .seealso: PetscSynchronizedFlush(), PetscSynchronizedFPrintf(), PetscFPrintf(), PetscVSNPrintf(), 267257d2499SJed Brown PetscPrintf(), PetscViewerASCIIPrintf(), PetscViewerASCIISynchronizedPrintf(), PetscSNPrintf() 268257d2499SJed Brown @*/ 269257d2499SJed Brown PetscErrorCode PetscSNPrintfCount(char *str,size_t len,const char format[],size_t *countused,...) 270257d2499SJed Brown { 271257d2499SJed Brown PetscErrorCode ierr; 272257d2499SJed Brown va_list Argp; 273257d2499SJed Brown 274257d2499SJed Brown PetscFunctionBegin; 275257d2499SJed Brown va_start(Argp,countused); 276257d2499SJed Brown ierr = PetscVSNPrintf(str,len,format,countused,Argp);CHKERRQ(ierr); 277257d2499SJed Brown PetscFunctionReturn(0); 278257d2499SJed Brown } 279257d2499SJed Brown 280e5c89e4eSSatish Balay /* ----------------------------------------------------------------------- */ 281e5c89e4eSSatish Balay 282d30b0576SJed Brown PrintfQueue petsc_printfqueue = 0,petsc_printfqueuebase = 0; 283d30b0576SJed Brown int petsc_printfqueuelength = 0; 284d30b0576SJed Brown FILE *petsc_printfqueuefile = PETSC_NULL; 285e5c89e4eSSatish Balay 286e5c89e4eSSatish Balay #undef __FUNCT__ 287e5c89e4eSSatish Balay #define __FUNCT__ "PetscSynchronizedPrintf" 288e5c89e4eSSatish Balay /*@C 289e5c89e4eSSatish Balay PetscSynchronizedPrintf - Prints synchronized output from several processors. 290e5c89e4eSSatish Balay Output of the first processor is followed by that of the second, etc. 291e5c89e4eSSatish Balay 292e5c89e4eSSatish Balay Not Collective 293e5c89e4eSSatish Balay 294e5c89e4eSSatish Balay Input Parameters: 295e5c89e4eSSatish Balay + comm - the communicator 296e5c89e4eSSatish Balay - format - the usual printf() format string 297e5c89e4eSSatish Balay 298e5c89e4eSSatish Balay Level: intermediate 299e5c89e4eSSatish Balay 300e5c89e4eSSatish Balay Notes: 301e5c89e4eSSatish Balay REQUIRES a intervening call to PetscSynchronizedFlush() for the information 302e5c89e4eSSatish Balay from all the processors to be printed. 303e5c89e4eSSatish Balay 304e5c89e4eSSatish Balay Fortran Note: 305d60d70ccSBarry Smith The call sequence is PetscSynchronizedPrintf(MPI_Comm, character(*), PetscErrorCode ierr) from Fortran. 306e5c89e4eSSatish Balay That is, you can only pass a single character string from Fortran. 307e5c89e4eSSatish Balay 308e5c89e4eSSatish Balay .seealso: PetscSynchronizedFlush(), PetscSynchronizedFPrintf(), PetscFPrintf(), 309e5c89e4eSSatish Balay PetscPrintf(), PetscViewerASCIIPrintf(), PetscViewerASCIISynchronizedPrintf() 310e5c89e4eSSatish Balay @*/ 3117087cfbeSBarry Smith PetscErrorCode PetscSynchronizedPrintf(MPI_Comm comm,const char format[],...) 312e5c89e4eSSatish Balay { 313e5c89e4eSSatish Balay PetscErrorCode ierr; 314e5c89e4eSSatish Balay PetscMPIInt rank; 315e5c89e4eSSatish Balay 316e5c89e4eSSatish Balay PetscFunctionBegin; 317e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 318e5c89e4eSSatish Balay 319e5c89e4eSSatish Balay /* First processor prints immediately to stdout */ 320e5c89e4eSSatish Balay if (!rank) { 321e5c89e4eSSatish Balay va_list Argp; 322e5c89e4eSSatish Balay va_start(Argp,format); 3231179db26SBarry Smith ierr = (*PetscVFPrintf)(PETSC_STDOUT,format,Argp);CHKERRQ(ierr); 324e5c89e4eSSatish Balay if (petsc_history) { 325cdc7d174SSatish Balay va_start(Argp,format); 3261179db26SBarry Smith ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr); 327e5c89e4eSSatish Balay } 328e5c89e4eSSatish Balay va_end(Argp); 329e5c89e4eSSatish Balay } else { /* other processors add to local queue */ 330e5c89e4eSSatish Balay va_list Argp; 331e5c89e4eSSatish Balay PrintfQueue next; 332c9a19010SBarry Smith size_t fullLength = 8191; 333e5c89e4eSSatish Balay 334e5c89e4eSSatish Balay ierr = PetscNew(struct _PrintfQueue,&next);CHKERRQ(ierr); 335d30b0576SJed Brown if (petsc_printfqueue) {petsc_printfqueue->next = next; petsc_printfqueue = next; petsc_printfqueue->next = 0;} 336d30b0576SJed Brown else {petsc_printfqueuebase = petsc_printfqueue = next;} 337d30b0576SJed Brown petsc_printfqueuelength++; 3382d609e63SMatthew Knepley next->size = -1; 3399ad23270SJed Brown while((PetscInt)fullLength >= next->size) { 3402d609e63SMatthew Knepley next->size = fullLength+1; 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 349e5c89e4eSSatish Balay PetscFunctionReturn(0); 350e5c89e4eSSatish Balay } 351e5c89e4eSSatish Balay 352e5c89e4eSSatish Balay #undef __FUNCT__ 353e5c89e4eSSatish Balay #define __FUNCT__ "PetscSynchronizedFPrintf" 354e5c89e4eSSatish Balay /*@C 355e5c89e4eSSatish Balay PetscSynchronizedFPrintf - Prints synchronized output to the specified file from 356e5c89e4eSSatish Balay several processors. Output of the first processor is followed by that of the 357e5c89e4eSSatish Balay second, etc. 358e5c89e4eSSatish Balay 359e5c89e4eSSatish Balay Not Collective 360e5c89e4eSSatish Balay 361e5c89e4eSSatish Balay Input Parameters: 362e5c89e4eSSatish Balay + comm - the communicator 363e5c89e4eSSatish Balay . fd - the file pointer 364e5c89e4eSSatish Balay - format - the usual printf() format string 365e5c89e4eSSatish Balay 366e5c89e4eSSatish Balay Level: intermediate 367e5c89e4eSSatish Balay 368e5c89e4eSSatish Balay Notes: 369e5c89e4eSSatish Balay REQUIRES a intervening call to PetscSynchronizedFlush() for the information 370e5c89e4eSSatish Balay from all the processors to be printed. 371e5c89e4eSSatish Balay 372e5c89e4eSSatish Balay .seealso: PetscSynchronizedPrintf(), PetscSynchronizedFlush(), PetscFPrintf(), 373e5c89e4eSSatish Balay PetscFOpen(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIPrintf() 374e5c89e4eSSatish Balay 375e5c89e4eSSatish Balay @*/ 3767087cfbeSBarry Smith PetscErrorCode PetscSynchronizedFPrintf(MPI_Comm comm,FILE* fp,const char format[],...) 377e5c89e4eSSatish Balay { 378e5c89e4eSSatish Balay PetscErrorCode ierr; 379e5c89e4eSSatish Balay PetscMPIInt rank; 380e5c89e4eSSatish Balay 381e5c89e4eSSatish Balay PetscFunctionBegin; 382e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 383e5c89e4eSSatish Balay 384e5c89e4eSSatish Balay /* First processor prints immediately to fp */ 385e5c89e4eSSatish Balay if (!rank) { 386e5c89e4eSSatish Balay va_list Argp; 387e5c89e4eSSatish Balay va_start(Argp,format); 3881179db26SBarry Smith ierr = (*PetscVFPrintf)(fp,format,Argp);CHKERRQ(ierr); 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); 400d30b0576SJed Brown if (petsc_printfqueue) {petsc_printfqueue->next = next; petsc_printfqueue = next; petsc_printfqueue->next = 0;} 401d30b0576SJed Brown else {petsc_printfqueuebase = petsc_printfqueue = next;} 402d30b0576SJed Brown petsc_printfqueuelength++; 4032d609e63SMatthew Knepley next->size = -1; 4049ad23270SJed Brown while((PetscInt)fullLength >= next->size) { 4052d609e63SMatthew Knepley next->size = fullLength+1; 4062d609e63SMatthew Knepley ierr = PetscMalloc(next->size * sizeof(char), &next->string);CHKERRQ(ierr); 407e5c89e4eSSatish Balay va_start(Argp,format); 4082d609e63SMatthew Knepley ierr = PetscMemzero(next->string,next->size);CHKERRQ(ierr); 4092d609e63SMatthew Knepley ierr = PetscVSNPrintf(next->string,next->size,format,&fullLength,Argp);CHKERRQ(ierr); 410e5c89e4eSSatish Balay va_end(Argp); 411e5c89e4eSSatish Balay } 4122d609e63SMatthew Knepley } 413e5c89e4eSSatish Balay PetscFunctionReturn(0); 414e5c89e4eSSatish Balay } 415e5c89e4eSSatish Balay 416e5c89e4eSSatish Balay #undef __FUNCT__ 417e5c89e4eSSatish Balay #define __FUNCT__ "PetscSynchronizedFlush" 418e30d2299SSatish Balay /*@ 419e5c89e4eSSatish Balay PetscSynchronizedFlush - Flushes to the screen output from all processors 420e5c89e4eSSatish Balay involved in previous PetscSynchronizedPrintf() calls. 421e5c89e4eSSatish Balay 422e5c89e4eSSatish Balay Collective on MPI_Comm 423e5c89e4eSSatish Balay 424e5c89e4eSSatish Balay Input Parameters: 425e5c89e4eSSatish Balay . comm - the communicator 426e5c89e4eSSatish Balay 427e5c89e4eSSatish Balay Level: intermediate 428e5c89e4eSSatish Balay 429e5c89e4eSSatish Balay Notes: 430e5c89e4eSSatish Balay Usage of PetscSynchronizedPrintf() and PetscSynchronizedFPrintf() with 431e5c89e4eSSatish Balay different MPI communicators REQUIRES an intervening call to PetscSynchronizedFlush(). 432e5c89e4eSSatish Balay 433e5c89e4eSSatish Balay .seealso: PetscSynchronizedPrintf(), PetscFPrintf(), PetscPrintf(), PetscViewerASCIIPrintf(), 434e5c89e4eSSatish Balay PetscViewerASCIISynchronizedPrintf() 435e5c89e4eSSatish Balay @*/ 4367087cfbeSBarry Smith PetscErrorCode PetscSynchronizedFlush(MPI_Comm comm) 437e5c89e4eSSatish Balay { 438e5c89e4eSSatish Balay PetscErrorCode ierr; 439bf3b0749SBarry Smith PetscMPIInt rank,size,tag,i,j,n,dummy = 0; 4402d609e63SMatthew Knepley char *message; 441e5c89e4eSSatish Balay MPI_Status status; 442e5c89e4eSSatish Balay FILE *fd; 443e5c89e4eSSatish Balay 444e5c89e4eSSatish Balay PetscFunctionBegin; 445e5c89e4eSSatish Balay ierr = PetscCommDuplicate(comm,&comm,&tag);CHKERRQ(ierr); 446e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 447e5c89e4eSSatish Balay ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 448e5c89e4eSSatish Balay 449e5c89e4eSSatish Balay /* First processor waits for messages from all other processors */ 450e5c89e4eSSatish Balay if (!rank) { 451d30b0576SJed Brown if (petsc_printfqueuefile) { 452d30b0576SJed Brown fd = petsc_printfqueuefile; 453e5c89e4eSSatish Balay } else { 454e5c89e4eSSatish Balay fd = PETSC_STDOUT; 455e5c89e4eSSatish Balay } 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); 466*3bf036e2SBarry Smith ierr = PetscFPrintf(comm,fd,"%s",message);CHKERRQ(ierr); 4672d609e63SMatthew Knepley ierr = PetscFree(message);CHKERRQ(ierr); 468e5c89e4eSSatish Balay } 469e5c89e4eSSatish Balay } 470d30b0576SJed Brown petsc_printfqueuefile = PETSC_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) { 659047b9c12SMatthew G Knepley if (feof(fp)) { 660047b9c12SMatthew G Knepley len = 0; 6614c2b986eSBarry Smith } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_FILE_READ, "Error reading from file: %d", errno); 662047b9c12SMatthew G Knepley } 663e5c89e4eSSatish Balay } 664e5c89e4eSSatish Balay ierr = MPI_Bcast(string,len,MPI_BYTE,0,comm);CHKERRQ(ierr); 665e5c89e4eSSatish Balay PetscFunctionReturn(0); 666e5c89e4eSSatish Balay } 667238ccf28SShri Abhyankar 668238ccf28SShri Abhyankar #if defined(PETSC_HAVE_MATLAB_ENGINE) 669c6db04a5SJed Brown #include <mex.h> 670238ccf28SShri Abhyankar #undef __FUNCT__ 671238ccf28SShri Abhyankar #define __FUNCT__ "PetscVFPrintf_Matlab" 6727087cfbeSBarry Smith PetscErrorCode PetscVFPrintf_Matlab(FILE *fd,const char format[],va_list Argp) 673238ccf28SShri Abhyankar { 674238ccf28SShri Abhyankar PetscErrorCode ierr; 675238ccf28SShri Abhyankar 676238ccf28SShri Abhyankar PetscFunctionBegin; 677238ccf28SShri Abhyankar if (fd != stdout && fd != stderr) { /* handle regular files */ 678238ccf28SShri Abhyankar ierr = PetscVFPrintfDefault(fd,format,Argp);CHKERRQ(ierr); 679238ccf28SShri Abhyankar } else { 680238ccf28SShri Abhyankar size_t len=8*1024,length; 681238ccf28SShri Abhyankar char buf[len]; 682238ccf28SShri Abhyankar 683238ccf28SShri Abhyankar ierr = PetscVSNPrintf(buf,len,format,&length,Argp);CHKERRQ(ierr); 684df413903SBarry Smith mexPrintf("%s",buf); 685238ccf28SShri Abhyankar } 686238ccf28SShri Abhyankar PetscFunctionReturn(0); 687238ccf28SShri Abhyankar } 688238ccf28SShri Abhyankar #endif 6896fc7ef2bSBarry Smith 6906fc7ef2bSBarry Smith #undef __FUNCT__ 6918c74ee41SBarry Smith #define __FUNCT__ "PetscFormatStrip" 6928c74ee41SBarry Smith /*@C 6938c74ee41SBarry Smith PetscFormatStrip - Takes a PETSc format string and removes all numerical modifiers to % operations 6948c74ee41SBarry Smith 6958c74ee41SBarry Smith Input Parameters: 6968c74ee41SBarry Smith . format - the PETSc format string 6978c74ee41SBarry Smith 6988c74ee41SBarry Smith Level: developer 6998c74ee41SBarry Smith 7008c74ee41SBarry Smith @*/ 7018c74ee41SBarry Smith PetscErrorCode PetscFormatStrip(char *format) 7028c74ee41SBarry Smith { 7038c74ee41SBarry Smith size_t loc1 = 0, loc2 = 0; 7048c74ee41SBarry Smith 7058c74ee41SBarry Smith PetscFunctionBegin; 7068c74ee41SBarry Smith while (format[loc2]){ 7078c74ee41SBarry Smith if (format[loc2] == '%') { 7088c74ee41SBarry Smith format[loc1++] = format[loc2++]; 7098c74ee41SBarry Smith while (format[loc2] && ((format[loc2] >= '0' && format[loc2] <= '9') || format[loc2] == '.')) loc2++; 7108c74ee41SBarry Smith } 7118c74ee41SBarry Smith format[loc1++] = format[loc2++]; 7128c74ee41SBarry Smith } 7138c74ee41SBarry Smith PetscFunctionReturn(0); 7148c74ee41SBarry Smith } 7158c74ee41SBarry Smith 716