Actual source code: ams.c


  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;

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

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

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

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

 74: static PetscErrorCode PetscViewerDestroy_SAWs(PetscViewer viewer)
 75: {
 76:   /*
 77:      Make sure that we mark that the stack is no longer published
 78:   */
 79:   if (PetscObjectComm((PetscObject)viewer) == PETSC_COMM_WORLD) PetscStackSAWsViewOff();
 80:   return 0;
 81: }

 83: PETSC_EXTERN PetscErrorCode PetscViewerCreate_SAWs(PetscViewer v)
 84: {
 85:   v->ops->destroy = PetscViewerDestroy_SAWs;
 86:   return 0;
 87: }