xref: /petsc/src/sys/classes/viewer/impls/ams/ams.c (revision d083f849a86f1f43e18d534ee43954e2786cb29a)
15c6c1daeSBarry Smith 
2af0996ceSBarry 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 /*@C
13aef26384SBarry Smith      PETSC_VIEWER_SAWS_ - Creates an SAWs PetscViewer shared by all processors in a communicator.
145c6c1daeSBarry Smith 
15*d083f849SBarry Smith      Collective
165c6c1daeSBarry Smith 
175c6c1daeSBarry Smith      Input Parameters:
185c6c1daeSBarry Smith .    comm - the MPI communicator to share the PetscViewer
195c6c1daeSBarry Smith 
205c6c1daeSBarry Smith      Level: developer
215c6c1daeSBarry Smith 
225c6c1daeSBarry Smith      Notes:
23e04113cfSBarry Smith      Unlike almost all other PETSc routines, PETSC_VIEWER_SAWS_() does not return
24aef26384SBarry Smith      an error code.  The resulting PetscViewer is usually used in the form
25e04113cfSBarry Smith $       XXXView(XXX object,PETSC_VIEWER_SAWS_(comm));
265c6c1daeSBarry Smith 
27e04113cfSBarry Smith .seealso: PETSC_VIEWER_SAWS_WORLD, PETSC_VIEWER_SAWS_SELF
285c6c1daeSBarry Smith @*/
29e04113cfSBarry Smith PetscViewer PETSC_VIEWER_SAWS_(MPI_Comm comm)
305c6c1daeSBarry Smith {
315c6c1daeSBarry Smith   PetscErrorCode ierr;
325c6c1daeSBarry Smith   PetscMPIInt    flag;
335c6c1daeSBarry Smith   PetscViewer    viewer;
345c6c1daeSBarry Smith   MPI_Comm       ncomm;
355c6c1daeSBarry Smith 
365c6c1daeSBarry Smith   PetscFunctionBegin;
371a1c1e04SBarry Smith   ierr = PetscCommDuplicate(comm,&ncomm,NULL);if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
38e04113cfSBarry Smith   if (Petsc_Viewer_SAWs_keyval == MPI_KEYVAL_INVALID) {
3912801b39SBarry Smith     ierr = MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN,MPI_COMM_NULL_DELETE_FN,&Petsc_Viewer_SAWs_keyval,0);
407737a228SBarry Smith     if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,1,PETSC_ERROR_INITIAL," "); viewer = NULL;}
415c6c1daeSBarry Smith   }
4247435625SJed Brown   ierr = MPI_Comm_get_attr(ncomm,Petsc_Viewer_SAWs_keyval,(void**)&viewer,&flag);
437737a228SBarry Smith   if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,1,PETSC_ERROR_INITIAL," "); viewer = NULL;}
445c6c1daeSBarry Smith   if (!flag) { /* PetscViewer not yet created */
4592081c13SBarry Smith     ierr = PetscViewerSAWsOpen(comm,&viewer);
467737a228SBarry Smith     if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,1,PETSC_ERROR_INITIAL," "); viewer = NULL;}
475c6c1daeSBarry Smith     ierr = PetscObjectRegisterDestroy((PetscObject)viewer);
487737a228SBarry Smith     if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,1,PETSC_ERROR_INITIAL," "); viewer = NULL;}
4947435625SJed Brown     ierr = MPI_Comm_set_attr(ncomm,Petsc_Viewer_SAWs_keyval,(void*)viewer);
507737a228SBarry Smith     if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,1,PETSC_ERROR_INITIAL," "); viewer = NULL;}
515c6c1daeSBarry Smith   }
528b9c1301SBarry Smith   ierr = PetscCommDestroy(&ncomm);
537737a228SBarry Smith   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
545c6c1daeSBarry Smith   PetscFunctionReturn(viewer);
555c6c1daeSBarry Smith }
565c6c1daeSBarry Smith 
575c6c1daeSBarry Smith /*
585c6c1daeSBarry Smith        If there is a PetscViewer associated with this communicator, it is destroyed.
595c6c1daeSBarry Smith */
60e04113cfSBarry Smith PetscErrorCode PetscViewer_SAWS_Destroy(MPI_Comm comm)
615c6c1daeSBarry Smith {
625c6c1daeSBarry Smith   PetscErrorCode ierr;
635c6c1daeSBarry Smith   PetscMPIInt    flag;
645c6c1daeSBarry Smith   PetscViewer    viewer;
655c6c1daeSBarry Smith 
665c6c1daeSBarry Smith   PetscFunctionBegin;
67e04113cfSBarry Smith   if (Petsc_Viewer_SAWs_keyval == MPI_KEYVAL_INVALID) PetscFunctionReturn(0);
68a297a907SKarl Rupp 
6947435625SJed Brown   ierr = MPI_Comm_get_attr(comm,Petsc_Viewer_SAWs_keyval,(void**)&viewer,&flag);CHKERRQ(ierr);
705c6c1daeSBarry Smith   if (flag) {
715c6c1daeSBarry Smith     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
7247435625SJed Brown     ierr = MPI_Comm_delete_attr(comm,Petsc_Viewer_SAWs_keyval);CHKERRQ(ierr);
735c6c1daeSBarry Smith   }
745c6c1daeSBarry Smith   PetscFunctionReturn(0);
755c6c1daeSBarry Smith }
765c6c1daeSBarry Smith 
77e04113cfSBarry Smith static PetscErrorCode PetscViewerDestroy_SAWs(PetscViewer viewer)
785c6c1daeSBarry Smith {
795c6c1daeSBarry Smith   PetscErrorCode ierr;
805c6c1daeSBarry Smith 
815c6c1daeSBarry Smith   PetscFunctionBegin;
825c6c1daeSBarry Smith   /*
835c6c1daeSBarry Smith      Make sure that we mark that the stack is no longer published
845c6c1daeSBarry Smith   */
85ce94432eSBarry Smith   if (PetscObjectComm((PetscObject)viewer) == PETSC_COMM_WORLD) {
86e04113cfSBarry Smith     ierr = PetscStackSAWsViewOff();CHKERRQ(ierr);
875c6c1daeSBarry Smith   }
885c6c1daeSBarry Smith   PetscFunctionReturn(0);
895c6c1daeSBarry Smith }
905c6c1daeSBarry Smith 
91e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscViewerCreate_SAWs(PetscViewer v)
925c6c1daeSBarry Smith {
935c6c1daeSBarry Smith   PetscFunctionBegin;
94e04113cfSBarry Smith   v->ops->destroy = PetscViewerDestroy_SAWs;
955c6c1daeSBarry Smith   PetscFunctionReturn(0);
965c6c1daeSBarry Smith }
9799e0435eSBarry Smith 
985c6c1daeSBarry Smith 
995c6c1daeSBarry Smith 
100