Actual source code: view.c

petsc-3.12.5 2020-03-29
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:    PetscViewerView - Visualizes a viewer object.

336:    Collective on PetscViewer

338:    Input Parameters:
339: +  v - the viewer to be viewed
340: -  viewer - visualization context

342:   Notes:
343:   The available visualization contexts include
344: +    PETSC_VIEWER_STDOUT_SELF - standard output (default)
345: .    PETSC_VIEWER_STDOUT_WORLD - synchronized standard
346:         output where only the first processor opens
347:         the file.  All other processors send their
348:         data to the first processor to print.
349: -     PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure

351:    Level: beginner

353: .seealso: PetscViewerPushFormat(), PetscViewerASCIIOpen(), PetscViewerDrawOpen(),
354:           PetscViewerSocketOpen(), PetscViewerBinaryOpen(), PetscViewerLoad()
355: @*/
356: PetscErrorCode  PetscViewerView(PetscViewer v,PetscViewer viewer)
357: {
358:   PetscErrorCode    ierr;
359:   PetscBool         iascii;
360:   PetscViewerFormat format;
361: #if defined(PETSC_HAVE_SAWS)
362:   PetscBool         issaws;
363: #endif

368:   if (!viewer) {
369:     PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)v),&viewer);
370:   }

374:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
375: #if defined(PETSC_HAVE_SAWS)
376:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSAWS,&issaws);
377: #endif
378:   if (iascii) {
379:     PetscViewerGetFormat(viewer,&format);
380:     PetscObjectPrintClassNamePrefixType((PetscObject)v,viewer);
381:     if (format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
382:       if (v->format) {
383:         PetscViewerASCIIPrintf(viewer,"  Viewer format = %s\n",PetscViewerFormats[v->format]);
384:       }
385:       PetscViewerASCIIPushTab(viewer);
386:       if (v->ops->view) {
387:         (*v->ops->view)(v,viewer);
388:       }
389:       PetscViewerASCIIPopTab(viewer);
390:     }
391: #if defined(PETSC_HAVE_SAWS)
392:   } else if (issaws) {
393:     if (!((PetscObject)v)->amsmem) {
394:       PetscObjectViewSAWs((PetscObject)v,viewer);
395:       if (v->ops->view) {
396:         (*v->ops->view)(v,viewer);
397:       }
398:     }
399: #endif
400:   }
401:   return(0);
402: }

404: /*@C
405:    PetscViewerRead - Reads data from a PetscViewer

407:    Collective

409:    Input Parameters:
410: +  viewer   - The viewer
411: .  data     - Location to write the data
412: .  num      - Number of items of data to read
413: -  datatype - Type of data to read

415:    Output Parameters:
416: .  count - number of items of data actually read, or NULL

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

422:    Level: beginner

424: .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(),
425:           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
426:           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer
427: @*/
428: PetscErrorCode  PetscViewerRead(PetscViewer viewer, void *data, PetscInt num, PetscInt *count, PetscDataType dtype)
429: {

434:   if (dtype == PETSC_STRING) {
435:     PetscInt c, i = 0, cnt;
436:     char *s = (char *)data;
437:     if (num >= 0) {
438:       for (c = 0; c < num; c++) {
439:         /* Skip leading whitespaces */
440:         do {(*viewer->ops->read)(viewer, &(s[i]), 1, &cnt, PETSC_CHAR); if (!cnt) break;}
441:         while (s[i]=='\n' || s[i]=='\t' || s[i]==' ' || s[i]=='\0' || s[i]=='\v' || s[i]=='\f' || s[i]=='\r');
442:         i++;
443:         /* Read strings one char at a time */
444:         do {(*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR); if (!cnt) break;}
445:         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');
446:         /* Terminate final string */
447:         if (c == num-1) s[i-1] = '\0';
448:       }
449:     } else {
450:       /* Read until a \n is encountered (-num is the max size allowed) */
451:       do {(*viewer->ops->read)(viewer, &(s[i++]), 1, &cnt, PETSC_CHAR); if (i == -num || !cnt) break;}
452:       while (s[i-1]!='\n');
453:       /* Terminate final string */
454:       s[i-1] = '\0';
455:       c      = i;
456:     }
457:     if (count) *count = c;
458:     else if (c < num) SETERRQ2(PetscObjectComm((PetscObject) viewer), PETSC_ERR_FILE_READ, "Insufficient data, only read %D < %D strings", c, num);
459:   } else {
460:     (*viewer->ops->read)(viewer, data, num, count, dtype);
461:   }
462:   return(0);
463: }

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

468:    Not Collective

470:    Input Parameters:
471: .  viewer - the PetscViewer context

473:    Output Parameters:
474: .  flg - PETSC_TRUE if the viewer is readable, PETSC_FALSE otherwise

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

481:    Level: intermediate

483: .seealso: PetscViewerWritable(), PetscViewerCheckReadable(), PetscViewerCreate(), PetscViewerFileSetMode(), PetscViewerFileSetType()
484: @*/
485: PetscErrorCode  PetscViewerReadable(PetscViewer viewer, PetscBool *flg)
486: {
487:   PetscErrorCode    ierr;
488:   PetscFileMode     mode;
489:   PetscErrorCode    (*f)(PetscViewer,PetscFileMode*) = NULL;

494:   PetscObjectQueryFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", &f);
495:   *flg = PETSC_FALSE;
496:   if (!f) return(0);
497:   (*f)(viewer, &mode);
498:   switch (mode) {
499:     case FILE_MODE_READ:
500:     case FILE_MODE_UPDATE:
501:     case FILE_MODE_APPEND_UPDATE:
502:       *flg = PETSC_TRUE;
503:     default: break;
504:   }
505:   return(0);
506: }

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

511:    Not Collective

513:    Input Parameters:
514: .  viewer - the PetscViewer context

516:    Output Parameters:
517: .  flg - PETSC_TRUE if the viewer is writable, PETSC_FALSE otherwise

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

523:    Level: intermediate

525: .seealso: PetscViewerReadable(), PetscViewerCheckWritable(), PetscViewerCreate(), PetscViewerFileSetMode(), PetscViewerFileSetType()
526: @*/
527: PetscErrorCode  PetscViewerWritable(PetscViewer viewer, PetscBool *flg)
528: {
529:   PetscErrorCode    ierr;
530:   PetscFileMode     mode;
531:   PetscErrorCode    (*f)(PetscViewer,PetscFileMode*) = NULL;

536:   PetscObjectQueryFunction((PetscObject)viewer, "PetscViewerFileGetMode_C", &f);
537:   *flg = PETSC_TRUE;
538:   if (!f) return(0);
539:   (*f)(viewer, &mode);
540:   if (mode == FILE_MODE_READ) *flg = PETSC_FALSE;
541:   return(0);
542: }

544: /*@
545:    PetscViewerCheckReadable - Check whether the viewer can be read from

547:    Collective

549:    Input Parameters:
550: .  viewer - the PetscViewer context

552:    Level: intermediate

554: .seealso: PetscViewerReadable(), PetscViewerCheckWritable(), PetscViewerCreate(), PetscViewerFileSetMode(), PetscViewerFileSetType()
555: @*/
556: PetscErrorCode  PetscViewerCheckReadable(PetscViewer viewer)
557: {
558:   PetscBool         flg;
559:   PetscErrorCode    ierr;

563:   PetscViewerReadable(viewer, &flg);
564:   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)");
565:   return(0);
566: }

568: /*@
569:    PetscViewerCheckWritable - Check whether the viewer can be written to

571:    Collective

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

576:    Level: intermediate

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

587:   PetscViewerWritable(viewer, &flg);
588:   if (!flg) SETERRQ(PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Viewer doesn't support writing, or is in FILE_MODE_READ mode");
589:   return(0);
590: }