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 15*aef26384SBarry Smith PETSC_VIEWER_SAWS_ - Creates an SAWs PetscViewer shared by all processors in a communicator. 165c6c1daeSBarry Smith 175c6c1daeSBarry Smith Collective on MPI_Comm 185c6c1daeSBarry Smith 195c6c1daeSBarry Smith Input Parameters: 205c6c1daeSBarry Smith . comm - the MPI communicator to share the PetscViewer 215c6c1daeSBarry Smith 225c6c1daeSBarry Smith Level: developer 235c6c1daeSBarry Smith 245c6c1daeSBarry Smith Notes: 25e04113cfSBarry Smith Unlike almost all other PETSc routines, PETSC_VIEWER_SAWS_() does not return 26*aef26384SBarry Smith an error code. The resulting PetscViewer is usually used in the form 27e04113cfSBarry Smith $ XXXView(XXX object,PETSC_VIEWER_SAWS_(comm)); 285c6c1daeSBarry Smith 29e04113cfSBarry Smith .seealso: PETSC_VIEWER_SAWS_WORLD, PETSC_VIEWER_SAWS_SELF 305c6c1daeSBarry Smith @*/ 31e04113cfSBarry Smith PetscViewer PETSC_VIEWER_SAWS_(MPI_Comm comm) 325c6c1daeSBarry Smith { 335c6c1daeSBarry Smith PetscErrorCode ierr; 345c6c1daeSBarry Smith PetscMPIInt flag; 355c6c1daeSBarry Smith PetscViewer viewer; 365c6c1daeSBarry Smith MPI_Comm ncomm; 375c6c1daeSBarry Smith 385c6c1daeSBarry Smith PetscFunctionBegin; 391a1c1e04SBarry 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);} 40e04113cfSBarry Smith if (Petsc_Viewer_SAWs_keyval == MPI_KEYVAL_INVALID) { 41e04113cfSBarry Smith ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_SAWs_keyval,0); 427737a228SBarry Smith if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,1,PETSC_ERROR_INITIAL," "); viewer = NULL;} 435c6c1daeSBarry Smith } 44e04113cfSBarry Smith ierr = MPI_Attr_get(ncomm,Petsc_Viewer_SAWs_keyval,(void**)&viewer,&flag); 457737a228SBarry Smith if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,1,PETSC_ERROR_INITIAL," "); viewer = NULL;} 465c6c1daeSBarry Smith if (!flag) { /* PetscViewer not yet created */ 4792081c13SBarry Smith ierr = PetscViewerSAWsOpen(comm,&viewer); 487737a228SBarry Smith if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,1,PETSC_ERROR_INITIAL," "); viewer = NULL;} 495c6c1daeSBarry Smith ierr = PetscObjectRegisterDestroy((PetscObject)viewer); 507737a228SBarry Smith if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,1,PETSC_ERROR_INITIAL," "); viewer = NULL;} 51e04113cfSBarry Smith ierr = MPI_Attr_put(ncomm,Petsc_Viewer_SAWs_keyval,(void*)viewer); 527737a228SBarry Smith if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,1,PETSC_ERROR_INITIAL," "); viewer = NULL;} 535c6c1daeSBarry Smith } 548b9c1301SBarry Smith ierr = PetscCommDestroy(&ncomm); 557737a228SBarry Smith if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,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__ 63e04113cfSBarry Smith #define __FUNCT__ "PetscViewer_SAWS_Destroy" 64e04113cfSBarry 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; 71e04113cfSBarry Smith if (Petsc_Viewer_SAWs_keyval == MPI_KEYVAL_INVALID) PetscFunctionReturn(0); 72a297a907SKarl Rupp 73e04113cfSBarry 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); 76e04113cfSBarry 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__ 82e04113cfSBarry Smith #define __FUNCT__ "PetscViewerDestroy_SAWs" 83e04113cfSBarry 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) { 92e04113cfSBarry Smith ierr = PetscStackSAWsViewOff();CHKERRQ(ierr); 935c6c1daeSBarry Smith } 945c6c1daeSBarry Smith PetscFunctionReturn(0); 955c6c1daeSBarry Smith } 965c6c1daeSBarry Smith 975c6c1daeSBarry Smith #undef __FUNCT__ 98e04113cfSBarry Smith #define __FUNCT__ "PetscViewerCreate_SAWs" 99e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscViewerCreate_SAWs(PetscViewer v) 1005c6c1daeSBarry Smith { 1015c6c1daeSBarry Smith PetscFunctionBegin; 102e04113cfSBarry Smith v->ops->destroy = PetscViewerDestroy_SAWs; 1035c6c1daeSBarry Smith PetscFunctionReturn(0); 1045c6c1daeSBarry Smith } 10599e0435eSBarry Smith 1065c6c1daeSBarry Smith 1075c6c1daeSBarry Smith 108