xref: /petsc/src/sys/classes/viewer/impls/binary/binv.c (revision 69e10bbac45da07e98105477f20d70798e525967)
15c6c1daeSBarry Smith 
2af0996ceSBarry Smith #include <petsc/private/viewerimpl.h>    /*I   "petscviewer.h"   I*/
35c6c1daeSBarry Smith #include <fcntl.h>
45c6c1daeSBarry Smith #if defined(PETSC_HAVE_UNISTD_H)
55c6c1daeSBarry Smith #include <unistd.h>
65c6c1daeSBarry Smith #endif
75c6c1daeSBarry Smith #if defined(PETSC_HAVE_IO_H)
85c6c1daeSBarry Smith #include <io.h>
95c6c1daeSBarry Smith #endif
105c6c1daeSBarry Smith 
115c6c1daeSBarry Smith typedef struct  {
125c6c1daeSBarry Smith   int           fdes;                 /* file descriptor, ignored if using MPI IO */
135c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
14bc196f7cSDave May   PetscBool     usempiio;
155c6c1daeSBarry Smith   MPI_File      mfdes;                /* ignored unless using MPI IO */
16e8a65b7dSLisandro Dalcin   MPI_File      mfsub;                /* subviewer support */
175c6c1daeSBarry Smith   MPI_Offset    moff;
185c6c1daeSBarry Smith #endif
195c6c1daeSBarry Smith   PetscFileMode btype;                /* read or write? */
205c6c1daeSBarry Smith   FILE          *fdes_info;           /* optional file containing info on binary file*/
215c6c1daeSBarry Smith   PetscBool     storecompressed;      /* gzip the write binary file when closing it*/
225c6c1daeSBarry Smith   char          *filename;
23f90597f1SStefano Zampini   char          *ogzfilename;         /* gzip can be run after the filename has been updated */
245c6c1daeSBarry Smith   PetscBool     skipinfo;             /* Don't create info file for writing; don't use for reading */
255c6c1daeSBarry Smith   PetscBool     skipoptions;          /* don't use PETSc options database when loading */
265c6c1daeSBarry Smith   PetscInt      flowcontrol;          /* allow only <flowcontrol> messages outstanding at a time while doing IO */
275c6c1daeSBarry Smith   PetscBool     skipheader;           /* don't write header, only raw data */
28a261c58fSBarry Smith   PetscBool     matlabheaderwritten;  /* if format is PETSC_VIEWER_BINARY_MATLAB has the MATLAB .info header been written yet */
29c98fd787SBarry Smith   PetscBool     setfromoptionscalled;
305c6c1daeSBarry Smith } PetscViewer_Binary;
315c6c1daeSBarry Smith 
3281f0254dSBarry Smith static PetscErrorCode PetscViewerGetSubViewer_Binary(PetscViewer viewer,MPI_Comm comm,PetscViewer *outviewer)
335c6c1daeSBarry Smith {
345c6c1daeSBarry Smith   int                rank;
355c6c1daeSBarry Smith   PetscErrorCode     ierr;
36e8a65b7dSLisandro Dalcin   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
375c6c1daeSBarry Smith 
385c6c1daeSBarry Smith   PetscFunctionBegin;
39c98fd787SBarry Smith   ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr);
40e8a65b7dSLisandro Dalcin 
41e8a65b7dSLisandro Dalcin   /* Return subviewer in process zero */
42ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
435c6c1daeSBarry Smith   if (!rank) {
44e5afcf28SBarry Smith     PetscMPIInt flg;
45e5afcf28SBarry Smith 
46e5afcf28SBarry Smith     ierr = MPI_Comm_compare(PETSC_COMM_SELF,comm,&flg);CHKERRQ(ierr);
47e5afcf28SBarry Smith     if (flg != MPI_IDENT && flg != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"PetscViewerGetSubViewer() for PETSCVIEWERBINARY requires a singleton MPI_Comm");
48e5afcf28SBarry Smith     ierr = PetscViewerCreate(comm,outviewer);CHKERRQ(ierr);
495c6c1daeSBarry Smith     ierr = PetscViewerSetType(*outviewer,PETSCVIEWERBINARY);CHKERRQ(ierr);
50e8a65b7dSLisandro Dalcin     ierr = PetscMemcpy((*outviewer)->data,vbinary,sizeof(PetscViewer_Binary));CHKERRQ(ierr);
5112f4c3a9SLisandro Dalcin     (*outviewer)->setupcalled = PETSC_TRUE;
5296509d17SLisandro Dalcin   } else {
5396509d17SLisandro Dalcin     *outviewer = NULL;
5496509d17SLisandro Dalcin   }
55e8a65b7dSLisandro Dalcin 
56e8a65b7dSLisandro Dalcin #if defined(PETSC_HAVE_MPIIO)
57e8a65b7dSLisandro Dalcin   if (vbinary->usempiio && *outviewer) {
58e8a65b7dSLisandro Dalcin     PetscViewer_Binary *obinary = (PetscViewer_Binary*)(*outviewer)->data;
59e8a65b7dSLisandro Dalcin     /* Parent viewer opens a new MPI file handle on PETSC_COMM_SELF and keeps track of it for future reuse */
60e8a65b7dSLisandro Dalcin     if (vbinary->mfsub == MPI_FILE_NULL) {
61e8a65b7dSLisandro Dalcin       int amode;
62e8a65b7dSLisandro Dalcin       switch (vbinary->btype) {
63e8a65b7dSLisandro Dalcin       case FILE_MODE_READ:  amode = MPI_MODE_RDONLY; break;
64e8a65b7dSLisandro Dalcin       case FILE_MODE_WRITE: amode = MPI_MODE_WRONLY; break;
65e8a65b7dSLisandro Dalcin       default: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unsupported file mode %s",PetscFileModes[vbinary->btype]);
66e8a65b7dSLisandro Dalcin       }
67e8a65b7dSLisandro Dalcin       ierr = MPI_File_open(PETSC_COMM_SELF,vbinary->filename,amode,MPI_INFO_NULL,&vbinary->mfsub);CHKERRQ(ierr);
68e8a65b7dSLisandro Dalcin     }
69e8a65b7dSLisandro Dalcin     /* Subviewer gets the MPI file handle on PETSC_COMM_SELF */
70e8a65b7dSLisandro Dalcin     obinary->mfdes = vbinary->mfsub;
71e8a65b7dSLisandro Dalcin     obinary->mfsub = MPI_FILE_NULL;
72e8a65b7dSLisandro Dalcin     obinary->moff  = vbinary->moff;
73e8a65b7dSLisandro Dalcin   }
74e8a65b7dSLisandro Dalcin #endif
755c6c1daeSBarry Smith   PetscFunctionReturn(0);
765c6c1daeSBarry Smith }
775c6c1daeSBarry Smith 
7881f0254dSBarry Smith static PetscErrorCode PetscViewerRestoreSubViewer_Binary(PetscViewer viewer,MPI_Comm comm,PetscViewer *outviewer)
795c6c1daeSBarry Smith {
80e5afcf28SBarry Smith   PetscMPIInt        rank;
81e8a65b7dSLisandro Dalcin   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
82e8a65b7dSLisandro Dalcin   PetscErrorCode     ierr;
83e8a65b7dSLisandro Dalcin #if defined(PETSC_HAVE_MPIIO)
84e8a65b7dSLisandro Dalcin   MPI_Offset         moff = 0;
85e8a65b7dSLisandro Dalcin #endif
865c6c1daeSBarry Smith 
875c6c1daeSBarry Smith   PetscFunctionBegin;
88ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
89e8a65b7dSLisandro Dalcin   if (rank && *outviewer) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Subviewer not obtained from viewer");
90e8a65b7dSLisandro Dalcin 
91e8a65b7dSLisandro Dalcin #if defined(PETSC_HAVE_MPIIO)
92e8a65b7dSLisandro Dalcin   if (vbinary->usempiio && *outviewer) {
93e8a65b7dSLisandro Dalcin     PetscViewer_Binary *obinary = (PetscViewer_Binary*)(*outviewer)->data;
94e8a65b7dSLisandro Dalcin     if (obinary->mfdes != vbinary->mfsub) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Subviewer not obtained from viewer");
95e8a65b7dSLisandro Dalcin     moff = obinary->moff;
96e8a65b7dSLisandro Dalcin   }
97e8a65b7dSLisandro Dalcin #endif
98e8a65b7dSLisandro Dalcin 
99e8a65b7dSLisandro Dalcin   if (*outviewer) {
100e8a65b7dSLisandro Dalcin     PetscViewer_Binary *obinary = (PetscViewer_Binary*)(*outviewer)->data;
101e8a65b7dSLisandro Dalcin     if (obinary->fdes != vbinary->fdes) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Subviewer not obtained from viewer");
1025c6c1daeSBarry Smith     ierr = PetscFree((*outviewer)->data);CHKERRQ(ierr);
1035c6c1daeSBarry Smith     ierr = PetscHeaderDestroy(outviewer);CHKERRQ(ierr);
1045c6c1daeSBarry Smith   }
105e8a65b7dSLisandro Dalcin 
106e8a65b7dSLisandro Dalcin #if defined(PETSC_HAVE_MPIIO)
107e8a65b7dSLisandro Dalcin   if (vbinary->usempiio) {
108e8a65b7dSLisandro Dalcin     PetscInt64 ioff = (PetscInt64)moff; /* We could use MPI_OFFSET datatype (requires MPI 2.2) */
109e8a65b7dSLisandro Dalcin     ierr = MPI_Bcast(&ioff,1,MPIU_INT64,0,PetscObjectComm((PetscObject)viewer));CHKERRQ(ierr);
110e8a65b7dSLisandro Dalcin     vbinary->moff = (MPI_Offset)ioff;
111e8a65b7dSLisandro Dalcin   }
112e8a65b7dSLisandro Dalcin #endif
1135c6c1daeSBarry Smith   PetscFunctionReturn(0);
1145c6c1daeSBarry Smith }
1155c6c1daeSBarry Smith 
1165c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
1175c6c1daeSBarry Smith /*@C
1185c6c1daeSBarry Smith     PetscViewerBinaryGetMPIIOOffset - Gets the current offset that should be passed to MPI_File_set_view()
1195c6c1daeSBarry Smith 
1205c6c1daeSBarry Smith     Not Collective
1215c6c1daeSBarry Smith 
1225c6c1daeSBarry Smith     Input Parameter:
1235c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
1245c6c1daeSBarry Smith 
1255c6c1daeSBarry Smith     Output Parameter:
1265c6c1daeSBarry Smith .    off - the current offset
1275c6c1daeSBarry Smith 
1285c6c1daeSBarry Smith     Level: advanced
1295c6c1daeSBarry Smith 
1305c6c1daeSBarry Smith     Fortran Note:
1315c6c1daeSBarry Smith     This routine is not supported in Fortran.
1325c6c1daeSBarry Smith 
1335c6c1daeSBarry Smith     Use PetscViewerBinaryAddMPIIOOffset() to increase this value after you have written a view.
1345c6c1daeSBarry Smith 
1355c6c1daeSBarry Smith 
13667918a83SBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetInfoPointer(), PetscViewerBinaryGetUseMPIIO(), PetscViewerBinarySetUseMPIIO(), PetscViewerBinaryAddMPIIOOffset()
1375c6c1daeSBarry Smith @*/
1385c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetMPIIOOffset(PetscViewer viewer,MPI_Offset *off)
1395c6c1daeSBarry Smith {
1405c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
1415c6c1daeSBarry Smith 
1425c6c1daeSBarry Smith   PetscFunctionBegin;
1435c6c1daeSBarry Smith   *off = vbinary->moff;
1445c6c1daeSBarry Smith   PetscFunctionReturn(0);
1455c6c1daeSBarry Smith }
1465c6c1daeSBarry Smith 
1475c6c1daeSBarry Smith /*@C
1485c6c1daeSBarry Smith     PetscViewerBinaryAddMPIIOOffset - Adds to the current offset that should be passed to MPI_File_set_view()
1495c6c1daeSBarry Smith 
1505c6c1daeSBarry Smith     Not Collective
1515c6c1daeSBarry Smith 
1525c6c1daeSBarry Smith     Input Parameters:
1535c6c1daeSBarry Smith +   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
1545c6c1daeSBarry Smith -    off - the addition to the offset
1555c6c1daeSBarry Smith 
1565c6c1daeSBarry Smith     Level: advanced
1575c6c1daeSBarry Smith 
1585c6c1daeSBarry Smith     Fortran Note:
1595c6c1daeSBarry Smith     This routine is not supported in Fortran.
1605c6c1daeSBarry Smith 
1615c6c1daeSBarry Smith     Use PetscViewerBinaryGetMPIIOOffset() to get the value that you should pass to MPI_File_set_view()
1625c6c1daeSBarry Smith 
1635c6c1daeSBarry Smith 
16467918a83SBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer(), PetscViewerBinaryGetUseMPIIO(), PetscViewerBinarySetUseMPIIO(), PetscViewerBinaryGetMPIIOOffset()
1655c6c1daeSBarry Smith @*/
1665c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryAddMPIIOOffset(PetscViewer viewer,MPI_Offset off)
1675c6c1daeSBarry Smith {
1685c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
1695c6c1daeSBarry Smith 
1705c6c1daeSBarry Smith   PetscFunctionBegin;
1715c6c1daeSBarry Smith   vbinary->moff += off;
1725c6c1daeSBarry Smith   PetscFunctionReturn(0);
1735c6c1daeSBarry Smith }
1745c6c1daeSBarry Smith 
1755c6c1daeSBarry Smith /*@C
1765c6c1daeSBarry Smith     PetscViewerBinaryGetMPIIODescriptor - Extracts the MPI IO file descriptor from a PetscViewer.
1775c6c1daeSBarry Smith 
1785c6c1daeSBarry Smith     Not Collective
1795c6c1daeSBarry Smith 
1805c6c1daeSBarry Smith     Input Parameter:
1815c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
1825c6c1daeSBarry Smith 
1835c6c1daeSBarry Smith     Output Parameter:
1845c6c1daeSBarry Smith .   fdes - file descriptor
1855c6c1daeSBarry Smith 
1865c6c1daeSBarry Smith     Level: advanced
1875c6c1daeSBarry Smith 
1885c6c1daeSBarry Smith     Fortran Note:
1895c6c1daeSBarry Smith     This routine is not supported in Fortran.
1905c6c1daeSBarry Smith 
1915c6c1daeSBarry Smith 
19267918a83SBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer(), PetscViewerBinaryGetUseMPIIO(), PetscViewerBinarySetUseMPIIO(), PetscViewerBinaryGetMPIIOOffset()
1935c6c1daeSBarry Smith @*/
1945c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetMPIIODescriptor(PetscViewer viewer,MPI_File *fdes)
1955c6c1daeSBarry Smith {
19603a1d59fSDave May   PetscErrorCode     ierr;
1975c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
1985c6c1daeSBarry Smith 
1995c6c1daeSBarry Smith   PetscFunctionBegin;
200c98fd787SBarry Smith   ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr);
2015c6c1daeSBarry Smith   *fdes = vbinary->mfdes;
2025c6c1daeSBarry Smith   PetscFunctionReturn(0);
2035c6c1daeSBarry Smith }
2045c6c1daeSBarry Smith 
20581f0254dSBarry Smith static PetscErrorCode PetscViewerBinaryGetUseMPIIO_Binary(PetscViewer viewer,PetscBool  *flg)
206a8aae444SDave May {
207a8aae444SDave May   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
208a8aae444SDave May 
209a8aae444SDave May   PetscFunctionBegin;
210bc196f7cSDave May   *flg = vbinary->usempiio;
211a8aae444SDave May   PetscFunctionReturn(0);
212a8aae444SDave May }
213a8aae444SDave May #endif
214a8aae444SDave May 
215a8aae444SDave May 
2165c6c1daeSBarry Smith /*@C
217bc196f7cSDave May     PetscViewerBinaryGetUseMPIIO - Returns PETSC_TRUE if the binary viewer uses MPI-IO.
2185c6c1daeSBarry Smith 
2195c6c1daeSBarry Smith     Not Collective
2205c6c1daeSBarry Smith 
2215c6c1daeSBarry Smith     Input Parameter:
2225c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
2235c6c1daeSBarry Smith 
2245c6c1daeSBarry Smith     Output Parameter:
225bc196f7cSDave May -   flg - PETSC_TRUE if MPI-IO is being used
2265c6c1daeSBarry Smith 
2275c6c1daeSBarry Smith     Options Database:
2285c6c1daeSBarry Smith     -viewer_binary_mpiio : Flag for using MPI-IO
2295c6c1daeSBarry Smith 
2305c6c1daeSBarry Smith     Level: advanced
2315c6c1daeSBarry Smith 
232bc196f7cSDave May     Note:
233bc196f7cSDave May     If MPI-IO is not available, this function will always return PETSC_FALSE
234bc196f7cSDave May 
2355c6c1daeSBarry Smith     Fortran Note:
2365c6c1daeSBarry Smith     This routine is not supported in Fortran.
2375c6c1daeSBarry Smith 
2385c6c1daeSBarry Smith 
23967918a83SBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetInfoPointer(), PetscViewerBinarySetUseMPIIO(), PetscViewerBinaryGetMPIIOOffset()
2405c6c1daeSBarry Smith @*/
241bc196f7cSDave May PetscErrorCode PetscViewerBinaryGetUseMPIIO(PetscViewer viewer,PetscBool *flg)
2425c6c1daeSBarry Smith {
243a8aae444SDave May   PetscErrorCode ierr;
2445c6c1daeSBarry Smith 
2455c6c1daeSBarry Smith   PetscFunctionBegin;
246a8aae444SDave May   *flg = PETSC_FALSE;
247bc196f7cSDave May   ierr = PetscTryMethod(viewer,"PetscViewerBinaryGetUseMPIIO_C",(PetscViewer,PetscBool*),(viewer,flg));CHKERRQ(ierr);
2485c6c1daeSBarry Smith   PetscFunctionReturn(0);
2495c6c1daeSBarry Smith }
2505c6c1daeSBarry Smith 
25181f0254dSBarry Smith static PetscErrorCode  PetscViewerBinaryGetFlowControl_Binary(PetscViewer viewer,PetscInt *fc)
2525c6c1daeSBarry Smith {
2535c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
2545c6c1daeSBarry Smith 
2555c6c1daeSBarry Smith   PetscFunctionBegin;
2565c6c1daeSBarry Smith   *fc = vbinary->flowcontrol;
2575c6c1daeSBarry Smith   PetscFunctionReturn(0);
2585c6c1daeSBarry Smith }
2595c6c1daeSBarry Smith 
2605c6c1daeSBarry Smith /*@C
2615c6c1daeSBarry Smith     PetscViewerBinaryGetFlowControl - Returns how many messages are allowed to outstanding at the same time during parallel IO reads/writes
2625c6c1daeSBarry Smith 
2635c6c1daeSBarry Smith     Not Collective
2645c6c1daeSBarry Smith 
2655c6c1daeSBarry Smith     Input Parameter:
2665c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
2675c6c1daeSBarry Smith 
2685c6c1daeSBarry Smith     Output Parameter:
2695c6c1daeSBarry Smith .   fc - the number of messages
2705c6c1daeSBarry Smith 
2715c6c1daeSBarry Smith     Level: advanced
2725c6c1daeSBarry Smith 
2735c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer(), PetscViewerBinarySetFlowControl()
2745c6c1daeSBarry Smith 
2755c6c1daeSBarry Smith @*/
2765c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetFlowControl(PetscViewer viewer,PetscInt *fc)
2775c6c1daeSBarry Smith {
2785c6c1daeSBarry Smith   PetscErrorCode ierr;
2795c6c1daeSBarry Smith 
2805c6c1daeSBarry Smith   PetscFunctionBegin;
281163d334eSBarry Smith   ierr = PetscUseMethod(viewer,"PetscViewerBinaryGetFlowControl_C",(PetscViewer,PetscInt*),(viewer,fc));CHKERRQ(ierr);
2825c6c1daeSBarry Smith   PetscFunctionReturn(0);
2835c6c1daeSBarry Smith }
2845c6c1daeSBarry Smith 
28581f0254dSBarry Smith static PetscErrorCode PetscViewerBinarySetFlowControl_Binary(PetscViewer viewer,PetscInt fc)
2865c6c1daeSBarry Smith {
2875c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
2885c6c1daeSBarry Smith 
2895c6c1daeSBarry Smith   PetscFunctionBegin;
290ce94432eSBarry Smith   if (fc <= 1) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_OUTOFRANGE,"Flow control count must be greater than 1, %D was set",fc);
2915c6c1daeSBarry Smith   vbinary->flowcontrol = fc;
2925c6c1daeSBarry Smith   PetscFunctionReturn(0);
2935c6c1daeSBarry Smith }
2945c6c1daeSBarry Smith 
2955c6c1daeSBarry Smith /*@C
296639ff905SBarry Smith     PetscViewerBinarySetFlowControl - Sets how many messages are allowed to outstanding at the same time during parallel IO reads/writes
2975c6c1daeSBarry Smith 
2985c6c1daeSBarry Smith     Not Collective
2995c6c1daeSBarry Smith 
3005c6c1daeSBarry Smith     Input Parameter:
3015c6c1daeSBarry Smith +   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
302639ff905SBarry Smith -   fc - the number of messages, defaults to 256 if this function was not called
3035c6c1daeSBarry Smith 
3045c6c1daeSBarry Smith     Level: advanced
3055c6c1daeSBarry Smith 
3065c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer(), PetscViewerBinaryGetFlowControl()
3075c6c1daeSBarry Smith 
3085c6c1daeSBarry Smith @*/
3095c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinarySetFlowControl(PetscViewer viewer,PetscInt fc)
3105c6c1daeSBarry Smith {
3115c6c1daeSBarry Smith   PetscErrorCode ierr;
3125c6c1daeSBarry Smith 
3135c6c1daeSBarry Smith   PetscFunctionBegin;
3145c6c1daeSBarry Smith   ierr = PetscUseMethod(viewer,"PetscViewerBinarySetFlowControl_C",(PetscViewer,PetscInt),(viewer,fc));CHKERRQ(ierr);
3155c6c1daeSBarry Smith   PetscFunctionReturn(0);
3165c6c1daeSBarry Smith }
3175c6c1daeSBarry Smith 
3185c6c1daeSBarry Smith /*@C
3195c6c1daeSBarry Smith     PetscViewerBinaryGetDescriptor - Extracts the file descriptor from a PetscViewer.
3205c6c1daeSBarry Smith 
3215872f025SBarry Smith     Collective On PetscViewer
3225c6c1daeSBarry Smith 
3235c6c1daeSBarry Smith     Input Parameter:
3245c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
3255c6c1daeSBarry Smith 
3265c6c1daeSBarry Smith     Output Parameter:
3275c6c1daeSBarry Smith .   fdes - file descriptor
3285c6c1daeSBarry Smith 
3295c6c1daeSBarry Smith     Level: advanced
3305c6c1daeSBarry Smith 
3315c6c1daeSBarry Smith     Notes:
3325c6c1daeSBarry Smith       For writable binary PetscViewers, the descriptor will only be valid for the
3335c6c1daeSBarry Smith     first processor in the communicator that shares the PetscViewer. For readable
3345c6c1daeSBarry Smith     files it will only be valid on nodes that have the file. If node 0 does not
3355c6c1daeSBarry Smith     have the file it generates an error even if another node does have the file.
3365c6c1daeSBarry Smith 
3375c6c1daeSBarry Smith     Fortran Note:
3385c6c1daeSBarry Smith     This routine is not supported in Fortran.
3395c6c1daeSBarry Smith 
34095452b02SPatrick Sanan     Developer Notes:
34195452b02SPatrick Sanan     This must be called on all processes because Dave May changed
3425872f025SBarry Smith     the source code that this may be trigger a PetscViewerSetUp() call if it was not previously triggered.
3435872f025SBarry Smith 
3445872f025SBarry Smith 
3455c6c1daeSBarry Smith 
3465c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer()
3475c6c1daeSBarry Smith @*/
3485c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetDescriptor(PetscViewer viewer,int *fdes)
3495c6c1daeSBarry Smith {
35003a1d59fSDave May   PetscErrorCode     ierr;
3515c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
3525c6c1daeSBarry Smith 
3535c6c1daeSBarry Smith   PetscFunctionBegin;
354c98fd787SBarry Smith   ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr);
3555c6c1daeSBarry Smith   *fdes = vbinary->fdes;
3565c6c1daeSBarry Smith   PetscFunctionReturn(0);
3575c6c1daeSBarry Smith }
3585c6c1daeSBarry Smith 
3595c6c1daeSBarry Smith /*@
3605c6c1daeSBarry Smith     PetscViewerBinarySkipInfo - Binary file will not have .info file created with it
3615c6c1daeSBarry Smith 
3625c6c1daeSBarry Smith     Not Collective
3635c6c1daeSBarry Smith 
364fd292e60Sprj-     Input Parameter:
3655c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerCreate()
3665c6c1daeSBarry Smith 
3675c6c1daeSBarry Smith     Options Database Key:
3685c6c1daeSBarry Smith .   -viewer_binary_skip_info
3695c6c1daeSBarry Smith 
3705c6c1daeSBarry Smith     Level: advanced
3715c6c1daeSBarry Smith 
37295452b02SPatrick Sanan     Notes:
37395452b02SPatrick Sanan     This must be called after PetscViewerSetType(). If you use PetscViewerBinaryOpen() then
3745c6c1daeSBarry Smith     you can only skip the info file with the -viewer_binary_skip_info flag. To use the function you must open the
375a2d7db39SDave May     viewer with PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinarySkipInfo().
3765c6c1daeSBarry Smith 
3775c6c1daeSBarry Smith     The .info contains meta information about the data in the binary file, for example the block size if it was
3785c6c1daeSBarry Smith     set for a vector or matrix.
3795c6c1daeSBarry Smith 
3805c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySetSkipOptions(),
381807ea322SDave May           PetscViewerBinaryGetSkipOptions(), PetscViewerBinaryGetSkipInfo()
3825c6c1daeSBarry Smith @*/
3835c6c1daeSBarry Smith PetscErrorCode PetscViewerBinarySkipInfo(PetscViewer viewer)
3845c6c1daeSBarry Smith {
3855c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
3865c6c1daeSBarry Smith 
3875c6c1daeSBarry Smith   PetscFunctionBegin;
3885c6c1daeSBarry Smith   vbinary->skipinfo = PETSC_TRUE;
3895c6c1daeSBarry Smith   PetscFunctionReturn(0);
3905c6c1daeSBarry Smith }
3915c6c1daeSBarry Smith 
39281f0254dSBarry Smith static PetscErrorCode PetscViewerBinarySetSkipInfo_Binary(PetscViewer viewer,PetscBool skip)
393807ea322SDave May {
394807ea322SDave May   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
395807ea322SDave May 
396807ea322SDave May   PetscFunctionBegin;
397807ea322SDave May   vbinary->skipinfo = skip;
398807ea322SDave May   PetscFunctionReturn(0);
399807ea322SDave May }
400807ea322SDave May 
401807ea322SDave May /*@
402807ea322SDave May     PetscViewerBinarySetSkipInfo - Binary file will not have .info file created with it
403807ea322SDave May 
404807ea322SDave May     Not Collective
405807ea322SDave May 
406fd292e60Sprj-     Input Parameter:
407807ea322SDave May .   viewer - PetscViewer context, obtained from PetscViewerCreate()
408807ea322SDave May 
409807ea322SDave May     Options Database Key:
410807ea322SDave May .   -viewer_binary_skip_info
411807ea322SDave May 
412807ea322SDave May     Level: advanced
413807ea322SDave May 
414807ea322SDave May .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySetSkipOptions(),
415807ea322SDave May           PetscViewerBinaryGetSkipOptions(), PetscViewerBinaryGetSkipInfo()
416807ea322SDave May @*/
417807ea322SDave May PetscErrorCode PetscViewerBinarySetSkipInfo(PetscViewer viewer,PetscBool skip)
418807ea322SDave May {
419807ea322SDave May   PetscErrorCode ierr;
420807ea322SDave May 
421807ea322SDave May   PetscFunctionBegin;
422807ea322SDave May   ierr = PetscUseMethod(viewer,"PetscViewerBinarySetSkipInfo_C",(PetscViewer,PetscBool),(viewer,skip));CHKERRQ(ierr);
423807ea322SDave May   PetscFunctionReturn(0);
424807ea322SDave May }
425807ea322SDave May 
42681f0254dSBarry Smith static PetscErrorCode PetscViewerBinaryGetSkipInfo_Binary(PetscViewer viewer,PetscBool *skip)
427807ea322SDave May {
428807ea322SDave May   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
429807ea322SDave May 
430807ea322SDave May   PetscFunctionBegin;
431807ea322SDave May   *skip  = vbinary->skipinfo;
432807ea322SDave May   PetscFunctionReturn(0);
433807ea322SDave May }
434807ea322SDave May 
435807ea322SDave May /*@
436807ea322SDave May     PetscViewerBinaryGetSkipInfo - check if viewer wrote a .info file
437807ea322SDave May 
438807ea322SDave May     Not Collective
439807ea322SDave May 
440807ea322SDave May     Input Parameter:
441807ea322SDave May .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
442807ea322SDave May 
443807ea322SDave May     Output Parameter:
444807ea322SDave May .   skip - PETSC_TRUE implies the .info file was not generated
445807ea322SDave May 
446807ea322SDave May     Level: advanced
447807ea322SDave May 
44895452b02SPatrick Sanan     Notes:
44995452b02SPatrick Sanan     This must be called after PetscViewerSetType()
450807ea322SDave May 
451807ea322SDave May .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(),
452807ea322SDave May           PetscViewerBinarySetSkipOptions(), PetscViewerBinarySetSkipInfo()
453807ea322SDave May @*/
454807ea322SDave May PetscErrorCode PetscViewerBinaryGetSkipInfo(PetscViewer viewer,PetscBool *skip)
455807ea322SDave May {
456807ea322SDave May   PetscErrorCode ierr;
457807ea322SDave May 
458807ea322SDave May   PetscFunctionBegin;
459807ea322SDave May   ierr = PetscUseMethod(viewer,"PetscViewerBinaryGetSkipInfo_C",(PetscViewer,PetscBool*),(viewer,skip));CHKERRQ(ierr);
460807ea322SDave May   PetscFunctionReturn(0);
461807ea322SDave May }
462807ea322SDave May 
46381f0254dSBarry Smith static PetscErrorCode PetscViewerBinarySetSkipOptions_Binary(PetscViewer viewer,PetscBool skip)
464807ea322SDave May {
465807ea322SDave May   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
466807ea322SDave May 
467807ea322SDave May   PetscFunctionBegin;
468807ea322SDave May   vbinary->skipoptions = skip;
469807ea322SDave May   PetscFunctionReturn(0);
470807ea322SDave May }
471807ea322SDave May 
4725c6c1daeSBarry Smith /*@
4735c6c1daeSBarry Smith     PetscViewerBinarySetSkipOptions - do not use the PETSc options database when loading objects
4745c6c1daeSBarry Smith 
4755c6c1daeSBarry Smith     Not Collective
4765c6c1daeSBarry Smith 
4775c6c1daeSBarry Smith     Input Parameters:
4785c6c1daeSBarry Smith +   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
4795c6c1daeSBarry Smith -   skip - PETSC_TRUE means do not use
4805c6c1daeSBarry Smith 
4815c6c1daeSBarry Smith     Options Database Key:
4825c6c1daeSBarry Smith .   -viewer_binary_skip_options
4835c6c1daeSBarry Smith 
4845c6c1daeSBarry Smith     Level: advanced
4855c6c1daeSBarry Smith 
48695452b02SPatrick Sanan     Notes:
48795452b02SPatrick Sanan     This must be called after PetscViewerSetType()
4885c6c1daeSBarry Smith 
4895c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(),
4905c6c1daeSBarry Smith           PetscViewerBinaryGetSkipOptions()
4915c6c1daeSBarry Smith @*/
4925c6c1daeSBarry Smith PetscErrorCode PetscViewerBinarySetSkipOptions(PetscViewer viewer,PetscBool skip)
4935c6c1daeSBarry Smith {
494807ea322SDave May   PetscErrorCode ierr;
4955c6c1daeSBarry Smith 
4965c6c1daeSBarry Smith   PetscFunctionBegin;
497807ea322SDave May   ierr = PetscUseMethod(viewer,"PetscViewerBinarySetSkipOptions_C",(PetscViewer,PetscBool),(viewer,skip));CHKERRQ(ierr);
498807ea322SDave May   PetscFunctionReturn(0);
499807ea322SDave May }
500807ea322SDave May 
50181f0254dSBarry Smith static PetscErrorCode PetscViewerBinaryGetSkipOptions_Binary(PetscViewer viewer,PetscBool *skip)
502807ea322SDave May {
503807ea322SDave May   PetscViewer_Binary *vbinary;
504807ea322SDave May 
505807ea322SDave May   PetscFunctionBegin;
506807ea322SDave May   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
507807ea322SDave May   vbinary = (PetscViewer_Binary*)viewer->data;
508807ea322SDave May   *skip   = vbinary->skipoptions;
5095c6c1daeSBarry Smith   PetscFunctionReturn(0);
5105c6c1daeSBarry Smith }
5115c6c1daeSBarry Smith 
5125c6c1daeSBarry Smith /*@
5135c6c1daeSBarry Smith     PetscViewerBinaryGetSkipOptions - checks if viewer uses the PETSc options database when loading objects
5145c6c1daeSBarry Smith 
5155c6c1daeSBarry Smith     Not Collective
5165c6c1daeSBarry Smith 
5175c6c1daeSBarry Smith     Input Parameter:
5185c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
5195c6c1daeSBarry Smith 
5205c6c1daeSBarry Smith     Output Parameter:
5215c6c1daeSBarry Smith .   skip - PETSC_TRUE means do not use
5225c6c1daeSBarry Smith 
5235c6c1daeSBarry Smith     Level: advanced
5245c6c1daeSBarry Smith 
52595452b02SPatrick Sanan     Notes:
52695452b02SPatrick Sanan     This must be called after PetscViewerSetType()
5275c6c1daeSBarry Smith 
5285c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(),
5295c6c1daeSBarry Smith           PetscViewerBinarySetSkipOptions()
5305c6c1daeSBarry Smith @*/
5315c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetSkipOptions(PetscViewer viewer,PetscBool *skip)
5325c6c1daeSBarry Smith {
533807ea322SDave May   PetscErrorCode ierr;
5345c6c1daeSBarry Smith 
5355c6c1daeSBarry Smith   PetscFunctionBegin;
536807ea322SDave May   ierr = PetscUseMethod(viewer,"PetscViewerBinaryGetSkipOptions_C",(PetscViewer,PetscBool*),(viewer,skip));CHKERRQ(ierr);
5375c6c1daeSBarry Smith   PetscFunctionReturn(0);
5385c6c1daeSBarry Smith }
5395c6c1daeSBarry Smith 
54081f0254dSBarry Smith static PetscErrorCode PetscViewerBinarySetSkipHeader_Binary(PetscViewer viewer,PetscBool skip)
5415c6c1daeSBarry Smith {
5425c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
5435c6c1daeSBarry Smith 
5445c6c1daeSBarry Smith   PetscFunctionBegin;
5455c6c1daeSBarry Smith   vbinary->skipheader = skip;
5465c6c1daeSBarry Smith   PetscFunctionReturn(0);
5475c6c1daeSBarry Smith }
5485c6c1daeSBarry Smith 
5495c6c1daeSBarry Smith /*@
5505c6c1daeSBarry Smith     PetscViewerBinarySetSkipHeader - do not write a header with size information on output, just raw data
5515c6c1daeSBarry Smith 
5525c6c1daeSBarry Smith     Not Collective
5535c6c1daeSBarry Smith 
5545c6c1daeSBarry Smith     Input Parameters:
5555c6c1daeSBarry Smith +   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
5565c6c1daeSBarry Smith -   skip - PETSC_TRUE means do not write header
5575c6c1daeSBarry Smith 
5585c6c1daeSBarry Smith     Options Database Key:
5595c6c1daeSBarry Smith .   -viewer_binary_skip_header
5605c6c1daeSBarry Smith 
5615c6c1daeSBarry Smith     Level: advanced
5625c6c1daeSBarry Smith 
56395452b02SPatrick Sanan     Notes:
56495452b02SPatrick Sanan     This must be called after PetscViewerSetType()
5655c6c1daeSBarry Smith 
5665c6c1daeSBarry Smith            Can ONLY be called on a binary viewer
5675c6c1daeSBarry Smith 
5685c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(),
5695c6c1daeSBarry Smith           PetscViewerBinaryGetSkipHeader()
5705c6c1daeSBarry Smith @*/
5715c6c1daeSBarry Smith PetscErrorCode PetscViewerBinarySetSkipHeader(PetscViewer viewer,PetscBool skip)
5725c6c1daeSBarry Smith {
5735c6c1daeSBarry Smith   PetscErrorCode ierr;
5745c6c1daeSBarry Smith 
5755c6c1daeSBarry Smith   PetscFunctionBegin;
5765c6c1daeSBarry Smith   ierr = PetscUseMethod(viewer,"PetscViewerBinarySetSkipHeader_C",(PetscViewer,PetscBool),(viewer,skip));CHKERRQ(ierr);
5775c6c1daeSBarry Smith   PetscFunctionReturn(0);
5785c6c1daeSBarry Smith }
5795c6c1daeSBarry Smith 
58081f0254dSBarry Smith static PetscErrorCode PetscViewerBinaryGetSkipHeader_Binary(PetscViewer viewer,PetscBool  *skip)
5815c6c1daeSBarry Smith {
5825c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
5835c6c1daeSBarry Smith 
5845c6c1daeSBarry Smith   PetscFunctionBegin;
5855c6c1daeSBarry Smith   *skip = vbinary->skipheader;
5865c6c1daeSBarry Smith   PetscFunctionReturn(0);
5875c6c1daeSBarry Smith }
5885c6c1daeSBarry Smith 
5895c6c1daeSBarry Smith /*@
5905c6c1daeSBarry Smith     PetscViewerBinaryGetSkipHeader - checks whether to write a header with size information on output, or just raw data
5915c6c1daeSBarry Smith 
5925c6c1daeSBarry Smith     Not Collective
5935c6c1daeSBarry Smith 
5945c6c1daeSBarry Smith     Input Parameter:
5955c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
5965c6c1daeSBarry Smith 
5975c6c1daeSBarry Smith     Output Parameter:
5985c6c1daeSBarry Smith .   skip - PETSC_TRUE means do not write header
5995c6c1daeSBarry Smith 
6005c6c1daeSBarry Smith     Level: advanced
6015c6c1daeSBarry Smith 
60295452b02SPatrick Sanan     Notes:
60395452b02SPatrick Sanan     This must be called after PetscViewerSetType()
6045c6c1daeSBarry Smith 
6055c6c1daeSBarry Smith             Returns false for PETSCSOCKETVIEWER, you cannot skip the header for it.
6065c6c1daeSBarry Smith 
6075c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(),
6085c6c1daeSBarry Smith           PetscViewerBinarySetSkipHeader()
6095c6c1daeSBarry Smith @*/
6105c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetSkipHeader(PetscViewer viewer,PetscBool  *skip)
6115c6c1daeSBarry Smith {
6125c6c1daeSBarry Smith   PetscErrorCode ierr;
6135c6c1daeSBarry Smith 
6145c6c1daeSBarry Smith   PetscFunctionBegin;
6155c6c1daeSBarry Smith   *skip = PETSC_FALSE;
616163d334eSBarry Smith   ierr  = PetscUseMethod(viewer,"PetscViewerBinaryGetSkipHeader_C",(PetscViewer,PetscBool*),(viewer,skip));CHKERRQ(ierr);
6175c6c1daeSBarry Smith   PetscFunctionReturn(0);
6185c6c1daeSBarry Smith }
6195c6c1daeSBarry Smith 
62081f0254dSBarry Smith static PetscErrorCode PetscViewerBinaryGetInfoPointer_Binary(PetscViewer viewer,FILE **file)
6215c6c1daeSBarry Smith {
622f3b211e4SSatish Balay   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
623a261c58fSBarry Smith   PetscErrorCode     ierr;
624a261c58fSBarry Smith   MPI_Comm           comm;
6255c6c1daeSBarry Smith 
6265c6c1daeSBarry Smith   PetscFunctionBegin;
627c98fd787SBarry Smith   ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr);
6285c6c1daeSBarry Smith   *file = vbinary->fdes_info;
629a261c58fSBarry Smith   if (viewer->format == PETSC_VIEWER_BINARY_MATLAB && !vbinary->matlabheaderwritten) {
630a261c58fSBarry Smith     vbinary->matlabheaderwritten = PETSC_TRUE;
631a261c58fSBarry Smith     ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
632da88d4d4SJed Brown     ierr = PetscFPrintf(comm,*file,"#--- begin code written by PetscViewerBinary for MATLAB format ---#\n");CHKERRQ(ierr);
633da88d4d4SJed Brown     ierr = PetscFPrintf(comm,*file,"#$$ Set.filename = '%s';\n",vbinary->filename);CHKERRQ(ierr);
634da88d4d4SJed Brown     ierr = PetscFPrintf(comm,*file,"#$$ fd = PetscOpenFile(Set.filename);\n");CHKERRQ(ierr);
635da88d4d4SJed Brown     ierr = PetscFPrintf(comm,*file,"#--- end code written by PetscViewerBinary for MATLAB format ---#\n\n");CHKERRQ(ierr);
636a261c58fSBarry Smith   }
6375c6c1daeSBarry Smith   PetscFunctionReturn(0);
6385c6c1daeSBarry Smith }
6395c6c1daeSBarry Smith 
6405c6c1daeSBarry Smith /*@C
6415c6c1daeSBarry Smith     PetscViewerBinaryGetInfoPointer - Extracts the file pointer for the ASCII
6425c6c1daeSBarry Smith           info file associated with a binary file.
6435c6c1daeSBarry Smith 
6445c6c1daeSBarry Smith     Not Collective
6455c6c1daeSBarry Smith 
6465c6c1daeSBarry Smith     Input Parameter:
6475c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
6485c6c1daeSBarry Smith 
6495c6c1daeSBarry Smith     Output Parameter:
6500298fd71SBarry Smith .   file - file pointer  Always returns NULL if not a binary viewer
6515c6c1daeSBarry Smith 
6525c6c1daeSBarry Smith     Level: advanced
6535c6c1daeSBarry Smith 
6545c6c1daeSBarry Smith     Notes:
6555c6c1daeSBarry Smith       For writable binary PetscViewers, the descriptor will only be valid for the
6565c6c1daeSBarry Smith     first processor in the communicator that shares the PetscViewer.
6575c6c1daeSBarry Smith 
6585c6c1daeSBarry Smith     Fortran Note:
6595c6c1daeSBarry Smith     This routine is not supported in Fortran.
6605c6c1daeSBarry Smith 
6615c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetDescriptor()
6625c6c1daeSBarry Smith @*/
6635c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetInfoPointer(PetscViewer viewer,FILE **file)
6645c6c1daeSBarry Smith {
6655c6c1daeSBarry Smith   PetscErrorCode ierr;
6665c6c1daeSBarry Smith 
6675c6c1daeSBarry Smith   PetscFunctionBegin;
6680298fd71SBarry Smith   *file = NULL;
6695c6c1daeSBarry Smith   ierr  = PetscTryMethod(viewer,"PetscViewerBinaryGetInfoPointer_C",(PetscViewer,FILE **),(viewer,file));CHKERRQ(ierr);
6705c6c1daeSBarry Smith   PetscFunctionReturn(0);
6715c6c1daeSBarry Smith }
6725c6c1daeSBarry Smith 
6735c6c1daeSBarry Smith static PetscErrorCode PetscViewerFileClose_Binary(PetscViewer v)
6745c6c1daeSBarry Smith {
6755c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data;
6765c6c1daeSBarry Smith   PetscErrorCode     ierr;
6775c6c1daeSBarry Smith   PetscMPIInt        rank;
6785c6c1daeSBarry Smith   int                err;
6795c6c1daeSBarry Smith 
6805c6c1daeSBarry Smith   PetscFunctionBegin;
681ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)v),&rank);CHKERRQ(ierr);
6825c6c1daeSBarry Smith   if ((!rank || vbinary->btype == FILE_MODE_READ) && vbinary->fdes) {
6835c6c1daeSBarry Smith     close(vbinary->fdes);
6845c6c1daeSBarry Smith     if (!rank && vbinary->storecompressed) {
6855c6c1daeSBarry Smith       char par[PETSC_MAX_PATH_LEN],buf[PETSC_MAX_PATH_LEN];
6865c6c1daeSBarry Smith       FILE *fp;
6875c6c1daeSBarry Smith       /* compress the file */
688a126751eSBarry Smith       ierr = PetscStrncpy(par,"gzip -f ",sizeof(par));CHKERRQ(ierr);
689a126751eSBarry Smith       ierr = PetscStrlcat(par,vbinary->ogzfilename ? vbinary->ogzfilename : vbinary->filename,sizeof(par));CHKERRQ(ierr);
690f90597f1SStefano Zampini       ierr = PetscFree(vbinary->ogzfilename);CHKERRQ(ierr);
6915c6c1daeSBarry Smith #if defined(PETSC_HAVE_POPEN)
6920298fd71SBarry Smith       ierr = PetscPOpen(PETSC_COMM_SELF,NULL,par,"r",&fp);CHKERRQ(ierr);
6935c6c1daeSBarry Smith       if (fgets(buf,1024,fp)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error from command %s\n%s",par,buf);
694016831caSBarry Smith       ierr = PetscPClose(PETSC_COMM_SELF,fp);CHKERRQ(ierr);
6955c6c1daeSBarry Smith #else
6965c6c1daeSBarry Smith       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
6975c6c1daeSBarry Smith #endif
6985c6c1daeSBarry Smith     }
6995c6c1daeSBarry Smith   }
7005c6c1daeSBarry Smith   if (vbinary->fdes_info) {
7015c6c1daeSBarry Smith     err = fclose(vbinary->fdes_info);
7025c6c1daeSBarry Smith     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
7035c6c1daeSBarry Smith   }
7045c6c1daeSBarry Smith   PetscFunctionReturn(0);
7055c6c1daeSBarry Smith }
7065c6c1daeSBarry Smith 
707e0385b85SDave May #if defined(PETSC_HAVE_MPIIO)
708e0385b85SDave May static PetscErrorCode PetscViewerFileClose_BinaryMPIIO(PetscViewer v)
709e0385b85SDave May {
710e0385b85SDave May   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data;
711e0385b85SDave May   int                err;
712e0385b85SDave May   PetscErrorCode     ierr;
713e0385b85SDave May 
714e0385b85SDave May   PetscFunctionBegin;
715e8a65b7dSLisandro Dalcin   if (vbinary->mfdes != MPI_FILE_NULL) {
716e0385b85SDave May     ierr = MPI_File_close(&vbinary->mfdes);CHKERRQ(ierr);
717e0385b85SDave May   }
718e8a65b7dSLisandro Dalcin   if (vbinary->mfsub != MPI_FILE_NULL) {
719e8a65b7dSLisandro Dalcin     ierr = MPI_File_close(&vbinary->mfsub);CHKERRQ(ierr);
720e8a65b7dSLisandro Dalcin   }
721e0385b85SDave May   if (vbinary->fdes_info) {
722e0385b85SDave May     err = fclose(vbinary->fdes_info);
723e0385b85SDave May     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
724e0385b85SDave May   }
725e0385b85SDave May   PetscFunctionReturn(0);
726e0385b85SDave May }
727e0385b85SDave May #endif
728e0385b85SDave May 
72981f0254dSBarry Smith static PetscErrorCode PetscViewerDestroy_Binary(PetscViewer v)
7305c6c1daeSBarry Smith {
7315c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data;
7325c6c1daeSBarry Smith   PetscErrorCode     ierr;
7335c6c1daeSBarry Smith 
7345c6c1daeSBarry Smith   PetscFunctionBegin;
735a261c58fSBarry Smith   if (v->format == PETSC_VIEWER_BINARY_MATLAB) {
736a261c58fSBarry Smith     MPI_Comm comm;
737a261c58fSBarry Smith     FILE     *info;
738a261c58fSBarry Smith 
739a261c58fSBarry Smith     ierr = PetscObjectGetComm((PetscObject)v,&comm);CHKERRQ(ierr);
740a261c58fSBarry Smith     ierr = PetscViewerBinaryGetInfoPointer(v,&info);CHKERRQ(ierr);
741da88d4d4SJed Brown     ierr = PetscFPrintf(comm,info,"#--- begin code written by PetscViewerBinary for MATLAB format ---#\n");CHKERRQ(ierr);
742da88d4d4SJed Brown     ierr = PetscFPrintf(comm,info,"#$$ close(fd);\n");CHKERRQ(ierr);
743da88d4d4SJed Brown     ierr = PetscFPrintf(comm,info,"#--- end code written by PetscViewerBinary for MATLAB format ---#\n\n");CHKERRQ(ierr);
744a261c58fSBarry Smith   }
7455c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
746bc196f7cSDave May   if (vbinary->usempiio) {
747e0385b85SDave May     ierr = PetscViewerFileClose_BinaryMPIIO(v);CHKERRQ(ierr);
748e0385b85SDave May   } else {
749e0385b85SDave May #endif
750e0385b85SDave May     ierr = PetscViewerFileClose_Binary(v);CHKERRQ(ierr);
751e0385b85SDave May #if defined(PETSC_HAVE_MPIIO)
7525c6c1daeSBarry Smith   }
7535c6c1daeSBarry Smith #endif
754f90597f1SStefano Zampini   ierr = PetscFree(vbinary->filename);CHKERRQ(ierr);
755f90597f1SStefano Zampini   ierr = PetscFree(vbinary->ogzfilename);CHKERRQ(ierr);
756e0385b85SDave May   ierr = PetscFree(vbinary);CHKERRQ(ierr);
757e0385b85SDave May   PetscFunctionReturn(0);
758e0385b85SDave May }
7595c6c1daeSBarry Smith 
7605c6c1daeSBarry Smith /*@C
7615c6c1daeSBarry Smith    PetscViewerBinaryOpen - Opens a file for binary input/output.
7625c6c1daeSBarry Smith 
763d083f849SBarry Smith    Collective
7645c6c1daeSBarry Smith 
7655c6c1daeSBarry Smith    Input Parameters:
7665c6c1daeSBarry Smith +  comm - MPI communicator
7675c6c1daeSBarry Smith .  name - name of file
7685c6c1daeSBarry Smith -  type - type of file
7695c6c1daeSBarry Smith $    FILE_MODE_WRITE - create new file for binary output
7705c6c1daeSBarry Smith $    FILE_MODE_READ - open existing file for binary input
7715c6c1daeSBarry Smith $    FILE_MODE_APPEND - open existing file for binary output
7725c6c1daeSBarry Smith 
7735c6c1daeSBarry Smith    Output Parameter:
7745c6c1daeSBarry Smith .  binv - PetscViewer for binary input/output to use with the specified file
7755c6c1daeSBarry Smith 
7765c6c1daeSBarry Smith     Options Database Keys:
77763c55180SPatrick Sanan +    -viewer_binary_filename <name> -
77863c55180SPatrick Sanan .    -viewer_binary_skip_info -
77963c55180SPatrick Sanan .    -viewer_binary_skip_options -
78063c55180SPatrick Sanan .    -viewer_binary_skip_header -
78163c55180SPatrick Sanan -    -viewer_binary_mpiio -
7825c6c1daeSBarry Smith 
7835c6c1daeSBarry Smith    Level: beginner
7845c6c1daeSBarry Smith 
7855c6c1daeSBarry Smith    Note:
7865c6c1daeSBarry Smith    This PetscViewer should be destroyed with PetscViewerDestroy().
7875c6c1daeSBarry Smith 
7885c6c1daeSBarry Smith     For reading files, the filename may begin with ftp:// or http:// and/or
7895c6c1daeSBarry Smith     end with .gz; in this case file is brought over and uncompressed.
7905c6c1daeSBarry Smith 
7915c6c1daeSBarry Smith     For creating files, if the file name ends with .gz it is automatically
7925c6c1daeSBarry Smith     compressed when closed.
7935c6c1daeSBarry Smith 
7945c6c1daeSBarry Smith     For writing files it only opens the file on processor 0 in the communicator.
7955c6c1daeSBarry Smith     For readable files it opens the file on all nodes that have the file. If
7965c6c1daeSBarry Smith     node 0 does not have the file it generates an error even if other nodes
7975c6c1daeSBarry Smith     do have the file.
7985c6c1daeSBarry Smith 
7996a9046bcSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(),
8005c6c1daeSBarry Smith           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
80167918a83SBarry Smith           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscViewerBinaryRead(), PetscViewerBinarySetUseMPIIO(),
80267918a83SBarry Smith           PetscViewerBinaryGetUseMPIIO(), PetscViewerBinaryGetMPIIOOffset()
8035c6c1daeSBarry Smith @*/
8045c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryOpen(MPI_Comm comm,const char name[],PetscFileMode type,PetscViewer *binv)
8055c6c1daeSBarry Smith {
8065c6c1daeSBarry Smith   PetscErrorCode ierr;
8075c6c1daeSBarry Smith 
8085c6c1daeSBarry Smith   PetscFunctionBegin;
8095c6c1daeSBarry Smith   ierr = PetscViewerCreate(comm,binv);CHKERRQ(ierr);
8105c6c1daeSBarry Smith   ierr = PetscViewerSetType(*binv,PETSCVIEWERBINARY);CHKERRQ(ierr);
8115c6c1daeSBarry Smith   ierr = PetscViewerFileSetMode(*binv,type);CHKERRQ(ierr);
8125c6c1daeSBarry Smith   ierr = PetscViewerFileSetName(*binv,name);CHKERRQ(ierr);
81303a1d59fSDave May   ierr = PetscViewerSetFromOptions(*binv);CHKERRQ(ierr);
8145c6c1daeSBarry Smith   PetscFunctionReturn(0);
8155c6c1daeSBarry Smith }
8165c6c1daeSBarry Smith 
8175c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
818060da220SMatthew G. Knepley static PetscErrorCode PetscViewerBinaryWriteReadMPIIO(PetscViewer viewer,void *data,PetscInt num,PetscInt *count,PetscDataType dtype,PetscBool write)
8195c6c1daeSBarry Smith {
8205c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
8215c6c1daeSBarry Smith   PetscErrorCode     ierr;
822e8a65b7dSLisandro Dalcin   MPI_File           mfdes;
8235c6c1daeSBarry Smith   MPI_Datatype       mdtype;
824*69e10bbaSLisandro Dalcin   PetscMPIInt        rank,cnt;
8255c6c1daeSBarry Smith   MPI_Status         status;
8265c6c1daeSBarry Smith   MPI_Aint           ul,dsize;
8275c6c1daeSBarry Smith 
8285c6c1daeSBarry Smith   PetscFunctionBegin;
829e8a65b7dSLisandro Dalcin   mfdes = vbinary->mfdes;
830060da220SMatthew G. Knepley   ierr = PetscMPIIntCast(num,&cnt);CHKERRQ(ierr);
8315c6c1daeSBarry Smith   ierr = PetscDataTypeToMPIDataType(dtype,&mdtype);CHKERRQ(ierr);
832*69e10bbaSLisandro Dalcin   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
8335c6c1daeSBarry Smith   if (write) {
834*69e10bbaSLisandro Dalcin     if (!rank) {
835*69e10bbaSLisandro Dalcin       ierr = MPIU_File_write_at(mfdes,vbinary->moff,data,cnt,mdtype,&status);CHKERRQ(ierr);
836*69e10bbaSLisandro Dalcin     }
8375c6c1daeSBarry Smith   } else {
838*69e10bbaSLisandro Dalcin     if (!rank) {
839*69e10bbaSLisandro Dalcin       ierr = MPIU_File_read_at(mfdes,vbinary->moff,data,cnt,mdtype,&status);CHKERRQ(ierr);
8409860990eSLisandro Dalcin       if (cnt > 0) {ierr = MPI_Get_count(&status,mdtype,&cnt);CHKERRQ(ierr);}
8415c6c1daeSBarry Smith     }
842*69e10bbaSLisandro Dalcin     ierr = MPI_Bcast(&cnt,1,MPI_INT,0,PetscObjectComm((PetscObject)viewer));CHKERRQ(ierr);
843*69e10bbaSLisandro Dalcin     ierr = MPI_Bcast(data,cnt,mdtype,0,PetscObjectComm((PetscObject)viewer));CHKERRQ(ierr);
844*69e10bbaSLisandro Dalcin   }
8455c6c1daeSBarry Smith   ierr = MPI_Type_get_extent(mdtype,&ul,&dsize);CHKERRQ(ierr);
846a297a907SKarl Rupp 
8475c6c1daeSBarry Smith   vbinary->moff += dsize*cnt;
8489860990eSLisandro Dalcin   if (count) *count = cnt;
8495c6c1daeSBarry Smith   PetscFunctionReturn(0);
8505c6c1daeSBarry Smith }
8515c6c1daeSBarry Smith #endif
8525c6c1daeSBarry Smith 
8535c6c1daeSBarry Smith /*@C
8545c6c1daeSBarry Smith    PetscViewerBinaryRead - Reads from a binary file, all processors get the same result
8555c6c1daeSBarry Smith 
856d083f849SBarry Smith    Collective
8575c6c1daeSBarry Smith 
8585c6c1daeSBarry Smith    Input Parameters:
8595c6c1daeSBarry Smith +  viewer - the binary viewer
860907376e6SBarry Smith .  data - location of the data to be written
861060da220SMatthew G. Knepley .  num - number of items of data to read
862907376e6SBarry Smith -  dtype - type of data to read
8635c6c1daeSBarry Smith 
864f8e4bde8SMatthew G. Knepley    Output Parameters:
8650780a867SBarry Smith .  count - number of items of data actually read, or NULL. Unless an error is generated this is always set to the input parameter num.
866f8e4bde8SMatthew G. Knepley 
8675c6c1daeSBarry Smith    Level: beginner
8685c6c1daeSBarry Smith 
8690780a867SBarry Smith    Developer Note: Since count is always set to num it is not clear what purpose the output argument count serves.
8700780a867SBarry Smith 
8716a9046bcSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(),
8725c6c1daeSBarry Smith           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
8735c6c1daeSBarry Smith           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead()
8745c6c1daeSBarry Smith @*/
875060da220SMatthew G. Knepley PetscErrorCode PetscViewerBinaryRead(PetscViewer viewer,void *data,PetscInt num,PetscInt *count,PetscDataType dtype)
8765c6c1daeSBarry Smith {
8775c6c1daeSBarry Smith   PetscErrorCode     ierr;
8785c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
8795c6c1daeSBarry Smith 
880c98fd787SBarry Smith   ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr);
8815c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
882bc196f7cSDave May   if (vbinary->usempiio) {
883060da220SMatthew G. Knepley     ierr = PetscViewerBinaryWriteReadMPIIO(viewer,data,num,count,dtype,PETSC_FALSE);CHKERRQ(ierr);
8845c6c1daeSBarry Smith   } else {
8855c6c1daeSBarry Smith #endif
8869860990eSLisandro Dalcin     ierr = PetscBinarySynchronizedRead(PetscObjectComm((PetscObject)viewer),vbinary->fdes,data,num,count,dtype);CHKERRQ(ierr);
8875c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
8885c6c1daeSBarry Smith   }
8895c6c1daeSBarry Smith #endif
8905c6c1daeSBarry Smith   PetscFunctionReturn(0);
8915c6c1daeSBarry Smith }
8925c6c1daeSBarry Smith 
8935c6c1daeSBarry Smith /*@C
8945c6c1daeSBarry Smith    PetscViewerBinaryWrite - writes to a binary file, only from the first process
8955c6c1daeSBarry Smith 
896d083f849SBarry Smith    Collective
8975c6c1daeSBarry Smith 
8985c6c1daeSBarry Smith    Input Parameters:
8995c6c1daeSBarry Smith +  viewer - the binary viewer
9005c6c1daeSBarry Smith .  data - location of data
9015824c9d0SPatrick Sanan .  count - number of items of data to write
9025824c9d0SPatrick Sanan .  dtype - type of data to write
9035c6c1daeSBarry Smith -  istemp - data may be overwritten
9045c6c1daeSBarry Smith 
9055c6c1daeSBarry Smith    Level: beginner
9065c6c1daeSBarry Smith 
90795452b02SPatrick Sanan    Notes:
90895452b02SPatrick Sanan     because byte-swapping may be done on the values in data it cannot be declared const
9095c6c1daeSBarry Smith 
9106a9046bcSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(),
9115c6c1daeSBarry Smith           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(), PetscDataType
9125c6c1daeSBarry Smith           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead()
9135c6c1daeSBarry Smith @*/
9145c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryWrite(PetscViewer viewer,void *data,PetscInt count,PetscDataType dtype,PetscBool istemp)
9155c6c1daeSBarry Smith {
9165c6c1daeSBarry Smith   PetscErrorCode     ierr;
9175c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
9185c6c1daeSBarry Smith 
9195c6c1daeSBarry Smith   PetscFunctionBegin;
920c98fd787SBarry Smith   ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr);
9215c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
922bc196f7cSDave May   if (vbinary->usempiio) {
923060da220SMatthew G. Knepley     ierr = PetscViewerBinaryWriteReadMPIIO(viewer,data,count,NULL,dtype,PETSC_TRUE);CHKERRQ(ierr);
9245c6c1daeSBarry Smith   } else {
9255c6c1daeSBarry Smith #endif
926ce94432eSBarry Smith     ierr = PetscBinarySynchronizedWrite(PetscObjectComm((PetscObject)viewer),vbinary->fdes,data,count,dtype,istemp);CHKERRQ(ierr);
9275c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
9285c6c1daeSBarry Smith   }
9295c6c1daeSBarry Smith #endif
9305c6c1daeSBarry Smith   PetscFunctionReturn(0);
9315c6c1daeSBarry Smith }
9325c6c1daeSBarry Smith 
9335c6c1daeSBarry Smith /*@C
9345c6c1daeSBarry Smith    PetscViewerBinaryWriteStringArray - writes to a binary file, only from the first process an array of strings
9355c6c1daeSBarry Smith 
936d083f849SBarry Smith    Collective
9375c6c1daeSBarry Smith 
9385c6c1daeSBarry Smith    Input Parameters:
9395c6c1daeSBarry Smith +  viewer - the binary viewer
9405c6c1daeSBarry Smith -  data - location of the array of strings
9415c6c1daeSBarry Smith 
9425c6c1daeSBarry Smith 
9435c6c1daeSBarry Smith    Level: intermediate
9445c6c1daeSBarry Smith 
94595452b02SPatrick Sanan     Notes:
94695452b02SPatrick Sanan     array of strings is null terminated
9475c6c1daeSBarry Smith 
9486a9046bcSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(),
9495c6c1daeSBarry Smith           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
9505c6c1daeSBarry Smith           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead()
9515c6c1daeSBarry Smith @*/
95278fbdcc8SBarry Smith PetscErrorCode PetscViewerBinaryWriteStringArray(PetscViewer viewer,const char * const *data)
9535c6c1daeSBarry Smith {
9545c6c1daeSBarry Smith   PetscErrorCode ierr;
9555c6c1daeSBarry Smith   PetscInt       i,n = 0,*sizes;
9565c6c1daeSBarry Smith 
957c98fd787SBarry Smith   ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr);
9585c6c1daeSBarry Smith   /* count number of strings */
9595c6c1daeSBarry Smith   while (data[n++]) ;
9605c6c1daeSBarry Smith   n--;
961854ce69bSBarry Smith   ierr     = PetscMalloc1(n+1,&sizes);CHKERRQ(ierr);
9625c6c1daeSBarry Smith   sizes[0] = n;
9635c6c1daeSBarry Smith   for (i=0; i<n; i++) {
9645c6c1daeSBarry Smith     size_t tmp;
9655c6c1daeSBarry Smith     ierr       = PetscStrlen(data[i],&tmp);CHKERRQ(ierr);
9665c6c1daeSBarry Smith     sizes[i+1] = tmp + 1;   /* size includes space for the null terminator */
9675c6c1daeSBarry Smith   }
9685c6c1daeSBarry Smith   ierr = PetscViewerBinaryWrite(viewer,sizes,n+1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
9695c6c1daeSBarry Smith   for (i=0; i<n; i++) {
97078fbdcc8SBarry Smith     ierr = PetscViewerBinaryWrite(viewer,(void*)data[i],sizes[i+1],PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
9715c6c1daeSBarry Smith   }
9725c6c1daeSBarry Smith   ierr = PetscFree(sizes);CHKERRQ(ierr);
9735c6c1daeSBarry Smith   PetscFunctionReturn(0);
9745c6c1daeSBarry Smith }
9755c6c1daeSBarry Smith 
9765c6c1daeSBarry Smith /*@C
9775c6c1daeSBarry Smith    PetscViewerBinaryReadStringArray - reads a binary file an array of strings
9785c6c1daeSBarry Smith 
979d083f849SBarry Smith    Collective
9805c6c1daeSBarry Smith 
9815c6c1daeSBarry Smith    Input Parameter:
9825c6c1daeSBarry Smith .  viewer - the binary viewer
9835c6c1daeSBarry Smith 
9845c6c1daeSBarry Smith    Output Parameter:
9855c6c1daeSBarry Smith .  data - location of the array of strings
9865c6c1daeSBarry Smith 
9875c6c1daeSBarry Smith    Level: intermediate
9885c6c1daeSBarry Smith 
98995452b02SPatrick Sanan     Notes:
99095452b02SPatrick Sanan     array of strings is null terminated
9915c6c1daeSBarry Smith 
9926a9046bcSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(),
9935c6c1daeSBarry Smith           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
9945c6c1daeSBarry Smith           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead()
9955c6c1daeSBarry Smith @*/
9965c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryReadStringArray(PetscViewer viewer,char ***data)
9975c6c1daeSBarry Smith {
9985c6c1daeSBarry Smith   PetscErrorCode ierr;
999060da220SMatthew G. Knepley   PetscInt       i,n,*sizes,N = 0;
10005c6c1daeSBarry Smith 
1001c98fd787SBarry Smith   ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr);
10025c6c1daeSBarry Smith   /* count number of strings */
1003060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&n,1,NULL,PETSC_INT);CHKERRQ(ierr);
1004785e854fSJed Brown   ierr = PetscMalloc1(n,&sizes);CHKERRQ(ierr);
1005060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,sizes,n,NULL,PETSC_INT);CHKERRQ(ierr);
1006a297a907SKarl Rupp   for (i=0; i<n; i++) N += sizes[i];
1007854ce69bSBarry Smith   ierr = PetscMalloc((n+1)*sizeof(char*) + N*sizeof(char),data);CHKERRQ(ierr);
10085c6c1daeSBarry Smith   (*data)[0] = (char*)((*data) + n + 1);
1009a297a907SKarl Rupp   for (i=1; i<n; i++) (*data)[i] = (*data)[i-1] + sizes[i-1];
1010060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,(*data)[0],N,NULL,PETSC_CHAR);CHKERRQ(ierr);
1011a297a907SKarl Rupp 
101202c9f0b5SLisandro Dalcin   (*data)[n] = NULL;
1013a297a907SKarl Rupp 
10145c6c1daeSBarry Smith   ierr = PetscFree(sizes);CHKERRQ(ierr);
10155c6c1daeSBarry Smith   PetscFunctionReturn(0);
10165c6c1daeSBarry Smith }
10175c6c1daeSBarry Smith 
101881f0254dSBarry Smith static PetscErrorCode PetscViewerFileGetName_Binary(PetscViewer viewer,const char **name)
10195c6c1daeSBarry Smith {
10205c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
10215c6c1daeSBarry Smith 
10225c6c1daeSBarry Smith   PetscFunctionBegin;
10235c6c1daeSBarry Smith   *name = vbinary->filename;
10245c6c1daeSBarry Smith   PetscFunctionReturn(0);
10255c6c1daeSBarry Smith }
10265c6c1daeSBarry Smith 
10275c6c1daeSBarry Smith /*@C
10285c6c1daeSBarry Smith      PetscViewerFileGetMode - Gets the type of file to be open
10295c6c1daeSBarry Smith 
10305c6c1daeSBarry Smith     Not Collective
10315c6c1daeSBarry Smith 
10325c6c1daeSBarry Smith   Input Parameter:
1033232ac771SBarry Smith .  viewer - the PetscViewer; must be a PETSCVIEWERBINARY, PETSCVIEWERMATLAB, PETSCVIEWERHDF5, or PETSCVIEWERASCII  PetscViewer
10345c6c1daeSBarry Smith 
10355c6c1daeSBarry Smith   Output Parameter:
10365c6c1daeSBarry Smith .  type - type of file
10375c6c1daeSBarry Smith $    FILE_MODE_WRITE - create new file for binary output
10385c6c1daeSBarry Smith $    FILE_MODE_READ - open existing file for binary input
10395c6c1daeSBarry Smith $    FILE_MODE_APPEND - open existing file for binary output
10405c6c1daeSBarry Smith 
10415c6c1daeSBarry Smith   Level: advanced
10425c6c1daeSBarry Smith 
10435c6c1daeSBarry Smith .seealso: PetscViewerFileSetMode(), PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinaryOpen()
10445c6c1daeSBarry Smith 
10455c6c1daeSBarry Smith @*/
10465c6c1daeSBarry Smith PetscErrorCode PetscViewerFileGetMode(PetscViewer viewer,PetscFileMode *type)
10475c6c1daeSBarry Smith {
10485c6c1daeSBarry Smith   PetscErrorCode ierr;
10495c6c1daeSBarry Smith 
10505c6c1daeSBarry Smith   PetscFunctionBegin;
10515c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
10525c6c1daeSBarry Smith   PetscValidPointer(type,2);
10535c6c1daeSBarry Smith   ierr = PetscUseMethod(viewer,"PetscViewerFileGetMode_C",(PetscViewer,PetscFileMode*),(viewer,type));CHKERRQ(ierr);
10545c6c1daeSBarry Smith   PetscFunctionReturn(0);
10555c6c1daeSBarry Smith }
10565c6c1daeSBarry Smith 
10575c6c1daeSBarry Smith /*@
1058bc196f7cSDave May     PetscViewerBinarySetUseMPIIO - Sets a binary viewer to use MPI-IO for reading/writing. Must be called
10595c6c1daeSBarry Smith         before PetscViewerFileSetName()
10605c6c1daeSBarry Smith 
10615c6c1daeSBarry Smith     Logically Collective on PetscViewer
10625c6c1daeSBarry Smith 
10635c6c1daeSBarry Smith     Input Parameters:
1064bc196f7cSDave May +   viewer - the PetscViewer; must be a binary
1065bc196f7cSDave May -   flg - PETSC_TRUE means MPI-IO will be used
10665c6c1daeSBarry Smith 
10675c6c1daeSBarry Smith     Options Database:
10685c6c1daeSBarry Smith     -viewer_binary_mpiio : Flag for using MPI-IO
10695c6c1daeSBarry Smith 
10705c6c1daeSBarry Smith     Level: advanced
10715c6c1daeSBarry Smith 
1072bc196f7cSDave May .seealso: PetscViewerFileSetMode(), PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinaryOpen(),
1073bc196f7cSDave May           PetscViewerBinaryGetUseMPIIO()
10745c6c1daeSBarry Smith 
10755c6c1daeSBarry Smith @*/
1076bc196f7cSDave May PetscErrorCode PetscViewerBinarySetUseMPIIO(PetscViewer viewer,PetscBool flg)
10775c6c1daeSBarry Smith {
10785c6c1daeSBarry Smith   PetscErrorCode ierr;
10795c6c1daeSBarry Smith 
10805c6c1daeSBarry Smith   PetscFunctionBegin;
10815c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
1082bc196f7cSDave May   ierr = PetscTryMethod(viewer,"PetscViewerBinarySetUseMPIIO_C",(PetscViewer,PetscBool),(viewer,flg));CHKERRQ(ierr);
10835c6c1daeSBarry Smith   PetscFunctionReturn(0);
10845c6c1daeSBarry Smith }
10855c6c1daeSBarry Smith 
10865c6c1daeSBarry Smith /*@C
10875c6c1daeSBarry Smith      PetscViewerFileSetMode - Sets the type of file to be open
10885c6c1daeSBarry Smith 
10895c6c1daeSBarry Smith     Logically Collective on PetscViewer
10905c6c1daeSBarry Smith 
10915c6c1daeSBarry Smith   Input Parameters:
1092232ac771SBarry Smith +  viewer - the PetscViewer; must be a a PETSCVIEWERBINARY, PETSCVIEWERMATLAB, PETSCVIEWERHDF5, or PETSCVIEWERASCII  PetscViewer
10935c6c1daeSBarry Smith -  type - type of file
1094f8859db6SBarry Smith $    FILE_MODE_WRITE - create new file for output
1095f8859db6SBarry Smith $    FILE_MODE_READ - open existing file for input
1096f8859db6SBarry Smith $    FILE_MODE_APPEND - open existing file for output
10975c6c1daeSBarry Smith 
10985c6c1daeSBarry Smith   Level: advanced
10995c6c1daeSBarry Smith 
11005c6c1daeSBarry Smith .seealso: PetscViewerFileSetMode(), PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinaryOpen()
11015c6c1daeSBarry Smith 
11025c6c1daeSBarry Smith @*/
11035c6c1daeSBarry Smith PetscErrorCode PetscViewerFileSetMode(PetscViewer viewer,PetscFileMode type)
11045c6c1daeSBarry Smith {
11055c6c1daeSBarry Smith   PetscErrorCode ierr;
11065c6c1daeSBarry Smith 
11075c6c1daeSBarry Smith   PetscFunctionBegin;
11085c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
11095c6c1daeSBarry Smith   PetscValidLogicalCollectiveEnum(viewer,type,2);
11105c6c1daeSBarry Smith   ierr = PetscTryMethod(viewer,"PetscViewerFileSetMode_C",(PetscViewer,PetscFileMode),(viewer,type));CHKERRQ(ierr);
11115c6c1daeSBarry Smith   PetscFunctionReturn(0);
11125c6c1daeSBarry Smith }
11135c6c1daeSBarry Smith 
111481f0254dSBarry Smith static PetscErrorCode PetscViewerFileGetMode_Binary(PetscViewer viewer,PetscFileMode *type)
11155c6c1daeSBarry Smith {
11165c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
11175c6c1daeSBarry Smith 
11185c6c1daeSBarry Smith   PetscFunctionBegin;
11195c6c1daeSBarry Smith   *type = vbinary->btype;
11205c6c1daeSBarry Smith   PetscFunctionReturn(0);
11215c6c1daeSBarry Smith }
11225c6c1daeSBarry Smith 
112381f0254dSBarry Smith static PetscErrorCode PetscViewerFileSetMode_Binary(PetscViewer viewer,PetscFileMode type)
11245c6c1daeSBarry Smith {
11255c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
11265c6c1daeSBarry Smith 
11275c6c1daeSBarry Smith   PetscFunctionBegin;
11285c6c1daeSBarry Smith   vbinary->btype = type;
11295c6c1daeSBarry Smith   PetscFunctionReturn(0);
11305c6c1daeSBarry Smith }
11315c6c1daeSBarry Smith 
113281f0254dSBarry Smith static PetscErrorCode PetscViewerFileSetName_Binary(PetscViewer viewer,const char name[])
1133e0385b85SDave May {
1134e0385b85SDave May   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
1135e0385b85SDave May   PetscErrorCode     ierr;
1136e0385b85SDave May 
1137e0385b85SDave May   PetscFunctionBegin;
1138f90597f1SStefano Zampini   if (vbinary->filename) {
1139f90597f1SStefano Zampini     /* gzip can be run after the file with the previous filename has been closed */
1140f90597f1SStefano Zampini     ierr = PetscFree(vbinary->ogzfilename);CHKERRQ(ierr);
1141f90597f1SStefano Zampini     ierr = PetscStrallocpy(vbinary->filename,&vbinary->ogzfilename);CHKERRQ(ierr);
1142f90597f1SStefano Zampini     ierr = PetscFree(vbinary->filename);CHKERRQ(ierr);
1143f90597f1SStefano Zampini     viewer->setupcalled = PETSC_FALSE;
1144f90597f1SStefano Zampini   }
1145e0385b85SDave May   ierr = PetscStrallocpy(name,&vbinary->filename);CHKERRQ(ierr);
1146e0385b85SDave May   PetscFunctionReturn(0);
1147e0385b85SDave May }
11485c6c1daeSBarry Smith /*
11495c6c1daeSBarry Smith         Actually opens the file
11505c6c1daeSBarry Smith */
1151e0877f53SBarry Smith static PetscErrorCode PetscViewerFileSetUp_Binary(PetscViewer viewer)
11525c6c1daeSBarry Smith {
11535c6c1daeSBarry Smith   PetscMPIInt        rank;
11545c6c1daeSBarry Smith   PetscErrorCode     ierr;
11555c6c1daeSBarry Smith   size_t             len;
11565c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
11575c6c1daeSBarry Smith   const char         *fname;
11582259a519SDave May   char               bname[PETSC_MAX_PATH_LEN],*gz;
11595c6c1daeSBarry Smith   PetscBool          found;
11605c6c1daeSBarry Smith   PetscFileMode      type = vbinary->btype;
11615c6c1daeSBarry Smith 
11625c6c1daeSBarry Smith   PetscFunctionBegin;
1163e0385b85SDave May   if (type == (PetscFileMode) -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetMode()");
11642259a519SDave May   if (!vbinary->filename) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetName()");
11655c6c1daeSBarry Smith   ierr = PetscViewerFileClose_Binary(viewer);CHKERRQ(ierr);
11665c6c1daeSBarry Smith 
1167ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
11685c6c1daeSBarry Smith 
11695c6c1daeSBarry Smith   /* if ends in .gz strip that off and note user wants file compressed */
11705c6c1daeSBarry Smith   vbinary->storecompressed = PETSC_FALSE;
11715c6c1daeSBarry Smith   if (!rank && type == FILE_MODE_WRITE) {
11725c6c1daeSBarry Smith     /* remove .gz if it ends library name */
11735c6c1daeSBarry Smith     ierr = PetscStrstr(vbinary->filename,".gz",&gz);CHKERRQ(ierr);
11745c6c1daeSBarry Smith     if (gz) {
11755c6c1daeSBarry Smith       ierr = PetscStrlen(gz,&len);CHKERRQ(ierr);
11765c6c1daeSBarry Smith       if (len == 3) {
11775c6c1daeSBarry Smith         *gz = 0;
11785c6c1daeSBarry Smith         vbinary->storecompressed = PETSC_TRUE;
11795c6c1daeSBarry Smith       }
11805c6c1daeSBarry Smith     }
11815c6c1daeSBarry Smith   }
11825c6c1daeSBarry Smith 
11835c6c1daeSBarry Smith   /* only first processor opens file if writeable */
11845c6c1daeSBarry Smith   if (!rank || type == FILE_MODE_READ) {
11855c6c1daeSBarry Smith 
11865c6c1daeSBarry Smith     if (type == FILE_MODE_READ) {
11875c6c1daeSBarry Smith       /* possibly get the file from remote site or compressed file */
1188ce94432eSBarry Smith       ierr  = PetscFileRetrieve(PetscObjectComm((PetscObject)viewer),vbinary->filename,bname,PETSC_MAX_PATH_LEN,&found);CHKERRQ(ierr);
11895c6c1daeSBarry Smith       fname = bname;
119041c4be4aSVaclav Hapla       /* comm below may be global as all ranks go here for FILE_MODE_READ and output 'found' of PetscFileRetrieve() is valid on all processes */
119141c4be4aSVaclav Hapla       if (!found) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_FILE_OPEN,"Cannot locate file: %s on node zero",vbinary->filename);
1192a297a907SKarl Rupp     } else fname = vbinary->filename;
119355819941SStefano Zampini     if (type == FILE_MODE_APPEND) { /* check if asked to append to a non-existing file */
119455819941SStefano Zampini       ierr = PetscTestFile(fname,'\0',&found);CHKERRQ(ierr);
119555819941SStefano Zampini     }
11965c6c1daeSBarry Smith 
11975c6c1daeSBarry Smith #if defined(PETSC_HAVE_O_BINARY)
119855819941SStefano Zampini     if (type == FILE_MODE_WRITE || (type == FILE_MODE_APPEND && !found) ) {
11995c6c1daeSBarry 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);
12005c6c1daeSBarry Smith     } else if (type == FILE_MODE_READ && fname) {
12015c6c1daeSBarry 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);
12025c6c1daeSBarry Smith     } else if (type == FILE_MODE_APPEND) {
12035c6c1daeSBarry 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);
12045c6c1daeSBarry Smith     } else if (fname) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown file type");
12055c6c1daeSBarry Smith #else
120655819941SStefano Zampini     if (type == FILE_MODE_WRITE || (type == FILE_MODE_APPEND && !found) ) {
12075c6c1daeSBarry Smith       if ((vbinary->fdes = creat(fname,0666)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot create file %s for writing",fname);
12085c6c1daeSBarry Smith     } else if (type == FILE_MODE_READ && fname) {
12095c6c1daeSBarry 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);
12105c6c1daeSBarry Smith     } else if (type == FILE_MODE_APPEND) {
12115c6c1daeSBarry 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);
12125c6c1daeSBarry Smith     } else if (fname) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown file type");
12135c6c1daeSBarry Smith #endif
12145c6c1daeSBarry Smith   } else vbinary->fdes = -1;
12155c6c1daeSBarry Smith 
12165c6c1daeSBarry Smith   /*
12175c6c1daeSBarry Smith       try to open info file: all processors open this file if read only
12185c6c1daeSBarry Smith   */
12195c6c1daeSBarry Smith   if (!vbinary->skipinfo && (!rank || type == FILE_MODE_READ)) {
12205c6c1daeSBarry Smith     char infoname[PETSC_MAX_PATH_LEN],iname[PETSC_MAX_PATH_LEN];
12215c6c1daeSBarry Smith 
1222a126751eSBarry Smith     ierr = PetscStrncpy(infoname,vbinary->filename,sizeof(infoname));CHKERRQ(ierr);
12235c6c1daeSBarry Smith     /* remove .gz if it ends library name */
12245c6c1daeSBarry Smith     ierr = PetscStrstr(infoname,".gz",&gz);CHKERRQ(ierr);
12255c6c1daeSBarry Smith     if (gz) {
12265c6c1daeSBarry Smith       ierr = PetscStrlen(gz,&len);CHKERRQ(ierr);
1227a297a907SKarl Rupp       if (len == 3) *gz = 0;
12285c6c1daeSBarry Smith     }
12295c6c1daeSBarry Smith 
1230a126751eSBarry Smith     ierr = PetscStrlcat(infoname,".info",sizeof(infoname));CHKERRQ(ierr);
12315c6c1daeSBarry Smith     ierr = PetscFixFilename(infoname,iname);CHKERRQ(ierr);
12325c6c1daeSBarry Smith     if (type == FILE_MODE_READ) {
1233ce94432eSBarry Smith       ierr = PetscFileRetrieve(PetscObjectComm((PetscObject)viewer),iname,infoname,PETSC_MAX_PATH_LEN,&found);CHKERRQ(ierr);
12341f5e1f59SVaclav Hapla       if (found) {
1235c5929fdfSBarry Smith         ierr = PetscOptionsInsertFile(PetscObjectComm((PetscObject)viewer),((PetscObject)viewer)->options,infoname,PETSC_FALSE);CHKERRQ(ierr);
12361f5e1f59SVaclav Hapla       }
12375c6c1daeSBarry Smith     } else {
12385c6c1daeSBarry Smith       vbinary->fdes_info = fopen(infoname,"w");
12395c6c1daeSBarry Smith       if (!vbinary->fdes_info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open .info file %s for writing",infoname);
12405c6c1daeSBarry Smith     }
12415c6c1daeSBarry Smith   }
12425c6c1daeSBarry Smith #if defined(PETSC_USE_LOG)
1243e0385b85SDave May   PetscLogObjectState((PetscObject)viewer,"File: %s",vbinary->filename);
12445c6c1daeSBarry Smith #endif
12455c6c1daeSBarry Smith   PetscFunctionReturn(0);
12465c6c1daeSBarry Smith }
12475c6c1daeSBarry Smith 
12485c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
1249e0385b85SDave May static PetscErrorCode PetscViewerFileSetUp_BinaryMPIIO(PetscViewer viewer)
12505c6c1daeSBarry Smith {
12515c6c1daeSBarry Smith   PetscMPIInt        rank;
12525c6c1daeSBarry Smith   PetscErrorCode     ierr;
12535c6c1daeSBarry Smith   size_t             len;
12545c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
12552259a519SDave May   char               *gz;
12565c6c1daeSBarry Smith   PetscBool          found;
12575c6c1daeSBarry Smith   PetscFileMode      type = vbinary->btype;
1258e8a65b7dSLisandro Dalcin   int                amode;
12595c6c1daeSBarry Smith 
12605c6c1daeSBarry Smith   PetscFunctionBegin;
1261e0385b85SDave May   if (type == (PetscFileMode) -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetMode()");
12622259a519SDave May   if (!vbinary->filename) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetName()");
1263e0385b85SDave May   ierr = PetscViewerFileClose_BinaryMPIIO(viewer);CHKERRQ(ierr);
12645c6c1daeSBarry Smith 
1265a297a907SKarl Rupp   vbinary->storecompressed = PETSC_FALSE;
12665c6c1daeSBarry Smith 
1267e8a65b7dSLisandro Dalcin   switch (type) {
1268e8a65b7dSLisandro Dalcin   case FILE_MODE_READ:  amode = MPI_MODE_RDONLY; break;
1269e8a65b7dSLisandro Dalcin   case FILE_MODE_WRITE: amode = MPI_MODE_WRONLY | MPI_MODE_CREATE; break;
1270e8a65b7dSLisandro Dalcin   default: SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Unsupported file mode %s",PetscFileModes[type]);
12715c6c1daeSBarry Smith   }
1272e8a65b7dSLisandro Dalcin   ierr = MPI_File_open(PetscObjectComm((PetscObject)viewer),vbinary->filename,amode,MPI_INFO_NULL,&vbinary->mfdes);CHKERRQ(ierr);
12735c6c1daeSBarry Smith 
12745c6c1daeSBarry Smith   /*
12755c6c1daeSBarry Smith       try to open info file: all processors open this file if read only
12765c6c1daeSBarry Smith 
1277bebe2cf6SSatish Balay       Below is identical code to the code for Binary above, should be put in separate routine
12785c6c1daeSBarry Smith   */
1279e8a65b7dSLisandro Dalcin   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
12805c6c1daeSBarry Smith   if (!vbinary->skipinfo && (!rank || type == FILE_MODE_READ)) {
12815c6c1daeSBarry Smith     char infoname[PETSC_MAX_PATH_LEN],iname[PETSC_MAX_PATH_LEN];
12825c6c1daeSBarry Smith 
1283a126751eSBarry Smith     ierr = PetscStrncpy(infoname,vbinary->filename,sizeof(infoname));CHKERRQ(ierr);
12845c6c1daeSBarry Smith     /* remove .gz if it ends library name */
12855c6c1daeSBarry Smith     ierr = PetscStrstr(infoname,".gz",&gz);CHKERRQ(ierr);
12865c6c1daeSBarry Smith     if (gz) {
12875c6c1daeSBarry Smith       ierr = PetscStrlen(gz,&len);CHKERRQ(ierr);
1288a297a907SKarl Rupp       if (len == 3) *gz = 0;
12895c6c1daeSBarry Smith     }
12905c6c1daeSBarry Smith 
1291a126751eSBarry Smith     ierr = PetscStrlcat(infoname,".info",sizeof(infoname));CHKERRQ(ierr);
12925c6c1daeSBarry Smith     ierr = PetscFixFilename(infoname,iname);CHKERRQ(ierr);
12935c6c1daeSBarry Smith     if (type == FILE_MODE_READ) {
1294ce94432eSBarry Smith       ierr = PetscFileRetrieve(PetscObjectComm((PetscObject)viewer),iname,infoname,PETSC_MAX_PATH_LEN,&found);CHKERRQ(ierr);
1295c5929fdfSBarry Smith       ierr = PetscOptionsInsertFile(PetscObjectComm((PetscObject)viewer),((PetscObject)viewer)->options,infoname,PETSC_FALSE);CHKERRQ(ierr);
12965c6c1daeSBarry Smith     } else {
12975c6c1daeSBarry Smith       vbinary->fdes_info = fopen(infoname,"w");
12985c6c1daeSBarry Smith       if (!vbinary->fdes_info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open .info file %s for writing",infoname);
12995c6c1daeSBarry Smith     }
13005c6c1daeSBarry Smith   }
13015c6c1daeSBarry Smith #if defined(PETSC_USE_LOG)
1302e0385b85SDave May   PetscLogObjectState((PetscObject)viewer,"File: %s",vbinary->filename);
13035c6c1daeSBarry Smith #endif
13045c6c1daeSBarry Smith   PetscFunctionReturn(0);
13055c6c1daeSBarry Smith }
13065c6c1daeSBarry Smith 
130781f0254dSBarry Smith static PetscErrorCode PetscViewerBinarySetUseMPIIO_Binary(PetscViewer viewer,PetscBool flg)
13085c6c1daeSBarry Smith {
13095c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
13105c6c1daeSBarry Smith   PetscFunctionBegin;
1311bc196f7cSDave May   vbinary->usempiio = flg;
13125c6c1daeSBarry Smith   PetscFunctionReturn(0);
13135c6c1daeSBarry Smith }
13145c6c1daeSBarry Smith #endif
13155c6c1daeSBarry Smith 
131681f0254dSBarry Smith static PetscErrorCode PetscViewerView_Binary(PetscViewer v,PetscViewer viewer)
13172bf49c77SBarry Smith {
13182bf49c77SBarry Smith   PetscErrorCode     ierr;
13192bf49c77SBarry Smith   PetscViewer_Binary *binary = (PetscViewer_Binary*)v->data;
13202bf49c77SBarry Smith 
13212bf49c77SBarry Smith   PetscFunctionBegin;
13222bf49c77SBarry Smith   if (binary->filename) {
13232bf49c77SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"Filename: %s\n",binary->filename);CHKERRQ(ierr);
13242bf49c77SBarry Smith   }
13252bf49c77SBarry Smith   PetscFunctionReturn(0);
13262bf49c77SBarry Smith }
13272bf49c77SBarry Smith 
132803a1d59fSDave May static PetscErrorCode PetscViewerSetUp_Binary(PetscViewer v)
132903a1d59fSDave May {
133003a1d59fSDave May   PetscErrorCode     ierr;
133103a1d59fSDave May   PetscViewer_Binary *binary = (PetscViewer_Binary*)v->data;
1332e0385b85SDave May 
133303a1d59fSDave May   PetscFunctionBegin;
1334da07bb01SDave May   if (!binary->setfromoptionscalled) { ierr = PetscViewerSetFromOptions(v);CHKERRQ(ierr); }
133503a1d59fSDave May 
1336e0385b85SDave May #if defined(PETSC_HAVE_MPIIO)
1337bc196f7cSDave May   if (binary->usempiio) {
1338e0385b85SDave May     ierr = PetscViewerFileSetUp_BinaryMPIIO(v);CHKERRQ(ierr);
1339e0385b85SDave May   } else {
1340e0385b85SDave May #endif
1341e0385b85SDave May     ierr = PetscViewerFileSetUp_Binary(v);CHKERRQ(ierr);
1342e0385b85SDave May #if defined(PETSC_HAVE_MPIIO)
1343e0385b85SDave May   }
1344e0385b85SDave May #endif
134503a1d59fSDave May   PetscFunctionReturn(0);
134603a1d59fSDave May }
134703a1d59fSDave May 
13484416b707SBarry Smith static PetscErrorCode PetscViewerSetFromOptions_Binary(PetscOptionItems *PetscOptionsObject,PetscViewer v)
134903a1d59fSDave May {
135003a1d59fSDave May   PetscErrorCode     ierr;
135103a1d59fSDave May   PetscViewer_Binary *binary = (PetscViewer_Binary*)v->data;
1352d22fd6bcSDave May   char               defaultname[PETSC_MAX_PATH_LEN];
135303a1d59fSDave May   PetscBool          flg;
1354e0385b85SDave May 
135503a1d59fSDave May   PetscFunctionBegin;
135603a1d59fSDave May   ierr = PetscOptionsHead(PetscOptionsObject,"Binary PetscViewer Options");CHKERRQ(ierr);
1357d22fd6bcSDave May   ierr = PetscSNPrintf(defaultname,PETSC_MAX_PATH_LEN-1,"binaryoutput");CHKERRQ(ierr);
1358d22fd6bcSDave May   ierr = PetscOptionsString("-viewer_binary_filename","Specify filename","PetscViewerFileSetName",defaultname,defaultname,PETSC_MAX_PATH_LEN-1,&flg);CHKERRQ(ierr);
1359d22fd6bcSDave May   if (flg) { ierr = PetscViewerFileSetName_Binary(v,defaultname);CHKERRQ(ierr); }
136034a9cc2cSBarry Smith   ierr = PetscOptionsBool("-viewer_binary_skip_info","Skip writing/reading .info file","PetscViewerBinarySetSkipInfo",PETSC_FALSE,&binary->skipinfo,NULL);CHKERRQ(ierr);
136134a9cc2cSBarry Smith   ierr = PetscOptionsBool("-viewer_binary_skip_options","Skip parsing vec load options","PetscViewerBinarySetSkipOptions",PETSC_TRUE,&binary->skipoptions,NULL);CHKERRQ(ierr);
136234a9cc2cSBarry Smith   ierr = PetscOptionsBool("-viewer_binary_skip_header","Skip writing/reading header information","PetscViewerBinarySetSkipHeader",PETSC_FALSE,&binary->skipheader,NULL);CHKERRQ(ierr);
136303a1d59fSDave May #if defined(PETSC_HAVE_MPIIO)
136434a9cc2cSBarry Smith   ierr = PetscOptionsBool("-viewer_binary_mpiio","Use MPI-IO functionality to write/read binary file","PetscViewerBinarySetUseMPIIO",PETSC_FALSE,&binary->usempiio,NULL);CHKERRQ(ierr);
136534a9cc2cSBarry Smith #elif defined(PETSC_HAVE_MPIUNI)
136634a9cc2cSBarry Smith   ierr = PetscOptionsBool("-viewer_binary_mpiio","Use MPI-IO functionality to write/read binary file","PetscViewerBinarySetUseMPIIO",PETSC_FALSE,NULL,NULL);CHKERRQ(ierr);
136703a1d59fSDave May #endif
136803a1d59fSDave May   ierr = PetscOptionsTail();CHKERRQ(ierr);
1369bc196f7cSDave May   binary->setfromoptionscalled = PETSC_TRUE;
137003a1d59fSDave May   PetscFunctionReturn(0);
137103a1d59fSDave May }
137203a1d59fSDave May 
13738556b5ebSBarry Smith /*MC
13748556b5ebSBarry Smith    PETSCVIEWERBINARY - A viewer that saves to binary files
13758556b5ebSBarry Smith 
13768556b5ebSBarry Smith 
13778556b5ebSBarry Smith .seealso:  PetscViewerBinaryOpen(), PETSC_VIEWER_STDOUT_(),PETSC_VIEWER_STDOUT_SELF, PETSC_VIEWER_STDOUT_WORLD, PetscViewerCreate(), PetscViewerASCIIOpen(),
13788556b5ebSBarry Smith            PetscViewerMatlabOpen(), VecView(), DMView(), PetscViewerMatlabPutArray(), PETSCVIEWERASCII, PETSCVIEWERMATLAB, PETSCVIEWERDRAW,
137967918a83SBarry Smith            PetscViewerFileSetName(), PetscViewerFileSetMode(), PetscViewerFormat, PetscViewerType, PetscViewerSetType(),
138067918a83SBarry Smith            PetscViewerBinaryGetUseMPIIO(), PetscViewerBinarySetUseMPIIO()
13818556b5ebSBarry Smith 
13821b266c99SBarry Smith   Level: beginner
13831b266c99SBarry Smith 
13848556b5ebSBarry Smith M*/
13858556b5ebSBarry Smith 
13868cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscViewerCreate_Binary(PetscViewer v)
13875c6c1daeSBarry Smith {
13885c6c1daeSBarry Smith   PetscErrorCode     ierr;
13895c6c1daeSBarry Smith   PetscViewer_Binary *vbinary;
13905c6c1daeSBarry Smith 
13915c6c1daeSBarry Smith   PetscFunctionBegin;
1392b00a9115SJed Brown   ierr                     = PetscNewLog(v,&vbinary);CHKERRQ(ierr);
13935c6c1daeSBarry Smith   v->data                  = (void*)vbinary;
139403a1d59fSDave May   v->ops->setfromoptions   = PetscViewerSetFromOptions_Binary;
13955c6c1daeSBarry Smith   v->ops->destroy          = PetscViewerDestroy_Binary;
13962bf49c77SBarry Smith   v->ops->view             = PetscViewerView_Binary;
1397c98fd787SBarry Smith   v->ops->setup            = PetscViewerSetUp_Binary;
1398c98fd787SBarry Smith   v->ops->flush            = NULL;
13995c6c1daeSBarry Smith   vbinary->fdes            = 0;
1400e8a65b7dSLisandro Dalcin #if defined(PETSC_HAVE_MPIIO)
1401e8a65b7dSLisandro Dalcin   vbinary->mfdes           = MPI_FILE_NULL;
1402e8a65b7dSLisandro Dalcin   vbinary->mfsub           = MPI_FILE_NULL;
1403e8a65b7dSLisandro Dalcin #endif
140402c9f0b5SLisandro Dalcin   vbinary->fdes_info       = NULL;
14055c6c1daeSBarry Smith   vbinary->skipinfo        = PETSC_FALSE;
14065c6c1daeSBarry Smith   vbinary->skipoptions     = PETSC_TRUE;
14075c6c1daeSBarry Smith   vbinary->skipheader      = PETSC_FALSE;
1408da07bb01SDave May   vbinary->setfromoptionscalled = PETSC_FALSE;
1409559f443fSBarry Smith   v->ops->getsubviewer     = PetscViewerGetSubViewer_Binary;
1410559f443fSBarry Smith   v->ops->restoresubviewer = PetscViewerRestoreSubViewer_Binary;
14111d641e7bSMichael Lange   v->ops->read             = PetscViewerBinaryRead;
14125c6c1daeSBarry Smith   vbinary->btype           = (PetscFileMode) -1;
14135c6c1daeSBarry Smith   vbinary->storecompressed = PETSC_FALSE;
1414f90597f1SStefano Zampini   vbinary->filename        = NULL;
1415f90597f1SStefano Zampini   vbinary->ogzfilename     = NULL;
14165c6c1daeSBarry Smith   vbinary->flowcontrol     = 256; /* seems a good number for Cray XT-5 */
14175c6c1daeSBarry Smith 
1418bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetFlowControl_C",PetscViewerBinaryGetFlowControl_Binary);CHKERRQ(ierr);
1419bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetFlowControl_C",PetscViewerBinarySetFlowControl_Binary);CHKERRQ(ierr);
1420bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetSkipHeader_C",PetscViewerBinarySetSkipHeader_Binary);CHKERRQ(ierr);
1421bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetSkipHeader_C",PetscViewerBinaryGetSkipHeader_Binary);CHKERRQ(ierr);
1422807ea322SDave May   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetSkipOptions_C",PetscViewerBinaryGetSkipOptions_Binary);CHKERRQ(ierr);
1423807ea322SDave May   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetSkipOptions_C",PetscViewerBinarySetSkipOptions_Binary);CHKERRQ(ierr);
1424807ea322SDave May   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetSkipInfo_C",PetscViewerBinaryGetSkipInfo_Binary);CHKERRQ(ierr);
1425807ea322SDave May   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetSkipInfo_C",PetscViewerBinarySetSkipInfo_Binary);CHKERRQ(ierr);
1426bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetInfoPointer_C",PetscViewerBinaryGetInfoPointer_Binary);CHKERRQ(ierr);
1427bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetName_C",PetscViewerFileSetName_Binary);CHKERRQ(ierr);
1428bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetMode_C",PetscViewerFileSetMode_Binary);CHKERRQ(ierr);
1429bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileGetMode_C",PetscViewerFileGetMode_Binary);CHKERRQ(ierr);
1430bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileGetName_C",PetscViewerFileGetName_Binary);CHKERRQ(ierr);
14315c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
1432bc196f7cSDave May   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetUseMPIIO_C",PetscViewerBinaryGetUseMPIIO_Binary);CHKERRQ(ierr);
1433bc196f7cSDave May   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetUseMPIIO_C",PetscViewerBinarySetUseMPIIO_Binary);CHKERRQ(ierr);
14345c6c1daeSBarry Smith #endif
14355c6c1daeSBarry Smith   PetscFunctionReturn(0);
14365c6c1daeSBarry Smith }
14375c6c1daeSBarry Smith 
14385c6c1daeSBarry Smith /* ---------------------------------------------------------------------*/
14395c6c1daeSBarry Smith /*
14405c6c1daeSBarry Smith     The variable Petsc_Viewer_Binary_keyval is used to indicate an MPI attribute that
14415c6c1daeSBarry Smith   is attached to a communicator, in this case the attribute is a PetscViewer.
14425c6c1daeSBarry Smith */
1443d4c7638eSBarry Smith PetscMPIInt Petsc_Viewer_Binary_keyval = MPI_KEYVAL_INVALID;
14445c6c1daeSBarry Smith 
14455c6c1daeSBarry Smith /*@C
14465c6c1daeSBarry Smith      PETSC_VIEWER_BINARY_ - Creates a binary PetscViewer shared by all processors
14475c6c1daeSBarry Smith                      in a communicator.
14485c6c1daeSBarry Smith 
1449d083f849SBarry Smith      Collective
14505c6c1daeSBarry Smith 
14515c6c1daeSBarry Smith      Input Parameter:
14525c6c1daeSBarry Smith .    comm - the MPI communicator to share the binary PetscViewer
14535c6c1daeSBarry Smith 
14545c6c1daeSBarry Smith      Level: intermediate
14555c6c1daeSBarry Smith 
14565c6c1daeSBarry Smith    Options Database Keys:
14575c6c1daeSBarry Smith +    -viewer_binary_filename <name>
14585c6c1daeSBarry Smith .    -viewer_binary_skip_info
1459a2d7db39SDave May .    -viewer_binary_skip_options
1460a2d7db39SDave May .    -viewer_binary_skip_header
1461a2d7db39SDave May -    -viewer_binary_mpiio
14625c6c1daeSBarry Smith 
14635c6c1daeSBarry Smith    Environmental variables:
14645c6c1daeSBarry Smith -   PETSC_VIEWER_BINARY_FILENAME
14655c6c1daeSBarry Smith 
14665c6c1daeSBarry Smith      Notes:
14675c6c1daeSBarry Smith      Unlike almost all other PETSc routines, PETSC_VIEWER_BINARY_ does not return
14685c6c1daeSBarry Smith      an error code.  The binary PetscViewer is usually used in the form
14695c6c1daeSBarry Smith $       XXXView(XXX object,PETSC_VIEWER_BINARY_(comm));
14705c6c1daeSBarry Smith 
14715c6c1daeSBarry Smith .seealso: PETSC_VIEWER_BINARY_WORLD, PETSC_VIEWER_BINARY_SELF, PetscViewerBinaryOpen(), PetscViewerCreate(),
14725c6c1daeSBarry Smith           PetscViewerDestroy()
14735c6c1daeSBarry Smith @*/
14745c6c1daeSBarry Smith PetscViewer PETSC_VIEWER_BINARY_(MPI_Comm comm)
14755c6c1daeSBarry Smith {
14765c6c1daeSBarry Smith   PetscErrorCode ierr;
14775c6c1daeSBarry Smith   PetscBool      flg;
14785c6c1daeSBarry Smith   PetscViewer    viewer;
14795c6c1daeSBarry Smith   char           fname[PETSC_MAX_PATH_LEN];
14805c6c1daeSBarry Smith   MPI_Comm       ncomm;
14815c6c1daeSBarry Smith 
14825c6c1daeSBarry Smith   PetscFunctionBegin;
148302c9f0b5SLisandro Dalcin   ierr = PetscCommDuplicate(comm,&ncomm,NULL);if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(NULL);}
14845c6c1daeSBarry Smith   if (Petsc_Viewer_Binary_keyval == MPI_KEYVAL_INVALID) {
148502c9f0b5SLisandro Dalcin     ierr = MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN,MPI_COMM_NULL_DELETE_FN,&Petsc_Viewer_Binary_keyval,NULL);
148602c9f0b5SLisandro Dalcin     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(NULL);}
14875c6c1daeSBarry Smith   }
148847435625SJed Brown   ierr = MPI_Comm_get_attr(ncomm,Petsc_Viewer_Binary_keyval,(void**)&viewer,(int*)&flg);
148902c9f0b5SLisandro Dalcin   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(NULL);}
14905c6c1daeSBarry Smith   if (!flg) { /* PetscViewer not yet created */
14915c6c1daeSBarry Smith     ierr = PetscOptionsGetenv(ncomm,"PETSC_VIEWER_BINARY_FILENAME",fname,PETSC_MAX_PATH_LEN,&flg);
149202c9f0b5SLisandro Dalcin     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(NULL);}
14935c6c1daeSBarry Smith     if (!flg) {
14945c6c1daeSBarry Smith       ierr = PetscStrcpy(fname,"binaryoutput");
149502c9f0b5SLisandro Dalcin       if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(NULL);}
14965c6c1daeSBarry Smith     }
14975c6c1daeSBarry Smith     ierr = PetscViewerBinaryOpen(ncomm,fname,FILE_MODE_WRITE,&viewer);
149802c9f0b5SLisandro Dalcin     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(NULL);}
14995c6c1daeSBarry Smith     ierr = PetscObjectRegisterDestroy((PetscObject)viewer);
150002c9f0b5SLisandro Dalcin     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(NULL);}
150147435625SJed Brown     ierr = MPI_Comm_set_attr(ncomm,Petsc_Viewer_Binary_keyval,(void*)viewer);
150202c9f0b5SLisandro Dalcin     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(NULL);}
15035c6c1daeSBarry Smith   }
15045c6c1daeSBarry Smith   ierr = PetscCommDestroy(&ncomm);
150502c9f0b5SLisandro Dalcin   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(NULL);}
15065c6c1daeSBarry Smith   PetscFunctionReturn(viewer);
15075c6c1daeSBarry Smith }
1508