xref: /petsc/src/sys/classes/viewer/interface/dupl.c (revision c410d8cc8908fa7552d5d2cadc5c41ff54401973)
15c6c1daeSBarry Smith 
2af0996ceSBarry Smith #include <petsc/private/viewerimpl.h> /*I "petscviewer.h" I*/
35c6c1daeSBarry Smith 
4b3fa1ec5SBarry Smith /*@C
5811af0c4SBarry Smith    PetscViewerGetSubViewer - Creates a new `PetscViewer` (same type as the old)
6*c410d8ccSBarry Smith     that lives on a subcommunicator of the original viewer's communicator
75c6c1daeSBarry Smith 
8c3339decSBarry Smith     Collective
95c6c1daeSBarry Smith 
105c6c1daeSBarry Smith    Input Parameter:
11811af0c4SBarry Smith .  viewer - the `PetscViewer` to be reproduced
125c6c1daeSBarry Smith 
135c6c1daeSBarry Smith    Output Parameter:
14811af0c4SBarry 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
20*c410d8ccSBarry Smith     viewer on two MPI processes is decomposed into two subviewers, the output from the first viewer is
21811af0c4SBarry Smith     all printed before the output from the second viewer. You must call `PetscViewerFlush()` after
22811af0c4SBarry Smith     the call to `PetscViewerRestoreSubViewer()`.
23a62913d3SBarry Smith 
24811af0c4SBarry 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
27811af0c4SBarry Smith     parallel object. For example `PCView()` on a `PCBJACOBI` could use this to obtain a
28811af0c4SBarry Smith     `PetscViewer` that is used with the sequential `KSP` on one block of the preconditioner.
295c6c1daeSBarry Smith 
30811af0c4SBarry Smith     Between the calls to `PetscViewerGetSubViewer()` and `PetscViewerRestoreSubViewer()` the original
31e5afcf28SBarry Smith     viewer should not be used
32e5afcf28SBarry Smith 
33*c410d8ccSBarry Smith     `PETSCVIEWERDRAW` and `PETSCVIEWERBINARY` only support returning a singleton viewer on MPI rank 0,
34*c410d8ccSBarry Smith     all other ranks will return a `NULL` viewer
35e5afcf28SBarry Smith 
36e5afcf28SBarry Smith   Developer Notes:
37*c410d8ccSBarry Smith     There is currently incomplete error checking to ensure the user does not use the original viewer between the
38811af0c4SBarry 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 
41811af0c4SBarry Smith     It would be nice if the call to `PetscViewerFlush()` was not required and was handled by
42811af0c4SBarry Smith     `PetscViewerRestoreSubViewer()`
43a62913d3SBarry Smith 
44*c410d8ccSBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerSocketOpen()`, `PetscViewerASCIIOpen()`, `PetscViewerDrawOpen()`, `PetscViewerRestoreSubViewer()`
455c6c1daeSBarry Smith @*/
46d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerGetSubViewer(PetscViewer viewer, MPI_Comm comm, PetscViewer *outviewer)
47d71ae5a4SJacob Faibussowitsch {
485c6c1daeSBarry Smith   PetscFunctionBegin;
495c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
50064a246eSJacob Faibussowitsch   PetscValidPointer(outviewer, 3);
51dbbe0bcdSBarry Smith   PetscUseTypeMethod(viewer, getsubviewer, comm, outviewer);
523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
535c6c1daeSBarry Smith }
545c6c1daeSBarry Smith 
55b3fa1ec5SBarry Smith /*@C
56*c410d8ccSBarry Smith    PetscViewerRestoreSubViewer - Restores a  `PetscViewer` obtained with `PetscViewerGetSubViewer()`.
575c6c1daeSBarry Smith 
58c3339decSBarry Smith     Collective
595c6c1daeSBarry Smith 
605c6c1daeSBarry Smith    Input Parameters:
61811af0c4SBarry Smith +  viewer - the `PetscViewer` that was reproduced
62*c410d8ccSBarry Smith -  outviewer - the subviewer to be returned
635c6c1daeSBarry Smith 
645c6c1daeSBarry Smith    Level: advanced
655c6c1daeSBarry Smith 
66*c410d8ccSBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerSocketOpen()`, `PetscViewerASCIIOpen()`, `PetscViewerDrawOpen()`, `PetscViewerGetSubViewer()`
675c6c1daeSBarry Smith @*/
68d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerRestoreSubViewer(PetscViewer viewer, MPI_Comm comm, PetscViewer *outviewer)
69d71ae5a4SJacob Faibussowitsch {
705c6c1daeSBarry Smith   PetscFunctionBegin;
715c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
72ffc4695bSBarry Smith 
73dbbe0bcdSBarry Smith   PetscUseTypeMethod(viewer, restoresubviewer, comm, outviewer);
743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
755c6c1daeSBarry Smith }
76