xref: /petsc/src/sys/classes/viewer/impls/ascii/vcreatea.c (revision 3ba1676111f5c958fe6c2729b46ca4d523958bb3)
15c6c1daeSBarry Smith 
2f5860696SSatish Balay #include <../src/sys/classes/viewer/impls/ascii/asciiimpl.h> /*I     "petscviewer.h"   I*/
35c6c1daeSBarry Smith 
45c6c1daeSBarry Smith /* ---------------------------------------------------------------------*/
55c6c1daeSBarry Smith 
6e2dcd6d3SBarry Smith /*
7e2dcd6d3SBarry Smith     The variable Petsc_Viewer_Stdout_keyval is used to indicate an MPI attribute that
8e2dcd6d3SBarry Smith   is attached to a communicator, in this case the attribute is a PetscViewer.
9e2dcd6d3SBarry Smith */
10d4c7638eSBarry Smith PetscMPIInt Petsc_Viewer_Stdout_keyval = MPI_KEYVAL_INVALID;
11e2dcd6d3SBarry Smith 
129c5ded49SBarry Smith /*@
13811af0c4SBarry Smith    PetscViewerASCIIGetStdout - Creates a `PETSCVIEWERASCII` `PetscViewer` shared by all processors
14811af0c4SBarry Smith                     in a communicator. Error returning version of `PETSC_VIEWER_STDOUT_()`
155c6c1daeSBarry Smith 
16d083f849SBarry Smith    Collective
175c6c1daeSBarry Smith 
185c6c1daeSBarry Smith    Input Parameter:
19811af0c4SBarry Smith .  comm - the MPI communicator to share the `PetscViewer`
205c6c1daeSBarry Smith 
215c6c1daeSBarry Smith    Level: beginner
225c6c1daeSBarry Smith 
23811af0c4SBarry Smith    Note:
24811af0c4SBarry Smith      This should be used in all PETSc source code instead of `PETSC_VIEWER_STDOUT_()`
255c6c1daeSBarry Smith 
26d1f92df0SBarry Smith .seealso: [](sec_viewers), `PETSC_VIEWER_DRAW_()`, `PetscViewerASCIIOpen()`, `PETSC_VIEWER_STDERR_`, `PETSC_VIEWER_STDOUT_WORLD`,
27db781477SPatrick Sanan           `PETSC_VIEWER_STDOUT_SELF`
285c6c1daeSBarry Smith @*/
29d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIIGetStdout(MPI_Comm comm, PetscViewer *viewer)
30d71ae5a4SJacob Faibussowitsch {
31e2dcd6d3SBarry Smith   PetscBool flg;
32e2dcd6d3SBarry Smith   MPI_Comm  ncomm;
335c6c1daeSBarry Smith 
345c6c1daeSBarry Smith   PetscFunctionBegin;
359566063dSJacob Faibussowitsch   PetscCall(PetscSpinlockLock(&PetscViewerASCIISpinLockStdout));
369566063dSJacob Faibussowitsch   PetscCall(PetscCommDuplicate(comm, &ncomm, NULL));
3748a46eb9SPierre Jolivet   if (Petsc_Viewer_Stdout_keyval == MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN, MPI_COMM_NULL_DELETE_FN, &Petsc_Viewer_Stdout_keyval, NULL));
389566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_get_attr(ncomm, Petsc_Viewer_Stdout_keyval, (void **)viewer, (PetscMPIInt *)&flg));
39e2dcd6d3SBarry Smith   if (!flg) { /* PetscViewer not yet created */
409566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIOpen(ncomm, "stdout", viewer));
419566063dSJacob Faibussowitsch     PetscCall(PetscObjectRegisterDestroy((PetscObject)*viewer));
429566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_set_attr(ncomm, Petsc_Viewer_Stdout_keyval, (void *)*viewer));
43e2dcd6d3SBarry Smith   }
449566063dSJacob Faibussowitsch   PetscCall(PetscCommDestroy(&ncomm));
459566063dSJacob Faibussowitsch   PetscCall(PetscSpinlockUnlock(&PetscViewerASCIISpinLockStdout));
46*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
475c6c1daeSBarry Smith }
485c6c1daeSBarry Smith 
495c6c1daeSBarry Smith /*@C
50811af0c4SBarry Smith    PETSC_VIEWER_STDOUT_ - Creates a ASCII `PetscViewer` shared by all processors
515c6c1daeSBarry Smith                     in a communicator.
525c6c1daeSBarry Smith 
53d083f849SBarry Smith    Collective
545c6c1daeSBarry Smith 
555c6c1daeSBarry Smith    Input Parameter:
56811af0c4SBarry Smith .  comm - the MPI communicator to share the `PetscViewer`
575c6c1daeSBarry Smith 
585c6c1daeSBarry Smith    Level: beginner
595c6c1daeSBarry Smith 
60811af0c4SBarry Smith    Note:
615c6c1daeSBarry Smith    Unlike almost all other PETSc routines, this does not return
625c6c1daeSBarry Smith    an error code. Usually used in the form
635c6c1daeSBarry Smith $      XXXView(XXX object,PETSC_VIEWER_STDOUT_(comm));
645c6c1daeSBarry Smith 
65d1f92df0SBarry Smith .seealso: [](sec_viewers), `PETSC_VIEWER_DRAW_()`, `PetscViewerASCIIOpen()`, `PETSC_VIEWER_STDERR_`, `PETSC_VIEWER_STDOUT_WORLD`,
66db781477SPatrick Sanan           `PETSC_VIEWER_STDOUT_SELF`
675c6c1daeSBarry Smith @*/
68d71ae5a4SJacob Faibussowitsch PetscViewer PETSC_VIEWER_STDOUT_(MPI_Comm comm)
69d71ae5a4SJacob Faibussowitsch {
705c6c1daeSBarry Smith   PetscErrorCode ierr;
715c6c1daeSBarry Smith   PetscViewer    viewer;
725c6c1daeSBarry Smith 
735c6c1daeSBarry Smith   PetscFunctionBegin;
745c6c1daeSBarry Smith   ierr = PetscViewerASCIIGetStdout(comm, &viewer);
759371c9d4SSatish Balay   if (ierr) {
76*3ba16761SJacob Faibussowitsch     ierr = PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_STDOUT_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_INITIAL, " ");
779371c9d4SSatish Balay     PetscFunctionReturn(NULL);
789371c9d4SSatish Balay   }
795c6c1daeSBarry Smith   PetscFunctionReturn(viewer);
805c6c1daeSBarry Smith }
815c6c1daeSBarry Smith 
825c6c1daeSBarry Smith /* ---------------------------------------------------------------------*/
835c6c1daeSBarry Smith 
84e2dcd6d3SBarry Smith /*
85e2dcd6d3SBarry Smith     The variable Petsc_Viewer_Stderr_keyval is used to indicate an MPI attribute that
86e2dcd6d3SBarry Smith   is attached to a communicator, in this case the attribute is a PetscViewer.
87e2dcd6d3SBarry Smith */
88d4c7638eSBarry Smith PetscMPIInt Petsc_Viewer_Stderr_keyval = MPI_KEYVAL_INVALID;
89e2dcd6d3SBarry Smith 
909c5ded49SBarry Smith /*@
91811af0c4SBarry Smith    PetscViewerASCIIGetStderr - Creates a `PETSCVIEWERASCII` `PetscViewer` shared by all processors
92811af0c4SBarry Smith                     in a communicator. Error returning version of `PETSC_VIEWER_STDERR_()`
935c6c1daeSBarry Smith 
94d083f849SBarry Smith    Collective
955c6c1daeSBarry Smith 
965c6c1daeSBarry Smith    Input Parameter:
97811af0c4SBarry Smith .  comm - the MPI communicator to share the `PetscViewer`
985c6c1daeSBarry Smith 
995c6c1daeSBarry Smith    Level: beginner
1005c6c1daeSBarry Smith 
101811af0c4SBarry Smith    Note:
102811af0c4SBarry Smith      This should be used in all PETSc source code instead of `PETSC_VIEWER_STDERR_()`
1035c6c1daeSBarry Smith 
104d1f92df0SBarry Smith .seealso: [](sec_viewers), `PETSC_VIEWER_DRAW_()`, `PetscViewerASCIIOpen()`, `PETSC_VIEWER_STDERR_`, `PETSC_VIEWER_STDERR_WORLD`,
105db781477SPatrick Sanan           `PETSC_VIEWER_STDERR_SELF`
1065c6c1daeSBarry Smith @*/
107d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIIGetStderr(MPI_Comm comm, PetscViewer *viewer)
108d71ae5a4SJacob Faibussowitsch {
109e2dcd6d3SBarry Smith   PetscBool flg;
110e2dcd6d3SBarry Smith   MPI_Comm  ncomm;
111e2dcd6d3SBarry Smith 
112e2dcd6d3SBarry Smith   PetscFunctionBegin;
1139566063dSJacob Faibussowitsch   PetscCall(PetscSpinlockLock(&PetscViewerASCIISpinLockStderr));
1149566063dSJacob Faibussowitsch   PetscCall(PetscCommDuplicate(comm, &ncomm, NULL));
11548a46eb9SPierre Jolivet   if (Petsc_Viewer_Stderr_keyval == MPI_KEYVAL_INVALID) PetscCallMPI(MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN, MPI_COMM_NULL_DELETE_FN, &Petsc_Viewer_Stderr_keyval, NULL));
1169566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_get_attr(ncomm, Petsc_Viewer_Stderr_keyval, (void **)viewer, (PetscMPIInt *)&flg));
117e2dcd6d3SBarry Smith   if (!flg) { /* PetscViewer not yet created */
1189566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIOpen(ncomm, "stderr", viewer));
1199566063dSJacob Faibussowitsch     PetscCall(PetscObjectRegisterDestroy((PetscObject)*viewer));
1209566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_set_attr(ncomm, Petsc_Viewer_Stderr_keyval, (void *)*viewer));
121e2dcd6d3SBarry Smith   }
1229566063dSJacob Faibussowitsch   PetscCall(PetscCommDestroy(&ncomm));
1239566063dSJacob Faibussowitsch   PetscCall(PetscSpinlockUnlock(&PetscViewerASCIISpinLockStderr));
124*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1255c6c1daeSBarry Smith }
1265c6c1daeSBarry Smith 
1275c6c1daeSBarry Smith /*@C
128811af0c4SBarry Smith    PETSC_VIEWER_STDERR_ - Creates a `PETSCVIEWERASCII` `PetscViewer` shared by all processors
1295c6c1daeSBarry Smith                     in a communicator.
1305c6c1daeSBarry Smith 
131d083f849SBarry Smith    Collective
1325c6c1daeSBarry Smith 
1335c6c1daeSBarry Smith    Input Parameter:
134811af0c4SBarry Smith .  comm - the MPI communicator to share the `PetscViewer`
1355c6c1daeSBarry Smith 
1365c6c1daeSBarry Smith    Level: beginner
1375c6c1daeSBarry Smith 
1385c6c1daeSBarry Smith    Note:
1395c6c1daeSBarry Smith    Unlike almost all other PETSc routines, this does not return
1405c6c1daeSBarry Smith    an error code. Usually used in the form
1415c6c1daeSBarry Smith $      XXXView(XXX object,PETSC_VIEWER_STDERR_(comm));
1425c6c1daeSBarry Smith 
143d1f92df0SBarry Smith .seealso: [](sec_viewers), `PETSC_VIEWER_DRAW_`, `PetscViewerASCIIOpen()`, `PETSC_VIEWER_STDOUT_`, `PETSC_VIEWER_STDOUT_WORLD`,
144db781477SPatrick Sanan           `PETSC_VIEWER_STDOUT_SELF`, `PETSC_VIEWER_STDERR_WORLD`, `PETSC_VIEWER_STDERR_SELF`
1455c6c1daeSBarry Smith @*/
146d71ae5a4SJacob Faibussowitsch PetscViewer PETSC_VIEWER_STDERR_(MPI_Comm comm)
147d71ae5a4SJacob Faibussowitsch {
1485c6c1daeSBarry Smith   PetscErrorCode ierr;
1495c6c1daeSBarry Smith   PetscViewer    viewer;
1505c6c1daeSBarry Smith 
1515c6c1daeSBarry Smith   PetscFunctionBegin;
1525c6c1daeSBarry Smith   ierr = PetscViewerASCIIGetStderr(comm, &viewer);
1539371c9d4SSatish Balay   if (ierr) {
154*3ba16761SJacob Faibussowitsch     ierr = PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_STDERR_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_INITIAL, " ");
1559371c9d4SSatish Balay     PetscFunctionReturn(NULL);
1569371c9d4SSatish Balay   }
1575c6c1daeSBarry Smith   PetscFunctionReturn(viewer);
1585c6c1daeSBarry Smith }
1595c6c1daeSBarry Smith 
1605c6c1daeSBarry Smith PetscMPIInt Petsc_Viewer_keyval = MPI_KEYVAL_INVALID;
1615c6c1daeSBarry Smith /*
1625c6c1daeSBarry Smith    Called with MPI_Comm_free() is called on a communicator that has a viewer as an attribute. The viewer is not actually destroyed because that is managed by
1635c6c1daeSBarry Smith    PetscObjectDestroyRegisterAll(). PetscViewerASCIIGetStdout() registers the viewer with PetscObjectDestroyRegister() to be destroyed when PetscFinalize() is called.
1645c6c1daeSBarry Smith 
1655c6c1daeSBarry Smith   This is called by MPI, not by users.
1665c6c1daeSBarry Smith 
1675c6c1daeSBarry Smith */
168d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscMPIInt MPIAPI Petsc_DelViewer(MPI_Comm comm, PetscMPIInt keyval, void *attr_val, void *extra_state)
169d71ae5a4SJacob Faibussowitsch {
1705c6c1daeSBarry Smith   PetscFunctionBegin;
1719566063dSJacob Faibussowitsch   PetscCallMPI(PetscInfo(NULL, "Removing viewer data attribute in an MPI_Comm %ld\n", (long)comm));
1725c6c1daeSBarry Smith   PetscFunctionReturn(MPI_SUCCESS);
1735c6c1daeSBarry Smith }
1745c6c1daeSBarry Smith 
1755c6c1daeSBarry Smith /*@C
176811af0c4SBarry Smith    PetscViewerASCIIOpen - Opens an ASCII file for writing as a `PetscViewer`.
1775c6c1daeSBarry Smith 
178d083f849SBarry Smith    Collective
1795c6c1daeSBarry Smith 
1805c6c1daeSBarry Smith    Input Parameters:
1815c6c1daeSBarry Smith +  comm - the communicator
1825c6c1daeSBarry Smith -  name - the file name
1835c6c1daeSBarry Smith 
1845c6c1daeSBarry Smith    Output Parameter:
1855c6c1daeSBarry Smith .  lab - the PetscViewer to use with the specified file
1865c6c1daeSBarry Smith 
1875c6c1daeSBarry Smith    Level: beginner
1885c6c1daeSBarry Smith 
1895c6c1daeSBarry Smith    Notes:
190f8859db6SBarry Smith    To open a ASCII file as a viewer for reading one must use the sequence
191811af0c4SBarry Smith .vb
192811af0c4SBarry Smith    PetscViewerCreate(comm,&lab);
193811af0c4SBarry Smith    PetscViewerSetType(lab,PETSCVIEWERASCII);
194811af0c4SBarry Smith    PetscViewerFileSetMode(lab,FILE_MODE_READ);
195811af0c4SBarry Smith    PetscViewerFileSetName(lab,name);
196811af0c4SBarry Smith .ve
197f8859db6SBarry Smith 
198811af0c4SBarry Smith    This `PetscViewer` can be destroyed with `PetscViewerDestroy()`.
1995c6c1daeSBarry Smith 
2002ea3bc1cSBarry Smith    The MPI communicator used here must match that used by the object one is viewing. For example if the
201811af0c4SBarry Smith    Mat was created with a `PETSC_COMM_WORLD`, then the Viewer must be created with `PETSC_COMM_WORLD`
2025c6c1daeSBarry Smith 
203811af0c4SBarry Smith    As shown below, `PetscViewerASCIIOpen()` is useful in conjunction with
204811af0c4SBarry Smith    `MatView()` and `VecView()`
2055c6c1daeSBarry Smith .vb
2065c6c1daeSBarry Smith      PetscViewerASCIIOpen(PETSC_COMM_WORLD,"mat.output",&viewer);
2075c6c1daeSBarry Smith      MatView(matrix,viewer);
2085c6c1daeSBarry Smith .ve
2095c6c1daeSBarry Smith 
210d1f92df0SBarry Smith .seealso: [](sec_viewers), `MatView()`, `VecView()`, `PetscViewerDestroy()`, `PetscViewerBinaryOpen()`, `PetscViewerASCIIRead()`, `PETSCVIEWERASCII`
211db781477SPatrick Sanan           `PetscViewerASCIIGetPointer()`, `PetscViewerPushFormat()`, `PETSC_VIEWER_STDOUT_`, `PETSC_VIEWER_STDERR_`,
212db781477SPatrick Sanan           `PETSC_VIEWER_STDOUT_WORLD`, `PETSC_VIEWER_STDOUT_SELF`,
2135c6c1daeSBarry Smith @*/
214d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIIOpen(MPI_Comm comm, const char name[], PetscViewer *lab)
215d71ae5a4SJacob Faibussowitsch {
2165c6c1daeSBarry Smith   PetscViewerLink *vlink, *nv;
2175c6c1daeSBarry Smith   PetscBool        flg, eq;
2185c6c1daeSBarry Smith   size_t           len;
2195c6c1daeSBarry Smith 
2205c6c1daeSBarry Smith   PetscFunctionBegin;
2219566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(name, &len));
2225c6c1daeSBarry Smith   if (!len) {
2239566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIGetStdout(comm, lab));
2249566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)*lab));
225*3ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
2265c6c1daeSBarry Smith   }
2279566063dSJacob Faibussowitsch   PetscCall(PetscSpinlockLock(&PetscViewerASCIISpinLockOpen));
22848a46eb9SPierre 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));
2292bf49c77SBarry Smith   /*
2302bf49c77SBarry Smith        It would be better to move this code to PetscFileSetName() but since it must return a preexiting communicator
2312bf49c77SBarry Smith      we cannot do that, since PetscFileSetName() takes a communicator that already exists.
2322bf49c77SBarry Smith 
2332bf49c77SBarry Smith       Plus if the original communicator that created the file has since been close this will not detect the old
2342bf49c77SBarry Smith       communictor and hence will overwrite the old data. It may be better to simply remove all this code
2352bf49c77SBarry Smith   */
2365c6c1daeSBarry Smith   /* make sure communicator is a PETSc communicator */
2379566063dSJacob Faibussowitsch   PetscCall(PetscCommDuplicate(comm, &comm, NULL));
2385c6c1daeSBarry Smith   /* has file already been opened into a viewer */
2399566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_get_attr(comm, Petsc_Viewer_keyval, (void **)&vlink, (PetscMPIInt *)&flg));
2405c6c1daeSBarry Smith   if (flg) {
2415c6c1daeSBarry Smith     while (vlink) {
2429566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, ((PetscViewer_ASCII *)(vlink->viewer->data))->filename, &eq));
2435c6c1daeSBarry Smith       if (eq) {
2449566063dSJacob Faibussowitsch         PetscCall(PetscObjectReference((PetscObject)vlink->viewer));
2455c6c1daeSBarry Smith         *lab = vlink->viewer;
2469566063dSJacob Faibussowitsch         PetscCall(PetscCommDestroy(&comm));
2479566063dSJacob Faibussowitsch         PetscCall(PetscSpinlockUnlock(&PetscViewerASCIISpinLockOpen));
248*3ba16761SJacob Faibussowitsch         PetscFunctionReturn(PETSC_SUCCESS);
2495c6c1daeSBarry Smith       }
2505c6c1daeSBarry Smith       vlink = vlink->next;
2515c6c1daeSBarry Smith     }
2525c6c1daeSBarry Smith   }
2539566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(comm, lab));
2549566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(*lab, PETSCVIEWERASCII));
2551baa6e33SBarry Smith   if (name) PetscCall(PetscViewerFileSetName(*lab, name));
2565c6c1daeSBarry Smith   /* save viewer into communicator if needed later */
2579566063dSJacob Faibussowitsch   PetscCall(PetscNew(&nv));
2585c6c1daeSBarry Smith   nv->viewer = *lab;
2595c6c1daeSBarry Smith   if (!flg) {
2609566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_set_attr(comm, Petsc_Viewer_keyval, nv));
2615c6c1daeSBarry Smith   } else {
2629566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_get_attr(comm, Petsc_Viewer_keyval, (void **)&vlink, (PetscMPIInt *)&flg));
2635c6c1daeSBarry Smith     if (vlink) {
2645c6c1daeSBarry Smith       while (vlink->next) vlink = vlink->next;
2655c6c1daeSBarry Smith       vlink->next = nv;
2665c6c1daeSBarry Smith     } else {
2679566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Comm_set_attr(comm, Petsc_Viewer_keyval, nv));
2685c6c1daeSBarry Smith     }
2695c6c1daeSBarry Smith   }
2709566063dSJacob Faibussowitsch   PetscCall(PetscCommDestroy(&comm));
2719566063dSJacob Faibussowitsch   PetscCall(PetscSpinlockUnlock(&PetscViewerASCIISpinLockOpen));
272*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2735c6c1daeSBarry Smith }
2745c6c1daeSBarry Smith 
2755c6c1daeSBarry Smith /*@C
2765c6c1daeSBarry Smith    PetscViewerASCIIOpenWithFILE - Given an open file creates an ASCII viewer that prints to it.
2775c6c1daeSBarry Smith 
278d083f849SBarry Smith    Collective
2795c6c1daeSBarry Smith 
2805c6c1daeSBarry Smith    Input Parameters:
2815c6c1daeSBarry Smith +  comm - the communicator
2825c6c1daeSBarry Smith -  fd - the FILE pointer
2835c6c1daeSBarry Smith 
2845c6c1daeSBarry Smith    Output Parameter:
285811af0c4SBarry Smith .  lab - the `PetscViewer` to use with the specified file
2865c6c1daeSBarry Smith 
2875c6c1daeSBarry Smith    Level: beginner
2885c6c1daeSBarry Smith 
2895c6c1daeSBarry Smith    Notes:
290811af0c4SBarry Smith    This `PetscViewer` can be destroyed with `PetscViewerDestroy()`, but the fd will NOT be closed.
2915c6c1daeSBarry Smith 
292811af0c4SBarry Smith    If a multiprocessor communicator is used (such as `PETSC_COMM_WORLD`),
2935c6c1daeSBarry Smith    then only the first processor in the group uses the file.  All other
2945c6c1daeSBarry Smith    processors send their data to the first processor to print.
2955c6c1daeSBarry Smith 
296d1f92df0SBarry Smith .seealso: [](sec_viewers), `MatView()`, `VecView()`, `PetscViewerDestroy()`, `PetscViewerBinaryOpen()`,
297db781477SPatrick Sanan           `PetscViewerASCIIGetPointer()`, `PetscViewerPushFormat()`, `PETSC_VIEWER_STDOUT_`, `PETSC_VIEWER_STDERR_`,
298811af0c4SBarry Smith           `PETSC_VIEWER_STDOUT_WORLD`, `PETSC_VIEWER_STDOUT_SELF`, `PetscViewerASCIIOpen()`, `PetscViewerASCIISetFILE()`, `PETSCVIEWERASCII`
2995c6c1daeSBarry Smith @*/
300d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIIOpenWithFILE(MPI_Comm comm, FILE *fd, PetscViewer *lab)
301d71ae5a4SJacob Faibussowitsch {
3025c6c1daeSBarry Smith   PetscFunctionBegin;
3039566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(comm, lab));
3049566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(*lab, PETSCVIEWERASCII));
3059566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISetFILE(*lab, fd));
306*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3075c6c1daeSBarry Smith }
3085c6c1daeSBarry Smith 
309811af0c4SBarry Smith /*@C
310811af0c4SBarry Smith    PetscViewerASCIISetFILE - Given an open file sets the ASCII viewer to use the file for output
311811af0c4SBarry Smith 
312811af0c4SBarry Smith    Not collective
313811af0c4SBarry Smith 
314811af0c4SBarry Smith    Input Parameters:
315811af0c4SBarry Smith +  viewer - the `PetscViewer` to use with the specified file
316811af0c4SBarry Smith -  fd - the FILE pointer
317811af0c4SBarry Smith 
318811af0c4SBarry Smith    Level: beginner
319811af0c4SBarry Smith 
320811af0c4SBarry Smith    Notes:
321811af0c4SBarry Smith    This `PetscViewer` can be destroyed with `PetscViewerDestroy()`, but the fd will NOT be closed.
322811af0c4SBarry Smith 
323811af0c4SBarry Smith    If a multiprocessor communicator is used (such as `PETSC_COMM_WORLD`),
324811af0c4SBarry Smith    then only the first processor in the group uses the file.  All other
325811af0c4SBarry Smith    processors send their data to the first processor to print.
326811af0c4SBarry Smith 
327811af0c4SBarry Smith .seealso: `MatView()`, `VecView()`, `PetscViewerDestroy()`, `PetscViewerBinaryOpen()`,
328811af0c4SBarry Smith           `PetscViewerASCIIGetPointer()`, `PetscViewerPushFormat()`, `PETSC_VIEWER_STDOUT_`, `PETSC_VIEWER_STDERR_`,
329811af0c4SBarry Smith           `PETSC_VIEWER_STDOUT_WORLD`, `PETSC_VIEWER_STDOUT_SELF`, `PetscViewerASCIIOpen()`, `PetscViewerASCIIOpenWithFILE()`, `PETSCVIEWERASCII`
330811af0c4SBarry Smith @*/
331d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerASCIISetFILE(PetscViewer viewer, FILE *fd)
332d71ae5a4SJacob Faibussowitsch {
3335c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data;
3345c6c1daeSBarry Smith 
3355c6c1daeSBarry Smith   PetscFunctionBegin;
3365c6c1daeSBarry Smith   vascii->fd        = fd;
3375c6c1daeSBarry Smith   vascii->closefile = PETSC_FALSE;
338*3ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3395c6c1daeSBarry Smith }
340