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