Actual source code: ams.c

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

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

     11: /*@C
     12:      PETSC_VIEWER_SAWS_ - Creates a SAWs `PetscViewer` shared by all MPI processes in a communicator.

     14:      Collective

     16:      Input Parameter:
     17: .    comm - the MPI communicator to share the `PetscViewer`

     19:      Level: developer

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

     26: .seealso: [](sec_viewers), `PetscViewer`, `PETSC_VIEWER_SAWS_WORLD`, `PETSC_VIEWER_SAWS_SELF`
     27: @*/
     28: PetscViewer PETSC_VIEWER_SAWS_(MPI_Comm comm)
     29: {
     30:   PetscErrorCode ierr;
     31:   PetscMPIInt    flag;
     32:   PetscViewer    viewer;
     33:   MPI_Comm       ncomm;

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

     56: static PetscErrorCode PetscViewerDestroy_SAWs(PetscViewer viewer)
     57: {
     58:   PetscFunctionBegin;
     59:   /*
     60:      Make sure that we mark that the stack is no longer published
     61:   */
     62:   if (PetscObjectComm((PetscObject)viewer) == PETSC_COMM_WORLD) PetscCall(PetscStackSAWsViewOff());
     63:   PetscFunctionReturn(PETSC_SUCCESS);
     64: }

     66: PETSC_EXTERN PetscErrorCode PetscViewerCreate_SAWs(PetscViewer v)
     67: {
     68:   PetscFunctionBegin;
     69:   v->ops->destroy = PetscViewerDestroy_SAWs;
     70:   PetscFunctionReturn(PETSC_SUCCESS);
     71: }