xref: /petsc/src/sys/classes/viewer/impls/ascii/filev.c (revision 08fb59bf369ff7450e813e03e40938a50c72e6f6)
15c6c1daeSBarry Smith 
2665c2dedSJed Brown #include <../src/sys/classes/viewer/impls/ascii/asciiimpl.h>  /*I "petscviewer.h" I*/
35c6c1daeSBarry Smith 
45c6c1daeSBarry Smith #define QUEUESTRINGSIZE 8192
55c6c1daeSBarry Smith 
65c6c1daeSBarry Smith static PetscErrorCode PetscViewerFileClose_ASCII(PetscViewer viewer)
75c6c1daeSBarry Smith {
85c6c1daeSBarry Smith   PetscErrorCode    ierr;
95c6c1daeSBarry Smith   PetscMPIInt       rank;
105c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
115c6c1daeSBarry Smith   int               err;
125c6c1daeSBarry Smith 
135c6c1daeSBarry Smith   PetscFunctionBegin;
14ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
155c6c1daeSBarry Smith   if (!rank && vascii->fd != stderr && vascii->fd != PETSC_STDOUT) {
165c6c1daeSBarry Smith     if (vascii->fd && vascii->closefile) {
175c6c1daeSBarry Smith       err = fclose(vascii->fd);
185c6c1daeSBarry Smith       if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
195c6c1daeSBarry Smith     }
205c6c1daeSBarry Smith     if (vascii->storecompressed) {
215c6c1daeSBarry Smith       char par[PETSC_MAX_PATH_LEN],buf[PETSC_MAX_PATH_LEN];
225c6c1daeSBarry Smith       FILE *fp;
235c6c1daeSBarry Smith       ierr = PetscStrcpy(par,"gzip ");CHKERRQ(ierr);
245c6c1daeSBarry Smith       ierr = PetscStrcat(par,vascii->filename);CHKERRQ(ierr);
255c6c1daeSBarry Smith #if defined(PETSC_HAVE_POPEN)
260298fd71SBarry Smith       ierr = PetscPOpen(PETSC_COMM_SELF,NULL,par,"r",&fp);CHKERRQ(ierr);
27f23aa3ddSBarry Smith       if (fgets(buf,1024,fp)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error from compression command %s\n%s",par,buf);
280298fd71SBarry Smith       ierr = PetscPClose(PETSC_COMM_SELF,fp,NULL);CHKERRQ(ierr);
295c6c1daeSBarry Smith #else
305c6c1daeSBarry Smith       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
315c6c1daeSBarry Smith #endif
325c6c1daeSBarry Smith     }
335c6c1daeSBarry Smith   }
345c6c1daeSBarry Smith   ierr = PetscFree(vascii->filename);CHKERRQ(ierr);
355c6c1daeSBarry Smith   PetscFunctionReturn(0);
365c6c1daeSBarry Smith }
375c6c1daeSBarry Smith 
385c6c1daeSBarry Smith /* ----------------------------------------------------------------------*/
395c6c1daeSBarry Smith PetscErrorCode PetscViewerDestroy_ASCII(PetscViewer viewer)
405c6c1daeSBarry Smith {
415c6c1daeSBarry Smith   PetscErrorCode    ierr;
425c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
435c6c1daeSBarry Smith   PetscViewerLink   *vlink;
445c6c1daeSBarry Smith   PetscBool         flg;
455c6c1daeSBarry Smith 
465c6c1daeSBarry Smith   PetscFunctionBegin;
475c6c1daeSBarry Smith   if (vascii->sviewer) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"ASCII PetscViewer destroyed before restoring singleton or subcomm PetscViewer");
485c6c1daeSBarry Smith   ierr = PetscViewerFileClose_ASCII(viewer);CHKERRQ(ierr);
495c6c1daeSBarry Smith   ierr = PetscFree(vascii);CHKERRQ(ierr);
505c6c1daeSBarry Smith 
515c6c1daeSBarry Smith   /* remove the viewer from the list in the MPI Communicator */
525c6c1daeSBarry Smith   if (Petsc_Viewer_keyval == MPI_KEYVAL_INVALID) {
535c6c1daeSBarry Smith     ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelViewer,&Petsc_Viewer_keyval,(void*)0);CHKERRQ(ierr);
545c6c1daeSBarry Smith   }
555c6c1daeSBarry Smith 
56ce94432eSBarry Smith   ierr = MPI_Attr_get(PetscObjectComm((PetscObject)viewer),Petsc_Viewer_keyval,(void**)&vlink,(PetscMPIInt*)&flg);CHKERRQ(ierr);
575c6c1daeSBarry Smith   if (flg) {
585c6c1daeSBarry Smith     if (vlink && vlink->viewer == viewer) {
59e5840a18SBarry Smith       if (vlink->next) {
60ce94432eSBarry Smith         ierr = MPI_Attr_put(PetscObjectComm((PetscObject)viewer),Petsc_Viewer_keyval,vlink->next);CHKERRQ(ierr);
61e5840a18SBarry Smith       } else {
62e5840a18SBarry Smith         ierr = MPI_Attr_delete(PetscObjectComm((PetscObject)viewer),Petsc_Viewer_keyval);CHKERRQ(ierr);
63e5840a18SBarry Smith       }
645c6c1daeSBarry Smith       ierr = PetscFree(vlink);CHKERRQ(ierr);
655c6c1daeSBarry Smith     } else {
665c6c1daeSBarry Smith       while (vlink && vlink->next) {
675c6c1daeSBarry Smith         if (vlink->next->viewer == viewer) {
685c6c1daeSBarry Smith           PetscViewerLink *nv = vlink->next;
695c6c1daeSBarry Smith           vlink->next = vlink->next->next;
705c6c1daeSBarry Smith           ierr = PetscFree(nv);CHKERRQ(ierr);
715c6c1daeSBarry Smith         }
725c6c1daeSBarry Smith         vlink = vlink->next;
735c6c1daeSBarry Smith       }
745c6c1daeSBarry Smith     }
755c6c1daeSBarry Smith   }
765c6c1daeSBarry Smith   PetscFunctionReturn(0);
775c6c1daeSBarry Smith }
785c6c1daeSBarry Smith 
793f08860eSBarry Smith PetscErrorCode PetscViewerDestroy_ASCII_SubViewer(PetscViewer viewer)
805c6c1daeSBarry Smith {
815c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
825c6c1daeSBarry Smith   PetscErrorCode    ierr;
835fd66863SKarl Rupp 
845c6c1daeSBarry Smith   PetscFunctionBegin;
853f08860eSBarry Smith   ierr = PetscViewerRestoreSubViewer(vascii->bviewer,0,&viewer);CHKERRQ(ierr);
865c6c1daeSBarry Smith   PetscFunctionReturn(0);
875c6c1daeSBarry Smith }
885c6c1daeSBarry Smith 
895c6c1daeSBarry Smith PetscErrorCode PetscViewerFlush_ASCII(PetscViewer viewer)
905c6c1daeSBarry Smith {
915c6c1daeSBarry Smith   PetscErrorCode    ierr;
925c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
935c6c1daeSBarry Smith   int               err;
94559f443fSBarry Smith   MPI_Comm          comm;
95559f443fSBarry Smith   PetscMPIInt       rank,size;
96559f443fSBarry Smith   FILE              *fd = vascii->fd;
975c6c1daeSBarry Smith 
985c6c1daeSBarry Smith   PetscFunctionBegin;
99559f443fSBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
100559f443fSBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
101559f443fSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
102559f443fSBarry Smith 
103559f443fSBarry Smith   if (!vascii->bviewer && !rank && (vascii->mode != FILE_MODE_READ)) {
1045c6c1daeSBarry Smith     err = fflush(vascii->fd);
1055c6c1daeSBarry Smith     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() call failed");
1065c6c1daeSBarry Smith   }
1075c6c1daeSBarry Smith 
1085c6c1daeSBarry Smith   if (vascii->allowsynchronized) {
109559f443fSBarry Smith     PetscMPIInt   tag,i,j,n = 0,dummy = 0;
110559f443fSBarry Smith     char          *message;
111559f443fSBarry Smith     MPI_Status    status;
112559f443fSBarry Smith 
113559f443fSBarry Smith     ierr = PetscCommDuplicate(comm,&comm,&tag);CHKERRQ(ierr);
114559f443fSBarry Smith 
115559f443fSBarry Smith     /* First processor waits for messages from all other processors */
116559f443fSBarry Smith     if (!rank) {
117559f443fSBarry Smith       /* flush my own messages that I may have queued up */
118559f443fSBarry Smith       PrintfQueue next = vascii->petsc_printfqueuebase,previous;
119559f443fSBarry Smith       for (i=0; i<vascii->petsc_printfqueuelength; i++) {
120559f443fSBarry Smith         if (!vascii->bviewer) {
121559f443fSBarry Smith           ierr = PetscFPrintf(comm,fd,"%s",next->string);CHKERRQ(ierr);
122559f443fSBarry Smith         } else {
123559f443fSBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(vascii->bviewer,"%s",next->string);CHKERRQ(ierr);
124559f443fSBarry Smith         }
125559f443fSBarry Smith         previous = next;
126559f443fSBarry Smith         next     = next->next;
127559f443fSBarry Smith         ierr     = PetscFree(previous->string);CHKERRQ(ierr);
128559f443fSBarry Smith         ierr     = PetscFree(previous);CHKERRQ(ierr);
129559f443fSBarry Smith       }
130559f443fSBarry Smith       vascii->petsc_printfqueue       = 0;
131559f443fSBarry Smith       vascii->petsc_printfqueuelength = 0;
132559f443fSBarry Smith       for (i=1; i<size; i++) {
133559f443fSBarry Smith         /* to prevent a flood of messages to process zero, request each message separately */
134559f443fSBarry Smith         ierr = MPI_Send(&dummy,1,MPI_INT,i,tag,comm);CHKERRQ(ierr);
135559f443fSBarry Smith         ierr = MPI_Recv(&n,1,MPI_INT,i,tag,comm,&status);CHKERRQ(ierr);
136559f443fSBarry Smith         for (j=0; j<n; j++) {
137559f443fSBarry Smith           PetscMPIInt size = 0;
138559f443fSBarry Smith 
139559f443fSBarry Smith           ierr = MPI_Recv(&size,1,MPI_INT,i,tag,comm,&status);CHKERRQ(ierr);
140559f443fSBarry Smith           ierr = PetscMalloc1(size, &message);CHKERRQ(ierr);
141559f443fSBarry Smith           ierr = MPI_Recv(message,size,MPI_CHAR,i,tag,comm,&status);CHKERRQ(ierr);
142559f443fSBarry Smith           if (!vascii->bviewer) {
143559f443fSBarry Smith             ierr = PetscFPrintf(comm,fd,"%s",message);CHKERRQ(ierr);
144559f443fSBarry Smith           } else {
145559f443fSBarry Smith             ierr = PetscViewerASCIISynchronizedPrintf(vascii->bviewer,"%s",message);CHKERRQ(ierr);
146559f443fSBarry Smith           }
147559f443fSBarry Smith           ierr = PetscFree(message);CHKERRQ(ierr);
148559f443fSBarry Smith         }
149559f443fSBarry Smith       }
150559f443fSBarry Smith     } else { /* other processors send queue to processor 0 */
151559f443fSBarry Smith       PrintfQueue next = vascii->petsc_printfqueuebase,previous;
152559f443fSBarry Smith 
153559f443fSBarry Smith       ierr = MPI_Recv(&dummy,1,MPI_INT,0,tag,comm,&status);CHKERRQ(ierr);
154559f443fSBarry Smith       ierr = MPI_Send(&vascii->petsc_printfqueuelength,1,MPI_INT,0,tag,comm);CHKERRQ(ierr);
155559f443fSBarry Smith       for (i=0; i<vascii->petsc_printfqueuelength; i++) {
156559f443fSBarry Smith         ierr     = MPI_Send(&next->size,1,MPI_INT,0,tag,comm);CHKERRQ(ierr);
157559f443fSBarry Smith         ierr     = MPI_Send(next->string,next->size,MPI_CHAR,0,tag,comm);CHKERRQ(ierr);
158559f443fSBarry Smith         previous = next;
159559f443fSBarry Smith         next     = next->next;
160559f443fSBarry Smith         ierr     = PetscFree(previous->string);CHKERRQ(ierr);
161559f443fSBarry Smith         ierr     = PetscFree(previous);CHKERRQ(ierr);
162559f443fSBarry Smith       }
163559f443fSBarry Smith       vascii->petsc_printfqueue       = 0;
164559f443fSBarry Smith       vascii->petsc_printfqueuelength = 0;
165559f443fSBarry Smith     }
166559f443fSBarry Smith     ierr = PetscCommDestroy(&comm);CHKERRQ(ierr);
1675c6c1daeSBarry Smith   }
1685c6c1daeSBarry Smith   PetscFunctionReturn(0);
1695c6c1daeSBarry Smith }
1705c6c1daeSBarry Smith 
1715c6c1daeSBarry Smith /*@C
1725c6c1daeSBarry Smith     PetscViewerASCIIGetPointer - Extracts the file pointer from an ASCII PetscViewer.
1735c6c1daeSBarry Smith 
1745c6c1daeSBarry Smith     Not Collective
1755c6c1daeSBarry Smith 
1765c6c1daeSBarry Smith +   viewer - PetscViewer context, obtained from PetscViewerASCIIOpen()
1775c6c1daeSBarry Smith -   fd - file pointer
1785c6c1daeSBarry Smith 
1795c6c1daeSBarry Smith     Level: intermediate
1805c6c1daeSBarry Smith 
1815c6c1daeSBarry Smith     Fortran Note:
1825c6c1daeSBarry Smith     This routine is not supported in Fortran.
1835c6c1daeSBarry Smith 
1845c6c1daeSBarry Smith   Concepts: PetscViewer^file pointer
1855c6c1daeSBarry Smith   Concepts: file pointer^getting from PetscViewer
1865c6c1daeSBarry Smith 
1875c6c1daeSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerCreate(), PetscViewerASCIIPrintf(),
1885c6c1daeSBarry Smith           PetscViewerASCIISynchronizedPrintf(), PetscViewerFlush()
1895c6c1daeSBarry Smith @*/
1905c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIGetPointer(PetscViewer viewer,FILE **fd)
1915c6c1daeSBarry Smith {
1925c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
1935c6c1daeSBarry Smith 
1945c6c1daeSBarry Smith   PetscFunctionBegin;
1955c6c1daeSBarry Smith   *fd = vascii->fd;
1965c6c1daeSBarry Smith   PetscFunctionReturn(0);
1975c6c1daeSBarry Smith }
1985c6c1daeSBarry Smith 
1995c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileGetMode_ASCII(PetscViewer viewer, PetscFileMode *mode)
2005c6c1daeSBarry Smith {
2015c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
2025c6c1daeSBarry Smith 
2035c6c1daeSBarry Smith   PetscFunctionBegin;
2045c6c1daeSBarry Smith   *mode = vascii->mode;
2055c6c1daeSBarry Smith   PetscFunctionReturn(0);
2065c6c1daeSBarry Smith }
2075c6c1daeSBarry Smith 
2085c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileSetMode_ASCII(PetscViewer viewer, PetscFileMode mode)
2095c6c1daeSBarry Smith {
2105c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
2115c6c1daeSBarry Smith 
2125c6c1daeSBarry Smith   PetscFunctionBegin;
2135c6c1daeSBarry Smith   vascii->mode = mode;
2145c6c1daeSBarry Smith   PetscFunctionReturn(0);
2155c6c1daeSBarry Smith }
2165c6c1daeSBarry Smith 
2175c6c1daeSBarry Smith /*
2185c6c1daeSBarry Smith    If petsc_history is on, then all Petsc*Printf() results are saved
2195c6c1daeSBarry Smith    if the appropriate (usually .petschistory) file.
2205c6c1daeSBarry Smith */
2215c6c1daeSBarry Smith extern FILE *petsc_history;
2225c6c1daeSBarry Smith 
2235c6c1daeSBarry Smith /*@
2245c6c1daeSBarry Smith     PetscViewerASCIISetTab - Causes PetscViewer to tab in a number of times
2255c6c1daeSBarry Smith 
2265c6c1daeSBarry Smith     Not Collective, but only first processor in set has any effect
2275c6c1daeSBarry Smith 
2285c6c1daeSBarry Smith     Input Parameters:
2291575c14dSBarry Smith +    viewer - obtained with PetscViewerASCIIOpen()
2305c6c1daeSBarry Smith -    tabs - number of tabs
2315c6c1daeSBarry Smith 
2325c6c1daeSBarry Smith     Level: developer
2335c6c1daeSBarry Smith 
2345c6c1daeSBarry Smith     Fortran Note:
2355c6c1daeSBarry Smith     This routine is not supported in Fortran.
2365c6c1daeSBarry Smith 
2375c6c1daeSBarry Smith   Concepts: PetscViewerASCII^formating
2385c6c1daeSBarry Smith   Concepts: tab^setting
2395c6c1daeSBarry Smith 
2405c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(), PetscViewerASCIIGetTab(),
2415c6c1daeSBarry Smith           PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(),
2425c6c1daeSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer(), PetscViewerASCIIPushTab()
2435c6c1daeSBarry Smith @*/
2445c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIISetTab(PetscViewer viewer,PetscInt tabs)
2455c6c1daeSBarry Smith {
2465c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
2475c6c1daeSBarry Smith   PetscBool         iascii;
2485c6c1daeSBarry Smith   PetscErrorCode    ierr;
2495c6c1daeSBarry Smith 
2505c6c1daeSBarry Smith   PetscFunctionBegin;
2515c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
2525c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
253a297a907SKarl Rupp   if (iascii) ascii->tab = tabs;
2545c6c1daeSBarry Smith   PetscFunctionReturn(0);
2555c6c1daeSBarry Smith }
2565c6c1daeSBarry Smith 
2575c6c1daeSBarry Smith /*@
2585c6c1daeSBarry Smith     PetscViewerASCIIGetTab - Return the number of tabs used by PetscViewer.
2595c6c1daeSBarry Smith 
2605c6c1daeSBarry Smith     Not Collective, meaningful on first processor only.
2615c6c1daeSBarry Smith 
2625c6c1daeSBarry Smith     Input Parameters:
2631575c14dSBarry Smith .    viewer - obtained with PetscViewerASCIIOpen()
2645c6c1daeSBarry Smith     Output Parameters:
2655c6c1daeSBarry Smith .    tabs - number of tabs
2665c6c1daeSBarry Smith 
2675c6c1daeSBarry Smith     Level: developer
2685c6c1daeSBarry Smith 
2695c6c1daeSBarry Smith     Fortran Note:
2705c6c1daeSBarry Smith     This routine is not supported in Fortran.
2715c6c1daeSBarry Smith 
2725c6c1daeSBarry Smith   Concepts: PetscViewerASCII^formating
2735c6c1daeSBarry Smith   Concepts: tab^retrieval
2745c6c1daeSBarry Smith 
2755c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(), PetscViewerASCIISetTab(),
2765c6c1daeSBarry Smith           PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(),
2775c6c1daeSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer(), PetscViewerASCIIPushTab()
2785c6c1daeSBarry Smith @*/
2795c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIGetTab(PetscViewer viewer,PetscInt *tabs)
2805c6c1daeSBarry Smith {
2815c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
2825c6c1daeSBarry Smith   PetscBool         iascii;
2835c6c1daeSBarry Smith   PetscErrorCode    ierr;
2845c6c1daeSBarry Smith 
2855c6c1daeSBarry Smith   PetscFunctionBegin;
2865c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
2875c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
288a297a907SKarl Rupp   if (iascii && tabs) *tabs = ascii->tab;
2895c6c1daeSBarry Smith   PetscFunctionReturn(0);
2905c6c1daeSBarry Smith }
2915c6c1daeSBarry Smith 
2925c6c1daeSBarry Smith /*@
2935c6c1daeSBarry Smith     PetscViewerASCIIAddTab - Add to the number of times an ASCII viewer tabs before printing
2945c6c1daeSBarry Smith 
2955c6c1daeSBarry Smith     Not Collective, but only first processor in set has any effect
2965c6c1daeSBarry Smith 
2975c6c1daeSBarry Smith     Input Parameters:
2981575c14dSBarry Smith +    viewer - obtained with PetscViewerASCIIOpen()
2995c6c1daeSBarry Smith -    tabs - number of tabs
3005c6c1daeSBarry Smith 
3015c6c1daeSBarry Smith     Level: developer
3025c6c1daeSBarry Smith 
3035c6c1daeSBarry Smith     Fortran Note:
3045c6c1daeSBarry Smith     This routine is not supported in Fortran.
3055c6c1daeSBarry Smith 
3065c6c1daeSBarry Smith   Concepts: PetscViewerASCII^formating
3075c6c1daeSBarry Smith   Concepts: tab^setting
3085c6c1daeSBarry Smith 
3095c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(),
3105c6c1daeSBarry Smith           PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(),
3115c6c1daeSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer(), PetscViewerASCIIPushTab()
3125c6c1daeSBarry Smith @*/
3135c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIAddTab(PetscViewer viewer,PetscInt tabs)
3145c6c1daeSBarry Smith {
3155c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
3165c6c1daeSBarry Smith   PetscBool         iascii;
3175c6c1daeSBarry Smith   PetscErrorCode    ierr;
3185c6c1daeSBarry Smith 
3195c6c1daeSBarry Smith   PetscFunctionBegin;
3205c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
3215c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
322a297a907SKarl Rupp   if (iascii) ascii->tab += tabs;
3235c6c1daeSBarry Smith   PetscFunctionReturn(0);
3245c6c1daeSBarry Smith }
3255c6c1daeSBarry Smith 
3265c6c1daeSBarry Smith /*@
3275c6c1daeSBarry Smith     PetscViewerASCIISubtractTab - Subtracts from the number of times an ASCII viewer tabs before printing
3285c6c1daeSBarry Smith 
3295c6c1daeSBarry Smith     Not Collective, but only first processor in set has any effect
3305c6c1daeSBarry Smith 
3315c6c1daeSBarry Smith     Input Parameters:
3321575c14dSBarry Smith +    viewer - obtained with PetscViewerASCIIOpen()
3335c6c1daeSBarry Smith -    tabs - number of tabs
3345c6c1daeSBarry Smith 
3355c6c1daeSBarry Smith     Level: developer
3365c6c1daeSBarry Smith 
3375c6c1daeSBarry Smith     Fortran Note:
3385c6c1daeSBarry Smith     This routine is not supported in Fortran.
3395c6c1daeSBarry Smith 
3405c6c1daeSBarry Smith   Concepts: PetscViewerASCII^formating
3415c6c1daeSBarry Smith   Concepts: tab^setting
3425c6c1daeSBarry Smith 
3435c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(),
3445c6c1daeSBarry Smith           PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(),
3455c6c1daeSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer(), PetscViewerASCIIPushTab()
3465c6c1daeSBarry Smith @*/
3475c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIISubtractTab(PetscViewer viewer,PetscInt tabs)
3485c6c1daeSBarry Smith {
3495c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
3505c6c1daeSBarry Smith   PetscBool         iascii;
3515c6c1daeSBarry Smith   PetscErrorCode    ierr;
3525c6c1daeSBarry Smith 
3535c6c1daeSBarry Smith   PetscFunctionBegin;
3545c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
3555c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
356a297a907SKarl Rupp   if (iascii) ascii->tab -= tabs;
3575c6c1daeSBarry Smith   PetscFunctionReturn(0);
3585c6c1daeSBarry Smith }
3595c6c1daeSBarry Smith 
3605c6c1daeSBarry Smith /*@C
3611575c14dSBarry Smith     PetscViewerASCIIPushSynchronized - Allows calls to PetscViewerASCIISynchronizedPrintf() for this viewer
3625c6c1daeSBarry Smith 
3635c6c1daeSBarry Smith     Collective on PetscViewer
3645c6c1daeSBarry Smith 
3655c6c1daeSBarry Smith     Input Parameters:
3661575c14dSBarry Smith .    viewer - obtained with PetscViewerASCIIOpen()
3675c6c1daeSBarry Smith 
3685c6c1daeSBarry Smith     Level: intermediate
3695c6c1daeSBarry Smith 
3705c6c1daeSBarry Smith   Concepts: PetscViewerASCII^formating
3715c6c1daeSBarry Smith   Concepts: tab^setting
3725c6c1daeSBarry Smith 
3731575c14dSBarry Smith .seealso: PetscViewerASCIIPopSynchronized(), PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(),
3745c6c1daeSBarry Smith           PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(),
3755c6c1daeSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer()
3765c6c1daeSBarry Smith @*/
3771575c14dSBarry Smith PetscErrorCode  PetscViewerASCIIPushSynchronized(PetscViewer viewer)
3785c6c1daeSBarry Smith {
3795c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
3805c6c1daeSBarry Smith   PetscBool         iascii;
3815c6c1daeSBarry Smith   PetscErrorCode    ierr;
3825c6c1daeSBarry Smith 
3835c6c1daeSBarry Smith   PetscFunctionBegin;
3845c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
3855c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
3861575c14dSBarry Smith   if (iascii) ascii->allowsynchronized++;
3871575c14dSBarry Smith   PetscFunctionReturn(0);
3881575c14dSBarry Smith }
3891575c14dSBarry Smith 
3901575c14dSBarry Smith /*@C
3911575c14dSBarry Smith     PetscViewerASCIIPopSynchronized - Undoes most recent PetscViewerASCIIPushSynchronized() for this viewer
3921575c14dSBarry Smith 
3931575c14dSBarry Smith     Collective on PetscViewer
3941575c14dSBarry Smith 
3951575c14dSBarry Smith     Input Parameters:
3961575c14dSBarry Smith .    viewer - obtained with PetscViewerASCIIOpen()
3971575c14dSBarry Smith 
3981575c14dSBarry Smith     Level: intermediate
3991575c14dSBarry Smith 
4001575c14dSBarry Smith   Concepts: PetscViewerASCII^formating
4011575c14dSBarry Smith   Concepts: tab^setting
4021575c14dSBarry Smith 
4031575c14dSBarry Smith .seealso: PetscViewerASCIIPushSynchronized(), PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(),
4041575c14dSBarry Smith           PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(),
4051575c14dSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer()
4061575c14dSBarry Smith @*/
4071575c14dSBarry Smith PetscErrorCode  PetscViewerASCIIPopSynchronized(PetscViewer viewer)
4081575c14dSBarry Smith {
4091575c14dSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
4101575c14dSBarry Smith   PetscBool         iascii;
4111575c14dSBarry Smith   PetscErrorCode    ierr;
4121575c14dSBarry Smith 
4131575c14dSBarry Smith   PetscFunctionBegin;
4141575c14dSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
4151575c14dSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
4161575c14dSBarry Smith   if (iascii) {
4171575c14dSBarry Smith     ascii->allowsynchronized--;
4181575c14dSBarry Smith     if (ascii->allowsynchronized < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Called more times than PetscViewerASCIIPushSynchronized()");
4191575c14dSBarry Smith   }
4205c6c1daeSBarry Smith   PetscFunctionReturn(0);
4215c6c1daeSBarry Smith }
4225c6c1daeSBarry Smith 
4235c6c1daeSBarry Smith /*@
4245c6c1daeSBarry Smith     PetscViewerASCIIPushTab - Adds one more tab to the amount that PetscViewerASCIIPrintf()
4255c6c1daeSBarry Smith      lines are tabbed.
4265c6c1daeSBarry Smith 
4275c6c1daeSBarry Smith     Not Collective, but only first processor in set has any effect
4285c6c1daeSBarry Smith 
4295c6c1daeSBarry Smith     Input Parameters:
4301575c14dSBarry Smith .    viewer - obtained with PetscViewerASCIIOpen()
4315c6c1daeSBarry Smith 
4325c6c1daeSBarry Smith     Level: developer
4335c6c1daeSBarry Smith 
4345c6c1daeSBarry Smith     Fortran Note:
4355c6c1daeSBarry Smith     This routine is not supported in Fortran.
4365c6c1daeSBarry Smith 
4375c6c1daeSBarry Smith   Concepts: PetscViewerASCII^formating
4385c6c1daeSBarry Smith   Concepts: tab^setting
4395c6c1daeSBarry Smith 
4405c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(),
4415c6c1daeSBarry Smith           PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(),
4425c6c1daeSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer()
4435c6c1daeSBarry Smith @*/
4445c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIPushTab(PetscViewer viewer)
4455c6c1daeSBarry Smith {
4465c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
4475c6c1daeSBarry Smith   PetscBool         iascii;
4485c6c1daeSBarry Smith   PetscErrorCode    ierr;
4495c6c1daeSBarry Smith 
4505c6c1daeSBarry Smith   PetscFunctionBegin;
4515c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
4525c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
453a297a907SKarl Rupp   if (iascii) ascii->tab++;
4545c6c1daeSBarry Smith   PetscFunctionReturn(0);
4555c6c1daeSBarry Smith }
4565c6c1daeSBarry Smith 
4575c6c1daeSBarry Smith /*@
4585c6c1daeSBarry Smith     PetscViewerASCIIPopTab - Removes one tab from the amount that PetscViewerASCIIPrintf()
4595c6c1daeSBarry Smith      lines are tabbed.
4605c6c1daeSBarry Smith 
4615c6c1daeSBarry Smith     Not Collective, but only first processor in set has any effect
4625c6c1daeSBarry Smith 
4635c6c1daeSBarry Smith     Input Parameters:
4641575c14dSBarry Smith .    viewer - obtained with PetscViewerASCIIOpen()
4655c6c1daeSBarry Smith 
4665c6c1daeSBarry Smith     Level: developer
4675c6c1daeSBarry Smith 
4685c6c1daeSBarry Smith     Fortran Note:
4695c6c1daeSBarry Smith     This routine is not supported in Fortran.
4705c6c1daeSBarry Smith 
4715c6c1daeSBarry Smith   Concepts: PetscViewerASCII^formating
4725c6c1daeSBarry Smith   Concepts: tab^setting
4735c6c1daeSBarry Smith 
4745c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(),
4755c6c1daeSBarry Smith           PetscViewerASCIIPushTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(),
4765c6c1daeSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer()
4775c6c1daeSBarry Smith @*/
4785c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIPopTab(PetscViewer viewer)
4795c6c1daeSBarry Smith {
4805c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
4815c6c1daeSBarry Smith   PetscErrorCode    ierr;
4825c6c1daeSBarry Smith   PetscBool         iascii;
4835c6c1daeSBarry Smith 
4845c6c1daeSBarry Smith   PetscFunctionBegin;
4855c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
4865c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
4875c6c1daeSBarry Smith   if (iascii) {
4885c6c1daeSBarry Smith     if (ascii->tab <= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"More tabs popped than pushed");
4895c6c1daeSBarry Smith     ascii->tab--;
4905c6c1daeSBarry Smith   }
4915c6c1daeSBarry Smith   PetscFunctionReturn(0);
4925c6c1daeSBarry Smith }
4935c6c1daeSBarry Smith 
4945c6c1daeSBarry Smith /*@
4955c6c1daeSBarry Smith     PetscViewerASCIIUseTabs - Turns on or off the use of tabs with the ASCII PetscViewer
4965c6c1daeSBarry Smith 
4975c6c1daeSBarry Smith     Not Collective, but only first processor in set has any effect
4985c6c1daeSBarry Smith 
4995c6c1daeSBarry Smith     Input Parameters:
5001575c14dSBarry Smith +    viewer - obtained with PetscViewerASCIIOpen()
5015c6c1daeSBarry Smith -    flg - PETSC_TRUE or PETSC_FALSE
5025c6c1daeSBarry Smith 
5035c6c1daeSBarry Smith     Level: developer
5045c6c1daeSBarry Smith 
5055c6c1daeSBarry Smith     Fortran Note:
5065c6c1daeSBarry Smith     This routine is not supported in Fortran.
5075c6c1daeSBarry Smith 
5085c6c1daeSBarry Smith   Concepts: PetscViewerASCII^formating
5095c6c1daeSBarry Smith   Concepts: tab^setting
5105c6c1daeSBarry Smith 
5115c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(),
5125c6c1daeSBarry Smith           PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIPushTab(), PetscViewerASCIIOpen(),
5135c6c1daeSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer()
5145c6c1daeSBarry Smith @*/
5155c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIUseTabs(PetscViewer viewer,PetscBool flg)
5165c6c1daeSBarry Smith {
5175c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
5185c6c1daeSBarry Smith   PetscBool         iascii;
5195c6c1daeSBarry Smith   PetscErrorCode    ierr;
5205c6c1daeSBarry Smith 
5215c6c1daeSBarry Smith   PetscFunctionBegin;
5225c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
5235c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
5245c6c1daeSBarry Smith   if (iascii) {
525a297a907SKarl Rupp     if (flg) ascii->tab = ascii->tab_store;
526a297a907SKarl Rupp     else {
5275c6c1daeSBarry Smith       ascii->tab_store = ascii->tab;
5285c6c1daeSBarry Smith       ascii->tab       = 0;
5295c6c1daeSBarry Smith     }
5305c6c1daeSBarry Smith   }
5315c6c1daeSBarry Smith   PetscFunctionReturn(0);
5325c6c1daeSBarry Smith }
5335c6c1daeSBarry Smith 
5345c6c1daeSBarry Smith /* ----------------------------------------------------------------------- */
5355c6c1daeSBarry Smith 
5365c6c1daeSBarry Smith 
5375c6c1daeSBarry Smith /*@C
5385c6c1daeSBarry Smith     PetscViewerASCIIPrintf - Prints to a file, only from the first
5395c6c1daeSBarry Smith     processor in the PetscViewer
5405c6c1daeSBarry Smith 
5415c6c1daeSBarry Smith     Not Collective, but only first processor in set has any effect
5425c6c1daeSBarry Smith 
5435c6c1daeSBarry Smith     Input Parameters:
5441575c14dSBarry Smith +    viewer - obtained with PetscViewerASCIIOpen()
5455c6c1daeSBarry Smith -    format - the usual printf() format string
5465c6c1daeSBarry Smith 
5475c6c1daeSBarry Smith     Level: developer
5485c6c1daeSBarry Smith 
5495c6c1daeSBarry Smith     Fortran Note:
5505c6c1daeSBarry Smith     The call sequence is PetscViewerASCIIPrintf(PetscViewer, character(*), int ierr) from Fortran.
5515c6c1daeSBarry Smith     That is, you can only pass a single character string from Fortran.
5525c6c1daeSBarry Smith 
5535c6c1daeSBarry Smith   Concepts: PetscViewerASCII^printing
5545c6c1daeSBarry Smith   Concepts: printing^to file
5555c6c1daeSBarry Smith   Concepts: printf
5565c6c1daeSBarry Smith 
5575c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIOpen(),
5585c6c1daeSBarry Smith           PetscViewerASCIIPushTab(), PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(),
5591575c14dSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer(), PetscViewerASCIIPushSynchronized()
5605c6c1daeSBarry Smith @*/
5615c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIPrintf(PetscViewer viewer,const char format[],...)
5625c6c1daeSBarry Smith {
5635c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
5645c6c1daeSBarry Smith   PetscMPIInt       rank;
565dd2fa690SBarry Smith   PetscInt          tab,intab = ascii->tab;
5665c6c1daeSBarry Smith   PetscErrorCode    ierr;
5675c6c1daeSBarry Smith   FILE              *fd = ascii->fd;
5683f08860eSBarry Smith   PetscBool         iascii;
5695c6c1daeSBarry Smith   int               err;
5705c6c1daeSBarry Smith 
5715c6c1daeSBarry Smith   PetscFunctionBegin;
5725c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
5735c6c1daeSBarry Smith   PetscValidCharPointer(format,2);
5745c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
5755c6c1daeSBarry Smith   if (!iascii) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Not ASCII PetscViewer");
576ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
577559f443fSBarry Smith   if (rank) PetscFunctionReturn(0);
5783f08860eSBarry Smith 
5793f08860eSBarry Smith   if (ascii->bviewer) { /* pass string up to parent viewer */
5803f08860eSBarry Smith     char        *string;
5813f08860eSBarry Smith     va_list     Argp;
5823f08860eSBarry Smith     size_t      fullLength;
5833f08860eSBarry Smith 
5843f08860eSBarry Smith     ierr       = PetscCalloc1(QUEUESTRINGSIZE, &string);CHKERRQ(ierr);
5853f08860eSBarry Smith     va_start(Argp,format);
5861575c14dSBarry Smith     ierr = PetscVSNPrintf(string,QUEUESTRINGSIZE,format,&fullLength,Argp);CHKERRQ(ierr);
5873f08860eSBarry Smith     va_end(Argp);
588559f443fSBarry Smith     ierr = PetscViewerASCIISynchronizedPrintf(viewer,"%s",string);CHKERRQ(ierr);
5893f08860eSBarry Smith     ierr = PetscFree(string);CHKERRQ(ierr);
5903f08860eSBarry Smith   } else { /* write directly to file */
5915c6c1daeSBarry Smith     va_list Argp;
592559f443fSBarry Smith     /* flush my own messages that I may have queued up */
593559f443fSBarry Smith     PrintfQueue next = ascii->petsc_printfqueuebase,previous;
594559f443fSBarry Smith     PetscInt    i;
595559f443fSBarry Smith     for (i=0; i<ascii->petsc_printfqueuelength; i++) {
596559f443fSBarry Smith       ierr = PetscFPrintf(PETSC_COMM_SELF,fd,"%s",next->string);CHKERRQ(ierr);
597559f443fSBarry Smith       previous = next;
598559f443fSBarry Smith       next     = next->next;
599559f443fSBarry Smith       ierr     = PetscFree(previous->string);CHKERRQ(ierr);
600559f443fSBarry Smith       ierr     = PetscFree(previous);CHKERRQ(ierr);
601559f443fSBarry Smith     }
602559f443fSBarry Smith     ascii->petsc_printfqueue       = 0;
603559f443fSBarry Smith     ascii->petsc_printfqueuelength = 0;
604dd2fa690SBarry Smith     tab = intab;
605a297a907SKarl Rupp     while (tab--) {
606a297a907SKarl Rupp       ierr = PetscFPrintf(PETSC_COMM_SELF,fd,"  ");CHKERRQ(ierr);
607a297a907SKarl Rupp     }
6085c6c1daeSBarry Smith 
6095c6c1daeSBarry Smith     va_start(Argp,format);
6105c6c1daeSBarry Smith     ierr = (*PetscVFPrintf)(fd,format,Argp);CHKERRQ(ierr);
6115c6c1daeSBarry Smith     err  = fflush(fd);
6125c6c1daeSBarry Smith     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
6135c6c1daeSBarry Smith     if (petsc_history) {
6145c6c1daeSBarry Smith       va_start(Argp,format);
615dd2fa690SBarry Smith       tab = intab;
616a297a907SKarl Rupp       while (tab--) {
617706d7a88SBarry Smith         ierr = PetscFPrintf(PETSC_COMM_SELF,petsc_history,"  ");CHKERRQ(ierr);
618a297a907SKarl Rupp       }
6195c6c1daeSBarry Smith       ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr);
6205c6c1daeSBarry Smith       err  = fflush(petsc_history);
6215c6c1daeSBarry Smith       if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
6225c6c1daeSBarry Smith     }
6235c6c1daeSBarry Smith     va_end(Argp);
6245c6c1daeSBarry Smith   }
6255c6c1daeSBarry Smith   PetscFunctionReturn(0);
6265c6c1daeSBarry Smith }
6275c6c1daeSBarry Smith 
6285c6c1daeSBarry Smith /*@C
6295c6c1daeSBarry Smith      PetscViewerFileSetName - Sets the name of the file the PetscViewer uses.
6305c6c1daeSBarry Smith 
6315c6c1daeSBarry Smith     Collective on PetscViewer
6325c6c1daeSBarry Smith 
6335c6c1daeSBarry Smith   Input Parameters:
6345c6c1daeSBarry Smith +  viewer - the PetscViewer; either ASCII or binary
6355c6c1daeSBarry Smith -  name - the name of the file it should use
6365c6c1daeSBarry Smith 
6375c6c1daeSBarry Smith     Level: advanced
6385c6c1daeSBarry Smith 
6395c6c1daeSBarry Smith .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerASCIIOpen(), PetscViewerBinaryOpen(), PetscViewerDestroy(),
6405c6c1daeSBarry Smith           PetscViewerASCIIGetPointer(), PetscViewerASCIIPrintf(), PetscViewerASCIISynchronizedPrintf()
6415c6c1daeSBarry Smith 
6425c6c1daeSBarry Smith @*/
6435c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileSetName(PetscViewer viewer,const char name[])
6445c6c1daeSBarry Smith {
6455c6c1daeSBarry Smith   PetscErrorCode ierr;
6465c6c1daeSBarry Smith 
6475c6c1daeSBarry Smith   PetscFunctionBegin;
6485c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
6495c6c1daeSBarry Smith   PetscValidCharPointer(name,2);
6505c6c1daeSBarry Smith   ierr = PetscTryMethod(viewer,"PetscViewerFileSetName_C",(PetscViewer,const char[]),(viewer,name));CHKERRQ(ierr);
6515c6c1daeSBarry Smith   PetscFunctionReturn(0);
6525c6c1daeSBarry Smith }
6535c6c1daeSBarry Smith 
6545c6c1daeSBarry Smith /*@C
6555c6c1daeSBarry Smith      PetscViewerFileGetName - Gets the name of the file the PetscViewer uses.
6565c6c1daeSBarry Smith 
6575c6c1daeSBarry Smith     Not Collective
6585c6c1daeSBarry Smith 
6595c6c1daeSBarry Smith   Input Parameter:
6605c6c1daeSBarry Smith .  viewer - the PetscViewer; either ASCII or binary
6615c6c1daeSBarry Smith 
6625c6c1daeSBarry Smith   Output Parameter:
6635c6c1daeSBarry Smith .  name - the name of the file it is using
6645c6c1daeSBarry Smith 
6655c6c1daeSBarry Smith     Level: advanced
6665c6c1daeSBarry Smith 
6675c6c1daeSBarry Smith .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerASCIIOpen(), PetscViewerBinaryOpen(), PetscViewerFileSetName()
6685c6c1daeSBarry Smith 
6695c6c1daeSBarry Smith @*/
6705c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileGetName(PetscViewer viewer,const char **name)
6715c6c1daeSBarry Smith {
6725c6c1daeSBarry Smith   PetscErrorCode ierr;
6735c6c1daeSBarry Smith 
6745c6c1daeSBarry Smith   PetscFunctionBegin;
6755c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
676163d334eSBarry Smith   ierr = PetscUseMethod(viewer,"PetscViewerFileGetName_C",(PetscViewer,const char**),(viewer,name));CHKERRQ(ierr);
6775c6c1daeSBarry Smith   PetscFunctionReturn(0);
6785c6c1daeSBarry Smith }
6795c6c1daeSBarry Smith 
6805c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileGetName_ASCII(PetscViewer viewer,const char **name)
6815c6c1daeSBarry Smith {
6825c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
6835c6c1daeSBarry Smith 
6845c6c1daeSBarry Smith   PetscFunctionBegin;
6855c6c1daeSBarry Smith   *name = vascii->filename;
6865c6c1daeSBarry Smith   PetscFunctionReturn(0);
6875c6c1daeSBarry Smith }
6885c6c1daeSBarry Smith 
6895c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileSetName_ASCII(PetscViewer viewer,const char name[])
6905c6c1daeSBarry Smith {
6915c6c1daeSBarry Smith   PetscErrorCode    ierr;
6925c6c1daeSBarry Smith   size_t            len;
6935c6c1daeSBarry Smith   char              fname[PETSC_MAX_PATH_LEN],*gz;
6945c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
6955c6c1daeSBarry Smith   PetscBool         isstderr,isstdout;
6965c6c1daeSBarry Smith   PetscMPIInt       rank;
6975c6c1daeSBarry Smith 
6985c6c1daeSBarry Smith   PetscFunctionBegin;
6995c6c1daeSBarry Smith   ierr = PetscViewerFileClose_ASCII(viewer);CHKERRQ(ierr);
7005c6c1daeSBarry Smith   if (!name) PetscFunctionReturn(0);
7015c6c1daeSBarry Smith   ierr = PetscStrallocpy(name,&vascii->filename);CHKERRQ(ierr);
7025c6c1daeSBarry Smith 
7035c6c1daeSBarry Smith   /* Is this file to be compressed */
7045c6c1daeSBarry Smith   vascii->storecompressed = PETSC_FALSE;
705a297a907SKarl Rupp 
7065c6c1daeSBarry Smith   ierr = PetscStrstr(vascii->filename,".gz",&gz);CHKERRQ(ierr);
7075c6c1daeSBarry Smith   if (gz) {
7085c6c1daeSBarry Smith     ierr = PetscStrlen(gz,&len);CHKERRQ(ierr);
7095c6c1daeSBarry Smith     if (len == 3) {
710*08fb59bfSBarry Smith       if (vascii->mode != FILE_MODE_WRITE) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Cannot open ASCII PetscViewer file that is compressed; uncompress it manually first");
7115c6c1daeSBarry Smith       *gz = 0;
7125c6c1daeSBarry Smith       vascii->storecompressed = PETSC_TRUE;
7135c6c1daeSBarry Smith     }
7145c6c1daeSBarry Smith   }
715ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
7165c6c1daeSBarry Smith   if (!rank) {
7175c6c1daeSBarry Smith     ierr = PetscStrcmp(name,"stderr",&isstderr);CHKERRQ(ierr);
7185c6c1daeSBarry Smith     ierr = PetscStrcmp(name,"stdout",&isstdout);CHKERRQ(ierr);
7195c6c1daeSBarry Smith     /* empty filename means stdout */
7205c6c1daeSBarry Smith     if (name[0] == 0)  isstdout = PETSC_TRUE;
7215c6c1daeSBarry Smith     if (isstderr)      vascii->fd = PETSC_STDERR;
7225c6c1daeSBarry Smith     else if (isstdout) vascii->fd = PETSC_STDOUT;
7235c6c1daeSBarry Smith     else {
7245c6c1daeSBarry Smith 
7255c6c1daeSBarry Smith 
7265c6c1daeSBarry Smith       ierr = PetscFixFilename(name,fname);CHKERRQ(ierr);
7275c6c1daeSBarry Smith       switch (vascii->mode) {
7285c6c1daeSBarry Smith       case FILE_MODE_READ:
7295c6c1daeSBarry Smith         vascii->fd = fopen(fname,"r");
7305c6c1daeSBarry Smith         break;
7315c6c1daeSBarry Smith       case FILE_MODE_WRITE:
7325c6c1daeSBarry Smith         vascii->fd = fopen(fname,"w");
7335c6c1daeSBarry Smith         break;
7345c6c1daeSBarry Smith       case FILE_MODE_APPEND:
7355c6c1daeSBarry Smith         vascii->fd = fopen(fname,"a");
7365c6c1daeSBarry Smith         break;
7375c6c1daeSBarry Smith       case FILE_MODE_UPDATE:
7385c6c1daeSBarry Smith         vascii->fd = fopen(fname,"r+");
739a297a907SKarl Rupp         if (!vascii->fd) vascii->fd = fopen(fname,"w+");
7405c6c1daeSBarry Smith         break;
7415c6c1daeSBarry Smith       case FILE_MODE_APPEND_UPDATE:
7425c6c1daeSBarry Smith         /* I really want a file which is opened at the end for updating,
7435c6c1daeSBarry Smith            not a+, which opens at the beginning, but makes writes at the end.
7445c6c1daeSBarry Smith         */
7455c6c1daeSBarry Smith         vascii->fd = fopen(fname,"r+");
746a297a907SKarl Rupp         if (!vascii->fd) vascii->fd = fopen(fname,"w+");
747a297a907SKarl Rupp         else {
7485c6c1daeSBarry Smith           ierr     = fseek(vascii->fd, 0, SEEK_END);CHKERRQ(ierr);
7495c6c1daeSBarry Smith         }
7505c6c1daeSBarry Smith         break;
7515c6c1daeSBarry Smith       default:
7525c6c1daeSBarry Smith         SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Invalid file mode %d", vascii->mode);
7535c6c1daeSBarry Smith       }
7545c6c1daeSBarry Smith       if (!vascii->fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open PetscViewer file: %s",fname);
7555c6c1daeSBarry Smith     }
7565c6c1daeSBarry Smith   }
7575c6c1daeSBarry Smith #if defined(PETSC_USE_LOG)
7585c6c1daeSBarry Smith   PetscLogObjectState((PetscObject)viewer,"File: %s",name);
7595c6c1daeSBarry Smith #endif
7605c6c1daeSBarry Smith   PetscFunctionReturn(0);
7615c6c1daeSBarry Smith }
7625c6c1daeSBarry Smith 
7633f08860eSBarry Smith PetscErrorCode PetscViewerGetSubViewer_ASCII(PetscViewer viewer,MPI_Comm subcomm,PetscViewer *outviewer)
7645c6c1daeSBarry Smith {
7655c6c1daeSBarry Smith   PetscMPIInt       rank;
7665c6c1daeSBarry Smith   PetscErrorCode    ierr;
7675c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data,*ovascii;
7685c6c1daeSBarry Smith 
7695c6c1daeSBarry Smith   PetscFunctionBegin;
7703f08860eSBarry Smith   if (vascii->sviewer) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"SubViewer already obtained from PetscViewer and not restored");
7711575c14dSBarry Smith   ierr         = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
7723f08860eSBarry Smith   ierr         = PetscViewerCreate(subcomm,outviewer);CHKERRQ(ierr);
7735c6c1daeSBarry Smith   ierr         = PetscViewerSetType(*outviewer,PETSCVIEWERASCII);CHKERRQ(ierr);
7741575c14dSBarry Smith   ierr         = PetscViewerASCIIPushSynchronized(*outviewer);CHKERRQ(ierr);
7755c6c1daeSBarry Smith   ovascii      = (PetscViewer_ASCII*)(*outviewer)->data;
7765c6c1daeSBarry Smith   ovascii->fd  = vascii->fd;
7775c6c1daeSBarry Smith   ovascii->tab = vascii->tab;
778ba5a0b41SBarry Smith   ovascii->closefile = PETSC_FALSE;
7795c6c1daeSBarry Smith 
7805c6c1daeSBarry Smith   vascii->sviewer = *outviewer;
7815c6c1daeSBarry Smith 
7825c6c1daeSBarry Smith   (*outviewer)->format  = viewer->format;
7835c6c1daeSBarry Smith 
784ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
7855c6c1daeSBarry Smith   ((PetscViewer_ASCII*)((*outviewer)->data))->bviewer = viewer;
7863f08860eSBarry Smith   (*outviewer)->ops->destroy = PetscViewerDestroy_ASCII_SubViewer;
7875c6c1daeSBarry Smith   PetscFunctionReturn(0);
7885c6c1daeSBarry Smith }
7895c6c1daeSBarry Smith 
7903f08860eSBarry Smith PetscErrorCode PetscViewerRestoreSubViewer_ASCII(PetscViewer viewer,MPI_Comm comm,PetscViewer *outviewer)
7915c6c1daeSBarry Smith {
7925c6c1daeSBarry Smith   PetscErrorCode    ierr;
7935c6c1daeSBarry Smith   PetscViewer_ASCII *ascii  = (PetscViewer_ASCII*)viewer->data;
7945c6c1daeSBarry Smith 
7955c6c1daeSBarry Smith   PetscFunctionBegin;
7961575c14dSBarry Smith   if (!ascii->sviewer) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"SubViewer never obtained from PetscViewer");
7971575c14dSBarry Smith   if (ascii->sviewer != *outviewer) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"This PetscViewer did not generate this SubViewer");
7985c6c1daeSBarry Smith 
7995c6c1daeSBarry Smith   ascii->sviewer             = 0;
8005c6c1daeSBarry Smith   (*outviewer)->ops->destroy = PetscViewerDestroy_ASCII;
8015c6c1daeSBarry Smith   ierr                       = PetscViewerDestroy(outviewer);CHKERRQ(ierr);
8025c6c1daeSBarry Smith   PetscFunctionReturn(0);
8035c6c1daeSBarry Smith }
8045c6c1daeSBarry Smith 
8052bf49c77SBarry Smith PetscErrorCode  PetscViewerView_ASCII(PetscViewer v,PetscViewer viewer)
8062bf49c77SBarry Smith {
8072bf49c77SBarry Smith   PetscErrorCode    ierr;
8082bf49c77SBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)v->data;
8092bf49c77SBarry Smith 
8102bf49c77SBarry Smith   PetscFunctionBegin;
8112bf49c77SBarry Smith   if (ascii->filename) {
8122bf49c77SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"Filename: %s\n",ascii->filename);CHKERRQ(ierr);
8132bf49c77SBarry Smith   }
8142bf49c77SBarry Smith   PetscFunctionReturn(0);
8152bf49c77SBarry Smith }
8162bf49c77SBarry Smith 
8178556b5ebSBarry Smith /*MC
8188556b5ebSBarry Smith    PETSCVIEWERASCII - A viewer that prints to stdout or an ASCII file
8198556b5ebSBarry Smith 
8208556b5ebSBarry Smith 
8218556b5ebSBarry Smith .seealso:  PETSC_VIEWER_STDOUT_(),PETSC_VIEWER_STDOUT_SELF, PETSC_VIEWER_STDOUT_WORLD,PetscViewerCreate(), PetscViewerASCIIOpen(),
8228556b5ebSBarry Smith            PetscViewerMatlabOpen(), VecView(), DMView(), PetscViewerMatlabPutArray(), PETSCVIEWERBINARY, PETSCVIEWERMATLAB,
8238556b5ebSBarry Smith            PetscViewerFileSetName(), PetscViewerFileSetMode(), PetscViewerFormat, PetscViewerType, PetscViewerSetType()
8248556b5ebSBarry Smith 
8251b266c99SBarry Smith   Level: beginner
8261b266c99SBarry Smith 
8278556b5ebSBarry Smith M*/
8288cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscViewerCreate_ASCII(PetscViewer viewer)
8295c6c1daeSBarry Smith {
8305c6c1daeSBarry Smith   PetscViewer_ASCII *vascii;
8315c6c1daeSBarry Smith   PetscErrorCode    ierr;
8325c6c1daeSBarry Smith 
8335c6c1daeSBarry Smith   PetscFunctionBegin;
834b00a9115SJed Brown   ierr         = PetscNewLog(viewer,&vascii);CHKERRQ(ierr);
8355c6c1daeSBarry Smith   viewer->data = (void*)vascii;
8365c6c1daeSBarry Smith 
8375c6c1daeSBarry Smith   viewer->ops->destroy          = PetscViewerDestroy_ASCII;
8385c6c1daeSBarry Smith   viewer->ops->flush            = PetscViewerFlush_ASCII;
839559f443fSBarry Smith   viewer->ops->getsubviewer     = PetscViewerGetSubViewer_ASCII;
840559f443fSBarry Smith   viewer->ops->restoresubviewer = PetscViewerRestoreSubViewer_ASCII;
8412bf49c77SBarry Smith   viewer->ops->view             = PetscViewerView_ASCII;
8421d641e7bSMichael Lange   viewer->ops->read             = PetscViewerASCIIRead;
8435c6c1daeSBarry Smith 
8445c6c1daeSBarry Smith   /* defaults to stdout unless set with PetscViewerFileSetName() */
8455c6c1daeSBarry Smith   vascii->fd        = PETSC_STDOUT;
8465c6c1daeSBarry Smith   vascii->mode      = FILE_MODE_WRITE;
8475c6c1daeSBarry Smith   vascii->bviewer   = 0;
848dd2fa690SBarry Smith   vascii->subviewer = 0;
8495c6c1daeSBarry Smith   vascii->sviewer   = 0;
8505c6c1daeSBarry Smith   vascii->tab       = 0;
8515c6c1daeSBarry Smith   vascii->tab_store = 0;
8525c6c1daeSBarry Smith   vascii->filename  = 0;
8535c6c1daeSBarry Smith   vascii->closefile = PETSC_TRUE;
8545c6c1daeSBarry Smith 
855bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetName_C",PetscViewerFileSetName_ASCII);CHKERRQ(ierr);
856bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileGetName_C",PetscViewerFileGetName_ASCII);CHKERRQ(ierr);
857bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileGetMode_C",PetscViewerFileGetMode_ASCII);CHKERRQ(ierr);
858bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetMode_C",PetscViewerFileSetMode_ASCII);CHKERRQ(ierr);
8595c6c1daeSBarry Smith   PetscFunctionReturn(0);
8605c6c1daeSBarry Smith }
8615c6c1daeSBarry Smith 
8625c6c1daeSBarry Smith /*@C
8635c6c1daeSBarry Smith     PetscViewerASCIISynchronizedPrintf - Prints synchronized output to the specified file from
8645c6c1daeSBarry Smith     several processors.  Output of the first processor is followed by that of the
8655c6c1daeSBarry Smith     second, etc.
8665c6c1daeSBarry Smith 
8675c6c1daeSBarry Smith     Not Collective, must call collective PetscViewerFlush() to get the results out
8685c6c1daeSBarry Smith 
8695c6c1daeSBarry Smith     Input Parameters:
8705c6c1daeSBarry Smith +   viewer - the ASCII PetscViewer
8715c6c1daeSBarry Smith -   format - the usual printf() format string
8725c6c1daeSBarry Smith 
8735c6c1daeSBarry Smith     Level: intermediate
8745c6c1daeSBarry Smith 
8755c6c1daeSBarry Smith     Notes: You must have previously called PetscViewerASCIISynchronizeAllow() to allow this routine to be called.
8765c6c1daeSBarry Smith 
8775c6c1daeSBarry Smith     Fortran Note:
8785c6c1daeSBarry Smith       Can only print a single character* string
8795c6c1daeSBarry Smith 
8805c6c1daeSBarry Smith .seealso: PetscSynchronizedPrintf(), PetscSynchronizedFlush(), PetscFPrintf(),
8815c6c1daeSBarry Smith           PetscFOpen(), PetscViewerFlush(), PetscViewerASCIIGetPointer(), PetscViewerDestroy(), PetscViewerASCIIOpen(),
8821575c14dSBarry Smith           PetscViewerASCIIPrintf(), PetscViewerASCIIPushSynchronized()
8835c6c1daeSBarry Smith 
8845c6c1daeSBarry Smith @*/
8855c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIISynchronizedPrintf(PetscViewer viewer,const char format[],...)
8865c6c1daeSBarry Smith {
8875c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
8885c6c1daeSBarry Smith   PetscErrorCode    ierr;
8893f08860eSBarry Smith   PetscMPIInt       rank;
8905c6c1daeSBarry Smith   PetscInt          tab = vascii->tab;
8915c6c1daeSBarry Smith   MPI_Comm          comm;
8925c6c1daeSBarry Smith   FILE              *fp;
893559f443fSBarry Smith   PetscBool         iascii,hasbviewer = PETSC_FALSE;
8945c6c1daeSBarry Smith   int               err;
8955c6c1daeSBarry Smith 
8965c6c1daeSBarry Smith   PetscFunctionBegin;
8975c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
8985c6c1daeSBarry Smith   PetscValidCharPointer(format,2);
8995c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
9005c6c1daeSBarry Smith   if (!iascii) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Not ASCII PetscViewer");
9011575c14dSBarry Smith   if (!vascii->allowsynchronized) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"First call PetscViewerASCIIPushSynchronized() to allow this call");
9025c6c1daeSBarry Smith 
903ce94432eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
9045c6c1daeSBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
9055c6c1daeSBarry Smith 
906559f443fSBarry Smith   if (vascii->bviewer) {
907559f443fSBarry Smith     hasbviewer = PETSC_TRUE;
908559f443fSBarry Smith     if (!rank) {
909559f443fSBarry Smith       vascii = (PetscViewer_ASCII*)vascii->bviewer->data;
910559f443fSBarry Smith       ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
911559f443fSBarry Smith       ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
912559f443fSBarry Smith     }
913559f443fSBarry Smith   }
9143f08860eSBarry Smith 
915559f443fSBarry Smith   fp   = vascii->fd;
916559f443fSBarry Smith 
917559f443fSBarry Smith   if (!rank && !hasbviewer) {   /* First processor prints immediately to fp */
9185c6c1daeSBarry Smith     va_list Argp;
919559f443fSBarry Smith     /* flush my own messages that I may have queued up */
920559f443fSBarry Smith     PrintfQueue next = vascii->petsc_printfqueuebase,previous;
921559f443fSBarry Smith     PetscInt    i;
922559f443fSBarry Smith     for (i=0; i<vascii->petsc_printfqueuelength; i++) {
923559f443fSBarry Smith       ierr = PetscFPrintf(comm,fp,"%s",next->string);CHKERRQ(ierr);
924559f443fSBarry Smith       previous = next;
925559f443fSBarry Smith       next     = next->next;
926559f443fSBarry Smith       ierr     = PetscFree(previous->string);CHKERRQ(ierr);
927559f443fSBarry Smith       ierr     = PetscFree(previous);CHKERRQ(ierr);
928559f443fSBarry Smith     }
929559f443fSBarry Smith     vascii->petsc_printfqueue       = 0;
930559f443fSBarry Smith     vascii->petsc_printfqueuelength = 0;
9315c6c1daeSBarry Smith 
932a297a907SKarl Rupp     while (tab--) {
933a297a907SKarl Rupp       ierr = PetscFPrintf(PETSC_COMM_SELF,fp,"  ");CHKERRQ(ierr);
934a297a907SKarl Rupp     }
9355c6c1daeSBarry Smith 
9365c6c1daeSBarry Smith     va_start(Argp,format);
9375c6c1daeSBarry Smith     ierr = (*PetscVFPrintf)(fp,format,Argp);CHKERRQ(ierr);
9385c6c1daeSBarry Smith     err  = fflush(fp);
9395c6c1daeSBarry Smith     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
9405c6c1daeSBarry Smith     if (petsc_history) {
9415c6c1daeSBarry Smith       va_start(Argp,format);
9425c6c1daeSBarry Smith       ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr);
9435c6c1daeSBarry Smith       err  = fflush(petsc_history);
9445c6c1daeSBarry Smith       if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
9455c6c1daeSBarry Smith     }
9465c6c1daeSBarry Smith     va_end(Argp);
947559f443fSBarry Smith   } else { /* other processors add to queue */
9485c6c1daeSBarry Smith     char        *string;
9495c6c1daeSBarry Smith     va_list     Argp;
9505c6c1daeSBarry Smith     size_t      fullLength;
9515c6c1daeSBarry Smith     PrintfQueue next;
9525c6c1daeSBarry Smith 
953b00a9115SJed Brown     ierr = PetscNew(&next);CHKERRQ(ierr);
954559f443fSBarry Smith     if (vascii->petsc_printfqueue) {
955559f443fSBarry Smith       vascii->petsc_printfqueue->next = next;
956559f443fSBarry Smith       vascii->petsc_printfqueue       = next;
957a297a907SKarl Rupp     } else {
958559f443fSBarry Smith       vascii->petsc_printfqueuebase = vascii->petsc_printfqueue = next;
959a297a907SKarl Rupp     }
960559f443fSBarry Smith     vascii->petsc_printfqueuelength++;
9615c6c1daeSBarry Smith     next->size = QUEUESTRINGSIZE;
962785e854fSJed Brown     ierr       = PetscMalloc1(next->size, &next->string);CHKERRQ(ierr);
9635c6c1daeSBarry Smith     ierr       = PetscMemzero(next->string,next->size);CHKERRQ(ierr);
9645c6c1daeSBarry Smith     string     = next->string;
9655c6c1daeSBarry Smith     tab       *= 2;
966a297a907SKarl Rupp     while (tab--) {
967a297a907SKarl Rupp       *string++ = ' ';
968a297a907SKarl Rupp     }
9695c6c1daeSBarry Smith     va_start(Argp,format);
97022d28d08SBarry Smith     ierr = PetscVSNPrintf(string,next->size-2*vascii->tab,format,&fullLength,Argp);CHKERRQ(ierr);
9715c6c1daeSBarry Smith     va_end(Argp);
9725c6c1daeSBarry Smith   }
9735c6c1daeSBarry Smith   PetscFunctionReturn(0);
9745c6c1daeSBarry Smith }
9755c6c1daeSBarry Smith 
9762655f987SMichael Lange /*@C
9772655f987SMichael Lange    PetscViewerASCIIRead - Reads from am ASCII file
9782655f987SMichael Lange 
9792655f987SMichael Lange    Collective on MPI_Comm
9802655f987SMichael Lange 
9812655f987SMichael Lange    Input Parameters:
9822655f987SMichael Lange +  viewer - the ascii viewer
9832655f987SMichael Lange .  data - location to write the data
984060da220SMatthew G. Knepley .  num - number of items of data to read
9852655f987SMichael Lange -  datatype - type of data to read
9862655f987SMichael Lange 
987f8e4bde8SMatthew G. Knepley    Output Parameters:
988060da220SMatthew G. Knepley .  count - number of items of data actually read, or NULL
989f8e4bde8SMatthew G. Knepley 
9902655f987SMichael Lange    Level: beginner
9912655f987SMichael Lange 
9922655f987SMichael Lange    Concepts: ascii files
9932655f987SMichael Lange 
9946a9046bcSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(),
9952655f987SMichael Lange           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
9962655f987SMichael Lange           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead()
9972655f987SMichael Lange @*/
998060da220SMatthew G. Knepley PetscErrorCode PetscViewerASCIIRead(PetscViewer viewer,void *data,PetscInt num,PetscInt *count,PetscDataType dtype)
9992655f987SMichael Lange {
10002655f987SMichael Lange   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
10012655f987SMichael Lange   FILE              *fd = vascii->fd;
10022655f987SMichael Lange   PetscInt           i;
10033b7fe8c3SMatthew G. Knepley   int                ret = 0;
10042655f987SMichael Lange 
10052655f987SMichael Lange   PetscFunctionBegin;
10062655f987SMichael Lange   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
1007060da220SMatthew G. Knepley   for (i=0; i<num; i++) {
1008f8e4bde8SMatthew G. Knepley     if (dtype == PETSC_CHAR)         ret = fscanf(fd, "%c",  &(((char*)data)[i]));
1009f8e4bde8SMatthew G. Knepley     else if (dtype == PETSC_STRING)  ret = fscanf(fd, "%s",  &(((char*)data)[i]));
1010a05e1a72SSatish Balay     else if (dtype == PETSC_INT)     ret = fscanf(fd, "%" PetscInt_FMT,  &(((PetscInt*)data)[i]));
1011f8e4bde8SMatthew G. Knepley     else if (dtype == PETSC_ENUM)    ret = fscanf(fd, "%d",  &(((int*)data)[i]));
1012f8e4bde8SMatthew G. Knepley     else if (dtype == PETSC_FLOAT)   ret = fscanf(fd, "%f",  &(((float*)data)[i]));
1013f8e4bde8SMatthew G. Knepley     else if (dtype == PETSC_DOUBLE)  ret = fscanf(fd, "%lg", &(((double*)data)[i]));
1014f8e4bde8SMatthew G. Knepley     else {SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Data type %d not supported", (int) dtype);}
1015f8e4bde8SMatthew G. Knepley     if (!ret) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Conversion error for data type %d", (int) dtype);
1016f8e4bde8SMatthew G. Knepley     else if (ret < 0) break; /* Proxy for EOF, need to check for it in configure */
10172655f987SMichael Lange   }
1018060da220SMatthew G. Knepley   if (count) *count = i;
1019060da220SMatthew G. Knepley   else if (ret < 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Insufficient data, read only %D < %D items", i, num);
10202655f987SMichael Lange   PetscFunctionReturn(0);
10212655f987SMichael Lange }
10225c6c1daeSBarry Smith 
1023