xref: /petsc/src/sys/classes/viewer/impls/binary/binv.c (revision 2bf49c77dedd4b28e6efd10dcebd37482390fa09)
15c6c1daeSBarry Smith 
25c6c1daeSBarry Smith #include <petsc-private/viewerimpl.h>    /*I   "petscsys.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 */
265c6c1daeSBarry Smith } PetscViewer_Binary;
275c6c1daeSBarry Smith 
285c6c1daeSBarry Smith #undef __FUNCT__
295c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerGetSingleton_Binary"
305c6c1daeSBarry Smith PetscErrorCode PetscViewerGetSingleton_Binary(PetscViewer viewer,PetscViewer *outviewer)
315c6c1daeSBarry Smith {
325c6c1daeSBarry Smith   int                rank;
335c6c1daeSBarry Smith   PetscErrorCode     ierr;
345c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data,*obinary;
355c6c1daeSBarry Smith 
365c6c1daeSBarry Smith   PetscFunctionBegin;
375c6c1daeSBarry Smith   ierr = MPI_Comm_rank(((PetscObject)viewer)->comm,&rank);CHKERRQ(ierr);
385c6c1daeSBarry Smith   if (!rank) {
395c6c1daeSBarry Smith     ierr    = PetscViewerCreate(PETSC_COMM_SELF,outviewer);CHKERRQ(ierr);
405c6c1daeSBarry Smith     ierr    = PetscViewerSetType(*outviewer,PETSCVIEWERBINARY);CHKERRQ(ierr);
415c6c1daeSBarry Smith     obinary = (PetscViewer_Binary*)(*outviewer)->data;
425c6c1daeSBarry Smith     ierr    = PetscMemcpy(obinary,vbinary,sizeof(PetscViewer_Binary));CHKERRQ(ierr);
435c6c1daeSBarry Smith   } else {
445c6c1daeSBarry Smith     *outviewer = 0;
455c6c1daeSBarry Smith   }
465c6c1daeSBarry Smith   PetscFunctionReturn(0);
475c6c1daeSBarry Smith }
485c6c1daeSBarry Smith 
495c6c1daeSBarry Smith #undef __FUNCT__
505c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerRestoreSingleton_Binary"
515c6c1daeSBarry Smith PetscErrorCode PetscViewerRestoreSingleton_Binary(PetscViewer viewer,PetscViewer *outviewer)
525c6c1daeSBarry Smith {
535c6c1daeSBarry Smith   PetscErrorCode ierr;
545c6c1daeSBarry Smith   PetscErrorCode rank;
555c6c1daeSBarry Smith 
565c6c1daeSBarry Smith   PetscFunctionBegin;
575c6c1daeSBarry Smith   ierr = MPI_Comm_rank(((PetscObject)viewer)->comm,&rank);CHKERRQ(ierr);
585c6c1daeSBarry Smith   if (!rank) {
595c6c1daeSBarry Smith     ierr = PetscFree((*outviewer)->data);CHKERRQ(ierr);
605c6c1daeSBarry Smith     ierr = PetscHeaderDestroy(outviewer);CHKERRQ(ierr);
615c6c1daeSBarry Smith   }
625c6c1daeSBarry Smith   PetscFunctionReturn(0);
635c6c1daeSBarry Smith }
645c6c1daeSBarry Smith 
655c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
665c6c1daeSBarry Smith #undef __FUNCT__
675c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetMPIIOOffset"
685c6c1daeSBarry Smith /*@C
695c6c1daeSBarry Smith     PetscViewerBinaryGetMPIIOOffset - Gets the current offset that should be passed to MPI_File_set_view()
705c6c1daeSBarry Smith 
715c6c1daeSBarry Smith     Not Collective
725c6c1daeSBarry Smith 
735c6c1daeSBarry Smith     Input Parameter:
745c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
755c6c1daeSBarry Smith 
765c6c1daeSBarry Smith     Output Parameter:
775c6c1daeSBarry Smith .    off - the current offset
785c6c1daeSBarry Smith 
795c6c1daeSBarry Smith     Level: advanced
805c6c1daeSBarry Smith 
815c6c1daeSBarry Smith     Fortran Note:
825c6c1daeSBarry Smith     This routine is not supported in Fortran.
835c6c1daeSBarry Smith 
845c6c1daeSBarry Smith     Use PetscViewerBinaryAddMPIIOOffset() to increase this value after you have written a view.
855c6c1daeSBarry Smith 
865c6c1daeSBarry Smith   Concepts: file descriptor^getting
875c6c1daeSBarry Smith   Concepts: PetscViewerBinary^accessing file descriptor
885c6c1daeSBarry Smith 
895c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer()
905c6c1daeSBarry Smith @*/
915c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryGetMPIIOOffset(PetscViewer viewer,MPI_Offset *off)
925c6c1daeSBarry Smith {
935c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
945c6c1daeSBarry Smith 
955c6c1daeSBarry Smith   PetscFunctionBegin;
965c6c1daeSBarry Smith   *off = vbinary->moff;
975c6c1daeSBarry Smith   PetscFunctionReturn(0);
985c6c1daeSBarry Smith }
995c6c1daeSBarry Smith 
1005c6c1daeSBarry Smith #undef __FUNCT__
1015c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryAddMPIIOOffset"
1025c6c1daeSBarry Smith /*@C
1035c6c1daeSBarry Smith     PetscViewerBinaryAddMPIIOOffset - Adds to the current offset that should be passed to MPI_File_set_view()
1045c6c1daeSBarry Smith 
1055c6c1daeSBarry Smith     Not Collective
1065c6c1daeSBarry Smith 
1075c6c1daeSBarry Smith     Input Parameters:
1085c6c1daeSBarry Smith +   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
1095c6c1daeSBarry Smith -    off - the addition to the offset
1105c6c1daeSBarry Smith 
1115c6c1daeSBarry Smith     Level: advanced
1125c6c1daeSBarry Smith 
1135c6c1daeSBarry Smith     Fortran Note:
1145c6c1daeSBarry Smith     This routine is not supported in Fortran.
1155c6c1daeSBarry Smith 
1165c6c1daeSBarry Smith     Use PetscViewerBinaryGetMPIIOOffset() to get the value that you should pass to MPI_File_set_view()
1175c6c1daeSBarry Smith 
1185c6c1daeSBarry Smith   Concepts: file descriptor^getting
1195c6c1daeSBarry Smith   Concepts: PetscViewerBinary^accessing file descriptor
1205c6c1daeSBarry Smith 
1215c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer()
1225c6c1daeSBarry Smith @*/
1235c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryAddMPIIOOffset(PetscViewer viewer,MPI_Offset off)
1245c6c1daeSBarry Smith {
1255c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
1265c6c1daeSBarry Smith 
1275c6c1daeSBarry Smith   PetscFunctionBegin;
1285c6c1daeSBarry Smith   vbinary->moff += off;
1295c6c1daeSBarry Smith   PetscFunctionReturn(0);
1305c6c1daeSBarry Smith }
1315c6c1daeSBarry Smith 
1325c6c1daeSBarry Smith #undef __FUNCT__
1335c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetMPIIODescriptor"
1345c6c1daeSBarry Smith /*@C
1355c6c1daeSBarry Smith     PetscViewerBinaryGetMPIIODescriptor - Extracts the MPI IO file descriptor from a PetscViewer.
1365c6c1daeSBarry Smith 
1375c6c1daeSBarry Smith     Not Collective
1385c6c1daeSBarry Smith 
1395c6c1daeSBarry Smith     Input Parameter:
1405c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
1415c6c1daeSBarry Smith 
1425c6c1daeSBarry Smith     Output Parameter:
1435c6c1daeSBarry Smith .   fdes - file descriptor
1445c6c1daeSBarry Smith 
1455c6c1daeSBarry Smith     Level: advanced
1465c6c1daeSBarry Smith 
1475c6c1daeSBarry Smith     Fortran Note:
1485c6c1daeSBarry Smith     This routine is not supported in Fortran.
1495c6c1daeSBarry Smith 
1505c6c1daeSBarry Smith   Concepts: file descriptor^getting
1515c6c1daeSBarry Smith   Concepts: PetscViewerBinary^accessing file descriptor
1525c6c1daeSBarry Smith 
1535c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer()
1545c6c1daeSBarry Smith @*/
1555c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryGetMPIIODescriptor(PetscViewer viewer,MPI_File *fdes)
1565c6c1daeSBarry Smith {
1575c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
1585c6c1daeSBarry Smith 
1595c6c1daeSBarry Smith   PetscFunctionBegin;
1605c6c1daeSBarry Smith   *fdes = vbinary->mfdes;
1615c6c1daeSBarry Smith   PetscFunctionReturn(0);
1625c6c1daeSBarry Smith }
1635c6c1daeSBarry Smith 
1645c6c1daeSBarry Smith #undef __FUNCT__
1655c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetMPIIO"
1665c6c1daeSBarry Smith /*@C
1675c6c1daeSBarry Smith     PetscViewerBinaryGetMPIIO - Returns PETSC_TRUE if the binary viewer is an MPI viewer.
1685c6c1daeSBarry Smith 
1695c6c1daeSBarry Smith     Not Collective
1705c6c1daeSBarry Smith 
1715c6c1daeSBarry Smith     Input Parameter:
1725c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
1735c6c1daeSBarry Smith 
1745c6c1daeSBarry Smith     Output Parameter:
1755c6c1daeSBarry Smith -   flg - PETSC_TRUE if MPI IO is being used
1765c6c1daeSBarry Smith 
1775c6c1daeSBarry Smith     Options Database:
1785c6c1daeSBarry Smith     -viewer_binary_mpiio : Flag for using MPI-IO
1795c6c1daeSBarry Smith 
1805c6c1daeSBarry Smith     Level: advanced
1815c6c1daeSBarry Smith 
1825c6c1daeSBarry Smith     Fortran Note:
1835c6c1daeSBarry Smith     This routine is not supported in Fortran.
1845c6c1daeSBarry Smith 
1855c6c1daeSBarry Smith   Concepts: file descriptor^getting
1865c6c1daeSBarry Smith   Concepts: PetscViewerBinary^accessing file descriptor
1875c6c1daeSBarry Smith 
1885c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetInfoPointer()
1895c6c1daeSBarry Smith @*/
1905c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryGetMPIIO(PetscViewer viewer,PetscBool  *flg)
1915c6c1daeSBarry Smith {
1925c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
1935c6c1daeSBarry Smith 
1945c6c1daeSBarry Smith   PetscFunctionBegin;
1955c6c1daeSBarry Smith   *flg = vbinary->MPIIO;
1965c6c1daeSBarry Smith   PetscFunctionReturn(0);
1975c6c1daeSBarry Smith }
1985c6c1daeSBarry Smith #endif
1995c6c1daeSBarry Smith 
2005c6c1daeSBarry Smith EXTERN_C_BEGIN
2015c6c1daeSBarry Smith #undef __FUNCT__
2025c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetFlowControl_Binary"
2035c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryGetFlowControl_Binary(PetscViewer viewer,PetscInt *fc)
2045c6c1daeSBarry Smith {
2055c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
2065c6c1daeSBarry Smith 
2075c6c1daeSBarry Smith   PetscFunctionBegin;
2085c6c1daeSBarry Smith   *fc = vbinary->flowcontrol;
2095c6c1daeSBarry Smith   PetscFunctionReturn(0);
2105c6c1daeSBarry Smith }
2115c6c1daeSBarry Smith EXTERN_C_END
2125c6c1daeSBarry Smith 
2135c6c1daeSBarry Smith #undef __FUNCT__
2145c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetFlowControl"
2155c6c1daeSBarry Smith /*@C
2165c6c1daeSBarry Smith     PetscViewerBinaryGetFlowControl - Returns how many messages are allowed to outstanding at the same time during parallel IO reads/writes
2175c6c1daeSBarry Smith 
2185c6c1daeSBarry Smith     Not Collective
2195c6c1daeSBarry Smith 
2205c6c1daeSBarry Smith     Input Parameter:
2215c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
2225c6c1daeSBarry Smith 
2235c6c1daeSBarry Smith     Output Parameter:
2245c6c1daeSBarry Smith .   fc - the number of messages
2255c6c1daeSBarry Smith 
2265c6c1daeSBarry Smith     Level: advanced
2275c6c1daeSBarry Smith 
2285c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer(), PetscViewerBinarySetFlowControl()
2295c6c1daeSBarry Smith 
2305c6c1daeSBarry Smith @*/
2315c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryGetFlowControl(PetscViewer viewer,PetscInt *fc)
2325c6c1daeSBarry Smith {
2335c6c1daeSBarry Smith   PetscErrorCode ierr;
2345c6c1daeSBarry Smith 
2355c6c1daeSBarry Smith   PetscFunctionBegin;
2365c6c1daeSBarry Smith   *fc = 256;
2375c6c1daeSBarry Smith   ierr = PetscTryMethod(viewer,"PetscViewerBinaryGetFlowControl_C",(PetscViewer,PetscInt *),(viewer,fc));CHKERRQ(ierr);
2385c6c1daeSBarry Smith   PetscFunctionReturn(0);
2395c6c1daeSBarry Smith }
2405c6c1daeSBarry Smith 
2415c6c1daeSBarry Smith EXTERN_C_BEGIN
2425c6c1daeSBarry Smith #undef __FUNCT__
2435c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinarySetFlowControl_Binary"
2445c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinarySetFlowControl_Binary(PetscViewer viewer,PetscInt fc)
2455c6c1daeSBarry Smith {
2465c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
2475c6c1daeSBarry Smith 
2485c6c1daeSBarry Smith   PetscFunctionBegin;
2495c6c1daeSBarry Smith   if (fc <= 1) SETERRQ1(((PetscObject)viewer)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Flow control count must be greater than 1, %D was set",fc);
2505c6c1daeSBarry Smith   vbinary->flowcontrol = fc;
2515c6c1daeSBarry Smith   PetscFunctionReturn(0);
2525c6c1daeSBarry Smith }
2535c6c1daeSBarry Smith EXTERN_C_END
2545c6c1daeSBarry Smith 
2555c6c1daeSBarry Smith #undef __FUNCT__
2565c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinarySetFlowControl"
2575c6c1daeSBarry Smith /*@C
2585c6c1daeSBarry Smith     PetscViewerBinarySetFlowControl - Returns how many messages are allowed to outstanding at the same time during parallel IO reads/writes
2595c6c1daeSBarry Smith 
2605c6c1daeSBarry Smith     Not Collective
2615c6c1daeSBarry Smith 
2625c6c1daeSBarry Smith     Input Parameter:
2635c6c1daeSBarry Smith +   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
2645c6c1daeSBarry Smith -   fc - the number of messages, defaults to 256
2655c6c1daeSBarry Smith 
2665c6c1daeSBarry Smith     Level: advanced
2675c6c1daeSBarry Smith 
2685c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer(), PetscViewerBinaryGetFlowControl()
2695c6c1daeSBarry Smith 
2705c6c1daeSBarry Smith @*/
2715c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinarySetFlowControl(PetscViewer viewer,PetscInt fc)
2725c6c1daeSBarry Smith {
2735c6c1daeSBarry Smith   PetscErrorCode ierr;
2745c6c1daeSBarry Smith 
2755c6c1daeSBarry Smith   PetscFunctionBegin;
2765c6c1daeSBarry Smith   ierr = PetscUseMethod(viewer,"PetscViewerBinarySetFlowControl_C",(PetscViewer,PetscInt),(viewer,fc));CHKERRQ(ierr);
2775c6c1daeSBarry Smith   PetscFunctionReturn(0);
2785c6c1daeSBarry Smith }
2795c6c1daeSBarry Smith 
2805c6c1daeSBarry Smith #undef __FUNCT__
2815c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetDescriptor"
2825c6c1daeSBarry Smith /*@C
2835c6c1daeSBarry Smith     PetscViewerBinaryGetDescriptor - Extracts the file descriptor from a PetscViewer.
2845c6c1daeSBarry Smith 
2855c6c1daeSBarry Smith     Not Collective
2865c6c1daeSBarry Smith 
2875c6c1daeSBarry Smith     Input Parameter:
2885c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
2895c6c1daeSBarry Smith 
2905c6c1daeSBarry Smith     Output Parameter:
2915c6c1daeSBarry Smith .   fdes - file descriptor
2925c6c1daeSBarry Smith 
2935c6c1daeSBarry Smith     Level: advanced
2945c6c1daeSBarry Smith 
2955c6c1daeSBarry Smith     Notes:
2965c6c1daeSBarry Smith       For writable binary PetscViewers, the descriptor will only be valid for the
2975c6c1daeSBarry Smith     first processor in the communicator that shares the PetscViewer. For readable
2985c6c1daeSBarry Smith     files it will only be valid on nodes that have the file. If node 0 does not
2995c6c1daeSBarry Smith     have the file it generates an error even if another node does have the file.
3005c6c1daeSBarry Smith 
3015c6c1daeSBarry Smith     Fortran Note:
3025c6c1daeSBarry Smith     This routine is not supported in Fortran.
3035c6c1daeSBarry Smith 
3045c6c1daeSBarry Smith   Concepts: file descriptor^getting
3055c6c1daeSBarry Smith   Concepts: PetscViewerBinary^accessing file descriptor
3065c6c1daeSBarry Smith 
3075c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer()
3085c6c1daeSBarry Smith @*/
3095c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryGetDescriptor(PetscViewer viewer,int *fdes)
3105c6c1daeSBarry Smith {
3115c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
3125c6c1daeSBarry Smith 
3135c6c1daeSBarry Smith   PetscFunctionBegin;
3145c6c1daeSBarry Smith   *fdes = vbinary->fdes;
3155c6c1daeSBarry Smith   PetscFunctionReturn(0);
3165c6c1daeSBarry Smith }
3175c6c1daeSBarry Smith 
3185c6c1daeSBarry Smith #undef __FUNCT__
3195c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinarySkipInfo"
3205c6c1daeSBarry Smith /*@
3215c6c1daeSBarry Smith     PetscViewerBinarySkipInfo - Binary file will not have .info file created with it
3225c6c1daeSBarry Smith 
3235c6c1daeSBarry Smith     Not Collective
3245c6c1daeSBarry Smith 
3255c6c1daeSBarry Smith     Input Paramter:
3265c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerCreate()
3275c6c1daeSBarry Smith 
3285c6c1daeSBarry Smith     Options Database Key:
3295c6c1daeSBarry Smith .   -viewer_binary_skip_info
3305c6c1daeSBarry Smith 
3315c6c1daeSBarry Smith     Level: advanced
3325c6c1daeSBarry Smith 
3335c6c1daeSBarry Smith     Notes: This must be called after PetscViewerSetType() but before PetscViewerFileSetName(). If you use PetscViewerBinaryOpen() then
3345c6c1daeSBarry Smith     you can only skip the info file with the -viewer_binary_skip_info flag. To use the function you must open the
3355c6c1daeSBarry Smith     viewer with PetscViewerCreate(), PetscViewerSetType(), PetscViewerFileSetName().
3365c6c1daeSBarry Smith 
3375c6c1daeSBarry Smith     The .info contains meta information about the data in the binary file, for example the block size if it was
3385c6c1daeSBarry Smith     set for a vector or matrix.
3395c6c1daeSBarry Smith 
3405c6c1daeSBarry Smith    Concepts: PetscViewerBinary^accessing info file
3415c6c1daeSBarry Smith 
3425c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySetSkipOptions(),
3435c6c1daeSBarry Smith           PetscViewerBinaryGetSkipOptions()
3445c6c1daeSBarry Smith @*/
3455c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinarySkipInfo(PetscViewer viewer)
3465c6c1daeSBarry Smith {
3475c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
3485c6c1daeSBarry Smith 
3495c6c1daeSBarry Smith   PetscFunctionBegin;
3505c6c1daeSBarry Smith   vbinary->skipinfo = PETSC_TRUE;
3515c6c1daeSBarry Smith   PetscFunctionReturn(0);
3525c6c1daeSBarry Smith }
3535c6c1daeSBarry Smith 
3545c6c1daeSBarry Smith #undef __FUNCT__
3555c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinarySetSkipOptions"
3565c6c1daeSBarry Smith /*@
3575c6c1daeSBarry Smith     PetscViewerBinarySetSkipOptions - do not use the PETSc options database when loading objects
3585c6c1daeSBarry Smith 
3595c6c1daeSBarry Smith     Not Collective
3605c6c1daeSBarry Smith 
3615c6c1daeSBarry Smith     Input Parameters:
3625c6c1daeSBarry Smith +   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
3635c6c1daeSBarry Smith -   skip - PETSC_TRUE means do not use
3645c6c1daeSBarry Smith 
3655c6c1daeSBarry Smith     Options Database Key:
3665c6c1daeSBarry Smith .   -viewer_binary_skip_options
3675c6c1daeSBarry Smith 
3685c6c1daeSBarry Smith     Level: advanced
3695c6c1daeSBarry Smith 
3705c6c1daeSBarry Smith     Notes: This must be called after PetscViewerSetType()
3715c6c1daeSBarry Smith 
3725c6c1daeSBarry Smith    Concepts: PetscViewerBinary^accessing info file
3735c6c1daeSBarry Smith 
3745c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(),
3755c6c1daeSBarry Smith           PetscViewerBinaryGetSkipOptions()
3765c6c1daeSBarry Smith @*/
3775c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinarySetSkipOptions(PetscViewer viewer,PetscBool  skip)
3785c6c1daeSBarry Smith {
3795c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
3805c6c1daeSBarry Smith 
3815c6c1daeSBarry Smith   PetscFunctionBegin;
3825c6c1daeSBarry Smith   vbinary->skipoptions = skip;
3835c6c1daeSBarry Smith   PetscFunctionReturn(0);
3845c6c1daeSBarry Smith }
3855c6c1daeSBarry Smith 
3865c6c1daeSBarry Smith #undef __FUNCT__
3875c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetSkipOptions"
3885c6c1daeSBarry Smith /*@
3895c6c1daeSBarry Smith     PetscViewerBinaryGetSkipOptions - checks if viewer uses the PETSc options database when loading objects
3905c6c1daeSBarry Smith 
3915c6c1daeSBarry Smith     Not Collective
3925c6c1daeSBarry Smith 
3935c6c1daeSBarry Smith     Input Parameter:
3945c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
3955c6c1daeSBarry Smith 
3965c6c1daeSBarry Smith     Output Parameter:
3975c6c1daeSBarry Smith .   skip - PETSC_TRUE means do not use
3985c6c1daeSBarry Smith 
3995c6c1daeSBarry Smith     Level: advanced
4005c6c1daeSBarry Smith 
4015c6c1daeSBarry Smith     Notes: This must be called after PetscViewerSetType()
4025c6c1daeSBarry Smith 
4035c6c1daeSBarry Smith    Concepts: PetscViewerBinary^accessing info file
4045c6c1daeSBarry Smith 
4055c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(),
4065c6c1daeSBarry Smith           PetscViewerBinarySetSkipOptions()
4075c6c1daeSBarry Smith @*/
4085c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryGetSkipOptions(PetscViewer viewer,PetscBool  *skip)
4095c6c1daeSBarry Smith {
4105c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
4115c6c1daeSBarry Smith 
4125c6c1daeSBarry Smith   PetscFunctionBegin;
4135c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
4145c6c1daeSBarry Smith   vbinary = (PetscViewer_Binary*)viewer->data;
4155c6c1daeSBarry Smith   *skip = vbinary->skipoptions;
4165c6c1daeSBarry Smith   PetscFunctionReturn(0);
4175c6c1daeSBarry Smith }
4185c6c1daeSBarry Smith 
4195c6c1daeSBarry Smith EXTERN_C_BEGIN
4205c6c1daeSBarry Smith #undef __FUNCT__
4215c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinarySetSkipHeader_Binary"
4225c6c1daeSBarry Smith PetscErrorCode PetscViewerBinarySetSkipHeader_Binary(PetscViewer viewer,PetscBool  skip)
4235c6c1daeSBarry Smith {
4245c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
4255c6c1daeSBarry Smith 
4265c6c1daeSBarry Smith   PetscFunctionBegin;
4275c6c1daeSBarry Smith   vbinary->skipheader = skip;
4285c6c1daeSBarry Smith   PetscFunctionReturn(0);
4295c6c1daeSBarry Smith }
4305c6c1daeSBarry Smith EXTERN_C_END
4315c6c1daeSBarry Smith 
4325c6c1daeSBarry Smith #undef __FUNCT__
4335c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinarySetSkipHeader"
4345c6c1daeSBarry Smith /*@
4355c6c1daeSBarry Smith     PetscViewerBinarySetSkipHeader - do not write a header with size information on output, just raw data
4365c6c1daeSBarry Smith 
4375c6c1daeSBarry Smith     Not Collective
4385c6c1daeSBarry Smith 
4395c6c1daeSBarry Smith     Input Parameters:
4405c6c1daeSBarry Smith +   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
4415c6c1daeSBarry Smith -   skip - PETSC_TRUE means do not write header
4425c6c1daeSBarry Smith 
4435c6c1daeSBarry Smith     Options Database Key:
4445c6c1daeSBarry Smith .   -viewer_binary_skip_header
4455c6c1daeSBarry Smith 
4465c6c1daeSBarry Smith     Level: advanced
4475c6c1daeSBarry Smith 
4485c6c1daeSBarry Smith     Notes: This must be called after PetscViewerSetType()
4495c6c1daeSBarry Smith 
4505c6c1daeSBarry Smith            Can ONLY be called on a binary viewer
4515c6c1daeSBarry Smith 
4525c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(),
4535c6c1daeSBarry Smith           PetscViewerBinaryGetSkipHeader()
4545c6c1daeSBarry Smith @*/
4555c6c1daeSBarry Smith PetscErrorCode PetscViewerBinarySetSkipHeader(PetscViewer viewer,PetscBool  skip)
4565c6c1daeSBarry Smith {
4575c6c1daeSBarry Smith   PetscErrorCode ierr;
4585c6c1daeSBarry Smith 
4595c6c1daeSBarry Smith   PetscFunctionBegin;
4605c6c1daeSBarry Smith   ierr = PetscUseMethod(viewer,"PetscViewerBinarySetSkipHeader_C",(PetscViewer,PetscBool),(viewer,skip));CHKERRQ(ierr);
4615c6c1daeSBarry Smith   PetscFunctionReturn(0);
4625c6c1daeSBarry Smith }
4635c6c1daeSBarry Smith 
4645c6c1daeSBarry Smith EXTERN_C_BEGIN
4655c6c1daeSBarry Smith #undef __FUNCT__
4665c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetSkipHeader_Binary"
4675c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetSkipHeader_Binary(PetscViewer viewer,PetscBool  *skip)
4685c6c1daeSBarry Smith {
4695c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
4705c6c1daeSBarry Smith 
4715c6c1daeSBarry Smith   PetscFunctionBegin;
4725c6c1daeSBarry Smith   *skip   = vbinary->skipheader;
4735c6c1daeSBarry Smith   PetscFunctionReturn(0);
4745c6c1daeSBarry Smith }
4755c6c1daeSBarry Smith EXTERN_C_END
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 EXTERN_C_BEGIN
5105c6c1daeSBarry Smith #undef __FUNCT__
5115c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetInfoPointer_Binary"
5125c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryGetInfoPointer_Binary(PetscViewer viewer,FILE **file)
5135c6c1daeSBarry Smith {
5145c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;;
5155c6c1daeSBarry Smith 
5165c6c1daeSBarry Smith   PetscFunctionBegin;
5175c6c1daeSBarry Smith   *file = vbinary->fdes_info;
5185c6c1daeSBarry Smith   PetscFunctionReturn(0);
5195c6c1daeSBarry Smith }
5205c6c1daeSBarry Smith EXTERN_C_END
5215c6c1daeSBarry Smith 
5225c6c1daeSBarry Smith #undef __FUNCT__
5235c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetInfoPointer"
5245c6c1daeSBarry Smith /*@C
5255c6c1daeSBarry Smith     PetscViewerBinaryGetInfoPointer - Extracts the file pointer for the ASCII
5265c6c1daeSBarry Smith           info file associated with a binary file.
5275c6c1daeSBarry Smith 
5285c6c1daeSBarry Smith     Not Collective
5295c6c1daeSBarry Smith 
5305c6c1daeSBarry Smith     Input Parameter:
5315c6c1daeSBarry Smith .   viewer - PetscViewer context, obtained from PetscViewerBinaryOpen()
5325c6c1daeSBarry Smith 
5335c6c1daeSBarry Smith     Output Parameter:
5345c6c1daeSBarry Smith .   file - file pointer  Always returns PETSC_NULL if not a binary viewer
5355c6c1daeSBarry Smith 
5365c6c1daeSBarry Smith     Level: advanced
5375c6c1daeSBarry Smith 
5385c6c1daeSBarry Smith     Notes:
5395c6c1daeSBarry Smith       For writable binary PetscViewers, the descriptor will only be valid for the
5405c6c1daeSBarry Smith     first processor in the communicator that shares the PetscViewer.
5415c6c1daeSBarry Smith 
5425c6c1daeSBarry Smith     Fortran Note:
5435c6c1daeSBarry Smith     This routine is not supported in Fortran.
5445c6c1daeSBarry Smith 
5455c6c1daeSBarry Smith   Concepts: PetscViewerBinary^accessing info file
5465c6c1daeSBarry Smith 
5475c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetDescriptor()
5485c6c1daeSBarry Smith @*/
5495c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryGetInfoPointer(PetscViewer viewer,FILE **file)
5505c6c1daeSBarry Smith {
5515c6c1daeSBarry Smith   PetscErrorCode     ierr;
5525c6c1daeSBarry Smith 
5535c6c1daeSBarry Smith   PetscFunctionBegin;
5545c6c1daeSBarry Smith   *file = PETSC_NULL;
5555c6c1daeSBarry Smith   ierr = PetscTryMethod(viewer,"PetscViewerBinaryGetInfoPointer_C",(PetscViewer,FILE **),(viewer,file));CHKERRQ(ierr);
5565c6c1daeSBarry Smith   PetscFunctionReturn(0);
5575c6c1daeSBarry Smith }
5585c6c1daeSBarry Smith 
5595c6c1daeSBarry Smith #undef __FUNCT__
5605c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileClose_Binary"
5615c6c1daeSBarry Smith static PetscErrorCode PetscViewerFileClose_Binary(PetscViewer v)
5625c6c1daeSBarry Smith {
5635c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data;
5645c6c1daeSBarry Smith   PetscErrorCode     ierr;
5655c6c1daeSBarry Smith   PetscMPIInt        rank;
5665c6c1daeSBarry Smith   int                err;
5675c6c1daeSBarry Smith 
5685c6c1daeSBarry Smith   PetscFunctionBegin;
5695c6c1daeSBarry Smith   ierr = MPI_Comm_rank(((PetscObject)v)->comm,&rank);CHKERRQ(ierr);
5705c6c1daeSBarry Smith   if ((!rank || vbinary->btype == FILE_MODE_READ) && vbinary->fdes) {
5715c6c1daeSBarry Smith     close(vbinary->fdes);
5725c6c1daeSBarry Smith     if (!rank && vbinary->storecompressed) {
5735c6c1daeSBarry Smith       char par[PETSC_MAX_PATH_LEN],buf[PETSC_MAX_PATH_LEN];
5745c6c1daeSBarry Smith       FILE *fp;
5755c6c1daeSBarry Smith       /* compress the file */
5765c6c1daeSBarry Smith       ierr = PetscStrcpy(par,"gzip -f ");CHKERRQ(ierr);
5775c6c1daeSBarry Smith       ierr = PetscStrcat(par,vbinary->filename);CHKERRQ(ierr);
5785c6c1daeSBarry Smith #if defined(PETSC_HAVE_POPEN)
5795c6c1daeSBarry Smith       ierr = PetscPOpen(PETSC_COMM_SELF,PETSC_NULL,par,"r",&fp);CHKERRQ(ierr);
5805c6c1daeSBarry Smith       if (fgets(buf,1024,fp)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error from command %s\n%s",par,buf);
5815c6c1daeSBarry Smith       ierr = PetscPClose(PETSC_COMM_SELF,fp,PETSC_NULL);CHKERRQ(ierr);
5825c6c1daeSBarry Smith #else
5835c6c1daeSBarry Smith       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
5845c6c1daeSBarry Smith #endif
5855c6c1daeSBarry Smith     }
5865c6c1daeSBarry Smith   }
5875c6c1daeSBarry Smith   if (vbinary->fdes_info) {
5885c6c1daeSBarry Smith     err = fclose(vbinary->fdes_info);
5895c6c1daeSBarry Smith     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
5905c6c1daeSBarry Smith   }
5915c6c1daeSBarry Smith   ierr = PetscFree(vbinary->filename);CHKERRQ(ierr);
5925c6c1daeSBarry Smith   PetscFunctionReturn(0);
5935c6c1daeSBarry Smith }
5945c6c1daeSBarry Smith 
5955c6c1daeSBarry Smith #undef __FUNCT__
5965c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerDestroy_Binary"
5975c6c1daeSBarry Smith PetscErrorCode PetscViewerDestroy_Binary(PetscViewer v)
5985c6c1daeSBarry Smith {
5995c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data;
6005c6c1daeSBarry Smith   PetscErrorCode     ierr;
6015c6c1daeSBarry Smith 
6025c6c1daeSBarry Smith   PetscFunctionBegin;
6035c6c1daeSBarry Smith   ierr = PetscViewerFileClose_Binary(v);CHKERRQ(ierr);
6045c6c1daeSBarry Smith   ierr = PetscFree(vbinary);CHKERRQ(ierr);
6055c6c1daeSBarry Smith   PetscFunctionReturn(0);
6065c6c1daeSBarry Smith }
6075c6c1daeSBarry Smith 
6085c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
6095c6c1daeSBarry Smith #undef __FUNCT__
6105c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileClose_MPIIO"
6115c6c1daeSBarry Smith static PetscErrorCode PetscViewerFileClose_MPIIO(PetscViewer v)
6125c6c1daeSBarry Smith {
6135c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data;
6145c6c1daeSBarry Smith   int                err;
6155c6c1daeSBarry Smith   PetscErrorCode     ierr;
6165c6c1daeSBarry Smith 
6175c6c1daeSBarry Smith   PetscFunctionBegin;
6185c6c1daeSBarry Smith   if (vbinary->mfdes) {
6195c6c1daeSBarry Smith     ierr = MPI_File_close(&vbinary->mfdes);CHKERRQ(ierr);
6205c6c1daeSBarry Smith   }
6215c6c1daeSBarry Smith   if (vbinary->fdes_info) {
6225c6c1daeSBarry Smith     err = fclose(vbinary->fdes_info);
6235c6c1daeSBarry Smith     if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
6245c6c1daeSBarry Smith   }
6255c6c1daeSBarry Smith   ierr = PetscFree(vbinary->filename);CHKERRQ(ierr);
6265c6c1daeSBarry Smith   PetscFunctionReturn(0);
6275c6c1daeSBarry Smith }
6285c6c1daeSBarry Smith 
6295c6c1daeSBarry Smith 
6305c6c1daeSBarry Smith #undef __FUNCT__
6315c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerDestroy_MPIIO"
6325c6c1daeSBarry Smith PetscErrorCode PetscViewerDestroy_MPIIO(PetscViewer v)
6335c6c1daeSBarry Smith {
6345c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data;
6355c6c1daeSBarry Smith   PetscErrorCode     ierr;
6365c6c1daeSBarry Smith 
6375c6c1daeSBarry Smith   PetscFunctionBegin;
6385c6c1daeSBarry Smith   ierr = PetscViewerFileClose_MPIIO(v);CHKERRQ(ierr);
6395c6c1daeSBarry Smith   ierr = PetscFree(vbinary);CHKERRQ(ierr);
6405c6c1daeSBarry Smith   PetscFunctionReturn(0);
6415c6c1daeSBarry Smith }
6425c6c1daeSBarry Smith #endif
6435c6c1daeSBarry Smith 
6445c6c1daeSBarry Smith #undef __FUNCT__
6455c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryOpen"
6465c6c1daeSBarry Smith /*@C
6475c6c1daeSBarry Smith    PetscViewerBinaryOpen - Opens a file for binary input/output.
6485c6c1daeSBarry Smith 
6495c6c1daeSBarry Smith    Collective on MPI_Comm
6505c6c1daeSBarry Smith 
6515c6c1daeSBarry Smith    Input Parameters:
6525c6c1daeSBarry Smith +  comm - MPI communicator
6535c6c1daeSBarry Smith .  name - name of file
6545c6c1daeSBarry Smith -  type - type of file
6555c6c1daeSBarry Smith $    FILE_MODE_WRITE - create new file for binary output
6565c6c1daeSBarry Smith $    FILE_MODE_READ - open existing file for binary input
6575c6c1daeSBarry Smith $    FILE_MODE_APPEND - open existing file for binary output
6585c6c1daeSBarry Smith 
6595c6c1daeSBarry Smith    Output Parameter:
6605c6c1daeSBarry Smith .  binv - PetscViewer for binary input/output to use with the specified file
6615c6c1daeSBarry Smith 
6625c6c1daeSBarry Smith     Options Database Keys:
6635c6c1daeSBarry Smith +    -viewer_binary_skip_info
6645c6c1daeSBarry Smith .    -viewer_binary_skip_options
6655c6c1daeSBarry Smith -    -viewer_binary_skip_header
6665c6c1daeSBarry Smith 
6675c6c1daeSBarry Smith    Level: beginner
6685c6c1daeSBarry Smith 
6695c6c1daeSBarry Smith    Note:
6705c6c1daeSBarry Smith    This PetscViewer should be destroyed with PetscViewerDestroy().
6715c6c1daeSBarry Smith 
6725c6c1daeSBarry Smith     For reading files, the filename may begin with ftp:// or http:// and/or
6735c6c1daeSBarry Smith     end with .gz; in this case file is brought over and uncompressed.
6745c6c1daeSBarry Smith 
6755c6c1daeSBarry Smith     For creating files, if the file name ends with .gz it is automatically
6765c6c1daeSBarry Smith     compressed when closed.
6775c6c1daeSBarry Smith 
6785c6c1daeSBarry Smith     For writing files it only opens the file on processor 0 in the communicator.
6795c6c1daeSBarry Smith     For readable files it opens the file on all nodes that have the file. If
6805c6c1daeSBarry Smith     node 0 does not have the file it generates an error even if other nodes
6815c6c1daeSBarry Smith     do have the file.
6825c6c1daeSBarry Smith 
6835c6c1daeSBarry Smith    Concepts: binary files
6845c6c1daeSBarry Smith    Concepts: PetscViewerBinary^creating
6855c6c1daeSBarry Smith    Concepts: gzip
6865c6c1daeSBarry Smith    Concepts: accessing remote file
6875c6c1daeSBarry Smith    Concepts: remote file
6885c6c1daeSBarry Smith 
6895c6c1daeSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(),
6905c6c1daeSBarry Smith           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
6915c6c1daeSBarry Smith           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscViewerBinaryRead()
6925c6c1daeSBarry Smith @*/
6935c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryOpen(MPI_Comm comm,const char name[],PetscFileMode type,PetscViewer *binv)
6945c6c1daeSBarry Smith {
6955c6c1daeSBarry Smith   PetscErrorCode ierr;
6965c6c1daeSBarry Smith 
6975c6c1daeSBarry Smith   PetscFunctionBegin;
6985c6c1daeSBarry Smith   ierr = PetscViewerCreate(comm,binv);CHKERRQ(ierr);
6995c6c1daeSBarry Smith   ierr = PetscViewerSetType(*binv,PETSCVIEWERBINARY);CHKERRQ(ierr);
7005c6c1daeSBarry Smith   ierr = PetscViewerFileSetMode(*binv,type);CHKERRQ(ierr);
7015c6c1daeSBarry Smith   ierr = PetscViewerFileSetName(*binv,name);CHKERRQ(ierr);
7025c6c1daeSBarry Smith   PetscFunctionReturn(0);
7035c6c1daeSBarry Smith }
7045c6c1daeSBarry Smith 
7055c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
7065c6c1daeSBarry Smith #undef __FUNCT__
7075c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryMPIIO"
7085c6c1daeSBarry Smith static PetscErrorCode PetscViewerBinaryMPIIO(PetscViewer viewer,void *data,PetscInt count,PetscDataType dtype,PetscBool  write)
7095c6c1daeSBarry Smith {
7105c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
7115c6c1daeSBarry Smith   PetscErrorCode     ierr;
7125c6c1daeSBarry Smith   MPI_Datatype       mdtype;
7135c6c1daeSBarry Smith   PetscMPIInt        cnt = PetscMPIIntCast(count);
7145c6c1daeSBarry Smith   MPI_Status         status;
7155c6c1daeSBarry Smith   MPI_Aint           ul,dsize;
7165c6c1daeSBarry Smith 
7175c6c1daeSBarry Smith   PetscFunctionBegin;
7185c6c1daeSBarry Smith   ierr = PetscDataTypeToMPIDataType(dtype,&mdtype);CHKERRQ(ierr);
7195c6c1daeSBarry Smith   ierr = MPI_File_set_view(vbinary->mfdes,vbinary->moff,mdtype,mdtype,(char *)"native",MPI_INFO_NULL);CHKERRQ(ierr);
7205c6c1daeSBarry Smith   if (write) {
7215c6c1daeSBarry Smith     ierr = MPIU_File_write_all(vbinary->mfdes,data,cnt,mdtype,&status);CHKERRQ(ierr);
7225c6c1daeSBarry Smith   } else {
7235c6c1daeSBarry Smith     ierr = MPIU_File_read_all(vbinary->mfdes,data,cnt,mdtype,&status);CHKERRQ(ierr);
7245c6c1daeSBarry Smith   }
7255c6c1daeSBarry Smith   ierr = MPI_Type_get_extent(mdtype,&ul,&dsize);CHKERRQ(ierr);
7265c6c1daeSBarry Smith   vbinary->moff += dsize*cnt;
7275c6c1daeSBarry Smith   PetscFunctionReturn(0);
7285c6c1daeSBarry Smith }
7295c6c1daeSBarry Smith #endif
7305c6c1daeSBarry Smith 
7315c6c1daeSBarry Smith #undef __FUNCT__
7325c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryRead"
7335c6c1daeSBarry Smith /*@C
7345c6c1daeSBarry Smith    PetscViewerBinaryRead - Reads from a binary file, all processors get the same result
7355c6c1daeSBarry Smith 
7365c6c1daeSBarry Smith    Collective on MPI_Comm
7375c6c1daeSBarry Smith 
7385c6c1daeSBarry Smith    Input Parameters:
7395c6c1daeSBarry Smith +  viewer - the binary viewer
7405c6c1daeSBarry Smith .  data - location to write the data
7415c6c1daeSBarry Smith .  count - number of items of data to read
7425c6c1daeSBarry Smith -  datatype - type of data to read
7435c6c1daeSBarry Smith 
7445c6c1daeSBarry Smith    Level: beginner
7455c6c1daeSBarry Smith 
7465c6c1daeSBarry Smith    Concepts: binary files
7475c6c1daeSBarry Smith 
7485c6c1daeSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(),
7495c6c1daeSBarry Smith           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
7505c6c1daeSBarry Smith           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead()
7515c6c1daeSBarry Smith @*/
7525c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryRead(PetscViewer viewer,void *data,PetscInt count,PetscDataType dtype)
7535c6c1daeSBarry Smith {
7545c6c1daeSBarry Smith   PetscErrorCode     ierr;
7555c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
7565c6c1daeSBarry Smith 
7575c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
7585c6c1daeSBarry Smith   if (vbinary->MPIIO) {
7595c6c1daeSBarry Smith     ierr = PetscViewerBinaryMPIIO(viewer,data,count,dtype,PETSC_FALSE);CHKERRQ(ierr);
7605c6c1daeSBarry Smith   } else {
7615c6c1daeSBarry Smith #endif
7625c6c1daeSBarry Smith     ierr = PetscBinarySynchronizedRead(((PetscObject)viewer)->comm,vbinary->fdes,data,count,dtype);CHKERRQ(ierr);
7635c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
7645c6c1daeSBarry Smith   }
7655c6c1daeSBarry Smith #endif
7665c6c1daeSBarry Smith   PetscFunctionReturn(0);
7675c6c1daeSBarry Smith }
7685c6c1daeSBarry Smith 
7695c6c1daeSBarry Smith 
7705c6c1daeSBarry Smith #undef __FUNCT__
7715c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryWrite"
7725c6c1daeSBarry Smith /*@C
7735c6c1daeSBarry Smith    PetscViewerBinaryWrite - writes to a binary file, only from the first process
7745c6c1daeSBarry Smith 
7755c6c1daeSBarry Smith    Collective on MPI_Comm
7765c6c1daeSBarry Smith 
7775c6c1daeSBarry Smith    Input Parameters:
7785c6c1daeSBarry Smith +  viewer - the binary viewer
7795c6c1daeSBarry Smith .  data - location of data
7805c6c1daeSBarry Smith .  count - number of items of data to read
7815c6c1daeSBarry Smith .  dtype - type of data to read
7825c6c1daeSBarry Smith -  istemp - data may be overwritten
7835c6c1daeSBarry Smith 
7845c6c1daeSBarry Smith    Level: beginner
7855c6c1daeSBarry Smith 
7865c6c1daeSBarry Smith    Notes: because byte-swapping may be done on the values in data it cannot be declared const
7875c6c1daeSBarry Smith 
7885c6c1daeSBarry Smith    Concepts: binary files
7895c6c1daeSBarry Smith 
7905c6c1daeSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(),
7915c6c1daeSBarry Smith           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(), PetscDataType
7925c6c1daeSBarry Smith           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead()
7935c6c1daeSBarry Smith @*/
7945c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryWrite(PetscViewer viewer,void *data,PetscInt count,PetscDataType dtype,PetscBool  istemp)
7955c6c1daeSBarry Smith {
7965c6c1daeSBarry Smith   PetscErrorCode     ierr;
7975c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
7985c6c1daeSBarry Smith 
7995c6c1daeSBarry Smith   PetscFunctionBegin;
8005c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
8015c6c1daeSBarry Smith   if (vbinary->MPIIO) {
8025c6c1daeSBarry Smith     ierr = PetscViewerBinaryMPIIO(viewer,data,count,dtype,PETSC_TRUE);CHKERRQ(ierr);
8035c6c1daeSBarry Smith   } else {
8045c6c1daeSBarry Smith #endif
8055c6c1daeSBarry Smith     ierr = PetscBinarySynchronizedWrite(((PetscObject)viewer)->comm,vbinary->fdes,data,count,dtype,istemp);CHKERRQ(ierr);
8065c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
8075c6c1daeSBarry Smith   }
8085c6c1daeSBarry Smith #endif
8095c6c1daeSBarry Smith   PetscFunctionReturn(0);
8105c6c1daeSBarry Smith }
8115c6c1daeSBarry Smith 
8125c6c1daeSBarry Smith #undef __FUNCT__
8135c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryWriteStringArray"
8145c6c1daeSBarry Smith /*@C
8155c6c1daeSBarry Smith    PetscViewerBinaryWriteStringArray - writes to a binary file, only from the first process an array of strings
8165c6c1daeSBarry Smith 
8175c6c1daeSBarry Smith    Collective on MPI_Comm
8185c6c1daeSBarry Smith 
8195c6c1daeSBarry Smith    Input Parameters:
8205c6c1daeSBarry Smith +  viewer - the binary viewer
8215c6c1daeSBarry Smith -  data - location of the array of strings
8225c6c1daeSBarry Smith 
8235c6c1daeSBarry Smith 
8245c6c1daeSBarry Smith    Level: intermediate
8255c6c1daeSBarry Smith 
8265c6c1daeSBarry Smith    Concepts: binary files
8275c6c1daeSBarry Smith 
8285c6c1daeSBarry Smith     Notes: array of strings is null terminated
8295c6c1daeSBarry Smith 
8305c6c1daeSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(),
8315c6c1daeSBarry Smith           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
8325c6c1daeSBarry Smith           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead()
8335c6c1daeSBarry Smith @*/
8345c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryWriteStringArray(PetscViewer viewer,char **data)
8355c6c1daeSBarry Smith {
8365c6c1daeSBarry Smith   PetscErrorCode     ierr;
8375c6c1daeSBarry Smith   PetscInt           i,n = 0,*sizes;
8385c6c1daeSBarry Smith 
8395c6c1daeSBarry Smith   /* count number of strings */
8405c6c1daeSBarry Smith   while (data[n++]);
8415c6c1daeSBarry Smith   n--;
8425c6c1daeSBarry Smith   ierr = PetscMalloc((n+1)*sizeof(PetscInt),&sizes);CHKERRQ(ierr);
8435c6c1daeSBarry Smith   sizes[0] = n;
8445c6c1daeSBarry Smith   for (i=0; i<n; i++) {
8455c6c1daeSBarry Smith     size_t tmp;
8465c6c1daeSBarry Smith     ierr       = PetscStrlen(data[i],&tmp);CHKERRQ(ierr);
8475c6c1daeSBarry Smith     sizes[i+1] = tmp + 1;   /* size includes space for the null terminator */
8485c6c1daeSBarry Smith   }
8495c6c1daeSBarry Smith   ierr = PetscViewerBinaryWrite(viewer,sizes,n+1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
8505c6c1daeSBarry Smith   for (i=0; i<n; i++) {
8515c6c1daeSBarry Smith     ierr = PetscViewerBinaryWrite(viewer,data[i],sizes[i+1],PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
8525c6c1daeSBarry Smith   }
8535c6c1daeSBarry Smith   ierr = PetscFree(sizes);CHKERRQ(ierr);
8545c6c1daeSBarry Smith   PetscFunctionReturn(0);
8555c6c1daeSBarry Smith }
8565c6c1daeSBarry Smith 
8575c6c1daeSBarry Smith /*@C
8585c6c1daeSBarry Smith    PetscViewerBinaryReadStringArray - reads a binary file an array of strings
8595c6c1daeSBarry Smith 
8605c6c1daeSBarry Smith    Collective on MPI_Comm
8615c6c1daeSBarry Smith 
8625c6c1daeSBarry Smith    Input Parameter:
8635c6c1daeSBarry Smith .  viewer - the binary viewer
8645c6c1daeSBarry Smith 
8655c6c1daeSBarry Smith    Output Parameter:
8665c6c1daeSBarry Smith .  data - location of the array of strings
8675c6c1daeSBarry Smith 
8685c6c1daeSBarry Smith    Level: intermediate
8695c6c1daeSBarry Smith 
8705c6c1daeSBarry Smith    Concepts: binary files
8715c6c1daeSBarry Smith 
8725c6c1daeSBarry Smith     Notes: array of strings is null terminated
8735c6c1daeSBarry Smith 
8745c6c1daeSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(),
8755c6c1daeSBarry Smith           VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(),
8765c6c1daeSBarry Smith           PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead()
8775c6c1daeSBarry Smith @*/
8785c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinaryReadStringArray(PetscViewer viewer,char ***data)
8795c6c1daeSBarry Smith {
8805c6c1daeSBarry Smith   PetscErrorCode     ierr;
8815c6c1daeSBarry Smith   PetscInt           i,n,*sizes,N = 0;
8825c6c1daeSBarry Smith 
8835c6c1daeSBarry Smith   /* count number of strings */
8845c6c1daeSBarry Smith   ierr = PetscViewerBinaryRead(viewer,&n,1,PETSC_INT);CHKERRQ(ierr);
8855c6c1daeSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&sizes);CHKERRQ(ierr);
8865c6c1daeSBarry Smith   ierr = PetscViewerBinaryRead(viewer,sizes,n,PETSC_INT);CHKERRQ(ierr);
8875c6c1daeSBarry Smith   for (i=0; i<n; i++) {
8885c6c1daeSBarry Smith     N += sizes[i];
8895c6c1daeSBarry Smith   }
8905c6c1daeSBarry Smith   ierr = PetscMalloc((n+1)*sizeof(char*) + N*sizeof(char),data);CHKERRQ(ierr);
8915c6c1daeSBarry Smith   (*data)[0] = (char*)((*data) + n + 1);
8925c6c1daeSBarry Smith   for (i=1; i<n; i++) {
8935c6c1daeSBarry Smith     (*data)[i] = (*data)[i-1] + sizes[i-1];
8945c6c1daeSBarry Smith   }
8955c6c1daeSBarry Smith   ierr = PetscViewerBinaryRead(viewer,(*data)[0],N,PETSC_CHAR);CHKERRQ(ierr);
8965c6c1daeSBarry Smith   (*data)[n] = 0;
8975c6c1daeSBarry Smith   ierr = PetscFree(sizes);CHKERRQ(ierr);
8985c6c1daeSBarry Smith   PetscFunctionReturn(0);
8995c6c1daeSBarry Smith }
9005c6c1daeSBarry Smith 
9015c6c1daeSBarry Smith EXTERN_C_BEGIN
9025c6c1daeSBarry Smith #undef __FUNCT__
9035c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileGetName_Binary"
9045c6c1daeSBarry Smith PetscErrorCode PetscViewerFileGetName_Binary(PetscViewer viewer,const char **name)
9055c6c1daeSBarry Smith {
9065c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
9075c6c1daeSBarry Smith 
9085c6c1daeSBarry Smith   PetscFunctionBegin;
9095c6c1daeSBarry Smith   *name = vbinary->filename;
9105c6c1daeSBarry Smith   PetscFunctionReturn(0);
9115c6c1daeSBarry Smith }
9125c6c1daeSBarry Smith EXTERN_C_END
9135c6c1daeSBarry Smith 
9145c6c1daeSBarry Smith #undef __FUNCT__
9155c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileGetMode"
9165c6c1daeSBarry Smith /*@C
9175c6c1daeSBarry Smith      PetscViewerFileGetMode - Gets the type of file to be open
9185c6c1daeSBarry Smith 
9195c6c1daeSBarry Smith     Not Collective
9205c6c1daeSBarry Smith 
9215c6c1daeSBarry Smith   Input Parameter:
9225c6c1daeSBarry Smith .  viewer - the PetscViewer; must be a binary, MATLAB, hdf, or netcdf PetscViewer
9235c6c1daeSBarry Smith 
9245c6c1daeSBarry Smith   Output Parameter:
9255c6c1daeSBarry Smith .  type - type of file
9265c6c1daeSBarry Smith $    FILE_MODE_WRITE - create new file for binary output
9275c6c1daeSBarry Smith $    FILE_MODE_READ - open existing file for binary input
9285c6c1daeSBarry Smith $    FILE_MODE_APPEND - open existing file for binary output
9295c6c1daeSBarry Smith 
9305c6c1daeSBarry Smith   Level: advanced
9315c6c1daeSBarry Smith 
9325c6c1daeSBarry Smith .seealso: PetscViewerFileSetMode(), PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinaryOpen()
9335c6c1daeSBarry Smith 
9345c6c1daeSBarry Smith @*/
9355c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileGetMode(PetscViewer viewer,PetscFileMode *type)
9365c6c1daeSBarry Smith {
9375c6c1daeSBarry Smith   PetscErrorCode ierr;
9385c6c1daeSBarry Smith 
9395c6c1daeSBarry Smith   PetscFunctionBegin;
9405c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
9415c6c1daeSBarry Smith   PetscValidPointer(type,2);
9425c6c1daeSBarry Smith   ierr = PetscUseMethod(viewer,"PetscViewerFileGetMode_C",(PetscViewer,PetscFileMode*),(viewer,type));CHKERRQ(ierr);
9435c6c1daeSBarry Smith   PetscFunctionReturn(0);
9445c6c1daeSBarry Smith }
9455c6c1daeSBarry Smith 
9465c6c1daeSBarry Smith #undef __FUNCT__
9475c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinarySetMPIIO"
9485c6c1daeSBarry Smith /*@
9495c6c1daeSBarry Smith      PetscViewerBinarySetMPIIO - Sets a binary viewer to use MPI IO for reading/writing. Must be called
9505c6c1daeSBarry Smith         before PetscViewerFileSetName()
9515c6c1daeSBarry Smith 
9525c6c1daeSBarry Smith     Logically Collective on PetscViewer
9535c6c1daeSBarry Smith 
9545c6c1daeSBarry Smith   Input Parameters:
9555c6c1daeSBarry Smith .  viewer - the PetscViewer; must be a binary
9565c6c1daeSBarry Smith 
9575c6c1daeSBarry Smith     Options Database:
9585c6c1daeSBarry Smith     -viewer_binary_mpiio : Flag for using MPI-IO
9595c6c1daeSBarry Smith 
9605c6c1daeSBarry Smith    Notes: turns off the default usage of the .info file since that is not scalable
9615c6c1daeSBarry Smith 
9625c6c1daeSBarry Smith   Level: advanced
9635c6c1daeSBarry Smith 
9645c6c1daeSBarry Smith .seealso: PetscViewerFileSetMode(), PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinaryOpen()
9655c6c1daeSBarry Smith 
9665c6c1daeSBarry Smith @*/
9675c6c1daeSBarry Smith PetscErrorCode  PetscViewerBinarySetMPIIO(PetscViewer viewer)
9685c6c1daeSBarry Smith {
9695c6c1daeSBarry Smith   PetscErrorCode ierr;
9705c6c1daeSBarry Smith 
9715c6c1daeSBarry Smith   PetscFunctionBegin;
9725c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
9735c6c1daeSBarry Smith   ierr = PetscTryMethod(viewer,"PetscViewerBinarySetMPIIO_C",(PetscViewer),(viewer));CHKERRQ(ierr);
9745c6c1daeSBarry Smith   PetscFunctionReturn(0);
9755c6c1daeSBarry Smith }
9765c6c1daeSBarry Smith 
9775c6c1daeSBarry Smith 
9785c6c1daeSBarry Smith #undef __FUNCT__
9795c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileSetMode"
9805c6c1daeSBarry Smith /*@C
9815c6c1daeSBarry Smith      PetscViewerFileSetMode - Sets the type of file to be open
9825c6c1daeSBarry Smith 
9835c6c1daeSBarry Smith     Logically Collective on PetscViewer
9845c6c1daeSBarry Smith 
9855c6c1daeSBarry Smith   Input Parameters:
9865c6c1daeSBarry Smith +  viewer - the PetscViewer; must be a binary, Matlab, hdf, or netcdf PetscViewer
9875c6c1daeSBarry Smith -  type - type of file
9885c6c1daeSBarry Smith $    FILE_MODE_WRITE - create new file for binary output
9895c6c1daeSBarry Smith $    FILE_MODE_READ - open existing file for binary input
9905c6c1daeSBarry Smith $    FILE_MODE_APPEND - open existing file for binary output
9915c6c1daeSBarry Smith 
9925c6c1daeSBarry Smith   Level: advanced
9935c6c1daeSBarry Smith 
9945c6c1daeSBarry Smith .seealso: PetscViewerFileSetMode(), PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinaryOpen()
9955c6c1daeSBarry Smith 
9965c6c1daeSBarry Smith @*/
9975c6c1daeSBarry Smith PetscErrorCode  PetscViewerFileSetMode(PetscViewer viewer,PetscFileMode type)
9985c6c1daeSBarry Smith {
9995c6c1daeSBarry Smith   PetscErrorCode ierr;
10005c6c1daeSBarry Smith 
10015c6c1daeSBarry Smith   PetscFunctionBegin;
10025c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1);
10035c6c1daeSBarry Smith   PetscValidLogicalCollectiveEnum(viewer,type,2);
10045c6c1daeSBarry Smith   ierr = PetscTryMethod(viewer,"PetscViewerFileSetMode_C",(PetscViewer,PetscFileMode),(viewer,type));CHKERRQ(ierr);
10055c6c1daeSBarry Smith   PetscFunctionReturn(0);
10065c6c1daeSBarry Smith }
10075c6c1daeSBarry Smith 
10085c6c1daeSBarry Smith EXTERN_C_BEGIN
10095c6c1daeSBarry Smith #undef __FUNCT__
10105c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileGetMode_Binary"
10115c6c1daeSBarry Smith PetscErrorCode PetscViewerFileGetMode_Binary(PetscViewer viewer,PetscFileMode *type)
10125c6c1daeSBarry Smith {
10135c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
10145c6c1daeSBarry Smith 
10155c6c1daeSBarry Smith   PetscFunctionBegin;
10165c6c1daeSBarry Smith   *type = vbinary->btype;
10175c6c1daeSBarry Smith   PetscFunctionReturn(0);
10185c6c1daeSBarry Smith }
10195c6c1daeSBarry Smith EXTERN_C_END
10205c6c1daeSBarry Smith 
10215c6c1daeSBarry Smith EXTERN_C_BEGIN
10225c6c1daeSBarry Smith #undef __FUNCT__
10235c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileSetMode_Binary"
10245c6c1daeSBarry Smith PetscErrorCode PetscViewerFileSetMode_Binary(PetscViewer viewer,PetscFileMode type)
10255c6c1daeSBarry Smith {
10265c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
10275c6c1daeSBarry Smith 
10285c6c1daeSBarry Smith   PetscFunctionBegin;
10295c6c1daeSBarry Smith   vbinary->btype = type;
10305c6c1daeSBarry Smith   PetscFunctionReturn(0);
10315c6c1daeSBarry Smith }
10325c6c1daeSBarry Smith EXTERN_C_END
10335c6c1daeSBarry Smith 
10345c6c1daeSBarry Smith /*
10355c6c1daeSBarry Smith         Actually opens the file
10365c6c1daeSBarry Smith */
10375c6c1daeSBarry Smith EXTERN_C_BEGIN
10385c6c1daeSBarry Smith #undef __FUNCT__
10395c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileSetName_Binary"
10405c6c1daeSBarry Smith PetscErrorCode PetscViewerFileSetName_Binary(PetscViewer viewer,const char name[])
10415c6c1daeSBarry Smith {
10425c6c1daeSBarry Smith   PetscMPIInt         rank;
10435c6c1daeSBarry Smith   PetscErrorCode      ierr;
10445c6c1daeSBarry Smith   size_t              len;
10455c6c1daeSBarry Smith   PetscViewer_Binary  *vbinary = (PetscViewer_Binary*)viewer->data;
10465c6c1daeSBarry Smith   const char          *fname;
10475c6c1daeSBarry Smith   char                bname[PETSC_MAX_PATH_LEN],*gz;
10485c6c1daeSBarry Smith   PetscBool           found;
10495c6c1daeSBarry Smith   PetscFileMode       type = vbinary->btype;
10505c6c1daeSBarry Smith 
10515c6c1daeSBarry Smith   PetscFunctionBegin;
10525c6c1daeSBarry Smith   if (type == (PetscFileMode) -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetMode() before PetscViewerFileSetName()");
10535c6c1daeSBarry Smith   ierr = PetscViewerFileClose_Binary(viewer);CHKERRQ(ierr);
10545c6c1daeSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)viewer)->prefix,"-viewer_binary_skip_info",&vbinary->skipinfo,PETSC_NULL);CHKERRQ(ierr);
10555c6c1daeSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)viewer)->prefix,"-viewer_binary_skip_options",&vbinary->skipoptions,PETSC_NULL);CHKERRQ(ierr);
10565c6c1daeSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)viewer)->prefix,"-viewer_binary_skip_header",&vbinary->skipheader,PETSC_NULL);CHKERRQ(ierr);
10575c6c1daeSBarry Smith 
10585c6c1daeSBarry Smith   ierr = MPI_Comm_rank(((PetscObject)viewer)->comm,&rank);CHKERRQ(ierr);
10595c6c1daeSBarry Smith 
10605c6c1daeSBarry Smith   /* copy name so we can edit it */
10615c6c1daeSBarry Smith   ierr = PetscStrallocpy(name,&vbinary->filename);CHKERRQ(ierr);
10625c6c1daeSBarry Smith 
10635c6c1daeSBarry Smith   /* if ends in .gz strip that off and note user wants file compressed */
10645c6c1daeSBarry Smith   vbinary->storecompressed = PETSC_FALSE;
10655c6c1daeSBarry Smith   if (!rank && type == FILE_MODE_WRITE) {
10665c6c1daeSBarry Smith     /* remove .gz if it ends library name */
10675c6c1daeSBarry Smith     ierr = PetscStrstr(vbinary->filename,".gz",&gz);CHKERRQ(ierr);
10685c6c1daeSBarry Smith     if (gz) {
10695c6c1daeSBarry Smith       ierr = PetscStrlen(gz,&len);CHKERRQ(ierr);
10705c6c1daeSBarry Smith       if (len == 3) {
10715c6c1daeSBarry Smith         *gz = 0;
10725c6c1daeSBarry Smith         vbinary->storecompressed = PETSC_TRUE;
10735c6c1daeSBarry Smith       }
10745c6c1daeSBarry Smith     }
10755c6c1daeSBarry Smith   }
10765c6c1daeSBarry Smith 
10775c6c1daeSBarry Smith   /* only first processor opens file if writeable */
10785c6c1daeSBarry Smith   if (!rank || type == FILE_MODE_READ) {
10795c6c1daeSBarry Smith 
10805c6c1daeSBarry Smith     if (type == FILE_MODE_READ){
10815c6c1daeSBarry Smith       /* possibly get the file from remote site or compressed file */
10825c6c1daeSBarry Smith       ierr  = PetscFileRetrieve(((PetscObject)viewer)->comm,vbinary->filename,bname,PETSC_MAX_PATH_LEN,&found);CHKERRQ(ierr);
10835c6c1daeSBarry Smith       fname = bname;
10845c6c1daeSBarry Smith       if (!rank && !found) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot locate file: %s on node zero",vbinary->filename);
10855c6c1daeSBarry Smith       else if (!found) {
10865c6c1daeSBarry Smith         ierr = PetscInfo(viewer,"Nonzero processor did not locate readonly file\n");CHKERRQ(ierr);
10875c6c1daeSBarry Smith         fname = 0;
10885c6c1daeSBarry Smith       }
10895c6c1daeSBarry Smith     } else {
10905c6c1daeSBarry Smith       fname = vbinary->filename;
10915c6c1daeSBarry Smith     }
10925c6c1daeSBarry Smith 
10935c6c1daeSBarry Smith #if defined(PETSC_HAVE_O_BINARY)
10945c6c1daeSBarry Smith     if (type == FILE_MODE_WRITE) {
10955c6c1daeSBarry 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);
10965c6c1daeSBarry Smith     } else if (type == FILE_MODE_READ && fname) {
10975c6c1daeSBarry 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);
10985c6c1daeSBarry Smith     } else if (type == FILE_MODE_APPEND) {
10995c6c1daeSBarry 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);
11005c6c1daeSBarry Smith     } else if (fname) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown file type");
11015c6c1daeSBarry Smith #else
11025c6c1daeSBarry Smith     if (type == FILE_MODE_WRITE) {
11035c6c1daeSBarry Smith       if ((vbinary->fdes = creat(fname,0666)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot create file %s for writing",fname);
11045c6c1daeSBarry Smith     } else if (type == FILE_MODE_READ && fname) {
11055c6c1daeSBarry 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);
11065c6c1daeSBarry Smith     } else if (type == FILE_MODE_APPEND) {
11075c6c1daeSBarry 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);
11085c6c1daeSBarry Smith     } else if (fname) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown file type");
11095c6c1daeSBarry Smith #endif
11105c6c1daeSBarry Smith   } else vbinary->fdes = -1;
11115c6c1daeSBarry Smith   viewer->format = PETSC_VIEWER_NOFORMAT;
11125c6c1daeSBarry Smith 
11135c6c1daeSBarry Smith   /*
11145c6c1daeSBarry Smith       try to open info file: all processors open this file if read only
11155c6c1daeSBarry Smith   */
11165c6c1daeSBarry Smith   if (!vbinary->skipinfo && (!rank || type == FILE_MODE_READ)) {
11175c6c1daeSBarry Smith     char infoname[PETSC_MAX_PATH_LEN],iname[PETSC_MAX_PATH_LEN];
11185c6c1daeSBarry Smith 
11195c6c1daeSBarry Smith     ierr = PetscStrcpy(infoname,name);CHKERRQ(ierr);
11205c6c1daeSBarry Smith     /* remove .gz if it ends library name */
11215c6c1daeSBarry Smith     ierr = PetscStrstr(infoname,".gz",&gz);CHKERRQ(ierr);
11225c6c1daeSBarry Smith     if (gz) {
11235c6c1daeSBarry Smith       ierr = PetscStrlen(gz,&len);CHKERRQ(ierr);
11245c6c1daeSBarry Smith       if (len == 3) {
11255c6c1daeSBarry Smith         *gz = 0;
11265c6c1daeSBarry Smith       }
11275c6c1daeSBarry Smith     }
11285c6c1daeSBarry Smith 
11295c6c1daeSBarry Smith     ierr = PetscStrcat(infoname,".info");CHKERRQ(ierr);
11305c6c1daeSBarry Smith     ierr = PetscFixFilename(infoname,iname);CHKERRQ(ierr);
11315c6c1daeSBarry Smith     if (type == FILE_MODE_READ) {
11325c6c1daeSBarry Smith       ierr = PetscFileRetrieve(((PetscObject)viewer)->comm,iname,infoname,PETSC_MAX_PATH_LEN,&found);CHKERRQ(ierr);
11335c6c1daeSBarry Smith       ierr = PetscOptionsInsertFile(((PetscObject)viewer)->comm,infoname,PETSC_FALSE);CHKERRQ(ierr);
11345c6c1daeSBarry Smith     } else {
11355c6c1daeSBarry Smith       vbinary->fdes_info = fopen(infoname,"w");
11365c6c1daeSBarry Smith       if (!vbinary->fdes_info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open .info file %s for writing",infoname);
11375c6c1daeSBarry Smith     }
11385c6c1daeSBarry Smith   }
11395c6c1daeSBarry Smith 
11405c6c1daeSBarry Smith #if defined(PETSC_USE_LOG)
11415c6c1daeSBarry Smith   PetscLogObjectState((PetscObject)viewer,"File: %s",name);
11425c6c1daeSBarry Smith #endif
11435c6c1daeSBarry Smith   PetscFunctionReturn(0);
11445c6c1daeSBarry Smith }
11455c6c1daeSBarry Smith EXTERN_C_END
11465c6c1daeSBarry Smith 
11475c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
11485c6c1daeSBarry Smith EXTERN_C_BEGIN
11495c6c1daeSBarry Smith #undef __FUNCT__
11505c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileSetName_MPIIO"
11515c6c1daeSBarry Smith PetscErrorCode PetscViewerFileSetName_MPIIO(PetscViewer viewer,const char name[])
11525c6c1daeSBarry Smith {
11535c6c1daeSBarry Smith   PetscMPIInt         rank;
11545c6c1daeSBarry Smith   PetscErrorCode      ierr;
11555c6c1daeSBarry Smith   size_t              len;
11565c6c1daeSBarry Smith   PetscViewer_Binary  *vbinary = (PetscViewer_Binary*)viewer->data;
11575c6c1daeSBarry Smith   char                *gz;
11585c6c1daeSBarry Smith   PetscBool           found;
11595c6c1daeSBarry Smith   PetscFileMode       type = vbinary->btype;
11605c6c1daeSBarry Smith 
11615c6c1daeSBarry Smith   PetscFunctionBegin;
11625c6c1daeSBarry Smith   if (type == (PetscFileMode) -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetMode() before PetscViewerFileSetName()");
11635c6c1daeSBarry Smith   ierr = PetscViewerFileClose_MPIIO(viewer);CHKERRQ(ierr);
11645c6c1daeSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)viewer)->prefix,"-viewer_binary_skip_info",&vbinary->skipinfo,PETSC_NULL);CHKERRQ(ierr);
11655c6c1daeSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject)viewer)->prefix,"-viewer_binary_skip_options",&vbinary->skipoptions,PETSC_NULL);CHKERRQ(ierr);
11665c6c1daeSBarry Smith 
11675c6c1daeSBarry Smith   ierr = MPI_Comm_rank(((PetscObject)viewer)->comm,&rank);CHKERRQ(ierr);
11685c6c1daeSBarry Smith   ierr = PetscStrallocpy(name,&vbinary->filename);CHKERRQ(ierr);
11695c6c1daeSBarry Smith   vbinary->storecompressed = PETSC_FALSE;
11705c6c1daeSBarry Smith 
11715c6c1daeSBarry Smith 
11725c6c1daeSBarry Smith   /* only first processor opens file if writeable */
11735c6c1daeSBarry Smith   if (type == FILE_MODE_READ) {
11745c6c1daeSBarry Smith     MPI_File_open(((PetscObject)viewer)->comm,vbinary->filename,MPI_MODE_RDONLY,MPI_INFO_NULL,&vbinary->mfdes);CHKERRQ(ierr);
11755c6c1daeSBarry Smith   } else if (type == FILE_MODE_WRITE) {
11765c6c1daeSBarry Smith     MPI_File_open(((PetscObject)viewer)->comm,vbinary->filename,MPI_MODE_WRONLY | MPI_MODE_CREATE,MPI_INFO_NULL,&vbinary->mfdes);CHKERRQ(ierr);
11775c6c1daeSBarry Smith   }
11785c6c1daeSBarry Smith   viewer->format = PETSC_VIEWER_NOFORMAT;
11795c6c1daeSBarry Smith 
11805c6c1daeSBarry Smith   /*
11815c6c1daeSBarry Smith       try to open info file: all processors open this file if read only
11825c6c1daeSBarry Smith 
11835c6c1daeSBarry Smith       Below is identical code to the code for Binary above, should be put in seperate routine
11845c6c1daeSBarry Smith   */
11855c6c1daeSBarry Smith   if (!vbinary->skipinfo && (!rank || type == FILE_MODE_READ)) {
11865c6c1daeSBarry Smith     char infoname[PETSC_MAX_PATH_LEN],iname[PETSC_MAX_PATH_LEN];
11875c6c1daeSBarry Smith 
11885c6c1daeSBarry Smith     ierr = PetscStrcpy(infoname,name);CHKERRQ(ierr);
11895c6c1daeSBarry Smith     /* remove .gz if it ends library name */
11905c6c1daeSBarry Smith     ierr = PetscStrstr(infoname,".gz",&gz);CHKERRQ(ierr);
11915c6c1daeSBarry Smith     if (gz) {
11925c6c1daeSBarry Smith       ierr = PetscStrlen(gz,&len);CHKERRQ(ierr);
11935c6c1daeSBarry Smith       if (len == 3) {
11945c6c1daeSBarry Smith         *gz = 0;
11955c6c1daeSBarry Smith       }
11965c6c1daeSBarry Smith     }
11975c6c1daeSBarry Smith 
11985c6c1daeSBarry Smith     ierr = PetscStrcat(infoname,".info");CHKERRQ(ierr);
11995c6c1daeSBarry Smith     ierr = PetscFixFilename(infoname,iname);CHKERRQ(ierr);
12005c6c1daeSBarry Smith     if (type == FILE_MODE_READ) {
12015c6c1daeSBarry Smith       ierr = PetscFileRetrieve(((PetscObject)viewer)->comm,iname,infoname,PETSC_MAX_PATH_LEN,&found);CHKERRQ(ierr);
12025c6c1daeSBarry Smith       ierr = PetscOptionsInsertFile(((PetscObject)viewer)->comm,infoname,PETSC_FALSE);CHKERRQ(ierr);
12035c6c1daeSBarry Smith     } else {
12045c6c1daeSBarry Smith       vbinary->fdes_info = fopen(infoname,"w");
12055c6c1daeSBarry Smith       if (!vbinary->fdes_info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open .info file %s for writing",infoname);
12065c6c1daeSBarry Smith     }
12075c6c1daeSBarry Smith   }
12085c6c1daeSBarry Smith 
12095c6c1daeSBarry Smith #if defined(PETSC_USE_LOG)
12105c6c1daeSBarry Smith   PetscLogObjectState((PetscObject)viewer,"File: %s",name);
12115c6c1daeSBarry Smith #endif
12125c6c1daeSBarry Smith   PetscFunctionReturn(0);
12135c6c1daeSBarry Smith }
12145c6c1daeSBarry Smith EXTERN_C_END
12155c6c1daeSBarry Smith 
12165c6c1daeSBarry Smith EXTERN_C_BEGIN
12175c6c1daeSBarry Smith #undef __FUNCT__
12185c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinarySetMPIIO_Binary"
12195c6c1daeSBarry Smith PetscErrorCode PetscViewerBinarySetMPIIO_Binary(PetscViewer viewer)
12205c6c1daeSBarry Smith {
12215c6c1daeSBarry Smith   PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;
12225c6c1daeSBarry Smith   PetscErrorCode     ierr;
12235c6c1daeSBarry Smith 
12245c6c1daeSBarry Smith   PetscFunctionBegin;
12255c6c1daeSBarry Smith   if (vbinary->filename) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call before calling PetscViewerFileSetName()");
12265c6c1daeSBarry Smith   viewer->ops->destroy = PetscViewerDestroy_MPIIO;
12275c6c1daeSBarry Smith   vbinary->MPIIO       = PETSC_TRUE;
12285c6c1daeSBarry Smith   /*  vbinary->skipinfo    = PETSC_TRUE; */
12295c6c1daeSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)viewer,"PetscViewerFileSetName_C","PetscViewerFileSetName_MPIIO",PetscViewerFileSetName_MPIIO);CHKERRQ(ierr);
12305c6c1daeSBarry Smith   PetscFunctionReturn(0);
12315c6c1daeSBarry Smith }
12325c6c1daeSBarry Smith EXTERN_C_END
12335c6c1daeSBarry Smith #endif
12345c6c1daeSBarry Smith 
1235*2bf49c77SBarry Smith #undef __FUNCT__
1236*2bf49c77SBarry Smith #define __FUNCT__ "PetscViewerView_Binary"
1237*2bf49c77SBarry Smith PetscErrorCode  PetscViewerView_Binary(PetscViewer v,PetscViewer viewer)
1238*2bf49c77SBarry Smith {
1239*2bf49c77SBarry Smith   PetscErrorCode     ierr;
1240*2bf49c77SBarry Smith   PetscViewer_Binary *binary  = (PetscViewer_Binary *)v->data;
1241*2bf49c77SBarry Smith 
1242*2bf49c77SBarry Smith   PetscFunctionBegin;
1243*2bf49c77SBarry Smith   if (binary->filename) {
1244*2bf49c77SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"Filename: %s\n",binary->filename);CHKERRQ(ierr);
1245*2bf49c77SBarry Smith   }
1246*2bf49c77SBarry Smith   PetscFunctionReturn(0);
1247*2bf49c77SBarry Smith }
1248*2bf49c77SBarry Smith 
12495c6c1daeSBarry Smith EXTERN_C_BEGIN
12505c6c1daeSBarry Smith #undef __FUNCT__
12515c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerCreate_Binary"
12525c6c1daeSBarry Smith PetscErrorCode PetscViewerCreate_Binary(PetscViewer v)
12535c6c1daeSBarry Smith {
12545c6c1daeSBarry Smith   PetscErrorCode     ierr;
12555c6c1daeSBarry Smith   PetscViewer_Binary *vbinary;
12565c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
12575c6c1daeSBarry Smith   PetscBool          useMPIIO = PETSC_FALSE;
12585c6c1daeSBarry Smith #endif
12595c6c1daeSBarry Smith 
12605c6c1daeSBarry Smith   PetscFunctionBegin;
12615c6c1daeSBarry Smith   ierr               = PetscNewLog(v,PetscViewer_Binary,&vbinary);CHKERRQ(ierr);
12625c6c1daeSBarry Smith   v->data            = (void*)vbinary;
12635c6c1daeSBarry Smith   v->ops->destroy    = PetscViewerDestroy_Binary;
1264*2bf49c77SBarry Smith   v->ops->view       = PetscViewerView_Binary;
12655c6c1daeSBarry Smith   v->ops->flush      = 0;
12665c6c1daeSBarry Smith   v->iformat         = 0;
12675c6c1daeSBarry Smith   vbinary->fdes_info = 0;
12685c6c1daeSBarry Smith   vbinary->fdes      = 0;
12695c6c1daeSBarry Smith   vbinary->skipinfo        = PETSC_FALSE;
12705c6c1daeSBarry Smith   vbinary->skipoptions     = PETSC_TRUE;
12715c6c1daeSBarry Smith   vbinary->skipheader      = PETSC_FALSE;
12725c6c1daeSBarry Smith   v->ops->getsingleton     = PetscViewerGetSingleton_Binary;
12735c6c1daeSBarry Smith   v->ops->restoresingleton = PetscViewerRestoreSingleton_Binary;
12745c6c1daeSBarry Smith   vbinary->btype           = (PetscFileMode) -1;
12755c6c1daeSBarry Smith   vbinary->storecompressed = PETSC_FALSE;
12765c6c1daeSBarry Smith   vbinary->filename        = 0;
12775c6c1daeSBarry Smith   vbinary->flowcontrol     = 256; /* seems a good number for Cray XT-5 */
12785c6c1daeSBarry Smith 
12795c6c1daeSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerBinaryGetFlowControl_C",
12805c6c1daeSBarry Smith                                     "PetscViewerBinaryGetFlowControl_Binary",
12815c6c1daeSBarry Smith                                      PetscViewerBinaryGetFlowControl_Binary);CHKERRQ(ierr);
12825c6c1daeSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerBinarySetFlowControl_C",
12835c6c1daeSBarry Smith                                     "PetscViewerBinarySetFlowControl_Binary",
12845c6c1daeSBarry Smith                                      PetscViewerBinarySetFlowControl_Binary);CHKERRQ(ierr);
12855c6c1daeSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerBinarySetSkipHeader_C",
12865c6c1daeSBarry Smith                                     "PetscViewerBinarySetSkipHeader_Binary",
12875c6c1daeSBarry Smith                                      PetscViewerBinarySetSkipHeader_Binary);CHKERRQ(ierr);
12885c6c1daeSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerBinaryGetSkipHeader_C",
12895c6c1daeSBarry Smith                                     "PetscViewerBinaryGetSkipHeader_Binary",
12905c6c1daeSBarry Smith                                      PetscViewerBinaryGetSkipHeader_Binary);CHKERRQ(ierr);
12915c6c1daeSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerBinaryGetInfoPointer_C",
12925c6c1daeSBarry Smith                                     "PetscViewerBinaryGetInfoPointer_Binary",
12935c6c1daeSBarry Smith                                      PetscViewerBinaryGetInfoPointer_Binary);CHKERRQ(ierr);
12945c6c1daeSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerFileSetName_C",
12955c6c1daeSBarry Smith                                     "PetscViewerFileSetName_Binary",
12965c6c1daeSBarry Smith                                      PetscViewerFileSetName_Binary);CHKERRQ(ierr);
12975c6c1daeSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerFileSetMode_C",
12985c6c1daeSBarry Smith                                     "PetscViewerFileSetMode_Binary",
12995c6c1daeSBarry Smith                                      PetscViewerFileSetMode_Binary);CHKERRQ(ierr);
13005c6c1daeSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerFileGetMode_C",
13015c6c1daeSBarry Smith                                     "PetscViewerFileGetMode_Binary",
13025c6c1daeSBarry Smith                                      PetscViewerFileGetMode_Binary);CHKERRQ(ierr);
13035c6c1daeSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerFileGetName_C",
13045c6c1daeSBarry Smith 				    "PetscViewerFileGetName_Binary",
13055c6c1daeSBarry Smith 				     PetscViewerFileGetName_Binary);CHKERRQ(ierr);
13065c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO)
13075c6c1daeSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)v,"PetscViewerBinarySetMPIIO_C",
13085c6c1daeSBarry Smith                                     "PetscViewerBinarySetMPIIO_Binary",
13095c6c1daeSBarry Smith                                      PetscViewerBinarySetMPIIO_Binary);CHKERRQ(ierr);
13105c6c1daeSBarry Smith 
13115c6c1daeSBarry Smith   ierr = PetscOptionsGetBool(PETSC_NULL,"-viewer_binary_mpiio",&useMPIIO,PETSC_NULL);CHKERRQ(ierr);
13125c6c1daeSBarry Smith   if (useMPIIO) {
13135c6c1daeSBarry Smith     ierr = PetscViewerBinarySetMPIIO(v);CHKERRQ(ierr);
13145c6c1daeSBarry Smith   }
13155c6c1daeSBarry Smith #endif
13165c6c1daeSBarry Smith   PetscFunctionReturn(0);
13175c6c1daeSBarry Smith }
13185c6c1daeSBarry Smith EXTERN_C_END
13195c6c1daeSBarry Smith 
13205c6c1daeSBarry Smith 
13215c6c1daeSBarry Smith /* ---------------------------------------------------------------------*/
13225c6c1daeSBarry Smith /*
13235c6c1daeSBarry Smith     The variable Petsc_Viewer_Binary_keyval is used to indicate an MPI attribute that
13245c6c1daeSBarry Smith   is attached to a communicator, in this case the attribute is a PetscViewer.
13255c6c1daeSBarry Smith */
13265c6c1daeSBarry Smith static int Petsc_Viewer_Binary_keyval = MPI_KEYVAL_INVALID;
13275c6c1daeSBarry Smith 
13285c6c1daeSBarry Smith #undef __FUNCT__
13295c6c1daeSBarry Smith #define __FUNCT__ "PETSC_VIEWER_BINARY_"
13305c6c1daeSBarry Smith /*@C
13315c6c1daeSBarry Smith      PETSC_VIEWER_BINARY_ - Creates a binary PetscViewer shared by all processors
13325c6c1daeSBarry Smith                      in a communicator.
13335c6c1daeSBarry Smith 
13345c6c1daeSBarry Smith      Collective on MPI_Comm
13355c6c1daeSBarry Smith 
13365c6c1daeSBarry Smith      Input Parameter:
13375c6c1daeSBarry Smith .    comm - the MPI communicator to share the binary PetscViewer
13385c6c1daeSBarry Smith 
13395c6c1daeSBarry Smith      Level: intermediate
13405c6c1daeSBarry Smith 
13415c6c1daeSBarry Smith    Options Database Keys:
13425c6c1daeSBarry Smith +    -viewer_binary_filename <name>
13435c6c1daeSBarry Smith .    -viewer_binary_skip_info
13445c6c1daeSBarry Smith -    -viewer_binary_skip_options
13455c6c1daeSBarry Smith 
13465c6c1daeSBarry Smith    Environmental variables:
13475c6c1daeSBarry Smith -   PETSC_VIEWER_BINARY_FILENAME
13485c6c1daeSBarry Smith 
13495c6c1daeSBarry Smith      Notes:
13505c6c1daeSBarry Smith      Unlike almost all other PETSc routines, PETSC_VIEWER_BINARY_ does not return
13515c6c1daeSBarry Smith      an error code.  The binary PetscViewer is usually used in the form
13525c6c1daeSBarry Smith $       XXXView(XXX object,PETSC_VIEWER_BINARY_(comm));
13535c6c1daeSBarry Smith 
13545c6c1daeSBarry Smith .seealso: PETSC_VIEWER_BINARY_WORLD, PETSC_VIEWER_BINARY_SELF, PetscViewerBinaryOpen(), PetscViewerCreate(),
13555c6c1daeSBarry Smith           PetscViewerDestroy()
13565c6c1daeSBarry Smith @*/
13575c6c1daeSBarry Smith PetscViewer  PETSC_VIEWER_BINARY_(MPI_Comm comm)
13585c6c1daeSBarry Smith {
13595c6c1daeSBarry Smith   PetscErrorCode ierr;
13605c6c1daeSBarry Smith   PetscBool      flg;
13615c6c1daeSBarry Smith   PetscViewer    viewer;
13625c6c1daeSBarry Smith   char           fname[PETSC_MAX_PATH_LEN];
13635c6c1daeSBarry Smith   MPI_Comm       ncomm;
13645c6c1daeSBarry Smith 
13655c6c1daeSBarry Smith   PetscFunctionBegin;
13665c6c1daeSBarry Smith   ierr = PetscCommDuplicate(comm,&ncomm,PETSC_NULL);if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
13675c6c1daeSBarry Smith   if (Petsc_Viewer_Binary_keyval == MPI_KEYVAL_INVALID) {
13685c6c1daeSBarry Smith     ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Binary_keyval,0);
13695c6c1daeSBarry Smith     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
13705c6c1daeSBarry Smith   }
13715c6c1daeSBarry Smith   ierr = MPI_Attr_get(ncomm,Petsc_Viewer_Binary_keyval,(void **)&viewer,(int*)&flg);
13725c6c1daeSBarry Smith   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
13735c6c1daeSBarry Smith   if (!flg) { /* PetscViewer not yet created */
13745c6c1daeSBarry Smith     ierr = PetscOptionsGetenv(ncomm,"PETSC_VIEWER_BINARY_FILENAME",fname,PETSC_MAX_PATH_LEN,&flg);
13755c6c1daeSBarry Smith     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
13765c6c1daeSBarry Smith     if (!flg) {
13775c6c1daeSBarry Smith       ierr = PetscStrcpy(fname,"binaryoutput");
13785c6c1daeSBarry Smith       if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
13795c6c1daeSBarry Smith     }
13805c6c1daeSBarry Smith     ierr = PetscViewerBinaryOpen(ncomm,fname,FILE_MODE_WRITE,&viewer);
13815c6c1daeSBarry Smith     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
13825c6c1daeSBarry Smith     ierr = PetscObjectRegisterDestroy((PetscObject)viewer);
13835c6c1daeSBarry Smith     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
13845c6c1daeSBarry Smith     ierr = MPI_Attr_put(ncomm,Petsc_Viewer_Binary_keyval,(void*)viewer);
13855c6c1daeSBarry Smith     if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
13865c6c1daeSBarry Smith   }
13875c6c1daeSBarry Smith   ierr = PetscCommDestroy(&ncomm);
13885c6c1daeSBarry Smith   if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,__SDIR__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);}
13895c6c1daeSBarry Smith   PetscFunctionReturn(viewer);
13905c6c1daeSBarry Smith }
1391