xref: /petsc/src/sys/classes/viewer/impls/ascii/filev.c (revision e5afcf2835ad2df3c79a70d4d9a0fbb86e97247e)
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;
14*e5afcf28SBarry Smith   if (vascii->sviewer) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_WRONGSTATE,"Cannot call with outstanding call to PetscViewerRestoreSubViewer()");
15ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
165c6c1daeSBarry Smith   if (!rank && vascii->fd != stderr && vascii->fd != PETSC_STDOUT) {
175c6c1daeSBarry Smith     if (vascii->fd && vascii->closefile) {
185c6c1daeSBarry Smith       err = fclose(vascii->fd);
195c6c1daeSBarry Smith       if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
205c6c1daeSBarry Smith     }
215c6c1daeSBarry Smith     if (vascii->storecompressed) {
225c6c1daeSBarry Smith       char par[PETSC_MAX_PATH_LEN],buf[PETSC_MAX_PATH_LEN];
235c6c1daeSBarry Smith       FILE *fp;
24a126751eSBarry Smith       ierr = PetscStrncpy(par,"gzip ",sizeof(par));CHKERRQ(ierr);
25a126751eSBarry Smith       ierr = PetscStrlcat(par,vascii->filename,sizeof(par));CHKERRQ(ierr);
265c6c1daeSBarry Smith #if defined(PETSC_HAVE_POPEN)
270298fd71SBarry Smith       ierr = PetscPOpen(PETSC_COMM_SELF,NULL,par,"r",&fp);CHKERRQ(ierr);
28f23aa3ddSBarry Smith       if (fgets(buf,1024,fp)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error from compression command %s\n%s",par,buf);
29016831caSBarry Smith       ierr = PetscPClose(PETSC_COMM_SELF,fp);CHKERRQ(ierr);
305c6c1daeSBarry Smith #else
315c6c1daeSBarry Smith       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
325c6c1daeSBarry Smith #endif
335c6c1daeSBarry Smith     }
345c6c1daeSBarry Smith   }
355c6c1daeSBarry Smith   ierr = PetscFree(vascii->filename);CHKERRQ(ierr);
365c6c1daeSBarry Smith   PetscFunctionReturn(0);
375c6c1daeSBarry Smith }
385c6c1daeSBarry Smith 
395c6c1daeSBarry Smith /* ----------------------------------------------------------------------*/
405c6c1daeSBarry Smith PetscErrorCode PetscViewerDestroy_ASCII(PetscViewer viewer)
415c6c1daeSBarry Smith {
425c6c1daeSBarry Smith   PetscErrorCode    ierr;
435c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
445c6c1daeSBarry Smith   PetscViewerLink   *vlink;
455c6c1daeSBarry Smith   PetscBool         flg;
465c6c1daeSBarry Smith 
475c6c1daeSBarry Smith   PetscFunctionBegin;
48*e5afcf28SBarry Smith   if (vascii->sviewer) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_WRONGSTATE,"Cannot call with outstanding call to PetscViewerRestoreSubViewer()");
495c6c1daeSBarry Smith   ierr = PetscViewerFileClose_ASCII(viewer);CHKERRQ(ierr);
505c6c1daeSBarry Smith   ierr = PetscFree(vascii);CHKERRQ(ierr);
515c6c1daeSBarry Smith 
525c6c1daeSBarry Smith   /* remove the viewer from the list in the MPI Communicator */
535c6c1daeSBarry Smith   if (Petsc_Viewer_keyval == MPI_KEYVAL_INVALID) {
5412801b39SBarry Smith     ierr = MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN,Petsc_DelViewer,&Petsc_Viewer_keyval,(void*)0);CHKERRQ(ierr);
555c6c1daeSBarry Smith   }
565c6c1daeSBarry Smith 
5747435625SJed Brown   ierr = MPI_Comm_get_attr(PetscObjectComm((PetscObject)viewer),Petsc_Viewer_keyval,(void**)&vlink,(PetscMPIInt*)&flg);CHKERRQ(ierr);
585c6c1daeSBarry Smith   if (flg) {
595c6c1daeSBarry Smith     if (vlink && vlink->viewer == viewer) {
60e5840a18SBarry Smith       if (vlink->next) {
6147435625SJed Brown         ierr = MPI_Comm_set_attr(PetscObjectComm((PetscObject)viewer),Petsc_Viewer_keyval,vlink->next);CHKERRQ(ierr);
62e5840a18SBarry Smith       } else {
6347435625SJed Brown         ierr = MPI_Comm_delete_attr(PetscObjectComm((PetscObject)viewer),Petsc_Viewer_keyval);CHKERRQ(ierr);
64e5840a18SBarry Smith       }
655c6c1daeSBarry Smith       ierr = PetscFree(vlink);CHKERRQ(ierr);
665c6c1daeSBarry Smith     } else {
675c6c1daeSBarry Smith       while (vlink && vlink->next) {
685c6c1daeSBarry Smith         if (vlink->next->viewer == viewer) {
695c6c1daeSBarry Smith           PetscViewerLink *nv = vlink->next;
705c6c1daeSBarry Smith           vlink->next = vlink->next->next;
715c6c1daeSBarry Smith           ierr = PetscFree(nv);CHKERRQ(ierr);
725c6c1daeSBarry Smith         }
735c6c1daeSBarry Smith         vlink = vlink->next;
745c6c1daeSBarry Smith       }
755c6c1daeSBarry Smith     }
765c6c1daeSBarry Smith   }
77aa139df6SJed Brown 
78aa139df6SJed Brown   if (Petsc_Viewer_Stdout_keyval != MPI_KEYVAL_INVALID) {
79aa139df6SJed Brown     PetscViewer aviewer;
8047435625SJed Brown     ierr = MPI_Comm_get_attr(PetscObjectComm((PetscObject)viewer),Petsc_Viewer_Stdout_keyval,(void**)&aviewer,(PetscMPIInt*)&flg);CHKERRQ(ierr);
81aa139df6SJed Brown     if (flg && aviewer == viewer) {
8247435625SJed Brown       ierr = MPI_Comm_delete_attr(PetscObjectComm((PetscObject)viewer),Petsc_Viewer_Stdout_keyval);CHKERRQ(ierr);
83aa139df6SJed Brown     }
84aa139df6SJed Brown   }
85aa139df6SJed Brown   if (Petsc_Viewer_Stderr_keyval != MPI_KEYVAL_INVALID) {
86aa139df6SJed Brown     PetscViewer aviewer;
8747435625SJed Brown     ierr = MPI_Comm_get_attr(PetscObjectComm((PetscObject)viewer),Petsc_Viewer_Stderr_keyval,(void**)&aviewer,(PetscMPIInt*)&flg);CHKERRQ(ierr);
88aa139df6SJed Brown     if (flg && aviewer == viewer) {
8947435625SJed Brown       ierr = MPI_Comm_delete_attr(PetscObjectComm((PetscObject)viewer),Petsc_Viewer_Stderr_keyval);CHKERRQ(ierr);
90aa139df6SJed Brown     }
91aa139df6SJed Brown   }
925c6c1daeSBarry Smith   PetscFunctionReturn(0);
935c6c1daeSBarry Smith }
945c6c1daeSBarry Smith 
953f08860eSBarry Smith PetscErrorCode PetscViewerDestroy_ASCII_SubViewer(PetscViewer viewer)
965c6c1daeSBarry Smith {
975c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
985c6c1daeSBarry Smith   PetscErrorCode    ierr;
995fd66863SKarl Rupp 
1005c6c1daeSBarry Smith   PetscFunctionBegin;
1013f08860eSBarry Smith   ierr = PetscViewerRestoreSubViewer(vascii->bviewer,0,&viewer);CHKERRQ(ierr);
1025c6c1daeSBarry Smith   PetscFunctionReturn(0);
1035c6c1daeSBarry Smith }
1045c6c1daeSBarry Smith 
1055c6c1daeSBarry Smith PetscErrorCode PetscViewerFlush_ASCII(PetscViewer viewer)
1065c6c1daeSBarry Smith {
1075c6c1daeSBarry Smith   PetscErrorCode    ierr;
1085c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
1095c6c1daeSBarry Smith   int               err;
110559f443fSBarry Smith   MPI_Comm          comm;
111559f443fSBarry Smith   PetscMPIInt       rank,size;
112559f443fSBarry Smith   FILE              *fd = vascii->fd;
1135c6c1daeSBarry Smith 
1145c6c1daeSBarry Smith   PetscFunctionBegin;
115*e5afcf28SBarry Smith   if (vascii->sviewer) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_WRONGSTATE,"Cannot call with outstanding call to PetscViewerRestoreSubViewer()");
116559f443fSBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
117559f443fSBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
118559f443fSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
119559f443fSBarry Smith 
120559f443fSBarry Smith   if (!vascii->bviewer && !rank && (vascii->mode != FILE_MODE_READ)) {
1215c6c1daeSBarry Smith     err = fflush(vascii->fd);
1225c6c1daeSBarry Smith     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() call failed");
1235c6c1daeSBarry Smith   }
1245c6c1daeSBarry Smith 
1255c6c1daeSBarry Smith   if (vascii->allowsynchronized) {
126559f443fSBarry Smith     PetscMPIInt   tag,i,j,n = 0,dummy = 0;
127559f443fSBarry Smith     char          *message;
128559f443fSBarry Smith     MPI_Status    status;
129559f443fSBarry Smith 
130559f443fSBarry Smith     ierr = PetscCommDuplicate(comm,&comm,&tag);CHKERRQ(ierr);
131559f443fSBarry Smith 
132559f443fSBarry Smith     /* First processor waits for messages from all other processors */
133559f443fSBarry Smith     if (!rank) {
134559f443fSBarry Smith       /* flush my own messages that I may have queued up */
135559f443fSBarry Smith       PrintfQueue next = vascii->petsc_printfqueuebase,previous;
136559f443fSBarry Smith       for (i=0; i<vascii->petsc_printfqueuelength; i++) {
137559f443fSBarry Smith         if (!vascii->bviewer) {
138559f443fSBarry Smith           ierr = PetscFPrintf(comm,fd,"%s",next->string);CHKERRQ(ierr);
139559f443fSBarry Smith         } else {
140559f443fSBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(vascii->bviewer,"%s",next->string);CHKERRQ(ierr);
141559f443fSBarry Smith         }
142559f443fSBarry Smith         previous = next;
143559f443fSBarry Smith         next     = next->next;
144559f443fSBarry Smith         ierr     = PetscFree(previous->string);CHKERRQ(ierr);
145559f443fSBarry Smith         ierr     = PetscFree(previous);CHKERRQ(ierr);
146559f443fSBarry Smith       }
147559f443fSBarry Smith       vascii->petsc_printfqueue       = 0;
148559f443fSBarry Smith       vascii->petsc_printfqueuelength = 0;
149559f443fSBarry Smith       for (i=1; i<size; i++) {
150559f443fSBarry Smith         /* to prevent a flood of messages to process zero, request each message separately */
151559f443fSBarry Smith         ierr = MPI_Send(&dummy,1,MPI_INT,i,tag,comm);CHKERRQ(ierr);
152559f443fSBarry Smith         ierr = MPI_Recv(&n,1,MPI_INT,i,tag,comm,&status);CHKERRQ(ierr);
153559f443fSBarry Smith         for (j=0; j<n; j++) {
154559f443fSBarry Smith           PetscMPIInt size = 0;
155559f443fSBarry Smith 
156559f443fSBarry Smith           ierr = MPI_Recv(&size,1,MPI_INT,i,tag,comm,&status);CHKERRQ(ierr);
157559f443fSBarry Smith           ierr = PetscMalloc1(size, &message);CHKERRQ(ierr);
158559f443fSBarry Smith           ierr = MPI_Recv(message,size,MPI_CHAR,i,tag,comm,&status);CHKERRQ(ierr);
159559f443fSBarry Smith           if (!vascii->bviewer) {
160559f443fSBarry Smith             ierr = PetscFPrintf(comm,fd,"%s",message);CHKERRQ(ierr);
161559f443fSBarry Smith           } else {
162559f443fSBarry Smith             ierr = PetscViewerASCIISynchronizedPrintf(vascii->bviewer,"%s",message);CHKERRQ(ierr);
163559f443fSBarry Smith           }
164559f443fSBarry Smith           ierr = PetscFree(message);CHKERRQ(ierr);
165559f443fSBarry Smith         }
166559f443fSBarry Smith       }
167559f443fSBarry Smith     } else { /* other processors send queue to processor 0 */
168559f443fSBarry Smith       PrintfQueue next = vascii->petsc_printfqueuebase,previous;
169559f443fSBarry Smith 
170559f443fSBarry Smith       ierr = MPI_Recv(&dummy,1,MPI_INT,0,tag,comm,&status);CHKERRQ(ierr);
171559f443fSBarry Smith       ierr = MPI_Send(&vascii->petsc_printfqueuelength,1,MPI_INT,0,tag,comm);CHKERRQ(ierr);
172559f443fSBarry Smith       for (i=0; i<vascii->petsc_printfqueuelength; i++) {
173559f443fSBarry Smith         ierr     = MPI_Send(&next->size,1,MPI_INT,0,tag,comm);CHKERRQ(ierr);
174559f443fSBarry Smith         ierr     = MPI_Send(next->string,next->size,MPI_CHAR,0,tag,comm);CHKERRQ(ierr);
175559f443fSBarry Smith         previous = next;
176559f443fSBarry Smith         next     = next->next;
177559f443fSBarry Smith         ierr     = PetscFree(previous->string);CHKERRQ(ierr);
178559f443fSBarry Smith         ierr     = PetscFree(previous);CHKERRQ(ierr);
179559f443fSBarry Smith       }
180559f443fSBarry Smith       vascii->petsc_printfqueue       = 0;
181559f443fSBarry Smith       vascii->petsc_printfqueuelength = 0;
182559f443fSBarry Smith     }
183559f443fSBarry Smith     ierr = PetscCommDestroy(&comm);CHKERRQ(ierr);
1845c6c1daeSBarry Smith   }
1855c6c1daeSBarry Smith   PetscFunctionReturn(0);
1865c6c1daeSBarry Smith }
1875c6c1daeSBarry Smith 
1885c6c1daeSBarry Smith /*@C
1895c6c1daeSBarry Smith     PetscViewerASCIIGetPointer - Extracts the file pointer from an ASCII PetscViewer.
1905c6c1daeSBarry Smith 
191f8859db6SBarry Smith     Not Collective, depending on the viewer the value may be meaningless except for process 0 of the viewer
1925c6c1daeSBarry Smith 
193f8859db6SBarry Smith     Input Parameter:
194f8859db6SBarry Smith .    viewer - PetscViewer context, obtained from PetscViewerASCIIOpen()
195f8859db6SBarry Smith 
196f8859db6SBarry Smith     Output Parameter:
197f8859db6SBarry Smith .    fd - file pointer
198f8859db6SBarry Smith 
199f8859db6SBarry Smith     Notes: for the standard PETSCVIEWERASCII the value is valid only on process 0 of the viewer
2005c6c1daeSBarry Smith 
2015c6c1daeSBarry Smith     Level: intermediate
2025c6c1daeSBarry Smith 
2035c6c1daeSBarry Smith     Fortran Note:
2045c6c1daeSBarry Smith     This routine is not supported in Fortran.
2055c6c1daeSBarry Smith 
2065c6c1daeSBarry Smith   Concepts: PetscViewer^file pointer
2075c6c1daeSBarry Smith   Concepts: file pointer^getting from PetscViewer
2085c6c1daeSBarry Smith 
2095c6c1daeSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerCreate(), PetscViewerASCIIPrintf(),
2105c6c1daeSBarry Smith           PetscViewerASCIISynchronizedPrintf(), PetscViewerFlush()
2115c6c1daeSBarry Smith @*/
2125c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIGetPointer(PetscViewer viewer,FILE **fd)
2135c6c1daeSBarry Smith {
2145c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
2155c6c1daeSBarry Smith 
2165c6c1daeSBarry Smith   PetscFunctionBegin;
2175c6c1daeSBarry Smith   *fd = vascii->fd;
2185c6c1daeSBarry Smith   PetscFunctionReturn(0);
2195c6c1daeSBarry Smith }
2205c6c1daeSBarry Smith 
2215c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileGetMode_ASCII(PetscViewer viewer, PetscFileMode *mode)
2225c6c1daeSBarry Smith {
2235c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
2245c6c1daeSBarry Smith 
2255c6c1daeSBarry Smith   PetscFunctionBegin;
2265c6c1daeSBarry Smith   *mode = vascii->mode;
2275c6c1daeSBarry Smith   PetscFunctionReturn(0);
2285c6c1daeSBarry Smith }
2295c6c1daeSBarry Smith 
2305c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileSetMode_ASCII(PetscViewer viewer, PetscFileMode mode)
2315c6c1daeSBarry Smith {
2325c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
2335c6c1daeSBarry Smith 
2345c6c1daeSBarry Smith   PetscFunctionBegin;
2355c6c1daeSBarry Smith   vascii->mode = mode;
2365c6c1daeSBarry Smith   PetscFunctionReturn(0);
2375c6c1daeSBarry Smith }
2385c6c1daeSBarry Smith 
2395c6c1daeSBarry Smith /*
2405c6c1daeSBarry Smith    If petsc_history is on, then all Petsc*Printf() results are saved
2415c6c1daeSBarry Smith    if the appropriate (usually .petschistory) file.
2425c6c1daeSBarry Smith */
24395c0884eSLisandro Dalcin PETSC_INTERN FILE *petsc_history;
2445c6c1daeSBarry Smith 
2455c6c1daeSBarry Smith /*@
2465c6c1daeSBarry Smith     PetscViewerASCIISetTab - Causes PetscViewer to tab in a number of times
2475c6c1daeSBarry Smith 
2485c6c1daeSBarry Smith     Not Collective, but only first processor in set has any effect
2495c6c1daeSBarry Smith 
2505c6c1daeSBarry Smith     Input Parameters:
2511575c14dSBarry Smith +    viewer - obtained with PetscViewerASCIIOpen()
2525c6c1daeSBarry Smith -    tabs - number of tabs
2535c6c1daeSBarry Smith 
2545c6c1daeSBarry Smith     Level: developer
2555c6c1daeSBarry Smith 
2565c6c1daeSBarry Smith     Fortran Note:
2575c6c1daeSBarry Smith     This routine is not supported in Fortran.
2585c6c1daeSBarry Smith 
2595c6c1daeSBarry Smith   Concepts: PetscViewerASCII^formating
2605c6c1daeSBarry Smith   Concepts: tab^setting
2615c6c1daeSBarry Smith 
2625c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(), PetscViewerASCIIGetTab(),
2635c6c1daeSBarry Smith           PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(),
2645c6c1daeSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer(), PetscViewerASCIIPushTab()
2655c6c1daeSBarry Smith @*/
2665c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIISetTab(PetscViewer viewer,PetscInt tabs)
2675c6c1daeSBarry Smith {
2685c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
2695c6c1daeSBarry Smith   PetscBool         iascii;
2705c6c1daeSBarry Smith   PetscErrorCode    ierr;
2715c6c1daeSBarry Smith 
2725c6c1daeSBarry Smith   PetscFunctionBegin;
2735c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
2745c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
275a297a907SKarl Rupp   if (iascii) ascii->tab = tabs;
2765c6c1daeSBarry Smith   PetscFunctionReturn(0);
2775c6c1daeSBarry Smith }
2785c6c1daeSBarry Smith 
2795c6c1daeSBarry Smith /*@
2805c6c1daeSBarry Smith     PetscViewerASCIIGetTab - Return the number of tabs used by PetscViewer.
2815c6c1daeSBarry Smith 
2825c6c1daeSBarry Smith     Not Collective, meaningful on first processor only.
2835c6c1daeSBarry Smith 
2845c6c1daeSBarry Smith     Input Parameters:
2851575c14dSBarry Smith .    viewer - obtained with PetscViewerASCIIOpen()
2865c6c1daeSBarry Smith     Output Parameters:
2875c6c1daeSBarry Smith .    tabs - number of tabs
2885c6c1daeSBarry Smith 
2895c6c1daeSBarry Smith     Level: developer
2905c6c1daeSBarry Smith 
2915c6c1daeSBarry Smith     Fortran Note:
2925c6c1daeSBarry Smith     This routine is not supported in Fortran.
2935c6c1daeSBarry Smith 
2945c6c1daeSBarry Smith   Concepts: PetscViewerASCII^formating
2955c6c1daeSBarry Smith   Concepts: tab^retrieval
2965c6c1daeSBarry Smith 
2975c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(), PetscViewerASCIISetTab(),
2985c6c1daeSBarry Smith           PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(),
2995c6c1daeSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer(), PetscViewerASCIIPushTab()
3005c6c1daeSBarry Smith @*/
3015c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIGetTab(PetscViewer viewer,PetscInt *tabs)
3025c6c1daeSBarry Smith {
3035c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
3045c6c1daeSBarry Smith   PetscBool         iascii;
3055c6c1daeSBarry Smith   PetscErrorCode    ierr;
3065c6c1daeSBarry Smith 
3075c6c1daeSBarry Smith   PetscFunctionBegin;
3085c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
3095c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
310a297a907SKarl Rupp   if (iascii && tabs) *tabs = ascii->tab;
3115c6c1daeSBarry Smith   PetscFunctionReturn(0);
3125c6c1daeSBarry Smith }
3135c6c1daeSBarry Smith 
3145c6c1daeSBarry Smith /*@
3155c6c1daeSBarry Smith     PetscViewerASCIIAddTab - Add to the number of times an ASCII viewer tabs before printing
3165c6c1daeSBarry Smith 
3175c6c1daeSBarry Smith     Not Collective, but only first processor in set has any effect
3185c6c1daeSBarry Smith 
3195c6c1daeSBarry Smith     Input Parameters:
3201575c14dSBarry Smith +    viewer - obtained with PetscViewerASCIIOpen()
3215c6c1daeSBarry Smith -    tabs - number of tabs
3225c6c1daeSBarry Smith 
3235c6c1daeSBarry Smith     Level: developer
3245c6c1daeSBarry Smith 
3255c6c1daeSBarry Smith     Fortran Note:
3265c6c1daeSBarry Smith     This routine is not supported in Fortran.
3275c6c1daeSBarry Smith 
3285c6c1daeSBarry Smith   Concepts: PetscViewerASCII^formating
3295c6c1daeSBarry Smith   Concepts: tab^setting
3305c6c1daeSBarry Smith 
3315c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(),
3325c6c1daeSBarry Smith           PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(),
3335c6c1daeSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer(), PetscViewerASCIIPushTab()
3345c6c1daeSBarry Smith @*/
3355c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIAddTab(PetscViewer viewer,PetscInt tabs)
3365c6c1daeSBarry Smith {
3375c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
3385c6c1daeSBarry Smith   PetscBool         iascii;
3395c6c1daeSBarry Smith   PetscErrorCode    ierr;
3405c6c1daeSBarry Smith 
3415c6c1daeSBarry Smith   PetscFunctionBegin;
3425c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
3435c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
344a297a907SKarl Rupp   if (iascii) ascii->tab += tabs;
3455c6c1daeSBarry Smith   PetscFunctionReturn(0);
3465c6c1daeSBarry Smith }
3475c6c1daeSBarry Smith 
3485c6c1daeSBarry Smith /*@
3495c6c1daeSBarry Smith     PetscViewerASCIISubtractTab - Subtracts from the number of times an ASCII viewer tabs before printing
3505c6c1daeSBarry Smith 
3515c6c1daeSBarry Smith     Not Collective, but only first processor in set has any effect
3525c6c1daeSBarry Smith 
3535c6c1daeSBarry Smith     Input Parameters:
3541575c14dSBarry Smith +    viewer - obtained with PetscViewerASCIIOpen()
3555c6c1daeSBarry Smith -    tabs - number of tabs
3565c6c1daeSBarry Smith 
3575c6c1daeSBarry Smith     Level: developer
3585c6c1daeSBarry Smith 
3595c6c1daeSBarry Smith     Fortran Note:
3605c6c1daeSBarry Smith     This routine is not supported in Fortran.
3615c6c1daeSBarry Smith 
3625c6c1daeSBarry Smith   Concepts: PetscViewerASCII^formating
3635c6c1daeSBarry Smith   Concepts: tab^setting
3645c6c1daeSBarry Smith 
3655c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(),
3665c6c1daeSBarry Smith           PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(),
3675c6c1daeSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer(), PetscViewerASCIIPushTab()
3685c6c1daeSBarry Smith @*/
3695c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIISubtractTab(PetscViewer viewer,PetscInt tabs)
3705c6c1daeSBarry Smith {
3715c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
3725c6c1daeSBarry Smith   PetscBool         iascii;
3735c6c1daeSBarry Smith   PetscErrorCode    ierr;
3745c6c1daeSBarry Smith 
3755c6c1daeSBarry Smith   PetscFunctionBegin;
3765c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
3775c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
378a297a907SKarl Rupp   if (iascii) ascii->tab -= tabs;
3795c6c1daeSBarry Smith   PetscFunctionReturn(0);
3805c6c1daeSBarry Smith }
3815c6c1daeSBarry Smith 
3825c6c1daeSBarry Smith /*@C
3831575c14dSBarry Smith     PetscViewerASCIIPushSynchronized - Allows calls to PetscViewerASCIISynchronizedPrintf() for this viewer
3845c6c1daeSBarry Smith 
3855c6c1daeSBarry Smith     Collective on PetscViewer
3865c6c1daeSBarry Smith 
3875c6c1daeSBarry Smith     Input Parameters:
3881575c14dSBarry Smith .    viewer - obtained with PetscViewerASCIIOpen()
3895c6c1daeSBarry Smith 
3905c6c1daeSBarry Smith     Level: intermediate
3915c6c1daeSBarry Smith 
3925c6c1daeSBarry Smith   Concepts: PetscViewerASCII^formating
3935c6c1daeSBarry Smith   Concepts: tab^setting
3945c6c1daeSBarry Smith 
3951575c14dSBarry Smith .seealso: PetscViewerASCIIPopSynchronized(), PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(),
3965c6c1daeSBarry Smith           PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(),
3975c6c1daeSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer()
3985c6c1daeSBarry Smith @*/
3991575c14dSBarry Smith PetscErrorCode  PetscViewerASCIIPushSynchronized(PetscViewer viewer)
4005c6c1daeSBarry Smith {
4015c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
4025c6c1daeSBarry Smith   PetscBool         iascii;
4035c6c1daeSBarry Smith   PetscErrorCode    ierr;
4045c6c1daeSBarry Smith 
4055c6c1daeSBarry Smith   PetscFunctionBegin;
4065c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
407*e5afcf28SBarry Smith   if (ascii->sviewer) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_WRONGSTATE,"Cannot call with outstanding call to PetscViewerRestoreSubViewer()");
4085c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
4091575c14dSBarry Smith   if (iascii) ascii->allowsynchronized++;
4101575c14dSBarry Smith   PetscFunctionReturn(0);
4111575c14dSBarry Smith }
4121575c14dSBarry Smith 
4131575c14dSBarry Smith /*@C
4141575c14dSBarry Smith     PetscViewerASCIIPopSynchronized - Undoes most recent PetscViewerASCIIPushSynchronized() for this viewer
4151575c14dSBarry Smith 
4161575c14dSBarry Smith     Collective on PetscViewer
4171575c14dSBarry Smith 
4181575c14dSBarry Smith     Input Parameters:
4191575c14dSBarry Smith .    viewer - obtained with PetscViewerASCIIOpen()
4201575c14dSBarry Smith 
4211575c14dSBarry Smith     Level: intermediate
4221575c14dSBarry Smith 
4231575c14dSBarry Smith   Concepts: PetscViewerASCII^formating
4241575c14dSBarry Smith   Concepts: tab^setting
4251575c14dSBarry Smith 
4261575c14dSBarry Smith .seealso: PetscViewerASCIIPushSynchronized(), PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(),
4271575c14dSBarry Smith           PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(),
4281575c14dSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer()
4291575c14dSBarry Smith @*/
4301575c14dSBarry Smith PetscErrorCode  PetscViewerASCIIPopSynchronized(PetscViewer viewer)
4311575c14dSBarry Smith {
4321575c14dSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
4331575c14dSBarry Smith   PetscBool         iascii;
4341575c14dSBarry Smith   PetscErrorCode    ierr;
4351575c14dSBarry Smith 
4361575c14dSBarry Smith   PetscFunctionBegin;
4371575c14dSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
438*e5afcf28SBarry Smith   if (ascii->sviewer) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_WRONGSTATE,"Cannot call with outstanding call to PetscViewerRestoreSubViewer()");
4391575c14dSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
4401575c14dSBarry Smith   if (iascii) {
4411575c14dSBarry Smith     ascii->allowsynchronized--;
4421575c14dSBarry Smith     if (ascii->allowsynchronized < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Called more times than PetscViewerASCIIPushSynchronized()");
4431575c14dSBarry Smith   }
4445c6c1daeSBarry Smith   PetscFunctionReturn(0);
4455c6c1daeSBarry Smith }
4465c6c1daeSBarry Smith 
4471c297824SMatthew G. Knepley /*@C
4485c6c1daeSBarry Smith     PetscViewerASCIIPushTab - Adds one more tab to the amount that PetscViewerASCIIPrintf()
4495c6c1daeSBarry Smith      lines are tabbed.
4505c6c1daeSBarry Smith 
4515c6c1daeSBarry Smith     Not Collective, but only first processor in set has any effect
4525c6c1daeSBarry Smith 
4535c6c1daeSBarry Smith     Input Parameters:
4541575c14dSBarry Smith .    viewer - obtained with PetscViewerASCIIOpen()
4555c6c1daeSBarry Smith 
4565c6c1daeSBarry Smith     Level: developer
4575c6c1daeSBarry Smith 
4585c6c1daeSBarry Smith     Fortran Note:
4595c6c1daeSBarry Smith     This routine is not supported in Fortran.
4605c6c1daeSBarry Smith 
4615c6c1daeSBarry Smith   Concepts: PetscViewerASCII^formating
4625c6c1daeSBarry Smith   Concepts: tab^setting
4635c6c1daeSBarry Smith 
4645c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(),
4655c6c1daeSBarry Smith           PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(),
4665c6c1daeSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer()
4675c6c1daeSBarry Smith @*/
4685c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIPushTab(PetscViewer viewer)
4695c6c1daeSBarry Smith {
4705c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
4715c6c1daeSBarry Smith   PetscBool         iascii;
4725c6c1daeSBarry Smith   PetscErrorCode    ierr;
4735c6c1daeSBarry Smith 
4745c6c1daeSBarry Smith   PetscFunctionBegin;
4755c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
4765c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
477a297a907SKarl Rupp   if (iascii) ascii->tab++;
4785c6c1daeSBarry Smith   PetscFunctionReturn(0);
4795c6c1daeSBarry Smith }
4805c6c1daeSBarry Smith 
4811c297824SMatthew G. Knepley /*@C
4825c6c1daeSBarry Smith     PetscViewerASCIIPopTab - Removes one tab from the amount that PetscViewerASCIIPrintf()
4835c6c1daeSBarry Smith      lines are tabbed.
4845c6c1daeSBarry Smith 
4855c6c1daeSBarry Smith     Not Collective, but only first processor in set has any effect
4865c6c1daeSBarry Smith 
4875c6c1daeSBarry Smith     Input Parameters:
4881575c14dSBarry Smith .    viewer - obtained with PetscViewerASCIIOpen()
4895c6c1daeSBarry Smith 
4905c6c1daeSBarry Smith     Level: developer
4915c6c1daeSBarry Smith 
4925c6c1daeSBarry Smith     Fortran Note:
4935c6c1daeSBarry Smith     This routine is not supported in Fortran.
4945c6c1daeSBarry Smith 
4955c6c1daeSBarry Smith   Concepts: PetscViewerASCII^formating
4965c6c1daeSBarry Smith   Concepts: tab^setting
4975c6c1daeSBarry Smith 
4985c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(),
4995c6c1daeSBarry Smith           PetscViewerASCIIPushTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(),
5005c6c1daeSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer()
5015c6c1daeSBarry Smith @*/
5025c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIPopTab(PetscViewer viewer)
5035c6c1daeSBarry Smith {
5045c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
5055c6c1daeSBarry Smith   PetscErrorCode    ierr;
5065c6c1daeSBarry Smith   PetscBool         iascii;
5075c6c1daeSBarry Smith 
5085c6c1daeSBarry Smith   PetscFunctionBegin;
5095c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
5105c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
5115c6c1daeSBarry Smith   if (iascii) {
5125c6c1daeSBarry Smith     if (ascii->tab <= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"More tabs popped than pushed");
5135c6c1daeSBarry Smith     ascii->tab--;
5145c6c1daeSBarry Smith   }
5155c6c1daeSBarry Smith   PetscFunctionReturn(0);
5165c6c1daeSBarry Smith }
5175c6c1daeSBarry Smith 
5185c6c1daeSBarry Smith /*@
5195c6c1daeSBarry Smith     PetscViewerASCIIUseTabs - Turns on or off the use of tabs with the ASCII PetscViewer
5205c6c1daeSBarry Smith 
5215c6c1daeSBarry Smith     Not Collective, but only first processor in set has any effect
5225c6c1daeSBarry Smith 
5235c6c1daeSBarry Smith     Input Parameters:
5241575c14dSBarry Smith +    viewer - obtained with PetscViewerASCIIOpen()
5255c6c1daeSBarry Smith -    flg - PETSC_TRUE or PETSC_FALSE
5265c6c1daeSBarry Smith 
5275c6c1daeSBarry Smith     Level: developer
5285c6c1daeSBarry Smith 
5295c6c1daeSBarry Smith     Fortran Note:
5305c6c1daeSBarry Smith     This routine is not supported in Fortran.
5315c6c1daeSBarry Smith 
5325c6c1daeSBarry Smith   Concepts: PetscViewerASCII^formating
5335c6c1daeSBarry Smith   Concepts: tab^setting
5345c6c1daeSBarry Smith 
5355c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(),
5365c6c1daeSBarry Smith           PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIPushTab(), PetscViewerASCIIOpen(),
5375c6c1daeSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer()
5385c6c1daeSBarry Smith @*/
5395c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIUseTabs(PetscViewer viewer,PetscBool flg)
5405c6c1daeSBarry Smith {
5415c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
5425c6c1daeSBarry Smith   PetscBool         iascii;
5435c6c1daeSBarry Smith   PetscErrorCode    ierr;
5445c6c1daeSBarry Smith 
5455c6c1daeSBarry Smith   PetscFunctionBegin;
5465c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
5475c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
5485c6c1daeSBarry Smith   if (iascii) {
549a297a907SKarl Rupp     if (flg) ascii->tab = ascii->tab_store;
550a297a907SKarl Rupp     else {
5515c6c1daeSBarry Smith       ascii->tab_store = ascii->tab;
5525c6c1daeSBarry Smith       ascii->tab       = 0;
5535c6c1daeSBarry Smith     }
5545c6c1daeSBarry Smith   }
5555c6c1daeSBarry Smith   PetscFunctionReturn(0);
5565c6c1daeSBarry Smith }
5575c6c1daeSBarry Smith 
5585c6c1daeSBarry Smith /* ----------------------------------------------------------------------- */
5595c6c1daeSBarry Smith 
5605c6c1daeSBarry Smith 
5615c6c1daeSBarry Smith /*@C
5625c6c1daeSBarry Smith     PetscViewerASCIIPrintf - Prints to a file, only from the first
5635c6c1daeSBarry Smith     processor in the PetscViewer
5645c6c1daeSBarry Smith 
5655c6c1daeSBarry Smith     Not Collective, but only first processor in set has any effect
5665c6c1daeSBarry Smith 
5675c6c1daeSBarry Smith     Input Parameters:
5681575c14dSBarry Smith +    viewer - obtained with PetscViewerASCIIOpen()
5695c6c1daeSBarry Smith -    format - the usual printf() format string
5705c6c1daeSBarry Smith 
5715c6c1daeSBarry Smith     Level: developer
5725c6c1daeSBarry Smith 
5735c6c1daeSBarry Smith     Fortran Note:
5745c6c1daeSBarry Smith     The call sequence is PetscViewerASCIIPrintf(PetscViewer, character(*), int ierr) from Fortran.
5755c6c1daeSBarry Smith     That is, you can only pass a single character string from Fortran.
5765c6c1daeSBarry Smith 
5775c6c1daeSBarry Smith   Concepts: PetscViewerASCII^printing
5785c6c1daeSBarry Smith   Concepts: printing^to file
5795c6c1daeSBarry Smith   Concepts: printf
5805c6c1daeSBarry Smith 
5815c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIOpen(),
5825c6c1daeSBarry Smith           PetscViewerASCIIPushTab(), PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(),
5831575c14dSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer(), PetscViewerASCIIPushSynchronized()
5845c6c1daeSBarry Smith @*/
5855c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIPrintf(PetscViewer viewer,const char format[],...)
5865c6c1daeSBarry Smith {
5875c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
5885c6c1daeSBarry Smith   PetscMPIInt       rank;
589dd2fa690SBarry Smith   PetscInt          tab,intab = ascii->tab;
5905c6c1daeSBarry Smith   PetscErrorCode    ierr;
5915c6c1daeSBarry Smith   FILE              *fd = ascii->fd;
5923f08860eSBarry Smith   PetscBool         iascii;
5935c6c1daeSBarry Smith   int               err;
5945c6c1daeSBarry Smith 
5955c6c1daeSBarry Smith   PetscFunctionBegin;
5965c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
597*e5afcf28SBarry Smith   if (ascii->sviewer) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_WRONGSTATE,"Cannot call with outstanding call to PetscViewerRestoreSubViewer()");
5985c6c1daeSBarry Smith   PetscValidCharPointer(format,2);
5995c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
6005c6c1daeSBarry Smith   if (!iascii) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Not ASCII PetscViewer");
601ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
602559f443fSBarry Smith   if (rank) PetscFunctionReturn(0);
6033f08860eSBarry Smith 
6043f08860eSBarry Smith   if (ascii->bviewer) { /* pass string up to parent viewer */
6053f08860eSBarry Smith     char        *string;
6063f08860eSBarry Smith     va_list     Argp;
6073f08860eSBarry Smith     size_t      fullLength;
6083f08860eSBarry Smith 
6093f08860eSBarry Smith     ierr = PetscCalloc1(QUEUESTRINGSIZE, &string);CHKERRQ(ierr);
6103f08860eSBarry Smith     va_start(Argp,format);
6111575c14dSBarry Smith     ierr = PetscVSNPrintf(string,QUEUESTRINGSIZE,format,&fullLength,Argp);CHKERRQ(ierr);
6123f08860eSBarry Smith     va_end(Argp);
613559f443fSBarry Smith     ierr = PetscViewerASCIISynchronizedPrintf(viewer,"%s",string);CHKERRQ(ierr);
6143f08860eSBarry Smith     ierr = PetscFree(string);CHKERRQ(ierr);
6153f08860eSBarry Smith   } else { /* write directly to file */
6165c6c1daeSBarry Smith     va_list Argp;
617559f443fSBarry Smith     /* flush my own messages that I may have queued up */
618559f443fSBarry Smith     PrintfQueue next = ascii->petsc_printfqueuebase,previous;
619559f443fSBarry Smith     PetscInt    i;
620559f443fSBarry Smith     for (i=0; i<ascii->petsc_printfqueuelength; i++) {
621559f443fSBarry Smith       ierr = PetscFPrintf(PETSC_COMM_SELF,fd,"%s",next->string);CHKERRQ(ierr);
622559f443fSBarry Smith       previous = next;
623559f443fSBarry Smith       next     = next->next;
624559f443fSBarry Smith       ierr     = PetscFree(previous->string);CHKERRQ(ierr);
625559f443fSBarry Smith       ierr     = PetscFree(previous);CHKERRQ(ierr);
626559f443fSBarry Smith     }
627559f443fSBarry Smith     ascii->petsc_printfqueue       = 0;
628559f443fSBarry Smith     ascii->petsc_printfqueuelength = 0;
629dd2fa690SBarry Smith     tab = intab;
630a297a907SKarl Rupp     while (tab--) {
631a297a907SKarl Rupp       ierr = PetscFPrintf(PETSC_COMM_SELF,fd,"  ");CHKERRQ(ierr);
632a297a907SKarl Rupp     }
6335c6c1daeSBarry Smith 
6345c6c1daeSBarry Smith     va_start(Argp,format);
6355c6c1daeSBarry Smith     ierr = (*PetscVFPrintf)(fd,format,Argp);CHKERRQ(ierr);
6365c6c1daeSBarry Smith     err  = fflush(fd);
6375c6c1daeSBarry Smith     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
6385c6c1daeSBarry Smith     if (petsc_history) {
6395c6c1daeSBarry Smith       va_start(Argp,format);
640dd2fa690SBarry Smith       tab = intab;
641a297a907SKarl Rupp       while (tab--) {
642706d7a88SBarry Smith         ierr = PetscFPrintf(PETSC_COMM_SELF,petsc_history,"  ");CHKERRQ(ierr);
643a297a907SKarl Rupp       }
6445c6c1daeSBarry Smith       ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr);
6455c6c1daeSBarry Smith       err  = fflush(petsc_history);
6465c6c1daeSBarry Smith       if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
6475c6c1daeSBarry Smith     }
6485c6c1daeSBarry Smith     va_end(Argp);
6495c6c1daeSBarry Smith   }
6505c6c1daeSBarry Smith   PetscFunctionReturn(0);
6515c6c1daeSBarry Smith }
6525c6c1daeSBarry Smith 
6535c6c1daeSBarry Smith /*@C
6545c6c1daeSBarry Smith      PetscViewerFileSetName - Sets the name of the file the PetscViewer uses.
6555c6c1daeSBarry Smith 
6565c6c1daeSBarry Smith     Collective on PetscViewer
6575c6c1daeSBarry Smith 
6585c6c1daeSBarry Smith   Input Parameters:
6595c6c1daeSBarry Smith +  viewer - the PetscViewer; either ASCII or binary
6605c6c1daeSBarry Smith -  name - the name of the file it should use
6615c6c1daeSBarry Smith 
6625c6c1daeSBarry Smith     Level: advanced
6635c6c1daeSBarry Smith 
6645c6c1daeSBarry Smith .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerASCIIOpen(), PetscViewerBinaryOpen(), PetscViewerDestroy(),
6655c6c1daeSBarry Smith           PetscViewerASCIIGetPointer(), PetscViewerASCIIPrintf(), PetscViewerASCIISynchronizedPrintf()
6665c6c1daeSBarry Smith 
6675c6c1daeSBarry Smith @*/
6685c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileSetName(PetscViewer viewer,const char name[])
6695c6c1daeSBarry Smith {
6705c6c1daeSBarry Smith   PetscErrorCode ierr;
6712fbecc10SBarry Smith   char           b[PETSC_MAX_PATH_LEN];
6725c6c1daeSBarry Smith 
6735c6c1daeSBarry Smith   PetscFunctionBegin;
6745c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
6755c6c1daeSBarry Smith   PetscValidCharPointer(name,2);
6762fbecc10SBarry Smith   ierr = PetscStrreplace(PetscObjectComm((PetscObject)viewer),name,b,sizeof(b));CHKERRQ(ierr);
6772fbecc10SBarry Smith   ierr = PetscTryMethod(viewer,"PetscViewerFileSetName_C",(PetscViewer,const char[]),(viewer,b));CHKERRQ(ierr);
6785c6c1daeSBarry Smith   PetscFunctionReturn(0);
6795c6c1daeSBarry Smith }
6805c6c1daeSBarry Smith 
6815c6c1daeSBarry Smith /*@C
6825c6c1daeSBarry Smith      PetscViewerFileGetName - Gets the name of the file the PetscViewer uses.
6835c6c1daeSBarry Smith 
6845c6c1daeSBarry Smith     Not Collective
6855c6c1daeSBarry Smith 
6865c6c1daeSBarry Smith   Input Parameter:
6875c6c1daeSBarry Smith .  viewer - the PetscViewer; either ASCII or binary
6885c6c1daeSBarry Smith 
6895c6c1daeSBarry Smith   Output Parameter:
6905c6c1daeSBarry Smith .  name - the name of the file it is using
6915c6c1daeSBarry Smith 
6925c6c1daeSBarry Smith     Level: advanced
6935c6c1daeSBarry Smith 
6945c6c1daeSBarry Smith .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerASCIIOpen(), PetscViewerBinaryOpen(), PetscViewerFileSetName()
6955c6c1daeSBarry Smith 
6965c6c1daeSBarry Smith @*/
6975c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileGetName(PetscViewer viewer,const char **name)
6985c6c1daeSBarry Smith {
6995c6c1daeSBarry Smith   PetscErrorCode ierr;
7005c6c1daeSBarry Smith 
7015c6c1daeSBarry Smith   PetscFunctionBegin;
7025c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
7036e05b1faSLisandro Dalcin   PetscValidPointer(name,2);
704163d334eSBarry Smith   ierr = PetscUseMethod(viewer,"PetscViewerFileGetName_C",(PetscViewer,const char**),(viewer,name));CHKERRQ(ierr);
7055c6c1daeSBarry Smith   PetscFunctionReturn(0);
7065c6c1daeSBarry Smith }
7075c6c1daeSBarry Smith 
7085c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileGetName_ASCII(PetscViewer viewer,const char **name)
7095c6c1daeSBarry Smith {
7105c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
7115c6c1daeSBarry Smith 
7125c6c1daeSBarry Smith   PetscFunctionBegin;
7135c6c1daeSBarry Smith   *name = vascii->filename;
7145c6c1daeSBarry Smith   PetscFunctionReturn(0);
7155c6c1daeSBarry Smith }
7165c6c1daeSBarry Smith 
7175c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileSetName_ASCII(PetscViewer viewer,const char name[])
7185c6c1daeSBarry Smith {
7195c6c1daeSBarry Smith   PetscErrorCode    ierr;
7205c6c1daeSBarry Smith   size_t            len;
7215c6c1daeSBarry Smith   char              fname[PETSC_MAX_PATH_LEN],*gz;
7225c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
7235c6c1daeSBarry Smith   PetscBool         isstderr,isstdout;
7245c6c1daeSBarry Smith   PetscMPIInt       rank;
7255c6c1daeSBarry Smith 
7265c6c1daeSBarry Smith   PetscFunctionBegin;
7275c6c1daeSBarry Smith   ierr = PetscViewerFileClose_ASCII(viewer);CHKERRQ(ierr);
7285c6c1daeSBarry Smith   if (!name) PetscFunctionReturn(0);
7295c6c1daeSBarry Smith   ierr = PetscStrallocpy(name,&vascii->filename);CHKERRQ(ierr);
7305c6c1daeSBarry Smith 
7315c6c1daeSBarry Smith   /* Is this file to be compressed */
7325c6c1daeSBarry Smith   vascii->storecompressed = PETSC_FALSE;
733a297a907SKarl Rupp 
7345c6c1daeSBarry Smith   ierr = PetscStrstr(vascii->filename,".gz",&gz);CHKERRQ(ierr);
7355c6c1daeSBarry Smith   if (gz) {
7365c6c1daeSBarry Smith     ierr = PetscStrlen(gz,&len);CHKERRQ(ierr);
7375c6c1daeSBarry Smith     if (len == 3) {
73808fb59bfSBarry 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");
7395c6c1daeSBarry Smith       *gz = 0;
7405c6c1daeSBarry Smith       vascii->storecompressed = PETSC_TRUE;
7415c6c1daeSBarry Smith     }
7425c6c1daeSBarry Smith   }
743ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
7445c6c1daeSBarry Smith   if (!rank) {
7455c6c1daeSBarry Smith     ierr = PetscStrcmp(name,"stderr",&isstderr);CHKERRQ(ierr);
7465c6c1daeSBarry Smith     ierr = PetscStrcmp(name,"stdout",&isstdout);CHKERRQ(ierr);
7475c6c1daeSBarry Smith     /* empty filename means stdout */
7485c6c1daeSBarry Smith     if (name[0] == 0)  isstdout = PETSC_TRUE;
7495c6c1daeSBarry Smith     if (isstderr)      vascii->fd = PETSC_STDERR;
7505c6c1daeSBarry Smith     else if (isstdout) vascii->fd = PETSC_STDOUT;
7515c6c1daeSBarry Smith     else {
7525c6c1daeSBarry Smith 
7535c6c1daeSBarry Smith 
7545c6c1daeSBarry Smith       ierr = PetscFixFilename(name,fname);CHKERRQ(ierr);
7555c6c1daeSBarry Smith       switch (vascii->mode) {
7565c6c1daeSBarry Smith       case FILE_MODE_READ:
7575c6c1daeSBarry Smith         vascii->fd = fopen(fname,"r");
7585c6c1daeSBarry Smith         break;
7595c6c1daeSBarry Smith       case FILE_MODE_WRITE:
7605c6c1daeSBarry Smith         vascii->fd = fopen(fname,"w");
7615c6c1daeSBarry Smith         break;
7625c6c1daeSBarry Smith       case FILE_MODE_APPEND:
7635c6c1daeSBarry Smith         vascii->fd = fopen(fname,"a");
7645c6c1daeSBarry Smith         break;
7655c6c1daeSBarry Smith       case FILE_MODE_UPDATE:
7665c6c1daeSBarry Smith         vascii->fd = fopen(fname,"r+");
767a297a907SKarl Rupp         if (!vascii->fd) vascii->fd = fopen(fname,"w+");
7685c6c1daeSBarry Smith         break;
7695c6c1daeSBarry Smith       case FILE_MODE_APPEND_UPDATE:
7705c6c1daeSBarry Smith         /* I really want a file which is opened at the end for updating,
7715c6c1daeSBarry Smith            not a+, which opens at the beginning, but makes writes at the end.
7725c6c1daeSBarry Smith         */
7735c6c1daeSBarry Smith         vascii->fd = fopen(fname,"r+");
774a297a907SKarl Rupp         if (!vascii->fd) vascii->fd = fopen(fname,"w+");
775a297a907SKarl Rupp         else {
7765c6c1daeSBarry Smith           ierr     = fseek(vascii->fd, 0, SEEK_END);CHKERRQ(ierr);
7775c6c1daeSBarry Smith         }
7785c6c1daeSBarry Smith         break;
7795c6c1daeSBarry Smith       default:
7805c6c1daeSBarry Smith         SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Invalid file mode %d", vascii->mode);
7815c6c1daeSBarry Smith       }
7825c6c1daeSBarry Smith       if (!vascii->fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open PetscViewer file: %s",fname);
7835c6c1daeSBarry Smith     }
7845c6c1daeSBarry Smith   }
7855c6c1daeSBarry Smith #if defined(PETSC_USE_LOG)
7865c6c1daeSBarry Smith   PetscLogObjectState((PetscObject)viewer,"File: %s",name);
7875c6c1daeSBarry Smith #endif
7885c6c1daeSBarry Smith   PetscFunctionReturn(0);
7895c6c1daeSBarry Smith }
7905c6c1daeSBarry Smith 
7913f08860eSBarry Smith PetscErrorCode PetscViewerGetSubViewer_ASCII(PetscViewer viewer,MPI_Comm subcomm,PetscViewer *outviewer)
7925c6c1daeSBarry Smith {
7935c6c1daeSBarry Smith   PetscMPIInt       rank;
7945c6c1daeSBarry Smith   PetscErrorCode    ierr;
7955c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data,*ovascii;
7965c6c1daeSBarry Smith 
7975c6c1daeSBarry Smith   PetscFunctionBegin;
798*e5afcf28SBarry Smith   ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
7993f08860eSBarry Smith   if (vascii->sviewer) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"SubViewer already obtained from PetscViewer and not restored");
800*e5afcf28SBarry Smith   /*
801*e5afcf28SBarry Smith      The following line is a bug; but if it is removed the code won't work because it relies on this behavior. In particular
802*e5afcf28SBarry Smith      this line causes the synchronized flush to occur when the viewer is destroyed (since the count never gets to zero)
803*e5afcf28SBarry Smith      in some examples this displays information that otherwise would be lost
804*e5afcf28SBarry Smith   */
8051575c14dSBarry Smith   ierr         = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
8063f08860eSBarry Smith   ierr         = PetscViewerCreate(subcomm,outviewer);CHKERRQ(ierr);
8075c6c1daeSBarry Smith   ierr         = PetscViewerSetType(*outviewer,PETSCVIEWERASCII);CHKERRQ(ierr);
8081575c14dSBarry Smith   ierr         = PetscViewerASCIIPushSynchronized(*outviewer);CHKERRQ(ierr);
8095c6c1daeSBarry Smith   ovascii      = (PetscViewer_ASCII*)(*outviewer)->data;
8105c6c1daeSBarry Smith   ovascii->fd  = vascii->fd;
8115c6c1daeSBarry Smith   ovascii->tab = vascii->tab;
812ba5a0b41SBarry Smith   ovascii->closefile = PETSC_FALSE;
8135c6c1daeSBarry Smith 
8145c6c1daeSBarry Smith   vascii->sviewer = *outviewer;
8155c6c1daeSBarry Smith 
8165c6c1daeSBarry Smith   (*outviewer)->format  = viewer->format;
8175c6c1daeSBarry Smith 
818ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
8195c6c1daeSBarry Smith   ((PetscViewer_ASCII*)((*outviewer)->data))->bviewer = viewer;
8203f08860eSBarry Smith   (*outviewer)->ops->destroy = PetscViewerDestroy_ASCII_SubViewer;
8215c6c1daeSBarry Smith   PetscFunctionReturn(0);
8225c6c1daeSBarry Smith }
8235c6c1daeSBarry Smith 
8243f08860eSBarry Smith PetscErrorCode PetscViewerRestoreSubViewer_ASCII(PetscViewer viewer,MPI_Comm comm,PetscViewer *outviewer)
8255c6c1daeSBarry Smith {
8265c6c1daeSBarry Smith   PetscErrorCode    ierr;
8275c6c1daeSBarry Smith   PetscViewer_ASCII *ascii  = (PetscViewer_ASCII*)viewer->data;
8285c6c1daeSBarry Smith 
8295c6c1daeSBarry Smith   PetscFunctionBegin;
8301575c14dSBarry Smith   if (!ascii->sviewer) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"SubViewer never obtained from PetscViewer");
8311575c14dSBarry Smith   if (ascii->sviewer != *outviewer) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"This PetscViewer did not generate this SubViewer");
8325c6c1daeSBarry Smith 
833*e5afcf28SBarry Smith   ascii->sviewer             = NULL;
8345c6c1daeSBarry Smith   (*outviewer)->ops->destroy = PetscViewerDestroy_ASCII;
8355c6c1daeSBarry Smith   ierr                       = PetscViewerDestroy(outviewer);CHKERRQ(ierr);
836*e5afcf28SBarry Smith   ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
8375c6c1daeSBarry Smith   PetscFunctionReturn(0);
8385c6c1daeSBarry Smith }
8395c6c1daeSBarry Smith 
8402bf49c77SBarry Smith PetscErrorCode  PetscViewerView_ASCII(PetscViewer v,PetscViewer viewer)
8412bf49c77SBarry Smith {
8422bf49c77SBarry Smith   PetscErrorCode    ierr;
8432bf49c77SBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)v->data;
8442bf49c77SBarry Smith 
8452bf49c77SBarry Smith   PetscFunctionBegin;
8462bf49c77SBarry Smith   if (ascii->filename) {
8472bf49c77SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"Filename: %s\n",ascii->filename);CHKERRQ(ierr);
8482bf49c77SBarry Smith   }
8492bf49c77SBarry Smith   PetscFunctionReturn(0);
8502bf49c77SBarry Smith }
8512bf49c77SBarry Smith 
8528556b5ebSBarry Smith /*MC
8538556b5ebSBarry Smith    PETSCVIEWERASCII - A viewer that prints to stdout or an ASCII file
8548556b5ebSBarry Smith 
8558556b5ebSBarry Smith 
8568556b5ebSBarry Smith .seealso:  PETSC_VIEWER_STDOUT_(),PETSC_VIEWER_STDOUT_SELF, PETSC_VIEWER_STDOUT_WORLD,PetscViewerCreate(), PetscViewerASCIIOpen(),
8578556b5ebSBarry Smith            PetscViewerMatlabOpen(), VecView(), DMView(), PetscViewerMatlabPutArray(), PETSCVIEWERBINARY, PETSCVIEWERMATLAB,
8588556b5ebSBarry Smith            PetscViewerFileSetName(), PetscViewerFileSetMode(), PetscViewerFormat, PetscViewerType, PetscViewerSetType()
8598556b5ebSBarry Smith 
8601b266c99SBarry Smith   Level: beginner
8611b266c99SBarry Smith 
8628556b5ebSBarry Smith M*/
8638cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscViewerCreate_ASCII(PetscViewer viewer)
8645c6c1daeSBarry Smith {
8655c6c1daeSBarry Smith   PetscViewer_ASCII *vascii;
8665c6c1daeSBarry Smith   PetscErrorCode    ierr;
8675c6c1daeSBarry Smith 
8685c6c1daeSBarry Smith   PetscFunctionBegin;
869b00a9115SJed Brown   ierr         = PetscNewLog(viewer,&vascii);CHKERRQ(ierr);
8705c6c1daeSBarry Smith   viewer->data = (void*)vascii;
8715c6c1daeSBarry Smith 
8725c6c1daeSBarry Smith   viewer->ops->destroy          = PetscViewerDestroy_ASCII;
8735c6c1daeSBarry Smith   viewer->ops->flush            = PetscViewerFlush_ASCII;
874559f443fSBarry Smith   viewer->ops->getsubviewer     = PetscViewerGetSubViewer_ASCII;
875559f443fSBarry Smith   viewer->ops->restoresubviewer = PetscViewerRestoreSubViewer_ASCII;
8762bf49c77SBarry Smith   viewer->ops->view             = PetscViewerView_ASCII;
8771d641e7bSMichael Lange   viewer->ops->read             = PetscViewerASCIIRead;
8785c6c1daeSBarry Smith 
8795c6c1daeSBarry Smith   /* defaults to stdout unless set with PetscViewerFileSetName() */
8805c6c1daeSBarry Smith   vascii->fd        = PETSC_STDOUT;
8815c6c1daeSBarry Smith   vascii->mode      = FILE_MODE_WRITE;
8825c6c1daeSBarry Smith   vascii->bviewer   = 0;
883dd2fa690SBarry Smith   vascii->subviewer = 0;
8845c6c1daeSBarry Smith   vascii->sviewer   = 0;
8855c6c1daeSBarry Smith   vascii->tab       = 0;
8865c6c1daeSBarry Smith   vascii->tab_store = 0;
8875c6c1daeSBarry Smith   vascii->filename  = 0;
8885c6c1daeSBarry Smith   vascii->closefile = PETSC_TRUE;
8895c6c1daeSBarry Smith 
890bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetName_C",PetscViewerFileSetName_ASCII);CHKERRQ(ierr);
891bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileGetName_C",PetscViewerFileGetName_ASCII);CHKERRQ(ierr);
892bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileGetMode_C",PetscViewerFileGetMode_ASCII);CHKERRQ(ierr);
893bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetMode_C",PetscViewerFileSetMode_ASCII);CHKERRQ(ierr);
8945c6c1daeSBarry Smith   PetscFunctionReturn(0);
8955c6c1daeSBarry Smith }
8965c6c1daeSBarry Smith 
8975c6c1daeSBarry Smith /*@C
8985c6c1daeSBarry Smith     PetscViewerASCIISynchronizedPrintf - Prints synchronized output to the specified file from
8995c6c1daeSBarry Smith     several processors.  Output of the first processor is followed by that of the
9005c6c1daeSBarry Smith     second, etc.
9015c6c1daeSBarry Smith 
9025c6c1daeSBarry Smith     Not Collective, must call collective PetscViewerFlush() to get the results out
9035c6c1daeSBarry Smith 
9045c6c1daeSBarry Smith     Input Parameters:
9055c6c1daeSBarry Smith +   viewer - the ASCII PetscViewer
9065c6c1daeSBarry Smith -   format - the usual printf() format string
9075c6c1daeSBarry Smith 
9085c6c1daeSBarry Smith     Level: intermediate
9095c6c1daeSBarry Smith 
91095452b02SPatrick Sanan     Notes:
91195452b02SPatrick Sanan     You must have previously called PetscViewerASCIISynchronizeAllow() to allow this routine to be called.
9125c6c1daeSBarry Smith 
9135c6c1daeSBarry Smith     Fortran Note:
9145c6c1daeSBarry Smith       Can only print a single character* string
9155c6c1daeSBarry Smith 
9165c6c1daeSBarry Smith .seealso: PetscSynchronizedPrintf(), PetscSynchronizedFlush(), PetscFPrintf(),
9175c6c1daeSBarry Smith           PetscFOpen(), PetscViewerFlush(), PetscViewerASCIIGetPointer(), PetscViewerDestroy(), PetscViewerASCIIOpen(),
9181575c14dSBarry Smith           PetscViewerASCIIPrintf(), PetscViewerASCIIPushSynchronized()
9195c6c1daeSBarry Smith 
9205c6c1daeSBarry Smith @*/
9215c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIISynchronizedPrintf(PetscViewer viewer,const char format[],...)
9225c6c1daeSBarry Smith {
9235c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
9245c6c1daeSBarry Smith   PetscErrorCode    ierr;
9253f08860eSBarry Smith   PetscMPIInt       rank;
9265c6c1daeSBarry Smith   PetscInt          tab = vascii->tab;
9275c6c1daeSBarry Smith   MPI_Comm          comm;
9285c6c1daeSBarry Smith   FILE              *fp;
929559f443fSBarry Smith   PetscBool         iascii,hasbviewer = PETSC_FALSE;
9305c6c1daeSBarry Smith   int               err;
9315c6c1daeSBarry Smith 
9325c6c1daeSBarry Smith   PetscFunctionBegin;
9335c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
9345c6c1daeSBarry Smith   PetscValidCharPointer(format,2);
9355c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
9365c6c1daeSBarry Smith   if (!iascii) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Not ASCII PetscViewer");
9371575c14dSBarry Smith   if (!vascii->allowsynchronized) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"First call PetscViewerASCIIPushSynchronized() to allow this call");
9385c6c1daeSBarry Smith 
939ce94432eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
9405c6c1daeSBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
9415c6c1daeSBarry Smith 
942559f443fSBarry Smith   if (vascii->bviewer) {
943559f443fSBarry Smith     hasbviewer = PETSC_TRUE;
944559f443fSBarry Smith     if (!rank) {
945559f443fSBarry Smith       vascii = (PetscViewer_ASCII*)vascii->bviewer->data;
946559f443fSBarry Smith       ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
947559f443fSBarry Smith       ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
948559f443fSBarry Smith     }
949559f443fSBarry Smith   }
9503f08860eSBarry Smith 
951559f443fSBarry Smith   fp   = vascii->fd;
952559f443fSBarry Smith 
953559f443fSBarry Smith   if (!rank && !hasbviewer) {   /* First processor prints immediately to fp */
9545c6c1daeSBarry Smith     va_list Argp;
955559f443fSBarry Smith     /* flush my own messages that I may have queued up */
956559f443fSBarry Smith     PrintfQueue next = vascii->petsc_printfqueuebase,previous;
957559f443fSBarry Smith     PetscInt    i;
958559f443fSBarry Smith     for (i=0; i<vascii->petsc_printfqueuelength; i++) {
959559f443fSBarry Smith       ierr = PetscFPrintf(comm,fp,"%s",next->string);CHKERRQ(ierr);
960559f443fSBarry Smith       previous = next;
961559f443fSBarry Smith       next     = next->next;
962559f443fSBarry Smith       ierr     = PetscFree(previous->string);CHKERRQ(ierr);
963559f443fSBarry Smith       ierr     = PetscFree(previous);CHKERRQ(ierr);
964559f443fSBarry Smith     }
965559f443fSBarry Smith     vascii->petsc_printfqueue       = 0;
966559f443fSBarry Smith     vascii->petsc_printfqueuelength = 0;
9675c6c1daeSBarry Smith 
968a297a907SKarl Rupp     while (tab--) {
969a297a907SKarl Rupp       ierr = PetscFPrintf(PETSC_COMM_SELF,fp,"  ");CHKERRQ(ierr);
970a297a907SKarl Rupp     }
9715c6c1daeSBarry Smith 
9725c6c1daeSBarry Smith     va_start(Argp,format);
9735c6c1daeSBarry Smith     ierr = (*PetscVFPrintf)(fp,format,Argp);CHKERRQ(ierr);
9745c6c1daeSBarry Smith     err  = fflush(fp);
9755c6c1daeSBarry Smith     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
9765c6c1daeSBarry Smith     if (petsc_history) {
9775c6c1daeSBarry Smith       va_start(Argp,format);
9785c6c1daeSBarry Smith       ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr);
9795c6c1daeSBarry Smith       err  = fflush(petsc_history);
9805c6c1daeSBarry Smith       if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
9815c6c1daeSBarry Smith     }
9825c6c1daeSBarry Smith     va_end(Argp);
983559f443fSBarry Smith   } else { /* other processors add to queue */
9845c6c1daeSBarry Smith     char        *string;
9855c6c1daeSBarry Smith     va_list     Argp;
9865c6c1daeSBarry Smith     size_t      fullLength;
9875c6c1daeSBarry Smith     PrintfQueue next;
9885c6c1daeSBarry Smith 
989b00a9115SJed Brown     ierr = PetscNew(&next);CHKERRQ(ierr);
990559f443fSBarry Smith     if (vascii->petsc_printfqueue) {
991559f443fSBarry Smith       vascii->petsc_printfqueue->next = next;
992559f443fSBarry Smith       vascii->petsc_printfqueue       = next;
993a297a907SKarl Rupp     } else {
994559f443fSBarry Smith       vascii->petsc_printfqueuebase = vascii->petsc_printfqueue = next;
995a297a907SKarl Rupp     }
996559f443fSBarry Smith     vascii->petsc_printfqueuelength++;
9975c6c1daeSBarry Smith     next->size = QUEUESTRINGSIZE;
998785e854fSJed Brown     ierr       = PetscMalloc1(next->size, &next->string);CHKERRQ(ierr);
9995c6c1daeSBarry Smith     ierr       = PetscMemzero(next->string,next->size);CHKERRQ(ierr);
10005c6c1daeSBarry Smith     string     = next->string;
10015c6c1daeSBarry Smith     tab       *= 2;
1002a297a907SKarl Rupp     while (tab--) {
1003a297a907SKarl Rupp       *string++ = ' ';
1004a297a907SKarl Rupp     }
10055c6c1daeSBarry Smith     va_start(Argp,format);
100622d28d08SBarry Smith     ierr = PetscVSNPrintf(string,next->size-2*vascii->tab,format,&fullLength,Argp);CHKERRQ(ierr);
10075c6c1daeSBarry Smith     va_end(Argp);
1008cb500232SBarry Smith     if (fullLength > (size_t) (next->size-2*vascii->tab)) {
100914416c0eSBarry Smith       ierr       = PetscFree(next->string);CHKERRQ(ierr);
101014416c0eSBarry Smith       next->size = fullLength + 2*vascii->tab;
101114416c0eSBarry Smith       ierr       = PetscMalloc1(next->size, &next->string);CHKERRQ(ierr);
101214416c0eSBarry Smith       ierr       = PetscMemzero(next->string,next->size);CHKERRQ(ierr);
101314416c0eSBarry Smith       string     = next->string;
101414416c0eSBarry Smith       tab        = 2*vascii->tab;
101514416c0eSBarry Smith       while (tab--) {
101614416c0eSBarry Smith         *string++ = ' ';
101714416c0eSBarry Smith       }
101814416c0eSBarry Smith       va_start(Argp,format);
101914416c0eSBarry Smith       ierr = PetscVSNPrintf(string,next->size-2*vascii->tab,format,NULL,Argp);CHKERRQ(ierr);
102014416c0eSBarry Smith       va_end(Argp);
102114416c0eSBarry Smith     }
10225c6c1daeSBarry Smith   }
10235c6c1daeSBarry Smith   PetscFunctionReturn(0);
10245c6c1daeSBarry Smith }
10255c6c1daeSBarry Smith 
10262655f987SMichael Lange /*@C
1027f8859db6SBarry Smith    PetscViewerASCIIRead - Reads from a ASCII file
10282655f987SMichael Lange 
1029f8859db6SBarry Smith    Only process 0 in the PetscViewer may call this
10302655f987SMichael Lange 
10312655f987SMichael Lange    Input Parameters:
10322655f987SMichael Lange +  viewer - the ascii viewer
10332655f987SMichael Lange .  data - location to write the data
1034060da220SMatthew G. Knepley .  num - number of items of data to read
10352655f987SMichael Lange -  datatype - type of data to read
10362655f987SMichael Lange 
1037f8e4bde8SMatthew G. Knepley    Output Parameters:
1038060da220SMatthew G. Knepley .  count - number of items of data actually read, or NULL
1039f8e4bde8SMatthew G. Knepley 
10402655f987SMichael Lange    Level: beginner
10412655f987SMichael Lange 
10422655f987SMichael Lange    Concepts: ascii files
10432655f987SMichael Lange 
1044f8859db6SBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(), PetscViewerCreate(), PetscViewerFileSetMode(), PetscViewerFileSetName()
10452655f987SMichael Lange           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
104605119932SBarry Smith           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscViewerBinaryRead()
10472655f987SMichael Lange @*/
1048060da220SMatthew G. Knepley PetscErrorCode PetscViewerASCIIRead(PetscViewer viewer,void *data,PetscInt num,PetscInt *count,PetscDataType dtype)
10492655f987SMichael Lange {
10502655f987SMichael Lange   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
10512655f987SMichael Lange   FILE              *fd = vascii->fd;
10522655f987SMichael Lange   PetscInt           i;
10533b7fe8c3SMatthew G. Knepley   int                ret = 0;
1054f8859db6SBarry Smith   PetscMPIInt        rank;
1055f8859db6SBarry Smith   PetscErrorCode     ierr;
10562655f987SMichael Lange 
10572655f987SMichael Lange   PetscFunctionBegin;
10582655f987SMichael Lange   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
1059f8859db6SBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
1060f8859db6SBarry Smith   if (rank) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG,"Can only be called from process 0 in the PetscViewer");
1061060da220SMatthew G. Knepley   for (i=0; i<num; i++) {
1062f8e4bde8SMatthew G. Knepley     if (dtype == PETSC_CHAR)         ret = fscanf(fd, "%c",  &(((char*)data)[i]));
1063f8e4bde8SMatthew G. Knepley     else if (dtype == PETSC_STRING)  ret = fscanf(fd, "%s",  &(((char*)data)[i]));
1064a05e1a72SSatish Balay     else if (dtype == PETSC_INT)     ret = fscanf(fd, "%" PetscInt_FMT,  &(((PetscInt*)data)[i]));
1065f8e4bde8SMatthew G. Knepley     else if (dtype == PETSC_ENUM)    ret = fscanf(fd, "%d",  &(((int*)data)[i]));
10669e3e4c22SLisandro Dalcin     else if (dtype == PETSC_INT64)   ret = fscanf(fd, "%" PetscInt64_FMT,  &(((PetscInt64*)data)[i]));
1067972064b6SLisandro Dalcin     else if (dtype == PETSC_LONG)    ret = fscanf(fd, "%ld", &(((long*)data)[i]));
1068f8e4bde8SMatthew G. Knepley     else if (dtype == PETSC_FLOAT)   ret = fscanf(fd, "%f",  &(((float*)data)[i]));
1069f8e4bde8SMatthew G. Knepley     else if (dtype == PETSC_DOUBLE)  ret = fscanf(fd, "%lg", &(((double*)data)[i]));
1070a6e181c6SToby Isaac #if defined(PETSC_USE_REAL___FLOAT128)
1071fba955ccSBarry Smith     else if (dtype == PETSC___FLOAT128) {
1072fba955ccSBarry Smith       double tmp;
1073fba955ccSBarry Smith       ret = fscanf(fd, "%lg", &tmp);
1074a6e181c6SToby Isaac       ((__float128*)data)[i] = tmp;
1075a6e181c6SToby Isaac     }
1076fba955ccSBarry Smith #endif
1077f8e4bde8SMatthew G. Knepley     else {SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Data type %d not supported", (int) dtype);}
1078f8e4bde8SMatthew G. Knepley     if (!ret) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Conversion error for data type %d", (int) dtype);
1079f8e4bde8SMatthew G. Knepley     else if (ret < 0) break; /* Proxy for EOF, need to check for it in configure */
10802655f987SMichael Lange   }
1081060da220SMatthew G. Knepley   if (count) *count = i;
1082060da220SMatthew G. Knepley   else if (ret < 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Insufficient data, read only %D < %D items", i, num);
10832655f987SMichael Lange   PetscFunctionReturn(0);
10842655f987SMichael Lange }
10855c6c1daeSBarry Smith 
1086