xref: /petsc/src/sys/classes/viewer/interface/viewreg.c (revision 6a9046bcf1dc7e213a87d3843bfa02f323786ad4)
1 
2 #include <petsc/private/viewerimpl.h>  /*I "petscviewer.h" I*/
3 #if defined(PETSC_HAVE_SAWS)
4 #include <petscviewersaws.h>
5 #endif
6 
7 PetscFunctionList PetscViewerList = 0;
8 
9 #undef __FUNCT__
10 #define __FUNCT__ "PetscOptionsGetViewer"
11 /*@C
12    PetscOptionsGetViewer - Gets a viewer appropriate for the type indicated by the user
13 
14    Collective on MPI_Comm
15 
16    Input Parameters:
17 +  comm - the communicator to own the viewer
18 .  pre - the string to prepend to the name or NULL
19 -  name - the option one is seeking
20 
21    Output Parameter:
22 +  viewer - the viewer, pass NULL if not needed
23 .  format - the PetscViewerFormat requested by the user, pass NULL if not needed
24 -  set - PETSC_TRUE if found, else PETSC_FALSE
25 
26    Level: intermediate
27 
28    Notes: If no value is provided ascii:stdout is used
29 $       ascii[:[filename][:[format][:append]]]    defaults to stdout - format can be one of ascii_info, ascii_info_detail, or ascii_matlab,
30                                                   for example ascii::ascii_info prints just the information about the object not all details
31                                                   unless :append is given filename opens in write mode, overwriting what was already there
32 $       binary[:[filename][:[format][:append]]]   defaults to the file binaryoutput
33 $       draw[:drawtype]                           for example, draw:tikz  or draw:x
34 $       socket[:port]                             defaults to the standard output port
35 $       saws[:communicatorname]                    publishes object to the Scientific Application Webserver (SAWs)
36 
37    Use PetscViewerDestroy() after using the viewer, otherwise a memory leak will occur
38 
39 .seealso: PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
40           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
41           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
42           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
43           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
44           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
45           PetscOptionsFList(), PetscOptionsEList()
46 @*/
47 PetscErrorCode  PetscOptionsGetViewer(MPI_Comm comm,const char pre[],const char name[],PetscViewer *viewer,PetscViewerFormat *format,PetscBool  *set)
48 {
49   char           *value;
50   PetscErrorCode ierr;
51   PetscBool      flag,hashelp;
52 
53   PetscFunctionBegin;
54   PetscValidCharPointer(name,3);
55 
56   ierr = PetscOptionsHasName(NULL,NULL,"-help",&hashelp);CHKERRQ(ierr);
57   if (hashelp) {
58     ierr = (*PetscHelpPrintf)(comm,"  -%s%s ascii[:[filename][:[format][:append]]]: %s (%s)\n",pre ? pre : "",name+1,"Triggers display of a PETSc object to screen or ASCII file","PetscOptionsGetViewer");CHKERRQ(ierr);
59     ierr = (*PetscHelpPrintf)(comm,"  -%s%s binary[:[filename][:[format][:append]]]: %s (%s)\n",pre ? pre : "",name+1,"Triggers saving of a PETSc object to a binary file","PetscOptionsGetViewer");CHKERRQ(ierr);
60     ierr = (*PetscHelpPrintf)(comm,"  -%s%s draw[:drawtype]: %s (%s)\n",pre ? pre : "",name+1,"Triggers drawing of a PETSc object","PetscOptionsGetViewer");CHKERRQ(ierr);
61     ierr = (*PetscHelpPrintf)(comm,"  -%s%s socket[:port]: %s (%s)\n",pre ? pre : "",name+1,"Triggers push of a PETSc object to a Unix socket","PetscOptionsGetViewer");CHKERRQ(ierr);
62     ierr = (*PetscHelpPrintf)(comm,"  -%s%s saws[:communicatorname]: %s (%s)\n",pre ? pre : "",name+1,"Triggers publishing of a PETSc object to SAWs","PetscOptionsGetViewer");CHKERRQ(ierr);
63   }
64 
65   if (format) *format = PETSC_VIEWER_DEFAULT;
66   if (set) *set = PETSC_FALSE;
67   ierr = PetscOptionsFindPair_Private(NULL,pre,name,&value,&flag);CHKERRQ(ierr);
68   if (flag) {
69     if (set) *set = PETSC_TRUE;
70     if (!value) {
71       if (viewer) {
72         ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr);
73         ierr = PetscObjectReference((PetscObject)*viewer);CHKERRQ(ierr);
74       }
75     } else {
76       char       *loc0_vtype,*loc1_fname,*loc2_fmt = NULL,*loc3_fmode = NULL;
77       PetscInt   cnt;
78       const char *viewers[] = {PETSCVIEWERASCII,PETSCVIEWERBINARY,PETSCVIEWERDRAW,PETSCVIEWERSOCKET,PETSCVIEWERMATLAB,PETSCVIEWERSAWS,PETSCVIEWERVTK,PETSCVIEWERHDF5,0};
79 
80       ierr = PetscStrallocpy(value,&loc0_vtype);CHKERRQ(ierr);
81       ierr = PetscStrchr(loc0_vtype,':',&loc1_fname);CHKERRQ(ierr);
82       if (loc1_fname) {
83         *loc1_fname++ = 0;
84         ierr = PetscStrchr(loc1_fname,':',&loc2_fmt);CHKERRQ(ierr);
85       }
86       if (loc2_fmt) {
87         *loc2_fmt++ = 0;
88         ierr = PetscStrchr(loc2_fmt,':',&loc3_fmode);CHKERRQ(ierr);
89       }
90       if (loc3_fmode) *loc3_fmode++ = 0;
91       ierr = PetscStrendswithwhich(*loc0_vtype ? loc0_vtype : "ascii",viewers,&cnt);CHKERRQ(ierr);
92       if (cnt > (PetscInt) sizeof(viewers)-1) SETERRQ1(comm,PETSC_ERR_ARG_OUTOFRANGE,"Unknown viewer type: %s",loc0_vtype);
93       if (viewer) {
94         if (!loc1_fname) {
95           switch (cnt) {
96           case 0:
97             ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr);
98             break;
99           case 1:
100             if (!(*viewer = PETSC_VIEWER_BINARY_(comm))) CHKERRQ(PETSC_ERR_PLIB);
101             break;
102           case 2:
103             if (!(*viewer = PETSC_VIEWER_DRAW_(comm))) CHKERRQ(PETSC_ERR_PLIB);
104             break;
105 #if defined(PETSC_USE_SOCKET_VIEWER)
106           case 3:
107             if (!(*viewer = PETSC_VIEWER_SOCKET_(comm))) CHKERRQ(PETSC_ERR_PLIB);
108             break;
109 #endif
110 #if defined(PETSC_HAVE_MATLAB_ENGINE)
111           case 4:
112             if (!(*viewer = PETSC_VIEWER_MATLAB_(comm))) CHKERRQ(PETSC_ERR_PLIB);
113             break;
114 #endif
115 #if defined(PETSC_HAVE_SAWS)
116           case 5:
117             if (!(*viewer = PETSC_VIEWER_SAWS_(comm))) CHKERRQ(PETSC_ERR_PLIB);
118             break;
119 #endif
120 #if defined(PETSC_HAVE_HDF5)
121           case 7:
122             if (!(*viewer = PETSC_VIEWER_HDF5_(comm))) CHKERRQ(PETSC_ERR_PLIB);
123             break;
124 #endif
125           default: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unsupported viewer %s",loc0_vtype);
126           }
127           ierr = PetscObjectReference((PetscObject)*viewer);CHKERRQ(ierr);
128         } else {
129           if (loc2_fmt && !*loc1_fname && (cnt == 0)) { /* ASCII format without file name */
130             ierr = PetscViewerASCIIGetStdout(comm,viewer);CHKERRQ(ierr);
131             ierr = PetscObjectReference((PetscObject)*viewer);CHKERRQ(ierr);
132           } else {
133             PetscFileMode fmode;
134             ierr = PetscViewerCreate(comm,viewer);CHKERRQ(ierr);
135             ierr = PetscViewerSetType(*viewer,*loc0_vtype ? loc0_vtype : "ascii");CHKERRQ(ierr);
136             fmode = FILE_MODE_WRITE;
137             if (loc3_fmode && *loc3_fmode) { /* Has non-empty file mode ("write" or "append") */
138               ierr = PetscEnumFind(PetscFileModes,loc3_fmode,(PetscEnum*)&fmode,&flag);CHKERRQ(ierr);
139               if (!flag) SETERRQ1(comm,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown file mode: %s",loc3_fmode);
140             }
141             ierr = PetscViewerFileSetMode(*viewer,flag?fmode:FILE_MODE_WRITE);CHKERRQ(ierr);
142             ierr = PetscViewerFileSetName(*viewer,loc1_fname);CHKERRQ(ierr);
143             ierr = PetscViewerDrawSetDrawType(*viewer,loc1_fname);CHKERRQ(ierr);
144           }
145         }
146       }
147       if (viewer) {
148         ierr = PetscViewerSetUp(*viewer);CHKERRQ(ierr);
149       }
150       if (loc2_fmt && *loc2_fmt) {
151         ierr = PetscEnumFind(PetscViewerFormats,loc2_fmt,(PetscEnum*)format,&flag);CHKERRQ(ierr);
152         if (!flag) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unknown viewer format %s",loc2_fmt);CHKERRQ(ierr);
153       }
154       ierr = PetscFree(loc0_vtype);CHKERRQ(ierr);
155     }
156   }
157   PetscFunctionReturn(0);
158 }
159 
160 #undef __FUNCT__
161 #define __FUNCT__ "PetscViewerCreate"
162 /*@
163    PetscViewerCreate - Creates a viewing context
164 
165    Collective on MPI_Comm
166 
167    Input Parameter:
168 .  comm - MPI communicator
169 
170    Output Parameter:
171 .  inviewer - location to put the PetscViewer context
172 
173    Level: advanced
174 
175    Concepts: graphics^creating PetscViewer
176    Concepts: file input/output^creating PetscViewer
177    Concepts: sockets^creating PetscViewer
178 
179 .seealso: PetscViewerDestroy(), PetscViewerSetType(), PetscViewerType
180 
181 @*/
182 PetscErrorCode  PetscViewerCreate(MPI_Comm comm,PetscViewer *inviewer)
183 {
184   PetscViewer    viewer;
185   PetscErrorCode ierr;
186 
187   PetscFunctionBegin;
188   *inviewer = 0;
189   ierr = PetscViewerInitializePackage();CHKERRQ(ierr);
190   ierr         = PetscHeaderCreate(viewer,PETSC_VIEWER_CLASSID,"PetscViewer","PetscViewer","Viewer",comm,PetscViewerDestroy,NULL);CHKERRQ(ierr);
191   *inviewer    = viewer;
192   viewer->data = 0;
193   PetscFunctionReturn(0);
194 }
195 
196 #undef __FUNCT__
197 #define __FUNCT__ "PetscViewerSetType"
198 /*@C
199    PetscViewerSetType - Builds PetscViewer for a particular implementation.
200 
201    Collective on PetscViewer
202 
203    Input Parameter:
204 +  viewer      - the PetscViewer context
205 -  type        - for example, PETSCVIEWERASCII
206 
207    Options Database Command:
208 .  -draw_type  <type> - Sets the type; use -help for a list
209     of available methods (for instance, ascii)
210 
211    Level: advanced
212 
213    Notes:
214    See "include/petscviewer.h" for available methods (for instance,
215    PETSCVIEWERSOCKET)
216 
217 .seealso: PetscViewerCreate(), PetscViewerGetType(), PetscViewerType, PetscViewerPushFormat()
218 @*/
219 PetscErrorCode  PetscViewerSetType(PetscViewer viewer,PetscViewerType type)
220 {
221   PetscErrorCode ierr,(*r)(PetscViewer);
222   PetscBool      match;
223 
224   PetscFunctionBegin;
225   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
226   PetscValidCharPointer(type,2);
227   ierr = PetscObjectTypeCompare((PetscObject)viewer,type,&match);CHKERRQ(ierr);
228   if (match) PetscFunctionReturn(0);
229 
230   /* cleanup any old type that may be there */
231   if (viewer->data) {
232     ierr         = (*viewer->ops->destroy)(viewer);CHKERRQ(ierr);
233 
234     viewer->ops->destroy = NULL;
235     viewer->data         = 0;
236   }
237   ierr = PetscMemzero(viewer->ops,sizeof(struct _PetscViewerOps));CHKERRQ(ierr);
238 
239   ierr =  PetscFunctionListFind(PetscViewerList,type,&r);CHKERRQ(ierr);
240   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown PetscViewer type given: %s",type);
241 
242   ierr = PetscObjectChangeTypeName((PetscObject)viewer,type);CHKERRQ(ierr);
243   ierr = (*r)(viewer);CHKERRQ(ierr);
244   PetscFunctionReturn(0);
245 }
246 
247 #undef __FUNCT__
248 #define __FUNCT__ "PetscViewerRegister"
249 /*@C
250    PetscViewerRegister - Adds a viewer
251 
252    Not Collective
253 
254    Input Parameters:
255 +  name_solver - name of a new user-defined viewer
256 -  routine_create - routine to create method context
257 
258    Level: developer
259    Notes:
260    PetscViewerRegister() may be called multiple times to add several user-defined viewers.
261 
262    Sample usage:
263 .vb
264    PetscViewerRegister("my_viewer_type",MyViewerCreate);
265 .ve
266 
267    Then, your solver can be chosen with the procedural interface via
268 $     PetscViewerSetType(viewer,"my_viewer_type")
269    or at runtime via the option
270 $     -viewer_type my_viewer_type
271 
272   Concepts: registering^Viewers
273 
274 .seealso: PetscViewerRegisterAll(), PetscViewerRegisterDestroy()
275  @*/
276 PetscErrorCode  PetscViewerRegister(const char *sname,PetscErrorCode (*function)(PetscViewer))
277 {
278   PetscErrorCode ierr;
279 
280   PetscFunctionBegin;
281   ierr = PetscFunctionListAdd(&PetscViewerList,sname,function);CHKERRQ(ierr);
282   PetscFunctionReturn(0);
283 }
284 
285 #undef __FUNCT__
286 #define __FUNCT__ "PetscViewerSetFromOptions"
287 /*@C
288    PetscViewerSetFromOptions - Sets the graphics type from the options database.
289       Defaults to a PETSc X windows graphics.
290 
291    Collective on PetscViewer
292 
293    Input Parameter:
294 .     PetscViewer - the graphics context
295 
296    Level: intermediate
297 
298    Notes:
299     Must be called after PetscViewerCreate() before the PetscViewer is used.
300 
301   Concepts: PetscViewer^setting options
302 
303 .seealso: PetscViewerCreate(), PetscViewerSetType(), PetscViewerType
304 
305 @*/
306 PetscErrorCode  PetscViewerSetFromOptions(PetscViewer viewer)
307 {
308   PetscErrorCode    ierr;
309   char              vtype[256];
310   PetscBool         flg;
311 
312   PetscFunctionBegin;
313   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
314 
315   if (!PetscViewerList) {
316     ierr = PetscViewerRegisterAll();CHKERRQ(ierr);
317   }
318   ierr = PetscObjectOptionsBegin((PetscObject)viewer);CHKERRQ(ierr);
319   ierr = PetscOptionsFList("-viewer_type","Type of PetscViewer","None",PetscViewerList,(char*)(((PetscObject)viewer)->type_name ? ((PetscObject)viewer)->type_name : PETSCVIEWERASCII),vtype,256,&flg);CHKERRQ(ierr);
320   if (flg) {
321     ierr = PetscViewerSetType(viewer,vtype);CHKERRQ(ierr);
322   }
323   /* type has not been set? */
324   if (!((PetscObject)viewer)->type_name) {
325     ierr = PetscViewerSetType(viewer,PETSCVIEWERASCII);CHKERRQ(ierr);
326   }
327   if (viewer->ops->setfromoptions) {
328     ierr = (*viewer->ops->setfromoptions)(PetscOptionsObject,viewer);CHKERRQ(ierr);
329   }
330 
331   /* process any options handlers added with PetscObjectAddOptionsHandler() */
332   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject)viewer);CHKERRQ(ierr);
333   ierr = PetscViewerViewFromOptions(viewer,NULL,"-viewer_view");CHKERRQ(ierr);
334   ierr = PetscOptionsEnd();CHKERRQ(ierr);
335   PetscFunctionReturn(0);
336 }
337 
338 #undef __FUNCT__
339 #define __FUNCT__ "PetscViewerFlowControlStart"
340 PetscErrorCode PetscViewerFlowControlStart(PetscViewer viewer,PetscInt *mcnt,PetscInt *cnt)
341 {
342   PetscErrorCode ierr;
343   PetscFunctionBegin;
344   ierr = PetscViewerBinaryGetFlowControl(viewer,mcnt);CHKERRQ(ierr);
345   ierr = PetscViewerBinaryGetFlowControl(viewer,cnt);CHKERRQ(ierr);
346   PetscFunctionReturn(0);
347 }
348 
349 #undef __FUNCT__
350 #define __FUNCT__ "PetscViewerFlowControlStepMaster"
351 PetscErrorCode PetscViewerFlowControlStepMaster(PetscViewer viewer,PetscInt i,PetscInt *mcnt,PetscInt cnt)
352 {
353   PetscErrorCode ierr;
354   MPI_Comm       comm;
355 
356   PetscFunctionBegin;
357   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
358   if (i >= *mcnt) {
359     *mcnt += cnt;
360     ierr = MPI_Bcast(mcnt,1,MPIU_INT,0,comm);CHKERRQ(ierr);
361   }
362   PetscFunctionReturn(0);
363 }
364 
365 #undef __FUNCT__
366 #define __FUNCT__ "PetscViewerFlowControlEndMaster"
367 PetscErrorCode PetscViewerFlowControlEndMaster(PetscViewer viewer,PetscInt *mcnt)
368 {
369   PetscErrorCode ierr;
370   MPI_Comm       comm;
371   PetscFunctionBegin;
372   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
373   *mcnt = 0;
374   ierr = MPI_Bcast(mcnt,1,MPIU_INT,0,comm);CHKERRQ(ierr);
375   PetscFunctionReturn(0);
376 }
377 
378 #undef __FUNCT__
379 #define __FUNCT__ "PetscViewerFlowControlStepWorker"
380 PetscErrorCode PetscViewerFlowControlStepWorker(PetscViewer viewer,PetscMPIInt rank,PetscInt *mcnt)
381 {
382   PetscErrorCode ierr;
383   MPI_Comm       comm;
384   PetscFunctionBegin;
385   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
386   while (PETSC_TRUE) {
387     if (rank < *mcnt) break;
388     ierr = MPI_Bcast(mcnt,1,MPIU_INT,0,comm);CHKERRQ(ierr);
389   }
390   PetscFunctionReturn(0);
391 }
392 
393 #undef __FUNCT__
394 #define __FUNCT__ "PetscViewerFlowControlEndWorker"
395 PetscErrorCode PetscViewerFlowControlEndWorker(PetscViewer viewer,PetscInt *mcnt)
396 {
397   PetscErrorCode ierr;
398   MPI_Comm       comm;
399   PetscFunctionBegin;
400   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
401   while (PETSC_TRUE) {
402     ierr = MPI_Bcast(mcnt,1,MPIU_INT,0,comm);CHKERRQ(ierr);
403     if (!*mcnt) break;
404   }
405   PetscFunctionReturn(0);
406 }
407