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: {
18: if (Petsc_Viewer_keyval != MPI_KEYVAL_INVALID) {
19: MPI_Comm_free_keyval(&Petsc_Viewer_keyval);
20: }
21: if (Petsc_Viewer_Stdout_keyval != MPI_KEYVAL_INVALID) {
22: MPI_Comm_free_keyval(&Petsc_Viewer_Stdout_keyval);
23: }
24: if (Petsc_Viewer_Stderr_keyval != MPI_KEYVAL_INVALID) {
25: MPI_Comm_free_keyval(&Petsc_Viewer_Stderr_keyval);
26: }
27: if (Petsc_Viewer_Binary_keyval != MPI_KEYVAL_INVALID) {
28: MPI_Comm_free_keyval(&Petsc_Viewer_Binary_keyval);
29: }
30: if (Petsc_Viewer_Draw_keyval != MPI_KEYVAL_INVALID) {
31: MPI_Comm_free_keyval(&Petsc_Viewer_Draw_keyval);
32: }
33: #if defined(PETSC_HAVE_HDF5)
34: if (Petsc_Viewer_HDF5_keyval != MPI_KEYVAL_INVALID) {
35: MPI_Comm_free_keyval(&Petsc_Viewer_HDF5_keyval);
36: }
37: #endif
38: #if defined(PETSC_USE_SOCKETVIEWER)
39: if (Petsc_Viewer_Socket_keyval != MPI_KEYVAL_INVALID) {
40: MPI_Comm_free_keyval(&Petsc_Viewer_Socket_keyval);
41: }
42: #endif
43: PetscFunctionListDestroy(&PetscViewerList);
44: PetscViewerPackageInitialized = PETSC_FALSE;
45: PetscViewerRegisterAllCalled = PETSC_FALSE;
46: return 0;
47: }
49: /*@C
50: PetscViewerInitializePackage - This function initializes everything in the main PetscViewer package.
52: Level: developer
54: .seealso: PetscInitialize()
55: @*/
56: PetscErrorCode PetscViewerInitializePackage(void)
57: {
58: char logList[256];
59: PetscBool opt,pkg;
61: if (PetscViewerPackageInitialized) return 0;
62: PetscViewerPackageInitialized = PETSC_TRUE;
63: /* Register Classes */
64: PetscClassIdRegister("Viewer",&PETSC_VIEWER_CLASSID);
65: /* Register Constructors */
66: PetscViewerRegisterAll();
67: /* Process Info */
68: {
69: PetscClassId classids[1];
71: classids[0] = PETSC_VIEWER_CLASSID;
72: PetscInfoProcessClass("viewer", 1, classids);
73: }
74: /* Process summary exclusions */
75: PetscOptionsGetString(NULL,NULL,"-log_exclude",logList,sizeof(logList),&opt);
76: if (opt) {
77: PetscStrInList("viewer",logList,',',&pkg);
78: if (pkg) PetscLogEventExcludeClass(PETSC_VIEWER_CLASSID);
79: }
80: #if defined(PETSC_HAVE_MATHEMATICA)
81: PetscViewerMathematicaInitializePackage();
82: #endif
83: /* Register package finalizer */
84: PetscRegisterFinalize(PetscViewerFinalizePackage);
85: return 0;
86: }
88: /*@
89: PetscViewerDestroy - Destroys a PetscViewer.
91: Collective on PetscViewer
93: Input Parameters:
94: . viewer - the PetscViewer to be destroyed.
96: Level: beginner
98: .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen()
100: @*/
101: PetscErrorCode PetscViewerDestroy(PetscViewer *viewer)
102: {
103: if (!*viewer) return 0;
106: PetscViewerFlush(*viewer);
107: if (--((PetscObject)(*viewer))->refct > 0) {*viewer = NULL; return 0;}
109: PetscObjectSAWsViewOff((PetscObject)*viewer);
110: if ((*viewer)->ops->destroy) {
111: (*(*viewer)->ops->destroy)(*viewer);
112: }
113: PetscHeaderDestroy(viewer);
114: return 0;
115: }
117: /*@C
118: PetscViewerAndFormatCreate - Creates a PetscViewerAndFormat struct.
120: Collective on PetscViewer
122: Input Parameters:
123: + viewer - the viewer
124: - format - the format
126: Output Parameter:
127: . vf - viewer and format object
129: Notes:
130: This increases the reference count of the viewer so you can destroy the viewer object after this call
131: Level: developer
133: This is used as the context variable for many of the TS, SNES, and KSP monitor functions
135: .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen(), PetscViewerAndFormatDestroy()
137: @*/
138: PetscErrorCode PetscViewerAndFormatCreate(PetscViewer viewer, PetscViewerFormat format, PetscViewerAndFormat **vf)
139: {
140: PetscObjectReference((PetscObject)viewer);
141: PetscNew(vf);
142: (*vf)->viewer = viewer;
143: (*vf)->format = format;
144: (*vf)->lg = NULL;
145: (*vf)->data = NULL;
146: return 0;
147: }
149: /*@C
150: PetscViewerAndFormatDestroy - Destroys a PetscViewerAndFormat struct.
152: Collective on PetscViewer
154: Input Parameters:
155: . vf - the PetscViewerAndFormat to be destroyed.
157: Level: developer
159: .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen(), PetscViewerAndFormatCreate()
160: @*/
161: PetscErrorCode PetscViewerAndFormatDestroy(PetscViewerAndFormat **vf)
162: {
163: PetscViewerDestroy(&(*vf)->viewer);
164: PetscDrawLGDestroy(&(*vf)->lg);
165: PetscFree(*vf);
166: return 0;
167: }
169: /*@C
170: PetscViewerGetType - Returns the type of a PetscViewer.
172: Not Collective
174: Input Parameter:
175: . viewer - the PetscViewer
177: Output Parameter:
178: . type - PetscViewer type (see below)
180: Available Types Include:
181: + PETSCVIEWERSOCKET - Socket PetscViewer
182: . PETSCVIEWERASCII - ASCII PetscViewer
183: . PETSCVIEWERBINARY - binary file PetscViewer
184: . PETSCVIEWERSTRING - string PetscViewer
185: - PETSCVIEWERDRAW - drawing PetscViewer
187: Level: intermediate
189: Note:
190: See include/petscviewer.h for a complete list of PetscViewers.
192: PetscViewerType is actually a string
194: .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerType
196: @*/
197: PetscErrorCode PetscViewerGetType(PetscViewer viewer,PetscViewerType *type)
198: {
201: *type = ((PetscObject)viewer)->type_name;
202: return 0;
203: }
205: /*@C
206: PetscViewerSetOptionsPrefix - Sets the prefix used for searching for all
207: PetscViewer options in the database.
209: Logically Collective on PetscViewer
211: Input Parameters:
212: + viewer - the PetscViewer context
213: - prefix - the prefix to prepend to all option names
215: Notes:
216: A hyphen (-) must NOT be given at the beginning of the prefix name.
217: The first character of all runtime options is AUTOMATICALLY the hyphen.
219: Level: advanced
221: .seealso: PetscViewerSetFromOptions()
222: @*/
223: PetscErrorCode PetscViewerSetOptionsPrefix(PetscViewer viewer,const char prefix[])
224: {
226: PetscObjectSetOptionsPrefix((PetscObject)viewer,prefix);
227: return 0;
228: }
230: /*@C
231: PetscViewerAppendOptionsPrefix - Appends to the prefix used for searching for all
232: PetscViewer options in the database.
234: Logically Collective on PetscViewer
236: Input Parameters:
237: + viewer - the PetscViewer context
238: - prefix - the prefix to prepend to all option names
240: Notes:
241: A hyphen (-) must NOT be given at the beginning of the prefix name.
242: The first character of all runtime options is AUTOMATICALLY the hyphen.
244: Level: advanced
246: .seealso: PetscViewerGetOptionsPrefix()
247: @*/
248: PetscErrorCode PetscViewerAppendOptionsPrefix(PetscViewer viewer,const char prefix[])
249: {
251: PetscObjectAppendOptionsPrefix((PetscObject)viewer,prefix);
252: return 0;
253: }
255: /*@C
256: PetscViewerGetOptionsPrefix - Sets the prefix used for searching for all
257: PetscViewer options in the database.
259: Not Collective
261: Input Parameter:
262: . viewer - the PetscViewer context
264: Output Parameter:
265: . prefix - pointer to the prefix string used
267: Notes:
268: On the fortran side, the user should pass in a string 'prefix' of
269: sufficient length to hold the prefix.
271: Level: advanced
273: .seealso: PetscViewerAppendOptionsPrefix()
274: @*/
275: PetscErrorCode PetscViewerGetOptionsPrefix(PetscViewer viewer,const char *prefix[])
276: {
278: PetscObjectGetOptionsPrefix((PetscObject)viewer,prefix);
279: return 0;
280: }
282: /*@
283: PetscViewerSetUp - Sets up the internal viewer data structures for the later use.
285: Collective on PetscViewer
287: Input Parameters:
288: . viewer - the PetscViewer context
290: Notes:
291: For basic use of the PetscViewer classes the user need not explicitly call
292: PetscViewerSetUp(), since these actions will happen automatically.
294: Level: advanced
296: .seealso: PetscViewerCreate(), PetscViewerDestroy()
297: @*/
298: PetscErrorCode PetscViewerSetUp(PetscViewer viewer)
299: {
301: if (viewer->setupcalled) return 0;
302: if (viewer->ops->setup) {
303: (*viewer->ops->setup)(viewer);
304: }
305: viewer->setupcalled = PETSC_TRUE;
306: return 0;
307: }
309: /*@C
310: PetscViewerViewFromOptions - View from Options
312: Collective on PetscViewer
314: Input Parameters:
315: + A - the PetscViewer context
316: . obj - Optional object
317: - name - command line option
319: Level: intermediate
320: .seealso: PetscViewer, PetscViewerView, PetscObjectViewFromOptions(), PetscViewerCreate()
321: @*/
322: PetscErrorCode PetscViewerViewFromOptions(PetscViewer A,PetscObject obj,const char name[])
323: {
325: PetscObjectViewFromOptions((PetscObject)A,obj,name);
326: return 0;
327: }
329: /*@C
330: PetscViewerView - Visualizes a viewer object.
332: Collective on PetscViewer
334: Input Parameters:
335: + v - the viewer to be viewed
336: - viewer - visualization context
338: Notes:
339: The available visualization contexts include
340: + PETSC_VIEWER_STDOUT_SELF - standard output (default)
341: . PETSC_VIEWER_STDOUT_WORLD - synchronized standard
342: output where only the first processor opens
343: the file. All other processors send their
344: data to the first processor to print.
345: - PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure
347: Level: beginner
349: .seealso: PetscViewerPushFormat(), PetscViewerASCIIOpen(), PetscViewerDrawOpen(),
350: PetscViewerSocketOpen(), PetscViewerBinaryOpen(), PetscViewerLoad()
351: @*/
352: PetscErrorCode PetscViewerView(PetscViewer v,PetscViewer viewer)
353: {
354: PetscBool iascii;
355: PetscViewerFormat format;
356: #if defined(PETSC_HAVE_SAWS)
357: PetscBool issaws;
358: #endif
362: if (!viewer) {
363: PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)v),&viewer);
364: }
368: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
369: #if defined(PETSC_HAVE_SAWS)
370: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);
371: #endif
372: if (iascii) {
373: PetscViewerGetFormat(viewer,&format);
374: PetscObjectPrintClassNamePrefixType((PetscObject)v,viewer);
375: if (format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
376: if (v->format) {
377: PetscViewerASCIIPrintf(viewer," Viewer format = %s\n",PetscViewerFormats[v->format]);
378: }
379: PetscViewerASCIIPushTab(viewer);
380: if (v->ops->view) {
381: (*v->ops->view)(v,viewer);
382: }
383: PetscViewerASCIIPopTab(viewer);
384: }
385: #if defined(PETSC_HAVE_SAWS)
386: } else if (issaws) {
387: if (!((PetscObject)v)->amsmem) {
388: PetscObjectViewSAWs((PetscObject)v,viewer);
389: if (v->ops->view) {
390: (*v->ops->view)(v,viewer);
391: }
392: }
393: #endif
394: }
395: return 0;
396: }
398: /*@C
399: PetscViewerRead - Reads data from a PetscViewer
401: Collective
403: Input Parameters:
404: + viewer - The viewer
405: . data - Location to write the data
406: . num - Number of items of data to read
407: - datatype - Type of data to read
409: Output Parameters:
410: . count - number of items of data actually read, or NULL
412: Notes:
413: If datatype is PETSC_STRING and num is negative, reads until a newline character is found,
414: until a maximum of (-num - 1) chars.
416: Level: beginner
418: .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(),
419: VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
420: PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer
421: @*/
422: PetscErrorCode PetscViewerRead(PetscViewer viewer, void *data, PetscInt num, PetscInt *count, PetscDataType dtype)
423: {
425: if (dtype == PETSC_STRING) {
426: PetscInt c, i = 0, cnt;
427: char *s = (char *)data;
428: if (num >= 0) {
429: for (c = 0; c < num; c++) {
430: /* Skip leading whitespaces */
431: do {(*viewer->ops->read)(viewer, &(s[i]), 1, &cnt, PETSC_CHAR); if (!cnt) break;}
432: while (s[i]=='\n' || s[i]=='\t' || s[i]==' ' || s[i]=='\0' || s[i]=='\v' || s[i]=='\f' || s[i]=='\r');
433: i++;
434: /* Read strings one char at a time */
435: do {(*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR); if (!cnt) break;}
436: 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');
437: /* Terminate final string */
438: if (c == num-1) s[i-1] = '\0';
439: }
440: } else {
441: /* Read until a \n is encountered (-num is the max size allowed) */
442: do {(*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR); if (i == -num || !cnt) break;}
443: while (s[i-1]!='\n');
444: /* Terminate final string */
445: s[i-1] = '\0';
446: c = i;
447: }
448: if (count) *count = c;
450: } else {
451: (*viewer->ops->read)(viewer, data, num, count, dtype);
452: }
453: return 0;
454: }
456: /*@
457: PetscViewerReadable - Return a flag whether the viewer can be read from
459: Not Collective
461: Input Parameters:
462: . viewer - the PetscViewer context
464: Output Parameters:
465: . flg - PETSC_TRUE if the viewer is readable, PETSC_FALSE otherwise
467: Notes:
468: PETSC_TRUE means that viewer's PetscViewerType supports reading (this holds e.g. for PETSCVIEWERBINARY)
469: and viewer is in a mode allowing reading, i.e. PetscViewerFileGetMode()
470: returns one of FILE_MODE_READ, FILE_MODE_UPDATE, FILE_MODE_APPEND_UPDATE.
472: Level: intermediate
474: .seealso: PetscViewerWritable(), PetscViewerCheckReadable(), PetscViewerCreate(), PetscViewerFileSetMode(), PetscViewerFileSetType()
475: @*/
476: PetscErrorCode PetscViewerReadable(PetscViewer viewer, PetscBool *flg)
477: {
478: PetscFileMode mode;
479: PetscErrorCode (*f)(PetscViewer,PetscFileMode*) = NULL;
483: PetscObjectQueryFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", &f);
484: *flg = PETSC_FALSE;
485: if (!f) return 0;
486: (*f)(viewer, &mode);
487: switch (mode) {
488: case FILE_MODE_READ:
489: case FILE_MODE_UPDATE:
490: case FILE_MODE_APPEND_UPDATE:
491: *flg = PETSC_TRUE;
492: default: break;
493: }
494: return 0;
495: }
497: /*@
498: PetscViewerWritable - Return a flag whether the viewer can be written to
500: Not Collective
502: Input Parameters:
503: . viewer - the PetscViewer context
505: Output Parameters:
506: . flg - PETSC_TRUE if the viewer is writable, PETSC_FALSE otherwise
508: Notes:
509: PETSC_TRUE means viewer is in a mode allowing writing, i.e. PetscViewerFileGetMode()
510: returns one of FILE_MODE_WRITE, FILE_MODE_APPEND, FILE_MODE_UPDATE, FILE_MODE_APPEND_UPDATE.
512: Level: intermediate
514: .seealso: PetscViewerReadable(), PetscViewerCheckWritable(), PetscViewerCreate(), PetscViewerFileSetMode(), PetscViewerFileSetType()
515: @*/
516: PetscErrorCode PetscViewerWritable(PetscViewer viewer, PetscBool *flg)
517: {
518: PetscFileMode mode;
519: PetscErrorCode (*f)(PetscViewer,PetscFileMode*) = NULL;
523: PetscObjectQueryFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", &f);
524: *flg = PETSC_TRUE;
525: if (!f) return 0;
526: (*f)(viewer, &mode);
527: if (mode == FILE_MODE_READ) *flg = PETSC_FALSE;
528: return 0;
529: }
531: /*@
532: PetscViewerCheckReadable - Check whether the viewer can be read from
534: Collective
536: Input Parameters:
537: . viewer - the PetscViewer context
539: Level: intermediate
541: .seealso: PetscViewerReadable(), PetscViewerCheckWritable(), PetscViewerCreate(), PetscViewerFileSetMode(), PetscViewerFileSetType()
542: @*/
543: PetscErrorCode PetscViewerCheckReadable(PetscViewer viewer)
544: {
545: PetscBool flg;
548: PetscViewerReadable(viewer, &flg);
550: return 0;
551: }
553: /*@
554: PetscViewerCheckWritable - Check whether the viewer can be written to
556: Collective
558: Input Parameters:
559: . viewer - the PetscViewer context
561: Level: intermediate
563: .seealso: PetscViewerWritable(), PetscViewerCheckReadable(), PetscViewerCreate(), PetscViewerFileSetMode(), PetscViewerFileSetType()
564: @*/
565: PetscErrorCode PetscViewerCheckWritable(PetscViewer viewer)
566: {
567: PetscBool flg;
570: PetscViewerWritable(viewer, &flg);
572: return 0;
573: }