xref: /petsc/src/sys/classes/viewer/impls/ascii/filev.c (revision 163d334ea1df412efbe58c2e8fce360a2afeaf87)
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 #undef __FUNCT__
75c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileClose_ASCII"
85c6c1daeSBarry Smith static PetscErrorCode PetscViewerFileClose_ASCII(PetscViewer viewer)
95c6c1daeSBarry Smith {
105c6c1daeSBarry Smith   PetscErrorCode    ierr;
115c6c1daeSBarry Smith   PetscMPIInt       rank;
125c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
135c6c1daeSBarry Smith   int               err;
145c6c1daeSBarry Smith 
155c6c1daeSBarry Smith   PetscFunctionBegin;
16ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
175c6c1daeSBarry Smith   if (!rank && vascii->fd != stderr && vascii->fd != PETSC_STDOUT) {
185c6c1daeSBarry Smith     if (vascii->fd && vascii->closefile) {
195c6c1daeSBarry Smith       err = fclose(vascii->fd);
205c6c1daeSBarry Smith       if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
215c6c1daeSBarry Smith     }
225c6c1daeSBarry Smith     if (vascii->storecompressed) {
235c6c1daeSBarry Smith       char par[PETSC_MAX_PATH_LEN],buf[PETSC_MAX_PATH_LEN];
245c6c1daeSBarry Smith       FILE *fp;
255c6c1daeSBarry Smith       ierr = PetscStrcpy(par,"gzip ");CHKERRQ(ierr);
265c6c1daeSBarry Smith       ierr = PetscStrcat(par,vascii->filename);CHKERRQ(ierr);
275c6c1daeSBarry Smith #if defined(PETSC_HAVE_POPEN)
280298fd71SBarry Smith       ierr = PetscPOpen(PETSC_COMM_SELF,NULL,par,"r",&fp);CHKERRQ(ierr);
29f23aa3ddSBarry Smith       if (fgets(buf,1024,fp)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error from compression command %s\n%s",par,buf);
300298fd71SBarry Smith       ierr = PetscPClose(PETSC_COMM_SELF,fp,NULL);CHKERRQ(ierr);
315c6c1daeSBarry Smith #else
325c6c1daeSBarry Smith       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
335c6c1daeSBarry Smith #endif
345c6c1daeSBarry Smith     }
355c6c1daeSBarry Smith   }
365c6c1daeSBarry Smith   ierr = PetscFree(vascii->filename);CHKERRQ(ierr);
375c6c1daeSBarry Smith   PetscFunctionReturn(0);
385c6c1daeSBarry Smith }
395c6c1daeSBarry Smith 
405c6c1daeSBarry Smith /* ----------------------------------------------------------------------*/
415c6c1daeSBarry Smith #undef __FUNCT__
425c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerDestroy_ASCII"
435c6c1daeSBarry Smith PetscErrorCode PetscViewerDestroy_ASCII(PetscViewer viewer)
445c6c1daeSBarry Smith {
455c6c1daeSBarry Smith   PetscErrorCode    ierr;
465c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
475c6c1daeSBarry Smith   PetscViewerLink   *vlink;
485c6c1daeSBarry Smith   PetscBool         flg;
495c6c1daeSBarry Smith 
505c6c1daeSBarry Smith   PetscFunctionBegin;
515c6c1daeSBarry Smith   if (vascii->sviewer) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"ASCII PetscViewer destroyed before restoring singleton or subcomm PetscViewer");
525c6c1daeSBarry Smith   ierr = PetscViewerFileClose_ASCII(viewer);CHKERRQ(ierr);
535c6c1daeSBarry Smith   ierr = PetscFree(vascii);CHKERRQ(ierr);
545c6c1daeSBarry Smith 
555c6c1daeSBarry Smith   /* remove the viewer from the list in the MPI Communicator */
565c6c1daeSBarry Smith   if (Petsc_Viewer_keyval == MPI_KEYVAL_INVALID) {
575c6c1daeSBarry Smith     ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,Petsc_DelViewer,&Petsc_Viewer_keyval,(void*)0);CHKERRQ(ierr);
585c6c1daeSBarry Smith   }
595c6c1daeSBarry Smith 
60ce94432eSBarry Smith   ierr = MPI_Attr_get(PetscObjectComm((PetscObject)viewer),Petsc_Viewer_keyval,(void**)&vlink,(PetscMPIInt*)&flg);CHKERRQ(ierr);
615c6c1daeSBarry Smith   if (flg) {
625c6c1daeSBarry Smith     if (vlink && vlink->viewer == viewer) {
63e5840a18SBarry Smith       if (vlink->next) {
64ce94432eSBarry Smith         ierr = MPI_Attr_put(PetscObjectComm((PetscObject)viewer),Petsc_Viewer_keyval,vlink->next);CHKERRQ(ierr);
65e5840a18SBarry Smith       } else {
66e5840a18SBarry Smith         ierr = MPI_Attr_delete(PetscObjectComm((PetscObject)viewer),Petsc_Viewer_keyval);CHKERRQ(ierr);
67e5840a18SBarry Smith       }
685c6c1daeSBarry Smith       ierr = PetscFree(vlink);CHKERRQ(ierr);
695c6c1daeSBarry Smith     } else {
705c6c1daeSBarry Smith       while (vlink && vlink->next) {
715c6c1daeSBarry Smith         if (vlink->next->viewer == viewer) {
725c6c1daeSBarry Smith           PetscViewerLink *nv = vlink->next;
735c6c1daeSBarry Smith           vlink->next = vlink->next->next;
745c6c1daeSBarry Smith           ierr = PetscFree(nv);CHKERRQ(ierr);
755c6c1daeSBarry Smith         }
765c6c1daeSBarry Smith         vlink = vlink->next;
775c6c1daeSBarry Smith       }
785c6c1daeSBarry Smith     }
795c6c1daeSBarry Smith   }
805c6c1daeSBarry Smith   PetscFunctionReturn(0);
815c6c1daeSBarry Smith }
825c6c1daeSBarry Smith 
835c6c1daeSBarry Smith #undef __FUNCT__
843f08860eSBarry Smith #define __FUNCT__ "PetscViewerDestroy_ASCII_SubViewer"
853f08860eSBarry Smith PetscErrorCode PetscViewerDestroy_ASCII_SubViewer(PetscViewer viewer)
865c6c1daeSBarry Smith {
875c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
885c6c1daeSBarry Smith   PetscErrorCode    ierr;
895fd66863SKarl Rupp 
905c6c1daeSBarry Smith   PetscFunctionBegin;
913f08860eSBarry Smith   ierr = PetscViewerRestoreSubViewer(vascii->bviewer,0,&viewer);CHKERRQ(ierr);
925c6c1daeSBarry Smith   PetscFunctionReturn(0);
935c6c1daeSBarry Smith }
945c6c1daeSBarry Smith 
955c6c1daeSBarry Smith #undef __FUNCT__
965c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFlush_ASCII"
975c6c1daeSBarry Smith PetscErrorCode PetscViewerFlush_ASCII(PetscViewer viewer)
985c6c1daeSBarry Smith {
995c6c1daeSBarry Smith   PetscErrorCode    ierr;
1005c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
1015c6c1daeSBarry Smith   int               err;
102559f443fSBarry Smith   MPI_Comm          comm;
103559f443fSBarry Smith   PetscMPIInt       rank,size;
104559f443fSBarry Smith   FILE              *fd = vascii->fd;
1055c6c1daeSBarry Smith 
1065c6c1daeSBarry Smith   PetscFunctionBegin;
107559f443fSBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
108559f443fSBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
109559f443fSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
110559f443fSBarry Smith 
111559f443fSBarry Smith   if (!vascii->bviewer && !rank && (vascii->mode != FILE_MODE_READ)) {
1125c6c1daeSBarry Smith     err = fflush(vascii->fd);
1135c6c1daeSBarry Smith     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() call failed");
1145c6c1daeSBarry Smith   }
1155c6c1daeSBarry Smith 
1165c6c1daeSBarry Smith   if (vascii->allowsynchronized) {
117559f443fSBarry Smith     PetscMPIInt   tag,i,j,n = 0,dummy = 0;
118559f443fSBarry Smith     char          *message;
119559f443fSBarry Smith     MPI_Status    status;
120559f443fSBarry Smith 
121559f443fSBarry Smith     ierr = PetscCommDuplicate(comm,&comm,&tag);CHKERRQ(ierr);
122559f443fSBarry Smith 
123559f443fSBarry Smith     /* First processor waits for messages from all other processors */
124559f443fSBarry Smith     if (!rank) {
125559f443fSBarry Smith       /* flush my own messages that I may have queued up */
126559f443fSBarry Smith       PrintfQueue next = vascii->petsc_printfqueuebase,previous;
127559f443fSBarry Smith       for (i=0; i<vascii->petsc_printfqueuelength; i++) {
128559f443fSBarry Smith         if (!vascii->bviewer) {
129559f443fSBarry Smith           ierr = PetscFPrintf(comm,fd,"%s",next->string);CHKERRQ(ierr);
130559f443fSBarry Smith         } else {
131559f443fSBarry Smith           ierr = PetscViewerASCIISynchronizedPrintf(vascii->bviewer,"%s",next->string);CHKERRQ(ierr);
132559f443fSBarry Smith         }
133559f443fSBarry Smith         previous = next;
134559f443fSBarry Smith         next     = next->next;
135559f443fSBarry Smith         ierr     = PetscFree(previous->string);CHKERRQ(ierr);
136559f443fSBarry Smith         ierr     = PetscFree(previous);CHKERRQ(ierr);
137559f443fSBarry Smith       }
138559f443fSBarry Smith       vascii->petsc_printfqueue       = 0;
139559f443fSBarry Smith       vascii->petsc_printfqueuelength = 0;
140559f443fSBarry Smith       for (i=1; i<size; i++) {
141559f443fSBarry Smith         /* to prevent a flood of messages to process zero, request each message separately */
142559f443fSBarry Smith         ierr = MPI_Send(&dummy,1,MPI_INT,i,tag,comm);CHKERRQ(ierr);
143559f443fSBarry Smith         ierr = MPI_Recv(&n,1,MPI_INT,i,tag,comm,&status);CHKERRQ(ierr);
144559f443fSBarry Smith         for (j=0; j<n; j++) {
145559f443fSBarry Smith           PetscMPIInt size = 0;
146559f443fSBarry Smith 
147559f443fSBarry Smith           ierr = MPI_Recv(&size,1,MPI_INT,i,tag,comm,&status);CHKERRQ(ierr);
148559f443fSBarry Smith           ierr = PetscMalloc1(size, &message);CHKERRQ(ierr);
149559f443fSBarry Smith           ierr = MPI_Recv(message,size,MPI_CHAR,i,tag,comm,&status);CHKERRQ(ierr);
150559f443fSBarry Smith           if (!vascii->bviewer) {
151559f443fSBarry Smith             ierr = PetscFPrintf(comm,fd,"%s",message);CHKERRQ(ierr);
152559f443fSBarry Smith           } else {
153559f443fSBarry Smith             ierr = PetscViewerASCIISynchronizedPrintf(vascii->bviewer,"%s",message);CHKERRQ(ierr);
154559f443fSBarry Smith           }
155559f443fSBarry Smith           ierr = PetscFree(message);CHKERRQ(ierr);
156559f443fSBarry Smith         }
157559f443fSBarry Smith       }
158559f443fSBarry Smith     } else { /* other processors send queue to processor 0 */
159559f443fSBarry Smith       PrintfQueue next = vascii->petsc_printfqueuebase,previous;
160559f443fSBarry Smith 
161559f443fSBarry Smith       ierr = MPI_Recv(&dummy,1,MPI_INT,0,tag,comm,&status);CHKERRQ(ierr);
162559f443fSBarry Smith       ierr = MPI_Send(&vascii->petsc_printfqueuelength,1,MPI_INT,0,tag,comm);CHKERRQ(ierr);
163559f443fSBarry Smith       for (i=0; i<vascii->petsc_printfqueuelength; i++) {
164559f443fSBarry Smith         ierr     = MPI_Send(&next->size,1,MPI_INT,0,tag,comm);CHKERRQ(ierr);
165559f443fSBarry Smith         ierr     = MPI_Send(next->string,next->size,MPI_CHAR,0,tag,comm);CHKERRQ(ierr);
166559f443fSBarry Smith         previous = next;
167559f443fSBarry Smith         next     = next->next;
168559f443fSBarry Smith         ierr     = PetscFree(previous->string);CHKERRQ(ierr);
169559f443fSBarry Smith         ierr     = PetscFree(previous);CHKERRQ(ierr);
170559f443fSBarry Smith       }
171559f443fSBarry Smith       vascii->petsc_printfqueue       = 0;
172559f443fSBarry Smith       vascii->petsc_printfqueuelength = 0;
173559f443fSBarry Smith     }
174559f443fSBarry Smith     ierr = PetscCommDestroy(&comm);CHKERRQ(ierr);
1755c6c1daeSBarry Smith   }
1765c6c1daeSBarry Smith   PetscFunctionReturn(0);
1775c6c1daeSBarry Smith }
1785c6c1daeSBarry Smith 
1795c6c1daeSBarry Smith #undef __FUNCT__
1805c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIIGetPointer"
1815c6c1daeSBarry Smith /*@C
1825c6c1daeSBarry Smith     PetscViewerASCIIGetPointer - Extracts the file pointer from an ASCII PetscViewer.
1835c6c1daeSBarry Smith 
1845c6c1daeSBarry Smith     Not Collective
1855c6c1daeSBarry Smith 
1865c6c1daeSBarry Smith +   viewer - PetscViewer context, obtained from PetscViewerASCIIOpen()
1875c6c1daeSBarry Smith -   fd - file pointer
1885c6c1daeSBarry Smith 
1895c6c1daeSBarry Smith     Level: intermediate
1905c6c1daeSBarry Smith 
1915c6c1daeSBarry Smith     Fortran Note:
1925c6c1daeSBarry Smith     This routine is not supported in Fortran.
1935c6c1daeSBarry Smith 
1945c6c1daeSBarry Smith   Concepts: PetscViewer^file pointer
1955c6c1daeSBarry Smith   Concepts: file pointer^getting from PetscViewer
1965c6c1daeSBarry Smith 
1975c6c1daeSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerCreate(), PetscViewerASCIIPrintf(),
1985c6c1daeSBarry Smith           PetscViewerASCIISynchronizedPrintf(), PetscViewerFlush()
1995c6c1daeSBarry Smith @*/
2005c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIGetPointer(PetscViewer viewer,FILE **fd)
2015c6c1daeSBarry Smith {
2025c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
2035c6c1daeSBarry Smith 
2045c6c1daeSBarry Smith   PetscFunctionBegin;
2055c6c1daeSBarry Smith   *fd = vascii->fd;
2065c6c1daeSBarry Smith   PetscFunctionReturn(0);
2075c6c1daeSBarry Smith }
2085c6c1daeSBarry Smith 
2095c6c1daeSBarry Smith #undef __FUNCT__
2105c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileGetMode_ASCII"
2115c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileGetMode_ASCII(PetscViewer viewer, PetscFileMode *mode)
2125c6c1daeSBarry Smith {
2135c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
2145c6c1daeSBarry Smith 
2155c6c1daeSBarry Smith   PetscFunctionBegin;
2165c6c1daeSBarry Smith   *mode = vascii->mode;
2175c6c1daeSBarry Smith   PetscFunctionReturn(0);
2185c6c1daeSBarry Smith }
2195c6c1daeSBarry Smith 
2205c6c1daeSBarry Smith #undef __FUNCT__
2215c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileSetMode_ASCII"
2225c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileSetMode_ASCII(PetscViewer viewer, PetscFileMode mode)
2235c6c1daeSBarry Smith {
2245c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
2255c6c1daeSBarry Smith 
2265c6c1daeSBarry Smith   PetscFunctionBegin;
2275c6c1daeSBarry Smith   vascii->mode = mode;
2285c6c1daeSBarry Smith   PetscFunctionReturn(0);
2295c6c1daeSBarry Smith }
2305c6c1daeSBarry Smith 
2315c6c1daeSBarry Smith /*
2325c6c1daeSBarry Smith    If petsc_history is on, then all Petsc*Printf() results are saved
2335c6c1daeSBarry Smith    if the appropriate (usually .petschistory) file.
2345c6c1daeSBarry Smith */
2355c6c1daeSBarry Smith extern FILE *petsc_history;
2365c6c1daeSBarry Smith 
2375c6c1daeSBarry Smith #undef __FUNCT__
2385c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIISetTab"
2395c6c1daeSBarry Smith /*@
2405c6c1daeSBarry Smith     PetscViewerASCIISetTab - Causes PetscViewer to tab in a number of times
2415c6c1daeSBarry Smith 
2425c6c1daeSBarry Smith     Not Collective, but only first processor in set has any effect
2435c6c1daeSBarry Smith 
2445c6c1daeSBarry Smith     Input Parameters:
2451575c14dSBarry Smith +    viewer - obtained with PetscViewerASCIIOpen()
2465c6c1daeSBarry Smith -    tabs - number of tabs
2475c6c1daeSBarry Smith 
2485c6c1daeSBarry Smith     Level: developer
2495c6c1daeSBarry Smith 
2505c6c1daeSBarry Smith     Fortran Note:
2515c6c1daeSBarry Smith     This routine is not supported in Fortran.
2525c6c1daeSBarry Smith 
2535c6c1daeSBarry Smith   Concepts: PetscViewerASCII^formating
2545c6c1daeSBarry Smith   Concepts: tab^setting
2555c6c1daeSBarry Smith 
2565c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(), PetscViewerASCIIGetTab(),
2575c6c1daeSBarry Smith           PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(),
2585c6c1daeSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer(), PetscViewerASCIIPushTab()
2595c6c1daeSBarry Smith @*/
2605c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIISetTab(PetscViewer viewer,PetscInt tabs)
2615c6c1daeSBarry Smith {
2625c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
2635c6c1daeSBarry Smith   PetscBool         iascii;
2645c6c1daeSBarry Smith   PetscErrorCode    ierr;
2655c6c1daeSBarry Smith 
2665c6c1daeSBarry Smith   PetscFunctionBegin;
2675c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
2685c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
269a297a907SKarl Rupp   if (iascii) ascii->tab = tabs;
2705c6c1daeSBarry Smith   PetscFunctionReturn(0);
2715c6c1daeSBarry Smith }
2725c6c1daeSBarry Smith 
2735c6c1daeSBarry Smith #undef __FUNCT__
2745c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIIGetTab"
2755c6c1daeSBarry Smith /*@
2765c6c1daeSBarry Smith     PetscViewerASCIIGetTab - Return the number of tabs used by PetscViewer.
2775c6c1daeSBarry Smith 
2785c6c1daeSBarry Smith     Not Collective, meaningful on first processor only.
2795c6c1daeSBarry Smith 
2805c6c1daeSBarry Smith     Input Parameters:
2811575c14dSBarry Smith .    viewer - obtained with PetscViewerASCIIOpen()
2825c6c1daeSBarry Smith     Output Parameters:
2835c6c1daeSBarry Smith .    tabs - number of tabs
2845c6c1daeSBarry Smith 
2855c6c1daeSBarry Smith     Level: developer
2865c6c1daeSBarry Smith 
2875c6c1daeSBarry Smith     Fortran Note:
2885c6c1daeSBarry Smith     This routine is not supported in Fortran.
2895c6c1daeSBarry Smith 
2905c6c1daeSBarry Smith   Concepts: PetscViewerASCII^formating
2915c6c1daeSBarry Smith   Concepts: tab^retrieval
2925c6c1daeSBarry Smith 
2935c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(), PetscViewerASCIISetTab(),
2945c6c1daeSBarry Smith           PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(),
2955c6c1daeSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer(), PetscViewerASCIIPushTab()
2965c6c1daeSBarry Smith @*/
2975c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIGetTab(PetscViewer viewer,PetscInt *tabs)
2985c6c1daeSBarry Smith {
2995c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
3005c6c1daeSBarry Smith   PetscBool         iascii;
3015c6c1daeSBarry Smith   PetscErrorCode    ierr;
3025c6c1daeSBarry Smith 
3035c6c1daeSBarry Smith   PetscFunctionBegin;
3045c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
3055c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
306a297a907SKarl Rupp   if (iascii && tabs) *tabs = ascii->tab;
3075c6c1daeSBarry Smith   PetscFunctionReturn(0);
3085c6c1daeSBarry Smith }
3095c6c1daeSBarry Smith 
3105c6c1daeSBarry Smith #undef __FUNCT__
3115c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIIAddTab"
3125c6c1daeSBarry Smith /*@
3135c6c1daeSBarry Smith     PetscViewerASCIIAddTab - Add to the number of times an ASCII viewer tabs before printing
3145c6c1daeSBarry Smith 
3155c6c1daeSBarry Smith     Not Collective, but only first processor in set has any effect
3165c6c1daeSBarry Smith 
3175c6c1daeSBarry Smith     Input Parameters:
3181575c14dSBarry Smith +    viewer - obtained with PetscViewerASCIIOpen()
3195c6c1daeSBarry Smith -    tabs - number of tabs
3205c6c1daeSBarry Smith 
3215c6c1daeSBarry Smith     Level: developer
3225c6c1daeSBarry Smith 
3235c6c1daeSBarry Smith     Fortran Note:
3245c6c1daeSBarry Smith     This routine is not supported in Fortran.
3255c6c1daeSBarry Smith 
3265c6c1daeSBarry Smith   Concepts: PetscViewerASCII^formating
3275c6c1daeSBarry Smith   Concepts: tab^setting
3285c6c1daeSBarry Smith 
3295c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(),
3305c6c1daeSBarry Smith           PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(),
3315c6c1daeSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer(), PetscViewerASCIIPushTab()
3325c6c1daeSBarry Smith @*/
3335c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIAddTab(PetscViewer viewer,PetscInt tabs)
3345c6c1daeSBarry Smith {
3355c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
3365c6c1daeSBarry Smith   PetscBool         iascii;
3375c6c1daeSBarry Smith   PetscErrorCode    ierr;
3385c6c1daeSBarry Smith 
3395c6c1daeSBarry Smith   PetscFunctionBegin;
3405c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
3415c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
342a297a907SKarl Rupp   if (iascii) ascii->tab += tabs;
3435c6c1daeSBarry Smith   PetscFunctionReturn(0);
3445c6c1daeSBarry Smith }
3455c6c1daeSBarry Smith 
3465c6c1daeSBarry Smith #undef __FUNCT__
3475c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIISubtractTab"
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 #undef __FUNCT__
3831575c14dSBarry Smith #define __FUNCT__ "PetscViewerASCIIPushSynchronized"
3845c6c1daeSBarry Smith /*@C
3851575c14dSBarry Smith     PetscViewerASCIIPushSynchronized - Allows calls to PetscViewerASCIISynchronizedPrintf() for this viewer
3865c6c1daeSBarry Smith 
3875c6c1daeSBarry Smith     Collective on PetscViewer
3885c6c1daeSBarry Smith 
3895c6c1daeSBarry Smith     Input Parameters:
3901575c14dSBarry Smith .    viewer - obtained with PetscViewerASCIIOpen()
3915c6c1daeSBarry Smith 
3925c6c1daeSBarry Smith     Level: intermediate
3935c6c1daeSBarry Smith 
3945c6c1daeSBarry Smith   Concepts: PetscViewerASCII^formating
3955c6c1daeSBarry Smith   Concepts: tab^setting
3965c6c1daeSBarry Smith 
3971575c14dSBarry Smith .seealso: PetscViewerASCIIPopSynchronized(), PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(),
3985c6c1daeSBarry Smith           PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(),
3995c6c1daeSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer()
4005c6c1daeSBarry Smith @*/
4011575c14dSBarry Smith PetscErrorCode  PetscViewerASCIIPushSynchronized(PetscViewer viewer)
4025c6c1daeSBarry Smith {
4035c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
4045c6c1daeSBarry Smith   PetscBool         iascii;
4055c6c1daeSBarry Smith   PetscErrorCode    ierr;
4065c6c1daeSBarry Smith 
4075c6c1daeSBarry Smith   PetscFunctionBegin;
4085c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
4095c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
4101575c14dSBarry Smith   if (iascii) ascii->allowsynchronized++;
4111575c14dSBarry Smith   PetscFunctionReturn(0);
4121575c14dSBarry Smith }
4131575c14dSBarry Smith 
4141575c14dSBarry Smith #undef __FUNCT__
4151575c14dSBarry Smith #define __FUNCT__ "PetscViewerASCIIPopSynchronized"
4161575c14dSBarry Smith /*@C
4171575c14dSBarry Smith     PetscViewerASCIIPopSynchronized - Undoes most recent PetscViewerASCIIPushSynchronized() for this viewer
4181575c14dSBarry Smith 
4191575c14dSBarry Smith     Collective on PetscViewer
4201575c14dSBarry Smith 
4211575c14dSBarry Smith     Input Parameters:
4221575c14dSBarry Smith .    viewer - obtained with PetscViewerASCIIOpen()
4231575c14dSBarry Smith 
4241575c14dSBarry Smith     Level: intermediate
4251575c14dSBarry Smith 
4261575c14dSBarry Smith   Concepts: PetscViewerASCII^formating
4271575c14dSBarry Smith   Concepts: tab^setting
4281575c14dSBarry Smith 
4291575c14dSBarry Smith .seealso: PetscViewerASCIIPushSynchronized(), PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(),
4301575c14dSBarry Smith           PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(),
4311575c14dSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer()
4321575c14dSBarry Smith @*/
4331575c14dSBarry Smith PetscErrorCode  PetscViewerASCIIPopSynchronized(PetscViewer viewer)
4341575c14dSBarry Smith {
4351575c14dSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
4361575c14dSBarry Smith   PetscBool         iascii;
4371575c14dSBarry Smith   PetscErrorCode    ierr;
4381575c14dSBarry Smith 
4391575c14dSBarry Smith   PetscFunctionBegin;
4401575c14dSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
4411575c14dSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
4421575c14dSBarry Smith   if (iascii) {
4431575c14dSBarry Smith     ascii->allowsynchronized--;
4441575c14dSBarry Smith     if (ascii->allowsynchronized < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Called more times than PetscViewerASCIIPushSynchronized()");
4451575c14dSBarry Smith   }
4465c6c1daeSBarry Smith   PetscFunctionReturn(0);
4475c6c1daeSBarry Smith }
4485c6c1daeSBarry Smith 
4495c6c1daeSBarry Smith #undef __FUNCT__
4505c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIIPushTab"
4515c6c1daeSBarry Smith /*@
4525c6c1daeSBarry Smith     PetscViewerASCIIPushTab - Adds one more tab to the amount that PetscViewerASCIIPrintf()
4535c6c1daeSBarry Smith      lines are tabbed.
4545c6c1daeSBarry Smith 
4555c6c1daeSBarry Smith     Not Collective, but only first processor in set has any effect
4565c6c1daeSBarry Smith 
4575c6c1daeSBarry Smith     Input Parameters:
4581575c14dSBarry Smith .    viewer - obtained with PetscViewerASCIIOpen()
4595c6c1daeSBarry Smith 
4605c6c1daeSBarry Smith     Level: developer
4615c6c1daeSBarry Smith 
4625c6c1daeSBarry Smith     Fortran Note:
4635c6c1daeSBarry Smith     This routine is not supported in Fortran.
4645c6c1daeSBarry Smith 
4655c6c1daeSBarry Smith   Concepts: PetscViewerASCII^formating
4665c6c1daeSBarry Smith   Concepts: tab^setting
4675c6c1daeSBarry Smith 
4685c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(),
4695c6c1daeSBarry Smith           PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(),
4705c6c1daeSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer()
4715c6c1daeSBarry Smith @*/
4725c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIPushTab(PetscViewer viewer)
4735c6c1daeSBarry Smith {
4745c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
4755c6c1daeSBarry Smith   PetscBool         iascii;
4765c6c1daeSBarry Smith   PetscErrorCode    ierr;
4775c6c1daeSBarry Smith 
4785c6c1daeSBarry Smith   PetscFunctionBegin;
4795c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
4805c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
481a297a907SKarl Rupp   if (iascii) ascii->tab++;
4825c6c1daeSBarry Smith   PetscFunctionReturn(0);
4835c6c1daeSBarry Smith }
4845c6c1daeSBarry Smith 
4855c6c1daeSBarry Smith #undef __FUNCT__
4865c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIIPopTab"
4875c6c1daeSBarry Smith /*@
4885c6c1daeSBarry Smith     PetscViewerASCIIPopTab - Removes one tab from the amount that PetscViewerASCIIPrintf()
4895c6c1daeSBarry Smith      lines are tabbed.
4905c6c1daeSBarry Smith 
4915c6c1daeSBarry Smith     Not Collective, but only first processor in set has any effect
4925c6c1daeSBarry Smith 
4935c6c1daeSBarry Smith     Input Parameters:
4941575c14dSBarry Smith .    viewer - obtained with PetscViewerASCIIOpen()
4955c6c1daeSBarry Smith 
4965c6c1daeSBarry Smith     Level: developer
4975c6c1daeSBarry Smith 
4985c6c1daeSBarry Smith     Fortran Note:
4995c6c1daeSBarry Smith     This routine is not supported in Fortran.
5005c6c1daeSBarry Smith 
5015c6c1daeSBarry Smith   Concepts: PetscViewerASCII^formating
5025c6c1daeSBarry Smith   Concepts: tab^setting
5035c6c1daeSBarry Smith 
5045c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(),
5055c6c1daeSBarry Smith           PetscViewerASCIIPushTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIOpen(),
5065c6c1daeSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer()
5075c6c1daeSBarry Smith @*/
5085c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIPopTab(PetscViewer viewer)
5095c6c1daeSBarry Smith {
5105c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
5115c6c1daeSBarry Smith   PetscErrorCode    ierr;
5125c6c1daeSBarry Smith   PetscBool         iascii;
5135c6c1daeSBarry Smith 
5145c6c1daeSBarry Smith   PetscFunctionBegin;
5155c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
5165c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
5175c6c1daeSBarry Smith   if (iascii) {
5185c6c1daeSBarry Smith     if (ascii->tab <= 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"More tabs popped than pushed");
5195c6c1daeSBarry Smith     ascii->tab--;
5205c6c1daeSBarry Smith   }
5215c6c1daeSBarry Smith   PetscFunctionReturn(0);
5225c6c1daeSBarry Smith }
5235c6c1daeSBarry Smith 
5245c6c1daeSBarry Smith #undef __FUNCT__
5255c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIIUseTabs"
5265c6c1daeSBarry Smith /*@
5275c6c1daeSBarry Smith     PetscViewerASCIIUseTabs - Turns on or off the use of tabs with the ASCII PetscViewer
5285c6c1daeSBarry Smith 
5295c6c1daeSBarry Smith     Not Collective, but only first processor in set has any effect
5305c6c1daeSBarry Smith 
5315c6c1daeSBarry Smith     Input Parameters:
5321575c14dSBarry Smith +    viewer - obtained with PetscViewerASCIIOpen()
5335c6c1daeSBarry Smith -    flg - PETSC_TRUE or PETSC_FALSE
5345c6c1daeSBarry Smith 
5355c6c1daeSBarry Smith     Level: developer
5365c6c1daeSBarry Smith 
5375c6c1daeSBarry Smith     Fortran Note:
5385c6c1daeSBarry Smith     This routine is not supported in Fortran.
5395c6c1daeSBarry Smith 
5405c6c1daeSBarry Smith   Concepts: PetscViewerASCII^formating
5415c6c1daeSBarry Smith   Concepts: tab^setting
5425c6c1daeSBarry Smith 
5435c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIPrintf(),
5445c6c1daeSBarry Smith           PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(), PetscViewerASCIIPushTab(), PetscViewerASCIIOpen(),
5455c6c1daeSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer()
5465c6c1daeSBarry Smith @*/
5475c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIUseTabs(PetscViewer viewer,PetscBool flg)
5485c6c1daeSBarry Smith {
5495c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
5505c6c1daeSBarry Smith   PetscBool         iascii;
5515c6c1daeSBarry Smith   PetscErrorCode    ierr;
5525c6c1daeSBarry Smith 
5535c6c1daeSBarry Smith   PetscFunctionBegin;
5545c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
5555c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
5565c6c1daeSBarry Smith   if (iascii) {
557a297a907SKarl Rupp     if (flg) ascii->tab = ascii->tab_store;
558a297a907SKarl Rupp     else {
5595c6c1daeSBarry Smith       ascii->tab_store = ascii->tab;
5605c6c1daeSBarry Smith       ascii->tab       = 0;
5615c6c1daeSBarry Smith     }
5625c6c1daeSBarry Smith   }
5635c6c1daeSBarry Smith   PetscFunctionReturn(0);
5645c6c1daeSBarry Smith }
5655c6c1daeSBarry Smith 
5665c6c1daeSBarry Smith /* ----------------------------------------------------------------------- */
5675c6c1daeSBarry Smith 
5685c6c1daeSBarry Smith 
5695c6c1daeSBarry Smith #undef __FUNCT__
5705c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIIPrintf"
5715c6c1daeSBarry Smith /*@C
5725c6c1daeSBarry Smith     PetscViewerASCIIPrintf - Prints to a file, only from the first
5735c6c1daeSBarry Smith     processor in the PetscViewer
5745c6c1daeSBarry Smith 
5755c6c1daeSBarry Smith     Not Collective, but only first processor in set has any effect
5765c6c1daeSBarry Smith 
5775c6c1daeSBarry Smith     Input Parameters:
5781575c14dSBarry Smith +    viewer - obtained with PetscViewerASCIIOpen()
5795c6c1daeSBarry Smith -    format - the usual printf() format string
5805c6c1daeSBarry Smith 
5815c6c1daeSBarry Smith     Level: developer
5825c6c1daeSBarry Smith 
5835c6c1daeSBarry Smith     Fortran Note:
5845c6c1daeSBarry Smith     The call sequence is PetscViewerASCIIPrintf(PetscViewer, character(*), int ierr) from Fortran.
5855c6c1daeSBarry Smith     That is, you can only pass a single character string from Fortran.
5865c6c1daeSBarry Smith 
5875c6c1daeSBarry Smith   Concepts: PetscViewerASCII^printing
5885c6c1daeSBarry Smith   Concepts: printing^to file
5895c6c1daeSBarry Smith   Concepts: printf
5905c6c1daeSBarry Smith 
5915c6c1daeSBarry Smith .seealso: PetscPrintf(), PetscSynchronizedPrintf(), PetscViewerASCIIOpen(),
5925c6c1daeSBarry Smith           PetscViewerASCIIPushTab(), PetscViewerASCIIPopTab(), PetscViewerASCIISynchronizedPrintf(),
5931575c14dSBarry Smith           PetscViewerCreate(), PetscViewerDestroy(), PetscViewerSetType(), PetscViewerASCIIGetPointer(), PetscViewerASCIIPushSynchronized()
5945c6c1daeSBarry Smith @*/
5955c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIIPrintf(PetscViewer viewer,const char format[],...)
5965c6c1daeSBarry Smith {
5975c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)viewer->data;
5985c6c1daeSBarry Smith   PetscMPIInt       rank;
599dd2fa690SBarry Smith   PetscInt          tab,intab = ascii->tab;
6005c6c1daeSBarry Smith   PetscErrorCode    ierr;
6015c6c1daeSBarry Smith   FILE              *fd = ascii->fd;
6023f08860eSBarry Smith   PetscBool         iascii;
6035c6c1daeSBarry Smith   int               err;
6045c6c1daeSBarry Smith 
6055c6c1daeSBarry Smith   PetscFunctionBegin;
6065c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
6075c6c1daeSBarry Smith   PetscValidCharPointer(format,2);
6085c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
6095c6c1daeSBarry Smith   if (!iascii) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Not ASCII PetscViewer");
610ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
611559f443fSBarry Smith   if (rank) PetscFunctionReturn(0);
6123f08860eSBarry Smith 
6133f08860eSBarry Smith   if (ascii->bviewer) { /* pass string up to parent viewer */
6143f08860eSBarry Smith     char        *string;
6153f08860eSBarry Smith     va_list     Argp;
6163f08860eSBarry Smith     size_t      fullLength;
6173f08860eSBarry Smith 
6183f08860eSBarry Smith     ierr       = PetscCalloc1(QUEUESTRINGSIZE, &string);CHKERRQ(ierr);
6193f08860eSBarry Smith     va_start(Argp,format);
6201575c14dSBarry Smith     ierr = PetscVSNPrintf(string,QUEUESTRINGSIZE,format,&fullLength,Argp);CHKERRQ(ierr);
6213f08860eSBarry Smith     va_end(Argp);
622559f443fSBarry Smith     ierr = PetscViewerASCIISynchronizedPrintf(viewer,"%s",string);CHKERRQ(ierr);
6233f08860eSBarry Smith     ierr = PetscFree(string);CHKERRQ(ierr);
6243f08860eSBarry Smith   } else { /* write directly to file */
6255c6c1daeSBarry Smith     va_list Argp;
626559f443fSBarry Smith     /* flush my own messages that I may have queued up */
627559f443fSBarry Smith     PrintfQueue next = ascii->petsc_printfqueuebase,previous;
628559f443fSBarry Smith     PetscInt    i;
629559f443fSBarry Smith     for (i=0; i<ascii->petsc_printfqueuelength; i++) {
630559f443fSBarry Smith       ierr = PetscFPrintf(PETSC_COMM_SELF,fd,"%s",next->string);CHKERRQ(ierr);
631559f443fSBarry Smith       previous = next;
632559f443fSBarry Smith       next     = next->next;
633559f443fSBarry Smith       ierr     = PetscFree(previous->string);CHKERRQ(ierr);
634559f443fSBarry Smith       ierr     = PetscFree(previous);CHKERRQ(ierr);
635559f443fSBarry Smith     }
636559f443fSBarry Smith     ascii->petsc_printfqueue       = 0;
637559f443fSBarry Smith     ascii->petsc_printfqueuelength = 0;
638dd2fa690SBarry Smith     tab = intab;
639a297a907SKarl Rupp     while (tab--) {
640a297a907SKarl Rupp       ierr = PetscFPrintf(PETSC_COMM_SELF,fd,"  ");CHKERRQ(ierr);
641a297a907SKarl Rupp     }
6425c6c1daeSBarry Smith 
6435c6c1daeSBarry Smith     va_start(Argp,format);
6445c6c1daeSBarry Smith     ierr = (*PetscVFPrintf)(fd,format,Argp);CHKERRQ(ierr);
6455c6c1daeSBarry Smith     err  = fflush(fd);
6465c6c1daeSBarry Smith     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
6475c6c1daeSBarry Smith     if (petsc_history) {
6485c6c1daeSBarry Smith       va_start(Argp,format);
649dd2fa690SBarry Smith       tab = intab;
650a297a907SKarl Rupp       while (tab--) {
651706d7a88SBarry Smith         ierr = PetscFPrintf(PETSC_COMM_SELF,petsc_history,"  ");CHKERRQ(ierr);
652a297a907SKarl Rupp       }
6535c6c1daeSBarry Smith       ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr);
6545c6c1daeSBarry Smith       err  = fflush(petsc_history);
6555c6c1daeSBarry Smith       if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
6565c6c1daeSBarry Smith     }
6575c6c1daeSBarry Smith     va_end(Argp);
6585c6c1daeSBarry Smith   }
6595c6c1daeSBarry Smith   PetscFunctionReturn(0);
6605c6c1daeSBarry Smith }
6615c6c1daeSBarry Smith 
6625c6c1daeSBarry Smith #undef __FUNCT__
6635c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileSetName"
6645c6c1daeSBarry Smith /*@C
6655c6c1daeSBarry Smith      PetscViewerFileSetName - Sets the name of the file the PetscViewer uses.
6665c6c1daeSBarry Smith 
6675c6c1daeSBarry Smith     Collective on PetscViewer
6685c6c1daeSBarry Smith 
6695c6c1daeSBarry Smith   Input Parameters:
6705c6c1daeSBarry Smith +  viewer - the PetscViewer; either ASCII or binary
6715c6c1daeSBarry Smith -  name - the name of the file it should use
6725c6c1daeSBarry Smith 
6735c6c1daeSBarry Smith     Level: advanced
6745c6c1daeSBarry Smith 
6755c6c1daeSBarry Smith .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerASCIIOpen(), PetscViewerBinaryOpen(), PetscViewerDestroy(),
6765c6c1daeSBarry Smith           PetscViewerASCIIGetPointer(), PetscViewerASCIIPrintf(), PetscViewerASCIISynchronizedPrintf()
6775c6c1daeSBarry Smith 
6785c6c1daeSBarry Smith @*/
6795c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileSetName(PetscViewer viewer,const char name[])
6805c6c1daeSBarry Smith {
6815c6c1daeSBarry Smith   PetscErrorCode ierr;
6825c6c1daeSBarry Smith 
6835c6c1daeSBarry Smith   PetscFunctionBegin;
6845c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
6855c6c1daeSBarry Smith   PetscValidCharPointer(name,2);
6865c6c1daeSBarry Smith   ierr = PetscTryMethod(viewer,"PetscViewerFileSetName_C",(PetscViewer,const char[]),(viewer,name));CHKERRQ(ierr);
6875c6c1daeSBarry Smith   PetscFunctionReturn(0);
6885c6c1daeSBarry Smith }
6895c6c1daeSBarry Smith 
6905c6c1daeSBarry Smith #undef __FUNCT__
6915c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileGetName"
6925c6c1daeSBarry Smith /*@C
6935c6c1daeSBarry Smith      PetscViewerFileGetName - Gets the name of the file the PetscViewer uses.
6945c6c1daeSBarry Smith 
6955c6c1daeSBarry Smith     Not Collective
6965c6c1daeSBarry Smith 
6975c6c1daeSBarry Smith   Input Parameter:
6985c6c1daeSBarry Smith .  viewer - the PetscViewer; either ASCII or binary
6995c6c1daeSBarry Smith 
7005c6c1daeSBarry Smith   Output Parameter:
7015c6c1daeSBarry Smith .  name - the name of the file it is using
7025c6c1daeSBarry Smith 
7035c6c1daeSBarry Smith     Level: advanced
7045c6c1daeSBarry Smith 
7055c6c1daeSBarry Smith .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerASCIIOpen(), PetscViewerBinaryOpen(), PetscViewerFileSetName()
7065c6c1daeSBarry Smith 
7075c6c1daeSBarry Smith @*/
7085c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileGetName(PetscViewer viewer,const char **name)
7095c6c1daeSBarry Smith {
7105c6c1daeSBarry Smith   PetscErrorCode ierr;
7115c6c1daeSBarry Smith 
7125c6c1daeSBarry Smith   PetscFunctionBegin;
7135c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
714*163d334eSBarry Smith   ierr = PetscUseMethod(viewer,"PetscViewerFileGetName_C",(PetscViewer,const char**),(viewer,name));CHKERRQ(ierr);
7155c6c1daeSBarry Smith   PetscFunctionReturn(0);
7165c6c1daeSBarry Smith }
7175c6c1daeSBarry Smith 
7185c6c1daeSBarry Smith #undef __FUNCT__
7195c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileGetName_ASCII"
7205c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileGetName_ASCII(PetscViewer viewer,const char **name)
7215c6c1daeSBarry Smith {
7225c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
7235c6c1daeSBarry Smith 
7245c6c1daeSBarry Smith   PetscFunctionBegin;
7255c6c1daeSBarry Smith   *name = vascii->filename;
7265c6c1daeSBarry Smith   PetscFunctionReturn(0);
7275c6c1daeSBarry Smith }
7285c6c1daeSBarry Smith 
7295c6c1daeSBarry Smith #undef __FUNCT__
7305c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileSetName_ASCII"
7315c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileSetName_ASCII(PetscViewer viewer,const char name[])
7325c6c1daeSBarry Smith {
7335c6c1daeSBarry Smith   PetscErrorCode    ierr;
7345c6c1daeSBarry Smith   size_t            len;
7355c6c1daeSBarry Smith   char              fname[PETSC_MAX_PATH_LEN],*gz;
7365c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
7375c6c1daeSBarry Smith   PetscBool         isstderr,isstdout;
7385c6c1daeSBarry Smith   PetscMPIInt       rank;
7395c6c1daeSBarry Smith 
7405c6c1daeSBarry Smith   PetscFunctionBegin;
7415c6c1daeSBarry Smith   ierr = PetscViewerFileClose_ASCII(viewer);CHKERRQ(ierr);
7425c6c1daeSBarry Smith   if (!name) PetscFunctionReturn(0);
7435c6c1daeSBarry Smith   ierr = PetscStrallocpy(name,&vascii->filename);CHKERRQ(ierr);
7445c6c1daeSBarry Smith 
7455c6c1daeSBarry Smith   /* Is this file to be compressed */
7465c6c1daeSBarry Smith   vascii->storecompressed = PETSC_FALSE;
747a297a907SKarl Rupp 
7485c6c1daeSBarry Smith   ierr = PetscStrstr(vascii->filename,".gz",&gz);CHKERRQ(ierr);
7495c6c1daeSBarry Smith   if (gz) {
7505c6c1daeSBarry Smith     ierr = PetscStrlen(gz,&len);CHKERRQ(ierr);
7515c6c1daeSBarry Smith     if (len == 3) {
7525c6c1daeSBarry Smith       *gz = 0;
7535c6c1daeSBarry Smith       vascii->storecompressed = PETSC_TRUE;
7545c6c1daeSBarry Smith     }
7555c6c1daeSBarry Smith   }
756ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
7575c6c1daeSBarry Smith   if (!rank) {
7585c6c1daeSBarry Smith     ierr = PetscStrcmp(name,"stderr",&isstderr);CHKERRQ(ierr);
7595c6c1daeSBarry Smith     ierr = PetscStrcmp(name,"stdout",&isstdout);CHKERRQ(ierr);
7605c6c1daeSBarry Smith     /* empty filename means stdout */
7615c6c1daeSBarry Smith     if (name[0] == 0)  isstdout = PETSC_TRUE;
7625c6c1daeSBarry Smith     if (isstderr)      vascii->fd = PETSC_STDERR;
7635c6c1daeSBarry Smith     else if (isstdout) vascii->fd = PETSC_STDOUT;
7645c6c1daeSBarry Smith     else {
7655c6c1daeSBarry Smith 
7665c6c1daeSBarry Smith 
7675c6c1daeSBarry Smith       ierr = PetscFixFilename(name,fname);CHKERRQ(ierr);
7685c6c1daeSBarry Smith       switch (vascii->mode) {
7695c6c1daeSBarry Smith       case FILE_MODE_READ:
7705c6c1daeSBarry Smith         vascii->fd = fopen(fname,"r");
7715c6c1daeSBarry Smith         break;
7725c6c1daeSBarry Smith       case FILE_MODE_WRITE:
7735c6c1daeSBarry Smith         vascii->fd = fopen(fname,"w");
7745c6c1daeSBarry Smith         break;
7755c6c1daeSBarry Smith       case FILE_MODE_APPEND:
7765c6c1daeSBarry Smith         vascii->fd = fopen(fname,"a");
7775c6c1daeSBarry Smith         break;
7785c6c1daeSBarry Smith       case FILE_MODE_UPDATE:
7795c6c1daeSBarry Smith         vascii->fd = fopen(fname,"r+");
780a297a907SKarl Rupp         if (!vascii->fd) vascii->fd = fopen(fname,"w+");
7815c6c1daeSBarry Smith         break;
7825c6c1daeSBarry Smith       case FILE_MODE_APPEND_UPDATE:
7835c6c1daeSBarry Smith         /* I really want a file which is opened at the end for updating,
7845c6c1daeSBarry Smith            not a+, which opens at the beginning, but makes writes at the end.
7855c6c1daeSBarry Smith         */
7865c6c1daeSBarry Smith         vascii->fd = fopen(fname,"r+");
787a297a907SKarl Rupp         if (!vascii->fd) vascii->fd = fopen(fname,"w+");
788a297a907SKarl Rupp         else {
7895c6c1daeSBarry Smith           ierr     = fseek(vascii->fd, 0, SEEK_END);CHKERRQ(ierr);
7905c6c1daeSBarry Smith         }
7915c6c1daeSBarry Smith         break;
7925c6c1daeSBarry Smith       default:
7935c6c1daeSBarry Smith         SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Invalid file mode %d", vascii->mode);
7945c6c1daeSBarry Smith       }
7955c6c1daeSBarry Smith       if (!vascii->fd) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open PetscViewer file: %s",fname);
7965c6c1daeSBarry Smith     }
7975c6c1daeSBarry Smith   }
7985c6c1daeSBarry Smith #if defined(PETSC_USE_LOG)
7995c6c1daeSBarry Smith   PetscLogObjectState((PetscObject)viewer,"File: %s",name);
8005c6c1daeSBarry Smith #endif
8015c6c1daeSBarry Smith   PetscFunctionReturn(0);
8025c6c1daeSBarry Smith }
8035c6c1daeSBarry Smith 
8045c6c1daeSBarry Smith #undef __FUNCT__
8053f08860eSBarry Smith #define __FUNCT__ "PetscViewerGetSubViewer_ASCII"
8063f08860eSBarry Smith PetscErrorCode PetscViewerGetSubViewer_ASCII(PetscViewer viewer,MPI_Comm subcomm,PetscViewer *outviewer)
8075c6c1daeSBarry Smith {
8085c6c1daeSBarry Smith   PetscMPIInt       rank;
8095c6c1daeSBarry Smith   PetscErrorCode    ierr;
8105c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data,*ovascii;
8115c6c1daeSBarry Smith 
8125c6c1daeSBarry Smith   PetscFunctionBegin;
8133f08860eSBarry Smith   if (vascii->sviewer) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"SubViewer already obtained from PetscViewer and not restored");
8141575c14dSBarry Smith   ierr         = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
8153f08860eSBarry Smith   ierr         = PetscViewerCreate(subcomm,outviewer);CHKERRQ(ierr);
8165c6c1daeSBarry Smith   ierr         = PetscViewerSetType(*outviewer,PETSCVIEWERASCII);CHKERRQ(ierr);
8171575c14dSBarry Smith   ierr         = PetscViewerASCIIPushSynchronized(*outviewer);CHKERRQ(ierr);
8185c6c1daeSBarry Smith   ovascii      = (PetscViewer_ASCII*)(*outviewer)->data;
8195c6c1daeSBarry Smith   ovascii->fd  = vascii->fd;
8205c6c1daeSBarry Smith   ovascii->tab = vascii->tab;
821ba5a0b41SBarry Smith   ovascii->closefile = PETSC_FALSE;
8225c6c1daeSBarry Smith 
8235c6c1daeSBarry Smith   vascii->sviewer = *outviewer;
8245c6c1daeSBarry Smith 
8255c6c1daeSBarry Smith   (*outviewer)->format  = viewer->format;
8265c6c1daeSBarry Smith 
827ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
8285c6c1daeSBarry Smith   ((PetscViewer_ASCII*)((*outviewer)->data))->bviewer = viewer;
8293f08860eSBarry Smith   (*outviewer)->ops->destroy = PetscViewerDestroy_ASCII_SubViewer;
8305c6c1daeSBarry Smith   PetscFunctionReturn(0);
8315c6c1daeSBarry Smith }
8325c6c1daeSBarry Smith 
8335c6c1daeSBarry Smith #undef __FUNCT__
8343f08860eSBarry Smith #define __FUNCT__ "PetscViewerRestoreSubViewer_ASCII"
8353f08860eSBarry Smith PetscErrorCode PetscViewerRestoreSubViewer_ASCII(PetscViewer viewer,MPI_Comm comm,PetscViewer *outviewer)
8365c6c1daeSBarry Smith {
8375c6c1daeSBarry Smith   PetscErrorCode    ierr;
8385c6c1daeSBarry Smith   PetscViewer_ASCII *ascii  = (PetscViewer_ASCII*)viewer->data;
8395c6c1daeSBarry Smith 
8405c6c1daeSBarry Smith   PetscFunctionBegin;
8411575c14dSBarry Smith   if (!ascii->sviewer) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"SubViewer never obtained from PetscViewer");
8421575c14dSBarry Smith   if (ascii->sviewer != *outviewer) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"This PetscViewer did not generate this SubViewer");
8435c6c1daeSBarry Smith 
8445c6c1daeSBarry Smith   ascii->sviewer             = 0;
8455c6c1daeSBarry Smith   (*outviewer)->ops->destroy = PetscViewerDestroy_ASCII;
8465c6c1daeSBarry Smith   ierr                       = PetscViewerDestroy(outviewer);CHKERRQ(ierr);
8475c6c1daeSBarry Smith   PetscFunctionReturn(0);
8485c6c1daeSBarry Smith }
8495c6c1daeSBarry Smith 
8505c6c1daeSBarry Smith #undef __FUNCT__
8512bf49c77SBarry Smith #define __FUNCT__ "PetscViewerView_ASCII"
8522bf49c77SBarry Smith PetscErrorCode  PetscViewerView_ASCII(PetscViewer v,PetscViewer viewer)
8532bf49c77SBarry Smith {
8542bf49c77SBarry Smith   PetscErrorCode    ierr;
8552bf49c77SBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII*)v->data;
8562bf49c77SBarry Smith 
8572bf49c77SBarry Smith   PetscFunctionBegin;
8582bf49c77SBarry Smith   if (ascii->filename) {
8592bf49c77SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"Filename: %s\n",ascii->filename);CHKERRQ(ierr);
8602bf49c77SBarry Smith   }
8612bf49c77SBarry Smith   PetscFunctionReturn(0);
8622bf49c77SBarry Smith }
8632bf49c77SBarry Smith 
8645c6c1daeSBarry Smith #undef __FUNCT__
8655c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerCreate_ASCII"
8668cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscViewerCreate_ASCII(PetscViewer viewer)
8675c6c1daeSBarry Smith {
8685c6c1daeSBarry Smith   PetscViewer_ASCII *vascii;
8695c6c1daeSBarry Smith   PetscErrorCode    ierr;
8705c6c1daeSBarry Smith 
8715c6c1daeSBarry Smith   PetscFunctionBegin;
872b00a9115SJed Brown   ierr         = PetscNewLog(viewer,&vascii);CHKERRQ(ierr);
8735c6c1daeSBarry Smith   viewer->data = (void*)vascii;
8745c6c1daeSBarry Smith 
8755c6c1daeSBarry Smith   viewer->ops->destroy          = PetscViewerDestroy_ASCII;
8765c6c1daeSBarry Smith   viewer->ops->flush            = PetscViewerFlush_ASCII;
877559f443fSBarry Smith   viewer->ops->getsubviewer     = PetscViewerGetSubViewer_ASCII;
878559f443fSBarry Smith   viewer->ops->restoresubviewer = PetscViewerRestoreSubViewer_ASCII;
8792bf49c77SBarry Smith   viewer->ops->view             = PetscViewerView_ASCII;
8801d641e7bSMichael Lange   viewer->ops->read             = PetscViewerASCIIRead;
8815c6c1daeSBarry Smith 
8825c6c1daeSBarry Smith   /* defaults to stdout unless set with PetscViewerFileSetName() */
8835c6c1daeSBarry Smith   vascii->fd        = PETSC_STDOUT;
8845c6c1daeSBarry Smith   vascii->mode      = FILE_MODE_WRITE;
8855c6c1daeSBarry Smith   vascii->bviewer   = 0;
886dd2fa690SBarry Smith   vascii->subviewer = 0;
8875c6c1daeSBarry Smith   vascii->sviewer   = 0;
8885c6c1daeSBarry Smith   vascii->tab       = 0;
8895c6c1daeSBarry Smith   vascii->tab_store = 0;
8905c6c1daeSBarry Smith   vascii->filename  = 0;
8915c6c1daeSBarry Smith   vascii->closefile = PETSC_TRUE;
8925c6c1daeSBarry Smith 
893bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetName_C",PetscViewerFileSetName_ASCII);CHKERRQ(ierr);
894bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileGetName_C",PetscViewerFileGetName_ASCII);CHKERRQ(ierr);
895bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileGetMode_C",PetscViewerFileGetMode_ASCII);CHKERRQ(ierr);
896bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)viewer,"PetscViewerFileSetMode_C",PetscViewerFileSetMode_ASCII);CHKERRQ(ierr);
8975c6c1daeSBarry Smith   PetscFunctionReturn(0);
8985c6c1daeSBarry Smith }
8995c6c1daeSBarry Smith 
9005c6c1daeSBarry Smith #undef __FUNCT__
9015c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerASCIISynchronizedPrintf"
9025c6c1daeSBarry Smith /*@C
9035c6c1daeSBarry Smith     PetscViewerASCIISynchronizedPrintf - Prints synchronized output to the specified file from
9045c6c1daeSBarry Smith     several processors.  Output of the first processor is followed by that of the
9055c6c1daeSBarry Smith     second, etc.
9065c6c1daeSBarry Smith 
9075c6c1daeSBarry Smith     Not Collective, must call collective PetscViewerFlush() to get the results out
9085c6c1daeSBarry Smith 
9095c6c1daeSBarry Smith     Input Parameters:
9105c6c1daeSBarry Smith +   viewer - the ASCII PetscViewer
9115c6c1daeSBarry Smith -   format - the usual printf() format string
9125c6c1daeSBarry Smith 
9135c6c1daeSBarry Smith     Level: intermediate
9145c6c1daeSBarry Smith 
9155c6c1daeSBarry Smith     Notes: You must have previously called PetscViewerASCIISynchronizeAllow() to allow this routine to be called.
9165c6c1daeSBarry Smith 
9175c6c1daeSBarry Smith     Fortran Note:
9185c6c1daeSBarry Smith       Can only print a single character* string
9195c6c1daeSBarry Smith 
9205c6c1daeSBarry Smith .seealso: PetscSynchronizedPrintf(), PetscSynchronizedFlush(), PetscFPrintf(),
9215c6c1daeSBarry Smith           PetscFOpen(), PetscViewerFlush(), PetscViewerASCIIGetPointer(), PetscViewerDestroy(), PetscViewerASCIIOpen(),
9221575c14dSBarry Smith           PetscViewerASCIIPrintf(), PetscViewerASCIIPushSynchronized()
9235c6c1daeSBarry Smith 
9245c6c1daeSBarry Smith @*/
9255c6c1daeSBarry Smith PetscErrorCode  PetscViewerASCIISynchronizedPrintf(PetscViewer viewer,const char format[],...)
9265c6c1daeSBarry Smith {
9275c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
9285c6c1daeSBarry Smith   PetscErrorCode    ierr;
9293f08860eSBarry Smith   PetscMPIInt       rank;
9305c6c1daeSBarry Smith   PetscInt          tab = vascii->tab;
9315c6c1daeSBarry Smith   MPI_Comm          comm;
9325c6c1daeSBarry Smith   FILE              *fp;
933559f443fSBarry Smith   PetscBool         iascii,hasbviewer = PETSC_FALSE;
9345c6c1daeSBarry Smith   int               err;
9355c6c1daeSBarry Smith 
9365c6c1daeSBarry Smith   PetscFunctionBegin;
9375c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
9385c6c1daeSBarry Smith   PetscValidCharPointer(format,2);
9395c6c1daeSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
9405c6c1daeSBarry Smith   if (!iascii) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Not ASCII PetscViewer");
9411575c14dSBarry Smith   if (!vascii->allowsynchronized) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"First call PetscViewerASCIIPushSynchronized() to allow this call");
9425c6c1daeSBarry Smith 
943ce94432eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
9445c6c1daeSBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
9455c6c1daeSBarry Smith 
946559f443fSBarry Smith   if (vascii->bviewer) {
947559f443fSBarry Smith     hasbviewer = PETSC_TRUE;
948559f443fSBarry Smith     if (!rank) {
949559f443fSBarry Smith       vascii = (PetscViewer_ASCII*)vascii->bviewer->data;
950559f443fSBarry Smith       ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
951559f443fSBarry Smith       ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
952559f443fSBarry Smith     }
953559f443fSBarry Smith   }
9543f08860eSBarry Smith 
955559f443fSBarry Smith   fp   = vascii->fd;
956559f443fSBarry Smith 
957559f443fSBarry Smith   if (!rank && !hasbviewer) {   /* First processor prints immediately to fp */
9585c6c1daeSBarry Smith     va_list Argp;
959559f443fSBarry Smith     /* flush my own messages that I may have queued up */
960559f443fSBarry Smith     PrintfQueue next = vascii->petsc_printfqueuebase,previous;
961559f443fSBarry Smith     PetscInt    i;
962559f443fSBarry Smith     for (i=0; i<vascii->petsc_printfqueuelength; i++) {
963559f443fSBarry Smith       ierr = PetscFPrintf(comm,fp,"%s",next->string);CHKERRQ(ierr);
964559f443fSBarry Smith       previous = next;
965559f443fSBarry Smith       next     = next->next;
966559f443fSBarry Smith       ierr     = PetscFree(previous->string);CHKERRQ(ierr);
967559f443fSBarry Smith       ierr     = PetscFree(previous);CHKERRQ(ierr);
968559f443fSBarry Smith     }
969559f443fSBarry Smith     vascii->petsc_printfqueue       = 0;
970559f443fSBarry Smith     vascii->petsc_printfqueuelength = 0;
9715c6c1daeSBarry Smith 
972a297a907SKarl Rupp     while (tab--) {
973a297a907SKarl Rupp       ierr = PetscFPrintf(PETSC_COMM_SELF,fp,"  ");CHKERRQ(ierr);
974a297a907SKarl Rupp     }
9755c6c1daeSBarry Smith 
9765c6c1daeSBarry Smith     va_start(Argp,format);
9775c6c1daeSBarry Smith     ierr = (*PetscVFPrintf)(fp,format,Argp);CHKERRQ(ierr);
9785c6c1daeSBarry Smith     err  = fflush(fp);
9795c6c1daeSBarry Smith     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
9805c6c1daeSBarry Smith     if (petsc_history) {
9815c6c1daeSBarry Smith       va_start(Argp,format);
9825c6c1daeSBarry Smith       ierr = (*PetscVFPrintf)(petsc_history,format,Argp);CHKERRQ(ierr);
9835c6c1daeSBarry Smith       err  = fflush(petsc_history);
9845c6c1daeSBarry Smith       if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
9855c6c1daeSBarry Smith     }
9865c6c1daeSBarry Smith     va_end(Argp);
987559f443fSBarry Smith   } else { /* other processors add to queue */
9885c6c1daeSBarry Smith     char        *string;
9895c6c1daeSBarry Smith     va_list     Argp;
9905c6c1daeSBarry Smith     size_t      fullLength;
9915c6c1daeSBarry Smith     PrintfQueue next;
9925c6c1daeSBarry Smith 
993b00a9115SJed Brown     ierr = PetscNew(&next);CHKERRQ(ierr);
994559f443fSBarry Smith     if (vascii->petsc_printfqueue) {
995559f443fSBarry Smith       vascii->petsc_printfqueue->next = next;
996559f443fSBarry Smith       vascii->petsc_printfqueue       = next;
997a297a907SKarl Rupp     } else {
998559f443fSBarry Smith       vascii->petsc_printfqueuebase = vascii->petsc_printfqueue = next;
999a297a907SKarl Rupp     }
1000559f443fSBarry Smith     vascii->petsc_printfqueuelength++;
10015c6c1daeSBarry Smith     next->size = QUEUESTRINGSIZE;
1002785e854fSJed Brown     ierr       = PetscMalloc1(next->size, &next->string);CHKERRQ(ierr);
10035c6c1daeSBarry Smith     ierr       = PetscMemzero(next->string,next->size);CHKERRQ(ierr);
10045c6c1daeSBarry Smith     string     = next->string;
10055c6c1daeSBarry Smith     tab       *= 2;
1006a297a907SKarl Rupp     while (tab--) {
1007a297a907SKarl Rupp       *string++ = ' ';
1008a297a907SKarl Rupp     }
10095c6c1daeSBarry Smith     va_start(Argp,format);
101022d28d08SBarry Smith     ierr = PetscVSNPrintf(string,next->size-2*vascii->tab,format,&fullLength,Argp);CHKERRQ(ierr);
10115c6c1daeSBarry Smith     va_end(Argp);
10125c6c1daeSBarry Smith   }
10135c6c1daeSBarry Smith   PetscFunctionReturn(0);
10145c6c1daeSBarry Smith }
10155c6c1daeSBarry Smith 
10162655f987SMichael Lange #undef __FUNCT__
10172655f987SMichael Lange #define __FUNCT__ "PetscViewerASCIIRead"
10182655f987SMichael Lange /*@C
10192655f987SMichael Lange    PetscViewerASCIIRead - Reads from am ASCII file
10202655f987SMichael Lange 
10212655f987SMichael Lange    Collective on MPI_Comm
10222655f987SMichael Lange 
10232655f987SMichael Lange    Input Parameters:
10242655f987SMichael Lange +  viewer - the ascii viewer
10252655f987SMichael Lange .  data - location to write the data
1026060da220SMatthew G. Knepley .  num - number of items of data to read
10272655f987SMichael Lange -  datatype - type of data to read
10282655f987SMichael Lange 
1029f8e4bde8SMatthew G. Knepley    Output Parameters:
1030060da220SMatthew G. Knepley .  count - number of items of data actually read, or NULL
1031f8e4bde8SMatthew G. Knepley 
10322655f987SMichael Lange    Level: beginner
10332655f987SMichael Lange 
10342655f987SMichael Lange    Concepts: ascii files
10352655f987SMichael Lange 
10366a9046bcSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(),
10372655f987SMichael Lange           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
10382655f987SMichael Lange           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead()
10392655f987SMichael Lange @*/
1040060da220SMatthew G. Knepley PetscErrorCode PetscViewerASCIIRead(PetscViewer viewer,void *data,PetscInt num,PetscInt *count,PetscDataType dtype)
10412655f987SMichael Lange {
10422655f987SMichael Lange   PetscViewer_ASCII *vascii = (PetscViewer_ASCII*)viewer->data;
10432655f987SMichael Lange   FILE              *fd = vascii->fd;
10442655f987SMichael Lange   PetscInt           i;
10453b7fe8c3SMatthew G. Knepley   int                ret = 0;
10462655f987SMichael Lange 
10472655f987SMichael Lange   PetscFunctionBegin;
10482655f987SMichael Lange   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
1049060da220SMatthew G. Knepley   for (i=0; i<num; i++) {
1050f8e4bde8SMatthew G. Knepley     if (dtype == PETSC_CHAR)         ret = fscanf(fd, "%c",  &(((char*)data)[i]));
1051f8e4bde8SMatthew G. Knepley     else if (dtype == PETSC_STRING)  ret = fscanf(fd, "%s",  &(((char*)data)[i]));
1052b9eae255SMichael Lange #if PETSC_USE_64BIT_INDICES
10530a3e67a1SMichael Lange #if (PETSC_SIZEOF_LONG_LONG == 8)
1054f8e4bde8SMatthew G. Knepley     else if (dtype == PETSC_INT)     ret = fscanf(fd, "%ld",  &(((PetscInt*)data)[i]));
10550a3e67a1SMichael Lange #else
1056f8e4bde8SMatthew G. Knepley     else if (dtype == PETSC_INT)     ret = fscanf(fd, "%lld",  &(((PetscInt*)data)[i]));
10570a3e67a1SMichael Lange #endif
1058b9eae255SMichael Lange #else
1059f8e4bde8SMatthew G. Knepley     else if (dtype == PETSC_INT)     ret = fscanf(fd, "%d",  &(((PetscInt*)data)[i]));
1060b9eae255SMichael Lange #endif
1061f8e4bde8SMatthew G. Knepley     else if (dtype == PETSC_ENUM)    ret = fscanf(fd, "%d",  &(((int*)data)[i]));
1062f8e4bde8SMatthew G. Knepley     else if (dtype == PETSC_FLOAT)   ret = fscanf(fd, "%f",  &(((float*)data)[i]));
1063f8e4bde8SMatthew G. Knepley     else if (dtype == PETSC_DOUBLE)  ret = fscanf(fd, "%lg", &(((double*)data)[i]));
1064f8e4bde8SMatthew G. Knepley     else {SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Data type %d not supported", (int) dtype);}
1065f8e4bde8SMatthew G. Knepley     if (!ret) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Conversion error for data type %d", (int) dtype);
1066f8e4bde8SMatthew G. Knepley     else if (ret < 0) break; /* Proxy for EOF, need to check for it in configure */
10672655f987SMichael Lange   }
1068060da220SMatthew G. Knepley   if (count) *count = i;
1069060da220SMatthew G. Knepley   else if (ret < 0) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Insufficient data, read only %D < %D items", i, num);
10702655f987SMichael Lange   PetscFunctionReturn(0);
10712655f987SMichael Lange }
10725c6c1daeSBarry Smith 
1073