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