Actual source code: ams.c

petsc-3.12.5 2020-03-29
Report Typos and Errors

  2:  #include <petsc/private/viewerimpl.h>
  3:  #include <petscviewersaws.h>
  4:  #include <petscsys.h>

  6: /*
  7:     The variable Petsc_Viewer_SAWs_keyval is used to indicate an MPI attribute that
  8:   is attached to a communicator, in this case the attribute is a PetscViewer.
  9: */
 10: static PetscMPIInt Petsc_Viewer_SAWs_keyval = MPI_KEYVAL_INVALID;

 12: /*@C
 13:      PETSC_VIEWER_SAWS_ - Creates an SAWs PetscViewer shared by all processors in a communicator.

 15:      Collective

 17:      Input Parameters:
 18: .    comm - the MPI communicator to share the PetscViewer

 20:      Level: developer

 22:      Notes:
 23:      Unlike almost all other PETSc routines, PETSC_VIEWER_SAWS_() does not return
 24:      an error code.  The resulting PetscViewer is usually used in the form
 25: $       XXXView(XXX object,PETSC_VIEWER_SAWS_(comm));

 27: .seealso: PETSC_VIEWER_SAWS_WORLD, PETSC_VIEWER_SAWS_SELF
 28: @*/
 29: PetscViewer PETSC_VIEWER_SAWS_(MPI_Comm comm)
 30: {
 32:   PetscMPIInt    flag;
 33:   PetscViewer    viewer;
 34:   MPI_Comm       ncomm;

 37:   PetscCommDuplicate(comm,&ncomm,NULL);if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
 38:   if (Petsc_Viewer_SAWs_keyval == MPI_KEYVAL_INVALID) {
 39:     MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN,MPI_COMM_NULL_DELETE_FN,&Petsc_Viewer_SAWs_keyval,0);
 40:     if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,1,PETSC_ERROR_INITIAL," "); viewer = NULL;}
 41:   }
 42:   MPI_Comm_get_attr(ncomm,Petsc_Viewer_SAWs_keyval,(void**)&viewer,&flag);
 43:   if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,1,PETSC_ERROR_INITIAL," "); viewer = NULL;}
 44:   if (!flag) { /* PetscViewer not yet created */
 45:     PetscViewerSAWsOpen(comm,&viewer);
 46:     if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,1,PETSC_ERROR_INITIAL," "); viewer = NULL;}
 47:     PetscObjectRegisterDestroy((PetscObject)viewer);
 48:     if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,1,PETSC_ERROR_INITIAL," "); viewer = NULL;}
 49:     MPI_Comm_set_attr(ncomm,Petsc_Viewer_SAWs_keyval,(void*)viewer);
 50:     if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,1,PETSC_ERROR_INITIAL," "); viewer = NULL;}
 51:   }
 52:   PetscCommDestroy(&ncomm);
 53:   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
 54:   PetscFunctionReturn(viewer);
 55: }

 57: /*
 58:        If there is a PetscViewer associated with this communicator, it is destroyed.
 59: */
 60: PetscErrorCode PetscViewer_SAWS_Destroy(MPI_Comm comm)
 61: {
 63:   PetscMPIInt    flag;
 64:   PetscViewer    viewer;

 67:   if (Petsc_Viewer_SAWs_keyval == MPI_KEYVAL_INVALID) return(0);

 69:   MPI_Comm_get_attr(comm,Petsc_Viewer_SAWs_keyval,(void**)&viewer,&flag);
 70:   if (flag) {
 71:     PetscViewerDestroy(&viewer);
 72:     MPI_Comm_delete_attr(comm,Petsc_Viewer_SAWs_keyval);
 73:   }
 74:   return(0);
 75: }

 77: static PetscErrorCode PetscViewerDestroy_SAWs(PetscViewer viewer)
 78: {

 82:   /*
 83:      Make sure that we mark that the stack is no longer published
 84:   */
 85:   if (PetscObjectComm((PetscObject)viewer) == PETSC_COMM_WORLD) {
 86:     PetscStackSAWsViewOff();
 87:   }
 88:   return(0);
 89: }

 91: PETSC_EXTERN PetscErrorCode PetscViewerCreate_SAWs(PetscViewer v)
 92: {
 94:   v->ops->destroy = PetscViewerDestroy_SAWs;
 95:   return(0);
 96: }