xref: /petsc/src/sys/classes/viewer/interface/dupl.c (revision 811af0c4b09a35de4306c442f88bd09fdc09897d)
15c6c1daeSBarry Smith 
2af0996ceSBarry Smith #include <petsc/private/viewerimpl.h> /*I "petscviewer.h" I*/
35c6c1daeSBarry Smith 
4b3fa1ec5SBarry Smith /*@C
5*811af0c4SBarry Smith    PetscViewerGetSubViewer - Creates a new `PetscViewer` (same type as the old)
63f08860eSBarry Smith     that lives on a subcommunicator
75c6c1daeSBarry Smith 
8*811af0c4SBarry Smith     Collective on viewer
95c6c1daeSBarry Smith 
105c6c1daeSBarry Smith    Input Parameter:
11*811af0c4SBarry Smith .  viewer - the `PetscViewer` to be reproduced
125c6c1daeSBarry Smith 
135c6c1daeSBarry Smith    Output Parameter:
14*811af0c4SBarry Smith .  outviewer - new `PetscViewer`
155c6c1daeSBarry Smith 
165c6c1daeSBarry Smith    Level: advanced
175c6c1daeSBarry Smith 
1895452b02SPatrick Sanan    Notes:
19a62913d3SBarry Smith     The output of the subviewers is synchronized against the original viewer. For example, if a
20a62913d3SBarry Smith     viewer on two MPI ranks is decomposed into two subviewers, the output from the first viewer is
21*811af0c4SBarry Smith     all printed before the output from the second viewer. You must call `PetscViewerFlush()` after
22*811af0c4SBarry Smith     the call to `PetscViewerRestoreSubViewer()`.
23a62913d3SBarry Smith 
24*811af0c4SBarry Smith     Call `PetscViewerRestoreSubViewer()` to destroy this `PetscViewer`, NOT `PetscViewerDestroy()`
255c6c1daeSBarry Smith 
265c6c1daeSBarry Smith      This is most commonly used to view a sequential object that is part of a
27*811af0c4SBarry Smith     parallel object. For example `PCView()` on a `PCBJACOBI` could use this to obtain a
28*811af0c4SBarry Smith     `PetscViewer` that is used with the sequential `KSP` on one block of the preconditioner.
295c6c1daeSBarry Smith 
30*811af0c4SBarry Smith     Between the calls to `PetscViewerGetSubViewer()` and `PetscViewerRestoreSubViewer()` the original
31e5afcf28SBarry Smith     viewer should not be used
32e5afcf28SBarry Smith 
33*811af0c4SBarry Smith     `PETSCVIEWERDRAW` and `PETSCVIEWERBINARY` only support returning a singleton viewer on rank 0,
34e5afcf28SBarry Smith     all other ranks will return a NULL viewer
35e5afcf28SBarry Smith 
36e5afcf28SBarry Smith   Developer Notes:
37e5afcf28SBarry Smith     There is currently incomplete error checking that the user does not use the original viewer between the
38*811af0c4SBarry Smith     the calls to `PetscViewerGetSubViewer()` and `PetscViewerRestoreSubViewer()`. If the user does there
39e5afcf28SBarry Smith     could be errors in the viewing that go undetected or crash the code.
40e5afcf28SBarry Smith 
41*811af0c4SBarry Smith     It would be nice if the call to `PetscViewerFlush()` was not required and was handled by
42*811af0c4SBarry Smith     `PetscViewerRestoreSubViewer()`
43a62913d3SBarry Smith 
44db781477SPatrick Sanan .seealso: `PetscViewerSocketOpen()`, `PetscViewerASCIIOpen()`, `PetscViewerDrawOpen()`, `PetscViewerRestoreSubViewer()`
455c6c1daeSBarry Smith @*/
469371c9d4SSatish Balay PetscErrorCode PetscViewerGetSubViewer(PetscViewer viewer, MPI_Comm comm, PetscViewer *outviewer) {
475c6c1daeSBarry Smith   PetscFunctionBegin;
485c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
49064a246eSJacob Faibussowitsch   PetscValidPointer(outviewer, 3);
50dbbe0bcdSBarry Smith   PetscUseTypeMethod(viewer, getsubviewer, comm, outviewer);
515c6c1daeSBarry Smith   PetscFunctionReturn(0);
525c6c1daeSBarry Smith }
535c6c1daeSBarry Smith 
54b3fa1ec5SBarry Smith /*@C
55*811af0c4SBarry Smith    PetscViewerRestoreSubViewer - Restores a new `PetscViewer` obtained with `PetscViewerGetSubViewer()`.
565c6c1daeSBarry Smith 
57*811af0c4SBarry Smith     Collective on viewer
585c6c1daeSBarry Smith 
595c6c1daeSBarry Smith    Input Parameters:
60*811af0c4SBarry Smith +  viewer - the `PetscViewer` that was reproduced
61*811af0c4SBarry Smith -  outviewer - the subviewer to be returned `PetscViewer`
625c6c1daeSBarry Smith 
635c6c1daeSBarry Smith    Level: advanced
645c6c1daeSBarry Smith 
65db781477SPatrick Sanan .seealso: `PetscViewerSocketOpen()`, `PetscViewerASCIIOpen()`, `PetscViewerDrawOpen()`, `PetscViewerGetSubViewer()`
665c6c1daeSBarry Smith @*/
679371c9d4SSatish Balay PetscErrorCode PetscViewerRestoreSubViewer(PetscViewer viewer, MPI_Comm comm, PetscViewer *outviewer) {
685c6c1daeSBarry Smith   PetscFunctionBegin;
695c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
70ffc4695bSBarry Smith 
71dbbe0bcdSBarry Smith   PetscUseTypeMethod(viewer, restoresubviewer, comm, outviewer);
725c6c1daeSBarry Smith   PetscFunctionReturn(0);
735c6c1daeSBarry Smith }
74