xref: /petsc/src/sys/classes/viewer/impls/ascii/vcreatea.c (revision 811af0c4b09a35de4306c442f88bd09fdc09897d)
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 /*@
13*811af0c4SBarry Smith    PetscViewerASCIIGetStdout - Creates a `PETSCVIEWERASCII` `PetscViewer` shared by all processors
14*811af0c4SBarry Smith                     in a communicator. Error returning version of `PETSC_VIEWER_STDOUT_()`
155c6c1daeSBarry Smith 
16d083f849SBarry Smith    Collective
175c6c1daeSBarry Smith 
185c6c1daeSBarry Smith    Input Parameter:
19*811af0c4SBarry Smith .  comm - the MPI communicator to share the `PetscViewer`
205c6c1daeSBarry Smith 
215c6c1daeSBarry Smith    Level: beginner
225c6c1daeSBarry Smith 
23*811af0c4SBarry Smith    Note:
24*811af0c4SBarry Smith      This should be used in all PETSc source code instead of `PETSC_VIEWER_STDOUT_()`
255c6c1daeSBarry Smith 
26db781477SPatrick Sanan .seealso: `PETSC_VIEWER_DRAW_()`, `PetscViewerASCIIOpen()`, `PETSC_VIEWER_STDERR_`, `PETSC_VIEWER_STDOUT_WORLD`,
27db781477SPatrick Sanan           `PETSC_VIEWER_STDOUT_SELF`
285c6c1daeSBarry Smith @*/
299371c9d4SSatish Balay PetscErrorCode PetscViewerASCIIGetStdout(MPI_Comm comm, PetscViewer *viewer) {
30e2dcd6d3SBarry Smith   PetscBool flg;
31e2dcd6d3SBarry Smith   MPI_Comm  ncomm;
325c6c1daeSBarry Smith 
335c6c1daeSBarry Smith   PetscFunctionBegin;
349566063dSJacob Faibussowitsch   PetscCall(PetscSpinlockLock(&PetscViewerASCIISpinLockStdout));
359566063dSJacob Faibussowitsch   PetscCall(PetscCommDuplicate(comm, &ncomm, NULL));
3648a46eb9SPierre 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));
379566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_get_attr(ncomm, Petsc_Viewer_Stdout_keyval, (void **)viewer, (PetscMPIInt *)&flg));
38e2dcd6d3SBarry Smith   if (!flg) { /* PetscViewer not yet created */
399566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIOpen(ncomm, "stdout", viewer));
409566063dSJacob Faibussowitsch     PetscCall(PetscObjectRegisterDestroy((PetscObject)*viewer));
419566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_set_attr(ncomm, Petsc_Viewer_Stdout_keyval, (void *)*viewer));
42e2dcd6d3SBarry Smith   }
439566063dSJacob Faibussowitsch   PetscCall(PetscCommDestroy(&ncomm));
449566063dSJacob Faibussowitsch   PetscCall(PetscSpinlockUnlock(&PetscViewerASCIISpinLockStdout));
455c6c1daeSBarry Smith   PetscFunctionReturn(0);
465c6c1daeSBarry Smith }
475c6c1daeSBarry Smith 
485c6c1daeSBarry Smith /*@C
49*811af0c4SBarry Smith    PETSC_VIEWER_STDOUT_ - Creates a ASCII `PetscViewer` shared by all processors
505c6c1daeSBarry Smith                     in a communicator.
515c6c1daeSBarry Smith 
52d083f849SBarry Smith    Collective
535c6c1daeSBarry Smith 
545c6c1daeSBarry Smith    Input Parameter:
55*811af0c4SBarry Smith .  comm - the MPI communicator to share the `PetscViewer`
565c6c1daeSBarry Smith 
575c6c1daeSBarry Smith    Level: beginner
585c6c1daeSBarry Smith 
59*811af0c4SBarry Smith    Note:
605c6c1daeSBarry Smith    Unlike almost all other PETSc routines, this does not return
615c6c1daeSBarry Smith    an error code. Usually used in the form
625c6c1daeSBarry Smith $      XXXView(XXX object,PETSC_VIEWER_STDOUT_(comm));
635c6c1daeSBarry Smith 
64db781477SPatrick Sanan .seealso: `PETSC_VIEWER_DRAW_()`, `PetscViewerASCIIOpen()`, `PETSC_VIEWER_STDERR_`, `PETSC_VIEWER_STDOUT_WORLD`,
65db781477SPatrick Sanan           `PETSC_VIEWER_STDOUT_SELF`
665c6c1daeSBarry Smith @*/
679371c9d4SSatish Balay PetscViewer PETSC_VIEWER_STDOUT_(MPI_Comm comm) {
685c6c1daeSBarry Smith   PetscErrorCode ierr;
695c6c1daeSBarry Smith   PetscViewer    viewer;
705c6c1daeSBarry Smith 
715c6c1daeSBarry Smith   PetscFunctionBegin;
725c6c1daeSBarry Smith   ierr = PetscViewerASCIIGetStdout(comm, &viewer);
739371c9d4SSatish Balay   if (ierr) {
749371c9d4SSatish Balay     PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_STDOUT_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_INITIAL, " ");
759371c9d4SSatish Balay     PetscFunctionReturn(NULL);
769371c9d4SSatish Balay   }
775c6c1daeSBarry Smith   PetscFunctionReturn(viewer);
785c6c1daeSBarry Smith }
795c6c1daeSBarry Smith 
805c6c1daeSBarry Smith /* ---------------------------------------------------------------------*/
815c6c1daeSBarry Smith 
82e2dcd6d3SBarry Smith /*
83e2dcd6d3SBarry Smith     The variable Petsc_Viewer_Stderr_keyval is used to indicate an MPI attribute that
84e2dcd6d3SBarry Smith   is attached to a communicator, in this case the attribute is a PetscViewer.
85e2dcd6d3SBarry Smith */
86d4c7638eSBarry Smith PetscMPIInt Petsc_Viewer_Stderr_keyval = MPI_KEYVAL_INVALID;
87e2dcd6d3SBarry Smith 
889c5ded49SBarry Smith /*@
89*811af0c4SBarry Smith    PetscViewerASCIIGetStderr - Creates a `PETSCVIEWERASCII` `PetscViewer` shared by all processors
90*811af0c4SBarry Smith                     in a communicator. Error returning version of `PETSC_VIEWER_STDERR_()`
915c6c1daeSBarry Smith 
92d083f849SBarry Smith    Collective
935c6c1daeSBarry Smith 
945c6c1daeSBarry Smith    Input Parameter:
95*811af0c4SBarry Smith .  comm - the MPI communicator to share the `PetscViewer`
965c6c1daeSBarry Smith 
975c6c1daeSBarry Smith    Level: beginner
985c6c1daeSBarry Smith 
99*811af0c4SBarry Smith    Note:
100*811af0c4SBarry Smith      This should be used in all PETSc source code instead of `PETSC_VIEWER_STDERR_()`
1015c6c1daeSBarry Smith 
102db781477SPatrick Sanan .seealso: `PETSC_VIEWER_DRAW_()`, `PetscViewerASCIIOpen()`, `PETSC_VIEWER_STDERR_`, `PETSC_VIEWER_STDERR_WORLD`,
103db781477SPatrick Sanan           `PETSC_VIEWER_STDERR_SELF`
1045c6c1daeSBarry Smith @*/
1059371c9d4SSatish Balay PetscErrorCode PetscViewerASCIIGetStderr(MPI_Comm comm, PetscViewer *viewer) {
106e2dcd6d3SBarry Smith   PetscBool flg;
107e2dcd6d3SBarry Smith   MPI_Comm  ncomm;
108e2dcd6d3SBarry Smith 
109e2dcd6d3SBarry Smith   PetscFunctionBegin;
1109566063dSJacob Faibussowitsch   PetscCall(PetscSpinlockLock(&PetscViewerASCIISpinLockStderr));
1119566063dSJacob Faibussowitsch   PetscCall(PetscCommDuplicate(comm, &ncomm, NULL));
11248a46eb9SPierre 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));
1139566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_get_attr(ncomm, Petsc_Viewer_Stderr_keyval, (void **)viewer, (PetscMPIInt *)&flg));
114e2dcd6d3SBarry Smith   if (!flg) { /* PetscViewer not yet created */
1159566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIOpen(ncomm, "stderr", viewer));
1169566063dSJacob Faibussowitsch     PetscCall(PetscObjectRegisterDestroy((PetscObject)*viewer));
1179566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_set_attr(ncomm, Petsc_Viewer_Stderr_keyval, (void *)*viewer));
118e2dcd6d3SBarry Smith   }
1199566063dSJacob Faibussowitsch   PetscCall(PetscCommDestroy(&ncomm));
1209566063dSJacob Faibussowitsch   PetscCall(PetscSpinlockUnlock(&PetscViewerASCIISpinLockStderr));
1215c6c1daeSBarry Smith   PetscFunctionReturn(0);
1225c6c1daeSBarry Smith }
1235c6c1daeSBarry Smith 
1245c6c1daeSBarry Smith /*@C
125*811af0c4SBarry Smith    PETSC_VIEWER_STDERR_ - Creates a `PETSCVIEWERASCII` `PetscViewer` shared by all processors
1265c6c1daeSBarry Smith                     in a communicator.
1275c6c1daeSBarry Smith 
128d083f849SBarry Smith    Collective
1295c6c1daeSBarry Smith 
1305c6c1daeSBarry Smith    Input Parameter:
131*811af0c4SBarry Smith .  comm - the MPI communicator to share the `PetscViewer`
1325c6c1daeSBarry Smith 
1335c6c1daeSBarry Smith    Level: beginner
1345c6c1daeSBarry Smith 
1355c6c1daeSBarry Smith    Note:
1365c6c1daeSBarry Smith    Unlike almost all other PETSc routines, this does not return
1375c6c1daeSBarry Smith    an error code. Usually used in the form
1385c6c1daeSBarry Smith $      XXXView(XXX object,PETSC_VIEWER_STDERR_(comm));
1395c6c1daeSBarry Smith 
140db781477SPatrick Sanan .seealso: `PETSC_VIEWER_DRAW_`, `PetscViewerASCIIOpen()`, `PETSC_VIEWER_STDOUT_`, `PETSC_VIEWER_STDOUT_WORLD`,
141db781477SPatrick Sanan           `PETSC_VIEWER_STDOUT_SELF`, `PETSC_VIEWER_STDERR_WORLD`, `PETSC_VIEWER_STDERR_SELF`
1425c6c1daeSBarry Smith @*/
1439371c9d4SSatish Balay PetscViewer PETSC_VIEWER_STDERR_(MPI_Comm comm) {
1445c6c1daeSBarry Smith   PetscErrorCode ierr;
1455c6c1daeSBarry Smith   PetscViewer    viewer;
1465c6c1daeSBarry Smith 
1475c6c1daeSBarry Smith   PetscFunctionBegin;
1485c6c1daeSBarry Smith   ierr = PetscViewerASCIIGetStderr(comm, &viewer);
1499371c9d4SSatish Balay   if (ierr) {
1509371c9d4SSatish Balay     PetscError(PETSC_COMM_SELF, __LINE__, "PETSC_VIEWER_STDERR_", __FILE__, PETSC_ERR_PLIB, PETSC_ERROR_INITIAL, " ");
1519371c9d4SSatish Balay     PetscFunctionReturn(NULL);
1529371c9d4SSatish Balay   }
1535c6c1daeSBarry Smith   PetscFunctionReturn(viewer);
1545c6c1daeSBarry Smith }
1555c6c1daeSBarry Smith 
1565c6c1daeSBarry Smith PetscMPIInt                     Petsc_Viewer_keyval = MPI_KEYVAL_INVALID;
1575c6c1daeSBarry Smith /*
1585c6c1daeSBarry 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
1595c6c1daeSBarry Smith    PetscObjectDestroyRegisterAll(). PetscViewerASCIIGetStdout() registers the viewer with PetscObjectDestroyRegister() to be destroyed when PetscFinalize() is called.
1605c6c1daeSBarry Smith 
1615c6c1daeSBarry Smith   This is called by MPI, not by users.
1625c6c1daeSBarry Smith 
1635c6c1daeSBarry Smith */
1649371c9d4SSatish Balay PETSC_EXTERN PetscMPIInt MPIAPI Petsc_DelViewer(MPI_Comm comm, PetscMPIInt keyval, void *attr_val, void *extra_state) {
1655c6c1daeSBarry Smith   PetscFunctionBegin;
1669566063dSJacob Faibussowitsch   PetscCallMPI(PetscInfo(NULL, "Removing viewer data attribute in an MPI_Comm %ld\n", (long)comm));
1675c6c1daeSBarry Smith   PetscFunctionReturn(MPI_SUCCESS);
1685c6c1daeSBarry Smith }
1695c6c1daeSBarry Smith 
1705c6c1daeSBarry Smith /*@C
171*811af0c4SBarry Smith    PetscViewerASCIIOpen - Opens an ASCII file for writing as a `PetscViewer`.
1725c6c1daeSBarry Smith 
173d083f849SBarry Smith    Collective
1745c6c1daeSBarry Smith 
1755c6c1daeSBarry Smith    Input Parameters:
1765c6c1daeSBarry Smith +  comm - the communicator
1775c6c1daeSBarry Smith -  name - the file name
1785c6c1daeSBarry Smith 
1795c6c1daeSBarry Smith    Output Parameter:
1805c6c1daeSBarry Smith .  lab - the PetscViewer to use with the specified file
1815c6c1daeSBarry Smith 
1825c6c1daeSBarry Smith    Level: beginner
1835c6c1daeSBarry Smith 
1845c6c1daeSBarry Smith    Notes:
185f8859db6SBarry Smith    To open a ASCII file as a viewer for reading one must use the sequence
186*811af0c4SBarry Smith .vb
187*811af0c4SBarry Smith    PetscViewerCreate(comm,&lab);
188*811af0c4SBarry Smith    PetscViewerSetType(lab,PETSCVIEWERASCII);
189*811af0c4SBarry Smith    PetscViewerFileSetMode(lab,FILE_MODE_READ);
190*811af0c4SBarry Smith    PetscViewerFileSetName(lab,name);
191*811af0c4SBarry Smith .ve
192f8859db6SBarry Smith 
193*811af0c4SBarry Smith    This `PetscViewer` can be destroyed with `PetscViewerDestroy()`.
1945c6c1daeSBarry Smith 
1952ea3bc1cSBarry Smith    The MPI communicator used here must match that used by the object one is viewing. For example if the
196*811af0c4SBarry Smith    Mat was created with a `PETSC_COMM_WORLD`, then the Viewer must be created with `PETSC_COMM_WORLD`
1975c6c1daeSBarry Smith 
198*811af0c4SBarry Smith    As shown below, `PetscViewerASCIIOpen()` is useful in conjunction with
199*811af0c4SBarry Smith    `MatView()` and `VecView()`
2005c6c1daeSBarry Smith .vb
2015c6c1daeSBarry Smith      PetscViewerASCIIOpen(PETSC_COMM_WORLD,"mat.output",&viewer);
2025c6c1daeSBarry Smith      MatView(matrix,viewer);
2035c6c1daeSBarry Smith .ve
2045c6c1daeSBarry Smith 
205*811af0c4SBarry Smith .seealso: `MatView()`, `VecView()`, `PetscViewerDestroy()`, `PetscViewerBinaryOpen()`, `PetscViewerASCIIRead()`, `PETSCVIEWERASCII`
206db781477SPatrick Sanan           `PetscViewerASCIIGetPointer()`, `PetscViewerPushFormat()`, `PETSC_VIEWER_STDOUT_`, `PETSC_VIEWER_STDERR_`,
207db781477SPatrick Sanan           `PETSC_VIEWER_STDOUT_WORLD`, `PETSC_VIEWER_STDOUT_SELF`,
2085c6c1daeSBarry Smith @*/
2099371c9d4SSatish Balay PetscErrorCode PetscViewerASCIIOpen(MPI_Comm comm, const char name[], PetscViewer *lab) {
2105c6c1daeSBarry Smith   PetscViewerLink *vlink, *nv;
2115c6c1daeSBarry Smith   PetscBool        flg, eq;
2125c6c1daeSBarry Smith   size_t           len;
2135c6c1daeSBarry Smith 
2145c6c1daeSBarry Smith   PetscFunctionBegin;
2159566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(name, &len));
2165c6c1daeSBarry Smith   if (!len) {
2179566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIGetStdout(comm, lab));
2189566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)*lab));
2195c6c1daeSBarry Smith     PetscFunctionReturn(0);
2205c6c1daeSBarry Smith   }
2219566063dSJacob Faibussowitsch   PetscCall(PetscSpinlockLock(&PetscViewerASCIISpinLockOpen));
22248a46eb9SPierre 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));
2232bf49c77SBarry Smith   /*
2242bf49c77SBarry Smith        It would be better to move this code to PetscFileSetName() but since it must return a preexiting communicator
2252bf49c77SBarry Smith      we cannot do that, since PetscFileSetName() takes a communicator that already exists.
2262bf49c77SBarry Smith 
2272bf49c77SBarry Smith       Plus if the original communicator that created the file has since been close this will not detect the old
2282bf49c77SBarry Smith       communictor and hence will overwrite the old data. It may be better to simply remove all this code
2292bf49c77SBarry Smith   */
2305c6c1daeSBarry Smith   /* make sure communicator is a PETSc communicator */
2319566063dSJacob Faibussowitsch   PetscCall(PetscCommDuplicate(comm, &comm, NULL));
2325c6c1daeSBarry Smith   /* has file already been opened into a viewer */
2339566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_get_attr(comm, Petsc_Viewer_keyval, (void **)&vlink, (PetscMPIInt *)&flg));
2345c6c1daeSBarry Smith   if (flg) {
2355c6c1daeSBarry Smith     while (vlink) {
2369566063dSJacob Faibussowitsch       PetscCall(PetscStrcmp(name, ((PetscViewer_ASCII *)(vlink->viewer->data))->filename, &eq));
2375c6c1daeSBarry Smith       if (eq) {
2389566063dSJacob Faibussowitsch         PetscCall(PetscObjectReference((PetscObject)vlink->viewer));
2395c6c1daeSBarry Smith         *lab = vlink->viewer;
2409566063dSJacob Faibussowitsch         PetscCall(PetscCommDestroy(&comm));
2419566063dSJacob Faibussowitsch         PetscCall(PetscSpinlockUnlock(&PetscViewerASCIISpinLockOpen));
2425c6c1daeSBarry Smith         PetscFunctionReturn(0);
2435c6c1daeSBarry Smith       }
2445c6c1daeSBarry Smith       vlink = vlink->next;
2455c6c1daeSBarry Smith     }
2465c6c1daeSBarry Smith   }
2479566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(comm, lab));
2489566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(*lab, PETSCVIEWERASCII));
2491baa6e33SBarry Smith   if (name) PetscCall(PetscViewerFileSetName(*lab, name));
2505c6c1daeSBarry Smith   /* save viewer into communicator if needed later */
2519566063dSJacob Faibussowitsch   PetscCall(PetscNew(&nv));
2525c6c1daeSBarry Smith   nv->viewer = *lab;
2535c6c1daeSBarry Smith   if (!flg) {
2549566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_set_attr(comm, Petsc_Viewer_keyval, nv));
2555c6c1daeSBarry Smith   } else {
2569566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Comm_get_attr(comm, Petsc_Viewer_keyval, (void **)&vlink, (PetscMPIInt *)&flg));
2575c6c1daeSBarry Smith     if (vlink) {
2585c6c1daeSBarry Smith       while (vlink->next) vlink = vlink->next;
2595c6c1daeSBarry Smith       vlink->next = nv;
2605c6c1daeSBarry Smith     } else {
2619566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Comm_set_attr(comm, Petsc_Viewer_keyval, nv));
2625c6c1daeSBarry Smith     }
2635c6c1daeSBarry Smith   }
2649566063dSJacob Faibussowitsch   PetscCall(PetscCommDestroy(&comm));
2659566063dSJacob Faibussowitsch   PetscCall(PetscSpinlockUnlock(&PetscViewerASCIISpinLockOpen));
2665c6c1daeSBarry Smith   PetscFunctionReturn(0);
2675c6c1daeSBarry Smith }
2685c6c1daeSBarry Smith 
2695c6c1daeSBarry Smith /*@C
2705c6c1daeSBarry Smith    PetscViewerASCIIOpenWithFILE - Given an open file creates an ASCII viewer that prints to it.
2715c6c1daeSBarry Smith 
272d083f849SBarry Smith    Collective
2735c6c1daeSBarry Smith 
2745c6c1daeSBarry Smith    Input Parameters:
2755c6c1daeSBarry Smith +  comm - the communicator
2765c6c1daeSBarry Smith -  fd - the FILE pointer
2775c6c1daeSBarry Smith 
2785c6c1daeSBarry Smith    Output Parameter:
279*811af0c4SBarry Smith .  lab - the `PetscViewer` to use with the specified file
2805c6c1daeSBarry Smith 
2815c6c1daeSBarry Smith    Level: beginner
2825c6c1daeSBarry Smith 
2835c6c1daeSBarry Smith    Notes:
284*811af0c4SBarry Smith    This `PetscViewer` can be destroyed with `PetscViewerDestroy()`, but the fd will NOT be closed.
2855c6c1daeSBarry Smith 
286*811af0c4SBarry Smith    If a multiprocessor communicator is used (such as `PETSC_COMM_WORLD`),
2875c6c1daeSBarry Smith    then only the first processor in the group uses the file.  All other
2885c6c1daeSBarry Smith    processors send their data to the first processor to print.
2895c6c1daeSBarry Smith 
290db781477SPatrick Sanan .seealso: `MatView()`, `VecView()`, `PetscViewerDestroy()`, `PetscViewerBinaryOpen()`,
291db781477SPatrick Sanan           `PetscViewerASCIIGetPointer()`, `PetscViewerPushFormat()`, `PETSC_VIEWER_STDOUT_`, `PETSC_VIEWER_STDERR_`,
292*811af0c4SBarry Smith           `PETSC_VIEWER_STDOUT_WORLD`, `PETSC_VIEWER_STDOUT_SELF`, `PetscViewerASCIIOpen()`, `PetscViewerASCIISetFILE()`, `PETSCVIEWERASCII`
2935c6c1daeSBarry Smith @*/
2949371c9d4SSatish Balay PetscErrorCode PetscViewerASCIIOpenWithFILE(MPI_Comm comm, FILE *fd, PetscViewer *lab) {
2955c6c1daeSBarry Smith   PetscFunctionBegin;
2969566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(comm, lab));
2979566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(*lab, PETSCVIEWERASCII));
2989566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISetFILE(*lab, fd));
2995c6c1daeSBarry Smith   PetscFunctionReturn(0);
3005c6c1daeSBarry Smith }
3015c6c1daeSBarry Smith 
302*811af0c4SBarry Smith /*@C
303*811af0c4SBarry Smith    PetscViewerASCIISetFILE - Given an open file sets the ASCII viewer to use the file for output
304*811af0c4SBarry Smith 
305*811af0c4SBarry Smith    Not collective
306*811af0c4SBarry Smith 
307*811af0c4SBarry Smith    Input Parameters:
308*811af0c4SBarry Smith +  viewer - the `PetscViewer` to use with the specified file
309*811af0c4SBarry Smith -  fd - the FILE pointer
310*811af0c4SBarry Smith 
311*811af0c4SBarry Smith    Level: beginner
312*811af0c4SBarry Smith 
313*811af0c4SBarry Smith    Notes:
314*811af0c4SBarry Smith    This `PetscViewer` can be destroyed with `PetscViewerDestroy()`, but the fd will NOT be closed.
315*811af0c4SBarry Smith 
316*811af0c4SBarry Smith    If a multiprocessor communicator is used (such as `PETSC_COMM_WORLD`),
317*811af0c4SBarry Smith    then only the first processor in the group uses the file.  All other
318*811af0c4SBarry Smith    processors send their data to the first processor to print.
319*811af0c4SBarry Smith 
320*811af0c4SBarry Smith .seealso: `MatView()`, `VecView()`, `PetscViewerDestroy()`, `PetscViewerBinaryOpen()`,
321*811af0c4SBarry Smith           `PetscViewerASCIIGetPointer()`, `PetscViewerPushFormat()`, `PETSC_VIEWER_STDOUT_`, `PETSC_VIEWER_STDERR_`,
322*811af0c4SBarry Smith           `PETSC_VIEWER_STDOUT_WORLD`, `PETSC_VIEWER_STDOUT_SELF`, `PetscViewerASCIIOpen()`, `PetscViewerASCIIOpenWithFILE()`, `PETSCVIEWERASCII`
323*811af0c4SBarry Smith @*/
3249371c9d4SSatish Balay PetscErrorCode PetscViewerASCIISetFILE(PetscViewer viewer, FILE *fd) {
3255c6c1daeSBarry Smith   PetscViewer_ASCII *vascii = (PetscViewer_ASCII *)viewer->data;
3265c6c1daeSBarry Smith 
3275c6c1daeSBarry Smith   PetscFunctionBegin;
3285c6c1daeSBarry Smith   vascii->fd        = fd;
3295c6c1daeSBarry Smith   vascii->closefile = PETSC_FALSE;
3305c6c1daeSBarry Smith   PetscFunctionReturn(0);
3315c6c1daeSBarry Smith }
332