Actual source code: ams.c
petsc-3.3-p7 2013-05-11
2: #include <petsc-private/viewerimpl.h>
3: #include <petscsys.h>
4: #if defined(PETSC_HAVE_STDLIB_H)
5: #include <stdlib.h>
6: #endif
8: #include <ams.h>
9: typedef struct {
10: char *ams_name;
11: AMS_Comm ams_comm;
12: } PetscViewer_AMS;
14: EXTERN_C_BEGIN
17: PetscErrorCode PetscViewerAMSSetCommName_AMS(PetscViewer v,const char name[])
18: {
19: PetscViewer_AMS *vams = (PetscViewer_AMS*)v->data;
20: PetscErrorCode ierr;
21: int port = -1;
22: PetscBool flg,flg2;
23: char m[64];
26: PetscOptionsGetInt(PETSC_NULL,"-ams_port",&port,PETSC_NULL);
27: PetscInfo1(v,"Publishing with the AMS on port %d\n",port);
28: AMS_Comm_publish((char *)name,&vams->ams_comm,MPI_TYPE,((PetscObject)v)->comm,&port);
30: PetscOptionsHasName(PETSC_NULL,"-ams_printf",&flg);
31: if (!flg) {
32: #if !defined(PETSC_MISSING_DEV_NULL)
33: AMS_Set_output_file("/dev/null");
34: #endif
35: }
37: PetscOptionsGetString(PETSC_NULL,"-ams_matlab",m,16,&flg);
38: if (flg) {
39: FILE *fp;
40: PetscStartMatlab(((PetscObject)v)->comm,m,"petscview",&fp);
41: }
43: PetscGetHostName(m,64);
44: PetscOptionsHasName(PETSC_NULL,"-ams_java",&flg);
45: if (flg) {
46: PetscOptionsGetString(PETSC_NULL,"-ams_java",m,64,&flg);
47: PetscOptionsHasName(PETSC_NULL,"-options_gui",&flg2);
48: if (flg2) {
49: char cmd[PETSC_MAX_PATH_LEN];
50: PetscStrcpy(cmd,"cd ${PETSC_DIR}/${PETSC_ARCH}/bin;java -d64 -classpath .:");
51: PetscStrcat(cmd,PETSC_AMS_DIR);
52: PetscStrcat(cmd,"/java -Djava.library.path=");
53: PetscStrcat(cmd,PETSC_AMS_DIR);
54: PetscStrcat(cmd,"/lib amsoptions -ams_server ${HOSTNAME}");
55: PetscPOpen(((PetscObject)v)->comm,m,cmd,"r",PETSC_NULL);
56: }
57: }
58: return(0);
59: }
60: EXTERN_C_END
62: EXTERN_C_BEGIN
65: PetscErrorCode PetscViewerAMSGetAMSComm_AMS(PetscViewer lab,AMS_Comm *ams_comm)
66: {
67: PetscViewer_AMS *vams = (PetscViewer_AMS *)lab->data;
70: if (vams->ams_comm == -1) SETERRQ(((PetscObject)lab)->comm,PETSC_ERR_ARG_WRONGSTATE,"AMS communicator name not yet set with PetscViewerAMSSetCommName()");
71: *ams_comm = vams->ams_comm;
72: return(0);
73: }
74: EXTERN_C_END
78: PetscErrorCode PetscViewerAMSSetCommName(PetscViewer v,const char name[])
79: {
84: PetscTryMethod(v,"PetscViewerAMSSetCommName_C",(PetscViewer,const char[]),(v,name));
85: return(0);
86: }
90: /*@C
91: PetscViewerAMSGetAMSComm - Gets the AMS communicator associated with the PetscViewer.
93: Collective on MPI_Comm
95: Input Parameters:
96: . lab - the PetscViewer
98: Output Parameter:
99: . ams_comm - the AMS communicator
101: Level: developer
103: Fortran Note:
104: This routine is not supported in Fortran.
106: Concepts: publishing variables
107: Concepts: AMS^getting communicator
108: Concepts: communicator^accessing AMS communicator
110: .seealso: PetscViewerDestroy(), PetscViewerAMSOpen(), PetscViewer_AMS_, PetscViewer_AMS_WORLD, PetscViewer_AMS_SELF
112: @*/
113: PetscErrorCode PetscViewerAMSGetAMSComm(PetscViewer v,AMS_Comm *ams_comm)
114: {
119: PetscTryMethod(v,"PetscViewerAMSGetAMSComm_C",(PetscViewer,AMS_Comm *),(v,ams_comm));
120: return(0);
121: }
123: /*
124: The variable Petsc_Viewer_Ams_keyval is used to indicate an MPI attribute that
125: is attached to a communicator, in this case the attribute is a PetscViewer.
126: */
127: static PetscMPIInt Petsc_Viewer_Ams_keyval = MPI_KEYVAL_INVALID;
131: /*@C
132: PETSC_VIEWER_AMS_ - Creates an AMS memory snooper PetscViewer shared by all processors
133: in a communicator.
135: Collective on MPI_Comm
137: Input Parameters:
138: . comm - the MPI communicator to share the PetscViewer
140: Level: developer
142: Notes:
143: Unlike almost all other PETSc routines, PetscViewer_AMS_ does not return
144: an error code. The window PetscViewer is usually used in the form
145: $ XXXView(XXX object,PETSC_VIEWER_AMS_(comm));
147: .seealso: PetscViewer_AMS_WORLD, PetscViewer_AMS_SELF, PetscViewerAMSOpen(),
148: @*/
149: PetscViewer PETSC_VIEWER_AMS_(MPI_Comm comm)
150: {
152: PetscMPIInt flag;
153: PetscViewer viewer;
154: char name[128];
155: MPI_Comm ncomm;
158: PetscCommDuplicate(comm,&ncomm,PETSC_NULL);if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");return(0);}
159: if (Petsc_Viewer_Ams_keyval == MPI_KEYVAL_INVALID) {
160: MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Ams_keyval,0);
161: if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,PETSC_ERROR_INITIAL," "); viewer = 0;}
162: }
163: MPI_Attr_get(ncomm,Petsc_Viewer_Ams_keyval,(void **)&viewer,&flag);
164: if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,PETSC_ERROR_INITIAL," "); viewer = 0;}
165: if (!flag) { /* PetscViewer not yet created */
166: PetscStrcpy(name,"PETSc");
167: if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,PETSC_ERROR_INITIAL," "); viewer = 0;}
168: PetscViewerAMSOpen(ncomm,name,&viewer);
169: if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,PETSC_ERROR_INITIAL," "); viewer = 0;}
170: PetscObjectRegisterDestroy((PetscObject)viewer);
171: if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,PETSC_ERROR_INITIAL," "); viewer = 0;}
172: MPI_Attr_put(ncomm,Petsc_Viewer_Ams_keyval,(void*)viewer);
173: if (ierr) {PetscError(ncomm,__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,PETSC_ERROR_INITIAL," "); viewer = 0;}
174: }
175: PetscFunctionReturn(viewer);
176: }
178: /*
179: If there is a PetscViewer associated with this communicator, it is destroyed.
180: */
183: PetscErrorCode PetscViewer_AMS_Destroy(MPI_Comm comm)
184: {
186: PetscMPIInt flag;
187: PetscViewer viewer;
190: if (Petsc_Viewer_Ams_keyval == MPI_KEYVAL_INVALID) {
191: return(0);
192: }
193: MPI_Attr_get(comm,Petsc_Viewer_Ams_keyval,(void **)&viewer,&flag);
194: if (flag) {
195: PetscViewerDestroy(&viewer);
196: MPI_Attr_delete(comm,Petsc_Viewer_Ams_keyval);
197: }
198: return(0);
199: }
203: static PetscErrorCode PetscViewerDestroy_AMS(PetscViewer viewer)
204: {
205: PetscViewer_AMS *vams = (PetscViewer_AMS*)viewer->data;
206: PetscErrorCode ierr;
209: /*
210: Make sure that we mark that the stack is no longer published
211: */
212: if (((PetscObject)viewer)->comm == PETSC_COMM_WORLD) {
213: PetscStackDepublish();
214: }
216: AMS_Comm_destroy(vams->ams_comm);
217: if (ierr) {
218: char *err;
219: AMS_Explain_error(ierr,&err);
220: SETERRQ(((PetscObject)viewer)->comm,ierr,err);
221: }
222: PetscFree(vams);
223: return(0);
224: }
226: EXTERN_C_BEGIN
229: PetscErrorCode PetscViewerCreate_AMS(PetscViewer v)
230: {
231: PetscViewer_AMS *vams;
232: PetscErrorCode ierr;
235: v->ops->destroy = PetscViewerDestroy_AMS;
236: PetscNew(PetscViewer_AMS,&vams);
237: v->data = (void*)vams;
238: vams->ams_comm = -1;
239: PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerAMSSetCommName_C",
240: "PetscViewerAMSSetCommName_AMS",
241: PetscViewerAMSSetCommName_AMS);
242: PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerAMSGetAMSComm_C",
243: "PetscViewerAMSGetAMSComm_AMS",
244: PetscViewerAMSGetAMSComm_AMS);
245: return(0);
246: }
247: EXTERN_C_END