xref: /petsc/src/sys/classes/viewer/interface/viewreg.c (revision e907feaad8a9ad15ff003b1a1f8acb2ecb25e843)
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) PetscCall(PetscViewerASCIIGetStdout(comm, viewer));
182     PetscFunctionReturn(PETSC_SUCCESS);
183   }
184 
185   PetscCall(PetscStrallocpy(value, &loc0_vtype));
186   PetscCall(PetscStrchr(loc0_vtype, ':', &loc1_fname));
187   if (loc1_fname) {
188     *loc1_fname++ = 0;
189     PetscCall(PetscStrchr(loc1_fname, ':', &loc2_fmt));
190   }
191   if (loc2_fmt) {
192     *loc2_fmt++ = 0;
193     PetscCall(PetscStrchr(loc2_fmt, ':', &loc3_fmode));
194   }
195   if (loc3_fmode) *loc3_fmode++ = 0;
196   PetscCall(PetscStrendswithwhich(*loc0_vtype ? loc0_vtype : "ascii", viewers, &cnt));
197   PetscCheck(cnt <= (PetscInt)sizeof(viewers) - 1, comm, PETSC_ERR_ARG_OUTOFRANGE, "Unknown viewer type: %s", loc0_vtype);
198   if (viewer) {
199     if (!loc1_fname) {
200       switch (cnt) {
201       case 0:
202         PetscCall(PetscViewerASCIIGetStdout(comm, viewer));
203         break;
204       case 1:
205         if (!(*viewer = PETSC_VIEWER_BINARY_(comm))) PetscCall(PETSC_ERR_PLIB);
206         break;
207       case 2:
208         if (!(*viewer = PETSC_VIEWER_DRAW_(comm))) PetscCall(PETSC_ERR_PLIB);
209         break;
210 #if defined(PETSC_USE_SOCKET_VIEWER)
211       case 3:
212         if (!(*viewer = PETSC_VIEWER_SOCKET_(comm))) PetscCall(PETSC_ERR_PLIB);
213         break;
214 #endif
215 #if defined(PETSC_HAVE_MATLAB)
216       case 4:
217         if (!(*viewer = PETSC_VIEWER_MATLAB_(comm))) PetscCall(PETSC_ERR_PLIB);
218         break;
219 #endif
220 #if defined(PETSC_HAVE_SAWS)
221       case 5:
222         if (!(*viewer = PETSC_VIEWER_SAWS_(comm))) PetscCall(PETSC_ERR_PLIB);
223         break;
224 #endif
225 #if defined(PETSC_HAVE_HDF5)
226       case 7:
227         if (!(*viewer = PETSC_VIEWER_HDF5_(comm))) PetscCall(PETSC_ERR_PLIB);
228         break;
229 #endif
230       case 8:
231         if (!(*viewer = PETSC_VIEWER_GLVIS_(comm))) PetscCall(PETSC_ERR_PLIB);
232         break;
233 #if defined(PETSC_HAVE_EXODUSII)
234       case 9:
235         if (!(*viewer = PETSC_VIEWER_EXODUSII_(comm))) PetscCall(PETSC_ERR_PLIB);
236         break;
237 #endif
238       default:
239         SETERRQ(comm, PETSC_ERR_SUP, "Unsupported viewer %s", loc0_vtype);
240       }
241     } else {
242       if (loc2_fmt && !*loc1_fname && (cnt == 0)) { /* ASCII format without file name */
243         PetscCall(PetscViewerASCIIGetStdout(comm, viewer));
244       } else {
245         PetscFileMode fmode;
246         PetscBool     flag = PETSC_FALSE;
247 
248         PetscCall(PetscViewerCreate(comm, viewer));
249         PetscCall(PetscViewerSetType(*viewer, *loc0_vtype ? loc0_vtype : "ascii"));
250         fmode = FILE_MODE_WRITE;
251         if (loc3_fmode && *loc3_fmode) { /* Has non-empty file mode ("write" or "append") */
252           PetscCall(PetscEnumFind(PetscFileModes, loc3_fmode, (PetscEnum *)&fmode, &flag));
253           PetscCheck(flag, comm, PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown file mode: %s", loc3_fmode);
254         }
255         if (loc2_fmt) {
256           PetscBool tk, im;
257           PetscCall(PetscStrcmp(loc1_fname, "tikz", &tk));
258           PetscCall(PetscStrcmp(loc1_fname, "image", &im));
259           if (tk || im) {
260             PetscCall(PetscViewerDrawSetInfo(*viewer, NULL, loc2_fmt, PETSC_DECIDE, PETSC_DECIDE, PETSC_DECIDE, PETSC_DECIDE));
261             *loc2_fmt = 0;
262           }
263         }
264         PetscCall(PetscViewerFileSetMode(*viewer, flag ? fmode : FILE_MODE_WRITE));
265         PetscCall(PetscViewerFileSetName(*viewer, loc1_fname));
266         if (*loc1_fname) PetscCall(PetscViewerDrawSetDrawType(*viewer, loc1_fname));
267         PetscCall(PetscViewerSetFromOptions(*viewer));
268       }
269     }
270   }
271   if (viewer) PetscCall(PetscViewerSetUp(*viewer));
272   if (loc2_fmt && *loc2_fmt) {
273     PetscViewerFormat tfmt;
274     PetscBool         flag;
275 
276     PetscCall(PetscEnumFind(PetscViewerFormats, loc2_fmt, (PetscEnum *)&tfmt, &flag));
277     if (format) *format = tfmt;
278     PetscCheck(flag, comm, PETSC_ERR_SUP, "Unknown viewer format %s", loc2_fmt);
279   } else if (viewer && (cnt == 6) && format) { /* Get format from VTK viewer */
280     PetscCall(PetscViewerGetFormat(*viewer, format));
281   }
282   PetscCall(PetscFree(loc0_vtype));
283 
284   PetscFunctionReturn(PETSC_SUCCESS);
285 }
286 
287 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)
288 {
289   const char *value;
290   PetscBool   flag, hashelp;
291   PetscInt    n_max;
292 
293   PetscFunctionBegin;
294   PetscAssertPointer(name, 4);
295   PetscAssertPointer(n_max_p, 5);
296   n_max = *n_max_p;
297   PetscCheck(n_max >= 0, comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid size %" PetscInt_FMT " of passed arrays", *n_max_p);
298   *n_max_p = 0;
299 
300   if (set) *set = PETSC_FALSE;
301   PetscCall(PetscOptionsGetViewerOff(&flag));
302   if (flag) PetscFunctionReturn(PETSC_SUCCESS);
303 
304   PetscCall(PetscOptionsHasHelp(NULL, &hashelp));
305   if (hashelp) {
306     PetscBool found;
307 
308     if (!PetscOptionsHelpPrintedSingleton) PetscCall(PetscOptionsHelpPrintedCreate(&PetscOptionsHelpPrintedSingleton));
309     PetscCall(PetscOptionsHelpPrintedCheck(PetscOptionsHelpPrintedSingleton, pre, name, &found));
310     if (!found && viewer) {
311       PetscCall((*PetscHelpPrintf)(comm, "----------------------------------------\nViewer (-%s%s) options:\n", pre ? pre : "", name + 1));
312       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));
313       PetscCall((*PetscHelpPrintf)(comm, "  -%s%s binary[:[filename][:[format][:append]]]: %s (%s)\n", pre ? pre : "", name + 1, "Saves object to a binary file", func_name));
314       PetscCall((*PetscHelpPrintf)(comm, "  -%s%s draw[:[drawtype][:filename|format]] %s (%s)\n", pre ? pre : "", name + 1, "Draws object", func_name));
315       PetscCall((*PetscHelpPrintf)(comm, "  -%s%s socket[:port]: %s (%s)\n", pre ? pre : "", name + 1, "Pushes object to a Unix socket", func_name));
316       PetscCall((*PetscHelpPrintf)(comm, "  -%s%s saws[:communicatorname]: %s (%s)\n", pre ? pre : "", name + 1, "Publishes object to SAWs", func_name));
317       if (allow_multiple) PetscCall((*PetscHelpPrintf)(comm, "  -%s%s v1[,v2,...]: %s (%s)\n", pre ? pre : "", name + 1, "Multiple viewers", func_name));
318     }
319   }
320 
321   PetscCall(PetscOptionsFindPair(options, pre, name, &value, &flag));
322   if (flag) {
323     if (set) *set = PETSC_TRUE;
324     if (!value) {
325       PetscCheck(n_max > 0, comm, PETSC_ERR_ARG_SIZ, "More viewers (1) than max available (0)");
326       if (format) *format = PETSC_VIEWER_DEFAULT;
327       if (viewer) PetscCall(PetscViewerASCIIGetStdout(comm, viewer));
328       *n_max_p = 1;
329     } else {
330       char  *loc0_viewer_string = NULL, *this_viewer_string = NULL;
331       size_t viewer_string_length;
332 
333       PetscCall(PetscStrallocpy(value, &loc0_viewer_string));
334       PetscCall(PetscStrlen(loc0_viewer_string, &viewer_string_length));
335       this_viewer_string = loc0_viewer_string;
336 
337       do {
338         PetscViewer       *this_viewer;
339         PetscViewerFormat *this_viewer_format;
340         char              *next_viewer_string = NULL;
341         char              *comma_separator    = NULL;
342         PetscInt           n                  = *n_max_p;
343 
344         PetscCheck(n < n_max, comm, PETSC_ERR_PLIB, "More viewers than max available (%d)", (int)n_max);
345 
346         PetscCall(PetscStrchr(this_viewer_string, ',', &comma_separator));
347         if (comma_separator) {
348           PetscCheck(allow_multiple, comm, PETSC_ERR_ARG_OUTOFRANGE, "Trying to pass multiple viewers to %s: only one allowed.  Use PetscOptionsGetViewers() instead", func_name);
349           *comma_separator   = 0;
350           next_viewer_string = comma_separator + 1;
351         }
352         this_viewer = viewer ? &viewer[n] : NULL;
353         if (this_viewer) *this_viewer = NULL;
354         this_viewer_format = format ? &format[n] : NULL;
355         if (this_viewer_format) *this_viewer_format = PETSC_VIEWER_DEFAULT;
356         PetscCall(PetscOptionsGetViewers_Single(comm, this_viewer_string, this_viewer, this_viewer_format));
357         this_viewer_string = next_viewer_string;
358         (*n_max_p)++;
359       } while (this_viewer_string);
360       PetscCall(PetscFree(loc0_viewer_string));
361     }
362   }
363   PetscFunctionReturn(PETSC_SUCCESS);
364 }
365 
366 /*@C
367   PetscOptionsGetViewer - Gets a viewer appropriate for the type indicated by the user
368 
369   Collective
370 
371   Input Parameters:
372 + comm    - the communicator to own the viewer
373 . options - options database, use `NULL` for default global database
374 . pre     - the string to prepend to the name or `NULL`
375 - name    - the options database name that will be checked for
376 
377   Output Parameters:
378 + viewer - the viewer, pass `NULL` if not needed
379 . format - the `PetscViewerFormat` requested by the user, pass `NULL` if not needed
380 - set    - `PETSC_TRUE` if found, else `PETSC_FALSE`
381 
382   Level: intermediate
383 
384   Notes:
385   If no value is provided ascii:stdout is used
386 +       ascii[:[filename][:[format][:append]]]  -  defaults to stdout - format can be one of ascii_info, ascii_info_detail, or ascii_matlab,
387   for example ascii::ascii_info prints just the information about the object not all details
388   unless :append is given filename opens in write mode, overwriting what was already there
389 .       binary[:[filename][:[format][:append]]] -  defaults to the file binaryoutput
390 .       draw[:drawtype[:filename]]              -  for example, draw:tikz, draw:tikz:figure.tex  or draw:x
391 .       socket[:port]                           -  defaults to the standard output port
392 -       saws[:communicatorname]                 -   publishes object to the Scientific Application Webserver (SAWs)
393 
394   Use `PetscOptionsRestoreViewer()` after using the viewer, do not use `PetscViewerDestroy()`
395 
396   You can control whether calls to this function create a viewer (or return early with *set of `PETSC_FALSE`) with
397   `PetscOptionsPushGetViewerOff()`.  This is useful if calling many small subsolves, in which case XXXViewFromOptions can take
398   an appreciable fraction of the runtime.
399 
400   If PETSc is configured with `--with-viewfromoptions=0` this function always returns with *set of `PETSC_FALSE`
401 
402   This routine is thread-safe for accessing predefined `PetscViewer`s like `PETSC_VIEWER_STDOUT_SELF` but not for accessing
403   files by name.
404 
405 .seealso: [](sec_viewers), `PetscOptionsRestoreViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
406           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
407           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`,
408           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
409           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
410           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
411           `PetscOptionsFList()`, `PetscOptionsEList()`, `PetscOptionsPushGetViewerOff()`, `PetscOptionsPopGetViewerOff()`,
412           `PetscOptionsGetViewerOff()`
413 @*/
414 PetscErrorCode PetscOptionsGetViewer(MPI_Comm comm, PetscOptions options, const char pre[], const char name[], PetscViewer *viewer, PetscViewerFormat *format, PetscBool *set)
415 {
416   PetscInt  n_max = 1;
417   PetscBool set_internal;
418 
419   PetscFunctionBegin;
420   if (viewer) *viewer = NULL;
421   if (format) *format = PETSC_VIEWER_DEFAULT;
422   PetscCall(PetscOptionsGetViewers_Internal(comm, options, pre, name, &n_max, viewer, format, &set_internal, PETSC_FUNCTION_NAME, PETSC_FALSE));
423   if (set_internal) PetscAssert(n_max == 1, comm, PETSC_ERR_PLIB, "Unexpected: %d != 1 viewers set", (int)n_max);
424   if (set) *set = set_internal;
425   PetscFunctionReturn(PETSC_SUCCESS);
426 }
427 
428 /*@C
429   PetscOptionsRestoreViewer - restore a `PetscViewer` obtained from `PetscOptionsGetViewer()` or `PetscOptionsGetViewers()`
430 
431   Collective
432 
433   Input Parameter:
434 . viewer - the viewer
435 
436   Level: intermediate
437 
438   Developer Note:
439   This function exists to handle persistent viewers such as `PETSC_VIEWER_STDOUT_SELF` that may be shared between threads where
440   changing the reference count would not be thread safe.
441 
442 .seealso: [](sec_viewers), `PetscOptionsGetViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
443           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
444           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`,
445           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
446           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
447           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
448           `PetscOptionsFList()`, `PetscOptionsEList()`, `PetscOptionsPushGetViewerOff()`, `PetscOptionsPopGetViewerOff()`,
449           `PetscOptionsGetViewerOff()`
450 @*/
451 PetscErrorCode PetscOptionsRestoreViewer(PetscViewer *viewer)
452 {
453   PetscFunctionBegin;
454   if (!*viewer) PetscFunctionReturn(PETSC_SUCCESS);
455   if (!((PetscObject)*viewer)->persistent) PetscCall(PetscViewerDestroy(viewer));
456   else *viewer = NULL;
457   PetscFunctionReturn(PETSC_SUCCESS);
458 }
459 
460 /*@C
461   PetscOptionsGetViewers - Get multiple viewers from a comma-separated list in the options database
462 
463   Collective
464 
465   Input Parameters:
466 + comm    - the communicator to own the viewers
467 . options - options database, use `NULL` for default global database
468 . pre     - the string to prepend to the name or `NULL`
469 . name    - the options database name that will be checked for
470 - n_max   - on input: the maximum number of viewers; on output: the number of viewers in the comma-separated list
471 
472   Output Parameters:
473 + viewers - an array to hold at least `n_max` `PetscViewer`s, or `NULL` if not needed; on output: if not `NULL`, the
474              first `n_max` entries are initialized `PetscViewer`s
475 . formats - an array to hold at least `n_max` `PetscViewerFormat`s, or `NULL` if not needed; on output: if not
476              `NULL`, the first `n_max` entries are valid `PetscViewewFormat`s
477 - set     - `PETSC_TRUE` if found, else `PETSC_FALSE`
478 
479   Level: intermediate
480 
481   Note:
482   See `PetscOptionsGetViewer()` for how the format strings for the viewers are interpreted.  Use `PetscOptionsRestoreViewer()` on each viewer, otherwise a memory leak will occur.
483 
484   If PETSc is configured with `--with-viewfromoptions=0` this function always returns with `n_max` of 0 and `set` of `PETSC_FALSE`
485 
486 .seealso: [](sec_viewers), `PetscOptionsGetViewer()`
487 @*/
488 PetscErrorCode PetscOptionsGetViewers(MPI_Comm comm, PetscOptions options, const char pre[], const char name[], PetscInt *n_max, PetscViewer viewers[], PetscViewerFormat formats[], PetscBool *set)
489 {
490   PetscFunctionBegin;
491   PetscCall(PetscOptionsGetViewers_Internal(comm, options, pre, name, n_max, viewers, formats, set, PETSC_FUNCTION_NAME, PETSC_TRUE));
492   PetscFunctionReturn(PETSC_SUCCESS);
493 }
494 
495 /*@
496   PetscViewerCreate - Creates a viewing context. A `PetscViewer` represents a file, a graphical window, a Unix socket or a variety of other ways
497   of viewing a PETSc object
498 
499   Collective
500 
501   Input Parameter:
502 . comm - MPI communicator
503 
504   Output Parameter:
505 . inviewer - location to put the `PetscViewer` context
506 
507   Level: advanced
508 
509 .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerDestroy()`, `PetscViewerSetType()`, `PetscViewerType`
510 @*/
511 PetscErrorCode PetscViewerCreate(MPI_Comm comm, PetscViewer *inviewer)
512 {
513   PetscViewer viewer;
514 
515   PetscFunctionBegin;
516   *inviewer = NULL;
517   PetscCall(PetscViewerInitializePackage());
518   PetscCall(PetscHeaderCreate(viewer, PETSC_VIEWER_CLASSID, "PetscViewer", "PetscViewer", "Viewer", comm, PetscViewerDestroy, PetscViewerView));
519   *inviewer    = viewer;
520   viewer->data = NULL;
521   PetscFunctionReturn(PETSC_SUCCESS);
522 }
523 
524 /*@C
525   PetscViewerSetType - Builds `PetscViewer` for a particular implementation.
526 
527   Collective
528 
529   Input Parameters:
530 + viewer - the `PetscViewer` context obtained with `PetscViewerCreate()`
531 - type   - for example, `PETSCVIEWERASCII`
532 
533   Options Database Key:
534 . -viewer_type  <type> - Sets the type; use -help for a list of available methods (for instance, ascii)
535 
536   Level: advanced
537 
538   Note:
539   See `PetscViewerType` for possible values
540 
541 .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerCreate()`, `PetscViewerGetType()`, `PetscViewerType`, `PetscViewerPushFormat()`
542 @*/
543 PetscErrorCode PetscViewerSetType(PetscViewer viewer, PetscViewerType type)
544 {
545   PetscBool match;
546   PetscErrorCode (*r)(PetscViewer);
547 
548   PetscFunctionBegin;
549   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
550   PetscAssertPointer(type, 2);
551   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, type, &match));
552   if (match) PetscFunctionReturn(PETSC_SUCCESS);
553 
554   /* cleanup any old type that may be there */
555   PetscTryTypeMethod(viewer, destroy);
556   viewer->ops->destroy = NULL;
557   viewer->data         = NULL;
558 
559   PetscCall(PetscMemzero(viewer->ops, sizeof(struct _PetscViewerOps)));
560 
561   PetscCall(PetscFunctionListFind(PetscViewerList, type, &r));
562   PetscCheck(r, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PetscViewer type given: %s", type);
563 
564   PetscCall(PetscObjectChangeTypeName((PetscObject)viewer, type));
565   PetscCall((*r)(viewer));
566   PetscFunctionReturn(PETSC_SUCCESS);
567 }
568 
569 /*@C
570   PetscViewerRegister - Adds a viewer to those available for use with `PetscViewerSetType()`
571 
572   Not Collective
573 
574   Input Parameters:
575 + sname    - name of a new user-defined viewer
576 - function - routine to create method context
577 
578   Level: developer
579 
580   Note:
581   `PetscViewerRegister()` may be called multiple times to add several user-defined viewers.
582 
583   Example Usage:
584 .vb
585    PetscViewerRegister("my_viewer_type", MyViewerCreate);
586 .ve
587 
588   Then, your solver can be chosen with the procedural interface via
589 $     PetscViewerSetType(viewer, "my_viewer_type")
590   or at runtime via the option
591 $     -viewer_type my_viewer_type
592 
593 .seealso: [](sec_viewers), `PetscViewerRegisterAll()`
594  @*/
595 PetscErrorCode PetscViewerRegister(const char *sname, PetscErrorCode (*function)(PetscViewer))
596 {
597   PetscFunctionBegin;
598   PetscCall(PetscViewerInitializePackage());
599   PetscCall(PetscFunctionListAdd(&PetscViewerList, sname, function));
600   PetscFunctionReturn(PETSC_SUCCESS);
601 }
602 
603 /*@C
604   PetscViewerSetFromOptions - Sets various options for a viewer based on values in the options database.
605 
606   Collective
607 
608   Input Parameter:
609 . viewer - the viewer context
610 
611   Level: intermediate
612 
613   Note:
614   Must be called after `PetscViewerCreate()` but before the `PetscViewer` is used.
615 
616 .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerCreate()`, `PetscViewerSetType()`, `PetscViewerType`
617 @*/
618 PetscErrorCode PetscViewerSetFromOptions(PetscViewer viewer)
619 {
620   char      vtype[256];
621   PetscBool flg;
622 
623   PetscFunctionBegin;
624   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
625 
626   if (!PetscViewerList) PetscCall(PetscViewerRegisterAll());
627   PetscObjectOptionsBegin((PetscObject)viewer);
628   PetscCall(PetscOptionsFList("-viewer_type", "Type of PetscViewer", "None", PetscViewerList, (char *)(((PetscObject)viewer)->type_name ? ((PetscObject)viewer)->type_name : PETSCVIEWERASCII), vtype, 256, &flg));
629   if (flg) PetscCall(PetscViewerSetType(viewer, vtype));
630   /* type has not been set? */
631   if (!((PetscObject)viewer)->type_name) PetscCall(PetscViewerSetType(viewer, PETSCVIEWERASCII));
632   PetscTryTypeMethod(viewer, setfromoptions, PetscOptionsObject);
633 
634   /* process any options handlers added with PetscObjectAddOptionsHandler() */
635   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)viewer, PetscOptionsObject));
636   PetscCall(PetscViewerViewFromOptions(viewer, NULL, "-viewer_view"));
637   PetscOptionsEnd();
638   PetscFunctionReturn(PETSC_SUCCESS);
639 }
640 
641 PetscErrorCode PetscViewerFlowControlStart(PetscViewer viewer, PetscInt *mcnt, PetscInt *cnt)
642 {
643   PetscFunctionBegin;
644   PetscCall(PetscViewerBinaryGetFlowControl(viewer, mcnt));
645   PetscCall(PetscViewerBinaryGetFlowControl(viewer, cnt));
646   PetscFunctionReturn(PETSC_SUCCESS);
647 }
648 
649 PetscErrorCode PetscViewerFlowControlStepMain(PetscViewer viewer, PetscInt i, PetscInt *mcnt, PetscInt cnt)
650 {
651   MPI_Comm comm;
652 
653   PetscFunctionBegin;
654   PetscCall(PetscObjectGetComm((PetscObject)viewer, &comm));
655   if (i >= *mcnt) {
656     *mcnt += cnt;
657     PetscCallMPI(MPI_Bcast(mcnt, 1, MPIU_INT, 0, comm));
658   }
659   PetscFunctionReturn(PETSC_SUCCESS);
660 }
661 
662 PetscErrorCode PetscViewerFlowControlEndMain(PetscViewer viewer, PetscInt *mcnt)
663 {
664   MPI_Comm comm;
665   PetscFunctionBegin;
666   PetscCall(PetscObjectGetComm((PetscObject)viewer, &comm));
667   *mcnt = 0;
668   PetscCallMPI(MPI_Bcast(mcnt, 1, MPIU_INT, 0, comm));
669   PetscFunctionReturn(PETSC_SUCCESS);
670 }
671 
672 PetscErrorCode PetscViewerFlowControlStepWorker(PetscViewer viewer, PetscMPIInt rank, PetscInt *mcnt)
673 {
674   MPI_Comm comm;
675   PetscFunctionBegin;
676   PetscCall(PetscObjectGetComm((PetscObject)viewer, &comm));
677   while (PETSC_TRUE) {
678     if (rank < *mcnt) break;
679     PetscCallMPI(MPI_Bcast(mcnt, 1, MPIU_INT, 0, comm));
680   }
681   PetscFunctionReturn(PETSC_SUCCESS);
682 }
683 
684 PetscErrorCode PetscViewerFlowControlEndWorker(PetscViewer viewer, PetscInt *mcnt)
685 {
686   MPI_Comm comm;
687   PetscFunctionBegin;
688   PetscCall(PetscObjectGetComm((PetscObject)viewer, &comm));
689   while (PETSC_TRUE) {
690     PetscCallMPI(MPI_Bcast(mcnt, 1, MPIU_INT, 0, comm));
691     if (!*mcnt) break;
692   }
693   PetscFunctionReturn(PETSC_SUCCESS);
694 }
695