Actual source code: viewreg.c

petsc-master 2016-08-26
Report Typos and Errors
 2:  #include <petsc/private/viewerimpl.h>
  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[:filename]]                for example, draw:tikz, draw:tikz:figure.tex  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[:filename]] %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:             if (loc2_fmt) {
252:               PetscBool tk;
253:               PetscStrcmp(loc1_fname,"tikz",&tk);
254:               if (tk) {
255:                 PetscViewerDrawSetInfo(*viewer,NULL,loc2_fmt,0,0,0,0);
256:                 *loc2_fmt = 0;
257:               }
258:             }
259:             PetscViewerFileSetMode(*viewer,flag?fmode:FILE_MODE_WRITE);
260:             PetscViewerFileSetName(*viewer,loc1_fname);
261:             PetscViewerDrawSetDrawType(*viewer,loc1_fname);
262:           }
263:         }
264:       }
265:       if (viewer) {
266:         PetscViewerSetUp(*viewer);
267:       }
268:       if (loc2_fmt && *loc2_fmt) {
269:         PetscEnumFind(PetscViewerFormats,loc2_fmt,(PetscEnum*)format,&flag);
270:         if (!flag) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unknown viewer format %s",loc2_fmt);
271:       }
272:       PetscFree(loc0_vtype);
273:     }
274:   }
275:   return(0);
276: }

280: /*@
281:    PetscViewerCreate - Creates a viewing context

283:    Collective on MPI_Comm

285:    Input Parameter:
286: .  comm - MPI communicator

288:    Output Parameter:
289: .  inviewer - location to put the PetscViewer context

291:    Level: advanced

293:    Concepts: graphics^creating PetscViewer
294:    Concepts: file input/output^creating PetscViewer
295:    Concepts: sockets^creating PetscViewer

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

299: @*/
300: PetscErrorCode  PetscViewerCreate(MPI_Comm comm,PetscViewer *inviewer)
301: {
302:   PetscViewer    viewer;

306:   *inviewer = 0;
307:   PetscViewerInitializePackage();
308:   PetscHeaderCreate(viewer,PETSC_VIEWER_CLASSID,"PetscViewer","PetscViewer","Viewer",comm,PetscViewerDestroy,NULL);
309:   *inviewer    = viewer;
310:   viewer->data = 0;
311:   return(0);
312: }

316: /*@C
317:    PetscViewerSetType - Builds PetscViewer for a particular implementation.

319:    Collective on PetscViewer

321:    Input Parameter:
322: +  viewer      - the PetscViewer context
323: -  type        - for example, PETSCVIEWERASCII

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

329:    Level: advanced

331:    Notes:
332:    See "include/petscviewer.h" for available methods (for instance,
333:    PETSCVIEWERSOCKET)

335: .seealso: PetscViewerCreate(), PetscViewerGetType(), PetscViewerType, PetscViewerPushFormat()
336: @*/
337: PetscErrorCode  PetscViewerSetType(PetscViewer viewer,PetscViewerType type)
338: {
339:   PetscErrorCode ierr,(*r)(PetscViewer);
340:   PetscBool      match;

345:   PetscObjectTypeCompare((PetscObject)viewer,type,&match);
346:   if (match) return(0);

348:   /* cleanup any old type that may be there */
349:   if (viewer->data) {
350:     (*viewer->ops->destroy)(viewer);

352:     viewer->ops->destroy = NULL;
353:     viewer->data         = 0;
354:   }
355:   PetscMemzero(viewer->ops,sizeof(struct _PetscViewerOps));

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

360:   PetscObjectChangeTypeName((PetscObject)viewer,type);
361:   (*r)(viewer);
362:   return(0);
363: }

367: /*@C
368:    PetscViewerRegister - Adds a viewer

370:    Not Collective

372:    Input Parameters:
373: +  name_solver - name of a new user-defined viewer
374: -  routine_create - routine to create method context

376:    Level: developer
377:    Notes:
378:    PetscViewerRegister() may be called multiple times to add several user-defined viewers.

380:    Sample usage:
381: .vb
382:    PetscViewerRegister("my_viewer_type",MyViewerCreate);
383: .ve

385:    Then, your solver can be chosen with the procedural interface via
386: $     PetscViewerSetType(viewer,"my_viewer_type")
387:    or at runtime via the option
388: $     -viewer_type my_viewer_type

390:   Concepts: registering^Viewers

392: .seealso: PetscViewerRegisterAll(), PetscViewerRegisterDestroy()
393:  @*/
394: PetscErrorCode  PetscViewerRegister(const char *sname,PetscErrorCode (*function)(PetscViewer))
395: {

399:   PetscFunctionListAdd(&PetscViewerList,sname,function);
400:   return(0);
401: }

405: /*@C
406:    PetscViewerSetFromOptions - Sets the graphics type from the options database.
407:       Defaults to a PETSc X windows graphics.

409:    Collective on PetscViewer

411:    Input Parameter:
412: .     PetscViewer - the graphics context

414:    Level: intermediate

416:    Notes:
417:     Must be called after PetscViewerCreate() before the PetscViewer is used.

419:   Concepts: PetscViewer^setting options

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

423: @*/
424: PetscErrorCode  PetscViewerSetFromOptions(PetscViewer viewer)
425: {
426:   PetscErrorCode    ierr;
427:   char              vtype[256];
428:   PetscBool         flg;


433:   if (!PetscViewerList) {
434:     PetscViewerRegisterAll();
435:   }
436:   PetscObjectOptionsBegin((PetscObject)viewer);
437:   PetscOptionsFList("-viewer_type","Type of PetscViewer","None",PetscViewerList,(char*)(((PetscObject)viewer)->type_name ? ((PetscObject)viewer)->type_name : PETSCVIEWERASCII),vtype,256,&flg);
438:   if (flg) {
439:     PetscViewerSetType(viewer,vtype);
440:   }
441:   /* type has not been set? */
442:   if (!((PetscObject)viewer)->type_name) {
443:     PetscViewerSetType(viewer,PETSCVIEWERASCII);
444:   }
445:   if (viewer->ops->setfromoptions) {
446:     (*viewer->ops->setfromoptions)(PetscOptionsObject,viewer);
447:   }

449:   /* process any options handlers added with PetscObjectAddOptionsHandler() */
450:   PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)viewer);
451:   PetscViewerViewFromOptions(viewer,NULL,"-viewer_view");
452:   PetscOptionsEnd();
453:   return(0);
454: }

458: PetscErrorCode PetscViewerFlowControlStart(PetscViewer viewer,PetscInt *mcnt,PetscInt *cnt)
459: {
462:   PetscViewerBinaryGetFlowControl(viewer,mcnt);
463:   PetscViewerBinaryGetFlowControl(viewer,cnt);
464:   return(0);
465: }

469: PetscErrorCode PetscViewerFlowControlStepMaster(PetscViewer viewer,PetscInt i,PetscInt *mcnt,PetscInt cnt)
470: {
472:   MPI_Comm       comm;

475:   PetscObjectGetComm((PetscObject)viewer,&comm);
476:   if (i >= *mcnt) {
477:     *mcnt += cnt;
478:     MPI_Bcast(mcnt,1,MPIU_INT,0,comm);
479:   }
480:   return(0);
481: }

485: PetscErrorCode PetscViewerFlowControlEndMaster(PetscViewer viewer,PetscInt *mcnt)
486: {
488:   MPI_Comm       comm;
490:   PetscObjectGetComm((PetscObject)viewer,&comm);
491:   *mcnt = 0;
492:   MPI_Bcast(mcnt,1,MPIU_INT,0,comm);
493:   return(0);
494: }

498: PetscErrorCode PetscViewerFlowControlStepWorker(PetscViewer viewer,PetscMPIInt rank,PetscInt *mcnt)
499: {
501:   MPI_Comm       comm;
503:   PetscObjectGetComm((PetscObject)viewer,&comm);
504:   while (PETSC_TRUE) {
505:     if (rank < *mcnt) break;
506:     MPI_Bcast(mcnt,1,MPIU_INT,0,comm);
507:   }
508:   return(0);
509: }

513: PetscErrorCode PetscViewerFlowControlEndWorker(PetscViewer viewer,PetscInt *mcnt)
514: {
516:   MPI_Comm       comm;
518:   PetscObjectGetComm((PetscObject)viewer,&comm);
519:   while (PETSC_TRUE) {
520:     MPI_Bcast(mcnt,1,MPIU_INT,0,comm);
521:     if (!*mcnt) break;
522:   }
523:   return(0);
524: }