Actual source code: viewreg.c
petsc-3.7.0 2016-04-25
2: #include <petsc/private/viewerimpl.h> /*I "petscviewer.h" I*/
3: #if defined(PETSC_HAVE_SAWS)
4: #include <petscviewersaws.h>
5: #endif
7: PetscFunctionList PetscViewerList = 0;
11: /*@C
12: PetscOptionsGetViewer - Gets a viewer appropriate for the type indicated by the user
14: Collective on MPI_Comm
16: Input Parameters:
17: + comm - the communicator to own the viewer
18: . pre - the string to prepend to the name or NULL
19: - name - the option one is seeking
21: Output Parameter:
22: + viewer - the viewer, pass NULL if not needed
23: . format - the PetscViewerFormat requested by the user, pass NULL if not needed
24: - set - PETSC_TRUE if found, else PETSC_FALSE
26: Level: intermediate
28: Notes: If no value is provided ascii:stdout is used
29: $ ascii[:[filename][:[format][:append]]] defaults to stdout - format can be one of ascii_info, ascii_info_detail, or ascii_matlab,
30: for example ascii::ascii_info prints just the information about the object not all details
31: unless :append is given filename opens in write mode, overwriting what was already there
32: $ binary[:[filename][:[format][:append]]] defaults to the file binaryoutput
33: $ draw[:drawtype] for example, draw:tikz or draw:x
34: $ socket[:port] defaults to the standard output port
35: $ saws[:communicatorname] publishes object to the Scientific Application Webserver (SAWs)
37: Use PetscViewerDestroy() after using the viewer, otherwise a memory leak will occur
39: If PETSc is configured with --with-viewfromoptions=0 this function always returns with *set of PETSC_FALSE
41: .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
42: PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
43: PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
44: PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
45: PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
46: PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
47: PetscOptionsFList(), PetscOptionsEList()
48: @*/
49: PetscErrorCode PetscOptionsGetViewer(MPI_Comm comm,const char pre[],const char name[],PetscViewer *viewer,PetscViewerFormat *format,PetscBool *set)
50: {
51: char *value;
53: PetscBool flag,hashelp;
58: if (set) *set = PETSC_FALSE;
59: #if defined(PETSC_SKIP_VIEWFROMOPTIONS)
60: return(0);
61: #endif
63: PetscOptionsHasName(NULL,NULL,"-help",&hashelp);
64: if (hashelp) {
65: (*PetscHelpPrintf)(comm," -%s%s ascii[:[filename][:[format][:append]]]: %s (%s)\n",pre ? pre : "",name+1,"Triggers display of a PETSc object to screen or ASCII file","PetscOptionsGetViewer");
66: (*PetscHelpPrintf)(comm," -%s%s binary[:[filename][:[format][:append]]]: %s (%s)\n",pre ? pre : "",name+1,"Triggers saving of a PETSc object to a binary file","PetscOptionsGetViewer");
67: (*PetscHelpPrintf)(comm," -%s%s draw[:drawtype]: %s (%s)\n",pre ? pre : "",name+1,"Triggers drawing of a PETSc object","PetscOptionsGetViewer");
68: (*PetscHelpPrintf)(comm," -%s%s socket[:port]: %s (%s)\n",pre ? pre : "",name+1,"Triggers push of a PETSc object to a Unix socket","PetscOptionsGetViewer");
69: (*PetscHelpPrintf)(comm," -%s%s saws[:communicatorname]: %s (%s)\n",pre ? pre : "",name+1,"Triggers publishing of a PETSc object to SAWs","PetscOptionsGetViewer");
70: }
72: if (format) *format = PETSC_VIEWER_DEFAULT;
73: PetscOptionsFindPair_Private(NULL,pre,name,&value,&flag);
74: if (flag) {
75: if (set) *set = PETSC_TRUE;
76: if (!value) {
77: if (viewer) {
78: PetscViewerASCIIGetStdout(comm,viewer);
79: PetscObjectReference((PetscObject)*viewer);
80: }
81: } else {
82: char *loc0_vtype,*loc1_fname,*loc2_fmt = NULL,*loc3_fmode = NULL;
83: PetscInt cnt;
84: const char *viewers[] = {PETSCVIEWERASCII,PETSCVIEWERBINARY,PETSCVIEWERDRAW,PETSCVIEWERSOCKET,PETSCVIEWERMATLAB,PETSCVIEWERSAWS,PETSCVIEWERVTK,PETSCVIEWERHDF5,0};
86: PetscStrallocpy(value,&loc0_vtype);
87: PetscStrchr(loc0_vtype,':',&loc1_fname);
88: if (loc1_fname) {
89: *loc1_fname++ = 0;
90: PetscStrchr(loc1_fname,':',&loc2_fmt);
91: }
92: if (loc2_fmt) {
93: *loc2_fmt++ = 0;
94: PetscStrchr(loc2_fmt,':',&loc3_fmode);
95: }
96: if (loc3_fmode) *loc3_fmode++ = 0;
97: PetscStrendswithwhich(*loc0_vtype ? loc0_vtype : "ascii",viewers,&cnt);
98: if (cnt > (PetscInt) sizeof(viewers)-1) SETERRQ1(comm,PETSC_ERR_ARG_OUTOFRANGE,"Unknown viewer type: %s",loc0_vtype);
99: if (viewer) {
100: if (!loc1_fname) {
101: switch (cnt) {
102: case 0:
103: PetscViewerASCIIGetStdout(comm,viewer);
104: break;
105: case 1:
106: if (!(*viewer = PETSC_VIEWER_BINARY_(comm))) CHKERRQ(PETSC_ERR_PLIB);
107: break;
108: case 2:
109: if (!(*viewer = PETSC_VIEWER_DRAW_(comm))) CHKERRQ(PETSC_ERR_PLIB);
110: break;
111: #if defined(PETSC_USE_SOCKET_VIEWER)
112: case 3:
113: if (!(*viewer = PETSC_VIEWER_SOCKET_(comm))) CHKERRQ(PETSC_ERR_PLIB);
114: break;
115: #endif
116: #if defined(PETSC_HAVE_MATLAB_ENGINE)
117: case 4:
118: if (!(*viewer = PETSC_VIEWER_MATLAB_(comm))) CHKERRQ(PETSC_ERR_PLIB);
119: break;
120: #endif
121: #if defined(PETSC_HAVE_SAWS)
122: case 5:
123: if (!(*viewer = PETSC_VIEWER_SAWS_(comm))) CHKERRQ(PETSC_ERR_PLIB);
124: break;
125: #endif
126: #if defined(PETSC_HAVE_HDF5)
127: case 7:
128: if (!(*viewer = PETSC_VIEWER_HDF5_(comm))) CHKERRQ(PETSC_ERR_PLIB);
129: break;
130: #endif
131: default: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unsupported viewer %s",loc0_vtype);
132: }
133: PetscObjectReference((PetscObject)*viewer);
134: } else {
135: if (loc2_fmt && !*loc1_fname && (cnt == 0)) { /* ASCII format without file name */
136: PetscViewerASCIIGetStdout(comm,viewer);
137: PetscObjectReference((PetscObject)*viewer);
138: } else {
139: PetscFileMode fmode;
140: PetscViewerCreate(comm,viewer);
141: PetscViewerSetType(*viewer,*loc0_vtype ? loc0_vtype : "ascii");
142: fmode = FILE_MODE_WRITE;
143: if (loc3_fmode && *loc3_fmode) { /* Has non-empty file mode ("write" or "append") */
144: PetscEnumFind(PetscFileModes,loc3_fmode,(PetscEnum*)&fmode,&flag);
145: if (!flag) SETERRQ1(comm,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown file mode: %s",loc3_fmode);
146: }
147: PetscViewerFileSetMode(*viewer,flag?fmode:FILE_MODE_WRITE);
148: PetscViewerFileSetName(*viewer,loc1_fname);
149: PetscViewerDrawSetDrawType(*viewer,loc1_fname);
150: }
151: }
152: }
153: if (viewer) {
154: PetscViewerSetUp(*viewer);
155: }
156: if (loc2_fmt && *loc2_fmt) {
157: PetscEnumFind(PetscViewerFormats,loc2_fmt,(PetscEnum*)format,&flag);
158: if (!flag) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unknown viewer format %s",loc2_fmt);
159: }
160: PetscFree(loc0_vtype);
161: }
162: }
163: return(0);
164: }
168: /*@
169: PetscViewerCreate - Creates a viewing context
171: Collective on MPI_Comm
173: Input Parameter:
174: . comm - MPI communicator
176: Output Parameter:
177: . inviewer - location to put the PetscViewer context
179: Level: advanced
181: Concepts: graphics^creating PetscViewer
182: Concepts: file input/output^creating PetscViewer
183: Concepts: sockets^creating PetscViewer
185: .seealso: PetscViewerDestroy(), PetscViewerSetType(), PetscViewerType
187: @*/
188: PetscErrorCode PetscViewerCreate(MPI_Comm comm,PetscViewer *inviewer)
189: {
190: PetscViewer viewer;
194: *inviewer = 0;
195: PetscViewerInitializePackage();
196: PetscHeaderCreate(viewer,PETSC_VIEWER_CLASSID,"PetscViewer","PetscViewer","Viewer",comm,PetscViewerDestroy,NULL);
197: *inviewer = viewer;
198: viewer->data = 0;
199: return(0);
200: }
204: /*@C
205: PetscViewerSetType - Builds PetscViewer for a particular implementation.
207: Collective on PetscViewer
209: Input Parameter:
210: + viewer - the PetscViewer context
211: - type - for example, PETSCVIEWERASCII
213: Options Database Command:
214: . -draw_type <type> - Sets the type; use -help for a list
215: of available methods (for instance, ascii)
217: Level: advanced
219: Notes:
220: See "include/petscviewer.h" for available methods (for instance,
221: PETSCVIEWERSOCKET)
223: .seealso: PetscViewerCreate(), PetscViewerGetType(), PetscViewerType, PetscViewerPushFormat()
224: @*/
225: PetscErrorCode PetscViewerSetType(PetscViewer viewer,PetscViewerType type)
226: {
227: PetscErrorCode ierr,(*r)(PetscViewer);
228: PetscBool match;
233: PetscObjectTypeCompare((PetscObject)viewer,type,&match);
234: if (match) return(0);
236: /* cleanup any old type that may be there */
237: if (viewer->data) {
238: (*viewer->ops->destroy)(viewer);
240: viewer->ops->destroy = NULL;
241: viewer->data = 0;
242: }
243: PetscMemzero(viewer->ops,sizeof(struct _PetscViewerOps));
245: PetscFunctionListFind(PetscViewerList,type,&r);
246: if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown PetscViewer type given: %s",type);
248: PetscObjectChangeTypeName((PetscObject)viewer,type);
249: (*r)(viewer);
250: return(0);
251: }
255: /*@C
256: PetscViewerRegister - Adds a viewer
258: Not Collective
260: Input Parameters:
261: + name_solver - name of a new user-defined viewer
262: - routine_create - routine to create method context
264: Level: developer
265: Notes:
266: PetscViewerRegister() may be called multiple times to add several user-defined viewers.
268: Sample usage:
269: .vb
270: PetscViewerRegister("my_viewer_type",MyViewerCreate);
271: .ve
273: Then, your solver can be chosen with the procedural interface via
274: $ PetscViewerSetType(viewer,"my_viewer_type")
275: or at runtime via the option
276: $ -viewer_type my_viewer_type
278: Concepts: registering^Viewers
280: .seealso: PetscViewerRegisterAll(), PetscViewerRegisterDestroy()
281: @*/
282: PetscErrorCode PetscViewerRegister(const char *sname,PetscErrorCode (*function)(PetscViewer))
283: {
287: PetscFunctionListAdd(&PetscViewerList,sname,function);
288: return(0);
289: }
293: /*@C
294: PetscViewerSetFromOptions - Sets the graphics type from the options database.
295: Defaults to a PETSc X windows graphics.
297: Collective on PetscViewer
299: Input Parameter:
300: . PetscViewer - the graphics context
302: Level: intermediate
304: Notes:
305: Must be called after PetscViewerCreate() before the PetscViewer is used.
307: Concepts: PetscViewer^setting options
309: .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerType
311: @*/
312: PetscErrorCode PetscViewerSetFromOptions(PetscViewer viewer)
313: {
314: PetscErrorCode ierr;
315: char vtype[256];
316: PetscBool flg;
321: if (!PetscViewerList) {
322: PetscViewerRegisterAll();
323: }
324: PetscObjectOptionsBegin((PetscObject)viewer);
325: PetscOptionsFList("-viewer_type","Type of PetscViewer","None",PetscViewerList,(char*)(((PetscObject)viewer)->type_name ? ((PetscObject)viewer)->type_name : PETSCVIEWERASCII),vtype,256,&flg);
326: if (flg) {
327: PetscViewerSetType(viewer,vtype);
328: }
329: /* type has not been set? */
330: if (!((PetscObject)viewer)->type_name) {
331: PetscViewerSetType(viewer,PETSCVIEWERASCII);
332: }
333: if (viewer->ops->setfromoptions) {
334: (*viewer->ops->setfromoptions)(PetscOptionsObject,viewer);
335: }
337: /* process any options handlers added with PetscObjectAddOptionsHandler() */
338: PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)viewer);
339: PetscViewerViewFromOptions(viewer,NULL,"-viewer_view");
340: PetscOptionsEnd();
341: return(0);
342: }
346: PetscErrorCode PetscViewerFlowControlStart(PetscViewer viewer,PetscInt *mcnt,PetscInt *cnt)
347: {
350: PetscViewerBinaryGetFlowControl(viewer,mcnt);
351: PetscViewerBinaryGetFlowControl(viewer,cnt);
352: return(0);
353: }
357: PetscErrorCode PetscViewerFlowControlStepMaster(PetscViewer viewer,PetscInt i,PetscInt *mcnt,PetscInt cnt)
358: {
360: MPI_Comm comm;
363: PetscObjectGetComm((PetscObject)viewer,&comm);
364: if (i >= *mcnt) {
365: *mcnt += cnt;
366: MPI_Bcast(mcnt,1,MPIU_INT,0,comm);
367: }
368: return(0);
369: }
373: PetscErrorCode PetscViewerFlowControlEndMaster(PetscViewer viewer,PetscInt *mcnt)
374: {
376: MPI_Comm comm;
378: PetscObjectGetComm((PetscObject)viewer,&comm);
379: *mcnt = 0;
380: MPI_Bcast(mcnt,1,MPIU_INT,0,comm);
381: return(0);
382: }
386: PetscErrorCode PetscViewerFlowControlStepWorker(PetscViewer viewer,PetscMPIInt rank,PetscInt *mcnt)
387: {
389: MPI_Comm comm;
391: PetscObjectGetComm((PetscObject)viewer,&comm);
392: while (PETSC_TRUE) {
393: if (rank < *mcnt) break;
394: MPI_Bcast(mcnt,1,MPIU_INT,0,comm);
395: }
396: return(0);
397: }
401: PetscErrorCode PetscViewerFlowControlEndWorker(PetscViewer viewer,PetscInt *mcnt)
402: {
404: MPI_Comm comm;
406: PetscObjectGetComm((PetscObject)viewer,&comm);
407: while (PETSC_TRUE) {
408: MPI_Bcast(mcnt,1,MPIU_INT,0,comm);
409: if (!*mcnt) break;
410: }
411: return(0);
412: }