Actual source code: ams.c
1: /*$Id: ams.c,v 1.47 2001/04/10 19:34:07 bsmith Exp $*/
3: #include src/sys/src/viewer/viewerimpl.h
4: #include petscsys.h
5: #if defined(PETSC_HAVE_STDLIB_H)
6: #include <stdlib.h>
7: #endif
9: #include "ams.h"
10: typedef struct {
11: char *ams_name;
12: AMS_Comm ams_comm;
13: } PetscViewer_AMS;
15: EXTERN_C_BEGIN
18: int PetscViewerAMSSetCommName_AMS(PetscViewer v,const char name[])
19: {
20: PetscViewer_AMS *vams = (PetscViewer_AMS*)v->data;
21: int ierr,port = -1;
22: PetscTruth flg,flg2;
23: char m[16],*pdir;
26: PetscOptionsGetInt(PETSC_NULL,"-ams_port",&port,PETSC_NULL);
27: PetscLogInfo(v,"Publishing with the AMS on port %d\n",port);
28: AMS_Comm_publish((char *)name,&vams->ams_comm,MPI_TYPE,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(v->comm,m,"petscview",&fp);
41: }
43: PetscOptionsGetString(PETSC_NULL,"-ams_java",m,16,&flg);
44: if (flg) {
45: PetscOptionsHasName(PETSC_NULL,"-ams_publish_options",&flg2);
46: if (flg2) {
47: char cmd[PETSC_MAX_PATH_LEN];
48: PetscStrcpy(cmd,"cd ");
49: PetscGetPetscDir(&pdir);
50: PetscStrcat(cmd,pdir);
51: PetscStrcat(cmd,"/src/sys/src/objects/ams/java;make runamsoptions AMS_OPTIONS=\"-ams_server ${HOSTNAME}\"");
52: PetscPOpen(v->comm,m,cmd,"r",PETSC_NULL);
53: }
55: PetscOptionsHasName(PETSC_NULL,"-ams_publish_objects",&flg2);
56: if (flg2) {
57: char dir[PETSC_MAX_PATH_LEN];
58: #if defined(PETSC_HAVE_UCBPS)
59: char buf[PETSC_MAX_PATH_LEN],*found;
60: FILE *fp;
62: /* check if jacc is not already running */
63: PetscPOpen(v->comm,m,"/usr/ucb/ps -ugxww | grep jacc | grep -v grep","r",&fp);
64: found = fgets(buf,1024,fp);
65: PetscFClose(v->comm,fp);
66: if (found) return(0);
67: #endif
68: PetscOptionsGetenv(v->comm,"AMS_HOME",dir,PETSC_MAX_PATH_LEN,&flg);
69: if (!flg) {
70: PetscStrncpy(dir,AMS_HOME,PETSC_MAX_PATH_LEN);
71: }
72: /* PetscStrcat(dir,"/java/client/jacc -display ${DISPLAY}"); */
73: PetscStrcat(dir,"/java/client/jacc");
74: PetscPOpen(v->comm,m,dir,"r",PETSC_NULL);
75: }
76: }
77: return(0);
78: }
79: EXTERN_C_END
81: EXTERN_C_BEGIN
84: int PetscViewerAMSGetAMSComm_AMS(PetscViewer lab,AMS_Comm *ams_comm)
85: {
86: PetscViewer_AMS *vams = (PetscViewer_AMS *)lab->data;
89: if (vams->ams_comm == -1) SETERRQ(1,"AMS communicator name not yet set with PetscViewerAMSSetCommName()");
90: *ams_comm = vams->ams_comm;
91: return(0);
92: }
93: EXTERN_C_END
97: int PetscViewerAMSSetCommName(PetscViewer v,const char name[])
98: {
99: int ierr,(*f)(PetscViewer,const char[]);
104: PetscObjectQueryFunction((PetscObject)v,"PetscViewerAMSSetCommName_C",(void (**)(void))&f);
105: if (f) {
106: (*f)(v,name);
107: }
108: return(0);
109: }
113: /*@C
114: PetscViewerAMSGetAMSComm - Gets the AMS communicator associated with the PetscViewer.
116: Collective on MPI_Comm
118: Input Parameters:
119: . lab - the PetscViewer
121: Output Parameter:
122: . ams_comm - the AMS communicator
124: Level: developer
126: Fortran Note:
127: This routine is not supported in Fortran.
129: Concepts: publishing variables
130: Concepts: AMS^getting communicator
131: Concepts: communicator^accessing AMS communicator
133: .seealso: PetscViewerDestroy(), PetscViewerAMSOpen(), PetscViewer_AMS_, PetscViewer_AMS_WORLD, PetscViewer_AMS_SELF
135: @*/
136: int PetscViewerAMSGetAMSComm(PetscViewer v,AMS_Comm *ams_comm)
137: {
138: int ierr,(*f)(PetscViewer,AMS_Comm *);
142: PetscObjectQueryFunction((PetscObject)v,"PetscViewerAMSGetAMSComm_C",(void (**)(void))&f);
143: if (f) {
144: (*f)(v,ams_comm);
145: }
146: return(0);
147: }
149: /*
150: The variable Petsc_Viewer_Ams_keyval is used to indicate an MPI attribute that
151: is attached to a communicator, in this case the attribute is a PetscViewer.
152: */
153: static int Petsc_Viewer_Ams_keyval = MPI_KEYVAL_INVALID;
157: /*@C
158: PetscViewer_AMS_ - Creates an AMS memory snooper PetscViewer shared by all processors
159: in a communicator.
161: Collective on MPI_Comm
163: Input Parameters:
164: . comm - the MPI communicator to share the PetscViewer
166: Level: developer
168: Notes:
169: Unlike almost all other PETSc routines, PetscViewer_AMS_ does not return
170: an error code. The window PetscViewer is usually used in the form
171: $ XXXView(XXX object,PETSC_VIEWER_AMS_(comm));
173: .seealso: PetscViewer_AMS_WORLD, PetscViewer_AMS_SELF, PetscViewerAMSOpen(),
174: @*/
175: PetscViewer PETSC_VIEWER_AMS_(MPI_Comm comm)
176: {
177: int ierr,flag,size,rank;
178: PetscViewer viewer;
179: char name[128];
182: if (Petsc_Viewer_Ams_keyval == MPI_KEYVAL_INVALID) {
183: MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Ams_keyval,0);
184: if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,1," "); viewer = 0;}
185: }
186: MPI_Attr_get(comm,Petsc_Viewer_Ams_keyval,(void **)&viewer,&flag);
187: if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,1," "); viewer = 0;}
188: if (!flag) { /* PetscViewer not yet created */
189: if (comm == PETSC_COMM_WORLD) {
190: PetscStrcpy(name,"PETSc");
191: if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,1," "); viewer = 0;}
192: } else {
193: MPI_Comm_size(comm,&size);
194: if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,1," "); viewer = 0;}
195: if (size == 1) {
196: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);if (ierr) return(0);
197: sprintf(name,"PETSc_%d",rank);
198: } else {
199: PetscError(__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,1," "); viewer = 0;
200: }
201: }
202: PetscViewerAMSOpen(comm,name,&viewer);
203: if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,1," "); viewer = 0;}
204: PetscObjectRegisterDestroy((PetscObject)viewer);
205: if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDOUT_",__FILE__,__SDIR__,1,1," "); viewer = 0;}
206: MPI_Attr_put(comm,Petsc_Viewer_Ams_keyval,(void*)viewer);
207: if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,1," "); viewer = 0;}
208: }
209: PetscFunctionReturn(viewer);
210: }
212: /*
213: If there is a PetscViewer associated with this communicator, it is destroyed.
214: */
217: int PetscViewer_AMS_Destroy(MPI_Comm comm)
218: {
219: int ierr,flag;
220: PetscViewer viewer;
223: if (Petsc_Viewer_Ams_keyval == MPI_KEYVAL_INVALID) {
224: return(0);
225: }
226: MPI_Attr_get(comm,Petsc_Viewer_Ams_keyval,(void **)&viewer,&flag);
227: if (flag) {
228: PetscViewerDestroy(viewer);
229: MPI_Attr_delete(comm,Petsc_Viewer_Ams_keyval);
230: }
231: return(0);
232: }
236: static int PetscViewerDestroy_AMS(PetscViewer viewer)
237: {
238: PetscViewer_AMS *vams = (PetscViewer_AMS*)viewer->data;
239: int ierr;
243: /*
244: Make sure that we mark that the stack is no longer published
245: */
246: if (viewer->comm == PETSC_COMM_WORLD) {
247: PetscStackDepublish();
248: }
250: AMS_Comm_destroy(vams->ams_comm);
251: if (ierr) {
252: char *err;
253: AMS_Explain_error(ierr,&err);
254: SETERRQ(ierr,err);
255: }
256: PetscFree(vams);
257: return(0);
258: }
260: EXTERN_C_BEGIN
263: int PetscViewerCreate_AMS(PetscViewer v)
264: {
265: PetscViewer_AMS *vams;
266: int ierr;
269: v->ops->destroy = PetscViewerDestroy_AMS;
270: PetscStrallocpy(PETSC_VIEWER_AMS,&v->type_name);
271: PetscNew(PetscViewer_AMS,&vams);
272: v->data = (void*)vams;
273: vams->ams_comm = -1;
274: PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerAMSSetCommName_C",
275: "PetscViewerAMSSetCommName_AMS",
276: PetscViewerAMSSetCommName_AMS);
277: PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerAMSGetAMSComm_C",
278: "PetscViewerAMSGetAMSComm_AMS",
279: PetscViewerAMSGetAMSComm_AMS);
280: return(0);
281: }
282: EXTERN_C_END