xref: /petsc/src/sys/classes/viewer/impls/binary/binv.c (revision 5c6c1daec53e1d9ab0bec9db5309fd8fc7645b8d)
1*5c6c1daeSBarry Smith 
2*5c6c1daeSBarry Smith #include <petsc-private/viewerimpl.h>    /*I   "petscsys.h"   I*/
3*5c6c1daeSBarry Smith #include <fcntl.h>
4*5c6c1daeSBarry Smith #if defined(PETSC_HAVE_UNISTD_H)
5*5c6c1daeSBarry Smith #include <unistd.h>
6*5c6c1daeSBarry Smith #endif
7*5c6c1daeSBarry Smith #if defined (PETSC_HAVE_IO_H)
8*5c6c1daeSBarry Smith #include <io.h>
9*5c6c1daeSBarry Smith #endif
10*5c6c1daeSBarry Smith 
11*5c6c1daeSBarry Smith typedef struct  {
12*5c6c1daeSBarry Smith   int           fdes;            /* file descriptor, ignored if using MPI IO */
13*5c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
14*5c6c1daeSBarry Smith   PetscBool     MPIIO;
15*5c6c1daeSBarry Smith   MPI_File      mfdes;           /* ignored unless using MPI IO */
16*5c6c1daeSBarry Smith   MPI_Offset    moff;
17*5c6c1daeSBarry Smith #endif
18*5c6c1daeSBarry Smith   PetscFileMode btype;           /* read or write? */
19*5c6c1daeSBarry Smith   FILE          *fdes_info;      /* optional file containing info on binary file*/
20*5c6c1daeSBarry Smith   PetscBool     storecompressed; /* gzip the write binary file when closing it*/
21*5c6c1daeSBarry Smith   char          *filename;
22*5c6c1daeSBarry Smith   PetscBool     skipinfo;        /* Don't create info file for writing; don't use for reading */
23*5c6c1daeSBarry Smith   PetscBool     skipoptions;     /* don't use PETSc options database when loading */
24*5c6c1daeSBarry Smith   PetscInt      flowcontrol;     /* allow only <flowcontrol> messages outstanding at a time while doing IO */
25*5c6c1daeSBarry Smith   PetscBool     skipheader;      /* don't write header, only raw data */
26*5c6c1daeSBarry Smith } PetscViewer_Binary;
27*5c6c1daeSBarry Smith 
28*5c6c1daeSBarry Smith #undef __FUNCT__
29*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerGetSingleton_Binary"
30*5c6c1daeSBarry Smith PetscErrorCode PetscViewerGetSingleton_Binary(PetscViewer viewer,PetscViewer *outviewer)
31*5c6c1daeSBarry Smith {
32*5c6c1daeSBarry Smith   int                rank;
33*5c6c1daeSBarry Smith   PetscErrorCode     ierr;
34*5c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data,*obinary;
35*5c6c1daeSBarry Smith 
36*5c6c1daeSBarry Smith   PetscFunctionBegin;
37*5c6c1daeSBarry Smith   ierr = MPI_Comm_rank(((PetscObject)viewer)->comm,&rank);CHKERRQ(ierr);
38*5c6c1daeSBarry Smith   if (!rank) {
39*5c6c1daeSBarry Smith     ierr    = PetscViewerCreate(PETSC_COMM_SELF,outviewer);CHKERRQ(ierr);
40*5c6c1daeSBarry Smith     ierr    = PetscViewerSetType(*outviewer,PETSCVIEWERBINARY);CHKERRQ(ierr);
41*5c6c1daeSBarry Smith     obinary = (PetscViewer_Binary*)(*outviewer)->data;
42*5c6c1daeSBarry Smith     ierr    = PetscMemcpy(obinary,vbinary,sizeof(PetscViewer_Binary));CHKERRQ(ierr);
43*5c6c1daeSBarry Smith   } else {
44*5c6c1daeSBarry Smith     *outviewer = 0;
45*5c6c1daeSBarry Smith   }
46*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
47*5c6c1daeSBarry Smith }
48*5c6c1daeSBarry Smith 
49*5c6c1daeSBarry Smith #undef __FUNCT__
50*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerRestoreSingleton_Binary"
51*5c6c1daeSBarry Smith PetscErrorCode PetscViewerRestoreSingleton_Binary(PetscViewer viewer,PetscViewer *outviewer)
52*5c6c1daeSBarry Smith {
53*5c6c1daeSBarry Smith   PetscErrorCode ierr;
54*5c6c1daeSBarry Smith   PetscErrorCode rank;
55*5c6c1daeSBarry Smith 
56*5c6c1daeSBarry Smith   PetscFunctionBegin;
57*5c6c1daeSBarry Smith   ierr = MPI_Comm_rank(((PetscObject)viewer)->comm,&rank);CHKERRQ(ierr);
58*5c6c1daeSBarry Smith   if (!rank) {
59*5c6c1daeSBarry Smith     ierr = PetscFree((*outviewer)->data);CHKERRQ(ierr);
60*5c6c1daeSBarry Smith     ierr = PetscHeaderDestroy(outviewer);CHKERRQ(ierr);
61*5c6c1daeSBarry Smith   }
62*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
63*5c6c1daeSBarry Smith }
64*5c6c1daeSBarry Smith 
65*5c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
66*5c6c1daeSBarry Smith #undef __FUNCT__
67*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetMPIIOOffset"
68*5c6c1daeSBarry Smith /*@C
69*5c6c1daeSBarry Smith     PetscViewerBinaryGetMPIIOOffset - Gets the current offset that should be passed to MPI_File_set_view()
70*5c6c1daeSBarry Smith 
71*5c6c1daeSBarry Smith     Not Collective
72*5c6c1daeSBarry Smith 
73*5c6c1daeSBarry Smith     Input Parameter:
74*5c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
75*5c6c1daeSBarry Smith 
76*5c6c1daeSBarry Smith     Output Parameter:
77*5c6c1daeSBarry Smith .    off - the current offset
78*5c6c1daeSBarry Smith 
79*5c6c1daeSBarry Smith     Level: advanced
80*5c6c1daeSBarry Smith 
81*5c6c1daeSBarry Smith     Fortran Note:
82*5c6c1daeSBarry Smith     This routine is not supported in Fortran.
83*5c6c1daeSBarry Smith 
84*5c6c1daeSBarry Smith     Use PetscViewerBinaryAddMPIIOOffset() to increase this value after you have written a view.
85*5c6c1daeSBarry Smith 
86*5c6c1daeSBarry Smith   Concepts: file descriptor^getting
87*5c6c1daeSBarry Smith   Concepts: PetscViewerBinary^accessing file descriptor
88*5c6c1daeSBarry Smith 
89*5c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer()
90*5c6c1daeSBarry Smith @*/
91*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryGetMPIIOOffset(PetscViewer viewer,MPI_Offset *off)
92*5c6c1daeSBarry Smith {
93*5c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
94*5c6c1daeSBarry Smith 
95*5c6c1daeSBarry Smith   PetscFunctionBegin;
96*5c6c1daeSBarry Smith   *off = vbinary->moff;
97*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
98*5c6c1daeSBarry Smith }
99*5c6c1daeSBarry Smith 
100*5c6c1daeSBarry Smith #undef __FUNCT__
101*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryAddMPIIOOffset"
102*5c6c1daeSBarry Smith /*@C
103*5c6c1daeSBarry Smith     PetscViewerBinaryAddMPIIOOffset - Adds to the current offset that should be passed to MPI_File_set_view()
104*5c6c1daeSBarry Smith 
105*5c6c1daeSBarry Smith     Not Collective
106*5c6c1daeSBarry Smith 
107*5c6c1daeSBarry Smith     Input Parameters:
108*5c6c1daeSBarry Smith +   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
109*5c6c1daeSBarry Smith -    off - the addition to the offset
110*5c6c1daeSBarry Smith 
111*5c6c1daeSBarry Smith     Level: advanced
112*5c6c1daeSBarry Smith 
113*5c6c1daeSBarry Smith     Fortran Note:
114*5c6c1daeSBarry Smith     This routine is not supported in Fortran.
115*5c6c1daeSBarry Smith 
116*5c6c1daeSBarry Smith     Use PetscViewerBinaryGetMPIIOOffset() to get the value that you should pass to MPI_File_set_view()
117*5c6c1daeSBarry Smith 
118*5c6c1daeSBarry Smith   Concepts: file descriptor^getting
119*5c6c1daeSBarry Smith   Concepts: PetscViewerBinary^accessing file descriptor
120*5c6c1daeSBarry Smith 
121*5c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer()
122*5c6c1daeSBarry Smith @*/
123*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryAddMPIIOOffset(PetscViewer viewer,MPI_Offset off)
124*5c6c1daeSBarry Smith {
125*5c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
126*5c6c1daeSBarry Smith 
127*5c6c1daeSBarry Smith   PetscFunctionBegin;
128*5c6c1daeSBarry Smith   vbinary->moff += off;
129*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
130*5c6c1daeSBarry Smith }
131*5c6c1daeSBarry Smith 
132*5c6c1daeSBarry Smith #undef __FUNCT__
133*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetMPIIODescriptor"
134*5c6c1daeSBarry Smith /*@C
135*5c6c1daeSBarry Smith     PetscViewerBinaryGetMPIIODescriptor - Extracts the MPI IO file descriptor from a PetscViewer.
136*5c6c1daeSBarry Smith 
137*5c6c1daeSBarry Smith     Not Collective
138*5c6c1daeSBarry Smith 
139*5c6c1daeSBarry Smith     Input Parameter:
140*5c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
141*5c6c1daeSBarry Smith 
142*5c6c1daeSBarry Smith     Output Parameter:
143*5c6c1daeSBarry Smith .   fdes - file descriptor
144*5c6c1daeSBarry Smith 
145*5c6c1daeSBarry Smith     Level: advanced
146*5c6c1daeSBarry Smith 
147*5c6c1daeSBarry Smith     Fortran Note:
148*5c6c1daeSBarry Smith     This routine is not supported in Fortran.
149*5c6c1daeSBarry Smith 
150*5c6c1daeSBarry Smith   Concepts: file descriptor^getting
151*5c6c1daeSBarry Smith   Concepts: PetscViewerBinary^accessing file descriptor
152*5c6c1daeSBarry Smith 
153*5c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer()
154*5c6c1daeSBarry Smith @*/
155*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryGetMPIIODescriptor(PetscViewer viewer,MPI_File *fdes)
156*5c6c1daeSBarry Smith {
157*5c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
158*5c6c1daeSBarry Smith 
159*5c6c1daeSBarry Smith   PetscFunctionBegin;
160*5c6c1daeSBarry Smith   *fdes = vbinary->mfdes;
161*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
162*5c6c1daeSBarry Smith }
163*5c6c1daeSBarry Smith 
164*5c6c1daeSBarry Smith #undef __FUNCT__
165*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetMPIIO"
166*5c6c1daeSBarry Smith /*@C
167*5c6c1daeSBarry Smith     PetscViewerBinaryGetMPIIO - Returns PETSC_TRUE if the binary viewer is an MPI viewer.
168*5c6c1daeSBarry Smith 
169*5c6c1daeSBarry Smith     Not Collective
170*5c6c1daeSBarry Smith 
171*5c6c1daeSBarry Smith     Input Parameter:
172*5c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
173*5c6c1daeSBarry Smith 
174*5c6c1daeSBarry Smith     Output Parameter:
175*5c6c1daeSBarry Smith -   flg - PETSC_TRUE if MPI IO is being used
176*5c6c1daeSBarry Smith 
177*5c6c1daeSBarry Smith     Options Database:
178*5c6c1daeSBarry Smith     -viewer_binary_mpiio : Flag for using MPI-IO
179*5c6c1daeSBarry Smith 
180*5c6c1daeSBarry Smith     Level: advanced
181*5c6c1daeSBarry Smith 
182*5c6c1daeSBarry Smith     Fortran Note:
183*5c6c1daeSBarry Smith     This routine is not supported in Fortran.
184*5c6c1daeSBarry Smith 
185*5c6c1daeSBarry Smith   Concepts: file descriptor^getting
186*5c6c1daeSBarry Smith   Concepts: PetscViewerBinary^accessing file descriptor
187*5c6c1daeSBarry Smith 
188*5c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetInfoPointer()
189*5c6c1daeSBarry Smith @*/
190*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryGetMPIIO(PetscViewer viewer,PetscBool  *flg)
191*5c6c1daeSBarry Smith {
192*5c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
193*5c6c1daeSBarry Smith 
194*5c6c1daeSBarry Smith   PetscFunctionBegin;
195*5c6c1daeSBarry Smith   *flg = vbinary->MPIIO;
196*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
197*5c6c1daeSBarry Smith }
198*5c6c1daeSBarry Smith #endif
199*5c6c1daeSBarry Smith 
200*5c6c1daeSBarry Smith EXTERN_C_BEGIN
201*5c6c1daeSBarry Smith #undef __FUNCT__
202*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetFlowControl_Binary"
203*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryGetFlowControl_Binary(PetscViewer viewer,PetscInt *fc)
204*5c6c1daeSBarry Smith {
205*5c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
206*5c6c1daeSBarry Smith 
207*5c6c1daeSBarry Smith   PetscFunctionBegin;
208*5c6c1daeSBarry Smith   *fc = vbinary->flowcontrol;
209*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
210*5c6c1daeSBarry Smith }
211*5c6c1daeSBarry Smith EXTERN_C_END
212*5c6c1daeSBarry Smith 
213*5c6c1daeSBarry Smith #undef __FUNCT__
214*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetFlowControl"
215*5c6c1daeSBarry Smith /*@C
216*5c6c1daeSBarry Smith     PetscViewerBinaryGetFlowControl - Returns how many messages are allowed to outstanding at the same time during parallel IO reads/writes
217*5c6c1daeSBarry Smith 
218*5c6c1daeSBarry Smith     Not Collective
219*5c6c1daeSBarry Smith 
220*5c6c1daeSBarry Smith     Input Parameter:
221*5c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
222*5c6c1daeSBarry Smith 
223*5c6c1daeSBarry Smith     Output Parameter:
224*5c6c1daeSBarry Smith .   fc - the number of messages
225*5c6c1daeSBarry Smith 
226*5c6c1daeSBarry Smith     Level: advanced
227*5c6c1daeSBarry Smith 
228*5c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer(), PetscViewerBinarySetFlowControl()
229*5c6c1daeSBarry Smith 
230*5c6c1daeSBarry Smith @*/
231*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryGetFlowControl(PetscViewer viewer,PetscInt *fc)
232*5c6c1daeSBarry Smith {
233*5c6c1daeSBarry Smith   PetscErrorCode ierr;
234*5c6c1daeSBarry Smith 
235*5c6c1daeSBarry Smith   PetscFunctionBegin;
236*5c6c1daeSBarry Smith   *fc = 256;
237*5c6c1daeSBarry Smith   ierr = PetscTryMethod(viewer,"PetscViewerBinaryGetFlowControl_C",(PetscViewer,PetscInt *),(viewer,fc));CHKERRQ(ierr);
238*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
239*5c6c1daeSBarry Smith }
240*5c6c1daeSBarry Smith 
241*5c6c1daeSBarry Smith EXTERN_C_BEGIN
242*5c6c1daeSBarry Smith #undef __FUNCT__
243*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinarySetFlowControl_Binary"
244*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinarySetFlowControl_Binary(PetscViewer viewer,PetscInt fc)
245*5c6c1daeSBarry Smith {
246*5c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
247*5c6c1daeSBarry Smith 
248*5c6c1daeSBarry Smith   PetscFunctionBegin;
249*5c6c1daeSBarry Smith   if (fc <= 1) SETERRQ1(((PetscObject)viewer)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Flow control count must be greater than 1, %D was set",fc);
250*5c6c1daeSBarry Smith   vbinary->flowcontrol = fc;
251*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
252*5c6c1daeSBarry Smith }
253*5c6c1daeSBarry Smith EXTERN_C_END
254*5c6c1daeSBarry Smith 
255*5c6c1daeSBarry Smith #undef __FUNCT__
256*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinarySetFlowControl"
257*5c6c1daeSBarry Smith /*@C
258*5c6c1daeSBarry Smith     PetscViewerBinarySetFlowControl - Returns how many messages are allowed to outstanding at the same time during parallel IO reads/writes
259*5c6c1daeSBarry Smith 
260*5c6c1daeSBarry Smith     Not Collective
261*5c6c1daeSBarry Smith 
262*5c6c1daeSBarry Smith     Input Parameter:
263*5c6c1daeSBarry Smith +   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
264*5c6c1daeSBarry Smith -   fc - the number of messages, defaults to 256
265*5c6c1daeSBarry Smith 
266*5c6c1daeSBarry Smith     Level: advanced
267*5c6c1daeSBarry Smith 
268*5c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer(), PetscViewerBinaryGetFlowControl()
269*5c6c1daeSBarry Smith 
270*5c6c1daeSBarry Smith @*/
271*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinarySetFlowControl(PetscViewer viewer,PetscInt fc)
272*5c6c1daeSBarry Smith {
273*5c6c1daeSBarry Smith   PetscErrorCode ierr;
274*5c6c1daeSBarry Smith 
275*5c6c1daeSBarry Smith   PetscFunctionBegin;
276*5c6c1daeSBarry Smith   ierr = PetscUseMethod(viewer,"PetscViewerBinarySetFlowControl_C",(PetscViewer,PetscInt),(viewer,fc));CHKERRQ(ierr);
277*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
278*5c6c1daeSBarry Smith }
279*5c6c1daeSBarry Smith 
280*5c6c1daeSBarry Smith #undef __FUNCT__
281*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetDescriptor"
282*5c6c1daeSBarry Smith /*@C
283*5c6c1daeSBarry Smith     PetscViewerBinaryGetDescriptor - Extracts the file descriptor from a PetscViewer.
284*5c6c1daeSBarry Smith 
285*5c6c1daeSBarry Smith     Not Collective
286*5c6c1daeSBarry Smith 
287*5c6c1daeSBarry Smith     Input Parameter:
288*5c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
289*5c6c1daeSBarry Smith 
290*5c6c1daeSBarry Smith     Output Parameter:
291*5c6c1daeSBarry Smith .   fdes - file descriptor
292*5c6c1daeSBarry Smith 
293*5c6c1daeSBarry Smith     Level: advanced
294*5c6c1daeSBarry Smith 
295*5c6c1daeSBarry Smith     Notes:
296*5c6c1daeSBarry Smith       For writable binary PetscViewers, the descriptor will only be valid for the
297*5c6c1daeSBarry Smith     first processor in the communicator that shares the PetscViewer. For readable
298*5c6c1daeSBarry Smith     files it will only be valid on nodes that have the file. If node 0 does not
299*5c6c1daeSBarry Smith     have the file it generates an error even if another node does have the file.
300*5c6c1daeSBarry Smith 
301*5c6c1daeSBarry Smith     Fortran Note:
302*5c6c1daeSBarry Smith     This routine is not supported in Fortran.
303*5c6c1daeSBarry Smith 
304*5c6c1daeSBarry Smith   Concepts: file descriptor^getting
305*5c6c1daeSBarry Smith   Concepts: PetscViewerBinary^accessing file descriptor
306*5c6c1daeSBarry Smith 
307*5c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer()
308*5c6c1daeSBarry Smith @*/
309*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryGetDescriptor(PetscViewer viewer,int *fdes)
310*5c6c1daeSBarry Smith {
311*5c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
312*5c6c1daeSBarry Smith 
313*5c6c1daeSBarry Smith   PetscFunctionBegin;
314*5c6c1daeSBarry Smith   *fdes = vbinary->fdes;
315*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
316*5c6c1daeSBarry Smith }
317*5c6c1daeSBarry Smith 
318*5c6c1daeSBarry Smith #undef __FUNCT__
319*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinarySkipInfo"
320*5c6c1daeSBarry Smith /*@
321*5c6c1daeSBarry Smith     PetscViewerBinarySkipInfo - Binary file will not have .info file created with it
322*5c6c1daeSBarry Smith 
323*5c6c1daeSBarry Smith     Not Collective
324*5c6c1daeSBarry Smith 
325*5c6c1daeSBarry Smith     Input Paramter:
326*5c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerCreate()
327*5c6c1daeSBarry Smith 
328*5c6c1daeSBarry Smith     Options Database Key:
329*5c6c1daeSBarry Smith .   -viewer_binary_skip_info
330*5c6c1daeSBarry Smith 
331*5c6c1daeSBarry Smith     Level: advanced
332*5c6c1daeSBarry Smith 
333*5c6c1daeSBarry Smith     Notes: This must be called after PetscViewerSetType() but before PetscViewerFileSetName(). If you use PetscViewerBinaryOpen() then
334*5c6c1daeSBarry Smith     you can only skip the info file with the -viewer_binary_skip_info flag. To use the function you must open the
335*5c6c1daeSBarry Smith     viewer with PetscViewerCreate(), PetscViewerSetType(), PetscViewerFileSetName().
336*5c6c1daeSBarry Smith 
337*5c6c1daeSBarry Smith     The .info contains meta information about the data in the binary file, for example the block size if it was
338*5c6c1daeSBarry Smith     set for a vector or matrix.
339*5c6c1daeSBarry Smith 
340*5c6c1daeSBarry Smith    Concepts: PetscViewerBinary^accessing info file
341*5c6c1daeSBarry Smith 
342*5c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySetSkipOptions(),
343*5c6c1daeSBarry Smith           PetscViewerBinaryGetSkipOptions()
344*5c6c1daeSBarry Smith @*/
345*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinarySkipInfo(PetscViewer viewer)
346*5c6c1daeSBarry Smith {
347*5c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
348*5c6c1daeSBarry Smith 
349*5c6c1daeSBarry Smith   PetscFunctionBegin;
350*5c6c1daeSBarry Smith   vbinary->skipinfo = PETSC_TRUE;
351*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
352*5c6c1daeSBarry Smith }
353*5c6c1daeSBarry Smith 
354*5c6c1daeSBarry Smith #undef __FUNCT__
355*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinarySetSkipOptions"
356*5c6c1daeSBarry Smith /*@
357*5c6c1daeSBarry Smith     PetscViewerBinarySetSkipOptions - do not use the PETSc options database when loading objects
358*5c6c1daeSBarry Smith 
359*5c6c1daeSBarry Smith     Not Collective
360*5c6c1daeSBarry Smith 
361*5c6c1daeSBarry Smith     Input Parameters:
362*5c6c1daeSBarry Smith +   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
363*5c6c1daeSBarry Smith -   skip - PETSC_TRUE means do not use
364*5c6c1daeSBarry Smith 
365*5c6c1daeSBarry Smith     Options Database Key:
366*5c6c1daeSBarry Smith .   -viewer_binary_skip_options
367*5c6c1daeSBarry Smith 
368*5c6c1daeSBarry Smith     Level: advanced
369*5c6c1daeSBarry Smith 
370*5c6c1daeSBarry Smith     Notes: This must be called after PetscViewerSetType()
371*5c6c1daeSBarry Smith 
372*5c6c1daeSBarry Smith    Concepts: PetscViewerBinary^accessing info file
373*5c6c1daeSBarry Smith 
374*5c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(),
375*5c6c1daeSBarry Smith           PetscViewerBinaryGetSkipOptions()
376*5c6c1daeSBarry Smith @*/
377*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinarySetSkipOptions(PetscViewer viewer,PetscBool  skip)
378*5c6c1daeSBarry Smith {
379*5c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
380*5c6c1daeSBarry Smith 
381*5c6c1daeSBarry Smith   PetscFunctionBegin;
382*5c6c1daeSBarry Smith   vbinary->skipoptions = skip;
383*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
384*5c6c1daeSBarry Smith }
385*5c6c1daeSBarry Smith 
386*5c6c1daeSBarry Smith #undef __FUNCT__
387*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetSkipOptions"
388*5c6c1daeSBarry Smith /*@
389*5c6c1daeSBarry Smith     PetscViewerBinaryGetSkipOptions - checks if viewer uses the PETSc options database when loading objects
390*5c6c1daeSBarry Smith 
391*5c6c1daeSBarry Smith     Not Collective
392*5c6c1daeSBarry Smith 
393*5c6c1daeSBarry Smith     Input Parameter:
394*5c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
395*5c6c1daeSBarry Smith 
396*5c6c1daeSBarry Smith     Output Parameter:
397*5c6c1daeSBarry Smith .   skip - PETSC_TRUE means do not use
398*5c6c1daeSBarry Smith 
399*5c6c1daeSBarry Smith     Level: advanced
400*5c6c1daeSBarry Smith 
401*5c6c1daeSBarry Smith     Notes: This must be called after PetscViewerSetType()
402*5c6c1daeSBarry Smith 
403*5c6c1daeSBarry Smith    Concepts: PetscViewerBinary^accessing info file
404*5c6c1daeSBarry Smith 
405*5c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(),
406*5c6c1daeSBarry Smith           PetscViewerBinarySetSkipOptions()
407*5c6c1daeSBarry Smith @*/
408*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryGetSkipOptions(PetscViewer viewer,PetscBool  *skip)
409*5c6c1daeSBarry Smith {
410*5c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
411*5c6c1daeSBarry Smith 
412*5c6c1daeSBarry Smith   PetscFunctionBegin;
413*5c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
414*5c6c1daeSBarry Smith   vbinary = (PetscViewer_Binary*)viewer->data;
415*5c6c1daeSBarry Smith   *skip = vbinary->skipoptions;
416*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
417*5c6c1daeSBarry Smith }
418*5c6c1daeSBarry Smith 
419*5c6c1daeSBarry Smith EXTERN_C_BEGIN
420*5c6c1daeSBarry Smith #undef __FUNCT__
421*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinarySetSkipHeader_Binary"
422*5c6c1daeSBarry Smith PetscErrorCode PetscViewerBinarySetSkipHeader_Binary(PetscViewer viewer,PetscBool  skip)
423*5c6c1daeSBarry Smith {
424*5c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
425*5c6c1daeSBarry Smith 
426*5c6c1daeSBarry Smith   PetscFunctionBegin;
427*5c6c1daeSBarry Smith   vbinary->skipheader = skip;
428*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
429*5c6c1daeSBarry Smith }
430*5c6c1daeSBarry Smith EXTERN_C_END
431*5c6c1daeSBarry Smith 
432*5c6c1daeSBarry Smith #undef __FUNCT__
433*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinarySetSkipHeader"
434*5c6c1daeSBarry Smith /*@
435*5c6c1daeSBarry Smith     PetscViewerBinarySetSkipHeader - do not write a header with size information on output, just raw data
436*5c6c1daeSBarry Smith 
437*5c6c1daeSBarry Smith     Not Collective
438*5c6c1daeSBarry Smith 
439*5c6c1daeSBarry Smith     Input Parameters:
440*5c6c1daeSBarry Smith +   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
441*5c6c1daeSBarry Smith -   skip - PETSC_TRUE means do not write header
442*5c6c1daeSBarry Smith 
443*5c6c1daeSBarry Smith     Options Database Key:
444*5c6c1daeSBarry Smith .   -viewer_binary_skip_header
445*5c6c1daeSBarry Smith 
446*5c6c1daeSBarry Smith     Level: advanced
447*5c6c1daeSBarry Smith 
448*5c6c1daeSBarry Smith     Notes: This must be called after PetscViewerSetType()
449*5c6c1daeSBarry Smith 
450*5c6c1daeSBarry Smith            Can ONLY be called on a binary viewer
451*5c6c1daeSBarry Smith 
452*5c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(),
453*5c6c1daeSBarry Smith           PetscViewerBinaryGetSkipHeader()
454*5c6c1daeSBarry Smith @*/
455*5c6c1daeSBarry Smith PetscErrorCode PetscViewerBinarySetSkipHeader(PetscViewer viewer,PetscBool  skip)
456*5c6c1daeSBarry Smith {
457*5c6c1daeSBarry Smith   PetscErrorCode ierr;
458*5c6c1daeSBarry Smith 
459*5c6c1daeSBarry Smith   PetscFunctionBegin;
460*5c6c1daeSBarry Smith   ierr = PetscUseMethod(viewer,"PetscViewerBinarySetSkipHeader_C",(PetscViewer,PetscBool),(viewer,skip));CHKERRQ(ierr);
461*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
462*5c6c1daeSBarry Smith }
463*5c6c1daeSBarry Smith 
464*5c6c1daeSBarry Smith EXTERN_C_BEGIN
465*5c6c1daeSBarry Smith #undef __FUNCT__
466*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetSkipHeader_Binary"
467*5c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetSkipHeader_Binary(PetscViewer viewer,PetscBool  *skip)
468*5c6c1daeSBarry Smith {
469*5c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
470*5c6c1daeSBarry Smith 
471*5c6c1daeSBarry Smith   PetscFunctionBegin;
472*5c6c1daeSBarry Smith   *skip   = vbinary->skipheader;
473*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
474*5c6c1daeSBarry Smith }
475*5c6c1daeSBarry Smith EXTERN_C_END
476*5c6c1daeSBarry Smith 
477*5c6c1daeSBarry Smith #undef __FUNCT__
478*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetSkipHeader"
479*5c6c1daeSBarry Smith /*@
480*5c6c1daeSBarry Smith     PetscViewerBinaryGetSkipHeader - checks whether to write a header with size information on output, or just raw data
481*5c6c1daeSBarry Smith 
482*5c6c1daeSBarry Smith     Not Collective
483*5c6c1daeSBarry Smith 
484*5c6c1daeSBarry Smith     Input Parameter:
485*5c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
486*5c6c1daeSBarry Smith 
487*5c6c1daeSBarry Smith     Output Parameter:
488*5c6c1daeSBarry Smith .   skip - PETSC_TRUE means do not write header
489*5c6c1daeSBarry Smith 
490*5c6c1daeSBarry Smith     Level: advanced
491*5c6c1daeSBarry Smith 
492*5c6c1daeSBarry Smith     Notes: This must be called after PetscViewerSetType()
493*5c6c1daeSBarry Smith 
494*5c6c1daeSBarry Smith             Returns false for PETSCSOCKETVIEWER, you cannot skip the header for it.
495*5c6c1daeSBarry Smith 
496*5c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(),
497*5c6c1daeSBarry Smith           PetscViewerBinarySetSkipHeader()
498*5c6c1daeSBarry Smith @*/
499*5c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetSkipHeader(PetscViewer viewer,PetscBool  *skip)
500*5c6c1daeSBarry Smith {
501*5c6c1daeSBarry Smith   PetscErrorCode ierr;
502*5c6c1daeSBarry Smith 
503*5c6c1daeSBarry Smith   PetscFunctionBegin;
504*5c6c1daeSBarry Smith   *skip = PETSC_FALSE;
505*5c6c1daeSBarry Smith   ierr = PetscTryMethod(viewer,"PetscViewerBinaryGetSkipHeader_C",(PetscViewer,PetscBool*),(viewer,skip));CHKERRQ(ierr);
506*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
507*5c6c1daeSBarry Smith }
508*5c6c1daeSBarry Smith 
509*5c6c1daeSBarry Smith EXTERN_C_BEGIN
510*5c6c1daeSBarry Smith #undef __FUNCT__
511*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetInfoPointer_Binary"
512*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryGetInfoPointer_Binary(PetscViewer viewer,FILE **file)
513*5c6c1daeSBarry Smith {
514*5c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;;
515*5c6c1daeSBarry Smith 
516*5c6c1daeSBarry Smith   PetscFunctionBegin;
517*5c6c1daeSBarry Smith   *file = vbinary->fdes_info;
518*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
519*5c6c1daeSBarry Smith }
520*5c6c1daeSBarry Smith EXTERN_C_END
521*5c6c1daeSBarry Smith 
522*5c6c1daeSBarry Smith #undef __FUNCT__
523*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetInfoPointer"
524*5c6c1daeSBarry Smith /*@C
525*5c6c1daeSBarry Smith     PetscViewerBinaryGetInfoPointer - Extracts the file pointer for the ASCII
526*5c6c1daeSBarry Smith           info file associated with a binary file.
527*5c6c1daeSBarry Smith 
528*5c6c1daeSBarry Smith     Not Collective
529*5c6c1daeSBarry Smith 
530*5c6c1daeSBarry Smith     Input Parameter:
531*5c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
532*5c6c1daeSBarry Smith 
533*5c6c1daeSBarry Smith     Output Parameter:
534*5c6c1daeSBarry Smith .   file - file pointer  Always returns PETSC_NULL if not a binary viewer
535*5c6c1daeSBarry Smith 
536*5c6c1daeSBarry Smith     Level: advanced
537*5c6c1daeSBarry Smith 
538*5c6c1daeSBarry Smith     Notes:
539*5c6c1daeSBarry Smith       For writable binary PetscViewers, the descriptor will only be valid for the
540*5c6c1daeSBarry Smith     first processor in the communicator that shares the PetscViewer.
541*5c6c1daeSBarry Smith 
542*5c6c1daeSBarry Smith     Fortran Note:
543*5c6c1daeSBarry Smith     This routine is not supported in Fortran.
544*5c6c1daeSBarry Smith 
545*5c6c1daeSBarry Smith   Concepts: PetscViewerBinary^accessing info file
546*5c6c1daeSBarry Smith 
547*5c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetDescriptor()
548*5c6c1daeSBarry Smith @*/
549*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryGetInfoPointer(PetscViewer viewer,FILE **file)
550*5c6c1daeSBarry Smith {
551*5c6c1daeSBarry Smith   PetscErrorCode     ierr;
552*5c6c1daeSBarry Smith 
553*5c6c1daeSBarry Smith   PetscFunctionBegin;
554*5c6c1daeSBarry Smith   *file = PETSC_NULL;
555*5c6c1daeSBarry Smith   ierr = PetscTryMethod(viewer,"PetscViewerBinaryGetInfoPointer_C",(PetscViewer,FILE **),(viewer,file));CHKERRQ(ierr);
556*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
557*5c6c1daeSBarry Smith }
558*5c6c1daeSBarry Smith 
559*5c6c1daeSBarry Smith #undef __FUNCT__
560*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileClose_Binary"
561*5c6c1daeSBarry Smith static PetscErrorCode PetscViewerFileClose_Binary(PetscViewer v)
562*5c6c1daeSBarry Smith {
563*5c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data;
564*5c6c1daeSBarry Smith   PetscErrorCode     ierr;
565*5c6c1daeSBarry Smith   PetscMPIInt        rank;
566*5c6c1daeSBarry Smith   int                err;
567*5c6c1daeSBarry Smith 
568*5c6c1daeSBarry Smith   PetscFunctionBegin;
569*5c6c1daeSBarry Smith   ierr = MPI_Comm_rank(((PetscObject)v)->comm,&rank);CHKERRQ(ierr);
570*5c6c1daeSBarry Smith   if ((!rank || vbinary->btype == FILE_MODE_READ) && vbinary->fdes) {
571*5c6c1daeSBarry Smith     close(vbinary->fdes);
572*5c6c1daeSBarry Smith     if (!rank && vbinary->storecompressed) {
573*5c6c1daeSBarry Smith       char par[PETSC_MAX_PATH_LEN],buf[PETSC_MAX_PATH_LEN];
574*5c6c1daeSBarry Smith       FILE *fp;
575*5c6c1daeSBarry Smith       /* compress the file */
576*5c6c1daeSBarry Smith       ierr = PetscStrcpy(par,"gzip -f ");CHKERRQ(ierr);
577*5c6c1daeSBarry Smith       ierr = PetscStrcat(par,vbinary->filename);CHKERRQ(ierr);
578*5c6c1daeSBarry Smith #if defined(PETSC_HAVE_POPEN)
579*5c6c1daeSBarry Smith       ierr = PetscPOpen(PETSC_COMM_SELF,PETSC_NULL,par,"r",&fp);CHKERRQ(ierr);
580*5c6c1daeSBarry Smith       if (fgets(buf,1024,fp)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error from command %s\n%s",par,buf);
581*5c6c1daeSBarry Smith       ierr = PetscPClose(PETSC_COMM_SELF,fp,PETSC_NULL);CHKERRQ(ierr);
582*5c6c1daeSBarry Smith #else
583*5c6c1daeSBarry Smith       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
584*5c6c1daeSBarry Smith #endif
585*5c6c1daeSBarry Smith     }
586*5c6c1daeSBarry Smith   }
587*5c6c1daeSBarry Smith   if (vbinary->fdes_info) {
588*5c6c1daeSBarry Smith     err = fclose(vbinary->fdes_info);
589*5c6c1daeSBarry Smith     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
590*5c6c1daeSBarry Smith   }
591*5c6c1daeSBarry Smith   ierr = PetscFree(vbinary->filename);CHKERRQ(ierr);
592*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
593*5c6c1daeSBarry Smith }
594*5c6c1daeSBarry Smith 
595*5c6c1daeSBarry Smith #undef __FUNCT__
596*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerDestroy_Binary"
597*5c6c1daeSBarry Smith PetscErrorCode PetscViewerDestroy_Binary(PetscViewer v)
598*5c6c1daeSBarry Smith {
599*5c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data;
600*5c6c1daeSBarry Smith   PetscErrorCode     ierr;
601*5c6c1daeSBarry Smith 
602*5c6c1daeSBarry Smith   PetscFunctionBegin;
603*5c6c1daeSBarry Smith   ierr = PetscViewerFileClose_Binary(v);CHKERRQ(ierr);
604*5c6c1daeSBarry Smith   ierr = PetscFree(vbinary);CHKERRQ(ierr);
605*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
606*5c6c1daeSBarry Smith }
607*5c6c1daeSBarry Smith 
608*5c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
609*5c6c1daeSBarry Smith #undef __FUNCT__
610*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileClose_MPIIO"
611*5c6c1daeSBarry Smith static PetscErrorCode PetscViewerFileClose_MPIIO(PetscViewer v)
612*5c6c1daeSBarry Smith {
613*5c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data;
614*5c6c1daeSBarry Smith   int                err;
615*5c6c1daeSBarry Smith   PetscErrorCode     ierr;
616*5c6c1daeSBarry Smith 
617*5c6c1daeSBarry Smith   PetscFunctionBegin;
618*5c6c1daeSBarry Smith   if (vbinary->mfdes) {
619*5c6c1daeSBarry Smith     ierr = MPI_File_close(&vbinary->mfdes);CHKERRQ(ierr);
620*5c6c1daeSBarry Smith   }
621*5c6c1daeSBarry Smith   if (vbinary->fdes_info) {
622*5c6c1daeSBarry Smith     err = fclose(vbinary->fdes_info);
623*5c6c1daeSBarry Smith     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
624*5c6c1daeSBarry Smith   }
625*5c6c1daeSBarry Smith   ierr = PetscFree(vbinary->filename);CHKERRQ(ierr);
626*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
627*5c6c1daeSBarry Smith }
628*5c6c1daeSBarry Smith 
629*5c6c1daeSBarry Smith 
630*5c6c1daeSBarry Smith #undef __FUNCT__
631*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerDestroy_MPIIO"
632*5c6c1daeSBarry Smith PetscErrorCode PetscViewerDestroy_MPIIO(PetscViewer v)
633*5c6c1daeSBarry Smith {
634*5c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data;
635*5c6c1daeSBarry Smith   PetscErrorCode     ierr;
636*5c6c1daeSBarry Smith 
637*5c6c1daeSBarry Smith   PetscFunctionBegin;
638*5c6c1daeSBarry Smith   ierr = PetscViewerFileClose_MPIIO(v);CHKERRQ(ierr);
639*5c6c1daeSBarry Smith   ierr = PetscFree(vbinary);CHKERRQ(ierr);
640*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
641*5c6c1daeSBarry Smith }
642*5c6c1daeSBarry Smith #endif
643*5c6c1daeSBarry Smith 
644*5c6c1daeSBarry Smith #undef __FUNCT__
645*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryOpen"
646*5c6c1daeSBarry Smith /*@C
647*5c6c1daeSBarry Smith    PetscViewerBinaryOpen - Opens a file for binary input/output.
648*5c6c1daeSBarry Smith 
649*5c6c1daeSBarry Smith    Collective on MPI_Comm
650*5c6c1daeSBarry Smith 
651*5c6c1daeSBarry Smith    Input Parameters:
652*5c6c1daeSBarry Smith +  comm - MPI communicator
653*5c6c1daeSBarry Smith .  name - name of file
654*5c6c1daeSBarry Smith -  type - type of file
655*5c6c1daeSBarry Smith $    FILE_MODE_WRITE - create new file for binary output
656*5c6c1daeSBarry Smith $    FILE_MODE_READ - open existing file for binary input
657*5c6c1daeSBarry Smith $    FILE_MODE_APPEND - open existing file for binary output
658*5c6c1daeSBarry Smith 
659*5c6c1daeSBarry Smith    Output Parameter:
660*5c6c1daeSBarry Smith .  binv - PetscViewer for binary input/output to use with the specified file
661*5c6c1daeSBarry Smith 
662*5c6c1daeSBarry Smith     Options Database Keys:
663*5c6c1daeSBarry Smith +    -viewer_binary_skip_info
664*5c6c1daeSBarry Smith .    -viewer_binary_skip_options
665*5c6c1daeSBarry Smith -    -viewer_binary_skip_header
666*5c6c1daeSBarry Smith 
667*5c6c1daeSBarry Smith    Level: beginner
668*5c6c1daeSBarry Smith 
669*5c6c1daeSBarry Smith    Note:
670*5c6c1daeSBarry Smith    This PetscViewer should be destroyed with PetscViewerDestroy().
671*5c6c1daeSBarry Smith 
672*5c6c1daeSBarry Smith     For reading files, the filename may begin with ftp:// or http:// and/or
673*5c6c1daeSBarry Smith     end with .gz; in this case file is brought over and uncompressed.
674*5c6c1daeSBarry Smith 
675*5c6c1daeSBarry Smith     For creating files, if the file name ends with .gz it is automatically
676*5c6c1daeSBarry Smith     compressed when closed.
677*5c6c1daeSBarry Smith 
678*5c6c1daeSBarry Smith     For writing files it only opens the file on processor 0 in the communicator.
679*5c6c1daeSBarry Smith     For readable files it opens the file on all nodes that have the file. If
680*5c6c1daeSBarry Smith     node 0 does not have the file it generates an error even if other nodes
681*5c6c1daeSBarry Smith     do have the file.
682*5c6c1daeSBarry Smith 
683*5c6c1daeSBarry Smith    Concepts: binary files
684*5c6c1daeSBarry Smith    Concepts: PetscViewerBinary^creating
685*5c6c1daeSBarry Smith    Concepts: gzip
686*5c6c1daeSBarry Smith    Concepts: accessing remote file
687*5c6c1daeSBarry Smith    Concepts: remote file
688*5c6c1daeSBarry Smith 
689*5c6c1daeSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(),
690*5c6c1daeSBarry Smith           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
691*5c6c1daeSBarry Smith           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscViewerBinaryRead()
692*5c6c1daeSBarry Smith @*/
693*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryOpen(MPI_Comm comm,const char name[],PetscFileMode type,PetscViewer *binv)
694*5c6c1daeSBarry Smith {
695*5c6c1daeSBarry Smith   PetscErrorCode ierr;
696*5c6c1daeSBarry Smith 
697*5c6c1daeSBarry Smith   PetscFunctionBegin;
698*5c6c1daeSBarry Smith   ierr = PetscViewerCreate(comm,binv);CHKERRQ(ierr);
699*5c6c1daeSBarry Smith   ierr = PetscViewerSetType(*binv,PETSCVIEWERBINARY);CHKERRQ(ierr);
700*5c6c1daeSBarry Smith   ierr = PetscViewerFileSetMode(*binv,type);CHKERRQ(ierr);
701*5c6c1daeSBarry Smith   ierr = PetscViewerFileSetName(*binv,name);CHKERRQ(ierr);
702*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
703*5c6c1daeSBarry Smith }
704*5c6c1daeSBarry Smith 
705*5c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
706*5c6c1daeSBarry Smith #undef __FUNCT__
707*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryMPIIO"
708*5c6c1daeSBarry Smith static PetscErrorCode PetscViewerBinaryMPIIO(PetscViewer viewer,void *data,PetscInt count,PetscDataType dtype,PetscBool  write)
709*5c6c1daeSBarry Smith {
710*5c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
711*5c6c1daeSBarry Smith   PetscErrorCode     ierr;
712*5c6c1daeSBarry Smith   MPI_Datatype       mdtype;
713*5c6c1daeSBarry Smith   PetscMPIInt        cnt = PetscMPIIntCast(count);
714*5c6c1daeSBarry Smith   MPI_Status         status;
715*5c6c1daeSBarry Smith   MPI_Aint           ul,dsize;
716*5c6c1daeSBarry Smith 
717*5c6c1daeSBarry Smith   PetscFunctionBegin;
718*5c6c1daeSBarry Smith   ierr = PetscDataTypeToMPIDataType(dtype,&mdtype);CHKERRQ(ierr);
719*5c6c1daeSBarry Smith   ierr = MPI_File_set_view(vbinary->mfdes,vbinary->moff,mdtype,mdtype,(char *)"native",MPI_INFO_NULL);CHKERRQ(ierr);
720*5c6c1daeSBarry Smith   if (write) {
721*5c6c1daeSBarry Smith     ierr = MPIU_File_write_all(vbinary->mfdes,data,cnt,mdtype,&status);CHKERRQ(ierr);
722*5c6c1daeSBarry Smith   } else {
723*5c6c1daeSBarry Smith     ierr = MPIU_File_read_all(vbinary->mfdes,data,cnt,mdtype,&status);CHKERRQ(ierr);
724*5c6c1daeSBarry Smith   }
725*5c6c1daeSBarry Smith   ierr = MPI_Type_get_extent(mdtype,&ul,&dsize);CHKERRQ(ierr);
726*5c6c1daeSBarry Smith   vbinary->moff += dsize*cnt;
727*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
728*5c6c1daeSBarry Smith }
729*5c6c1daeSBarry Smith #endif
730*5c6c1daeSBarry Smith 
731*5c6c1daeSBarry Smith #undef __FUNCT__
732*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryRead"
733*5c6c1daeSBarry Smith /*@C
734*5c6c1daeSBarry Smith    PetscViewerBinaryRead - Reads from a binary file, all processors get the same result
735*5c6c1daeSBarry Smith 
736*5c6c1daeSBarry Smith    Collective on MPI_Comm
737*5c6c1daeSBarry Smith 
738*5c6c1daeSBarry Smith    Input Parameters:
739*5c6c1daeSBarry Smith +  viewer - the binary viewer
740*5c6c1daeSBarry Smith .  data - location to write the data
741*5c6c1daeSBarry Smith .  count - number of items of data to read
742*5c6c1daeSBarry Smith -  datatype - type of data to read
743*5c6c1daeSBarry Smith 
744*5c6c1daeSBarry Smith    Level: beginner
745*5c6c1daeSBarry Smith 
746*5c6c1daeSBarry Smith    Concepts: binary files
747*5c6c1daeSBarry Smith 
748*5c6c1daeSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(),
749*5c6c1daeSBarry Smith           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
750*5c6c1daeSBarry Smith           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead()
751*5c6c1daeSBarry Smith @*/
752*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryRead(PetscViewer viewer,void *data,PetscInt count,PetscDataType dtype)
753*5c6c1daeSBarry Smith {
754*5c6c1daeSBarry Smith   PetscErrorCode     ierr;
755*5c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
756*5c6c1daeSBarry Smith 
757*5c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
758*5c6c1daeSBarry Smith   if (vbinary->MPIIO) {
759*5c6c1daeSBarry Smith     ierr = PetscViewerBinaryMPIIO(viewer,data,count,dtype,PETSC_FALSE);CHKERRQ(ierr);
760*5c6c1daeSBarry Smith   } else {
761*5c6c1daeSBarry Smith #endif
762*5c6c1daeSBarry Smith     ierr = PetscBinarySynchronizedRead(((PetscObject)viewer)->comm,vbinary->fdes,data,count,dtype);CHKERRQ(ierr);
763*5c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
764*5c6c1daeSBarry Smith   }
765*5c6c1daeSBarry Smith #endif
766*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
767*5c6c1daeSBarry Smith }
768*5c6c1daeSBarry Smith 
769*5c6c1daeSBarry Smith 
770*5c6c1daeSBarry Smith #undef __FUNCT__
771*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryWrite"
772*5c6c1daeSBarry Smith /*@C
773*5c6c1daeSBarry Smith    PetscViewerBinaryWrite - writes to a binary file, only from the first process
774*5c6c1daeSBarry Smith 
775*5c6c1daeSBarry Smith    Collective on MPI_Comm
776*5c6c1daeSBarry Smith 
777*5c6c1daeSBarry Smith    Input Parameters:
778*5c6c1daeSBarry Smith +  viewer - the binary viewer
779*5c6c1daeSBarry Smith .  data - location of data
780*5c6c1daeSBarry Smith .  count - number of items of data to read
781*5c6c1daeSBarry Smith .  dtype - type of data to read
782*5c6c1daeSBarry Smith -  istemp - data may be overwritten
783*5c6c1daeSBarry Smith 
784*5c6c1daeSBarry Smith    Level: beginner
785*5c6c1daeSBarry Smith 
786*5c6c1daeSBarry Smith    Notes: because byte-swapping may be done on the values in data it cannot be declared const
787*5c6c1daeSBarry Smith 
788*5c6c1daeSBarry Smith    Concepts: binary files
789*5c6c1daeSBarry Smith 
790*5c6c1daeSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(),
791*5c6c1daeSBarry Smith           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(), PetscDataType
792*5c6c1daeSBarry Smith           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead()
793*5c6c1daeSBarry Smith @*/
794*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryWrite(PetscViewer viewer,void *data,PetscInt count,PetscDataType dtype,PetscBool  istemp)
795*5c6c1daeSBarry Smith {
796*5c6c1daeSBarry Smith   PetscErrorCode     ierr;
797*5c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
798*5c6c1daeSBarry Smith 
799*5c6c1daeSBarry Smith   PetscFunctionBegin;
800*5c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
801*5c6c1daeSBarry Smith   if (vbinary->MPIIO) {
802*5c6c1daeSBarry Smith     ierr = PetscViewerBinaryMPIIO(viewer,data,count,dtype,PETSC_TRUE);CHKERRQ(ierr);
803*5c6c1daeSBarry Smith   } else {
804*5c6c1daeSBarry Smith #endif
805*5c6c1daeSBarry Smith     ierr = PetscBinarySynchronizedWrite(((PetscObject)viewer)->comm,vbinary->fdes,data,count,dtype,istemp);CHKERRQ(ierr);
806*5c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
807*5c6c1daeSBarry Smith   }
808*5c6c1daeSBarry Smith #endif
809*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
810*5c6c1daeSBarry Smith }
811*5c6c1daeSBarry Smith 
812*5c6c1daeSBarry Smith #undef __FUNCT__
813*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryWriteStringArray"
814*5c6c1daeSBarry Smith /*@C
815*5c6c1daeSBarry Smith    PetscViewerBinaryWriteStringArray - writes to a binary file, only from the first process an array of strings
816*5c6c1daeSBarry Smith 
817*5c6c1daeSBarry Smith    Collective on MPI_Comm
818*5c6c1daeSBarry Smith 
819*5c6c1daeSBarry Smith    Input Parameters:
820*5c6c1daeSBarry Smith +  viewer - the binary viewer
821*5c6c1daeSBarry Smith -  data - location of the array of strings
822*5c6c1daeSBarry Smith 
823*5c6c1daeSBarry Smith 
824*5c6c1daeSBarry Smith    Level: intermediate
825*5c6c1daeSBarry Smith 
826*5c6c1daeSBarry Smith    Concepts: binary files
827*5c6c1daeSBarry Smith 
828*5c6c1daeSBarry Smith     Notes: array of strings is null terminated
829*5c6c1daeSBarry Smith 
830*5c6c1daeSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(),
831*5c6c1daeSBarry Smith           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
832*5c6c1daeSBarry Smith           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead()
833*5c6c1daeSBarry Smith @*/
834*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryWriteStringArray(PetscViewer viewer,char **data)
835*5c6c1daeSBarry Smith {
836*5c6c1daeSBarry Smith   PetscErrorCode     ierr;
837*5c6c1daeSBarry Smith   PetscInt           i,n = 0,*sizes;
838*5c6c1daeSBarry Smith 
839*5c6c1daeSBarry Smith   /* count number of strings */
840*5c6c1daeSBarry Smith   while (data[n++]);
841*5c6c1daeSBarry Smith   n--;
842*5c6c1daeSBarry Smith   ierr = PetscMalloc((n+1)*sizeof(PetscInt),&sizes);CHKERRQ(ierr);
843*5c6c1daeSBarry Smith   sizes[0] = n;
844*5c6c1daeSBarry Smith   for (i=0; i<n; i++) {
845*5c6c1daeSBarry Smith     size_t tmp;
846*5c6c1daeSBarry Smith     ierr       = PetscStrlen(data[i],&tmp);CHKERRQ(ierr);
847*5c6c1daeSBarry Smith     sizes[i+1] = tmp + 1;   /* size includes space for the null terminator */
848*5c6c1daeSBarry Smith   }
849*5c6c1daeSBarry Smith   ierr = PetscViewerBinaryWrite(viewer,sizes,n+1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
850*5c6c1daeSBarry Smith   for (i=0; i<n; i++) {
851*5c6c1daeSBarry Smith     ierr = PetscViewerBinaryWrite(viewer,data[i],sizes[i+1],PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
852*5c6c1daeSBarry Smith   }
853*5c6c1daeSBarry Smith   ierr = PetscFree(sizes);CHKERRQ(ierr);
854*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
855*5c6c1daeSBarry Smith }
856*5c6c1daeSBarry Smith 
857*5c6c1daeSBarry Smith /*@C
858*5c6c1daeSBarry Smith    PetscViewerBinaryReadStringArray - reads a binary file an array of strings
859*5c6c1daeSBarry Smith 
860*5c6c1daeSBarry Smith    Collective on MPI_Comm
861*5c6c1daeSBarry Smith 
862*5c6c1daeSBarry Smith    Input Parameter:
863*5c6c1daeSBarry Smith .  viewer - the binary viewer
864*5c6c1daeSBarry Smith 
865*5c6c1daeSBarry Smith    Output Parameter:
866*5c6c1daeSBarry Smith .  data - location of the array of strings
867*5c6c1daeSBarry Smith 
868*5c6c1daeSBarry Smith    Level: intermediate
869*5c6c1daeSBarry Smith 
870*5c6c1daeSBarry Smith    Concepts: binary files
871*5c6c1daeSBarry Smith 
872*5c6c1daeSBarry Smith     Notes: array of strings is null terminated
873*5c6c1daeSBarry Smith 
874*5c6c1daeSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(),
875*5c6c1daeSBarry Smith           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
876*5c6c1daeSBarry Smith           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead()
877*5c6c1daeSBarry Smith @*/
878*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryReadStringArray(PetscViewer viewer,char ***data)
879*5c6c1daeSBarry Smith {
880*5c6c1daeSBarry Smith   PetscErrorCode     ierr;
881*5c6c1daeSBarry Smith   PetscInt           i,n,*sizes,N = 0;
882*5c6c1daeSBarry Smith 
883*5c6c1daeSBarry Smith   /* count number of strings */
884*5c6c1daeSBarry Smith   ierr = PetscViewerBinaryRead(viewer,&n,1,PETSC_INT);CHKERRQ(ierr);
885*5c6c1daeSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&sizes);CHKERRQ(ierr);
886*5c6c1daeSBarry Smith   ierr = PetscViewerBinaryRead(viewer,sizes,n,PETSC_INT);CHKERRQ(ierr);
887*5c6c1daeSBarry Smith   for (i=0; i<n; i++) {
888*5c6c1daeSBarry Smith     N += sizes[i];
889*5c6c1daeSBarry Smith   }
890*5c6c1daeSBarry Smith   ierr = PetscMalloc((n+1)*sizeof(char*) + N*sizeof(char),data);CHKERRQ(ierr);
891*5c6c1daeSBarry Smith   (*data)[0] = (char*)((*data) + n + 1);
892*5c6c1daeSBarry Smith   for (i=1; i<n; i++) {
893*5c6c1daeSBarry Smith     (*data)[i] = (*data)[i-1] + sizes[i-1];
894*5c6c1daeSBarry Smith   }
895*5c6c1daeSBarry Smith   ierr = PetscViewerBinaryRead(viewer,(*data)[0],N,PETSC_CHAR);CHKERRQ(ierr);
896*5c6c1daeSBarry Smith   (*data)[n] = 0;
897*5c6c1daeSBarry Smith   ierr = PetscFree(sizes);CHKERRQ(ierr);
898*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
899*5c6c1daeSBarry Smith }
900*5c6c1daeSBarry Smith 
901*5c6c1daeSBarry Smith EXTERN_C_BEGIN
902*5c6c1daeSBarry Smith #undef __FUNCT__
903*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileGetName_Binary"
904*5c6c1daeSBarry Smith PetscErrorCode PetscViewerFileGetName_Binary(PetscViewer viewer,const char **name)
905*5c6c1daeSBarry Smith {
906*5c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
907*5c6c1daeSBarry Smith 
908*5c6c1daeSBarry Smith   PetscFunctionBegin;
909*5c6c1daeSBarry Smith   *name = vbinary->filename;
910*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
911*5c6c1daeSBarry Smith }
912*5c6c1daeSBarry Smith EXTERN_C_END
913*5c6c1daeSBarry Smith 
914*5c6c1daeSBarry Smith #undef __FUNCT__
915*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileGetMode"
916*5c6c1daeSBarry Smith /*@C
917*5c6c1daeSBarry Smith      PetscViewerFileGetMode - Gets the type of file to be open
918*5c6c1daeSBarry Smith 
919*5c6c1daeSBarry Smith     Not Collective
920*5c6c1daeSBarry Smith 
921*5c6c1daeSBarry Smith   Input Parameter:
922*5c6c1daeSBarry Smith .  viewer - the PetscViewer; must be a binary, MATLAB, hdf, or netcdf PetscViewer
923*5c6c1daeSBarry Smith 
924*5c6c1daeSBarry Smith   Output Parameter:
925*5c6c1daeSBarry Smith .  type - type of file
926*5c6c1daeSBarry Smith $    FILE_MODE_WRITE - create new file for binary output
927*5c6c1daeSBarry Smith $    FILE_MODE_READ - open existing file for binary input
928*5c6c1daeSBarry Smith $    FILE_MODE_APPEND - open existing file for binary output
929*5c6c1daeSBarry Smith 
930*5c6c1daeSBarry Smith   Level: advanced
931*5c6c1daeSBarry Smith 
932*5c6c1daeSBarry Smith .seealso: PetscViewerFileSetMode(), PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinaryOpen()
933*5c6c1daeSBarry Smith 
934*5c6c1daeSBarry Smith @*/
935*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileGetMode(PetscViewer viewer,PetscFileMode *type)
936*5c6c1daeSBarry Smith {
937*5c6c1daeSBarry Smith   PetscErrorCode ierr;
938*5c6c1daeSBarry Smith 
939*5c6c1daeSBarry Smith   PetscFunctionBegin;
940*5c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
941*5c6c1daeSBarry Smith   PetscValidPointer(type,2);
942*5c6c1daeSBarry Smith   ierr = PetscUseMethod(viewer,"PetscViewerFileGetMode_C",(PetscViewer,PetscFileMode*),(viewer,type));CHKERRQ(ierr);
943*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
944*5c6c1daeSBarry Smith }
945*5c6c1daeSBarry Smith 
946*5c6c1daeSBarry Smith #undef __FUNCT__
947*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinarySetMPIIO"
948*5c6c1daeSBarry Smith /*@
949*5c6c1daeSBarry Smith      PetscViewerBinarySetMPIIO - Sets a binary viewer to use MPI IO for reading/writing. Must be called
950*5c6c1daeSBarry Smith         before PetscViewerFileSetName()
951*5c6c1daeSBarry Smith 
952*5c6c1daeSBarry Smith     Logically Collective on PetscViewer
953*5c6c1daeSBarry Smith 
954*5c6c1daeSBarry Smith   Input Parameters:
955*5c6c1daeSBarry Smith .  viewer - the PetscViewer; must be a binary
956*5c6c1daeSBarry Smith 
957*5c6c1daeSBarry Smith     Options Database:
958*5c6c1daeSBarry Smith     -viewer_binary_mpiio : Flag for using MPI-IO
959*5c6c1daeSBarry Smith 
960*5c6c1daeSBarry Smith    Notes: turns off the default usage of the .info file since that is not scalable
961*5c6c1daeSBarry Smith 
962*5c6c1daeSBarry Smith   Level: advanced
963*5c6c1daeSBarry Smith 
964*5c6c1daeSBarry Smith .seealso: PetscViewerFileSetMode(), PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinaryOpen()
965*5c6c1daeSBarry Smith 
966*5c6c1daeSBarry Smith @*/
967*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinarySetMPIIO(PetscViewer viewer)
968*5c6c1daeSBarry Smith {
969*5c6c1daeSBarry Smith   PetscErrorCode ierr;
970*5c6c1daeSBarry Smith 
971*5c6c1daeSBarry Smith   PetscFunctionBegin;
972*5c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
973*5c6c1daeSBarry Smith   ierr = PetscTryMethod(viewer,"PetscViewerBinarySetMPIIO_C",(PetscViewer),(viewer));CHKERRQ(ierr);
974*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
975*5c6c1daeSBarry Smith }
976*5c6c1daeSBarry Smith 
977*5c6c1daeSBarry Smith 
978*5c6c1daeSBarry Smith #undef __FUNCT__
979*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileSetMode"
980*5c6c1daeSBarry Smith /*@C
981*5c6c1daeSBarry Smith      PetscViewerFileSetMode - Sets the type of file to be open
982*5c6c1daeSBarry Smith 
983*5c6c1daeSBarry Smith     Logically Collective on PetscViewer
984*5c6c1daeSBarry Smith 
985*5c6c1daeSBarry Smith   Input Parameters:
986*5c6c1daeSBarry Smith +  viewer - the PetscViewer; must be a binary, Matlab, hdf, or netcdf PetscViewer
987*5c6c1daeSBarry Smith -  type - type of file
988*5c6c1daeSBarry Smith $    FILE_MODE_WRITE - create new file for binary output
989*5c6c1daeSBarry Smith $    FILE_MODE_READ - open existing file for binary input
990*5c6c1daeSBarry Smith $    FILE_MODE_APPEND - open existing file for binary output
991*5c6c1daeSBarry Smith 
992*5c6c1daeSBarry Smith   Level: advanced
993*5c6c1daeSBarry Smith 
994*5c6c1daeSBarry Smith .seealso: PetscViewerFileSetMode(), PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinaryOpen()
995*5c6c1daeSBarry Smith 
996*5c6c1daeSBarry Smith @*/
997*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileSetMode(PetscViewer viewer,PetscFileMode type)
998*5c6c1daeSBarry Smith {
999*5c6c1daeSBarry Smith   PetscErrorCode ierr;
1000*5c6c1daeSBarry Smith 
1001*5c6c1daeSBarry Smith   PetscFunctionBegin;
1002*5c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
1003*5c6c1daeSBarry Smith   PetscValidLogicalCollectiveEnum(viewer,type,2);
1004*5c6c1daeSBarry Smith   ierr = PetscTryMethod(viewer,"PetscViewerFileSetMode_C",(PetscViewer,PetscFileMode),(viewer,type));CHKERRQ(ierr);
1005*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
1006*5c6c1daeSBarry Smith }
1007*5c6c1daeSBarry Smith 
1008*5c6c1daeSBarry Smith EXTERN_C_BEGIN
1009*5c6c1daeSBarry Smith #undef __FUNCT__
1010*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileGetMode_Binary"
1011*5c6c1daeSBarry Smith PetscErrorCode PetscViewerFileGetMode_Binary(PetscViewer viewer,PetscFileMode *type)
1012*5c6c1daeSBarry Smith {
1013*5c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
1014*5c6c1daeSBarry Smith 
1015*5c6c1daeSBarry Smith   PetscFunctionBegin;
1016*5c6c1daeSBarry Smith   *type = vbinary->btype;
1017*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
1018*5c6c1daeSBarry Smith }
1019*5c6c1daeSBarry Smith EXTERN_C_END
1020*5c6c1daeSBarry Smith 
1021*5c6c1daeSBarry Smith EXTERN_C_BEGIN
1022*5c6c1daeSBarry Smith #undef __FUNCT__
1023*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileSetMode_Binary"
1024*5c6c1daeSBarry Smith PetscErrorCode PetscViewerFileSetMode_Binary(PetscViewer viewer,PetscFileMode type)
1025*5c6c1daeSBarry Smith {
1026*5c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
1027*5c6c1daeSBarry Smith 
1028*5c6c1daeSBarry Smith   PetscFunctionBegin;
1029*5c6c1daeSBarry Smith   vbinary->btype = type;
1030*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
1031*5c6c1daeSBarry Smith }
1032*5c6c1daeSBarry Smith EXTERN_C_END
1033*5c6c1daeSBarry Smith 
1034*5c6c1daeSBarry Smith /*
1035*5c6c1daeSBarry Smith         Actually opens the file
1036*5c6c1daeSBarry Smith */
1037*5c6c1daeSBarry Smith EXTERN_C_BEGIN
1038*5c6c1daeSBarry Smith #undef __FUNCT__
1039*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileSetName_Binary"
1040*5c6c1daeSBarry Smith PetscErrorCode PetscViewerFileSetName_Binary(PetscViewer viewer,const char name[])
1041*5c6c1daeSBarry Smith {
1042*5c6c1daeSBarry Smith   PetscMPIInt         rank;
1043*5c6c1daeSBarry Smith   PetscErrorCode      ierr;
1044*5c6c1daeSBarry Smith   size_t              len;
1045*5c6c1daeSBarry Smith   PetscViewer_Binary  *vbinary = (PetscViewer_Binary*)viewer->data;
1046*5c6c1daeSBarry Smith   const char          *fname;
1047*5c6c1daeSBarry Smith   char                bname[PETSC_MAX_PATH_LEN],*gz;
1048*5c6c1daeSBarry Smith   PetscBool           found;
1049*5c6c1daeSBarry Smith   PetscFileMode       type = vbinary->btype;
1050*5c6c1daeSBarry Smith 
1051*5c6c1daeSBarry Smith   PetscFunctionBegin;
1052*5c6c1daeSBarry Smith   if (type == (PetscFileMode) -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetMode() before PetscViewerFileSetName()");
1053*5c6c1daeSBarry Smith   ierr = PetscViewerFileClose_Binary(viewer);CHKERRQ(ierr);
1054*5c6c1daeSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)viewer)->prefix,"-viewer_binary_skip_info",&vbinary->skipinfo,PETSC_NULL);CHKERRQ(ierr);
1055*5c6c1daeSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)viewer)->prefix,"-viewer_binary_skip_options",&vbinary->skipoptions,PETSC_NULL);CHKERRQ(ierr);
1056*5c6c1daeSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)viewer)->prefix,"-viewer_binary_skip_header",&vbinary->skipheader,PETSC_NULL);CHKERRQ(ierr);
1057*5c6c1daeSBarry Smith 
1058*5c6c1daeSBarry Smith   ierr = MPI_Comm_rank(((PetscObject)viewer)->comm,&rank);CHKERRQ(ierr);
1059*5c6c1daeSBarry Smith 
1060*5c6c1daeSBarry Smith   /* copy name so we can edit it */
1061*5c6c1daeSBarry Smith   ierr = PetscStrallocpy(name,&vbinary->filename);CHKERRQ(ierr);
1062*5c6c1daeSBarry Smith 
1063*5c6c1daeSBarry Smith   /* if ends in .gz strip that off and note user wants file compressed */
1064*5c6c1daeSBarry Smith   vbinary->storecompressed = PETSC_FALSE;
1065*5c6c1daeSBarry Smith   if (!rank && type == FILE_MODE_WRITE) {
1066*5c6c1daeSBarry Smith     /* remove .gz if it ends library name */
1067*5c6c1daeSBarry Smith     ierr = PetscStrstr(vbinary->filename,".gz",&gz);CHKERRQ(ierr);
1068*5c6c1daeSBarry Smith     if (gz) {
1069*5c6c1daeSBarry Smith       ierr = PetscStrlen(gz,&len);CHKERRQ(ierr);
1070*5c6c1daeSBarry Smith       if (len == 3) {
1071*5c6c1daeSBarry Smith         *gz = 0;
1072*5c6c1daeSBarry Smith         vbinary->storecompressed = PETSC_TRUE;
1073*5c6c1daeSBarry Smith       }
1074*5c6c1daeSBarry Smith     }
1075*5c6c1daeSBarry Smith   }
1076*5c6c1daeSBarry Smith 
1077*5c6c1daeSBarry Smith   /* only first processor opens file if writeable */
1078*5c6c1daeSBarry Smith   if (!rank || type == FILE_MODE_READ) {
1079*5c6c1daeSBarry Smith 
1080*5c6c1daeSBarry Smith     if (type == FILE_MODE_READ){
1081*5c6c1daeSBarry Smith       /* possibly get the file from remote site or compressed file */
1082*5c6c1daeSBarry Smith       ierr  = PetscFileRetrieve(((PetscObject)viewer)->comm,vbinary->filename,bname,PETSC_MAX_PATH_LEN,&found);CHKERRQ(ierr);
1083*5c6c1daeSBarry Smith       fname = bname;
1084*5c6c1daeSBarry Smith       if (!rank && !found) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot locate file: %s on node zero",vbinary->filename);
1085*5c6c1daeSBarry Smith       else if (!found) {
1086*5c6c1daeSBarry Smith         ierr = PetscInfo(viewer,"Nonzero processor did not locate readonly file\n");CHKERRQ(ierr);
1087*5c6c1daeSBarry Smith         fname = 0;
1088*5c6c1daeSBarry Smith       }
1089*5c6c1daeSBarry Smith     } else {
1090*5c6c1daeSBarry Smith       fname = vbinary->filename;
1091*5c6c1daeSBarry Smith     }
1092*5c6c1daeSBarry Smith 
1093*5c6c1daeSBarry Smith #if defined(PETSC_HAVE_O_BINARY)
1094*5c6c1daeSBarry Smith     if (type == FILE_MODE_WRITE) {
1095*5c6c1daeSBarry Smith       if ((vbinary->fdes = open(fname,O_WRONLY|O_CREAT|O_TRUNC|O_BINARY,0666)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot create file %s for writing",fname);
1096*5c6c1daeSBarry Smith     } else if (type == FILE_MODE_READ && fname) {
1097*5c6c1daeSBarry Smith       if ((vbinary->fdes = open(fname,O_RDONLY|O_BINARY,0)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open file %s for reading",fname);
1098*5c6c1daeSBarry Smith     } else if (type == FILE_MODE_APPEND) {
1099*5c6c1daeSBarry Smith       if ((vbinary->fdes = open(fname,O_WRONLY|O_APPEND|O_BINARY,0)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open file %s for writing",fname);
1100*5c6c1daeSBarry Smith     } else if (fname) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown file type");
1101*5c6c1daeSBarry Smith #else
1102*5c6c1daeSBarry Smith     if (type == FILE_MODE_WRITE) {
1103*5c6c1daeSBarry Smith       if ((vbinary->fdes = creat(fname,0666)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot create file %s for writing",fname);
1104*5c6c1daeSBarry Smith     } else if (type == FILE_MODE_READ && fname) {
1105*5c6c1daeSBarry Smith       if ((vbinary->fdes = open(fname,O_RDONLY,0)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open file %s for reading",fname);
1106*5c6c1daeSBarry Smith     } else if (type == FILE_MODE_APPEND) {
1107*5c6c1daeSBarry Smith       if ((vbinary->fdes = open(fname,O_WRONLY|O_APPEND,0)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open file %s for writing",fname);
1108*5c6c1daeSBarry Smith     } else if (fname) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown file type");
1109*5c6c1daeSBarry Smith #endif
1110*5c6c1daeSBarry Smith   } else vbinary->fdes = -1;
1111*5c6c1daeSBarry Smith   viewer->format = PETSC_VIEWER_NOFORMAT;
1112*5c6c1daeSBarry Smith 
1113*5c6c1daeSBarry Smith   /*
1114*5c6c1daeSBarry Smith       try to open info file: all processors open this file if read only
1115*5c6c1daeSBarry Smith   */
1116*5c6c1daeSBarry Smith   if (!vbinary->skipinfo && (!rank || type == FILE_MODE_READ)) {
1117*5c6c1daeSBarry Smith     char infoname[PETSC_MAX_PATH_LEN],iname[PETSC_MAX_PATH_LEN];
1118*5c6c1daeSBarry Smith 
1119*5c6c1daeSBarry Smith     ierr = PetscStrcpy(infoname,name);CHKERRQ(ierr);
1120*5c6c1daeSBarry Smith     /* remove .gz if it ends library name */
1121*5c6c1daeSBarry Smith     ierr = PetscStrstr(infoname,".gz",&gz);CHKERRQ(ierr);
1122*5c6c1daeSBarry Smith     if (gz) {
1123*5c6c1daeSBarry Smith       ierr = PetscStrlen(gz,&len);CHKERRQ(ierr);
1124*5c6c1daeSBarry Smith       if (len == 3) {
1125*5c6c1daeSBarry Smith         *gz = 0;
1126*5c6c1daeSBarry Smith       }
1127*5c6c1daeSBarry Smith     }
1128*5c6c1daeSBarry Smith 
1129*5c6c1daeSBarry Smith     ierr = PetscStrcat(infoname,".info");CHKERRQ(ierr);
1130*5c6c1daeSBarry Smith     ierr = PetscFixFilename(infoname,iname);CHKERRQ(ierr);
1131*5c6c1daeSBarry Smith     if (type == FILE_MODE_READ) {
1132*5c6c1daeSBarry Smith       ierr = PetscFileRetrieve(((PetscObject)viewer)->comm,iname,infoname,PETSC_MAX_PATH_LEN,&found);CHKERRQ(ierr);
1133*5c6c1daeSBarry Smith       ierr = PetscOptionsInsertFile(((PetscObject)viewer)->comm,infoname,PETSC_FALSE);CHKERRQ(ierr);
1134*5c6c1daeSBarry Smith     } else {
1135*5c6c1daeSBarry Smith       vbinary->fdes_info = fopen(infoname,"w");
1136*5c6c1daeSBarry Smith       if (!vbinary->fdes_info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open .info file %s for writing",infoname);
1137*5c6c1daeSBarry Smith     }
1138*5c6c1daeSBarry Smith   }
1139*5c6c1daeSBarry Smith 
1140*5c6c1daeSBarry Smith #if defined(PETSC_USE_LOG)
1141*5c6c1daeSBarry Smith   PetscLogObjectState((PetscObject)viewer,"File: %s",name);
1142*5c6c1daeSBarry Smith #endif
1143*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
1144*5c6c1daeSBarry Smith }
1145*5c6c1daeSBarry Smith EXTERN_C_END
1146*5c6c1daeSBarry Smith 
1147*5c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
1148*5c6c1daeSBarry Smith EXTERN_C_BEGIN
1149*5c6c1daeSBarry Smith #undef __FUNCT__
1150*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileSetName_MPIIO"
1151*5c6c1daeSBarry Smith PetscErrorCode PetscViewerFileSetName_MPIIO(PetscViewer viewer,const char name[])
1152*5c6c1daeSBarry Smith {
1153*5c6c1daeSBarry Smith   PetscMPIInt         rank;
1154*5c6c1daeSBarry Smith   PetscErrorCode      ierr;
1155*5c6c1daeSBarry Smith   size_t              len;
1156*5c6c1daeSBarry Smith   PetscViewer_Binary  *vbinary = (PetscViewer_Binary*)viewer->data;
1157*5c6c1daeSBarry Smith   char                *gz;
1158*5c6c1daeSBarry Smith   PetscBool           found;
1159*5c6c1daeSBarry Smith   PetscFileMode       type = vbinary->btype;
1160*5c6c1daeSBarry Smith 
1161*5c6c1daeSBarry Smith   PetscFunctionBegin;
1162*5c6c1daeSBarry Smith   if (type == (PetscFileMode) -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetMode() before PetscViewerFileSetName()");
1163*5c6c1daeSBarry Smith   ierr = PetscViewerFileClose_MPIIO(viewer);CHKERRQ(ierr);
1164*5c6c1daeSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)viewer)->prefix,"-viewer_binary_skip_info",&vbinary->skipinfo,PETSC_NULL);CHKERRQ(ierr);
1165*5c6c1daeSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)viewer)->prefix,"-viewer_binary_skip_options",&vbinary->skipoptions,PETSC_NULL);CHKERRQ(ierr);
1166*5c6c1daeSBarry Smith 
1167*5c6c1daeSBarry Smith   ierr = MPI_Comm_rank(((PetscObject)viewer)->comm,&rank);CHKERRQ(ierr);
1168*5c6c1daeSBarry Smith   ierr = PetscStrallocpy(name,&vbinary->filename);CHKERRQ(ierr);
1169*5c6c1daeSBarry Smith   vbinary->storecompressed = PETSC_FALSE;
1170*5c6c1daeSBarry Smith 
1171*5c6c1daeSBarry Smith 
1172*5c6c1daeSBarry Smith   /* only first processor opens file if writeable */
1173*5c6c1daeSBarry Smith   if (type == FILE_MODE_READ) {
1174*5c6c1daeSBarry Smith     MPI_File_open(((PetscObject)viewer)->comm,vbinary->filename,MPI_MODE_RDONLY,MPI_INFO_NULL,&vbinary->mfdes);CHKERRQ(ierr);
1175*5c6c1daeSBarry Smith   } else if (type == FILE_MODE_WRITE) {
1176*5c6c1daeSBarry Smith     MPI_File_open(((PetscObject)viewer)->comm,vbinary->filename,MPI_MODE_WRONLY | MPI_MODE_CREATE,MPI_INFO_NULL,&vbinary->mfdes);CHKERRQ(ierr);
1177*5c6c1daeSBarry Smith   }
1178*5c6c1daeSBarry Smith   viewer->format = PETSC_VIEWER_NOFORMAT;
1179*5c6c1daeSBarry Smith 
1180*5c6c1daeSBarry Smith   /*
1181*5c6c1daeSBarry Smith       try to open info file: all processors open this file if read only
1182*5c6c1daeSBarry Smith 
1183*5c6c1daeSBarry Smith       Below is identical code to the code for Binary above, should be put in seperate routine
1184*5c6c1daeSBarry Smith   */
1185*5c6c1daeSBarry Smith   if (!vbinary->skipinfo && (!rank || type == FILE_MODE_READ)) {
1186*5c6c1daeSBarry Smith     char infoname[PETSC_MAX_PATH_LEN],iname[PETSC_MAX_PATH_LEN];
1187*5c6c1daeSBarry Smith 
1188*5c6c1daeSBarry Smith     ierr = PetscStrcpy(infoname,name);CHKERRQ(ierr);
1189*5c6c1daeSBarry Smith     /* remove .gz if it ends library name */
1190*5c6c1daeSBarry Smith     ierr = PetscStrstr(infoname,".gz",&gz);CHKERRQ(ierr);
1191*5c6c1daeSBarry Smith     if (gz) {
1192*5c6c1daeSBarry Smith       ierr = PetscStrlen(gz,&len);CHKERRQ(ierr);
1193*5c6c1daeSBarry Smith       if (len == 3) {
1194*5c6c1daeSBarry Smith         *gz = 0;
1195*5c6c1daeSBarry Smith       }
1196*5c6c1daeSBarry Smith     }
1197*5c6c1daeSBarry Smith 
1198*5c6c1daeSBarry Smith     ierr = PetscStrcat(infoname,".info");CHKERRQ(ierr);
1199*5c6c1daeSBarry Smith     ierr = PetscFixFilename(infoname,iname);CHKERRQ(ierr);
1200*5c6c1daeSBarry Smith     if (type == FILE_MODE_READ) {
1201*5c6c1daeSBarry Smith       ierr = PetscFileRetrieve(((PetscObject)viewer)->comm,iname,infoname,PETSC_MAX_PATH_LEN,&found);CHKERRQ(ierr);
1202*5c6c1daeSBarry Smith       ierr = PetscOptionsInsertFile(((PetscObject)viewer)->comm,infoname,PETSC_FALSE);CHKERRQ(ierr);
1203*5c6c1daeSBarry Smith     } else {
1204*5c6c1daeSBarry Smith       vbinary->fdes_info = fopen(infoname,"w");
1205*5c6c1daeSBarry Smith       if (!vbinary->fdes_info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open .info file %s for writing",infoname);
1206*5c6c1daeSBarry Smith     }
1207*5c6c1daeSBarry Smith   }
1208*5c6c1daeSBarry Smith 
1209*5c6c1daeSBarry Smith #if defined(PETSC_USE_LOG)
1210*5c6c1daeSBarry Smith   PetscLogObjectState((PetscObject)viewer,"File: %s",name);
1211*5c6c1daeSBarry Smith #endif
1212*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
1213*5c6c1daeSBarry Smith }
1214*5c6c1daeSBarry Smith EXTERN_C_END
1215*5c6c1daeSBarry Smith 
1216*5c6c1daeSBarry Smith EXTERN_C_BEGIN
1217*5c6c1daeSBarry Smith #undef __FUNCT__
1218*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinarySetMPIIO_Binary"
1219*5c6c1daeSBarry Smith PetscErrorCode PetscViewerBinarySetMPIIO_Binary(PetscViewer viewer)
1220*5c6c1daeSBarry Smith {
1221*5c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
1222*5c6c1daeSBarry Smith   PetscErrorCode     ierr;
1223*5c6c1daeSBarry Smith 
1224*5c6c1daeSBarry Smith   PetscFunctionBegin;
1225*5c6c1daeSBarry Smith   if (vbinary->filename) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call before calling PetscViewerFileSetName()");
1226*5c6c1daeSBarry Smith   viewer->ops->destroy = PetscViewerDestroy_MPIIO;
1227*5c6c1daeSBarry Smith   vbinary->MPIIO       = PETSC_TRUE;
1228*5c6c1daeSBarry Smith   /*  vbinary->skipinfo    = PETSC_TRUE; */
1229*5c6c1daeSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)viewer,"PetscViewerFileSetName_C","PetscViewerFileSetName_MPIIO",PetscViewerFileSetName_MPIIO);CHKERRQ(ierr);
1230*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
1231*5c6c1daeSBarry Smith }
1232*5c6c1daeSBarry Smith EXTERN_C_END
1233*5c6c1daeSBarry Smith #endif
1234*5c6c1daeSBarry Smith 
1235*5c6c1daeSBarry Smith EXTERN_C_BEGIN
1236*5c6c1daeSBarry Smith #undef __FUNCT__
1237*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerCreate_Binary"
1238*5c6c1daeSBarry Smith PetscErrorCode PetscViewerCreate_Binary(PetscViewer v)
1239*5c6c1daeSBarry Smith {
1240*5c6c1daeSBarry Smith   PetscErrorCode     ierr;
1241*5c6c1daeSBarry Smith   PetscViewer_Binary *vbinary;
1242*5c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
1243*5c6c1daeSBarry Smith   PetscBool          useMPIIO = PETSC_FALSE;
1244*5c6c1daeSBarry Smith #endif
1245*5c6c1daeSBarry Smith 
1246*5c6c1daeSBarry Smith   PetscFunctionBegin;
1247*5c6c1daeSBarry Smith   ierr               = PetscNewLog(v,PetscViewer_Binary,&vbinary);CHKERRQ(ierr);
1248*5c6c1daeSBarry Smith   v->data            = (void*)vbinary;
1249*5c6c1daeSBarry Smith   v->ops->destroy    = PetscViewerDestroy_Binary;
1250*5c6c1daeSBarry Smith   v->ops->flush      = 0;
1251*5c6c1daeSBarry Smith   v->iformat         = 0;
1252*5c6c1daeSBarry Smith   vbinary->fdes_info = 0;
1253*5c6c1daeSBarry Smith   vbinary->fdes      = 0;
1254*5c6c1daeSBarry Smith   vbinary->skipinfo        = PETSC_FALSE;
1255*5c6c1daeSBarry Smith   vbinary->skipoptions     = PETSC_TRUE;
1256*5c6c1daeSBarry Smith   vbinary->skipheader      = PETSC_FALSE;
1257*5c6c1daeSBarry Smith   v->ops->getsingleton     = PetscViewerGetSingleton_Binary;
1258*5c6c1daeSBarry Smith   v->ops->restoresingleton = PetscViewerRestoreSingleton_Binary;
1259*5c6c1daeSBarry Smith   vbinary->btype           = (PetscFileMode) -1;
1260*5c6c1daeSBarry Smith   vbinary->storecompressed = PETSC_FALSE;
1261*5c6c1daeSBarry Smith   vbinary->filename        = 0;
1262*5c6c1daeSBarry Smith   vbinary->flowcontrol     = 256; /* seems a good number for Cray XT-5 */
1263*5c6c1daeSBarry Smith 
1264*5c6c1daeSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerBinaryGetFlowControl_C",
1265*5c6c1daeSBarry Smith                                     "PetscViewerBinaryGetFlowControl_Binary",
1266*5c6c1daeSBarry Smith                                      PetscViewerBinaryGetFlowControl_Binary);CHKERRQ(ierr);
1267*5c6c1daeSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerBinarySetFlowControl_C",
1268*5c6c1daeSBarry Smith                                     "PetscViewerBinarySetFlowControl_Binary",
1269*5c6c1daeSBarry Smith                                      PetscViewerBinarySetFlowControl_Binary);CHKERRQ(ierr);
1270*5c6c1daeSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerBinarySetSkipHeader_C",
1271*5c6c1daeSBarry Smith                                     "PetscViewerBinarySetSkipHeader_Binary",
1272*5c6c1daeSBarry Smith                                      PetscViewerBinarySetSkipHeader_Binary);CHKERRQ(ierr);
1273*5c6c1daeSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerBinaryGetSkipHeader_C",
1274*5c6c1daeSBarry Smith                                     "PetscViewerBinaryGetSkipHeader_Binary",
1275*5c6c1daeSBarry Smith                                      PetscViewerBinaryGetSkipHeader_Binary);CHKERRQ(ierr);
1276*5c6c1daeSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerBinaryGetInfoPointer_C",
1277*5c6c1daeSBarry Smith                                     "PetscViewerBinaryGetInfoPointer_Binary",
1278*5c6c1daeSBarry Smith                                      PetscViewerBinaryGetInfoPointer_Binary);CHKERRQ(ierr);
1279*5c6c1daeSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerFileSetName_C",
1280*5c6c1daeSBarry Smith                                     "PetscViewerFileSetName_Binary",
1281*5c6c1daeSBarry Smith                                      PetscViewerFileSetName_Binary);CHKERRQ(ierr);
1282*5c6c1daeSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerFileSetMode_C",
1283*5c6c1daeSBarry Smith                                     "PetscViewerFileSetMode_Binary",
1284*5c6c1daeSBarry Smith                                      PetscViewerFileSetMode_Binary);CHKERRQ(ierr);
1285*5c6c1daeSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerFileGetMode_C",
1286*5c6c1daeSBarry Smith                                     "PetscViewerFileGetMode_Binary",
1287*5c6c1daeSBarry Smith                                      PetscViewerFileGetMode_Binary);CHKERRQ(ierr);
1288*5c6c1daeSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerFileGetName_C",
1289*5c6c1daeSBarry Smith 				    "PetscViewerFileGetName_Binary",
1290*5c6c1daeSBarry Smith 				     PetscViewerFileGetName_Binary);CHKERRQ(ierr);
1291*5c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
1292*5c6c1daeSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerBinarySetMPIIO_C",
1293*5c6c1daeSBarry Smith                                     "PetscViewerBinarySetMPIIO_Binary",
1294*5c6c1daeSBarry Smith                                      PetscViewerBinarySetMPIIO_Binary);CHKERRQ(ierr);
1295*5c6c1daeSBarry Smith 
1296*5c6c1daeSBarry Smith   ierr = PetscOptionsGetBool(PETSC_NULL,"-viewer_binary_mpiio",&useMPIIO,PETSC_NULL);CHKERRQ(ierr);
1297*5c6c1daeSBarry Smith   if (useMPIIO) {
1298*5c6c1daeSBarry Smith     ierr = PetscViewerBinarySetMPIIO(v);CHKERRQ(ierr);
1299*5c6c1daeSBarry Smith   }
1300*5c6c1daeSBarry Smith #endif
1301*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
1302*5c6c1daeSBarry Smith }
1303*5c6c1daeSBarry Smith EXTERN_C_END
1304*5c6c1daeSBarry Smith 
1305*5c6c1daeSBarry Smith 
1306*5c6c1daeSBarry Smith /* ---------------------------------------------------------------------*/
1307*5c6c1daeSBarry Smith /*
1308*5c6c1daeSBarry Smith     The variable Petsc_Viewer_Binary_keyval is used to indicate an MPI attribute that
1309*5c6c1daeSBarry Smith   is attached to a communicator, in this case the attribute is a PetscViewer.
1310*5c6c1daeSBarry Smith */
1311*5c6c1daeSBarry Smith static int Petsc_Viewer_Binary_keyval = MPI_KEYVAL_INVALID;
1312*5c6c1daeSBarry Smith 
1313*5c6c1daeSBarry Smith #undef __FUNCT__
1314*5c6c1daeSBarry Smith #define __FUNCT__ "PETSC_VIEWER_BINARY_"
1315*5c6c1daeSBarry Smith /*@C
1316*5c6c1daeSBarry Smith      PETSC_VIEWER_BINARY_ - Creates a binary PetscViewer shared by all processors
1317*5c6c1daeSBarry Smith                      in a communicator.
1318*5c6c1daeSBarry Smith 
1319*5c6c1daeSBarry Smith      Collective on MPI_Comm
1320*5c6c1daeSBarry Smith 
1321*5c6c1daeSBarry Smith      Input Parameter:
1322*5c6c1daeSBarry Smith .    comm - the MPI communicator to share the binary PetscViewer
1323*5c6c1daeSBarry Smith 
1324*5c6c1daeSBarry Smith      Level: intermediate
1325*5c6c1daeSBarry Smith 
1326*5c6c1daeSBarry Smith    Options Database Keys:
1327*5c6c1daeSBarry Smith +    -viewer_binary_filename <name>
1328*5c6c1daeSBarry Smith .    -viewer_binary_skip_info
1329*5c6c1daeSBarry Smith -    -viewer_binary_skip_options
1330*5c6c1daeSBarry Smith 
1331*5c6c1daeSBarry Smith    Environmental variables:
1332*5c6c1daeSBarry Smith -   PETSC_VIEWER_BINARY_FILENAME
1333*5c6c1daeSBarry Smith 
1334*5c6c1daeSBarry Smith      Notes:
1335*5c6c1daeSBarry Smith      Unlike almost all other PETSc routines, PETSC_VIEWER_BINARY_ does not return
1336*5c6c1daeSBarry Smith      an error code.  The binary PetscViewer is usually used in the form
1337*5c6c1daeSBarry Smith $       XXXView(XXX object,PETSC_VIEWER_BINARY_(comm));
1338*5c6c1daeSBarry Smith 
1339*5c6c1daeSBarry Smith .seealso: PETSC_VIEWER_BINARY_WORLD, PETSC_VIEWER_BINARY_SELF, PetscViewerBinaryOpen(), PetscViewerCreate(),
1340*5c6c1daeSBarry Smith           PetscViewerDestroy()
1341*5c6c1daeSBarry Smith @*/
1342*5c6c1daeSBarry Smith PetscViewer  PETSC_VIEWER_BINARY_(MPI_Comm comm)
1343*5c6c1daeSBarry Smith {
1344*5c6c1daeSBarry Smith   PetscErrorCode ierr;
1345*5c6c1daeSBarry Smith   PetscBool      flg;
1346*5c6c1daeSBarry Smith   PetscViewer    viewer;
1347*5c6c1daeSBarry Smith   char           fname[PETSC_MAX_PATH_LEN];
1348*5c6c1daeSBarry Smith   MPI_Comm       ncomm;
1349*5c6c1daeSBarry Smith 
1350*5c6c1daeSBarry Smith   PetscFunctionBegin;
1351*5c6c1daeSBarry Smith   ierr = PetscCommDuplicate(comm,&ncomm,PETSC_NULL);if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
1352*5c6c1daeSBarry Smith   if (Petsc_Viewer_Binary_keyval == MPI_KEYVAL_INVALID) {
1353*5c6c1daeSBarry Smith     ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Binary_keyval,0);
1354*5c6c1daeSBarry Smith     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
1355*5c6c1daeSBarry Smith   }
1356*5c6c1daeSBarry Smith   ierr = MPI_Attr_get(ncomm,Petsc_Viewer_Binary_keyval,(void **)&viewer,(int*)&flg);
1357*5c6c1daeSBarry Smith   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
1358*5c6c1daeSBarry Smith   if (!flg) { /* PetscViewer not yet created */
1359*5c6c1daeSBarry Smith     ierr = PetscOptionsGetenv(ncomm,"PETSC_VIEWER_BINARY_FILENAME",fname,PETSC_MAX_PATH_LEN,&flg);
1360*5c6c1daeSBarry Smith     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
1361*5c6c1daeSBarry Smith     if (!flg) {
1362*5c6c1daeSBarry Smith       ierr = PetscStrcpy(fname,"binaryoutput");
1363*5c6c1daeSBarry Smith       if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
1364*5c6c1daeSBarry Smith     }
1365*5c6c1daeSBarry Smith     ierr = PetscViewerBinaryOpen(ncomm,fname,FILE_MODE_WRITE,&viewer);
1366*5c6c1daeSBarry Smith     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
1367*5c6c1daeSBarry Smith     ierr = PetscObjectRegisterDestroy((PetscObject)viewer);
1368*5c6c1daeSBarry Smith     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
1369*5c6c1daeSBarry Smith     ierr = MPI_Attr_put(ncomm,Petsc_Viewer_Binary_keyval,(void*)viewer);
1370*5c6c1daeSBarry Smith     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
1371*5c6c1daeSBarry Smith   }
1372*5c6c1daeSBarry Smith   ierr = PetscCommDestroy(&ncomm);
1373*5c6c1daeSBarry Smith   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
1374*5c6c1daeSBarry Smith   PetscFunctionReturn(viewer);
1375*5c6c1daeSBarry Smith }
1376