xref: /petsc/src/sys/classes/viewer/impls/ascii/filev.c (revision c410d8cc8908fa7552d5d2cadc5c41ff54401973)
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 
6d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscViewerFileClose_ASCII(PetscViewer viewer)
7d71ae5a4SJacob Faibussowitsch {
85c6c1daeSBarry Smith   PetscMPIInt        rank;
95c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data;
105c6c1daeSBarry Smith   int                err;
115c6c1daeSBarry Smith 
125c6c1daeSBarry Smith   PetscFunctionBegin;
1328b400f6SJacob Faibussowitsch   PetscCheck(!vascii->sviewer, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONGSTATE, "Cannot call with outstanding call to PetscViewerRestoreSubViewer()");
149566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank));
15dd400576SPatrick Sanan   if (rank == 0 && vascii->fd != stderr && vascii->fd != PETSC_STDOUT) {
165c6c1daeSBarry Smith     if (vascii->fd && vascii->closefile) {
175c6c1daeSBarry Smith       err = fclose(vascii->fd);
1828b400f6SJacob Faibussowitsch       PetscCheck(!err, PETSC_COMM_SELF, PETSC_ERR_SYS, "fclose() failed on file");
195c6c1daeSBarry Smith     }
205c6c1daeSBarry Smith     if (vascii->storecompressed) {
215c6c1daeSBarry Smith       char  par[PETSC_MAX_PATH_LEN], buf[PETSC_MAX_PATH_LEN];
225c6c1daeSBarry Smith       FILE *fp;
239566063dSJacob Faibussowitsch       PetscCall(PetscStrncpy(par, "gzip ", sizeof(par)));
249566063dSJacob Faibussowitsch       PetscCall(PetscStrlcat(par, vascii->filename, sizeof(par)));
255c6c1daeSBarry Smith #if defined(PETSC_HAVE_POPEN)
269566063dSJacob Faibussowitsch       PetscCall(PetscPOpen(PETSC_COMM_SELF, NULL, par, "r", &fp));
27cc73adaaSBarry Smith       PetscCheck(!fgets(buf, 1024, fp), PETSC_COMM_SELF, PETSC_ERR_LIB, "Error from compression command %s\n%s", par, buf);
289566063dSJacob Faibussowitsch       PetscCall(PetscPClose(PETSC_COMM_SELF, fp));
295c6c1daeSBarry Smith #else
305c6c1daeSBarry Smith       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP_SYS, "Cannot run external programs on this machine");
315c6c1daeSBarry Smith #endif
325c6c1daeSBarry Smith     }
335c6c1daeSBarry Smith   }
349566063dSJacob Faibussowitsch   PetscCall(PetscFree(vascii->filename));
353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
365c6c1daeSBarry Smith }
375c6c1daeSBarry Smith 
385c6c1daeSBarry Smith /* ----------------------------------------------------------------------*/
39d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerDestroy_ASCII(PetscViewer viewer)
40d71ae5a4SJacob Faibussowitsch {
415c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data;
425c6c1daeSBarry Smith   PetscViewerLink   *vlink;
435c6c1daeSBarry Smith   PetscBool          flg;
445c6c1daeSBarry Smith 
455c6c1daeSBarry Smith   PetscFunctionBegin;
4628b400f6SJacob Faibussowitsch   PetscCheck(!vascii->sviewer, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONGSTATE, "Cannot call with outstanding call to PetscViewerRestoreSubViewer()");
479566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileClose_ASCII(viewer));
489566063dSJacob Faibussowitsch   PetscCall(PetscFree(vascii));
495c6c1daeSBarry Smith 
505c6c1daeSBarry Smith   /* remove the viewer from the list in the MPI Communicator */
5148a46eb9SPierre Jolivet   if (Petsc_Viewer_keyval == MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN, Petsc_DelViewer, &Petsc_Viewer_keyval, (void *)0));
525c6c1daeSBarry Smith 
539566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_get_attr(PetscObjectComm((PetscObject)viewer), Petsc_Viewer_keyval, (void **)&vlink, (PetscMPIInt *)&flg));
545c6c1daeSBarry Smith   if (flg) {
555c6c1daeSBarry Smith     if (vlink && vlink->viewer == viewer) {
56e5840a18SBarry Smith       if (vlink->next) {
579566063dSJacob Faibussowitsch         PetscCallMPI(MPI_Comm_set_attr(PetscObjectComm((PetscObject)viewer), Petsc_Viewer_keyval, vlink->next));
58e5840a18SBarry Smith       } else {
599566063dSJacob Faibussowitsch         PetscCallMPI(MPI_Comm_delete_attr(PetscObjectComm((PetscObject)viewer), Petsc_Viewer_keyval));
60e5840a18SBarry Smith       }
619566063dSJacob Faibussowitsch       PetscCall(PetscFree(vlink));
625c6c1daeSBarry Smith     } else {
635c6c1daeSBarry Smith       while (vlink && vlink->next) {
645c6c1daeSBarry Smith         if (vlink->next->viewer == viewer) {
655c6c1daeSBarry Smith           PetscViewerLink *nv = vlink->next;
665c6c1daeSBarry Smith           vlink->next         = vlink->next->next;
679566063dSJacob Faibussowitsch           PetscCall(PetscFree(nv));
685c6c1daeSBarry Smith         }
695c6c1daeSBarry Smith         vlink = vlink->next;
705c6c1daeSBarry Smith       }
715c6c1daeSBarry Smith     }
725c6c1daeSBarry Smith   }
73aa139df6SJed Brown 
74aa139df6SJed Brown   if (Petsc_Viewer_Stdout_keyval != MPI_KEYVAL_INVALID) {
75aa139df6SJed Brown     PetscViewer aviewer;
769566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_get_attr(PetscObjectComm((PetscObject)viewer), Petsc_Viewer_Stdout_keyval, (void **)&aviewer, (PetscMPIInt *)&flg));
7748a46eb9SPierre Jolivet     if (flg && aviewer == viewer) PetscCallMPI(MPI_Comm_delete_attr(PetscObjectComm((PetscObject)viewer), Petsc_Viewer_Stdout_keyval));
78aa139df6SJed Brown   }
79aa139df6SJed Brown   if (Petsc_Viewer_Stderr_keyval != MPI_KEYVAL_INVALID) {
80aa139df6SJed Brown     PetscViewer aviewer;
819566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_get_attr(PetscObjectComm((PetscObject)viewer), Petsc_Viewer_Stderr_keyval, (void **)&aviewer, (PetscMPIInt *)&flg));
8248a46eb9SPierre Jolivet     if (flg && aviewer == viewer) PetscCallMPI(MPI_Comm_delete_attr(PetscObjectComm((PetscObject)viewer), Petsc_Viewer_Stderr_keyval));
83aa139df6SJed Brown   }
842e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetName_C", NULL));
852e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetName_C", NULL));
862e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", NULL));
872e956fe4SStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetMode_C", NULL));
883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
895c6c1daeSBarry Smith }
905c6c1daeSBarry Smith 
91d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerDestroy_ASCII_SubViewer(PetscViewer viewer)
92d71ae5a4SJacob Faibussowitsch {
935c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data;
945fd66863SKarl Rupp 
955c6c1daeSBarry Smith   PetscFunctionBegin;
969566063dSJacob Faibussowitsch   PetscCall(PetscViewerRestoreSubViewer(vascii->bviewer, 0, &viewer));
973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
985c6c1daeSBarry Smith }
995c6c1daeSBarry Smith 
100d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerFlush_ASCII(PetscViewer viewer)
101d71ae5a4SJacob Faibussowitsch {
1025c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data;
103559f443fSBarry Smith   MPI_Comm           comm;
104559f443fSBarry Smith   PetscMPIInt        rank, size;
105559f443fSBarry Smith   FILE              *fd = vascii->fd;
1065c6c1daeSBarry Smith 
1075c6c1daeSBarry Smith   PetscFunctionBegin;
10828b400f6SJacob Faibussowitsch   PetscCheck(!vascii->sviewer, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONGSTATE, "Cannot call with outstanding call to PetscViewerRestoreSubViewer()");
1099566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)viewer, &comm));
1109566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
1119566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
112559f443fSBarry Smith 
113c69effb2SJacob Faibussowitsch   if (!vascii->bviewer && rank == 0 && (vascii->mode != FILE_MODE_READ)) PetscCall(PetscFFlush(vascii->fd));
1145c6c1daeSBarry Smith 
1155c6c1daeSBarry Smith   if (vascii->allowsynchronized) {
116559f443fSBarry Smith     PetscMPIInt tag, i, j, n = 0, dummy = 0;
117559f443fSBarry Smith     char       *message;
118559f443fSBarry Smith     MPI_Status  status;
119559f443fSBarry Smith 
1209566063dSJacob Faibussowitsch     PetscCall(PetscCommDuplicate(comm, &comm, &tag));
121559f443fSBarry Smith 
122559f443fSBarry Smith     /* First processor waits for messages from all other processors */
123dd400576SPatrick Sanan     if (rank == 0) {
124559f443fSBarry Smith       /* flush my own messages that I may have queued up */
125559f443fSBarry Smith       PrintfQueue next = vascii->petsc_printfqueuebase, previous;
126559f443fSBarry Smith       for (i = 0; i < vascii->petsc_printfqueuelength; i++) {
127559f443fSBarry Smith         if (!vascii->bviewer) {
1289566063dSJacob Faibussowitsch           PetscCall(PetscFPrintf(comm, fd, "%s", next->string));
129559f443fSBarry Smith         } else {
1309566063dSJacob Faibussowitsch           PetscCall(PetscViewerASCIISynchronizedPrintf(vascii->bviewer, "%s", next->string));
131559f443fSBarry Smith         }
132559f443fSBarry Smith         previous = next;
133559f443fSBarry Smith         next     = next->next;
1349566063dSJacob Faibussowitsch         PetscCall(PetscFree(previous->string));
1359566063dSJacob Faibussowitsch         PetscCall(PetscFree(previous));
136559f443fSBarry Smith       }
13702c9f0b5SLisandro Dalcin       vascii->petsc_printfqueue       = NULL;
138559f443fSBarry Smith       vascii->petsc_printfqueuelength = 0;
139559f443fSBarry Smith       for (i = 1; i < size; i++) {
140559f443fSBarry Smith         /* to prevent a flood of messages to process zero, request each message separately */
1419566063dSJacob Faibussowitsch         PetscCallMPI(MPI_Send(&dummy, 1, MPI_INT, i, tag, comm));
1429566063dSJacob Faibussowitsch         PetscCallMPI(MPI_Recv(&n, 1, MPI_INT, i, tag, comm, &status));
143559f443fSBarry Smith         for (j = 0; j < n; j++) {
144559f443fSBarry Smith           PetscMPIInt size = 0;
145559f443fSBarry Smith 
1469566063dSJacob Faibussowitsch           PetscCallMPI(MPI_Recv(&size, 1, MPI_INT, i, tag, comm, &status));
1479566063dSJacob Faibussowitsch           PetscCall(PetscMalloc1(size, &message));
1489566063dSJacob Faibussowitsch           PetscCallMPI(MPI_Recv(message, size, MPI_CHAR, i, tag, comm, &status));
149559f443fSBarry Smith           if (!vascii->bviewer) {
1509566063dSJacob Faibussowitsch             PetscCall(PetscFPrintf(comm, fd, "%s", message));
151559f443fSBarry Smith           } else {
1529566063dSJacob Faibussowitsch             PetscCall(PetscViewerASCIISynchronizedPrintf(vascii->bviewer, "%s", message));
153559f443fSBarry Smith           }
1549566063dSJacob Faibussowitsch           PetscCall(PetscFree(message));
155559f443fSBarry Smith         }
156559f443fSBarry Smith       }
157559f443fSBarry Smith     } else { /* other processors send queue to processor 0 */
158559f443fSBarry Smith       PrintfQueue next = vascii->petsc_printfqueuebase, previous;
159559f443fSBarry Smith 
1609566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Recv(&dummy, 1, MPI_INT, 0, tag, comm, &status));
1619566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Send(&vascii->petsc_printfqueuelength, 1, MPI_INT, 0, tag, comm));
162559f443fSBarry Smith       for (i = 0; i < vascii->petsc_printfqueuelength; i++) {
1639566063dSJacob Faibussowitsch         PetscCallMPI(MPI_Send(&next->size, 1, MPI_INT, 0, tag, comm));
1649566063dSJacob Faibussowitsch         PetscCallMPI(MPI_Send(next->string, next->size, MPI_CHAR, 0, tag, comm));
165559f443fSBarry Smith         previous = next;
166559f443fSBarry Smith         next     = next->next;
1679566063dSJacob Faibussowitsch         PetscCall(PetscFree(previous->string));
1689566063dSJacob Faibussowitsch         PetscCall(PetscFree(previous));
169559f443fSBarry Smith       }
17002c9f0b5SLisandro Dalcin       vascii->petsc_printfqueue       = NULL;
171559f443fSBarry Smith       vascii->petsc_printfqueuelength = 0;
172559f443fSBarry Smith     }
1739566063dSJacob Faibussowitsch     PetscCall(PetscCommDestroy(&comm));
1745c6c1daeSBarry Smith   }
1753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1765c6c1daeSBarry Smith }
1775c6c1daeSBarry Smith 
1785c6c1daeSBarry Smith /*@C
179811af0c4SBarry Smith     PetscViewerASCIIGetPointer - Extracts the file pointer from an ASCII `PetscViewer`.
1805c6c1daeSBarry Smith 
18135cb6cd3SPierre Jolivet     Not Collective, depending on the viewer the value may be meaningless except for process 0 of the viewer; No Fortran Support
1825c6c1daeSBarry Smith 
183f8859db6SBarry Smith     Input Parameter:
1843f423023SBarry Smith .    viewer - `PetscViewer` context, obtained from `PetscViewerASCIIOpen()`
185f8859db6SBarry Smith 
186f8859db6SBarry Smith     Output Parameter:
187f8859db6SBarry Smith .    fd - file pointer
188f8859db6SBarry Smith 
1895c6c1daeSBarry Smith     Level: intermediate
1905c6c1daeSBarry Smith 
191811af0c4SBarry Smith     Note:
192*c410d8ccSBarry Smith     For the standard `PETSCVIEWERASCII` the value is valid only on MPI rank 0 of the viewer
193811af0c4SBarry Smith 
1943f423023SBarry Smith .seealso: [](sec_viewers), `PETSCVIEWERASCII`, `PetscViewerASCIIOpen()`, `PetscViewerDestroy()`, `PetscViewerSetType()`,
1953f423023SBarry Smith           `PetscViewerCreate()`, `PetscViewerASCIIPrintf()`, `PetscViewerASCIISynchronizedPrintf()`, `PetscViewerFlush()`
1965c6c1daeSBarry Smith @*/
197d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIIGetPointer(PetscViewer viewer, FILE **fd)
198d71ae5a4SJacob Faibussowitsch {
1995c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data;
2005c6c1daeSBarry Smith 
2015c6c1daeSBarry Smith   PetscFunctionBegin;
2025c6c1daeSBarry Smith   *fd = vascii->fd;
2033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2045c6c1daeSBarry Smith }
2055c6c1daeSBarry Smith 
206d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerFileGetMode_ASCII(PetscViewer viewer, PetscFileMode *mode)
207d71ae5a4SJacob Faibussowitsch {
2085c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data;
2095c6c1daeSBarry Smith 
2105c6c1daeSBarry Smith   PetscFunctionBegin;
2115c6c1daeSBarry Smith   *mode = vascii->mode;
2123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2135c6c1daeSBarry Smith }
2145c6c1daeSBarry Smith 
215d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerFileSetMode_ASCII(PetscViewer viewer, PetscFileMode mode)
216d71ae5a4SJacob Faibussowitsch {
2175c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data;
2185c6c1daeSBarry Smith 
2195c6c1daeSBarry Smith   PetscFunctionBegin;
2205c6c1daeSBarry Smith   vascii->mode = mode;
2213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2225c6c1daeSBarry Smith }
2235c6c1daeSBarry Smith 
2245c6c1daeSBarry Smith /*
2255c6c1daeSBarry Smith    If petsc_history is on, then all Petsc*Printf() results are saved
2265c6c1daeSBarry Smith    if the appropriate (usually .petschistory) file.
2275c6c1daeSBarry Smith */
22895c0884eSLisandro Dalcin PETSC_INTERN FILE *petsc_history;
2295c6c1daeSBarry Smith 
2305c6c1daeSBarry Smith /*@
2313f423023SBarry Smith     PetscViewerASCIISetTab - Causes `PetscViewer` to tab in a number of times before printing
2325c6c1daeSBarry Smith 
233cf53795eSBarry Smith     Not Collective, but only first processor in set has any effect; No Fortran Support
2345c6c1daeSBarry Smith 
2355c6c1daeSBarry Smith     Input Parameters:
236811af0c4SBarry Smith +    viewer - obtained with `PetscViewerASCIIOpen()`
2375c6c1daeSBarry Smith -    tabs - number of tabs
2385c6c1daeSBarry Smith 
2395c6c1daeSBarry Smith     Level: developer
2405c6c1daeSBarry Smith 
2413f423023SBarry Smith     Note:
2423f423023SBarry Smith      `PetscViewerASCIIPushTab()` and `PetscViewerASCIIPopTab()` are the preferred usage
2433f423023SBarry Smith 
2443f423023SBarry Smith .seealso: [](sec_viewers), `PETSCVIEWERASCII`, `PetscPrintf()`, `PetscSynchronizedPrintf()`, `PetscViewerASCIIPrintf()`,
2453f423023SBarry Smith           `PetscViewerASCIIGetTab()`,
246db781477SPatrick Sanan           `PetscViewerASCIIPopTab()`, `PetscViewerASCIISynchronizedPrintf()`, `PetscViewerASCIIOpen()`,
2473f423023SBarry Smith           `PetscViewerCreate()`, `PetscViewerDestroy()`, `PetscViewerSetType()`, `PetscViewerASCIIGetPointer()`,
2483f423023SBarry Smith           `PetscViewerASCIIPushTab()`
2495c6c1daeSBarry Smith @*/
250d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIISetTab(PetscViewer viewer, PetscInt tabs)
251d71ae5a4SJacob Faibussowitsch {
2525c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII *)viewer->data;
2535c6c1daeSBarry Smith   PetscBool          iascii;
2545c6c1daeSBarry Smith 
2555c6c1daeSBarry Smith   PetscFunctionBegin;
2565c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
2579566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
258a297a907SKarl Rupp   if (iascii) ascii->tab = tabs;
2593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2605c6c1daeSBarry Smith }
2615c6c1daeSBarry Smith 
2625c6c1daeSBarry Smith /*@
263811af0c4SBarry Smith     PetscViewerASCIIGetTab - Return the number of tabs used by `PetscViewer`.
2645c6c1daeSBarry Smith 
265cf53795eSBarry Smith     Not Collective, meaningful on first processor only; No Fortran Support
2665c6c1daeSBarry Smith 
26720f4b53cSBarry Smith     Input Parameter:
268811af0c4SBarry Smith .    viewer - obtained with `PetscViewerASCIIOpen()`
269a2b725a8SWilliam Gropp 
27020f4b53cSBarry Smith     Output Parameter:
2715c6c1daeSBarry Smith .    tabs - number of tabs
2725c6c1daeSBarry Smith 
2735c6c1daeSBarry Smith     Level: developer
2745c6c1daeSBarry Smith 
2753f423023SBarry Smith .seealso: [](sec_viewers), `PETSCVIEWERASCII`, `PetscPrintf()`, `PetscSynchronizedPrintf()`, `PetscViewerASCIIPrintf()`,
2763f423023SBarry Smith           `PetscViewerASCIISetTab()`,
277db781477SPatrick Sanan           `PetscViewerASCIIPopTab()`, `PetscViewerASCIISynchronizedPrintf()`, `PetscViewerASCIIOpen()`,
278db781477SPatrick Sanan           `PetscViewerCreate()`, `PetscViewerDestroy()`, `PetscViewerSetType()`, `PetscViewerASCIIGetPointer()`, `PetscViewerASCIIPushTab()`
2795c6c1daeSBarry Smith @*/
280d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIIGetTab(PetscViewer viewer, PetscInt *tabs)
281d71ae5a4SJacob Faibussowitsch {
2825c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII *)viewer->data;
2835c6c1daeSBarry Smith   PetscBool          iascii;
2845c6c1daeSBarry Smith 
2855c6c1daeSBarry Smith   PetscFunctionBegin;
2865c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
2879566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
288a297a907SKarl Rupp   if (iascii && tabs) *tabs = ascii->tab;
2893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2905c6c1daeSBarry Smith }
2915c6c1daeSBarry Smith 
2925c6c1daeSBarry Smith /*@
2933f423023SBarry Smith     PetscViewerASCIIAddTab - Add to the number of times a `PETSCVIEWERASCII` viewer tabs before printing
2945c6c1daeSBarry Smith 
295cf53795eSBarry Smith     Not Collective, but only first processor in set has any effect; No Fortran Support
2965c6c1daeSBarry Smith 
2975c6c1daeSBarry Smith     Input Parameters:
298811af0c4SBarry Smith +    viewer - obtained with `PetscViewerASCIIOpen()`
2995c6c1daeSBarry Smith -    tabs - number of tabs
3005c6c1daeSBarry Smith 
3015c6c1daeSBarry Smith     Level: developer
3025c6c1daeSBarry Smith 
3033f423023SBarry Smith     Note:
3043f423023SBarry Smith      `PetscViewerASCIIPushTab()` and `PetscViewerASCIIPopTab()` are the preferred usage
3053f423023SBarry Smith 
3063f423023SBarry Smith .seealso: [](sec_viewers), `PETSCVIEWERASCII`, `PetscPrintf()`, `PetscSynchronizedPrintf()`, `PetscViewerASCIIPrintf()`,
307db781477SPatrick Sanan           `PetscViewerASCIIPopTab()`, `PetscViewerASCIISynchronizedPrintf()`, `PetscViewerASCIIOpen()`,
308db781477SPatrick Sanan           `PetscViewerCreate()`, `PetscViewerDestroy()`, `PetscViewerSetType()`, `PetscViewerASCIIGetPointer()`, `PetscViewerASCIIPushTab()`
3095c6c1daeSBarry Smith @*/
310d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIIAddTab(PetscViewer viewer, PetscInt tabs)
311d71ae5a4SJacob Faibussowitsch {
3125c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII *)viewer->data;
3135c6c1daeSBarry Smith   PetscBool          iascii;
3145c6c1daeSBarry Smith 
3155c6c1daeSBarry Smith   PetscFunctionBegin;
3165c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
3179566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
318a297a907SKarl Rupp   if (iascii) ascii->tab += tabs;
3193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3205c6c1daeSBarry Smith }
3215c6c1daeSBarry Smith 
3225c6c1daeSBarry Smith /*@
3233f423023SBarry Smith     PetscViewerASCIISubtractTab - Subtracts from the number of times a `PETSCVIEWERASCII` viewer tabs before printing
3245c6c1daeSBarry Smith 
325cf53795eSBarry Smith     Not Collective, but only first processor in set has any effect; No Fortran Support
3265c6c1daeSBarry Smith 
3275c6c1daeSBarry Smith     Input Parameters:
328811af0c4SBarry Smith +    viewer - obtained with `PetscViewerASCIIOpen()`
3295c6c1daeSBarry Smith -    tabs - number of tabs
3305c6c1daeSBarry Smith 
3315c6c1daeSBarry Smith     Level: developer
3325c6c1daeSBarry Smith 
3333f423023SBarry Smith     Note:
3343f423023SBarry Smith      `PetscViewerASCIIPushTab()` and `PetscViewerASCIIPopTab()` are the preferred usage
3353f423023SBarry Smith 
3363f423023SBarry Smith .seealso: [](sec_viewers), `PETSCVIEWERASCII`, `PetscPrintf()`, `PetscSynchronizedPrintf()`, `PetscViewerASCIIPrintf()`,
337db781477SPatrick Sanan           `PetscViewerASCIIPopTab()`, `PetscViewerASCIISynchronizedPrintf()`, `PetscViewerASCIIOpen()`,
3383f423023SBarry Smith           `PetscViewerCreate()`, `PetscViewerDestroy()`, `PetscViewerSetType()`, `PetscViewerASCIIGetPointer()`,
3393f423023SBarry Smith           `PetscViewerASCIIPushTab()`
3405c6c1daeSBarry Smith @*/
341d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIISubtractTab(PetscViewer viewer, PetscInt tabs)
342d71ae5a4SJacob Faibussowitsch {
3435c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII *)viewer->data;
3445c6c1daeSBarry Smith   PetscBool          iascii;
3455c6c1daeSBarry Smith 
3465c6c1daeSBarry Smith   PetscFunctionBegin;
3475c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
3489566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
349a297a907SKarl Rupp   if (iascii) ascii->tab -= tabs;
3503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3515c6c1daeSBarry Smith }
3525c6c1daeSBarry Smith 
3535c6c1daeSBarry Smith /*@C
354811af0c4SBarry Smith     PetscViewerASCIIPushSynchronized - Allows calls to `PetscViewerASCIISynchronizedPrintf()` for this viewer
3555c6c1daeSBarry Smith 
356c3339decSBarry Smith     Collective
3575c6c1daeSBarry Smith 
35820f4b53cSBarry Smith     Input Parameter:
359811af0c4SBarry Smith .    viewer - obtained with `PetscViewerASCIIOpen()`
3605c6c1daeSBarry Smith 
3615c6c1daeSBarry Smith     Level: intermediate
3625c6c1daeSBarry Smith 
363811af0c4SBarry Smith     Note:
364811af0c4SBarry Smith     See documentation of `PetscViewerASCIISynchronizedPrintf()` for more details how the synchronized output should be done properly.
3655c6c1daeSBarry Smith 
366d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewerASCIISynchronizedPrintf()`, `PetscViewerFlush()`, `PetscViewerASCIIPopSynchronized()`,
367db781477SPatrick Sanan           `PetscSynchronizedPrintf()`, `PetscViewerASCIIPrintf()`, `PetscViewerASCIIOpen()`,
368db781477SPatrick Sanan           `PetscViewerCreate()`, `PetscViewerDestroy()`, `PetscViewerSetType()`
3695c6c1daeSBarry Smith @*/
370d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIIPushSynchronized(PetscViewer viewer)
371d71ae5a4SJacob Faibussowitsch {
3725c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII *)viewer->data;
3735c6c1daeSBarry Smith   PetscBool          iascii;
3745c6c1daeSBarry Smith 
3755c6c1daeSBarry Smith   PetscFunctionBegin;
3765c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
37728b400f6SJacob Faibussowitsch   PetscCheck(!ascii->sviewer, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONGSTATE, "Cannot call with outstanding call to PetscViewerRestoreSubViewer()");
3789566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
3791575c14dSBarry Smith   if (iascii) ascii->allowsynchronized++;
3803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3811575c14dSBarry Smith }
3821575c14dSBarry Smith 
3831575c14dSBarry Smith /*@C
384811af0c4SBarry Smith     PetscViewerASCIIPopSynchronized - Undoes most recent `PetscViewerASCIIPushSynchronized()` for this viewer
3851575c14dSBarry Smith 
386c3339decSBarry Smith     Collective
3871575c14dSBarry Smith 
38820f4b53cSBarry Smith     Input Parameter:
389811af0c4SBarry Smith .    viewer - obtained with `PetscViewerASCIIOpen()`
3901575c14dSBarry Smith 
3911575c14dSBarry Smith     Level: intermediate
3921575c14dSBarry Smith 
393811af0c4SBarry Smith     Note:
394811af0c4SBarry Smith     See documentation of `PetscViewerASCIISynchronizedPrintf()` for more details how the synchronized output should be done properly.
3951575c14dSBarry Smith 
396d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewerASCIIPushSynchronized()`, `PetscViewerASCIISynchronizedPrintf()`, `PetscViewerFlush()`,
397db781477SPatrick Sanan           `PetscSynchronizedPrintf()`, `PetscViewerASCIIPrintf()`, `PetscViewerASCIIOpen()`,
398db781477SPatrick Sanan           `PetscViewerCreate()`, `PetscViewerDestroy()`, `PetscViewerSetType()`
3991575c14dSBarry Smith @*/
400d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIIPopSynchronized(PetscViewer viewer)
401d71ae5a4SJacob Faibussowitsch {
4021575c14dSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII *)viewer->data;
4031575c14dSBarry Smith   PetscBool          iascii;
4041575c14dSBarry Smith 
4051575c14dSBarry Smith   PetscFunctionBegin;
4061575c14dSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
40728b400f6SJacob Faibussowitsch   PetscCheck(!ascii->sviewer, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONGSTATE, "Cannot call with outstanding call to PetscViewerRestoreSubViewer()");
4089566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
4091575c14dSBarry Smith   if (iascii) {
4101575c14dSBarry Smith     ascii->allowsynchronized--;
41108401ef6SPierre Jolivet     PetscCheck(ascii->allowsynchronized >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Called more times than PetscViewerASCIIPushSynchronized()");
4121575c14dSBarry Smith   }
4133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4145c6c1daeSBarry Smith }
4155c6c1daeSBarry Smith 
4161c297824SMatthew G. Knepley /*@C
417811af0c4SBarry Smith     PetscViewerASCIIPushTab - Adds one more tab to the amount that `PetscViewerASCIIPrintf()`
4185c6c1daeSBarry Smith      lines are tabbed.
4195c6c1daeSBarry Smith 
420*c410d8ccSBarry Smith     Not Collective, but only first MPI rank in the viewer has any effect; No Fortran Support
4215c6c1daeSBarry Smith 
42220f4b53cSBarry Smith     Input Parameter:
423811af0c4SBarry Smith .    viewer - obtained with `PetscViewerASCIIOpen()`
4245c6c1daeSBarry Smith 
4255c6c1daeSBarry Smith     Level: developer
4265c6c1daeSBarry Smith 
427d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscPrintf()`, `PetscSynchronizedPrintf()`, `PetscViewerASCIIPrintf()`,
428db781477SPatrick Sanan           `PetscViewerASCIIPopTab()`, `PetscViewerASCIISynchronizedPrintf()`, `PetscViewerASCIIOpen()`,
429db781477SPatrick Sanan           `PetscViewerCreate()`, `PetscViewerDestroy()`, `PetscViewerSetType()`, `PetscViewerASCIIGetPointer()`
4305c6c1daeSBarry Smith @*/
431d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIIPushTab(PetscViewer viewer)
432d71ae5a4SJacob Faibussowitsch {
4335c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII *)viewer->data;
4345c6c1daeSBarry Smith   PetscBool          iascii;
4355c6c1daeSBarry Smith 
4365c6c1daeSBarry Smith   PetscFunctionBegin;
4375c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
4389566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
439a297a907SKarl Rupp   if (iascii) ascii->tab++;
4403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4415c6c1daeSBarry Smith }
4425c6c1daeSBarry Smith 
4431c297824SMatthew G. Knepley /*@C
4443f423023SBarry Smith     PetscViewerASCIIPopTab - Removes one tab from the amount that `PetscViewerASCIIPrintf()` lines are tabbed that was provided by
4453f423023SBarry Smith     `PetscViewerASCIIPushTab()`
4465c6c1daeSBarry Smith 
447*c410d8ccSBarry Smith     Not Collective, but only first MPI rank in the viewer has any effect; No Fortran Support
4485c6c1daeSBarry Smith 
44920f4b53cSBarry Smith     Input Parameter:
450811af0c4SBarry Smith .    viewer - obtained with `PetscViewerASCIIOpen()`
4515c6c1daeSBarry Smith 
4525c6c1daeSBarry Smith     Level: developer
4535c6c1daeSBarry Smith 
454d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscPrintf()`, `PetscSynchronizedPrintf()`, `PetscViewerASCIIPrintf()`,
455db781477SPatrick Sanan           `PetscViewerASCIIPushTab()`, `PetscViewerASCIISynchronizedPrintf()`, `PetscViewerASCIIOpen()`,
456db781477SPatrick Sanan           `PetscViewerCreate()`, `PetscViewerDestroy()`, `PetscViewerSetType()`, `PetscViewerASCIIGetPointer()`
4575c6c1daeSBarry Smith @*/
458d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIIPopTab(PetscViewer viewer)
459d71ae5a4SJacob Faibussowitsch {
4605c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII *)viewer->data;
4615c6c1daeSBarry Smith   PetscBool          iascii;
4625c6c1daeSBarry Smith 
4635c6c1daeSBarry Smith   PetscFunctionBegin;
4645c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
4659566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
4665c6c1daeSBarry Smith   if (iascii) {
46708401ef6SPierre Jolivet     PetscCheck(ascii->tab > 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "More tabs popped than pushed");
4685c6c1daeSBarry Smith     ascii->tab--;
4695c6c1daeSBarry Smith   }
4703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4715c6c1daeSBarry Smith }
4725c6c1daeSBarry Smith 
4735c6c1daeSBarry Smith /*@
474*c410d8ccSBarry Smith     PetscViewerASCIIUseTabs - Turns on or off the use of tabs with the `PETSCVIEWERASCII` `PetscViewer`
4755c6c1daeSBarry Smith 
476*c410d8ccSBarry Smith     Not Collective, but only first MPI rank in the viewer has any effect; No Fortran Support
4775c6c1daeSBarry Smith 
4785c6c1daeSBarry Smith     Input Parameters:
479811af0c4SBarry Smith +    viewer - obtained with `PetscViewerASCIIOpen()`
480811af0c4SBarry Smith -    flg - `PETSC_TRUE` or `PETSC_FALSE`
4815c6c1daeSBarry Smith 
4825c6c1daeSBarry Smith     Level: developer
4835c6c1daeSBarry Smith 
484d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscPrintf()`, `PetscSynchronizedPrintf()`, `PetscViewerASCIIPrintf()`,
485db781477SPatrick Sanan           `PetscViewerASCIIPopTab()`, `PetscViewerASCIISynchronizedPrintf()`, `PetscViewerASCIIPushTab()`, `PetscViewerASCIIOpen()`,
486db781477SPatrick Sanan           `PetscViewerCreate()`, `PetscViewerDestroy()`, `PetscViewerSetType()`, `PetscViewerASCIIGetPointer()`
4875c6c1daeSBarry Smith @*/
488d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIIUseTabs(PetscViewer viewer, PetscBool flg)
489d71ae5a4SJacob Faibussowitsch {
4905c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII *)viewer->data;
4915c6c1daeSBarry Smith   PetscBool          iascii;
4925c6c1daeSBarry Smith 
4935c6c1daeSBarry Smith   PetscFunctionBegin;
4945c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
4959566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
4965c6c1daeSBarry Smith   if (iascii) {
497a297a907SKarl Rupp     if (flg) ascii->tab = ascii->tab_store;
498a297a907SKarl Rupp     else {
4995c6c1daeSBarry Smith       ascii->tab_store = ascii->tab;
5005c6c1daeSBarry Smith       ascii->tab       = 0;
5015c6c1daeSBarry Smith     }
5025c6c1daeSBarry Smith   }
5033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5045c6c1daeSBarry Smith }
5055c6c1daeSBarry Smith 
5065c6c1daeSBarry Smith /*@C
5075c6c1daeSBarry Smith     PetscViewerASCIIPrintf - Prints to a file, only from the first
5083f423023SBarry Smith     processor in the `PetscViewer` of type `PETSCVIEWERASCII`
5095c6c1daeSBarry Smith 
510*c410d8ccSBarry Smith     Not Collective, but only the first MPI rank in the viewer has any effect
5115c6c1daeSBarry Smith 
5125c6c1daeSBarry Smith     Input Parameters:
513811af0c4SBarry Smith +    viewer - obtained with `PetscViewerASCIIOpen()`
5145c6c1daeSBarry Smith -    format - the usual printf() format string
5155c6c1daeSBarry Smith 
5165c6c1daeSBarry Smith     Level: developer
5175c6c1daeSBarry Smith 
5185c6c1daeSBarry Smith     Fortran Note:
519*c410d8ccSBarry Smith     The call sequence is `PetscViewerASCIIPrintf`(`PetscViewer`, character(*), int ierr) from Fortran.
5205c6c1daeSBarry Smith     That is, you can only pass a single character string from Fortran.
5215c6c1daeSBarry Smith 
522d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscPrintf()`, `PetscSynchronizedPrintf()`, `PetscViewerASCIIOpen()`,
523db781477SPatrick Sanan           `PetscViewerASCIIPushTab()`, `PetscViewerASCIIPopTab()`, `PetscViewerASCIISynchronizedPrintf()`,
524db781477SPatrick Sanan           `PetscViewerCreate()`, `PetscViewerDestroy()`, `PetscViewerSetType()`, `PetscViewerASCIIGetPointer()`, `PetscViewerASCIIPushSynchronized()`
5255c6c1daeSBarry Smith @*/
526d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIIPrintf(PetscViewer viewer, const char format[], ...)
527d71ae5a4SJacob Faibussowitsch {
5285c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII *)viewer->data;
5295c6c1daeSBarry Smith   PetscMPIInt        rank;
530dd2fa690SBarry Smith   PetscInt           tab, intab = ascii->tab;
5315c6c1daeSBarry Smith   FILE              *fd = ascii->fd;
5323f08860eSBarry Smith   PetscBool          iascii;
5335c6c1daeSBarry Smith 
5345c6c1daeSBarry Smith   PetscFunctionBegin;
5355c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
53628b400f6SJacob Faibussowitsch   PetscCheck(!ascii->sviewer, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONGSTATE, "Cannot call with outstanding call to PetscViewerRestoreSubViewer()");
5375c6c1daeSBarry Smith   PetscValidCharPointer(format, 2);
5389566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
53928b400f6SJacob Faibussowitsch   PetscCheck(iascii, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Not ASCII PetscViewer");
5409566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank));
5413ba16761SJacob Faibussowitsch   if (rank) PetscFunctionReturn(PETSC_SUCCESS);
5423f08860eSBarry Smith 
5433f08860eSBarry Smith   if (ascii->bviewer) { /* pass string up to parent viewer */
5443f08860eSBarry Smith     char   *string;
5453f08860eSBarry Smith     va_list Argp;
5463f08860eSBarry Smith     size_t  fullLength;
5473f08860eSBarry Smith 
5489566063dSJacob Faibussowitsch     PetscCall(PetscCalloc1(QUEUESTRINGSIZE, &string));
5493f08860eSBarry Smith     va_start(Argp, format);
5509566063dSJacob Faibussowitsch     PetscCall(PetscVSNPrintf(string, QUEUESTRINGSIZE, format, &fullLength, Argp));
5513f08860eSBarry Smith     va_end(Argp);
5529566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%s", string));
5539566063dSJacob Faibussowitsch     PetscCall(PetscFree(string));
5543f08860eSBarry Smith   } else { /* write directly to file */
5555c6c1daeSBarry Smith     va_list Argp;
556559f443fSBarry Smith     /* flush my own messages that I may have queued up */
557559f443fSBarry Smith     PrintfQueue next = ascii->petsc_printfqueuebase, previous;
558559f443fSBarry Smith     PetscInt    i;
559559f443fSBarry Smith     for (i = 0; i < ascii->petsc_printfqueuelength; i++) {
5609566063dSJacob Faibussowitsch       PetscCall(PetscFPrintf(PETSC_COMM_SELF, fd, "%s", next->string));
561559f443fSBarry Smith       previous = next;
562559f443fSBarry Smith       next     = next->next;
5639566063dSJacob Faibussowitsch       PetscCall(PetscFree(previous->string));
5649566063dSJacob Faibussowitsch       PetscCall(PetscFree(previous));
565559f443fSBarry Smith     }
56602c9f0b5SLisandro Dalcin     ascii->petsc_printfqueue       = NULL;
567559f443fSBarry Smith     ascii->petsc_printfqueuelength = 0;
568dd2fa690SBarry Smith     tab                            = intab;
56948a46eb9SPierre Jolivet     while (tab--) PetscCall(PetscFPrintf(PETSC_COMM_SELF, fd, "  "));
5705c6c1daeSBarry Smith 
5715c6c1daeSBarry Smith     va_start(Argp, format);
5729566063dSJacob Faibussowitsch     PetscCall((*PetscVFPrintf)(fd, format, Argp));
573eae3dc7dSJacob Faibussowitsch     va_end(Argp);
574c69effb2SJacob Faibussowitsch     PetscCall(PetscFFlush(fd));
5755c6c1daeSBarry Smith     if (petsc_history) {
576dd2fa690SBarry Smith       tab = intab;
57748a46eb9SPierre Jolivet       while (tab--) PetscCall(PetscFPrintf(PETSC_COMM_SELF, petsc_history, "  "));
578eae3dc7dSJacob Faibussowitsch       va_start(Argp, format);
5799566063dSJacob Faibussowitsch       PetscCall((*PetscVFPrintf)(petsc_history, format, Argp));
580eae3dc7dSJacob Faibussowitsch       va_end(Argp);
581c69effb2SJacob Faibussowitsch       PetscCall(PetscFFlush(petsc_history));
5825c6c1daeSBarry Smith     }
5835c6c1daeSBarry Smith   }
5843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5855c6c1daeSBarry Smith }
5865c6c1daeSBarry Smith 
5875c6c1daeSBarry Smith /*@C
588*c410d8ccSBarry Smith      PetscViewerFileSetName - Sets the name of the file the `PetscViewer` should use.
5895c6c1daeSBarry Smith 
590c3339decSBarry Smith     Collective
5915c6c1daeSBarry Smith 
5925c6c1daeSBarry Smith   Input Parameters:
5933f423023SBarry Smith +  viewer - the `PetscViewer`; for example, of type `PETSCVIEWERASCII` or `PETSCVIEWERBINARY`
5945c6c1daeSBarry Smith -  name - the name of the file it should use
5955c6c1daeSBarry Smith 
5965c6c1daeSBarry Smith     Level: advanced
5975c6c1daeSBarry Smith 
598*c410d8ccSBarry Smith   Note:
599*c410d8ccSBarry Smith   This will have no effect on viewers that are not related to files
600*c410d8ccSBarry Smith 
601d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewerCreate()`, `PetscViewerSetType()`, `PetscViewerASCIIOpen()`, `PetscViewerBinaryOpen()`, `PetscViewerDestroy()`,
602db781477SPatrick Sanan           `PetscViewerASCIIGetPointer()`, `PetscViewerASCIIPrintf()`, `PetscViewerASCIISynchronizedPrintf()`
6035c6c1daeSBarry Smith @*/
604d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerFileSetName(PetscViewer viewer, const char name[])
605d71ae5a4SJacob Faibussowitsch {
606cc843e7aSLisandro Dalcin   char filename[PETSC_MAX_PATH_LEN];
6075c6c1daeSBarry Smith 
6085c6c1daeSBarry Smith   PetscFunctionBegin;
6095c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
6105c6c1daeSBarry Smith   PetscValidCharPointer(name, 2);
6119566063dSJacob Faibussowitsch   PetscCall(PetscStrreplace(PetscObjectComm((PetscObject)viewer), name, filename, sizeof(filename)));
612cac4c232SBarry Smith   PetscTryMethod(viewer, "PetscViewerFileSetName_C", (PetscViewer, const char[]), (viewer, filename));
6133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6145c6c1daeSBarry Smith }
6155c6c1daeSBarry Smith 
6165c6c1daeSBarry Smith /*@C
617*c410d8ccSBarry Smith      PetscViewerFileGetName - Gets the name of the file the `PetscViewer` is using
6185c6c1daeSBarry Smith 
6195c6c1daeSBarry Smith     Not Collective
6205c6c1daeSBarry Smith 
6215c6c1daeSBarry Smith   Input Parameter:
6223f423023SBarry Smith .  viewer - the `PetscViewer`
6235c6c1daeSBarry Smith 
6245c6c1daeSBarry Smith   Output Parameter:
6255c6c1daeSBarry Smith .  name - the name of the file it is using
6265c6c1daeSBarry Smith 
6275c6c1daeSBarry Smith     Level: advanced
6285c6c1daeSBarry Smith 
629*c410d8ccSBarry Smith   Note:
630*c410d8ccSBarry Smith   This will have no effect on viewers that are not related to files
631*c410d8ccSBarry Smith 
632d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewerCreate()`, `PetscViewerSetType()`, `PetscViewerASCIIOpen()`, `PetscViewerBinaryOpen()`, `PetscViewerFileSetName()`
6335c6c1daeSBarry Smith @*/
634d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerFileGetName(PetscViewer viewer, const char **name)
635d71ae5a4SJacob Faibussowitsch {
6365c6c1daeSBarry Smith   PetscFunctionBegin;
6375c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
6386e05b1faSLisandro Dalcin   PetscValidPointer(name, 2);
639cac4c232SBarry Smith   PetscUseMethod(viewer, "PetscViewerFileGetName_C", (PetscViewer, const char **), (viewer, name));
6403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6415c6c1daeSBarry Smith }
6425c6c1daeSBarry Smith 
643d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerFileGetName_ASCII(PetscViewer viewer, const char **name)
644d71ae5a4SJacob Faibussowitsch {
6455c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data;
6465c6c1daeSBarry Smith 
6475c6c1daeSBarry Smith   PetscFunctionBegin;
6485c6c1daeSBarry Smith   *name = vascii->filename;
6493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6505c6c1daeSBarry Smith }
6515c6c1daeSBarry Smith 
652d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerFileSetName_ASCII(PetscViewer viewer, const char name[])
653d71ae5a4SJacob Faibussowitsch {
6545c6c1daeSBarry Smith   size_t             len;
655bbcf679cSJacob Faibussowitsch   char               fname[PETSC_MAX_PATH_LEN], *gz = NULL;
6565c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data;
6575c6c1daeSBarry Smith   PetscBool          isstderr, isstdout;
6585c6c1daeSBarry Smith   PetscMPIInt        rank;
6595c6c1daeSBarry Smith 
6605c6c1daeSBarry Smith   PetscFunctionBegin;
6619566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileClose_ASCII(viewer));
6623ba16761SJacob Faibussowitsch   if (!name) PetscFunctionReturn(PETSC_SUCCESS);
6639566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(name, &vascii->filename));
6645c6c1daeSBarry Smith 
6655c6c1daeSBarry Smith   /* Is this file to be compressed */
6665c6c1daeSBarry Smith   vascii->storecompressed = PETSC_FALSE;
667a297a907SKarl Rupp 
6689566063dSJacob Faibussowitsch   PetscCall(PetscStrstr(vascii->filename, ".gz", &gz));
6695c6c1daeSBarry Smith   if (gz) {
6709566063dSJacob Faibussowitsch     PetscCall(PetscStrlen(gz, &len));
6715c6c1daeSBarry Smith     if (len == 3) {
67208401ef6SPierre Jolivet       PetscCheck(vascii->mode == FILE_MODE_WRITE, PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Cannot open ASCII PetscViewer file that is compressed; uncompress it manually first");
6735c6c1daeSBarry Smith       *gz                     = 0;
6745c6c1daeSBarry Smith       vascii->storecompressed = PETSC_TRUE;
6755c6c1daeSBarry Smith     }
6765c6c1daeSBarry Smith   }
6779566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank));
678dd400576SPatrick Sanan   if (rank == 0) {
6799566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, "stderr", &isstderr));
6809566063dSJacob Faibussowitsch     PetscCall(PetscStrcmp(name, "stdout", &isstdout));
6815c6c1daeSBarry Smith     /* empty filename means stdout */
6825c6c1daeSBarry Smith     if (name[0] == 0) isstdout = PETSC_TRUE;
6835c6c1daeSBarry Smith     if (isstderr) vascii->fd = PETSC_STDERR;
6845c6c1daeSBarry Smith     else if (isstdout) vascii->fd = PETSC_STDOUT;
6855c6c1daeSBarry Smith     else {
6869566063dSJacob Faibussowitsch       PetscCall(PetscFixFilename(name, fname));
6875c6c1daeSBarry Smith       switch (vascii->mode) {
688d71ae5a4SJacob Faibussowitsch       case FILE_MODE_READ:
689d71ae5a4SJacob Faibussowitsch         vascii->fd = fopen(fname, "r");
690d71ae5a4SJacob Faibussowitsch         break;
691d71ae5a4SJacob Faibussowitsch       case FILE_MODE_WRITE:
692d71ae5a4SJacob Faibussowitsch         vascii->fd = fopen(fname, "w");
693d71ae5a4SJacob Faibussowitsch         break;
694d71ae5a4SJacob Faibussowitsch       case FILE_MODE_APPEND:
695d71ae5a4SJacob Faibussowitsch         vascii->fd = fopen(fname, "a");
696d71ae5a4SJacob Faibussowitsch         break;
6975c6c1daeSBarry Smith       case FILE_MODE_UPDATE:
6985c6c1daeSBarry Smith         vascii->fd = fopen(fname, "r+");
699a297a907SKarl Rupp         if (!vascii->fd) vascii->fd = fopen(fname, "w+");
7005c6c1daeSBarry Smith         break;
7015c6c1daeSBarry Smith       case FILE_MODE_APPEND_UPDATE:
7025c6c1daeSBarry Smith         /* I really want a file which is opened at the end for updating,
7035c6c1daeSBarry Smith            not a+, which opens at the beginning, but makes writes at the end.
7045c6c1daeSBarry Smith         */
7055c6c1daeSBarry Smith         vascii->fd = fopen(fname, "r+");
706a297a907SKarl Rupp         if (!vascii->fd) vascii->fd = fopen(fname, "w+");
7073ba16761SJacob Faibussowitsch         else {
7083ba16761SJacob Faibussowitsch           int ret = fseek(vascii->fd, 0, SEEK_END);
7093ba16761SJacob Faibussowitsch           PetscCheck(!ret, PETSC_COMM_SELF, PETSC_ERR_LIB, "fseek() failed with error code %d", ret);
7103ba16761SJacob Faibussowitsch         }
7115c6c1daeSBarry Smith         break;
712d71ae5a4SJacob Faibussowitsch       default:
713d71ae5a4SJacob Faibussowitsch         SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Unsupported file mode %s", PetscFileModes[vascii->mode]);
7145c6c1daeSBarry Smith       }
71528b400f6SJacob Faibussowitsch       PetscCheck(vascii->fd, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Cannot open PetscViewer file: %s", fname);
7165c6c1daeSBarry Smith     }
7175c6c1daeSBarry Smith   }
7185c6c1daeSBarry Smith #if defined(PETSC_USE_LOG)
7193ba16761SJacob Faibussowitsch   PetscCall(PetscLogObjectState((PetscObject)viewer, "File: %s", name));
7205c6c1daeSBarry Smith #endif
7213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7225c6c1daeSBarry Smith }
7235c6c1daeSBarry Smith 
724d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerGetSubViewer_ASCII(PetscViewer viewer, MPI_Comm subcomm, PetscViewer *outviewer)
725d71ae5a4SJacob Faibussowitsch {
7265c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data, *ovascii;
7275c6c1daeSBarry Smith 
7285c6c1daeSBarry Smith   PetscFunctionBegin;
7299566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPushSynchronized(viewer));
73028b400f6SJacob Faibussowitsch   PetscCheck(!vascii->sviewer, PETSC_COMM_SELF, PETSC_ERR_ORDER, "SubViewer already obtained from PetscViewer and not restored");
731e5afcf28SBarry Smith   /*
7329530cbd7SBarry Smith      The following line is a bug; it does another PetscViewerASCIIPushSynchronized() on viewer, but if it is removed the code won't work
7339530cbd7SBarry Smith      because it relies on this behavior in other places. In particular this line causes the synchronized flush to occur when the viewer is destroyed
7349530cbd7SBarry Smith      (since the count never gets to zero) in some examples this displays information that otherwise would be lost
7359530cbd7SBarry Smith 
7369530cbd7SBarry Smith      This code also means another call to PetscViewerASCIIPopSynchronized() must be made after the PetscViewerRestoreSubViewer(), see, for example,
7379530cbd7SBarry Smith      PCView_GASM().
738e5afcf28SBarry Smith   */
7399566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPushSynchronized(viewer));
7409566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(subcomm, outviewer));
7419566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(*outviewer, PETSCVIEWERASCII));
7429566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPushSynchronized(*outviewer));
7435c6c1daeSBarry Smith   ovascii            = (PetscViewer_ASCII *)(*outviewer)->data;
7445c6c1daeSBarry Smith   ovascii->fd        = vascii->fd;
7455c6c1daeSBarry Smith   ovascii->tab       = vascii->tab;
746ba5a0b41SBarry Smith   ovascii->closefile = PETSC_FALSE;
7475c6c1daeSBarry Smith 
7485c6c1daeSBarry Smith   vascii->sviewer                                      = *outviewer;
7495c6c1daeSBarry Smith   (*outviewer)->format                                 = viewer->format;
7505c6c1daeSBarry Smith   ((PetscViewer_ASCII *)((*outviewer)->data))->bviewer = viewer;
7513f08860eSBarry Smith   (*outviewer)->ops->destroy                           = PetscViewerDestroy_ASCII_SubViewer;
7523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7535c6c1daeSBarry Smith }
7545c6c1daeSBarry Smith 
755d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerRestoreSubViewer_ASCII(PetscViewer viewer, MPI_Comm comm, PetscViewer *outviewer)
756d71ae5a4SJacob Faibussowitsch {
7575c6c1daeSBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII *)viewer->data;
7585c6c1daeSBarry Smith 
7595c6c1daeSBarry Smith   PetscFunctionBegin;
76028b400f6SJacob Faibussowitsch   PetscCheck(ascii->sviewer, PETSC_COMM_SELF, PETSC_ERR_ORDER, "SubViewer never obtained from PetscViewer");
76108401ef6SPierre Jolivet   PetscCheck(ascii->sviewer == *outviewer, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "This PetscViewer did not generate this SubViewer");
7625c6c1daeSBarry Smith 
7639566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPopSynchronized(*outviewer));
764e5afcf28SBarry Smith   ascii->sviewer             = NULL;
7655c6c1daeSBarry Smith   (*outviewer)->ops->destroy = PetscViewerDestroy_ASCII;
7669566063dSJacob Faibussowitsch   PetscCall(PetscViewerDestroy(outviewer));
7679566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPopSynchronized(viewer));
7683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7695c6c1daeSBarry Smith }
7705c6c1daeSBarry Smith 
771d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerView_ASCII(PetscViewer v, PetscViewer viewer)
772d71ae5a4SJacob Faibussowitsch {
7732bf49c77SBarry Smith   PetscViewer_ASCII *ascii = (PetscViewer_ASCII *)v->data;
7742bf49c77SBarry Smith 
7752bf49c77SBarry Smith   PetscFunctionBegin;
77648a46eb9SPierre Jolivet   if (ascii->filename) PetscCall(PetscViewerASCIIPrintf(viewer, "Filename: %s\n", ascii->filename));
7773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7782bf49c77SBarry Smith }
7792bf49c77SBarry Smith 
7808556b5ebSBarry Smith /*MC
7818556b5ebSBarry Smith    PETSCVIEWERASCII - A viewer that prints to stdout or an ASCII file
7828556b5ebSBarry Smith 
783811af0c4SBarry Smith   Level: beginner
784811af0c4SBarry Smith 
785d1f92df0SBarry Smith .seealso: [](sec_viewers), `PETSC_VIEWER_STDOUT_()`, `PETSC_VIEWER_STDOUT_SELF`, `PETSC_VIEWER_STDOUT_WORLD`, `PetscViewerCreate()`, `PetscViewerASCIIOpen()`,
786db781477SPatrick Sanan           `PetscViewerMatlabOpen()`, `VecView()`, `DMView()`, `PetscViewerMatlabPutArray()`, `PETSCVIEWERBINARY`, `PETSCVIEWERMATLAB`,
787db781477SPatrick Sanan           `PetscViewerFileSetName()`, `PetscViewerFileSetMode()`, `PetscViewerFormat`, `PetscViewerType`, `PetscViewerSetType()`
7888556b5ebSBarry Smith M*/
789d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscViewerCreate_ASCII(PetscViewer viewer)
790d71ae5a4SJacob Faibussowitsch {
7915c6c1daeSBarry Smith   PetscViewer_ASCII *vascii;
7925c6c1daeSBarry Smith 
7935c6c1daeSBarry Smith   PetscFunctionBegin;
7944dfa11a4SJacob Faibussowitsch   PetscCall(PetscNew(&vascii));
7955c6c1daeSBarry Smith   viewer->data = (void *)vascii;
7965c6c1daeSBarry Smith 
7975c6c1daeSBarry Smith   viewer->ops->destroy          = PetscViewerDestroy_ASCII;
7985c6c1daeSBarry Smith   viewer->ops->flush            = PetscViewerFlush_ASCII;
799559f443fSBarry Smith   viewer->ops->getsubviewer     = PetscViewerGetSubViewer_ASCII;
800559f443fSBarry Smith   viewer->ops->restoresubviewer = PetscViewerRestoreSubViewer_ASCII;
8012bf49c77SBarry Smith   viewer->ops->view             = PetscViewerView_ASCII;
8021d641e7bSMichael Lange   viewer->ops->read             = PetscViewerASCIIRead;
8035c6c1daeSBarry Smith 
8045c6c1daeSBarry Smith   /* defaults to stdout unless set with PetscViewerFileSetName() */
8055c6c1daeSBarry Smith   vascii->fd        = PETSC_STDOUT;
8065c6c1daeSBarry Smith   vascii->mode      = FILE_MODE_WRITE;
80702c9f0b5SLisandro Dalcin   vascii->bviewer   = NULL;
80802c9f0b5SLisandro Dalcin   vascii->subviewer = NULL;
80902c9f0b5SLisandro Dalcin   vascii->sviewer   = NULL;
8105c6c1daeSBarry Smith   vascii->tab       = 0;
8115c6c1daeSBarry Smith   vascii->tab_store = 0;
81202c9f0b5SLisandro Dalcin   vascii->filename  = NULL;
8135c6c1daeSBarry Smith   vascii->closefile = PETSC_TRUE;
8145c6c1daeSBarry Smith 
8159566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetName_C", PetscViewerFileSetName_ASCII));
8169566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetName_C", PetscViewerFileGetName_ASCII));
8179566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", PetscViewerFileGetMode_ASCII));
8189566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetMode_C", PetscViewerFileSetMode_ASCII));
8193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8205c6c1daeSBarry Smith }
8215c6c1daeSBarry Smith 
8225c6c1daeSBarry Smith /*@C
823*c410d8ccSBarry Smith     PetscViewerASCIISynchronizedPrintf - Prints synchronized output to the specified `PETSCVIEWERASCII` file from
8245c6c1daeSBarry Smith     several processors.  Output of the first processor is followed by that of the
8255c6c1daeSBarry Smith     second, etc.
8265c6c1daeSBarry Smith 
827*c410d8ccSBarry Smith     Not Collective, must call collective `PetscViewerFlush()` to get the results flushed
8285c6c1daeSBarry Smith 
8295c6c1daeSBarry Smith     Input Parameters:
830811af0c4SBarry Smith +   viewer - the `PETSCVIEWERASCII` `PetscViewer`
8315c6c1daeSBarry Smith -   format - the usual printf() format string
8325c6c1daeSBarry Smith 
8335c6c1daeSBarry Smith     Level: intermediate
8345c6c1daeSBarry Smith 
83595452b02SPatrick Sanan     Notes:
836811af0c4SBarry Smith     You must have previously called `PetscViewerASCIIPushSynchronized()` to allow this routine to be called.
837e6abc3ddSVáclav Hapla     Then you can do multiple independent calls to this routine.
838811af0c4SBarry Smith 
839811af0c4SBarry Smith     The actual synchronized print is then done using `PetscViewerFlush()`.
840811af0c4SBarry Smith     `PetscViewerASCIIPopSynchronized()` should be then called if we are already done with the synchronized output
841e6abc3ddSVáclav Hapla     to conclude the "synchronized session".
842811af0c4SBarry Smith 
843e6abc3ddSVáclav Hapla     So the typical calling sequence looks like
844811af0c4SBarry Smith .vb
845811af0c4SBarry Smith     PetscViewerASCIIPushSynchronized(viewer);
846811af0c4SBarry Smith     PetscViewerASCIISynchronizedPrintf(viewer, ...);
847811af0c4SBarry Smith     PetscViewerASCIISynchronizedPrintf(viewer, ...);
848811af0c4SBarry Smith     ...
849811af0c4SBarry Smith     PetscViewerFlush(viewer);
850811af0c4SBarry Smith     PetscViewerASCIISynchronizedPrintf(viewer, ...);
851811af0c4SBarry Smith     PetscViewerASCIISynchronizedPrintf(viewer, ...);
852811af0c4SBarry Smith     ...
853811af0c4SBarry Smith     PetscViewerFlush(viewer);
854811af0c4SBarry Smith    PetscViewerASCIIPopSynchronized(viewer);
855811af0c4SBarry Smith .ve
8565c6c1daeSBarry Smith 
8575c6c1daeSBarry Smith     Fortran Note:
8585c6c1daeSBarry Smith       Can only print a single character* string
8595c6c1daeSBarry Smith 
860d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewerASCIIPushSynchronized()`, `PetscViewerFlush()`, `PetscViewerASCIIPopSynchronized()`,
861db781477SPatrick Sanan           `PetscSynchronizedPrintf()`, `PetscViewerASCIIPrintf()`, `PetscViewerASCIIOpen()`,
862db781477SPatrick Sanan           `PetscViewerCreate()`, `PetscViewerDestroy()`, `PetscViewerSetType()`
8635c6c1daeSBarry Smith @*/
864d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIISynchronizedPrintf(PetscViewer viewer, const char format[], ...)
865d71ae5a4SJacob Faibussowitsch {
8665c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data;
8673f08860eSBarry Smith   PetscMPIInt        rank;
8685c6c1daeSBarry Smith   PetscInt           tab = vascii->tab;
8695c6c1daeSBarry Smith   MPI_Comm           comm;
8705c6c1daeSBarry Smith   FILE              *fp;
871559f443fSBarry Smith   PetscBool          iascii, hasbviewer = PETSC_FALSE;
8725c6c1daeSBarry Smith 
8735c6c1daeSBarry Smith   PetscFunctionBegin;
8745c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
8755c6c1daeSBarry Smith   PetscValidCharPointer(format, 2);
8769566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
87728b400f6SJacob Faibussowitsch   PetscCheck(iascii, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Not ASCII PetscViewer");
87828b400f6SJacob Faibussowitsch   PetscCheck(vascii->allowsynchronized, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "First call PetscViewerASCIIPushSynchronized() to allow this call");
8795c6c1daeSBarry Smith 
8809566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)viewer, &comm));
8819566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
8825c6c1daeSBarry Smith 
883559f443fSBarry Smith   if (vascii->bviewer) {
884559f443fSBarry Smith     hasbviewer = PETSC_TRUE;
885dd400576SPatrick Sanan     if (rank == 0) {
886559f443fSBarry Smith       vascii = (PetscViewer_ASCII *)vascii->bviewer->data;
8879566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetComm((PetscObject)viewer, &comm));
8889566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Comm_rank(comm, &rank));
889559f443fSBarry Smith     }
890559f443fSBarry Smith   }
8913f08860eSBarry Smith 
892559f443fSBarry Smith   fp = vascii->fd;
893559f443fSBarry Smith 
894dd400576SPatrick Sanan   if (rank == 0 && !hasbviewer) { /* First processor prints immediately to fp */
8955c6c1daeSBarry Smith     va_list Argp;
896559f443fSBarry Smith     /* flush my own messages that I may have queued up */
897559f443fSBarry Smith     PrintfQueue next = vascii->petsc_printfqueuebase, previous;
898559f443fSBarry Smith     PetscInt    i;
899559f443fSBarry Smith     for (i = 0; i < vascii->petsc_printfqueuelength; i++) {
9009566063dSJacob Faibussowitsch       PetscCall(PetscFPrintf(comm, fp, "%s", next->string));
901559f443fSBarry Smith       previous = next;
902559f443fSBarry Smith       next     = next->next;
9039566063dSJacob Faibussowitsch       PetscCall(PetscFree(previous->string));
9049566063dSJacob Faibussowitsch       PetscCall(PetscFree(previous));
905559f443fSBarry Smith     }
90602c9f0b5SLisandro Dalcin     vascii->petsc_printfqueue       = NULL;
907559f443fSBarry Smith     vascii->petsc_printfqueuelength = 0;
9085c6c1daeSBarry Smith 
90948a46eb9SPierre Jolivet     while (tab--) PetscCall(PetscFPrintf(PETSC_COMM_SELF, fp, "  "));
9105c6c1daeSBarry Smith 
9115c6c1daeSBarry Smith     va_start(Argp, format);
9129566063dSJacob Faibussowitsch     PetscCall((*PetscVFPrintf)(fp, format, Argp));
913eae3dc7dSJacob Faibussowitsch     va_end(Argp);
914c69effb2SJacob Faibussowitsch     PetscCall(PetscFFlush(fp));
9155c6c1daeSBarry Smith     if (petsc_history) {
9165c6c1daeSBarry Smith       va_start(Argp, format);
9179566063dSJacob Faibussowitsch       PetscCall((*PetscVFPrintf)(petsc_history, format, Argp));
918eae3dc7dSJacob Faibussowitsch       va_end(Argp);
919c69effb2SJacob Faibussowitsch       PetscCall(PetscFFlush(petsc_history));
9205c6c1daeSBarry Smith     }
9215c6c1daeSBarry Smith     va_end(Argp);
922559f443fSBarry Smith   } else { /* other processors add to queue */
9235c6c1daeSBarry Smith     char       *string;
9245c6c1daeSBarry Smith     va_list     Argp;
9255c6c1daeSBarry Smith     size_t      fullLength;
9265c6c1daeSBarry Smith     PrintfQueue next;
9275c6c1daeSBarry Smith 
9289566063dSJacob Faibussowitsch     PetscCall(PetscNew(&next));
929559f443fSBarry Smith     if (vascii->petsc_printfqueue) {
930559f443fSBarry Smith       vascii->petsc_printfqueue->next = next;
931559f443fSBarry Smith       vascii->petsc_printfqueue       = next;
932a297a907SKarl Rupp     } else {
933559f443fSBarry Smith       vascii->petsc_printfqueuebase = vascii->petsc_printfqueue = next;
934a297a907SKarl Rupp     }
935559f443fSBarry Smith     vascii->petsc_printfqueuelength++;
9365c6c1daeSBarry Smith     next->size = QUEUESTRINGSIZE;
9379566063dSJacob Faibussowitsch     PetscCall(PetscCalloc1(next->size, &next->string));
9385c6c1daeSBarry Smith     string = next->string;
9395c6c1daeSBarry Smith     tab *= 2;
940ad540459SPierre Jolivet     while (tab--) *string++ = ' ';
9415c6c1daeSBarry Smith     va_start(Argp, format);
9429566063dSJacob Faibussowitsch     PetscCall(PetscVSNPrintf(string, next->size - 2 * vascii->tab, format, &fullLength, Argp));
9435c6c1daeSBarry Smith     va_end(Argp);
944cb500232SBarry Smith     if (fullLength > (size_t)(next->size - 2 * vascii->tab)) {
9459566063dSJacob Faibussowitsch       PetscCall(PetscFree(next->string));
94614416c0eSBarry Smith       next->size = fullLength + 2 * vascii->tab;
9479566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(next->size, &next->string));
94814416c0eSBarry Smith       string = next->string;
94914416c0eSBarry Smith       tab    = 2 * vascii->tab;
950ad540459SPierre Jolivet       while (tab--) *string++ = ' ';
95114416c0eSBarry Smith       va_start(Argp, format);
9529566063dSJacob Faibussowitsch       PetscCall(PetscVSNPrintf(string, next->size - 2 * vascii->tab, format, NULL, Argp));
95314416c0eSBarry Smith       va_end(Argp);
95414416c0eSBarry Smith     }
9555c6c1daeSBarry Smith   }
9563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9575c6c1daeSBarry Smith }
9585c6c1daeSBarry Smith 
9592655f987SMichael Lange /*@C
960*c410d8ccSBarry Smith    PetscViewerASCIIRead - Reads from a `PETSCVIEWERASCII` file
9612655f987SMichael Lange 
962*c410d8ccSBarry Smith    Only MPI rank 0 in the `PetscViewer` may call this
9632655f987SMichael Lange 
9642655f987SMichael Lange    Input Parameters:
9653f423023SBarry Smith +  viewer - the `PETSCVIEWERASCII` viewer
966*c410d8ccSBarry Smith .  data - location to write the data, treated as an array of type indicated by `datatype`
967060da220SMatthew G. Knepley .  num - number of items of data to read
9682655f987SMichael Lange -  datatype - type of data to read
9692655f987SMichael Lange 
97020f4b53cSBarry Smith    Output Parameter:
9713f423023SBarry Smith .  count - number of items of data actually read, or `NULL`
972f8e4bde8SMatthew G. Knepley 
9732655f987SMichael Lange    Level: beginner
9742655f987SMichael Lange 
975d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewerASCIIOpen()`, `PetscViewerPushFormat()`, `PetscViewerDestroy()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetName()`
976db781477SPatrick Sanan           `VecView()`, `MatView()`, `VecLoad()`, `MatLoad()`, `PetscViewerBinaryGetDescriptor()`,
977db781477SPatrick Sanan           `PetscViewerBinaryGetInfoPointer()`, `PetscFileMode`, `PetscViewer`, `PetscViewerBinaryRead()`
9782655f987SMichael Lange @*/
979d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIIRead(PetscViewer viewer, void *data, PetscInt num, PetscInt *count, PetscDataType dtype)
980d71ae5a4SJacob Faibussowitsch {
9812655f987SMichael Lange   PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data;
9822655f987SMichael Lange   FILE              *fd     = vascii->fd;
9832655f987SMichael Lange   PetscInt           i;
9843b7fe8c3SMatthew G. Knepley   int                ret = 0;
985f8859db6SBarry Smith   PetscMPIInt        rank;
9862655f987SMichael Lange 
9872655f987SMichael Lange   PetscFunctionBegin;
9882655f987SMichael Lange   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
9899566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank));
990c5853193SPierre Jolivet   PetscCheck(rank == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Can only be called from process 0 in the PetscViewer");
991060da220SMatthew G. Knepley   for (i = 0; i < num; i++) {
992f8e4bde8SMatthew G. Knepley     if (dtype == PETSC_CHAR) ret = fscanf(fd, "%c", &(((char *)data)[i]));
993f8e4bde8SMatthew G. Knepley     else if (dtype == PETSC_STRING) ret = fscanf(fd, "%s", &(((char *)data)[i]));
994a05e1a72SSatish Balay     else if (dtype == PETSC_INT) ret = fscanf(fd, "%" PetscInt_FMT, &(((PetscInt *)data)[i]));
995f8e4bde8SMatthew G. Knepley     else if (dtype == PETSC_ENUM) ret = fscanf(fd, "%d", &(((int *)data)[i]));
9969e3e4c22SLisandro Dalcin     else if (dtype == PETSC_INT64) ret = fscanf(fd, "%" PetscInt64_FMT, &(((PetscInt64 *)data)[i]));
997972064b6SLisandro Dalcin     else if (dtype == PETSC_LONG) ret = fscanf(fd, "%ld", &(((long *)data)[i]));
998f8e4bde8SMatthew G. Knepley     else if (dtype == PETSC_FLOAT) ret = fscanf(fd, "%f", &(((float *)data)[i]));
999f8e4bde8SMatthew G. Knepley     else if (dtype == PETSC_DOUBLE) ret = fscanf(fd, "%lg", &(((double *)data)[i]));
1000a6e181c6SToby Isaac #if defined(PETSC_USE_REAL___FLOAT128)
1001fba955ccSBarry Smith     else if (dtype == PETSC___FLOAT128) {
1002fba955ccSBarry Smith       double tmp;
1003fba955ccSBarry Smith       ret                     = fscanf(fd, "%lg", &tmp);
1004a6e181c6SToby Isaac       ((__float128 *)data)[i] = tmp;
1005a6e181c6SToby Isaac     }
1006fba955ccSBarry Smith #endif
10079371c9d4SSatish Balay     else
10089371c9d4SSatish Balay       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Data type %d not supported", (int)dtype);
100928b400f6SJacob Faibussowitsch     PetscCheck(ret, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Conversion error for data type %d", (int)dtype);
1010f7d195e4SLawrence Mitchell     if (ret < 0) break; /* Proxy for EOF, need to check for it in configure */
10112655f987SMichael Lange   }
1012060da220SMatthew G. Knepley   if (count) *count = i;
101308401ef6SPierre Jolivet   else PetscCheck(ret >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Insufficient data, read only %" PetscInt_FMT " < %" PetscInt_FMT " items", i, num);
10143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
10152655f987SMichael Lange }
1016