1e5c89e4eSSatish Balay /* 2e5c89e4eSSatish Balay Utilites routines to add simple ASCII IO capability. 3e5c89e4eSSatish Balay */ 4*c6db04a5SJed Brown #include <../src/sys/fileio/mprint.h> 5e5c89e4eSSatish Balay /* 6e5c89e4eSSatish Balay If petsc_history is on, then all Petsc*Printf() results are saved 7e5c89e4eSSatish Balay if the appropriate (usually .petschistory) file. 8e5c89e4eSSatish Balay */ 9e5c89e4eSSatish Balay extern FILE *petsc_history; 10e5c89e4eSSatish Balay /* 11e5c89e4eSSatish Balay Allows one to overwrite where standard out is sent. For example 125106ddf5SBarry Smith PETSC_STDOUT = fopen("/dev/ttyXX","w") will cause all standard out 13e5c89e4eSSatish Balay writes to go to terminal XX; assuming you have write permission there 14e5c89e4eSSatish Balay */ 15e5c89e4eSSatish Balay FILE *PETSC_STDOUT = 0; 16ae9b4142SLisandro Dalcin /* 17ae9b4142SLisandro Dalcin Allows one to overwrite where standard error is sent. For example 18ae9b4142SLisandro Dalcin PETSC_STDERR = fopen("/dev/ttyXX","w") will cause all standard error 19ae9b4142SLisandro Dalcin writes to go to terminal XX; assuming you have write permission there 20ae9b4142SLisandro Dalcin */ 21ae9b4142SLisandro Dalcin FILE *PETSC_STDERR = 0; 22b13499bfSbcordonn /* 23b13499bfSbcordonn Used to output to Zope 24b13499bfSbcordonn */ 25b13499bfSbcordonn FILE *PETSC_ZOPEFD = 0; 26b13499bfSbcordonn 27e5c89e4eSSatish Balay #undef __FUNCT__ 28e5c89e4eSSatish Balay #define __FUNCT__ "PetscFormatConvert" 29c9a19010SBarry Smith /*@C 30c9a19010SBarry Smith PetscFormatConvert - Takes a PETSc format string and converts it to a reqular C format string 31c9a19010SBarry Smith 32c9a19010SBarry Smith Input Parameters: 33c9a19010SBarry Smith + format - the PETSc format string 34c9a19010SBarry Smith . newformat - the location to put the standard C format string values 35c9a19010SBarry Smith - size - the length of newformat 36c9a19010SBarry Smith 37c9a19010SBarry Smith Note: this exists so we can have the same code when PetscInt is either int or long long and PetscScalar is either double or float 38c9a19010SBarry Smith 39c9a19010SBarry Smith Level: developer 40c9a19010SBarry Smith 41c9a19010SBarry Smith @*/ 427087cfbeSBarry Smith PetscErrorCode PetscFormatConvert(const char *format,char *newformat,size_t size) 43e5c89e4eSSatish Balay { 44e5c89e4eSSatish Balay PetscInt i = 0,j = 0; 45e5c89e4eSSatish Balay 469daa610aSJed Brown while (format[i] && i < (PetscInt)size-1) { 47e5c89e4eSSatish Balay if (format[i] == '%' && format[i+1] == 'D') { 48e5c89e4eSSatish Balay newformat[j++] = '%'; 496de02169SBarry Smith #if !defined(PETSC_USE_64BIT_INDICES) 50e5c89e4eSSatish Balay newformat[j++] = 'd'; 51e5c89e4eSSatish Balay #else 52e5c89e4eSSatish Balay newformat[j++] = 'l'; 53e5c89e4eSSatish Balay newformat[j++] = 'l'; 54e5c89e4eSSatish Balay newformat[j++] = 'd'; 55e5c89e4eSSatish Balay #endif 56e5c89e4eSSatish Balay i += 2; 57e5c89e4eSSatish Balay } else if (format[i] == '%' && format[i+1] >= '1' && format[i+1] <= '9' && format[i+2] == 'D') { 58e5c89e4eSSatish Balay newformat[j++] = '%'; 59e5c89e4eSSatish Balay newformat[j++] = format[i+1]; 606de02169SBarry Smith #if !defined(PETSC_USE_64BIT_INDICES) 61e5c89e4eSSatish Balay newformat[j++] = 'd'; 62e5c89e4eSSatish Balay #else 63e5c89e4eSSatish Balay newformat[j++] = 'l'; 64e5c89e4eSSatish Balay newformat[j++] = 'l'; 65e5c89e4eSSatish Balay newformat[j++] = 'd'; 66e5c89e4eSSatish Balay #endif 67e5c89e4eSSatish Balay i += 3; 68a83599f4SBarry Smith } else if (format[i] == '%' && format[i+1] == 'G') { 69a83599f4SBarry Smith newformat[j++] = '%'; 700a9c782fSBarry Smith #if !defined(PETSC_USE_SCALAR_LONG_DOUBLE) 71a83599f4SBarry Smith newformat[j++] = 'g'; 72a83599f4SBarry Smith #else 73a83599f4SBarry Smith newformat[j++] = 'L'; 74a83599f4SBarry Smith newformat[j++] = 'g'; 75a83599f4SBarry Smith #endif 76a83599f4SBarry Smith i += 2; 77e5c89e4eSSatish Balay }else { 78e5c89e4eSSatish Balay newformat[j++] = format[i++]; 79e5c89e4eSSatish Balay } 80e5c89e4eSSatish Balay } 81e5c89e4eSSatish Balay newformat[j] = 0; 82e5c89e4eSSatish Balay return 0; 83e5c89e4eSSatish Balay } 84e5c89e4eSSatish Balay 85e5c89e4eSSatish Balay #undef __FUNCT__ 86e5c89e4eSSatish Balay #define __FUNCT__ "PetscVSNPrintf" 87c9a19010SBarry Smith /*@C 88c9a19010SBarry Smith PetscVSNPrintf - The PETSc version of vsnprintf(). Converts a PETSc format string into a standard C format string and then puts all the 89c9a19010SBarry Smith function arguments into a string using the format statement. 90c9a19010SBarry Smith 91c9a19010SBarry Smith Input Parameters: 92c9a19010SBarry Smith + str - location to put result 93c9a19010SBarry Smith . len - the amount of space in str 94c9a19010SBarry Smith + format - the PETSc format string 95c9a19010SBarry Smith - fullLength - the amount of space in str actually used. 96c9a19010SBarry Smith 97c9a19010SBarry Smith Note: No error handling because may be called by error handler 98c9a19010SBarry Smith 99c9a19010SBarry Smith Level: developer 100c9a19010SBarry Smith 101c9a19010SBarry Smith @*/ 1027087cfbeSBarry Smith PetscErrorCode PetscVSNPrintf(char *str,size_t len,const char *format,size_t *fullLength,va_list Argp) 103e5c89e4eSSatish Balay { 104e5c89e4eSSatish Balay /* no malloc since may be called by error handler */ 105e2135aedSMatthew Knepley char *newformat; 106e2135aedSMatthew Knepley char formatbuf[8*1024]; 107e2135aedSMatthew Knepley size_t oldLength,length; 1081a15ef5dSMatthew Knepley PetscErrorCode ierr; 109e5c89e4eSSatish Balay 110e2135aedSMatthew Knepley ierr = PetscStrlen(format, &oldLength);CHKERRQ(ierr); 111e2135aedSMatthew Knepley if (oldLength < 8*1024) { 112e2135aedSMatthew Knepley newformat = formatbuf; 113e2135aedSMatthew Knepley } else { 114e2135aedSMatthew Knepley ierr = PetscMalloc((oldLength+1) * sizeof(char), &newformat);CHKERRQ(ierr); 115e2135aedSMatthew Knepley } 116e2135aedSMatthew Knepley PetscFormatConvert(format,newformat,oldLength+1); 1171a15ef5dSMatthew Knepley ierr = PetscStrlen(newformat, &length);CHKERRQ(ierr); 1182d609e63SMatthew Knepley #if 0 1191a15ef5dSMatthew Knepley if (length > len) { 1201a15ef5dSMatthew Knepley newformat[len] = '\0'; 1211a15ef5dSMatthew Knepley } 1222d609e63SMatthew Knepley #endif 1235a058713SBarry Smith #if defined(PETSC_HAVE_VSNPRINTF_CHAR) 1242d609e63SMatthew Knepley *fullLength = vsnprintf(str,len,newformat,(char *)Argp); 12589b07760SSatish Balay #elif defined(PETSC_HAVE_VSNPRINTF) 12689b07760SSatish Balay *fullLength = vsnprintf(str,len,newformat,Argp); 127141c0d65SSatish Balay #elif defined(PETSC_HAVE__VSNPRINTF) 128141c0d65SSatish Balay *fullLength = _vsnprintf(str,len,newformat,Argp); 129e5c89e4eSSatish Balay #else 13089b07760SSatish Balay #error "vsnprintf not found" 131e5c89e4eSSatish Balay #endif 132e2135aedSMatthew Knepley if (oldLength >= 8*1024) { 133e2135aedSMatthew Knepley ierr = PetscFree(newformat);CHKERRQ(ierr); 134e2135aedSMatthew Knepley } 135e5c89e4eSSatish Balay return 0; 136e5c89e4eSSatish Balay } 137e5c89e4eSSatish Balay 138e5c89e4eSSatish Balay #undef __FUNCT__ 1397ead4f01Sbcordonn #define __FUNCT__ "PetscZopeLog" 1407087cfbeSBarry Smith PetscErrorCode PetscZopeLog(const char *format,va_list Argp) 1419c4c166aSBarry Smith { 1427ead4f01Sbcordonn /* no malloc since may be called by error handler */ 1437ead4f01Sbcordonn char newformat[8*1024]; 1447ead4f01Sbcordonn char log[8*1024]; 1457ead4f01Sbcordonn char logstart[] = " <<<log>>>"; 146c9a19010SBarry Smith size_t len,formatlen; 1479c4c166aSBarry Smith 1487ead4f01Sbcordonn PetscFormatConvert(format,newformat,8*1024); 1497ead4f01Sbcordonn PetscStrlen(logstart, &len); 1507ead4f01Sbcordonn PetscMemcpy(log, logstart, len); 1517ead4f01Sbcordonn PetscStrlen(newformat, &formatlen); 1527ead4f01Sbcordonn PetscMemcpy(&(log[len]), newformat, formatlen); 1539c4c166aSBarry Smith if (PETSC_ZOPEFD){ 1545a058713SBarry Smith #if defined(PETSC_HAVE_VFPRINTF_CHAR) 1557ead4f01Sbcordonn vfprintf(PETSC_ZOPEFD,log,(char *)Argp); 1567ead4f01Sbcordonn #else 1577ead4f01Sbcordonn vfprintf(PETSC_ZOPEFD,log,Argp); 1587ead4f01Sbcordonn #endif 1595a058713SBarry Smith fflush(PETSC_ZOPEFD); 1607ead4f01Sbcordonn } 1617ead4f01Sbcordonn return 0; 1627ead4f01Sbcordonn } 1637ead4f01Sbcordonn 1647ead4f01Sbcordonn #undef __FUNCT__ 165c9a19010SBarry Smith #define __FUNCT__ "PetscVFPrintfDefault" 166c9a19010SBarry Smith /*@C 167c9a19010SBarry Smith PetscVFPrintf - All PETSc standard out and error messages are sent through this function; so, in theory, this can 168e5c89e4eSSatish Balay can be replaced with something that does not simply write to a file. 169e5c89e4eSSatish Balay 170c9a19010SBarry Smith To use, write your own function for example, 171c9a19010SBarry Smith $PetscErrorCode mypetscvfprintf(FILE *fd,const char format[],va_list Argp) 172c9a19010SBarry Smith ${ 173c9a19010SBarry Smith $ PetscErrorCode ierr; 174c9a19010SBarry Smith $ 175c9a19010SBarry Smith $ PetscFunctionBegin; 176c9a19010SBarry Smith $ if (fd != stdout && fd != stderr) { handle regular files 177c9a19010SBarry Smith $ ierr = PetscVFPrintfDefault(fd,format,Argp); CHKERR(ierr); 178c9a19010SBarry Smith $ } else { 179c9a19010SBarry Smith $ char buff[BIG]; 180c9a19010SBarry Smith $ size_t length; 181c9a19010SBarry Smith $ ierr = PetscVSNPrintf(buff,BIG,format,&length,Argp);CHKERRQ(ierr); 182c9a19010SBarry Smith $ now send buff to whatever stream or whatever you want 183c9a19010SBarry Smith $ } 184c9a19010SBarry Smith $ PetscFunctionReturn(0); 185c9a19010SBarry Smith $} 186c9a19010SBarry Smith then before the call to PetscInitialize() do the assignment 187c9a19010SBarry Smith $ PetscVFPrintf = mypetscvfprintf; 188c9a19010SBarry Smith 189c9a19010SBarry Smith Notes: For error messages this may be called by any process, for regular standard out it is 190e5c89e4eSSatish Balay called only by process 0 of a given communicator 191e5c89e4eSSatish Balay 192e5c89e4eSSatish Balay No error handling because may be called by error handler 193c9a19010SBarry Smith 194c9a19010SBarry Smith Level: developer 195c9a19010SBarry Smith 196c9a19010SBarry Smith .seealso: PetscVSNPrintf(), PetscErrorPrintf() 197c9a19010SBarry Smith 198c9a19010SBarry Smith @*/ 1997087cfbeSBarry Smith PetscErrorCode PetscVFPrintfDefault(FILE *fd,const char *format,va_list Argp) 200e5c89e4eSSatish Balay { 201e2135aedSMatthew Knepley /* no malloc since may be called by error handler (assume no long messages in errors) */ 202e2135aedSMatthew Knepley char *newformat; 203e2135aedSMatthew Knepley char formatbuf[8*1024]; 204e2135aedSMatthew Knepley size_t oldLength; 2051179db26SBarry Smith 206e2135aedSMatthew Knepley PetscStrlen(format, &oldLength); 207e2135aedSMatthew Knepley if (oldLength < 8*1024) { 208e2135aedSMatthew Knepley newformat = formatbuf; 209e2135aedSMatthew Knepley } else { 210fee1560eSJed Brown (void)PetscMalloc((oldLength+1) * sizeof(char), &newformat); 211e2135aedSMatthew Knepley } 212e2135aedSMatthew Knepley PetscFormatConvert(format,newformat,oldLength+1); 213d8c6e182Sbcordonn 2145a058713SBarry Smith #if defined(PETSC_HAVE_VFPRINTF_CHAR) 215e5c89e4eSSatish Balay vfprintf(fd,newformat,(char *)Argp); 216e5c89e4eSSatish Balay #else 217e5c89e4eSSatish Balay vfprintf(fd,newformat,Argp); 218e5c89e4eSSatish Balay #endif 2195a058713SBarry Smith fflush(fd); 220e2135aedSMatthew Knepley if (oldLength >= 8*1024) { 2219a2231e1SJed Brown (void)PetscFree(newformat); 222e2135aedSMatthew Knepley } 223e5c89e4eSSatish Balay return 0; 224e5c89e4eSSatish Balay } 225e5c89e4eSSatish Balay 2265b5bc046SBarry Smith #undef __FUNCT__ 2275b5bc046SBarry Smith #define __FUNCT__ "PetscSNPrintf" 2285b5bc046SBarry Smith /*@C 2295b5bc046SBarry Smith PetscSNPrintf - Prints to a string of given length 2305b5bc046SBarry Smith 2315b5bc046SBarry Smith Not Collective 2325b5bc046SBarry Smith 2335b5bc046SBarry Smith Input Parameters: 2345b5bc046SBarry Smith + str - the string to print to 2355b5bc046SBarry Smith . len - the length of str 2365b5bc046SBarry Smith . format - the usual printf() format string 2375b5bc046SBarry Smith - any arguments 2385b5bc046SBarry Smith 2395b5bc046SBarry Smith Level: intermediate 2405b5bc046SBarry Smith 2415b5bc046SBarry Smith .seealso: PetscSynchronizedFlush(), PetscSynchronizedFPrintf(), PetscFPrintf(), PetscVSNPrintf(), 2425b5bc046SBarry Smith PetscPrintf(), PetscViewerASCIIPrintf(), PetscViewerASCIISynchronizedPrintf() 2435b5bc046SBarry Smith @*/ 2447087cfbeSBarry Smith PetscErrorCode PetscSNPrintf(char *str,size_t len,const char format[],...) 2455b5bc046SBarry Smith { 2465b5bc046SBarry Smith PetscErrorCode ierr; 247c9a19010SBarry Smith size_t fullLength; 2485b5bc046SBarry Smith va_list Argp; 2495b5bc046SBarry Smith 2505b5bc046SBarry Smith PetscFunctionBegin; 2515b5bc046SBarry Smith va_start(Argp,format); 2522d609e63SMatthew Knepley ierr = PetscVSNPrintf(str,len,format,&fullLength,Argp);CHKERRQ(ierr); 2535b5bc046SBarry Smith PetscFunctionReturn(0); 2545b5bc046SBarry Smith } 2555b5bc046SBarry Smith 256e5c89e4eSSatish Balay /* ----------------------------------------------------------------------- */ 257e5c89e4eSSatish Balay 258e5c89e4eSSatish Balay PrintfQueue queue = 0,queuebase = 0; 259e5c89e4eSSatish Balay int queuelength = 0; 260e5c89e4eSSatish Balay FILE *queuefile = PETSC_NULL; 261e5c89e4eSSatish Balay 262e5c89e4eSSatish Balay #undef __FUNCT__ 263e5c89e4eSSatish Balay #define __FUNCT__ "PetscSynchronizedPrintf" 264e5c89e4eSSatish Balay /*@C 265e5c89e4eSSatish Balay PetscSynchronizedPrintf - Prints synchronized output from several processors. 266e5c89e4eSSatish Balay Output of the first processor is followed by that of the second, etc. 267e5c89e4eSSatish Balay 268e5c89e4eSSatish Balay Not Collective 269e5c89e4eSSatish Balay 270e5c89e4eSSatish Balay Input Parameters: 271e5c89e4eSSatish Balay + comm - the communicator 272e5c89e4eSSatish Balay - format - the usual printf() format string 273e5c89e4eSSatish Balay 274e5c89e4eSSatish Balay Level: intermediate 275e5c89e4eSSatish Balay 276e5c89e4eSSatish Balay Notes: 277e5c89e4eSSatish Balay REQUIRES a intervening call to PetscSynchronizedFlush() for the information 278e5c89e4eSSatish Balay from all the processors to be printed. 279e5c89e4eSSatish Balay 280e5c89e4eSSatish Balay Fortran Note: 281d60d70ccSBarry Smith The call sequence is PetscSynchronizedPrintf(MPI_Comm, character(*), PetscErrorCode ierr) from Fortran. 282e5c89e4eSSatish Balay That is, you can only pass a single character string from Fortran. 283e5c89e4eSSatish Balay 284e5c89e4eSSatish Balay .seealso: PetscSynchronizedFlush(), PetscSynchronizedFPrintf(), PetscFPrintf(), 285e5c89e4eSSatish Balay PetscPrintf(), PetscViewerASCIIPrintf(), PetscViewerASCIISynchronizedPrintf() 286e5c89e4eSSatish Balay @*/ 2877087cfbeSBarry Smith PetscErrorCode PetscSynchronizedPrintf(MPI_Comm comm,const char format[],...) 288e5c89e4eSSatish Balay { 289e5c89e4eSSatish Balay PetscErrorCode ierr; 290e5c89e4eSSatish Balay PetscMPIInt rank; 291e5c89e4eSSatish Balay 292e5c89e4eSSatish Balay PetscFunctionBegin; 293e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 294e5c89e4eSSatish Balay 295e5c89e4eSSatish Balay /* First processor prints immediately to stdout */ 296e5c89e4eSSatish Balay if (!rank) { 297e5c89e4eSSatish Balay va_list Argp; 298e5c89e4eSSatish Balay va_start(Argp,format); 2991179db26SBarry Smith ierr = (*PetscVFPrintf)(PETSC_STDOUT,format,Argp);CHKERRQ(ierr); 300e5c89e4eSSatish Balay if (petsc_history) { 301cdc7d174SSatish Balay va_start(Argp,format); 3021179db26SBarry Smith ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr); 303e5c89e4eSSatish Balay } 304e5c89e4eSSatish Balay va_end(Argp); 305e5c89e4eSSatish Balay } else { /* other processors add to local queue */ 306e5c89e4eSSatish Balay va_list Argp; 307e5c89e4eSSatish Balay PrintfQueue next; 308c9a19010SBarry Smith size_t fullLength = 8191; 309e5c89e4eSSatish Balay 310e5c89e4eSSatish Balay ierr = PetscNew(struct _PrintfQueue,&next);CHKERRQ(ierr); 311e5c89e4eSSatish Balay if (queue) {queue->next = next; queue = next; queue->next = 0;} 312e5c89e4eSSatish Balay else {queuebase = queue = next;} 313e5c89e4eSSatish Balay queuelength++; 3142d609e63SMatthew Knepley next->size = -1; 3159ad23270SJed Brown while((PetscInt)fullLength >= next->size) { 3162d609e63SMatthew Knepley next->size = fullLength+1; 3172d609e63SMatthew Knepley ierr = PetscMalloc(next->size * sizeof(char), &next->string);CHKERRQ(ierr); 318e5c89e4eSSatish Balay va_start(Argp,format); 3192d609e63SMatthew Knepley ierr = PetscMemzero(next->string,next->size);CHKERRQ(ierr); 3202d609e63SMatthew Knepley ierr = PetscVSNPrintf(next->string,next->size,format, &fullLength,Argp);CHKERRQ(ierr); 321e5c89e4eSSatish Balay va_end(Argp); 322e5c89e4eSSatish Balay } 3232d609e63SMatthew Knepley } 324e5c89e4eSSatish Balay 325e5c89e4eSSatish Balay PetscFunctionReturn(0); 326e5c89e4eSSatish Balay } 327e5c89e4eSSatish Balay 328e5c89e4eSSatish Balay #undef __FUNCT__ 329e5c89e4eSSatish Balay #define __FUNCT__ "PetscSynchronizedFPrintf" 330e5c89e4eSSatish Balay /*@C 331e5c89e4eSSatish Balay PetscSynchronizedFPrintf - Prints synchronized output to the specified file from 332e5c89e4eSSatish Balay several processors. Output of the first processor is followed by that of the 333e5c89e4eSSatish Balay second, etc. 334e5c89e4eSSatish Balay 335e5c89e4eSSatish Balay Not Collective 336e5c89e4eSSatish Balay 337e5c89e4eSSatish Balay Input Parameters: 338e5c89e4eSSatish Balay + comm - the communicator 339e5c89e4eSSatish Balay . fd - the file pointer 340e5c89e4eSSatish Balay - format - the usual printf() format string 341e5c89e4eSSatish Balay 342e5c89e4eSSatish Balay Level: intermediate 343e5c89e4eSSatish Balay 344e5c89e4eSSatish Balay Notes: 345e5c89e4eSSatish Balay REQUIRES a intervening call to PetscSynchronizedFlush() for the information 346e5c89e4eSSatish Balay from all the processors to be printed. 347e5c89e4eSSatish Balay 348e5c89e4eSSatish Balay .seealso: PetscSynchronizedPrintf(), PetscSynchronizedFlush(), PetscFPrintf(), 349e5c89e4eSSatish Balay PetscFOpen(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIPrintf() 350e5c89e4eSSatish Balay 351e5c89e4eSSatish Balay @*/ 3527087cfbeSBarry Smith PetscErrorCode PetscSynchronizedFPrintf(MPI_Comm comm,FILE* fp,const char format[],...) 353e5c89e4eSSatish Balay { 354e5c89e4eSSatish Balay PetscErrorCode ierr; 355e5c89e4eSSatish Balay PetscMPIInt rank; 356e5c89e4eSSatish Balay 357e5c89e4eSSatish Balay PetscFunctionBegin; 358e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 359e5c89e4eSSatish Balay 360e5c89e4eSSatish Balay /* First processor prints immediately to fp */ 361e5c89e4eSSatish Balay if (!rank) { 362e5c89e4eSSatish Balay va_list Argp; 363e5c89e4eSSatish Balay va_start(Argp,format); 3641179db26SBarry Smith ierr = (*PetscVFPrintf)(fp,format,Argp);CHKERRQ(ierr); 365e5c89e4eSSatish Balay queuefile = fp; 366cdc7d174SSatish Balay if (petsc_history && (fp !=petsc_history)) { 367cdc7d174SSatish Balay va_start(Argp,format); 3681179db26SBarry Smith ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr); 369e5c89e4eSSatish Balay } 370e5c89e4eSSatish Balay va_end(Argp); 371e5c89e4eSSatish Balay } else { /* other processors add to local queue */ 372e5c89e4eSSatish Balay va_list Argp; 373e5c89e4eSSatish Balay PrintfQueue next; 374c9a19010SBarry Smith size_t fullLength = 8191; 375e5c89e4eSSatish Balay ierr = PetscNew(struct _PrintfQueue,&next);CHKERRQ(ierr); 376e5c89e4eSSatish Balay if (queue) {queue->next = next; queue = next; queue->next = 0;} 377e5c89e4eSSatish Balay else {queuebase = queue = next;} 378e5c89e4eSSatish Balay queuelength++; 3792d609e63SMatthew Knepley next->size = -1; 3809ad23270SJed Brown while((PetscInt)fullLength >= next->size) { 3812d609e63SMatthew Knepley next->size = fullLength+1; 3822d609e63SMatthew Knepley ierr = PetscMalloc(next->size * sizeof(char), &next->string);CHKERRQ(ierr); 383e5c89e4eSSatish Balay va_start(Argp,format); 3842d609e63SMatthew Knepley ierr = PetscMemzero(next->string,next->size);CHKERRQ(ierr); 3852d609e63SMatthew Knepley ierr = PetscVSNPrintf(next->string,next->size,format,&fullLength,Argp);CHKERRQ(ierr); 386e5c89e4eSSatish Balay va_end(Argp); 387e5c89e4eSSatish Balay } 3882d609e63SMatthew Knepley } 389e5c89e4eSSatish Balay PetscFunctionReturn(0); 390e5c89e4eSSatish Balay } 391e5c89e4eSSatish Balay 392e5c89e4eSSatish Balay #undef __FUNCT__ 393e5c89e4eSSatish Balay #define __FUNCT__ "PetscSynchronizedFlush" 394e30d2299SSatish Balay /*@ 395e5c89e4eSSatish Balay PetscSynchronizedFlush - Flushes to the screen output from all processors 396e5c89e4eSSatish Balay involved in previous PetscSynchronizedPrintf() calls. 397e5c89e4eSSatish Balay 398e5c89e4eSSatish Balay Collective on MPI_Comm 399e5c89e4eSSatish Balay 400e5c89e4eSSatish Balay Input Parameters: 401e5c89e4eSSatish Balay . comm - the communicator 402e5c89e4eSSatish Balay 403e5c89e4eSSatish Balay Level: intermediate 404e5c89e4eSSatish Balay 405e5c89e4eSSatish Balay Notes: 406e5c89e4eSSatish Balay Usage of PetscSynchronizedPrintf() and PetscSynchronizedFPrintf() with 407e5c89e4eSSatish Balay different MPI communicators REQUIRES an intervening call to PetscSynchronizedFlush(). 408e5c89e4eSSatish Balay 409e5c89e4eSSatish Balay .seealso: PetscSynchronizedPrintf(), PetscFPrintf(), PetscPrintf(), PetscViewerASCIIPrintf(), 410e5c89e4eSSatish Balay PetscViewerASCIISynchronizedPrintf() 411e5c89e4eSSatish Balay @*/ 4127087cfbeSBarry Smith PetscErrorCode PetscSynchronizedFlush(MPI_Comm comm) 413e5c89e4eSSatish Balay { 414e5c89e4eSSatish Balay PetscErrorCode ierr; 415bf3b0749SBarry Smith PetscMPIInt rank,size,tag,i,j,n,dummy = 0; 4162d609e63SMatthew Knepley char *message; 417e5c89e4eSSatish Balay MPI_Status status; 418e5c89e4eSSatish Balay FILE *fd; 419e5c89e4eSSatish Balay 420e5c89e4eSSatish Balay PetscFunctionBegin; 421e5c89e4eSSatish Balay ierr = PetscCommDuplicate(comm,&comm,&tag);CHKERRQ(ierr); 422e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 423e5c89e4eSSatish Balay ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 424e5c89e4eSSatish Balay 425e5c89e4eSSatish Balay /* First processor waits for messages from all other processors */ 426e5c89e4eSSatish Balay if (!rank) { 427e5c89e4eSSatish Balay if (queuefile) { 428e5c89e4eSSatish Balay fd = queuefile; 429e5c89e4eSSatish Balay } else { 430e5c89e4eSSatish Balay fd = PETSC_STDOUT; 431e5c89e4eSSatish Balay } 432e5c89e4eSSatish Balay for (i=1; i<size; i++) { 4339f73f8ecSBarry Smith /* to prevent a flood of messages to process zero, request each message separately */ 4349f73f8ecSBarry Smith ierr = MPI_Send(&dummy,1,MPI_INT,i,tag,comm);CHKERRQ(ierr); 435e5c89e4eSSatish Balay ierr = MPI_Recv(&n,1,MPI_INT,i,tag,comm,&status);CHKERRQ(ierr); 436e5c89e4eSSatish Balay for (j=0; j<n; j++) { 4379f73f8ecSBarry Smith PetscMPIInt size; 4382d609e63SMatthew Knepley 4392d609e63SMatthew Knepley ierr = MPI_Recv(&size,1,MPI_INT,i,tag,comm,&status);CHKERRQ(ierr); 4402d609e63SMatthew Knepley ierr = PetscMalloc(size * sizeof(char), &message);CHKERRQ(ierr); 4412d609e63SMatthew Knepley ierr = MPI_Recv(message,size,MPI_CHAR,i,tag,comm,&status);CHKERRQ(ierr); 442e5c89e4eSSatish Balay ierr = PetscFPrintf(comm,fd,"%s",message); 4432d609e63SMatthew Knepley ierr = PetscFree(message);CHKERRQ(ierr); 444e5c89e4eSSatish Balay } 445e5c89e4eSSatish Balay } 446e5c89e4eSSatish Balay queuefile = PETSC_NULL; 447e5c89e4eSSatish Balay } else { /* other processors send queue to processor 0 */ 448e5c89e4eSSatish Balay PrintfQueue next = queuebase,previous; 449e5c89e4eSSatish Balay 450b3ef9d35SBarry Smith ierr = MPI_Recv(&dummy,1,MPI_INT,0,tag,comm,&status);CHKERRQ(ierr); 451e5c89e4eSSatish Balay ierr = MPI_Send(&queuelength,1,MPI_INT,0,tag,comm);CHKERRQ(ierr); 452e5c89e4eSSatish Balay for (i=0; i<queuelength; i++) { 4532d609e63SMatthew Knepley ierr = MPI_Send(&next->size,1,MPI_INT,0,tag,comm);CHKERRQ(ierr); 4542d609e63SMatthew Knepley ierr = MPI_Send(next->string,next->size,MPI_CHAR,0,tag,comm);CHKERRQ(ierr); 455e5c89e4eSSatish Balay previous = next; 456e5c89e4eSSatish Balay next = next->next; 4572d609e63SMatthew Knepley ierr = PetscFree(previous->string);CHKERRQ(ierr); 458e5c89e4eSSatish Balay ierr = PetscFree(previous);CHKERRQ(ierr); 459e5c89e4eSSatish Balay } 460e5c89e4eSSatish Balay queue = 0; 461e5c89e4eSSatish Balay queuelength = 0; 462e5c89e4eSSatish Balay } 463e5c89e4eSSatish Balay ierr = PetscCommDestroy(&comm);CHKERRQ(ierr); 464e5c89e4eSSatish Balay PetscFunctionReturn(0); 465e5c89e4eSSatish Balay } 466e5c89e4eSSatish Balay 467e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------------------*/ 468e5c89e4eSSatish Balay 469e5c89e4eSSatish Balay #undef __FUNCT__ 470e5c89e4eSSatish Balay #define __FUNCT__ "PetscFPrintf" 471e5c89e4eSSatish Balay /*@C 472e5c89e4eSSatish Balay PetscFPrintf - Prints to a file, only from the first 473e5c89e4eSSatish Balay processor in the communicator. 474e5c89e4eSSatish Balay 475e5c89e4eSSatish Balay Not Collective 476e5c89e4eSSatish Balay 477e5c89e4eSSatish Balay Input Parameters: 478e5c89e4eSSatish Balay + comm - the communicator 479e5c89e4eSSatish Balay . fd - the file pointer 480e5c89e4eSSatish Balay - format - the usual printf() format string 481e5c89e4eSSatish Balay 482e5c89e4eSSatish Balay Level: intermediate 483e5c89e4eSSatish Balay 484e5c89e4eSSatish Balay Fortran Note: 485e5c89e4eSSatish Balay This routine is not supported in Fortran. 486e5c89e4eSSatish Balay 487e5c89e4eSSatish Balay Concepts: printing^in parallel 488e5c89e4eSSatish Balay Concepts: printf^in parallel 489e5c89e4eSSatish Balay 490e5c89e4eSSatish Balay .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(), 491e5c89e4eSSatish Balay PetscViewerASCIISynchronizedPrintf(), PetscSynchronizedFlush() 492e5c89e4eSSatish Balay @*/ 4937087cfbeSBarry Smith PetscErrorCode PetscFPrintf(MPI_Comm comm,FILE* fd,const char format[],...) 494e5c89e4eSSatish Balay { 495e5c89e4eSSatish Balay PetscErrorCode ierr; 496e5c89e4eSSatish Balay PetscMPIInt rank; 497e5c89e4eSSatish Balay 498e5c89e4eSSatish Balay PetscFunctionBegin; 499e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 500e5c89e4eSSatish Balay if (!rank) { 501e5c89e4eSSatish Balay va_list Argp; 502e5c89e4eSSatish Balay va_start(Argp,format); 5031179db26SBarry Smith ierr = (*PetscVFPrintf)(fd,format,Argp);CHKERRQ(ierr); 504cdc7d174SSatish Balay if (petsc_history && (fd !=petsc_history)) { 505cdc7d174SSatish Balay va_start(Argp,format); 5061179db26SBarry Smith ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr); 507e5c89e4eSSatish Balay } 508e5c89e4eSSatish Balay va_end(Argp); 509e5c89e4eSSatish Balay } 510e5c89e4eSSatish Balay PetscFunctionReturn(0); 511e5c89e4eSSatish Balay } 512e5c89e4eSSatish Balay 513e5c89e4eSSatish Balay #undef __FUNCT__ 514e5c89e4eSSatish Balay #define __FUNCT__ "PetscPrintf" 515e5c89e4eSSatish Balay /*@C 516e5c89e4eSSatish Balay PetscPrintf - Prints to standard out, only from the first 517e5c89e4eSSatish Balay processor in the communicator. 518e5c89e4eSSatish Balay 519e5c89e4eSSatish Balay Not Collective 520e5c89e4eSSatish Balay 521e5c89e4eSSatish Balay Input Parameters: 522e5c89e4eSSatish Balay + comm - the communicator 523e5c89e4eSSatish Balay - format - the usual printf() format string 524e5c89e4eSSatish Balay 525e5c89e4eSSatish Balay Level: intermediate 526e5c89e4eSSatish Balay 527e5c89e4eSSatish Balay Fortran Note: 528d60d70ccSBarry Smith The call sequence is PetscPrintf(MPI_Comm, character(*), PetscErrorCode ierr) from Fortran. 529e5c89e4eSSatish Balay That is, you can only pass a single character string from Fortran. 530e5c89e4eSSatish Balay 5318884cb1fSJed Brown Notes: The %A format specifier is special. It assumes an argument of type PetscReal 5328884cb1fSJed Brown and is replaced with %G unless the absolute value is < 1.e-12 when it is replaced 5338884cb1fSJed Brown with "< 1.e-12" (1.e-6 for single precision). 534e5c89e4eSSatish Balay 535e5c89e4eSSatish Balay Concepts: printing^in parallel 536e5c89e4eSSatish Balay Concepts: printf^in parallel 537e5c89e4eSSatish Balay 538e5c89e4eSSatish Balay .seealso: PetscFPrintf(), PetscSynchronizedPrintf() 539e5c89e4eSSatish Balay @*/ 5407087cfbeSBarry Smith PetscErrorCode PetscPrintf(MPI_Comm comm,const char format[],...) 541e5c89e4eSSatish Balay { 542e5c89e4eSSatish Balay PetscErrorCode ierr; 543e5c89e4eSSatish Balay PetscMPIInt rank; 544e5c89e4eSSatish Balay size_t len; 545e5c89e4eSSatish Balay char *nformat,*sub1,*sub2; 546e5c89e4eSSatish Balay PetscReal value; 547e5c89e4eSSatish Balay 548e5c89e4eSSatish Balay PetscFunctionBegin; 549e5c89e4eSSatish Balay if (!comm) comm = PETSC_COMM_WORLD; 550e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 551e5c89e4eSSatish Balay if (!rank) { 552e5c89e4eSSatish Balay va_list Argp; 553e5c89e4eSSatish Balay va_start(Argp,format); 554e5c89e4eSSatish Balay 555e5c89e4eSSatish Balay ierr = PetscStrstr(format,"%A",&sub1);CHKERRQ(ierr); 556e5c89e4eSSatish Balay if (sub1) { 557e5c89e4eSSatish Balay ierr = PetscStrstr(format,"%",&sub2);CHKERRQ(ierr); 558e32f2f54SBarry Smith if (sub1 != sub2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"%%A format must be first in format string"); 559e5c89e4eSSatish Balay ierr = PetscStrlen(format,&len);CHKERRQ(ierr); 560e5c89e4eSSatish Balay ierr = PetscMalloc((len+16)*sizeof(char),&nformat);CHKERRQ(ierr); 561e5c89e4eSSatish Balay ierr = PetscStrcpy(nformat,format);CHKERRQ(ierr); 562e5c89e4eSSatish Balay ierr = PetscStrstr(nformat,"%",&sub2);CHKERRQ(ierr); 563e5c89e4eSSatish Balay sub2[0] = 0; 56492eff512SVictor Minden value = va_arg(Argp,double); 5658884cb1fSJed Brown #if defined(PETSC_USE_SCALAR_SINGLE) 5668884cb1fSJed Brown if (PetscAbsReal(value) < 1.e-6) { 5678884cb1fSJed Brown ierr = PetscStrcat(nformat,"< 1.e-6");CHKERRQ(ierr); 5688884cb1fSJed Brown #else 569e5c89e4eSSatish Balay if (PetscAbsReal(value) < 1.e-12) { 570e5c89e4eSSatish Balay ierr = PetscStrcat(nformat,"< 1.e-12");CHKERRQ(ierr); 5718884cb1fSJed Brown #endif 572e5c89e4eSSatish Balay } else { 5738884cb1fSJed Brown ierr = PetscStrcat(nformat,"%G");CHKERRQ(ierr); 574e5c89e4eSSatish Balay va_end(Argp); 575e5c89e4eSSatish Balay va_start(Argp,format); 576e5c89e4eSSatish Balay } 577e5c89e4eSSatish Balay ierr = PetscStrcat(nformat,sub1+2);CHKERRQ(ierr); 578e5c89e4eSSatish Balay } else { 579e5c89e4eSSatish Balay nformat = (char*)format; 580e5c89e4eSSatish Balay } 5811179db26SBarry Smith ierr = (*PetscVFPrintf)(PETSC_STDOUT,nformat,Argp);CHKERRQ(ierr); 582e5c89e4eSSatish Balay if (petsc_history) { 583cdc7d174SSatish Balay va_start(Argp,format); 5841179db26SBarry Smith ierr = (*PetscVFPrintf)(petsc_history,nformat,Argp);CHKERRQ(ierr); 585e5c89e4eSSatish Balay } 586e5c89e4eSSatish Balay va_end(Argp); 587e5c89e4eSSatish Balay if (sub1) {ierr = PetscFree(nformat);CHKERRQ(ierr);} 588e5c89e4eSSatish Balay } 589e5c89e4eSSatish Balay PetscFunctionReturn(0); 590e5c89e4eSSatish Balay } 591e5c89e4eSSatish Balay 592e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------------------*/ 593e5c89e4eSSatish Balay #undef __FUNCT__ 594e5c89e4eSSatish Balay #define __FUNCT__ "PetscHelpPrintfDefault" 595c9a19010SBarry Smith /*@C 596c9a19010SBarry Smith PetscHelpPrintf - All PETSc help messages are passing through this function. You can change how help messages are printed by 597c9a19010SBarry Smith replacinng it with something that does not simply write to a stdout. 598c9a19010SBarry Smith 599c9a19010SBarry Smith To use, write your own function for example, 600c9a19010SBarry Smith $PetscErrorCode mypetschelpprintf(MPI_Comm comm,const char format[],....) 601c9a19010SBarry Smith ${ 602c9a19010SBarry Smith $ PetscFunctionReturn(0); 603c9a19010SBarry Smith $} 604c9a19010SBarry Smith then before the call to PetscInitialize() do the assignment 605c9a19010SBarry Smith $ PetscHelpPrintf = mypetschelpprintf; 606c9a19010SBarry Smith 607c9a19010SBarry Smith Note: the default routine used is called PetscHelpPrintfDefault(). 608c9a19010SBarry Smith 609c9a19010SBarry Smith Level: developer 610c9a19010SBarry Smith 611c9a19010SBarry Smith .seealso: PetscVSNPrintf(), PetscVFPrintf(), PetscErrorPrintf() 612c9a19010SBarry Smith @*/ 6137087cfbeSBarry Smith PetscErrorCode PetscHelpPrintfDefault(MPI_Comm comm,const char format[],...) 614e5c89e4eSSatish Balay { 615e5c89e4eSSatish Balay PetscErrorCode ierr; 616e5c89e4eSSatish Balay PetscMPIInt rank; 617e5c89e4eSSatish Balay 618e5c89e4eSSatish Balay PetscFunctionBegin; 619e5c89e4eSSatish Balay if (!comm) comm = PETSC_COMM_WORLD; 620e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 621e5c89e4eSSatish Balay if (!rank) { 622e5c89e4eSSatish Balay va_list Argp; 623e5c89e4eSSatish Balay va_start(Argp,format); 6241179db26SBarry Smith ierr = (*PetscVFPrintf)(PETSC_STDOUT,format,Argp);CHKERRQ(ierr); 625e5c89e4eSSatish Balay if (petsc_history) { 626cdc7d174SSatish Balay va_start(Argp,format); 6271179db26SBarry Smith ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr); 628e5c89e4eSSatish Balay } 629e5c89e4eSSatish Balay va_end(Argp); 630e5c89e4eSSatish Balay } 631e5c89e4eSSatish Balay PetscFunctionReturn(0); 632e5c89e4eSSatish Balay } 633e5c89e4eSSatish Balay 634e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------------------*/ 635e5c89e4eSSatish Balay 636e5c89e4eSSatish Balay 637e5c89e4eSSatish Balay #undef __FUNCT__ 638e5c89e4eSSatish Balay #define __FUNCT__ "PetscSynchronizedFGets" 639e5c89e4eSSatish Balay /*@C 640e5c89e4eSSatish Balay PetscSynchronizedFGets - Several processors all get the same line from a file. 641e5c89e4eSSatish Balay 642e5c89e4eSSatish Balay Collective on MPI_Comm 643e5c89e4eSSatish Balay 644e5c89e4eSSatish Balay Input Parameters: 645e5c89e4eSSatish Balay + comm - the communicator 646e5c89e4eSSatish Balay . fd - the file pointer 647e5c89e4eSSatish Balay - len - the length of the output buffer 648e5c89e4eSSatish Balay 649e5c89e4eSSatish Balay Output Parameter: 650e5c89e4eSSatish Balay . string - the line read from the file 651e5c89e4eSSatish Balay 652e5c89e4eSSatish Balay Level: intermediate 653e5c89e4eSSatish Balay 654e5c89e4eSSatish Balay .seealso: PetscSynchronizedPrintf(), PetscSynchronizedFlush(), 655e5c89e4eSSatish Balay PetscFOpen(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIPrintf() 656e5c89e4eSSatish Balay 657e5c89e4eSSatish Balay @*/ 6587087cfbeSBarry Smith PetscErrorCode PetscSynchronizedFGets(MPI_Comm comm,FILE* fp,size_t len,char string[]) 659e5c89e4eSSatish Balay { 660e5c89e4eSSatish Balay PetscErrorCode ierr; 661e5c89e4eSSatish Balay PetscMPIInt rank; 662e5c89e4eSSatish Balay 663e5c89e4eSSatish Balay PetscFunctionBegin; 664e5c89e4eSSatish Balay ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 665e5c89e4eSSatish Balay 666e5c89e4eSSatish Balay if (!rank) { 66718d3aa3fSBarry Smith (void) fgets(string,len,fp); /* Not very useful error behavior, but what is desired behavior for attempt to read at EOF? */ 668e5c89e4eSSatish Balay } 669e5c89e4eSSatish Balay ierr = MPI_Bcast(string,len,MPI_BYTE,0,comm);CHKERRQ(ierr); 670e5c89e4eSSatish Balay PetscFunctionReturn(0); 671e5c89e4eSSatish Balay } 672238ccf28SShri Abhyankar 673238ccf28SShri Abhyankar #if defined(PETSC_HAVE_MATLAB_ENGINE) 674*c6db04a5SJed Brown #include <mex.h> 675238ccf28SShri Abhyankar #undef __FUNCT__ 676238ccf28SShri Abhyankar #define __FUNCT__ "PetscVFPrintf_Matlab" 6777087cfbeSBarry Smith PetscErrorCode PetscVFPrintf_Matlab(FILE *fd,const char format[],va_list Argp) 678238ccf28SShri Abhyankar { 679238ccf28SShri Abhyankar PetscErrorCode ierr; 680238ccf28SShri Abhyankar 681238ccf28SShri Abhyankar PetscFunctionBegin; 682238ccf28SShri Abhyankar if (fd != stdout && fd != stderr) { /* handle regular files */ 683238ccf28SShri Abhyankar ierr = PetscVFPrintfDefault(fd,format,Argp); CHKERRQ(ierr); 684238ccf28SShri Abhyankar } else { 685238ccf28SShri Abhyankar size_t len=8*1024,length; 686238ccf28SShri Abhyankar char buf[len]; 687238ccf28SShri Abhyankar 688238ccf28SShri Abhyankar ierr = PetscVSNPrintf(buf,len,format,&length,Argp);CHKERRQ(ierr); 689df413903SBarry Smith mexPrintf("%s",buf); 690238ccf28SShri Abhyankar } 691238ccf28SShri Abhyankar PetscFunctionReturn(0); 692238ccf28SShri Abhyankar } 693238ccf28SShri Abhyankar #endif 694