xref: /petsc/src/sys/classes/viewer/impls/binary/binv.c (revision 2259a519bbf933f152169f219680cf5a385ef390)
15c6c1daeSBarry Smith 
2665c2dedSJed Brown #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)
145c6c1daeSBarry Smith   PetscBool     MPIIO;
155c6c1daeSBarry Smith   MPI_File      mfdes;                /* ignored unless using MPI IO */
165c6c1daeSBarry Smith   MPI_Offset    moff;
175c6c1daeSBarry Smith #endif
185c6c1daeSBarry Smith   PetscFileMode btype;                /* read or write? */
195c6c1daeSBarry Smith   FILE          *fdes_info;           /* optional file containing info on binary file*/
205c6c1daeSBarry Smith   PetscBool     storecompressed;      /* gzip the write binary file when closing it*/
215c6c1daeSBarry Smith   char          *filename;
225c6c1daeSBarry Smith   PetscBool     skipinfo;             /* Don't create info file for writing; don't use for reading */
235c6c1daeSBarry Smith   PetscBool     skipoptions;          /* don't use PETSc options database when loading */
245c6c1daeSBarry Smith   PetscInt      flowcontrol;          /* allow only <flowcontrol> messages outstanding at a time while doing IO */
255c6c1daeSBarry Smith   PetscBool     skipheader;           /* don't write header, only raw data */
26a261c58fSBarry Smith   PetscBool     matlabheaderwritten;  /* if format is PETSC_VIEWER_BINARY_MATLAB has the MATLAB .info header been written yet */
27da07bb01SDave May   PetscBool     setupcalled,setfromoptionscalled;
285c6c1daeSBarry Smith } PetscViewer_Binary;
295c6c1daeSBarry Smith 
3003a1d59fSDave May static PetscErrorCode PetscViewerSetUp_Binary(PetscViewer v);
3103a1d59fSDave May 
325c6c1daeSBarry Smith #undef __FUNCT__
335c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerGetSingleton_Binary"
345c6c1daeSBarry Smith PetscErrorCode PetscViewerGetSingleton_Binary(PetscViewer viewer,PetscViewer *outviewer)
355c6c1daeSBarry Smith {
365c6c1daeSBarry Smith   int                rank;
375c6c1daeSBarry Smith   PetscErrorCode     ierr;
385c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data,*obinary;
395c6c1daeSBarry Smith 
405c6c1daeSBarry Smith   PetscFunctionBegin;
4103a1d59fSDave May   ierr = PetscViewerSetUp_Binary(viewer);CHKERRQ(ierr);
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);
465c6c1daeSBarry Smith     obinary = (PetscViewer_Binary*)(*outviewer)->data;
475c6c1daeSBarry Smith     ierr    = PetscMemcpy(obinary,vbinary,sizeof(PetscViewer_Binary));CHKERRQ(ierr);
48c203d5c5SBarry Smith   } SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Cannot get singleton viewer for binary files or sockets");
495c6c1daeSBarry Smith   PetscFunctionReturn(0);
505c6c1daeSBarry Smith }
515c6c1daeSBarry Smith 
525c6c1daeSBarry Smith #undef __FUNCT__
535c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerRestoreSingleton_Binary"
545c6c1daeSBarry Smith PetscErrorCode PetscViewerRestoreSingleton_Binary(PetscViewer viewer,PetscViewer *outviewer)
555c6c1daeSBarry Smith {
565c6c1daeSBarry Smith   PetscErrorCode ierr;
575c6c1daeSBarry Smith   PetscErrorCode rank;
585c6c1daeSBarry Smith 
595c6c1daeSBarry Smith   PetscFunctionBegin;
60ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
615c6c1daeSBarry Smith   if (!rank) {
625c6c1daeSBarry Smith     ierr = PetscFree((*outviewer)->data);CHKERRQ(ierr);
635c6c1daeSBarry Smith     ierr = PetscHeaderDestroy(outviewer);CHKERRQ(ierr);
645c6c1daeSBarry Smith   }
655c6c1daeSBarry Smith   PetscFunctionReturn(0);
665c6c1daeSBarry Smith }
675c6c1daeSBarry Smith 
685c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
695c6c1daeSBarry Smith #undef __FUNCT__
705c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetMPIIOOffset"
715c6c1daeSBarry Smith /*@C
725c6c1daeSBarry Smith     PetscViewerBinaryGetMPIIOOffset - Gets the current offset that should be passed to MPI_File_set_view()
735c6c1daeSBarry Smith 
745c6c1daeSBarry Smith     Not Collective
755c6c1daeSBarry Smith 
765c6c1daeSBarry Smith     Input Parameter:
775c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
785c6c1daeSBarry Smith 
795c6c1daeSBarry Smith     Output Parameter:
805c6c1daeSBarry Smith .    off - the current offset
815c6c1daeSBarry Smith 
825c6c1daeSBarry Smith     Level: advanced
835c6c1daeSBarry Smith 
845c6c1daeSBarry Smith     Fortran Note:
855c6c1daeSBarry Smith     This routine is not supported in Fortran.
865c6c1daeSBarry Smith 
875c6c1daeSBarry Smith     Use PetscViewerBinaryAddMPIIOOffset() to increase this value after you have written a view.
885c6c1daeSBarry Smith 
895c6c1daeSBarry Smith   Concepts: file descriptor^getting
905c6c1daeSBarry Smith   Concepts: PetscViewerBinary^accessing file descriptor
915c6c1daeSBarry Smith 
925c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer()
935c6c1daeSBarry Smith @*/
945c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryGetMPIIOOffset(PetscViewer viewer,MPI_Offset *off)
955c6c1daeSBarry Smith {
965c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
975c6c1daeSBarry Smith 
985c6c1daeSBarry Smith   PetscFunctionBegin;
995c6c1daeSBarry Smith   *off = vbinary->moff;
1005c6c1daeSBarry Smith   PetscFunctionReturn(0);
1015c6c1daeSBarry Smith }
1025c6c1daeSBarry Smith 
1035c6c1daeSBarry Smith #undef __FUNCT__
1045c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryAddMPIIOOffset"
1055c6c1daeSBarry Smith /*@C
1065c6c1daeSBarry Smith     PetscViewerBinaryAddMPIIOOffset - Adds to the current offset that should be passed to MPI_File_set_view()
1075c6c1daeSBarry Smith 
1085c6c1daeSBarry Smith     Not Collective
1095c6c1daeSBarry Smith 
1105c6c1daeSBarry Smith     Input Parameters:
1115c6c1daeSBarry Smith +   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
1125c6c1daeSBarry Smith -    off - the addition to the offset
1135c6c1daeSBarry Smith 
1145c6c1daeSBarry Smith     Level: advanced
1155c6c1daeSBarry Smith 
1165c6c1daeSBarry Smith     Fortran Note:
1175c6c1daeSBarry Smith     This routine is not supported in Fortran.
1185c6c1daeSBarry Smith 
1195c6c1daeSBarry Smith     Use PetscViewerBinaryGetMPIIOOffset() to get the value that you should pass to MPI_File_set_view()
1205c6c1daeSBarry Smith 
1215c6c1daeSBarry Smith   Concepts: file descriptor^getting
1225c6c1daeSBarry Smith   Concepts: PetscViewerBinary^accessing file descriptor
1235c6c1daeSBarry Smith 
1245c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer()
1255c6c1daeSBarry Smith @*/
1265c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryAddMPIIOOffset(PetscViewer viewer,MPI_Offset off)
1275c6c1daeSBarry Smith {
1285c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
1295c6c1daeSBarry Smith 
1305c6c1daeSBarry Smith   PetscFunctionBegin;
1315c6c1daeSBarry Smith   vbinary->moff += off;
1325c6c1daeSBarry Smith   PetscFunctionReturn(0);
1335c6c1daeSBarry Smith }
1345c6c1daeSBarry Smith 
1355c6c1daeSBarry Smith #undef __FUNCT__
1365c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetMPIIODescriptor"
1375c6c1daeSBarry Smith /*@C
1385c6c1daeSBarry Smith     PetscViewerBinaryGetMPIIODescriptor - Extracts the MPI IO file descriptor from a PetscViewer.
1395c6c1daeSBarry Smith 
1405c6c1daeSBarry Smith     Not Collective
1415c6c1daeSBarry Smith 
1425c6c1daeSBarry Smith     Input Parameter:
1435c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
1445c6c1daeSBarry Smith 
1455c6c1daeSBarry Smith     Output Parameter:
1465c6c1daeSBarry Smith .   fdes - file descriptor
1475c6c1daeSBarry Smith 
1485c6c1daeSBarry Smith     Level: advanced
1495c6c1daeSBarry Smith 
1505c6c1daeSBarry Smith     Fortran Note:
1515c6c1daeSBarry Smith     This routine is not supported in Fortran.
1525c6c1daeSBarry Smith 
1535c6c1daeSBarry Smith   Concepts: file descriptor^getting
1545c6c1daeSBarry Smith   Concepts: PetscViewerBinary^accessing file descriptor
1555c6c1daeSBarry Smith 
1565c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer()
1575c6c1daeSBarry Smith @*/
1585c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryGetMPIIODescriptor(PetscViewer viewer,MPI_File *fdes)
1595c6c1daeSBarry Smith {
16003a1d59fSDave May   PetscErrorCode     ierr;
1615c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
1625c6c1daeSBarry Smith 
1635c6c1daeSBarry Smith   PetscFunctionBegin;
16403a1d59fSDave May   ierr = PetscViewerSetUp_Binary(viewer);CHKERRQ(ierr);
1655c6c1daeSBarry Smith   *fdes = vbinary->mfdes;
1665c6c1daeSBarry Smith   PetscFunctionReturn(0);
1675c6c1daeSBarry Smith }
1685c6c1daeSBarry Smith 
1695c6c1daeSBarry Smith #undef __FUNCT__
1705c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetMPIIO"
1715c6c1daeSBarry Smith /*@C
1725c6c1daeSBarry Smith     PetscViewerBinaryGetMPIIO - Returns PETSC_TRUE if the binary viewer is an MPI viewer.
1735c6c1daeSBarry Smith 
1745c6c1daeSBarry Smith     Not Collective
1755c6c1daeSBarry Smith 
1765c6c1daeSBarry Smith     Input Parameter:
1775c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
1785c6c1daeSBarry Smith 
1795c6c1daeSBarry Smith     Output Parameter:
1805c6c1daeSBarry Smith -   flg - PETSC_TRUE if MPI IO is being used
1815c6c1daeSBarry Smith 
1825c6c1daeSBarry Smith     Options Database:
1835c6c1daeSBarry Smith     -viewer_binary_mpiio : Flag for using MPI-IO
1845c6c1daeSBarry Smith 
1855c6c1daeSBarry Smith     Level: advanced
1865c6c1daeSBarry Smith 
1875c6c1daeSBarry Smith     Fortran Note:
1885c6c1daeSBarry Smith     This routine is not supported in Fortran.
1895c6c1daeSBarry Smith 
1905c6c1daeSBarry Smith   Concepts: file descriptor^getting
1915c6c1daeSBarry Smith   Concepts: PetscViewerBinary^accessing file descriptor
1925c6c1daeSBarry Smith 
1935c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetInfoPointer()
1945c6c1daeSBarry Smith @*/
1955c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryGetMPIIO(PetscViewer viewer,PetscBool  *flg)
1965c6c1daeSBarry Smith {
1975c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
1985c6c1daeSBarry Smith 
1995c6c1daeSBarry Smith   PetscFunctionBegin;
2005c6c1daeSBarry Smith   *flg = vbinary->MPIIO;
2015c6c1daeSBarry Smith   PetscFunctionReturn(0);
2025c6c1daeSBarry Smith }
2035c6c1daeSBarry Smith #endif
2045c6c1daeSBarry Smith 
2055c6c1daeSBarry Smith #undef __FUNCT__
2065c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetFlowControl_Binary"
2075c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryGetFlowControl_Binary(PetscViewer viewer,PetscInt *fc)
2085c6c1daeSBarry Smith {
2095c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
2105c6c1daeSBarry Smith 
2115c6c1daeSBarry Smith   PetscFunctionBegin;
2125c6c1daeSBarry Smith   *fc = vbinary->flowcontrol;
2135c6c1daeSBarry Smith   PetscFunctionReturn(0);
2145c6c1daeSBarry Smith }
2155c6c1daeSBarry Smith 
2165c6c1daeSBarry Smith #undef __FUNCT__
2175c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetFlowControl"
2185c6c1daeSBarry Smith /*@C
2195c6c1daeSBarry Smith     PetscViewerBinaryGetFlowControl - Returns how many messages are allowed to outstanding at the same time during parallel IO reads/writes
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:
2275c6c1daeSBarry Smith .   fc - the number of messages
2285c6c1daeSBarry Smith 
2295c6c1daeSBarry Smith     Level: advanced
2305c6c1daeSBarry Smith 
2315c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer(), PetscViewerBinarySetFlowControl()
2325c6c1daeSBarry Smith 
2335c6c1daeSBarry Smith @*/
2345c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryGetFlowControl(PetscViewer viewer,PetscInt *fc)
2355c6c1daeSBarry Smith {
236639ff905SBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
2375c6c1daeSBarry Smith   PetscErrorCode     ierr;
2385c6c1daeSBarry Smith 
2395c6c1daeSBarry Smith   PetscFunctionBegin;
240639ff905SBarry Smith   *fc  = vbinary->flowcontrol;
2415c6c1daeSBarry Smith   ierr = PetscTryMethod(viewer,"PetscViewerBinaryGetFlowControl_C",(PetscViewer,PetscInt*),(viewer,fc));CHKERRQ(ierr);
2425c6c1daeSBarry Smith   PetscFunctionReturn(0);
2435c6c1daeSBarry Smith }
2445c6c1daeSBarry Smith 
2455c6c1daeSBarry Smith #undef __FUNCT__
2465c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinarySetFlowControl_Binary"
2475c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinarySetFlowControl_Binary(PetscViewer viewer,PetscInt fc)
2485c6c1daeSBarry Smith {
2495c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
2505c6c1daeSBarry Smith 
2515c6c1daeSBarry Smith   PetscFunctionBegin;
252ce94432eSBarry Smith   if (fc <= 1) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_OUTOFRANGE,"Flow control count must be greater than 1, %D was set",fc);
2535c6c1daeSBarry Smith   vbinary->flowcontrol = fc;
2545c6c1daeSBarry Smith   PetscFunctionReturn(0);
2555c6c1daeSBarry Smith }
2565c6c1daeSBarry Smith 
2575c6c1daeSBarry Smith #undef __FUNCT__
2585c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinarySetFlowControl"
2595c6c1daeSBarry Smith /*@C
260639ff905SBarry Smith     PetscViewerBinarySetFlowControl - Sets how many messages are allowed to outstanding at the same time during parallel IO reads/writes
2615c6c1daeSBarry Smith 
2625c6c1daeSBarry Smith     Not Collective
2635c6c1daeSBarry Smith 
2645c6c1daeSBarry Smith     Input Parameter:
2655c6c1daeSBarry Smith +   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
266639ff905SBarry Smith -   fc - the number of messages, defaults to 256 if this function was not called
2675c6c1daeSBarry Smith 
2685c6c1daeSBarry Smith     Level: advanced
2695c6c1daeSBarry Smith 
2705c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer(), PetscViewerBinaryGetFlowControl()
2715c6c1daeSBarry Smith 
2725c6c1daeSBarry Smith @*/
2735c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinarySetFlowControl(PetscViewer viewer,PetscInt fc)
2745c6c1daeSBarry Smith {
2755c6c1daeSBarry Smith   PetscErrorCode ierr;
2765c6c1daeSBarry Smith 
2775c6c1daeSBarry Smith   PetscFunctionBegin;
2785c6c1daeSBarry Smith   ierr = PetscUseMethod(viewer,"PetscViewerBinarySetFlowControl_C",(PetscViewer,PetscInt),(viewer,fc));CHKERRQ(ierr);
2795c6c1daeSBarry Smith   PetscFunctionReturn(0);
2805c6c1daeSBarry Smith }
2815c6c1daeSBarry Smith 
2825c6c1daeSBarry Smith #undef __FUNCT__
2835c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetDescriptor"
2845c6c1daeSBarry Smith /*@C
2855c6c1daeSBarry Smith     PetscViewerBinaryGetDescriptor - Extracts the file descriptor from a PetscViewer.
2865c6c1daeSBarry Smith 
2875c6c1daeSBarry Smith     Not Collective
2885c6c1daeSBarry Smith 
2895c6c1daeSBarry Smith     Input Parameter:
2905c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
2915c6c1daeSBarry Smith 
2925c6c1daeSBarry Smith     Output Parameter:
2935c6c1daeSBarry Smith .   fdes - file descriptor
2945c6c1daeSBarry Smith 
2955c6c1daeSBarry Smith     Level: advanced
2965c6c1daeSBarry Smith 
2975c6c1daeSBarry Smith     Notes:
2985c6c1daeSBarry Smith       For writable binary PetscViewers, the descriptor will only be valid for the
2995c6c1daeSBarry Smith     first processor in the communicator that shares the PetscViewer. For readable
3005c6c1daeSBarry Smith     files it will only be valid on nodes that have the file. If node 0 does not
3015c6c1daeSBarry Smith     have the file it generates an error even if another node does have the file.
3025c6c1daeSBarry Smith 
3035c6c1daeSBarry Smith     Fortran Note:
3045c6c1daeSBarry Smith     This routine is not supported in Fortran.
3055c6c1daeSBarry Smith 
3065c6c1daeSBarry Smith   Concepts: file descriptor^getting
3075c6c1daeSBarry Smith   Concepts: PetscViewerBinary^accessing file descriptor
3085c6c1daeSBarry Smith 
3095c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer()
3105c6c1daeSBarry Smith @*/
3115c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryGetDescriptor(PetscViewer viewer,int *fdes)
3125c6c1daeSBarry Smith {
31303a1d59fSDave May   PetscErrorCode     ierr;
3145c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
3155c6c1daeSBarry Smith 
3165c6c1daeSBarry Smith   PetscFunctionBegin;
31703a1d59fSDave May   ierr = PetscViewerSetUp_Binary(viewer);CHKERRQ(ierr);
3185c6c1daeSBarry Smith   *fdes = vbinary->fdes;
3195c6c1daeSBarry Smith   PetscFunctionReturn(0);
3205c6c1daeSBarry Smith }
3215c6c1daeSBarry Smith 
3225c6c1daeSBarry Smith #undef __FUNCT__
3235c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinarySkipInfo"
3245c6c1daeSBarry Smith /*@
3255c6c1daeSBarry Smith     PetscViewerBinarySkipInfo - Binary file will not have .info file created with it
3265c6c1daeSBarry Smith 
3275c6c1daeSBarry Smith     Not Collective
3285c6c1daeSBarry Smith 
3295c6c1daeSBarry Smith     Input Paramter:
3305c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerCreate()
3315c6c1daeSBarry Smith 
3325c6c1daeSBarry Smith     Options Database Key:
3335c6c1daeSBarry Smith .   -viewer_binary_skip_info
3345c6c1daeSBarry Smith 
3355c6c1daeSBarry Smith     Level: advanced
3365c6c1daeSBarry Smith 
3375c6c1daeSBarry Smith     Notes: This must be called after PetscViewerSetType() but before PetscViewerFileSetName(). If you use PetscViewerBinaryOpen() then
3385c6c1daeSBarry Smith     you can only skip the info file with the -viewer_binary_skip_info flag. To use the function you must open the
3395c6c1daeSBarry Smith     viewer with PetscViewerCreate(), PetscViewerSetType(), PetscViewerFileSetName().
3405c6c1daeSBarry Smith 
3415c6c1daeSBarry Smith     The .info contains meta information about the data in the binary file, for example the block size if it was
3425c6c1daeSBarry Smith     set for a vector or matrix.
3435c6c1daeSBarry Smith 
3445c6c1daeSBarry Smith    Concepts: PetscViewerBinary^accessing info file
3455c6c1daeSBarry Smith 
3465c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySetSkipOptions(),
3475c6c1daeSBarry Smith           PetscViewerBinaryGetSkipOptions()
3485c6c1daeSBarry Smith @*/
3495c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinarySkipInfo(PetscViewer viewer)
3505c6c1daeSBarry Smith {
3515c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
3525c6c1daeSBarry Smith 
3535c6c1daeSBarry Smith   PetscFunctionBegin;
3545c6c1daeSBarry Smith   vbinary->skipinfo = PETSC_TRUE;
3555c6c1daeSBarry Smith   PetscFunctionReturn(0);
3565c6c1daeSBarry Smith }
3575c6c1daeSBarry Smith 
3585c6c1daeSBarry Smith #undef __FUNCT__
3595c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinarySetSkipOptions"
3605c6c1daeSBarry Smith /*@
3615c6c1daeSBarry Smith     PetscViewerBinarySetSkipOptions - do not use the PETSc options database when loading objects
3625c6c1daeSBarry Smith 
3635c6c1daeSBarry Smith     Not Collective
3645c6c1daeSBarry Smith 
3655c6c1daeSBarry Smith     Input Parameters:
3665c6c1daeSBarry Smith +   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
3675c6c1daeSBarry Smith -   skip - PETSC_TRUE means do not use
3685c6c1daeSBarry Smith 
3695c6c1daeSBarry Smith     Options Database Key:
3705c6c1daeSBarry Smith .   -viewer_binary_skip_options
3715c6c1daeSBarry Smith 
3725c6c1daeSBarry Smith     Level: advanced
3735c6c1daeSBarry Smith 
3745c6c1daeSBarry Smith     Notes: This must be called after PetscViewerSetType()
3755c6c1daeSBarry Smith 
3765c6c1daeSBarry Smith    Concepts: PetscViewerBinary^accessing info file
3775c6c1daeSBarry Smith 
3785c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(),
3795c6c1daeSBarry Smith           PetscViewerBinaryGetSkipOptions()
3805c6c1daeSBarry Smith @*/
3815c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinarySetSkipOptions(PetscViewer viewer,PetscBool skip)
3825c6c1daeSBarry Smith {
3835c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
3845c6c1daeSBarry Smith 
3855c6c1daeSBarry Smith   PetscFunctionBegin;
3865c6c1daeSBarry Smith   vbinary->skipoptions = skip;
3875c6c1daeSBarry Smith   PetscFunctionReturn(0);
3885c6c1daeSBarry Smith }
3895c6c1daeSBarry Smith 
3905c6c1daeSBarry Smith #undef __FUNCT__
3915c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetSkipOptions"
3925c6c1daeSBarry Smith /*@
3935c6c1daeSBarry Smith     PetscViewerBinaryGetSkipOptions - checks if viewer uses the PETSc options database when loading objects
3945c6c1daeSBarry Smith 
3955c6c1daeSBarry Smith     Not Collective
3965c6c1daeSBarry Smith 
3975c6c1daeSBarry Smith     Input Parameter:
3985c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
3995c6c1daeSBarry Smith 
4005c6c1daeSBarry Smith     Output Parameter:
4015c6c1daeSBarry Smith .   skip - PETSC_TRUE means do not use
4025c6c1daeSBarry Smith 
4035c6c1daeSBarry Smith     Level: advanced
4045c6c1daeSBarry Smith 
4055c6c1daeSBarry Smith     Notes: This must be called after PetscViewerSetType()
4065c6c1daeSBarry Smith 
4075c6c1daeSBarry Smith    Concepts: PetscViewerBinary^accessing info file
4085c6c1daeSBarry Smith 
4095c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(),
4105c6c1daeSBarry Smith           PetscViewerBinarySetSkipOptions()
4115c6c1daeSBarry Smith @*/
4125c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryGetSkipOptions(PetscViewer viewer,PetscBool  *skip)
4135c6c1daeSBarry Smith {
41422d28d08SBarry Smith   PetscViewer_Binary *vbinary;
4155c6c1daeSBarry Smith 
4165c6c1daeSBarry Smith   PetscFunctionBegin;
4175c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
4185c6c1daeSBarry Smith   vbinary = (PetscViewer_Binary*)viewer->data;
4195c6c1daeSBarry Smith   *skip   = vbinary->skipoptions;
4205c6c1daeSBarry Smith   PetscFunctionReturn(0);
4215c6c1daeSBarry Smith }
4225c6c1daeSBarry Smith 
4235c6c1daeSBarry Smith #undef __FUNCT__
4245c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinarySetSkipHeader_Binary"
4255c6c1daeSBarry Smith PetscErrorCode PetscViewerBinarySetSkipHeader_Binary(PetscViewer viewer,PetscBool skip)
4265c6c1daeSBarry Smith {
4275c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
4285c6c1daeSBarry Smith 
4295c6c1daeSBarry Smith   PetscFunctionBegin;
4305c6c1daeSBarry Smith   vbinary->skipheader = skip;
4315c6c1daeSBarry Smith   PetscFunctionReturn(0);
4325c6c1daeSBarry Smith }
4335c6c1daeSBarry Smith 
4345c6c1daeSBarry Smith #undef __FUNCT__
4355c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinarySetSkipHeader"
4365c6c1daeSBarry Smith /*@
4375c6c1daeSBarry Smith     PetscViewerBinarySetSkipHeader - do not write a header with size information on output, just raw data
4385c6c1daeSBarry Smith 
4395c6c1daeSBarry Smith     Not Collective
4405c6c1daeSBarry Smith 
4415c6c1daeSBarry Smith     Input Parameters:
4425c6c1daeSBarry Smith +   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
4435c6c1daeSBarry Smith -   skip - PETSC_TRUE means do not write header
4445c6c1daeSBarry Smith 
4455c6c1daeSBarry Smith     Options Database Key:
4465c6c1daeSBarry Smith .   -viewer_binary_skip_header
4475c6c1daeSBarry Smith 
4485c6c1daeSBarry Smith     Level: advanced
4495c6c1daeSBarry Smith 
4505c6c1daeSBarry Smith     Notes: This must be called after PetscViewerSetType()
4515c6c1daeSBarry Smith 
4525c6c1daeSBarry Smith            Can ONLY be called on a binary viewer
4535c6c1daeSBarry Smith 
4545c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(),
4555c6c1daeSBarry Smith           PetscViewerBinaryGetSkipHeader()
4565c6c1daeSBarry Smith @*/
4575c6c1daeSBarry Smith PetscErrorCode PetscViewerBinarySetSkipHeader(PetscViewer viewer,PetscBool skip)
4585c6c1daeSBarry Smith {
4595c6c1daeSBarry Smith   PetscErrorCode ierr;
4605c6c1daeSBarry Smith 
4615c6c1daeSBarry Smith   PetscFunctionBegin;
4625c6c1daeSBarry Smith   ierr = PetscUseMethod(viewer,"PetscViewerBinarySetSkipHeader_C",(PetscViewer,PetscBool),(viewer,skip));CHKERRQ(ierr);
4635c6c1daeSBarry Smith   PetscFunctionReturn(0);
4645c6c1daeSBarry Smith }
4655c6c1daeSBarry Smith 
4665c6c1daeSBarry Smith #undef __FUNCT__
4675c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetSkipHeader_Binary"
4685c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetSkipHeader_Binary(PetscViewer viewer,PetscBool  *skip)
4695c6c1daeSBarry Smith {
4705c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
4715c6c1daeSBarry Smith 
4725c6c1daeSBarry Smith   PetscFunctionBegin;
4735c6c1daeSBarry Smith   *skip = vbinary->skipheader;
4745c6c1daeSBarry Smith   PetscFunctionReturn(0);
4755c6c1daeSBarry Smith }
4765c6c1daeSBarry Smith 
4775c6c1daeSBarry Smith #undef __FUNCT__
4785c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetSkipHeader"
4795c6c1daeSBarry Smith /*@
4805c6c1daeSBarry Smith     PetscViewerBinaryGetSkipHeader - checks whether to write a header with size information on output, or just raw data
4815c6c1daeSBarry Smith 
4825c6c1daeSBarry Smith     Not Collective
4835c6c1daeSBarry Smith 
4845c6c1daeSBarry Smith     Input Parameter:
4855c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
4865c6c1daeSBarry Smith 
4875c6c1daeSBarry Smith     Output Parameter:
4885c6c1daeSBarry Smith .   skip - PETSC_TRUE means do not write header
4895c6c1daeSBarry Smith 
4905c6c1daeSBarry Smith     Level: advanced
4915c6c1daeSBarry Smith 
4925c6c1daeSBarry Smith     Notes: This must be called after PetscViewerSetType()
4935c6c1daeSBarry Smith 
4945c6c1daeSBarry Smith             Returns false for PETSCSOCKETVIEWER, you cannot skip the header for it.
4955c6c1daeSBarry Smith 
4965c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(),
4975c6c1daeSBarry Smith           PetscViewerBinarySetSkipHeader()
4985c6c1daeSBarry Smith @*/
4995c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetSkipHeader(PetscViewer viewer,PetscBool  *skip)
5005c6c1daeSBarry Smith {
5015c6c1daeSBarry Smith   PetscErrorCode ierr;
5025c6c1daeSBarry Smith 
5035c6c1daeSBarry Smith   PetscFunctionBegin;
5045c6c1daeSBarry Smith   *skip = PETSC_FALSE;
5055c6c1daeSBarry Smith   ierr  = PetscTryMethod(viewer,"PetscViewerBinaryGetSkipHeader_C",(PetscViewer,PetscBool*),(viewer,skip));CHKERRQ(ierr);
5065c6c1daeSBarry Smith   PetscFunctionReturn(0);
5075c6c1daeSBarry Smith }
5085c6c1daeSBarry Smith 
5095c6c1daeSBarry Smith #undef __FUNCT__
5105c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetInfoPointer_Binary"
5115c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryGetInfoPointer_Binary(PetscViewer viewer,FILE **file)
5125c6c1daeSBarry Smith {
513f3b211e4SSatish Balay   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
514a261c58fSBarry Smith   PetscErrorCode     ierr;
515a261c58fSBarry Smith   MPI_Comm           comm;
5165c6c1daeSBarry Smith 
5175c6c1daeSBarry Smith   PetscFunctionBegin;
51803a1d59fSDave May   ierr = PetscViewerSetUp_Binary(viewer);CHKERRQ(ierr);
5195c6c1daeSBarry Smith   *file = vbinary->fdes_info;
520a261c58fSBarry Smith   if (viewer->format == PETSC_VIEWER_BINARY_MATLAB && !vbinary->matlabheaderwritten) {
521a261c58fSBarry Smith     vbinary->matlabheaderwritten = PETSC_TRUE;
522a261c58fSBarry Smith     ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
523da88d4d4SJed Brown     ierr = PetscFPrintf(comm,*file,"#--- begin code written by PetscViewerBinary for MATLAB format ---#\n");CHKERRQ(ierr);
524da88d4d4SJed Brown     ierr = PetscFPrintf(comm,*file,"#$$ Set.filename = '%s';\n",vbinary->filename);CHKERRQ(ierr);
525da88d4d4SJed Brown     ierr = PetscFPrintf(comm,*file,"#$$ fd = PetscOpenFile(Set.filename);\n");CHKERRQ(ierr);
526da88d4d4SJed Brown     ierr = PetscFPrintf(comm,*file,"#--- end code written by PetscViewerBinary for MATLAB format ---#\n\n");CHKERRQ(ierr);
527a261c58fSBarry Smith   }
5285c6c1daeSBarry Smith   PetscFunctionReturn(0);
5295c6c1daeSBarry Smith }
5305c6c1daeSBarry Smith 
5315c6c1daeSBarry Smith #undef __FUNCT__
5325c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetInfoPointer"
5335c6c1daeSBarry Smith /*@C
5345c6c1daeSBarry Smith     PetscViewerBinaryGetInfoPointer - Extracts the file pointer for the ASCII
5355c6c1daeSBarry Smith           info file associated with a binary file.
5365c6c1daeSBarry Smith 
5375c6c1daeSBarry Smith     Not Collective
5385c6c1daeSBarry Smith 
5395c6c1daeSBarry Smith     Input Parameter:
5405c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
5415c6c1daeSBarry Smith 
5425c6c1daeSBarry Smith     Output Parameter:
5430298fd71SBarry Smith .   file - file pointer  Always returns NULL if not a binary viewer
5445c6c1daeSBarry Smith 
5455c6c1daeSBarry Smith     Level: advanced
5465c6c1daeSBarry Smith 
5475c6c1daeSBarry Smith     Notes:
5485c6c1daeSBarry Smith       For writable binary PetscViewers, the descriptor will only be valid for the
5495c6c1daeSBarry Smith     first processor in the communicator that shares the PetscViewer.
5505c6c1daeSBarry Smith 
5515c6c1daeSBarry Smith     Fortran Note:
5525c6c1daeSBarry Smith     This routine is not supported in Fortran.
5535c6c1daeSBarry Smith 
5545c6c1daeSBarry Smith   Concepts: PetscViewerBinary^accessing info file
5555c6c1daeSBarry Smith 
5565c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetDescriptor()
5575c6c1daeSBarry Smith @*/
5585c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryGetInfoPointer(PetscViewer viewer,FILE **file)
5595c6c1daeSBarry Smith {
5605c6c1daeSBarry Smith   PetscErrorCode ierr;
5615c6c1daeSBarry Smith 
5625c6c1daeSBarry Smith   PetscFunctionBegin;
5630298fd71SBarry Smith   *file = NULL;
5645c6c1daeSBarry Smith   ierr  = PetscTryMethod(viewer,"PetscViewerBinaryGetInfoPointer_C",(PetscViewer,FILE **),(viewer,file));CHKERRQ(ierr);
5655c6c1daeSBarry Smith   PetscFunctionReturn(0);
5665c6c1daeSBarry Smith }
5675c6c1daeSBarry Smith 
5685c6c1daeSBarry Smith #undef __FUNCT__
5695c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileClose_Binary"
5705c6c1daeSBarry Smith static PetscErrorCode PetscViewerFileClose_Binary(PetscViewer v)
5715c6c1daeSBarry Smith {
5725c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data;
5735c6c1daeSBarry Smith   PetscErrorCode     ierr;
5745c6c1daeSBarry Smith   PetscMPIInt        rank;
5755c6c1daeSBarry Smith   int                err;
5765c6c1daeSBarry Smith 
5775c6c1daeSBarry Smith   PetscFunctionBegin;
578ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)v),&rank);CHKERRQ(ierr);
5795c6c1daeSBarry Smith   if ((!rank || vbinary->btype == FILE_MODE_READ) && vbinary->fdes) {
5805c6c1daeSBarry Smith     close(vbinary->fdes);
5815c6c1daeSBarry Smith     if (!rank && vbinary->storecompressed) {
5825c6c1daeSBarry Smith       char par[PETSC_MAX_PATH_LEN],buf[PETSC_MAX_PATH_LEN];
5835c6c1daeSBarry Smith       FILE *fp;
5845c6c1daeSBarry Smith       /* compress the file */
5855c6c1daeSBarry Smith       ierr = PetscStrcpy(par,"gzip -f ");CHKERRQ(ierr);
5865c6c1daeSBarry Smith       ierr = PetscStrcat(par,vbinary->filename);CHKERRQ(ierr);
5875c6c1daeSBarry Smith #if defined(PETSC_HAVE_POPEN)
5880298fd71SBarry Smith       ierr = PetscPOpen(PETSC_COMM_SELF,NULL,par,"r",&fp);CHKERRQ(ierr);
5895c6c1daeSBarry Smith       if (fgets(buf,1024,fp)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error from command %s\n%s",par,buf);
5900298fd71SBarry Smith       ierr = PetscPClose(PETSC_COMM_SELF,fp,NULL);CHKERRQ(ierr);
5915c6c1daeSBarry Smith #else
5925c6c1daeSBarry Smith       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
5935c6c1daeSBarry Smith #endif
5945c6c1daeSBarry Smith     }
5955c6c1daeSBarry Smith   }
5965c6c1daeSBarry Smith   if (vbinary->fdes_info) {
5975c6c1daeSBarry Smith     err = fclose(vbinary->fdes_info);
5985c6c1daeSBarry Smith     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
5995c6c1daeSBarry Smith   }
6005c6c1daeSBarry Smith   PetscFunctionReturn(0);
6015c6c1daeSBarry Smith }
6025c6c1daeSBarry Smith 
603e0385b85SDave May #if defined(PETSC_HAVE_MPIIO)
604e0385b85SDave May #undef __FUNCT__
605e0385b85SDave May #define __FUNCT__ "PetscViewerFileClose_BinaryMPIIO"
606e0385b85SDave May static PetscErrorCode PetscViewerFileClose_BinaryMPIIO(PetscViewer v)
607e0385b85SDave May {
608e0385b85SDave May   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data;
609e0385b85SDave May   int                err;
610e0385b85SDave May   PetscErrorCode     ierr;
611e0385b85SDave May 
612e0385b85SDave May   PetscFunctionBegin;
613e0385b85SDave May   if (vbinary->mfdes) {
614e0385b85SDave May     ierr = MPI_File_close(&vbinary->mfdes);CHKERRQ(ierr);
615e0385b85SDave May   }
616e0385b85SDave May   if (vbinary->fdes_info) {
617e0385b85SDave May     err = fclose(vbinary->fdes_info);
618e0385b85SDave May     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
619e0385b85SDave May   }
620e0385b85SDave May   PetscFunctionReturn(0);
621e0385b85SDave May }
622e0385b85SDave May #endif
623e0385b85SDave May 
6245c6c1daeSBarry Smith #undef __FUNCT__
6255c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerDestroy_Binary"
6265c6c1daeSBarry Smith PetscErrorCode PetscViewerDestroy_Binary(PetscViewer v)
6275c6c1daeSBarry Smith {
6285c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data;
6295c6c1daeSBarry Smith   PetscErrorCode     ierr;
6305c6c1daeSBarry Smith 
6315c6c1daeSBarry Smith   PetscFunctionBegin;
632a261c58fSBarry Smith   if (v->format == PETSC_VIEWER_BINARY_MATLAB) {
633a261c58fSBarry Smith     MPI_Comm comm;
634a261c58fSBarry Smith     FILE     *info;
635a261c58fSBarry Smith 
636a261c58fSBarry Smith     ierr = PetscObjectGetComm((PetscObject)v,&comm);CHKERRQ(ierr);
637a261c58fSBarry Smith     ierr = PetscViewerBinaryGetInfoPointer(v,&info);CHKERRQ(ierr);
638da88d4d4SJed Brown     ierr = PetscFPrintf(comm,info,"#--- begin code written by PetscViewerBinary for MATLAB format ---#\n");CHKERRQ(ierr);
639da88d4d4SJed Brown     ierr = PetscFPrintf(comm,info,"#$$ close(fd);\n");CHKERRQ(ierr);
640da88d4d4SJed Brown     ierr = PetscFPrintf(comm,info,"#--- end code written by PetscViewerBinary for MATLAB format ---#\n\n");CHKERRQ(ierr);
641a261c58fSBarry Smith   }
6425c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
643e0385b85SDave May   if (vbinary->MPIIO) {
644e0385b85SDave May     ierr = PetscViewerFileClose_BinaryMPIIO(v);CHKERRQ(ierr);
645e0385b85SDave May   } else {
646e0385b85SDave May #endif
647e0385b85SDave May     ierr = PetscViewerFileClose_Binary(v);CHKERRQ(ierr);
648e0385b85SDave May #if defined(PETSC_HAVE_MPIIO)
6495c6c1daeSBarry Smith   }
6505c6c1daeSBarry Smith #endif
651*2259a519SDave May   if (vbinary->filename) { ierr = PetscFree(vbinary->filename);CHKERRQ(ierr); }
652e0385b85SDave May   ierr = PetscFree(vbinary);CHKERRQ(ierr);
653e0385b85SDave May   PetscFunctionReturn(0);
654e0385b85SDave May }
6555c6c1daeSBarry Smith 
6565c6c1daeSBarry Smith #undef __FUNCT__
6575c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryOpen"
6585c6c1daeSBarry Smith /*@C
6595c6c1daeSBarry Smith    PetscViewerBinaryOpen - Opens a file for binary input/output.
6605c6c1daeSBarry Smith 
6615c6c1daeSBarry Smith    Collective on MPI_Comm
6625c6c1daeSBarry Smith 
6635c6c1daeSBarry Smith    Input Parameters:
6645c6c1daeSBarry Smith +  comm - MPI communicator
6655c6c1daeSBarry Smith .  name - name of file
6665c6c1daeSBarry Smith -  type - type of file
6675c6c1daeSBarry Smith $    FILE_MODE_WRITE - create new file for binary output
6685c6c1daeSBarry Smith $    FILE_MODE_READ - open existing file for binary input
6695c6c1daeSBarry Smith $    FILE_MODE_APPEND - open existing file for binary output
6705c6c1daeSBarry Smith 
6715c6c1daeSBarry Smith    Output Parameter:
6725c6c1daeSBarry Smith .  binv - PetscViewer for binary input/output to use with the specified file
6735c6c1daeSBarry Smith 
6745c6c1daeSBarry Smith     Options Database Keys:
6755c6c1daeSBarry Smith +    -viewer_binary_skip_info
6765c6c1daeSBarry Smith .    -viewer_binary_skip_options
6775c6c1daeSBarry Smith -    -viewer_binary_skip_header
6785c6c1daeSBarry Smith 
6795c6c1daeSBarry Smith    Level: beginner
6805c6c1daeSBarry Smith 
6815c6c1daeSBarry Smith    Note:
6825c6c1daeSBarry Smith    This PetscViewer should be destroyed with PetscViewerDestroy().
6835c6c1daeSBarry Smith 
6845c6c1daeSBarry Smith     For reading files, the filename may begin with ftp:// or http:// and/or
6855c6c1daeSBarry Smith     end with .gz; in this case file is brought over and uncompressed.
6865c6c1daeSBarry Smith 
6875c6c1daeSBarry Smith     For creating files, if the file name ends with .gz it is automatically
6885c6c1daeSBarry Smith     compressed when closed.
6895c6c1daeSBarry Smith 
6905c6c1daeSBarry Smith     For writing files it only opens the file on processor 0 in the communicator.
6915c6c1daeSBarry Smith     For readable files it opens the file on all nodes that have the file. If
6925c6c1daeSBarry Smith     node 0 does not have the file it generates an error even if other nodes
6935c6c1daeSBarry Smith     do have the file.
6945c6c1daeSBarry Smith 
6955c6c1daeSBarry Smith    Concepts: binary files
6965c6c1daeSBarry Smith    Concepts: PetscViewerBinary^creating
6975c6c1daeSBarry Smith    Concepts: gzip
6985c6c1daeSBarry Smith    Concepts: accessing remote file
6995c6c1daeSBarry Smith    Concepts: remote file
7005c6c1daeSBarry Smith 
7015c6c1daeSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(),
7025c6c1daeSBarry Smith           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
7035c6c1daeSBarry Smith           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscViewerBinaryRead()
7045c6c1daeSBarry Smith @*/
7055c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryOpen(MPI_Comm comm,const char name[],PetscFileMode type,PetscViewer *binv)
7065c6c1daeSBarry Smith {
7075c6c1daeSBarry Smith   PetscErrorCode ierr;
7085c6c1daeSBarry Smith 
7095c6c1daeSBarry Smith   PetscFunctionBegin;
7105c6c1daeSBarry Smith   ierr = PetscViewerCreate(comm,binv);CHKERRQ(ierr);
7115c6c1daeSBarry Smith   ierr = PetscViewerSetType(*binv,PETSCVIEWERBINARY);CHKERRQ(ierr);
7125c6c1daeSBarry Smith   ierr = PetscViewerFileSetMode(*binv,type);CHKERRQ(ierr);
7135c6c1daeSBarry Smith   ierr = PetscViewerFileSetName(*binv,name);CHKERRQ(ierr);
71403a1d59fSDave May   ierr = PetscViewerSetFromOptions(*binv);CHKERRQ(ierr);
7155c6c1daeSBarry Smith   PetscFunctionReturn(0);
7165c6c1daeSBarry Smith }
7175c6c1daeSBarry Smith 
7185c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
7195c6c1daeSBarry Smith #undef __FUNCT__
720ff5f5b63SDave May #define __FUNCT__ "PetscViewerBinaryWriteReadMPIIO"
721ff5f5b63SDave May static PetscErrorCode PetscViewerBinaryWriteReadMPIIO(PetscViewer viewer,void *data,PetscInt count,PetscDataType dtype,PetscBool write)
7225c6c1daeSBarry Smith {
7235c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
7245c6c1daeSBarry Smith   PetscErrorCode     ierr;
7255c6c1daeSBarry Smith   MPI_Datatype       mdtype;
726e597bc1dSJed Brown   PetscMPIInt        cnt;
7275c6c1daeSBarry Smith   MPI_Status         status;
7285c6c1daeSBarry Smith   MPI_Aint           ul,dsize;
7295c6c1daeSBarry Smith 
7305c6c1daeSBarry Smith   PetscFunctionBegin;
7314dc2109aSBarry Smith   ierr = PetscMPIIntCast(count,&cnt);CHKERRQ(ierr);
7325c6c1daeSBarry Smith   ierr = PetscDataTypeToMPIDataType(dtype,&mdtype);CHKERRQ(ierr);
7335c6c1daeSBarry Smith   ierr = MPI_File_set_view(vbinary->mfdes,vbinary->moff,mdtype,mdtype,(char*)"native",MPI_INFO_NULL);CHKERRQ(ierr);
7345c6c1daeSBarry Smith   if (write) {
7355c6c1daeSBarry Smith     ierr = MPIU_File_write_all(vbinary->mfdes,data,cnt,mdtype,&status);CHKERRQ(ierr);
7365c6c1daeSBarry Smith   } else {
7375c6c1daeSBarry Smith     ierr = MPIU_File_read_all(vbinary->mfdes,data,cnt,mdtype,&status);CHKERRQ(ierr);
7385c6c1daeSBarry Smith   }
7395c6c1daeSBarry Smith   ierr = MPI_Type_get_extent(mdtype,&ul,&dsize);CHKERRQ(ierr);
740a297a907SKarl Rupp 
7415c6c1daeSBarry Smith   vbinary->moff += dsize*cnt;
7425c6c1daeSBarry Smith   PetscFunctionReturn(0);
7435c6c1daeSBarry Smith }
7445c6c1daeSBarry Smith #endif
7455c6c1daeSBarry Smith 
7465c6c1daeSBarry Smith #undef __FUNCT__
7475c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryRead"
7485c6c1daeSBarry Smith /*@C
7495c6c1daeSBarry Smith    PetscViewerBinaryRead - Reads from a binary file, all processors get the same result
7505c6c1daeSBarry Smith 
7515c6c1daeSBarry Smith    Collective on MPI_Comm
7525c6c1daeSBarry Smith 
7535c6c1daeSBarry Smith    Input Parameters:
7545c6c1daeSBarry Smith +  viewer - the binary viewer
755907376e6SBarry Smith .  data - location of the data to be written
7565c6c1daeSBarry Smith .  count - number of items of data to read
757907376e6SBarry Smith -  dtype - type of data to read
7585c6c1daeSBarry Smith 
7595c6c1daeSBarry Smith    Level: beginner
7605c6c1daeSBarry Smith 
7615c6c1daeSBarry Smith    Concepts: binary files
7625c6c1daeSBarry Smith 
7635c6c1daeSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(),
7645c6c1daeSBarry Smith           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
7655c6c1daeSBarry Smith           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead()
7665c6c1daeSBarry Smith @*/
7675c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryRead(PetscViewer viewer,void *data,PetscInt count,PetscDataType dtype)
7685c6c1daeSBarry Smith {
7695c6c1daeSBarry Smith   PetscErrorCode     ierr;
7705c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
7715c6c1daeSBarry Smith 
77203a1d59fSDave May   ierr = PetscViewerSetUp_Binary(viewer);CHKERRQ(ierr);
7735c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
7745c6c1daeSBarry Smith   if (vbinary->MPIIO) {
775ff5f5b63SDave May     ierr = PetscViewerBinaryWriteReadMPIIO(viewer,data,count,dtype,PETSC_FALSE);CHKERRQ(ierr);
7765c6c1daeSBarry Smith   } else {
7775c6c1daeSBarry Smith #endif
778ce94432eSBarry Smith     ierr = PetscBinarySynchronizedRead(PetscObjectComm((PetscObject)viewer),vbinary->fdes,data,count,dtype);CHKERRQ(ierr);
7795c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
7805c6c1daeSBarry Smith   }
7815c6c1daeSBarry Smith #endif
7825c6c1daeSBarry Smith   PetscFunctionReturn(0);
7835c6c1daeSBarry Smith }
7845c6c1daeSBarry Smith 
7855c6c1daeSBarry Smith 
7865c6c1daeSBarry Smith #undef __FUNCT__
7875c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryWrite"
7885c6c1daeSBarry Smith /*@C
7895c6c1daeSBarry Smith    PetscViewerBinaryWrite - writes to a binary file, only from the first process
7905c6c1daeSBarry Smith 
7915c6c1daeSBarry Smith    Collective on MPI_Comm
7925c6c1daeSBarry Smith 
7935c6c1daeSBarry Smith    Input Parameters:
7945c6c1daeSBarry Smith +  viewer - the binary viewer
7955c6c1daeSBarry Smith .  data - location of data
7965824c9d0SPatrick Sanan .  count - number of items of data to write
7975824c9d0SPatrick Sanan .  dtype - type of data to write
7985c6c1daeSBarry Smith -  istemp - data may be overwritten
7995c6c1daeSBarry Smith 
8005c6c1daeSBarry Smith    Level: beginner
8015c6c1daeSBarry Smith 
8025c6c1daeSBarry Smith    Notes: because byte-swapping may be done on the values in data it cannot be declared const
8035c6c1daeSBarry Smith 
8045c6c1daeSBarry Smith    Concepts: binary files
8055c6c1daeSBarry Smith 
8065c6c1daeSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(),
8075c6c1daeSBarry Smith           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(), PetscDataType
8085c6c1daeSBarry Smith           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead()
8095c6c1daeSBarry Smith @*/
8105c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryWrite(PetscViewer viewer,void *data,PetscInt count,PetscDataType dtype,PetscBool istemp)
8115c6c1daeSBarry Smith {
8125c6c1daeSBarry Smith   PetscErrorCode     ierr;
8135c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
8145c6c1daeSBarry Smith 
8155c6c1daeSBarry Smith   PetscFunctionBegin;
81603a1d59fSDave May   ierr = PetscViewerSetUp_Binary(viewer);CHKERRQ(ierr);
8175c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
8185c6c1daeSBarry Smith   if (vbinary->MPIIO) {
819ff5f5b63SDave May     ierr = PetscViewerBinaryWriteReadMPIIO(viewer,data,count,dtype,PETSC_TRUE);CHKERRQ(ierr);
8205c6c1daeSBarry Smith   } else {
8215c6c1daeSBarry Smith #endif
822ce94432eSBarry Smith     ierr = PetscBinarySynchronizedWrite(PetscObjectComm((PetscObject)viewer),vbinary->fdes,data,count,dtype,istemp);CHKERRQ(ierr);
8235c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
8245c6c1daeSBarry Smith   }
8255c6c1daeSBarry Smith #endif
8265c6c1daeSBarry Smith   PetscFunctionReturn(0);
8275c6c1daeSBarry Smith }
8285c6c1daeSBarry Smith 
8295c6c1daeSBarry Smith #undef __FUNCT__
8305c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryWriteStringArray"
8315c6c1daeSBarry Smith /*@C
8325c6c1daeSBarry Smith    PetscViewerBinaryWriteStringArray - writes to a binary file, only from the first process an array of strings
8335c6c1daeSBarry Smith 
8345c6c1daeSBarry Smith    Collective on MPI_Comm
8355c6c1daeSBarry Smith 
8365c6c1daeSBarry Smith    Input Parameters:
8375c6c1daeSBarry Smith +  viewer - the binary viewer
8385c6c1daeSBarry Smith -  data - location of the array of strings
8395c6c1daeSBarry Smith 
8405c6c1daeSBarry Smith 
8415c6c1daeSBarry Smith    Level: intermediate
8425c6c1daeSBarry Smith 
8435c6c1daeSBarry Smith    Concepts: binary files
8445c6c1daeSBarry Smith 
8455c6c1daeSBarry Smith     Notes: array of strings is null terminated
8465c6c1daeSBarry Smith 
8475c6c1daeSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(),
8485c6c1daeSBarry Smith           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
8495c6c1daeSBarry Smith           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead()
8505c6c1daeSBarry Smith @*/
8515c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryWriteStringArray(PetscViewer viewer,char **data)
8525c6c1daeSBarry Smith {
8535c6c1daeSBarry Smith   PetscErrorCode ierr;
8545c6c1daeSBarry Smith   PetscInt       i,n = 0,*sizes;
8555c6c1daeSBarry Smith 
85603a1d59fSDave May   ierr = PetscViewerSetUp_Binary(viewer);CHKERRQ(ierr);
8575c6c1daeSBarry Smith   /* count number of strings */
8585c6c1daeSBarry Smith   while (data[n++]) ;
8595c6c1daeSBarry Smith   n--;
860854ce69bSBarry Smith   ierr     = PetscMalloc1(n+1,&sizes);CHKERRQ(ierr);
8615c6c1daeSBarry Smith   sizes[0] = n;
8625c6c1daeSBarry Smith   for (i=0; i<n; i++) {
8635c6c1daeSBarry Smith     size_t tmp;
8645c6c1daeSBarry Smith     ierr       = PetscStrlen(data[i],&tmp);CHKERRQ(ierr);
8655c6c1daeSBarry Smith     sizes[i+1] = tmp + 1;   /* size includes space for the null terminator */
8665c6c1daeSBarry Smith   }
8675c6c1daeSBarry Smith   ierr = PetscViewerBinaryWrite(viewer,sizes,n+1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
8685c6c1daeSBarry Smith   for (i=0; i<n; i++) {
8695c6c1daeSBarry Smith     ierr = PetscViewerBinaryWrite(viewer,data[i],sizes[i+1],PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
8705c6c1daeSBarry Smith   }
8715c6c1daeSBarry Smith   ierr = PetscFree(sizes);CHKERRQ(ierr);
8725c6c1daeSBarry Smith   PetscFunctionReturn(0);
8735c6c1daeSBarry Smith }
8745c6c1daeSBarry Smith 
875818fcb50SDave May #undef __FUNCT__
876818fcb50SDave May #define __FUNCT__ "PetscViewerBinaryReadStringArray"
8775c6c1daeSBarry Smith /*@C
8785c6c1daeSBarry Smith    PetscViewerBinaryReadStringArray - reads a binary file an array of strings
8795c6c1daeSBarry Smith 
8805c6c1daeSBarry Smith    Collective on MPI_Comm
8815c6c1daeSBarry Smith 
8825c6c1daeSBarry Smith    Input Parameter:
8835c6c1daeSBarry Smith .  viewer - the binary viewer
8845c6c1daeSBarry Smith 
8855c6c1daeSBarry Smith    Output Parameter:
8865c6c1daeSBarry Smith .  data - location of the array of strings
8875c6c1daeSBarry Smith 
8885c6c1daeSBarry Smith    Level: intermediate
8895c6c1daeSBarry Smith 
8905c6c1daeSBarry Smith    Concepts: binary files
8915c6c1daeSBarry Smith 
8925c6c1daeSBarry Smith     Notes: array of strings is null terminated
8935c6c1daeSBarry Smith 
8945c6c1daeSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(),
8955c6c1daeSBarry Smith           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
8965c6c1daeSBarry Smith           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead()
8975c6c1daeSBarry Smith @*/
8985c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryReadStringArray(PetscViewer viewer,char ***data)
8995c6c1daeSBarry Smith {
9005c6c1daeSBarry Smith   PetscErrorCode ierr;
9015c6c1daeSBarry Smith   PetscInt       i,n,*sizes,N = 0;
9025c6c1daeSBarry Smith 
90303a1d59fSDave May   ierr = PetscViewerSetUp_Binary(viewer);CHKERRQ(ierr);
9045c6c1daeSBarry Smith   /* count number of strings */
9055c6c1daeSBarry Smith   ierr = PetscViewerBinaryRead(viewer,&n,1,PETSC_INT);CHKERRQ(ierr);
906785e854fSJed Brown   ierr = PetscMalloc1(n,&sizes);CHKERRQ(ierr);
9075c6c1daeSBarry Smith   ierr = PetscViewerBinaryRead(viewer,sizes,n,PETSC_INT);CHKERRQ(ierr);
908a297a907SKarl Rupp   for (i=0; i<n; i++) N += sizes[i];
909854ce69bSBarry Smith   ierr = PetscMalloc((n+1)*sizeof(char*) + N*sizeof(char),data);CHKERRQ(ierr);
9105c6c1daeSBarry Smith   (*data)[0] = (char*)((*data) + n + 1);
911a297a907SKarl Rupp   for (i=1; i<n; i++) (*data)[i] = (*data)[i-1] + sizes[i-1];
9125c6c1daeSBarry Smith   ierr = PetscViewerBinaryRead(viewer,(*data)[0],N,PETSC_CHAR);CHKERRQ(ierr);
913a297a907SKarl Rupp 
9145c6c1daeSBarry Smith   (*data)[n] = 0;
915a297a907SKarl Rupp 
9165c6c1daeSBarry Smith   ierr = PetscFree(sizes);CHKERRQ(ierr);
9175c6c1daeSBarry Smith   PetscFunctionReturn(0);
9185c6c1daeSBarry Smith }
9195c6c1daeSBarry Smith 
9205c6c1daeSBarry Smith #undef __FUNCT__
9215c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileGetName_Binary"
9225c6c1daeSBarry Smith PetscErrorCode PetscViewerFileGetName_Binary(PetscViewer viewer,const char **name)
9235c6c1daeSBarry Smith {
9245c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
9255c6c1daeSBarry Smith 
9265c6c1daeSBarry Smith   PetscFunctionBegin;
9275c6c1daeSBarry Smith   *name = vbinary->filename;
9285c6c1daeSBarry Smith   PetscFunctionReturn(0);
9295c6c1daeSBarry Smith }
9305c6c1daeSBarry Smith 
9315c6c1daeSBarry Smith #undef __FUNCT__
9325c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileGetMode"
9335c6c1daeSBarry Smith /*@C
9345c6c1daeSBarry Smith      PetscViewerFileGetMode - Gets the type of file to be open
9355c6c1daeSBarry Smith 
9365c6c1daeSBarry Smith     Not Collective
9375c6c1daeSBarry Smith 
9385c6c1daeSBarry Smith   Input Parameter:
9395c6c1daeSBarry Smith .  viewer - the PetscViewer; must be a binary, MATLAB, hdf, or netcdf PetscViewer
9405c6c1daeSBarry Smith 
9415c6c1daeSBarry Smith   Output Parameter:
9425c6c1daeSBarry Smith .  type - type of file
9435c6c1daeSBarry Smith $    FILE_MODE_WRITE - create new file for binary output
9445c6c1daeSBarry Smith $    FILE_MODE_READ - open existing file for binary input
9455c6c1daeSBarry Smith $    FILE_MODE_APPEND - open existing file for binary output
9465c6c1daeSBarry Smith 
9475c6c1daeSBarry Smith   Level: advanced
9485c6c1daeSBarry Smith 
9495c6c1daeSBarry Smith .seealso: PetscViewerFileSetMode(), PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinaryOpen()
9505c6c1daeSBarry Smith 
9515c6c1daeSBarry Smith @*/
9525c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileGetMode(PetscViewer viewer,PetscFileMode *type)
9535c6c1daeSBarry Smith {
9545c6c1daeSBarry Smith   PetscErrorCode ierr;
9555c6c1daeSBarry Smith 
9565c6c1daeSBarry Smith   PetscFunctionBegin;
9575c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
9585c6c1daeSBarry Smith   PetscValidPointer(type,2);
9595c6c1daeSBarry Smith   ierr = PetscUseMethod(viewer,"PetscViewerFileGetMode_C",(PetscViewer,PetscFileMode*),(viewer,type));CHKERRQ(ierr);
9605c6c1daeSBarry Smith   PetscFunctionReturn(0);
9615c6c1daeSBarry Smith }
9625c6c1daeSBarry Smith 
9635c6c1daeSBarry Smith #undef __FUNCT__
9645c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinarySetMPIIO"
9655c6c1daeSBarry Smith /*@
9665c6c1daeSBarry Smith      PetscViewerBinarySetMPIIO - Sets a binary viewer to use MPI IO for reading/writing. Must be called
9675c6c1daeSBarry Smith         before PetscViewerFileSetName()
9685c6c1daeSBarry Smith 
9695c6c1daeSBarry Smith     Logically Collective on PetscViewer
9705c6c1daeSBarry Smith 
9715c6c1daeSBarry Smith   Input Parameters:
9725c6c1daeSBarry Smith .  viewer - the PetscViewer; must be a binary
9735c6c1daeSBarry Smith 
9745c6c1daeSBarry Smith     Options Database:
9755c6c1daeSBarry Smith     -viewer_binary_mpiio : Flag for using MPI-IO
9765c6c1daeSBarry Smith 
9775c6c1daeSBarry Smith   Level: advanced
9785c6c1daeSBarry Smith 
9795c6c1daeSBarry Smith .seealso: PetscViewerFileSetMode(), PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinaryOpen()
9805c6c1daeSBarry Smith 
9815c6c1daeSBarry Smith @*/
9825c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinarySetMPIIO(PetscViewer viewer)
9835c6c1daeSBarry Smith {
9845c6c1daeSBarry Smith   PetscErrorCode ierr;
9855c6c1daeSBarry Smith 
9865c6c1daeSBarry Smith   PetscFunctionBegin;
9875c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
9885c6c1daeSBarry Smith   ierr = PetscTryMethod(viewer,"PetscViewerBinarySetMPIIO_C",(PetscViewer),(viewer));CHKERRQ(ierr);
9895c6c1daeSBarry Smith   PetscFunctionReturn(0);
9905c6c1daeSBarry Smith }
9915c6c1daeSBarry Smith 
9925c6c1daeSBarry Smith 
9935c6c1daeSBarry Smith #undef __FUNCT__
9945c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileSetMode"
9955c6c1daeSBarry Smith /*@C
9965c6c1daeSBarry Smith      PetscViewerFileSetMode - Sets the type of file to be open
9975c6c1daeSBarry Smith 
9985c6c1daeSBarry Smith     Logically Collective on PetscViewer
9995c6c1daeSBarry Smith 
10005c6c1daeSBarry Smith   Input Parameters:
10015c6c1daeSBarry Smith +  viewer - the PetscViewer; must be a binary, Matlab, hdf, or netcdf PetscViewer
10025c6c1daeSBarry Smith -  type - type of file
10035c6c1daeSBarry Smith $    FILE_MODE_WRITE - create new file for binary output
10045c6c1daeSBarry Smith $    FILE_MODE_READ - open existing file for binary input
10055c6c1daeSBarry Smith $    FILE_MODE_APPEND - open existing file for binary output
10065c6c1daeSBarry Smith 
10075c6c1daeSBarry Smith   Level: advanced
10085c6c1daeSBarry Smith 
10095c6c1daeSBarry Smith .seealso: PetscViewerFileSetMode(), PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinaryOpen()
10105c6c1daeSBarry Smith 
10115c6c1daeSBarry Smith @*/
10125c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileSetMode(PetscViewer viewer,PetscFileMode type)
10135c6c1daeSBarry Smith {
10145c6c1daeSBarry Smith   PetscErrorCode ierr;
10155c6c1daeSBarry Smith 
10165c6c1daeSBarry Smith   PetscFunctionBegin;
10175c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
10185c6c1daeSBarry Smith   PetscValidLogicalCollectiveEnum(viewer,type,2);
10195c6c1daeSBarry Smith   ierr = PetscTryMethod(viewer,"PetscViewerFileSetMode_C",(PetscViewer,PetscFileMode),(viewer,type));CHKERRQ(ierr);
10205c6c1daeSBarry Smith   PetscFunctionReturn(0);
10215c6c1daeSBarry Smith }
10225c6c1daeSBarry Smith 
10235c6c1daeSBarry Smith #undef __FUNCT__
10245c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileGetMode_Binary"
10255c6c1daeSBarry Smith PetscErrorCode PetscViewerFileGetMode_Binary(PetscViewer viewer,PetscFileMode *type)
10265c6c1daeSBarry Smith {
10275c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
10285c6c1daeSBarry Smith 
10295c6c1daeSBarry Smith   PetscFunctionBegin;
10305c6c1daeSBarry Smith   *type = vbinary->btype;
10315c6c1daeSBarry Smith   PetscFunctionReturn(0);
10325c6c1daeSBarry Smith }
10335c6c1daeSBarry Smith 
10345c6c1daeSBarry Smith #undef __FUNCT__
10355c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileSetMode_Binary"
10365c6c1daeSBarry Smith PetscErrorCode PetscViewerFileSetMode_Binary(PetscViewer viewer,PetscFileMode type)
10375c6c1daeSBarry Smith {
10385c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
10395c6c1daeSBarry Smith 
10405c6c1daeSBarry Smith   PetscFunctionBegin;
10415c6c1daeSBarry Smith   vbinary->btype = type;
10425c6c1daeSBarry Smith   PetscFunctionReturn(0);
10435c6c1daeSBarry Smith }
10445c6c1daeSBarry Smith 
1045e0385b85SDave May #undef __FUNCT__
1046e0385b85SDave May #define __FUNCT__ "PetscViewerFileSetName_Binary"
1047e0385b85SDave May PetscErrorCode PetscViewerFileSetName_Binary(PetscViewer viewer,const char name[])
1048e0385b85SDave May {
1049e0385b85SDave May   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
1050e0385b85SDave May   PetscErrorCode     ierr;
1051e0385b85SDave May 
1052e0385b85SDave May   PetscFunctionBegin;
1053c0e89164SDave May   if (vbinary->filename) { ierr = PetscFree(vbinary->filename);CHKERRQ(ierr); }
1054e0385b85SDave May   ierr = PetscStrallocpy(name,&vbinary->filename);CHKERRQ(ierr);
1055e0385b85SDave May   PetscFunctionReturn(0);
1056e0385b85SDave May }
10575c6c1daeSBarry Smith /*
10585c6c1daeSBarry Smith         Actually opens the file
10595c6c1daeSBarry Smith */
10605c6c1daeSBarry Smith #undef __FUNCT__
1061e0385b85SDave May #define __FUNCT__ "PetscViewerFileSetUp_Binary"
1062e0385b85SDave May static PetscErrorCode PetscViewerFileSetUp_Binary(PetscViewer viewer)
10635c6c1daeSBarry Smith {
10645c6c1daeSBarry Smith   PetscMPIInt        rank;
10655c6c1daeSBarry Smith   PetscErrorCode     ierr;
10665c6c1daeSBarry Smith   size_t             len;
10675c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
10685c6c1daeSBarry Smith   const char         *fname;
1069*2259a519SDave May   char               bname[PETSC_MAX_PATH_LEN],*gz;
10705c6c1daeSBarry Smith   PetscBool          found;
10715c6c1daeSBarry Smith   PetscFileMode      type = vbinary->btype;
10725c6c1daeSBarry Smith 
10735c6c1daeSBarry Smith   PetscFunctionBegin;
1074e0385b85SDave May   if (type == (PetscFileMode) -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetMode()");
1075*2259a519SDave May   if (!vbinary->filename) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetName()");
10765c6c1daeSBarry Smith   ierr = PetscViewerFileClose_Binary(viewer);CHKERRQ(ierr);
10775c6c1daeSBarry Smith 
1078ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
10795c6c1daeSBarry Smith 
10805c6c1daeSBarry Smith   /* if ends in .gz strip that off and note user wants file compressed */
10815c6c1daeSBarry Smith   vbinary->storecompressed = PETSC_FALSE;
10825c6c1daeSBarry Smith   if (!rank && type == FILE_MODE_WRITE) {
10835c6c1daeSBarry Smith     /* remove .gz if it ends library name */
10845c6c1daeSBarry Smith     ierr = PetscStrstr(vbinary->filename,".gz",&gz);CHKERRQ(ierr);
10855c6c1daeSBarry Smith     if (gz) {
10865c6c1daeSBarry Smith       ierr = PetscStrlen(gz,&len);CHKERRQ(ierr);
10875c6c1daeSBarry Smith       if (len == 3) {
10885c6c1daeSBarry Smith         *gz = 0;
10895c6c1daeSBarry Smith         vbinary->storecompressed = PETSC_TRUE;
10905c6c1daeSBarry Smith       }
10915c6c1daeSBarry Smith     }
10925c6c1daeSBarry Smith   }
10935c6c1daeSBarry Smith 
10945c6c1daeSBarry Smith   /* only first processor opens file if writeable */
10955c6c1daeSBarry Smith   if (!rank || type == FILE_MODE_READ) {
10965c6c1daeSBarry Smith 
10975c6c1daeSBarry Smith     if (type == FILE_MODE_READ) {
10985c6c1daeSBarry Smith       /* possibly get the file from remote site or compressed file */
1099ce94432eSBarry Smith       ierr  = PetscFileRetrieve(PetscObjectComm((PetscObject)viewer),vbinary->filename,bname,PETSC_MAX_PATH_LEN,&found);CHKERRQ(ierr);
11005c6c1daeSBarry Smith       fname = bname;
11015c6c1daeSBarry Smith       if (!rank && !found) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot locate file: %s on node zero",vbinary->filename);
11025c6c1daeSBarry Smith       else if (!found) {
11035c6c1daeSBarry Smith         ierr  = PetscInfo(viewer,"Nonzero processor did not locate readonly file\n");CHKERRQ(ierr);
11045c6c1daeSBarry Smith         fname = 0;
11055c6c1daeSBarry Smith       }
1106a297a907SKarl Rupp     } else fname = vbinary->filename;
11075c6c1daeSBarry Smith 
11085c6c1daeSBarry Smith #if defined(PETSC_HAVE_O_BINARY)
11095c6c1daeSBarry Smith     if (type == FILE_MODE_WRITE) {
11105c6c1daeSBarry 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);
11115c6c1daeSBarry Smith     } else if (type == FILE_MODE_READ && fname) {
11125c6c1daeSBarry 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);
11135c6c1daeSBarry Smith     } else if (type == FILE_MODE_APPEND) {
11145c6c1daeSBarry 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);
11155c6c1daeSBarry Smith     } else if (fname) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown file type");
11165c6c1daeSBarry Smith #else
11175c6c1daeSBarry Smith     if (type == FILE_MODE_WRITE) {
11185c6c1daeSBarry Smith       if ((vbinary->fdes = creat(fname,0666)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot create file %s for writing",fname);
11195c6c1daeSBarry Smith     } else if (type == FILE_MODE_READ && fname) {
11205c6c1daeSBarry 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);
11215c6c1daeSBarry Smith     } else if (type == FILE_MODE_APPEND) {
11225c6c1daeSBarry 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);
11235c6c1daeSBarry Smith     } else if (fname) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown file type");
11245c6c1daeSBarry Smith #endif
11255c6c1daeSBarry Smith   } else vbinary->fdes = -1;
11265c6c1daeSBarry Smith 
11275c6c1daeSBarry Smith   /*
11285c6c1daeSBarry Smith       try to open info file: all processors open this file if read only
11295c6c1daeSBarry Smith   */
11305c6c1daeSBarry Smith   if (!vbinary->skipinfo && (!rank || type == FILE_MODE_READ)) {
11315c6c1daeSBarry Smith     char infoname[PETSC_MAX_PATH_LEN],iname[PETSC_MAX_PATH_LEN];
11325c6c1daeSBarry Smith 
1133e0385b85SDave May     ierr = PetscStrcpy(infoname,vbinary->filename);CHKERRQ(ierr);
11345c6c1daeSBarry Smith     /* remove .gz if it ends library name */
11355c6c1daeSBarry Smith     ierr = PetscStrstr(infoname,".gz",&gz);CHKERRQ(ierr);
11365c6c1daeSBarry Smith     if (gz) {
11375c6c1daeSBarry Smith       ierr = PetscStrlen(gz,&len);CHKERRQ(ierr);
1138a297a907SKarl Rupp       if (len == 3) *gz = 0;
11395c6c1daeSBarry Smith     }
11405c6c1daeSBarry Smith 
11415c6c1daeSBarry Smith     ierr = PetscStrcat(infoname,".info");CHKERRQ(ierr);
11425c6c1daeSBarry Smith     ierr = PetscFixFilename(infoname,iname);CHKERRQ(ierr);
11435c6c1daeSBarry Smith     if (type == FILE_MODE_READ) {
1144ce94432eSBarry Smith       ierr = PetscFileRetrieve(PetscObjectComm((PetscObject)viewer),iname,infoname,PETSC_MAX_PATH_LEN,&found);CHKERRQ(ierr);
1145ce94432eSBarry Smith       ierr = PetscOptionsInsertFile(PetscObjectComm((PetscObject)viewer),infoname,PETSC_FALSE);CHKERRQ(ierr);
11465c6c1daeSBarry Smith     } else {
11475c6c1daeSBarry Smith       vbinary->fdes_info = fopen(infoname,"w");
11485c6c1daeSBarry Smith       if (!vbinary->fdes_info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open .info file %s for writing",infoname);
11495c6c1daeSBarry Smith     }
11505c6c1daeSBarry Smith   }
11515c6c1daeSBarry Smith #if defined(PETSC_USE_LOG)
1152e0385b85SDave May   PetscLogObjectState((PetscObject)viewer,"File: %s",vbinary->filename);
11535c6c1daeSBarry Smith #endif
11545c6c1daeSBarry Smith   PetscFunctionReturn(0);
11555c6c1daeSBarry Smith }
11565c6c1daeSBarry Smith 
11575c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
11585c6c1daeSBarry Smith #undef __FUNCT__
1159e0385b85SDave May #define __FUNCT__ "PetscViewerFileSetUp_BinaryMPIIO"
1160e0385b85SDave May static PetscErrorCode PetscViewerFileSetUp_BinaryMPIIO(PetscViewer viewer)
11615c6c1daeSBarry Smith {
11625c6c1daeSBarry Smith   PetscMPIInt        rank;
11635c6c1daeSBarry Smith   PetscErrorCode     ierr;
11645c6c1daeSBarry Smith   size_t             len;
11655c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
1166*2259a519SDave May   char               *gz;
11675c6c1daeSBarry Smith   PetscBool          found;
11685c6c1daeSBarry Smith   PetscFileMode      type = vbinary->btype;
11695c6c1daeSBarry Smith 
11705c6c1daeSBarry Smith   PetscFunctionBegin;
1171e0385b85SDave May   if (type == (PetscFileMode) -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetMode()");
1172*2259a519SDave May   if (!vbinary->filename) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetName()");
1173e0385b85SDave May   ierr = PetscViewerFileClose_BinaryMPIIO(viewer);CHKERRQ(ierr);
11745c6c1daeSBarry Smith 
1175ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr);
11765c6c1daeSBarry Smith 
1177a297a907SKarl Rupp   vbinary->storecompressed = PETSC_FALSE;
11785c6c1daeSBarry Smith 
11795c6c1daeSBarry Smith   /* only first processor opens file if writeable */
11805c6c1daeSBarry Smith   if (type == FILE_MODE_READ) {
1181ce94432eSBarry Smith     MPI_File_open(PetscObjectComm((PetscObject)viewer),vbinary->filename,MPI_MODE_RDONLY,MPI_INFO_NULL,&vbinary->mfdes);CHKERRQ(ierr);
11825c6c1daeSBarry Smith   } else if (type == FILE_MODE_WRITE) {
1183ce94432eSBarry Smith     MPI_File_open(PetscObjectComm((PetscObject)viewer),vbinary->filename,MPI_MODE_WRONLY | MPI_MODE_CREATE,MPI_INFO_NULL,&vbinary->mfdes);CHKERRQ(ierr);
11845c6c1daeSBarry Smith   }
11855c6c1daeSBarry Smith 
11865c6c1daeSBarry Smith   /*
11875c6c1daeSBarry Smith       try to open info file: all processors open this file if read only
11885c6c1daeSBarry Smith 
11895c6c1daeSBarry Smith       Below is identical code to the code for Binary above, should be put in seperate routine
11905c6c1daeSBarry Smith   */
11915c6c1daeSBarry Smith   if (!vbinary->skipinfo && (!rank || type == FILE_MODE_READ)) {
11925c6c1daeSBarry Smith     char infoname[PETSC_MAX_PATH_LEN],iname[PETSC_MAX_PATH_LEN];
11935c6c1daeSBarry Smith 
1194e0385b85SDave May     ierr = PetscStrcpy(infoname,vbinary->filename);CHKERRQ(ierr);
11955c6c1daeSBarry Smith     /* remove .gz if it ends library name */
11965c6c1daeSBarry Smith     ierr = PetscStrstr(infoname,".gz",&gz);CHKERRQ(ierr);
11975c6c1daeSBarry Smith     if (gz) {
11985c6c1daeSBarry Smith       ierr = PetscStrlen(gz,&len);CHKERRQ(ierr);
1199a297a907SKarl Rupp       if (len == 3) *gz = 0;
12005c6c1daeSBarry Smith     }
12015c6c1daeSBarry Smith 
12025c6c1daeSBarry Smith     ierr = PetscStrcat(infoname,".info");CHKERRQ(ierr);
12035c6c1daeSBarry Smith     ierr = PetscFixFilename(infoname,iname);CHKERRQ(ierr);
12045c6c1daeSBarry Smith     if (type == FILE_MODE_READ) {
1205ce94432eSBarry Smith       ierr = PetscFileRetrieve(PetscObjectComm((PetscObject)viewer),iname,infoname,PETSC_MAX_PATH_LEN,&found);CHKERRQ(ierr);
1206ce94432eSBarry Smith       ierr = PetscOptionsInsertFile(PetscObjectComm((PetscObject)viewer),infoname,PETSC_FALSE);CHKERRQ(ierr);
12075c6c1daeSBarry Smith     } else {
12085c6c1daeSBarry Smith       vbinary->fdes_info = fopen(infoname,"w");
12095c6c1daeSBarry Smith       if (!vbinary->fdes_info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open .info file %s for writing",infoname);
12105c6c1daeSBarry Smith     }
12115c6c1daeSBarry Smith   }
12125c6c1daeSBarry Smith #if defined(PETSC_USE_LOG)
1213e0385b85SDave May   PetscLogObjectState((PetscObject)viewer,"File: %s",vbinary->filename);
12145c6c1daeSBarry Smith #endif
12155c6c1daeSBarry Smith   PetscFunctionReturn(0);
12165c6c1daeSBarry Smith }
12175c6c1daeSBarry Smith 
12185c6c1daeSBarry Smith #undef __FUNCT__
12195c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinarySetMPIIO_Binary"
12205c6c1daeSBarry Smith PetscErrorCode PetscViewerBinarySetMPIIO_Binary(PetscViewer viewer)
12215c6c1daeSBarry Smith {
12225c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
12235c6c1daeSBarry Smith   PetscFunctionBegin;
12245c6c1daeSBarry Smith   vbinary->MPIIO       = PETSC_TRUE;
12255c6c1daeSBarry Smith   PetscFunctionReturn(0);
12265c6c1daeSBarry Smith }
12275c6c1daeSBarry Smith #endif
12285c6c1daeSBarry Smith 
12292bf49c77SBarry Smith #undef __FUNCT__
12302bf49c77SBarry Smith #define __FUNCT__ "PetscViewerView_Binary"
12312bf49c77SBarry Smith PetscErrorCode  PetscViewerView_Binary(PetscViewer v,PetscViewer viewer)
12322bf49c77SBarry Smith {
12332bf49c77SBarry Smith   PetscErrorCode     ierr;
12342bf49c77SBarry Smith   PetscViewer_Binary *binary = (PetscViewer_Binary*)v->data;
12352bf49c77SBarry Smith 
12362bf49c77SBarry Smith   PetscFunctionBegin;
12372bf49c77SBarry Smith   if (binary->filename) {
12382bf49c77SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"Filename: %s\n",binary->filename);CHKERRQ(ierr);
12392bf49c77SBarry Smith   }
12402bf49c77SBarry Smith   PetscFunctionReturn(0);
12412bf49c77SBarry Smith }
12422bf49c77SBarry Smith 
12435c6c1daeSBarry Smith #undef __FUNCT__
124403a1d59fSDave May #define __FUNCT__ "PetscViewerSetUp_Binary"
124503a1d59fSDave May static PetscErrorCode PetscViewerSetUp_Binary(PetscViewer v)
124603a1d59fSDave May {
124703a1d59fSDave May   PetscErrorCode     ierr;
124803a1d59fSDave May   PetscViewer_Binary *binary = (PetscViewer_Binary*)v->data;
1249e0385b85SDave May 
125003a1d59fSDave May   PetscFunctionBegin;
125103a1d59fSDave May   if (binary->setupcalled) { PetscFunctionReturn(0); }
1252da07bb01SDave May   if (!binary->setfromoptionscalled) { ierr = PetscViewerSetFromOptions(v);CHKERRQ(ierr); }
125303a1d59fSDave May 
1254e0385b85SDave May #if defined(PETSC_HAVE_MPIIO)
1255e0385b85SDave May   if (binary->MPIIO) {
1256e0385b85SDave May     ierr = PetscViewerFileSetUp_BinaryMPIIO(v);CHKERRQ(ierr);
1257e0385b85SDave May   } else {
1258e0385b85SDave May #endif
1259e0385b85SDave May     ierr = PetscViewerFileSetUp_Binary(v);CHKERRQ(ierr);
1260e0385b85SDave May #if defined(PETSC_HAVE_MPIIO)
1261e0385b85SDave May   }
1262e0385b85SDave May #endif
126303a1d59fSDave May   binary->setupcalled = PETSC_TRUE;
126403a1d59fSDave May 
126503a1d59fSDave May   PetscFunctionReturn(0);
126603a1d59fSDave May }
126703a1d59fSDave May 
126803a1d59fSDave May #undef __FUNCT__
126903a1d59fSDave May #define __FUNCT__ "PetscViewerSetFromOptions_Binary"
127003a1d59fSDave May static PetscErrorCode PetscViewerSetFromOptions_Binary(PetscOptions *PetscOptionsObject,PetscViewer v)
127103a1d59fSDave May {
127203a1d59fSDave May   PetscErrorCode     ierr;
127303a1d59fSDave May   PetscViewer_Binary *binary = (PetscViewer_Binary*)v->data;
127403a1d59fSDave May   PetscBool          flg;
127503a1d59fSDave May #if defined(PETSC_HAVE_MPIIO)
127603a1d59fSDave May   PetscBool          useMPIIO = PETSC_FALSE;
127703a1d59fSDave May #endif
1278e0385b85SDave May 
127903a1d59fSDave May   PetscFunctionBegin;
128003a1d59fSDave May   ierr = PetscOptionsHead(PetscOptionsObject,"Binary PetscViewer Options");CHKERRQ(ierr);
128103a1d59fSDave May   ierr = PetscOptionsBool("-viewer_binary_skip_info","Skip writing/reading .info file","PetscViewerBinarySkipInfo",PETSC_FALSE,&binary->skipinfo,&flg);CHKERRQ(ierr);
128203a1d59fSDave May   ierr = PetscOptionsBool("-viewer_binary_skip_options","Skip parsing vec load options","PetscViewerBinarySetSkipOptions",PETSC_TRUE,&binary->skipoptions,&flg);CHKERRQ(ierr);
128303a1d59fSDave May   ierr = PetscOptionsBool("-viewer_binary_skip_header","Skip writing/reading header information","PetscViewerBinarySetSkipHeader",PETSC_FALSE,&binary->skipheader,&flg);CHKERRQ(ierr);
128403a1d59fSDave May #if defined(PETSC_HAVE_MPIIO)
128503a1d59fSDave May   ierr = PetscOptionsBool("-viewer_binary_mpiio","Use MPI-IO functionality to write binary file","PetscViewerBinarySetMPIIO",PETSC_FALSE,&useMPIIO,&flg);CHKERRQ(ierr);
128603a1d59fSDave May   if (useMPIIO) {
128703a1d59fSDave May     ierr = PetscViewerBinarySetMPIIO(v);CHKERRQ(ierr);
128803a1d59fSDave May   }
128903a1d59fSDave May #endif
129003a1d59fSDave May   ierr = PetscOptionsTail();CHKERRQ(ierr);
1291da07bb01SDave May   binary->setfromoptionscalled = PETSC_FALSE;
129203a1d59fSDave May   PetscFunctionReturn(0);
129303a1d59fSDave May }
129403a1d59fSDave May 
129503a1d59fSDave May #undef __FUNCT__
12965c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerCreate_Binary"
12978cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscViewerCreate_Binary(PetscViewer v)
12985c6c1daeSBarry Smith {
12995c6c1daeSBarry Smith   PetscErrorCode     ierr;
13005c6c1daeSBarry Smith   PetscViewer_Binary *vbinary;
13015c6c1daeSBarry Smith 
13025c6c1daeSBarry Smith   PetscFunctionBegin;
1303b00a9115SJed Brown   ierr                     = PetscNewLog(v,&vbinary);CHKERRQ(ierr);
13045c6c1daeSBarry Smith   v->data                  = (void*)vbinary;
130503a1d59fSDave May   v->ops->setfromoptions   = PetscViewerSetFromOptions_Binary;
13065c6c1daeSBarry Smith   v->ops->destroy          = PetscViewerDestroy_Binary;
13072bf49c77SBarry Smith   v->ops->view             = PetscViewerView_Binary;
13085c6c1daeSBarry Smith   v->ops->flush            = 0;
13095c6c1daeSBarry Smith   vbinary->fdes_info       = 0;
13105c6c1daeSBarry Smith   vbinary->fdes            = 0;
13115c6c1daeSBarry Smith   vbinary->skipinfo        = PETSC_FALSE;
13125c6c1daeSBarry Smith   vbinary->skipoptions     = PETSC_TRUE;
13135c6c1daeSBarry Smith   vbinary->skipheader      = PETSC_FALSE;
131403a1d59fSDave May   vbinary->setupcalled     = PETSC_FALSE;
1315da07bb01SDave May   vbinary->setfromoptionscalled = PETSC_FALSE;
13165c6c1daeSBarry Smith   v->ops->getsingleton     = PetscViewerGetSingleton_Binary;
13175c6c1daeSBarry Smith   v->ops->restoresingleton = PetscViewerRestoreSingleton_Binary;
13181d641e7bSMichael Lange   v->ops->read             = PetscViewerBinaryRead;
13195c6c1daeSBarry Smith   vbinary->btype           = (PetscFileMode) -1;
13205c6c1daeSBarry Smith   vbinary->storecompressed = PETSC_FALSE;
13215c6c1daeSBarry Smith   vbinary->filename        = 0;
13225c6c1daeSBarry Smith   vbinary->flowcontrol     = 256; /* seems a good number for Cray XT-5 */
13235c6c1daeSBarry Smith 
1324bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetFlowControl_C",PetscViewerBinaryGetFlowControl_Binary);CHKERRQ(ierr);
1325bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetFlowControl_C",PetscViewerBinarySetFlowControl_Binary);CHKERRQ(ierr);
1326bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetSkipHeader_C",PetscViewerBinarySetSkipHeader_Binary);CHKERRQ(ierr);
1327bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetSkipHeader_C",PetscViewerBinaryGetSkipHeader_Binary);CHKERRQ(ierr);
1328bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetInfoPointer_C",PetscViewerBinaryGetInfoPointer_Binary);CHKERRQ(ierr);
1329bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetName_C",PetscViewerFileSetName_Binary);CHKERRQ(ierr);
1330bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetMode_C",PetscViewerFileSetMode_Binary);CHKERRQ(ierr);
1331bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileGetMode_C",PetscViewerFileGetMode_Binary);CHKERRQ(ierr);
1332bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileGetName_C",PetscViewerFileGetName_Binary);CHKERRQ(ierr);
13335c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
1334bdf89e91SBarry Smith   ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetMPIIO_C",PetscViewerBinarySetMPIIO_Binary);CHKERRQ(ierr);
13355c6c1daeSBarry Smith #endif
13365c6c1daeSBarry Smith   PetscFunctionReturn(0);
13375c6c1daeSBarry Smith }
13385c6c1daeSBarry Smith 
13395c6c1daeSBarry Smith /* ---------------------------------------------------------------------*/
13405c6c1daeSBarry Smith /*
13415c6c1daeSBarry Smith     The variable Petsc_Viewer_Binary_keyval is used to indicate an MPI attribute that
13425c6c1daeSBarry Smith   is attached to a communicator, in this case the attribute is a PetscViewer.
13435c6c1daeSBarry Smith */
13445c6c1daeSBarry Smith static int Petsc_Viewer_Binary_keyval = MPI_KEYVAL_INVALID;
13455c6c1daeSBarry Smith 
13465c6c1daeSBarry Smith #undef __FUNCT__
13475c6c1daeSBarry Smith #define __FUNCT__ "PETSC_VIEWER_BINARY_"
13485c6c1daeSBarry Smith /*@C
13495c6c1daeSBarry Smith      PETSC_VIEWER_BINARY_ - Creates a binary PetscViewer shared by all processors
13505c6c1daeSBarry Smith                      in a communicator.
13515c6c1daeSBarry Smith 
13525c6c1daeSBarry Smith      Collective on MPI_Comm
13535c6c1daeSBarry Smith 
13545c6c1daeSBarry Smith      Input Parameter:
13555c6c1daeSBarry Smith .    comm - the MPI communicator to share the binary PetscViewer
13565c6c1daeSBarry Smith 
13575c6c1daeSBarry Smith      Level: intermediate
13585c6c1daeSBarry Smith 
13595c6c1daeSBarry Smith    Options Database Keys:
13605c6c1daeSBarry Smith +    -viewer_binary_filename <name>
13615c6c1daeSBarry Smith .    -viewer_binary_skip_info
13625c6c1daeSBarry Smith -    -viewer_binary_skip_options
13635c6c1daeSBarry Smith 
13645c6c1daeSBarry Smith    Environmental variables:
13655c6c1daeSBarry Smith -   PETSC_VIEWER_BINARY_FILENAME
13665c6c1daeSBarry Smith 
13675c6c1daeSBarry Smith      Notes:
13685c6c1daeSBarry Smith      Unlike almost all other PETSc routines, PETSC_VIEWER_BINARY_ does not return
13695c6c1daeSBarry Smith      an error code.  The binary PetscViewer is usually used in the form
13705c6c1daeSBarry Smith $       XXXView(XXX object,PETSC_VIEWER_BINARY_(comm));
13715c6c1daeSBarry Smith 
13725c6c1daeSBarry Smith .seealso: PETSC_VIEWER_BINARY_WORLD, PETSC_VIEWER_BINARY_SELF, PetscViewerBinaryOpen(), PetscViewerCreate(),
13735c6c1daeSBarry Smith           PetscViewerDestroy()
13745c6c1daeSBarry Smith @*/
13755c6c1daeSBarry Smith PetscViewer  PETSC_VIEWER_BINARY_(MPI_Comm comm)
13765c6c1daeSBarry Smith {
13775c6c1daeSBarry Smith   PetscErrorCode ierr;
13785c6c1daeSBarry Smith   PetscBool      flg;
13795c6c1daeSBarry Smith   PetscViewer    viewer;
13805c6c1daeSBarry Smith   char           fname[PETSC_MAX_PATH_LEN];
13815c6c1daeSBarry Smith   MPI_Comm       ncomm;
13825c6c1daeSBarry Smith 
13835c6c1daeSBarry Smith   PetscFunctionBegin;
1384efca3c55SSatish 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);}
13855c6c1daeSBarry Smith   if (Petsc_Viewer_Binary_keyval == MPI_KEYVAL_INVALID) {
13865c6c1daeSBarry Smith     ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Binary_keyval,0);
1387efca3c55SSatish Balay     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
13885c6c1daeSBarry Smith   }
13895c6c1daeSBarry Smith   ierr = MPI_Attr_get(ncomm,Petsc_Viewer_Binary_keyval,(void**)&viewer,(int*)&flg);
1390efca3c55SSatish Balay   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
13915c6c1daeSBarry Smith   if (!flg) { /* PetscViewer not yet created */
13925c6c1daeSBarry Smith     ierr = PetscOptionsGetenv(ncomm,"PETSC_VIEWER_BINARY_FILENAME",fname,PETSC_MAX_PATH_LEN,&flg);
1393efca3c55SSatish Balay     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
13945c6c1daeSBarry Smith     if (!flg) {
13955c6c1daeSBarry Smith       ierr = PetscStrcpy(fname,"binaryoutput");
1396efca3c55SSatish Balay       if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
13975c6c1daeSBarry Smith     }
13985c6c1daeSBarry Smith     ierr = PetscViewerBinaryOpen(ncomm,fname,FILE_MODE_WRITE,&viewer);
1399efca3c55SSatish Balay     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
14005c6c1daeSBarry Smith     ierr = PetscObjectRegisterDestroy((PetscObject)viewer);
1401efca3c55SSatish Balay     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
14025c6c1daeSBarry Smith     ierr = MPI_Attr_put(ncomm,Petsc_Viewer_Binary_keyval,(void*)viewer);
1403efca3c55SSatish Balay     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
14045c6c1daeSBarry Smith   }
14055c6c1daeSBarry Smith   ierr = PetscCommDestroy(&ncomm);
1406efca3c55SSatish Balay   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
14075c6c1daeSBarry Smith   PetscFunctionReturn(viewer);
14085c6c1daeSBarry Smith }
1409