xref: /petsc/src/sys/classes/viewer/interface/viewreg.c (revision 0f1503a698da0451f8f2e3e02a165d4e1f526457)
1 
2 #include <petsc/private/viewerimpl.h>  /*I "petscviewer.h" I*/
3 #include <petsc/private/hashtable.h>
4 #if defined(PETSC_HAVE_SAWS)
5 #include <petscviewersaws.h>
6 #endif
7 
8 PetscFunctionList PetscViewerList = NULL;
9 
10 PetscOptionsHelpPrinted PetscOptionsHelpPrintedSingleton = NULL;
11 KHASH_SET_INIT_STR(HTPrinted)
12 struct  _n_PetscOptionsHelpPrinted{
13   khash_t(HTPrinted) *printed;
14   PetscSegBuffer     strings;
15 };
16 
17 PetscErrorCode PetscOptionsHelpPrintedDestroy(PetscOptionsHelpPrinted *hp)
18 {
19   PetscErrorCode ierr;
20 
21   PetscFunctionBegin;
22   if (!*hp) PetscFunctionReturn(0);
23   kh_destroy(HTPrinted,(*hp)->printed);
24   ierr = PetscSegBufferDestroy(&(*hp)->strings);CHKERRQ(ierr);
25   ierr = PetscFree(*hp);CHKERRQ(ierr);
26   PetscFunctionReturn(0);
27 }
28 
29 /*@C
30       PetscOptionsHelpPrintedCreate - Creates an object used to manage tracking which help messages have
31          been printed so they will not be printed again.
32 
33      Not collective
34 
35     Level: developer
36 
37 .seealso: PetscOptionsHelpPrintedCheck(), PetscOptionsHelpPrintChecked()
38 @*/
39 PetscErrorCode PetscOptionsHelpPrintedCreate(PetscOptionsHelpPrinted *hp)
40 {
41   PetscErrorCode             ierr;
42 
43   PetscFunctionBegin;
44   ierr = PetscNew(hp);CHKERRQ(ierr);
45   (*hp)->printed = kh_init(HTPrinted);
46   ierr = PetscSegBufferCreate(sizeof(char),10000,&(*hp)->strings);CHKERRQ(ierr);
47   PetscFunctionReturn(0);
48 }
49 
50 /*@C
51       PetscOptionsHelpPrintedCheck - Checks if a particular pre, name pair has previous been entered (meaning the help message was printed)
52 
53      Not collective
54 
55     Input Parameters:
56 +     hp - the object used to manage tracking what help messages have been printed
57 .     pre - the prefix part of the string, many be NULL
58 -     name - the string to look for (cannot be NULL)
59 
60     Output Parameter:
61 .     found - PETSC_TRUE if the string was already set
62 
63     Level: intermediate
64 
65 .seealso: PetscOptionsHelpPrintedCreate()
66 @*/
67 PetscErrorCode PetscOptionsHelpPrintedCheck(PetscOptionsHelpPrinted hp,const char *pre,const char* name,PetscBool *found)
68 {
69   size_t          l1,l2;
70 #if !defined(PETSC_HAVE_THREADSAFETY)
71   char            *both;
72   int             newitem;
73 #endif
74   PetscErrorCode  ierr;
75 
76   PetscFunctionBegin;
77   ierr = PetscStrlen(pre,&l1);CHKERRQ(ierr);
78   ierr = PetscStrlen(name,&l2);CHKERRQ(ierr);
79   if (l1+l2 == 0) {
80     *found = PETSC_FALSE;
81     PetscFunctionReturn(0);
82   }
83 #if !defined(PETSC_HAVE_THREADSAFETY)
84   ierr = PetscSegBufferGet(hp->strings,l1+l2+1,&both);CHKERRQ(ierr);
85   ierr = PetscStrcpy(both,pre);CHKERRQ(ierr);
86   ierr = PetscStrcat(both,name);CHKERRQ(ierr);
87   kh_put(HTPrinted,hp->printed,both,&newitem);
88   if (!newitem) {
89     ierr = PetscSegBufferUnuse(hp->strings,l1+l2+1);CHKERRQ(ierr);
90   }
91   *found = newitem ? PETSC_FALSE : PETSC_TRUE;
92 #else
93   *found = PETSC_FALSE;
94 #endif
95   PetscFunctionReturn(0);
96 }
97 
98 static PetscBool noviewer = PETSC_FALSE;
99 static PetscBool noviewers[PETSCVIEWERGETVIEWEROFFPUSHESMAX];
100 static PetscInt  inoviewers = 0;
101 
102 /*@
103   PetscOptionsPushGetViewerOff - control whether PetscOptionsGetViewer returns a viewer.
104 
105   Logically Collective
106 
107   Input Parameter:
108 . flg - PETSC_TRUE to turn off viewer creation, PETSC_FALSE to turn it on.
109 
110   Level: developer
111 
112   Notes:
113     Calling XXXViewFromOptions in an inner loop can be very expensive.  This can appear, for example, when using
114    many small subsolves.  Call this function to control viewer creation in PetscOptionsGetViewer, thus removing the expensive XXXViewFromOptions calls.
115 
116 .seealso: PetscOptionsGetViewer(), PetscOptionsPopGetViewerOff()
117 @*/
118 PetscErrorCode  PetscOptionsPushGetViewerOff(PetscBool flg)
119 {
120   PetscFunctionBegin;
121   if (inoviewers > PETSCVIEWERGETVIEWEROFFPUSHESMAX - 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many PetscOptionsPushGetViewerOff(), perhaps you forgot PetscOptionsPopGetViewerOff()?");
122 
123   noviewers[inoviewers++] = noviewer;
124   noviewer = flg;
125   PetscFunctionReturn(0);
126 }
127 
128 /*@
129   PetscOptionsPopGetViewerOff - reset whether PetscOptionsGetViewer returns a viewer.
130 
131   Logically Collective
132 
133   Level: developer
134 
135   Notes:
136     Calling XXXViewFromOptions in an inner loop can be very expensive.  This can appear, for example, when using
137    many small subsolves.  Call this function to control viewer creation in PetscOptionsGetViewer, thus removing the expensive XXXViewFromOptions calls.
138 
139 .seealso: PetscOptionsGetViewer(), PetscOptionsPushGetViewerOff()
140 @*/
141 PetscErrorCode  PetscOptionsPopGetViewerOff(void)
142 {
143   PetscFunctionBegin;
144   if (!inoviewers) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many PetscOptionsPopGetViewerOff(), perhaps you forgot PetscOptionsPushGetViewerOff()?");
145   noviewer = noviewers[--inoviewers];
146   PetscFunctionReturn(0);
147 }
148 
149 /*@
150   PetscOptionsGetViewerOff - does PetscOptionsGetViewer return a viewer?
151 
152   Logically Collective
153 
154   Output Parameter:
155 . flg - whether viewers are returned.
156 
157   Level: developer
158 
159   Notes:
160     Calling XXXViewFromOptions in an inner loop can be very expensive.  This can appear, for example, when using
161    many small subsolves.
162 
163 .seealso: PetscOptionsGetViewer(), PetscOptionsPushGetViewerOff(), PetscOptionsPopGetViewerOff()
164 @*/
165 PetscErrorCode  PetscOptionsGetViewerOff(PetscBool *flg)
166 {
167   PetscFunctionBegin;
168   PetscValidBoolPointer(flg,1);
169   *flg = noviewer;
170   PetscFunctionReturn(0);
171 }
172 
173 /*@C
174    PetscOptionsGetViewer - Gets a viewer appropriate for the type indicated by the user
175 
176    Collective
177 
178    Input Parameters:
179 +  comm - the communicator to own the viewer
180 .  options - options database, use NULL for default global database
181 .  pre - the string to prepend to the name or NULL
182 -  name - the option one is seeking
183 
184    Output Parameters:
185 +  viewer - the viewer, pass NULL if not needed
186 .  format - the PetscViewerFormat requested by the user, pass NULL if not needed
187 -  set - PETSC_TRUE if found, else PETSC_FALSE
188 
189    Level: intermediate
190 
191    Notes:
192     If no value is provided ascii:stdout is used
193 $       ascii[:[filename][:[format][:append]]]    defaults to stdout - format can be one of ascii_info, ascii_info_detail, or ascii_matlab,
194                                                   for example ascii::ascii_info prints just the information about the object not all details
195                                                   unless :append is given filename opens in write mode, overwriting what was already there
196 $       binary[:[filename][:[format][:append]]]   defaults to the file binaryoutput
197 $       draw[:drawtype[:filename]]                for example, draw:tikz, draw:tikz:figure.tex  or draw:x
198 $       socket[:port]                             defaults to the standard output port
199 $       saws[:communicatorname]                    publishes object to the Scientific Application Webserver (SAWs)
200 
201    Use PetscViewerDestroy() after using the viewer, otherwise a memory leak will occur
202 
203    You can control whether calls to this function create a viewer (or return early with *set of PETSC_FALSE) with
204    PetscOptionsPushGetViewerOff.  This is useful if calling many small subsolves, in which case XXXViewFromOptions can take
205    an appreciable fraction of the runtime.
206 
207    If PETSc is configured with --with-viewfromoptions=0 this function always returns with *set of PETSC_FALSE
208 
209 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
210           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
211           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
212           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
213           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
214           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
215           PetscOptionsFList(), PetscOptionsEList(), PetscOptionsPushGetViewerOff(), PetscOptionsPopGetViewerOff(),
216           PetscOptionsGetViewerOff()
217 @*/
218 PetscErrorCode  PetscOptionsGetViewer(MPI_Comm comm,PetscOptions options,const char pre[],const char name[],PetscViewer *viewer,PetscViewerFormat *format,PetscBool  *set)
219 {
220   const char                     *value;
221   PetscErrorCode                 ierr;
222   PetscBool                      flag,hashelp;
223 
224   PetscFunctionBegin;
225   PetscValidCharPointer(name,4);
226 
227   if (viewer) *viewer = NULL;
228   if (format) *format = PETSC_VIEWER_DEFAULT;
229   if (set)    *set    = PETSC_FALSE;
230   ierr = PetscOptionsGetViewerOff(&flag);CHKERRQ(ierr);
231   if (flag) PetscFunctionReturn(0);
232 
233   ierr = PetscOptionsHasHelp(NULL,&hashelp);CHKERRQ(ierr);
234   if (hashelp) {
235     PetscBool found;
236 
237     if (!PetscOptionsHelpPrintedSingleton) {
238       ierr = PetscOptionsHelpPrintedCreate(&PetscOptionsHelpPrintedSingleton);CHKERRQ(ierr);
239     }
240     ierr = PetscOptionsHelpPrintedCheck(PetscOptionsHelpPrintedSingleton,pre,name,&found);CHKERRQ(ierr);
241     if (!found && viewer) {
242       ierr = (*PetscHelpPrintf)(comm,"----------------------------------------\nViewer (-%s%s) options:\n",pre ? pre : "",name+1);CHKERRQ(ierr);
243       ierr = (*PetscHelpPrintf)(comm,"  -%s%s ascii[:[filename][:[format][:append]]]: %s (%s)\n",pre ? pre : "",name+1,"Prints object to stdout or ASCII file","PetscOptionsGetViewer");CHKERRQ(ierr);
244       ierr = (*PetscHelpPrintf)(comm,"  -%s%s binary[:[filename][:[format][:append]]]: %s (%s)\n",pre ? pre : "",name+1,"Saves object to a binary file","PetscOptionsGetViewer");CHKERRQ(ierr);
245       ierr = (*PetscHelpPrintf)(comm,"  -%s%s draw[:[drawtype][:filename|format]] %s (%s)\n",pre ? pre : "",name+1,"Draws object","PetscOptionsGetViewer");CHKERRQ(ierr);
246       ierr = (*PetscHelpPrintf)(comm,"  -%s%s socket[:port]: %s (%s)\n",pre ? pre : "",name+1,"Pushes object to a Unix socket","PetscOptionsGetViewer");CHKERRQ(ierr);
247       ierr = (*PetscHelpPrintf)(comm,"  -%s%s saws[:communicatorname]: %s (%s)\n",pre ? pre : "",name+1,"Publishes object to SAWs","PetscOptionsGetViewer");CHKERRQ(ierr);
248     }
249   }
250 
251   if (format) *format = PETSC_VIEWER_DEFAULT;
252   ierr = PetscOptionsFindPair(options,pre,name,&value,&flag);CHKERRQ(ierr);
253   if (flag) {
254     if (set) *set = PETSC_TRUE;
255     if (!value) {
256       if (viewer) {
257         ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr);
258         ierr = PetscObjectReference((PetscObject)*viewer);CHKERRQ(ierr);
259       }
260     } else {
261       char       *loc0_vtype,*loc1_fname,*loc2_fmt = NULL,*loc3_fmode = NULL;
262       PetscInt   cnt;
263       const char *viewers[] = {PETSCVIEWERASCII,PETSCVIEWERBINARY,PETSCVIEWERDRAW,PETSCVIEWERSOCKET,PETSCVIEWERMATLAB,PETSCVIEWERSAWS,PETSCVIEWERVTK,PETSCVIEWERHDF5,PETSCVIEWERGLVIS,PETSCVIEWEREXODUSII,NULL};
264 
265       ierr = PetscStrallocpy(value,&loc0_vtype);CHKERRQ(ierr);
266       ierr = PetscStrchr(loc0_vtype,':',&loc1_fname);CHKERRQ(ierr);
267       if (loc1_fname) {
268         *loc1_fname++ = 0;
269         ierr = PetscStrchr(loc1_fname,':',&loc2_fmt);CHKERRQ(ierr);
270       }
271       if (loc2_fmt) {
272         *loc2_fmt++ = 0;
273         ierr = PetscStrchr(loc2_fmt,':',&loc3_fmode);CHKERRQ(ierr);
274       }
275       if (loc3_fmode) *loc3_fmode++ = 0;
276       ierr = PetscStrendswithwhich(*loc0_vtype ? loc0_vtype : "ascii",viewers,&cnt);CHKERRQ(ierr);
277       if (cnt > (PetscInt) sizeof(viewers)-1) SETERRQ1(comm,PETSC_ERR_ARG_OUTOFRANGE,"Unknown viewer type: %s",loc0_vtype);
278       if (viewer) {
279         if (!loc1_fname) {
280           switch (cnt) {
281           case 0:
282             ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr);
283             break;
284           case 1:
285             if (!(*viewer = PETSC_VIEWER_BINARY_(comm))) CHKERRQ(PETSC_ERR_PLIB);
286             break;
287           case 2:
288             if (!(*viewer = PETSC_VIEWER_DRAW_(comm))) CHKERRQ(PETSC_ERR_PLIB);
289             break;
290 #if defined(PETSC_USE_SOCKET_VIEWER)
291           case 3:
292             if (!(*viewer = PETSC_VIEWER_SOCKET_(comm))) CHKERRQ(PETSC_ERR_PLIB);
293             break;
294 #endif
295 #if defined(PETSC_HAVE_MATLAB_ENGINE)
296           case 4:
297             if (!(*viewer = PETSC_VIEWER_MATLAB_(comm))) CHKERRQ(PETSC_ERR_PLIB);
298             break;
299 #endif
300 #if defined(PETSC_HAVE_SAWS)
301           case 5:
302             if (!(*viewer = PETSC_VIEWER_SAWS_(comm))) CHKERRQ(PETSC_ERR_PLIB);
303             break;
304 #endif
305 #if defined(PETSC_HAVE_HDF5)
306           case 7:
307             if (!(*viewer = PETSC_VIEWER_HDF5_(comm))) CHKERRQ(PETSC_ERR_PLIB);
308             break;
309 #endif
310           case 8:
311             if (!(*viewer = PETSC_VIEWER_GLVIS_(comm))) CHKERRQ(PETSC_ERR_PLIB);
312             break;
313 #if defined(PETSC_HAVE_EXODUSII)
314           case 9:
315             if (!(*viewer = PETSC_VIEWER_EXODUSII_(comm))) CHKERRQ(PETSC_ERR_PLIB);
316             break;
317 #endif
318           default: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unsupported viewer %s",loc0_vtype);
319           }
320           ierr = PetscObjectReference((PetscObject)*viewer);CHKERRQ(ierr);
321         } else {
322           if (loc2_fmt && !*loc1_fname && (cnt == 0)) { /* ASCII format without file name */
323             ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr);
324             ierr = PetscObjectReference((PetscObject)*viewer);CHKERRQ(ierr);
325           } else {
326             PetscFileMode fmode;
327             ierr = PetscViewerCreate(comm,viewer);CHKERRQ(ierr);
328             ierr = PetscViewerSetType(*viewer,*loc0_vtype ? loc0_vtype : "ascii");CHKERRQ(ierr);
329             fmode = FILE_MODE_WRITE;
330             if (loc3_fmode && *loc3_fmode) { /* Has non-empty file mode ("write" or "append") */
331               ierr = PetscEnumFind(PetscFileModes,loc3_fmode,(PetscEnum*)&fmode,&flag);CHKERRQ(ierr);
332               if (!flag) SETERRQ1(comm,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown file mode: %s",loc3_fmode);
333             }
334             if (loc2_fmt) {
335               PetscBool tk,im;
336               ierr = PetscStrcmp(loc1_fname,"tikz",&tk);CHKERRQ(ierr);
337               ierr = PetscStrcmp(loc1_fname,"image",&im);CHKERRQ(ierr);
338               if (tk || im) {
339                 ierr = PetscViewerDrawSetInfo(*viewer,NULL,loc2_fmt,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE);CHKERRQ(ierr);
340                 *loc2_fmt = 0;
341               }
342             }
343             ierr = PetscViewerFileSetMode(*viewer,flag?fmode:FILE_MODE_WRITE);CHKERRQ(ierr);
344             ierr = PetscViewerFileSetName(*viewer,loc1_fname);CHKERRQ(ierr);
345             if (*loc1_fname) {
346               ierr = PetscViewerDrawSetDrawType(*viewer,loc1_fname);CHKERRQ(ierr);
347             }
348             ierr = PetscViewerSetFromOptions(*viewer);CHKERRQ(ierr);
349           }
350         }
351       }
352       if (viewer) {
353         ierr = PetscViewerSetUp(*viewer);CHKERRQ(ierr);
354       }
355       if (loc2_fmt && *loc2_fmt) {
356         PetscViewerFormat tfmt;
357 
358         ierr = PetscEnumFind(PetscViewerFormats,loc2_fmt,(PetscEnum*)&tfmt,&flag);CHKERRQ(ierr);
359         if (format) *format = tfmt;
360         if (!flag) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unknown viewer format %s",loc2_fmt);
361       } else if (viewer && (cnt == 6) && format) { /* Get format from VTK viewer */
362         ierr = PetscViewerGetFormat(*viewer,format);CHKERRQ(ierr);
363       }
364       ierr = PetscFree(loc0_vtype);CHKERRQ(ierr);
365     }
366   }
367   PetscFunctionReturn(0);
368 }
369 
370 /*@
371    PetscViewerCreate - Creates a viewing context
372 
373    Collective
374 
375    Input Parameter:
376 .  comm - MPI communicator
377 
378    Output Parameter:
379 .  inviewer - location to put the PetscViewer context
380 
381    Level: advanced
382 
383 .seealso: PetscViewerDestroy(), PetscViewerSetType(), PetscViewerType
384 
385 @*/
386 PetscErrorCode  PetscViewerCreate(MPI_Comm comm,PetscViewer *inviewer)
387 {
388   PetscViewer    viewer;
389   PetscErrorCode ierr;
390 
391   PetscFunctionBegin;
392   *inviewer = NULL;
393   ierr = PetscViewerInitializePackage();CHKERRQ(ierr);
394   ierr         = PetscHeaderCreate(viewer,PETSC_VIEWER_CLASSID,"PetscViewer","PetscViewer","Viewer",comm,PetscViewerDestroy,PetscViewerView);CHKERRQ(ierr);
395   *inviewer    = viewer;
396   viewer->data = NULL;
397   PetscFunctionReturn(0);
398 }
399 
400 /*@C
401    PetscViewerSetType - Builds PetscViewer for a particular implementation.
402 
403    Collective on PetscViewer
404 
405    Input Parameters:
406 +  viewer      - the PetscViewer context
407 -  type        - for example, PETSCVIEWERASCII
408 
409    Options Database Command:
410 .  -viewer_type  <type> - Sets the type; use -help for a list
411     of available methods (for instance, ascii)
412 
413    Level: advanced
414 
415    Notes:
416    See "include/petscviewer.h" for available methods (for instance,
417    PETSCVIEWERSOCKET)
418 
419 .seealso: PetscViewerCreate(), PetscViewerGetType(), PetscViewerType, PetscViewerPushFormat()
420 @*/
421 PetscErrorCode  PetscViewerSetType(PetscViewer viewer,PetscViewerType type)
422 {
423   PetscErrorCode ierr,(*r)(PetscViewer);
424   PetscBool      match;
425 
426   PetscFunctionBegin;
427   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
428   PetscValidCharPointer(type,2);
429   ierr = PetscObjectTypeCompare((PetscObject)viewer,type,&match);CHKERRQ(ierr);
430   if (match) PetscFunctionReturn(0);
431 
432   /* cleanup any old type that may be there */
433   if (viewer->data) {
434     ierr         = (*viewer->ops->destroy)(viewer);CHKERRQ(ierr);
435 
436     viewer->ops->destroy = NULL;
437     viewer->data         = NULL;
438   }
439   ierr = PetscMemzero(viewer->ops,sizeof(struct _PetscViewerOps));CHKERRQ(ierr);
440 
441   ierr =  PetscFunctionListFind(PetscViewerList,type,&r);CHKERRQ(ierr);
442   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown PetscViewer type given: %s",type);
443 
444   ierr = PetscObjectChangeTypeName((PetscObject)viewer,type);CHKERRQ(ierr);
445   ierr = (*r)(viewer);CHKERRQ(ierr);
446   PetscFunctionReturn(0);
447 }
448 
449 /*@C
450    PetscViewerRegister - Adds a viewer
451 
452    Not Collective
453 
454    Input Parameters:
455 +  name_solver - name of a new user-defined viewer
456 -  routine_create - routine to create method context
457 
458    Level: developer
459    Notes:
460    PetscViewerRegister() may be called multiple times to add several user-defined viewers.
461 
462    Sample usage:
463 .vb
464    PetscViewerRegister("my_viewer_type",MyViewerCreate);
465 .ve
466 
467    Then, your solver can be chosen with the procedural interface via
468 $     PetscViewerSetType(viewer,"my_viewer_type")
469    or at runtime via the option
470 $     -viewer_type my_viewer_type
471 
472 .seealso: PetscViewerRegisterAll()
473  @*/
474 PetscErrorCode  PetscViewerRegister(const char *sname,PetscErrorCode (*function)(PetscViewer))
475 {
476   PetscErrorCode ierr;
477 
478   PetscFunctionBegin;
479   ierr = PetscViewerInitializePackage();CHKERRQ(ierr);
480   ierr = PetscFunctionListAdd(&PetscViewerList,sname,function);CHKERRQ(ierr);
481   PetscFunctionReturn(0);
482 }
483 
484 /*@C
485    PetscViewerSetFromOptions - Sets the graphics type from the options database.
486       Defaults to a PETSc X windows graphics.
487 
488    Collective on PetscViewer
489 
490    Input Parameter:
491 .     PetscViewer - the graphics context
492 
493    Level: intermediate
494 
495    Notes:
496     Must be called after PetscViewerCreate() before the PetscViewer is used.
497 
498 .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerType
499 
500 @*/
501 PetscErrorCode  PetscViewerSetFromOptions(PetscViewer viewer)
502 {
503   PetscErrorCode    ierr;
504   char              vtype[256];
505   PetscBool         flg;
506 
507   PetscFunctionBegin;
508   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
509 
510   if (!PetscViewerList) {
511     ierr = PetscViewerRegisterAll();CHKERRQ(ierr);
512   }
513   ierr = PetscObjectOptionsBegin((PetscObject)viewer);CHKERRQ(ierr);
514   ierr = PetscOptionsFList("-viewer_type","Type of PetscViewer","None",PetscViewerList,(char*)(((PetscObject)viewer)->type_name ? ((PetscObject)viewer)->type_name : PETSCVIEWERASCII),vtype,256,&flg);CHKERRQ(ierr);
515   if (flg) {
516     ierr = PetscViewerSetType(viewer,vtype);CHKERRQ(ierr);
517   }
518   /* type has not been set? */
519   if (!((PetscObject)viewer)->type_name) {
520     ierr = PetscViewerSetType(viewer,PETSCVIEWERASCII);CHKERRQ(ierr);
521   }
522   if (viewer->ops->setfromoptions) {
523     ierr = (*viewer->ops->setfromoptions)(PetscOptionsObject,viewer);CHKERRQ(ierr);
524   }
525 
526   /* process any options handlers added with PetscObjectAddOptionsHandler() */
527   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)viewer);CHKERRQ(ierr);
528   ierr = PetscViewerViewFromOptions(viewer,NULL,"-viewer_view");CHKERRQ(ierr);
529   ierr = PetscOptionsEnd();CHKERRQ(ierr);
530   PetscFunctionReturn(0);
531 }
532 
533 PetscErrorCode PetscViewerFlowControlStart(PetscViewer viewer,PetscInt *mcnt,PetscInt *cnt)
534 {
535   PetscErrorCode ierr;
536   PetscFunctionBegin;
537   ierr = PetscViewerBinaryGetFlowControl(viewer,mcnt);CHKERRQ(ierr);
538   ierr = PetscViewerBinaryGetFlowControl(viewer,cnt);CHKERRQ(ierr);
539   PetscFunctionReturn(0);
540 }
541 
542 PetscErrorCode PetscViewerFlowControlStepMain(PetscViewer viewer,PetscInt i,PetscInt *mcnt,PetscInt cnt)
543 {
544   PetscErrorCode ierr;
545   MPI_Comm       comm;
546 
547   PetscFunctionBegin;
548   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
549   if (i >= *mcnt) {
550     *mcnt += cnt;
551     ierr = MPI_Bcast(mcnt,1,MPIU_INT,0,comm);CHKERRMPI(ierr);
552   }
553   PetscFunctionReturn(0);
554 }
555 
556 PetscErrorCode PetscViewerFlowControlEndMain(PetscViewer viewer,PetscInt *mcnt)
557 {
558   PetscErrorCode ierr;
559   MPI_Comm       comm;
560   PetscFunctionBegin;
561   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
562   *mcnt = 0;
563   ierr = MPI_Bcast(mcnt,1,MPIU_INT,0,comm);CHKERRMPI(ierr);
564   PetscFunctionReturn(0);
565 }
566 
567 PetscErrorCode PetscViewerFlowControlStepWorker(PetscViewer viewer,PetscMPIInt rank,PetscInt *mcnt)
568 {
569   PetscErrorCode ierr;
570   MPI_Comm       comm;
571   PetscFunctionBegin;
572   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
573   while (PETSC_TRUE) {
574     if (rank < *mcnt) break;
575     ierr = MPI_Bcast(mcnt,1,MPIU_INT,0,comm);CHKERRMPI(ierr);
576   }
577   PetscFunctionReturn(0);
578 }
579 
580 PetscErrorCode PetscViewerFlowControlEndWorker(PetscViewer viewer,PetscInt *mcnt)
581 {
582   PetscErrorCode ierr;
583   MPI_Comm       comm;
584   PetscFunctionBegin;
585   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
586   while (PETSC_TRUE) {
587     ierr = MPI_Bcast(mcnt,1,MPIU_INT,0,comm);CHKERRMPI(ierr);
588     if (!*mcnt) break;
589   }
590   PetscFunctionReturn(0);
591 }
592