15c6c1daeSBarry Smith 25c6c1daeSBarry Smith #include <petsc-private/viewerimpl.h> 3*e04113cfSBarry Smith #include <petscviewersaws.h> 45c6c1daeSBarry Smith #include <petscsys.h> 55c6c1daeSBarry Smith 65c6c1daeSBarry Smith /* 7*e04113cfSBarry 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 */ 10*e04113cfSBarry Smith static PetscMPIInt Petsc_Viewer_SAWs_keyval = MPI_KEYVAL_INVALID; 115c6c1daeSBarry Smith 125c6c1daeSBarry Smith #undef __FUNCT__ 13*e04113cfSBarry Smith #define __FUNCT__ "PETSC_VIEWER_SAWS_" 145c6c1daeSBarry Smith /*@C 15*e04113cfSBarry 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: 26*e04113cfSBarry 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 28*e04113cfSBarry Smith $ XXXView(XXX object,PETSC_VIEWER_SAWS_(comm)); 295c6c1daeSBarry Smith 30*e04113cfSBarry Smith .seealso: PETSC_VIEWER_SAWS_WORLD, PETSC_VIEWER_SAWS_SELF 315c6c1daeSBarry Smith @*/ 32*e04113cfSBarry 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; 40*e04113cfSBarry 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);} 41*e04113cfSBarry Smith if (Petsc_Viewer_SAWs_keyval == MPI_KEYVAL_INVALID) { 42*e04113cfSBarry Smith ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_SAWs_keyval,0); 43*e04113cfSBarry Smith if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,__SDIR__,1,PETSC_ERROR_INITIAL," "); viewer = 0;} 445c6c1daeSBarry Smith } 45*e04113cfSBarry Smith ierr = MPI_Attr_get(ncomm,Petsc_Viewer_SAWs_keyval,(void**)&viewer,&flag); 46*e04113cfSBarry Smith if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,__SDIR__,1,PETSC_ERROR_INITIAL," "); viewer = 0;} 475c6c1daeSBarry Smith if (!flag) { /* PetscViewer not yet created */ 48*e04113cfSBarry Smith ierr = PetscViewerSAWsOpen(comm,&viewer);CHKERRQ(ierr); 495c6c1daeSBarry Smith ierr = PetscObjectRegisterDestroy((PetscObject)viewer); 50*e04113cfSBarry Smith if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,__SDIR__,1,PETSC_ERROR_INITIAL," "); viewer = 0;} 51*e04113cfSBarry Smith ierr = MPI_Attr_put(ncomm,Petsc_Viewer_SAWs_keyval,(void*)viewer); 52*e04113cfSBarry Smith if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,__SDIR__,1,PETSC_ERROR_INITIAL," "); viewer = 0;} 535c6c1daeSBarry Smith } 548b9c1301SBarry Smith ierr = PetscCommDestroy(&ncomm); 55*e04113cfSBarry Smith if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);} 565c6c1daeSBarry Smith PetscFunctionReturn(viewer); 575c6c1daeSBarry Smith } 585c6c1daeSBarry Smith 595c6c1daeSBarry Smith /* 605c6c1daeSBarry Smith If there is a PetscViewer associated with this communicator, it is destroyed. 615c6c1daeSBarry Smith */ 625c6c1daeSBarry Smith #undef __FUNCT__ 63*e04113cfSBarry Smith #define __FUNCT__ "PetscViewer_SAWS_Destroy" 64*e04113cfSBarry Smith PetscErrorCode PetscViewer_SAWS_Destroy(MPI_Comm comm) 655c6c1daeSBarry Smith { 665c6c1daeSBarry Smith PetscErrorCode ierr; 675c6c1daeSBarry Smith PetscMPIInt flag; 685c6c1daeSBarry Smith PetscViewer viewer; 695c6c1daeSBarry Smith 705c6c1daeSBarry Smith PetscFunctionBegin; 71*e04113cfSBarry Smith if (Petsc_Viewer_SAWs_keyval == MPI_KEYVAL_INVALID) PetscFunctionReturn(0); 72a297a907SKarl Rupp 73*e04113cfSBarry Smith ierr = MPI_Attr_get(comm,Petsc_Viewer_SAWs_keyval,(void**)&viewer,&flag);CHKERRQ(ierr); 745c6c1daeSBarry Smith if (flag) { 755c6c1daeSBarry Smith ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 76*e04113cfSBarry Smith ierr = MPI_Attr_delete(comm,Petsc_Viewer_SAWs_keyval);CHKERRQ(ierr); 775c6c1daeSBarry Smith } 785c6c1daeSBarry Smith PetscFunctionReturn(0); 795c6c1daeSBarry Smith } 805c6c1daeSBarry Smith 815c6c1daeSBarry Smith #undef __FUNCT__ 82*e04113cfSBarry Smith #define __FUNCT__ "PetscViewerDestroy_SAWs" 83*e04113cfSBarry Smith static PetscErrorCode PetscViewerDestroy_SAWs(PetscViewer viewer) 845c6c1daeSBarry Smith { 855c6c1daeSBarry Smith PetscErrorCode ierr; 865c6c1daeSBarry Smith 875c6c1daeSBarry Smith PetscFunctionBegin; 885c6c1daeSBarry Smith /* 895c6c1daeSBarry Smith Make sure that we mark that the stack is no longer published 905c6c1daeSBarry Smith */ 91ce94432eSBarry Smith if (PetscObjectComm((PetscObject)viewer) == PETSC_COMM_WORLD) { 92*e04113cfSBarry Smith ierr = PetscStackSAWsViewOff();CHKERRQ(ierr); 935c6c1daeSBarry Smith } 945c6c1daeSBarry Smith PetscFunctionReturn(0); 955c6c1daeSBarry Smith } 965c6c1daeSBarry Smith 975c6c1daeSBarry Smith #undef __FUNCT__ 98*e04113cfSBarry Smith #define __FUNCT__ "PetscViewerCreate_SAWs" 99*e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscViewerCreate_SAWs(PetscViewer v) 1005c6c1daeSBarry Smith { 1015c6c1daeSBarry Smith PetscFunctionBegin; 102*e04113cfSBarry Smith v->ops->destroy = PetscViewerDestroy_SAWs; 1035c6c1daeSBarry Smith PetscFunctionReturn(0); 1045c6c1daeSBarry Smith } 10599e0435eSBarry Smith 1065c6c1daeSBarry Smith 1075c6c1daeSBarry Smith 108