15c6c1daeSBarry Smith 25c6c1daeSBarry Smith #include <petsc-private/viewerimpl.h> 35c6c1daeSBarry Smith #include <petscsys.h> 45c6c1daeSBarry Smith 55c6c1daeSBarry Smith #include <ams.h> 65c6c1daeSBarry Smith typedef struct { 75c6c1daeSBarry Smith char *ams_name; 85c6c1daeSBarry Smith AMS_Comm ams_comm; 95c6c1daeSBarry Smith } PetscViewer_AMS; 105c6c1daeSBarry Smith 115c6c1daeSBarry Smith EXTERN_C_BEGIN 125c6c1daeSBarry Smith #undef __FUNCT__ 135c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerAMSSetCommName_AMS" 145c6c1daeSBarry Smith PetscErrorCode PetscViewerAMSSetCommName_AMS(PetscViewer v,const char name[]) 155c6c1daeSBarry Smith { 165c6c1daeSBarry Smith PetscViewer_AMS *vams = (PetscViewer_AMS*)v->data; 175c6c1daeSBarry Smith PetscErrorCode ierr; 185c6c1daeSBarry Smith int port = -1; 195c6c1daeSBarry Smith PetscBool flg,flg2; 205c6c1daeSBarry Smith char m[64]; 215c6c1daeSBarry Smith 225c6c1daeSBarry Smith PetscFunctionBegin; 230298fd71SBarry Smith ierr = PetscOptionsGetInt(NULL,"-ams_port",&port,NULL);CHKERRQ(ierr); 245c6c1daeSBarry Smith ierr = PetscInfo1(v,"Publishing with the AMS on port %d\n",port);CHKERRQ(ierr); 25*ce94432eSBarry Smith ierr = AMS_Comm_publish((char*)name,&vams->ams_comm,MPI_TYPE,PetscObjectComm((PetscObject)v),&port);CHKERRQ(ierr); 265c6c1daeSBarry Smith 270298fd71SBarry Smith ierr = PetscOptionsHasName(NULL,"-ams_printf",&flg);CHKERRQ(ierr); 285c6c1daeSBarry Smith if (!flg) { 295c6c1daeSBarry Smith #if !defined(PETSC_MISSING_DEV_NULL) 305c6c1daeSBarry Smith ierr = AMS_Set_output_file("/dev/null");CHKERRQ(ierr); 315c6c1daeSBarry Smith #endif 325c6c1daeSBarry Smith } 335c6c1daeSBarry Smith 340298fd71SBarry Smith ierr = PetscOptionsGetString(NULL,"-ams_matlab",m,16,&flg);CHKERRQ(ierr); 355c6c1daeSBarry Smith if (flg) { 365c6c1daeSBarry Smith FILE *fp; 37*ce94432eSBarry Smith ierr = PetscStartMatlab(PetscObjectComm((PetscObject)v),m,"petscview",&fp);CHKERRQ(ierr); 385c6c1daeSBarry Smith } 395c6c1daeSBarry Smith 405c6c1daeSBarry Smith ierr = PetscGetHostName(m,64);CHKERRQ(ierr); 410298fd71SBarry Smith ierr = PetscOptionsHasName(NULL,"-ams_java",&flg);CHKERRQ(ierr); 425c6c1daeSBarry Smith if (flg) { 430298fd71SBarry Smith ierr = PetscOptionsGetString(NULL,"-ams_java",m,64,&flg);CHKERRQ(ierr); 440298fd71SBarry Smith ierr = PetscOptionsHasName(NULL,"-options_gui",&flg2);CHKERRQ(ierr); 455c6c1daeSBarry Smith if (flg2) { 465c6c1daeSBarry Smith char cmd[PETSC_MAX_PATH_LEN]; 475c6c1daeSBarry Smith ierr = PetscStrcpy(cmd,"cd ${PETSC_DIR}/${PETSC_ARCH}/bin;java -d64 -classpath .:");CHKERRQ(ierr); 485c6c1daeSBarry Smith ierr = PetscStrcat(cmd,PETSC_AMS_DIR);CHKERRQ(ierr); 495c6c1daeSBarry Smith ierr = PetscStrcat(cmd,"/java -Djava.library.path=");CHKERRQ(ierr); 505c6c1daeSBarry Smith ierr = PetscStrcat(cmd,PETSC_AMS_DIR);CHKERRQ(ierr); 515c6c1daeSBarry Smith ierr = PetscStrcat(cmd,"/lib amsoptions -ams_server ${HOSTNAME}");CHKERRQ(ierr); 52*ce94432eSBarry Smith ierr = PetscPOpen(PetscObjectComm((PetscObject)v),m,cmd,"r",NULL);CHKERRQ(ierr); 535c6c1daeSBarry Smith } 545c6c1daeSBarry Smith } 555c6c1daeSBarry Smith PetscFunctionReturn(0); 565c6c1daeSBarry Smith } 575c6c1daeSBarry Smith EXTERN_C_END 585c6c1daeSBarry Smith 595c6c1daeSBarry Smith EXTERN_C_BEGIN 605c6c1daeSBarry Smith #undef __FUNCT__ 615c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerAMSGetAMSComm_AMS" 625c6c1daeSBarry Smith PetscErrorCode PetscViewerAMSGetAMSComm_AMS(PetscViewer lab,AMS_Comm *ams_comm) 635c6c1daeSBarry Smith { 645c6c1daeSBarry Smith PetscViewer_AMS *vams = (PetscViewer_AMS*)lab->data; 655c6c1daeSBarry Smith 665c6c1daeSBarry Smith PetscFunctionBegin; 67*ce94432eSBarry Smith if (vams->ams_comm == -1) SETERRQ(PetscObjectComm((PetscObject)lab),PETSC_ERR_ARG_WRONGSTATE,"AMS communicator name not yet set with PetscViewerAMSSetCommName()"); 685c6c1daeSBarry Smith *ams_comm = vams->ams_comm; 695c6c1daeSBarry Smith PetscFunctionReturn(0); 705c6c1daeSBarry Smith } 715c6c1daeSBarry Smith EXTERN_C_END 725c6c1daeSBarry Smith 735c6c1daeSBarry Smith #undef __FUNCT__ 745c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerAMSSetCommName" 755c6c1daeSBarry Smith PetscErrorCode PetscViewerAMSSetCommName(PetscViewer v,const char name[]) 765c6c1daeSBarry Smith { 775c6c1daeSBarry Smith PetscErrorCode ierr; 785c6c1daeSBarry Smith 795c6c1daeSBarry Smith PetscFunctionBegin; 805c6c1daeSBarry Smith PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,1); 815c6c1daeSBarry Smith ierr = PetscTryMethod(v,"PetscViewerAMSSetCommName_C",(PetscViewer,const char[]),(v,name));CHKERRQ(ierr); 825c6c1daeSBarry Smith PetscFunctionReturn(0); 835c6c1daeSBarry Smith } 845c6c1daeSBarry Smith 855c6c1daeSBarry Smith #undef __FUNCT__ 865c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerAMSGetAMSComm" 875c6c1daeSBarry Smith /*@C 885c6c1daeSBarry Smith PetscViewerAMSGetAMSComm - Gets the AMS communicator associated with the PetscViewer. 895c6c1daeSBarry Smith 905c6c1daeSBarry Smith Collective on MPI_Comm 915c6c1daeSBarry Smith 925c6c1daeSBarry Smith Input Parameters: 935c6c1daeSBarry Smith . lab - the PetscViewer 945c6c1daeSBarry Smith 955c6c1daeSBarry Smith Output Parameter: 965c6c1daeSBarry Smith . ams_comm - the AMS communicator 975c6c1daeSBarry Smith 985c6c1daeSBarry Smith Level: developer 995c6c1daeSBarry Smith 1005c6c1daeSBarry Smith Fortran Note: 1015c6c1daeSBarry Smith This routine is not supported in Fortran. 1025c6c1daeSBarry Smith 1035c6c1daeSBarry Smith Concepts: publishing variables 1045c6c1daeSBarry Smith Concepts: AMS^getting communicator 1055c6c1daeSBarry Smith Concepts: communicator^accessing AMS communicator 1065c6c1daeSBarry Smith 1075c6c1daeSBarry Smith .seealso: PetscViewerDestroy(), PetscViewerAMSOpen(), PetscViewer_AMS_, PetscViewer_AMS_WORLD, PetscViewer_AMS_SELF 1085c6c1daeSBarry Smith 1095c6c1daeSBarry Smith @*/ 1105c6c1daeSBarry Smith PetscErrorCode PetscViewerAMSGetAMSComm(PetscViewer v,AMS_Comm *ams_comm) 1115c6c1daeSBarry Smith { 1125c6c1daeSBarry Smith PetscErrorCode ierr; 1135c6c1daeSBarry Smith 1145c6c1daeSBarry Smith PetscFunctionBegin; 1155c6c1daeSBarry Smith PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,1); 1165c6c1daeSBarry Smith ierr = PetscTryMethod(v,"PetscViewerAMSGetAMSComm_C",(PetscViewer,AMS_Comm*),(v,ams_comm));CHKERRQ(ierr); 1175c6c1daeSBarry Smith PetscFunctionReturn(0); 1185c6c1daeSBarry Smith } 1195c6c1daeSBarry Smith 1205c6c1daeSBarry Smith /* 1215c6c1daeSBarry Smith The variable Petsc_Viewer_Ams_keyval is used to indicate an MPI attribute that 1225c6c1daeSBarry Smith is attached to a communicator, in this case the attribute is a PetscViewer. 1235c6c1daeSBarry Smith */ 1245c6c1daeSBarry Smith static PetscMPIInt Petsc_Viewer_Ams_keyval = MPI_KEYVAL_INVALID; 1255c6c1daeSBarry Smith 1265c6c1daeSBarry Smith #undef __FUNCT__ 1275c6c1daeSBarry Smith #define __FUNCT__ "PETSC_VIEWER_AMS_" 1285c6c1daeSBarry Smith /*@C 1295c6c1daeSBarry Smith PETSC_VIEWER_AMS_ - Creates an AMS memory snooper PetscViewer shared by all processors 1305c6c1daeSBarry Smith in a communicator. 1315c6c1daeSBarry Smith 1325c6c1daeSBarry Smith Collective on MPI_Comm 1335c6c1daeSBarry Smith 1345c6c1daeSBarry Smith Input Parameters: 1355c6c1daeSBarry Smith . comm - the MPI communicator to share the PetscViewer 1365c6c1daeSBarry Smith 1375c6c1daeSBarry Smith Level: developer 1385c6c1daeSBarry Smith 1395c6c1daeSBarry Smith Notes: 1405c6c1daeSBarry Smith Unlike almost all other PETSc routines, PetscViewer_AMS_ does not return 1415c6c1daeSBarry Smith an error code. The window PetscViewer is usually used in the form 1425c6c1daeSBarry Smith $ XXXView(XXX object,PETSC_VIEWER_AMS_(comm)); 1435c6c1daeSBarry Smith 1445c6c1daeSBarry Smith .seealso: PetscViewer_AMS_WORLD, PetscViewer_AMS_SELF, PetscViewerAMSOpen(), 1455c6c1daeSBarry Smith @*/ 1465c6c1daeSBarry Smith PetscViewer PETSC_VIEWER_AMS_(MPI_Comm comm) 1475c6c1daeSBarry Smith { 1485c6c1daeSBarry Smith PetscErrorCode ierr; 1495c6c1daeSBarry Smith PetscMPIInt flag; 1505c6c1daeSBarry Smith PetscViewer viewer; 1515c6c1daeSBarry Smith char name[128]; 1525c6c1daeSBarry Smith MPI_Comm ncomm; 1535c6c1daeSBarry Smith 1545c6c1daeSBarry Smith PetscFunctionBegin; 1550298fd71SBarry Smith ierr = PetscCommDuplicate(comm,&ncomm,NULL);if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);} 1565c6c1daeSBarry Smith if (Petsc_Viewer_Ams_keyval == MPI_KEYVAL_INVALID) { 1575c6c1daeSBarry Smith ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Ams_keyval,0); 1585c6c1daeSBarry Smith if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,PETSC_ERROR_INITIAL," "); viewer = 0;} 1595c6c1daeSBarry Smith } 1605c6c1daeSBarry Smith ierr = MPI_Attr_get(ncomm,Petsc_Viewer_Ams_keyval,(void**)&viewer,&flag); 1615c6c1daeSBarry Smith if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,PETSC_ERROR_INITIAL," "); viewer = 0;} 1625c6c1daeSBarry Smith if (!flag) { /* PetscViewer not yet created */ 1635c6c1daeSBarry Smith ierr = PetscStrcpy(name,"PETSc"); 1645c6c1daeSBarry Smith if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,PETSC_ERROR_INITIAL," "); viewer = 0;} 1655c6c1daeSBarry Smith ierr = PetscViewerAMSOpen(ncomm,name,&viewer); 1665c6c1daeSBarry Smith if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,PETSC_ERROR_INITIAL," "); viewer = 0;} 1675c6c1daeSBarry Smith ierr = PetscObjectRegisterDestroy((PetscObject)viewer); 1685c6c1daeSBarry Smith if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,PETSC_ERROR_INITIAL," "); viewer = 0;} 1695c6c1daeSBarry Smith ierr = MPI_Attr_put(ncomm,Petsc_Viewer_Ams_keyval,(void*)viewer); 1705c6c1daeSBarry Smith if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,PETSC_ERROR_INITIAL," "); viewer = 0;} 1715c6c1daeSBarry Smith } 1725c6c1daeSBarry Smith PetscFunctionReturn(viewer); 1735c6c1daeSBarry Smith } 1745c6c1daeSBarry Smith 1755c6c1daeSBarry Smith /* 1765c6c1daeSBarry Smith If there is a PetscViewer associated with this communicator, it is destroyed. 1775c6c1daeSBarry Smith */ 1785c6c1daeSBarry Smith #undef __FUNCT__ 1795c6c1daeSBarry Smith #define __FUNCT__ "PetscViewer_AMS_Destroy" 1805c6c1daeSBarry Smith PetscErrorCode PetscViewer_AMS_Destroy(MPI_Comm comm) 1815c6c1daeSBarry Smith { 1825c6c1daeSBarry Smith PetscErrorCode ierr; 1835c6c1daeSBarry Smith PetscMPIInt flag; 1845c6c1daeSBarry Smith PetscViewer viewer; 1855c6c1daeSBarry Smith 1865c6c1daeSBarry Smith PetscFunctionBegin; 187a297a907SKarl Rupp if (Petsc_Viewer_Ams_keyval == MPI_KEYVAL_INVALID) PetscFunctionReturn(0); 188a297a907SKarl Rupp 1895c6c1daeSBarry Smith ierr = MPI_Attr_get(comm,Petsc_Viewer_Ams_keyval,(void**)&viewer,&flag);CHKERRQ(ierr); 1905c6c1daeSBarry Smith if (flag) { 1915c6c1daeSBarry Smith ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 1925c6c1daeSBarry Smith ierr = MPI_Attr_delete(comm,Petsc_Viewer_Ams_keyval);CHKERRQ(ierr); 1935c6c1daeSBarry Smith } 1945c6c1daeSBarry Smith PetscFunctionReturn(0); 1955c6c1daeSBarry Smith } 1965c6c1daeSBarry Smith 1975c6c1daeSBarry Smith #undef __FUNCT__ 1985c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerDestroy_AMS" 1995c6c1daeSBarry Smith static PetscErrorCode PetscViewerDestroy_AMS(PetscViewer viewer) 2005c6c1daeSBarry Smith { 2015c6c1daeSBarry Smith PetscViewer_AMS *vams = (PetscViewer_AMS*)viewer->data; 2025c6c1daeSBarry Smith PetscErrorCode ierr; 2035c6c1daeSBarry Smith 2045c6c1daeSBarry Smith PetscFunctionBegin; 2055c6c1daeSBarry Smith /* 2065c6c1daeSBarry Smith Make sure that we mark that the stack is no longer published 2075c6c1daeSBarry Smith */ 208*ce94432eSBarry Smith if (PetscObjectComm((PetscObject)viewer) == PETSC_COMM_WORLD) { 2095c6c1daeSBarry Smith ierr = PetscStackDepublish();CHKERRQ(ierr); 2105c6c1daeSBarry Smith } 2115c6c1daeSBarry Smith 2125c6c1daeSBarry Smith ierr = AMS_Comm_destroy(vams->ams_comm); 2135c6c1daeSBarry Smith if (ierr) { 2145c6c1daeSBarry Smith char *err; 2155c6c1daeSBarry Smith AMS_Explain_error(ierr,&err); 216*ce94432eSBarry Smith SETERRQ(PetscObjectComm((PetscObject)viewer),ierr,err); 2175c6c1daeSBarry Smith } 2185c6c1daeSBarry Smith ierr = PetscFree(vams);CHKERRQ(ierr); 2195c6c1daeSBarry Smith PetscFunctionReturn(0); 2205c6c1daeSBarry Smith } 2215c6c1daeSBarry Smith 2225c6c1daeSBarry Smith EXTERN_C_BEGIN 2235c6c1daeSBarry Smith #undef __FUNCT__ 2245c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerCreate_AMS" 2255c6c1daeSBarry Smith PetscErrorCode PetscViewerCreate_AMS(PetscViewer v) 2265c6c1daeSBarry Smith { 2275c6c1daeSBarry Smith PetscViewer_AMS *vams; 2285c6c1daeSBarry Smith PetscErrorCode ierr; 2295c6c1daeSBarry Smith 2305c6c1daeSBarry Smith PetscFunctionBegin; 2315c6c1daeSBarry Smith v->ops->destroy = PetscViewerDestroy_AMS; 2325c6c1daeSBarry Smith ierr = PetscNew(PetscViewer_AMS,&vams);CHKERRQ(ierr); 2335c6c1daeSBarry Smith v->data = (void*)vams; 2345c6c1daeSBarry Smith vams->ams_comm = -1; 235a297a907SKarl Rupp 2365c6c1daeSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerAMSSetCommName_C", 2375c6c1daeSBarry Smith "PetscViewerAMSSetCommName_AMS", 2385c6c1daeSBarry Smith PetscViewerAMSSetCommName_AMS);CHKERRQ(ierr); 2395c6c1daeSBarry Smith ierr = PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerAMSGetAMSComm_C", 2405c6c1daeSBarry Smith "PetscViewerAMSGetAMSComm_AMS", 2415c6c1daeSBarry Smith PetscViewerAMSGetAMSComm_AMS);CHKERRQ(ierr); 2425c6c1daeSBarry Smith PetscFunctionReturn(0); 2435c6c1daeSBarry Smith } 2445c6c1daeSBarry Smith EXTERN_C_END 2455c6c1daeSBarry Smith 2465c6c1daeSBarry Smith 247