xref: /petsc/src/sys/classes/viewer/impls/ams/ams.c (revision 8cc058d9cd56c1ccb3be12a47760ddfc446aaffc)
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 #undef __FUNCT__
125c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerAMSSetCommName_AMS"
135c6c1daeSBarry Smith PetscErrorCode PetscViewerAMSSetCommName_AMS(PetscViewer v,const char name[])
145c6c1daeSBarry Smith {
155c6c1daeSBarry Smith   PetscViewer_AMS *vams = (PetscViewer_AMS*)v->data;
165c6c1daeSBarry Smith   PetscErrorCode  ierr;
175c6c1daeSBarry Smith   int             port = -1;
185c6c1daeSBarry Smith   PetscBool       flg,flg2;
195c6c1daeSBarry Smith   char            m[64];
205c6c1daeSBarry Smith 
215c6c1daeSBarry Smith   PetscFunctionBegin;
220298fd71SBarry Smith   ierr = PetscOptionsGetInt(NULL,"-ams_port",&port,NULL);CHKERRQ(ierr);
235c6c1daeSBarry Smith   ierr = PetscInfo1(v,"Publishing with the AMS on port %d\n",port);CHKERRQ(ierr);
24ce94432eSBarry Smith   ierr = AMS_Comm_publish((char*)name,&vams->ams_comm,MPI_TYPE,PetscObjectComm((PetscObject)v),&port);CHKERRQ(ierr);
255c6c1daeSBarry Smith 
260298fd71SBarry Smith   ierr = PetscOptionsHasName(NULL,"-ams_printf",&flg);CHKERRQ(ierr);
275c6c1daeSBarry Smith   if (!flg) {
285c6c1daeSBarry Smith #if !defined(PETSC_MISSING_DEV_NULL)
295c6c1daeSBarry Smith     ierr = AMS_Set_output_file("/dev/null");CHKERRQ(ierr);
305c6c1daeSBarry Smith #endif
315c6c1daeSBarry Smith   }
325c6c1daeSBarry Smith 
330298fd71SBarry Smith   ierr = PetscOptionsGetString(NULL,"-ams_matlab",m,16,&flg);CHKERRQ(ierr);
345c6c1daeSBarry Smith   if (flg) {
355c6c1daeSBarry Smith     FILE *fp;
36ce94432eSBarry Smith     ierr = PetscStartMatlab(PetscObjectComm((PetscObject)v),m,"petscview",&fp);CHKERRQ(ierr);
375c6c1daeSBarry Smith   }
385c6c1daeSBarry Smith 
395c6c1daeSBarry Smith   ierr = PetscGetHostName(m,64);CHKERRQ(ierr);
400298fd71SBarry Smith   ierr = PetscOptionsHasName(NULL,"-ams_java",&flg);CHKERRQ(ierr);
415c6c1daeSBarry Smith   if (flg) {
420298fd71SBarry Smith     ierr = PetscOptionsGetString(NULL,"-ams_java",m,64,&flg);CHKERRQ(ierr);
430298fd71SBarry Smith     ierr = PetscOptionsHasName(NULL,"-options_gui",&flg2);CHKERRQ(ierr);
445c6c1daeSBarry Smith     if (flg2) {
455c6c1daeSBarry Smith       char cmd[PETSC_MAX_PATH_LEN];
465c6c1daeSBarry Smith       ierr = PetscStrcpy(cmd,"cd ${PETSC_DIR}/${PETSC_ARCH}/bin;java -d64 -classpath .:");CHKERRQ(ierr);
475c6c1daeSBarry Smith       ierr = PetscStrcat(cmd,PETSC_AMS_DIR);CHKERRQ(ierr);
485c6c1daeSBarry Smith       ierr = PetscStrcat(cmd,"/java -Djava.library.path=");CHKERRQ(ierr);
495c6c1daeSBarry Smith       ierr = PetscStrcat(cmd,PETSC_AMS_DIR);CHKERRQ(ierr);
505c6c1daeSBarry Smith       ierr = PetscStrcat(cmd,"/lib amsoptions -ams_server ${HOSTNAME}");CHKERRQ(ierr);
51ce94432eSBarry Smith       ierr = PetscPOpen(PetscObjectComm((PetscObject)v),m,cmd,"r",NULL);CHKERRQ(ierr);
525c6c1daeSBarry Smith     }
535c6c1daeSBarry Smith   }
545c6c1daeSBarry Smith   PetscFunctionReturn(0);
555c6c1daeSBarry Smith }
565c6c1daeSBarry Smith 
575c6c1daeSBarry Smith #undef __FUNCT__
585c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerAMSGetAMSComm_AMS"
595c6c1daeSBarry Smith PetscErrorCode PetscViewerAMSGetAMSComm_AMS(PetscViewer lab,AMS_Comm *ams_comm)
605c6c1daeSBarry Smith {
615c6c1daeSBarry Smith   PetscViewer_AMS *vams = (PetscViewer_AMS*)lab->data;
625c6c1daeSBarry Smith 
635c6c1daeSBarry Smith   PetscFunctionBegin;
64ce94432eSBarry Smith   if (vams->ams_comm == -1) SETERRQ(PetscObjectComm((PetscObject)lab),PETSC_ERR_ARG_WRONGSTATE,"AMS communicator name not yet set with PetscViewerAMSSetCommName()");
655c6c1daeSBarry Smith   *ams_comm = vams->ams_comm;
665c6c1daeSBarry Smith   PetscFunctionReturn(0);
675c6c1daeSBarry Smith }
685c6c1daeSBarry Smith 
695c6c1daeSBarry Smith #undef __FUNCT__
705c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerAMSSetCommName"
715c6c1daeSBarry Smith PetscErrorCode PetscViewerAMSSetCommName(PetscViewer v,const char name[])
725c6c1daeSBarry Smith {
735c6c1daeSBarry Smith   PetscErrorCode ierr;
745c6c1daeSBarry Smith 
755c6c1daeSBarry Smith   PetscFunctionBegin;
765c6c1daeSBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,1);
775c6c1daeSBarry Smith   ierr = PetscTryMethod(v,"PetscViewerAMSSetCommName_C",(PetscViewer,const char[]),(v,name));CHKERRQ(ierr);
785c6c1daeSBarry Smith   PetscFunctionReturn(0);
795c6c1daeSBarry Smith }
805c6c1daeSBarry Smith 
815c6c1daeSBarry Smith #undef __FUNCT__
825c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerAMSGetAMSComm"
835c6c1daeSBarry Smith /*@C
845c6c1daeSBarry Smith     PetscViewerAMSGetAMSComm - Gets the AMS communicator associated with the PetscViewer.
855c6c1daeSBarry Smith 
865c6c1daeSBarry Smith     Collective on MPI_Comm
875c6c1daeSBarry Smith 
885c6c1daeSBarry Smith     Input Parameters:
895c6c1daeSBarry Smith .   lab - the PetscViewer
905c6c1daeSBarry Smith 
915c6c1daeSBarry Smith     Output Parameter:
925c6c1daeSBarry Smith .   ams_comm - the AMS communicator
935c6c1daeSBarry Smith 
945c6c1daeSBarry Smith     Level: developer
955c6c1daeSBarry Smith 
965c6c1daeSBarry Smith     Fortran Note:
975c6c1daeSBarry Smith     This routine is not supported in Fortran.
985c6c1daeSBarry Smith 
995c6c1daeSBarry Smith   Concepts: publishing variables
1005c6c1daeSBarry Smith   Concepts: AMS^getting communicator
1015c6c1daeSBarry Smith   Concepts: communicator^accessing AMS communicator
1025c6c1daeSBarry Smith 
1035c6c1daeSBarry Smith .seealso: PetscViewerDestroy(), PetscViewerAMSOpen(), PetscViewer_AMS_, PetscViewer_AMS_WORLD, PetscViewer_AMS_SELF
1045c6c1daeSBarry Smith 
1055c6c1daeSBarry Smith @*/
1065c6c1daeSBarry Smith PetscErrorCode PetscViewerAMSGetAMSComm(PetscViewer v,AMS_Comm *ams_comm)
1075c6c1daeSBarry Smith {
1085c6c1daeSBarry Smith   PetscErrorCode ierr;
1095c6c1daeSBarry Smith 
1105c6c1daeSBarry Smith   PetscFunctionBegin;
1115c6c1daeSBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,1);
1125c6c1daeSBarry Smith   ierr = PetscTryMethod(v,"PetscViewerAMSGetAMSComm_C",(PetscViewer,AMS_Comm*),(v,ams_comm));CHKERRQ(ierr);
1135c6c1daeSBarry Smith   PetscFunctionReturn(0);
1145c6c1daeSBarry Smith }
1155c6c1daeSBarry Smith 
1165c6c1daeSBarry Smith /*
1175c6c1daeSBarry Smith     The variable Petsc_Viewer_Ams_keyval is used to indicate an MPI attribute that
1185c6c1daeSBarry Smith   is attached to a communicator, in this case the attribute is a PetscViewer.
1195c6c1daeSBarry Smith */
1205c6c1daeSBarry Smith static PetscMPIInt Petsc_Viewer_Ams_keyval = MPI_KEYVAL_INVALID;
1215c6c1daeSBarry Smith 
1225c6c1daeSBarry Smith #undef __FUNCT__
1235c6c1daeSBarry Smith #define __FUNCT__ "PETSC_VIEWER_AMS_"
1245c6c1daeSBarry Smith /*@C
1255c6c1daeSBarry Smith      PETSC_VIEWER_AMS_ - Creates an AMS memory snooper PetscViewer shared by all processors
1265c6c1daeSBarry Smith                    in a communicator.
1275c6c1daeSBarry Smith 
1285c6c1daeSBarry Smith      Collective on MPI_Comm
1295c6c1daeSBarry Smith 
1305c6c1daeSBarry Smith      Input Parameters:
1315c6c1daeSBarry Smith .    comm - the MPI communicator to share the PetscViewer
1325c6c1daeSBarry Smith 
1335c6c1daeSBarry Smith      Level: developer
1345c6c1daeSBarry Smith 
1355c6c1daeSBarry Smith      Notes:
1365c6c1daeSBarry Smith      Unlike almost all other PETSc routines, PetscViewer_AMS_ does not return
1375c6c1daeSBarry Smith      an error code.  The window PetscViewer is usually used in the form
1385c6c1daeSBarry Smith $       XXXView(XXX object,PETSC_VIEWER_AMS_(comm));
1395c6c1daeSBarry Smith 
1405c6c1daeSBarry Smith .seealso: PetscViewer_AMS_WORLD, PetscViewer_AMS_SELF, PetscViewerAMSOpen(),
1415c6c1daeSBarry Smith @*/
1425c6c1daeSBarry Smith PetscViewer PETSC_VIEWER_AMS_(MPI_Comm comm)
1435c6c1daeSBarry Smith {
1445c6c1daeSBarry Smith   PetscErrorCode ierr;
1455c6c1daeSBarry Smith   PetscMPIInt    flag;
1465c6c1daeSBarry Smith   PetscViewer    viewer;
1475c6c1daeSBarry Smith   char           name[128];
1485c6c1daeSBarry Smith   MPI_Comm       ncomm;
1495c6c1daeSBarry Smith 
1505c6c1daeSBarry Smith   PetscFunctionBegin;
1510298fd71SBarry 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);}
1525c6c1daeSBarry Smith   if (Petsc_Viewer_Ams_keyval == MPI_KEYVAL_INVALID) {
1535c6c1daeSBarry Smith     ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Ams_keyval,0);
1545c6c1daeSBarry Smith     if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,PETSC_ERROR_INITIAL," "); viewer = 0;}
1555c6c1daeSBarry Smith   }
1565c6c1daeSBarry Smith   ierr = MPI_Attr_get(ncomm,Petsc_Viewer_Ams_keyval,(void**)&viewer,&flag);
1575c6c1daeSBarry Smith   if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,PETSC_ERROR_INITIAL," "); viewer = 0;}
1585c6c1daeSBarry Smith   if (!flag) { /* PetscViewer not yet created */
1595c6c1daeSBarry Smith     ierr = PetscStrcpy(name,"PETSc");
1605c6c1daeSBarry Smith     if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,PETSC_ERROR_INITIAL," "); viewer = 0;}
1615c6c1daeSBarry Smith     ierr = PetscViewerAMSOpen(ncomm,name,&viewer);
1625c6c1daeSBarry Smith     if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,PETSC_ERROR_INITIAL," "); viewer = 0;}
1635c6c1daeSBarry Smith     ierr = PetscObjectRegisterDestroy((PetscObject)viewer);
1645c6c1daeSBarry Smith     if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,PETSC_ERROR_INITIAL," "); viewer = 0;}
1655c6c1daeSBarry Smith     ierr = MPI_Attr_put(ncomm,Petsc_Viewer_Ams_keyval,(void*)viewer);
1665c6c1daeSBarry Smith     if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,PETSC_ERROR_INITIAL," "); viewer = 0;}
1675c6c1daeSBarry Smith   }
1685c6c1daeSBarry Smith   PetscFunctionReturn(viewer);
1695c6c1daeSBarry Smith }
1705c6c1daeSBarry Smith 
1715c6c1daeSBarry Smith /*
1725c6c1daeSBarry Smith        If there is a PetscViewer associated with this communicator, it is destroyed.
1735c6c1daeSBarry Smith */
1745c6c1daeSBarry Smith #undef __FUNCT__
1755c6c1daeSBarry Smith #define __FUNCT__ "PetscViewer_AMS_Destroy"
1765c6c1daeSBarry Smith PetscErrorCode PetscViewer_AMS_Destroy(MPI_Comm comm)
1775c6c1daeSBarry Smith {
1785c6c1daeSBarry Smith   PetscErrorCode ierr;
1795c6c1daeSBarry Smith   PetscMPIInt    flag;
1805c6c1daeSBarry Smith   PetscViewer    viewer;
1815c6c1daeSBarry Smith 
1825c6c1daeSBarry Smith   PetscFunctionBegin;
183a297a907SKarl Rupp   if (Petsc_Viewer_Ams_keyval == MPI_KEYVAL_INVALID) PetscFunctionReturn(0);
184a297a907SKarl Rupp 
1855c6c1daeSBarry Smith   ierr = MPI_Attr_get(comm,Petsc_Viewer_Ams_keyval,(void**)&viewer,&flag);CHKERRQ(ierr);
1865c6c1daeSBarry Smith   if (flag) {
1875c6c1daeSBarry Smith     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
1885c6c1daeSBarry Smith     ierr = MPI_Attr_delete(comm,Petsc_Viewer_Ams_keyval);CHKERRQ(ierr);
1895c6c1daeSBarry Smith   }
1905c6c1daeSBarry Smith   PetscFunctionReturn(0);
1915c6c1daeSBarry Smith }
1925c6c1daeSBarry Smith 
1935c6c1daeSBarry Smith #undef __FUNCT__
1945c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerDestroy_AMS"
1955c6c1daeSBarry Smith static PetscErrorCode PetscViewerDestroy_AMS(PetscViewer viewer)
1965c6c1daeSBarry Smith {
1975c6c1daeSBarry Smith   PetscViewer_AMS *vams = (PetscViewer_AMS*)viewer->data;
1985c6c1daeSBarry Smith   PetscErrorCode  ierr;
1995c6c1daeSBarry Smith 
2005c6c1daeSBarry Smith   PetscFunctionBegin;
2015c6c1daeSBarry Smith   /*
2025c6c1daeSBarry Smith      Make sure that we mark that the stack is no longer published
2035c6c1daeSBarry Smith   */
204ce94432eSBarry Smith   if (PetscObjectComm((PetscObject)viewer) == PETSC_COMM_WORLD) {
2055c6c1daeSBarry Smith     ierr = PetscStackDepublish();CHKERRQ(ierr);
2065c6c1daeSBarry Smith   }
2075c6c1daeSBarry Smith 
2085c6c1daeSBarry Smith   ierr = AMS_Comm_destroy(vams->ams_comm);
2095c6c1daeSBarry Smith   if (ierr) {
2105c6c1daeSBarry Smith     char *err;
2115c6c1daeSBarry Smith     AMS_Explain_error(ierr,&err);
212ce94432eSBarry Smith     SETERRQ(PetscObjectComm((PetscObject)viewer),ierr,err);
2135c6c1daeSBarry Smith   }
2145c6c1daeSBarry Smith   ierr = PetscFree(vams);CHKERRQ(ierr);
2155c6c1daeSBarry Smith   PetscFunctionReturn(0);
2165c6c1daeSBarry Smith }
2175c6c1daeSBarry Smith 
2185c6c1daeSBarry Smith #undef __FUNCT__
2195c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerCreate_AMS"
220*8cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscViewerCreate_AMS(PetscViewer v)
2215c6c1daeSBarry Smith {
2225c6c1daeSBarry Smith   PetscViewer_AMS *vams;
2235c6c1daeSBarry Smith   PetscErrorCode  ierr;
2245c6c1daeSBarry Smith 
2255c6c1daeSBarry Smith   PetscFunctionBegin;
2265c6c1daeSBarry Smith   v->ops->destroy = PetscViewerDestroy_AMS;
2275c6c1daeSBarry Smith   ierr            = PetscNew(PetscViewer_AMS,&vams);CHKERRQ(ierr);
2285c6c1daeSBarry Smith   v->data         = (void*)vams;
2295c6c1daeSBarry Smith   vams->ams_comm  = -1;
230a297a907SKarl Rupp 
23100de8ff0SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerAMSSetCommName_C","PetscViewerAMSSetCommName_AMS",PetscViewerAMSSetCommName_AMS);CHKERRQ(ierr);
23200de8ff0SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerAMSGetAMSComm_C","PetscViewerAMSGetAMSComm_AMS",PetscViewerAMSGetAMSComm_AMS);CHKERRQ(ierr);
2335c6c1daeSBarry Smith   PetscFunctionReturn(0);
2345c6c1daeSBarry Smith }
23599e0435eSBarry Smith 
2365c6c1daeSBarry Smith 
2375c6c1daeSBarry Smith 
238