Actual source code: ams.c
petsc-3.5.4 2015-05-23
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 memory snooper PetscViewer shared by all processors
16: in a communicator.
18: Collective on MPI_Comm
20: Input Parameters:
21: . comm - the MPI communicator to share the PetscViewer
23: Level: developer
25: Notes:
26: Unlike almost all other PETSc routines, PETSC_VIEWER_SAWS_() does not return
27: an error code. The window PetscViewer is usually used in the form
28: $ XXXView(XXX object,PETSC_VIEWER_SAWS_(comm));
30: .seealso: PETSC_VIEWER_SAWS_WORLD, PETSC_VIEWER_SAWS_SELF
31: @*/
32: PetscViewer PETSC_VIEWER_SAWS_(MPI_Comm comm)
33: {
35: PetscMPIInt flag;
36: PetscViewer viewer;
37: MPI_Comm ncomm;
40: PetscCommDuplicate(comm,&ncomm,NULL);if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
41: if (Petsc_Viewer_SAWs_keyval == MPI_KEYVAL_INVALID) {
42: MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_SAWs_keyval,0);
43: if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,1,PETSC_ERROR_INITIAL," "); viewer = NULL;}
44: }
45: MPI_Attr_get(ncomm,Petsc_Viewer_SAWs_keyval,(void**)&viewer,&flag);
46: if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,1,PETSC_ERROR_INITIAL," "); viewer = NULL;}
47: if (!flag) { /* PetscViewer not yet created */
48: PetscViewerSAWsOpen(comm,&viewer);
49: if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,1,PETSC_ERROR_INITIAL," "); viewer = NULL;}
50: PetscObjectRegisterDestroy((PetscObject)viewer);
51: if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,1,PETSC_ERROR_INITIAL," "); viewer = NULL;}
52: MPI_Attr_put(ncomm,Petsc_Viewer_SAWs_keyval,(void*)viewer);
53: if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,1,PETSC_ERROR_INITIAL," "); viewer = NULL;}
54: }
55: PetscCommDestroy(&ncomm);
56: if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
57: PetscFunctionReturn(viewer);
58: }
60: /*
61: If there is a PetscViewer associated with this communicator, it is destroyed.
62: */
65: PetscErrorCode PetscViewer_SAWS_Destroy(MPI_Comm comm)
66: {
68: PetscMPIInt flag;
69: PetscViewer viewer;
72: if (Petsc_Viewer_SAWs_keyval == MPI_KEYVAL_INVALID) return(0);
74: MPI_Attr_get(comm,Petsc_Viewer_SAWs_keyval,(void**)&viewer,&flag);
75: if (flag) {
76: PetscViewerDestroy(&viewer);
77: MPI_Attr_delete(comm,Petsc_Viewer_SAWs_keyval);
78: }
79: return(0);
80: }
84: static PetscErrorCode PetscViewerDestroy_SAWs(PetscViewer viewer)
85: {
89: /*
90: Make sure that we mark that the stack is no longer published
91: */
92: if (PetscObjectComm((PetscObject)viewer) == PETSC_COMM_WORLD) {
93: PetscStackSAWsViewOff();
94: }
95: return(0);
96: }
100: PETSC_EXTERN PetscErrorCode PetscViewerCreate_SAWs(PetscViewer v)
101: {
103: v->ops->destroy = PetscViewerDestroy_SAWs;
104: return(0);
105: }