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
16: #undef __FUNCT__
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 %dn",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: ierr = PetscPOpen(v->comm,m,"/usr/ucb/ps -ugxww | grep jacc | grep -v grep","r",&fp);
64: found = fgets(buf,1024,fp);
65: ierr = 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
82: #undef __FUNCT__
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
95: #undef __FUNCT__
97: int PetscViewerAMSSetCommName(PetscViewer v,const char name[])
98: {
99: int ierr,(*f)(PetscViewer,const char[]);
103: PetscObjectQueryFunction((PetscObject)v,"PetscViewerAMSSetCommName_C",(void (**)(void))&f);
104: if (f) {
105: (*f)(v,name);
106: }
107: return(0);
108: }
110: #undef __FUNCT__
112: /*@C
113: PetscViewerAMSGetAMSComm - Gets the AMS communicator associated with the PetscViewer.
115: Collective on MPI_Comm
117: Input Parameters:
118: . lab - the PetscViewer
120: Output Parameter:
121: . ams_comm - the AMS communicator
123: Level: developer
125: Fortran Note:
126: This routine is not supported in Fortran.
128: Concepts: publishing variables
129: Concepts: AMS^getting communicator
130: Concepts: communicator^accessing AMS communicator
132: .seealso: PetscViewerDestroy(), PetscViewerAMSOpen(), PetscViewer_AMS_, PetscViewer_AMS_WORLD, PetscViewer_AMS_SELF
134: @*/
135: int PetscViewerAMSGetAMSComm(PetscViewer v,AMS_Comm *ams_comm)
136: {
137: int ierr,(*f)(PetscViewer,AMS_Comm *);
141: PetscObjectQueryFunction((PetscObject)v,"PetscViewerAMSGetAMSComm_C",(void (**)(void))&f);
142: if (f) {
143: (*f)(v,ams_comm);
144: }
145: return(0);
146: }
148: /*
149: The variable Petsc_Viewer_Ams_keyval is used to indicate an MPI attribute that
150: is attached to a communicator, in this case the attribute is a PetscViewer.
151: */
152: static int Petsc_Viewer_Ams_keyval = MPI_KEYVAL_INVALID;
154: #undef __FUNCT__
156: /*@C
157: PetscViewer_AMS_ - Creates an AMS memory snooper PetscViewer shared by all processors
158: in a communicator.
160: Collective on MPI_Comm
162: Input Parameters:
163: . comm - the MPI communicator to share the PetscViewer
165: Level: developer
167: Notes:
168: Unlike almost all other PETSc routines, PetscViewer_AMS_ does not return
169: an error code. The window PetscViewer is usually used in the form
170: $ XXXView(XXX object,PETSC_VIEWER_AMS_(comm));
172: .seealso: PetscViewer_AMS_WORLD, PetscViewer_AMS_SELF, PetscViewerAMSOpen(),
173: @*/
174: PetscViewer PETSC_VIEWER_AMS_(MPI_Comm comm)
175: {
176: int ierr,flag,size,rank;
177: PetscViewer viewer;
178: char name[128];
181: if (Petsc_Viewer_Ams_keyval == MPI_KEYVAL_INVALID) {
182: MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Ams_keyval,0);
183: if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,1," "); viewer = 0;}
184: }
185: MPI_Attr_get(comm,Petsc_Viewer_Ams_keyval,(void **)&viewer,&flag);
186: if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,1," "); viewer = 0;}
187: if (!flag) { /* PetscViewer not yet created */
188: if (comm == PETSC_COMM_WORLD) {
189: PetscStrcpy(name,"PETSc");
190: if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,1," "); viewer = 0;}
191: } else {
192: MPI_Comm_size(comm,&size);
193: if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,1," "); viewer = 0;}
194: if (size == 1) {
195: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);if (ierr) return(0);
196: sprintf(name,"PETSc_%d",rank);
197: } else {
198: PetscError(__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,1," "); viewer = 0;
199: }
200: }
201: PetscViewerAMSOpen(comm,name,&viewer);
202: if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,1," "); viewer = 0;}
203: PetscObjectRegisterDestroy((PetscObject)viewer);
204: if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_STDOUT_",__FILE__,__SDIR__,1,1," "); viewer = 0;}
205: MPI_Attr_put(comm,Petsc_Viewer_Ams_keyval,(void*)viewer);
206: if (ierr) {PetscError(__LINE__,"PETSC_VIEWER_AMS_",__FILE__,__SDIR__,1,1," "); viewer = 0;}
207: }
208: PetscFunctionReturn(viewer);
209: }
211: /*
212: If there is a PetscViewer associated with this communicator, it is destroyed.
213: */
214: #undef __FUNCT__
216: int PetscViewer_AMS_Destroy(MPI_Comm comm)
217: {
218: int ierr,flag;
219: PetscViewer viewer;
222: if (Petsc_Viewer_Ams_keyval == MPI_KEYVAL_INVALID) {
223: return(0);
224: }
225: MPI_Attr_get(comm,Petsc_Viewer_Ams_keyval,(void **)&viewer,&flag);
226: if (flag) {
227: PetscViewerDestroy(viewer);
228: MPI_Attr_delete(comm,Petsc_Viewer_Ams_keyval);
229: }
230: return(0);
231: }
233: #undef __FUNCT__
235: static int PetscViewerDestroy_AMS(PetscViewer viewer)
236: {
237: PetscViewer_AMS *vams = (PetscViewer_AMS*)viewer->data;
238: int ierr;
242: /*
243: Make sure that we mark that the stack is no longer published
244: */
245: if (viewer->comm == PETSC_COMM_WORLD) {
246: PetscStackDepublish();
247: }
249: AMS_Comm_destroy(vams->ams_comm);
250: if (ierr) {
251: char *err;
252: AMS_Explain_error(ierr,&err);
253: SETERRQ(ierr,err);
254: }
255: PetscFree(vams);
256: return(0);
257: }
259: EXTERN_C_BEGIN
260: #undef __FUNCT__
262: int PetscViewerCreate_AMS(PetscViewer v)
263: {
264: PetscViewer_AMS *vams;
265: int ierr;
268: v->ops->destroy = PetscViewerDestroy_AMS;
269: ierr = PetscStrallocpy(PETSC_VIEWER_AMS,&v->type_name);
270: ierr = PetscNew(PetscViewer_AMS,&vams);
271: v->data = (void*)vams;
272: vams->ams_comm = -1;
273: PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerAMSSetCommName_C",
274: "PetscViewerAMSSetCommName_AMS",
275: PetscViewerAMSSetCommName_AMS);
276: PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerAMSGetAMSComm_C",
277: "PetscViewerAMSGetAMSComm_AMS",
278: PetscViewerAMSGetAMSComm_AMS);
279: return(0);
280: }
281: EXTERN_C_END