xref: /petsc/src/sys/fileio/mprint.c (revision 57ddf7da1103016db34deb816d124e8c373135f8)
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 /*
24b13499bfSbcordonn      Used to output to Zope
25b13499bfSbcordonn */
26b13499bfSbcordonn FILE *PETSC_ZOPEFD = 0;
27b13499bfSbcordonn 
28e5c89e4eSSatish Balay #undef __FUNCT__
29e5c89e4eSSatish Balay #define __FUNCT__ "PetscFormatConvert"
30c9a19010SBarry Smith /*@C
31c9a19010SBarry Smith      PetscFormatConvert - Takes a PETSc format string and converts it to a reqular C format string
32c9a19010SBarry Smith 
33c9a19010SBarry Smith    Input Parameters:
34c9a19010SBarry Smith +   format - the PETSc format string
35c9a19010SBarry Smith .   newformat - the location to put the standard C format string values
36c9a19010SBarry Smith -   size - the length of newformat
37c9a19010SBarry Smith 
38c9a19010SBarry 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
39c9a19010SBarry Smith 
40c9a19010SBarry Smith  Level: developer
41c9a19010SBarry Smith 
42c9a19010SBarry Smith @*/
437087cfbeSBarry Smith PetscErrorCode  PetscFormatConvert(const char *format,char *newformat,size_t size)
44e5c89e4eSSatish Balay {
45e5c89e4eSSatish Balay   PetscInt i = 0,j = 0;
46e5c89e4eSSatish Balay 
479daa610aSJed Brown   while (format[i] && i < (PetscInt)size-1) {
48e5c89e4eSSatish Balay     if (format[i] == '%' && format[i+1] == 'D') {
49e5c89e4eSSatish Balay       newformat[j++] = '%';
506de02169SBarry Smith #if !defined(PETSC_USE_64BIT_INDICES)
51e5c89e4eSSatish Balay       newformat[j++] = 'd';
52e5c89e4eSSatish Balay #else
53e5c89e4eSSatish Balay       newformat[j++] = 'l';
54e5c89e4eSSatish Balay       newformat[j++] = 'l';
55e5c89e4eSSatish Balay       newformat[j++] = 'd';
56e5c89e4eSSatish Balay #endif
57e5c89e4eSSatish Balay       i += 2;
58e5c89e4eSSatish Balay     } else if (format[i] == '%' && format[i+1] >= '1' && format[i+1] <= '9' && format[i+2] == 'D') {
59e5c89e4eSSatish Balay       newformat[j++] = '%';
60e5c89e4eSSatish Balay       newformat[j++] = format[i+1];
616de02169SBarry Smith #if !defined(PETSC_USE_64BIT_INDICES)
62e5c89e4eSSatish Balay       newformat[j++] = 'd';
63e5c89e4eSSatish Balay #else
64e5c89e4eSSatish Balay       newformat[j++] = 'l';
65e5c89e4eSSatish Balay       newformat[j++] = 'l';
66e5c89e4eSSatish Balay       newformat[j++] = 'd';
67e5c89e4eSSatish Balay #endif
68e5c89e4eSSatish Balay       i += 3;
69a83599f4SBarry Smith     } else if (format[i] == '%' && format[i+1] == 'G') {
70a83599f4SBarry Smith       newformat[j++] = '%';
71ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_DOUBLE) || defined(PETSC_USE_REAL_SINGLE)
72a83599f4SBarry Smith       newformat[j++] = 'g';
73ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_LONG_DOUBLE)
74a83599f4SBarry Smith       newformat[j++] = 'L';
75a83599f4SBarry Smith       newformat[j++] = 'g';
76ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL___FLOAT128)
77d9822059SBarry Smith       newformat[j++] = 'Q';
78*57ddf7daSJose Roman       newformat[j++] = 'g';
79a83599f4SBarry Smith #endif
80a83599f4SBarry Smith       i += 2;
81e5c89e4eSSatish Balay     }else {
82e5c89e4eSSatish Balay       newformat[j++] = format[i++];
83e5c89e4eSSatish Balay     }
84e5c89e4eSSatish Balay   }
85e5c89e4eSSatish Balay   newformat[j] = 0;
86e5c89e4eSSatish Balay   return 0;
87e5c89e4eSSatish Balay }
88e5c89e4eSSatish Balay 
89e5c89e4eSSatish Balay #undef __FUNCT__
90e5c89e4eSSatish Balay #define __FUNCT__ "PetscVSNPrintf"
91c9a19010SBarry Smith /*@C
92c9a19010SBarry Smith      PetscVSNPrintf - The PETSc version of vsnprintf(). Converts a PETSc format string into a standard C format string and then puts all the
93c9a19010SBarry Smith        function arguments into a string using the format statement.
94c9a19010SBarry Smith 
95c9a19010SBarry Smith    Input Parameters:
96c9a19010SBarry Smith +   str - location to put result
97c9a19010SBarry Smith .   len - the amount of space in str
98c9a19010SBarry Smith +   format - the PETSc format string
99c9a19010SBarry Smith -   fullLength - the amount of space in str actually used.
100c9a19010SBarry Smith 
101c9a19010SBarry Smith     Note:  No error handling because may be called by error handler
102c9a19010SBarry Smith 
103c9a19010SBarry Smith  Level: developer
104c9a19010SBarry Smith 
105c9a19010SBarry Smith @*/
1067087cfbeSBarry Smith PetscErrorCode  PetscVSNPrintf(char *str,size_t len,const char *format,size_t *fullLength,va_list Argp)
107e5c89e4eSSatish Balay {
108e5c89e4eSSatish Balay   /* no malloc since may be called by error handler */
109e2135aedSMatthew Knepley   char          *newformat;
110e2135aedSMatthew Knepley   char           formatbuf[8*1024];
1111a96acb8SJed Brown   size_t         oldLength,length;
1121a96acb8SJed Brown   int            fullLengthInt;
1131a15ef5dSMatthew Knepley   PetscErrorCode ierr;
114e5c89e4eSSatish Balay 
115e2135aedSMatthew Knepley   ierr = PetscStrlen(format, &oldLength);CHKERRQ(ierr);
116e2135aedSMatthew Knepley   if (oldLength < 8*1024) {
117e2135aedSMatthew Knepley     newformat = formatbuf;
118e2135aedSMatthew Knepley   } else {
119e2135aedSMatthew Knepley     ierr = PetscMalloc((oldLength+1) * sizeof(char), &newformat);CHKERRQ(ierr);
120e2135aedSMatthew Knepley   }
121e2135aedSMatthew Knepley   PetscFormatConvert(format,newformat,oldLength+1);
1221a15ef5dSMatthew Knepley   ierr = PetscStrlen(newformat, &length);CHKERRQ(ierr);
1232d609e63SMatthew Knepley #if 0
1241a15ef5dSMatthew Knepley   if (length > len) {
1251a15ef5dSMatthew Knepley     newformat[len] = '\0';
1261a15ef5dSMatthew Knepley   }
1272d609e63SMatthew Knepley #endif
1285a058713SBarry Smith #if defined(PETSC_HAVE_VSNPRINTF_CHAR)
12942c7c0bfSJed Brown   fullLengthInt = vsnprintf(str,len,newformat,(char *)Argp);
13089b07760SSatish Balay #elif defined(PETSC_HAVE_VSNPRINTF)
13142c7c0bfSJed Brown   fullLengthInt = vsnprintf(str,len,newformat,Argp);
132141c0d65SSatish Balay #elif defined(PETSC_HAVE__VSNPRINTF)
13342c7c0bfSJed Brown   fullLengthInt = _vsnprintf(str,len,newformat,Argp);
134e5c89e4eSSatish Balay #else
13589b07760SSatish Balay #error "vsnprintf not found"
136e5c89e4eSSatish Balay #endif
13742c7c0bfSJed Brown   if (fullLengthInt < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"vsnprintf() failed");
13842c7c0bfSJed Brown   *fullLength = (size_t)fullLengthInt;
139e2135aedSMatthew Knepley   if (oldLength >= 8*1024) {
140e2135aedSMatthew Knepley     ierr = PetscFree(newformat);CHKERRQ(ierr);
141e2135aedSMatthew Knepley   }
142e5c89e4eSSatish Balay   return 0;
143e5c89e4eSSatish Balay }
144e5c89e4eSSatish Balay 
145e5c89e4eSSatish Balay #undef __FUNCT__
1467ead4f01Sbcordonn #define __FUNCT__ "PetscZopeLog"
1477087cfbeSBarry Smith PetscErrorCode  PetscZopeLog(const char *format,va_list Argp)
1489c4c166aSBarry Smith {
1497ead4f01Sbcordonn   /* no malloc since may be called by error handler */
1507ead4f01Sbcordonn   char        newformat[8*1024];
1517ead4f01Sbcordonn   char        log[8*1024];
1527ead4f01Sbcordonn   char        logstart[] = " <<<log>>>";
153c9a19010SBarry Smith   size_t      len,formatlen;
1549c4c166aSBarry Smith 
1557ead4f01Sbcordonn   PetscFormatConvert(format,newformat,8*1024);
1567ead4f01Sbcordonn   PetscStrlen(logstart, &len);
1577ead4f01Sbcordonn   PetscMemcpy(log, logstart, len);
1587ead4f01Sbcordonn   PetscStrlen(newformat, &formatlen);
1597ead4f01Sbcordonn   PetscMemcpy(&(log[len]), newformat, formatlen);
1609c4c166aSBarry Smith   if (PETSC_ZOPEFD){
1615a058713SBarry Smith #if defined(PETSC_HAVE_VFPRINTF_CHAR)
1627ead4f01Sbcordonn     vfprintf(PETSC_ZOPEFD,log,(char *)Argp);
1637ead4f01Sbcordonn #else
1647ead4f01Sbcordonn     vfprintf(PETSC_ZOPEFD,log,Argp);
1657ead4f01Sbcordonn #endif
1665a058713SBarry Smith     fflush(PETSC_ZOPEFD);
1677ead4f01Sbcordonn   }
1687ead4f01Sbcordonn   return 0;
1697ead4f01Sbcordonn }
1707ead4f01Sbcordonn 
1717ead4f01Sbcordonn #undef __FUNCT__
172c9a19010SBarry Smith #define __FUNCT__ "PetscVFPrintfDefault"
173c9a19010SBarry Smith /*@C
174c9a19010SBarry Smith      PetscVFPrintf -  All PETSc standard out and error messages are sent through this function; so, in theory, this can
175e5c89e4eSSatish Balay         can be replaced with something that does not simply write to a file.
176e5c89e4eSSatish Balay 
177c9a19010SBarry Smith       To use, write your own function for example,
178c9a19010SBarry Smith $PetscErrorCode mypetscvfprintf(FILE *fd,const char format[],va_list Argp)
179c9a19010SBarry Smith ${
180c9a19010SBarry Smith $  PetscErrorCode ierr;
181c9a19010SBarry Smith $
182c9a19010SBarry Smith $  PetscFunctionBegin;
183c9a19010SBarry Smith $   if (fd != stdout && fd != stderr) {  handle regular files
184c9a19010SBarry Smith $      ierr = PetscVFPrintfDefault(fd,format,Argp); CHKERR(ierr);
185c9a19010SBarry Smith $  } else {
186c9a19010SBarry Smith $     char   buff[BIG];
187c9a19010SBarry Smith $     size_t length;
188c9a19010SBarry Smith $     ierr = PetscVSNPrintf(buff,BIG,format,&length,Argp);CHKERRQ(ierr);
189c9a19010SBarry Smith $     now send buff to whatever stream or whatever you want
190c9a19010SBarry Smith $ }
191c9a19010SBarry Smith $ PetscFunctionReturn(0);
192c9a19010SBarry Smith $}
193c9a19010SBarry Smith then before the call to PetscInitialize() do the assignment
194c9a19010SBarry Smith $    PetscVFPrintf = mypetscvfprintf;
195c9a19010SBarry Smith 
196c9a19010SBarry Smith       Notes: For error messages this may be called by any process, for regular standard out it is
197e5c89e4eSSatish Balay           called only by process 0 of a given communicator
198e5c89e4eSSatish Balay 
199e5c89e4eSSatish Balay       No error handling because may be called by error handler
200c9a19010SBarry Smith 
201c9a19010SBarry Smith   Level:  developer
202c9a19010SBarry Smith 
203c9a19010SBarry Smith .seealso: PetscVSNPrintf(), PetscErrorPrintf()
204c9a19010SBarry Smith 
205c9a19010SBarry Smith @*/
2067087cfbeSBarry Smith PetscErrorCode  PetscVFPrintfDefault(FILE *fd,const char *format,va_list Argp)
207e5c89e4eSSatish Balay {
208e2135aedSMatthew Knepley   /* no malloc since may be called by error handler (assume no long messages in errors) */
209e2135aedSMatthew Knepley   char        *newformat;
210e2135aedSMatthew Knepley   char         formatbuf[8*1024];
211e2135aedSMatthew Knepley   size_t       oldLength;
2121179db26SBarry Smith 
213e2135aedSMatthew Knepley   PetscStrlen(format, &oldLength);
214e2135aedSMatthew Knepley   if (oldLength < 8*1024) {
215e2135aedSMatthew Knepley     newformat = formatbuf;
216e2135aedSMatthew Knepley   } else {
217fee1560eSJed Brown     (void)PetscMalloc((oldLength+1) * sizeof(char), &newformat);
218e2135aedSMatthew Knepley   }
219e2135aedSMatthew Knepley   PetscFormatConvert(format,newformat,oldLength+1);
220d8c6e182Sbcordonn 
2215a058713SBarry Smith #if defined(PETSC_HAVE_VFPRINTF_CHAR)
222e5c89e4eSSatish Balay   vfprintf(fd,newformat,(char *)Argp);
223e5c89e4eSSatish Balay #else
224e5c89e4eSSatish Balay   vfprintf(fd,newformat,Argp);
225e5c89e4eSSatish Balay #endif
2265a058713SBarry Smith   fflush(fd);
227e2135aedSMatthew Knepley   if (oldLength >= 8*1024) {
2289a2231e1SJed Brown     (void)PetscFree(newformat);
229e2135aedSMatthew Knepley   }
230e5c89e4eSSatish Balay   return 0;
231e5c89e4eSSatish Balay }
232e5c89e4eSSatish Balay 
2335b5bc046SBarry Smith #undef __FUNCT__
2345b5bc046SBarry Smith #define __FUNCT__ "PetscSNPrintf"
2355b5bc046SBarry Smith /*@C
2365b5bc046SBarry Smith     PetscSNPrintf - Prints to a string of given length
2375b5bc046SBarry Smith 
2385b5bc046SBarry Smith     Not Collective
2395b5bc046SBarry Smith 
2405b5bc046SBarry Smith     Input Parameters:
2415b5bc046SBarry Smith +   str - the string to print to
2425b5bc046SBarry Smith .   len - the length of str
2435b5bc046SBarry Smith .   format - the usual printf() format string
2445b5bc046SBarry Smith -   any arguments
2455b5bc046SBarry Smith 
2465b5bc046SBarry Smith    Level: intermediate
2475b5bc046SBarry Smith 
2485b5bc046SBarry Smith .seealso: PetscSynchronizedFlush(), PetscSynchronizedFPrintf(), PetscFPrintf(), PetscVSNPrintf(),
2495b5bc046SBarry Smith           PetscPrintf(), PetscViewerASCIIPrintf(), PetscViewerASCIISynchronizedPrintf()
2505b5bc046SBarry Smith @*/
2517087cfbeSBarry Smith PetscErrorCode  PetscSNPrintf(char *str,size_t len,const char format[],...)
2525b5bc046SBarry Smith {
2535b5bc046SBarry Smith   PetscErrorCode ierr;
254c9a19010SBarry Smith   size_t         fullLength;
2555b5bc046SBarry Smith   va_list        Argp;
2565b5bc046SBarry Smith 
2575b5bc046SBarry Smith   PetscFunctionBegin;
2585b5bc046SBarry Smith   va_start(Argp,format);
2592d609e63SMatthew Knepley   ierr = PetscVSNPrintf(str,len,format,&fullLength,Argp);CHKERRQ(ierr);
2605b5bc046SBarry Smith   PetscFunctionReturn(0);
2615b5bc046SBarry Smith }
2625b5bc046SBarry Smith 
263257d2499SJed Brown #undef __FUNCT__
264257d2499SJed Brown #define __FUNCT__ "PetscSNPrintfCount"
265257d2499SJed Brown /*@C
266257d2499SJed Brown     PetscSNPrintfCount - Prints to a string of given length, returns count
267257d2499SJed Brown 
268257d2499SJed Brown     Not Collective
269257d2499SJed Brown 
270257d2499SJed Brown     Input Parameters:
271257d2499SJed Brown +   str - the string to print to
272257d2499SJed Brown .   len - the length of str
273257d2499SJed Brown .   format - the usual printf() format string
274257d2499SJed Brown .   countused - number of characters used
275257d2499SJed Brown -   any arguments
276257d2499SJed Brown 
277257d2499SJed Brown    Level: intermediate
278257d2499SJed Brown 
279257d2499SJed Brown .seealso: PetscSynchronizedFlush(), PetscSynchronizedFPrintf(), PetscFPrintf(), PetscVSNPrintf(),
280257d2499SJed Brown           PetscPrintf(), PetscViewerASCIIPrintf(), PetscViewerASCIISynchronizedPrintf(), PetscSNPrintf()
281257d2499SJed Brown @*/
282257d2499SJed Brown PetscErrorCode  PetscSNPrintfCount(char *str,size_t len,const char format[],size_t *countused,...)
283257d2499SJed Brown {
284257d2499SJed Brown   PetscErrorCode ierr;
285257d2499SJed Brown   va_list        Argp;
286257d2499SJed Brown 
287257d2499SJed Brown   PetscFunctionBegin;
288257d2499SJed Brown   va_start(Argp,countused);
289257d2499SJed Brown   ierr = PetscVSNPrintf(str,len,format,countused,Argp);CHKERRQ(ierr);
290257d2499SJed Brown   PetscFunctionReturn(0);
291257d2499SJed Brown }
292257d2499SJed Brown 
293e5c89e4eSSatish Balay /* ----------------------------------------------------------------------- */
294e5c89e4eSSatish Balay 
295e5c89e4eSSatish Balay PrintfQueue queue       = 0,queuebase = 0;
296e5c89e4eSSatish Balay int         queuelength = 0;
297e5c89e4eSSatish Balay FILE        *queuefile  = PETSC_NULL;
298e5c89e4eSSatish Balay 
299e5c89e4eSSatish Balay #undef __FUNCT__
300e5c89e4eSSatish Balay #define __FUNCT__ "PetscSynchronizedPrintf"
301e5c89e4eSSatish Balay /*@C
302e5c89e4eSSatish Balay     PetscSynchronizedPrintf - Prints synchronized output from several processors.
303e5c89e4eSSatish Balay     Output of the first processor is followed by that of the second, etc.
304e5c89e4eSSatish Balay 
305e5c89e4eSSatish Balay     Not Collective
306e5c89e4eSSatish Balay 
307e5c89e4eSSatish Balay     Input Parameters:
308e5c89e4eSSatish Balay +   comm - the communicator
309e5c89e4eSSatish Balay -   format - the usual printf() format string
310e5c89e4eSSatish Balay 
311e5c89e4eSSatish Balay    Level: intermediate
312e5c89e4eSSatish Balay 
313e5c89e4eSSatish Balay     Notes:
314e5c89e4eSSatish Balay     REQUIRES a intervening call to PetscSynchronizedFlush() for the information
315e5c89e4eSSatish Balay     from all the processors to be printed.
316e5c89e4eSSatish Balay 
317e5c89e4eSSatish Balay     Fortran Note:
318d60d70ccSBarry Smith     The call sequence is PetscSynchronizedPrintf(MPI_Comm, character(*), PetscErrorCode ierr) from Fortran.
319e5c89e4eSSatish Balay     That is, you can only pass a single character string from Fortran.
320e5c89e4eSSatish Balay 
321e5c89e4eSSatish Balay .seealso: PetscSynchronizedFlush(), PetscSynchronizedFPrintf(), PetscFPrintf(),
322e5c89e4eSSatish Balay           PetscPrintf(), PetscViewerASCIIPrintf(), PetscViewerASCIISynchronizedPrintf()
323e5c89e4eSSatish Balay @*/
3247087cfbeSBarry Smith PetscErrorCode  PetscSynchronizedPrintf(MPI_Comm comm,const char format[],...)
325e5c89e4eSSatish Balay {
326e5c89e4eSSatish Balay   PetscErrorCode ierr;
327e5c89e4eSSatish Balay   PetscMPIInt    rank;
328e5c89e4eSSatish Balay 
329e5c89e4eSSatish Balay   PetscFunctionBegin;
330e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
331e5c89e4eSSatish Balay 
332e5c89e4eSSatish Balay   /* First processor prints immediately to stdout */
333e5c89e4eSSatish Balay   if (!rank) {
334e5c89e4eSSatish Balay     va_list Argp;
335e5c89e4eSSatish Balay     va_start(Argp,format);
3361179db26SBarry Smith     ierr = (*PetscVFPrintf)(PETSC_STDOUT,format,Argp);CHKERRQ(ierr);
337e5c89e4eSSatish Balay     if (petsc_history) {
338cdc7d174SSatish Balay       va_start(Argp,format);
3391179db26SBarry Smith       ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr);
340e5c89e4eSSatish Balay     }
341e5c89e4eSSatish Balay     va_end(Argp);
342e5c89e4eSSatish Balay   } else { /* other processors add to local queue */
343e5c89e4eSSatish Balay     va_list     Argp;
344e5c89e4eSSatish Balay     PrintfQueue next;
345c9a19010SBarry Smith     size_t      fullLength = 8191;
346e5c89e4eSSatish Balay 
347e5c89e4eSSatish Balay     ierr = PetscNew(struct _PrintfQueue,&next);CHKERRQ(ierr);
348e5c89e4eSSatish Balay     if (queue) {queue->next = next; queue = next; queue->next = 0;}
349e5c89e4eSSatish Balay     else       {queuebase   = queue = next;}
350e5c89e4eSSatish Balay     queuelength++;
3512d609e63SMatthew Knepley     next->size = -1;
3529ad23270SJed Brown     while((PetscInt)fullLength >= next->size) {
3532d609e63SMatthew Knepley       next->size = fullLength+1;
3542d609e63SMatthew Knepley       ierr = PetscMalloc(next->size * sizeof(char), &next->string);CHKERRQ(ierr);
355e5c89e4eSSatish Balay       va_start(Argp,format);
3562d609e63SMatthew Knepley       ierr = PetscMemzero(next->string,next->size);CHKERRQ(ierr);
3572d609e63SMatthew Knepley       ierr = PetscVSNPrintf(next->string,next->size,format, &fullLength,Argp);CHKERRQ(ierr);
358e5c89e4eSSatish Balay       va_end(Argp);
359e5c89e4eSSatish Balay     }
3602d609e63SMatthew Knepley   }
361e5c89e4eSSatish Balay 
362e5c89e4eSSatish Balay   PetscFunctionReturn(0);
363e5c89e4eSSatish Balay }
364e5c89e4eSSatish Balay 
365e5c89e4eSSatish Balay #undef __FUNCT__
366e5c89e4eSSatish Balay #define __FUNCT__ "PetscSynchronizedFPrintf"
367e5c89e4eSSatish Balay /*@C
368e5c89e4eSSatish Balay     PetscSynchronizedFPrintf - Prints synchronized output to the specified file from
369e5c89e4eSSatish Balay     several processors.  Output of the first processor is followed by that of the
370e5c89e4eSSatish Balay     second, etc.
371e5c89e4eSSatish Balay 
372e5c89e4eSSatish Balay     Not Collective
373e5c89e4eSSatish Balay 
374e5c89e4eSSatish Balay     Input Parameters:
375e5c89e4eSSatish Balay +   comm - the communicator
376e5c89e4eSSatish Balay .   fd - the file pointer
377e5c89e4eSSatish Balay -   format - the usual printf() format string
378e5c89e4eSSatish Balay 
379e5c89e4eSSatish Balay     Level: intermediate
380e5c89e4eSSatish Balay 
381e5c89e4eSSatish Balay     Notes:
382e5c89e4eSSatish Balay     REQUIRES a intervening call to PetscSynchronizedFlush() for the information
383e5c89e4eSSatish Balay     from all the processors to be printed.
384e5c89e4eSSatish Balay 
385e5c89e4eSSatish Balay .seealso: PetscSynchronizedPrintf(), PetscSynchronizedFlush(), PetscFPrintf(),
386e5c89e4eSSatish Balay           PetscFOpen(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIPrintf()
387e5c89e4eSSatish Balay 
388e5c89e4eSSatish Balay @*/
3897087cfbeSBarry Smith PetscErrorCode  PetscSynchronizedFPrintf(MPI_Comm comm,FILE* fp,const char format[],...)
390e5c89e4eSSatish Balay {
391e5c89e4eSSatish Balay   PetscErrorCode ierr;
392e5c89e4eSSatish Balay   PetscMPIInt    rank;
393e5c89e4eSSatish Balay 
394e5c89e4eSSatish Balay   PetscFunctionBegin;
395e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
396e5c89e4eSSatish Balay 
397e5c89e4eSSatish Balay   /* First processor prints immediately to fp */
398e5c89e4eSSatish Balay   if (!rank) {
399e5c89e4eSSatish Balay     va_list Argp;
400e5c89e4eSSatish Balay     va_start(Argp,format);
4011179db26SBarry Smith     ierr = (*PetscVFPrintf)(fp,format,Argp);CHKERRQ(ierr);
402e5c89e4eSSatish Balay     queuefile = fp;
403cdc7d174SSatish Balay     if (petsc_history && (fp !=petsc_history)) {
404cdc7d174SSatish Balay       va_start(Argp,format);
4051179db26SBarry Smith       ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr);
406e5c89e4eSSatish Balay     }
407e5c89e4eSSatish Balay     va_end(Argp);
408e5c89e4eSSatish Balay   } else { /* other processors add to local queue */
409e5c89e4eSSatish Balay     va_list     Argp;
410e5c89e4eSSatish Balay     PrintfQueue next;
411c9a19010SBarry Smith     size_t      fullLength = 8191;
412e5c89e4eSSatish Balay     ierr = PetscNew(struct _PrintfQueue,&next);CHKERRQ(ierr);
413e5c89e4eSSatish Balay     if (queue) {queue->next = next; queue = next; queue->next = 0;}
414e5c89e4eSSatish Balay     else       {queuebase   = queue = next;}
415e5c89e4eSSatish Balay     queuelength++;
4162d609e63SMatthew Knepley     next->size = -1;
4179ad23270SJed Brown     while((PetscInt)fullLength >= next->size) {
4182d609e63SMatthew Knepley       next->size = fullLength+1;
4192d609e63SMatthew Knepley       ierr = PetscMalloc(next->size * sizeof(char), &next->string);CHKERRQ(ierr);
420e5c89e4eSSatish Balay       va_start(Argp,format);
4212d609e63SMatthew Knepley       ierr = PetscMemzero(next->string,next->size);CHKERRQ(ierr);
4222d609e63SMatthew Knepley       ierr = PetscVSNPrintf(next->string,next->size,format,&fullLength,Argp);CHKERRQ(ierr);
423e5c89e4eSSatish Balay       va_end(Argp);
424e5c89e4eSSatish Balay     }
4252d609e63SMatthew Knepley   }
426e5c89e4eSSatish Balay   PetscFunctionReturn(0);
427e5c89e4eSSatish Balay }
428e5c89e4eSSatish Balay 
429e5c89e4eSSatish Balay #undef __FUNCT__
430e5c89e4eSSatish Balay #define __FUNCT__ "PetscSynchronizedFlush"
431e30d2299SSatish Balay /*@
432e5c89e4eSSatish Balay     PetscSynchronizedFlush - Flushes to the screen output from all processors
433e5c89e4eSSatish Balay     involved in previous PetscSynchronizedPrintf() calls.
434e5c89e4eSSatish Balay 
435e5c89e4eSSatish Balay     Collective on MPI_Comm
436e5c89e4eSSatish Balay 
437e5c89e4eSSatish Balay     Input Parameters:
438e5c89e4eSSatish Balay .   comm - the communicator
439e5c89e4eSSatish Balay 
440e5c89e4eSSatish Balay     Level: intermediate
441e5c89e4eSSatish Balay 
442e5c89e4eSSatish Balay     Notes:
443e5c89e4eSSatish Balay     Usage of PetscSynchronizedPrintf() and PetscSynchronizedFPrintf() with
444e5c89e4eSSatish Balay     different MPI communicators REQUIRES an intervening call to PetscSynchronizedFlush().
445e5c89e4eSSatish Balay 
446e5c89e4eSSatish Balay .seealso: PetscSynchronizedPrintf(), PetscFPrintf(), PetscPrintf(), PetscViewerASCIIPrintf(),
447e5c89e4eSSatish Balay           PetscViewerASCIISynchronizedPrintf()
448e5c89e4eSSatish Balay @*/
4497087cfbeSBarry Smith PetscErrorCode  PetscSynchronizedFlush(MPI_Comm comm)
450e5c89e4eSSatish Balay {
451e5c89e4eSSatish Balay   PetscErrorCode ierr;
452bf3b0749SBarry Smith   PetscMPIInt    rank,size,tag,i,j,n,dummy = 0;
4532d609e63SMatthew Knepley   char          *message;
454e5c89e4eSSatish Balay   MPI_Status     status;
455e5c89e4eSSatish Balay   FILE           *fd;
456e5c89e4eSSatish Balay 
457e5c89e4eSSatish Balay   PetscFunctionBegin;
458e5c89e4eSSatish Balay   ierr = PetscCommDuplicate(comm,&comm,&tag);CHKERRQ(ierr);
459e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
460e5c89e4eSSatish Balay   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
461e5c89e4eSSatish Balay 
462e5c89e4eSSatish Balay   /* First processor waits for messages from all other processors */
463e5c89e4eSSatish Balay   if (!rank) {
464e5c89e4eSSatish Balay     if (queuefile) {
465e5c89e4eSSatish Balay       fd = queuefile;
466e5c89e4eSSatish Balay     } else {
467e5c89e4eSSatish Balay       fd = PETSC_STDOUT;
468e5c89e4eSSatish Balay     }
469e5c89e4eSSatish Balay     for (i=1; i<size; i++) {
4709f73f8ecSBarry Smith       /* to prevent a flood of messages to process zero, request each message separately */
4719f73f8ecSBarry Smith       ierr = MPI_Send(&dummy,1,MPI_INT,i,tag,comm);CHKERRQ(ierr);
472e5c89e4eSSatish Balay       ierr = MPI_Recv(&n,1,MPI_INT,i,tag,comm,&status);CHKERRQ(ierr);
473e5c89e4eSSatish Balay       for (j=0; j<n; j++) {
4749f73f8ecSBarry Smith         PetscMPIInt size;
4752d609e63SMatthew Knepley 
4762d609e63SMatthew Knepley         ierr = MPI_Recv(&size,1,MPI_INT,i,tag,comm,&status);CHKERRQ(ierr);
4772d609e63SMatthew Knepley         ierr = PetscMalloc(size * sizeof(char), &message);CHKERRQ(ierr);
4782d609e63SMatthew Knepley         ierr = MPI_Recv(message,size,MPI_CHAR,i,tag,comm,&status);CHKERRQ(ierr);
479e5c89e4eSSatish Balay         ierr = PetscFPrintf(comm,fd,"%s",message);
4802d609e63SMatthew Knepley         ierr = PetscFree(message);CHKERRQ(ierr);
481e5c89e4eSSatish Balay       }
482e5c89e4eSSatish Balay     }
483e5c89e4eSSatish Balay     queuefile = PETSC_NULL;
484e5c89e4eSSatish Balay   } else { /* other processors send queue to processor 0 */
485e5c89e4eSSatish Balay     PrintfQueue next = queuebase,previous;
486e5c89e4eSSatish Balay 
487b3ef9d35SBarry Smith     ierr = MPI_Recv(&dummy,1,MPI_INT,0,tag,comm,&status);CHKERRQ(ierr);
488e5c89e4eSSatish Balay     ierr = MPI_Send(&queuelength,1,MPI_INT,0,tag,comm);CHKERRQ(ierr);
489e5c89e4eSSatish Balay     for (i=0; i<queuelength; i++) {
4902d609e63SMatthew Knepley       ierr     = MPI_Send(&next->size,1,MPI_INT,0,tag,comm);CHKERRQ(ierr);
4912d609e63SMatthew Knepley       ierr     = MPI_Send(next->string,next->size,MPI_CHAR,0,tag,comm);CHKERRQ(ierr);
492e5c89e4eSSatish Balay       previous = next;
493e5c89e4eSSatish Balay       next     = next->next;
4942d609e63SMatthew Knepley       ierr     = PetscFree(previous->string);CHKERRQ(ierr);
495e5c89e4eSSatish Balay       ierr     = PetscFree(previous);CHKERRQ(ierr);
496e5c89e4eSSatish Balay     }
497e5c89e4eSSatish Balay     queue       = 0;
498e5c89e4eSSatish Balay     queuelength = 0;
499e5c89e4eSSatish Balay   }
500e5c89e4eSSatish Balay   ierr = PetscCommDestroy(&comm);CHKERRQ(ierr);
501e5c89e4eSSatish Balay   PetscFunctionReturn(0);
502e5c89e4eSSatish Balay }
503e5c89e4eSSatish Balay 
504e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------------------*/
505e5c89e4eSSatish Balay 
506e5c89e4eSSatish Balay #undef __FUNCT__
507e5c89e4eSSatish Balay #define __FUNCT__ "PetscFPrintf"
508e5c89e4eSSatish Balay /*@C
509e5c89e4eSSatish Balay     PetscFPrintf - Prints to a file, only from the first
510e5c89e4eSSatish Balay     processor in the communicator.
511e5c89e4eSSatish Balay 
512e5c89e4eSSatish Balay     Not Collective
513e5c89e4eSSatish Balay 
514e5c89e4eSSatish Balay     Input Parameters:
515e5c89e4eSSatish Balay +   comm - the communicator
516e5c89e4eSSatish Balay .   fd - the file pointer
517e5c89e4eSSatish Balay -   format - the usual printf() format string
518e5c89e4eSSatish Balay 
519e5c89e4eSSatish Balay     Level: intermediate
520e5c89e4eSSatish Balay 
521e5c89e4eSSatish Balay     Fortran Note:
522e5c89e4eSSatish Balay     This routine is not supported in Fortran.
523e5c89e4eSSatish Balay 
524e5c89e4eSSatish Balay    Concepts: printing^in parallel
525e5c89e4eSSatish Balay    Concepts: printf^in parallel
526e5c89e4eSSatish Balay 
527e5c89e4eSSatish Balay .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(),
528e5c89e4eSSatish Balay           PetscViewerASCIISynchronizedPrintf(), PetscSynchronizedFlush()
529e5c89e4eSSatish Balay @*/
5307087cfbeSBarry Smith PetscErrorCode  PetscFPrintf(MPI_Comm comm,FILE* fd,const char format[],...)
531e5c89e4eSSatish Balay {
532e5c89e4eSSatish Balay   PetscErrorCode ierr;
533e5c89e4eSSatish Balay   PetscMPIInt    rank;
534e5c89e4eSSatish Balay 
535e5c89e4eSSatish Balay   PetscFunctionBegin;
536e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
537e5c89e4eSSatish Balay   if (!rank) {
538e5c89e4eSSatish Balay     va_list Argp;
539e5c89e4eSSatish Balay     va_start(Argp,format);
5401179db26SBarry Smith     ierr = (*PetscVFPrintf)(fd,format,Argp);CHKERRQ(ierr);
541cdc7d174SSatish Balay     if (petsc_history && (fd !=petsc_history)) {
542cdc7d174SSatish Balay       va_start(Argp,format);
5431179db26SBarry Smith       ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr);
544e5c89e4eSSatish Balay       }
545e5c89e4eSSatish Balay     va_end(Argp);
546e5c89e4eSSatish Balay   }
547e5c89e4eSSatish Balay   PetscFunctionReturn(0);
548e5c89e4eSSatish Balay }
549e5c89e4eSSatish Balay 
550e5c89e4eSSatish Balay #undef __FUNCT__
551e5c89e4eSSatish Balay #define __FUNCT__ "PetscPrintf"
552e5c89e4eSSatish Balay /*@C
553e5c89e4eSSatish Balay     PetscPrintf - Prints to standard out, only from the first
554e5c89e4eSSatish Balay     processor in the communicator.
555e5c89e4eSSatish Balay 
556e5c89e4eSSatish Balay     Not Collective
557e5c89e4eSSatish Balay 
558e5c89e4eSSatish Balay     Input Parameters:
559e5c89e4eSSatish Balay +   comm - the communicator
560e5c89e4eSSatish Balay -   format - the usual printf() format string
561e5c89e4eSSatish Balay 
562e5c89e4eSSatish Balay    Level: intermediate
563e5c89e4eSSatish Balay 
564e5c89e4eSSatish Balay     Fortran Note:
565d60d70ccSBarry Smith     The call sequence is PetscPrintf(MPI_Comm, character(*), PetscErrorCode ierr) from Fortran.
566e5c89e4eSSatish Balay     That is, you can only pass a single character string from Fortran.
567e5c89e4eSSatish Balay 
5688884cb1fSJed Brown    Notes: The %A format specifier is special.  It assumes an argument of type PetscReal
5698884cb1fSJed Brown           and is replaced with %G unless the absolute value is < 1.e-12 when it is replaced
5708884cb1fSJed Brown           with "< 1.e-12" (1.e-6 for single precision).
571e5c89e4eSSatish Balay 
572e5c89e4eSSatish Balay    Concepts: printing^in parallel
573e5c89e4eSSatish Balay    Concepts: printf^in parallel
574e5c89e4eSSatish Balay 
575e5c89e4eSSatish Balay .seealso: PetscFPrintf(), PetscSynchronizedPrintf()
576e5c89e4eSSatish Balay @*/
5777087cfbeSBarry Smith PetscErrorCode  PetscPrintf(MPI_Comm comm,const char format[],...)
578e5c89e4eSSatish Balay {
579e5c89e4eSSatish Balay   PetscErrorCode ierr;
580e5c89e4eSSatish Balay   PetscMPIInt    rank;
581e5c89e4eSSatish Balay   size_t         len;
582e5c89e4eSSatish Balay   char           *nformat,*sub1,*sub2;
583e5c89e4eSSatish Balay   PetscReal      value;
584e5c89e4eSSatish Balay 
585e5c89e4eSSatish Balay   PetscFunctionBegin;
586e5c89e4eSSatish Balay   if (!comm) comm = PETSC_COMM_WORLD;
587e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
588e5c89e4eSSatish Balay   if (!rank) {
589e5c89e4eSSatish Balay     va_list Argp;
590e5c89e4eSSatish Balay     va_start(Argp,format);
591e5c89e4eSSatish Balay 
592e5c89e4eSSatish Balay     ierr = PetscStrstr(format,"%A",&sub1);CHKERRQ(ierr);
593e5c89e4eSSatish Balay     if (sub1) {
594e5c89e4eSSatish Balay       ierr = PetscStrstr(format,"%",&sub2);CHKERRQ(ierr);
595e32f2f54SBarry Smith       if (sub1 != sub2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"%%A format must be first in format string");
596e5c89e4eSSatish Balay       ierr    = PetscStrlen(format,&len);CHKERRQ(ierr);
597e5c89e4eSSatish Balay       ierr    = PetscMalloc((len+16)*sizeof(char),&nformat);CHKERRQ(ierr);
598e5c89e4eSSatish Balay       ierr    = PetscStrcpy(nformat,format);CHKERRQ(ierr);
599e5c89e4eSSatish Balay       ierr    = PetscStrstr(nformat,"%",&sub2);CHKERRQ(ierr);
600e5c89e4eSSatish Balay       sub2[0] = 0;
60192eff512SVictor Minden       value   = va_arg(Argp,double);
602ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
6038884cb1fSJed Brown       if (PetscAbsReal(value) < 1.e-6) {
6048884cb1fSJed Brown         ierr    = PetscStrcat(nformat,"< 1.e-6");CHKERRQ(ierr);
6058884cb1fSJed Brown #else
606e5c89e4eSSatish Balay       if (PetscAbsReal(value) < 1.e-12) {
607e5c89e4eSSatish Balay         ierr    = PetscStrcat(nformat,"< 1.e-12");CHKERRQ(ierr);
6088884cb1fSJed Brown #endif
609e5c89e4eSSatish Balay       } else {
6108884cb1fSJed Brown         ierr    = PetscStrcat(nformat,"%G");CHKERRQ(ierr);
611e5c89e4eSSatish Balay         va_end(Argp);
612e5c89e4eSSatish Balay         va_start(Argp,format);
613e5c89e4eSSatish Balay       }
614e5c89e4eSSatish Balay       ierr    = PetscStrcat(nformat,sub1+2);CHKERRQ(ierr);
615e5c89e4eSSatish Balay     } else {
616e5c89e4eSSatish Balay       nformat = (char*)format;
617e5c89e4eSSatish Balay     }
6181179db26SBarry Smith     ierr = (*PetscVFPrintf)(PETSC_STDOUT,nformat,Argp);CHKERRQ(ierr);
619e5c89e4eSSatish Balay     if (petsc_history) {
620cdc7d174SSatish Balay       va_start(Argp,format);
6211179db26SBarry Smith       ierr = (*PetscVFPrintf)(petsc_history,nformat,Argp);CHKERRQ(ierr);
622e5c89e4eSSatish Balay     }
623e5c89e4eSSatish Balay     va_end(Argp);
624e5c89e4eSSatish Balay     if (sub1) {ierr = PetscFree(nformat);CHKERRQ(ierr);}
625e5c89e4eSSatish Balay   }
626e5c89e4eSSatish Balay   PetscFunctionReturn(0);
627e5c89e4eSSatish Balay }
628e5c89e4eSSatish Balay 
629e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------------------*/
630e5c89e4eSSatish Balay #undef __FUNCT__
631e5c89e4eSSatish Balay #define __FUNCT__ "PetscHelpPrintfDefault"
632c9a19010SBarry Smith /*@C
633c9a19010SBarry Smith      PetscHelpPrintf -  All PETSc help messages are passing through this function. You can change how help messages are printed by
634c9a19010SBarry Smith         replacinng it  with something that does not simply write to a stdout.
635c9a19010SBarry Smith 
636c9a19010SBarry Smith       To use, write your own function for example,
637c9a19010SBarry Smith $PetscErrorCode mypetschelpprintf(MPI_Comm comm,const char format[],....)
638c9a19010SBarry Smith ${
639c9a19010SBarry Smith $ PetscFunctionReturn(0);
640c9a19010SBarry Smith $}
641c9a19010SBarry Smith then before the call to PetscInitialize() do the assignment
642c9a19010SBarry Smith $    PetscHelpPrintf = mypetschelpprintf;
643c9a19010SBarry Smith 
644c9a19010SBarry Smith   Note: the default routine used is called PetscHelpPrintfDefault().
645c9a19010SBarry Smith 
646c9a19010SBarry Smith   Level:  developer
647c9a19010SBarry Smith 
648c9a19010SBarry Smith .seealso: PetscVSNPrintf(), PetscVFPrintf(), PetscErrorPrintf()
649c9a19010SBarry Smith @*/
6507087cfbeSBarry Smith PetscErrorCode  PetscHelpPrintfDefault(MPI_Comm comm,const char format[],...)
651e5c89e4eSSatish Balay {
652e5c89e4eSSatish Balay   PetscErrorCode ierr;
653e5c89e4eSSatish Balay   PetscMPIInt    rank;
654e5c89e4eSSatish Balay 
655e5c89e4eSSatish Balay   PetscFunctionBegin;
656e5c89e4eSSatish Balay   if (!comm) comm = PETSC_COMM_WORLD;
657e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
658e5c89e4eSSatish Balay   if (!rank) {
659e5c89e4eSSatish Balay     va_list Argp;
660e5c89e4eSSatish Balay     va_start(Argp,format);
6611179db26SBarry Smith     ierr = (*PetscVFPrintf)(PETSC_STDOUT,format,Argp);CHKERRQ(ierr);
662e5c89e4eSSatish Balay     if (petsc_history) {
663cdc7d174SSatish Balay       va_start(Argp,format);
6641179db26SBarry Smith       ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr);
665e5c89e4eSSatish Balay     }
666e5c89e4eSSatish Balay     va_end(Argp);
667e5c89e4eSSatish Balay   }
668e5c89e4eSSatish Balay   PetscFunctionReturn(0);
669e5c89e4eSSatish Balay }
670e5c89e4eSSatish Balay 
671e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------------------*/
672e5c89e4eSSatish Balay 
673e5c89e4eSSatish Balay 
674e5c89e4eSSatish Balay #undef __FUNCT__
675e5c89e4eSSatish Balay #define __FUNCT__ "PetscSynchronizedFGets"
676e5c89e4eSSatish Balay /*@C
677e5c89e4eSSatish Balay     PetscSynchronizedFGets - Several processors all get the same line from a file.
678e5c89e4eSSatish Balay 
679e5c89e4eSSatish Balay     Collective on MPI_Comm
680e5c89e4eSSatish Balay 
681e5c89e4eSSatish Balay     Input Parameters:
682e5c89e4eSSatish Balay +   comm - the communicator
683e5c89e4eSSatish Balay .   fd - the file pointer
684e5c89e4eSSatish Balay -   len - the length of the output buffer
685e5c89e4eSSatish Balay 
686e5c89e4eSSatish Balay     Output Parameter:
687e5c89e4eSSatish Balay .   string - the line read from the file
688e5c89e4eSSatish Balay 
689e5c89e4eSSatish Balay     Level: intermediate
690e5c89e4eSSatish Balay 
691e5c89e4eSSatish Balay .seealso: PetscSynchronizedPrintf(), PetscSynchronizedFlush(),
692e5c89e4eSSatish Balay           PetscFOpen(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIPrintf()
693e5c89e4eSSatish Balay 
694e5c89e4eSSatish Balay @*/
6957087cfbeSBarry Smith PetscErrorCode  PetscSynchronizedFGets(MPI_Comm comm,FILE* fp,size_t len,char string[])
696e5c89e4eSSatish Balay {
697e5c89e4eSSatish Balay   PetscErrorCode ierr;
698e5c89e4eSSatish Balay   PetscMPIInt    rank;
699e5c89e4eSSatish Balay 
700e5c89e4eSSatish Balay   PetscFunctionBegin;
701e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
702e5c89e4eSSatish Balay 
703e5c89e4eSSatish Balay   if (!rank) {
704047b9c12SMatthew G Knepley     char *ptr = fgets(string, len, fp);
705047b9c12SMatthew G Knepley 
706047b9c12SMatthew G Knepley     if (!ptr) {
707047b9c12SMatthew G Knepley       if (feof(fp)) {
708047b9c12SMatthew G Knepley         len = 0;
7094c2b986eSBarry Smith       } else SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_FILE_READ, "Error reading from file: %d", errno);
710047b9c12SMatthew G Knepley     }
711e5c89e4eSSatish Balay   }
712e5c89e4eSSatish Balay   ierr = MPI_Bcast(string,len,MPI_BYTE,0,comm);CHKERRQ(ierr);
713e5c89e4eSSatish Balay   PetscFunctionReturn(0);
714e5c89e4eSSatish Balay }
715238ccf28SShri Abhyankar 
716238ccf28SShri Abhyankar #if defined(PETSC_HAVE_MATLAB_ENGINE)
717c6db04a5SJed Brown #include <mex.h>
718238ccf28SShri Abhyankar #undef __FUNCT__
719238ccf28SShri Abhyankar #define __FUNCT__ "PetscVFPrintf_Matlab"
7207087cfbeSBarry Smith PetscErrorCode  PetscVFPrintf_Matlab(FILE *fd,const char format[],va_list Argp)
721238ccf28SShri Abhyankar {
722238ccf28SShri Abhyankar   PetscErrorCode ierr;
723238ccf28SShri Abhyankar 
724238ccf28SShri Abhyankar   PetscFunctionBegin;
725238ccf28SShri Abhyankar   if (fd != stdout && fd != stderr) { /* handle regular files */
726238ccf28SShri Abhyankar     ierr = PetscVFPrintfDefault(fd,format,Argp); CHKERRQ(ierr);
727238ccf28SShri Abhyankar   } else {
728238ccf28SShri Abhyankar     size_t len=8*1024,length;
729238ccf28SShri Abhyankar     char   buf[len];
730238ccf28SShri Abhyankar 
731238ccf28SShri Abhyankar     ierr = PetscVSNPrintf(buf,len,format,&length,Argp);CHKERRQ(ierr);
732df413903SBarry Smith     mexPrintf("%s",buf);
733238ccf28SShri Abhyankar  }
734238ccf28SShri Abhyankar  PetscFunctionReturn(0);
735238ccf28SShri Abhyankar }
736238ccf28SShri Abhyankar #endif
737