xref: /petsc/src/sys/fileio/mprint.c (revision 238ccf28d5b55217fb43d30eea286119e561c8cf)
1e5c89e4eSSatish Balay #define PETSC_DLL
2e5c89e4eSSatish Balay /*
3e5c89e4eSSatish Balay       Utilites routines to add simple ASCII IO capability.
4e5c89e4eSSatish Balay */
57c4f633dSBarry Smith #include "../src/sys/fileio/mprint.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 @*/
43c9a19010SBarry Smith PetscErrorCode PETSCSYS_DLLEXPORT 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++] = '%';
7165460251SBarry Smith #if defined(PETSC_USE_SCALAR_INT)
7203c60df9SBarry Smith       newformat[j++] = 'd';
7365460251SBarry Smith #elif !defined(PETSC_USE_SCALAR_LONG_DOUBLE)
74a83599f4SBarry Smith       newformat[j++] = 'g';
75a83599f4SBarry Smith #else
76a83599f4SBarry Smith       newformat[j++] = 'L';
77a83599f4SBarry Smith       newformat[j++] = 'g';
78a83599f4SBarry Smith #endif
79a83599f4SBarry Smith       i += 2;
80e5c89e4eSSatish Balay     }else {
81e5c89e4eSSatish Balay       newformat[j++] = format[i++];
82e5c89e4eSSatish Balay     }
83e5c89e4eSSatish Balay   }
84e5c89e4eSSatish Balay   newformat[j] = 0;
85e5c89e4eSSatish Balay   return 0;
86e5c89e4eSSatish Balay }
87e5c89e4eSSatish Balay 
88e5c89e4eSSatish Balay #undef __FUNCT__
89e5c89e4eSSatish Balay #define __FUNCT__ "PetscVSNPrintf"
90c9a19010SBarry Smith /*@C
91c9a19010SBarry Smith      PetscVSNPrintf - The PETSc version of vsnprintf(). Converts a PETSc format string into a standard C format string and then puts all the
92c9a19010SBarry Smith        function arguments into a string using the format statement.
93c9a19010SBarry Smith 
94c9a19010SBarry Smith    Input Parameters:
95c9a19010SBarry Smith +   str - location to put result
96c9a19010SBarry Smith .   len - the amount of space in str
97c9a19010SBarry Smith +   format - the PETSc format string
98c9a19010SBarry Smith -   fullLength - the amount of space in str actually used.
99c9a19010SBarry Smith 
100c9a19010SBarry Smith     Note:  No error handling because may be called by error handler
101c9a19010SBarry Smith 
102c9a19010SBarry Smith  Level: developer
103c9a19010SBarry Smith 
104c9a19010SBarry Smith @*/
105c9a19010SBarry Smith PetscErrorCode PETSCSYS_DLLEXPORT PetscVSNPrintf(char *str,size_t len,const char *format,size_t *fullLength,va_list Argp)
106e5c89e4eSSatish Balay {
107e5c89e4eSSatish Balay   /* no malloc since may be called by error handler */
108e2135aedSMatthew Knepley   char          *newformat;
109e2135aedSMatthew Knepley   char           formatbuf[8*1024];
110e2135aedSMatthew Knepley   size_t         oldLength,length;
1111a15ef5dSMatthew Knepley   PetscErrorCode ierr;
112e5c89e4eSSatish Balay 
113e2135aedSMatthew Knepley   ierr = PetscStrlen(format, &oldLength);CHKERRQ(ierr);
114e2135aedSMatthew Knepley   if (oldLength < 8*1024) {
115e2135aedSMatthew Knepley     newformat = formatbuf;
116e2135aedSMatthew Knepley   } else {
117e2135aedSMatthew Knepley     ierr = PetscMalloc((oldLength+1) * sizeof(char), &newformat);CHKERRQ(ierr);
118e2135aedSMatthew Knepley   }
119e2135aedSMatthew Knepley   PetscFormatConvert(format,newformat,oldLength+1);
1201a15ef5dSMatthew Knepley   ierr = PetscStrlen(newformat, &length);CHKERRQ(ierr);
1212d609e63SMatthew Knepley #if 0
1221a15ef5dSMatthew Knepley   if (length > len) {
1231a15ef5dSMatthew Knepley     newformat[len] = '\0';
1241a15ef5dSMatthew Knepley   }
1252d609e63SMatthew Knepley #endif
1265a058713SBarry Smith #if defined(PETSC_HAVE_VSNPRINTF_CHAR)
1272d609e63SMatthew Knepley   *fullLength = vsnprintf(str,len,newformat,(char *)Argp);
12889b07760SSatish Balay #elif defined(PETSC_HAVE_VSNPRINTF)
12989b07760SSatish Balay   *fullLength = vsnprintf(str,len,newformat,Argp);
130141c0d65SSatish Balay #elif defined(PETSC_HAVE__VSNPRINTF)
131141c0d65SSatish Balay   *fullLength = _vsnprintf(str,len,newformat,Argp);
132e5c89e4eSSatish Balay #else
13389b07760SSatish Balay #error "vsnprintf not found"
134e5c89e4eSSatish Balay #endif
135e2135aedSMatthew Knepley   if (oldLength >= 8*1024) {
136e2135aedSMatthew Knepley     ierr = PetscFree(newformat);CHKERRQ(ierr);
137e2135aedSMatthew Knepley   }
138e5c89e4eSSatish Balay   return 0;
139e5c89e4eSSatish Balay }
140e5c89e4eSSatish Balay 
141e5c89e4eSSatish Balay #undef __FUNCT__
1427ead4f01Sbcordonn #define __FUNCT__ "PetscZopeLog"
1438738c821SJed Brown PetscErrorCode PETSCSYS_DLLEXPORT PetscZopeLog(const char *format,va_list Argp)
1449c4c166aSBarry Smith {
1457ead4f01Sbcordonn   /* no malloc since may be called by error handler */
1467ead4f01Sbcordonn   char        newformat[8*1024];
1477ead4f01Sbcordonn   char        log[8*1024];
1487ead4f01Sbcordonn   char        logstart[] = " <<<log>>>";
149c9a19010SBarry Smith   size_t      len,formatlen;
1509c4c166aSBarry Smith 
1517ead4f01Sbcordonn   PetscFormatConvert(format,newformat,8*1024);
1527ead4f01Sbcordonn   PetscStrlen(logstart, &len);
1537ead4f01Sbcordonn   PetscMemcpy(log, logstart, len);
1547ead4f01Sbcordonn   PetscStrlen(newformat, &formatlen);
1557ead4f01Sbcordonn   PetscMemcpy(&(log[len]), newformat, formatlen);
1569c4c166aSBarry Smith   if (PETSC_ZOPEFD){
1575a058713SBarry Smith #if defined(PETSC_HAVE_VFPRINTF_CHAR)
1587ead4f01Sbcordonn     vfprintf(PETSC_ZOPEFD,log,(char *)Argp);
1597ead4f01Sbcordonn #else
1607ead4f01Sbcordonn     vfprintf(PETSC_ZOPEFD,log,Argp);
1617ead4f01Sbcordonn #endif
1625a058713SBarry Smith     fflush(PETSC_ZOPEFD);
1637ead4f01Sbcordonn   }
1647ead4f01Sbcordonn   return 0;
1657ead4f01Sbcordonn }
1667ead4f01Sbcordonn 
1677ead4f01Sbcordonn #undef __FUNCT__
168c9a19010SBarry Smith #define __FUNCT__ "PetscVFPrintfDefault"
169c9a19010SBarry Smith /*@C
170c9a19010SBarry Smith      PetscVFPrintf -  All PETSc standard out and error messages are sent through this function; so, in theory, this can
171e5c89e4eSSatish Balay         can be replaced with something that does not simply write to a file.
172e5c89e4eSSatish Balay 
173c9a19010SBarry Smith       To use, write your own function for example,
174c9a19010SBarry Smith $PetscErrorCode mypetscvfprintf(FILE *fd,const char format[],va_list Argp)
175c9a19010SBarry Smith ${
176c9a19010SBarry Smith $  PetscErrorCode ierr;
177c9a19010SBarry Smith $
178c9a19010SBarry Smith $  PetscFunctionBegin;
179c9a19010SBarry Smith $   if (fd != stdout && fd != stderr) {  handle regular files
180c9a19010SBarry Smith $      ierr = PetscVFPrintfDefault(fd,format,Argp); CHKERR(ierr);
181c9a19010SBarry Smith $  } else {
182c9a19010SBarry Smith $     char   buff[BIG];
183c9a19010SBarry Smith $     size_t length;
184c9a19010SBarry Smith $     ierr = PetscVSNPrintf(buff,BIG,format,&length,Argp);CHKERRQ(ierr);
185c9a19010SBarry Smith $     now send buff to whatever stream or whatever you want
186c9a19010SBarry Smith $ }
187c9a19010SBarry Smith $ PetscFunctionReturn(0);
188c9a19010SBarry Smith $}
189c9a19010SBarry Smith then before the call to PetscInitialize() do the assignment
190c9a19010SBarry Smith $    PetscVFPrintf = mypetscvfprintf;
191c9a19010SBarry Smith 
192c9a19010SBarry Smith       Notes: For error messages this may be called by any process, for regular standard out it is
193e5c89e4eSSatish Balay           called only by process 0 of a given communicator
194e5c89e4eSSatish Balay 
195e5c89e4eSSatish Balay       No error handling because may be called by error handler
196c9a19010SBarry Smith 
197c9a19010SBarry Smith   Level:  developer
198c9a19010SBarry Smith 
199c9a19010SBarry Smith .seealso: PetscVSNPrintf(), PetscErrorPrintf()
200c9a19010SBarry Smith 
201c9a19010SBarry Smith @*/
2028738c821SJed Brown PetscErrorCode PETSCSYS_DLLEXPORT PetscVFPrintfDefault(FILE *fd,const char *format,va_list Argp)
203e5c89e4eSSatish Balay {
204e2135aedSMatthew Knepley   /* no malloc since may be called by error handler (assume no long messages in errors) */
205e2135aedSMatthew Knepley   char        *newformat;
206e2135aedSMatthew Knepley   char         formatbuf[8*1024];
207e2135aedSMatthew Knepley   size_t       oldLength;
2081179db26SBarry Smith 
209e2135aedSMatthew Knepley   PetscStrlen(format, &oldLength);
210e2135aedSMatthew Knepley   if (oldLength < 8*1024) {
211e2135aedSMatthew Knepley     newformat = formatbuf;
212e2135aedSMatthew Knepley   } else {
213fee1560eSJed Brown     (void)PetscMalloc((oldLength+1) * sizeof(char), &newformat);
214e2135aedSMatthew Knepley   }
215e2135aedSMatthew Knepley   PetscFormatConvert(format,newformat,oldLength+1);
2169c4c166aSBarry Smith   if (PETSC_ZOPEFD && PETSC_ZOPEFD != PETSC_STDOUT){
217d8c6e182Sbcordonn     va_list s;
218738b3be2SBarry Smith #if defined(PETSC_HAVE_VA_COPY)
219d8c6e182Sbcordonn     va_copy(s, Argp);
220738b3be2SBarry Smith #elif defined(PETSC_HAVE___VA_COPY)
221738b3be2SBarry Smith     __va_copy(s, Argp);
222738b3be2SBarry Smith #else
223e32f2f54SBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Zope not supported due to missing va_copy()");
224738b3be2SBarry Smith #endif
225738b3be2SBarry Smith 
2266adf639fSBarry Smith #if defined(PETSC_HAVE_VA_COPY) || defined(PETSC_HAVE___VA_COPY)
2275a058713SBarry Smith #if defined(PETSC_HAVE_VFPRINTF_CHAR)
228d8c6e182Sbcordonn     vfprintf(PETSC_ZOPEFD,newformat,(char *)s);
229d8c6e182Sbcordonn #else
230d8c6e182Sbcordonn     vfprintf(PETSC_ZOPEFD,newformat,s);
231d8c6e182Sbcordonn #endif
2325a058713SBarry Smith     fflush(PETSC_ZOPEFD);
2330cd8598bSSatish Balay #endif
234d8c6e182Sbcordonn   }
235d8c6e182Sbcordonn 
2365a058713SBarry Smith #if defined(PETSC_HAVE_VFPRINTF_CHAR)
237e5c89e4eSSatish Balay   vfprintf(fd,newformat,(char *)Argp);
238e5c89e4eSSatish Balay #else
239e5c89e4eSSatish Balay   vfprintf(fd,newformat,Argp);
240e5c89e4eSSatish Balay #endif
2415a058713SBarry Smith   fflush(fd);
242e2135aedSMatthew Knepley   if (oldLength >= 8*1024) {
2432f0d4f1eSMatthew Knepley     if (PetscFree(newformat)) {};
244e2135aedSMatthew Knepley   }
245e5c89e4eSSatish Balay   return 0;
246e5c89e4eSSatish Balay }
247e5c89e4eSSatish Balay 
2485b5bc046SBarry Smith #undef __FUNCT__
2495b5bc046SBarry Smith #define __FUNCT__ "PetscSNPrintf"
2505b5bc046SBarry Smith /*@C
2515b5bc046SBarry Smith     PetscSNPrintf - Prints to a string of given length
2525b5bc046SBarry Smith 
2535b5bc046SBarry Smith     Not Collective
2545b5bc046SBarry Smith 
2555b5bc046SBarry Smith     Input Parameters:
2565b5bc046SBarry Smith +   str - the string to print to
2575b5bc046SBarry Smith .   len - the length of str
2585b5bc046SBarry Smith .   format - the usual printf() format string
2595b5bc046SBarry Smith -   any arguments
2605b5bc046SBarry Smith 
2615b5bc046SBarry Smith    Level: intermediate
2625b5bc046SBarry Smith 
2635b5bc046SBarry Smith .seealso: PetscSynchronizedFlush(), PetscSynchronizedFPrintf(), PetscFPrintf(), PetscVSNPrintf(),
2645b5bc046SBarry Smith           PetscPrintf(), PetscViewerASCIIPrintf(), PetscViewerASCIISynchronizedPrintf()
2655b5bc046SBarry Smith @*/
2668738c821SJed Brown PetscErrorCode PETSCSYS_DLLEXPORT PetscSNPrintf(char *str,size_t len,const char format[],...)
2675b5bc046SBarry Smith {
2685b5bc046SBarry Smith   PetscErrorCode ierr;
269c9a19010SBarry Smith   size_t         fullLength;
2705b5bc046SBarry Smith   va_list        Argp;
2715b5bc046SBarry Smith 
2725b5bc046SBarry Smith   PetscFunctionBegin;
2735b5bc046SBarry Smith   va_start(Argp,format);
2742d609e63SMatthew Knepley   ierr = PetscVSNPrintf(str,len,format,&fullLength,Argp);CHKERRQ(ierr);
2755b5bc046SBarry Smith   PetscFunctionReturn(0);
2765b5bc046SBarry Smith }
2775b5bc046SBarry Smith 
278e5c89e4eSSatish Balay /* ----------------------------------------------------------------------- */
279e5c89e4eSSatish Balay 
280e5c89e4eSSatish Balay PrintfQueue queue       = 0,queuebase = 0;
281e5c89e4eSSatish Balay int         queuelength = 0;
282e5c89e4eSSatish Balay FILE        *queuefile  = PETSC_NULL;
283e5c89e4eSSatish Balay 
284e5c89e4eSSatish Balay #undef __FUNCT__
285e5c89e4eSSatish Balay #define __FUNCT__ "PetscSynchronizedPrintf"
286e5c89e4eSSatish Balay /*@C
287e5c89e4eSSatish Balay     PetscSynchronizedPrintf - Prints synchronized output from several processors.
288e5c89e4eSSatish Balay     Output of the first processor is followed by that of the second, etc.
289e5c89e4eSSatish Balay 
290e5c89e4eSSatish Balay     Not Collective
291e5c89e4eSSatish Balay 
292e5c89e4eSSatish Balay     Input Parameters:
293e5c89e4eSSatish Balay +   comm - the communicator
294e5c89e4eSSatish Balay -   format - the usual printf() format string
295e5c89e4eSSatish Balay 
296e5c89e4eSSatish Balay    Level: intermediate
297e5c89e4eSSatish Balay 
298e5c89e4eSSatish Balay     Notes:
299e5c89e4eSSatish Balay     REQUIRES a intervening call to PetscSynchronizedFlush() for the information
300e5c89e4eSSatish Balay     from all the processors to be printed.
301e5c89e4eSSatish Balay 
302e5c89e4eSSatish Balay     Fortran Note:
303d60d70ccSBarry Smith     The call sequence is PetscSynchronizedPrintf(MPI_Comm, character(*), PetscErrorCode ierr) from Fortran.
304e5c89e4eSSatish Balay     That is, you can only pass a single character string from Fortran.
305e5c89e4eSSatish Balay 
306e5c89e4eSSatish Balay .seealso: PetscSynchronizedFlush(), PetscSynchronizedFPrintf(), PetscFPrintf(),
307e5c89e4eSSatish Balay           PetscPrintf(), PetscViewerASCIIPrintf(), PetscViewerASCIISynchronizedPrintf()
308e5c89e4eSSatish Balay @*/
3098738c821SJed Brown PetscErrorCode PETSCSYS_DLLEXPORT PetscSynchronizedPrintf(MPI_Comm comm,const char format[],...)
310e5c89e4eSSatish Balay {
311e5c89e4eSSatish Balay   PetscErrorCode ierr;
312e5c89e4eSSatish Balay   PetscMPIInt    rank;
313e5c89e4eSSatish Balay 
314e5c89e4eSSatish Balay   PetscFunctionBegin;
315e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
316e5c89e4eSSatish Balay 
317e5c89e4eSSatish Balay   /* First processor prints immediately to stdout */
318e5c89e4eSSatish Balay   if (!rank) {
319e5c89e4eSSatish Balay     va_list Argp;
320e5c89e4eSSatish Balay     va_start(Argp,format);
3211179db26SBarry Smith     ierr = (*PetscVFPrintf)(PETSC_STDOUT,format,Argp);CHKERRQ(ierr);
322e5c89e4eSSatish Balay     if (petsc_history) {
323cdc7d174SSatish Balay       va_start(Argp,format);
3241179db26SBarry Smith       ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr);
325e5c89e4eSSatish Balay     }
326e5c89e4eSSatish Balay     va_end(Argp);
327e5c89e4eSSatish Balay   } else { /* other processors add to local queue */
328e5c89e4eSSatish Balay     va_list     Argp;
329e5c89e4eSSatish Balay     PrintfQueue next;
330c9a19010SBarry Smith     size_t      fullLength = 8191;
331e5c89e4eSSatish Balay 
332e5c89e4eSSatish Balay     ierr = PetscNew(struct _PrintfQueue,&next);CHKERRQ(ierr);
333e5c89e4eSSatish Balay     if (queue) {queue->next = next; queue = next; queue->next = 0;}
334e5c89e4eSSatish Balay     else       {queuebase   = queue = next;}
335e5c89e4eSSatish Balay     queuelength++;
3362d609e63SMatthew Knepley     next->size = -1;
3379ad23270SJed Brown     while((PetscInt)fullLength >= next->size) {
3382d609e63SMatthew Knepley       next->size = fullLength+1;
3392d609e63SMatthew Knepley       ierr = PetscMalloc(next->size * sizeof(char), &next->string);CHKERRQ(ierr);
340e5c89e4eSSatish Balay       va_start(Argp,format);
3412d609e63SMatthew Knepley       ierr = PetscMemzero(next->string,next->size);CHKERRQ(ierr);
3422d609e63SMatthew Knepley       ierr = PetscVSNPrintf(next->string,next->size,format, &fullLength,Argp);CHKERRQ(ierr);
343e5c89e4eSSatish Balay       va_end(Argp);
344e5c89e4eSSatish Balay     }
3452d609e63SMatthew Knepley   }
346e5c89e4eSSatish Balay 
347e5c89e4eSSatish Balay   PetscFunctionReturn(0);
348e5c89e4eSSatish Balay }
349e5c89e4eSSatish Balay 
350e5c89e4eSSatish Balay #undef __FUNCT__
351e5c89e4eSSatish Balay #define __FUNCT__ "PetscSynchronizedFPrintf"
352e5c89e4eSSatish Balay /*@C
353e5c89e4eSSatish Balay     PetscSynchronizedFPrintf - Prints synchronized output to the specified file from
354e5c89e4eSSatish Balay     several processors.  Output of the first processor is followed by that of the
355e5c89e4eSSatish Balay     second, etc.
356e5c89e4eSSatish Balay 
357e5c89e4eSSatish Balay     Not Collective
358e5c89e4eSSatish Balay 
359e5c89e4eSSatish Balay     Input Parameters:
360e5c89e4eSSatish Balay +   comm - the communicator
361e5c89e4eSSatish Balay .   fd - the file pointer
362e5c89e4eSSatish Balay -   format - the usual printf() format string
363e5c89e4eSSatish Balay 
364e5c89e4eSSatish Balay     Level: intermediate
365e5c89e4eSSatish Balay 
366e5c89e4eSSatish Balay     Notes:
367e5c89e4eSSatish Balay     REQUIRES a intervening call to PetscSynchronizedFlush() for the information
368e5c89e4eSSatish Balay     from all the processors to be printed.
369e5c89e4eSSatish Balay 
370e5c89e4eSSatish Balay .seealso: PetscSynchronizedPrintf(), PetscSynchronizedFlush(), PetscFPrintf(),
371e5c89e4eSSatish Balay           PetscFOpen(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIPrintf()
372e5c89e4eSSatish Balay 
373e5c89e4eSSatish Balay @*/
3748738c821SJed Brown PetscErrorCode PETSCSYS_DLLEXPORT PetscSynchronizedFPrintf(MPI_Comm comm,FILE* fp,const char format[],...)
375e5c89e4eSSatish Balay {
376e5c89e4eSSatish Balay   PetscErrorCode ierr;
377e5c89e4eSSatish Balay   PetscMPIInt    rank;
378e5c89e4eSSatish Balay 
379e5c89e4eSSatish Balay   PetscFunctionBegin;
380e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
381e5c89e4eSSatish Balay 
382e5c89e4eSSatish Balay   /* First processor prints immediately to fp */
383e5c89e4eSSatish Balay   if (!rank) {
384e5c89e4eSSatish Balay     va_list Argp;
385e5c89e4eSSatish Balay     va_start(Argp,format);
3861179db26SBarry Smith     ierr = (*PetscVFPrintf)(fp,format,Argp);CHKERRQ(ierr);
387e5c89e4eSSatish Balay     queuefile = fp;
388cdc7d174SSatish Balay     if (petsc_history && (fp !=petsc_history)) {
389cdc7d174SSatish Balay       va_start(Argp,format);
3901179db26SBarry Smith       ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr);
391e5c89e4eSSatish Balay     }
392e5c89e4eSSatish Balay     va_end(Argp);
393e5c89e4eSSatish Balay   } else { /* other processors add to local queue */
394e5c89e4eSSatish Balay     va_list     Argp;
395e5c89e4eSSatish Balay     PrintfQueue next;
396c9a19010SBarry Smith     size_t      fullLength = 8191;
397e5c89e4eSSatish Balay     ierr = PetscNew(struct _PrintfQueue,&next);CHKERRQ(ierr);
398e5c89e4eSSatish Balay     if (queue) {queue->next = next; queue = next; queue->next = 0;}
399e5c89e4eSSatish Balay     else       {queuebase   = queue = next;}
400e5c89e4eSSatish Balay     queuelength++;
4012d609e63SMatthew Knepley     next->size = -1;
4029ad23270SJed Brown     while((PetscInt)fullLength >= next->size) {
4032d609e63SMatthew Knepley       next->size = fullLength+1;
4042d609e63SMatthew Knepley       ierr = PetscMalloc(next->size * sizeof(char), &next->string);CHKERRQ(ierr);
405e5c89e4eSSatish Balay       va_start(Argp,format);
4062d609e63SMatthew Knepley       ierr = PetscMemzero(next->string,next->size);CHKERRQ(ierr);
4072d609e63SMatthew Knepley       ierr = PetscVSNPrintf(next->string,next->size,format,&fullLength,Argp);CHKERRQ(ierr);
408e5c89e4eSSatish Balay       va_end(Argp);
409e5c89e4eSSatish Balay     }
4102d609e63SMatthew Knepley   }
411e5c89e4eSSatish Balay   PetscFunctionReturn(0);
412e5c89e4eSSatish Balay }
413e5c89e4eSSatish Balay 
414e5c89e4eSSatish Balay #undef __FUNCT__
415e5c89e4eSSatish Balay #define __FUNCT__ "PetscSynchronizedFlush"
416e30d2299SSatish Balay /*@
417e5c89e4eSSatish Balay     PetscSynchronizedFlush - Flushes to the screen output from all processors
418e5c89e4eSSatish Balay     involved in previous PetscSynchronizedPrintf() calls.
419e5c89e4eSSatish Balay 
420e5c89e4eSSatish Balay     Collective on MPI_Comm
421e5c89e4eSSatish Balay 
422e5c89e4eSSatish Balay     Input Parameters:
423e5c89e4eSSatish Balay .   comm - the communicator
424e5c89e4eSSatish Balay 
425e5c89e4eSSatish Balay     Level: intermediate
426e5c89e4eSSatish Balay 
427e5c89e4eSSatish Balay     Notes:
428e5c89e4eSSatish Balay     Usage of PetscSynchronizedPrintf() and PetscSynchronizedFPrintf() with
429e5c89e4eSSatish Balay     different MPI communicators REQUIRES an intervening call to PetscSynchronizedFlush().
430e5c89e4eSSatish Balay 
431e5c89e4eSSatish Balay .seealso: PetscSynchronizedPrintf(), PetscFPrintf(), PetscPrintf(), PetscViewerASCIIPrintf(),
432e5c89e4eSSatish Balay           PetscViewerASCIISynchronizedPrintf()
433e5c89e4eSSatish Balay @*/
4348738c821SJed Brown PetscErrorCode PETSCSYS_DLLEXPORT PetscSynchronizedFlush(MPI_Comm comm)
435e5c89e4eSSatish Balay {
436e5c89e4eSSatish Balay   PetscErrorCode ierr;
437bf3b0749SBarry Smith   PetscMPIInt    rank,size,tag,i,j,n,dummy = 0;
4382d609e63SMatthew Knepley   char          *message;
439e5c89e4eSSatish Balay   MPI_Status     status;
440e5c89e4eSSatish Balay   FILE           *fd;
441e5c89e4eSSatish Balay 
442e5c89e4eSSatish Balay   PetscFunctionBegin;
443e5c89e4eSSatish Balay   ierr = PetscCommDuplicate(comm,&comm,&tag);CHKERRQ(ierr);
444e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
445e5c89e4eSSatish Balay   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
446e5c89e4eSSatish Balay 
447e5c89e4eSSatish Balay   /* First processor waits for messages from all other processors */
448e5c89e4eSSatish Balay   if (!rank) {
449e5c89e4eSSatish Balay     if (queuefile) {
450e5c89e4eSSatish Balay       fd = queuefile;
451e5c89e4eSSatish Balay     } else {
452e5c89e4eSSatish Balay       fd = PETSC_STDOUT;
453e5c89e4eSSatish Balay     }
454e5c89e4eSSatish Balay     for (i=1; i<size; i++) {
4559f73f8ecSBarry Smith       /* to prevent a flood of messages to process zero, request each message separately */
4569f73f8ecSBarry Smith       ierr = MPI_Send(&dummy,1,MPI_INT,i,tag,comm);CHKERRQ(ierr);
457e5c89e4eSSatish Balay       ierr = MPI_Recv(&n,1,MPI_INT,i,tag,comm,&status);CHKERRQ(ierr);
458e5c89e4eSSatish Balay       for (j=0; j<n; j++) {
4599f73f8ecSBarry Smith         PetscMPIInt size;
4602d609e63SMatthew Knepley 
4612d609e63SMatthew Knepley         ierr = MPI_Recv(&size,1,MPI_INT,i,tag,comm,&status);CHKERRQ(ierr);
4622d609e63SMatthew Knepley         ierr = PetscMalloc(size * sizeof(char), &message);CHKERRQ(ierr);
4632d609e63SMatthew Knepley         ierr = MPI_Recv(message,size,MPI_CHAR,i,tag,comm,&status);CHKERRQ(ierr);
464e5c89e4eSSatish Balay         ierr = PetscFPrintf(comm,fd,"%s",message);
4652d609e63SMatthew Knepley         ierr = PetscFree(message);CHKERRQ(ierr);
466e5c89e4eSSatish Balay       }
467e5c89e4eSSatish Balay     }
468e5c89e4eSSatish Balay     queuefile = PETSC_NULL;
469e5c89e4eSSatish Balay   } else { /* other processors send queue to processor 0 */
470e5c89e4eSSatish Balay     PrintfQueue next = queuebase,previous;
471e5c89e4eSSatish Balay 
472b3ef9d35SBarry Smith     ierr = MPI_Recv(&dummy,1,MPI_INT,0,tag,comm,&status);CHKERRQ(ierr);
473e5c89e4eSSatish Balay     ierr = MPI_Send(&queuelength,1,MPI_INT,0,tag,comm);CHKERRQ(ierr);
474e5c89e4eSSatish Balay     for (i=0; i<queuelength; i++) {
4752d609e63SMatthew Knepley       ierr     = MPI_Send(&next->size,1,MPI_INT,0,tag,comm);CHKERRQ(ierr);
4762d609e63SMatthew Knepley       ierr     = MPI_Send(next->string,next->size,MPI_CHAR,0,tag,comm);CHKERRQ(ierr);
477e5c89e4eSSatish Balay       previous = next;
478e5c89e4eSSatish Balay       next     = next->next;
4792d609e63SMatthew Knepley       ierr     = PetscFree(previous->string);CHKERRQ(ierr);
480e5c89e4eSSatish Balay       ierr     = PetscFree(previous);CHKERRQ(ierr);
481e5c89e4eSSatish Balay     }
482e5c89e4eSSatish Balay     queue       = 0;
483e5c89e4eSSatish Balay     queuelength = 0;
484e5c89e4eSSatish Balay   }
485e5c89e4eSSatish Balay   ierr = PetscCommDestroy(&comm);CHKERRQ(ierr);
486e5c89e4eSSatish Balay   PetscFunctionReturn(0);
487e5c89e4eSSatish Balay }
488e5c89e4eSSatish Balay 
489e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------------------*/
490e5c89e4eSSatish Balay 
491e5c89e4eSSatish Balay #undef __FUNCT__
492e5c89e4eSSatish Balay #define __FUNCT__ "PetscFPrintf"
493e5c89e4eSSatish Balay /*@C
494e5c89e4eSSatish Balay     PetscFPrintf - Prints to a file, only from the first
495e5c89e4eSSatish Balay     processor in the communicator.
496e5c89e4eSSatish Balay 
497e5c89e4eSSatish Balay     Not Collective
498e5c89e4eSSatish Balay 
499e5c89e4eSSatish Balay     Input Parameters:
500e5c89e4eSSatish Balay +   comm - the communicator
501e5c89e4eSSatish Balay .   fd - the file pointer
502e5c89e4eSSatish Balay -   format - the usual printf() format string
503e5c89e4eSSatish Balay 
504e5c89e4eSSatish Balay     Level: intermediate
505e5c89e4eSSatish Balay 
506e5c89e4eSSatish Balay     Fortran Note:
507e5c89e4eSSatish Balay     This routine is not supported in Fortran.
508e5c89e4eSSatish Balay 
509e5c89e4eSSatish Balay    Concepts: printing^in parallel
510e5c89e4eSSatish Balay    Concepts: printf^in parallel
511e5c89e4eSSatish Balay 
512e5c89e4eSSatish Balay .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(),
513e5c89e4eSSatish Balay           PetscViewerASCIISynchronizedPrintf(), PetscSynchronizedFlush()
514e5c89e4eSSatish Balay @*/
5158738c821SJed Brown PetscErrorCode PETSCSYS_DLLEXPORT PetscFPrintf(MPI_Comm comm,FILE* fd,const char format[],...)
516e5c89e4eSSatish Balay {
517e5c89e4eSSatish Balay   PetscErrorCode ierr;
518e5c89e4eSSatish Balay   PetscMPIInt    rank;
519e5c89e4eSSatish Balay 
520e5c89e4eSSatish Balay   PetscFunctionBegin;
521e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
522e5c89e4eSSatish Balay   if (!rank) {
523e5c89e4eSSatish Balay     va_list Argp;
524e5c89e4eSSatish Balay     va_start(Argp,format);
5251179db26SBarry Smith     ierr = (*PetscVFPrintf)(fd,format,Argp);CHKERRQ(ierr);
526cdc7d174SSatish Balay     if (petsc_history && (fd !=petsc_history)) {
527cdc7d174SSatish Balay       va_start(Argp,format);
5281179db26SBarry Smith       ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr);
529e5c89e4eSSatish Balay       }
530e5c89e4eSSatish Balay     va_end(Argp);
531e5c89e4eSSatish Balay   }
532e5c89e4eSSatish Balay   PetscFunctionReturn(0);
533e5c89e4eSSatish Balay }
534e5c89e4eSSatish Balay 
535e5c89e4eSSatish Balay #undef __FUNCT__
536e5c89e4eSSatish Balay #define __FUNCT__ "PetscPrintf"
537e5c89e4eSSatish Balay /*@C
538e5c89e4eSSatish Balay     PetscPrintf - Prints to standard out, only from the first
539e5c89e4eSSatish Balay     processor in the communicator.
540e5c89e4eSSatish Balay 
541e5c89e4eSSatish Balay     Not Collective
542e5c89e4eSSatish Balay 
543e5c89e4eSSatish Balay     Input Parameters:
544e5c89e4eSSatish Balay +   comm - the communicator
545e5c89e4eSSatish Balay -   format - the usual printf() format string
546e5c89e4eSSatish Balay 
547e5c89e4eSSatish Balay    Level: intermediate
548e5c89e4eSSatish Balay 
549e5c89e4eSSatish Balay     Fortran Note:
550d60d70ccSBarry Smith     The call sequence is PetscPrintf(MPI_Comm, character(*), PetscErrorCode ierr) from Fortran.
551e5c89e4eSSatish Balay     That is, you can only pass a single character string from Fortran.
552e5c89e4eSSatish Balay 
5538884cb1fSJed Brown    Notes: The %A format specifier is special.  It assumes an argument of type PetscReal
5548884cb1fSJed Brown           and is replaced with %G unless the absolute value is < 1.e-12 when it is replaced
5558884cb1fSJed Brown           with "< 1.e-12" (1.e-6 for single precision).
556e5c89e4eSSatish Balay 
557e5c89e4eSSatish Balay    Concepts: printing^in parallel
558e5c89e4eSSatish Balay    Concepts: printf^in parallel
559e5c89e4eSSatish Balay 
560e5c89e4eSSatish Balay .seealso: PetscFPrintf(), PetscSynchronizedPrintf()
561e5c89e4eSSatish Balay @*/
5628738c821SJed Brown PetscErrorCode PETSCSYS_DLLEXPORT PetscPrintf(MPI_Comm comm,const char format[],...)
563e5c89e4eSSatish Balay {
564e5c89e4eSSatish Balay   PetscErrorCode ierr;
565e5c89e4eSSatish Balay   PetscMPIInt    rank;
566e5c89e4eSSatish Balay   size_t         len;
567e5c89e4eSSatish Balay   char           *nformat,*sub1,*sub2;
568e5c89e4eSSatish Balay   PetscReal      value;
569e5c89e4eSSatish Balay 
570e5c89e4eSSatish Balay   PetscFunctionBegin;
571e5c89e4eSSatish Balay   if (!comm) comm = PETSC_COMM_WORLD;
572e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
573e5c89e4eSSatish Balay   if (!rank) {
574e5c89e4eSSatish Balay     va_list Argp;
575e5c89e4eSSatish Balay     va_start(Argp,format);
576e5c89e4eSSatish Balay 
577e5c89e4eSSatish Balay     ierr = PetscStrstr(format,"%A",&sub1);CHKERRQ(ierr);
578e5c89e4eSSatish Balay     if (sub1) {
579e5c89e4eSSatish Balay       ierr = PetscStrstr(format,"%",&sub2);CHKERRQ(ierr);
580e32f2f54SBarry Smith       if (sub1 != sub2) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"%%A format must be first in format string");
581e5c89e4eSSatish Balay       ierr    = PetscStrlen(format,&len);CHKERRQ(ierr);
582e5c89e4eSSatish Balay       ierr    = PetscMalloc((len+16)*sizeof(char),&nformat);CHKERRQ(ierr);
583e5c89e4eSSatish Balay       ierr    = PetscStrcpy(nformat,format);CHKERRQ(ierr);
584e5c89e4eSSatish Balay       ierr    = PetscStrstr(nformat,"%",&sub2);CHKERRQ(ierr);
585e5c89e4eSSatish Balay       sub2[0] = 0;
58692eff512SVictor Minden       value   = va_arg(Argp,double);
5878884cb1fSJed Brown #if defined(PETSC_USE_SCALAR_SINGLE)
5888884cb1fSJed Brown       if (PetscAbsReal(value) < 1.e-6) {
5898884cb1fSJed Brown         ierr    = PetscStrcat(nformat,"< 1.e-6");CHKERRQ(ierr);
5908884cb1fSJed Brown #else
591e5c89e4eSSatish Balay       if (PetscAbsReal(value) < 1.e-12) {
592e5c89e4eSSatish Balay         ierr    = PetscStrcat(nformat,"< 1.e-12");CHKERRQ(ierr);
5938884cb1fSJed Brown #endif
594e5c89e4eSSatish Balay       } else {
5958884cb1fSJed Brown         ierr    = PetscStrcat(nformat,"%G");CHKERRQ(ierr);
596e5c89e4eSSatish Balay         va_end(Argp);
597e5c89e4eSSatish Balay         va_start(Argp,format);
598e5c89e4eSSatish Balay       }
599e5c89e4eSSatish Balay       ierr    = PetscStrcat(nformat,sub1+2);CHKERRQ(ierr);
600e5c89e4eSSatish Balay     } else {
601e5c89e4eSSatish Balay       nformat = (char*)format;
602e5c89e4eSSatish Balay     }
6031179db26SBarry Smith     ierr = (*PetscVFPrintf)(PETSC_STDOUT,nformat,Argp);CHKERRQ(ierr);
604e5c89e4eSSatish Balay     if (petsc_history) {
605cdc7d174SSatish Balay       va_start(Argp,format);
6061179db26SBarry Smith       ierr = (*PetscVFPrintf)(petsc_history,nformat,Argp);CHKERRQ(ierr);
607e5c89e4eSSatish Balay     }
608e5c89e4eSSatish Balay     va_end(Argp);
609e5c89e4eSSatish Balay     if (sub1) {ierr = PetscFree(nformat);CHKERRQ(ierr);}
610e5c89e4eSSatish Balay   }
611e5c89e4eSSatish Balay   PetscFunctionReturn(0);
612e5c89e4eSSatish Balay }
613e5c89e4eSSatish Balay 
614e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------------------*/
615e5c89e4eSSatish Balay #undef __FUNCT__
616e5c89e4eSSatish Balay #define __FUNCT__ "PetscHelpPrintfDefault"
617c9a19010SBarry Smith /*@C
618c9a19010SBarry Smith      PetscHelpPrintf -  All PETSc help messages are passing through this function. You can change how help messages are printed by
619c9a19010SBarry Smith         replacinng it  with something that does not simply write to a stdout.
620c9a19010SBarry Smith 
621c9a19010SBarry Smith       To use, write your own function for example,
622c9a19010SBarry Smith $PetscErrorCode mypetschelpprintf(MPI_Comm comm,const char format[],....)
623c9a19010SBarry Smith ${
624c9a19010SBarry Smith $ PetscFunctionReturn(0);
625c9a19010SBarry Smith $}
626c9a19010SBarry Smith then before the call to PetscInitialize() do the assignment
627c9a19010SBarry Smith $    PetscHelpPrintf = mypetschelpprintf;
628c9a19010SBarry Smith 
629c9a19010SBarry Smith   Note: the default routine used is called PetscHelpPrintfDefault().
630c9a19010SBarry Smith 
631c9a19010SBarry Smith   Level:  developer
632c9a19010SBarry Smith 
633c9a19010SBarry Smith .seealso: PetscVSNPrintf(), PetscVFPrintf(), PetscErrorPrintf()
634c9a19010SBarry Smith @*/
6358738c821SJed Brown PetscErrorCode PETSCSYS_DLLEXPORT PetscHelpPrintfDefault(MPI_Comm comm,const char format[],...)
636e5c89e4eSSatish Balay {
637e5c89e4eSSatish Balay   PetscErrorCode ierr;
638e5c89e4eSSatish Balay   PetscMPIInt    rank;
639e5c89e4eSSatish Balay 
640e5c89e4eSSatish Balay   PetscFunctionBegin;
641e5c89e4eSSatish Balay   if (!comm) comm = PETSC_COMM_WORLD;
642e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
643e5c89e4eSSatish Balay   if (!rank) {
644e5c89e4eSSatish Balay     va_list Argp;
645e5c89e4eSSatish Balay     va_start(Argp,format);
6461179db26SBarry Smith     ierr = (*PetscVFPrintf)(PETSC_STDOUT,format,Argp);CHKERRQ(ierr);
647e5c89e4eSSatish Balay     if (petsc_history) {
648cdc7d174SSatish Balay       va_start(Argp,format);
6491179db26SBarry Smith       ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr);
650e5c89e4eSSatish Balay     }
651e5c89e4eSSatish Balay     va_end(Argp);
652e5c89e4eSSatish Balay   }
653e5c89e4eSSatish Balay   PetscFunctionReturn(0);
654e5c89e4eSSatish Balay }
655e5c89e4eSSatish Balay 
656e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------------------*/
657e5c89e4eSSatish Balay 
658e5c89e4eSSatish Balay 
659e5c89e4eSSatish Balay #undef __FUNCT__
660e5c89e4eSSatish Balay #define __FUNCT__ "PetscSynchronizedFGets"
661e5c89e4eSSatish Balay /*@C
662e5c89e4eSSatish Balay     PetscSynchronizedFGets - Several processors all get the same line from a file.
663e5c89e4eSSatish Balay 
664e5c89e4eSSatish Balay     Collective on MPI_Comm
665e5c89e4eSSatish Balay 
666e5c89e4eSSatish Balay     Input Parameters:
667e5c89e4eSSatish Balay +   comm - the communicator
668e5c89e4eSSatish Balay .   fd - the file pointer
669e5c89e4eSSatish Balay -   len - the length of the output buffer
670e5c89e4eSSatish Balay 
671e5c89e4eSSatish Balay     Output Parameter:
672e5c89e4eSSatish Balay .   string - the line read from the file
673e5c89e4eSSatish Balay 
674e5c89e4eSSatish Balay     Level: intermediate
675e5c89e4eSSatish Balay 
676e5c89e4eSSatish Balay .seealso: PetscSynchronizedPrintf(), PetscSynchronizedFlush(),
677e5c89e4eSSatish Balay           PetscFOpen(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIPrintf()
678e5c89e4eSSatish Balay 
679e5c89e4eSSatish Balay @*/
6808738c821SJed Brown PetscErrorCode PETSCSYS_DLLEXPORT PetscSynchronizedFGets(MPI_Comm comm,FILE* fp,size_t len,char string[])
681e5c89e4eSSatish Balay {
682e5c89e4eSSatish Balay   PetscErrorCode ierr;
683e5c89e4eSSatish Balay   PetscMPIInt    rank;
68415536cb6SHong Zhang   char           *str;
685e5c89e4eSSatish Balay 
686e5c89e4eSSatish Balay   PetscFunctionBegin;
687e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
688e5c89e4eSSatish Balay 
689e5c89e4eSSatish Balay   if (!rank) {
69015536cb6SHong Zhang     str = fgets(string,len,fp); /* Not very useful error behavior, but what is desired behavior for attempt to read at EOF? */
691e5c89e4eSSatish Balay   }
692e5c89e4eSSatish Balay   ierr = MPI_Bcast(string,len,MPI_BYTE,0,comm);CHKERRQ(ierr);
693e5c89e4eSSatish Balay   PetscFunctionReturn(0);
694e5c89e4eSSatish Balay }
695*238ccf28SShri Abhyankar 
696*238ccf28SShri Abhyankar #if defined(PETSC_HAVE_MATLAB_ENGINE)
697*238ccf28SShri Abhyankar #include "mex.h"
698*238ccf28SShri Abhyankar #undef __FUNCT__
699*238ccf28SShri Abhyankar #define __FUNCT__ "PetscVFPrintf_Matlab"
700*238ccf28SShri Abhyankar PetscErrorCode PETSCSYS_DLLEXPORT PetscVFPrintf_Matlab(FILE *fd,const char format[],va_list Argp)
701*238ccf28SShri Abhyankar {
702*238ccf28SShri Abhyankar   PetscErrorCode ierr;
703*238ccf28SShri Abhyankar 
704*238ccf28SShri Abhyankar   PetscFunctionBegin;
705*238ccf28SShri Abhyankar   if (fd != stdout && fd != stderr) { /* handle regular files */
706*238ccf28SShri Abhyankar       ierr = PetscVFPrintfDefault(fd,format,Argp); CHKERRQ(ierr);
707*238ccf28SShri Abhyankar   } else {
708*238ccf28SShri Abhyankar     size_t len=8*1024,length;
709*238ccf28SShri Abhyankar     char   buf[len];
710*238ccf28SShri Abhyankar 
711*238ccf28SShri Abhyankar      ierr = PetscVSNPrintf(buf,len,format,&length,Argp);CHKERRQ(ierr);
712*238ccf28SShri Abhyankar      /* printf is defined to mexPrintf in mex.h. So by calling printf, the output
713*238ccf28SShri Abhyankar         will be directed to the matlab console i/o  */
714*238ccf28SShri Abhyankar      printf("%s",buf);
715*238ccf28SShri Abhyankar  }
716*238ccf28SShri Abhyankar  PetscFunctionReturn(0);
717*238ccf28SShri Abhyankar }
718*238ccf28SShri Abhyankar #endif
719