15c6c1daeSBarry Smith 2*af0996ceSBarry Smith #include <petsc/private/viewerimpl.h> /*I "petscviewer.h" I*/ 35c6c1daeSBarry Smith #include <fcntl.h> 45c6c1daeSBarry Smith #if defined(PETSC_HAVE_UNISTD_H) 55c6c1daeSBarry Smith #include <unistd.h> 65c6c1daeSBarry Smith #endif 75c6c1daeSBarry Smith #if defined(PETSC_HAVE_IO_H) 85c6c1daeSBarry Smith #include <io.h> 95c6c1daeSBarry Smith #endif 105c6c1daeSBarry Smith 115c6c1daeSBarry Smith typedef struct { 125c6c1daeSBarry Smith int fdes; /* file descriptor, ignored if using MPI IO */ 135c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO) 14bc196f7cSDave May PetscBool usempiio; 155c6c1daeSBarry Smith MPI_File mfdes; /* ignored unless using MPI IO */ 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 */ 27c98fd787SBarry Smith PetscBool setfromoptionscalled; 285c6c1daeSBarry Smith } PetscViewer_Binary; 295c6c1daeSBarry Smith 305c6c1daeSBarry Smith #undef __FUNCT__ 315c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerGetSingleton_Binary" 325c6c1daeSBarry Smith PetscErrorCode PetscViewerGetSingleton_Binary(PetscViewer viewer,PetscViewer *outviewer) 335c6c1daeSBarry Smith { 345c6c1daeSBarry Smith int rank; 355c6c1daeSBarry Smith PetscErrorCode ierr; 365c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data,*obinary; 375c6c1daeSBarry Smith 385c6c1daeSBarry Smith PetscFunctionBegin; 39c98fd787SBarry Smith ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr); 40ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr); 415c6c1daeSBarry Smith if (!rank) { 425c6c1daeSBarry Smith ierr = PetscViewerCreate(PETSC_COMM_SELF,outviewer);CHKERRQ(ierr); 435c6c1daeSBarry Smith ierr = PetscViewerSetType(*outviewer,PETSCVIEWERBINARY);CHKERRQ(ierr); 445c6c1daeSBarry Smith obinary = (PetscViewer_Binary*)(*outviewer)->data; 455c6c1daeSBarry Smith ierr = PetscMemcpy(obinary,vbinary,sizeof(PetscViewer_Binary));CHKERRQ(ierr); 46c203d5c5SBarry Smith } SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Cannot get singleton viewer for binary files or sockets"); 475c6c1daeSBarry Smith PetscFunctionReturn(0); 485c6c1daeSBarry Smith } 495c6c1daeSBarry Smith 505c6c1daeSBarry Smith #undef __FUNCT__ 515c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerRestoreSingleton_Binary" 525c6c1daeSBarry Smith PetscErrorCode PetscViewerRestoreSingleton_Binary(PetscViewer viewer,PetscViewer *outviewer) 535c6c1daeSBarry Smith { 545c6c1daeSBarry Smith PetscErrorCode ierr; 555c6c1daeSBarry Smith PetscErrorCode rank; 565c6c1daeSBarry Smith 575c6c1daeSBarry Smith PetscFunctionBegin; 58ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr); 595c6c1daeSBarry Smith if (!rank) { 605c6c1daeSBarry Smith ierr = PetscFree((*outviewer)->data);CHKERRQ(ierr); 615c6c1daeSBarry Smith ierr = PetscHeaderDestroy(outviewer);CHKERRQ(ierr); 625c6c1daeSBarry Smith } 635c6c1daeSBarry Smith PetscFunctionReturn(0); 645c6c1daeSBarry Smith } 655c6c1daeSBarry Smith 665c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO) 675c6c1daeSBarry Smith #undef __FUNCT__ 685c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetMPIIOOffset" 695c6c1daeSBarry Smith /*@C 705c6c1daeSBarry Smith PetscViewerBinaryGetMPIIOOffset - Gets the current offset that should be passed to MPI_File_set_view() 715c6c1daeSBarry Smith 725c6c1daeSBarry Smith Not Collective 735c6c1daeSBarry Smith 745c6c1daeSBarry Smith Input Parameter: 755c6c1daeSBarry Smith . viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 765c6c1daeSBarry Smith 775c6c1daeSBarry Smith Output Parameter: 785c6c1daeSBarry Smith . off - the current offset 795c6c1daeSBarry Smith 805c6c1daeSBarry Smith Level: advanced 815c6c1daeSBarry Smith 825c6c1daeSBarry Smith Fortran Note: 835c6c1daeSBarry Smith This routine is not supported in Fortran. 845c6c1daeSBarry Smith 855c6c1daeSBarry Smith Use PetscViewerBinaryAddMPIIOOffset() to increase this value after you have written a view. 865c6c1daeSBarry Smith 875c6c1daeSBarry Smith Concepts: file descriptor^getting 885c6c1daeSBarry Smith Concepts: PetscViewerBinary^accessing file descriptor 895c6c1daeSBarry Smith 905c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer() 915c6c1daeSBarry Smith @*/ 925c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetMPIIOOffset(PetscViewer viewer,MPI_Offset *off) 935c6c1daeSBarry Smith { 945c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 955c6c1daeSBarry Smith 965c6c1daeSBarry Smith PetscFunctionBegin; 975c6c1daeSBarry Smith *off = vbinary->moff; 985c6c1daeSBarry Smith PetscFunctionReturn(0); 995c6c1daeSBarry Smith } 1005c6c1daeSBarry Smith 1015c6c1daeSBarry Smith #undef __FUNCT__ 1025c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryAddMPIIOOffset" 1035c6c1daeSBarry Smith /*@C 1045c6c1daeSBarry Smith PetscViewerBinaryAddMPIIOOffset - Adds to the current offset that should be passed to MPI_File_set_view() 1055c6c1daeSBarry Smith 1065c6c1daeSBarry Smith Not Collective 1075c6c1daeSBarry Smith 1085c6c1daeSBarry Smith Input Parameters: 1095c6c1daeSBarry Smith + viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 1105c6c1daeSBarry Smith - off - the addition to the offset 1115c6c1daeSBarry Smith 1125c6c1daeSBarry Smith Level: advanced 1135c6c1daeSBarry Smith 1145c6c1daeSBarry Smith Fortran Note: 1155c6c1daeSBarry Smith This routine is not supported in Fortran. 1165c6c1daeSBarry Smith 1175c6c1daeSBarry Smith Use PetscViewerBinaryGetMPIIOOffset() to get the value that you should pass to MPI_File_set_view() 1185c6c1daeSBarry Smith 1195c6c1daeSBarry Smith Concepts: file descriptor^getting 1205c6c1daeSBarry Smith Concepts: PetscViewerBinary^accessing file descriptor 1215c6c1daeSBarry Smith 1225c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer() 1235c6c1daeSBarry Smith @*/ 1245c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryAddMPIIOOffset(PetscViewer viewer,MPI_Offset off) 1255c6c1daeSBarry Smith { 1265c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 1275c6c1daeSBarry Smith 1285c6c1daeSBarry Smith PetscFunctionBegin; 1295c6c1daeSBarry Smith vbinary->moff += off; 1305c6c1daeSBarry Smith PetscFunctionReturn(0); 1315c6c1daeSBarry Smith } 1325c6c1daeSBarry Smith 1335c6c1daeSBarry Smith #undef __FUNCT__ 1345c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetMPIIODescriptor" 1355c6c1daeSBarry Smith /*@C 1365c6c1daeSBarry Smith PetscViewerBinaryGetMPIIODescriptor - Extracts the MPI IO file descriptor from a PetscViewer. 1375c6c1daeSBarry Smith 1385c6c1daeSBarry Smith Not Collective 1395c6c1daeSBarry Smith 1405c6c1daeSBarry Smith Input Parameter: 1415c6c1daeSBarry Smith . viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 1425c6c1daeSBarry Smith 1435c6c1daeSBarry Smith Output Parameter: 1445c6c1daeSBarry Smith . fdes - file descriptor 1455c6c1daeSBarry Smith 1465c6c1daeSBarry Smith Level: advanced 1475c6c1daeSBarry Smith 1485c6c1daeSBarry Smith Fortran Note: 1495c6c1daeSBarry Smith This routine is not supported in Fortran. 1505c6c1daeSBarry Smith 1515c6c1daeSBarry Smith Concepts: file descriptor^getting 1525c6c1daeSBarry Smith Concepts: PetscViewerBinary^accessing file descriptor 1535c6c1daeSBarry Smith 1545c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer() 1555c6c1daeSBarry Smith @*/ 1565c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetMPIIODescriptor(PetscViewer viewer,MPI_File *fdes) 1575c6c1daeSBarry Smith { 15803a1d59fSDave May PetscErrorCode ierr; 1595c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 1605c6c1daeSBarry Smith 1615c6c1daeSBarry Smith PetscFunctionBegin; 162c98fd787SBarry Smith ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr); 1635c6c1daeSBarry Smith *fdes = vbinary->mfdes; 1645c6c1daeSBarry Smith PetscFunctionReturn(0); 1655c6c1daeSBarry Smith } 1665c6c1daeSBarry Smith 167807ea322SDave May #undef __FUNCT__ 168bc196f7cSDave May #define __FUNCT__ "PetscViewerBinaryGetUseMPIIO_Binary" 169bc196f7cSDave May PetscErrorCode PetscViewerBinaryGetUseMPIIO_Binary(PetscViewer viewer,PetscBool *flg) 170a8aae444SDave May { 171a8aae444SDave May PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 172a8aae444SDave May 173a8aae444SDave May PetscFunctionBegin; 174bc196f7cSDave May *flg = vbinary->usempiio; 175a8aae444SDave May PetscFunctionReturn(0); 176a8aae444SDave May } 177a8aae444SDave May #endif 178a8aae444SDave May 179a8aae444SDave May 1805c6c1daeSBarry Smith #undef __FUNCT__ 181bc196f7cSDave May #define __FUNCT__ "PetscViewerBinaryGetUseMPIIO" 1825c6c1daeSBarry Smith /*@C 183bc196f7cSDave May PetscViewerBinaryGetUseMPIIO - Returns PETSC_TRUE if the binary viewer uses MPI-IO. 1845c6c1daeSBarry Smith 1855c6c1daeSBarry Smith Not Collective 1865c6c1daeSBarry Smith 1875c6c1daeSBarry Smith Input Parameter: 1885c6c1daeSBarry Smith . viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 1895c6c1daeSBarry Smith 1905c6c1daeSBarry Smith Output Parameter: 191bc196f7cSDave May - flg - PETSC_TRUE if MPI-IO is being used 1925c6c1daeSBarry Smith 1935c6c1daeSBarry Smith Options Database: 1945c6c1daeSBarry Smith -viewer_binary_mpiio : Flag for using MPI-IO 1955c6c1daeSBarry Smith 1965c6c1daeSBarry Smith Level: advanced 1975c6c1daeSBarry Smith 198bc196f7cSDave May Note: 199bc196f7cSDave May If MPI-IO is not available, this function will always return PETSC_FALSE 200bc196f7cSDave May 2015c6c1daeSBarry Smith Fortran Note: 2025c6c1daeSBarry Smith This routine is not supported in Fortran. 2035c6c1daeSBarry Smith 2045c6c1daeSBarry Smith Concepts: file descriptor^getting 2055c6c1daeSBarry Smith Concepts: PetscViewerBinary^accessing file descriptor 2065c6c1daeSBarry Smith 2075c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetInfoPointer() 2085c6c1daeSBarry Smith @*/ 209bc196f7cSDave May PetscErrorCode PetscViewerBinaryGetUseMPIIO(PetscViewer viewer,PetscBool *flg) 2105c6c1daeSBarry Smith { 211a8aae444SDave May PetscErrorCode ierr; 2125c6c1daeSBarry Smith 2135c6c1daeSBarry Smith PetscFunctionBegin; 214a8aae444SDave May *flg = PETSC_FALSE; 215bc196f7cSDave May ierr = PetscTryMethod(viewer,"PetscViewerBinaryGetUseMPIIO_C",(PetscViewer,PetscBool*),(viewer,flg));CHKERRQ(ierr); 2165c6c1daeSBarry Smith PetscFunctionReturn(0); 2175c6c1daeSBarry Smith } 2185c6c1daeSBarry Smith 2195c6c1daeSBarry Smith #undef __FUNCT__ 2205c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetFlowControl_Binary" 2215c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetFlowControl_Binary(PetscViewer viewer,PetscInt *fc) 2225c6c1daeSBarry Smith { 2235c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 2245c6c1daeSBarry Smith 2255c6c1daeSBarry Smith PetscFunctionBegin; 2265c6c1daeSBarry Smith *fc = vbinary->flowcontrol; 2275c6c1daeSBarry Smith PetscFunctionReturn(0); 2285c6c1daeSBarry Smith } 2295c6c1daeSBarry Smith 2305c6c1daeSBarry Smith #undef __FUNCT__ 2315c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetFlowControl" 2325c6c1daeSBarry Smith /*@C 2335c6c1daeSBarry Smith PetscViewerBinaryGetFlowControl - Returns how many messages are allowed to outstanding at the same time during parallel IO reads/writes 2345c6c1daeSBarry Smith 2355c6c1daeSBarry Smith Not Collective 2365c6c1daeSBarry Smith 2375c6c1daeSBarry Smith Input Parameter: 2385c6c1daeSBarry Smith . viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 2395c6c1daeSBarry Smith 2405c6c1daeSBarry Smith Output Parameter: 2415c6c1daeSBarry Smith . fc - the number of messages 2425c6c1daeSBarry Smith 2435c6c1daeSBarry Smith Level: advanced 2445c6c1daeSBarry Smith 2455c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer(), PetscViewerBinarySetFlowControl() 2465c6c1daeSBarry Smith 2475c6c1daeSBarry Smith @*/ 2485c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetFlowControl(PetscViewer viewer,PetscInt *fc) 2495c6c1daeSBarry Smith { 2505c6c1daeSBarry Smith PetscErrorCode ierr; 2515c6c1daeSBarry Smith 2525c6c1daeSBarry Smith PetscFunctionBegin; 2535c6c1daeSBarry Smith ierr = PetscTryMethod(viewer,"PetscViewerBinaryGetFlowControl_C",(PetscViewer,PetscInt*),(viewer,fc));CHKERRQ(ierr); 2545c6c1daeSBarry Smith PetscFunctionReturn(0); 2555c6c1daeSBarry Smith } 2565c6c1daeSBarry Smith 2575c6c1daeSBarry Smith #undef __FUNCT__ 2585c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinarySetFlowControl_Binary" 2595c6c1daeSBarry Smith PetscErrorCode PetscViewerBinarySetFlowControl_Binary(PetscViewer viewer,PetscInt fc) 2605c6c1daeSBarry Smith { 2615c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 2625c6c1daeSBarry Smith 2635c6c1daeSBarry Smith PetscFunctionBegin; 264ce94432eSBarry Smith if (fc <= 1) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_OUTOFRANGE,"Flow control count must be greater than 1, %D was set",fc); 2655c6c1daeSBarry Smith vbinary->flowcontrol = fc; 2665c6c1daeSBarry Smith PetscFunctionReturn(0); 2675c6c1daeSBarry Smith } 2685c6c1daeSBarry Smith 2695c6c1daeSBarry Smith #undef __FUNCT__ 2705c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinarySetFlowControl" 2715c6c1daeSBarry Smith /*@C 272639ff905SBarry Smith PetscViewerBinarySetFlowControl - Sets how many messages are allowed to outstanding at the same time during parallel IO reads/writes 2735c6c1daeSBarry Smith 2745c6c1daeSBarry Smith Not Collective 2755c6c1daeSBarry Smith 2765c6c1daeSBarry Smith Input Parameter: 2775c6c1daeSBarry Smith + viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 278639ff905SBarry Smith - fc - the number of messages, defaults to 256 if this function was not called 2795c6c1daeSBarry Smith 2805c6c1daeSBarry Smith Level: advanced 2815c6c1daeSBarry Smith 2825c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer(), PetscViewerBinaryGetFlowControl() 2835c6c1daeSBarry Smith 2845c6c1daeSBarry Smith @*/ 2855c6c1daeSBarry Smith PetscErrorCode PetscViewerBinarySetFlowControl(PetscViewer viewer,PetscInt fc) 2865c6c1daeSBarry Smith { 2875c6c1daeSBarry Smith PetscErrorCode ierr; 2885c6c1daeSBarry Smith 2895c6c1daeSBarry Smith PetscFunctionBegin; 2905c6c1daeSBarry Smith ierr = PetscUseMethod(viewer,"PetscViewerBinarySetFlowControl_C",(PetscViewer,PetscInt),(viewer,fc));CHKERRQ(ierr); 2915c6c1daeSBarry Smith PetscFunctionReturn(0); 2925c6c1daeSBarry Smith } 2935c6c1daeSBarry Smith 2945c6c1daeSBarry Smith #undef __FUNCT__ 2955c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetDescriptor" 2965c6c1daeSBarry Smith /*@C 2975c6c1daeSBarry Smith PetscViewerBinaryGetDescriptor - Extracts the file descriptor from a PetscViewer. 2985c6c1daeSBarry Smith 2995872f025SBarry Smith Collective On PetscViewer 3005c6c1daeSBarry Smith 3015c6c1daeSBarry Smith Input Parameter: 3025c6c1daeSBarry Smith . viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 3035c6c1daeSBarry Smith 3045c6c1daeSBarry Smith Output Parameter: 3055c6c1daeSBarry Smith . fdes - file descriptor 3065c6c1daeSBarry Smith 3075c6c1daeSBarry Smith Level: advanced 3085c6c1daeSBarry Smith 3095c6c1daeSBarry Smith Notes: 3105c6c1daeSBarry Smith For writable binary PetscViewers, the descriptor will only be valid for the 3115c6c1daeSBarry Smith first processor in the communicator that shares the PetscViewer. For readable 3125c6c1daeSBarry Smith files it will only be valid on nodes that have the file. If node 0 does not 3135c6c1daeSBarry Smith have the file it generates an error even if another node does have the file. 3145c6c1daeSBarry Smith 3155c6c1daeSBarry Smith Fortran Note: 3165c6c1daeSBarry Smith This routine is not supported in Fortran. 3175c6c1daeSBarry Smith 3185872f025SBarry Smith Developer Notes: This must be called on all processes because Dave May changed 3195872f025SBarry Smith the source code that this may be trigger a PetscViewerSetUp() call if it was not previously triggered. 3205872f025SBarry Smith 3215872f025SBarry Smith 3225c6c1daeSBarry Smith Concepts: file descriptor^getting 3235c6c1daeSBarry Smith Concepts: PetscViewerBinary^accessing file descriptor 3245c6c1daeSBarry Smith 3255c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer() 3265c6c1daeSBarry Smith @*/ 3275c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetDescriptor(PetscViewer viewer,int *fdes) 3285c6c1daeSBarry Smith { 32903a1d59fSDave May PetscErrorCode ierr; 3305c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 3315c6c1daeSBarry Smith 3325c6c1daeSBarry Smith PetscFunctionBegin; 333c98fd787SBarry Smith ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr); 3345c6c1daeSBarry Smith *fdes = vbinary->fdes; 3355c6c1daeSBarry Smith PetscFunctionReturn(0); 3365c6c1daeSBarry Smith } 3375c6c1daeSBarry Smith 3385c6c1daeSBarry Smith #undef __FUNCT__ 3395c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinarySkipInfo" 3405c6c1daeSBarry Smith /*@ 3415c6c1daeSBarry Smith PetscViewerBinarySkipInfo - Binary file will not have .info file created with it 3425c6c1daeSBarry Smith 3435c6c1daeSBarry Smith Not Collective 3445c6c1daeSBarry Smith 3455c6c1daeSBarry Smith Input Paramter: 3465c6c1daeSBarry Smith . viewer - PetscViewer context, obtained from PetscViewerCreate() 3475c6c1daeSBarry Smith 3485c6c1daeSBarry Smith Options Database Key: 3495c6c1daeSBarry Smith . -viewer_binary_skip_info 3505c6c1daeSBarry Smith 3515c6c1daeSBarry Smith Level: advanced 3525c6c1daeSBarry Smith 353a2d7db39SDave May Notes: This must be called after PetscViewerSetType(). If you use PetscViewerBinaryOpen() then 3545c6c1daeSBarry Smith you can only skip the info file with the -viewer_binary_skip_info flag. To use the function you must open the 355a2d7db39SDave May viewer with PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinarySkipInfo(). 3565c6c1daeSBarry Smith 3575c6c1daeSBarry Smith The .info contains meta information about the data in the binary file, for example the block size if it was 3585c6c1daeSBarry Smith set for a vector or matrix. 3595c6c1daeSBarry Smith 3605c6c1daeSBarry Smith Concepts: PetscViewerBinary^accessing info file 3615c6c1daeSBarry Smith 3625c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySetSkipOptions(), 363807ea322SDave May PetscViewerBinaryGetSkipOptions(), PetscViewerBinaryGetSkipInfo() 3645c6c1daeSBarry Smith @*/ 3655c6c1daeSBarry Smith PetscErrorCode PetscViewerBinarySkipInfo(PetscViewer viewer) 3665c6c1daeSBarry Smith { 3675c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 3685c6c1daeSBarry Smith 3695c6c1daeSBarry Smith PetscFunctionBegin; 3705c6c1daeSBarry Smith vbinary->skipinfo = PETSC_TRUE; 3715c6c1daeSBarry Smith PetscFunctionReturn(0); 3725c6c1daeSBarry Smith } 3735c6c1daeSBarry Smith 3745c6c1daeSBarry Smith #undef __FUNCT__ 375807ea322SDave May #define __FUNCT__ "PetscViewerBinarySetSkipInfo_Binary" 376807ea322SDave May PetscErrorCode PetscViewerBinarySetSkipInfo_Binary(PetscViewer viewer,PetscBool skip) 377807ea322SDave May { 378807ea322SDave May PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 379807ea322SDave May 380807ea322SDave May PetscFunctionBegin; 381807ea322SDave May vbinary->skipinfo = skip; 382807ea322SDave May PetscFunctionReturn(0); 383807ea322SDave May } 384807ea322SDave May 385807ea322SDave May #undef __FUNCT__ 386807ea322SDave May #define __FUNCT__ "PetscViewerBinarySetSkipInfo" 387807ea322SDave May /*@ 388807ea322SDave May PetscViewerBinarySetSkipInfo - Binary file will not have .info file created with it 389807ea322SDave May 390807ea322SDave May Not Collective 391807ea322SDave May 392807ea322SDave May Input Paramter: 393807ea322SDave May . viewer - PetscViewer context, obtained from PetscViewerCreate() 394807ea322SDave May 395807ea322SDave May Options Database Key: 396807ea322SDave May . -viewer_binary_skip_info 397807ea322SDave May 398807ea322SDave May Level: advanced 399807ea322SDave May 400807ea322SDave May Concepts: PetscViewerBinary^accessing info file 401807ea322SDave May 402807ea322SDave May .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySetSkipOptions(), 403807ea322SDave May PetscViewerBinaryGetSkipOptions(), PetscViewerBinaryGetSkipInfo() 404807ea322SDave May @*/ 405807ea322SDave May PetscErrorCode PetscViewerBinarySetSkipInfo(PetscViewer viewer,PetscBool skip) 406807ea322SDave May { 407807ea322SDave May PetscErrorCode ierr; 408807ea322SDave May 409807ea322SDave May PetscFunctionBegin; 410807ea322SDave May ierr = PetscUseMethod(viewer,"PetscViewerBinarySetSkipInfo_C",(PetscViewer,PetscBool),(viewer,skip));CHKERRQ(ierr); 411807ea322SDave May PetscFunctionReturn(0); 412807ea322SDave May } 413807ea322SDave May 414807ea322SDave May #undef __FUNCT__ 415807ea322SDave May #define __FUNCT__ "PetscViewerBinaryGetSkipInfo_Binary" 416807ea322SDave May PetscErrorCode PetscViewerBinaryGetSkipInfo_Binary(PetscViewer viewer,PetscBool *skip) 417807ea322SDave May { 418807ea322SDave May PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 419807ea322SDave May 420807ea322SDave May PetscFunctionBegin; 421807ea322SDave May *skip = vbinary->skipinfo; 422807ea322SDave May PetscFunctionReturn(0); 423807ea322SDave May } 424807ea322SDave May 425807ea322SDave May #undef __FUNCT__ 426807ea322SDave May #define __FUNCT__ "PetscViewerBinaryGetSkipInfo" 427807ea322SDave May /*@ 428807ea322SDave May PetscViewerBinaryGetSkipInfo - check if viewer wrote a .info file 429807ea322SDave May 430807ea322SDave May Not Collective 431807ea322SDave May 432807ea322SDave May Input Parameter: 433807ea322SDave May . viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 434807ea322SDave May 435807ea322SDave May Output Parameter: 436807ea322SDave May . skip - PETSC_TRUE implies the .info file was not generated 437807ea322SDave May 438807ea322SDave May Level: advanced 439807ea322SDave May 440807ea322SDave May Notes: This must be called after PetscViewerSetType() 441807ea322SDave May 442807ea322SDave May Concepts: PetscViewerBinary^accessing info file 443807ea322SDave May 444807ea322SDave May .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(), 445807ea322SDave May PetscViewerBinarySetSkipOptions(), PetscViewerBinarySetSkipInfo() 446807ea322SDave May @*/ 447807ea322SDave May PetscErrorCode PetscViewerBinaryGetSkipInfo(PetscViewer viewer,PetscBool *skip) 448807ea322SDave May { 449807ea322SDave May PetscErrorCode ierr; 450807ea322SDave May 451807ea322SDave May PetscFunctionBegin; 452807ea322SDave May ierr = PetscUseMethod(viewer,"PetscViewerBinaryGetSkipInfo_C",(PetscViewer,PetscBool*),(viewer,skip));CHKERRQ(ierr); 453807ea322SDave May PetscFunctionReturn(0); 454807ea322SDave May } 455807ea322SDave May 456807ea322SDave May #undef __FUNCT__ 457807ea322SDave May #define __FUNCT__ "PetscViewerBinarySetSkipOptions_Binary" 458807ea322SDave May PetscErrorCode PetscViewerBinarySetSkipOptions_Binary(PetscViewer viewer,PetscBool skip) 459807ea322SDave May { 460807ea322SDave May PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 461807ea322SDave May 462807ea322SDave May PetscFunctionBegin; 463807ea322SDave May vbinary->skipoptions = skip; 464807ea322SDave May PetscFunctionReturn(0); 465807ea322SDave May } 466807ea322SDave May 467807ea322SDave May #undef __FUNCT__ 4685c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinarySetSkipOptions" 4695c6c1daeSBarry Smith /*@ 4705c6c1daeSBarry Smith PetscViewerBinarySetSkipOptions - do not use the PETSc options database when loading objects 4715c6c1daeSBarry Smith 4725c6c1daeSBarry Smith Not Collective 4735c6c1daeSBarry Smith 4745c6c1daeSBarry Smith Input Parameters: 4755c6c1daeSBarry Smith + viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 4765c6c1daeSBarry Smith - skip - PETSC_TRUE means do not use 4775c6c1daeSBarry Smith 4785c6c1daeSBarry Smith Options Database Key: 4795c6c1daeSBarry Smith . -viewer_binary_skip_options 4805c6c1daeSBarry Smith 4815c6c1daeSBarry Smith Level: advanced 4825c6c1daeSBarry Smith 4835c6c1daeSBarry Smith Notes: This must be called after PetscViewerSetType() 4845c6c1daeSBarry Smith 4855c6c1daeSBarry Smith Concepts: PetscViewerBinary^accessing info file 4865c6c1daeSBarry Smith 4875c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(), 4885c6c1daeSBarry Smith PetscViewerBinaryGetSkipOptions() 4895c6c1daeSBarry Smith @*/ 4905c6c1daeSBarry Smith PetscErrorCode PetscViewerBinarySetSkipOptions(PetscViewer viewer,PetscBool skip) 4915c6c1daeSBarry Smith { 492807ea322SDave May PetscErrorCode ierr; 4935c6c1daeSBarry Smith 4945c6c1daeSBarry Smith PetscFunctionBegin; 495807ea322SDave May ierr = PetscUseMethod(viewer,"PetscViewerBinarySetSkipOptions_C",(PetscViewer,PetscBool),(viewer,skip));CHKERRQ(ierr); 496807ea322SDave May PetscFunctionReturn(0); 497807ea322SDave May } 498807ea322SDave May 499807ea322SDave May #undef __FUNCT__ 500807ea322SDave May #define __FUNCT__ "PetscViewerBinaryGetSkipOptions_Binary" 501807ea322SDave May PetscErrorCode PetscViewerBinaryGetSkipOptions_Binary(PetscViewer viewer,PetscBool *skip) 502807ea322SDave May { 503807ea322SDave May PetscViewer_Binary *vbinary; 504807ea322SDave May 505807ea322SDave May PetscFunctionBegin; 506807ea322SDave May PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 507807ea322SDave May vbinary = (PetscViewer_Binary*)viewer->data; 508807ea322SDave May *skip = vbinary->skipoptions; 5095c6c1daeSBarry Smith PetscFunctionReturn(0); 5105c6c1daeSBarry Smith } 5115c6c1daeSBarry Smith 5125c6c1daeSBarry Smith #undef __FUNCT__ 5135c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetSkipOptions" 5145c6c1daeSBarry Smith /*@ 5155c6c1daeSBarry Smith PetscViewerBinaryGetSkipOptions - checks if viewer uses the PETSc options database when loading objects 5165c6c1daeSBarry Smith 5175c6c1daeSBarry Smith Not Collective 5185c6c1daeSBarry Smith 5195c6c1daeSBarry Smith Input Parameter: 5205c6c1daeSBarry Smith . viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 5215c6c1daeSBarry Smith 5225c6c1daeSBarry Smith Output Parameter: 5235c6c1daeSBarry Smith . skip - PETSC_TRUE means do not use 5245c6c1daeSBarry Smith 5255c6c1daeSBarry Smith Level: advanced 5265c6c1daeSBarry Smith 5275c6c1daeSBarry Smith Notes: This must be called after PetscViewerSetType() 5285c6c1daeSBarry Smith 5295c6c1daeSBarry Smith Concepts: PetscViewerBinary^accessing info file 5305c6c1daeSBarry Smith 5315c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(), 5325c6c1daeSBarry Smith PetscViewerBinarySetSkipOptions() 5335c6c1daeSBarry Smith @*/ 5345c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetSkipOptions(PetscViewer viewer,PetscBool *skip) 5355c6c1daeSBarry Smith { 536807ea322SDave May PetscErrorCode ierr; 5375c6c1daeSBarry Smith 5385c6c1daeSBarry Smith PetscFunctionBegin; 539807ea322SDave May ierr = PetscUseMethod(viewer,"PetscViewerBinaryGetSkipOptions_C",(PetscViewer,PetscBool*),(viewer,skip));CHKERRQ(ierr); 5405c6c1daeSBarry Smith PetscFunctionReturn(0); 5415c6c1daeSBarry Smith } 5425c6c1daeSBarry Smith 5435c6c1daeSBarry Smith #undef __FUNCT__ 5445c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinarySetSkipHeader_Binary" 5455c6c1daeSBarry Smith PetscErrorCode PetscViewerBinarySetSkipHeader_Binary(PetscViewer viewer,PetscBool skip) 5465c6c1daeSBarry Smith { 5475c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 5485c6c1daeSBarry Smith 5495c6c1daeSBarry Smith PetscFunctionBegin; 5505c6c1daeSBarry Smith vbinary->skipheader = skip; 5515c6c1daeSBarry Smith PetscFunctionReturn(0); 5525c6c1daeSBarry Smith } 5535c6c1daeSBarry Smith 5545c6c1daeSBarry Smith #undef __FUNCT__ 5555c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinarySetSkipHeader" 5565c6c1daeSBarry Smith /*@ 5575c6c1daeSBarry Smith PetscViewerBinarySetSkipHeader - do not write a header with size information on output, just raw data 5585c6c1daeSBarry Smith 5595c6c1daeSBarry Smith Not Collective 5605c6c1daeSBarry Smith 5615c6c1daeSBarry Smith Input Parameters: 5625c6c1daeSBarry Smith + viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 5635c6c1daeSBarry Smith - skip - PETSC_TRUE means do not write header 5645c6c1daeSBarry Smith 5655c6c1daeSBarry Smith Options Database Key: 5665c6c1daeSBarry Smith . -viewer_binary_skip_header 5675c6c1daeSBarry Smith 5685c6c1daeSBarry Smith Level: advanced 5695c6c1daeSBarry Smith 5705c6c1daeSBarry Smith Notes: This must be called after PetscViewerSetType() 5715c6c1daeSBarry Smith 5725c6c1daeSBarry Smith Can ONLY be called on a binary viewer 5735c6c1daeSBarry Smith 5745c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(), 5755c6c1daeSBarry Smith PetscViewerBinaryGetSkipHeader() 5765c6c1daeSBarry Smith @*/ 5775c6c1daeSBarry Smith PetscErrorCode PetscViewerBinarySetSkipHeader(PetscViewer viewer,PetscBool skip) 5785c6c1daeSBarry Smith { 5795c6c1daeSBarry Smith PetscErrorCode ierr; 5805c6c1daeSBarry Smith 5815c6c1daeSBarry Smith PetscFunctionBegin; 5825c6c1daeSBarry Smith ierr = PetscUseMethod(viewer,"PetscViewerBinarySetSkipHeader_C",(PetscViewer,PetscBool),(viewer,skip));CHKERRQ(ierr); 5835c6c1daeSBarry Smith PetscFunctionReturn(0); 5845c6c1daeSBarry Smith } 5855c6c1daeSBarry Smith 5865c6c1daeSBarry Smith #undef __FUNCT__ 5875c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetSkipHeader_Binary" 5885c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetSkipHeader_Binary(PetscViewer viewer,PetscBool *skip) 5895c6c1daeSBarry Smith { 5905c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 5915c6c1daeSBarry Smith 5925c6c1daeSBarry Smith PetscFunctionBegin; 5935c6c1daeSBarry Smith *skip = vbinary->skipheader; 5945c6c1daeSBarry Smith PetscFunctionReturn(0); 5955c6c1daeSBarry Smith } 5965c6c1daeSBarry Smith 5975c6c1daeSBarry Smith #undef __FUNCT__ 5985c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetSkipHeader" 5995c6c1daeSBarry Smith /*@ 6005c6c1daeSBarry Smith PetscViewerBinaryGetSkipHeader - checks whether to write a header with size information on output, or just raw data 6015c6c1daeSBarry Smith 6025c6c1daeSBarry Smith Not Collective 6035c6c1daeSBarry Smith 6045c6c1daeSBarry Smith Input Parameter: 6055c6c1daeSBarry Smith . viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 6065c6c1daeSBarry Smith 6075c6c1daeSBarry Smith Output Parameter: 6085c6c1daeSBarry Smith . skip - PETSC_TRUE means do not write header 6095c6c1daeSBarry Smith 6105c6c1daeSBarry Smith Level: advanced 6115c6c1daeSBarry Smith 6125c6c1daeSBarry Smith Notes: This must be called after PetscViewerSetType() 6135c6c1daeSBarry Smith 6145c6c1daeSBarry Smith Returns false for PETSCSOCKETVIEWER, you cannot skip the header for it. 6155c6c1daeSBarry Smith 6165c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(), 6175c6c1daeSBarry Smith PetscViewerBinarySetSkipHeader() 6185c6c1daeSBarry Smith @*/ 6195c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetSkipHeader(PetscViewer viewer,PetscBool *skip) 6205c6c1daeSBarry Smith { 6215c6c1daeSBarry Smith PetscErrorCode ierr; 6225c6c1daeSBarry Smith 6235c6c1daeSBarry Smith PetscFunctionBegin; 6245c6c1daeSBarry Smith *skip = PETSC_FALSE; 6255c6c1daeSBarry Smith ierr = PetscTryMethod(viewer,"PetscViewerBinaryGetSkipHeader_C",(PetscViewer,PetscBool*),(viewer,skip));CHKERRQ(ierr); 6265c6c1daeSBarry Smith PetscFunctionReturn(0); 6275c6c1daeSBarry Smith } 6285c6c1daeSBarry Smith 6295c6c1daeSBarry Smith #undef __FUNCT__ 6305c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetInfoPointer_Binary" 6315c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetInfoPointer_Binary(PetscViewer viewer,FILE **file) 6325c6c1daeSBarry Smith { 633f3b211e4SSatish Balay PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 634a261c58fSBarry Smith PetscErrorCode ierr; 635a261c58fSBarry Smith MPI_Comm comm; 6365c6c1daeSBarry Smith 6375c6c1daeSBarry Smith PetscFunctionBegin; 638c98fd787SBarry Smith ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr); 6395c6c1daeSBarry Smith *file = vbinary->fdes_info; 640a261c58fSBarry Smith if (viewer->format == PETSC_VIEWER_BINARY_MATLAB && !vbinary->matlabheaderwritten) { 641a261c58fSBarry Smith vbinary->matlabheaderwritten = PETSC_TRUE; 642a261c58fSBarry Smith ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr); 643da88d4d4SJed Brown ierr = PetscFPrintf(comm,*file,"#--- begin code written by PetscViewerBinary for MATLAB format ---#\n");CHKERRQ(ierr); 644da88d4d4SJed Brown ierr = PetscFPrintf(comm,*file,"#$$ Set.filename = '%s';\n",vbinary->filename);CHKERRQ(ierr); 645da88d4d4SJed Brown ierr = PetscFPrintf(comm,*file,"#$$ fd = PetscOpenFile(Set.filename);\n");CHKERRQ(ierr); 646da88d4d4SJed Brown ierr = PetscFPrintf(comm,*file,"#--- end code written by PetscViewerBinary for MATLAB format ---#\n\n");CHKERRQ(ierr); 647a261c58fSBarry Smith } 6485c6c1daeSBarry Smith PetscFunctionReturn(0); 6495c6c1daeSBarry Smith } 6505c6c1daeSBarry Smith 6515c6c1daeSBarry Smith #undef __FUNCT__ 6525c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryGetInfoPointer" 6535c6c1daeSBarry Smith /*@C 6545c6c1daeSBarry Smith PetscViewerBinaryGetInfoPointer - Extracts the file pointer for the ASCII 6555c6c1daeSBarry Smith info file associated with a binary file. 6565c6c1daeSBarry Smith 6575c6c1daeSBarry Smith Not Collective 6585c6c1daeSBarry Smith 6595c6c1daeSBarry Smith Input Parameter: 6605c6c1daeSBarry Smith . viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 6615c6c1daeSBarry Smith 6625c6c1daeSBarry Smith Output Parameter: 6630298fd71SBarry Smith . file - file pointer Always returns NULL if not a binary viewer 6645c6c1daeSBarry Smith 6655c6c1daeSBarry Smith Level: advanced 6665c6c1daeSBarry Smith 6675c6c1daeSBarry Smith Notes: 6685c6c1daeSBarry Smith For writable binary PetscViewers, the descriptor will only be valid for the 6695c6c1daeSBarry Smith first processor in the communicator that shares the PetscViewer. 6705c6c1daeSBarry Smith 6715c6c1daeSBarry Smith Fortran Note: 6725c6c1daeSBarry Smith This routine is not supported in Fortran. 6735c6c1daeSBarry Smith 6745c6c1daeSBarry Smith Concepts: PetscViewerBinary^accessing info file 6755c6c1daeSBarry Smith 6765c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetDescriptor() 6775c6c1daeSBarry Smith @*/ 6785c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetInfoPointer(PetscViewer viewer,FILE **file) 6795c6c1daeSBarry Smith { 6805c6c1daeSBarry Smith PetscErrorCode ierr; 6815c6c1daeSBarry Smith 6825c6c1daeSBarry Smith PetscFunctionBegin; 6830298fd71SBarry Smith *file = NULL; 6845c6c1daeSBarry Smith ierr = PetscTryMethod(viewer,"PetscViewerBinaryGetInfoPointer_C",(PetscViewer,FILE **),(viewer,file));CHKERRQ(ierr); 6855c6c1daeSBarry Smith PetscFunctionReturn(0); 6865c6c1daeSBarry Smith } 6875c6c1daeSBarry Smith 6885c6c1daeSBarry Smith #undef __FUNCT__ 6895c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileClose_Binary" 6905c6c1daeSBarry Smith static PetscErrorCode PetscViewerFileClose_Binary(PetscViewer v) 6915c6c1daeSBarry Smith { 6925c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data; 6935c6c1daeSBarry Smith PetscErrorCode ierr; 6945c6c1daeSBarry Smith PetscMPIInt rank; 6955c6c1daeSBarry Smith int err; 6965c6c1daeSBarry Smith 6975c6c1daeSBarry Smith PetscFunctionBegin; 698ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)v),&rank);CHKERRQ(ierr); 6995c6c1daeSBarry Smith if ((!rank || vbinary->btype == FILE_MODE_READ) && vbinary->fdes) { 7005c6c1daeSBarry Smith close(vbinary->fdes); 7015c6c1daeSBarry Smith if (!rank && vbinary->storecompressed) { 7025c6c1daeSBarry Smith char par[PETSC_MAX_PATH_LEN],buf[PETSC_MAX_PATH_LEN]; 7035c6c1daeSBarry Smith FILE *fp; 7045c6c1daeSBarry Smith /* compress the file */ 7055c6c1daeSBarry Smith ierr = PetscStrcpy(par,"gzip -f ");CHKERRQ(ierr); 7065c6c1daeSBarry Smith ierr = PetscStrcat(par,vbinary->filename);CHKERRQ(ierr); 7075c6c1daeSBarry Smith #if defined(PETSC_HAVE_POPEN) 7080298fd71SBarry Smith ierr = PetscPOpen(PETSC_COMM_SELF,NULL,par,"r",&fp);CHKERRQ(ierr); 7095c6c1daeSBarry Smith if (fgets(buf,1024,fp)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error from command %s\n%s",par,buf); 7100298fd71SBarry Smith ierr = PetscPClose(PETSC_COMM_SELF,fp,NULL);CHKERRQ(ierr); 7115c6c1daeSBarry Smith #else 7125c6c1daeSBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine"); 7135c6c1daeSBarry Smith #endif 7145c6c1daeSBarry Smith } 7155c6c1daeSBarry Smith } 7165c6c1daeSBarry Smith if (vbinary->fdes_info) { 7175c6c1daeSBarry Smith err = fclose(vbinary->fdes_info); 7185c6c1daeSBarry Smith if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file"); 7195c6c1daeSBarry Smith } 7205c6c1daeSBarry Smith PetscFunctionReturn(0); 7215c6c1daeSBarry Smith } 7225c6c1daeSBarry Smith 723e0385b85SDave May #if defined(PETSC_HAVE_MPIIO) 724e0385b85SDave May #undef __FUNCT__ 725e0385b85SDave May #define __FUNCT__ "PetscViewerFileClose_BinaryMPIIO" 726e0385b85SDave May static PetscErrorCode PetscViewerFileClose_BinaryMPIIO(PetscViewer v) 727e0385b85SDave May { 728e0385b85SDave May PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data; 729e0385b85SDave May int err; 730e0385b85SDave May PetscErrorCode ierr; 731e0385b85SDave May 732e0385b85SDave May PetscFunctionBegin; 733e0385b85SDave May if (vbinary->mfdes) { 734e0385b85SDave May ierr = MPI_File_close(&vbinary->mfdes);CHKERRQ(ierr); 735e0385b85SDave May } 736e0385b85SDave May if (vbinary->fdes_info) { 737e0385b85SDave May err = fclose(vbinary->fdes_info); 738e0385b85SDave May if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file"); 739e0385b85SDave May } 740e0385b85SDave May PetscFunctionReturn(0); 741e0385b85SDave May } 742e0385b85SDave May #endif 743e0385b85SDave May 7445c6c1daeSBarry Smith #undef __FUNCT__ 7455c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerDestroy_Binary" 7465c6c1daeSBarry Smith PetscErrorCode PetscViewerDestroy_Binary(PetscViewer v) 7475c6c1daeSBarry Smith { 7485c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data; 7495c6c1daeSBarry Smith PetscErrorCode ierr; 7505c6c1daeSBarry Smith 7515c6c1daeSBarry Smith PetscFunctionBegin; 752a261c58fSBarry Smith if (v->format == PETSC_VIEWER_BINARY_MATLAB) { 753a261c58fSBarry Smith MPI_Comm comm; 754a261c58fSBarry Smith FILE *info; 755a261c58fSBarry Smith 756a261c58fSBarry Smith ierr = PetscObjectGetComm((PetscObject)v,&comm);CHKERRQ(ierr); 757a261c58fSBarry Smith ierr = PetscViewerBinaryGetInfoPointer(v,&info);CHKERRQ(ierr); 758da88d4d4SJed Brown ierr = PetscFPrintf(comm,info,"#--- begin code written by PetscViewerBinary for MATLAB format ---#\n");CHKERRQ(ierr); 759da88d4d4SJed Brown ierr = PetscFPrintf(comm,info,"#$$ close(fd);\n");CHKERRQ(ierr); 760da88d4d4SJed Brown ierr = PetscFPrintf(comm,info,"#--- end code written by PetscViewerBinary for MATLAB format ---#\n\n");CHKERRQ(ierr); 761a261c58fSBarry Smith } 7625c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO) 763bc196f7cSDave May if (vbinary->usempiio) { 764e0385b85SDave May ierr = PetscViewerFileClose_BinaryMPIIO(v);CHKERRQ(ierr); 765e0385b85SDave May } else { 766e0385b85SDave May #endif 767e0385b85SDave May ierr = PetscViewerFileClose_Binary(v);CHKERRQ(ierr); 768e0385b85SDave May #if defined(PETSC_HAVE_MPIIO) 7695c6c1daeSBarry Smith } 7705c6c1daeSBarry Smith #endif 7712259a519SDave May if (vbinary->filename) { ierr = PetscFree(vbinary->filename);CHKERRQ(ierr); } 772e0385b85SDave May ierr = PetscFree(vbinary);CHKERRQ(ierr); 773e0385b85SDave May PetscFunctionReturn(0); 774e0385b85SDave May } 7755c6c1daeSBarry Smith 7765c6c1daeSBarry Smith #undef __FUNCT__ 7775c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryOpen" 7785c6c1daeSBarry Smith /*@C 7795c6c1daeSBarry Smith PetscViewerBinaryOpen - Opens a file for binary input/output. 7805c6c1daeSBarry Smith 7815c6c1daeSBarry Smith Collective on MPI_Comm 7825c6c1daeSBarry Smith 7835c6c1daeSBarry Smith Input Parameters: 7845c6c1daeSBarry Smith + comm - MPI communicator 7855c6c1daeSBarry Smith . name - name of file 7865c6c1daeSBarry Smith - type - type of file 7875c6c1daeSBarry Smith $ FILE_MODE_WRITE - create new file for binary output 7885c6c1daeSBarry Smith $ FILE_MODE_READ - open existing file for binary input 7895c6c1daeSBarry Smith $ FILE_MODE_APPEND - open existing file for binary output 7905c6c1daeSBarry Smith 7915c6c1daeSBarry Smith Output Parameter: 7925c6c1daeSBarry Smith . binv - PetscViewer for binary input/output to use with the specified file 7935c6c1daeSBarry Smith 7945c6c1daeSBarry Smith Options Database Keys: 795a2d7db39SDave May + -viewer_binary_filename <name> 796a2d7db39SDave May . -viewer_binary_skip_info 7975c6c1daeSBarry Smith . -viewer_binary_skip_options 798a2d7db39SDave May . -viewer_binary_skip_header 799a2d7db39SDave May - -viewer_binary_mpiio 8005c6c1daeSBarry Smith 8015c6c1daeSBarry Smith Level: beginner 8025c6c1daeSBarry Smith 8035c6c1daeSBarry Smith Note: 8045c6c1daeSBarry Smith This PetscViewer should be destroyed with PetscViewerDestroy(). 8055c6c1daeSBarry Smith 8065c6c1daeSBarry Smith For reading files, the filename may begin with ftp:// or http:// and/or 8075c6c1daeSBarry Smith end with .gz; in this case file is brought over and uncompressed. 8085c6c1daeSBarry Smith 8095c6c1daeSBarry Smith For creating files, if the file name ends with .gz it is automatically 8105c6c1daeSBarry Smith compressed when closed. 8115c6c1daeSBarry Smith 8125c6c1daeSBarry Smith For writing files it only opens the file on processor 0 in the communicator. 8135c6c1daeSBarry Smith For readable files it opens the file on all nodes that have the file. If 8145c6c1daeSBarry Smith node 0 does not have the file it generates an error even if other nodes 8155c6c1daeSBarry Smith do have the file. 8165c6c1daeSBarry Smith 8175c6c1daeSBarry Smith Concepts: binary files 8185c6c1daeSBarry Smith Concepts: PetscViewerBinary^creating 8195c6c1daeSBarry Smith Concepts: gzip 8205c6c1daeSBarry Smith Concepts: accessing remote file 8215c6c1daeSBarry Smith Concepts: remote file 8225c6c1daeSBarry Smith 8235c6c1daeSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(), 8245c6c1daeSBarry Smith VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(), 8255c6c1daeSBarry Smith PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscViewerBinaryRead() 8265c6c1daeSBarry Smith @*/ 8275c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryOpen(MPI_Comm comm,const char name[],PetscFileMode type,PetscViewer *binv) 8285c6c1daeSBarry Smith { 8295c6c1daeSBarry Smith PetscErrorCode ierr; 8305c6c1daeSBarry Smith 8315c6c1daeSBarry Smith PetscFunctionBegin; 8325c6c1daeSBarry Smith ierr = PetscViewerCreate(comm,binv);CHKERRQ(ierr); 8335c6c1daeSBarry Smith ierr = PetscViewerSetType(*binv,PETSCVIEWERBINARY);CHKERRQ(ierr); 8345c6c1daeSBarry Smith ierr = PetscViewerFileSetMode(*binv,type);CHKERRQ(ierr); 8355c6c1daeSBarry Smith ierr = PetscViewerFileSetName(*binv,name);CHKERRQ(ierr); 83603a1d59fSDave May ierr = PetscViewerSetFromOptions(*binv);CHKERRQ(ierr); 8375c6c1daeSBarry Smith PetscFunctionReturn(0); 8385c6c1daeSBarry Smith } 8395c6c1daeSBarry Smith 8405c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO) 8415c6c1daeSBarry Smith #undef __FUNCT__ 842ff5f5b63SDave May #define __FUNCT__ "PetscViewerBinaryWriteReadMPIIO" 843060da220SMatthew G. Knepley static PetscErrorCode PetscViewerBinaryWriteReadMPIIO(PetscViewer viewer,void *data,PetscInt num,PetscInt *count,PetscDataType dtype,PetscBool write) 8445c6c1daeSBarry Smith { 8455c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 8465c6c1daeSBarry Smith PetscErrorCode ierr; 8475c6c1daeSBarry Smith MPI_Datatype mdtype; 848e597bc1dSJed Brown PetscMPIInt cnt; 8495c6c1daeSBarry Smith MPI_Status status; 8505c6c1daeSBarry Smith MPI_Aint ul,dsize; 8515c6c1daeSBarry Smith 8525c6c1daeSBarry Smith PetscFunctionBegin; 853060da220SMatthew G. Knepley ierr = PetscMPIIntCast(num,&cnt);CHKERRQ(ierr); 8545c6c1daeSBarry Smith ierr = PetscDataTypeToMPIDataType(dtype,&mdtype);CHKERRQ(ierr); 8555c6c1daeSBarry Smith ierr = MPI_File_set_view(vbinary->mfdes,vbinary->moff,mdtype,mdtype,(char*)"native",MPI_INFO_NULL);CHKERRQ(ierr); 8565c6c1daeSBarry Smith if (write) { 8575c6c1daeSBarry Smith ierr = MPIU_File_write_all(vbinary->mfdes,data,cnt,mdtype,&status);CHKERRQ(ierr); 8585c6c1daeSBarry Smith } else { 8595c6c1daeSBarry Smith ierr = MPIU_File_read_all(vbinary->mfdes,data,cnt,mdtype,&status);CHKERRQ(ierr); 8605c6c1daeSBarry Smith } 8615c6c1daeSBarry Smith ierr = MPI_Type_get_extent(mdtype,&ul,&dsize);CHKERRQ(ierr); 862a297a907SKarl Rupp 8635c6c1daeSBarry Smith vbinary->moff += dsize*cnt; 864060da220SMatthew G. Knepley if (count) *count = num; 8655c6c1daeSBarry Smith PetscFunctionReturn(0); 8665c6c1daeSBarry Smith } 8675c6c1daeSBarry Smith #endif 8685c6c1daeSBarry Smith 8695c6c1daeSBarry Smith #undef __FUNCT__ 8705c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryRead" 8715c6c1daeSBarry Smith /*@C 8725c6c1daeSBarry Smith PetscViewerBinaryRead - Reads from a binary file, all processors get the same result 8735c6c1daeSBarry Smith 8745c6c1daeSBarry Smith Collective on MPI_Comm 8755c6c1daeSBarry Smith 8765c6c1daeSBarry Smith Input Parameters: 8775c6c1daeSBarry Smith + viewer - the binary viewer 878907376e6SBarry Smith . data - location of the data to be written 879060da220SMatthew G. Knepley . num - number of items of data to read 880907376e6SBarry Smith - dtype - type of data to read 8815c6c1daeSBarry Smith 882f8e4bde8SMatthew G. Knepley Output Parameters: 883060da220SMatthew G. Knepley . count - number of items of data actually read, or NULL 884f8e4bde8SMatthew G. Knepley 8855c6c1daeSBarry Smith Level: beginner 8865c6c1daeSBarry Smith 8875c6c1daeSBarry Smith Concepts: binary files 8885c6c1daeSBarry Smith 8895c6c1daeSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(), 8905c6c1daeSBarry Smith VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(), 8915c6c1daeSBarry Smith PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead() 8925c6c1daeSBarry Smith @*/ 893060da220SMatthew G. Knepley PetscErrorCode PetscViewerBinaryRead(PetscViewer viewer,void *data,PetscInt num,PetscInt *count,PetscDataType dtype) 8945c6c1daeSBarry Smith { 8955c6c1daeSBarry Smith PetscErrorCode ierr; 8965c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 8975c6c1daeSBarry Smith 898c98fd787SBarry Smith ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr); 8995c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO) 900bc196f7cSDave May if (vbinary->usempiio) { 901060da220SMatthew G. Knepley ierr = PetscViewerBinaryWriteReadMPIIO(viewer,data,num,count,dtype,PETSC_FALSE);CHKERRQ(ierr); 9025c6c1daeSBarry Smith } else { 9035c6c1daeSBarry Smith #endif 904060da220SMatthew G. Knepley ierr = PetscBinarySynchronizedRead(PetscObjectComm((PetscObject)viewer),vbinary->fdes,data,num,dtype);CHKERRQ(ierr); 9055c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO) 9065c6c1daeSBarry Smith } 9075c6c1daeSBarry Smith #endif 9085c6c1daeSBarry Smith PetscFunctionReturn(0); 9095c6c1daeSBarry Smith } 9105c6c1daeSBarry Smith 9115c6c1daeSBarry Smith #undef __FUNCT__ 9125c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryWrite" 9135c6c1daeSBarry Smith /*@C 9145c6c1daeSBarry Smith PetscViewerBinaryWrite - writes to a binary file, only from the first process 9155c6c1daeSBarry Smith 9165c6c1daeSBarry Smith Collective on MPI_Comm 9175c6c1daeSBarry Smith 9185c6c1daeSBarry Smith Input Parameters: 9195c6c1daeSBarry Smith + viewer - the binary viewer 9205c6c1daeSBarry Smith . data - location of data 9215824c9d0SPatrick Sanan . count - number of items of data to write 9225824c9d0SPatrick Sanan . dtype - type of data to write 9235c6c1daeSBarry Smith - istemp - data may be overwritten 9245c6c1daeSBarry Smith 9255c6c1daeSBarry Smith Level: beginner 9265c6c1daeSBarry Smith 9275c6c1daeSBarry Smith Notes: because byte-swapping may be done on the values in data it cannot be declared const 9285c6c1daeSBarry Smith 9295c6c1daeSBarry Smith Concepts: binary files 9305c6c1daeSBarry Smith 9315c6c1daeSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(), 9325c6c1daeSBarry Smith VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(), PetscDataType 9335c6c1daeSBarry Smith PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead() 9345c6c1daeSBarry Smith @*/ 9355c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryWrite(PetscViewer viewer,void *data,PetscInt count,PetscDataType dtype,PetscBool istemp) 9365c6c1daeSBarry Smith { 9375c6c1daeSBarry Smith PetscErrorCode ierr; 9385c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 9395c6c1daeSBarry Smith 9405c6c1daeSBarry Smith PetscFunctionBegin; 941c98fd787SBarry Smith ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr); 9425c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO) 943bc196f7cSDave May if (vbinary->usempiio) { 944060da220SMatthew G. Knepley ierr = PetscViewerBinaryWriteReadMPIIO(viewer,data,count,NULL,dtype,PETSC_TRUE);CHKERRQ(ierr); 9455c6c1daeSBarry Smith } else { 9465c6c1daeSBarry Smith #endif 947ce94432eSBarry Smith ierr = PetscBinarySynchronizedWrite(PetscObjectComm((PetscObject)viewer),vbinary->fdes,data,count,dtype,istemp);CHKERRQ(ierr); 9485c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO) 9495c6c1daeSBarry Smith } 9505c6c1daeSBarry Smith #endif 9515c6c1daeSBarry Smith PetscFunctionReturn(0); 9525c6c1daeSBarry Smith } 9535c6c1daeSBarry Smith 9545c6c1daeSBarry Smith #undef __FUNCT__ 9555c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerBinaryWriteStringArray" 9565c6c1daeSBarry Smith /*@C 9575c6c1daeSBarry Smith PetscViewerBinaryWriteStringArray - writes to a binary file, only from the first process an array of strings 9585c6c1daeSBarry Smith 9595c6c1daeSBarry Smith Collective on MPI_Comm 9605c6c1daeSBarry Smith 9615c6c1daeSBarry Smith Input Parameters: 9625c6c1daeSBarry Smith + viewer - the binary viewer 9635c6c1daeSBarry Smith - data - location of the array of strings 9645c6c1daeSBarry Smith 9655c6c1daeSBarry Smith 9665c6c1daeSBarry Smith Level: intermediate 9675c6c1daeSBarry Smith 9685c6c1daeSBarry Smith Concepts: binary files 9695c6c1daeSBarry Smith 9705c6c1daeSBarry Smith Notes: array of strings is null terminated 9715c6c1daeSBarry Smith 9725c6c1daeSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(), 9735c6c1daeSBarry Smith VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(), 9745c6c1daeSBarry Smith PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead() 9755c6c1daeSBarry Smith @*/ 9765c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryWriteStringArray(PetscViewer viewer,char **data) 9775c6c1daeSBarry Smith { 9785c6c1daeSBarry Smith PetscErrorCode ierr; 9795c6c1daeSBarry Smith PetscInt i,n = 0,*sizes; 9805c6c1daeSBarry Smith 981c98fd787SBarry Smith ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr); 9825c6c1daeSBarry Smith /* count number of strings */ 9835c6c1daeSBarry Smith while (data[n++]) ; 9845c6c1daeSBarry Smith n--; 985854ce69bSBarry Smith ierr = PetscMalloc1(n+1,&sizes);CHKERRQ(ierr); 9865c6c1daeSBarry Smith sizes[0] = n; 9875c6c1daeSBarry Smith for (i=0; i<n; i++) { 9885c6c1daeSBarry Smith size_t tmp; 9895c6c1daeSBarry Smith ierr = PetscStrlen(data[i],&tmp);CHKERRQ(ierr); 9905c6c1daeSBarry Smith sizes[i+1] = tmp + 1; /* size includes space for the null terminator */ 9915c6c1daeSBarry Smith } 9925c6c1daeSBarry Smith ierr = PetscViewerBinaryWrite(viewer,sizes,n+1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 9935c6c1daeSBarry Smith for (i=0; i<n; i++) { 9945c6c1daeSBarry Smith ierr = PetscViewerBinaryWrite(viewer,data[i],sizes[i+1],PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 9955c6c1daeSBarry Smith } 9965c6c1daeSBarry Smith ierr = PetscFree(sizes);CHKERRQ(ierr); 9975c6c1daeSBarry Smith PetscFunctionReturn(0); 9985c6c1daeSBarry Smith } 9995c6c1daeSBarry Smith 1000818fcb50SDave May #undef __FUNCT__ 1001818fcb50SDave May #define __FUNCT__ "PetscViewerBinaryReadStringArray" 10025c6c1daeSBarry Smith /*@C 10035c6c1daeSBarry Smith PetscViewerBinaryReadStringArray - reads a binary file an array of strings 10045c6c1daeSBarry Smith 10055c6c1daeSBarry Smith Collective on MPI_Comm 10065c6c1daeSBarry Smith 10075c6c1daeSBarry Smith Input Parameter: 10085c6c1daeSBarry Smith . viewer - the binary viewer 10095c6c1daeSBarry Smith 10105c6c1daeSBarry Smith Output Parameter: 10115c6c1daeSBarry Smith . data - location of the array of strings 10125c6c1daeSBarry Smith 10135c6c1daeSBarry Smith Level: intermediate 10145c6c1daeSBarry Smith 10155c6c1daeSBarry Smith Concepts: binary files 10165c6c1daeSBarry Smith 10175c6c1daeSBarry Smith Notes: array of strings is null terminated 10185c6c1daeSBarry Smith 10195c6c1daeSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerSetFormat(), PetscViewerDestroy(), 10205c6c1daeSBarry Smith VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(), 10215c6c1daeSBarry Smith PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead() 10225c6c1daeSBarry Smith @*/ 10235c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryReadStringArray(PetscViewer viewer,char ***data) 10245c6c1daeSBarry Smith { 10255c6c1daeSBarry Smith PetscErrorCode ierr; 1026060da220SMatthew G. Knepley PetscInt i,n,*sizes,N = 0; 10275c6c1daeSBarry Smith 1028c98fd787SBarry Smith ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr); 10295c6c1daeSBarry Smith /* count number of strings */ 1030060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,&n,1,NULL,PETSC_INT);CHKERRQ(ierr); 1031785e854fSJed Brown ierr = PetscMalloc1(n,&sizes);CHKERRQ(ierr); 1032060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,sizes,n,NULL,PETSC_INT);CHKERRQ(ierr); 1033a297a907SKarl Rupp for (i=0; i<n; i++) N += sizes[i]; 1034854ce69bSBarry Smith ierr = PetscMalloc((n+1)*sizeof(char*) + N*sizeof(char),data);CHKERRQ(ierr); 10355c6c1daeSBarry Smith (*data)[0] = (char*)((*data) + n + 1); 1036a297a907SKarl Rupp for (i=1; i<n; i++) (*data)[i] = (*data)[i-1] + sizes[i-1]; 1037060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,(*data)[0],N,NULL,PETSC_CHAR);CHKERRQ(ierr); 1038a297a907SKarl Rupp 10395c6c1daeSBarry Smith (*data)[n] = 0; 1040a297a907SKarl Rupp 10415c6c1daeSBarry Smith ierr = PetscFree(sizes);CHKERRQ(ierr); 10425c6c1daeSBarry Smith PetscFunctionReturn(0); 10435c6c1daeSBarry Smith } 10445c6c1daeSBarry Smith 10455c6c1daeSBarry Smith #undef __FUNCT__ 10465c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileGetName_Binary" 10475c6c1daeSBarry Smith PetscErrorCode PetscViewerFileGetName_Binary(PetscViewer viewer,const char **name) 10485c6c1daeSBarry Smith { 10495c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 10505c6c1daeSBarry Smith 10515c6c1daeSBarry Smith PetscFunctionBegin; 10525c6c1daeSBarry Smith *name = vbinary->filename; 10535c6c1daeSBarry Smith PetscFunctionReturn(0); 10545c6c1daeSBarry Smith } 10555c6c1daeSBarry Smith 10565c6c1daeSBarry Smith #undef __FUNCT__ 10575c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileGetMode" 10585c6c1daeSBarry Smith /*@C 10595c6c1daeSBarry Smith PetscViewerFileGetMode - Gets the type of file to be open 10605c6c1daeSBarry Smith 10615c6c1daeSBarry Smith Not Collective 10625c6c1daeSBarry Smith 10635c6c1daeSBarry Smith Input Parameter: 10645c6c1daeSBarry Smith . viewer - the PetscViewer; must be a binary, MATLAB, hdf, or netcdf PetscViewer 10655c6c1daeSBarry Smith 10665c6c1daeSBarry Smith Output Parameter: 10675c6c1daeSBarry Smith . type - type of file 10685c6c1daeSBarry Smith $ FILE_MODE_WRITE - create new file for binary output 10695c6c1daeSBarry Smith $ FILE_MODE_READ - open existing file for binary input 10705c6c1daeSBarry Smith $ FILE_MODE_APPEND - open existing file for binary output 10715c6c1daeSBarry Smith 10725c6c1daeSBarry Smith Level: advanced 10735c6c1daeSBarry Smith 10745c6c1daeSBarry Smith .seealso: PetscViewerFileSetMode(), PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinaryOpen() 10755c6c1daeSBarry Smith 10765c6c1daeSBarry Smith @*/ 10775c6c1daeSBarry Smith PetscErrorCode PetscViewerFileGetMode(PetscViewer viewer,PetscFileMode *type) 10785c6c1daeSBarry Smith { 10795c6c1daeSBarry Smith PetscErrorCode ierr; 10805c6c1daeSBarry Smith 10815c6c1daeSBarry Smith PetscFunctionBegin; 10825c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 10835c6c1daeSBarry Smith PetscValidPointer(type,2); 10845c6c1daeSBarry Smith ierr = PetscUseMethod(viewer,"PetscViewerFileGetMode_C",(PetscViewer,PetscFileMode*),(viewer,type));CHKERRQ(ierr); 10855c6c1daeSBarry Smith PetscFunctionReturn(0); 10865c6c1daeSBarry Smith } 10875c6c1daeSBarry Smith 10885c6c1daeSBarry Smith #undef __FUNCT__ 1089bc196f7cSDave May #define __FUNCT__ "PetscViewerBinarySetUseMPIIO" 10905c6c1daeSBarry Smith /*@ 1091bc196f7cSDave May PetscViewerBinarySetUseMPIIO - Sets a binary viewer to use MPI-IO for reading/writing. Must be called 10925c6c1daeSBarry Smith before PetscViewerFileSetName() 10935c6c1daeSBarry Smith 10945c6c1daeSBarry Smith Logically Collective on PetscViewer 10955c6c1daeSBarry Smith 10965c6c1daeSBarry Smith Input Parameters: 1097bc196f7cSDave May + viewer - the PetscViewer; must be a binary 1098bc196f7cSDave May - flg - PETSC_TRUE means MPI-IO will be used 10995c6c1daeSBarry Smith 11005c6c1daeSBarry Smith Options Database: 11015c6c1daeSBarry Smith -viewer_binary_mpiio : Flag for using MPI-IO 11025c6c1daeSBarry Smith 11035c6c1daeSBarry Smith Level: advanced 11045c6c1daeSBarry Smith 1105bc196f7cSDave May .seealso: PetscViewerFileSetMode(), PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinaryOpen(), 1106bc196f7cSDave May PetscViewerBinaryGetUseMPIIO() 11075c6c1daeSBarry Smith 11085c6c1daeSBarry Smith @*/ 1109bc196f7cSDave May PetscErrorCode PetscViewerBinarySetUseMPIIO(PetscViewer viewer,PetscBool flg) 11105c6c1daeSBarry Smith { 11115c6c1daeSBarry Smith PetscErrorCode ierr; 11125c6c1daeSBarry Smith 11135c6c1daeSBarry Smith PetscFunctionBegin; 11145c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 1115bc196f7cSDave May ierr = PetscTryMethod(viewer,"PetscViewerBinarySetUseMPIIO_C",(PetscViewer,PetscBool),(viewer,flg));CHKERRQ(ierr); 11165c6c1daeSBarry Smith PetscFunctionReturn(0); 11175c6c1daeSBarry Smith } 11185c6c1daeSBarry Smith 11195c6c1daeSBarry Smith #undef __FUNCT__ 11205c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileSetMode" 11215c6c1daeSBarry Smith /*@C 11225c6c1daeSBarry Smith PetscViewerFileSetMode - Sets the type of file to be open 11235c6c1daeSBarry Smith 11245c6c1daeSBarry Smith Logically Collective on PetscViewer 11255c6c1daeSBarry Smith 11265c6c1daeSBarry Smith Input Parameters: 11275c6c1daeSBarry Smith + viewer - the PetscViewer; must be a binary, Matlab, hdf, or netcdf PetscViewer 11285c6c1daeSBarry Smith - type - type of file 11295c6c1daeSBarry Smith $ FILE_MODE_WRITE - create new file for binary output 11305c6c1daeSBarry Smith $ FILE_MODE_READ - open existing file for binary input 11315c6c1daeSBarry Smith $ FILE_MODE_APPEND - open existing file for binary output 11325c6c1daeSBarry Smith 11335c6c1daeSBarry Smith Level: advanced 11345c6c1daeSBarry Smith 11355c6c1daeSBarry Smith .seealso: PetscViewerFileSetMode(), PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinaryOpen() 11365c6c1daeSBarry Smith 11375c6c1daeSBarry Smith @*/ 11385c6c1daeSBarry Smith PetscErrorCode PetscViewerFileSetMode(PetscViewer viewer,PetscFileMode type) 11395c6c1daeSBarry Smith { 11405c6c1daeSBarry Smith PetscErrorCode ierr; 11415c6c1daeSBarry Smith 11425c6c1daeSBarry Smith PetscFunctionBegin; 11435c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 11445c6c1daeSBarry Smith PetscValidLogicalCollectiveEnum(viewer,type,2); 11455c6c1daeSBarry Smith ierr = PetscTryMethod(viewer,"PetscViewerFileSetMode_C",(PetscViewer,PetscFileMode),(viewer,type));CHKERRQ(ierr); 11465c6c1daeSBarry Smith PetscFunctionReturn(0); 11475c6c1daeSBarry Smith } 11485c6c1daeSBarry Smith 11495c6c1daeSBarry Smith #undef __FUNCT__ 11505c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileGetMode_Binary" 11515c6c1daeSBarry Smith PetscErrorCode PetscViewerFileGetMode_Binary(PetscViewer viewer,PetscFileMode *type) 11525c6c1daeSBarry Smith { 11535c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 11545c6c1daeSBarry Smith 11555c6c1daeSBarry Smith PetscFunctionBegin; 11565c6c1daeSBarry Smith *type = vbinary->btype; 11575c6c1daeSBarry Smith PetscFunctionReturn(0); 11585c6c1daeSBarry Smith } 11595c6c1daeSBarry Smith 11605c6c1daeSBarry Smith #undef __FUNCT__ 11615c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerFileSetMode_Binary" 11625c6c1daeSBarry Smith PetscErrorCode PetscViewerFileSetMode_Binary(PetscViewer viewer,PetscFileMode type) 11635c6c1daeSBarry Smith { 11645c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 11655c6c1daeSBarry Smith 11665c6c1daeSBarry Smith PetscFunctionBegin; 11675c6c1daeSBarry Smith vbinary->btype = type; 11685c6c1daeSBarry Smith PetscFunctionReturn(0); 11695c6c1daeSBarry Smith } 11705c6c1daeSBarry Smith 1171e0385b85SDave May #undef __FUNCT__ 1172e0385b85SDave May #define __FUNCT__ "PetscViewerFileSetName_Binary" 1173e0385b85SDave May PetscErrorCode PetscViewerFileSetName_Binary(PetscViewer viewer,const char name[]) 1174e0385b85SDave May { 1175e0385b85SDave May PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 1176e0385b85SDave May PetscErrorCode ierr; 1177e0385b85SDave May 1178e0385b85SDave May PetscFunctionBegin; 1179c0e89164SDave May if (vbinary->filename) { ierr = PetscFree(vbinary->filename);CHKERRQ(ierr); } 1180e0385b85SDave May ierr = PetscStrallocpy(name,&vbinary->filename);CHKERRQ(ierr); 1181e0385b85SDave May PetscFunctionReturn(0); 1182e0385b85SDave May } 11835c6c1daeSBarry Smith /* 11845c6c1daeSBarry Smith Actually opens the file 11855c6c1daeSBarry Smith */ 11865c6c1daeSBarry Smith #undef __FUNCT__ 1187e0385b85SDave May #define __FUNCT__ "PetscViewerFileSetUp_Binary" 1188e0385b85SDave May static PetscErrorCode PetscViewerFileSetUp_Binary(PetscViewer viewer) 11895c6c1daeSBarry Smith { 11905c6c1daeSBarry Smith PetscMPIInt rank; 11915c6c1daeSBarry Smith PetscErrorCode ierr; 11925c6c1daeSBarry Smith size_t len; 11935c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 11945c6c1daeSBarry Smith const char *fname; 11952259a519SDave May char bname[PETSC_MAX_PATH_LEN],*gz; 11965c6c1daeSBarry Smith PetscBool found; 11975c6c1daeSBarry Smith PetscFileMode type = vbinary->btype; 11985c6c1daeSBarry Smith 11995c6c1daeSBarry Smith PetscFunctionBegin; 1200e0385b85SDave May if (type == (PetscFileMode) -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetMode()"); 12012259a519SDave May if (!vbinary->filename) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetName()"); 12025c6c1daeSBarry Smith ierr = PetscViewerFileClose_Binary(viewer);CHKERRQ(ierr); 12035c6c1daeSBarry Smith 1204ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr); 12055c6c1daeSBarry Smith 12065c6c1daeSBarry Smith /* if ends in .gz strip that off and note user wants file compressed */ 12075c6c1daeSBarry Smith vbinary->storecompressed = PETSC_FALSE; 12085c6c1daeSBarry Smith if (!rank && type == FILE_MODE_WRITE) { 12095c6c1daeSBarry Smith /* remove .gz if it ends library name */ 12105c6c1daeSBarry Smith ierr = PetscStrstr(vbinary->filename,".gz",&gz);CHKERRQ(ierr); 12115c6c1daeSBarry Smith if (gz) { 12125c6c1daeSBarry Smith ierr = PetscStrlen(gz,&len);CHKERRQ(ierr); 12135c6c1daeSBarry Smith if (len == 3) { 12145c6c1daeSBarry Smith *gz = 0; 12155c6c1daeSBarry Smith vbinary->storecompressed = PETSC_TRUE; 12165c6c1daeSBarry Smith } 12175c6c1daeSBarry Smith } 12185c6c1daeSBarry Smith } 12195c6c1daeSBarry Smith 12205c6c1daeSBarry Smith /* only first processor opens file if writeable */ 12215c6c1daeSBarry Smith if (!rank || type == FILE_MODE_READ) { 12225c6c1daeSBarry Smith 12235c6c1daeSBarry Smith if (type == FILE_MODE_READ) { 12245c6c1daeSBarry Smith /* possibly get the file from remote site or compressed file */ 1225ce94432eSBarry Smith ierr = PetscFileRetrieve(PetscObjectComm((PetscObject)viewer),vbinary->filename,bname,PETSC_MAX_PATH_LEN,&found);CHKERRQ(ierr); 12265c6c1daeSBarry Smith fname = bname; 12275c6c1daeSBarry Smith if (!rank && !found) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot locate file: %s on node zero",vbinary->filename); 12285c6c1daeSBarry Smith else if (!found) { 12295c6c1daeSBarry Smith ierr = PetscInfo(viewer,"Nonzero processor did not locate readonly file\n");CHKERRQ(ierr); 12305c6c1daeSBarry Smith fname = 0; 12315c6c1daeSBarry Smith } 1232a297a907SKarl Rupp } else fname = vbinary->filename; 12335c6c1daeSBarry Smith 12345c6c1daeSBarry Smith #if defined(PETSC_HAVE_O_BINARY) 12355c6c1daeSBarry Smith if (type == FILE_MODE_WRITE) { 12365c6c1daeSBarry 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); 12375c6c1daeSBarry Smith } else if (type == FILE_MODE_READ && fname) { 12385c6c1daeSBarry 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); 12395c6c1daeSBarry Smith } else if (type == FILE_MODE_APPEND) { 12405c6c1daeSBarry 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); 12415c6c1daeSBarry Smith } else if (fname) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown file type"); 12425c6c1daeSBarry Smith #else 12435c6c1daeSBarry Smith if (type == FILE_MODE_WRITE) { 12445c6c1daeSBarry Smith if ((vbinary->fdes = creat(fname,0666)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot create file %s for writing",fname); 12455c6c1daeSBarry Smith } else if (type == FILE_MODE_READ && fname) { 12465c6c1daeSBarry 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); 12475c6c1daeSBarry Smith } else if (type == FILE_MODE_APPEND) { 12485c6c1daeSBarry 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); 12495c6c1daeSBarry Smith } else if (fname) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown file type"); 12505c6c1daeSBarry Smith #endif 12515c6c1daeSBarry Smith } else vbinary->fdes = -1; 12525c6c1daeSBarry Smith 12535c6c1daeSBarry Smith /* 12545c6c1daeSBarry Smith try to open info file: all processors open this file if read only 12555c6c1daeSBarry Smith */ 12565c6c1daeSBarry Smith if (!vbinary->skipinfo && (!rank || type == FILE_MODE_READ)) { 12575c6c1daeSBarry Smith char infoname[PETSC_MAX_PATH_LEN],iname[PETSC_MAX_PATH_LEN]; 12585c6c1daeSBarry Smith 1259e0385b85SDave May ierr = PetscStrcpy(infoname,vbinary->filename);CHKERRQ(ierr); 12605c6c1daeSBarry Smith /* remove .gz if it ends library name */ 12615c6c1daeSBarry Smith ierr = PetscStrstr(infoname,".gz",&gz);CHKERRQ(ierr); 12625c6c1daeSBarry Smith if (gz) { 12635c6c1daeSBarry Smith ierr = PetscStrlen(gz,&len);CHKERRQ(ierr); 1264a297a907SKarl Rupp if (len == 3) *gz = 0; 12655c6c1daeSBarry Smith } 12665c6c1daeSBarry Smith 12675c6c1daeSBarry Smith ierr = PetscStrcat(infoname,".info");CHKERRQ(ierr); 12685c6c1daeSBarry Smith ierr = PetscFixFilename(infoname,iname);CHKERRQ(ierr); 12695c6c1daeSBarry Smith if (type == FILE_MODE_READ) { 1270ce94432eSBarry Smith ierr = PetscFileRetrieve(PetscObjectComm((PetscObject)viewer),iname,infoname,PETSC_MAX_PATH_LEN,&found);CHKERRQ(ierr); 1271ce94432eSBarry Smith ierr = PetscOptionsInsertFile(PetscObjectComm((PetscObject)viewer),infoname,PETSC_FALSE);CHKERRQ(ierr); 12725c6c1daeSBarry Smith } else { 12735c6c1daeSBarry Smith vbinary->fdes_info = fopen(infoname,"w"); 12745c6c1daeSBarry Smith if (!vbinary->fdes_info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open .info file %s for writing",infoname); 12755c6c1daeSBarry Smith } 12765c6c1daeSBarry Smith } 12775c6c1daeSBarry Smith #if defined(PETSC_USE_LOG) 1278e0385b85SDave May PetscLogObjectState((PetscObject)viewer,"File: %s",vbinary->filename); 12795c6c1daeSBarry Smith #endif 12805c6c1daeSBarry Smith PetscFunctionReturn(0); 12815c6c1daeSBarry Smith } 12825c6c1daeSBarry Smith 12835c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO) 12845c6c1daeSBarry Smith #undef __FUNCT__ 1285e0385b85SDave May #define __FUNCT__ "PetscViewerFileSetUp_BinaryMPIIO" 1286e0385b85SDave May static PetscErrorCode PetscViewerFileSetUp_BinaryMPIIO(PetscViewer viewer) 12875c6c1daeSBarry Smith { 12885c6c1daeSBarry Smith PetscMPIInt rank; 12895c6c1daeSBarry Smith PetscErrorCode ierr; 12905c6c1daeSBarry Smith size_t len; 12915c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 12922259a519SDave May char *gz; 12935c6c1daeSBarry Smith PetscBool found; 12945c6c1daeSBarry Smith PetscFileMode type = vbinary->btype; 12955c6c1daeSBarry Smith 12965c6c1daeSBarry Smith PetscFunctionBegin; 1297e0385b85SDave May if (type == (PetscFileMode) -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetMode()"); 12982259a519SDave May if (!vbinary->filename) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetName()"); 1299e0385b85SDave May ierr = PetscViewerFileClose_BinaryMPIIO(viewer);CHKERRQ(ierr); 13005c6c1daeSBarry Smith 1301ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr); 13025c6c1daeSBarry Smith 1303a297a907SKarl Rupp vbinary->storecompressed = PETSC_FALSE; 13045c6c1daeSBarry Smith 13055c6c1daeSBarry Smith /* only first processor opens file if writeable */ 13065c6c1daeSBarry Smith if (type == FILE_MODE_READ) { 1307ce94432eSBarry Smith MPI_File_open(PetscObjectComm((PetscObject)viewer),vbinary->filename,MPI_MODE_RDONLY,MPI_INFO_NULL,&vbinary->mfdes);CHKERRQ(ierr); 13085c6c1daeSBarry Smith } else if (type == FILE_MODE_WRITE) { 1309ce94432eSBarry Smith MPI_File_open(PetscObjectComm((PetscObject)viewer),vbinary->filename,MPI_MODE_WRONLY | MPI_MODE_CREATE,MPI_INFO_NULL,&vbinary->mfdes);CHKERRQ(ierr); 13105c6c1daeSBarry Smith } 13115c6c1daeSBarry Smith 13125c6c1daeSBarry Smith /* 13135c6c1daeSBarry Smith try to open info file: all processors open this file if read only 13145c6c1daeSBarry Smith 13155c6c1daeSBarry Smith Below is identical code to the code for Binary above, should be put in seperate routine 13165c6c1daeSBarry Smith */ 13175c6c1daeSBarry Smith if (!vbinary->skipinfo && (!rank || type == FILE_MODE_READ)) { 13185c6c1daeSBarry Smith char infoname[PETSC_MAX_PATH_LEN],iname[PETSC_MAX_PATH_LEN]; 13195c6c1daeSBarry Smith 1320e0385b85SDave May ierr = PetscStrcpy(infoname,vbinary->filename);CHKERRQ(ierr); 13215c6c1daeSBarry Smith /* remove .gz if it ends library name */ 13225c6c1daeSBarry Smith ierr = PetscStrstr(infoname,".gz",&gz);CHKERRQ(ierr); 13235c6c1daeSBarry Smith if (gz) { 13245c6c1daeSBarry Smith ierr = PetscStrlen(gz,&len);CHKERRQ(ierr); 1325a297a907SKarl Rupp if (len == 3) *gz = 0; 13265c6c1daeSBarry Smith } 13275c6c1daeSBarry Smith 13285c6c1daeSBarry Smith ierr = PetscStrcat(infoname,".info");CHKERRQ(ierr); 13295c6c1daeSBarry Smith ierr = PetscFixFilename(infoname,iname);CHKERRQ(ierr); 13305c6c1daeSBarry Smith if (type == FILE_MODE_READ) { 1331ce94432eSBarry Smith ierr = PetscFileRetrieve(PetscObjectComm((PetscObject)viewer),iname,infoname,PETSC_MAX_PATH_LEN,&found);CHKERRQ(ierr); 1332ce94432eSBarry Smith ierr = PetscOptionsInsertFile(PetscObjectComm((PetscObject)viewer),infoname,PETSC_FALSE);CHKERRQ(ierr); 13335c6c1daeSBarry Smith } else { 13345c6c1daeSBarry Smith vbinary->fdes_info = fopen(infoname,"w"); 13355c6c1daeSBarry Smith if (!vbinary->fdes_info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open .info file %s for writing",infoname); 13365c6c1daeSBarry Smith } 13375c6c1daeSBarry Smith } 13385c6c1daeSBarry Smith #if defined(PETSC_USE_LOG) 1339e0385b85SDave May PetscLogObjectState((PetscObject)viewer,"File: %s",vbinary->filename); 13405c6c1daeSBarry Smith #endif 13415c6c1daeSBarry Smith PetscFunctionReturn(0); 13425c6c1daeSBarry Smith } 13435c6c1daeSBarry Smith 13445c6c1daeSBarry Smith #undef __FUNCT__ 1345bc196f7cSDave May #define __FUNCT__ "PetscViewerBinarySetUseMPIIO_Binary" 1346bc196f7cSDave May PetscErrorCode PetscViewerBinarySetUseMPIIO_Binary(PetscViewer viewer,PetscBool flg) 13475c6c1daeSBarry Smith { 13485c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 13495c6c1daeSBarry Smith PetscFunctionBegin; 1350bc196f7cSDave May vbinary->usempiio = flg; 13515c6c1daeSBarry Smith PetscFunctionReturn(0); 13525c6c1daeSBarry Smith } 13535c6c1daeSBarry Smith #endif 13545c6c1daeSBarry Smith 13552bf49c77SBarry Smith #undef __FUNCT__ 13562bf49c77SBarry Smith #define __FUNCT__ "PetscViewerView_Binary" 13572bf49c77SBarry Smith PetscErrorCode PetscViewerView_Binary(PetscViewer v,PetscViewer viewer) 13582bf49c77SBarry Smith { 13592bf49c77SBarry Smith PetscErrorCode ierr; 13602bf49c77SBarry Smith PetscViewer_Binary *binary = (PetscViewer_Binary*)v->data; 13612bf49c77SBarry Smith 13622bf49c77SBarry Smith PetscFunctionBegin; 13632bf49c77SBarry Smith if (binary->filename) { 13642bf49c77SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Filename: %s\n",binary->filename);CHKERRQ(ierr); 13652bf49c77SBarry Smith } 13662bf49c77SBarry Smith PetscFunctionReturn(0); 13672bf49c77SBarry Smith } 13682bf49c77SBarry Smith 13695c6c1daeSBarry Smith #undef __FUNCT__ 137003a1d59fSDave May #define __FUNCT__ "PetscViewerSetUp_Binary" 137103a1d59fSDave May static PetscErrorCode PetscViewerSetUp_Binary(PetscViewer v) 137203a1d59fSDave May { 137303a1d59fSDave May PetscErrorCode ierr; 137403a1d59fSDave May PetscViewer_Binary *binary = (PetscViewer_Binary*)v->data; 1375e0385b85SDave May 137603a1d59fSDave May PetscFunctionBegin; 1377da07bb01SDave May if (!binary->setfromoptionscalled) { ierr = PetscViewerSetFromOptions(v);CHKERRQ(ierr); } 137803a1d59fSDave May 1379e0385b85SDave May #if defined(PETSC_HAVE_MPIIO) 1380bc196f7cSDave May if (binary->usempiio) { 1381e0385b85SDave May ierr = PetscViewerFileSetUp_BinaryMPIIO(v);CHKERRQ(ierr); 1382e0385b85SDave May } else { 1383e0385b85SDave May #endif 1384e0385b85SDave May ierr = PetscViewerFileSetUp_Binary(v);CHKERRQ(ierr); 1385e0385b85SDave May #if defined(PETSC_HAVE_MPIIO) 1386e0385b85SDave May } 1387e0385b85SDave May #endif 138803a1d59fSDave May PetscFunctionReturn(0); 138903a1d59fSDave May } 139003a1d59fSDave May 139103a1d59fSDave May #undef __FUNCT__ 139203a1d59fSDave May #define __FUNCT__ "PetscViewerSetFromOptions_Binary" 139303a1d59fSDave May static PetscErrorCode PetscViewerSetFromOptions_Binary(PetscOptions *PetscOptionsObject,PetscViewer v) 139403a1d59fSDave May { 139503a1d59fSDave May PetscErrorCode ierr; 139603a1d59fSDave May PetscViewer_Binary *binary = (PetscViewer_Binary*)v->data; 1397d22fd6bcSDave May char defaultname[PETSC_MAX_PATH_LEN]; 139803a1d59fSDave May PetscBool flg; 1399e0385b85SDave May 140003a1d59fSDave May PetscFunctionBegin; 140103a1d59fSDave May ierr = PetscOptionsHead(PetscOptionsObject,"Binary PetscViewer Options");CHKERRQ(ierr); 1402d22fd6bcSDave May ierr = PetscSNPrintf(defaultname,PETSC_MAX_PATH_LEN-1,"binaryoutput");CHKERRQ(ierr); 1403d22fd6bcSDave May ierr = PetscOptionsString("-viewer_binary_filename","Specify filename","PetscViewerFileSetName",defaultname,defaultname,PETSC_MAX_PATH_LEN-1,&flg);CHKERRQ(ierr); 1404d22fd6bcSDave May if (flg) { ierr = PetscViewerFileSetName_Binary(v,defaultname);CHKERRQ(ierr); } 1405bc196f7cSDave May ierr = PetscOptionsBool("-viewer_binary_skip_info","Skip writing/reading .info file","PetscViewerBinarySetSkipInfo",PETSC_FALSE,&binary->skipinfo,&flg);CHKERRQ(ierr); 140603a1d59fSDave May ierr = PetscOptionsBool("-viewer_binary_skip_options","Skip parsing vec load options","PetscViewerBinarySetSkipOptions",PETSC_TRUE,&binary->skipoptions,&flg);CHKERRQ(ierr); 140703a1d59fSDave May ierr = PetscOptionsBool("-viewer_binary_skip_header","Skip writing/reading header information","PetscViewerBinarySetSkipHeader",PETSC_FALSE,&binary->skipheader,&flg);CHKERRQ(ierr); 140803a1d59fSDave May #if defined(PETSC_HAVE_MPIIO) 1409bc196f7cSDave May ierr = PetscOptionsBool("-viewer_binary_mpiio","Use MPI-IO functionality to write/read binary file","PetscViewerBinarySetUseMPIIO",PETSC_FALSE,&binary->usempiio,&flg);CHKERRQ(ierr); 141003a1d59fSDave May #endif 141103a1d59fSDave May ierr = PetscOptionsTail();CHKERRQ(ierr); 1412bc196f7cSDave May binary->setfromoptionscalled = PETSC_TRUE; 141303a1d59fSDave May PetscFunctionReturn(0); 141403a1d59fSDave May } 141503a1d59fSDave May 141603a1d59fSDave May #undef __FUNCT__ 14175c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerCreate_Binary" 14188cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscViewerCreate_Binary(PetscViewer v) 14195c6c1daeSBarry Smith { 14205c6c1daeSBarry Smith PetscErrorCode ierr; 14215c6c1daeSBarry Smith PetscViewer_Binary *vbinary; 14225c6c1daeSBarry Smith 14235c6c1daeSBarry Smith PetscFunctionBegin; 1424b00a9115SJed Brown ierr = PetscNewLog(v,&vbinary);CHKERRQ(ierr); 14255c6c1daeSBarry Smith v->data = (void*)vbinary; 142603a1d59fSDave May v->ops->setfromoptions = PetscViewerSetFromOptions_Binary; 14275c6c1daeSBarry Smith v->ops->destroy = PetscViewerDestroy_Binary; 14282bf49c77SBarry Smith v->ops->view = PetscViewerView_Binary; 1429c98fd787SBarry Smith v->ops->setup = PetscViewerSetUp_Binary; 1430c98fd787SBarry Smith v->ops->flush = NULL; 14315c6c1daeSBarry Smith vbinary->fdes_info = 0; 14325c6c1daeSBarry Smith vbinary->fdes = 0; 14335c6c1daeSBarry Smith vbinary->skipinfo = PETSC_FALSE; 14345c6c1daeSBarry Smith vbinary->skipoptions = PETSC_TRUE; 14355c6c1daeSBarry Smith vbinary->skipheader = PETSC_FALSE; 1436da07bb01SDave May vbinary->setfromoptionscalled = PETSC_FALSE; 14375c6c1daeSBarry Smith v->ops->getsingleton = PetscViewerGetSingleton_Binary; 14385c6c1daeSBarry Smith v->ops->restoresingleton = PetscViewerRestoreSingleton_Binary; 14391d641e7bSMichael Lange v->ops->read = PetscViewerBinaryRead; 14405c6c1daeSBarry Smith vbinary->btype = (PetscFileMode) -1; 14415c6c1daeSBarry Smith vbinary->storecompressed = PETSC_FALSE; 14425c6c1daeSBarry Smith vbinary->filename = 0; 14435c6c1daeSBarry Smith vbinary->flowcontrol = 256; /* seems a good number for Cray XT-5 */ 14445c6c1daeSBarry Smith 1445bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetFlowControl_C",PetscViewerBinaryGetFlowControl_Binary);CHKERRQ(ierr); 1446bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetFlowControl_C",PetscViewerBinarySetFlowControl_Binary);CHKERRQ(ierr); 1447bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetSkipHeader_C",PetscViewerBinarySetSkipHeader_Binary);CHKERRQ(ierr); 1448bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetSkipHeader_C",PetscViewerBinaryGetSkipHeader_Binary);CHKERRQ(ierr); 1449807ea322SDave May ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetSkipOptions_C",PetscViewerBinaryGetSkipOptions_Binary);CHKERRQ(ierr); 1450807ea322SDave May ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetSkipOptions_C",PetscViewerBinarySetSkipOptions_Binary);CHKERRQ(ierr); 1451807ea322SDave May ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetSkipInfo_C",PetscViewerBinaryGetSkipInfo_Binary);CHKERRQ(ierr); 1452807ea322SDave May ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetSkipInfo_C",PetscViewerBinarySetSkipInfo_Binary);CHKERRQ(ierr); 1453bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetInfoPointer_C",PetscViewerBinaryGetInfoPointer_Binary);CHKERRQ(ierr); 1454bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetName_C",PetscViewerFileSetName_Binary);CHKERRQ(ierr); 1455bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetMode_C",PetscViewerFileSetMode_Binary);CHKERRQ(ierr); 1456bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileGetMode_C",PetscViewerFileGetMode_Binary);CHKERRQ(ierr); 1457bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileGetName_C",PetscViewerFileGetName_Binary);CHKERRQ(ierr); 14585c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO) 1459bc196f7cSDave May ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetUseMPIIO_C",PetscViewerBinaryGetUseMPIIO_Binary);CHKERRQ(ierr); 1460bc196f7cSDave May ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetUseMPIIO_C",PetscViewerBinarySetUseMPIIO_Binary);CHKERRQ(ierr); 14615c6c1daeSBarry Smith #endif 14625c6c1daeSBarry Smith PetscFunctionReturn(0); 14635c6c1daeSBarry Smith } 14645c6c1daeSBarry Smith 14655c6c1daeSBarry Smith /* ---------------------------------------------------------------------*/ 14665c6c1daeSBarry Smith /* 14675c6c1daeSBarry Smith The variable Petsc_Viewer_Binary_keyval is used to indicate an MPI attribute that 14685c6c1daeSBarry Smith is attached to a communicator, in this case the attribute is a PetscViewer. 14695c6c1daeSBarry Smith */ 14705c6c1daeSBarry Smith static int Petsc_Viewer_Binary_keyval = MPI_KEYVAL_INVALID; 14715c6c1daeSBarry Smith 14725c6c1daeSBarry Smith #undef __FUNCT__ 14735c6c1daeSBarry Smith #define __FUNCT__ "PETSC_VIEWER_BINARY_" 14745c6c1daeSBarry Smith /*@C 14755c6c1daeSBarry Smith PETSC_VIEWER_BINARY_ - Creates a binary PetscViewer shared by all processors 14765c6c1daeSBarry Smith in a communicator. 14775c6c1daeSBarry Smith 14785c6c1daeSBarry Smith Collective on MPI_Comm 14795c6c1daeSBarry Smith 14805c6c1daeSBarry Smith Input Parameter: 14815c6c1daeSBarry Smith . comm - the MPI communicator to share the binary PetscViewer 14825c6c1daeSBarry Smith 14835c6c1daeSBarry Smith Level: intermediate 14845c6c1daeSBarry Smith 14855c6c1daeSBarry Smith Options Database Keys: 14865c6c1daeSBarry Smith + -viewer_binary_filename <name> 14875c6c1daeSBarry Smith . -viewer_binary_skip_info 1488a2d7db39SDave May . -viewer_binary_skip_options 1489a2d7db39SDave May . -viewer_binary_skip_header 1490a2d7db39SDave May - -viewer_binary_mpiio 14915c6c1daeSBarry Smith 14925c6c1daeSBarry Smith Environmental variables: 14935c6c1daeSBarry Smith - PETSC_VIEWER_BINARY_FILENAME 14945c6c1daeSBarry Smith 14955c6c1daeSBarry Smith Notes: 14965c6c1daeSBarry Smith Unlike almost all other PETSc routines, PETSC_VIEWER_BINARY_ does not return 14975c6c1daeSBarry Smith an error code. The binary PetscViewer is usually used in the form 14985c6c1daeSBarry Smith $ XXXView(XXX object,PETSC_VIEWER_BINARY_(comm)); 14995c6c1daeSBarry Smith 15005c6c1daeSBarry Smith .seealso: PETSC_VIEWER_BINARY_WORLD, PETSC_VIEWER_BINARY_SELF, PetscViewerBinaryOpen(), PetscViewerCreate(), 15015c6c1daeSBarry Smith PetscViewerDestroy() 15025c6c1daeSBarry Smith @*/ 15035c6c1daeSBarry Smith PetscViewer PETSC_VIEWER_BINARY_(MPI_Comm comm) 15045c6c1daeSBarry Smith { 15055c6c1daeSBarry Smith PetscErrorCode ierr; 15065c6c1daeSBarry Smith PetscBool flg; 15075c6c1daeSBarry Smith PetscViewer viewer; 15085c6c1daeSBarry Smith char fname[PETSC_MAX_PATH_LEN]; 15095c6c1daeSBarry Smith MPI_Comm ncomm; 15105c6c1daeSBarry Smith 15115c6c1daeSBarry Smith PetscFunctionBegin; 1512efca3c55SSatish 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);} 15135c6c1daeSBarry Smith if (Petsc_Viewer_Binary_keyval == MPI_KEYVAL_INVALID) { 15145c6c1daeSBarry Smith ierr = MPI_Keyval_create(MPI_NULL_COPY_FN,MPI_NULL_DELETE_FN,&Petsc_Viewer_Binary_keyval,0); 1515efca3c55SSatish Balay if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);} 15165c6c1daeSBarry Smith } 15175c6c1daeSBarry Smith ierr = MPI_Attr_get(ncomm,Petsc_Viewer_Binary_keyval,(void**)&viewer,(int*)&flg); 1518efca3c55SSatish Balay if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);} 15195c6c1daeSBarry Smith if (!flg) { /* PetscViewer not yet created */ 15205c6c1daeSBarry Smith ierr = PetscOptionsGetenv(ncomm,"PETSC_VIEWER_BINARY_FILENAME",fname,PETSC_MAX_PATH_LEN,&flg); 1521efca3c55SSatish Balay if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);} 15225c6c1daeSBarry Smith if (!flg) { 15235c6c1daeSBarry Smith ierr = PetscStrcpy(fname,"binaryoutput"); 1524efca3c55SSatish Balay if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);} 15255c6c1daeSBarry Smith } 15265c6c1daeSBarry Smith ierr = PetscViewerBinaryOpen(ncomm,fname,FILE_MODE_WRITE,&viewer); 1527efca3c55SSatish Balay if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);} 15285c6c1daeSBarry Smith ierr = PetscObjectRegisterDestroy((PetscObject)viewer); 1529efca3c55SSatish Balay if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);} 15305c6c1daeSBarry Smith ierr = MPI_Attr_put(ncomm,Petsc_Viewer_Binary_keyval,(void*)viewer); 1531efca3c55SSatish Balay if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);} 15325c6c1daeSBarry Smith } 15335c6c1daeSBarry Smith ierr = PetscCommDestroy(&ncomm); 1534efca3c55SSatish Balay if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);} 15355c6c1daeSBarry Smith PetscFunctionReturn(viewer); 15365c6c1daeSBarry Smith } 1537