Date: 2016-07-24T16:46:24+00:00

Actual source code: viewreg.c
petsc-3.7.3 2016-07-24
Report Typos and Errors

      2: #include <petsc/private/viewerimpl.h>  /*I "petscviewer.h" I*/
      3: #if defined(PETSC_HAVE_SAWS)
      4: #include <petscviewersaws.h>
      5: #endif

      7: PetscFunctionList PetscViewerList = 0;

     9:  #include ../src/sys/utils/hash.h

     12: PetscOptionsHelpPrinted PetscOptionsHelpPrintedSingleton = NULL;
     13: KHASH_SET_INIT_STR(HTPrinted)
     14: struct  _n_PetscOptionsHelpPrinted{
     15:   khash_t(HTPrinted) *printed;
     16:   PetscSegBuffer     strings;
     17: };

     21: PetscErrorCode PetscOptionsHelpPrintedDestroy(PetscOptionsHelpPrinted *hp)
     22: {

     26:   if (!*hp) return(0);
     27:   kh_destroy(HTPrinted,(*hp)->printed);
     28:   PetscSegBufferDestroy(&(*hp)->strings);
     29:   PetscFree(*hp);
     30:   return(0);
     31: }

     35: /*@C
     36:       PetscOptionsHelpPrintedCreate - Creates an object used to manage tracking which help messages have
     37:          been printed so they will not be printed again.

     39:      Not collective

     41:     Level: developer

     43: .seealso: PetscOptionsHelpPrintedCheck(), PetscOptionsHelpPrintChecked()
     44: @*/
     45: PetscErrorCode PetscOptionsHelpPrintedCreate(PetscOptionsHelpPrinted *hp)
     46: {
     47:   PetscErrorCode             ierr;

     50:   PetscNew(hp);
     51:   (*hp)->printed = kh_init(HTPrinted);
     52:   PetscSegBufferCreate(sizeof(char),10000,&(*hp)->strings);
     53:   return(0);
     54: }

     58: /*@C
     59:       PetscOptionsHelpPrintedCheck - Checks if a particular pre, name pair has previous been entered (meaning the help message was printed)

     61:      Not collective

     63:     Input Parameters:
     64: +     hp - the object used to manage tracking what help messages have been printed
     65: .     pre - the prefix part of the string, many be NULL
     66: -     name - the string to look for (cannot be NULL)

     68:     Output Parameter:
     69: .     found - PETSC_TRUE if the string was already set

     71:     Level: intermediate

     74: .seealso: PetscOptionsHelpPrintedCreate()
     75: @*/
     76: PetscErrorCode PetscOptionsHelpPrintedCheck(PetscOptionsHelpPrinted hp,const char *pre,const char* name,PetscBool *found)
     77: {
     78:   size_t          l1,l2;
     79:   char            *both;
     80:   khint_t         newitem;
     81:   PetscErrorCode  ierr;

     84:   PetscStrlen(pre,&l1);
     85:   PetscStrlen(name,&l2);
     86:   if (l1+l2 == 0) {
     87:     *found = PETSC_FALSE;
     88:     return(0);
     89:   }
     90:   PetscSegBufferGet(hp->strings,l1+l2+1,&both);
     91:   PetscStrcpy(both,pre);
     92:   PetscStrcat(both,name);
     93:   kh_put(HTPrinted,hp->printed,both,&newitem);
     94:   if (!newitem) {
     95:     PetscSegBufferUnuse(hp->strings,l1+l2+1);
     96:   }
     97:   *found = newitem ? PETSC_FALSE : PETSC_TRUE;
     98:   return(0);
     99: }

    103: /*@C
    104:    PetscOptionsGetViewer - Gets a viewer appropriate for the type indicated by the user

    106:    Collective on MPI_Comm

    108:    Input Parameters:
    109: +  comm - the communicator to own the viewer
    110: .  pre - the string to prepend to the name or NULL
    111: -  name - the option one is seeking

    113:    Output Parameter:
    114: +  viewer - the viewer, pass NULL if not needed
    115: .  format - the PetscViewerFormat requested by the user, pass NULL if not needed
    116: -  set - PETSC_TRUE if found, else PETSC_FALSE

    118:    Level: intermediate

    120:    Notes: If no value is provided ascii:stdout is used
    121: $       ascii[:[filename][:[format][:append]]]    defaults to stdout - format can be one of ascii_info, ascii_info_detail, or ascii_matlab,
    122:                                                   for example ascii::ascii_info prints just the information about the object not all details
    123:                                                   unless :append is given filename opens in write mode, overwriting what was already there
    124: $       binary[:[filename][:[format][:append]]]   defaults to the file binaryoutput
    125: $       draw[:drawtype]                           for example, draw:tikz  or draw:x
    126: $       socket[:port]                             defaults to the standard output port
    127: $       saws[:communicatorname]                    publishes object to the Scientific Application Webserver (SAWs)

    129:    Use PetscViewerDestroy() after using the viewer, otherwise a memory leak will occur

    131:    If PETSc is configured with --with-viewfromoptions=0 this function always returns with *set of PETSC_FALSE

    133: .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
    134:           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
    135:           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
    136:           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
    137:           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
    138:           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
    139:           PetscOptionsFList(), PetscOptionsEList()
    140: @*/
    141: PetscErrorCode  PetscOptionsGetViewer(MPI_Comm comm,const char pre[],const char name[],PetscViewer *viewer,PetscViewerFormat *format,PetscBool  *set)
    142: {
    143:   char                           *value;
    144:   PetscErrorCode                 ierr;
    145:   PetscBool                      flag,hashelp;

    150:   if (set) *set = PETSC_FALSE;
    151: #if defined(PETSC_SKIP_VIEWFROMOPTIONS)
    152:   return(0);
    153: #endif

    155:   PetscOptionsHasName(NULL,NULL,"-help",&hashelp);
    156:   if (hashelp) {
    157:     PetscBool found;

    159:     if (!PetscOptionsHelpPrintedSingleton) {
    160:       PetscOptionsHelpPrintedCreate(&PetscOptionsHelpPrintedSingleton);
    161:     }
    162:     PetscOptionsHelpPrintedCheck(PetscOptionsHelpPrintedSingleton,pre,name,&found);
    163:     if (!found) {
    164:       if (viewer) {
    165:         (*PetscHelpPrintf)(comm,"\n  -%s%s ascii[:[filename][:[format][:append]]]: %s (%s)\n",pre ? pre : "",name+1,"Prints object to stdout or ASCII file","PetscOptionsGetViewer");
    166:         (*PetscHelpPrintf)(comm,"    -%s%s binary[:[filename][:[format][:append]]]: %s (%s)\n",pre ? pre : "",name+1,"Saves object to a binary file","PetscOptionsGetViewer");
    167:         (*PetscHelpPrintf)(comm,"    -%s%s draw[:drawtype]: %s (%s)\n",pre ? pre : "",name+1,"Draws object","PetscOptionsGetViewer");
    168:         (*PetscHelpPrintf)(comm,"    -%s%s socket[:port]: %s (%s)\n",pre ? pre : "",name+1,"Pushes object to a Unix socket","PetscOptionsGetViewer");
    169:         (*PetscHelpPrintf)(comm,"    -%s%s saws[:communicatorname]: %s (%s)\n\n",pre ? pre : "",name+1,"Publishes object to SAWs","PetscOptionsGetViewer");
    170:       } else {
    171:         (*PetscHelpPrintf)(comm,"    -%s%s\n",pre ? pre : "",name+1);
    172:       }
    173:     }
    174:   }

    176:   if (format) *format = PETSC_VIEWER_DEFAULT;
    177:   PetscOptionsFindPair_Private(NULL,pre,name,&value,&flag);
    178:   if (flag) {
    179:     if (set) *set = PETSC_TRUE;
    180:     if (!value) {
    181:       if (viewer) {
    182:         PetscViewerASCIIGetStdout(comm,viewer);
    183:         PetscObjectReference((PetscObject)*viewer);
    184:       }
    185:     } else {
    186:       char       *loc0_vtype,*loc1_fname,*loc2_fmt = NULL,*loc3_fmode = NULL;
    187:       PetscInt   cnt;
    188:       const char *viewers[] = {PETSCVIEWERASCII,PETSCVIEWERBINARY,PETSCVIEWERDRAW,PETSCVIEWERSOCKET,PETSCVIEWERMATLAB,PETSCVIEWERSAWS,PETSCVIEWERVTK,PETSCVIEWERHDF5,0};

    190:       PetscStrallocpy(value,&loc0_vtype);
    191:       PetscStrchr(loc0_vtype,':',&loc1_fname);
    192:       if (loc1_fname) {
    193:         *loc1_fname++ = 0;
    194:         PetscStrchr(loc1_fname,':',&loc2_fmt);
    195:       }
    196:       if (loc2_fmt) {
    197:         *loc2_fmt++ = 0;
    198:         PetscStrchr(loc2_fmt,':',&loc3_fmode);
    199:       }
    200:       if (loc3_fmode) *loc3_fmode++ = 0;
    201:       PetscStrendswithwhich(*loc0_vtype ? loc0_vtype : "ascii",viewers,&cnt);
    202:       if (cnt > (PetscInt) sizeof(viewers)-1) SETERRQ1(comm,PETSC_ERR_ARG_OUTOFRANGE,"Unknown viewer type: %s",loc0_vtype);
    203:       if (viewer) {
    204:         if (!loc1_fname) {
    205:           switch (cnt) {
    206:           case 0:
    207:             PetscViewerASCIIGetStdout(comm,viewer);
    208:             break;
    209:           case 1:
    210:             if (!(*viewer = PETSC_VIEWER_BINARY_(comm))) CHKERRQ(PETSC_ERR_PLIB);
    211:             break;
    212:           case 2:
    213:             if (!(*viewer = PETSC_VIEWER_DRAW_(comm))) CHKERRQ(PETSC_ERR_PLIB);
    214:             break;
    215: #if defined(PETSC_USE_SOCKET_VIEWER)
    216:           case 3:
    217:             if (!(*viewer = PETSC_VIEWER_SOCKET_(comm))) CHKERRQ(PETSC_ERR_PLIB);
    218:             break;
    219: #endif
    220: #if defined(PETSC_HAVE_MATLAB_ENGINE)
    221:           case 4:
    222:             if (!(*viewer = PETSC_VIEWER_MATLAB_(comm))) CHKERRQ(PETSC_ERR_PLIB);
    223:             break;
    224: #endif
    225: #if defined(PETSC_HAVE_SAWS)
    226:           case 5:
    227:             if (!(*viewer = PETSC_VIEWER_SAWS_(comm))) CHKERRQ(PETSC_ERR_PLIB);
    228:             break;
    229: #endif
    230: #if defined(PETSC_HAVE_HDF5)
    231:           case 7:
    232:             if (!(*viewer = PETSC_VIEWER_HDF5_(comm))) CHKERRQ(PETSC_ERR_PLIB);
    233:             break;
    234: #endif
    235:           default: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unsupported viewer %s",loc0_vtype);
    236:           }
    237:           PetscObjectReference((PetscObject)*viewer);
    238:         } else {
    239:           if (loc2_fmt && !*loc1_fname && (cnt == 0)) { /* ASCII format without file name */
    240:             PetscViewerASCIIGetStdout(comm,viewer);
    241:             PetscObjectReference((PetscObject)*viewer);
    242:           } else {
    243:             PetscFileMode fmode;
    244:             PetscViewerCreate(comm,viewer);
    245:             PetscViewerSetType(*viewer,*loc0_vtype ? loc0_vtype : "ascii");
    246:             fmode = FILE_MODE_WRITE;
    247:             if (loc3_fmode && *loc3_fmode) { /* Has non-empty file mode ("write" or "append") */
    248:               PetscEnumFind(PetscFileModes,loc3_fmode,(PetscEnum*)&fmode,&flag);
    249:               if (!flag) SETERRQ1(comm,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown file mode: %s",loc3_fmode);
    250:             }
    251:             PetscViewerFileSetMode(*viewer,flag?fmode:FILE_MODE_WRITE);
    252:             PetscViewerFileSetName(*viewer,loc1_fname);
    253:             PetscViewerDrawSetDrawType(*viewer,loc1_fname);
    254:           }
    255:         }
    256:       }
    257:       if (viewer) {
    258:         PetscViewerSetUp(*viewer);
    259:       }
    260:       if (loc2_fmt && *loc2_fmt) {
    261:         PetscEnumFind(PetscViewerFormats,loc2_fmt,(PetscEnum*)format,&flag);
    262:         if (!flag) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unknown viewer format %s",loc2_fmt);
    263:       }
    264:       PetscFree(loc0_vtype);
    265:     }
    266:   }
    267:   return(0);
    268: }

    272: /*@
    273:    PetscViewerCreate - Creates a viewing context

    275:    Collective on MPI_Comm

    277:    Input Parameter:
    278: .  comm - MPI communicator

    280:    Output Parameter:
    281: .  inviewer - location to put the PetscViewer context

    283:    Level: advanced

    285:    Concepts: graphics^creating PetscViewer
    286:    Concepts: file input/output^creating PetscViewer
    287:    Concepts: sockets^creating PetscViewer

    289: .seealso: PetscViewerDestroy(), PetscViewerSetType(), PetscViewerType

    291: @*/
    292: PetscErrorCode  PetscViewerCreate(MPI_Comm comm,PetscViewer *inviewer)
    293: {
    294:   PetscViewer    viewer;

    298:   *inviewer = 0;
    299:   PetscViewerInitializePackage();
    300:   PetscHeaderCreate(viewer,PETSC_VIEWER_CLASSID,"PetscViewer","PetscViewer","Viewer",comm,PetscViewerDestroy,NULL);
    301:   *inviewer    = viewer;
    302:   viewer->data = 0;
    303:   return(0);
    304: }

    308: /*@C
    309:    PetscViewerSetType - Builds PetscViewer for a particular implementation.

    311:    Collective on PetscViewer

    313:    Input Parameter:
    314: +  viewer      - the PetscViewer context
    315: -  type        - for example, PETSCVIEWERASCII

    317:    Options Database Command:
    318: .  -draw_type  <type> - Sets the type; use -help for a list
    319:     of available methods (for instance, ascii)

    321:    Level: advanced

    323:    Notes:
    324:    See "include/petscviewer.h" for available methods (for instance,
    325:    PETSCVIEWERSOCKET)

    327: .seealso: PetscViewerCreate(), PetscViewerGetType(), PetscViewerType, PetscViewerPushFormat()
    328: @*/
    329: PetscErrorCode  PetscViewerSetType(PetscViewer viewer,PetscViewerType type)
    330: {
    331:   PetscErrorCode ierr,(*r)(PetscViewer);
    332:   PetscBool      match;

    337:   PetscObjectTypeCompare((PetscObject)viewer,type,&match);
    338:   if (match) return(0);

    340:   /* cleanup any old type that may be there */
    341:   if (viewer->data) {
    342:     (*viewer->ops->destroy)(viewer);

    344:     viewer->ops->destroy = NULL;
    345:     viewer->data         = 0;
    346:   }
    347:   PetscMemzero(viewer->ops,sizeof(struct _PetscViewerOps));

    349:    PetscFunctionListFind(PetscViewerList,type,&r);
    350:   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown PetscViewer type given: %s",type);

    352:   PetscObjectChangeTypeName((PetscObject)viewer,type);
    353:   (*r)(viewer);
    354:   return(0);
    355: }

    359: /*@C
    360:    PetscViewerRegister - Adds a viewer

    362:    Not Collective

    364:    Input Parameters:
    365: +  name_solver - name of a new user-defined viewer
    366: -  routine_create - routine to create method context

    368:    Level: developer
    369:    Notes:
    370:    PetscViewerRegister() may be called multiple times to add several user-defined viewers.

    372:    Sample usage:
    373: .vb
    374:    PetscViewerRegister("my_viewer_type",MyViewerCreate);
    375: .ve

    377:    Then, your solver can be chosen with the procedural interface via
    378: $     PetscViewerSetType(viewer,"my_viewer_type")
    379:    or at runtime via the option
    380: $     -viewer_type my_viewer_type

    382:   Concepts: registering^Viewers

    384: .seealso: PetscViewerRegisterAll(), PetscViewerRegisterDestroy()
    385:  @*/
    386: PetscErrorCode  PetscViewerRegister(const char *sname,PetscErrorCode (*function)(PetscViewer))
    387: {

    391:   PetscFunctionListAdd(&PetscViewerList,sname,function);
    392:   return(0);
    393: }

    397: /*@C
    398:    PetscViewerSetFromOptions - Sets the graphics type from the options database.
    399:       Defaults to a PETSc X windows graphics.

    401:    Collective on PetscViewer

    403:    Input Parameter:
    404: .     PetscViewer - the graphics context

    406:    Level: intermediate

    408:    Notes:
    409:     Must be called after PetscViewerCreate() before the PetscViewer is used.

    411:   Concepts: PetscViewer^setting options

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

    415: @*/
    416: PetscErrorCode  PetscViewerSetFromOptions(PetscViewer viewer)
    417: {
    418:   PetscErrorCode    ierr;
    419:   char              vtype[256];
    420:   PetscBool         flg;

    425:   if (!PetscViewerList) {
    426:     PetscViewerRegisterAll();
    427:   }
    428:   PetscObjectOptionsBegin((PetscObject)viewer);
    429:   PetscOptionsFList("-viewer_type","Type of PetscViewer","None",PetscViewerList,(char*)(((PetscObject)viewer)->type_name ? ((PetscObject)viewer)->type_name : PETSCVIEWERASCII),vtype,256,&flg);
    430:   if (flg) {
    431:     PetscViewerSetType(viewer,vtype);
    432:   }
    433:   /* type has not been set? */
    434:   if (!((PetscObject)viewer)->type_name) {
    435:     PetscViewerSetType(viewer,PETSCVIEWERASCII);
    436:   }
    437:   if (viewer->ops->setfromoptions) {
    438:     (*viewer->ops->setfromoptions)(PetscOptionsObject,viewer);
    439:   }

    441:   /* process any options handlers added with PetscObjectAddOptionsHandler() */
    442:   PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)viewer);
    443:   PetscViewerViewFromOptions(viewer,NULL,"-viewer_view");
    444:   PetscOptionsEnd();
    445:   return(0);
    446: }

    450: PetscErrorCode PetscViewerFlowControlStart(PetscViewer viewer,PetscInt *mcnt,PetscInt *cnt)
    451: {
    454:   PetscViewerBinaryGetFlowControl(viewer,mcnt);
    455:   PetscViewerBinaryGetFlowControl(viewer,cnt);
    456:   return(0);
    457: }

    461: PetscErrorCode PetscViewerFlowControlStepMaster(PetscViewer viewer,PetscInt i,PetscInt *mcnt,PetscInt cnt)
    462: {
    464:   MPI_Comm       comm;

    467:   PetscObjectGetComm((PetscObject)viewer,&comm);
    468:   if (i >= *mcnt) {
    469:     *mcnt += cnt;
    470:     MPI_Bcast(mcnt,1,MPIU_INT,0,comm);
    471:   }
    472:   return(0);
    473: }

    477: PetscErrorCode PetscViewerFlowControlEndMaster(PetscViewer viewer,PetscInt *mcnt)
    478: {
    480:   MPI_Comm       comm;
    482:   PetscObjectGetComm((PetscObject)viewer,&comm);
    483:   *mcnt = 0;
    484:   MPI_Bcast(mcnt,1,MPIU_INT,0,comm);
    485:   return(0);
    486: }

    490: PetscErrorCode PetscViewerFlowControlStepWorker(PetscViewer viewer,PetscMPIInt rank,PetscInt *mcnt)
    491: {
    493:   MPI_Comm       comm;
    495:   PetscObjectGetComm((PetscObject)viewer,&comm);
    496:   while (PETSC_TRUE) {
    497:     if (rank < *mcnt) break;
    498:     MPI_Bcast(mcnt,1,MPIU_INT,0,comm);
    499:   }
    500:   return(0);
    501: }

    505: PetscErrorCode PetscViewerFlowControlEndWorker(PetscViewer viewer,PetscInt *mcnt)
    506: {
    508:   MPI_Comm       comm;
    510:   PetscObjectGetComm((PetscObject)viewer,&comm);
    511:   while (PETSC_TRUE) {
    512:     MPI_Bcast(mcnt,1,MPIU_INT,0,comm);
    513:     if (!*mcnt) break;
    514:   }
    515:   return(0);
    516: }