xref: /petsc/src/sys/classes/viewer/impls/ams/ams.c (revision 7737a2283cf556a0edfb21b038abf2d3e4668ed6)
15c6c1daeSBarry Smith 
25c6c1daeSBarry Smith #include <petsc-private/viewerimpl.h>
3e04113cfSBarry Smith #include <petscviewersaws.h>
45c6c1daeSBarry Smith #include <petscsys.h>
55c6c1daeSBarry Smith 
65c6c1daeSBarry Smith /*
7e04113cfSBarry Smith     The variable Petsc_Viewer_SAWs_keyval is used to indicate an MPI attribute that
85c6c1daeSBarry Smith   is attached to a communicator, in this case the attribute is a PetscViewer.
95c6c1daeSBarry Smith */
10e04113cfSBarry Smith static PetscMPIInt Petsc_Viewer_SAWs_keyval = MPI_KEYVAL_INVALID;
115c6c1daeSBarry Smith 
125c6c1daeSBarry Smith #undef __FUNCT__
13e04113cfSBarry Smith #define __FUNCT__ "PETSC_VIEWER_SAWS_"
145c6c1daeSBarry Smith /*@C
15e04113cfSBarry Smith      PETSC_VIEWER_SAWS_ - Creates an SAWs memory snooper PetscViewer shared by all processors
165c6c1daeSBarry Smith                    in a communicator.
175c6c1daeSBarry Smith 
185c6c1daeSBarry Smith      Collective on MPI_Comm
195c6c1daeSBarry Smith 
205c6c1daeSBarry Smith      Input Parameters:
215c6c1daeSBarry Smith .    comm - the MPI communicator to share the PetscViewer
225c6c1daeSBarry Smith 
235c6c1daeSBarry Smith      Level: developer
245c6c1daeSBarry Smith 
255c6c1daeSBarry Smith      Notes:
26e04113cfSBarry Smith      Unlike almost all other PETSc routines, PETSC_VIEWER_SAWS_() does not return
275c6c1daeSBarry Smith      an error code.  The window PetscViewer is usually used in the form
28e04113cfSBarry Smith $       XXXView(XXX object,PETSC_VIEWER_SAWS_(comm));
295c6c1daeSBarry Smith 
30e04113cfSBarry Smith .seealso: PETSC_VIEWER_SAWS_WORLD, PETSC_VIEWER_SAWS_SELF
315c6c1daeSBarry Smith @*/
32e04113cfSBarry Smith PetscViewer PETSC_VIEWER_SAWS_(MPI_Comm comm)
335c6c1daeSBarry Smith {
345c6c1daeSBarry Smith   PetscErrorCode ierr;
355c6c1daeSBarry Smith   PetscMPIInt    flag;
365c6c1daeSBarry Smith   PetscViewer    viewer;
375c6c1daeSBarry Smith   MPI_Comm       ncomm;
385c6c1daeSBarry Smith 
395c6c1daeSBarry Smith   PetscFunctionBegin;
40e04113cfSBarry Smith   ierr = PetscCommDuplicate(comm,&ncomm,NULL);if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
41e04113cfSBarry Smith   if (Petsc_Viewer_SAWs_keyval == MPI_KEYVAL_INVALID) {
42e04113cfSBarry Smith     ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_SAWs_keyval,0);
43*7737a228SBarry Smith     if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,1,PETSC_ERROR_INITIAL," "); viewer = NULL;}
445c6c1daeSBarry Smith   }
45e04113cfSBarry Smith   ierr = MPI_Attr_get(ncomm,Petsc_Viewer_SAWs_keyval,(void**)&viewer,&flag);
46*7737a228SBarry Smith   if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,1,PETSC_ERROR_INITIAL," "); viewer = NULL;}
475c6c1daeSBarry Smith   if (!flag) { /* PetscViewer not yet created */
4892081c13SBarry Smith     ierr = PetscViewerSAWsOpen(comm,&viewer);
49*7737a228SBarry Smith     if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,1,PETSC_ERROR_INITIAL," "); viewer = NULL;}
505c6c1daeSBarry Smith     ierr = PetscObjectRegisterDestroy((PetscObject)viewer);
51*7737a228SBarry Smith     if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,1,PETSC_ERROR_INITIAL," "); viewer = NULL;}
52e04113cfSBarry Smith     ierr = MPI_Attr_put(ncomm,Petsc_Viewer_SAWs_keyval,(void*)viewer);
53*7737a228SBarry Smith     if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,1,PETSC_ERROR_INITIAL," "); viewer = NULL;}
545c6c1daeSBarry Smith   }
558b9c1301SBarry Smith   ierr = PetscCommDestroy(&ncomm);
56*7737a228SBarry Smith   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
575c6c1daeSBarry Smith   PetscFunctionReturn(viewer);
585c6c1daeSBarry Smith }
595c6c1daeSBarry Smith 
605c6c1daeSBarry Smith /*
615c6c1daeSBarry Smith        If there is a PetscViewer associated with this communicator, it is destroyed.
625c6c1daeSBarry Smith */
635c6c1daeSBarry Smith #undef __FUNCT__
64e04113cfSBarry Smith #define __FUNCT__ "PetscViewer_SAWS_Destroy"
65e04113cfSBarry Smith PetscErrorCode PetscViewer_SAWS_Destroy(MPI_Comm comm)
665c6c1daeSBarry Smith {
675c6c1daeSBarry Smith   PetscErrorCode ierr;
685c6c1daeSBarry Smith   PetscMPIInt    flag;
695c6c1daeSBarry Smith   PetscViewer    viewer;
705c6c1daeSBarry Smith 
715c6c1daeSBarry Smith   PetscFunctionBegin;
72e04113cfSBarry Smith   if (Petsc_Viewer_SAWs_keyval == MPI_KEYVAL_INVALID) PetscFunctionReturn(0);
73a297a907SKarl Rupp 
74e04113cfSBarry Smith   ierr = MPI_Attr_get(comm,Petsc_Viewer_SAWs_keyval,(void**)&viewer,&flag);CHKERRQ(ierr);
755c6c1daeSBarry Smith   if (flag) {
765c6c1daeSBarry Smith     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
77e04113cfSBarry Smith     ierr = MPI_Attr_delete(comm,Petsc_Viewer_SAWs_keyval);CHKERRQ(ierr);
785c6c1daeSBarry Smith   }
795c6c1daeSBarry Smith   PetscFunctionReturn(0);
805c6c1daeSBarry Smith }
815c6c1daeSBarry Smith 
825c6c1daeSBarry Smith #undef __FUNCT__
83e04113cfSBarry Smith #define __FUNCT__ "PetscViewerDestroy_SAWs"
84e04113cfSBarry Smith static PetscErrorCode PetscViewerDestroy_SAWs(PetscViewer viewer)
855c6c1daeSBarry Smith {
865c6c1daeSBarry Smith   PetscErrorCode ierr;
875c6c1daeSBarry Smith 
885c6c1daeSBarry Smith   PetscFunctionBegin;
895c6c1daeSBarry Smith   /*
905c6c1daeSBarry Smith      Make sure that we mark that the stack is no longer published
915c6c1daeSBarry Smith   */
92ce94432eSBarry Smith   if (PetscObjectComm((PetscObject)viewer) == PETSC_COMM_WORLD) {
93e04113cfSBarry Smith     ierr = PetscStackSAWsViewOff();CHKERRQ(ierr);
945c6c1daeSBarry Smith   }
955c6c1daeSBarry Smith   PetscFunctionReturn(0);
965c6c1daeSBarry Smith }
975c6c1daeSBarry Smith 
985c6c1daeSBarry Smith #undef __FUNCT__
99e04113cfSBarry Smith #define __FUNCT__ "PetscViewerCreate_SAWs"
100e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscViewerCreate_SAWs(PetscViewer v)
1015c6c1daeSBarry Smith {
1025c6c1daeSBarry Smith   PetscFunctionBegin;
103e04113cfSBarry Smith   v->ops->destroy = PetscViewerDestroy_SAWs;
1045c6c1daeSBarry Smith   PetscFunctionReturn(0);
1055c6c1daeSBarry Smith }
10699e0435eSBarry Smith 
1075c6c1daeSBarry Smith 
1085c6c1daeSBarry Smith 
109