1665c2dedSJed Brown #include <../src/sys/classes/viewer/impls/ascii/asciiimpl.h> /*I "petscviewer.h" I*/ 25c6c1daeSBarry Smith 35c6c1daeSBarry Smith #define QUEUESTRINGSIZE 8192 45c6c1daeSBarry Smith 5d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscViewerFileClose_ASCII(PetscViewer viewer) 6d71ae5a4SJacob Faibussowitsch { 75c6c1daeSBarry Smith PetscMPIInt rank; 85c6c1daeSBarry Smith PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data; 95c6c1daeSBarry Smith int err; 105c6c1daeSBarry Smith 115c6c1daeSBarry Smith PetscFunctionBegin; 1228b400f6SJacob Faibussowitsch PetscCheck(!vascii->sviewer, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONGSTATE, "Cannot call with outstanding call to PetscViewerRestoreSubViewer()"); 139566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank)); 14dd400576SPatrick Sanan if (rank == 0 && vascii->fd != stderr && vascii->fd != PETSC_STDOUT) { 155c6c1daeSBarry Smith if (vascii->fd && vascii->closefile) { 165c6c1daeSBarry Smith err = fclose(vascii->fd); 1728b400f6SJacob Faibussowitsch PetscCheck(!err, PETSC_COMM_SELF, PETSC_ERR_SYS, "fclose() failed on file"); 185c6c1daeSBarry Smith } 195c6c1daeSBarry Smith if (vascii->storecompressed) { 205c6c1daeSBarry Smith char par[PETSC_MAX_PATH_LEN], buf[PETSC_MAX_PATH_LEN]; 215c6c1daeSBarry Smith FILE *fp; 229566063dSJacob Faibussowitsch PetscCall(PetscStrncpy(par, "gzip ", sizeof(par))); 239566063dSJacob Faibussowitsch PetscCall(PetscStrlcat(par, vascii->filename, sizeof(par))); 245c6c1daeSBarry Smith #if defined(PETSC_HAVE_POPEN) 259566063dSJacob Faibussowitsch PetscCall(PetscPOpen(PETSC_COMM_SELF, NULL, par, "r", &fp)); 2600045ab3SPierre Jolivet PetscCheck(!fgets(buf, 1024, fp), PETSC_COMM_SELF, PETSC_ERR_LIB, "Error from compression command %s %s", par, buf); 279566063dSJacob Faibussowitsch PetscCall(PetscPClose(PETSC_COMM_SELF, fp)); 285c6c1daeSBarry Smith #else 295c6c1daeSBarry Smith SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP_SYS, "Cannot run external programs on this machine"); 305c6c1daeSBarry Smith #endif 315c6c1daeSBarry Smith } 325c6c1daeSBarry Smith } 339566063dSJacob Faibussowitsch PetscCall(PetscFree(vascii->filename)); 343ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 355c6c1daeSBarry Smith } 365c6c1daeSBarry Smith 375c6c1daeSBarry Smith /* ----------------------------------------------------------------------*/ 3834e79e72SJacob Faibussowitsch static PetscErrorCode PetscViewerDestroy_ASCII(PetscViewer viewer) 39d71ae5a4SJacob Faibussowitsch { 405c6c1daeSBarry Smith PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data; 415c6c1daeSBarry Smith PetscViewerLink *vlink; 425c6c1daeSBarry Smith PetscBool flg; 435c6c1daeSBarry Smith 445c6c1daeSBarry Smith PetscFunctionBegin; 4528b400f6SJacob Faibussowitsch PetscCheck(!vascii->sviewer, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONGSTATE, "Cannot call with outstanding call to PetscViewerRestoreSubViewer()"); 469566063dSJacob Faibussowitsch PetscCall(PetscViewerFileClose_ASCII(viewer)); 479566063dSJacob Faibussowitsch PetscCall(PetscFree(vascii)); 485c6c1daeSBarry Smith 495c6c1daeSBarry Smith /* remove the viewer from the list in the MPI Communicator */ 5048a46eb9SPierre 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)); 515c6c1daeSBarry Smith 529566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_get_attr(PetscObjectComm((PetscObject)viewer), Petsc_Viewer_keyval, (void **)&vlink, (PetscMPIInt *)&flg)); 535c6c1daeSBarry Smith if (flg) { 545c6c1daeSBarry Smith if (vlink && vlink->viewer == viewer) { 55e5840a18SBarry Smith if (vlink->next) { 569566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_set_attr(PetscObjectComm((PetscObject)viewer), Petsc_Viewer_keyval, vlink->next)); 57e5840a18SBarry Smith } else { 589566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_delete_attr(PetscObjectComm((PetscObject)viewer), Petsc_Viewer_keyval)); 59e5840a18SBarry Smith } 609566063dSJacob Faibussowitsch PetscCall(PetscFree(vlink)); 615c6c1daeSBarry Smith } else { 625c6c1daeSBarry Smith while (vlink && vlink->next) { 635c6c1daeSBarry Smith if (vlink->next->viewer == viewer) { 645c6c1daeSBarry Smith PetscViewerLink *nv = vlink->next; 655c6c1daeSBarry Smith vlink->next = vlink->next->next; 669566063dSJacob Faibussowitsch PetscCall(PetscFree(nv)); 675c6c1daeSBarry Smith } 685c6c1daeSBarry Smith vlink = vlink->next; 695c6c1daeSBarry Smith } 705c6c1daeSBarry Smith } 715c6c1daeSBarry Smith } 72aa139df6SJed Brown 73aa139df6SJed Brown if (Petsc_Viewer_Stdout_keyval != MPI_KEYVAL_INVALID) { 74aa139df6SJed Brown PetscViewer aviewer; 759566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_get_attr(PetscObjectComm((PetscObject)viewer), Petsc_Viewer_Stdout_keyval, (void **)&aviewer, (PetscMPIInt *)&flg)); 7648a46eb9SPierre Jolivet if (flg && aviewer == viewer) PetscCallMPI(MPI_Comm_delete_attr(PetscObjectComm((PetscObject)viewer), Petsc_Viewer_Stdout_keyval)); 77aa139df6SJed Brown } 78aa139df6SJed Brown if (Petsc_Viewer_Stderr_keyval != MPI_KEYVAL_INVALID) { 79aa139df6SJed Brown PetscViewer aviewer; 809566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_get_attr(PetscObjectComm((PetscObject)viewer), Petsc_Viewer_Stderr_keyval, (void **)&aviewer, (PetscMPIInt *)&flg)); 8148a46eb9SPierre Jolivet if (flg && aviewer == viewer) PetscCallMPI(MPI_Comm_delete_attr(PetscObjectComm((PetscObject)viewer), Petsc_Viewer_Stderr_keyval)); 82aa139df6SJed Brown } 832e956fe4SStefano Zampini PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetName_C", NULL)); 842e956fe4SStefano Zampini PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetName_C", NULL)); 852e956fe4SStefano Zampini PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", NULL)); 862e956fe4SStefano Zampini PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetMode_C", NULL)); 873ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 885c6c1daeSBarry Smith } 895c6c1daeSBarry Smith 9034e79e72SJacob Faibussowitsch static PetscErrorCode PetscViewerDestroy_ASCII_SubViewer(PetscViewer viewer) 91d71ae5a4SJacob Faibussowitsch { 925c6c1daeSBarry Smith PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data; 935fd66863SKarl Rupp 945c6c1daeSBarry Smith PetscFunctionBegin; 959566063dSJacob Faibussowitsch PetscCall(PetscViewerRestoreSubViewer(vascii->bviewer, 0, &viewer)); 963ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 975c6c1daeSBarry Smith } 985c6c1daeSBarry Smith 9934e79e72SJacob Faibussowitsch static PetscErrorCode PetscViewerFlush_ASCII(PetscViewer viewer) 100d71ae5a4SJacob Faibussowitsch { 1015c6c1daeSBarry Smith PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data; 102559f443fSBarry Smith MPI_Comm comm; 103559f443fSBarry Smith PetscMPIInt rank, size; 104559f443fSBarry Smith FILE *fd = vascii->fd; 1055c6c1daeSBarry Smith 1065c6c1daeSBarry Smith PetscFunctionBegin; 10728b400f6SJacob Faibussowitsch PetscCheck(!vascii->sviewer, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONGSTATE, "Cannot call with outstanding call to PetscViewerRestoreSubViewer()"); 1089566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)viewer, &comm)); 1099566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 1109566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(comm, &size)); 111559f443fSBarry Smith 112c69effb2SJacob Faibussowitsch if (!vascii->bviewer && rank == 0 && (vascii->mode != FILE_MODE_READ)) PetscCall(PetscFFlush(vascii->fd)); 1135c6c1daeSBarry Smith 1145c6c1daeSBarry Smith if (vascii->allowsynchronized) { 115559f443fSBarry Smith PetscMPIInt tag, i, j, n = 0, dummy = 0; 116559f443fSBarry Smith char *message; 117559f443fSBarry Smith MPI_Status status; 118559f443fSBarry Smith 1199566063dSJacob Faibussowitsch PetscCall(PetscCommDuplicate(comm, &comm, &tag)); 120559f443fSBarry Smith 121559f443fSBarry Smith /* First processor waits for messages from all other processors */ 122dd400576SPatrick Sanan if (rank == 0) { 123559f443fSBarry Smith /* flush my own messages that I may have queued up */ 124559f443fSBarry Smith PrintfQueue next = vascii->petsc_printfqueuebase, previous; 125559f443fSBarry Smith for (i = 0; i < vascii->petsc_printfqueuelength; i++) { 126559f443fSBarry Smith if (!vascii->bviewer) { 1279566063dSJacob Faibussowitsch PetscCall(PetscFPrintf(comm, fd, "%s", next->string)); 128559f443fSBarry Smith } else { 1299566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISynchronizedPrintf(vascii->bviewer, "%s", next->string)); 130559f443fSBarry Smith } 131559f443fSBarry Smith previous = next; 132559f443fSBarry Smith next = next->next; 1339566063dSJacob Faibussowitsch PetscCall(PetscFree(previous->string)); 1349566063dSJacob Faibussowitsch PetscCall(PetscFree(previous)); 135559f443fSBarry Smith } 13602c9f0b5SLisandro Dalcin vascii->petsc_printfqueue = NULL; 137559f443fSBarry Smith vascii->petsc_printfqueuelength = 0; 138559f443fSBarry Smith for (i = 1; i < size; i++) { 139559f443fSBarry Smith /* to prevent a flood of messages to process zero, request each message separately */ 1409566063dSJacob Faibussowitsch PetscCallMPI(MPI_Send(&dummy, 1, MPI_INT, i, tag, comm)); 1419566063dSJacob Faibussowitsch PetscCallMPI(MPI_Recv(&n, 1, MPI_INT, i, tag, comm, &status)); 142559f443fSBarry Smith for (j = 0; j < n; j++) { 143559f443fSBarry Smith PetscMPIInt size = 0; 144559f443fSBarry Smith 1459566063dSJacob Faibussowitsch PetscCallMPI(MPI_Recv(&size, 1, MPI_INT, i, tag, comm, &status)); 1469566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(size, &message)); 1479566063dSJacob Faibussowitsch PetscCallMPI(MPI_Recv(message, size, MPI_CHAR, i, tag, comm, &status)); 148559f443fSBarry Smith if (!vascii->bviewer) { 1499566063dSJacob Faibussowitsch PetscCall(PetscFPrintf(comm, fd, "%s", message)); 150559f443fSBarry Smith } else { 1519566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIISynchronizedPrintf(vascii->bviewer, "%s", message)); 152559f443fSBarry Smith } 1539566063dSJacob Faibussowitsch PetscCall(PetscFree(message)); 154559f443fSBarry Smith } 155559f443fSBarry Smith } 156559f443fSBarry Smith } else { /* other processors send queue to processor 0 */ 157559f443fSBarry Smith PrintfQueue next = vascii->petsc_printfqueuebase, previous; 158559f443fSBarry Smith 1599566063dSJacob Faibussowitsch PetscCallMPI(MPI_Recv(&dummy, 1, MPI_INT, 0, tag, comm, &status)); 1609566063dSJacob Faibussowitsch PetscCallMPI(MPI_Send(&vascii->petsc_printfqueuelength, 1, MPI_INT, 0, tag, comm)); 161559f443fSBarry Smith for (i = 0; i < vascii->petsc_printfqueuelength; i++) { 1629566063dSJacob Faibussowitsch PetscCallMPI(MPI_Send(&next->size, 1, MPI_INT, 0, tag, comm)); 1639566063dSJacob Faibussowitsch PetscCallMPI(MPI_Send(next->string, next->size, MPI_CHAR, 0, tag, comm)); 164559f443fSBarry Smith previous = next; 165559f443fSBarry Smith next = next->next; 1669566063dSJacob Faibussowitsch PetscCall(PetscFree(previous->string)); 1679566063dSJacob Faibussowitsch PetscCall(PetscFree(previous)); 168559f443fSBarry Smith } 16902c9f0b5SLisandro Dalcin vascii->petsc_printfqueue = NULL; 170559f443fSBarry Smith vascii->petsc_printfqueuelength = 0; 171559f443fSBarry Smith } 1729566063dSJacob Faibussowitsch PetscCall(PetscCommDestroy(&comm)); 1735c6c1daeSBarry Smith } 1743ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1755c6c1daeSBarry Smith } 1765c6c1daeSBarry Smith 1775c6c1daeSBarry Smith /*@C 178811af0c4SBarry Smith PetscViewerASCIIGetPointer - Extracts the file pointer from an ASCII `PetscViewer`. 1795c6c1daeSBarry Smith 18035cb6cd3SPierre Jolivet Not Collective, depending on the viewer the value may be meaningless except for process 0 of the viewer; No Fortran Support 1815c6c1daeSBarry Smith 182f8859db6SBarry Smith Input Parameter: 1833f423023SBarry Smith . viewer - `PetscViewer` context, obtained from `PetscViewerASCIIOpen()` 184f8859db6SBarry Smith 185f8859db6SBarry Smith Output Parameter: 186f8859db6SBarry Smith . fd - file pointer 187f8859db6SBarry Smith 1885c6c1daeSBarry Smith Level: intermediate 1895c6c1daeSBarry Smith 190811af0c4SBarry Smith Note: 191c410d8ccSBarry Smith For the standard `PETSCVIEWERASCII` the value is valid only on MPI rank 0 of the viewer 192811af0c4SBarry Smith 1933f423023SBarry Smith .seealso: [](sec_viewers), `PETSCVIEWERASCII`, `PetscViewerASCIIOpen()`, `PetscViewerDestroy()`, `PetscViewerSetType()`, 1943f423023SBarry Smith `PetscViewerCreate()`, `PetscViewerASCIIPrintf()`, `PetscViewerASCIISynchronizedPrintf()`, `PetscViewerFlush()` 1955c6c1daeSBarry Smith @*/ 196d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIIGetPointer(PetscViewer viewer, FILE **fd) 197d71ae5a4SJacob Faibussowitsch { 1985c6c1daeSBarry Smith PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data; 1995c6c1daeSBarry Smith 2005c6c1daeSBarry Smith PetscFunctionBegin; 201*c621c6acSBarry Smith PetscCheck(!vascii->fileunit, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONGSTATE, "Cannot request file pointer for viewers that use Fortran files"); 2025c6c1daeSBarry Smith *fd = vascii->fd; 2033ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2045c6c1daeSBarry Smith } 2055c6c1daeSBarry Smith 20634e79e72SJacob Faibussowitsch static 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 21534e79e72SJacob Faibussowitsch static 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 420c410d8ccSBarry 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 447c410d8ccSBarry 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 /*@ 474c410d8ccSBarry Smith PetscViewerASCIIUseTabs - Turns on or off the use of tabs with the `PETSCVIEWERASCII` `PetscViewer` 4755c6c1daeSBarry Smith 476c410d8ccSBarry 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 506e4096674SBarry Smith #if defined(PETSC_USE_FORTRAN_BINDINGS) 507e4096674SBarry Smith 508e4096674SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS) 509e4096674SBarry Smith #define petscviewerasciiopenwithfileunit_ PETSCVIEWERASCIIOPENWITHFILEUNIT 510e4096674SBarry Smith #define petscviewerasciisetfilefileunit_ PETSCVIEWERASCIISETFILEUNIT 511e4096674SBarry Smith #define petscfortranprinttounit_ PETSCFORTRANPRINTTOUNIT 512e4096674SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE) 513e4096674SBarry Smith #define petscviewerasciiopenwithfileunit_ petscviewerasciiopenwithfileunit 514e4096674SBarry Smith #define petscviewerasciisetfileunit_ petscviewerasciisetfileunit 515e4096674SBarry Smith #define petscfortranprinttounit_ petscfortranprinttounit 516e4096674SBarry Smith #endif 517e4096674SBarry Smith 518e4096674SBarry Smith #if defined(__cplusplus) 519e4096674SBarry Smith extern "C" void petscfortranprinttounit_(PetscInt *, const char *, PetscErrorCode *, PETSC_FORTRAN_CHARLEN_T); 520e4096674SBarry Smith #else 521e4096674SBarry Smith extern void petscfortranprinttounit_(PetscInt *, const char *, PetscErrorCode *, PETSC_FORTRAN_CHARLEN_T); 522e4096674SBarry Smith #endif 523e4096674SBarry Smith 524e4096674SBarry Smith #define PETSCDEFAULTBUFFERSIZE 8 * 1024 525e4096674SBarry Smith 52610450e9eSJacob Faibussowitsch // PetscClangLinter pragma disable: -fdoc-synopsis-macro-explicit-synopsis-valid-header 527489d2c6aSPierre Jolivet /*MC 528baca6076SPierre Jolivet PetscViewerASCIISetFileUnit - sets the `PETSCVIEWERASCII` to write to a Fortran IO unit 529e4096674SBarry Smith 53010450e9eSJacob Faibussowitsch Synopsis: 53110450e9eSJacob Faibussowitsch #include <petscviewer.h> 532e4096674SBarry Smith void PetscViewerASCIISetFileUnit(PetscViewer lab, PetscInt unit, PetscErrorCode ierr) 533e4096674SBarry Smith 534e4096674SBarry Smith Input Parameters: 535e4096674SBarry Smith + lab - the viewer 536e4096674SBarry Smith - unit - the unit number 537e4096674SBarry Smith 538e4096674SBarry Smith Output Parameter: 539e4096674SBarry Smith . ierr - the error code 540e4096674SBarry Smith 54110450e9eSJacob Faibussowitsch Level: intermediate 54210450e9eSJacob Faibussowitsch 543e4096674SBarry Smith Note: 544e4096674SBarry Smith `PetscViewerDestroy()` does not close the unit for this `PetscViewer` 545e4096674SBarry Smith 546aec76313SJacob Faibussowitsch Fortran Notes: 547e4096674SBarry Smith Only for Fortran, use `PetscViewerASCIISetFILE()` for C 548e4096674SBarry Smith 549e4096674SBarry Smith .seealso: `PetscViewerASCIISetFILE()`, `PETSCVIEWERASCII`, `PetscViewerASCIIOpenWithFileUnit()` 550489d2c6aSPierre Jolivet M*/ 551e4096674SBarry Smith PETSC_EXTERN void petscviewerasciisetfileunit_(PetscViewer *lab, PetscInt *unit, PetscErrorCode *ierr) 552e4096674SBarry Smith { 553e4096674SBarry Smith PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)(*lab)->data; 554e4096674SBarry Smith 555e4096674SBarry Smith if (vascii->mode == FILE_MODE_READ) { 556e4096674SBarry Smith *ierr = PETSC_ERR_ARG_WRONGSTATE; 557e4096674SBarry Smith return; 558e4096674SBarry Smith } 559e4096674SBarry Smith vascii->fileunit = *unit; 560e4096674SBarry Smith } 561e4096674SBarry Smith 56210450e9eSJacob Faibussowitsch // PetscClangLinter pragma disable: -fdoc-synopsis-macro-explicit-synopsis-valid-header 563489d2c6aSPierre Jolivet /*MC 564baca6076SPierre Jolivet PetscViewerASCIIOpenWithFileUnit - opens a `PETSCVIEWERASCII` to write to a Fortran IO unit 565e4096674SBarry Smith 56610450e9eSJacob Faibussowitsch Synopsis: 56710450e9eSJacob Faibussowitsch #include <petscviewer.h> 568e4096674SBarry Smith void PetscViewerASCIIOpenWithFileUnit(MPI_Comm comm, PetscInt unit, PetscViewer viewer, PetscErrorCode ierr) 569e4096674SBarry Smith 570e4096674SBarry Smith Input Parameters: 571e4096674SBarry Smith + comm - the `MPI_Comm` to share the viewer 572e4096674SBarry Smith - unit - the unit number 573e4096674SBarry Smith 574e4096674SBarry Smith Output Parameters: 575e4096674SBarry Smith + lab - the viewer 576e4096674SBarry Smith - ierr - the error code 577e4096674SBarry Smith 57810450e9eSJacob Faibussowitsch Level: intermediate 57910450e9eSJacob Faibussowitsch 580e4096674SBarry Smith Note: 581e4096674SBarry Smith `PetscViewerDestroy()` does not close the unit for this `PetscViewer` 582e4096674SBarry Smith 583aec76313SJacob Faibussowitsch Fortran Notes: 584e4096674SBarry Smith Only for Fortran, use `PetscViewerASCIIOpenWithFILE()` for C 585e4096674SBarry Smith 586e4096674SBarry Smith .seealso: `PetscViewerASCIISetFileUnit()`, `PetscViewerASCIISetFILE()`, `PETSCVIEWERASCII`, `PetscViewerASCIIOpenWithFILE()` 587489d2c6aSPierre Jolivet M*/ 588e4096674SBarry Smith PETSC_EXTERN void petscviewerasciiopenwithfileunit_(MPI_Comm *comm, PetscInt *unit, PetscViewer *lab, PetscErrorCode *ierr) 589e4096674SBarry Smith { 590e4096674SBarry Smith *ierr = PetscViewerCreate(MPI_Comm_f2c(*(MPI_Fint *)&*comm), lab); 591e4096674SBarry Smith if (*ierr) return; 592e4096674SBarry Smith *ierr = PetscViewerSetType(*lab, PETSCVIEWERASCII); 593e4096674SBarry Smith if (*ierr) return; 594e4096674SBarry Smith *ierr = PetscViewerFileSetMode(*lab, FILE_MODE_WRITE); 595e4096674SBarry Smith if (*ierr) return; 596e4096674SBarry Smith petscviewerasciisetfileunit_(lab, unit, ierr); 597e4096674SBarry Smith } 598e4096674SBarry Smith 599e4096674SBarry Smith static PetscErrorCode PetscVFPrintfFortran(PetscInt unit, const char format[], va_list Argp) 600e4096674SBarry Smith { 601e4096674SBarry Smith PetscErrorCode ierr; 602e4096674SBarry Smith char str[PETSCDEFAULTBUFFERSIZE]; 603e4096674SBarry Smith size_t len; 604e4096674SBarry Smith 605e4096674SBarry Smith PetscFunctionBegin; 606e4096674SBarry Smith PetscCall(PetscVSNPrintf(str, sizeof(str), format, NULL, Argp)); 607e4096674SBarry Smith PetscCall(PetscStrlen(str, &len)); 608e4096674SBarry Smith petscfortranprinttounit_(&unit, str, &ierr, (int)len); 609e4096674SBarry Smith PetscFunctionReturn(PETSC_SUCCESS); 610e4096674SBarry Smith } 611e4096674SBarry Smith 612e4096674SBarry Smith static PetscErrorCode PetscFPrintfFortran(PetscInt unit, const char str[]) 613e4096674SBarry Smith { 614e4096674SBarry Smith PetscErrorCode ierr; 615e4096674SBarry Smith size_t len; 616e4096674SBarry Smith 617e4096674SBarry Smith PetscFunctionBegin; 618e4096674SBarry Smith PetscCall(PetscStrlen(str, &len)); 619e4096674SBarry Smith petscfortranprinttounit_(&unit, str, &ierr, (int)len); 620e4096674SBarry Smith PetscFunctionReturn(PETSC_SUCCESS); 621e4096674SBarry Smith } 622e4096674SBarry Smith 623e4096674SBarry Smith #else 624e4096674SBarry Smith 625e4096674SBarry Smith /* these will never be used; but are needed to link with */ 626e4096674SBarry Smith static PetscErrorCode PetscVFPrintfFortran(PetscInt unit, const char format[], va_list Argp) 627e4096674SBarry Smith { 628e4096674SBarry Smith PetscFunctionBegin; 629e4096674SBarry Smith PetscFunctionReturn(PETSC_SUCCESS); 630e4096674SBarry Smith } 631e4096674SBarry Smith 632e4096674SBarry Smith static PetscErrorCode PetscFPrintfFortran(PetscInt unit, const char str[]) 633e4096674SBarry Smith { 634e4096674SBarry Smith PetscFunctionBegin; 635e4096674SBarry Smith PetscFunctionReturn(PETSC_SUCCESS); 636e4096674SBarry Smith } 637e4096674SBarry Smith #endif 638e4096674SBarry Smith 6395c6c1daeSBarry Smith /*@C 6405c6c1daeSBarry Smith PetscViewerASCIIPrintf - Prints to a file, only from the first 6413f423023SBarry Smith processor in the `PetscViewer` of type `PETSCVIEWERASCII` 6425c6c1daeSBarry Smith 643c410d8ccSBarry Smith Not Collective, but only the first MPI rank in the viewer has any effect 6445c6c1daeSBarry Smith 6455c6c1daeSBarry Smith Input Parameters: 646811af0c4SBarry Smith + viewer - obtained with `PetscViewerASCIIOpen()` 6475c6c1daeSBarry Smith - format - the usual printf() format string 6485c6c1daeSBarry Smith 6495c6c1daeSBarry Smith Level: developer 6505c6c1daeSBarry Smith 651aec76313SJacob Faibussowitsch Fortran Notes: 652c410d8ccSBarry Smith The call sequence is `PetscViewerASCIIPrintf`(`PetscViewer`, character(*), int ierr) from Fortran. 6535c6c1daeSBarry Smith That is, you can only pass a single character string from Fortran. 6545c6c1daeSBarry Smith 655d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscPrintf()`, `PetscSynchronizedPrintf()`, `PetscViewerASCIIOpen()`, 656db781477SPatrick Sanan `PetscViewerASCIIPushTab()`, `PetscViewerASCIIPopTab()`, `PetscViewerASCIISynchronizedPrintf()`, 657db781477SPatrick Sanan `PetscViewerCreate()`, `PetscViewerDestroy()`, `PetscViewerSetType()`, `PetscViewerASCIIGetPointer()`, `PetscViewerASCIIPushSynchronized()` 6585c6c1daeSBarry Smith @*/ 659d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIIPrintf(PetscViewer viewer, const char format[], ...) 660d71ae5a4SJacob Faibussowitsch { 6615c6c1daeSBarry Smith PetscViewer_ASCII *ascii = (PetscViewer_ASCII *)viewer->data; 6625c6c1daeSBarry Smith PetscMPIInt rank; 663fe8fb074SBarry Smith PetscInt tab = 0, intab = ascii->tab; 6645c6c1daeSBarry Smith FILE *fd = ascii->fd; 6653f08860eSBarry Smith PetscBool iascii; 6665c6c1daeSBarry Smith 6675c6c1daeSBarry Smith PetscFunctionBegin; 6685c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1); 66928b400f6SJacob Faibussowitsch PetscCheck(!ascii->sviewer, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_WRONGSTATE, "Cannot call with outstanding call to PetscViewerRestoreSubViewer()"); 6704f572ea9SToby Isaac PetscAssertPointer(format, 2); 6719566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii)); 67228b400f6SJacob Faibussowitsch PetscCheck(iascii, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Not ASCII PetscViewer"); 6739566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank)); 6743ba16761SJacob Faibussowitsch if (rank) PetscFunctionReturn(PETSC_SUCCESS); 6753f08860eSBarry Smith 6763f08860eSBarry Smith if (ascii->bviewer) { /* pass string up to parent viewer */ 6773f08860eSBarry Smith char *string; 6783f08860eSBarry Smith va_list Argp; 6793f08860eSBarry Smith size_t fullLength; 6803f08860eSBarry Smith 6819566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(QUEUESTRINGSIZE, &string)); 682fe8fb074SBarry Smith for (; tab < ascii->tab; tab++) { string[2 * tab] = string[2 * tab + 1] = ' '; } 6833f08860eSBarry Smith va_start(Argp, format); 684fe8fb074SBarry Smith PetscCall(PetscVSNPrintf(string + 2 * intab, QUEUESTRINGSIZE - 2 * intab, format, &fullLength, Argp)); 6853f08860eSBarry Smith va_end(Argp); 686fe8fb074SBarry Smith PetscCall(PetscViewerASCIISynchronizedPrintf(ascii->bviewer, "%s", string)); 6879566063dSJacob Faibussowitsch PetscCall(PetscFree(string)); 6883f08860eSBarry Smith } else { /* write directly to file */ 6895c6c1daeSBarry Smith va_list Argp; 690fe8fb074SBarry Smith 691dd2fa690SBarry Smith tab = intab; 692e4096674SBarry Smith while (tab--) { 693e4096674SBarry Smith if (!ascii->fileunit) PetscCall(PetscFPrintf(PETSC_COMM_SELF, fd, " ")); 694e4096674SBarry Smith else PetscCall(PetscFPrintfFortran(ascii->fileunit, " ")); 695e4096674SBarry Smith } 6965c6c1daeSBarry Smith 6975c6c1daeSBarry Smith va_start(Argp, format); 698e4096674SBarry Smith if (!ascii->fileunit) PetscCall((*PetscVFPrintf)(fd, format, Argp)); 699e4096674SBarry Smith else PetscCall(PetscVFPrintfFortran(ascii->fileunit, format, Argp)); 700eae3dc7dSJacob Faibussowitsch va_end(Argp); 701c69effb2SJacob Faibussowitsch PetscCall(PetscFFlush(fd)); 7025c6c1daeSBarry Smith } 7033ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 7045c6c1daeSBarry Smith } 7055c6c1daeSBarry Smith 7065c6c1daeSBarry Smith /*@C 707c410d8ccSBarry Smith PetscViewerFileSetName - Sets the name of the file the `PetscViewer` should use. 7085c6c1daeSBarry Smith 709c3339decSBarry Smith Collective 7105c6c1daeSBarry Smith 7115c6c1daeSBarry Smith Input Parameters: 7123f423023SBarry Smith + viewer - the `PetscViewer`; for example, of type `PETSCVIEWERASCII` or `PETSCVIEWERBINARY` 7135c6c1daeSBarry Smith - name - the name of the file it should use 7145c6c1daeSBarry Smith 7155c6c1daeSBarry Smith Level: advanced 7165c6c1daeSBarry Smith 717c410d8ccSBarry Smith Note: 718c410d8ccSBarry Smith This will have no effect on viewers that are not related to files 719c410d8ccSBarry Smith 720d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewerCreate()`, `PetscViewerSetType()`, `PetscViewerASCIIOpen()`, `PetscViewerBinaryOpen()`, `PetscViewerDestroy()`, 721db781477SPatrick Sanan `PetscViewerASCIIGetPointer()`, `PetscViewerASCIIPrintf()`, `PetscViewerASCIISynchronizedPrintf()` 7225c6c1daeSBarry Smith @*/ 723d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerFileSetName(PetscViewer viewer, const char name[]) 724d71ae5a4SJacob Faibussowitsch { 725cc843e7aSLisandro Dalcin char filename[PETSC_MAX_PATH_LEN]; 7265c6c1daeSBarry Smith 7275c6c1daeSBarry Smith PetscFunctionBegin; 7285c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1); 7294f572ea9SToby Isaac PetscAssertPointer(name, 2); 7309566063dSJacob Faibussowitsch PetscCall(PetscStrreplace(PetscObjectComm((PetscObject)viewer), name, filename, sizeof(filename))); 731cac4c232SBarry Smith PetscTryMethod(viewer, "PetscViewerFileSetName_C", (PetscViewer, const char[]), (viewer, filename)); 7323ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 7335c6c1daeSBarry Smith } 7345c6c1daeSBarry Smith 7355c6c1daeSBarry Smith /*@C 736c410d8ccSBarry Smith PetscViewerFileGetName - Gets the name of the file the `PetscViewer` is using 7375c6c1daeSBarry Smith 7385c6c1daeSBarry Smith Not Collective 7395c6c1daeSBarry Smith 7405c6c1daeSBarry Smith Input Parameter: 7413f423023SBarry Smith . viewer - the `PetscViewer` 7425c6c1daeSBarry Smith 7435c6c1daeSBarry Smith Output Parameter: 7445c6c1daeSBarry Smith . name - the name of the file it is using 7455c6c1daeSBarry Smith 7465c6c1daeSBarry Smith Level: advanced 7475c6c1daeSBarry Smith 748c410d8ccSBarry Smith Note: 749c410d8ccSBarry Smith This will have no effect on viewers that are not related to files 750c410d8ccSBarry Smith 751d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewerCreate()`, `PetscViewerSetType()`, `PetscViewerASCIIOpen()`, `PetscViewerBinaryOpen()`, `PetscViewerFileSetName()` 7525c6c1daeSBarry Smith @*/ 753d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerFileGetName(PetscViewer viewer, const char **name) 754d71ae5a4SJacob Faibussowitsch { 7555c6c1daeSBarry Smith PetscFunctionBegin; 7565c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1); 7574f572ea9SToby Isaac PetscAssertPointer(name, 2); 758cac4c232SBarry Smith PetscUseMethod(viewer, "PetscViewerFileGetName_C", (PetscViewer, const char **), (viewer, name)); 7593ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 7605c6c1daeSBarry Smith } 7615c6c1daeSBarry Smith 76234e79e72SJacob Faibussowitsch static PetscErrorCode PetscViewerFileGetName_ASCII(PetscViewer viewer, const char **name) 763d71ae5a4SJacob Faibussowitsch { 7645c6c1daeSBarry Smith PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data; 7655c6c1daeSBarry Smith 7665c6c1daeSBarry Smith PetscFunctionBegin; 7675c6c1daeSBarry Smith *name = vascii->filename; 7683ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 7695c6c1daeSBarry Smith } 7705c6c1daeSBarry Smith 771bf31d2d3SBarry Smith #include <errno.h> 77234e79e72SJacob Faibussowitsch static PetscErrorCode PetscViewerFileSetName_ASCII(PetscViewer viewer, const char name[]) 773d71ae5a4SJacob Faibussowitsch { 7745c6c1daeSBarry Smith size_t len; 775bbcf679cSJacob Faibussowitsch char fname[PETSC_MAX_PATH_LEN], *gz = NULL; 7765c6c1daeSBarry Smith PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data; 7775c6c1daeSBarry Smith PetscBool isstderr, isstdout; 7785c6c1daeSBarry Smith PetscMPIInt rank; 7795c6c1daeSBarry Smith 7805c6c1daeSBarry Smith PetscFunctionBegin; 7819566063dSJacob Faibussowitsch PetscCall(PetscViewerFileClose_ASCII(viewer)); 7823ba16761SJacob Faibussowitsch if (!name) PetscFunctionReturn(PETSC_SUCCESS); 7839566063dSJacob Faibussowitsch PetscCall(PetscStrallocpy(name, &vascii->filename)); 7845c6c1daeSBarry Smith 7855c6c1daeSBarry Smith /* Is this file to be compressed */ 7865c6c1daeSBarry Smith vascii->storecompressed = PETSC_FALSE; 787a297a907SKarl Rupp 7889566063dSJacob Faibussowitsch PetscCall(PetscStrstr(vascii->filename, ".gz", &gz)); 7895c6c1daeSBarry Smith if (gz) { 7909566063dSJacob Faibussowitsch PetscCall(PetscStrlen(gz, &len)); 7915c6c1daeSBarry Smith if (len == 3) { 79208401ef6SPierre 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"); 7935c6c1daeSBarry Smith *gz = 0; 7945c6c1daeSBarry Smith vascii->storecompressed = PETSC_TRUE; 7955c6c1daeSBarry Smith } 7965c6c1daeSBarry Smith } 7979566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank)); 798dd400576SPatrick Sanan if (rank == 0) { 7999566063dSJacob Faibussowitsch PetscCall(PetscStrcmp(name, "stderr", &isstderr)); 8009566063dSJacob Faibussowitsch PetscCall(PetscStrcmp(name, "stdout", &isstdout)); 8015c6c1daeSBarry Smith /* empty filename means stdout */ 8025c6c1daeSBarry Smith if (name[0] == 0) isstdout = PETSC_TRUE; 8035c6c1daeSBarry Smith if (isstderr) vascii->fd = PETSC_STDERR; 8045c6c1daeSBarry Smith else if (isstdout) vascii->fd = PETSC_STDOUT; 8055c6c1daeSBarry Smith else { 8069566063dSJacob Faibussowitsch PetscCall(PetscFixFilename(name, fname)); 8075c6c1daeSBarry Smith switch (vascii->mode) { 808d71ae5a4SJacob Faibussowitsch case FILE_MODE_READ: 809d71ae5a4SJacob Faibussowitsch vascii->fd = fopen(fname, "r"); 810d71ae5a4SJacob Faibussowitsch break; 811d71ae5a4SJacob Faibussowitsch case FILE_MODE_WRITE: 812d71ae5a4SJacob Faibussowitsch vascii->fd = fopen(fname, "w"); 813d71ae5a4SJacob Faibussowitsch break; 814d71ae5a4SJacob Faibussowitsch case FILE_MODE_APPEND: 815d71ae5a4SJacob Faibussowitsch vascii->fd = fopen(fname, "a"); 816d71ae5a4SJacob Faibussowitsch break; 8175c6c1daeSBarry Smith case FILE_MODE_UPDATE: 8185c6c1daeSBarry Smith vascii->fd = fopen(fname, "r+"); 819a297a907SKarl Rupp if (!vascii->fd) vascii->fd = fopen(fname, "w+"); 8205c6c1daeSBarry Smith break; 8215c6c1daeSBarry Smith case FILE_MODE_APPEND_UPDATE: 8225c6c1daeSBarry Smith /* I really want a file which is opened at the end for updating, 8235c6c1daeSBarry Smith not a+, which opens at the beginning, but makes writes at the end. 8245c6c1daeSBarry Smith */ 8255c6c1daeSBarry Smith vascii->fd = fopen(fname, "r+"); 826a297a907SKarl Rupp if (!vascii->fd) vascii->fd = fopen(fname, "w+"); 8273ba16761SJacob Faibussowitsch else { 8283ba16761SJacob Faibussowitsch int ret = fseek(vascii->fd, 0, SEEK_END); 8293ba16761SJacob Faibussowitsch PetscCheck(!ret, PETSC_COMM_SELF, PETSC_ERR_LIB, "fseek() failed with error code %d", ret); 8303ba16761SJacob Faibussowitsch } 8315c6c1daeSBarry Smith break; 832d71ae5a4SJacob Faibussowitsch default: 833d71ae5a4SJacob Faibussowitsch SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Unsupported file mode %s", PetscFileModes[vascii->mode]); 8345c6c1daeSBarry Smith } 835bf31d2d3SBarry Smith PetscCheck(vascii->fd, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Cannot open PetscViewer file: %s due to \"%s\"", fname, strerror(errno)); 8365c6c1daeSBarry Smith } 8375c6c1daeSBarry Smith } 8383ba16761SJacob Faibussowitsch PetscCall(PetscLogObjectState((PetscObject)viewer, "File: %s", name)); 8393ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 8405c6c1daeSBarry Smith } 8415c6c1daeSBarry Smith 84234e79e72SJacob Faibussowitsch static PetscErrorCode PetscViewerGetSubViewer_ASCII(PetscViewer viewer, MPI_Comm subcomm, PetscViewer *outviewer) 843d71ae5a4SJacob Faibussowitsch { 8445c6c1daeSBarry Smith PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data, *ovascii; 8455c6c1daeSBarry Smith 8465c6c1daeSBarry Smith PetscFunctionBegin; 84728b400f6SJacob Faibussowitsch PetscCheck(!vascii->sviewer, PETSC_COMM_SELF, PETSC_ERR_ORDER, "SubViewer already obtained from PetscViewer and not restored"); 848fe8fb074SBarry Smith PetscCall(PetscViewerASCIIPushSynchronized(viewer)); 849e5afcf28SBarry Smith /* 8509530cbd7SBarry Smith The following line is a bug; it does another PetscViewerASCIIPushSynchronized() on viewer, but if it is removed the code won't work 8519530cbd7SBarry 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 8529530cbd7SBarry Smith (since the count never gets to zero) in some examples this displays information that otherwise would be lost 8539530cbd7SBarry Smith 8549530cbd7SBarry Smith This code also means another call to PetscViewerASCIIPopSynchronized() must be made after the PetscViewerRestoreSubViewer(), see, for example, 8559530cbd7SBarry Smith PCView_GASM(). 856e5afcf28SBarry Smith */ 8579566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushSynchronized(viewer)); 858b4025f61SBarry Smith PetscCall(PetscViewerFlush(viewer)); 8599566063dSJacob Faibussowitsch PetscCall(PetscViewerCreate(subcomm, outviewer)); 8609566063dSJacob Faibussowitsch PetscCall(PetscViewerSetType(*outviewer, PETSCVIEWERASCII)); 8619566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushSynchronized(*outviewer)); 8625c6c1daeSBarry Smith ovascii = (PetscViewer_ASCII *)(*outviewer)->data; 8635c6c1daeSBarry Smith ovascii->fd = vascii->fd; 864ba5a0b41SBarry Smith ovascii->closefile = PETSC_FALSE; 8655c6c1daeSBarry Smith 8665c6c1daeSBarry Smith vascii->sviewer = *outviewer; 8675c6c1daeSBarry Smith (*outviewer)->format = viewer->format; 8685c6c1daeSBarry Smith ((PetscViewer_ASCII *)((*outviewer)->data))->bviewer = viewer; 8693f08860eSBarry Smith (*outviewer)->ops->destroy = PetscViewerDestroy_ASCII_SubViewer; 8703ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 8715c6c1daeSBarry Smith } 8725c6c1daeSBarry Smith 87334e79e72SJacob Faibussowitsch static PetscErrorCode PetscViewerRestoreSubViewer_ASCII(PetscViewer viewer, MPI_Comm comm, PetscViewer *outviewer) 874d71ae5a4SJacob Faibussowitsch { 8755c6c1daeSBarry Smith PetscViewer_ASCII *ascii = (PetscViewer_ASCII *)viewer->data; 8765c6c1daeSBarry Smith 8775c6c1daeSBarry Smith PetscFunctionBegin; 87828b400f6SJacob Faibussowitsch PetscCheck(ascii->sviewer, PETSC_COMM_SELF, PETSC_ERR_ORDER, "SubViewer never obtained from PetscViewer"); 87908401ef6SPierre Jolivet PetscCheck(ascii->sviewer == *outviewer, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "This PetscViewer did not generate this SubViewer"); 8805c6c1daeSBarry Smith 8819566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopSynchronized(*outviewer)); 882e5afcf28SBarry Smith ascii->sviewer = NULL; 8835c6c1daeSBarry Smith (*outviewer)->ops->destroy = PetscViewerDestroy_ASCII; 8849566063dSJacob Faibussowitsch PetscCall(PetscViewerDestroy(outviewer)); 885fe8fb074SBarry Smith PetscCall(PetscViewerFlush(viewer)); 8869566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopSynchronized(viewer)); 8873ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 8885c6c1daeSBarry Smith } 8895c6c1daeSBarry Smith 89034e79e72SJacob Faibussowitsch static PetscErrorCode PetscViewerView_ASCII(PetscViewer v, PetscViewer viewer) 891d71ae5a4SJacob Faibussowitsch { 8922bf49c77SBarry Smith PetscViewer_ASCII *ascii = (PetscViewer_ASCII *)v->data; 8932bf49c77SBarry Smith 8942bf49c77SBarry Smith PetscFunctionBegin; 89548a46eb9SPierre Jolivet if (ascii->filename) PetscCall(PetscViewerASCIIPrintf(viewer, "Filename: %s\n", ascii->filename)); 8963ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 8972bf49c77SBarry Smith } 8982bf49c77SBarry Smith 8998556b5ebSBarry Smith /*MC 9008556b5ebSBarry Smith PETSCVIEWERASCII - A viewer that prints to stdout or an ASCII file 9018556b5ebSBarry Smith 902811af0c4SBarry Smith Level: beginner 903811af0c4SBarry Smith 904d1f92df0SBarry Smith .seealso: [](sec_viewers), `PETSC_VIEWER_STDOUT_()`, `PETSC_VIEWER_STDOUT_SELF`, `PETSC_VIEWER_STDOUT_WORLD`, `PetscViewerCreate()`, `PetscViewerASCIIOpen()`, 905db781477SPatrick Sanan `PetscViewerMatlabOpen()`, `VecView()`, `DMView()`, `PetscViewerMatlabPutArray()`, `PETSCVIEWERBINARY`, `PETSCVIEWERMATLAB`, 906db781477SPatrick Sanan `PetscViewerFileSetName()`, `PetscViewerFileSetMode()`, `PetscViewerFormat`, `PetscViewerType`, `PetscViewerSetType()` 9078556b5ebSBarry Smith M*/ 908d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscViewerCreate_ASCII(PetscViewer viewer) 909d71ae5a4SJacob Faibussowitsch { 9105c6c1daeSBarry Smith PetscViewer_ASCII *vascii; 9115c6c1daeSBarry Smith 9125c6c1daeSBarry Smith PetscFunctionBegin; 9134dfa11a4SJacob Faibussowitsch PetscCall(PetscNew(&vascii)); 9145c6c1daeSBarry Smith viewer->data = (void *)vascii; 9155c6c1daeSBarry Smith 9165c6c1daeSBarry Smith viewer->ops->destroy = PetscViewerDestroy_ASCII; 9175c6c1daeSBarry Smith viewer->ops->flush = PetscViewerFlush_ASCII; 918559f443fSBarry Smith viewer->ops->getsubviewer = PetscViewerGetSubViewer_ASCII; 919559f443fSBarry Smith viewer->ops->restoresubviewer = PetscViewerRestoreSubViewer_ASCII; 9202bf49c77SBarry Smith viewer->ops->view = PetscViewerView_ASCII; 9211d641e7bSMichael Lange viewer->ops->read = PetscViewerASCIIRead; 9225c6c1daeSBarry Smith 9235c6c1daeSBarry Smith /* defaults to stdout unless set with PetscViewerFileSetName() */ 9245c6c1daeSBarry Smith vascii->fd = PETSC_STDOUT; 9255c6c1daeSBarry Smith vascii->mode = FILE_MODE_WRITE; 92602c9f0b5SLisandro Dalcin vascii->bviewer = NULL; 92702c9f0b5SLisandro Dalcin vascii->subviewer = NULL; 92802c9f0b5SLisandro Dalcin vascii->sviewer = NULL; 9295c6c1daeSBarry Smith vascii->tab = 0; 9305c6c1daeSBarry Smith vascii->tab_store = 0; 93102c9f0b5SLisandro Dalcin vascii->filename = NULL; 9325c6c1daeSBarry Smith vascii->closefile = PETSC_TRUE; 9335c6c1daeSBarry Smith 9349566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetName_C", PetscViewerFileSetName_ASCII)); 9359566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetName_C", PetscViewerFileGetName_ASCII)); 9369566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", PetscViewerFileGetMode_ASCII)); 9379566063dSJacob Faibussowitsch PetscCall(PetscObjectComposeFunction((PetscObject)viewer, "PetscViewerFileSetMode_C", PetscViewerFileSetMode_ASCII)); 9383ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 9395c6c1daeSBarry Smith } 9405c6c1daeSBarry Smith 9415c6c1daeSBarry Smith /*@C 942c410d8ccSBarry Smith PetscViewerASCIISynchronizedPrintf - Prints synchronized output to the specified `PETSCVIEWERASCII` file from 9435c6c1daeSBarry Smith several processors. Output of the first processor is followed by that of the 9445c6c1daeSBarry Smith second, etc. 9455c6c1daeSBarry Smith 946c410d8ccSBarry Smith Not Collective, must call collective `PetscViewerFlush()` to get the results flushed 9475c6c1daeSBarry Smith 9485c6c1daeSBarry Smith Input Parameters: 949811af0c4SBarry Smith + viewer - the `PETSCVIEWERASCII` `PetscViewer` 9505c6c1daeSBarry Smith - format - the usual printf() format string 9515c6c1daeSBarry Smith 9525c6c1daeSBarry Smith Level: intermediate 9535c6c1daeSBarry Smith 95495452b02SPatrick Sanan Notes: 955811af0c4SBarry Smith You must have previously called `PetscViewerASCIIPushSynchronized()` to allow this routine to be called. 956e6abc3ddSVáclav Hapla Then you can do multiple independent calls to this routine. 957811af0c4SBarry Smith 958811af0c4SBarry Smith The actual synchronized print is then done using `PetscViewerFlush()`. 959811af0c4SBarry Smith `PetscViewerASCIIPopSynchronized()` should be then called if we are already done with the synchronized output 960e6abc3ddSVáclav Hapla to conclude the "synchronized session". 961811af0c4SBarry Smith 962e6abc3ddSVáclav Hapla So the typical calling sequence looks like 963811af0c4SBarry Smith .vb 964811af0c4SBarry Smith PetscViewerASCIIPushSynchronized(viewer); 965811af0c4SBarry Smith PetscViewerASCIISynchronizedPrintf(viewer, ...); 966811af0c4SBarry Smith PetscViewerASCIISynchronizedPrintf(viewer, ...); 967811af0c4SBarry Smith ... 968811af0c4SBarry Smith PetscViewerFlush(viewer); 969811af0c4SBarry Smith PetscViewerASCIISynchronizedPrintf(viewer, ...); 970811af0c4SBarry Smith PetscViewerASCIISynchronizedPrintf(viewer, ...); 971811af0c4SBarry Smith ... 972811af0c4SBarry Smith PetscViewerFlush(viewer); 973811af0c4SBarry Smith PetscViewerASCIIPopSynchronized(viewer); 974811af0c4SBarry Smith .ve 9755c6c1daeSBarry Smith 976aec76313SJacob Faibussowitsch Fortran Notes: 9775c6c1daeSBarry Smith Can only print a single character* string 9785c6c1daeSBarry Smith 979d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewerASCIIPushSynchronized()`, `PetscViewerFlush()`, `PetscViewerASCIIPopSynchronized()`, 980db781477SPatrick Sanan `PetscSynchronizedPrintf()`, `PetscViewerASCIIPrintf()`, `PetscViewerASCIIOpen()`, 981db781477SPatrick Sanan `PetscViewerCreate()`, `PetscViewerDestroy()`, `PetscViewerSetType()` 9825c6c1daeSBarry Smith @*/ 983d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIISynchronizedPrintf(PetscViewer viewer, const char format[], ...) 984d71ae5a4SJacob Faibussowitsch { 9855c6c1daeSBarry Smith PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data; 9863f08860eSBarry Smith PetscMPIInt rank; 987fe8fb074SBarry Smith PetscInt tab = 0; 9885c6c1daeSBarry Smith MPI_Comm comm; 989fe8fb074SBarry Smith PetscBool iascii; 9905c6c1daeSBarry Smith 9915c6c1daeSBarry Smith PetscFunctionBegin; 9925c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1); 9934f572ea9SToby Isaac PetscAssertPointer(format, 2); 9949566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii)); 99528b400f6SJacob Faibussowitsch PetscCheck(iascii, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Not ASCII PetscViewer"); 99628b400f6SJacob Faibussowitsch PetscCheck(vascii->allowsynchronized, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "First call PetscViewerASCIIPushSynchronized() to allow this call"); 9975c6c1daeSBarry Smith 9989566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)viewer, &comm)); 9999566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 10005c6c1daeSBarry Smith 1001559f443fSBarry Smith if (vascii->bviewer) { 1002fe8fb074SBarry Smith char *string; 10035c6c1daeSBarry Smith va_list Argp; 1004fe8fb074SBarry Smith size_t fullLength; 10055c6c1daeSBarry Smith 1006fe8fb074SBarry Smith PetscCall(PetscCalloc1(QUEUESTRINGSIZE, &string)); 1007fe8fb074SBarry Smith for (; tab < vascii->tab; tab++) { string[2 * tab] = string[2 * tab + 1] = ' '; } 1008fe8fb074SBarry Smith va_start(Argp, format); 1009fe8fb074SBarry Smith PetscCall(PetscVSNPrintf(string + 2 * tab, QUEUESTRINGSIZE - 2 * tab, format, &fullLength, Argp)); 1010fe8fb074SBarry Smith va_end(Argp); 1011fe8fb074SBarry Smith PetscCall(PetscViewerASCIISynchronizedPrintf(vascii->bviewer, "%s", string)); 1012fe8fb074SBarry Smith PetscCall(PetscFree(string)); 1013fe8fb074SBarry Smith } else if (rank == 0) { /* First processor prints immediately to fp */ 1014fe8fb074SBarry Smith va_list Argp; 1015fe8fb074SBarry Smith FILE *fp = vascii->fd; 1016fe8fb074SBarry Smith 1017fe8fb074SBarry Smith tab = vascii->tab; 101848a46eb9SPierre Jolivet while (tab--) PetscCall(PetscFPrintf(PETSC_COMM_SELF, fp, " ")); 10195c6c1daeSBarry Smith 10205c6c1daeSBarry Smith va_start(Argp, format); 10219566063dSJacob Faibussowitsch PetscCall((*PetscVFPrintf)(fp, format, Argp)); 1022eae3dc7dSJacob Faibussowitsch va_end(Argp); 1023c69effb2SJacob Faibussowitsch PetscCall(PetscFFlush(fp)); 10245c6c1daeSBarry Smith if (petsc_history) { 10255c6c1daeSBarry Smith va_start(Argp, format); 10269566063dSJacob Faibussowitsch PetscCall((*PetscVFPrintf)(petsc_history, format, Argp)); 1027eae3dc7dSJacob Faibussowitsch va_end(Argp); 1028c69effb2SJacob Faibussowitsch PetscCall(PetscFFlush(petsc_history)); 10295c6c1daeSBarry Smith } 10305c6c1daeSBarry Smith va_end(Argp); 1031559f443fSBarry Smith } else { /* other processors add to queue */ 10325c6c1daeSBarry Smith char *string; 10335c6c1daeSBarry Smith va_list Argp; 10345c6c1daeSBarry Smith size_t fullLength; 10355c6c1daeSBarry Smith PrintfQueue next; 10365c6c1daeSBarry Smith 10379566063dSJacob Faibussowitsch PetscCall(PetscNew(&next)); 1038559f443fSBarry Smith if (vascii->petsc_printfqueue) { 1039559f443fSBarry Smith vascii->petsc_printfqueue->next = next; 1040559f443fSBarry Smith vascii->petsc_printfqueue = next; 1041a297a907SKarl Rupp } else { 1042559f443fSBarry Smith vascii->petsc_printfqueuebase = vascii->petsc_printfqueue = next; 1043a297a907SKarl Rupp } 1044559f443fSBarry Smith vascii->petsc_printfqueuelength++; 10455c6c1daeSBarry Smith next->size = QUEUESTRINGSIZE; 10469566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(next->size, &next->string)); 10475c6c1daeSBarry Smith string = next->string; 1048f15bb73eSStefano Zampini 1049f15bb73eSStefano Zampini tab = vascii->tab; 10505c6c1daeSBarry Smith tab *= 2; 1051ad540459SPierre Jolivet while (tab--) *string++ = ' '; 10525c6c1daeSBarry Smith va_start(Argp, format); 10539566063dSJacob Faibussowitsch PetscCall(PetscVSNPrintf(string, next->size - 2 * vascii->tab, format, &fullLength, Argp)); 10545c6c1daeSBarry Smith va_end(Argp); 1055cb500232SBarry Smith if (fullLength > (size_t)(next->size - 2 * vascii->tab)) { 10569566063dSJacob Faibussowitsch PetscCall(PetscFree(next->string)); 105714416c0eSBarry Smith next->size = fullLength + 2 * vascii->tab; 10589566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(next->size, &next->string)); 105914416c0eSBarry Smith string = next->string; 106014416c0eSBarry Smith tab = 2 * vascii->tab; 1061ad540459SPierre Jolivet while (tab--) *string++ = ' '; 106214416c0eSBarry Smith va_start(Argp, format); 10639566063dSJacob Faibussowitsch PetscCall(PetscVSNPrintf(string, next->size - 2 * vascii->tab, format, NULL, Argp)); 106414416c0eSBarry Smith va_end(Argp); 106514416c0eSBarry Smith } 10665c6c1daeSBarry Smith } 10673ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 10685c6c1daeSBarry Smith } 10695c6c1daeSBarry Smith 10702655f987SMichael Lange /*@C 1071c410d8ccSBarry Smith PetscViewerASCIIRead - Reads from a `PETSCVIEWERASCII` file 10722655f987SMichael Lange 1073c410d8ccSBarry Smith Only MPI rank 0 in the `PetscViewer` may call this 10742655f987SMichael Lange 10752655f987SMichael Lange Input Parameters: 10763f423023SBarry Smith + viewer - the `PETSCVIEWERASCII` viewer 1077c410d8ccSBarry Smith . data - location to write the data, treated as an array of type indicated by `datatype` 1078060da220SMatthew G. Knepley . num - number of items of data to read 1079aec76313SJacob Faibussowitsch - dtype - type of data to read 10802655f987SMichael Lange 108120f4b53cSBarry Smith Output Parameter: 10823f423023SBarry Smith . count - number of items of data actually read, or `NULL` 1083f8e4bde8SMatthew G. Knepley 10842655f987SMichael Lange Level: beginner 10852655f987SMichael Lange 1086d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewerASCIIOpen()`, `PetscViewerPushFormat()`, `PetscViewerDestroy()`, `PetscViewerCreate()`, `PetscViewerFileSetMode()`, `PetscViewerFileSetName()` 1087db781477SPatrick Sanan `VecView()`, `MatView()`, `VecLoad()`, `MatLoad()`, `PetscViewerBinaryGetDescriptor()`, 1088db781477SPatrick Sanan `PetscViewerBinaryGetInfoPointer()`, `PetscFileMode`, `PetscViewer`, `PetscViewerBinaryRead()` 10892655f987SMichael Lange @*/ 1090d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIIRead(PetscViewer viewer, void *data, PetscInt num, PetscInt *count, PetscDataType dtype) 1091d71ae5a4SJacob Faibussowitsch { 10922655f987SMichael Lange PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data; 10932655f987SMichael Lange FILE *fd = vascii->fd; 10942655f987SMichael Lange PetscInt i; 10953b7fe8c3SMatthew G. Knepley int ret = 0; 1096f8859db6SBarry Smith PetscMPIInt rank; 10972655f987SMichael Lange 10982655f987SMichael Lange PetscFunctionBegin; 10992655f987SMichael Lange PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1); 11009566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank)); 1101c5853193SPierre Jolivet PetscCheck(rank == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Can only be called from process 0 in the PetscViewer"); 1102060da220SMatthew G. Knepley for (i = 0; i < num; i++) { 1103f8e4bde8SMatthew G. Knepley if (dtype == PETSC_CHAR) ret = fscanf(fd, "%c", &(((char *)data)[i])); 1104f8e4bde8SMatthew G. Knepley else if (dtype == PETSC_STRING) ret = fscanf(fd, "%s", &(((char *)data)[i])); 1105a05e1a72SSatish Balay else if (dtype == PETSC_INT) ret = fscanf(fd, "%" PetscInt_FMT, &(((PetscInt *)data)[i])); 1106f8e4bde8SMatthew G. Knepley else if (dtype == PETSC_ENUM) ret = fscanf(fd, "%d", &(((int *)data)[i])); 11079e3e4c22SLisandro Dalcin else if (dtype == PETSC_INT64) ret = fscanf(fd, "%" PetscInt64_FMT, &(((PetscInt64 *)data)[i])); 1108972064b6SLisandro Dalcin else if (dtype == PETSC_LONG) ret = fscanf(fd, "%ld", &(((long *)data)[i])); 1109f8e4bde8SMatthew G. Knepley else if (dtype == PETSC_FLOAT) ret = fscanf(fd, "%f", &(((float *)data)[i])); 1110f8e4bde8SMatthew G. Knepley else if (dtype == PETSC_DOUBLE) ret = fscanf(fd, "%lg", &(((double *)data)[i])); 1111a6e181c6SToby Isaac #if defined(PETSC_USE_REAL___FLOAT128) 1112fba955ccSBarry Smith else if (dtype == PETSC___FLOAT128) { 1113fba955ccSBarry Smith double tmp; 1114fba955ccSBarry Smith ret = fscanf(fd, "%lg", &tmp); 1115a6e181c6SToby Isaac ((__float128 *)data)[i] = tmp; 1116a6e181c6SToby Isaac } 1117fba955ccSBarry Smith #endif 11189371c9d4SSatish Balay else 11199371c9d4SSatish Balay SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Data type %d not supported", (int)dtype); 112028b400f6SJacob Faibussowitsch PetscCheck(ret, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Conversion error for data type %d", (int)dtype); 1121f7d195e4SLawrence Mitchell if (ret < 0) break; /* Proxy for EOF, need to check for it in configure */ 11222655f987SMichael Lange } 1123060da220SMatthew G. Knepley if (count) *count = i; 112408401ef6SPierre Jolivet else PetscCheck(ret >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Insufficient data, read only %" PetscInt_FMT " < %" PetscInt_FMT " items", i, num); 11253ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 11262655f987SMichael Lange } 1127