1 2 #include <petsc/private/viewerimpl.h> /*I "petscviewer.h" I*/ 3 4 /*@C 5 PetscViewerGetSubViewer - Creates a new PetscViewer (same type as the old) 6 that lives on a subcommunicator 7 8 Collective on PetscViewer 9 10 Input Parameter: 11 . viewer - the PetscViewer to be reproduced 12 13 Output Parameter: 14 . outviewer - new PetscViewer 15 16 Level: advanced 17 18 Notes: 19 Call PetscViewerRestoreSubViewer() to return this PetscViewer, NOT PetscViewerDestroy() 20 21 This is most commonly used to view a sequential object that is part of a 22 parallel object. For example block Jacobi PC view could use this to obtain a 23 PetscViewer that is used with the sequential KSP on one block of the preconditioner. 24 25 Between the calls to PetscViewerGetSubViewer() and PetscViewerRestoreSubViewer() the original 26 viewer should not be used 27 28 PETSCVIEWERDRAW and PETSCVIEWERBINARY only support returning a singleton viewer on rank 0, 29 all other ranks will return a NULL viewer 30 31 Developer Notes: 32 There is currently incomplete error checking that the user does not use the original viewer between the 33 the calls to PetscViewerGetSubViewer() and PetscViewerRestoreSubViewer(). If the user does there 34 could be errors in the viewing that go undetected or crash the code. 35 36 .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerDrawOpen(), PetscViewerRestoreSubViewer() 37 @*/ 38 PetscErrorCode PetscViewerGetSubViewer(PetscViewer viewer,MPI_Comm comm,PetscViewer *outviewer) 39 { 40 PetscErrorCode ierr; 41 PetscMPIInt size; 42 43 PetscFunctionBegin; 44 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 45 PetscValidPointer(outviewer,2); 46 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)viewer),&size);CHKERRQ(ierr); 47 if (size == 1) { 48 ierr = PetscObjectReference((PetscObject)viewer);CHKERRQ(ierr); 49 *outviewer = viewer; 50 } else if (viewer->ops->getsubviewer) { 51 ierr = (*viewer->ops->getsubviewer)(viewer,comm,outviewer);CHKERRQ(ierr); 52 } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot get SubViewer PetscViewer for type %s",((PetscObject)viewer)->type_name); 53 PetscFunctionReturn(0); 54 } 55 56 /*@C 57 PetscViewerRestoreSubViewer - Restores a new PetscViewer obtained with PetscViewerGetSubViewer(). 58 59 Collective on PetscViewer 60 61 Input Parameters: 62 + viewer - the PetscViewer that was reproduced 63 - outviewer - new PetscViewer 64 65 Level: advanced 66 67 Notes: 68 Call PetscViewerGetSubViewer() to get this PetscViewer, NOT PetscViewerCreate() 69 70 .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerDrawOpen(), PetscViewerGetSubViewer() 71 @*/ 72 PetscErrorCode PetscViewerRestoreSubViewer(PetscViewer viewer,MPI_Comm comm,PetscViewer *outviewer) 73 { 74 PetscErrorCode ierr; 75 PetscMPIInt size; 76 77 PetscFunctionBegin; 78 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 79 80 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)viewer),&size);CHKERRQ(ierr); 81 if (size == 1) { 82 ierr = PetscObjectDereference((PetscObject)viewer);CHKERRQ(ierr); 83 if (outviewer) *outviewer = NULL; 84 } else if (viewer->ops->restoresubviewer) { 85 ierr = (*viewer->ops->restoresubviewer)(viewer,comm,outviewer);CHKERRQ(ierr); 86 } 87 PetscFunctionReturn(0); 88 } 89 90