Actual source code: ams.c

petsc-3.4.4 2014-03-13
  2: #include <petsc-private/viewerimpl.h>
  3: #include <petscviewerams.h>
  4: #include <petscsys.h>

  6: typedef struct {
  7:   char     *ams_name;
  8:   AMS_Comm ams_comm;
  9: } PetscViewer_AMS;

 13: PetscErrorCode PetscViewerAMSSetCommName_AMS(PetscViewer v,const char name[])
 14: {
 15:   PetscViewer_AMS *vams = (PetscViewer_AMS*)v->data;
 16:   PetscErrorCode  ierr;
 17:   int             port = -1;
 18:   PetscBool       flg,flg2;
 19:   char            m[64];

 22:   PetscOptionsGetInt(NULL,"-ams_port",&port,NULL);
 23:   PetscInfo1(v,"Publishing with the AMS on port %d\n",port);
 24:   PetscStackCallAMS(AMS_Comm_publish,((char*)name,&vams->ams_comm,MPI_TYPE,PetscObjectComm((PetscObject)v),&port));

 26:   PetscOptionsHasName(NULL,"-ams_printf",&flg);
 27:   if (!flg) {
 28: #if !defined(PETSC_MISSING_DEV_NULL)
 29:     PetscStackCallAMS(AMS_Set_output_file,("/dev/null"));
 30: #endif
 31:   }

 33:   PetscOptionsGetString(NULL,"-ams_matlab",m,16,&flg);
 34:   if (flg) {
 35:     FILE *fp;
 36:     PetscStartMatlab(PetscObjectComm((PetscObject)v),m,"petscview",&fp);
 37:   }

 39:   PetscGetHostName(m,64);
 40:   PetscOptionsHasName(NULL,"-ams_java",&flg);
 41:   if (flg) {
 42:     PetscOptionsGetString(NULL,"-ams_java",m,64,&flg);
 43:     PetscOptionsHasName(NULL,"-options_gui",&flg2);
 44:     if (flg2) {
 45:       char cmd[PETSC_MAX_PATH_LEN];
 46:       PetscStrcpy(cmd,"cd ${PETSC_DIR}/${PETSC_ARCH}/bin;java -d64 -classpath .:");
 47:       PetscStrcat(cmd,PETSC_AMS_DIR);
 48:       PetscStrcat(cmd,"/java -Djava.library.path=");
 49:       PetscStrcat(cmd,PETSC_AMS_DIR);
 50:       PetscStrcat(cmd,"/lib amsoptions -ams_server ${HOSTNAME}");
 51:       PetscPOpen(PetscObjectComm((PetscObject)v),m,cmd,"r",NULL);
 52:     }
 53:   }
 54:   return(0);
 55: }

 59: PetscErrorCode PetscViewerAMSGetAMSComm_AMS(PetscViewer lab,AMS_Comm *ams_comm)
 60: {
 61:   PetscViewer_AMS *vams = (PetscViewer_AMS*)lab->data;

 64:   if (vams->ams_comm == -1) SETERRQ(PetscObjectComm((PetscObject)lab),PETSC_ERR_ARG_WRONGSTATE,"AMS communicator name not yet set with PetscViewerAMSSetCommName()");
 65:   *ams_comm = vams->ams_comm;
 66:   return(0);
 67: }

 71: PetscErrorCode PetscViewerAMSSetCommName(PetscViewer v,const char name[])
 72: {

 77:   PetscTryMethod(v,"PetscViewerAMSSetCommName_C",(PetscViewer,const char[]),(v,name));
 78:   return(0);
 79: }

 83: /*@C
 84:     PetscViewerAMSGetAMSComm - Gets the AMS communicator associated with the PetscViewer.

 86:     Collective on MPI_Comm

 88:     Input Parameters:
 89: .   lab - the PetscViewer

 91:     Output Parameter:
 92: .   ams_comm - the AMS communicator

 94:     Level: developer

 96:     Fortran Note:
 97:     This routine is not supported in Fortran.

 99:   Concepts: publishing variables
100:   Concepts: AMS^getting communicator
101:   Concepts: communicator^accessing AMS communicator

103: .seealso: PetscViewerDestroy(), PetscViewerAMSOpen(), PETSC_VIEWER_AMS_, PETSC_VIEWER_AMS_WORLD, PETSC_VIEWER_AMS_SELF

105: @*/
106: PetscErrorCode PetscViewerAMSGetAMSComm(PetscViewer v,AMS_Comm *ams_comm)
107: {

112:   PetscTryMethod(v,"PetscViewerAMSGetAMSComm_C",(PetscViewer,AMS_Comm*),(v,ams_comm));
113:   return(0);
114: }

116: /*
117:     The variable Petsc_Viewer_Ams_keyval is used to indicate an MPI attribute that
118:   is attached to a communicator, in this case the attribute is a PetscViewer.
119: */
120: static PetscMPIInt Petsc_Viewer_Ams_keyval = MPI_KEYVAL_INVALID;

124: /*@C
125:      PETSC_VIEWER_AMS_ - Creates an AMS memory snooper PetscViewer shared by all processors
126:                    in a communicator.

128:      Collective on MPI_Comm

130:      Input Parameters:
131: .    comm - the MPI communicator to share the PetscViewer

133:      Level: developer

135:      Notes:
136:      Unlike almost all other PETSc routines, PETSC_VIEWER_AMS_() does not return
137:      an error code.  The window PetscViewer is usually used in the form
138: $       XXXView(XXX object,PETSC_VIEWER_AMS_(comm));

140: .seealso: PETSC_VIEWER_AMS_WORLD, PETSC_VIEWER_AMS_SELF
141: @*/
142: PetscViewer PETSC_VIEWER_AMS_(MPI_Comm comm)
143: {
145:   PetscMPIInt    flag;
146:   PetscViewer    viewer;
147:   char           name[128];
148:   MPI_Comm       ncomm;

151:   PetscCommDuplicate(comm,&ncomm,NULL);if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
152:   if (Petsc_Viewer_Ams_keyval == MPI_KEYVAL_INVALID) {
153:     MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Ams_keyval,0);
154:     if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,PETSC_ERROR_INITIAL," "); viewer = 0;}
155:   }
156:   MPI_Attr_get(ncomm,Petsc_Viewer_Ams_keyval,(void**)&viewer,&flag);
157:   if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,PETSC_ERROR_INITIAL," "); viewer = 0;}
158:   if (!flag) { /* PetscViewer not yet created */
159:     PetscStrcpy(name,"PETSc");
160:     if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,PETSC_ERROR_INITIAL," "); viewer = 0;}
161:     PetscViewerAMSOpen(ncomm,name,&viewer);
162:     if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,PETSC_ERROR_INITIAL," "); viewer = 0;}
163:     PetscObjectRegisterDestroy((PetscObject)viewer);
164:     if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,PETSC_ERROR_INITIAL," "); viewer = 0;}
165:     MPI_Attr_put(ncomm,Petsc_Viewer_Ams_keyval,(void*)viewer);
166:     if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,PETSC_ERROR_INITIAL," "); viewer = 0;}
167:   }
168:   PetscCommDestroy(&ncomm);
169:   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
170:   PetscFunctionReturn(viewer);
171: }

173: /*
174:        If there is a PetscViewer associated with this communicator, it is destroyed.
175: */
178: PetscErrorCode PetscViewer_AMS_Destroy(MPI_Comm comm)
179: {
181:   PetscMPIInt    flag;
182:   PetscViewer    viewer;

185:   if (Petsc_Viewer_Ams_keyval == MPI_KEYVAL_INVALID) return(0);

187:   MPI_Attr_get(comm,Petsc_Viewer_Ams_keyval,(void**)&viewer,&flag);
188:   if (flag) {
189:     PetscViewerDestroy(&viewer);
190:     MPI_Attr_delete(comm,Petsc_Viewer_Ams_keyval);
191:   }
192:   return(0);
193: }

197: static PetscErrorCode PetscViewerDestroy_AMS(PetscViewer viewer)
198: {
199:   PetscViewer_AMS *vams = (PetscViewer_AMS*)viewer->data;
200:   PetscErrorCode  ierr;

203:   /*
204:      Make sure that we mark that the stack is no longer published
205:   */
206:   if (PetscObjectComm((PetscObject)viewer) == PETSC_COMM_WORLD) {
207:     PetscStackAMSViewOff();
208:   }

210:   PetscStackCallAMS(AMS_Comm_destroy,(vams->ams_comm));
211:   PetscFree(vams);
212:   return(0);
213: }

217: PETSC_EXTERN PetscErrorCode PetscViewerCreate_AMS(PetscViewer v)
218: {
219:   PetscViewer_AMS *vams;
220:   PetscErrorCode  ierr;

223:   v->ops->destroy = PetscViewerDestroy_AMS;
224:   PetscNew(PetscViewer_AMS,&vams);
225:   v->data         = (void*)vams;
226:   vams->ams_comm  = -1;

228:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerAMSSetCommName_C",PetscViewerAMSSetCommName_AMS);
229:   PetscObjectComposeFunction((PetscObject)v,"PetscViewerAMSGetAMSComm_C",PetscViewerAMSGetAMSComm_AMS);
230:   return(0);
231: }