xref: /petsc/src/sys/fileio/mprint.c (revision 6180deefa357988de3a2db558b96fbf5d4bb9b58)
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;
2800298fd71SBarry 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;
313*6180deefSBarry Smith   if (comm == MPI_COMM_NULL) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Called with MPI_COMM_NULL, likely PetscObjectComm() failed");
314e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
315e5c89e4eSSatish Balay 
316e5c89e4eSSatish Balay   /* First processor prints immediately to stdout */
317e5c89e4eSSatish Balay   if (!rank) {
318e5c89e4eSSatish Balay     va_list Argp;
319e5c89e4eSSatish Balay     va_start(Argp,format);
3201179db26SBarry Smith     ierr = (*PetscVFPrintf)(PETSC_STDOUT,format,Argp);CHKERRQ(ierr);
321e5c89e4eSSatish Balay     if (petsc_history) {
322cdc7d174SSatish Balay       va_start(Argp,format);
3231179db26SBarry Smith       ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr);
324e5c89e4eSSatish Balay     }
325e5c89e4eSSatish Balay     va_end(Argp);
326e5c89e4eSSatish Balay   } else { /* other processors add to local queue */
327e5c89e4eSSatish Balay     va_list     Argp;
328e5c89e4eSSatish Balay     PrintfQueue next;
329c9a19010SBarry Smith     size_t      fullLength = 8191;
330e5c89e4eSSatish Balay 
331e5c89e4eSSatish Balay     ierr = PetscNew(struct _PrintfQueue,&next);CHKERRQ(ierr);
332a297a907SKarl Rupp     if (petsc_printfqueue) {
333a297a907SKarl Rupp       petsc_printfqueue->next = next;
334a297a907SKarl Rupp       petsc_printfqueue       = next;
335a297a907SKarl Rupp       petsc_printfqueue->next = 0;
336a297a907SKarl Rupp     } 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;
341a297a907SKarl Rupp 
3422d609e63SMatthew Knepley       ierr = PetscMalloc(next->size * sizeof(char), &next->string);CHKERRQ(ierr);
343e5c89e4eSSatish Balay       va_start(Argp,format);
3442d609e63SMatthew Knepley       ierr = PetscMemzero(next->string,next->size);CHKERRQ(ierr);
3452d609e63SMatthew Knepley       ierr = PetscVSNPrintf(next->string,next->size,format, &fullLength,Argp);CHKERRQ(ierr);
346e5c89e4eSSatish Balay       va_end(Argp);
347e5c89e4eSSatish Balay     }
3482d609e63SMatthew Knepley   }
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;
382*6180deefSBarry Smith   if (comm == MPI_COMM_NULL) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Called with MPI_COMM_NULL, likely PetscObjectComm() failed");
383e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
384e5c89e4eSSatish Balay 
385e5c89e4eSSatish Balay   /* First processor prints immediately to fp */
386e5c89e4eSSatish Balay   if (!rank) {
387e5c89e4eSSatish Balay     va_list Argp;
388e5c89e4eSSatish Balay     va_start(Argp,format);
3891179db26SBarry Smith     ierr = (*PetscVFPrintf)(fp,format,Argp);CHKERRQ(ierr);
390a297a907SKarl Rupp 
391d30b0576SJed Brown     petsc_printfqueuefile = fp;
392cdc7d174SSatish Balay     if (petsc_history && (fp !=petsc_history)) {
393cdc7d174SSatish Balay       va_start(Argp,format);
3941179db26SBarry Smith       ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr);
395e5c89e4eSSatish Balay     }
396e5c89e4eSSatish Balay     va_end(Argp);
397e5c89e4eSSatish Balay   } else { /* other processors add to local queue */
398e5c89e4eSSatish Balay     va_list     Argp;
399e5c89e4eSSatish Balay     PrintfQueue next;
400c9a19010SBarry Smith     size_t      fullLength = 8191;
401e5c89e4eSSatish Balay     ierr = PetscNew(struct _PrintfQueue,&next);CHKERRQ(ierr);
402a297a907SKarl Rupp     if (petsc_printfqueue) {
403a297a907SKarl Rupp       petsc_printfqueue->next = next;
404a297a907SKarl Rupp       petsc_printfqueue       = next;
405a297a907SKarl Rupp       petsc_printfqueue->next = 0;
406a297a907SKarl Rupp     } else petsc_printfqueuebase = petsc_printfqueue = next;
407d30b0576SJed Brown     petsc_printfqueuelength++;
4082d609e63SMatthew Knepley     next->size = -1;
4099ad23270SJed Brown     while ((PetscInt)fullLength >= next->size) {
4102d609e63SMatthew Knepley       next->size = fullLength+1;
4112d609e63SMatthew Knepley       ierr = PetscMalloc(next->size * sizeof(char), &next->string);CHKERRQ(ierr);
412e5c89e4eSSatish Balay       va_start(Argp,format);
4132d609e63SMatthew Knepley       ierr = PetscMemzero(next->string,next->size);CHKERRQ(ierr);
4142d609e63SMatthew Knepley       ierr = PetscVSNPrintf(next->string,next->size,format,&fullLength,Argp);CHKERRQ(ierr);
415e5c89e4eSSatish Balay       va_end(Argp);
416e5c89e4eSSatish Balay     }
4172d609e63SMatthew Knepley   }
418e5c89e4eSSatish Balay   PetscFunctionReturn(0);
419e5c89e4eSSatish Balay }
420e5c89e4eSSatish Balay 
421e5c89e4eSSatish Balay #undef __FUNCT__
422e5c89e4eSSatish Balay #define __FUNCT__ "PetscSynchronizedFlush"
423e30d2299SSatish Balay /*@
424e5c89e4eSSatish Balay     PetscSynchronizedFlush - Flushes to the screen output from all processors
425e5c89e4eSSatish Balay     involved in previous PetscSynchronizedPrintf() calls.
426e5c89e4eSSatish Balay 
427e5c89e4eSSatish Balay     Collective on MPI_Comm
428e5c89e4eSSatish Balay 
429e5c89e4eSSatish Balay     Input Parameters:
430e5c89e4eSSatish Balay .   comm - the communicator
431e5c89e4eSSatish Balay 
432e5c89e4eSSatish Balay     Level: intermediate
433e5c89e4eSSatish Balay 
434e5c89e4eSSatish Balay     Notes:
435e5c89e4eSSatish Balay     Usage of PetscSynchronizedPrintf() and PetscSynchronizedFPrintf() with
436e5c89e4eSSatish Balay     different MPI communicators REQUIRES an intervening call to PetscSynchronizedFlush().
437e5c89e4eSSatish Balay 
438e5c89e4eSSatish Balay .seealso: PetscSynchronizedPrintf(), PetscFPrintf(), PetscPrintf(), PetscViewerASCIIPrintf(),
439e5c89e4eSSatish Balay           PetscViewerASCIISynchronizedPrintf()
440e5c89e4eSSatish Balay @*/
4417087cfbeSBarry Smith PetscErrorCode  PetscSynchronizedFlush(MPI_Comm comm)
442e5c89e4eSSatish Balay {
443e5c89e4eSSatish Balay   PetscErrorCode ierr;
444bf3b0749SBarry Smith   PetscMPIInt    rank,size,tag,i,j,n,dummy = 0;
4452d609e63SMatthew Knepley   char          *message;
446e5c89e4eSSatish Balay   MPI_Status     status;
447e5c89e4eSSatish Balay   FILE           *fd;
448e5c89e4eSSatish Balay 
449e5c89e4eSSatish Balay   PetscFunctionBegin;
450e5c89e4eSSatish Balay   ierr = PetscCommDuplicate(comm,&comm,&tag);CHKERRQ(ierr);
451e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
452e5c89e4eSSatish Balay   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
453e5c89e4eSSatish Balay 
454e5c89e4eSSatish Balay   /* First processor waits for messages from all other processors */
455e5c89e4eSSatish Balay   if (!rank) {
456a297a907SKarl Rupp     if (petsc_printfqueuefile) fd = petsc_printfqueuefile;
457a297a907SKarl Rupp     else fd = PETSC_STDOUT;
458e5c89e4eSSatish Balay     for (i=1; i<size; i++) {
4599f73f8ecSBarry Smith       /* to prevent a flood of messages to process zero, request each message separately */
4609f73f8ecSBarry Smith       ierr = MPI_Send(&dummy,1,MPI_INT,i,tag,comm);CHKERRQ(ierr);
461e5c89e4eSSatish Balay       ierr = MPI_Recv(&n,1,MPI_INT,i,tag,comm,&status);CHKERRQ(ierr);
462e5c89e4eSSatish Balay       for (j=0; j<n; j++) {
4639f73f8ecSBarry Smith         PetscMPIInt size;
4642d609e63SMatthew Knepley 
4652d609e63SMatthew Knepley         ierr = MPI_Recv(&size,1,MPI_INT,i,tag,comm,&status);CHKERRQ(ierr);
4662d609e63SMatthew Knepley         ierr = PetscMalloc(size * sizeof(char), &message);CHKERRQ(ierr);
4672d609e63SMatthew Knepley         ierr = MPI_Recv(message,size,MPI_CHAR,i,tag,comm,&status);CHKERRQ(ierr);
4683bf036e2SBarry Smith         ierr = PetscFPrintf(comm,fd,"%s",message);CHKERRQ(ierr);
4692d609e63SMatthew Knepley         ierr = PetscFree(message);CHKERRQ(ierr);
470e5c89e4eSSatish Balay       }
471e5c89e4eSSatish Balay     }
4720298fd71SBarry Smith     petsc_printfqueuefile = NULL;
473e5c89e4eSSatish Balay   } else { /* other processors send queue to processor 0 */
474d30b0576SJed Brown     PrintfQueue next = petsc_printfqueuebase,previous;
475e5c89e4eSSatish Balay 
476b3ef9d35SBarry Smith     ierr = MPI_Recv(&dummy,1,MPI_INT,0,tag,comm,&status);CHKERRQ(ierr);
477d30b0576SJed Brown     ierr = MPI_Send(&petsc_printfqueuelength,1,MPI_INT,0,tag,comm);CHKERRQ(ierr);
478d30b0576SJed Brown     for (i=0; i<petsc_printfqueuelength; i++) {
4792d609e63SMatthew Knepley       ierr     = MPI_Send(&next->size,1,MPI_INT,0,tag,comm);CHKERRQ(ierr);
4802d609e63SMatthew Knepley       ierr     = MPI_Send(next->string,next->size,MPI_CHAR,0,tag,comm);CHKERRQ(ierr);
481e5c89e4eSSatish Balay       previous = next;
482e5c89e4eSSatish Balay       next     = next->next;
4832d609e63SMatthew Knepley       ierr     = PetscFree(previous->string);CHKERRQ(ierr);
484e5c89e4eSSatish Balay       ierr     = PetscFree(previous);CHKERRQ(ierr);
485e5c89e4eSSatish Balay     }
486d30b0576SJed Brown     petsc_printfqueue       = 0;
487d30b0576SJed Brown     petsc_printfqueuelength = 0;
488e5c89e4eSSatish Balay   }
489e5c89e4eSSatish Balay   ierr = PetscCommDestroy(&comm);CHKERRQ(ierr);
490e5c89e4eSSatish Balay   PetscFunctionReturn(0);
491e5c89e4eSSatish Balay }
492e5c89e4eSSatish Balay 
493e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------------------*/
494e5c89e4eSSatish Balay 
495e5c89e4eSSatish Balay #undef __FUNCT__
496e5c89e4eSSatish Balay #define __FUNCT__ "PetscFPrintf"
497e5c89e4eSSatish Balay /*@C
498e5c89e4eSSatish Balay     PetscFPrintf - Prints to a file, only from the first
499e5c89e4eSSatish Balay     processor in the communicator.
500e5c89e4eSSatish Balay 
501e5c89e4eSSatish Balay     Not Collective
502e5c89e4eSSatish Balay 
503e5c89e4eSSatish Balay     Input Parameters:
504e5c89e4eSSatish Balay +   comm - the communicator
505e5c89e4eSSatish Balay .   fd - the file pointer
506e5c89e4eSSatish Balay -   format - the usual printf() format string
507e5c89e4eSSatish Balay 
508e5c89e4eSSatish Balay     Level: intermediate
509e5c89e4eSSatish Balay 
510e5c89e4eSSatish Balay     Fortran Note:
511e5c89e4eSSatish Balay     This routine is not supported in Fortran.
512e5c89e4eSSatish Balay 
513e5c89e4eSSatish Balay    Concepts: printing^in parallel
514e5c89e4eSSatish Balay    Concepts: printf^in parallel
515e5c89e4eSSatish Balay 
516e5c89e4eSSatish Balay .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(),
517e5c89e4eSSatish Balay           PetscViewerASCIISynchronizedPrintf(), PetscSynchronizedFlush()
518e5c89e4eSSatish Balay @*/
5197087cfbeSBarry Smith PetscErrorCode  PetscFPrintf(MPI_Comm comm,FILE* fd,const char format[],...)
520e5c89e4eSSatish Balay {
521e5c89e4eSSatish Balay   PetscErrorCode ierr;
522e5c89e4eSSatish Balay   PetscMPIInt    rank;
523e5c89e4eSSatish Balay 
524e5c89e4eSSatish Balay   PetscFunctionBegin;
525*6180deefSBarry Smith   if (comm == MPI_COMM_NULL) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Called with MPI_COMM_NULL, likely PetscObjectComm() failed");
526e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
527e5c89e4eSSatish Balay   if (!rank) {
528e5c89e4eSSatish Balay     va_list Argp;
529e5c89e4eSSatish Balay     va_start(Argp,format);
5301179db26SBarry Smith     ierr = (*PetscVFPrintf)(fd,format,Argp);CHKERRQ(ierr);
531cdc7d174SSatish Balay     if (petsc_history && (fd !=petsc_history)) {
532cdc7d174SSatish Balay       va_start(Argp,format);
5331179db26SBarry Smith       ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr);
534e5c89e4eSSatish Balay     }
535e5c89e4eSSatish Balay     va_end(Argp);
536e5c89e4eSSatish Balay   }
537e5c89e4eSSatish Balay   PetscFunctionReturn(0);
538e5c89e4eSSatish Balay }
539e5c89e4eSSatish Balay 
540e5c89e4eSSatish Balay #undef __FUNCT__
541e5c89e4eSSatish Balay #define __FUNCT__ "PetscPrintf"
542e5c89e4eSSatish Balay /*@C
543e5c89e4eSSatish Balay     PetscPrintf - Prints to standard out, only from the first
544eed5747fSBarry Smith     processor in the communicator. Calls from other processes are ignored.
545e5c89e4eSSatish Balay 
546e5c89e4eSSatish Balay     Not Collective
547e5c89e4eSSatish Balay 
548e5c89e4eSSatish Balay     Input Parameters:
549e5c89e4eSSatish Balay +   comm - the communicator
550e5c89e4eSSatish Balay -   format - the usual printf() format string
551e5c89e4eSSatish Balay 
552e5c89e4eSSatish Balay    Level: intermediate
553e5c89e4eSSatish Balay 
554e5c89e4eSSatish Balay     Fortran Note:
555d60d70ccSBarry Smith     The call sequence is PetscPrintf(MPI_Comm, character(*), PetscErrorCode ierr) from Fortran.
556e5c89e4eSSatish Balay     That is, you can only pass a single character string from Fortran.
557e5c89e4eSSatish Balay 
558e5c89e4eSSatish Balay    Concepts: printing^in parallel
559e5c89e4eSSatish Balay    Concepts: printf^in parallel
560e5c89e4eSSatish Balay 
561e5c89e4eSSatish Balay .seealso: PetscFPrintf(), PetscSynchronizedPrintf()
562e5c89e4eSSatish Balay @*/
5637087cfbeSBarry Smith PetscErrorCode  PetscPrintf(MPI_Comm comm,const char format[],...)
564e5c89e4eSSatish Balay {
565e5c89e4eSSatish Balay   PetscErrorCode ierr;
566e5c89e4eSSatish Balay   PetscMPIInt    rank;
567e5c89e4eSSatish Balay 
568e5c89e4eSSatish Balay   PetscFunctionBegin;
569*6180deefSBarry Smith   if (comm == MPI_COMM_NULL) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Called with MPI_COMM_NULL, likely PetscObjectComm() failed");
570e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
571e5c89e4eSSatish Balay   if (!rank) {
572e5c89e4eSSatish Balay     va_list Argp;
573e5c89e4eSSatish Balay     va_start(Argp,format);
574eed5747fSBarry Smith     ierr = (*PetscVFPrintf)(PETSC_STDOUT,format,Argp);CHKERRQ(ierr);
575e5c89e4eSSatish Balay     if (petsc_history) {
576cdc7d174SSatish Balay       va_start(Argp,format);
577eed5747fSBarry Smith       ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr);
578e5c89e4eSSatish Balay     }
579e5c89e4eSSatish Balay     va_end(Argp);
580e5c89e4eSSatish Balay   }
581e5c89e4eSSatish Balay   PetscFunctionReturn(0);
582e5c89e4eSSatish Balay }
583e5c89e4eSSatish Balay 
584e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------------------*/
585e5c89e4eSSatish Balay #undef __FUNCT__
586e5c89e4eSSatish Balay #define __FUNCT__ "PetscHelpPrintfDefault"
587c9a19010SBarry Smith /*@C
588c9a19010SBarry Smith      PetscHelpPrintf -  All PETSc help messages are passing through this function. You can change how help messages are printed by
589c9a19010SBarry Smith         replacinng it  with something that does not simply write to a stdout.
590c9a19010SBarry Smith 
591c9a19010SBarry Smith       To use, write your own function for example,
592c9a19010SBarry Smith $PetscErrorCode mypetschelpprintf(MPI_Comm comm,const char format[],....)
593c9a19010SBarry Smith ${
594c9a19010SBarry Smith $ PetscFunctionReturn(0);
595c9a19010SBarry Smith $}
596c9a19010SBarry Smith then before the call to PetscInitialize() do the assignment
597c9a19010SBarry Smith $    PetscHelpPrintf = mypetschelpprintf;
598c9a19010SBarry Smith 
599c9a19010SBarry Smith   Note: the default routine used is called PetscHelpPrintfDefault().
600c9a19010SBarry Smith 
601c9a19010SBarry Smith   Level:  developer
602c9a19010SBarry Smith 
603c9a19010SBarry Smith .seealso: PetscVSNPrintf(), PetscVFPrintf(), PetscErrorPrintf()
604c9a19010SBarry Smith @*/
6057087cfbeSBarry Smith PetscErrorCode  PetscHelpPrintfDefault(MPI_Comm comm,const char format[],...)
606e5c89e4eSSatish Balay {
607e5c89e4eSSatish Balay   PetscErrorCode ierr;
608e5c89e4eSSatish Balay   PetscMPIInt    rank;
609e5c89e4eSSatish Balay 
610e5c89e4eSSatish Balay   PetscFunctionBegin;
611*6180deefSBarry Smith   if (comm == MPI_COMM_NULL) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Called with MPI_COMM_NULL, likely PetscObjectComm() failed");
612e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
613e5c89e4eSSatish Balay   if (!rank) {
614e5c89e4eSSatish Balay     va_list Argp;
615e5c89e4eSSatish Balay     va_start(Argp,format);
6161179db26SBarry Smith     ierr = (*PetscVFPrintf)(PETSC_STDOUT,format,Argp);CHKERRQ(ierr);
617e5c89e4eSSatish Balay     if (petsc_history) {
618cdc7d174SSatish Balay       va_start(Argp,format);
6191179db26SBarry Smith       ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr);
620e5c89e4eSSatish Balay     }
621e5c89e4eSSatish Balay     va_end(Argp);
622e5c89e4eSSatish Balay   }
623e5c89e4eSSatish Balay   PetscFunctionReturn(0);
624e5c89e4eSSatish Balay }
625e5c89e4eSSatish Balay 
626e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------------------*/
627e5c89e4eSSatish Balay 
628e5c89e4eSSatish Balay 
629e5c89e4eSSatish Balay #undef __FUNCT__
630e5c89e4eSSatish Balay #define __FUNCT__ "PetscSynchronizedFGets"
631e5c89e4eSSatish Balay /*@C
632e5c89e4eSSatish Balay     PetscSynchronizedFGets - Several processors all get the same line from a file.
633e5c89e4eSSatish Balay 
634e5c89e4eSSatish Balay     Collective on MPI_Comm
635e5c89e4eSSatish Balay 
636e5c89e4eSSatish Balay     Input Parameters:
637e5c89e4eSSatish Balay +   comm - the communicator
638e5c89e4eSSatish Balay .   fd - the file pointer
639e5c89e4eSSatish Balay -   len - the length of the output buffer
640e5c89e4eSSatish Balay 
641e5c89e4eSSatish Balay     Output Parameter:
642e5c89e4eSSatish Balay .   string - the line read from the file
643e5c89e4eSSatish Balay 
644e5c89e4eSSatish Balay     Level: intermediate
645e5c89e4eSSatish Balay 
646e5c89e4eSSatish Balay .seealso: PetscSynchronizedPrintf(), PetscSynchronizedFlush(),
647e5c89e4eSSatish Balay           PetscFOpen(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIPrintf()
648e5c89e4eSSatish Balay 
649e5c89e4eSSatish Balay @*/
6507087cfbeSBarry Smith PetscErrorCode  PetscSynchronizedFGets(MPI_Comm comm,FILE *fp,size_t len,char string[])
651e5c89e4eSSatish Balay {
652e5c89e4eSSatish Balay   PetscErrorCode ierr;
653e5c89e4eSSatish Balay   PetscMPIInt    rank;
654e5c89e4eSSatish Balay 
655e5c89e4eSSatish Balay   PetscFunctionBegin;
656e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
657e5c89e4eSSatish Balay 
658e5c89e4eSSatish Balay   if (!rank) {
659047b9c12SMatthew G Knepley     char *ptr = fgets(string, len, fp);
660047b9c12SMatthew G Knepley 
661047b9c12SMatthew G Knepley     if (!ptr) {
662a297a907SKarl Rupp       if (feof(fp)) len = 0;
663a297a907SKarl Rupp       else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_FILE_READ, "Error reading from file: %d", errno);
664047b9c12SMatthew G Knepley     }
665e5c89e4eSSatish Balay   }
666e5c89e4eSSatish Balay   ierr = MPI_Bcast(string,len,MPI_BYTE,0,comm);CHKERRQ(ierr);
667e5c89e4eSSatish Balay   PetscFunctionReturn(0);
668e5c89e4eSSatish Balay }
669238ccf28SShri Abhyankar 
670238ccf28SShri Abhyankar #if defined(PETSC_HAVE_MATLAB_ENGINE)
671c6db04a5SJed Brown #include <mex.h>
672238ccf28SShri Abhyankar #undef __FUNCT__
673238ccf28SShri Abhyankar #define __FUNCT__ "PetscVFPrintf_Matlab"
6747087cfbeSBarry Smith PetscErrorCode  PetscVFPrintf_Matlab(FILE *fd,const char format[],va_list Argp)
675238ccf28SShri Abhyankar {
676238ccf28SShri Abhyankar   PetscErrorCode ierr;
677238ccf28SShri Abhyankar 
678238ccf28SShri Abhyankar   PetscFunctionBegin;
679238ccf28SShri Abhyankar   if (fd != stdout && fd != stderr) { /* handle regular files */
680238ccf28SShri Abhyankar     ierr = PetscVFPrintfDefault(fd,format,Argp);CHKERRQ(ierr);
681238ccf28SShri Abhyankar   } else {
682238ccf28SShri Abhyankar     size_t len=8*1024,length;
683238ccf28SShri Abhyankar     char   buf[len];
684238ccf28SShri Abhyankar 
685238ccf28SShri Abhyankar     ierr = PetscVSNPrintf(buf,len,format,&length,Argp);CHKERRQ(ierr);
686df413903SBarry Smith     mexPrintf("%s",buf);
687238ccf28SShri Abhyankar   }
688238ccf28SShri Abhyankar   PetscFunctionReturn(0);
689238ccf28SShri Abhyankar }
690238ccf28SShri Abhyankar #endif
6916fc7ef2bSBarry Smith 
6926fc7ef2bSBarry Smith #undef __FUNCT__
6938c74ee41SBarry Smith #define __FUNCT__ "PetscFormatStrip"
6948c74ee41SBarry Smith /*@C
6958c74ee41SBarry Smith      PetscFormatStrip - Takes a PETSc format string and removes all numerical modifiers to % operations
6968c74ee41SBarry Smith 
6978c74ee41SBarry Smith    Input Parameters:
6988c74ee41SBarry Smith .   format - the PETSc format string
6998c74ee41SBarry Smith 
7008c74ee41SBarry Smith  Level: developer
7018c74ee41SBarry Smith 
7028c74ee41SBarry Smith @*/
7038c74ee41SBarry Smith PetscErrorCode  PetscFormatStrip(char *format)
7048c74ee41SBarry Smith {
7058c74ee41SBarry Smith   size_t loc1 = 0, loc2 = 0;
7068c74ee41SBarry Smith 
7078c74ee41SBarry Smith   PetscFunctionBegin;
7088c74ee41SBarry Smith   while (format[loc2]) {
7098c74ee41SBarry Smith     if (format[loc2] == '%') {
7108c74ee41SBarry Smith       format[loc1++] = format[loc2++];
7118c74ee41SBarry Smith       while (format[loc2] && ((format[loc2] >= '0' && format[loc2] <= '9') || format[loc2] == '.')) loc2++;
7128c74ee41SBarry Smith     }
7138c74ee41SBarry Smith     format[loc1++] = format[loc2++];
7148c74ee41SBarry Smith   }
7158c74ee41SBarry Smith   PetscFunctionReturn(0);
7168c74ee41SBarry Smith }
7178c74ee41SBarry Smith 
718