xref: /petsc/src/sys/classes/viewer/impls/binary/binv.c (revision 63c55180f3a1699b45d3541b604931f10ad6dae3)
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) {
445c6c1daeSBarry Smith     ierr = PetscViewerCreate(PETSC_COMM_SELF,outviewer);CHKERRQ(ierr);
455c6c1daeSBarry Smith     ierr = PetscViewerSetType(*outviewer,PETSCVIEWERBINARY);CHKERRQ(ierr);
46e8a65b7dSLisandro Dalcin     ierr = PetscMemcpy((*outviewer)->data,vbinary,sizeof(PetscViewer_Binary));CHKERRQ(ierr);
4712f4c3a9SLisandro Dalcin     (*outviewer)->setupcalled = PETSC_TRUE;
4896509d17SLisandro Dalcin   } else {
4996509d17SLisandro Dalcin     *outviewer = NULL;
5096509d17SLisandro Dalcin   }
51e8a65b7dSLisandro Dalcin 
52e8a65b7dSLisandro Dalcin #if defined(PETSC_HAVE_MPIIO)
53e8a65b7dSLisandro Dalcin   if (vbinary->usempiio && *outviewer) {
54e8a65b7dSLisandro Dalcin     PetscViewer_Binary *obinary = (PetscViewer_Binary*)(*outviewer)->data;
55e8a65b7dSLisandro Dalcin     /* Parent viewer opens a new MPI file handle on PETSC_COMM_SELF and keeps track of it for future reuse */
56e8a65b7dSLisandro Dalcin     if (vbinary->mfsub == MPI_FILE_NULL) {
57e8a65b7dSLisandro Dalcin       int amode;
58e8a65b7dSLisandro Dalcin       switch (vbinary->btype) {
59e8a65b7dSLisandro Dalcin       case FILE_MODE_READ:  amode = MPI_MODE_RDONLY; break;
60e8a65b7dSLisandro Dalcin       case FILE_MODE_WRITE: amode = MPI_MODE_WRONLY; break;
61e8a65b7dSLisandro Dalcin       default: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unsupported file mode %s",PetscFileModes[vbinary->btype]);
62e8a65b7dSLisandro Dalcin       }
63e8a65b7dSLisandro Dalcin       ierr = MPI_File_open(PETSC_COMM_SELF,vbinary->filename,amode,MPI_INFO_NULL,&vbinary->mfsub);CHKERRQ(ierr);
64e8a65b7dSLisandro Dalcin     }
65e8a65b7dSLisandro Dalcin     /* Subviewer gets the MPI file handle on PETSC_COMM_SELF */
66e8a65b7dSLisandro Dalcin     obinary->mfdes = vbinary->mfsub;
67e8a65b7dSLisandro Dalcin     obinary->mfsub = MPI_FILE_NULL;
68e8a65b7dSLisandro Dalcin     obinary->moff  = vbinary->moff;
69e8a65b7dSLisandro Dalcin   }
70e8a65b7dSLisandro Dalcin #endif
715c6c1daeSBarry Smith   PetscFunctionReturn(0);
725c6c1daeSBarry Smith }
735c6c1daeSBarry Smith 
7481f0254dSBarry Smith static PetscErrorCode PetscViewerRestoreSubViewer_Binary(PetscViewer viewer,MPI_Comm comm,PetscViewer *outviewer)
755c6c1daeSBarry Smith {
765c6c1daeSBarry Smith   PetscErrorCode     rank;
77e8a65b7dSLisandro Dalcin   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
78e8a65b7dSLisandro Dalcin   PetscErrorCode     ierr;
79e8a65b7dSLisandro Dalcin #if defined(PETSC_HAVE_MPIIO)
80e8a65b7dSLisandro Dalcin   MPI_Offset         moff = 0;
81e8a65b7dSLisandro Dalcin #endif
825c6c1daeSBarry Smith 
835c6c1daeSBarry Smith   PetscFunctionBegin;
84ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
85e8a65b7dSLisandro Dalcin   if (rank && *outviewer) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Subviewer not obtained from viewer");
86e8a65b7dSLisandro Dalcin 
87e8a65b7dSLisandro Dalcin #if defined(PETSC_HAVE_MPIIO)
88e8a65b7dSLisandro Dalcin   if (vbinary->usempiio && *outviewer) {
89e8a65b7dSLisandro Dalcin     PetscViewer_Binary *obinary = (PetscViewer_Binary*)(*outviewer)->data;
90e8a65b7dSLisandro Dalcin     if (obinary->mfdes != vbinary->mfsub) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Subviewer not obtained from viewer");
91e8a65b7dSLisandro Dalcin     moff = obinary->moff;
92e8a65b7dSLisandro Dalcin   }
93e8a65b7dSLisandro Dalcin #endif
94e8a65b7dSLisandro Dalcin 
95e8a65b7dSLisandro Dalcin   if (*outviewer) {
96e8a65b7dSLisandro Dalcin     PetscViewer_Binary *obinary = (PetscViewer_Binary*)(*outviewer)->data;
97e8a65b7dSLisandro Dalcin     if (obinary->fdes != vbinary->fdes) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Subviewer not obtained from viewer");
985c6c1daeSBarry Smith     ierr = PetscFree((*outviewer)->data);CHKERRQ(ierr);
995c6c1daeSBarry Smith     ierr = PetscHeaderDestroy(outviewer);CHKERRQ(ierr);
1005c6c1daeSBarry Smith   }
101e8a65b7dSLisandro Dalcin 
102e8a65b7dSLisandro Dalcin #if defined(PETSC_HAVE_MPIIO)
103e8a65b7dSLisandro Dalcin   if (vbinary->usempiio) {
104e8a65b7dSLisandro Dalcin     PetscInt64 ioff = (PetscInt64)moff; /* We could use MPI_OFFSET datatype (requires MPI 2.2) */
105e8a65b7dSLisandro Dalcin     ierr = MPI_Bcast(&ioff,1,MPIU_INT64,0,PetscObjectComm((PetscObject)viewer));CHKERRQ(ierr);
106e8a65b7dSLisandro Dalcin     vbinary->moff = (MPI_Offset)ioff;
107e8a65b7dSLisandro Dalcin   }
108e8a65b7dSLisandro Dalcin #endif
1095c6c1daeSBarry Smith   PetscFunctionReturn(0);
1105c6c1daeSBarry Smith }
1115c6c1daeSBarry Smith 
1125c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
1135c6c1daeSBarry Smith /*@C
1145c6c1daeSBarry Smith     PetscViewerBinaryGetMPIIOOffset - Gets the current offset that should be passed to MPI_File_set_view()
1155c6c1daeSBarry Smith 
1165c6c1daeSBarry Smith     Not Collective
1175c6c1daeSBarry Smith 
1185c6c1daeSBarry Smith     Input Parameter:
1195c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
1205c6c1daeSBarry Smith 
1215c6c1daeSBarry Smith     Output Parameter:
1225c6c1daeSBarry Smith .    off - the current offset
1235c6c1daeSBarry Smith 
1245c6c1daeSBarry Smith     Level: advanced
1255c6c1daeSBarry Smith 
1265c6c1daeSBarry Smith     Fortran Note:
1275c6c1daeSBarry Smith     This routine is not supported in Fortran.
1285c6c1daeSBarry Smith 
1295c6c1daeSBarry Smith     Use PetscViewerBinaryAddMPIIOOffset() to increase this value after you have written a view.
1305c6c1daeSBarry Smith 
1315c6c1daeSBarry Smith   Concepts: file descriptor^getting
1325c6c1daeSBarry Smith   Concepts: PetscViewerBinary^accessing file descriptor
1335c6c1daeSBarry Smith 
13467918a83SBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetInfoPointer(), PetscViewerBinaryGetUseMPIIO(), PetscViewerBinarySetUseMPIIO(), PetscViewerBinaryAddMPIIOOffset()
1355c6c1daeSBarry Smith @*/
1365c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetMPIIOOffset(PetscViewer viewer,MPI_Offset *off)
1375c6c1daeSBarry Smith {
1385c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
1395c6c1daeSBarry Smith 
1405c6c1daeSBarry Smith   PetscFunctionBegin;
1415c6c1daeSBarry Smith   *off = vbinary->moff;
1425c6c1daeSBarry Smith   PetscFunctionReturn(0);
1435c6c1daeSBarry Smith }
1445c6c1daeSBarry Smith 
1455c6c1daeSBarry Smith /*@C
1465c6c1daeSBarry Smith     PetscViewerBinaryAddMPIIOOffset - Adds to the current offset that should be passed to MPI_File_set_view()
1475c6c1daeSBarry Smith 
1485c6c1daeSBarry Smith     Not Collective
1495c6c1daeSBarry Smith 
1505c6c1daeSBarry Smith     Input Parameters:
1515c6c1daeSBarry Smith +   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
1525c6c1daeSBarry Smith -    off - the addition to the offset
1535c6c1daeSBarry Smith 
1545c6c1daeSBarry Smith     Level: advanced
1555c6c1daeSBarry Smith 
1565c6c1daeSBarry Smith     Fortran Note:
1575c6c1daeSBarry Smith     This routine is not supported in Fortran.
1585c6c1daeSBarry Smith 
1595c6c1daeSBarry Smith     Use PetscViewerBinaryGetMPIIOOffset() to get the value that you should pass to MPI_File_set_view()
1605c6c1daeSBarry Smith 
1615c6c1daeSBarry Smith   Concepts: file descriptor^getting
1625c6c1daeSBarry Smith   Concepts: PetscViewerBinary^accessing file descriptor
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   Concepts: file descriptor^getting
1925c6c1daeSBarry Smith   Concepts: PetscViewerBinary^accessing file descriptor
1935c6c1daeSBarry Smith 
19467918a83SBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer(), PetscViewerBinaryGetUseMPIIO(), PetscViewerBinarySetUseMPIIO(), PetscViewerBinaryGetMPIIOOffset()
1955c6c1daeSBarry Smith @*/
1965c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetMPIIODescriptor(PetscViewer viewer,MPI_File *fdes)
1975c6c1daeSBarry Smith {
19803a1d59fSDave May   PetscErrorCode     ierr;
1995c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
2005c6c1daeSBarry Smith 
2015c6c1daeSBarry Smith   PetscFunctionBegin;
202c98fd787SBarry Smith   ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr);
2035c6c1daeSBarry Smith   *fdes = vbinary->mfdes;
2045c6c1daeSBarry Smith   PetscFunctionReturn(0);
2055c6c1daeSBarry Smith }
2065c6c1daeSBarry Smith 
20781f0254dSBarry Smith static PetscErrorCode PetscViewerBinaryGetUseMPIIO_Binary(PetscViewer viewer,PetscBool  *flg)
208a8aae444SDave May {
209a8aae444SDave May   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
210a8aae444SDave May 
211a8aae444SDave May   PetscFunctionBegin;
212bc196f7cSDave May   *flg = vbinary->usempiio;
213a8aae444SDave May   PetscFunctionReturn(0);
214a8aae444SDave May }
215a8aae444SDave May #endif
216a8aae444SDave May 
217a8aae444SDave May 
2185c6c1daeSBarry Smith /*@C
219bc196f7cSDave May     PetscViewerBinaryGetUseMPIIO - Returns PETSC_TRUE if the binary viewer uses MPI-IO.
2205c6c1daeSBarry Smith 
2215c6c1daeSBarry Smith     Not Collective
2225c6c1daeSBarry Smith 
2235c6c1daeSBarry Smith     Input Parameter:
2245c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
2255c6c1daeSBarry Smith 
2265c6c1daeSBarry Smith     Output Parameter:
227bc196f7cSDave May -   flg - PETSC_TRUE if MPI-IO is being used
2285c6c1daeSBarry Smith 
2295c6c1daeSBarry Smith     Options Database:
2305c6c1daeSBarry Smith     -viewer_binary_mpiio : Flag for using MPI-IO
2315c6c1daeSBarry Smith 
2325c6c1daeSBarry Smith     Level: advanced
2335c6c1daeSBarry Smith 
234bc196f7cSDave May     Note:
235bc196f7cSDave May     If MPI-IO is not available, this function will always return PETSC_FALSE
236bc196f7cSDave May 
2375c6c1daeSBarry Smith     Fortran Note:
2385c6c1daeSBarry Smith     This routine is not supported in Fortran.
2395c6c1daeSBarry Smith 
2405c6c1daeSBarry Smith   Concepts: file descriptor^getting
2415c6c1daeSBarry Smith   Concepts: PetscViewerBinary^accessing file descriptor
2425c6c1daeSBarry Smith 
24367918a83SBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetInfoPointer(), PetscViewerBinarySetUseMPIIO(), PetscViewerBinaryGetMPIIOOffset()
2445c6c1daeSBarry Smith @*/
245bc196f7cSDave May PetscErrorCode PetscViewerBinaryGetUseMPIIO(PetscViewer viewer,PetscBool *flg)
2465c6c1daeSBarry Smith {
247a8aae444SDave May   PetscErrorCode ierr;
2485c6c1daeSBarry Smith 
2495c6c1daeSBarry Smith   PetscFunctionBegin;
250a8aae444SDave May   *flg = PETSC_FALSE;
251bc196f7cSDave May   ierr = PetscTryMethod(viewer,"PetscViewerBinaryGetUseMPIIO_C",(PetscViewer,PetscBool*),(viewer,flg));CHKERRQ(ierr);
2525c6c1daeSBarry Smith   PetscFunctionReturn(0);
2535c6c1daeSBarry Smith }
2545c6c1daeSBarry Smith 
25581f0254dSBarry Smith static PetscErrorCode  PetscViewerBinaryGetFlowControl_Binary(PetscViewer viewer,PetscInt *fc)
2565c6c1daeSBarry Smith {
2575c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
2585c6c1daeSBarry Smith 
2595c6c1daeSBarry Smith   PetscFunctionBegin;
2605c6c1daeSBarry Smith   *fc = vbinary->flowcontrol;
2615c6c1daeSBarry Smith   PetscFunctionReturn(0);
2625c6c1daeSBarry Smith }
2635c6c1daeSBarry Smith 
2645c6c1daeSBarry Smith /*@C
2655c6c1daeSBarry Smith     PetscViewerBinaryGetFlowControl - Returns how many messages are allowed to outstanding at the same time during parallel IO reads/writes
2665c6c1daeSBarry Smith 
2675c6c1daeSBarry Smith     Not Collective
2685c6c1daeSBarry Smith 
2695c6c1daeSBarry Smith     Input Parameter:
2705c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
2715c6c1daeSBarry Smith 
2725c6c1daeSBarry Smith     Output Parameter:
2735c6c1daeSBarry Smith .   fc - the number of messages
2745c6c1daeSBarry Smith 
2755c6c1daeSBarry Smith     Level: advanced
2765c6c1daeSBarry Smith 
2775c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer(), PetscViewerBinarySetFlowControl()
2785c6c1daeSBarry Smith 
2795c6c1daeSBarry Smith @*/
2805c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetFlowControl(PetscViewer viewer,PetscInt *fc)
2815c6c1daeSBarry Smith {
2825c6c1daeSBarry Smith   PetscErrorCode ierr;
2835c6c1daeSBarry Smith 
2845c6c1daeSBarry Smith   PetscFunctionBegin;
285163d334eSBarry Smith   ierr = PetscUseMethod(viewer,"PetscViewerBinaryGetFlowControl_C",(PetscViewer,PetscInt*),(viewer,fc));CHKERRQ(ierr);
2865c6c1daeSBarry Smith   PetscFunctionReturn(0);
2875c6c1daeSBarry Smith }
2885c6c1daeSBarry Smith 
28981f0254dSBarry Smith static PetscErrorCode PetscViewerBinarySetFlowControl_Binary(PetscViewer viewer,PetscInt fc)
2905c6c1daeSBarry Smith {
2915c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
2925c6c1daeSBarry Smith 
2935c6c1daeSBarry Smith   PetscFunctionBegin;
294ce94432eSBarry Smith   if (fc <= 1) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_OUTOFRANGE,"Flow control count must be greater than 1, %D was set",fc);
2955c6c1daeSBarry Smith   vbinary->flowcontrol = fc;
2965c6c1daeSBarry Smith   PetscFunctionReturn(0);
2975c6c1daeSBarry Smith }
2985c6c1daeSBarry Smith 
2995c6c1daeSBarry Smith /*@C
300639ff905SBarry Smith     PetscViewerBinarySetFlowControl - Sets how many messages are allowed to outstanding at the same time during parallel IO reads/writes
3015c6c1daeSBarry Smith 
3025c6c1daeSBarry Smith     Not Collective
3035c6c1daeSBarry Smith 
3045c6c1daeSBarry Smith     Input Parameter:
3055c6c1daeSBarry Smith +   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
306639ff905SBarry Smith -   fc - the number of messages, defaults to 256 if this function was not called
3075c6c1daeSBarry Smith 
3085c6c1daeSBarry Smith     Level: advanced
3095c6c1daeSBarry Smith 
3105c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer(), PetscViewerBinaryGetFlowControl()
3115c6c1daeSBarry Smith 
3125c6c1daeSBarry Smith @*/
3135c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinarySetFlowControl(PetscViewer viewer,PetscInt fc)
3145c6c1daeSBarry Smith {
3155c6c1daeSBarry Smith   PetscErrorCode ierr;
3165c6c1daeSBarry Smith 
3175c6c1daeSBarry Smith   PetscFunctionBegin;
3185c6c1daeSBarry Smith   ierr = PetscUseMethod(viewer,"PetscViewerBinarySetFlowControl_C",(PetscViewer,PetscInt),(viewer,fc));CHKERRQ(ierr);
3195c6c1daeSBarry Smith   PetscFunctionReturn(0);
3205c6c1daeSBarry Smith }
3215c6c1daeSBarry Smith 
3225c6c1daeSBarry Smith /*@C
3235c6c1daeSBarry Smith     PetscViewerBinaryGetDescriptor - Extracts the file descriptor from a PetscViewer.
3245c6c1daeSBarry Smith 
3255872f025SBarry Smith     Collective On PetscViewer
3265c6c1daeSBarry Smith 
3275c6c1daeSBarry Smith     Input Parameter:
3285c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
3295c6c1daeSBarry Smith 
3305c6c1daeSBarry Smith     Output Parameter:
3315c6c1daeSBarry Smith .   fdes - file descriptor
3325c6c1daeSBarry Smith 
3335c6c1daeSBarry Smith     Level: advanced
3345c6c1daeSBarry Smith 
3355c6c1daeSBarry Smith     Notes:
3365c6c1daeSBarry Smith       For writable binary PetscViewers, the descriptor will only be valid for the
3375c6c1daeSBarry Smith     first processor in the communicator that shares the PetscViewer. For readable
3385c6c1daeSBarry Smith     files it will only be valid on nodes that have the file. If node 0 does not
3395c6c1daeSBarry Smith     have the file it generates an error even if another node does have the file.
3405c6c1daeSBarry Smith 
3415c6c1daeSBarry Smith     Fortran Note:
3425c6c1daeSBarry Smith     This routine is not supported in Fortran.
3435c6c1daeSBarry Smith 
34495452b02SPatrick Sanan     Developer Notes:
34595452b02SPatrick Sanan     This must be called on all processes because Dave May changed
3465872f025SBarry Smith     the source code that this may be trigger a PetscViewerSetUp() call if it was not previously triggered.
3475872f025SBarry Smith 
3485872f025SBarry Smith 
3495c6c1daeSBarry Smith   Concepts: file descriptor^getting
3505c6c1daeSBarry Smith   Concepts: PetscViewerBinary^accessing file descriptor
3515c6c1daeSBarry Smith 
3525c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer()
3535c6c1daeSBarry Smith @*/
3545c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetDescriptor(PetscViewer viewer,int *fdes)
3555c6c1daeSBarry Smith {
35603a1d59fSDave May   PetscErrorCode     ierr;
3575c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
3585c6c1daeSBarry Smith 
3595c6c1daeSBarry Smith   PetscFunctionBegin;
360c98fd787SBarry Smith   ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr);
3615c6c1daeSBarry Smith   *fdes = vbinary->fdes;
3625c6c1daeSBarry Smith   PetscFunctionReturn(0);
3635c6c1daeSBarry Smith }
3645c6c1daeSBarry Smith 
3655c6c1daeSBarry Smith /*@
3665c6c1daeSBarry Smith     PetscViewerBinarySkipInfo - Binary file will not have .info file created with it
3675c6c1daeSBarry Smith 
3685c6c1daeSBarry Smith     Not Collective
3695c6c1daeSBarry Smith 
3705c6c1daeSBarry Smith     Input Paramter:
3715c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerCreate()
3725c6c1daeSBarry Smith 
3735c6c1daeSBarry Smith     Options Database Key:
3745c6c1daeSBarry Smith .   -viewer_binary_skip_info
3755c6c1daeSBarry Smith 
3765c6c1daeSBarry Smith     Level: advanced
3775c6c1daeSBarry Smith 
37895452b02SPatrick Sanan     Notes:
37995452b02SPatrick Sanan     This must be called after PetscViewerSetType(). If you use PetscViewerBinaryOpen() then
3805c6c1daeSBarry Smith     you can only skip the info file with the -viewer_binary_skip_info flag. To use the function you must open the
381a2d7db39SDave May     viewer with PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinarySkipInfo().
3825c6c1daeSBarry Smith 
3835c6c1daeSBarry Smith     The .info contains meta information about the data in the binary file, for example the block size if it was
3845c6c1daeSBarry Smith     set for a vector or matrix.
3855c6c1daeSBarry Smith 
3865c6c1daeSBarry Smith    Concepts: PetscViewerBinary^accessing info file
3875c6c1daeSBarry Smith 
3885c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySetSkipOptions(),
389807ea322SDave May           PetscViewerBinaryGetSkipOptions(), PetscViewerBinaryGetSkipInfo()
3905c6c1daeSBarry Smith @*/
3915c6c1daeSBarry Smith PetscErrorCode PetscViewerBinarySkipInfo(PetscViewer viewer)
3925c6c1daeSBarry Smith {
3935c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
3945c6c1daeSBarry Smith 
3955c6c1daeSBarry Smith   PetscFunctionBegin;
3965c6c1daeSBarry Smith   vbinary->skipinfo = PETSC_TRUE;
3975c6c1daeSBarry Smith   PetscFunctionReturn(0);
3985c6c1daeSBarry Smith }
3995c6c1daeSBarry Smith 
40081f0254dSBarry Smith static PetscErrorCode PetscViewerBinarySetSkipInfo_Binary(PetscViewer viewer,PetscBool skip)
401807ea322SDave May {
402807ea322SDave May   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
403807ea322SDave May 
404807ea322SDave May   PetscFunctionBegin;
405807ea322SDave May   vbinary->skipinfo = skip;
406807ea322SDave May   PetscFunctionReturn(0);
407807ea322SDave May }
408807ea322SDave May 
409807ea322SDave May /*@
410807ea322SDave May     PetscViewerBinarySetSkipInfo - Binary file will not have .info file created with it
411807ea322SDave May 
412807ea322SDave May     Not Collective
413807ea322SDave May 
414807ea322SDave May     Input Paramter:
415807ea322SDave May .   viewer - PetscViewer context, obtained from PetscViewerCreate()
416807ea322SDave May 
417807ea322SDave May     Options Database Key:
418807ea322SDave May .   -viewer_binary_skip_info
419807ea322SDave May 
420807ea322SDave May     Level: advanced
421807ea322SDave May 
422807ea322SDave May     Concepts: PetscViewerBinary^accessing info file
423807ea322SDave May 
424807ea322SDave May .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySetSkipOptions(),
425807ea322SDave May           PetscViewerBinaryGetSkipOptions(), PetscViewerBinaryGetSkipInfo()
426807ea322SDave May @*/
427807ea322SDave May PetscErrorCode PetscViewerBinarySetSkipInfo(PetscViewer viewer,PetscBool skip)
428807ea322SDave May {
429807ea322SDave May   PetscErrorCode ierr;
430807ea322SDave May 
431807ea322SDave May   PetscFunctionBegin;
432807ea322SDave May   ierr = PetscUseMethod(viewer,"PetscViewerBinarySetSkipInfo_C",(PetscViewer,PetscBool),(viewer,skip));CHKERRQ(ierr);
433807ea322SDave May   PetscFunctionReturn(0);
434807ea322SDave May }
435807ea322SDave May 
43681f0254dSBarry Smith static PetscErrorCode PetscViewerBinaryGetSkipInfo_Binary(PetscViewer viewer,PetscBool *skip)
437807ea322SDave May {
438807ea322SDave May   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
439807ea322SDave May 
440807ea322SDave May   PetscFunctionBegin;
441807ea322SDave May   *skip  = vbinary->skipinfo;
442807ea322SDave May   PetscFunctionReturn(0);
443807ea322SDave May }
444807ea322SDave May 
445807ea322SDave May /*@
446807ea322SDave May     PetscViewerBinaryGetSkipInfo - check if viewer wrote a .info file
447807ea322SDave May 
448807ea322SDave May     Not Collective
449807ea322SDave May 
450807ea322SDave May     Input Parameter:
451807ea322SDave May .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
452807ea322SDave May 
453807ea322SDave May     Output Parameter:
454807ea322SDave May .   skip - PETSC_TRUE implies the .info file was not generated
455807ea322SDave May 
456807ea322SDave May     Level: advanced
457807ea322SDave May 
45895452b02SPatrick Sanan     Notes:
45995452b02SPatrick Sanan     This must be called after PetscViewerSetType()
460807ea322SDave May 
461807ea322SDave May     Concepts: PetscViewerBinary^accessing info file
462807ea322SDave May 
463807ea322SDave May .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(),
464807ea322SDave May           PetscViewerBinarySetSkipOptions(), PetscViewerBinarySetSkipInfo()
465807ea322SDave May @*/
466807ea322SDave May PetscErrorCode PetscViewerBinaryGetSkipInfo(PetscViewer viewer,PetscBool *skip)
467807ea322SDave May {
468807ea322SDave May   PetscErrorCode ierr;
469807ea322SDave May 
470807ea322SDave May   PetscFunctionBegin;
471807ea322SDave May   ierr = PetscUseMethod(viewer,"PetscViewerBinaryGetSkipInfo_C",(PetscViewer,PetscBool*),(viewer,skip));CHKERRQ(ierr);
472807ea322SDave May   PetscFunctionReturn(0);
473807ea322SDave May }
474807ea322SDave May 
47581f0254dSBarry Smith static PetscErrorCode PetscViewerBinarySetSkipOptions_Binary(PetscViewer viewer,PetscBool skip)
476807ea322SDave May {
477807ea322SDave May   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
478807ea322SDave May 
479807ea322SDave May   PetscFunctionBegin;
480807ea322SDave May   vbinary->skipoptions = skip;
481807ea322SDave May   PetscFunctionReturn(0);
482807ea322SDave May }
483807ea322SDave May 
4845c6c1daeSBarry Smith /*@
4855c6c1daeSBarry Smith     PetscViewerBinarySetSkipOptions - do not use the PETSc options database when loading objects
4865c6c1daeSBarry Smith 
4875c6c1daeSBarry Smith     Not Collective
4885c6c1daeSBarry Smith 
4895c6c1daeSBarry Smith     Input Parameters:
4905c6c1daeSBarry Smith +   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
4915c6c1daeSBarry Smith -   skip - PETSC_TRUE means do not use
4925c6c1daeSBarry Smith 
4935c6c1daeSBarry Smith     Options Database Key:
4945c6c1daeSBarry Smith .   -viewer_binary_skip_options
4955c6c1daeSBarry Smith 
4965c6c1daeSBarry Smith     Level: advanced
4975c6c1daeSBarry Smith 
49895452b02SPatrick Sanan     Notes:
49995452b02SPatrick Sanan     This must be called after PetscViewerSetType()
5005c6c1daeSBarry Smith 
5015c6c1daeSBarry Smith    Concepts: PetscViewerBinary^accessing info file
5025c6c1daeSBarry Smith 
5035c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(),
5045c6c1daeSBarry Smith           PetscViewerBinaryGetSkipOptions()
5055c6c1daeSBarry Smith @*/
5065c6c1daeSBarry Smith PetscErrorCode PetscViewerBinarySetSkipOptions(PetscViewer viewer,PetscBool skip)
5075c6c1daeSBarry Smith {
508807ea322SDave May   PetscErrorCode ierr;
5095c6c1daeSBarry Smith 
5105c6c1daeSBarry Smith   PetscFunctionBegin;
511807ea322SDave May   ierr = PetscUseMethod(viewer,"PetscViewerBinarySetSkipOptions_C",(PetscViewer,PetscBool),(viewer,skip));CHKERRQ(ierr);
512807ea322SDave May   PetscFunctionReturn(0);
513807ea322SDave May }
514807ea322SDave May 
51581f0254dSBarry Smith static PetscErrorCode PetscViewerBinaryGetSkipOptions_Binary(PetscViewer viewer,PetscBool *skip)
516807ea322SDave May {
517807ea322SDave May   PetscViewer_Binary *vbinary;
518807ea322SDave May 
519807ea322SDave May   PetscFunctionBegin;
520807ea322SDave May   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
521807ea322SDave May   vbinary = (PetscViewer_Binary*)viewer->data;
522807ea322SDave May   *skip   = vbinary->skipoptions;
5235c6c1daeSBarry Smith   PetscFunctionReturn(0);
5245c6c1daeSBarry Smith }
5255c6c1daeSBarry Smith 
5265c6c1daeSBarry Smith /*@
5275c6c1daeSBarry Smith     PetscViewerBinaryGetSkipOptions - checks if viewer uses the PETSc options database when loading objects
5285c6c1daeSBarry Smith 
5295c6c1daeSBarry Smith     Not Collective
5305c6c1daeSBarry Smith 
5315c6c1daeSBarry Smith     Input Parameter:
5325c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
5335c6c1daeSBarry Smith 
5345c6c1daeSBarry Smith     Output Parameter:
5355c6c1daeSBarry Smith .   skip - PETSC_TRUE means do not use
5365c6c1daeSBarry Smith 
5375c6c1daeSBarry Smith     Level: advanced
5385c6c1daeSBarry Smith 
53995452b02SPatrick Sanan     Notes:
54095452b02SPatrick Sanan     This must be called after PetscViewerSetType()
5415c6c1daeSBarry Smith 
5425c6c1daeSBarry Smith    Concepts: PetscViewerBinary^accessing info file
5435c6c1daeSBarry Smith 
5445c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(),
5455c6c1daeSBarry Smith           PetscViewerBinarySetSkipOptions()
5465c6c1daeSBarry Smith @*/
5475c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetSkipOptions(PetscViewer viewer,PetscBool *skip)
5485c6c1daeSBarry Smith {
549807ea322SDave May   PetscErrorCode ierr;
5505c6c1daeSBarry Smith 
5515c6c1daeSBarry Smith   PetscFunctionBegin;
552807ea322SDave May   ierr = PetscUseMethod(viewer,"PetscViewerBinaryGetSkipOptions_C",(PetscViewer,PetscBool*),(viewer,skip));CHKERRQ(ierr);
5535c6c1daeSBarry Smith   PetscFunctionReturn(0);
5545c6c1daeSBarry Smith }
5555c6c1daeSBarry Smith 
55681f0254dSBarry Smith static PetscErrorCode PetscViewerBinarySetSkipHeader_Binary(PetscViewer viewer,PetscBool skip)
5575c6c1daeSBarry Smith {
5585c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
5595c6c1daeSBarry Smith 
5605c6c1daeSBarry Smith   PetscFunctionBegin;
5615c6c1daeSBarry Smith   vbinary->skipheader = skip;
5625c6c1daeSBarry Smith   PetscFunctionReturn(0);
5635c6c1daeSBarry Smith }
5645c6c1daeSBarry Smith 
5655c6c1daeSBarry Smith /*@
5665c6c1daeSBarry Smith     PetscViewerBinarySetSkipHeader - do not write a header with size information on output, just raw data
5675c6c1daeSBarry Smith 
5685c6c1daeSBarry Smith     Not Collective
5695c6c1daeSBarry Smith 
5705c6c1daeSBarry Smith     Input Parameters:
5715c6c1daeSBarry Smith +   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
5725c6c1daeSBarry Smith -   skip - PETSC_TRUE means do not write header
5735c6c1daeSBarry Smith 
5745c6c1daeSBarry Smith     Options Database Key:
5755c6c1daeSBarry Smith .   -viewer_binary_skip_header
5765c6c1daeSBarry Smith 
5775c6c1daeSBarry Smith     Level: advanced
5785c6c1daeSBarry Smith 
57995452b02SPatrick Sanan     Notes:
58095452b02SPatrick Sanan     This must be called after PetscViewerSetType()
5815c6c1daeSBarry Smith 
5825c6c1daeSBarry Smith            Can ONLY be called on a binary viewer
5835c6c1daeSBarry Smith 
5845c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(),
5855c6c1daeSBarry Smith           PetscViewerBinaryGetSkipHeader()
5865c6c1daeSBarry Smith @*/
5875c6c1daeSBarry Smith PetscErrorCode PetscViewerBinarySetSkipHeader(PetscViewer viewer,PetscBool skip)
5885c6c1daeSBarry Smith {
5895c6c1daeSBarry Smith   PetscErrorCode ierr;
5905c6c1daeSBarry Smith 
5915c6c1daeSBarry Smith   PetscFunctionBegin;
5925c6c1daeSBarry Smith   ierr = PetscUseMethod(viewer,"PetscViewerBinarySetSkipHeader_C",(PetscViewer,PetscBool),(viewer,skip));CHKERRQ(ierr);
5935c6c1daeSBarry Smith   PetscFunctionReturn(0);
5945c6c1daeSBarry Smith }
5955c6c1daeSBarry Smith 
59681f0254dSBarry Smith static PetscErrorCode PetscViewerBinaryGetSkipHeader_Binary(PetscViewer viewer,PetscBool  *skip)
5975c6c1daeSBarry Smith {
5985c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
5995c6c1daeSBarry Smith 
6005c6c1daeSBarry Smith   PetscFunctionBegin;
6015c6c1daeSBarry Smith   *skip = vbinary->skipheader;
6025c6c1daeSBarry Smith   PetscFunctionReturn(0);
6035c6c1daeSBarry Smith }
6045c6c1daeSBarry Smith 
6055c6c1daeSBarry Smith /*@
6065c6c1daeSBarry Smith     PetscViewerBinaryGetSkipHeader - checks whether to write a header with size information on output, or just raw data
6075c6c1daeSBarry Smith 
6085c6c1daeSBarry Smith     Not Collective
6095c6c1daeSBarry Smith 
6105c6c1daeSBarry Smith     Input Parameter:
6115c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
6125c6c1daeSBarry Smith 
6135c6c1daeSBarry Smith     Output Parameter:
6145c6c1daeSBarry Smith .   skip - PETSC_TRUE means do not write header
6155c6c1daeSBarry Smith 
6165c6c1daeSBarry Smith     Level: advanced
6175c6c1daeSBarry Smith 
61895452b02SPatrick Sanan     Notes:
61995452b02SPatrick Sanan     This must be called after PetscViewerSetType()
6205c6c1daeSBarry Smith 
6215c6c1daeSBarry Smith             Returns false for PETSCSOCKETVIEWER, you cannot skip the header for it.
6225c6c1daeSBarry Smith 
6235c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(),
6245c6c1daeSBarry Smith           PetscViewerBinarySetSkipHeader()
6255c6c1daeSBarry Smith @*/
6265c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetSkipHeader(PetscViewer viewer,PetscBool  *skip)
6275c6c1daeSBarry Smith {
6285c6c1daeSBarry Smith   PetscErrorCode ierr;
6295c6c1daeSBarry Smith 
6305c6c1daeSBarry Smith   PetscFunctionBegin;
6315c6c1daeSBarry Smith   *skip = PETSC_FALSE;
632163d334eSBarry Smith   ierr  = PetscUseMethod(viewer,"PetscViewerBinaryGetSkipHeader_C",(PetscViewer,PetscBool*),(viewer,skip));CHKERRQ(ierr);
6335c6c1daeSBarry Smith   PetscFunctionReturn(0);
6345c6c1daeSBarry Smith }
6355c6c1daeSBarry Smith 
63681f0254dSBarry Smith static PetscErrorCode PetscViewerBinaryGetInfoPointer_Binary(PetscViewer viewer,FILE **file)
6375c6c1daeSBarry Smith {
638f3b211e4SSatish Balay   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
639a261c58fSBarry Smith   PetscErrorCode     ierr;
640a261c58fSBarry Smith   MPI_Comm           comm;
6415c6c1daeSBarry Smith 
6425c6c1daeSBarry Smith   PetscFunctionBegin;
643c98fd787SBarry Smith   ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr);
6445c6c1daeSBarry Smith   *file = vbinary->fdes_info;
645a261c58fSBarry Smith   if (viewer->format == PETSC_VIEWER_BINARY_MATLAB && !vbinary->matlabheaderwritten) {
646a261c58fSBarry Smith     vbinary->matlabheaderwritten = PETSC_TRUE;
647a261c58fSBarry Smith     ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
648da88d4d4SJed Brown     ierr = PetscFPrintf(comm,*file,"#--- begin code written by PetscViewerBinary for MATLAB format ---#\n");CHKERRQ(ierr);
649da88d4d4SJed Brown     ierr = PetscFPrintf(comm,*file,"#$$ Set.filename = '%s';\n",vbinary->filename);CHKERRQ(ierr);
650da88d4d4SJed Brown     ierr = PetscFPrintf(comm,*file,"#$$ fd = PetscOpenFile(Set.filename);\n");CHKERRQ(ierr);
651da88d4d4SJed Brown     ierr = PetscFPrintf(comm,*file,"#--- end code written by PetscViewerBinary for MATLAB format ---#\n\n");CHKERRQ(ierr);
652a261c58fSBarry Smith   }
6535c6c1daeSBarry Smith   PetscFunctionReturn(0);
6545c6c1daeSBarry Smith }
6555c6c1daeSBarry Smith 
6565c6c1daeSBarry Smith /*@C
6575c6c1daeSBarry Smith     PetscViewerBinaryGetInfoPointer - Extracts the file pointer for the ASCII
6585c6c1daeSBarry Smith           info file associated with a binary file.
6595c6c1daeSBarry Smith 
6605c6c1daeSBarry Smith     Not Collective
6615c6c1daeSBarry Smith 
6625c6c1daeSBarry Smith     Input Parameter:
6635c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
6645c6c1daeSBarry Smith 
6655c6c1daeSBarry Smith     Output Parameter:
6660298fd71SBarry Smith .   file - file pointer  Always returns NULL if not a binary viewer
6675c6c1daeSBarry Smith 
6685c6c1daeSBarry Smith     Level: advanced
6695c6c1daeSBarry Smith 
6705c6c1daeSBarry Smith     Notes:
6715c6c1daeSBarry Smith       For writable binary PetscViewers, the descriptor will only be valid for the
6725c6c1daeSBarry Smith     first processor in the communicator that shares the PetscViewer.
6735c6c1daeSBarry Smith 
6745c6c1daeSBarry Smith     Fortran Note:
6755c6c1daeSBarry Smith     This routine is not supported in Fortran.
6765c6c1daeSBarry Smith 
6775c6c1daeSBarry Smith   Concepts: PetscViewerBinary^accessing info file
6785c6c1daeSBarry Smith 
6795c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetDescriptor()
6805c6c1daeSBarry Smith @*/
6815c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetInfoPointer(PetscViewer viewer,FILE **file)
6825c6c1daeSBarry Smith {
6835c6c1daeSBarry Smith   PetscErrorCode ierr;
6845c6c1daeSBarry Smith 
6855c6c1daeSBarry Smith   PetscFunctionBegin;
6860298fd71SBarry Smith   *file = NULL;
6875c6c1daeSBarry Smith   ierr  = PetscTryMethod(viewer,"PetscViewerBinaryGetInfoPointer_C",(PetscViewer,FILE **),(viewer,file));CHKERRQ(ierr);
6885c6c1daeSBarry Smith   PetscFunctionReturn(0);
6895c6c1daeSBarry Smith }
6905c6c1daeSBarry Smith 
6915c6c1daeSBarry Smith static PetscErrorCode PetscViewerFileClose_Binary(PetscViewer v)
6925c6c1daeSBarry Smith {
6935c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data;
6945c6c1daeSBarry Smith   PetscErrorCode     ierr;
6955c6c1daeSBarry Smith   PetscMPIInt        rank;
6965c6c1daeSBarry Smith   int                err;
6975c6c1daeSBarry Smith 
6985c6c1daeSBarry Smith   PetscFunctionBegin;
699ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)v),&rank);CHKERRQ(ierr);
7005c6c1daeSBarry Smith   if ((!rank || vbinary->btype == FILE_MODE_READ) && vbinary->fdes) {
7015c6c1daeSBarry Smith     close(vbinary->fdes);
7025c6c1daeSBarry Smith     if (!rank && vbinary->storecompressed) {
7035c6c1daeSBarry Smith       char par[PETSC_MAX_PATH_LEN],buf[PETSC_MAX_PATH_LEN];
7045c6c1daeSBarry Smith       FILE *fp;
7055c6c1daeSBarry Smith       /* compress the file */
706a126751eSBarry Smith       ierr = PetscStrncpy(par,"gzip -f ",sizeof(par));CHKERRQ(ierr);
707a126751eSBarry Smith       ierr = PetscStrlcat(par,vbinary->ogzfilename ? vbinary->ogzfilename : vbinary->filename,sizeof(par));CHKERRQ(ierr);
708f90597f1SStefano Zampini       ierr = PetscFree(vbinary->ogzfilename);CHKERRQ(ierr);
7095c6c1daeSBarry Smith #if defined(PETSC_HAVE_POPEN)
7100298fd71SBarry Smith       ierr = PetscPOpen(PETSC_COMM_SELF,NULL,par,"r",&fp);CHKERRQ(ierr);
7115c6c1daeSBarry Smith       if (fgets(buf,1024,fp)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error from command %s\n%s",par,buf);
712016831caSBarry Smith       ierr = PetscPClose(PETSC_COMM_SELF,fp);CHKERRQ(ierr);
7135c6c1daeSBarry Smith #else
7145c6c1daeSBarry Smith       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
7155c6c1daeSBarry Smith #endif
7165c6c1daeSBarry Smith     }
7175c6c1daeSBarry Smith   }
7185c6c1daeSBarry Smith   if (vbinary->fdes_info) {
7195c6c1daeSBarry Smith     err = fclose(vbinary->fdes_info);
7205c6c1daeSBarry Smith     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
7215c6c1daeSBarry Smith   }
7225c6c1daeSBarry Smith   PetscFunctionReturn(0);
7235c6c1daeSBarry Smith }
7245c6c1daeSBarry Smith 
725e0385b85SDave May #if defined(PETSC_HAVE_MPIIO)
726e0385b85SDave May static PetscErrorCode PetscViewerFileClose_BinaryMPIIO(PetscViewer v)
727e0385b85SDave May {
728e0385b85SDave May   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data;
729e0385b85SDave May   int                err;
730e0385b85SDave May   PetscErrorCode     ierr;
731e0385b85SDave May 
732e0385b85SDave May   PetscFunctionBegin;
733e8a65b7dSLisandro Dalcin   if (vbinary->mfdes != MPI_FILE_NULL) {
734e0385b85SDave May     ierr = MPI_File_close(&vbinary->mfdes);CHKERRQ(ierr);
735e0385b85SDave May   }
736e8a65b7dSLisandro Dalcin   if (vbinary->mfsub != MPI_FILE_NULL) {
737e8a65b7dSLisandro Dalcin     ierr = MPI_File_close(&vbinary->mfsub);CHKERRQ(ierr);
738e8a65b7dSLisandro Dalcin   }
739e0385b85SDave May   if (vbinary->fdes_info) {
740e0385b85SDave May     err = fclose(vbinary->fdes_info);
741e0385b85SDave May     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
742e0385b85SDave May   }
743e0385b85SDave May   PetscFunctionReturn(0);
744e0385b85SDave May }
745e0385b85SDave May #endif
746e0385b85SDave May 
74781f0254dSBarry Smith static PetscErrorCode PetscViewerDestroy_Binary(PetscViewer v)
7485c6c1daeSBarry Smith {
7495c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data;
7505c6c1daeSBarry Smith   PetscErrorCode     ierr;
7515c6c1daeSBarry Smith 
7525c6c1daeSBarry Smith   PetscFunctionBegin;
753a261c58fSBarry Smith   if (v->format == PETSC_VIEWER_BINARY_MATLAB) {
754a261c58fSBarry Smith     MPI_Comm comm;
755a261c58fSBarry Smith     FILE     *info;
756a261c58fSBarry Smith 
757a261c58fSBarry Smith     ierr = PetscObjectGetComm((PetscObject)v,&comm);CHKERRQ(ierr);
758a261c58fSBarry Smith     ierr = PetscViewerBinaryGetInfoPointer(v,&info);CHKERRQ(ierr);
759da88d4d4SJed Brown     ierr = PetscFPrintf(comm,info,"#--- begin code written by PetscViewerBinary for MATLAB format ---#\n");CHKERRQ(ierr);
760da88d4d4SJed Brown     ierr = PetscFPrintf(comm,info,"#$$ close(fd);\n");CHKERRQ(ierr);
761da88d4d4SJed Brown     ierr = PetscFPrintf(comm,info,"#--- end code written by PetscViewerBinary for MATLAB format ---#\n\n");CHKERRQ(ierr);
762a261c58fSBarry Smith   }
7635c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
764bc196f7cSDave May   if (vbinary->usempiio) {
765e0385b85SDave May     ierr = PetscViewerFileClose_BinaryMPIIO(v);CHKERRQ(ierr);
766e0385b85SDave May   } else {
767e0385b85SDave May #endif
768e0385b85SDave May     ierr = PetscViewerFileClose_Binary(v);CHKERRQ(ierr);
769e0385b85SDave May #if defined(PETSC_HAVE_MPIIO)
7705c6c1daeSBarry Smith   }
7715c6c1daeSBarry Smith #endif
772f90597f1SStefano Zampini   ierr = PetscFree(vbinary->filename);CHKERRQ(ierr);
773f90597f1SStefano Zampini   ierr = PetscFree(vbinary->ogzfilename);CHKERRQ(ierr);
774e0385b85SDave May   ierr = PetscFree(vbinary);CHKERRQ(ierr);
775e0385b85SDave May   PetscFunctionReturn(0);
776e0385b85SDave May }
7775c6c1daeSBarry Smith 
7785c6c1daeSBarry Smith /*@C
7795c6c1daeSBarry Smith    PetscViewerBinaryOpen - Opens a file for binary input/output.
7805c6c1daeSBarry Smith 
7815c6c1daeSBarry Smith    Collective on MPI_Comm
7825c6c1daeSBarry Smith 
7835c6c1daeSBarry Smith    Input Parameters:
7845c6c1daeSBarry Smith +  comm - MPI communicator
7855c6c1daeSBarry Smith .  name - name of file
7865c6c1daeSBarry Smith -  type - type of file
7875c6c1daeSBarry Smith $    FILE_MODE_WRITE - create new file for binary output
7885c6c1daeSBarry Smith $    FILE_MODE_READ - open existing file for binary input
7895c6c1daeSBarry Smith $    FILE_MODE_APPEND - open existing file for binary output
7905c6c1daeSBarry Smith 
7915c6c1daeSBarry Smith    Output Parameter:
7925c6c1daeSBarry Smith .  binv - PetscViewer for binary input/output to use with the specified file
7935c6c1daeSBarry Smith 
7945c6c1daeSBarry Smith     Options Database Keys:
795*63c55180SPatrick Sanan +    -viewer_binary_filename <name> -
796*63c55180SPatrick Sanan .    -viewer_binary_skip_info -
797*63c55180SPatrick Sanan .    -viewer_binary_skip_options -
798*63c55180SPatrick Sanan .    -viewer_binary_skip_header -
799*63c55180SPatrick Sanan -    -viewer_binary_mpiio -
8005c6c1daeSBarry Smith 
8015c6c1daeSBarry Smith    Level: beginner
8025c6c1daeSBarry Smith 
8035c6c1daeSBarry Smith    Note:
8045c6c1daeSBarry Smith    This PetscViewer should be destroyed with PetscViewerDestroy().
8055c6c1daeSBarry Smith 
8065c6c1daeSBarry Smith     For reading files, the filename may begin with ftp:// or http:// and/or
8075c6c1daeSBarry Smith     end with .gz; in this case file is brought over and uncompressed.
8085c6c1daeSBarry Smith 
8095c6c1daeSBarry Smith     For creating files, if the file name ends with .gz it is automatically
8105c6c1daeSBarry Smith     compressed when closed.
8115c6c1daeSBarry Smith 
8125c6c1daeSBarry Smith     For writing files it only opens the file on processor 0 in the communicator.
8135c6c1daeSBarry Smith     For readable files it opens the file on all nodes that have the file. If
8145c6c1daeSBarry Smith     node 0 does not have the file it generates an error even if other nodes
8155c6c1daeSBarry Smith     do have the file.
8165c6c1daeSBarry Smith 
8175c6c1daeSBarry Smith    Concepts: binary files
8185c6c1daeSBarry Smith    Concepts: PetscViewerBinary^creating
8195c6c1daeSBarry Smith    Concepts: gzip
8205c6c1daeSBarry Smith    Concepts: accessing remote file
8215c6c1daeSBarry Smith    Concepts: remote file
8225c6c1daeSBarry Smith 
8236a9046bcSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(),
8245c6c1daeSBarry Smith           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
82567918a83SBarry Smith           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscViewerBinaryRead(), PetscViewerBinarySetUseMPIIO(),
82667918a83SBarry Smith           PetscViewerBinaryGetUseMPIIO(), PetscViewerBinaryGetMPIIOOffset()
8275c6c1daeSBarry Smith @*/
8285c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryOpen(MPI_Comm comm,const char name[],PetscFileMode type,PetscViewer *binv)
8295c6c1daeSBarry Smith {
8305c6c1daeSBarry Smith   PetscErrorCode ierr;
8315c6c1daeSBarry Smith 
8325c6c1daeSBarry Smith   PetscFunctionBegin;
8335c6c1daeSBarry Smith   ierr = PetscViewerCreate(comm,binv);CHKERRQ(ierr);
8345c6c1daeSBarry Smith   ierr = PetscViewerSetType(*binv,PETSCVIEWERBINARY);CHKERRQ(ierr);
8355c6c1daeSBarry Smith   ierr = PetscViewerFileSetMode(*binv,type);CHKERRQ(ierr);
8365c6c1daeSBarry Smith   ierr = PetscViewerFileSetName(*binv,name);CHKERRQ(ierr);
83703a1d59fSDave May   ierr = PetscViewerSetFromOptions(*binv);CHKERRQ(ierr);
8385c6c1daeSBarry Smith   PetscFunctionReturn(0);
8395c6c1daeSBarry Smith }
8405c6c1daeSBarry Smith 
8415c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
842060da220SMatthew G. Knepley static PetscErrorCode PetscViewerBinaryWriteReadMPIIO(PetscViewer viewer,void *data,PetscInt num,PetscInt *count,PetscDataType dtype,PetscBool write)
8435c6c1daeSBarry Smith {
8445c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
8455c6c1daeSBarry Smith   PetscErrorCode     ierr;
846e8a65b7dSLisandro Dalcin   MPI_File           mfdes;
8475c6c1daeSBarry Smith   MPI_Datatype       mdtype;
848e597bc1dSJed Brown   PetscMPIInt        cnt;
8495c6c1daeSBarry Smith   MPI_Status         status;
8505c6c1daeSBarry Smith   MPI_Aint           ul,dsize;
8515c6c1daeSBarry Smith 
8525c6c1daeSBarry Smith   PetscFunctionBegin;
853e8a65b7dSLisandro Dalcin   mfdes = vbinary->mfdes;
854060da220SMatthew G. Knepley   ierr = PetscMPIIntCast(num,&cnt);CHKERRQ(ierr);
8555c6c1daeSBarry Smith   ierr = PetscDataTypeToMPIDataType(dtype,&mdtype);CHKERRQ(ierr);
856e8a65b7dSLisandro Dalcin   ierr = MPI_File_set_view(mfdes,vbinary->moff,mdtype,mdtype,(char*)"native",MPI_INFO_NULL);CHKERRQ(ierr);
8575c6c1daeSBarry Smith   if (write) {
858e8a65b7dSLisandro Dalcin     ierr = MPIU_File_write_all(mfdes,data,cnt,mdtype,&status);CHKERRQ(ierr);
8595c6c1daeSBarry Smith   } else {
860e8a65b7dSLisandro Dalcin     ierr = MPIU_File_read_all(mfdes,data,cnt,mdtype,&status);CHKERRQ(ierr);
8615c6c1daeSBarry Smith   }
8625c6c1daeSBarry Smith   ierr = MPI_Type_get_extent(mdtype,&ul,&dsize);CHKERRQ(ierr);
863a297a907SKarl Rupp 
8645c6c1daeSBarry Smith   vbinary->moff += dsize*cnt;
865060da220SMatthew G. Knepley   if (count) *count = num;
8665c6c1daeSBarry Smith   PetscFunctionReturn(0);
8675c6c1daeSBarry Smith }
8685c6c1daeSBarry Smith #endif
8695c6c1daeSBarry Smith 
8705c6c1daeSBarry Smith /*@C
8715c6c1daeSBarry Smith    PetscViewerBinaryRead - Reads from a binary file, all processors get the same result
8725c6c1daeSBarry Smith 
8735c6c1daeSBarry Smith    Collective on MPI_Comm
8745c6c1daeSBarry Smith 
8755c6c1daeSBarry Smith    Input Parameters:
8765c6c1daeSBarry Smith +  viewer - the binary viewer
877907376e6SBarry Smith .  data - location of the data to be written
878060da220SMatthew G. Knepley .  num - number of items of data to read
879907376e6SBarry Smith -  dtype - type of data to read
8805c6c1daeSBarry Smith 
881f8e4bde8SMatthew G. Knepley    Output Parameters:
8820780a867SBarry 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.
883f8e4bde8SMatthew G. Knepley 
8845c6c1daeSBarry Smith    Level: beginner
8855c6c1daeSBarry Smith 
8865c6c1daeSBarry Smith    Concepts: binary files
8875c6c1daeSBarry Smith 
8880780a867SBarry Smith    Developer Note: Since count is always set to num it is not clear what purpose the output argument count serves.
8890780a867SBarry Smith 
8906a9046bcSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(),
8915c6c1daeSBarry Smith           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
8925c6c1daeSBarry Smith           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead()
8935c6c1daeSBarry Smith @*/
894060da220SMatthew G. Knepley PetscErrorCode PetscViewerBinaryRead(PetscViewer viewer,void *data,PetscInt num,PetscInt *count,PetscDataType dtype)
8955c6c1daeSBarry Smith {
8965c6c1daeSBarry Smith   PetscErrorCode     ierr;
8975c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
8985c6c1daeSBarry Smith 
899c98fd787SBarry Smith   ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr);
9005c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
901bc196f7cSDave May   if (vbinary->usempiio) {
902060da220SMatthew G. Knepley     ierr = PetscViewerBinaryWriteReadMPIIO(viewer,data,num,count,dtype,PETSC_FALSE);CHKERRQ(ierr);
9035c6c1daeSBarry Smith   } else {
9045c6c1daeSBarry Smith #endif
905060da220SMatthew G. Knepley     ierr = PetscBinarySynchronizedRead(PetscObjectComm((PetscObject)viewer),vbinary->fdes,data,num,dtype);CHKERRQ(ierr);
9060780a867SBarry Smith     if (count) *count = num;
9075c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
9085c6c1daeSBarry Smith   }
9095c6c1daeSBarry Smith #endif
9105c6c1daeSBarry Smith   PetscFunctionReturn(0);
9115c6c1daeSBarry Smith }
9125c6c1daeSBarry Smith 
9135c6c1daeSBarry Smith /*@C
9145c6c1daeSBarry Smith    PetscViewerBinaryWrite - writes to a binary file, only from the first process
9155c6c1daeSBarry Smith 
9165c6c1daeSBarry Smith    Collective on MPI_Comm
9175c6c1daeSBarry Smith 
9185c6c1daeSBarry Smith    Input Parameters:
9195c6c1daeSBarry Smith +  viewer - the binary viewer
9205c6c1daeSBarry Smith .  data - location of data
9215824c9d0SPatrick Sanan .  count - number of items of data to write
9225824c9d0SPatrick Sanan .  dtype - type of data to write
9235c6c1daeSBarry Smith -  istemp - data may be overwritten
9245c6c1daeSBarry Smith 
9255c6c1daeSBarry Smith    Level: beginner
9265c6c1daeSBarry Smith 
92795452b02SPatrick Sanan    Notes:
92895452b02SPatrick Sanan     because byte-swapping may be done on the values in data it cannot be declared const
9295c6c1daeSBarry Smith 
9305c6c1daeSBarry Smith    Concepts: binary files
9315c6c1daeSBarry Smith 
9326a9046bcSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(),
9335c6c1daeSBarry Smith           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(), PetscDataType
9345c6c1daeSBarry Smith           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead()
9355c6c1daeSBarry Smith @*/
9365c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryWrite(PetscViewer viewer,void *data,PetscInt count,PetscDataType dtype,PetscBool istemp)
9375c6c1daeSBarry Smith {
9385c6c1daeSBarry Smith   PetscErrorCode     ierr;
9395c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
9405c6c1daeSBarry Smith 
9415c6c1daeSBarry Smith   PetscFunctionBegin;
942c98fd787SBarry Smith   ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr);
9435c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
944bc196f7cSDave May   if (vbinary->usempiio) {
945060da220SMatthew G. Knepley     ierr = PetscViewerBinaryWriteReadMPIIO(viewer,data,count,NULL,dtype,PETSC_TRUE);CHKERRQ(ierr);
9465c6c1daeSBarry Smith   } else {
9475c6c1daeSBarry Smith #endif
948ce94432eSBarry Smith     ierr = PetscBinarySynchronizedWrite(PetscObjectComm((PetscObject)viewer),vbinary->fdes,data,count,dtype,istemp);CHKERRQ(ierr);
9495c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
9505c6c1daeSBarry Smith   }
9515c6c1daeSBarry Smith #endif
9525c6c1daeSBarry Smith   PetscFunctionReturn(0);
9535c6c1daeSBarry Smith }
9545c6c1daeSBarry Smith 
9555c6c1daeSBarry Smith /*@C
9565c6c1daeSBarry Smith    PetscViewerBinaryWriteStringArray - writes to a binary file, only from the first process an array of strings
9575c6c1daeSBarry Smith 
9585c6c1daeSBarry Smith    Collective on MPI_Comm
9595c6c1daeSBarry Smith 
9605c6c1daeSBarry Smith    Input Parameters:
9615c6c1daeSBarry Smith +  viewer - the binary viewer
9625c6c1daeSBarry Smith -  data - location of the array of strings
9635c6c1daeSBarry Smith 
9645c6c1daeSBarry Smith 
9655c6c1daeSBarry Smith    Level: intermediate
9665c6c1daeSBarry Smith 
9675c6c1daeSBarry Smith    Concepts: binary files
9685c6c1daeSBarry Smith 
96995452b02SPatrick Sanan     Notes:
97095452b02SPatrick Sanan     array of strings is null terminated
9715c6c1daeSBarry Smith 
9726a9046bcSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(),
9735c6c1daeSBarry Smith           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
9745c6c1daeSBarry Smith           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead()
9755c6c1daeSBarry Smith @*/
97678fbdcc8SBarry Smith PetscErrorCode PetscViewerBinaryWriteStringArray(PetscViewer viewer,const char * const *data)
9775c6c1daeSBarry Smith {
9785c6c1daeSBarry Smith   PetscErrorCode ierr;
9795c6c1daeSBarry Smith   PetscInt       i,n = 0,*sizes;
9805c6c1daeSBarry Smith 
981c98fd787SBarry Smith   ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr);
9825c6c1daeSBarry Smith   /* count number of strings */
9835c6c1daeSBarry Smith   while (data[n++]) ;
9845c6c1daeSBarry Smith   n--;
985854ce69bSBarry Smith   ierr     = PetscMalloc1(n+1,&sizes);CHKERRQ(ierr);
9865c6c1daeSBarry Smith   sizes[0] = n;
9875c6c1daeSBarry Smith   for (i=0; i<n; i++) {
9885c6c1daeSBarry Smith     size_t tmp;
9895c6c1daeSBarry Smith     ierr       = PetscStrlen(data[i],&tmp);CHKERRQ(ierr);
9905c6c1daeSBarry Smith     sizes[i+1] = tmp + 1;   /* size includes space for the null terminator */
9915c6c1daeSBarry Smith   }
9925c6c1daeSBarry Smith   ierr = PetscViewerBinaryWrite(viewer,sizes,n+1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
9935c6c1daeSBarry Smith   for (i=0; i<n; i++) {
99478fbdcc8SBarry Smith     ierr = PetscViewerBinaryWrite(viewer,(void*)data[i],sizes[i+1],PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
9955c6c1daeSBarry Smith   }
9965c6c1daeSBarry Smith   ierr = PetscFree(sizes);CHKERRQ(ierr);
9975c6c1daeSBarry Smith   PetscFunctionReturn(0);
9985c6c1daeSBarry Smith }
9995c6c1daeSBarry Smith 
10005c6c1daeSBarry Smith /*@C
10015c6c1daeSBarry Smith    PetscViewerBinaryReadStringArray - reads a binary file an array of strings
10025c6c1daeSBarry Smith 
10035c6c1daeSBarry Smith    Collective on MPI_Comm
10045c6c1daeSBarry Smith 
10055c6c1daeSBarry Smith    Input Parameter:
10065c6c1daeSBarry Smith .  viewer - the binary viewer
10075c6c1daeSBarry Smith 
10085c6c1daeSBarry Smith    Output Parameter:
10095c6c1daeSBarry Smith .  data - location of the array of strings
10105c6c1daeSBarry Smith 
10115c6c1daeSBarry Smith    Level: intermediate
10125c6c1daeSBarry Smith 
10135c6c1daeSBarry Smith    Concepts: binary files
10145c6c1daeSBarry Smith 
101595452b02SPatrick Sanan     Notes:
101695452b02SPatrick Sanan     array of strings is null terminated
10175c6c1daeSBarry Smith 
10186a9046bcSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(),
10195c6c1daeSBarry Smith           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
10205c6c1daeSBarry Smith           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead()
10215c6c1daeSBarry Smith @*/
10225c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryReadStringArray(PetscViewer viewer,char ***data)
10235c6c1daeSBarry Smith {
10245c6c1daeSBarry Smith   PetscErrorCode ierr;
1025060da220SMatthew G. Knepley   PetscInt       i,n,*sizes,N = 0;
10265c6c1daeSBarry Smith 
1027c98fd787SBarry Smith   ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr);
10285c6c1daeSBarry Smith   /* count number of strings */
1029060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,&n,1,NULL,PETSC_INT);CHKERRQ(ierr);
1030785e854fSJed Brown   ierr = PetscMalloc1(n,&sizes);CHKERRQ(ierr);
1031060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,sizes,n,NULL,PETSC_INT);CHKERRQ(ierr);
1032a297a907SKarl Rupp   for (i=0; i<n; i++) N += sizes[i];
1033854ce69bSBarry Smith   ierr = PetscMalloc((n+1)*sizeof(char*) + N*sizeof(char),data);CHKERRQ(ierr);
10345c6c1daeSBarry Smith   (*data)[0] = (char*)((*data) + n + 1);
1035a297a907SKarl Rupp   for (i=1; i<n; i++) (*data)[i] = (*data)[i-1] + sizes[i-1];
1036060da220SMatthew G. Knepley   ierr = PetscViewerBinaryRead(viewer,(*data)[0],N,NULL,PETSC_CHAR);CHKERRQ(ierr);
1037a297a907SKarl Rupp 
10385c6c1daeSBarry Smith   (*data)[n] = 0;
1039a297a907SKarl Rupp 
10405c6c1daeSBarry Smith   ierr = PetscFree(sizes);CHKERRQ(ierr);
10415c6c1daeSBarry Smith   PetscFunctionReturn(0);
10425c6c1daeSBarry Smith }
10435c6c1daeSBarry Smith 
104481f0254dSBarry Smith static PetscErrorCode PetscViewerFileGetName_Binary(PetscViewer viewer,const char **name)
10455c6c1daeSBarry Smith {
10465c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
10475c6c1daeSBarry Smith 
10485c6c1daeSBarry Smith   PetscFunctionBegin;
10495c6c1daeSBarry Smith   *name = vbinary->filename;
10505c6c1daeSBarry Smith   PetscFunctionReturn(0);
10515c6c1daeSBarry Smith }
10525c6c1daeSBarry Smith 
10535c6c1daeSBarry Smith /*@C
10545c6c1daeSBarry Smith      PetscViewerFileGetMode - Gets the type of file to be open
10555c6c1daeSBarry Smith 
10565c6c1daeSBarry Smith     Not Collective
10575c6c1daeSBarry Smith 
10585c6c1daeSBarry Smith   Input Parameter:
1059232ac771SBarry Smith .  viewer - the PetscViewer; must be a PETSCVIEWERBINARY, PETSCVIEWERMATLAB, PETSCVIEWERHDF5, or PETSCVIEWERASCII  PetscViewer
10605c6c1daeSBarry Smith 
10615c6c1daeSBarry Smith   Output Parameter:
10625c6c1daeSBarry Smith .  type - type of file
10635c6c1daeSBarry Smith $    FILE_MODE_WRITE - create new file for binary output
10645c6c1daeSBarry Smith $    FILE_MODE_READ - open existing file for binary input
10655c6c1daeSBarry Smith $    FILE_MODE_APPEND - open existing file for binary output
10665c6c1daeSBarry Smith 
10675c6c1daeSBarry Smith   Level: advanced
10685c6c1daeSBarry Smith 
10695c6c1daeSBarry Smith .seealso: PetscViewerFileSetMode(), PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinaryOpen()
10705c6c1daeSBarry Smith 
10715c6c1daeSBarry Smith @*/
10725c6c1daeSBarry Smith PetscErrorCode PetscViewerFileGetMode(PetscViewer viewer,PetscFileMode *type)
10735c6c1daeSBarry Smith {
10745c6c1daeSBarry Smith   PetscErrorCode ierr;
10755c6c1daeSBarry Smith 
10765c6c1daeSBarry Smith   PetscFunctionBegin;
10775c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
10785c6c1daeSBarry Smith   PetscValidPointer(type,2);
10795c6c1daeSBarry Smith   ierr = PetscUseMethod(viewer,"PetscViewerFileGetMode_C",(PetscViewer,PetscFileMode*),(viewer,type));CHKERRQ(ierr);
10805c6c1daeSBarry Smith   PetscFunctionReturn(0);
10815c6c1daeSBarry Smith }
10825c6c1daeSBarry Smith 
10835c6c1daeSBarry Smith /*@
1084bc196f7cSDave May     PetscViewerBinarySetUseMPIIO - Sets a binary viewer to use MPI-IO for reading/writing. Must be called
10855c6c1daeSBarry Smith         before PetscViewerFileSetName()
10865c6c1daeSBarry Smith 
10875c6c1daeSBarry Smith     Logically Collective on PetscViewer
10885c6c1daeSBarry Smith 
10895c6c1daeSBarry Smith     Input Parameters:
1090bc196f7cSDave May +   viewer - the PetscViewer; must be a binary
1091bc196f7cSDave May -   flg - PETSC_TRUE means MPI-IO will be used
10925c6c1daeSBarry Smith 
10935c6c1daeSBarry Smith     Options Database:
10945c6c1daeSBarry Smith     -viewer_binary_mpiio : Flag for using MPI-IO
10955c6c1daeSBarry Smith 
10965c6c1daeSBarry Smith     Level: advanced
10975c6c1daeSBarry Smith 
1098bc196f7cSDave May .seealso: PetscViewerFileSetMode(), PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinaryOpen(),
1099bc196f7cSDave May           PetscViewerBinaryGetUseMPIIO()
11005c6c1daeSBarry Smith 
11015c6c1daeSBarry Smith @*/
1102bc196f7cSDave May PetscErrorCode PetscViewerBinarySetUseMPIIO(PetscViewer viewer,PetscBool flg)
11035c6c1daeSBarry Smith {
11045c6c1daeSBarry Smith   PetscErrorCode ierr;
11055c6c1daeSBarry Smith 
11065c6c1daeSBarry Smith   PetscFunctionBegin;
11075c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
1108bc196f7cSDave May   ierr = PetscTryMethod(viewer,"PetscViewerBinarySetUseMPIIO_C",(PetscViewer,PetscBool),(viewer,flg));CHKERRQ(ierr);
11095c6c1daeSBarry Smith   PetscFunctionReturn(0);
11105c6c1daeSBarry Smith }
11115c6c1daeSBarry Smith 
11125c6c1daeSBarry Smith /*@C
11135c6c1daeSBarry Smith      PetscViewerFileSetMode - Sets the type of file to be open
11145c6c1daeSBarry Smith 
11155c6c1daeSBarry Smith     Logically Collective on PetscViewer
11165c6c1daeSBarry Smith 
11175c6c1daeSBarry Smith   Input Parameters:
1118232ac771SBarry Smith +  viewer - the PetscViewer; must be a a PETSCVIEWERBINARY, PETSCVIEWERMATLAB, PETSCVIEWERHDF5, or PETSCVIEWERASCII  PetscViewer
11195c6c1daeSBarry Smith -  type - type of file
1120f8859db6SBarry Smith $    FILE_MODE_WRITE - create new file for output
1121f8859db6SBarry Smith $    FILE_MODE_READ - open existing file for input
1122f8859db6SBarry Smith $    FILE_MODE_APPEND - open existing file for output
11235c6c1daeSBarry Smith 
11245c6c1daeSBarry Smith   Level: advanced
11255c6c1daeSBarry Smith 
11265c6c1daeSBarry Smith .seealso: PetscViewerFileSetMode(), PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinaryOpen()
11275c6c1daeSBarry Smith 
11285c6c1daeSBarry Smith @*/
11295c6c1daeSBarry Smith PetscErrorCode PetscViewerFileSetMode(PetscViewer viewer,PetscFileMode type)
11305c6c1daeSBarry Smith {
11315c6c1daeSBarry Smith   PetscErrorCode ierr;
11325c6c1daeSBarry Smith 
11335c6c1daeSBarry Smith   PetscFunctionBegin;
11345c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
11355c6c1daeSBarry Smith   PetscValidLogicalCollectiveEnum(viewer,type,2);
11365c6c1daeSBarry Smith   ierr = PetscTryMethod(viewer,"PetscViewerFileSetMode_C",(PetscViewer,PetscFileMode),(viewer,type));CHKERRQ(ierr);
11375c6c1daeSBarry Smith   PetscFunctionReturn(0);
11385c6c1daeSBarry Smith }
11395c6c1daeSBarry Smith 
114081f0254dSBarry Smith static PetscErrorCode PetscViewerFileGetMode_Binary(PetscViewer viewer,PetscFileMode *type)
11415c6c1daeSBarry Smith {
11425c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
11435c6c1daeSBarry Smith 
11445c6c1daeSBarry Smith   PetscFunctionBegin;
11455c6c1daeSBarry Smith   *type = vbinary->btype;
11465c6c1daeSBarry Smith   PetscFunctionReturn(0);
11475c6c1daeSBarry Smith }
11485c6c1daeSBarry Smith 
114981f0254dSBarry Smith static PetscErrorCode PetscViewerFileSetMode_Binary(PetscViewer viewer,PetscFileMode type)
11505c6c1daeSBarry Smith {
11515c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
11525c6c1daeSBarry Smith 
11535c6c1daeSBarry Smith   PetscFunctionBegin;
11545c6c1daeSBarry Smith   vbinary->btype = type;
11555c6c1daeSBarry Smith   PetscFunctionReturn(0);
11565c6c1daeSBarry Smith }
11575c6c1daeSBarry Smith 
115881f0254dSBarry Smith static PetscErrorCode PetscViewerFileSetName_Binary(PetscViewer viewer,const char name[])
1159e0385b85SDave May {
1160e0385b85SDave May   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
1161e0385b85SDave May   PetscErrorCode     ierr;
1162e0385b85SDave May 
1163e0385b85SDave May   PetscFunctionBegin;
1164f90597f1SStefano Zampini   if (vbinary->filename) {
1165f90597f1SStefano Zampini     /* gzip can be run after the file with the previous filename has been closed */
1166f90597f1SStefano Zampini     ierr = PetscFree(vbinary->ogzfilename);CHKERRQ(ierr);
1167f90597f1SStefano Zampini     ierr = PetscStrallocpy(vbinary->filename,&vbinary->ogzfilename);CHKERRQ(ierr);
1168f90597f1SStefano Zampini     ierr = PetscFree(vbinary->filename);CHKERRQ(ierr);
1169f90597f1SStefano Zampini     viewer->setupcalled = PETSC_FALSE;
1170f90597f1SStefano Zampini   }
1171e0385b85SDave May   ierr = PetscStrallocpy(name,&vbinary->filename);CHKERRQ(ierr);
1172e0385b85SDave May   PetscFunctionReturn(0);
1173e0385b85SDave May }
11745c6c1daeSBarry Smith /*
11755c6c1daeSBarry Smith         Actually opens the file
11765c6c1daeSBarry Smith */
1177e0877f53SBarry Smith static PetscErrorCode PetscViewerFileSetUp_Binary(PetscViewer viewer)
11785c6c1daeSBarry Smith {
11795c6c1daeSBarry Smith   PetscMPIInt        rank;
11805c6c1daeSBarry Smith   PetscErrorCode     ierr;
11815c6c1daeSBarry Smith   size_t             len;
11825c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
11835c6c1daeSBarry Smith   const char         *fname;
11842259a519SDave May   char               bname[PETSC_MAX_PATH_LEN],*gz;
11855c6c1daeSBarry Smith   PetscBool          found;
11865c6c1daeSBarry Smith   PetscFileMode      type = vbinary->btype;
11875c6c1daeSBarry Smith 
11885c6c1daeSBarry Smith   PetscFunctionBegin;
1189e0385b85SDave May   if (type == (PetscFileMode) -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetMode()");
11902259a519SDave May   if (!vbinary->filename) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetName()");
11915c6c1daeSBarry Smith   ierr = PetscViewerFileClose_Binary(viewer);CHKERRQ(ierr);
11925c6c1daeSBarry Smith 
1193ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
11945c6c1daeSBarry Smith 
11955c6c1daeSBarry Smith   /* if ends in .gz strip that off and note user wants file compressed */
11965c6c1daeSBarry Smith   vbinary->storecompressed = PETSC_FALSE;
11975c6c1daeSBarry Smith   if (!rank && type == FILE_MODE_WRITE) {
11985c6c1daeSBarry Smith     /* remove .gz if it ends library name */
11995c6c1daeSBarry Smith     ierr = PetscStrstr(vbinary->filename,".gz",&gz);CHKERRQ(ierr);
12005c6c1daeSBarry Smith     if (gz) {
12015c6c1daeSBarry Smith       ierr = PetscStrlen(gz,&len);CHKERRQ(ierr);
12025c6c1daeSBarry Smith       if (len == 3) {
12035c6c1daeSBarry Smith         *gz = 0;
12045c6c1daeSBarry Smith         vbinary->storecompressed = PETSC_TRUE;
12055c6c1daeSBarry Smith       }
12065c6c1daeSBarry Smith     }
12075c6c1daeSBarry Smith   }
12085c6c1daeSBarry Smith 
12095c6c1daeSBarry Smith   /* only first processor opens file if writeable */
12105c6c1daeSBarry Smith   if (!rank || type == FILE_MODE_READ) {
12115c6c1daeSBarry Smith 
12125c6c1daeSBarry Smith     if (type == FILE_MODE_READ) {
12135c6c1daeSBarry Smith       /* possibly get the file from remote site or compressed file */
1214ce94432eSBarry Smith       ierr  = PetscFileRetrieve(PetscObjectComm((PetscObject)viewer),vbinary->filename,bname,PETSC_MAX_PATH_LEN,&found);CHKERRQ(ierr);
12155c6c1daeSBarry Smith       fname = bname;
121641c4be4aSVaclav 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 */
121741c4be4aSVaclav Hapla       if (!found) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_FILE_OPEN,"Cannot locate file: %s on node zero",vbinary->filename);
1218a297a907SKarl Rupp     } else fname = vbinary->filename;
121955819941SStefano Zampini     if (type == FILE_MODE_APPEND) { /* check if asked to append to a non-existing file */
122055819941SStefano Zampini       ierr = PetscTestFile(fname,'\0',&found);CHKERRQ(ierr);
122155819941SStefano Zampini     }
12225c6c1daeSBarry Smith 
12235c6c1daeSBarry Smith #if defined(PETSC_HAVE_O_BINARY)
122455819941SStefano Zampini     if (type == FILE_MODE_WRITE || (type == FILE_MODE_APPEND && !found) ) {
12255c6c1daeSBarry 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);
12265c6c1daeSBarry Smith     } else if (type == FILE_MODE_READ && fname) {
12275c6c1daeSBarry 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);
12285c6c1daeSBarry Smith     } else if (type == FILE_MODE_APPEND) {
12295c6c1daeSBarry 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);
12305c6c1daeSBarry Smith     } else if (fname) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown file type");
12315c6c1daeSBarry Smith #else
123255819941SStefano Zampini     if (type == FILE_MODE_WRITE || (type == FILE_MODE_APPEND && !found) ) {
12335c6c1daeSBarry Smith       if ((vbinary->fdes = creat(fname,0666)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot create file %s for writing",fname);
12345c6c1daeSBarry Smith     } else if (type == FILE_MODE_READ && fname) {
12355c6c1daeSBarry 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);
12365c6c1daeSBarry Smith     } else if (type == FILE_MODE_APPEND) {
12375c6c1daeSBarry 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);
12385c6c1daeSBarry Smith     } else if (fname) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown file type");
12395c6c1daeSBarry Smith #endif
12405c6c1daeSBarry Smith   } else vbinary->fdes = -1;
12415c6c1daeSBarry Smith 
12425c6c1daeSBarry Smith   /*
12435c6c1daeSBarry Smith       try to open info file: all processors open this file if read only
12445c6c1daeSBarry Smith   */
12455c6c1daeSBarry Smith   if (!vbinary->skipinfo && (!rank || type == FILE_MODE_READ)) {
12465c6c1daeSBarry Smith     char infoname[PETSC_MAX_PATH_LEN],iname[PETSC_MAX_PATH_LEN];
12475c6c1daeSBarry Smith 
1248a126751eSBarry Smith     ierr = PetscStrncpy(infoname,vbinary->filename,sizeof(infoname));CHKERRQ(ierr);
12495c6c1daeSBarry Smith     /* remove .gz if it ends library name */
12505c6c1daeSBarry Smith     ierr = PetscStrstr(infoname,".gz",&gz);CHKERRQ(ierr);
12515c6c1daeSBarry Smith     if (gz) {
12525c6c1daeSBarry Smith       ierr = PetscStrlen(gz,&len);CHKERRQ(ierr);
1253a297a907SKarl Rupp       if (len == 3) *gz = 0;
12545c6c1daeSBarry Smith     }
12555c6c1daeSBarry Smith 
1256a126751eSBarry Smith     ierr = PetscStrlcat(infoname,".info",sizeof(infoname));CHKERRQ(ierr);
12575c6c1daeSBarry Smith     ierr = PetscFixFilename(infoname,iname);CHKERRQ(ierr);
12585c6c1daeSBarry Smith     if (type == FILE_MODE_READ) {
1259ce94432eSBarry Smith       ierr = PetscFileRetrieve(PetscObjectComm((PetscObject)viewer),iname,infoname,PETSC_MAX_PATH_LEN,&found);CHKERRQ(ierr);
12601f5e1f59SVaclav Hapla       if (found) {
1261c5929fdfSBarry Smith         ierr = PetscOptionsInsertFile(PetscObjectComm((PetscObject)viewer),((PetscObject)viewer)->options,infoname,PETSC_FALSE);CHKERRQ(ierr);
12621f5e1f59SVaclav Hapla       }
12635c6c1daeSBarry Smith     } else {
12645c6c1daeSBarry Smith       vbinary->fdes_info = fopen(infoname,"w");
12655c6c1daeSBarry Smith       if (!vbinary->fdes_info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open .info file %s for writing",infoname);
12665c6c1daeSBarry Smith     }
12675c6c1daeSBarry Smith   }
12685c6c1daeSBarry Smith #if defined(PETSC_USE_LOG)
1269e0385b85SDave May   PetscLogObjectState((PetscObject)viewer,"File: %s",vbinary->filename);
12705c6c1daeSBarry Smith #endif
12715c6c1daeSBarry Smith   PetscFunctionReturn(0);
12725c6c1daeSBarry Smith }
12735c6c1daeSBarry Smith 
12745c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
1275e0385b85SDave May static PetscErrorCode PetscViewerFileSetUp_BinaryMPIIO(PetscViewer viewer)
12765c6c1daeSBarry Smith {
12775c6c1daeSBarry Smith   PetscMPIInt        rank;
12785c6c1daeSBarry Smith   PetscErrorCode     ierr;
12795c6c1daeSBarry Smith   size_t             len;
12805c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
12812259a519SDave May   char               *gz;
12825c6c1daeSBarry Smith   PetscBool          found;
12835c6c1daeSBarry Smith   PetscFileMode      type = vbinary->btype;
1284e8a65b7dSLisandro Dalcin   int                amode;
12855c6c1daeSBarry Smith 
12865c6c1daeSBarry Smith   PetscFunctionBegin;
1287e0385b85SDave May   if (type == (PetscFileMode) -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetMode()");
12882259a519SDave May   if (!vbinary->filename) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetName()");
1289e0385b85SDave May   ierr = PetscViewerFileClose_BinaryMPIIO(viewer);CHKERRQ(ierr);
12905c6c1daeSBarry Smith 
1291a297a907SKarl Rupp   vbinary->storecompressed = PETSC_FALSE;
12925c6c1daeSBarry Smith 
1293e8a65b7dSLisandro Dalcin   switch (type) {
1294e8a65b7dSLisandro Dalcin   case FILE_MODE_READ:  amode = MPI_MODE_RDONLY; break;
1295e8a65b7dSLisandro Dalcin   case FILE_MODE_WRITE: amode = MPI_MODE_WRONLY | MPI_MODE_CREATE; break;
1296e8a65b7dSLisandro Dalcin   default: SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Unsupported file mode %s",PetscFileModes[type]);
12975c6c1daeSBarry Smith   }
1298e8a65b7dSLisandro Dalcin   ierr = MPI_File_open(PetscObjectComm((PetscObject)viewer),vbinary->filename,amode,MPI_INFO_NULL,&vbinary->mfdes);CHKERRQ(ierr);
12995c6c1daeSBarry Smith 
13005c6c1daeSBarry Smith   /*
13015c6c1daeSBarry Smith       try to open info file: all processors open this file if read only
13025c6c1daeSBarry Smith 
1303bebe2cf6SSatish Balay       Below is identical code to the code for Binary above, should be put in separate routine
13045c6c1daeSBarry Smith   */
1305e8a65b7dSLisandro Dalcin   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
13065c6c1daeSBarry Smith   if (!vbinary->skipinfo && (!rank || type == FILE_MODE_READ)) {
13075c6c1daeSBarry Smith     char infoname[PETSC_MAX_PATH_LEN],iname[PETSC_MAX_PATH_LEN];
13085c6c1daeSBarry Smith 
1309a126751eSBarry Smith     ierr = PetscStrncpy(infoname,vbinary->filename,sizeof(infoname));CHKERRQ(ierr);
13105c6c1daeSBarry Smith     /* remove .gz if it ends library name */
13115c6c1daeSBarry Smith     ierr = PetscStrstr(infoname,".gz",&gz);CHKERRQ(ierr);
13125c6c1daeSBarry Smith     if (gz) {
13135c6c1daeSBarry Smith       ierr = PetscStrlen(gz,&len);CHKERRQ(ierr);
1314a297a907SKarl Rupp       if (len == 3) *gz = 0;
13155c6c1daeSBarry Smith     }
13165c6c1daeSBarry Smith 
1317a126751eSBarry Smith     ierr = PetscStrlcat(infoname,".info",sizeof(infoname));CHKERRQ(ierr);
13185c6c1daeSBarry Smith     ierr = PetscFixFilename(infoname,iname);CHKERRQ(ierr);
13195c6c1daeSBarry Smith     if (type == FILE_MODE_READ) {
1320ce94432eSBarry Smith       ierr = PetscFileRetrieve(PetscObjectComm((PetscObject)viewer),iname,infoname,PETSC_MAX_PATH_LEN,&found);CHKERRQ(ierr);
1321c5929fdfSBarry Smith       ierr = PetscOptionsInsertFile(PetscObjectComm((PetscObject)viewer),((PetscObject)viewer)->options,infoname,PETSC_FALSE);CHKERRQ(ierr);
13225c6c1daeSBarry Smith     } else {
13235c6c1daeSBarry Smith       vbinary->fdes_info = fopen(infoname,"w");
13245c6c1daeSBarry Smith       if (!vbinary->fdes_info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open .info file %s for writing",infoname);
13255c6c1daeSBarry Smith     }
13265c6c1daeSBarry Smith   }
13275c6c1daeSBarry Smith #if defined(PETSC_USE_LOG)
1328e0385b85SDave May   PetscLogObjectState((PetscObject)viewer,"File: %s",vbinary->filename);
13295c6c1daeSBarry Smith #endif
13305c6c1daeSBarry Smith   PetscFunctionReturn(0);
13315c6c1daeSBarry Smith }
13325c6c1daeSBarry Smith 
133381f0254dSBarry Smith static PetscErrorCode PetscViewerBinarySetUseMPIIO_Binary(PetscViewer viewer,PetscBool flg)
13345c6c1daeSBarry Smith {
13355c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
13365c6c1daeSBarry Smith   PetscFunctionBegin;
1337bc196f7cSDave May   vbinary->usempiio = flg;
13385c6c1daeSBarry Smith   PetscFunctionReturn(0);
13395c6c1daeSBarry Smith }
13405c6c1daeSBarry Smith #endif
13415c6c1daeSBarry Smith 
134281f0254dSBarry Smith static PetscErrorCode PetscViewerView_Binary(PetscViewer v,PetscViewer viewer)
13432bf49c77SBarry Smith {
13442bf49c77SBarry Smith   PetscErrorCode     ierr;
13452bf49c77SBarry Smith   PetscViewer_Binary *binary = (PetscViewer_Binary*)v->data;
13462bf49c77SBarry Smith 
13472bf49c77SBarry Smith   PetscFunctionBegin;
13482bf49c77SBarry Smith   if (binary->filename) {
13492bf49c77SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"Filename: %s\n",binary->filename);CHKERRQ(ierr);
13502bf49c77SBarry Smith   }
13512bf49c77SBarry Smith   PetscFunctionReturn(0);
13522bf49c77SBarry Smith }
13532bf49c77SBarry Smith 
135403a1d59fSDave May static PetscErrorCode PetscViewerSetUp_Binary(PetscViewer v)
135503a1d59fSDave May {
135603a1d59fSDave May   PetscErrorCode     ierr;
135703a1d59fSDave May   PetscViewer_Binary *binary = (PetscViewer_Binary*)v->data;
1358e0385b85SDave May 
135903a1d59fSDave May   PetscFunctionBegin;
1360da07bb01SDave May   if (!binary->setfromoptionscalled) { ierr = PetscViewerSetFromOptions(v);CHKERRQ(ierr); }
136103a1d59fSDave May 
1362e0385b85SDave May #if defined(PETSC_HAVE_MPIIO)
1363bc196f7cSDave May   if (binary->usempiio) {
1364e0385b85SDave May     ierr = PetscViewerFileSetUp_BinaryMPIIO(v);CHKERRQ(ierr);
1365e0385b85SDave May   } else {
1366e0385b85SDave May #endif
1367e0385b85SDave May     ierr = PetscViewerFileSetUp_Binary(v);CHKERRQ(ierr);
1368e0385b85SDave May #if defined(PETSC_HAVE_MPIIO)
1369e0385b85SDave May   }
1370e0385b85SDave May #endif
137103a1d59fSDave May   PetscFunctionReturn(0);
137203a1d59fSDave May }
137303a1d59fSDave May 
13744416b707SBarry Smith static PetscErrorCode PetscViewerSetFromOptions_Binary(PetscOptionItems *PetscOptionsObject,PetscViewer v)
137503a1d59fSDave May {
137603a1d59fSDave May   PetscErrorCode     ierr;
137703a1d59fSDave May   PetscViewer_Binary *binary = (PetscViewer_Binary*)v->data;
1378d22fd6bcSDave May   char               defaultname[PETSC_MAX_PATH_LEN];
137903a1d59fSDave May   PetscBool          flg;
1380e0385b85SDave May 
138103a1d59fSDave May   PetscFunctionBegin;
138203a1d59fSDave May   ierr = PetscOptionsHead(PetscOptionsObject,"Binary PetscViewer Options");CHKERRQ(ierr);
1383d22fd6bcSDave May   ierr = PetscSNPrintf(defaultname,PETSC_MAX_PATH_LEN-1,"binaryoutput");CHKERRQ(ierr);
1384d22fd6bcSDave May   ierr = PetscOptionsString("-viewer_binary_filename","Specify filename","PetscViewerFileSetName",defaultname,defaultname,PETSC_MAX_PATH_LEN-1,&flg);CHKERRQ(ierr);
1385d22fd6bcSDave May   if (flg) { ierr = PetscViewerFileSetName_Binary(v,defaultname);CHKERRQ(ierr); }
138634a9cc2cSBarry Smith   ierr = PetscOptionsBool("-viewer_binary_skip_info","Skip writing/reading .info file","PetscViewerBinarySetSkipInfo",PETSC_FALSE,&binary->skipinfo,NULL);CHKERRQ(ierr);
138734a9cc2cSBarry Smith   ierr = PetscOptionsBool("-viewer_binary_skip_options","Skip parsing vec load options","PetscViewerBinarySetSkipOptions",PETSC_TRUE,&binary->skipoptions,NULL);CHKERRQ(ierr);
138834a9cc2cSBarry Smith   ierr = PetscOptionsBool("-viewer_binary_skip_header","Skip writing/reading header information","PetscViewerBinarySetSkipHeader",PETSC_FALSE,&binary->skipheader,NULL);CHKERRQ(ierr);
138903a1d59fSDave May #if defined(PETSC_HAVE_MPIIO)
139034a9cc2cSBarry Smith   ierr = PetscOptionsBool("-viewer_binary_mpiio","Use MPI-IO functionality to write/read binary file","PetscViewerBinarySetUseMPIIO",PETSC_FALSE,&binary->usempiio,NULL);CHKERRQ(ierr);
139134a9cc2cSBarry Smith #elif defined(PETSC_HAVE_MPIUNI)
139234a9cc2cSBarry Smith   ierr = PetscOptionsBool("-viewer_binary_mpiio","Use MPI-IO functionality to write/read binary file","PetscViewerBinarySetUseMPIIO",PETSC_FALSE,NULL,NULL);CHKERRQ(ierr);
139303a1d59fSDave May #endif
139403a1d59fSDave May   ierr = PetscOptionsTail();CHKERRQ(ierr);
1395bc196f7cSDave May   binary->setfromoptionscalled = PETSC_TRUE;
139603a1d59fSDave May   PetscFunctionReturn(0);
139703a1d59fSDave May }
139803a1d59fSDave May 
13998556b5ebSBarry Smith /*MC
14008556b5ebSBarry Smith    PETSCVIEWERBINARY - A viewer that saves to binary files
14018556b5ebSBarry Smith 
14028556b5ebSBarry Smith 
14038556b5ebSBarry Smith .seealso:  PetscViewerBinaryOpen(), PETSC_VIEWER_STDOUT_(),PETSC_VIEWER_STDOUT_SELF, PETSC_VIEWER_STDOUT_WORLD, PetscViewerCreate(), PetscViewerASCIIOpen(),
14048556b5ebSBarry Smith            PetscViewerMatlabOpen(), VecView(), DMView(), PetscViewerMatlabPutArray(), PETSCVIEWERASCII, PETSCVIEWERMATLAB, PETSCVIEWERDRAW,
140567918a83SBarry Smith            PetscViewerFileSetName(), PetscViewerFileSetMode(), PetscViewerFormat, PetscViewerType, PetscViewerSetType(),
140667918a83SBarry Smith            PetscViewerBinaryGetUseMPIIO(), PetscViewerBinarySetUseMPIIO()
14078556b5ebSBarry Smith 
14081b266c99SBarry Smith   Level: beginner
14091b266c99SBarry Smith 
14108556b5ebSBarry Smith M*/
14118556b5ebSBarry Smith 
14128cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscViewerCreate_Binary(PetscViewer v)
14135c6c1daeSBarry Smith {
14145c6c1daeSBarry Smith   PetscErrorCode     ierr;
14155c6c1daeSBarry Smith   PetscViewer_Binary *vbinary;
14165c6c1daeSBarry Smith 
14175c6c1daeSBarry Smith   PetscFunctionBegin;
1418b00a9115SJed Brown   ierr                     = PetscNewLog(v,&vbinary);CHKERRQ(ierr);
14195c6c1daeSBarry Smith   v->data                  = (void*)vbinary;
142003a1d59fSDave May   v->ops->setfromoptions   = PetscViewerSetFromOptions_Binary;
14215c6c1daeSBarry Smith   v->ops->destroy          = PetscViewerDestroy_Binary;
14222bf49c77SBarry Smith   v->ops->view             = PetscViewerView_Binary;
1423c98fd787SBarry Smith   v->ops->setup            = PetscViewerSetUp_Binary;
1424c98fd787SBarry Smith   v->ops->flush            = NULL;
14255c6c1daeSBarry Smith   vbinary->fdes            = 0;
1426e8a65b7dSLisandro Dalcin #if defined(PETSC_HAVE_MPIIO)
1427e8a65b7dSLisandro Dalcin   vbinary->mfdes           = MPI_FILE_NULL;
1428e8a65b7dSLisandro Dalcin   vbinary->mfsub           = MPI_FILE_NULL;
1429e8a65b7dSLisandro Dalcin #endif
1430e8a65b7dSLisandro Dalcin   vbinary->fdes_info       = 0;
14315c6c1daeSBarry Smith   vbinary->skipinfo        = PETSC_FALSE;
14325c6c1daeSBarry Smith   vbinary->skipoptions     = PETSC_TRUE;
14335c6c1daeSBarry Smith   vbinary->skipheader      = PETSC_FALSE;
1434da07bb01SDave May   vbinary->setfromoptionscalled = PETSC_FALSE;
1435559f443fSBarry Smith   v->ops->getsubviewer     = PetscViewerGetSubViewer_Binary;
1436559f443fSBarry Smith   v->ops->restoresubviewer = PetscViewerRestoreSubViewer_Binary;
14371d641e7bSMichael Lange   v->ops->read             = PetscViewerBinaryRead;
14385c6c1daeSBarry Smith   vbinary->btype           = (PetscFileMode) -1;
14395c6c1daeSBarry Smith   vbinary->storecompressed = PETSC_FALSE;
1440f90597f1SStefano Zampini   vbinary->filename        = NULL;
1441f90597f1SStefano Zampini   vbinary->ogzfilename     = NULL;
14425c6c1daeSBarry Smith   vbinary->flowcontrol     = 256; /* seems a good number for Cray XT-5 */
14435c6c1daeSBarry Smith 
1444bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetFlowControl_C",PetscViewerBinaryGetFlowControl_Binary);CHKERRQ(ierr);
1445bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetFlowControl_C",PetscViewerBinarySetFlowControl_Binary);CHKERRQ(ierr);
1446bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetSkipHeader_C",PetscViewerBinarySetSkipHeader_Binary);CHKERRQ(ierr);
1447bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetSkipHeader_C",PetscViewerBinaryGetSkipHeader_Binary);CHKERRQ(ierr);
1448807ea322SDave May   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetSkipOptions_C",PetscViewerBinaryGetSkipOptions_Binary);CHKERRQ(ierr);
1449807ea322SDave May   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetSkipOptions_C",PetscViewerBinarySetSkipOptions_Binary);CHKERRQ(ierr);
1450807ea322SDave May   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetSkipInfo_C",PetscViewerBinaryGetSkipInfo_Binary);CHKERRQ(ierr);
1451807ea322SDave May   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetSkipInfo_C",PetscViewerBinarySetSkipInfo_Binary);CHKERRQ(ierr);
1452bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetInfoPointer_C",PetscViewerBinaryGetInfoPointer_Binary);CHKERRQ(ierr);
1453bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetName_C",PetscViewerFileSetName_Binary);CHKERRQ(ierr);
1454bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetMode_C",PetscViewerFileSetMode_Binary);CHKERRQ(ierr);
1455bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileGetMode_C",PetscViewerFileGetMode_Binary);CHKERRQ(ierr);
1456bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileGetName_C",PetscViewerFileGetName_Binary);CHKERRQ(ierr);
14575c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
1458bc196f7cSDave May   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetUseMPIIO_C",PetscViewerBinaryGetUseMPIIO_Binary);CHKERRQ(ierr);
1459bc196f7cSDave May   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetUseMPIIO_C",PetscViewerBinarySetUseMPIIO_Binary);CHKERRQ(ierr);
14605c6c1daeSBarry Smith #endif
14615c6c1daeSBarry Smith   PetscFunctionReturn(0);
14625c6c1daeSBarry Smith }
14635c6c1daeSBarry Smith 
14645c6c1daeSBarry Smith /* ---------------------------------------------------------------------*/
14655c6c1daeSBarry Smith /*
14665c6c1daeSBarry Smith     The variable Petsc_Viewer_Binary_keyval is used to indicate an MPI attribute that
14675c6c1daeSBarry Smith   is attached to a communicator, in this case the attribute is a PetscViewer.
14685c6c1daeSBarry Smith */
1469d4c7638eSBarry Smith PetscMPIInt Petsc_Viewer_Binary_keyval = MPI_KEYVAL_INVALID;
14705c6c1daeSBarry Smith 
14715c6c1daeSBarry Smith /*@C
14725c6c1daeSBarry Smith      PETSC_VIEWER_BINARY_ - Creates a binary PetscViewer shared by all processors
14735c6c1daeSBarry Smith                      in a communicator.
14745c6c1daeSBarry Smith 
14755c6c1daeSBarry Smith      Collective on MPI_Comm
14765c6c1daeSBarry Smith 
14775c6c1daeSBarry Smith      Input Parameter:
14785c6c1daeSBarry Smith .    comm - the MPI communicator to share the binary PetscViewer
14795c6c1daeSBarry Smith 
14805c6c1daeSBarry Smith      Level: intermediate
14815c6c1daeSBarry Smith 
14825c6c1daeSBarry Smith    Options Database Keys:
14835c6c1daeSBarry Smith +    -viewer_binary_filename <name>
14845c6c1daeSBarry Smith .    -viewer_binary_skip_info
1485a2d7db39SDave May .    -viewer_binary_skip_options
1486a2d7db39SDave May .    -viewer_binary_skip_header
1487a2d7db39SDave May -    -viewer_binary_mpiio
14885c6c1daeSBarry Smith 
14895c6c1daeSBarry Smith    Environmental variables:
14905c6c1daeSBarry Smith -   PETSC_VIEWER_BINARY_FILENAME
14915c6c1daeSBarry Smith 
14925c6c1daeSBarry Smith      Notes:
14935c6c1daeSBarry Smith      Unlike almost all other PETSc routines, PETSC_VIEWER_BINARY_ does not return
14945c6c1daeSBarry Smith      an error code.  The binary PetscViewer is usually used in the form
14955c6c1daeSBarry Smith $       XXXView(XXX object,PETSC_VIEWER_BINARY_(comm));
14965c6c1daeSBarry Smith 
14975c6c1daeSBarry Smith .seealso: PETSC_VIEWER_BINARY_WORLD, PETSC_VIEWER_BINARY_SELF, PetscViewerBinaryOpen(), PetscViewerCreate(),
14985c6c1daeSBarry Smith           PetscViewerDestroy()
14995c6c1daeSBarry Smith @*/
15005c6c1daeSBarry Smith PetscViewer PETSC_VIEWER_BINARY_(MPI_Comm comm)
15015c6c1daeSBarry Smith {
15025c6c1daeSBarry Smith   PetscErrorCode ierr;
15035c6c1daeSBarry Smith   PetscBool      flg;
15045c6c1daeSBarry Smith   PetscViewer    viewer;
15055c6c1daeSBarry Smith   char           fname[PETSC_MAX_PATH_LEN];
15065c6c1daeSBarry Smith   MPI_Comm       ncomm;
15075c6c1daeSBarry Smith 
15085c6c1daeSBarry Smith   PetscFunctionBegin;
1509efca3c55SSatish Balay   ierr = PetscCommDuplicate(comm,&ncomm,NULL);if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
15105c6c1daeSBarry Smith   if (Petsc_Viewer_Binary_keyval == MPI_KEYVAL_INVALID) {
151112801b39SBarry Smith     ierr = MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN,MPI_COMM_NULL_DELETE_FN,&Petsc_Viewer_Binary_keyval,0);
1512efca3c55SSatish Balay     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
15135c6c1daeSBarry Smith   }
151447435625SJed Brown   ierr = MPI_Comm_get_attr(ncomm,Petsc_Viewer_Binary_keyval,(void**)&viewer,(int*)&flg);
1515efca3c55SSatish Balay   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
15165c6c1daeSBarry Smith   if (!flg) { /* PetscViewer not yet created */
15175c6c1daeSBarry Smith     ierr = PetscOptionsGetenv(ncomm,"PETSC_VIEWER_BINARY_FILENAME",fname,PETSC_MAX_PATH_LEN,&flg);
1518efca3c55SSatish Balay     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
15195c6c1daeSBarry Smith     if (!flg) {
15205c6c1daeSBarry Smith       ierr = PetscStrcpy(fname,"binaryoutput");
1521efca3c55SSatish Balay       if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
15225c6c1daeSBarry Smith     }
15235c6c1daeSBarry Smith     ierr = PetscViewerBinaryOpen(ncomm,fname,FILE_MODE_WRITE,&viewer);
1524efca3c55SSatish Balay     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
15255c6c1daeSBarry Smith     ierr = PetscObjectRegisterDestroy((PetscObject)viewer);
1526efca3c55SSatish Balay     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
152747435625SJed Brown     ierr = MPI_Comm_set_attr(ncomm,Petsc_Viewer_Binary_keyval,(void*)viewer);
1528efca3c55SSatish Balay     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
15295c6c1daeSBarry Smith   }
15305c6c1daeSBarry Smith   ierr = PetscCommDestroy(&ncomm);
1531efca3c55SSatish Balay   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
15325c6c1daeSBarry Smith   PetscFunctionReturn(viewer);
15335c6c1daeSBarry Smith }
1534