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 15d083f849SBarry 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 27*db781477SPatrick Sanan .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); 402cb5e1ccSBarry Smith if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,1,PETSC_ERROR_INITIAL," "); PetscFunctionReturn(NULL);} 415c6c1daeSBarry Smith } 4247435625SJed Brown ierr = MPI_Comm_get_attr(ncomm,Petsc_Viewer_SAWs_keyval,(void**)&viewer,&flag); 432cb5e1ccSBarry Smith if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,1,PETSC_ERROR_INITIAL," "); PetscFunctionReturn(NULL);} 445c6c1daeSBarry Smith if (!flag) { /* PetscViewer not yet created */ 4592081c13SBarry Smith ierr = PetscViewerSAWsOpen(comm,&viewer); 462cb5e1ccSBarry Smith if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,1,PETSC_ERROR_REPEAT," "); PetscFunctionReturn(NULL);} 475c6c1daeSBarry Smith ierr = PetscObjectRegisterDestroy((PetscObject)viewer); 482cb5e1ccSBarry Smith if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,1,PETSC_ERROR_REPEAT," "); PetscFunctionReturn(NULL);} 4947435625SJed Brown ierr = MPI_Comm_set_attr(ncomm,Petsc_Viewer_SAWs_keyval,(void*)viewer); 502cb5e1ccSBarry Smith if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,1,PETSC_ERROR_INITIAL," "); PetscFunctionReturn(NULL);} 515c6c1daeSBarry Smith } 528b9c1301SBarry Smith ierr = PetscCommDestroy(&ncomm); 532cb5e1ccSBarry Smith if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_SAWS_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_REPEAT," ");PetscFunctionReturn(NULL);} 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 PetscMPIInt flag; 635c6c1daeSBarry Smith PetscViewer viewer; 645c6c1daeSBarry Smith 655c6c1daeSBarry Smith PetscFunctionBegin; 66e04113cfSBarry Smith if (Petsc_Viewer_SAWs_keyval == MPI_KEYVAL_INVALID) PetscFunctionReturn(0); 67a297a907SKarl Rupp 689566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_get_attr(comm,Petsc_Viewer_SAWs_keyval,(void**)&viewer,&flag)); 695c6c1daeSBarry Smith if (flag) { 709566063dSJacob Faibussowitsch PetscCall(PetscViewerDestroy(&viewer)); 719566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_delete_attr(comm,Petsc_Viewer_SAWs_keyval)); 725c6c1daeSBarry Smith } 735c6c1daeSBarry Smith PetscFunctionReturn(0); 745c6c1daeSBarry Smith } 755c6c1daeSBarry Smith 76e04113cfSBarry Smith static PetscErrorCode PetscViewerDestroy_SAWs(PetscViewer viewer) 775c6c1daeSBarry Smith { 785c6c1daeSBarry Smith PetscFunctionBegin; 795c6c1daeSBarry Smith /* 805c6c1daeSBarry Smith Make sure that we mark that the stack is no longer published 815c6c1daeSBarry Smith */ 829566063dSJacob Faibussowitsch if (PetscObjectComm((PetscObject)viewer) == PETSC_COMM_WORLD) PetscCall(PetscStackSAWsViewOff()); 835c6c1daeSBarry Smith PetscFunctionReturn(0); 845c6c1daeSBarry Smith } 855c6c1daeSBarry Smith 86e04113cfSBarry Smith PETSC_EXTERN PetscErrorCode PetscViewerCreate_SAWs(PetscViewer v) 875c6c1daeSBarry Smith { 885c6c1daeSBarry Smith PetscFunctionBegin; 89e04113cfSBarry Smith v->ops->destroy = PetscViewerDestroy_SAWs; 905c6c1daeSBarry Smith PetscFunctionReturn(0); 915c6c1daeSBarry Smith } 92