xref: /petsc/src/sys/classes/viewer/interface/viewreg.c (revision 4d86920da9ee67c835173a5dfffa1b3a52fd24ca)
1af0996ceSBarry Smith #include <petsc/private/viewerimpl.h> /*I "petscviewer.h" I*/
2e8f14785SLisandro Dalcin #include <petsc/private/hashtable.h>
3e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
4e04113cfSBarry Smith   #include <petscviewersaws.h>
5bfb97211SBarry Smith #endif
65c6c1daeSBarry Smith 
702c9f0b5SLisandro Dalcin PetscFunctionList PetscViewerList = NULL;
85c6c1daeSBarry Smith 
99de0f6ecSBarry Smith PetscOptionsHelpPrinted PetscOptionsHelpPrintedSingleton = NULL;
109de0f6ecSBarry Smith KHASH_SET_INIT_STR(HTPrinted)
119de0f6ecSBarry Smith struct _n_PetscOptionsHelpPrinted {
129de0f6ecSBarry Smith   khash_t(HTPrinted) *printed;
139de0f6ecSBarry Smith   PetscSegBuffer      strings;
149de0f6ecSBarry Smith };
159de0f6ecSBarry Smith 
16d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscOptionsHelpPrintedDestroy(PetscOptionsHelpPrinted *hp)
17d71ae5a4SJacob Faibussowitsch {
189de0f6ecSBarry Smith   PetscFunctionBegin;
193ba16761SJacob Faibussowitsch   if (!*hp) PetscFunctionReturn(PETSC_SUCCESS);
209de0f6ecSBarry Smith   kh_destroy(HTPrinted, (*hp)->printed);
219566063dSJacob Faibussowitsch   PetscCall(PetscSegBufferDestroy(&(*hp)->strings));
229566063dSJacob Faibussowitsch   PetscCall(PetscFree(*hp));
233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
249de0f6ecSBarry Smith }
259de0f6ecSBarry Smith 
269de0f6ecSBarry Smith /*@C
279de0f6ecSBarry Smith   PetscOptionsHelpPrintedCreate - Creates an object used to manage tracking which help messages have
289de0f6ecSBarry Smith   been printed so they will not be printed again.
299de0f6ecSBarry Smith 
3010450e9eSJacob Faibussowitsch   Output Parameter:
3110450e9eSJacob Faibussowitsch . hp - the created object
3210450e9eSJacob Faibussowitsch 
3320f4b53cSBarry Smith   Not Collective
349de0f6ecSBarry Smith 
359de0f6ecSBarry Smith   Level: developer
369de0f6ecSBarry Smith 
37db781477SPatrick Sanan .seealso: `PetscOptionsHelpPrintedCheck()`, `PetscOptionsHelpPrintChecked()`
389de0f6ecSBarry Smith @*/
39d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscOptionsHelpPrintedCreate(PetscOptionsHelpPrinted *hp)
40d71ae5a4SJacob Faibussowitsch {
419de0f6ecSBarry Smith   PetscFunctionBegin;
429566063dSJacob Faibussowitsch   PetscCall(PetscNew(hp));
439de0f6ecSBarry Smith   (*hp)->printed = kh_init(HTPrinted);
449566063dSJacob Faibussowitsch   PetscCall(PetscSegBufferCreate(sizeof(char), 10000, &(*hp)->strings));
453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
469de0f6ecSBarry Smith }
479de0f6ecSBarry Smith 
489de0f6ecSBarry Smith /*@C
499de0f6ecSBarry Smith   PetscOptionsHelpPrintedCheck - Checks if a particular pre, name pair has previous been entered (meaning the help message was printed)
509de0f6ecSBarry Smith 
5120f4b53cSBarry Smith   Not Collective
529de0f6ecSBarry Smith 
539de0f6ecSBarry Smith   Input Parameters:
549de0f6ecSBarry Smith + hp   - the object used to manage tracking what help messages have been printed
553f423023SBarry Smith . pre  - the prefix part of the string, many be `NULL`
563f423023SBarry Smith - name - the string to look for (cannot be `NULL`)
579de0f6ecSBarry Smith 
589de0f6ecSBarry Smith   Output Parameter:
593f423023SBarry Smith . found - `PETSC_TRUE` if the string was already set
609de0f6ecSBarry Smith 
619de0f6ecSBarry Smith   Level: intermediate
629de0f6ecSBarry Smith 
63db781477SPatrick Sanan .seealso: `PetscOptionsHelpPrintedCreate()`
649de0f6ecSBarry Smith @*/
65d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscOptionsHelpPrintedCheck(PetscOptionsHelpPrinted hp, const char *pre, const char *name, PetscBool *found)
66d71ae5a4SJacob Faibussowitsch {
679de0f6ecSBarry Smith   size_t l1, l2;
68c1449d8eSBarry Smith #if !defined(PETSC_HAVE_THREADSAFETY)
699de0f6ecSBarry Smith   char *both;
70e8f14785SLisandro Dalcin   int   newitem;
71c1449d8eSBarry Smith #endif
729de0f6ecSBarry Smith 
739de0f6ecSBarry Smith   PetscFunctionBegin;
749566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(pre, &l1));
759566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(name, &l2));
769de0f6ecSBarry Smith   if (l1 + l2 == 0) {
779de0f6ecSBarry Smith     *found = PETSC_FALSE;
783ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
799de0f6ecSBarry Smith   }
80c1449d8eSBarry Smith #if !defined(PETSC_HAVE_THREADSAFETY)
81c6a7a370SJeremy L Thompson   size_t lboth = l1 + l2 + 1;
82c6a7a370SJeremy L Thompson   PetscCall(PetscSegBufferGet(hp->strings, lboth, &both));
83c6a7a370SJeremy L Thompson   PetscCall(PetscStrncpy(both, pre, lboth));
84c6a7a370SJeremy L Thompson   PetscCall(PetscStrncpy(both + l1, name, l2 + 1));
859de0f6ecSBarry Smith   kh_put(HTPrinted, hp->printed, both, &newitem);
86c6a7a370SJeremy L Thompson   if (!newitem) PetscCall(PetscSegBufferUnuse(hp->strings, lboth));
879de0f6ecSBarry Smith   *found = newitem ? PETSC_FALSE : PETSC_TRUE;
88c1449d8eSBarry Smith #else
89c1449d8eSBarry Smith   *found = PETSC_FALSE;
90c1449d8eSBarry Smith #endif
913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9294d6a431SBarry Smith }
9394d6a431SBarry Smith 
94eb55bdffSLawrence Mitchell static PetscBool noviewer = PETSC_FALSE;
95eb55bdffSLawrence Mitchell static PetscBool noviewers[PETSCVIEWERGETVIEWEROFFPUSHESMAX];
96eb55bdffSLawrence Mitchell static PetscInt  inoviewers = 0;
97eb55bdffSLawrence Mitchell 
98eb55bdffSLawrence Mitchell /*@
99811af0c4SBarry Smith   PetscOptionsPushGetViewerOff - sets if a `PetscOptionsGetViewer()` returns a viewer.
100eb55bdffSLawrence Mitchell 
101eb55bdffSLawrence Mitchell   Logically Collective
102eb55bdffSLawrence Mitchell 
103eb55bdffSLawrence Mitchell   Input Parameter:
104811af0c4SBarry Smith . flg - `PETSC_TRUE` to turn off viewer creation, `PETSC_FALSE` to turn it on.
105eb55bdffSLawrence Mitchell 
106eb55bdffSLawrence Mitchell   Level: developer
107eb55bdffSLawrence Mitchell 
108811af0c4SBarry Smith   Note:
109c410d8ccSBarry Smith   Calling `XXXViewFromOptions` in an inner loop can be expensive.  This can appear, for example, when using
110c410d8ccSBarry Smith   many small subsolves.  Call this function to control viewer creation in `PetscOptionsGetViewer()`, thus removing the expensive `XXXViewFromOptions` calls.
111c410d8ccSBarry Smith 
11210450e9eSJacob Faibussowitsch   Developer Notes:
113c410d8ccSBarry Smith   Instead of using this approach, the calls to `PetscOptionsGetViewer()` can be moved into `XXXSetFromOptions()`
114eb55bdffSLawrence Mitchell 
115d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscOptionsGetViewer()`, `PetscOptionsPopGetViewerOff()`
116eb55bdffSLawrence Mitchell @*/
117d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscOptionsPushGetViewerOff(PetscBool flg)
118d71ae5a4SJacob Faibussowitsch {
119eb55bdffSLawrence Mitchell   PetscFunctionBegin;
120cc73adaaSBarry Smith   PetscCheck(inoviewers < PETSCVIEWERGETVIEWEROFFPUSHESMAX, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many PetscOptionsPushGetViewerOff(), perhaps you forgot PetscOptionsPopGetViewerOff()?");
121eb55bdffSLawrence Mitchell 
122eb55bdffSLawrence Mitchell   noviewers[inoviewers++] = noviewer;
123eb55bdffSLawrence Mitchell   noviewer                = flg;
1243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
125eb55bdffSLawrence Mitchell }
126eb55bdffSLawrence Mitchell 
127eb55bdffSLawrence Mitchell /*@
128811af0c4SBarry Smith   PetscOptionsPopGetViewerOff - reset whether `PetscOptionsGetViewer()` returns a viewer.
129eb55bdffSLawrence Mitchell 
130eb55bdffSLawrence Mitchell   Logically Collective
131eb55bdffSLawrence Mitchell 
132eb55bdffSLawrence Mitchell   Level: developer
133eb55bdffSLawrence Mitchell 
134811af0c4SBarry Smith   Note:
135c410d8ccSBarry Smith   See `PetscOptionsPushGetViewerOff()`
136eb55bdffSLawrence Mitchell 
137d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscOptionsGetViewer()`, `PetscOptionsPushGetViewerOff()`
138eb55bdffSLawrence Mitchell @*/
139d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscOptionsPopGetViewerOff(void)
140d71ae5a4SJacob Faibussowitsch {
141eb55bdffSLawrence Mitchell   PetscFunctionBegin;
14228b400f6SJacob Faibussowitsch   PetscCheck(inoviewers, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many PetscOptionsPopGetViewerOff(), perhaps you forgot PetscOptionsPushGetViewerOff()?");
143eb55bdffSLawrence Mitchell   noviewer = noviewers[--inoviewers];
1443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
145eb55bdffSLawrence Mitchell }
146eb55bdffSLawrence Mitchell 
147eb55bdffSLawrence Mitchell /*@
148811af0c4SBarry Smith   PetscOptionsGetViewerOff - does `PetscOptionsGetViewer()` return a viewer?
149eb55bdffSLawrence Mitchell 
150eb55bdffSLawrence Mitchell   Logically Collective
151eb55bdffSLawrence Mitchell 
152eb55bdffSLawrence Mitchell   Output Parameter:
153eb55bdffSLawrence Mitchell . flg - whether viewers are returned.
154eb55bdffSLawrence Mitchell 
155eb55bdffSLawrence Mitchell   Level: developer
156eb55bdffSLawrence Mitchell 
157811af0c4SBarry Smith   Note:
158c410d8ccSBarry Smith   See `PetscOptionsPushGetViewerOff()`
159eb55bdffSLawrence Mitchell 
160d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscOptionsGetViewer()`, `PetscOptionsPushGetViewerOff()`, `PetscOptionsPopGetViewerOff()`
161eb55bdffSLawrence Mitchell @*/
162d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscOptionsGetViewerOff(PetscBool *flg)
163d71ae5a4SJacob Faibussowitsch {
164eb55bdffSLawrence Mitchell   PetscFunctionBegin;
1654f572ea9SToby Isaac   PetscAssertPointer(flg, 1);
166eb55bdffSLawrence Mitchell   *flg = noviewer;
1673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
168eb55bdffSLawrence Mitchell }
169eb55bdffSLawrence Mitchell 
17009222b14SToby Isaac static PetscErrorCode PetscOptionsGetViewers_Single(MPI_Comm comm, const char value[], PetscViewer *viewer, PetscViewerFormat *format)
171d71ae5a4SJacob Faibussowitsch {
17209222b14SToby Isaac   char       *loc0_vtype = NULL, *loc1_fname = NULL, *loc2_fmt = NULL, *loc3_fmode = NULL;
17309222b14SToby Isaac   PetscInt    cnt;
17409222b14SToby Isaac   size_t      viewer_string_length;
17509222b14SToby Isaac   const char *viewers[] = {PETSCVIEWERASCII, PETSCVIEWERBINARY, PETSCVIEWERDRAW, PETSCVIEWERSOCKET, PETSCVIEWERMATLAB, PETSCVIEWERSAWS, PETSCVIEWERVTK, PETSCVIEWERHDF5, PETSCVIEWERGLVIS, PETSCVIEWEREXODUSII, NULL};
1762bf49c77SBarry Smith 
1772bf49c77SBarry Smith   PetscFunctionBegin;
17809222b14SToby Isaac   PetscCall(PetscStrlen(value, &viewer_string_length));
17909222b14SToby Isaac   if (!viewer_string_length) {
1806348e711SLisandro Dalcin     if (format) *format = PETSC_VIEWER_DEFAULT;
181cd791dc2SBarry Smith     if (viewer) PetscCall(PetscViewerASCIIGetStdout(comm, viewer));
18209222b14SToby Isaac     PetscFunctionReturn(PETSC_SUCCESS);
18309222b14SToby Isaac   }
1842bf49c77SBarry Smith 
1859566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(value, &loc0_vtype));
1869566063dSJacob Faibussowitsch   PetscCall(PetscStrchr(loc0_vtype, ':', &loc1_fname));
18735d27ee3SJed Brown   if (loc1_fname) {
18835d27ee3SJed Brown     *loc1_fname++ = 0;
1899566063dSJacob Faibussowitsch     PetscCall(PetscStrchr(loc1_fname, ':', &loc2_fmt));
19035d27ee3SJed Brown   }
19135d27ee3SJed Brown   if (loc2_fmt) {
19235d27ee3SJed Brown     *loc2_fmt++ = 0;
1939566063dSJacob Faibussowitsch     PetscCall(PetscStrchr(loc2_fmt, ':', &loc3_fmode));
19435d27ee3SJed Brown   }
19535d27ee3SJed Brown   if (loc3_fmode) *loc3_fmode++ = 0;
1969566063dSJacob Faibussowitsch   PetscCall(PetscStrendswithwhich(*loc0_vtype ? loc0_vtype : "ascii", viewers, &cnt));
19708401ef6SPierre Jolivet   PetscCheck(cnt <= (PetscInt)sizeof(viewers) - 1, comm, PETSC_ERR_ARG_OUTOFRANGE, "Unknown viewer type: %s", loc0_vtype);
198bb1d7374SBarry Smith   if (viewer) {
19935d27ee3SJed Brown     if (!loc1_fname) {
20043b63833SBarry Smith       switch (cnt) {
201d71ae5a4SJacob Faibussowitsch       case 0:
202d71ae5a4SJacob Faibussowitsch         PetscCall(PetscViewerASCIIGetStdout(comm, viewer));
203d71ae5a4SJacob Faibussowitsch         break;
20443b63833SBarry Smith       case 1:
2059566063dSJacob Faibussowitsch         if (!(*viewer = PETSC_VIEWER_BINARY_(comm))) PetscCall(PETSC_ERR_PLIB);
20643b63833SBarry Smith         break;
20743b63833SBarry Smith       case 2:
2089566063dSJacob Faibussowitsch         if (!(*viewer = PETSC_VIEWER_DRAW_(comm))) PetscCall(PETSC_ERR_PLIB);
20943b63833SBarry Smith         break;
210b58ca069SBarry Smith #if defined(PETSC_USE_SOCKET_VIEWER)
21143b63833SBarry Smith       case 3:
2129566063dSJacob Faibussowitsch         if (!(*viewer = PETSC_VIEWER_SOCKET_(comm))) PetscCall(PETSC_ERR_PLIB);
21343b63833SBarry Smith         break;
214b58ca069SBarry Smith #endif
215d1e78c4fSBarry Smith #if defined(PETSC_HAVE_MATLAB)
21643b63833SBarry Smith       case 4:
2179566063dSJacob Faibussowitsch         if (!(*viewer = PETSC_VIEWER_MATLAB_(comm))) PetscCall(PETSC_ERR_PLIB);
21843b63833SBarry Smith         break;
21943b63833SBarry Smith #endif
220e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
221bfb97211SBarry Smith       case 5:
2229566063dSJacob Faibussowitsch         if (!(*viewer = PETSC_VIEWER_SAWS_(comm))) PetscCall(PETSC_ERR_PLIB);
223bfb97211SBarry Smith         break;
224bfb97211SBarry Smith #endif
225a75e6a4aSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
226a75e6a4aSMatthew G. Knepley       case 7:
2279566063dSJacob Faibussowitsch         if (!(*viewer = PETSC_VIEWER_HDF5_(comm))) PetscCall(PETSC_ERR_PLIB);
228a75e6a4aSMatthew G. Knepley         break;
229a75e6a4aSMatthew G. Knepley #endif
2308135c375SStefano Zampini       case 8:
2319566063dSJacob Faibussowitsch         if (!(*viewer = PETSC_VIEWER_GLVIS_(comm))) PetscCall(PETSC_ERR_PLIB);
2328135c375SStefano Zampini         break;
2331e50132fSMatthew G. Knepley #if defined(PETSC_HAVE_EXODUSII)
2341e50132fSMatthew G. Knepley       case 9:
2359566063dSJacob Faibussowitsch         if (!(*viewer = PETSC_VIEWER_EXODUSII_(comm))) PetscCall(PETSC_ERR_PLIB);
2361e50132fSMatthew G. Knepley         break;
2371e50132fSMatthew G. Knepley #endif
238d71ae5a4SJacob Faibussowitsch       default:
23909222b14SToby Isaac         SETERRQ(comm, PETSC_ERR_SUP, "Unsupported viewer %s", loc0_vtype);
2407f677774SBarry Smith       }
2417f677774SBarry Smith     } else {
24235d27ee3SJed Brown       if (loc2_fmt && !*loc1_fname && (cnt == 0)) { /* ASCII format without file name */
2439566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIIGetStdout(comm, viewer));
2447f677774SBarry Smith       } else {
2453550efbcSJed Brown         PetscFileMode fmode;
24609222b14SToby Isaac         PetscBool     flag = PETSC_FALSE;
24709222b14SToby Isaac 
2489566063dSJacob Faibussowitsch         PetscCall(PetscViewerCreate(comm, viewer));
2499566063dSJacob Faibussowitsch         PetscCall(PetscViewerSetType(*viewer, *loc0_vtype ? loc0_vtype : "ascii"));
2503550efbcSJed Brown         fmode = FILE_MODE_WRITE;
2513550efbcSJed Brown         if (loc3_fmode && *loc3_fmode) { /* Has non-empty file mode ("write" or "append") */
2529566063dSJacob Faibussowitsch           PetscCall(PetscEnumFind(PetscFileModes, loc3_fmode, (PetscEnum *)&fmode, &flag));
25328b400f6SJacob Faibussowitsch           PetscCheck(flag, comm, PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown file mode: %s", loc3_fmode);
2547f677774SBarry Smith         }
255acd7d2deSBarry Smith         if (loc2_fmt) {
2560ecfd9fcSLisandro Dalcin           PetscBool tk, im;
2579566063dSJacob Faibussowitsch           PetscCall(PetscStrcmp(loc1_fname, "tikz", &tk));
2589566063dSJacob Faibussowitsch           PetscCall(PetscStrcmp(loc1_fname, "image", &im));
2590ecfd9fcSLisandro Dalcin           if (tk || im) {
2609566063dSJacob Faibussowitsch             PetscCall(PetscViewerDrawSetInfo(*viewer, NULL, loc2_fmt, PETSC_DECIDE, PETSC_DECIDE, PETSC_DECIDE, PETSC_DECIDE));
261acd7d2deSBarry Smith             *loc2_fmt = 0;
262acd7d2deSBarry Smith           }
263acd7d2deSBarry Smith         }
2649566063dSJacob Faibussowitsch         PetscCall(PetscViewerFileSetMode(*viewer, flag ? fmode : FILE_MODE_WRITE));
2659566063dSJacob Faibussowitsch         PetscCall(PetscViewerFileSetName(*viewer, loc1_fname));
2661baa6e33SBarry Smith         if (*loc1_fname) PetscCall(PetscViewerDrawSetDrawType(*viewer, loc1_fname));
2679566063dSJacob Faibussowitsch         PetscCall(PetscViewerSetFromOptions(*viewer));
26805315717SToby Isaac       }
269bb1d7374SBarry Smith     }
27052f76066SLisandro Dalcin   }
2711baa6e33SBarry Smith   if (viewer) PetscCall(PetscViewerSetUp(*viewer));
27235d27ee3SJed Brown   if (loc2_fmt && *loc2_fmt) {
273e156c29bSStefano Zampini     PetscViewerFormat tfmt;
27409222b14SToby Isaac     PetscBool         flag;
275d6acdc46SStefano Zampini 
2769566063dSJacob Faibussowitsch     PetscCall(PetscEnumFind(PetscViewerFormats, loc2_fmt, (PetscEnum *)&tfmt, &flag));
277d6acdc46SStefano Zampini     if (format) *format = tfmt;
2786adde796SStefano Zampini     PetscCheck(flag, comm, PETSC_ERR_SUP, "Unknown viewer format %s", loc2_fmt);
279d6acdc46SStefano Zampini   } else if (viewer && (cnt == 6) && format) { /* Get format from VTK viewer */
2809566063dSJacob Faibussowitsch     PetscCall(PetscViewerGetFormat(*viewer, format));
2817f677774SBarry Smith   }
2829566063dSJacob Faibussowitsch   PetscCall(PetscFree(loc0_vtype));
28309222b14SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
28409222b14SToby Isaac }
28509222b14SToby Isaac 
28609222b14SToby Isaac 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)
28709222b14SToby Isaac {
28809222b14SToby Isaac   const char *value;
28909222b14SToby Isaac   PetscBool   flag, hashelp;
29009222b14SToby Isaac   PetscInt    n_max;
29109222b14SToby Isaac 
29209222b14SToby Isaac   PetscFunctionBegin;
29309222b14SToby Isaac   PetscAssertPointer(name, 4);
29409222b14SToby Isaac   PetscAssertPointer(n_max_p, 5);
29509222b14SToby Isaac   n_max = *n_max_p;
29609222b14SToby Isaac   PetscCheck(n_max >= 0, comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid size %" PetscInt_FMT " of passed arrays", *n_max_p);
29709222b14SToby Isaac   *n_max_p = 0;
29809222b14SToby Isaac 
29909222b14SToby Isaac   if (set) *set = PETSC_FALSE;
30009222b14SToby Isaac   PetscCall(PetscOptionsGetViewerOff(&flag));
30109222b14SToby Isaac   if (flag) PetscFunctionReturn(PETSC_SUCCESS);
30209222b14SToby Isaac 
30309222b14SToby Isaac   PetscCall(PetscOptionsHasHelp(NULL, &hashelp));
30409222b14SToby Isaac   if (hashelp) {
30509222b14SToby Isaac     PetscBool found;
30609222b14SToby Isaac 
30709222b14SToby Isaac     if (!PetscOptionsHelpPrintedSingleton) PetscCall(PetscOptionsHelpPrintedCreate(&PetscOptionsHelpPrintedSingleton));
30809222b14SToby Isaac     PetscCall(PetscOptionsHelpPrintedCheck(PetscOptionsHelpPrintedSingleton, pre, name, &found));
30909222b14SToby Isaac     if (!found && viewer) {
31009222b14SToby Isaac       PetscCall((*PetscHelpPrintf)(comm, "----------------------------------------\nViewer (-%s%s) options:\n", pre ? pre : "", name + 1));
31109222b14SToby Isaac       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));
31209222b14SToby Isaac       PetscCall((*PetscHelpPrintf)(comm, "  -%s%s binary[:[filename][:[format][:append]]]: %s (%s)\n", pre ? pre : "", name + 1, "Saves object to a binary file", func_name));
31309222b14SToby Isaac       PetscCall((*PetscHelpPrintf)(comm, "  -%s%s draw[:[drawtype][:filename|format]] %s (%s)\n", pre ? pre : "", name + 1, "Draws object", func_name));
31409222b14SToby Isaac       PetscCall((*PetscHelpPrintf)(comm, "  -%s%s socket[:port]: %s (%s)\n", pre ? pre : "", name + 1, "Pushes object to a Unix socket", func_name));
31509222b14SToby Isaac       PetscCall((*PetscHelpPrintf)(comm, "  -%s%s saws[:communicatorname]: %s (%s)\n", pre ? pre : "", name + 1, "Publishes object to SAWs", func_name));
31609222b14SToby Isaac       if (allow_multiple) PetscCall((*PetscHelpPrintf)(comm, "  -%s%s v1[,v2,...]: %s (%s)\n", pre ? pre : "", name + 1, "Multiple viewers", func_name));
3172bf49c77SBarry Smith     }
3182bf49c77SBarry Smith   }
31909222b14SToby Isaac 
32009222b14SToby Isaac   PetscCall(PetscOptionsFindPair(options, pre, name, &value, &flag));
32109222b14SToby Isaac   if (flag) {
32209222b14SToby Isaac     if (set) *set = PETSC_TRUE;
32309222b14SToby Isaac     if (!value) {
32409222b14SToby Isaac       PetscCheck(n_max > 0, comm, PETSC_ERR_ARG_SIZ, "More viewers (1) than max available (0)");
32509222b14SToby Isaac       if (format) *format = PETSC_VIEWER_DEFAULT;
326cd791dc2SBarry Smith       if (viewer) PetscCall(PetscViewerASCIIGetStdout(comm, viewer));
32709222b14SToby Isaac       *n_max_p = 1;
32809222b14SToby Isaac     } else {
32909222b14SToby Isaac       char  *loc0_viewer_string = NULL, *this_viewer_string = NULL;
33009222b14SToby Isaac       size_t viewer_string_length;
33109222b14SToby Isaac 
33209222b14SToby Isaac       PetscCall(PetscStrallocpy(value, &loc0_viewer_string));
33309222b14SToby Isaac       PetscCall(PetscStrlen(loc0_viewer_string, &viewer_string_length));
33409222b14SToby Isaac       this_viewer_string = loc0_viewer_string;
33509222b14SToby Isaac 
33609222b14SToby Isaac       do {
33709222b14SToby Isaac         PetscViewer       *this_viewer;
33809222b14SToby Isaac         PetscViewerFormat *this_viewer_format;
33909222b14SToby Isaac         char              *next_viewer_string = NULL;
34009222b14SToby Isaac         char              *comma_separator    = NULL;
34109222b14SToby Isaac         PetscInt           n                  = *n_max_p;
34209222b14SToby Isaac 
34309222b14SToby Isaac         PetscCheck(n < n_max, comm, PETSC_ERR_PLIB, "More viewers than max available (%d)", (int)n_max);
34409222b14SToby Isaac 
34509222b14SToby Isaac         PetscCall(PetscStrchr(this_viewer_string, ',', &comma_separator));
34609222b14SToby Isaac         if (comma_separator) {
34709222b14SToby Isaac           PetscCheck(allow_multiple, comm, PETSC_ERR_ARG_OUTOFRANGE, "Trying to pass multiple viewers to %s: only one allowed.  Use PetscOptionsGetViewers() instead", func_name);
34809222b14SToby Isaac           *comma_separator   = 0;
34909222b14SToby Isaac           next_viewer_string = comma_separator + 1;
35009222b14SToby Isaac         }
3518e3a54c0SPierre Jolivet         this_viewer = PetscSafePointerPlusOffset(viewer, n);
35209222b14SToby Isaac         if (this_viewer) *this_viewer = NULL;
3538e3a54c0SPierre Jolivet         this_viewer_format = PetscSafePointerPlusOffset(format, n);
35409222b14SToby Isaac         if (this_viewer_format) *this_viewer_format = PETSC_VIEWER_DEFAULT;
35509222b14SToby Isaac         PetscCall(PetscOptionsGetViewers_Single(comm, this_viewer_string, this_viewer, this_viewer_format));
35609222b14SToby Isaac         this_viewer_string = next_viewer_string;
35709222b14SToby Isaac         (*n_max_p)++;
35809222b14SToby Isaac       } while (this_viewer_string);
35909222b14SToby Isaac       PetscCall(PetscFree(loc0_viewer_string));
36009222b14SToby Isaac     }
36109222b14SToby Isaac   }
36209222b14SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
36309222b14SToby Isaac }
36409222b14SToby Isaac 
36509222b14SToby Isaac /*@C
36609222b14SToby Isaac   PetscOptionsGetViewer - Gets a viewer appropriate for the type indicated by the user
36709222b14SToby Isaac 
36809222b14SToby Isaac   Collective
36909222b14SToby Isaac 
37009222b14SToby Isaac   Input Parameters:
37109222b14SToby Isaac + comm    - the communicator to own the viewer
37209222b14SToby Isaac . options - options database, use `NULL` for default global database
37309222b14SToby Isaac . pre     - the string to prepend to the name or `NULL`
374cd791dc2SBarry Smith - name    - the options database name that will be checked for
37509222b14SToby Isaac 
37609222b14SToby Isaac   Output Parameters:
37709222b14SToby Isaac + viewer - the viewer, pass `NULL` if not needed
37809222b14SToby Isaac . format - the `PetscViewerFormat` requested by the user, pass `NULL` if not needed
37909222b14SToby Isaac - set    - `PETSC_TRUE` if found, else `PETSC_FALSE`
38009222b14SToby Isaac 
38109222b14SToby Isaac   Level: intermediate
38209222b14SToby Isaac 
38309222b14SToby Isaac   Notes:
38409222b14SToby Isaac   If no value is provided ascii:stdout is used
38509222b14SToby Isaac +       ascii[:[filename][:[format][:append]]]  -  defaults to stdout - format can be one of ascii_info, ascii_info_detail, or ascii_matlab,
38609222b14SToby Isaac   for example ascii::ascii_info prints just the information about the object not all details
38709222b14SToby Isaac   unless :append is given filename opens in write mode, overwriting what was already there
38809222b14SToby Isaac .       binary[:[filename][:[format][:append]]] -  defaults to the file binaryoutput
38909222b14SToby Isaac .       draw[:drawtype[:filename]]              -  for example, draw:tikz, draw:tikz:figure.tex  or draw:x
39009222b14SToby Isaac .       socket[:port]                           -  defaults to the standard output port
39109222b14SToby Isaac -       saws[:communicatorname]                 -   publishes object to the Scientific Application Webserver (SAWs)
39209222b14SToby Isaac 
393cd791dc2SBarry Smith   Use `PetscOptionsRestoreViewer()` after using the viewer, do not use `PetscViewerDestroy()`
39409222b14SToby Isaac 
39509222b14SToby Isaac   You can control whether calls to this function create a viewer (or return early with *set of `PETSC_FALSE`) with
39609222b14SToby Isaac   `PetscOptionsPushGetViewerOff()`.  This is useful if calling many small subsolves, in which case XXXViewFromOptions can take
39709222b14SToby Isaac   an appreciable fraction of the runtime.
39809222b14SToby Isaac 
39909222b14SToby Isaac   If PETSc is configured with `--with-viewfromoptions=0` this function always returns with *set of `PETSC_FALSE`
40009222b14SToby Isaac 
401cd791dc2SBarry Smith   This routine is thread-safe for accessing predefined `PetscViewer`s like `PETSC_VIEWER_STDOUT_SELF` but not for accessing
402cd791dc2SBarry Smith   files by name.
403cd791dc2SBarry Smith 
404cd791dc2SBarry Smith .seealso: [](sec_viewers), `PetscOptionsRestoreViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
40509222b14SToby Isaac           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
40609222b14SToby Isaac           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`,
40709222b14SToby Isaac           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
40809222b14SToby Isaac           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
40909222b14SToby Isaac           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
41009222b14SToby Isaac           `PetscOptionsFList()`, `PetscOptionsEList()`, `PetscOptionsPushGetViewerOff()`, `PetscOptionsPopGetViewerOff()`,
41109222b14SToby Isaac           `PetscOptionsGetViewerOff()`
41209222b14SToby Isaac @*/
41309222b14SToby Isaac PetscErrorCode PetscOptionsGetViewer(MPI_Comm comm, PetscOptions options, const char pre[], const char name[], PetscViewer *viewer, PetscViewerFormat *format, PetscBool *set)
41409222b14SToby Isaac {
41509222b14SToby Isaac   PetscInt  n_max = 1;
41609222b14SToby Isaac   PetscBool set_internal;
41709222b14SToby Isaac 
41809222b14SToby Isaac   PetscFunctionBegin;
41909222b14SToby Isaac   if (viewer) *viewer = NULL;
42009222b14SToby Isaac   if (format) *format = PETSC_VIEWER_DEFAULT;
42109222b14SToby Isaac   PetscCall(PetscOptionsGetViewers_Internal(comm, options, pre, name, &n_max, viewer, format, &set_internal, PETSC_FUNCTION_NAME, PETSC_FALSE));
42209222b14SToby Isaac   if (set_internal) PetscAssert(n_max == 1, comm, PETSC_ERR_PLIB, "Unexpected: %d != 1 viewers set", (int)n_max);
42309222b14SToby Isaac   if (set) *set = set_internal;
42409222b14SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
42509222b14SToby Isaac }
42609222b14SToby Isaac 
42709222b14SToby Isaac /*@C
428cd791dc2SBarry Smith   PetscOptionsRestoreViewer - restore a `PetscViewer` obtained from `PetscOptionsGetViewer()` or `PetscOptionsGetViewers()`
429cd791dc2SBarry Smith 
430cd791dc2SBarry Smith   Collective
431cd791dc2SBarry Smith 
432cd791dc2SBarry Smith   Input Parameter:
433cd791dc2SBarry Smith . viewer - the viewer
434cd791dc2SBarry Smith 
435cd791dc2SBarry Smith   Level: intermediate
436cd791dc2SBarry Smith 
437cd791dc2SBarry Smith   Developer Note:
438cd791dc2SBarry Smith   This function exists to handle persistent viewers such as `PETSC_VIEWER_STDOUT_SELF` that may be shared between threads where
439cd791dc2SBarry Smith   changing the reference count would not be thread safe.
440cd791dc2SBarry Smith 
441cd791dc2SBarry Smith .seealso: [](sec_viewers), `PetscOptionsGetViewer()`, `PetscOptionsGetReal()`, `PetscOptionsHasName()`, `PetscOptionsGetString()`,
442cd791dc2SBarry Smith           `PetscOptionsGetIntArray()`, `PetscOptionsGetRealArray()`, `PetscOptionsBool()`
443cd791dc2SBarry Smith           `PetscOptionsInt()`, `PetscOptionsString()`, `PetscOptionsReal()`,
444cd791dc2SBarry Smith           `PetscOptionsName()`, `PetscOptionsBegin()`, `PetscOptionsEnd()`, `PetscOptionsHeadBegin()`,
445cd791dc2SBarry Smith           `PetscOptionsStringArray()`, `PetscOptionsRealArray()`, `PetscOptionsScalar()`,
446cd791dc2SBarry Smith           `PetscOptionsBoolGroupBegin()`, `PetscOptionsBoolGroup()`, `PetscOptionsBoolGroupEnd()`,
447cd791dc2SBarry Smith           `PetscOptionsFList()`, `PetscOptionsEList()`, `PetscOptionsPushGetViewerOff()`, `PetscOptionsPopGetViewerOff()`,
448cd791dc2SBarry Smith           `PetscOptionsGetViewerOff()`
449cd791dc2SBarry Smith @*/
450cd791dc2SBarry Smith PetscErrorCode PetscOptionsRestoreViewer(PetscViewer *viewer)
451cd791dc2SBarry Smith {
452cd791dc2SBarry Smith   PetscFunctionBegin;
453cd791dc2SBarry Smith   if (!*viewer) PetscFunctionReturn(PETSC_SUCCESS);
454cd791dc2SBarry Smith   if (!((PetscObject)*viewer)->persistent) PetscCall(PetscViewerDestroy(viewer));
455cd791dc2SBarry Smith   else *viewer = NULL;
456cd791dc2SBarry Smith   PetscFunctionReturn(PETSC_SUCCESS);
457cd791dc2SBarry Smith }
458cd791dc2SBarry Smith 
459cd791dc2SBarry Smith /*@C
46009222b14SToby Isaac   PetscOptionsGetViewers - Get multiple viewers from a comma-separated list in the options database
46109222b14SToby Isaac 
46209222b14SToby Isaac   Collective
46309222b14SToby Isaac 
46409222b14SToby Isaac   Input Parameters:
465cd791dc2SBarry Smith + comm    - the communicator to own the viewers
46609222b14SToby Isaac . options - options database, use `NULL` for default global database
46709222b14SToby Isaac . pre     - the string to prepend to the name or `NULL`
468cd791dc2SBarry Smith . name    - the options database name that will be checked for
46909222b14SToby Isaac - n_max   - on input: the maximum number of viewers; on output: the number of viewers in the comma-separated list
47009222b14SToby Isaac 
47109222b14SToby Isaac   Output Parameters:
47209222b14SToby Isaac + viewers - an array to hold at least `n_max` `PetscViewer`s, or `NULL` if not needed; on output: if not `NULL`, the
47309222b14SToby Isaac              first `n_max` entries are initialized `PetscViewer`s
47409222b14SToby Isaac . formats - an array to hold at least `n_max` `PetscViewerFormat`s, or `NULL` if not needed; on output: if not
47509222b14SToby Isaac              `NULL`, the first `n_max` entries are valid `PetscViewewFormat`s
47609222b14SToby Isaac - set     - `PETSC_TRUE` if found, else `PETSC_FALSE`
47709222b14SToby Isaac 
47809222b14SToby Isaac   Level: intermediate
47909222b14SToby Isaac 
48009222b14SToby Isaac   Note:
481cd791dc2SBarry Smith   See `PetscOptionsGetViewer()` for how the format strings for the viewers are interpreted.  Use `PetscOptionsRestoreViewer()` on each viewer, otherwise a memory leak will occur.
48209222b14SToby Isaac 
48309222b14SToby Isaac   If PETSc is configured with `--with-viewfromoptions=0` this function always returns with `n_max` of 0 and `set` of `PETSC_FALSE`
48409222b14SToby Isaac 
48509222b14SToby Isaac .seealso: [](sec_viewers), `PetscOptionsGetViewer()`
48609222b14SToby Isaac @*/
48709222b14SToby Isaac PetscErrorCode PetscOptionsGetViewers(MPI_Comm comm, PetscOptions options, const char pre[], const char name[], PetscInt *n_max, PetscViewer viewers[], PetscViewerFormat formats[], PetscBool *set)
48809222b14SToby Isaac {
48909222b14SToby Isaac   PetscFunctionBegin;
49009222b14SToby Isaac   PetscCall(PetscOptionsGetViewers_Internal(comm, options, pre, name, n_max, viewers, formats, set, PETSC_FUNCTION_NAME, PETSC_TRUE));
4913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4922bf49c77SBarry Smith }
4932bf49c77SBarry Smith 
4945c6c1daeSBarry Smith /*@
495c410d8ccSBarry Smith   PetscViewerCreate - Creates a viewing context. A `PetscViewer` represents a file, a graphical window, a Unix socket or a variety of other ways
496c410d8ccSBarry Smith   of viewing a PETSc object
4975c6c1daeSBarry Smith 
498d083f849SBarry Smith   Collective
4995c6c1daeSBarry Smith 
5005c6c1daeSBarry Smith   Input Parameter:
5015c6c1daeSBarry Smith . comm - MPI communicator
5025c6c1daeSBarry Smith 
5035c6c1daeSBarry Smith   Output Parameter:
504811af0c4SBarry Smith . inviewer - location to put the `PetscViewer` context
5055c6c1daeSBarry Smith 
5065c6c1daeSBarry Smith   Level: advanced
5075c6c1daeSBarry Smith 
508d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerDestroy()`, `PetscViewerSetType()`, `PetscViewerType`
5095c6c1daeSBarry Smith @*/
510d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerCreate(MPI_Comm comm, PetscViewer *inviewer)
511d71ae5a4SJacob Faibussowitsch {
5125c6c1daeSBarry Smith   PetscViewer viewer;
5135c6c1daeSBarry Smith 
5145c6c1daeSBarry Smith   PetscFunctionBegin;
51502c9f0b5SLisandro Dalcin   *inviewer = NULL;
5169566063dSJacob Faibussowitsch   PetscCall(PetscViewerInitializePackage());
5179566063dSJacob Faibussowitsch   PetscCall(PetscHeaderCreate(viewer, PETSC_VIEWER_CLASSID, "PetscViewer", "PetscViewer", "Viewer", comm, PetscViewerDestroy, PetscViewerView));
5185c6c1daeSBarry Smith   *inviewer    = viewer;
51902c9f0b5SLisandro Dalcin   viewer->data = NULL;
5203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5215c6c1daeSBarry Smith }
5225c6c1daeSBarry Smith 
5235c6c1daeSBarry Smith /*@C
524811af0c4SBarry Smith   PetscViewerSetType - Builds `PetscViewer` for a particular implementation.
5255c6c1daeSBarry Smith 
526c3339decSBarry Smith   Collective
5275c6c1daeSBarry Smith 
528d8d19677SJose E. Roman   Input Parameters:
529811af0c4SBarry Smith + viewer - the `PetscViewer` context obtained with `PetscViewerCreate()`
530811af0c4SBarry Smith - type   - for example, `PETSCVIEWERASCII`
5315c6c1daeSBarry Smith 
5323c7db156SBarry Smith   Options Database Key:
5333c7db156SBarry Smith . -viewer_type  <type> - Sets the type; use -help for a list of available methods (for instance, ascii)
5345c6c1daeSBarry Smith 
5355c6c1daeSBarry Smith   Level: advanced
5365c6c1daeSBarry Smith 
537811af0c4SBarry Smith   Note:
5383f423023SBarry Smith   See `PetscViewerType` for possible values
5395c6c1daeSBarry Smith 
540d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerCreate()`, `PetscViewerGetType()`, `PetscViewerType`, `PetscViewerPushFormat()`
5415c6c1daeSBarry Smith @*/
542d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerSetType(PetscViewer viewer, PetscViewerType type)
543d71ae5a4SJacob Faibussowitsch {
5445c6c1daeSBarry Smith   PetscBool match;
5455f80ce2aSJacob Faibussowitsch   PetscErrorCode (*r)(PetscViewer);
5465c6c1daeSBarry Smith 
5475c6c1daeSBarry Smith   PetscFunctionBegin;
5485c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
5494f572ea9SToby Isaac   PetscAssertPointer(type, 2);
5509566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, type, &match));
5513ba16761SJacob Faibussowitsch   if (match) PetscFunctionReturn(PETSC_SUCCESS);
5525c6c1daeSBarry Smith 
5535c6c1daeSBarry Smith   /* cleanup any old type that may be there */
554dbbe0bcdSBarry Smith   PetscTryTypeMethod(viewer, destroy);
5550298fd71SBarry Smith   viewer->ops->destroy = NULL;
55602c9f0b5SLisandro Dalcin   viewer->data         = NULL;
557dbbe0bcdSBarry Smith 
5589566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(viewer->ops, sizeof(struct _PetscViewerOps)));
5595c6c1daeSBarry Smith 
5609566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(PetscViewerList, type, &r));
5616adde796SStefano Zampini   PetscCheck(r, PetscObjectComm((PetscObject)viewer), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PetscViewer type given: %s", type);
5625c6c1daeSBarry Smith 
5639566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)viewer, type));
5649566063dSJacob Faibussowitsch   PetscCall((*r)(viewer));
5653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5665c6c1daeSBarry Smith }
5675c6c1daeSBarry Smith 
5681c84c290SBarry Smith /*@C
569c410d8ccSBarry Smith   PetscViewerRegister - Adds a viewer to those available for use with `PetscViewerSetType()`
5701c84c290SBarry Smith 
5711c84c290SBarry Smith   Not Collective
5721c84c290SBarry Smith 
5731c84c290SBarry Smith   Input Parameters:
5742fe279fdSBarry Smith + sname    - name of a new user-defined viewer
5752fe279fdSBarry Smith - function - routine to create method context
5761c84c290SBarry Smith 
5771c84c290SBarry Smith   Level: developer
578811af0c4SBarry Smith 
579811af0c4SBarry Smith   Note:
580811af0c4SBarry Smith   `PetscViewerRegister()` may be called multiple times to add several user-defined viewers.
5811c84c290SBarry Smith 
582aec76313SJacob Faibussowitsch   Example Usage:
5831c84c290SBarry Smith .vb
584bdf89e91SBarry Smith    PetscViewerRegister("my_viewer_type", MyViewerCreate);
5851c84c290SBarry Smith .ve
5861c84c290SBarry Smith 
5871c84c290SBarry Smith   Then, your solver can be chosen with the procedural interface via
5881c84c290SBarry Smith $     PetscViewerSetType(viewer, "my_viewer_type")
5891c84c290SBarry Smith   or at runtime via the option
5901c84c290SBarry Smith $     -viewer_type my_viewer_type
5911c84c290SBarry Smith 
592d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewerRegisterAll()`
5931c84c290SBarry Smith  @*/
594d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerRegister(const char *sname, PetscErrorCode (*function)(PetscViewer))
595d71ae5a4SJacob Faibussowitsch {
5965c6c1daeSBarry Smith   PetscFunctionBegin;
5979566063dSJacob Faibussowitsch   PetscCall(PetscViewerInitializePackage());
5989566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&PetscViewerList, sname, function));
5993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6005c6c1daeSBarry Smith }
6015c6c1daeSBarry Smith 
6025c6c1daeSBarry Smith /*@C
6033f423023SBarry Smith   PetscViewerSetFromOptions - Sets various options for a viewer based on values in the options database.
6045c6c1daeSBarry Smith 
605c3339decSBarry Smith   Collective
6065c6c1daeSBarry Smith 
6075c6c1daeSBarry Smith   Input Parameter:
608811af0c4SBarry Smith . viewer - the viewer context
6095c6c1daeSBarry Smith 
6105c6c1daeSBarry Smith   Level: intermediate
6115c6c1daeSBarry Smith 
612811af0c4SBarry Smith   Note:
613c410d8ccSBarry Smith   Must be called after `PetscViewerCreate()` but before the `PetscViewer` is used.
6145c6c1daeSBarry Smith 
615d1f92df0SBarry Smith .seealso: [](sec_viewers), `PetscViewer`, `PetscViewerCreate()`, `PetscViewerSetType()`, `PetscViewerType`
6165c6c1daeSBarry Smith @*/
617d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerSetFromOptions(PetscViewer viewer)
618d71ae5a4SJacob Faibussowitsch {
6195c6c1daeSBarry Smith   char      vtype[256];
6205c6c1daeSBarry Smith   PetscBool flg;
6215c6c1daeSBarry Smith 
6225c6c1daeSBarry Smith   PetscFunctionBegin;
6235c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 1);
6245c6c1daeSBarry Smith 
62548a46eb9SPierre Jolivet   if (!PetscViewerList) PetscCall(PetscViewerRegisterAll());
626d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)viewer);
6279566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-viewer_type", "Type of PetscViewer", "None", PetscViewerList, (char *)(((PetscObject)viewer)->type_name ? ((PetscObject)viewer)->type_name : PETSCVIEWERASCII), vtype, 256, &flg));
6281baa6e33SBarry Smith   if (flg) PetscCall(PetscViewerSetType(viewer, vtype));
6295c6c1daeSBarry Smith   /* type has not been set? */
63048a46eb9SPierre Jolivet   if (!((PetscObject)viewer)->type_name) PetscCall(PetscViewerSetType(viewer, PETSCVIEWERASCII));
631dbbe0bcdSBarry Smith   PetscTryTypeMethod(viewer, setfromoptions, PetscOptionsObject);
6325c6c1daeSBarry Smith 
6335c6c1daeSBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
634dbbe0bcdSBarry Smith   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)viewer, PetscOptionsObject));
6359566063dSJacob Faibussowitsch   PetscCall(PetscViewerViewFromOptions(viewer, NULL, "-viewer_view"));
636d0609cedSBarry Smith   PetscOptionsEnd();
6373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6385c6c1daeSBarry Smith }
639816f7b76SBarry Smith 
640d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerFlowControlStart(PetscViewer viewer, PetscInt *mcnt, PetscInt *cnt)
641d71ae5a4SJacob Faibussowitsch {
642816f7b76SBarry Smith   PetscFunctionBegin;
6439566063dSJacob Faibussowitsch   PetscCall(PetscViewerBinaryGetFlowControl(viewer, mcnt));
6449566063dSJacob Faibussowitsch   PetscCall(PetscViewerBinaryGetFlowControl(viewer, cnt));
6453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
646816f7b76SBarry Smith }
647816f7b76SBarry Smith 
648d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerFlowControlStepMain(PetscViewer viewer, PetscInt i, PetscInt *mcnt, PetscInt cnt)
649d71ae5a4SJacob Faibussowitsch {
650816f7b76SBarry Smith   MPI_Comm comm;
651816f7b76SBarry Smith 
652816f7b76SBarry Smith   PetscFunctionBegin;
6539566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)viewer, &comm));
654816f7b76SBarry Smith   if (i >= *mcnt) {
655816f7b76SBarry Smith     *mcnt += cnt;
6569566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Bcast(mcnt, 1, MPIU_INT, 0, comm));
657816f7b76SBarry Smith   }
6583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
659816f7b76SBarry Smith }
660816f7b76SBarry Smith 
661d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerFlowControlEndMain(PetscViewer viewer, PetscInt *mcnt)
662d71ae5a4SJacob Faibussowitsch {
663816f7b76SBarry Smith   MPI_Comm comm;
664*4d86920dSPierre Jolivet 
665816f7b76SBarry Smith   PetscFunctionBegin;
6669566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)viewer, &comm));
667816f7b76SBarry Smith   *mcnt = 0;
6689566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Bcast(mcnt, 1, MPIU_INT, 0, comm));
6693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
670816f7b76SBarry Smith }
671816f7b76SBarry Smith 
672d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerFlowControlStepWorker(PetscViewer viewer, PetscMPIInt rank, PetscInt *mcnt)
673d71ae5a4SJacob Faibussowitsch {
674816f7b76SBarry Smith   MPI_Comm comm;
675*4d86920dSPierre Jolivet 
676816f7b76SBarry Smith   PetscFunctionBegin;
6779566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)viewer, &comm));
678816f7b76SBarry Smith   while (PETSC_TRUE) {
679816f7b76SBarry Smith     if (rank < *mcnt) break;
6809566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Bcast(mcnt, 1, MPIU_INT, 0, comm));
681816f7b76SBarry Smith   }
6823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
683816f7b76SBarry Smith }
684816f7b76SBarry Smith 
685d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscViewerFlowControlEndWorker(PetscViewer viewer, PetscInt *mcnt)
686d71ae5a4SJacob Faibussowitsch {
687816f7b76SBarry Smith   MPI_Comm comm;
688*4d86920dSPierre Jolivet 
689816f7b76SBarry Smith   PetscFunctionBegin;
6909566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)viewer, &comm));
691816f7b76SBarry Smith   while (PETSC_TRUE) {
6929566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Bcast(mcnt, 1, MPIU_INT, 0, comm));
693816f7b76SBarry Smith     if (!*mcnt) break;
694816f7b76SBarry Smith   }
6953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
696816f7b76SBarry Smith }
697