Actual source code: ams.c

petsc-3.7.7 2017-09-25
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;

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

 17:      Collective on MPI_Comm

 19:      Input Parameters:
 20: .    comm - the MPI communicator to share the PetscViewer

 22:      Level: developer

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

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

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

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

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

 73:   MPI_Attr_get(comm,Petsc_Viewer_SAWs_keyval,(void**)&viewer,&flag);
 74:   if (flag) {
 75:     PetscViewerDestroy(&viewer);
 76:     MPI_Attr_delete(comm,Petsc_Viewer_SAWs_keyval);
 77:   }
 78:   return(0);
 79: }

 83: static PetscErrorCode PetscViewerDestroy_SAWs(PetscViewer viewer)
 84: {

 88:   /*
 89:      Make sure that we mark that the stack is no longer published
 90:   */
 91:   if (PetscObjectComm((PetscObject)viewer) == PETSC_COMM_WORLD) {
 92:     PetscStackSAWsViewOff();
 93:   }
 94:   return(0);
 95: }

 99: PETSC_EXTERN PetscErrorCode PetscViewerCreate_SAWs(PetscViewer v)
100: {
102:   v->ops->destroy = PetscViewerDestroy_SAWs;
103:   return(0);
104: }