Actual source code: view.c

petsc-master 2019-12-08
Report Typos and Errors

  2:  #include <petsc/private/viewerimpl.h>

  4: PetscClassId PETSC_VIEWER_CLASSID;

  6: static PetscBool PetscViewerPackageInitialized = PETSC_FALSE;
  7: /*@C
  8:   PetscViewerFinalizePackage - This function destroys any global objects created in the Petsc viewers. It is
  9:   called from PetscFinalize().

 11:   Level: developer

 13: .seealso: PetscFinalize()
 14: @*/
 15: PetscErrorCode  PetscViewerFinalizePackage(void)
 16: {

 20:   if (Petsc_Viewer_keyval != MPI_KEYVAL_INVALID) {
 21:     MPI_Comm_free_keyval(&Petsc_Viewer_keyval);
 22:   }
 23:   if (Petsc_Viewer_Stdout_keyval != MPI_KEYVAL_INVALID) {
 24:     MPI_Comm_free_keyval(&Petsc_Viewer_Stdout_keyval);
 25:   }
 26:   if (Petsc_Viewer_Stderr_keyval != MPI_KEYVAL_INVALID) {
 27:     MPI_Comm_free_keyval(&Petsc_Viewer_Stderr_keyval);
 28:   }
 29:   if (Petsc_Viewer_Binary_keyval != MPI_KEYVAL_INVALID) {
 30:     MPI_Comm_free_keyval(&Petsc_Viewer_Binary_keyval);
 31:   }
 32:   if (Petsc_Viewer_Draw_keyval != MPI_KEYVAL_INVALID) {
 33:     MPI_Comm_free_keyval(&Petsc_Viewer_Draw_keyval);
 34:   }
 35: #if defined(PETSC_HAVE_HDF5)
 36:   if (Petsc_Viewer_HDF5_keyval != MPI_KEYVAL_INVALID) {
 37:     MPI_Comm_free_keyval(&Petsc_Viewer_HDF5_keyval);
 38:   }
 39: #endif
 40: #if defined(PETSC_USE_SOCKETVIEWER)
 41:   if (Petsc_Viewer_Socket_keyval != MPI_KEYVAL_INVALID) {
 42:     MPI_Comm_free_keyval(&Petsc_Viewer_Socket_keyval);
 43:   }
 44: #endif
 45:   PetscFunctionListDestroy(&PetscViewerList);
 46:   PetscViewerPackageInitialized = PETSC_FALSE;
 47:   PetscViewerRegisterAllCalled  = PETSC_FALSE;
 48:   return(0);
 49: }

 51: /*@C
 52:   PetscViewerInitializePackage - This function initializes everything in the main PetscViewer package.

 54:   Level: developer

 56: .seealso: PetscInitialize()
 57: @*/
 58: PetscErrorCode  PetscViewerInitializePackage(void)
 59: {
 60:   char           logList[256];
 61:   PetscBool      opt,pkg;

 65:   if (PetscViewerPackageInitialized) return(0);
 66:   PetscViewerPackageInitialized = PETSC_TRUE;
 67:   /* Register Classes */
 68:   PetscClassIdRegister("Viewer",&PETSC_VIEWER_CLASSID);
 69:   /* Register Constructors */
 70:   PetscViewerRegisterAll();
 71:   /* Process info exclusions */
 72:   PetscOptionsGetString(NULL,NULL,"-info_exclude",logList,sizeof(logList),&opt);
 73:   if (opt) {
 74:     PetscStrInList("viewer",logList,',',&pkg);
 75:     if (pkg) {PetscInfoDeactivateClass(PETSC_VIEWER_CLASSID);}
 76:   }
 77:   /* Process summary exclusions */
 78:   PetscOptionsGetString(NULL,NULL,"-log_exclude",logList,sizeof(logList),&opt);
 79:   if (opt) {
 80:     PetscStrInList("viewer",logList,',',&pkg);
 81:     if (pkg) {PetscLogEventExcludeClass(PETSC_VIEWER_CLASSID);}
 82:   }
 83: #if defined(PETSC_HAVE_MATHEMATICA)
 84:   PetscViewerMathematicaInitializePackage();
 85: #endif
 86:   /* Register package finalizer */
 87:   PetscRegisterFinalize(PetscViewerFinalizePackage);
 88:   return(0);
 89: }

 91: /*@
 92:    PetscViewerDestroy - Destroys a PetscViewer.

 94:    Collective on PetscViewer

 96:    Input Parameters:
 97: .  viewer - the PetscViewer to be destroyed.

 99:    Level: beginner

101: .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen()

103: @*/
104: PetscErrorCode  PetscViewerDestroy(PetscViewer *viewer)
105: {

109:   if (!*viewer) return(0);

112:   PetscViewerFlush(*viewer);
113:   if (--((PetscObject)(*viewer))->refct > 0) {*viewer = 0; return(0);}

115:   PetscObjectSAWsViewOff((PetscObject)*viewer);
116:   if ((*viewer)->ops->destroy) {
117:     (*(*viewer)->ops->destroy)(*viewer);
118:   }
119:   PetscHeaderDestroy(viewer);
120:   return(0);
121: }

123: /*@C
124:    PetscViewerAndFormatCreate - Creates a PetscViewerAndFormat struct.

126:    Collective on PetscViewer

128:    Input Parameters:
129: +  viewer - the viewer
130: -  format - the format 

132:    Output Parameter:
133: .   vf - viewer and format object

135:    Notes:
136:     This increases the reference count of the viewer so you can destroy the viewer object after this call
137:    Level: developer

139:    This is used as the context variable for many of the TS, SNES, and KSP monitor functions

141: .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen(), PetscViewerAndFormatDestroy()

143: @*/
144: PetscErrorCode  PetscViewerAndFormatCreate(PetscViewer viewer, PetscViewerFormat format,PetscViewerAndFormat **vf)
145: {

149:   PetscObjectReference((PetscObject)viewer);
150:   PetscNew(vf);
151:   (*vf)->viewer = viewer;
152:   (*vf)->format = format;
153:   return(0);
154: }


157: /*@C
158:    PetscViewerAndFormatDestroy - Destroys a PetscViewerAndFormat struct.

160:    Collective on PetscViewer

162:    Input Parameters:
163: .  viewer - the PetscViewerAndFormat to be destroyed.

165:    Level: developer

167: .seealso: PetscViewerSocketOpen(), PetscViewerASCIIOpen(), PetscViewerCreate(), PetscViewerDrawOpen(), PetscViewerAndFormatCreate()

169: @*/
170: PetscErrorCode  PetscViewerAndFormatDestroy(PetscViewerAndFormat **vf)
171: {

175:   PetscViewerDestroy(&(*vf)->viewer);
176:   PetscFree(*vf);
177:   return(0);
178: }

180: /*@C
181:    PetscViewerGetType - Returns the type of a PetscViewer.

183:    Not Collective

185:    Input Parameter:
186: .   viewer - the PetscViewer

188:    Output Parameter:
189: .  type - PetscViewer type (see below)

191:    Available Types Include:
192: +  PETSCVIEWERSOCKET - Socket PetscViewer
193: .  PETSCVIEWERASCII - ASCII PetscViewer
194: .  PETSCVIEWERBINARY - binary file PetscViewer
195: .  PETSCVIEWERSTRING - string PetscViewer
196: -  PETSCVIEWERDRAW - drawing PetscViewer

198:    Level: intermediate

200:    Note:
201:    See include/petscviewer.h for a complete list of PetscViewers.

203:    PetscViewerType is actually a string

205: .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerType

207: @*/
208: PetscErrorCode  PetscViewerGetType(PetscViewer viewer,PetscViewerType *type)
209: {
213:   *type = ((PetscObject)viewer)->type_name;
214:   return(0);
215: }

217: /*@C
218:    PetscViewerSetOptionsPrefix - Sets the prefix used for searching for all
219:    PetscViewer options in the database.

221:    Logically Collective on PetscViewer

223:    Input Parameter:
224: +  viewer - the PetscViewer context
225: -  prefix - the prefix to prepend to all option names

227:    Notes:
228:    A hyphen (-) must NOT be given at the beginning of the prefix name.
229:    The first character of all runtime options is AUTOMATICALLY the hyphen.

231:    Level: advanced

233: .seealso: PetscViewerSetFromOptions()
234: @*/
235: PetscErrorCode  PetscViewerSetOptionsPrefix(PetscViewer viewer,const char prefix[])
236: {

241:   PetscObjectSetOptionsPrefix((PetscObject)viewer,prefix);
242:   return(0);
243: }

245: /*@C
246:    PetscViewerAppendOptionsPrefix - Appends to the prefix used for searching for all
247:    PetscViewer options in the database.

249:    Logically Collective on PetscViewer

251:    Input Parameters:
252: +  viewer - the PetscViewer context
253: -  prefix - the prefix to prepend to all option names

255:    Notes:
256:    A hyphen (-) must NOT be given at the beginning of the prefix name.
257:    The first character of all runtime options is AUTOMATICALLY the hyphen.

259:    Level: advanced

261: .seealso: PetscViewerGetOptionsPrefix()
262: @*/
263: PetscErrorCode  PetscViewerAppendOptionsPrefix(PetscViewer viewer,const char prefix[])
264: {

269:   PetscObjectAppendOptionsPrefix((PetscObject)viewer,prefix);
270:   return(0);
271: }

273: /*@C
274:    PetscViewerGetOptionsPrefix - Sets the prefix used for searching for all
275:    PetscViewer options in the database.

277:    Not Collective

279:    Input Parameter:
280: .  viewer - the PetscViewer context

282:    Output Parameter:
283: .  prefix - pointer to the prefix string used

285:    Notes:
286:     On the fortran side, the user should pass in a string 'prefix' of
287:    sufficient length to hold the prefix.

289:    Level: advanced

291: .seealso: PetscViewerAppendOptionsPrefix()
292: @*/
293: PetscErrorCode  PetscViewerGetOptionsPrefix(PetscViewer viewer,const char *prefix[])
294: {

299:   PetscObjectGetOptionsPrefix((PetscObject)viewer,prefix);
300:   return(0);
301: }

303: /*@
304:    PetscViewerSetUp - Sets up the internal viewer data structures for the later use.

306:    Collective on PetscViewer

308:    Input Parameters:
309: .  viewer - the PetscViewer context

311:    Notes:
312:    For basic use of the PetscViewer classes the user need not explicitly call
313:    PetscViewerSetUp(), since these actions will happen automatically.

315:    Level: advanced

317: .seealso: PetscViewerCreate(), PetscViewerDestroy()
318: @*/
319: PetscErrorCode  PetscViewerSetUp(PetscViewer viewer)
320: {

325:   if (viewer->setupcalled) return(0);
326:   if (viewer->ops->setup) {
327:     (*viewer->ops->setup)(viewer);
328:   }
329:   viewer->setupcalled = PETSC_TRUE;
330:   return(0);
331: }

333: /*@C
334:    PetscViewerViewFromOptions - View from Options

336:    Collective on PetscViewer

338:    Input Parameters:
339: +  A - the PetscViewer context
340: .  obj - Optional object
341: -  name - command line option

343:    Level: intermediate
344: .seealso:  PetscViewer, PetscViewerView, PetscObjectViewFromOptions(), PetscViewerCreate()
345: @*/
346: PetscErrorCode  PetscViewerViewFromOptions(PetscViewer A,PetscObject obj,const char name[])
347: {

352:   PetscObjectViewFromOptions((PetscObject)A,obj,name);
353:   return(0);
354: }

356: /*@C
357:    PetscViewerView - Visualizes a viewer object.

359:    Collective on PetscViewer

361:    Input Parameters:
362: +  v - the viewer to be viewed
363: -  viewer - visualization context

365:   Notes:
366:   The available visualization contexts include
367: +    PETSC_VIEWER_STDOUT_SELF - standard output (default)
368: .    PETSC_VIEWER_STDOUT_WORLD - synchronized standard
369:         output where only the first processor opens
370:         the file.  All other processors send their
371:         data to the first processor to print.
372: -     PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure

374:    Level: beginner

376: .seealso: PetscViewerPushFormat(), PetscViewerASCIIOpen(), PetscViewerDrawOpen(),
377:           PetscViewerSocketOpen(), PetscViewerBinaryOpen(), PetscViewerLoad()
378: @*/
379: PetscErrorCode  PetscViewerView(PetscViewer v,PetscViewer viewer)
380: {
381:   PetscErrorCode    ierr;
382:   PetscBool         iascii;
383:   PetscViewerFormat format;
384: #if defined(PETSC_HAVE_SAWS)
385:   PetscBool         issaws;
386: #endif

391:   if (!viewer) {
392:     PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)v),&viewer);
393:   }

397:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
398: #if defined(PETSC_HAVE_SAWS)
399:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);
400: #endif
401:   if (iascii) {
402:     PetscViewerGetFormat(viewer,&format);
403:     PetscObjectPrintClassNamePrefixType((PetscObject)v,viewer);
404:     if (format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
405:       if (v->format) {
406:         PetscViewerASCIIPrintf(viewer,"  Viewer format = %s\n",PetscViewerFormats[v->format]);
407:       }
408:       PetscViewerASCIIPushTab(viewer);
409:       if (v->ops->view) {
410:         (*v->ops->view)(v,viewer);
411:       }
412:       PetscViewerASCIIPopTab(viewer);
413:     }
414: #if defined(PETSC_HAVE_SAWS)
415:   } else if (issaws) {
416:     if (!((PetscObject)v)->amsmem) {
417:       PetscObjectViewSAWs((PetscObject)v,viewer);
418:       if (v->ops->view) {
419:         (*v->ops->view)(v,viewer);
420:       }
421:     }
422: #endif
423:   }
424:   return(0);
425: }

427: /*@C
428:    PetscViewerRead - Reads data from a PetscViewer

430:    Collective

432:    Input Parameters:
433: +  viewer   - The viewer
434: .  data     - Location to write the data
435: .  num      - Number of items of data to read
436: -  datatype - Type of data to read

438:    Output Parameters:
439: .  count - number of items of data actually read, or NULL

441:    Notes:
442:    If datatype is PETSC_STRING and num is negative, reads until a newline character is found,
443:    until a maximum of (-num - 1) chars.

445:    Level: beginner

447: .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(),
448:           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
449:           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer
450: @*/
451: PetscErrorCode  PetscViewerRead(PetscViewer viewer, void *data, PetscInt num, PetscInt *count, PetscDataType dtype)
452: {

457:   if (dtype == PETSC_STRING) {
458:     PetscInt c, i = 0, cnt;
459:     char *s = (char *)data;
460:     if (num >= 0) {
461:       for (c = 0; c < num; c++) {
462:         /* Skip leading whitespaces */
463:         do {(*viewer->ops->read)(viewer, &(s[i]), 1, &cnt, PETSC_CHAR); if (!cnt) break;}
464:         while (s[i]=='\n' || s[i]=='\t' || s[i]==' ' || s[i]=='\0' || s[i]=='\v' || s[i]=='\f' || s[i]=='\r');
465:         i++;
466:         /* Read strings one char at a time */
467:         do {(*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR); if (!cnt) break;}
468:         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');
469:         /* Terminate final string */
470:         if (c == num-1) s[i-1] = '\0';
471:       }
472:     } else {
473:       /* Read until a \n is encountered (-num is the max size allowed) */
474:       do {(*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR); if (i == -num || !cnt) break;}
475:       while (s[i-1]!='\n');
476:       /* Terminate final string */
477:       s[i-1] = '\0';
478:       c      = i;
479:     }
480:     if (count) *count = c;
481:     else if (c < num) SETERRQ2(PetscObjectComm((PetscObject) viewer), PETSC_ERR_FILE_READ, "Insufficient data, only read %D < %D strings", c, num);
482:   } else {
483:     (*viewer->ops->read)(viewer, data, num, count, dtype);
484:   }
485:   return(0);
486: }

488: /*@
489:    PetscViewerReadable - Return a flag whether the viewer can be read from

491:    Not Collective

493:    Input Parameters:
494: .  viewer - the PetscViewer context

496:    Output Parameters:
497: .  flg - PETSC_TRUE if the viewer is readable, PETSC_FALSE otherwise

499:    Notes:
500:    PETSC_TRUE means that viewer's PetscViewerType supports reading (this holds e.g. for PETSCVIEWERBINARY)
501:    and viewer is in a mode allowing reading, i.e. PetscViewerFileGetMode()
502:    returns one of FILE_MODE_READ, FILE_MODE_UPDATE, FILE_MODE_APPEND_UPDATE.

504:    Level: intermediate

506: .seealso: PetscViewerWritable(), PetscViewerCheckReadable(), PetscViewerCreate(), PetscViewerFileSetMode(), PetscViewerFileSetType()
507: @*/
508: PetscErrorCode  PetscViewerReadable(PetscViewer viewer, PetscBool *flg)
509: {
510:   PetscErrorCode    ierr;
511:   PetscFileMode     mode;
512:   PetscErrorCode    (*f)(PetscViewer,PetscFileMode*) = NULL;

517:   PetscObjectQueryFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", &f);
518:   *flg = PETSC_FALSE;
519:   if (!f) return(0);
520:   (*f)(viewer, &mode);
521:   switch (mode) {
522:     case FILE_MODE_READ:
523:     case FILE_MODE_UPDATE:
524:     case FILE_MODE_APPEND_UPDATE:
525:       *flg = PETSC_TRUE;
526:     default: break;
527:   }
528:   return(0);
529: }

531: /*@
532:    PetscViewerWritable - Return a flag whether the viewer can be written to

534:    Not Collective

536:    Input Parameters:
537: .  viewer - the PetscViewer context

539:    Output Parameters:
540: .  flg - PETSC_TRUE if the viewer is writable, PETSC_FALSE otherwise

542:    Notes:
543:    PETSC_TRUE means viewer is in a mode allowing writing, i.e. PetscViewerFileGetMode()
544:    returns one of FILE_MODE_WRITE, FILE_MODE_APPEND, FILE_MODE_UPDATE, FILE_MODE_APPEND_UPDATE.

546:    Level: intermediate

548: .seealso: PetscViewerReadable(), PetscViewerCheckWritable(), PetscViewerCreate(), PetscViewerFileSetMode(), PetscViewerFileSetType()
549: @*/
550: PetscErrorCode  PetscViewerWritable(PetscViewer viewer, PetscBool *flg)
551: {
552:   PetscErrorCode    ierr;
553:   PetscFileMode     mode;
554:   PetscErrorCode    (*f)(PetscViewer,PetscFileMode*) = NULL;

559:   PetscObjectQueryFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", &f);
560:   *flg = PETSC_TRUE;
561:   if (!f) return(0);
562:   (*f)(viewer, &mode);
563:   if (mode == FILE_MODE_READ) *flg = PETSC_FALSE;
564:   return(0);
565: }

567: /*@
568:    PetscViewerCheckReadable - Check whether the viewer can be read from

570:    Collective

572:    Input Parameters:
573: .  viewer - the PetscViewer context

575:    Level: intermediate

577: .seealso: PetscViewerReadable(), PetscViewerCheckWritable(), PetscViewerCreate(), PetscViewerFileSetMode(), PetscViewerFileSetType()
578: @*/
579: PetscErrorCode  PetscViewerCheckReadable(PetscViewer viewer)
580: {
581:   PetscBool         flg;
582:   PetscErrorCode    ierr;

586:   PetscViewerReadable(viewer, &flg);
587:   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)");
588:   return(0);
589: }

591: /*@
592:    PetscViewerCheckWritable - Check whether the viewer can be written to

594:    Collective

596:    Input Parameters:
597: .  viewer - the PetscViewer context

599:    Level: intermediate

601: .seealso: PetscViewerWritable(), PetscViewerCheckReadable(), PetscViewerCreate(), PetscViewerFileSetMode(), PetscViewerFileSetType()
602: @*/
603: PetscErrorCode  PetscViewerCheckWritable(PetscViewer viewer)
604: {
605:   PetscBool         flg;
606:   PetscErrorCode    ierr;

610:   PetscViewerWritable(viewer, &flg);
611:   if (!flg) SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Viewer doesn't support writing, or is in FILE_MODE_READ mode");
612:   return(0);
613: }