# PetscViewerGetSubViewer
Creates a new `PetscViewer` (same type as the old) that lives on a subcommunicator 
## Synopsis
```
#include "petscviewer.h" 
PetscErrorCode PetscViewerGetSubViewer(PetscViewer viewer, MPI_Comm comm, PetscViewer *outviewer)
```
Collective


## Input Parameter

- ***viewer -*** the `PetscViewer` to be reproduced



## Output Parameter

- ***outviewer -*** new `PetscViewer`





## Notes
The output of the subviewers is synchronized against the original viewer. For example, if a
viewer on two MPI ranks is decomposed into two subviewers, the output from the first viewer is
all printed before the output from the second viewer. You must call `PetscViewerFlush()` after
the call to `PetscViewerRestoreSubViewer()`.

Call `PetscViewerRestoreSubViewer()` to destroy this `PetscViewer`, NOT `PetscViewerDestroy()`

This is most commonly used to view a sequential object that is part of a
parallel object. For example `PCView()` on a `PCBJACOBI` could use this to obtain a
`PetscViewer` that is used with the sequential `KSP` on one block of the preconditioner.

Between the calls to `PetscViewerGetSubViewer()` and `PetscViewerRestoreSubViewer()` the original
viewer should not be used

`PETSCVIEWERDRAW` and `PETSCVIEWERBINARY` only support returning a singleton viewer on rank 0,
all other ranks will return a NULL viewer


## Developer Notes
There is currently incomplete error checking that the user does not use the original viewer between the
the calls to `PetscViewerGetSubViewer()` and `PetscViewerRestoreSubViewer()`. If the user does there
could be errors in the viewing that go undetected or crash the code.

It would be nice if the call to `PetscViewerFlush()` was not required and was handled by
`PetscViewerRestoreSubViewer()`


## See Also
 [](sec_viewers), `PetscViewerSocketOpen()`, `PetscViewerASCIIOpen()`, `PetscViewerDrawOpen()`, `PetscViewerRestoreSubViewer()`

## Level
advanced

## Location
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/sys/classes/viewer/interface/dupl.c.html#PetscViewerGetSubViewer">src/sys/classes/viewer/interface/dupl.c</A>

## Examples
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/vec/vec/tutorials/ex14f.F90.html">src/vec/vec/tutorials/ex14f.F90.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/vec/vec/tutorials/ex9f.F90.html">src/vec/vec/tutorials/ex9f.F90.html</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/dm/tutorials/ex6.c.html">src/dm/tutorials/ex6.c.html</A><BR>

## Implementations

<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/sys/classes/viewer/impls/ascii/filev.c.html#PetscViewerGetSubViewer_ASCII">PetscViewerGetSubViewer_ASCII in src/sys/classes/viewer/impls/ascii/filev.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/sys/classes/viewer/impls/binary/binv.c.html#PetscViewerGetSubViewer_Binary">PetscViewerGetSubViewer_Binary in src/sys/classes/viewer/impls/binary/binv.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/sys/classes/viewer/impls/draw/drawv.c.html#PetscViewerGetSubViewer_Draw">PetscViewerGetSubViewer_Draw in src/sys/classes/viewer/impls/draw/drawv.c</A><BR>
<A HREF="PETSC_DOC_OUT_ROOT_PLACEHOLDER/src/sys/classes/viewer/impls/string/stringv.c.html#PetscViewerGetSubViewer_String">PetscViewerGetSubViewer_String in src/sys/classes/viewer/impls/string/stringv.c</A><BR>


---
[Edit on GitLab](https://gitlab.com/petsc/petsc/-/edit/release/src/sys/classes/viewer/interface/dupl.c)


[Index of all Viewer routines](index.md)  
[Table of Contents for all manual pages](/docs/manualpages/index.md)  
[Index of all manual pages](/docs/manualpages/singleindex.md)  
