Actual source code: view.c
2: #include <petsc/private/viewerimpl.h>
3: #include <petscdraw.h>
5: PetscClassId PETSC_VIEWER_CLASSID;
7: static PetscBool PetscViewerPackageInitialized = PETSC_FALSE;
8: /*@C
9: PetscViewerFinalizePackage - This function destroys any global objects created in the Petsc viewers. It is
10: called from PetscFinalize().
12: Level: developer
14: .seealso: PetscFinalize()
15: @*/
16: PetscErrorCode PetscViewerFinalizePackage(void)
17: {
21: if (Petsc_Viewer_keyval != MPI_KEYVAL_INVALID) {
22: MPI_Comm_free_keyval(&Petsc_Viewer_keyval);
23: }
24: if (Petsc_Viewer_Stdout_keyval != MPI_KEYVAL_INVALID) {
25: MPI_Comm_free_keyval(&Petsc_Viewer_Stdout_keyval);
26: }
27: if (Petsc_Viewer_Stderr_keyval != MPI_KEYVAL_INVALID) {
28: MPI_Comm_free_keyval(&Petsc_Viewer_Stderr_keyval);
29: }
30: if (Petsc_Viewer_Binary_keyval != MPI_KEYVAL_INVALID) {
31: MPI_Comm_free_keyval(&Petsc_Viewer_Binary_keyval);
32: }
33: if (Petsc_Viewer_Draw_keyval != MPI_KEYVAL_INVALID) {
34: MPI_Comm_free_keyval(&Petsc_Viewer_Draw_keyval);
35: }
36: #if defined(PETSC_HAVE_HDF5)
37: if (Petsc_Viewer_HDF5_keyval != MPI_KEYVAL_INVALID) {
38: MPI_Comm_free_keyval(&Petsc_Viewer_HDF5_keyval);
39: }
40: #endif
41: #if defined(PETSC_USE_SOCKETVIEWER)
42: if (Petsc_Viewer_Socket_keyval != MPI_KEYVAL_INVALID) {
43: MPI_Comm_free_keyval(&Petsc_Viewer_Socket_keyval);
44: }
45: #endif
46: PetscFunctionListDestroy(&PetscViewerList);
47: PetscViewerPackageInitialized = PETSC_FALSE;
48: PetscViewerRegisterAllCalled = PETSC_FALSE;
49: return(0);
50: }
52: /*@C
53: PetscViewerInitializePackage - This function initializes everything in the main PetscViewer package.
55: Level: developer
57: .seealso: PetscInitialize()
58: @*/
59: PetscErrorCode PetscViewerInitializePackage(void)
60: {
61: char logList[256];
62: PetscBool opt,pkg;
66: if (PetscViewerPackageInitialized) return(0);
67: PetscViewerPackageInitialized = PETSC_TRUE;
68: /* Register Classes */
69: PetscClassIdRegister("Viewer",&PETSC_VIEWER_CLASSID);
70: /* Register Constructors */
71: PetscViewerRegisterAll();
72: /* Process Info */
73: {
74: PetscClassId classids[1];
76: classids[0] = PETSC_VIEWER_CLASSID;
77: PetscInfoProcessClass("viewer", 1, classids);
78: }
79: /* Process summary exclusions */
80: PetscOptionsGetString(NULL,NULL,"-log_exclude",logList,sizeof(logList),&opt);
81: if (opt) {
82: PetscStrInList("viewer",logList,',',&pkg);
83: if (pkg) {PetscLogEventExcludeClass(PETSC_VIEWER_CLASSID);}
84: }
85: #if defined(PETSC_HAVE_MATHEMATICA)
86: PetscViewerMathematicaInitializePackage();
87: #endif
88: /* Register package finalizer */
89: PetscRegisterFinalize(PetscViewerFinalizePackage);
90: return(0);
91: }
93: /*@
94: PetscViewerDestroy - Destroys a PetscViewer.
96: Collective on PetscViewer
98: Input Parameters:
99: . viewer - the PetscViewer to be destroyed.
101: Level: beginner
103: .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen()
105: @*/
106: PetscErrorCode PetscViewerDestroy(PetscViewer *viewer)
107: {
111: if (!*viewer) return(0);
114: PetscViewerFlush(*viewer);
115: if (--((PetscObject)(*viewer))->refct > 0) {*viewer = NULL; return(0);}
117: PetscObjectSAWsViewOff((PetscObject)*viewer);
118: if ((*viewer)->ops->destroy) {
119: (*(*viewer)->ops->destroy)(*viewer);
120: }
121: PetscHeaderDestroy(viewer);
122: return(0);
123: }
125: /*@C
126: PetscViewerAndFormatCreate - Creates a PetscViewerAndFormat struct.
128: Collective on PetscViewer
130: Input Parameters:
131: + viewer - the viewer
132: - format - the format
134: Output Parameter:
135: . vf - viewer and format object
137: Notes:
138: This increases the reference count of the viewer so you can destroy the viewer object after this call
139: Level: developer
141: This is used as the context variable for many of the TS, SNES, and KSP monitor functions
143: .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen(), PetscViewerAndFormatDestroy()
145: @*/
146: PetscErrorCode PetscViewerAndFormatCreate(PetscViewer viewer, PetscViewerFormat format, PetscViewerAndFormat **vf)
147: {
151: PetscObjectReference((PetscObject)viewer);
152: PetscNew(vf);
153: (*vf)->viewer = viewer;
154: (*vf)->format = format;
155: (*vf)->lg = NULL;
156: (*vf)->data = NULL;
157: return(0);
158: }
160: /*@C
161: PetscViewerAndFormatDestroy - Destroys a PetscViewerAndFormat struct.
163: Collective on PetscViewer
165: Input Parameters:
166: . vf - the PetscViewerAndFormat to be destroyed.
168: Level: developer
170: .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen(), PetscViewerAndFormatCreate()
171: @*/
172: PetscErrorCode PetscViewerAndFormatDestroy(PetscViewerAndFormat **vf)
173: {
177: PetscViewerDestroy(&(*vf)->viewer);
178: PetscDrawLGDestroy(&(*vf)->lg);
179: PetscFree(*vf);
180: return(0);
181: }
183: /*@C
184: PetscViewerGetType - Returns the type of a PetscViewer.
186: Not Collective
188: Input Parameter:
189: . viewer - the PetscViewer
191: Output Parameter:
192: . type - PetscViewer type (see below)
194: Available Types Include:
195: + PETSCVIEWERSOCKET - Socket PetscViewer
196: . PETSCVIEWERASCII - ASCII PetscViewer
197: . PETSCVIEWERBINARY - binary file PetscViewer
198: . PETSCVIEWERSTRING - string PetscViewer
199: - PETSCVIEWERDRAW - drawing PetscViewer
201: Level: intermediate
203: Note:
204: See include/petscviewer.h for a complete list of PetscViewers.
206: PetscViewerType is actually a string
208: .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerType
210: @*/
211: PetscErrorCode PetscViewerGetType(PetscViewer viewer,PetscViewerType *type)
212: {
216: *type = ((PetscObject)viewer)->type_name;
217: return(0);
218: }
220: /*@C
221: PetscViewerSetOptionsPrefix - Sets the prefix used for searching for all
222: PetscViewer options in the database.
224: Logically Collective on PetscViewer
226: Input Parameters:
227: + viewer - the PetscViewer context
228: - prefix - the prefix to prepend to all option names
230: Notes:
231: A hyphen (-) must NOT be given at the beginning of the prefix name.
232: The first character of all runtime options is AUTOMATICALLY the hyphen.
234: Level: advanced
236: .seealso: PetscViewerSetFromOptions()
237: @*/
238: PetscErrorCode PetscViewerSetOptionsPrefix(PetscViewer viewer,const char prefix[])
239: {
244: PetscObjectSetOptionsPrefix((PetscObject)viewer,prefix);
245: return(0);
246: }
248: /*@C
249: PetscViewerAppendOptionsPrefix - Appends to the prefix used for searching for all
250: PetscViewer options in the database.
252: Logically Collective on PetscViewer
254: Input Parameters:
255: + viewer - the PetscViewer context
256: - prefix - the prefix to prepend to all option names
258: Notes:
259: A hyphen (-) must NOT be given at the beginning of the prefix name.
260: The first character of all runtime options is AUTOMATICALLY the hyphen.
262: Level: advanced
264: .seealso: PetscViewerGetOptionsPrefix()
265: @*/
266: PetscErrorCode PetscViewerAppendOptionsPrefix(PetscViewer viewer,const char prefix[])
267: {
272: PetscObjectAppendOptionsPrefix((PetscObject)viewer,prefix);
273: return(0);
274: }
276: /*@C
277: PetscViewerGetOptionsPrefix - Sets the prefix used for searching for all
278: PetscViewer options in the database.
280: Not Collective
282: Input Parameter:
283: . viewer - the PetscViewer context
285: Output Parameter:
286: . prefix - pointer to the prefix string used
288: Notes:
289: On the fortran side, the user should pass in a string 'prefix' of
290: sufficient length to hold the prefix.
292: Level: advanced
294: .seealso: PetscViewerAppendOptionsPrefix()
295: @*/
296: PetscErrorCode PetscViewerGetOptionsPrefix(PetscViewer viewer,const char *prefix[])
297: {
302: PetscObjectGetOptionsPrefix((PetscObject)viewer,prefix);
303: return(0);
304: }
306: /*@
307: PetscViewerSetUp - Sets up the internal viewer data structures for the later use.
309: Collective on PetscViewer
311: Input Parameters:
312: . viewer - the PetscViewer context
314: Notes:
315: For basic use of the PetscViewer classes the user need not explicitly call
316: PetscViewerSetUp(), since these actions will happen automatically.
318: Level: advanced
320: .seealso: PetscViewerCreate(), PetscViewerDestroy()
321: @*/
322: PetscErrorCode PetscViewerSetUp(PetscViewer viewer)
323: {
328: if (viewer->setupcalled) return(0);
329: if (viewer->ops->setup) {
330: (*viewer->ops->setup)(viewer);
331: }
332: viewer->setupcalled = PETSC_TRUE;
333: return(0);
334: }
336: /*@C
337: PetscViewerViewFromOptions - View from Options
339: Collective on PetscViewer
341: Input Parameters:
342: + A - the PetscViewer context
343: . obj - Optional object
344: - name - command line option
346: Level: intermediate
347: .seealso: PetscViewer, PetscViewerView, PetscObjectViewFromOptions(), PetscViewerCreate()
348: @*/
349: PetscErrorCode PetscViewerViewFromOptions(PetscViewer A,PetscObject obj,const char name[])
350: {
355: PetscObjectViewFromOptions((PetscObject)A,obj,name);
356: return(0);
357: }
359: /*@C
360: PetscViewerView - Visualizes a viewer object.
362: Collective on PetscViewer
364: Input Parameters:
365: + v - the viewer to be viewed
366: - viewer - visualization context
368: Notes:
369: The available visualization contexts include
370: + PETSC_VIEWER_STDOUT_SELF - standard output (default)
371: . PETSC_VIEWER_STDOUT_WORLD - synchronized standard
372: output where only the first processor opens
373: the file. All other processors send their
374: data to the first processor to print.
375: - PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure
377: Level: beginner
379: .seealso: PetscViewerPushFormat(), PetscViewerASCIIOpen(), PetscViewerDrawOpen(),
380: PetscViewerSocketOpen(), PetscViewerBinaryOpen(), PetscViewerLoad()
381: @*/
382: PetscErrorCode PetscViewerView(PetscViewer v,PetscViewer viewer)
383: {
384: PetscErrorCode ierr;
385: PetscBool iascii;
386: PetscViewerFormat format;
387: #if defined(PETSC_HAVE_SAWS)
388: PetscBool issaws;
389: #endif
394: if (!viewer) {
395: PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)v),&viewer);
396: }
400: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
401: #if defined(PETSC_HAVE_SAWS)
402: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);
403: #endif
404: if (iascii) {
405: PetscViewerGetFormat(viewer,&format);
406: PetscObjectPrintClassNamePrefixType((PetscObject)v,viewer);
407: if (format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
408: if (v->format) {
409: PetscViewerASCIIPrintf(viewer," Viewer format = %s\n",PetscViewerFormats[v->format]);
410: }
411: PetscViewerASCIIPushTab(viewer);
412: if (v->ops->view) {
413: (*v->ops->view)(v,viewer);
414: }
415: PetscViewerASCIIPopTab(viewer);
416: }
417: #if defined(PETSC_HAVE_SAWS)
418: } else if (issaws) {
419: if (!((PetscObject)v)->amsmem) {
420: PetscObjectViewSAWs((PetscObject)v,viewer);
421: if (v->ops->view) {
422: (*v->ops->view)(v,viewer);
423: }
424: }
425: #endif
426: }
427: return(0);
428: }
430: /*@C
431: PetscViewerRead - Reads data from a PetscViewer
433: Collective
435: Input Parameters:
436: + viewer - The viewer
437: . data - Location to write the data
438: . num - Number of items of data to read
439: - datatype - Type of data to read
441: Output Parameters:
442: . count - number of items of data actually read, or NULL
444: Notes:
445: If datatype is PETSC_STRING and num is negative, reads until a newline character is found,
446: until a maximum of (-num - 1) chars.
448: Level: beginner
450: .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(),
451: VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
452: PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer
453: @*/
454: PetscErrorCode PetscViewerRead(PetscViewer viewer, void *data, PetscInt num, PetscInt *count, PetscDataType dtype)
455: {
460: if (dtype == PETSC_STRING) {
461: PetscInt c, i = 0, cnt;
462: char *s = (char *)data;
463: if (num >= 0) {
464: for (c = 0; c < num; c++) {
465: /* Skip leading whitespaces */
466: do {(*viewer->ops->read)(viewer, &(s[i]), 1, &cnt, PETSC_CHAR); if (!cnt) break;}
467: while (s[i]=='\n' || s[i]=='\t' || s[i]==' ' || s[i]=='\0' || s[i]=='\v' || s[i]=='\f' || s[i]=='\r');
468: i++;
469: /* Read strings one char at a time */
470: do {(*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR); if (!cnt) break;}
471: while (s[i-1]!='\n' && s[i-1]!='\t' && s[i-1]!=' ' && s[i-1]!='\0' && s[i-1]!='\v' && s[i-1]!='\f' && s[i-1]!='\r');
472: /* Terminate final string */
473: if (c == num-1) s[i-1] = '\0';
474: }
475: } else {
476: /* Read until a \n is encountered (-num is the max size allowed) */
477: do {(*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR); if (i == -num || !cnt) break;}
478: while (s[i-1]!='\n');
479: /* Terminate final string */
480: s[i-1] = '\0';
481: c = i;
482: }
483: if (count) *count = c;
484: else if (c < num) SETERRQ2(PetscObjectComm((PetscObject) viewer), PETSC_ERR_FILE_READ, "Insufficient data, only read %D < %D strings", c, num);
485: } else {
486: (*viewer->ops->read)(viewer, data, num, count, dtype);
487: }
488: return(0);
489: }
491: /*@
492: PetscViewerReadable - Return a flag whether the viewer can be read from
494: Not Collective
496: Input Parameters:
497: . viewer - the PetscViewer context
499: Output Parameters:
500: . flg - PETSC_TRUE if the viewer is readable, PETSC_FALSE otherwise
502: Notes:
503: PETSC_TRUE means that viewer's PetscViewerType supports reading (this holds e.g. for PETSCVIEWERBINARY)
504: and viewer is in a mode allowing reading, i.e. PetscViewerFileGetMode()
505: returns one of FILE_MODE_READ, FILE_MODE_UPDATE, FILE_MODE_APPEND_UPDATE.
507: Level: intermediate
509: .seealso: PetscViewerWritable(), PetscViewerCheckReadable(), PetscViewerCreate(), PetscViewerFileSetMode(), PetscViewerFileSetType()
510: @*/
511: PetscErrorCode PetscViewerReadable(PetscViewer viewer, PetscBool *flg)
512: {
513: PetscErrorCode ierr;
514: PetscFileMode mode;
515: PetscErrorCode (*f)(PetscViewer,PetscFileMode*) = NULL;
520: PetscObjectQueryFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", &f);
521: *flg = PETSC_FALSE;
522: if (!f) return(0);
523: (*f)(viewer, &mode);
524: switch (mode) {
525: case FILE_MODE_READ:
526: case FILE_MODE_UPDATE:
527: case FILE_MODE_APPEND_UPDATE:
528: *flg = PETSC_TRUE;
529: default: break;
530: }
531: return(0);
532: }
534: /*@
535: PetscViewerWritable - Return a flag whether the viewer can be written to
537: Not Collective
539: Input Parameters:
540: . viewer - the PetscViewer context
542: Output Parameters:
543: . flg - PETSC_TRUE if the viewer is writable, PETSC_FALSE otherwise
545: Notes:
546: PETSC_TRUE means viewer is in a mode allowing writing, i.e. PetscViewerFileGetMode()
547: returns one of FILE_MODE_WRITE, FILE_MODE_APPEND, FILE_MODE_UPDATE, FILE_MODE_APPEND_UPDATE.
549: Level: intermediate
551: .seealso: PetscViewerReadable(), PetscViewerCheckWritable(), PetscViewerCreate(), PetscViewerFileSetMode(), PetscViewerFileSetType()
552: @*/
553: PetscErrorCode PetscViewerWritable(PetscViewer viewer, PetscBool *flg)
554: {
555: PetscErrorCode ierr;
556: PetscFileMode mode;
557: PetscErrorCode (*f)(PetscViewer,PetscFileMode*) = NULL;
562: PetscObjectQueryFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", &f);
563: *flg = PETSC_TRUE;
564: if (!f) return(0);
565: (*f)(viewer, &mode);
566: if (mode == FILE_MODE_READ) *flg = PETSC_FALSE;
567: return(0);
568: }
570: /*@
571: PetscViewerCheckReadable - Check whether the viewer can be read from
573: Collective
575: Input Parameters:
576: . viewer - the PetscViewer context
578: Level: intermediate
580: .seealso: PetscViewerReadable(), PetscViewerCheckWritable(), PetscViewerCreate(), PetscViewerFileSetMode(), PetscViewerFileSetType()
581: @*/
582: PetscErrorCode PetscViewerCheckReadable(PetscViewer viewer)
583: {
584: PetscBool flg;
585: PetscErrorCode ierr;
589: PetscViewerReadable(viewer, &flg);
590: if (!flg) SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Viewer doesn't support reading, or is not in reading mode (FILE_MODE_READ, FILE_MODE_UPDATE, FILE_MODE_APPEND_UPDATE)");
591: return(0);
592: }
594: /*@
595: PetscViewerCheckWritable - Check whether the viewer can be written to
597: Collective
599: Input Parameters:
600: . viewer - the PetscViewer context
602: Level: intermediate
604: .seealso: PetscViewerWritable(), PetscViewerCheckReadable(), PetscViewerCreate(), PetscViewerFileSetMode(), PetscViewerFileSetType()
605: @*/
606: PetscErrorCode PetscViewerCheckWritable(PetscViewer viewer)
607: {
608: PetscBool flg;
609: PetscErrorCode ierr;
613: PetscViewerWritable(viewer, &flg);
614: if (!flg) SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Viewer doesn't support writing, or is in FILE_MODE_READ mode");
615: return(0);
616: }