Actual source code: ams.c
1: #include <petsc/private/viewerimpl.h>
2: #include <petscviewersaws.h>
4: /*
5: The variable Petsc_Viewer_SAWs_keyval is used to indicate an MPI attribute that
6: is attached to a communicator, in this case the attribute is a PetscViewer.
7: */
8: static PetscMPIInt Petsc_Viewer_SAWs_keyval = MPI_KEYVAL_INVALID;
10: /*@
11: PETSC_VIEWER_SAWS_ - Creates a SAWs `PetscViewer` shared by all MPI processes in a communicator.
13: Collective
15: Input Parameter:
16: . comm - the MPI communicator to share the `PetscViewer`
18: Level: developer
20: Note:
21: Unlike almost all other PETSc routines, `PETSC_VIEWER_SAWS_()` does not return
22: an error code. The resulting `PetscViewer` is usually used in the form
23: .vb
24: XXXView(XXX object, PETSC_VIEWER_SAWS_(comm));
25: .ve
27: .seealso: [](sec_viewers), `PetscViewer`, `PETSC_VIEWER_SAWS_WORLD`, `PETSC_VIEWER_SAWS_SELF`
28: @*/
29: PetscViewer PETSC_VIEWER_SAWS_(MPI_Comm comm)
30: {
31: PetscErrorCode ierr;
32: PetscMPIInt iflg;
33: PetscViewer viewer;
34: MPI_Comm ncomm;
36: PetscFunctionBegin;
37: PetscCallNull(PetscCommDuplicate(comm, &ncomm, NULL));
38: 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));
39: ierr = (PetscErrorCode)MPI_Comm_get_attr(ncomm, Petsc_Viewer_SAWs_keyval, (void **)&viewer, &iflg);
40: if (ierr) {
41: ierr = PetscError(ncomm, __LINE__, "PETSC_VIEWER_SAWS_", __FILE__, PETSC_ERR_MPI, PETSC_ERROR_INITIAL, " ");
42: PetscFunctionReturn(NULL);
43: }
44: if (!iflg) { /* PetscViewer not yet created */
45: PetscCallNull(PetscViewerSAWsOpen(comm, &viewer));
46: PetscCallNull(PetscObjectRegisterDestroy((PetscObject)viewer));
47: ierr = (PetscErrorCode)MPI_Comm_set_attr(ncomm, Petsc_Viewer_SAWs_keyval, (void *)viewer);
48: if (ierr) {
49: ierr = PetscError(ncomm, __LINE__, "PETSC_VIEWER_SAWS_", __FILE__, PETSC_ERR_MPI, PETSC_ERROR_INITIAL, " ");
50: PetscFunctionReturn(NULL);
51: }
52: }
53: PetscCallNull(PetscCommDestroy(&ncomm));
54: PetscFunctionReturn(viewer);
55: }
57: static PetscErrorCode PetscViewerDestroy_SAWs(PetscViewer viewer)
58: {
59: PetscFunctionBegin;
60: /*
61: Make sure that we mark that the stack is no longer published
62: */
63: if (PetscObjectComm((PetscObject)viewer) == PETSC_COMM_WORLD) PetscCall(PetscStackSAWsViewOff());
64: PetscFunctionReturn(PETSC_SUCCESS);
65: }
67: PETSC_EXTERN PetscErrorCode PetscViewerCreate_SAWs(PetscViewer v)
68: {
69: PetscFunctionBegin;
70: v->ops->destroy = PetscViewerDestroy_SAWs;
71: PetscFunctionReturn(PETSC_SUCCESS);
72: }