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