Actual source code: filev.c
1: #define PETSC_DLL
3: #include src/sys/viewer/impls/ascii/asciiimpl.h
4: #include "petscfix.h"
5: #include <stdarg.h>
7: /* ----------------------------------------------------------------------*/
10: PetscErrorCode PetscViewerDestroy_ASCII(PetscViewer viewer)
11: {
12: PetscMPIInt rank;
13: PetscErrorCode ierr;
14: PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data;
15: PetscViewerLink *vlink;
16: PetscTruth flg;
19: if (vascii->sviewer) {
20: SETERRQ(PETSC_ERR_ORDER,"ASCII PetscViewer destroyed before restoring singleton PetscViewer");
21: }
22: MPI_Comm_rank(viewer->comm,&rank);
23: if (!rank && vascii->fd != stderr && vascii->fd != stdout) {
24: if (vascii->fd) fclose(vascii->fd);
25: if (vascii->storecompressed) {
26: char par[PETSC_MAX_PATH_LEN],buf[PETSC_MAX_PATH_LEN];
27: FILE *fp;
28: PetscStrcpy(par,"gzip ");
29: PetscStrcat(par,vascii->filename);
30: #if defined(PETSC_HAVE_POPEN)
31: PetscPOpen(PETSC_COMM_SELF,PETSC_NULL,par,"r",&fp);
32: if (fgets(buf,1024,fp)) {
33: SETERRQ2(PETSC_ERR_LIB,"Error from compression command %s\n%s",par,buf);
34: }
35: PetscPClose(PETSC_COMM_SELF,fp);
36: #else
37: SETERRQ(PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
38: #endif
39: }
40: }
41: PetscStrfree(vascii->filename);
42: PetscFree(vascii);
44: /* remove the viewer from the list in the MPI Communicator */
45: if (Petsc_Viewer_keyval == MPI_KEYVAL_INVALID) {
46: MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelViewer,&Petsc_Viewer_keyval,(void*)0);
47: }
49: MPI_Attr_get(viewer->comm,Petsc_Viewer_keyval,(void**)&vlink,(PetscMPIInt*)&flg);
50: if (flg) {
51: if (vlink && vlink->viewer == viewer) {
52: MPI_Attr_put(viewer->comm,Petsc_Viewer_keyval,vlink->next);
53: PetscFree(vlink);
54: } else {
55: while (vlink && vlink->next) {
56: if (vlink->next->viewer == viewer) {
57: PetscViewerLink *nv = vlink->next;
58: vlink->next = vlink->next->next;
59: PetscFree(nv);
60: }
61: vlink = vlink->next;
62: }
63: }
64: }
65: return(0);
66: }
70: PetscErrorCode PetscViewerDestroy_ASCII_Singleton(PetscViewer viewer)
71: {
72: PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data;
73: PetscErrorCode ierr;
75: PetscViewerRestoreSingleton(vascii->bviewer,&viewer);
76: return(0);
77: }
81: PetscErrorCode PetscViewerFlush_ASCII_Singleton_0(PetscViewer viewer)
82: {
83: PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data;
86: fflush(vascii->fd);
87: return(0);
88: }
92: PetscErrorCode PetscViewerFlush_ASCII(PetscViewer viewer)
93: {
94: PetscMPIInt rank;
95: PetscErrorCode ierr;
96: PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data;
99: MPI_Comm_rank(viewer->comm,&rank);
100: if (!rank) {
101: fflush(vascii->fd);
102: }
104: /*
105: Also flush anything printed with PetscViewerASCIISynchronizedPrintf()
106: */
107: PetscSynchronizedFlush(viewer->comm);
108: return(0);
109: }
113: /*@C
114: PetscViewerASCIIGetPointer - Extracts the file pointer from an ASCII PetscViewer.
116: Not Collective
118: + viewer - PetscViewer context, obtained from PetscViewerASCIIOpen()
119: - fd - file pointer
121: Level: intermediate
123: Fortran Note:
124: This routine is not supported in Fortran.
126: Concepts: PetscViewer^file pointer
127: Concepts: file pointer^getting from PetscViewer
129: .seealso: PetscViewerASCIIOpen(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerCreate(), PetscViewerASCIIPrintf(),
130: PetscViewerASCIISynchronizedPrintf(), PetscViewerFlush()
131: @*/
132: PetscErrorCode PetscViewerASCIIGetPointer(PetscViewer viewer,FILE **fd)
133: {
134: PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data;
137: *fd = vascii->fd;
138: return(0);
139: }
144: PetscErrorCode PetscViewerFileGetMode_ASCII(PetscViewer viewer, PetscFileMode *mode)
145: {
146: PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data;
149: *mode = vascii->mode;
150: return(0);
151: }
154: /*@C
155: PetscViewerFileSetMode - Sets the mode in which to open the file.
157: Not Collective
159: + viewer - viewer context, obtained from PetscViewerCreate()
160: - mode - The file mode
162: Level: intermediate
164: Fortran Note:
165: This routine is not supported in Fortran.
167: .keywords: Viewer, file, get, pointer
169: .seealso: PetscViewerASCIIOpen(), PetscViewerBinaryOpen()
170: @*/
175: PetscErrorCode PetscViewerFileSetMode_ASCII(PetscViewer viewer, PetscFileMode mode)
176: {
177: PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data;
180: vascii->mode = mode;
181: return(0);
182: }
185: /*
186: If petsc_history is on, then all Petsc*Printf() results are saved
187: if the appropriate (usually .petschistory) file.
188: */
193: /*@
194: PetscViewerASCIISetTab - Causes PetscViewer to tab in a number of times
196: Not Collective, but only first processor in set has any effect
198: Input Parameters:
199: + viewer - optained with PetscViewerASCIIOpen()
200: - tabs - number of tabs
202: Level: developer
204: Fortran Note:
205: This routine is not supported in Fortran.
207: Concepts: PetscViewerASCII^formating
208: Concepts: tab^setting
210: .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(),
211: PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(),
212: PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer(), PetscViewerASCIIPushTab()
213: @*/
214: PetscErrorCode PetscViewerASCIISetTab(PetscViewer viewer,PetscInt tabs)
215: {
216: PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
217: PetscTruth iascii;
218: PetscErrorCode ierr;
222: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
223: if (iascii) {
224: ascii->tab = tabs;
225: }
226: return(0);
227: }
231: /*@
232: PetscViewerASCIIPushTab - Adds one more tab to the amount that PetscViewerASCIIPrintf()
233: lines are tabbed.
235: Not Collective, but only first processor in set has any effect
237: Input Parameters:
238: . viewer - optained with PetscViewerASCIIOpen()
240: Level: developer
242: Fortran Note:
243: This routine is not supported in Fortran.
245: Concepts: PetscViewerASCII^formating
246: Concepts: tab^setting
248: .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(),
249: PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(),
250: PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer()
251: @*/
252: PetscErrorCode PetscViewerASCIIPushTab(PetscViewer viewer)
253: {
254: PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
255: PetscTruth iascii;
256: PetscErrorCode ierr;
260: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
261: if (iascii) {
262: ascii->tab++;
263: }
264: return(0);
265: }
269: /*@
270: PetscViewerASCIIPopTab - Removes one tab from the amount that PetscViewerASCIIPrintf()
271: lines are tabbed.
273: Not Collective, but only first processor in set has any effect
275: Input Parameters:
276: . viewer - optained with PetscViewerASCIIOpen()
278: Level: developer
280: Fortran Note:
281: This routine is not supported in Fortran.
283: Concepts: PetscViewerASCII^formating
284: Concepts: tab^setting
286: .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(),
287: PetscViewerASCIIPushTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(),
288: PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer()
289: @*/
290: PetscErrorCode PetscViewerASCIIPopTab(PetscViewer viewer)
291: {
292: PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
293: PetscErrorCode ierr;
294: PetscTruth iascii;
298: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
299: if (iascii) {
300: if (ascii->tab <= 0) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"More tabs popped than pushed");
301: ascii->tab--;
302: }
303: return(0);
304: }
308: /*@
309: PetscViewerASCIIUseTabs - Turns on or off the use of tabs with the ASCII PetscViewer
311: Not Collective, but only first processor in set has any effect
313: Input Parameters:
314: + viewer - optained with PetscViewerASCIIOpen()
315: - flg - PETSC_YES or PETSC_NO
317: Level: developer
319: Fortran Note:
320: This routine is not supported in Fortran.
322: Concepts: PetscViewerASCII^formating
323: Concepts: tab^setting
325: .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(),
326: PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIPushTab(), PetscViewerASCIIOpen(),
327: PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer()
328: @*/
329: PetscErrorCode PetscViewerASCIIUseTabs(PetscViewer viewer,PetscTruth flg)
330: {
331: PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
332: PetscTruth iascii;
333: PetscErrorCode ierr;
337: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
338: if (iascii) {
339: if (flg) {
340: ascii->tab = ascii->tab_store;
341: } else {
342: ascii->tab_store = ascii->tab;
343: ascii->tab = 0;
344: }
345: }
346: return(0);
347: }
349: /* ----------------------------------------------------------------------- */
351: #include src/sys/fileio/mprint.h
355: /*@C
356: PetscViewerASCIIPrintf - Prints to a file, only from the first
357: processor in the PetscViewer
359: Not Collective, but only first processor in set has any effect
361: Input Parameters:
362: + viewer - optained with PetscViewerASCIIOpen()
363: - format - the usual printf() format string
365: Level: developer
367: Fortran Note:
368: The call sequence is PetscViewerASCIIPrintf(PetscViewer, character(*), int ierr) from Fortran.
369: That is, you can only pass a single character string from Fortran.
371: Concepts: PetscViewerASCII^printing
372: Concepts: printing^to file
373: Concepts: printf
375: .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIOpen(),
376: PetscViewerASCIIPushTab(), PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(),
377: PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer()
378: @*/
379: PetscErrorCode PetscViewerASCIIPrintf(PetscViewer viewer,const char format[],...)
380: {
381: PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
382: PetscMPIInt rank;
383: PetscInt tab;
384: PetscErrorCode ierr;
385: FILE *fd = ascii->fd;
386: PetscTruth iascii;
391: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
392: if (!iascii) SETERRQ(PETSC_ERR_ARG_WRONG,"Not ASCII PetscViewer");
394: MPI_Comm_rank(viewer->comm,&rank);
395: if (ascii->bviewer) {MPI_Comm_rank(ascii->bviewer->comm,&rank);}
396: if (!rank) {
397: va_list Argp;
398: if (ascii->bviewer) {
399: queuefile = fd;
400: }
402: tab = ascii->tab;
403: while (tab--) {PetscFPrintf(PETSC_COMM_SELF,fd," ");}
405: va_start(Argp,format);
406: PetscVFPrintf(fd,format,Argp);
407: fflush(fd);
408: if (petsc_history) {
409: tab = ascii->tab;
410: while (tab--) {PetscFPrintf(PETSC_COMM_SELF,fd," ");}
411: PetscVFPrintf(petsc_history,format,Argp);
412: fflush(petsc_history);
413: }
414: va_end(Argp);
415: } else if (ascii->bviewer) { /* this is a singleton PetscViewer that is not on process 0 */
416: va_list Argp;
417: char *string;
419: PrintfQueue next;
420: PetscNew(struct _PrintfQueue,&next);
421: if (queue) {queue->next = next; queue = next;}
422: else {queuebase = queue = next;}
423: queuelength++;
424: string = next->string;
425: PetscMemzero(string,QUEUESTRINGSIZE);
426: tab = 2*ascii->tab;
427: while (tab--) {*string++ = ' ';}
428: va_start(Argp,format);
429: PetscVSNPrintf(string,QUEUESTRINGSIZE-2*ascii->tab,format,Argp);
430: va_end(Argp);
431: }
432: return(0);
433: }
437: /*@C
438: PetscViewerFileSetName - Sets the name of the file the PetscViewer uses.
440: Collective on PetscViewer
442: Input Parameters:
443: + viewer - the PetscViewer; either ASCII or binary
444: - name - the name of the file it should use
446: Level: advanced
448: .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerASCIIOpen(), PetscViewerBinaryOpen(), PetscViewerDestroy(),
449: PetscViewerASCIIGetPointer(), PetscViewerASCIIPrintf(), PetscViewerASCIISynchronizedPrintf()
451: @*/
452: PetscErrorCode PetscViewerFileSetName(PetscViewer viewer,const char name[])
453: {
454: PetscErrorCode ierr,(*f)(PetscViewer,const char[]);
459: PetscObjectQueryFunction((PetscObject)viewer,"PetscViewerFileSetName_C",(void (**)(void))&f);
460: if (f) {
461: (*f)(viewer,name);
462: }
463: return(0);
464: }
468: /*@C
469: PetscViewerFileGetName - Gets the name of the file the PetscViewer uses.
471: Not Collective
473: Input Parameter:
474: . viewer - the PetscViewer; either ASCII or binary
476: Output Parameter:
477: . name - the name of the file it is using
479: Level: advanced
481: .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerASCIIOpen(), PetscViewerBinaryOpen(), PetscViewerFileSetName()
483: @*/
484: PetscErrorCode PetscViewerFileGetName(PetscViewer viewer,char **name)
485: {
486: PetscErrorCode ierr,(*f)(PetscViewer,char **);
490: PetscObjectQueryFunction((PetscObject)viewer,"PetscViewerFileGetName_C",(void (**)(void))&f);
491: if (f) {
492: (*f)(viewer,name);
493: }
494: return(0);
495: }
500: PetscErrorCode PetscViewerFileGetName_ASCII(PetscViewer viewer,char **name)
501: {
502: PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
505: *name = vascii->filename;
506: return(0);
507: }
514: PetscErrorCode PetscViewerFileSetName_ASCII(PetscViewer viewer,const char name[])
515: {
516: PetscErrorCode ierr;
517: size_t len;
518: char fname[PETSC_MAX_PATH_LEN],*gz;
519: PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
520: PetscTruth isstderr,isstdout;
521: PetscMPIInt rank;
524: if (!name) return(0);
525: PetscStrfree(vascii->filename);
526: PetscStrallocpy(name,&vascii->filename);
528: /* Is this file to be compressed */
529: vascii->storecompressed = PETSC_FALSE;
530: PetscStrstr(vascii->filename,".gz",&gz);
531: if (gz) {
532: PetscStrlen(gz,&len);
533: if (len == 3) {
534: *gz = 0;
535: vascii->storecompressed = PETSC_TRUE;
536: }
537: }
538: MPI_Comm_rank(viewer->comm,&rank);
539: if (!rank) {
540: PetscStrcmp(name,"stderr",&isstderr);
541: PetscStrcmp(name,"stdout",&isstdout);
542: /* empty filename means stdout */
543: if (name[0] == 0) isstdout = PETSC_TRUE;
544: if (isstderr) vascii->fd = stderr;
545: else if (isstdout) vascii->fd = stdout;
546: else {
549: PetscFixFilename(name,fname);
550: switch(vascii->mode) {
551: case FILE_MODE_READ:
552: vascii->fd = fopen(fname,"r");
553: break;
554: case FILE_MODE_WRITE:
555: vascii->fd = fopen(fname,"w");
556: break;
557: case FILE_MODE_APPEND:
558: vascii->fd = fopen(fname,"a");
559: break;
560: case FILE_MODE_UPDATE:
561: vascii->fd = fopen(fname,"r+");
562: if (!vascii->fd) {
563: vascii->fd = fopen(fname,"w+");
564: }
565: break;
566: case FILE_MODE_APPEND_UPDATE:
567: /* I really want a file which is opened at the end for updating,
568: not a+, which opens at the beginning, but makes writes at the end.
569: */
570: vascii->fd = fopen(fname,"r+");
571: if (!vascii->fd) {
572: vascii->fd = fopen(fname,"w+");
573: } else {
574: fseek(vascii->fd, 0, SEEK_END);
575: }
576: break;
577: default:
578: SETERRQ1(PETSC_ERR_ARG_WRONG, "Invalid file mode %d", vascii->mode);
579: }
580: if (!vascii->fd) SETERRQ1(PETSC_ERR_FILE_OPEN,"Cannot open PetscViewer file: %s",fname);
581: }
582: }
583: #if defined(PETSC_USE_LOG)
584: PetscLogObjectState((PetscObject)viewer,"File: %s",name);
585: #endif
586: return(0);
587: }
592: PetscErrorCode PetscViewerGetSingleton_ASCII(PetscViewer viewer,PetscViewer *outviewer)
593: {
594: PetscMPIInt rank;
595: PetscErrorCode ierr;
596: PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data,*ovascii;
597: const char *name;
600: if (vascii->sviewer) {
601: SETERRQ(PETSC_ERR_ORDER,"Singleton already obtained from PetscViewer and not restored");
602: }
603: PetscViewerCreate(PETSC_COMM_SELF,outviewer);
604: PetscViewerSetType(*outviewer,PETSC_VIEWER_ASCII);
605: ovascii = (PetscViewer_ASCII*)(*outviewer)->data;
606: ovascii->fd = vascii->fd;
607: ovascii->tab = vascii->tab;
609: vascii->sviewer = *outviewer;
611: (*outviewer)->format = viewer->format;
612: (*outviewer)->iformat = viewer->iformat;
614: PetscObjectGetName((PetscObject)viewer,&name);
615: PetscObjectSetName((PetscObject)(*outviewer),name);
617: MPI_Comm_rank(viewer->comm,&rank);
618: ((PetscViewer_ASCII*)((*outviewer)->data))->bviewer = viewer;
619: (*outviewer)->ops->destroy = PetscViewerDestroy_ASCII_Singleton;
620: if (rank) {
621: (*outviewer)->ops->flush = 0;
622: } else {
623: (*outviewer)->ops->flush = PetscViewerFlush_ASCII_Singleton_0;
624: }
625: return(0);
626: }
630: PetscErrorCode PetscViewerRestoreSingleton_ASCII(PetscViewer viewer,PetscViewer *outviewer)
631: {
632: PetscErrorCode ierr;
633: PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)(*outviewer)->data;
634: PetscViewer_ASCII *ascii = (PetscViewer_ASCII *)viewer->data;
637: if (!ascii->sviewer) {
638: SETERRQ(PETSC_ERR_ORDER,"Singleton never obtained from PetscViewer");
639: }
640: if (ascii->sviewer != *outviewer) {
641: SETERRQ(PETSC_ERR_ARG_WRONG,"This PetscViewer did not generate singleton");
642: }
644: ascii->sviewer = 0;
645: vascii->fd = stdout;
646: (*outviewer)->ops->destroy = PetscViewerDestroy_ASCII;
647: PetscViewerDestroy(*outviewer);
648: PetscViewerFlush(viewer);
649: return(0);
650: }
655: PetscErrorCode PetscViewerCreate_ASCII(PetscViewer viewer)
656: {
657: PetscViewer_ASCII *vascii;
658: PetscErrorCode ierr;
661: PetscNew(PetscViewer_ASCII,&vascii);
662: viewer->data = (void*)vascii;
664: viewer->ops->destroy = PetscViewerDestroy_ASCII;
665: viewer->ops->flush = PetscViewerFlush_ASCII;
666: viewer->ops->getsingleton = PetscViewerGetSingleton_ASCII;
667: viewer->ops->restoresingleton = PetscViewerRestoreSingleton_ASCII;
669: /* defaults to stdout unless set with PetscViewerFileSetName() */
670: vascii->fd = stdout;
671: vascii->mode = FILE_MODE_WRITE;
672: vascii->bviewer = 0;
673: vascii->sviewer = 0;
674: viewer->format = PETSC_VIEWER_ASCII_DEFAULT;
675: viewer->iformat = 0;
676: vascii->tab = 0;
677: vascii->tab_store = 0;
678: vascii->filename = 0;
680: PetscObjectComposeFunctionDynamic((PetscObject)viewer,"PetscViewerFileSetName_C","PetscViewerFileSetName_ASCII",
681: PetscViewerFileSetName_ASCII);
682: PetscObjectComposeFunctionDynamic((PetscObject)viewer,"PetscViewerFileGetName_C","PetscViewerFileGetName_ASCII",
683: PetscViewerFileGetName_ASCII);
684: PetscObjectComposeFunctionDynamic((PetscObject)viewer,"PetscViewerFileGetMode_C","PetscViewerFileGetMode_ASCII",
685: PetscViewerFileGetMode_ASCII);
686: PetscObjectComposeFunctionDynamic((PetscObject)viewer,"PetscViewerFileSetMode_C","PetscViewerFileSetMode_ASCII",
687: PetscViewerFileSetMode_ASCII);
689: return(0);
690: }
696: /*@C
697: PetscViewerASCIISynchronizedPrintf - Prints synchronized output to the specified file from
698: several processors. Output of the first processor is followed by that of the
699: second, etc.
701: Not Collective, must call collective PetscViewerFlush() to get the results out
703: Input Parameters:
704: + viewer - the ASCII PetscViewer
705: - format - the usual printf() format string
707: Level: intermediate
709: Fortran Note:
710: Can only print a single character* string
712: .seealso: PetscSynchronizedPrintf(), PetscSynchronizedFlush(), PetscFPrintf(),
713: PetscFOpen(), PetscViewerFlush(), PetscViewerASCIIGetPointer(), PetscViewerDestroy(), PetscViewerASCIIOpen(),
714: PetscViewerASCIIPrintf()
716: @*/
717: PetscErrorCode PetscViewerASCIISynchronizedPrintf(PetscViewer viewer,const char format[],...)
718: {
719: PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data;
720: PetscErrorCode ierr;
721: PetscMPIInt rank;
722: PetscInt tab = vascii->tab;
723: MPI_Comm comm;
724: FILE *fp;
725: PetscTruth iascii;
730: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
731: if (!iascii) SETERRQ(PETSC_ERR_ARG_WRONG,"Not ASCII PetscViewer");
733: comm = viewer->comm;
734: fp = vascii->fd;
735: MPI_Comm_rank(comm,&rank);
736: if (vascii->bviewer) {MPI_Comm_rank(vascii->bviewer->comm,&rank);}
737:
739: /* First processor prints immediately to fp */
740: if (!rank) {
741: va_list Argp;
743: while (tab--) {PetscFPrintf(PETSC_COMM_SELF,fp," ");}
745: va_start(Argp,format);
746: PetscVFPrintf(fp,format,Argp);
747: fflush(fp);
748: queuefile = fp;
749: if (petsc_history) {
750: PetscVFPrintf(petsc_history,format,Argp);
751: fflush(petsc_history);
752: }
753: va_end(Argp);
754: } else { /* other processors add to local queue */
755: char *string;
756: va_list Argp;
757: PrintfQueue next;
759: PetscNew(struct _PrintfQueue,&next);
760: if (queue) {queue->next = next; queue = next;}
761: else {queuebase = queue = next;}
762: queuelength++;
763: string = next->string;
764: PetscMemzero(string,QUEUESTRINGSIZE);
765: tab *= 2;
766: while (tab--) {*string++ = ' ';}
767: va_start(Argp,format);
768: PetscVSNPrintf(string,QUEUESTRINGSIZE-2*vascii->tab,format,Argp);
769: va_end(Argp);
770: }
771: return(0);
772: }
777: /*@C
778: PetscViewerASCIIMonitorCreate - Opens an ASCII file as a monitor object, suitable for the default KSP, SNES and TS monitors
780: Collective on MPI_Comm
782: Input Parameters:
783: + comm - the communicator
784: . name - the file name
785: - tabs - how far in the text should be tabbed
787: Output Parameter:
788: . lab - the context to be used with KSP/SNES/TSMonitorSet()
790: Level: advanced
792: Notes:
793: This can be destroyed with PetscViewerASCIIMonitorDestroy().
795: See PetscViewerASCIIOpen()
797: .seealso: KSPMonitorSet(), SNESMonitorSet(), TSMonitorSet(), KSPMonitorDefault(), PetscViewerASCIIMonitor, PetscViewerASCIIMonitorDestroy()
799: @*/
800: PetscErrorCode PetscViewerASCIIMonitorCreate(MPI_Comm comm,const char *filename,PetscInt tabs,PetscViewerASCIIMonitor* ctx)
801: {
805: PetscNew(struct _p_PetscViewerASCIIMonitor,ctx);
806: PetscViewerASCIIOpen(comm,filename,&(*ctx)->viewer);
807: (*ctx)->tabs = tabs;
808: return(0);
809: }
813: /*@C
814: PetscViewerASCIIMonitorDestroys - removes a monitor context.
816: Collective on PetscViewerASCIIMonitor
818: Input Parameters:
819: . ctx - the monitor context created with PetscViewerASCIIMonitorCreate()
821: Level: advanced
823: Notes:
824: This is rarely called by users, it is usually called when the KSP, SNES or TS object is destroyed
826: .seealso: KSPMonitorSet(), SNESMonitorSet(), TSMonitorSet(), KSPMonitorDefault(), PetscViewerASCIIMonitor, PetscViewerASCIIMonitorCreate()
828: @*/
829: PetscErrorCode PetscViewerASCIIMonitorDestroy(PetscViewerASCIIMonitor ctx)
830: {
834: PetscViewerDestroy(ctx->viewer);
835: PetscFree(ctx);
836: return(0);
837: }
841: /*@C
842: PetscViewerASCIIMonitorPrintf - Prints to the viewer associated with this monitor context
844: Not Collective, but only first processor in set has any effect
846: Input Parameters:
847: + ctx - the context obtained with PetscViewerASCIIMonitorCreate()
848: - format - the usual printf() format string
850: Level: developer
852: Developer Notes: This code is virtually identical to PetscViewerASCIIPrintf(), however the code
853: could not simply be called from here due to the var args.
855: .seealso: KSPMonitorSet(), SNESMonitorSet(), TSMonitorSet(), KSPMonitorDefault(), PetscViewerASCIIMonitor, PetscViewerASCIIMonitorCreate(),
856: PetscPrintf(), PetscFPrintf(), PetscViewerASCIIPrintf()
859: @*/
860: PetscErrorCode PetscViewerASCIIMonitorPrintf(PetscViewerASCIIMonitor ctx,const char format[],...)
861: {
862: PetscViewer viewer = ctx->viewer;
863: PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
864: PetscMPIInt rank;
865: PetscInt tab;
866: PetscErrorCode ierr;
867: FILE *fd = ascii->fd;
868: PetscTruth iascii;
873: PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
874: if (!iascii) SETERRQ(PETSC_ERR_ARG_WRONG,"Not ASCII PetscViewer");
876: MPI_Comm_rank(viewer->comm,&rank);
877: if (ascii->bviewer) {MPI_Comm_rank(ascii->bviewer->comm,&rank);}
878: if (!rank) {
879: va_list Argp;
880: if (ascii->bviewer) {
881: queuefile = fd;
882: }
884: tab = ascii->tab + ctx->tabs;
885: while (tab--) {PetscFPrintf(PETSC_COMM_SELF,fd," ");}
887: va_start(Argp,format);
888: PetscVFPrintf(fd,format,Argp);
889: fflush(fd);
890: if (petsc_history) {
891: tab = ascii->tab + ctx->tabs;
892: while (tab--) {PetscFPrintf(PETSC_COMM_SELF,fd," ");}
893: PetscVFPrintf(petsc_history,format,Argp);
894: fflush(petsc_history);
895: }
896: va_end(Argp);
897: } else if (ascii->bviewer) { /* this is a singleton PetscViewer that is not on process 0 */
898: va_list Argp;
899: char *string;
901: PrintfQueue next;
902: PetscNew(struct _PrintfQueue,&next);
903: if (queue) {queue->next = next; queue = next;}
904: else {queuebase = queue = next;}
905: queuelength++;
906: string = next->string;
907: PetscMemzero(string,QUEUESTRINGSIZE);
908: tab = 2*(ascii->tab + ctx->tabs);
909: while (tab--) {*string++ = ' ';}
910: va_start(Argp,format);
911: PetscVSNPrintf(string,QUEUESTRINGSIZE-2*ascii->tab,format,Argp);
912: va_end(Argp);
913: }
914: return(0);
915: }