15c6c1daeSBarry Smith 2af0996ceSBarry Smith #include <petsc/private/viewerimpl.h> /*I "petscviewer.h" I*/ 35c6c1daeSBarry Smith #include <fcntl.h> 45c6c1daeSBarry Smith #if defined(PETSC_HAVE_UNISTD_H) 55c6c1daeSBarry Smith #include <unistd.h> 65c6c1daeSBarry Smith #endif 75c6c1daeSBarry Smith #if defined(PETSC_HAVE_IO_H) 85c6c1daeSBarry Smith #include <io.h> 95c6c1daeSBarry Smith #endif 105c6c1daeSBarry Smith 115c6c1daeSBarry Smith typedef struct { 125c6c1daeSBarry Smith int fdes; /* file descriptor, ignored if using MPI IO */ 135c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO) 14bc196f7cSDave May PetscBool usempiio; 155c6c1daeSBarry Smith MPI_File mfdes; /* ignored unless using MPI IO */ 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; 22f90597f1SStefano Zampini char *ogzfilename; /* gzip can be run after the filename has been updated */ 235c6c1daeSBarry Smith PetscBool skipinfo; /* Don't create info file for writing; don't use for reading */ 245c6c1daeSBarry Smith PetscBool skipoptions; /* don't use PETSc options database when loading */ 255c6c1daeSBarry Smith PetscInt flowcontrol; /* allow only <flowcontrol> messages outstanding at a time while doing IO */ 265c6c1daeSBarry Smith PetscBool skipheader; /* don't write header, only raw data */ 27a261c58fSBarry Smith PetscBool matlabheaderwritten; /* if format is PETSC_VIEWER_BINARY_MATLAB has the MATLAB .info header been written yet */ 28c98fd787SBarry Smith PetscBool setfromoptionscalled; 295c6c1daeSBarry Smith } PetscViewer_Binary; 305c6c1daeSBarry Smith 3181f0254dSBarry Smith static PetscErrorCode PetscViewerGetSubViewer_Binary(PetscViewer viewer,MPI_Comm comm,PetscViewer *outviewer) 325c6c1daeSBarry Smith { 335c6c1daeSBarry Smith int rank; 345c6c1daeSBarry Smith PetscErrorCode ierr; 355c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data,*obinary; 365c6c1daeSBarry Smith 375c6c1daeSBarry Smith PetscFunctionBegin; 38c98fd787SBarry Smith ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr); 3996509d17SLisandro Dalcin #if defined(PETSC_HAVE_MPIIO) 4096509d17SLisandro Dalcin if (vbinary->usempiio) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Not yet implemented for binary viewers using MPI I/O"); 4196509d17SLisandro Dalcin #endif 42ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr); 435c6c1daeSBarry Smith if (!rank) { 445c6c1daeSBarry Smith ierr = PetscViewerCreate(PETSC_COMM_SELF,outviewer);CHKERRQ(ierr); 455c6c1daeSBarry Smith ierr = PetscViewerSetType(*outviewer,PETSCVIEWERBINARY);CHKERRQ(ierr); 465c6c1daeSBarry Smith obinary = (PetscViewer_Binary*)(*outviewer)->data; 475c6c1daeSBarry Smith ierr = PetscMemcpy(obinary,vbinary,sizeof(PetscViewer_Binary));CHKERRQ(ierr); 4812f4c3a9SLisandro Dalcin (*outviewer)->setupcalled = PETSC_TRUE; 4996509d17SLisandro Dalcin } else { 5096509d17SLisandro Dalcin *outviewer = NULL; 5196509d17SLisandro Dalcin } 525c6c1daeSBarry Smith PetscFunctionReturn(0); 535c6c1daeSBarry Smith } 545c6c1daeSBarry Smith 5581f0254dSBarry Smith static PetscErrorCode PetscViewerRestoreSubViewer_Binary(PetscViewer viewer,MPI_Comm comm,PetscViewer *outviewer) 565c6c1daeSBarry Smith { 575c6c1daeSBarry Smith PetscErrorCode ierr; 585c6c1daeSBarry Smith PetscErrorCode rank; 595c6c1daeSBarry Smith 605c6c1daeSBarry Smith PetscFunctionBegin; 61ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr); 625c6c1daeSBarry Smith if (!rank) { 635c6c1daeSBarry Smith ierr = PetscFree((*outviewer)->data);CHKERRQ(ierr); 645c6c1daeSBarry Smith ierr = PetscHeaderDestroy(outviewer);CHKERRQ(ierr); 655c6c1daeSBarry Smith } 665c6c1daeSBarry Smith PetscFunctionReturn(0); 675c6c1daeSBarry Smith } 685c6c1daeSBarry Smith 695c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO) 705c6c1daeSBarry Smith /*@C 715c6c1daeSBarry Smith PetscViewerBinaryGetMPIIOOffset - Gets the current offset that should be passed to MPI_File_set_view() 725c6c1daeSBarry Smith 735c6c1daeSBarry Smith Not Collective 745c6c1daeSBarry Smith 755c6c1daeSBarry Smith Input Parameter: 765c6c1daeSBarry Smith . viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 775c6c1daeSBarry Smith 785c6c1daeSBarry Smith Output Parameter: 795c6c1daeSBarry Smith . off - the current offset 805c6c1daeSBarry Smith 815c6c1daeSBarry Smith Level: advanced 825c6c1daeSBarry Smith 835c6c1daeSBarry Smith Fortran Note: 845c6c1daeSBarry Smith This routine is not supported in Fortran. 855c6c1daeSBarry Smith 865c6c1daeSBarry Smith Use PetscViewerBinaryAddMPIIOOffset() to increase this value after you have written a view. 875c6c1daeSBarry Smith 885c6c1daeSBarry Smith Concepts: file descriptor^getting 895c6c1daeSBarry Smith Concepts: PetscViewerBinary^accessing file descriptor 905c6c1daeSBarry Smith 9167918a83SBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetInfoPointer(), PetscViewerBinaryGetUseMPIIO(), PetscViewerBinarySetUseMPIIO(), PetscViewerBinaryAddMPIIOOffset() 925c6c1daeSBarry Smith @*/ 935c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetMPIIOOffset(PetscViewer viewer,MPI_Offset *off) 945c6c1daeSBarry Smith { 955c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 965c6c1daeSBarry Smith 975c6c1daeSBarry Smith PetscFunctionBegin; 985c6c1daeSBarry Smith *off = vbinary->moff; 995c6c1daeSBarry Smith PetscFunctionReturn(0); 1005c6c1daeSBarry Smith } 1015c6c1daeSBarry Smith 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 12167918a83SBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer(), PetscViewerBinaryGetUseMPIIO(), PetscViewerBinarySetUseMPIIO(), PetscViewerBinaryGetMPIIOOffset() 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 /*@C 1335c6c1daeSBarry Smith PetscViewerBinaryGetMPIIODescriptor - Extracts the MPI IO file descriptor from a PetscViewer. 1345c6c1daeSBarry Smith 1355c6c1daeSBarry Smith Not Collective 1365c6c1daeSBarry Smith 1375c6c1daeSBarry Smith Input Parameter: 1385c6c1daeSBarry Smith . viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 1395c6c1daeSBarry Smith 1405c6c1daeSBarry Smith Output Parameter: 1415c6c1daeSBarry Smith . fdes - file descriptor 1425c6c1daeSBarry Smith 1435c6c1daeSBarry Smith Level: advanced 1445c6c1daeSBarry Smith 1455c6c1daeSBarry Smith Fortran Note: 1465c6c1daeSBarry Smith This routine is not supported in Fortran. 1475c6c1daeSBarry Smith 1485c6c1daeSBarry Smith Concepts: file descriptor^getting 1495c6c1daeSBarry Smith Concepts: PetscViewerBinary^accessing file descriptor 1505c6c1daeSBarry Smith 15167918a83SBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer(), PetscViewerBinaryGetUseMPIIO(), PetscViewerBinarySetUseMPIIO(), PetscViewerBinaryGetMPIIOOffset() 1525c6c1daeSBarry Smith @*/ 1535c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetMPIIODescriptor(PetscViewer viewer,MPI_File *fdes) 1545c6c1daeSBarry Smith { 15503a1d59fSDave May PetscErrorCode ierr; 1565c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 1575c6c1daeSBarry Smith 1585c6c1daeSBarry Smith PetscFunctionBegin; 159c98fd787SBarry Smith ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr); 1605c6c1daeSBarry Smith *fdes = vbinary->mfdes; 1615c6c1daeSBarry Smith PetscFunctionReturn(0); 1625c6c1daeSBarry Smith } 1635c6c1daeSBarry Smith 16481f0254dSBarry Smith static PetscErrorCode PetscViewerBinaryGetUseMPIIO_Binary(PetscViewer viewer,PetscBool *flg) 165a8aae444SDave May { 166a8aae444SDave May PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 167a8aae444SDave May 168a8aae444SDave May PetscFunctionBegin; 169bc196f7cSDave May *flg = vbinary->usempiio; 170a8aae444SDave May PetscFunctionReturn(0); 171a8aae444SDave May } 172a8aae444SDave May #endif 173a8aae444SDave May 174a8aae444SDave May 1755c6c1daeSBarry Smith /*@C 176bc196f7cSDave May PetscViewerBinaryGetUseMPIIO - Returns PETSC_TRUE if the binary viewer uses MPI-IO. 1775c6c1daeSBarry Smith 1785c6c1daeSBarry Smith Not Collective 1795c6c1daeSBarry Smith 1805c6c1daeSBarry Smith Input Parameter: 1815c6c1daeSBarry Smith . viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 1825c6c1daeSBarry Smith 1835c6c1daeSBarry Smith Output Parameter: 184bc196f7cSDave May - flg - PETSC_TRUE if MPI-IO is being used 1855c6c1daeSBarry Smith 1865c6c1daeSBarry Smith Options Database: 1875c6c1daeSBarry Smith -viewer_binary_mpiio : Flag for using MPI-IO 1885c6c1daeSBarry Smith 1895c6c1daeSBarry Smith Level: advanced 1905c6c1daeSBarry Smith 191bc196f7cSDave May Note: 192bc196f7cSDave May If MPI-IO is not available, this function will always return PETSC_FALSE 193bc196f7cSDave May 1945c6c1daeSBarry Smith Fortran Note: 1955c6c1daeSBarry Smith This routine is not supported in Fortran. 1965c6c1daeSBarry Smith 1975c6c1daeSBarry Smith Concepts: file descriptor^getting 1985c6c1daeSBarry Smith Concepts: PetscViewerBinary^accessing file descriptor 1995c6c1daeSBarry Smith 20067918a83SBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetInfoPointer(), PetscViewerBinarySetUseMPIIO(), PetscViewerBinaryGetMPIIOOffset() 2015c6c1daeSBarry Smith @*/ 202bc196f7cSDave May PetscErrorCode PetscViewerBinaryGetUseMPIIO(PetscViewer viewer,PetscBool *flg) 2035c6c1daeSBarry Smith { 204a8aae444SDave May PetscErrorCode ierr; 2055c6c1daeSBarry Smith 2065c6c1daeSBarry Smith PetscFunctionBegin; 207a8aae444SDave May *flg = PETSC_FALSE; 208bc196f7cSDave May ierr = PetscTryMethod(viewer,"PetscViewerBinaryGetUseMPIIO_C",(PetscViewer,PetscBool*),(viewer,flg));CHKERRQ(ierr); 2095c6c1daeSBarry Smith PetscFunctionReturn(0); 2105c6c1daeSBarry Smith } 2115c6c1daeSBarry Smith 21281f0254dSBarry Smith static PetscErrorCode PetscViewerBinaryGetFlowControl_Binary(PetscViewer viewer,PetscInt *fc) 2135c6c1daeSBarry Smith { 2145c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 2155c6c1daeSBarry Smith 2165c6c1daeSBarry Smith PetscFunctionBegin; 2175c6c1daeSBarry Smith *fc = vbinary->flowcontrol; 2185c6c1daeSBarry Smith PetscFunctionReturn(0); 2195c6c1daeSBarry Smith } 2205c6c1daeSBarry Smith 2215c6c1daeSBarry Smith /*@C 2225c6c1daeSBarry Smith PetscViewerBinaryGetFlowControl - Returns how many messages are allowed to outstanding at the same time during parallel IO reads/writes 2235c6c1daeSBarry Smith 2245c6c1daeSBarry Smith Not Collective 2255c6c1daeSBarry Smith 2265c6c1daeSBarry Smith Input Parameter: 2275c6c1daeSBarry Smith . viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 2285c6c1daeSBarry Smith 2295c6c1daeSBarry Smith Output Parameter: 2305c6c1daeSBarry Smith . fc - the number of messages 2315c6c1daeSBarry Smith 2325c6c1daeSBarry Smith Level: advanced 2335c6c1daeSBarry Smith 2345c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer(), PetscViewerBinarySetFlowControl() 2355c6c1daeSBarry Smith 2365c6c1daeSBarry Smith @*/ 2375c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetFlowControl(PetscViewer viewer,PetscInt *fc) 2385c6c1daeSBarry Smith { 2395c6c1daeSBarry Smith PetscErrorCode ierr; 2405c6c1daeSBarry Smith 2415c6c1daeSBarry Smith PetscFunctionBegin; 242163d334eSBarry Smith ierr = PetscUseMethod(viewer,"PetscViewerBinaryGetFlowControl_C",(PetscViewer,PetscInt*),(viewer,fc));CHKERRQ(ierr); 2435c6c1daeSBarry Smith PetscFunctionReturn(0); 2445c6c1daeSBarry Smith } 2455c6c1daeSBarry Smith 24681f0254dSBarry Smith static PetscErrorCode PetscViewerBinarySetFlowControl_Binary(PetscViewer viewer,PetscInt fc) 2475c6c1daeSBarry Smith { 2485c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 2495c6c1daeSBarry Smith 2505c6c1daeSBarry Smith PetscFunctionBegin; 251ce94432eSBarry Smith if (fc <= 1) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_OUTOFRANGE,"Flow control count must be greater than 1, %D was set",fc); 2525c6c1daeSBarry Smith vbinary->flowcontrol = fc; 2535c6c1daeSBarry Smith PetscFunctionReturn(0); 2545c6c1daeSBarry Smith } 2555c6c1daeSBarry Smith 2565c6c1daeSBarry Smith /*@C 257639ff905SBarry Smith PetscViewerBinarySetFlowControl - Sets how many messages are allowed to outstanding at the same time during parallel IO reads/writes 2585c6c1daeSBarry Smith 2595c6c1daeSBarry Smith Not Collective 2605c6c1daeSBarry Smith 2615c6c1daeSBarry Smith Input Parameter: 2625c6c1daeSBarry Smith + viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 263639ff905SBarry Smith - fc - the number of messages, defaults to 256 if this function was not called 2645c6c1daeSBarry Smith 2655c6c1daeSBarry Smith Level: advanced 2665c6c1daeSBarry Smith 2675c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer(), PetscViewerBinaryGetFlowControl() 2685c6c1daeSBarry Smith 2695c6c1daeSBarry Smith @*/ 2705c6c1daeSBarry Smith PetscErrorCode PetscViewerBinarySetFlowControl(PetscViewer viewer,PetscInt fc) 2715c6c1daeSBarry Smith { 2725c6c1daeSBarry Smith PetscErrorCode ierr; 2735c6c1daeSBarry Smith 2745c6c1daeSBarry Smith PetscFunctionBegin; 2755c6c1daeSBarry Smith ierr = PetscUseMethod(viewer,"PetscViewerBinarySetFlowControl_C",(PetscViewer,PetscInt),(viewer,fc));CHKERRQ(ierr); 2765c6c1daeSBarry Smith PetscFunctionReturn(0); 2775c6c1daeSBarry Smith } 2785c6c1daeSBarry Smith 2795c6c1daeSBarry Smith /*@C 2805c6c1daeSBarry Smith PetscViewerBinaryGetDescriptor - Extracts the file descriptor from a PetscViewer. 2815c6c1daeSBarry Smith 2825872f025SBarry Smith Collective On PetscViewer 2835c6c1daeSBarry Smith 2845c6c1daeSBarry Smith Input Parameter: 2855c6c1daeSBarry Smith . viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 2865c6c1daeSBarry Smith 2875c6c1daeSBarry Smith Output Parameter: 2885c6c1daeSBarry Smith . fdes - file descriptor 2895c6c1daeSBarry Smith 2905c6c1daeSBarry Smith Level: advanced 2915c6c1daeSBarry Smith 2925c6c1daeSBarry Smith Notes: 2935c6c1daeSBarry Smith For writable binary PetscViewers, the descriptor will only be valid for the 2945c6c1daeSBarry Smith first processor in the communicator that shares the PetscViewer. For readable 2955c6c1daeSBarry Smith files it will only be valid on nodes that have the file. If node 0 does not 2965c6c1daeSBarry Smith have the file it generates an error even if another node does have the file. 2975c6c1daeSBarry Smith 2985c6c1daeSBarry Smith Fortran Note: 2995c6c1daeSBarry Smith This routine is not supported in Fortran. 3005c6c1daeSBarry Smith 3015872f025SBarry Smith Developer Notes: This must be called on all processes because Dave May changed 3025872f025SBarry Smith the source code that this may be trigger a PetscViewerSetUp() call if it was not previously triggered. 3035872f025SBarry Smith 3045872f025SBarry Smith 3055c6c1daeSBarry Smith Concepts: file descriptor^getting 3065c6c1daeSBarry Smith Concepts: PetscViewerBinary^accessing file descriptor 3075c6c1daeSBarry Smith 3085c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer() 3095c6c1daeSBarry Smith @*/ 3105c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetDescriptor(PetscViewer viewer,int *fdes) 3115c6c1daeSBarry Smith { 31203a1d59fSDave May PetscErrorCode ierr; 3135c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 3145c6c1daeSBarry Smith 3155c6c1daeSBarry Smith PetscFunctionBegin; 316c98fd787SBarry Smith ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr); 3175c6c1daeSBarry Smith *fdes = vbinary->fdes; 3185c6c1daeSBarry Smith PetscFunctionReturn(0); 3195c6c1daeSBarry Smith } 3205c6c1daeSBarry Smith 3215c6c1daeSBarry Smith /*@ 3225c6c1daeSBarry Smith PetscViewerBinarySkipInfo - Binary file will not have .info file created with it 3235c6c1daeSBarry Smith 3245c6c1daeSBarry Smith Not Collective 3255c6c1daeSBarry Smith 3265c6c1daeSBarry Smith Input Paramter: 3275c6c1daeSBarry Smith . viewer - PetscViewer context, obtained from PetscViewerCreate() 3285c6c1daeSBarry Smith 3295c6c1daeSBarry Smith Options Database Key: 3305c6c1daeSBarry Smith . -viewer_binary_skip_info 3315c6c1daeSBarry Smith 3325c6c1daeSBarry Smith Level: advanced 3335c6c1daeSBarry Smith 334a2d7db39SDave May Notes: This must be called after PetscViewerSetType(). If you use PetscViewerBinaryOpen() then 3355c6c1daeSBarry Smith you can only skip the info file with the -viewer_binary_skip_info flag. To use the function you must open the 336a2d7db39SDave May viewer with PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinarySkipInfo(). 3375c6c1daeSBarry Smith 3385c6c1daeSBarry Smith The .info contains meta information about the data in the binary file, for example the block size if it was 3395c6c1daeSBarry Smith set for a vector or matrix. 3405c6c1daeSBarry Smith 3415c6c1daeSBarry Smith Concepts: PetscViewerBinary^accessing info file 3425c6c1daeSBarry Smith 3435c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySetSkipOptions(), 344807ea322SDave May PetscViewerBinaryGetSkipOptions(), PetscViewerBinaryGetSkipInfo() 3455c6c1daeSBarry Smith @*/ 3465c6c1daeSBarry Smith PetscErrorCode PetscViewerBinarySkipInfo(PetscViewer viewer) 3475c6c1daeSBarry Smith { 3485c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 3495c6c1daeSBarry Smith 3505c6c1daeSBarry Smith PetscFunctionBegin; 3515c6c1daeSBarry Smith vbinary->skipinfo = PETSC_TRUE; 3525c6c1daeSBarry Smith PetscFunctionReturn(0); 3535c6c1daeSBarry Smith } 3545c6c1daeSBarry Smith 35581f0254dSBarry Smith static PetscErrorCode PetscViewerBinarySetSkipInfo_Binary(PetscViewer viewer,PetscBool skip) 356807ea322SDave May { 357807ea322SDave May PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 358807ea322SDave May 359807ea322SDave May PetscFunctionBegin; 360807ea322SDave May vbinary->skipinfo = skip; 361807ea322SDave May PetscFunctionReturn(0); 362807ea322SDave May } 363807ea322SDave May 364807ea322SDave May /*@ 365807ea322SDave May PetscViewerBinarySetSkipInfo - Binary file will not have .info file created with it 366807ea322SDave May 367807ea322SDave May Not Collective 368807ea322SDave May 369807ea322SDave May Input Paramter: 370807ea322SDave May . viewer - PetscViewer context, obtained from PetscViewerCreate() 371807ea322SDave May 372807ea322SDave May Options Database Key: 373807ea322SDave May . -viewer_binary_skip_info 374807ea322SDave May 375807ea322SDave May Level: advanced 376807ea322SDave May 377807ea322SDave May Concepts: PetscViewerBinary^accessing info file 378807ea322SDave May 379807ea322SDave May .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySetSkipOptions(), 380807ea322SDave May PetscViewerBinaryGetSkipOptions(), PetscViewerBinaryGetSkipInfo() 381807ea322SDave May @*/ 382807ea322SDave May PetscErrorCode PetscViewerBinarySetSkipInfo(PetscViewer viewer,PetscBool skip) 383807ea322SDave May { 384807ea322SDave May PetscErrorCode ierr; 385807ea322SDave May 386807ea322SDave May PetscFunctionBegin; 387807ea322SDave May ierr = PetscUseMethod(viewer,"PetscViewerBinarySetSkipInfo_C",(PetscViewer,PetscBool),(viewer,skip));CHKERRQ(ierr); 388807ea322SDave May PetscFunctionReturn(0); 389807ea322SDave May } 390807ea322SDave May 39181f0254dSBarry Smith static PetscErrorCode PetscViewerBinaryGetSkipInfo_Binary(PetscViewer viewer,PetscBool *skip) 392807ea322SDave May { 393807ea322SDave May PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 394807ea322SDave May 395807ea322SDave May PetscFunctionBegin; 396807ea322SDave May *skip = vbinary->skipinfo; 397807ea322SDave May PetscFunctionReturn(0); 398807ea322SDave May } 399807ea322SDave May 400807ea322SDave May /*@ 401807ea322SDave May PetscViewerBinaryGetSkipInfo - check if viewer wrote a .info file 402807ea322SDave May 403807ea322SDave May Not Collective 404807ea322SDave May 405807ea322SDave May Input Parameter: 406807ea322SDave May . viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 407807ea322SDave May 408807ea322SDave May Output Parameter: 409807ea322SDave May . skip - PETSC_TRUE implies the .info file was not generated 410807ea322SDave May 411807ea322SDave May Level: advanced 412807ea322SDave May 413807ea322SDave May Notes: This must be called after PetscViewerSetType() 414807ea322SDave May 415807ea322SDave May Concepts: PetscViewerBinary^accessing info file 416807ea322SDave May 417807ea322SDave May .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(), 418807ea322SDave May PetscViewerBinarySetSkipOptions(), PetscViewerBinarySetSkipInfo() 419807ea322SDave May @*/ 420807ea322SDave May PetscErrorCode PetscViewerBinaryGetSkipInfo(PetscViewer viewer,PetscBool *skip) 421807ea322SDave May { 422807ea322SDave May PetscErrorCode ierr; 423807ea322SDave May 424807ea322SDave May PetscFunctionBegin; 425807ea322SDave May ierr = PetscUseMethod(viewer,"PetscViewerBinaryGetSkipInfo_C",(PetscViewer,PetscBool*),(viewer,skip));CHKERRQ(ierr); 426807ea322SDave May PetscFunctionReturn(0); 427807ea322SDave May } 428807ea322SDave May 42981f0254dSBarry Smith static PetscErrorCode PetscViewerBinarySetSkipOptions_Binary(PetscViewer viewer,PetscBool skip) 430807ea322SDave May { 431807ea322SDave May PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 432807ea322SDave May 433807ea322SDave May PetscFunctionBegin; 434807ea322SDave May vbinary->skipoptions = skip; 435807ea322SDave May PetscFunctionReturn(0); 436807ea322SDave May } 437807ea322SDave May 4385c6c1daeSBarry Smith /*@ 4395c6c1daeSBarry Smith PetscViewerBinarySetSkipOptions - do not use the PETSc options database when loading objects 4405c6c1daeSBarry Smith 4415c6c1daeSBarry Smith Not Collective 4425c6c1daeSBarry Smith 4435c6c1daeSBarry Smith Input Parameters: 4445c6c1daeSBarry Smith + viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 4455c6c1daeSBarry Smith - skip - PETSC_TRUE means do not use 4465c6c1daeSBarry Smith 4475c6c1daeSBarry Smith Options Database Key: 4485c6c1daeSBarry Smith . -viewer_binary_skip_options 4495c6c1daeSBarry Smith 4505c6c1daeSBarry Smith Level: advanced 4515c6c1daeSBarry Smith 4525c6c1daeSBarry Smith Notes: This must be called after PetscViewerSetType() 4535c6c1daeSBarry Smith 4545c6c1daeSBarry Smith Concepts: PetscViewerBinary^accessing info file 4555c6c1daeSBarry Smith 4565c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(), 4575c6c1daeSBarry Smith PetscViewerBinaryGetSkipOptions() 4585c6c1daeSBarry Smith @*/ 4595c6c1daeSBarry Smith PetscErrorCode PetscViewerBinarySetSkipOptions(PetscViewer viewer,PetscBool skip) 4605c6c1daeSBarry Smith { 461807ea322SDave May PetscErrorCode ierr; 4625c6c1daeSBarry Smith 4635c6c1daeSBarry Smith PetscFunctionBegin; 464807ea322SDave May ierr = PetscUseMethod(viewer,"PetscViewerBinarySetSkipOptions_C",(PetscViewer,PetscBool),(viewer,skip));CHKERRQ(ierr); 465807ea322SDave May PetscFunctionReturn(0); 466807ea322SDave May } 467807ea322SDave May 46881f0254dSBarry Smith static PetscErrorCode PetscViewerBinaryGetSkipOptions_Binary(PetscViewer viewer,PetscBool *skip) 469807ea322SDave May { 470807ea322SDave May PetscViewer_Binary *vbinary; 471807ea322SDave May 472807ea322SDave May PetscFunctionBegin; 473807ea322SDave May PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 474807ea322SDave May vbinary = (PetscViewer_Binary*)viewer->data; 475807ea322SDave May *skip = vbinary->skipoptions; 4765c6c1daeSBarry Smith PetscFunctionReturn(0); 4775c6c1daeSBarry Smith } 4785c6c1daeSBarry Smith 4795c6c1daeSBarry Smith /*@ 4805c6c1daeSBarry Smith PetscViewerBinaryGetSkipOptions - checks if viewer uses the PETSc options database when loading objects 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 use 4895c6c1daeSBarry Smith 4905c6c1daeSBarry Smith Level: advanced 4915c6c1daeSBarry Smith 4925c6c1daeSBarry Smith Notes: This must be called after PetscViewerSetType() 4935c6c1daeSBarry Smith 4945c6c1daeSBarry Smith Concepts: PetscViewerBinary^accessing info file 4955c6c1daeSBarry Smith 4965c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(), 4975c6c1daeSBarry Smith PetscViewerBinarySetSkipOptions() 4985c6c1daeSBarry Smith @*/ 4995c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetSkipOptions(PetscViewer viewer,PetscBool *skip) 5005c6c1daeSBarry Smith { 501807ea322SDave May PetscErrorCode ierr; 5025c6c1daeSBarry Smith 5035c6c1daeSBarry Smith PetscFunctionBegin; 504807ea322SDave May ierr = PetscUseMethod(viewer,"PetscViewerBinaryGetSkipOptions_C",(PetscViewer,PetscBool*),(viewer,skip));CHKERRQ(ierr); 5055c6c1daeSBarry Smith PetscFunctionReturn(0); 5065c6c1daeSBarry Smith } 5075c6c1daeSBarry Smith 50881f0254dSBarry Smith static PetscErrorCode PetscViewerBinarySetSkipHeader_Binary(PetscViewer viewer,PetscBool skip) 5095c6c1daeSBarry Smith { 5105c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 5115c6c1daeSBarry Smith 5125c6c1daeSBarry Smith PetscFunctionBegin; 5135c6c1daeSBarry Smith vbinary->skipheader = skip; 5145c6c1daeSBarry Smith PetscFunctionReturn(0); 5155c6c1daeSBarry Smith } 5165c6c1daeSBarry Smith 5175c6c1daeSBarry Smith /*@ 5185c6c1daeSBarry Smith PetscViewerBinarySetSkipHeader - do not write a header with size information on output, just raw data 5195c6c1daeSBarry Smith 5205c6c1daeSBarry Smith Not Collective 5215c6c1daeSBarry Smith 5225c6c1daeSBarry Smith Input Parameters: 5235c6c1daeSBarry Smith + viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 5245c6c1daeSBarry Smith - skip - PETSC_TRUE means do not write header 5255c6c1daeSBarry Smith 5265c6c1daeSBarry Smith Options Database Key: 5275c6c1daeSBarry Smith . -viewer_binary_skip_header 5285c6c1daeSBarry Smith 5295c6c1daeSBarry Smith Level: advanced 5305c6c1daeSBarry Smith 5315c6c1daeSBarry Smith Notes: This must be called after PetscViewerSetType() 5325c6c1daeSBarry Smith 5335c6c1daeSBarry Smith Can ONLY be called on a binary viewer 5345c6c1daeSBarry Smith 5355c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(), 5365c6c1daeSBarry Smith PetscViewerBinaryGetSkipHeader() 5375c6c1daeSBarry Smith @*/ 5385c6c1daeSBarry Smith PetscErrorCode PetscViewerBinarySetSkipHeader(PetscViewer viewer,PetscBool skip) 5395c6c1daeSBarry Smith { 5405c6c1daeSBarry Smith PetscErrorCode ierr; 5415c6c1daeSBarry Smith 5425c6c1daeSBarry Smith PetscFunctionBegin; 5435c6c1daeSBarry Smith ierr = PetscUseMethod(viewer,"PetscViewerBinarySetSkipHeader_C",(PetscViewer,PetscBool),(viewer,skip));CHKERRQ(ierr); 5445c6c1daeSBarry Smith PetscFunctionReturn(0); 5455c6c1daeSBarry Smith } 5465c6c1daeSBarry Smith 54781f0254dSBarry Smith static PetscErrorCode PetscViewerBinaryGetSkipHeader_Binary(PetscViewer viewer,PetscBool *skip) 5485c6c1daeSBarry Smith { 5495c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 5505c6c1daeSBarry Smith 5515c6c1daeSBarry Smith PetscFunctionBegin; 5525c6c1daeSBarry Smith *skip = vbinary->skipheader; 5535c6c1daeSBarry Smith PetscFunctionReturn(0); 5545c6c1daeSBarry Smith } 5555c6c1daeSBarry Smith 5565c6c1daeSBarry Smith /*@ 5575c6c1daeSBarry Smith PetscViewerBinaryGetSkipHeader - checks whether to write a header with size information on output, or just raw data 5585c6c1daeSBarry Smith 5595c6c1daeSBarry Smith Not Collective 5605c6c1daeSBarry Smith 5615c6c1daeSBarry Smith Input Parameter: 5625c6c1daeSBarry Smith . viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 5635c6c1daeSBarry Smith 5645c6c1daeSBarry Smith Output Parameter: 5655c6c1daeSBarry Smith . skip - PETSC_TRUE means do not write header 5665c6c1daeSBarry Smith 5675c6c1daeSBarry Smith Level: advanced 5685c6c1daeSBarry Smith 5695c6c1daeSBarry Smith Notes: This must be called after PetscViewerSetType() 5705c6c1daeSBarry Smith 5715c6c1daeSBarry Smith Returns false for PETSCSOCKETVIEWER, you cannot skip the header for it. 5725c6c1daeSBarry Smith 5735c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(), 5745c6c1daeSBarry Smith PetscViewerBinarySetSkipHeader() 5755c6c1daeSBarry Smith @*/ 5765c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetSkipHeader(PetscViewer viewer,PetscBool *skip) 5775c6c1daeSBarry Smith { 5785c6c1daeSBarry Smith PetscErrorCode ierr; 5795c6c1daeSBarry Smith 5805c6c1daeSBarry Smith PetscFunctionBegin; 5815c6c1daeSBarry Smith *skip = PETSC_FALSE; 582163d334eSBarry Smith ierr = PetscUseMethod(viewer,"PetscViewerBinaryGetSkipHeader_C",(PetscViewer,PetscBool*),(viewer,skip));CHKERRQ(ierr); 5835c6c1daeSBarry Smith PetscFunctionReturn(0); 5845c6c1daeSBarry Smith } 5855c6c1daeSBarry Smith 58681f0254dSBarry Smith static PetscErrorCode PetscViewerBinaryGetInfoPointer_Binary(PetscViewer viewer,FILE **file) 5875c6c1daeSBarry Smith { 588f3b211e4SSatish Balay PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 589a261c58fSBarry Smith PetscErrorCode ierr; 590a261c58fSBarry Smith MPI_Comm comm; 5915c6c1daeSBarry Smith 5925c6c1daeSBarry Smith PetscFunctionBegin; 593c98fd787SBarry Smith ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr); 5945c6c1daeSBarry Smith *file = vbinary->fdes_info; 595a261c58fSBarry Smith if (viewer->format == PETSC_VIEWER_BINARY_MATLAB && !vbinary->matlabheaderwritten) { 596a261c58fSBarry Smith vbinary->matlabheaderwritten = PETSC_TRUE; 597a261c58fSBarry Smith ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr); 598da88d4d4SJed Brown ierr = PetscFPrintf(comm,*file,"#--- begin code written by PetscViewerBinary for MATLAB format ---#\n");CHKERRQ(ierr); 599da88d4d4SJed Brown ierr = PetscFPrintf(comm,*file,"#$$ Set.filename = '%s';\n",vbinary->filename);CHKERRQ(ierr); 600da88d4d4SJed Brown ierr = PetscFPrintf(comm,*file,"#$$ fd = PetscOpenFile(Set.filename);\n");CHKERRQ(ierr); 601da88d4d4SJed Brown ierr = PetscFPrintf(comm,*file,"#--- end code written by PetscViewerBinary for MATLAB format ---#\n\n");CHKERRQ(ierr); 602a261c58fSBarry Smith } 6035c6c1daeSBarry Smith PetscFunctionReturn(0); 6045c6c1daeSBarry Smith } 6055c6c1daeSBarry Smith 6065c6c1daeSBarry Smith /*@C 6075c6c1daeSBarry Smith PetscViewerBinaryGetInfoPointer - Extracts the file pointer for the ASCII 6085c6c1daeSBarry Smith info file associated with a binary file. 6095c6c1daeSBarry Smith 6105c6c1daeSBarry Smith Not Collective 6115c6c1daeSBarry Smith 6125c6c1daeSBarry Smith Input Parameter: 6135c6c1daeSBarry Smith . viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 6145c6c1daeSBarry Smith 6155c6c1daeSBarry Smith Output Parameter: 6160298fd71SBarry Smith . file - file pointer Always returns NULL if not a binary viewer 6175c6c1daeSBarry Smith 6185c6c1daeSBarry Smith Level: advanced 6195c6c1daeSBarry Smith 6205c6c1daeSBarry Smith Notes: 6215c6c1daeSBarry Smith For writable binary PetscViewers, the descriptor will only be valid for the 6225c6c1daeSBarry Smith first processor in the communicator that shares the PetscViewer. 6235c6c1daeSBarry Smith 6245c6c1daeSBarry Smith Fortran Note: 6255c6c1daeSBarry Smith This routine is not supported in Fortran. 6265c6c1daeSBarry Smith 6275c6c1daeSBarry Smith Concepts: PetscViewerBinary^accessing info file 6285c6c1daeSBarry Smith 6295c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetDescriptor() 6305c6c1daeSBarry Smith @*/ 6315c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetInfoPointer(PetscViewer viewer,FILE **file) 6325c6c1daeSBarry Smith { 6335c6c1daeSBarry Smith PetscErrorCode ierr; 6345c6c1daeSBarry Smith 6355c6c1daeSBarry Smith PetscFunctionBegin; 6360298fd71SBarry Smith *file = NULL; 6375c6c1daeSBarry Smith ierr = PetscTryMethod(viewer,"PetscViewerBinaryGetInfoPointer_C",(PetscViewer,FILE **),(viewer,file));CHKERRQ(ierr); 6385c6c1daeSBarry Smith PetscFunctionReturn(0); 6395c6c1daeSBarry Smith } 6405c6c1daeSBarry Smith 6415c6c1daeSBarry Smith static PetscErrorCode PetscViewerFileClose_Binary(PetscViewer v) 6425c6c1daeSBarry Smith { 6435c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data; 6445c6c1daeSBarry Smith PetscErrorCode ierr; 6455c6c1daeSBarry Smith PetscMPIInt rank; 6465c6c1daeSBarry Smith int err; 6475c6c1daeSBarry Smith 6485c6c1daeSBarry Smith PetscFunctionBegin; 649ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)v),&rank);CHKERRQ(ierr); 6505c6c1daeSBarry Smith if ((!rank || vbinary->btype == FILE_MODE_READ) && vbinary->fdes) { 6515c6c1daeSBarry Smith close(vbinary->fdes); 6525c6c1daeSBarry Smith if (!rank && vbinary->storecompressed) { 6535c6c1daeSBarry Smith char par[PETSC_MAX_PATH_LEN],buf[PETSC_MAX_PATH_LEN]; 6545c6c1daeSBarry Smith FILE *fp; 6555c6c1daeSBarry Smith /* compress the file */ 656a126751eSBarry Smith ierr = PetscStrncpy(par,"gzip -f ",sizeof(par));CHKERRQ(ierr); 657a126751eSBarry Smith ierr = PetscStrlcat(par,vbinary->ogzfilename ? vbinary->ogzfilename : vbinary->filename,sizeof(par));CHKERRQ(ierr); 658f90597f1SStefano Zampini ierr = PetscFree(vbinary->ogzfilename);CHKERRQ(ierr); 6595c6c1daeSBarry Smith #if defined(PETSC_HAVE_POPEN) 6600298fd71SBarry Smith ierr = PetscPOpen(PETSC_COMM_SELF,NULL,par,"r",&fp);CHKERRQ(ierr); 6615c6c1daeSBarry Smith if (fgets(buf,1024,fp)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error from command %s\n%s",par,buf); 662016831caSBarry Smith ierr = PetscPClose(PETSC_COMM_SELF,fp);CHKERRQ(ierr); 6635c6c1daeSBarry Smith #else 6645c6c1daeSBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine"); 6655c6c1daeSBarry Smith #endif 6665c6c1daeSBarry Smith } 6675c6c1daeSBarry Smith } 6685c6c1daeSBarry Smith if (vbinary->fdes_info) { 6695c6c1daeSBarry Smith err = fclose(vbinary->fdes_info); 6705c6c1daeSBarry Smith if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file"); 6715c6c1daeSBarry Smith } 6725c6c1daeSBarry Smith PetscFunctionReturn(0); 6735c6c1daeSBarry Smith } 6745c6c1daeSBarry Smith 675e0385b85SDave May #if defined(PETSC_HAVE_MPIIO) 676e0385b85SDave May static PetscErrorCode PetscViewerFileClose_BinaryMPIIO(PetscViewer v) 677e0385b85SDave May { 678e0385b85SDave May PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data; 679e0385b85SDave May int err; 680e0385b85SDave May PetscErrorCode ierr; 681e0385b85SDave May 682e0385b85SDave May PetscFunctionBegin; 683e0385b85SDave May if (vbinary->mfdes) { 684e0385b85SDave May ierr = MPI_File_close(&vbinary->mfdes);CHKERRQ(ierr); 685e0385b85SDave May } 686e0385b85SDave May if (vbinary->fdes_info) { 687e0385b85SDave May err = fclose(vbinary->fdes_info); 688e0385b85SDave May if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file"); 689e0385b85SDave May } 690e0385b85SDave May PetscFunctionReturn(0); 691e0385b85SDave May } 692e0385b85SDave May #endif 693e0385b85SDave May 69481f0254dSBarry Smith static PetscErrorCode PetscViewerDestroy_Binary(PetscViewer v) 6955c6c1daeSBarry Smith { 6965c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data; 6975c6c1daeSBarry Smith PetscErrorCode ierr; 6985c6c1daeSBarry Smith 6995c6c1daeSBarry Smith PetscFunctionBegin; 700a261c58fSBarry Smith if (v->format == PETSC_VIEWER_BINARY_MATLAB) { 701a261c58fSBarry Smith MPI_Comm comm; 702a261c58fSBarry Smith FILE *info; 703a261c58fSBarry Smith 704a261c58fSBarry Smith ierr = PetscObjectGetComm((PetscObject)v,&comm);CHKERRQ(ierr); 705a261c58fSBarry Smith ierr = PetscViewerBinaryGetInfoPointer(v,&info);CHKERRQ(ierr); 706da88d4d4SJed Brown ierr = PetscFPrintf(comm,info,"#--- begin code written by PetscViewerBinary for MATLAB format ---#\n");CHKERRQ(ierr); 707da88d4d4SJed Brown ierr = PetscFPrintf(comm,info,"#$$ close(fd);\n");CHKERRQ(ierr); 708da88d4d4SJed Brown ierr = PetscFPrintf(comm,info,"#--- end code written by PetscViewerBinary for MATLAB format ---#\n\n");CHKERRQ(ierr); 709a261c58fSBarry Smith } 7105c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO) 711bc196f7cSDave May if (vbinary->usempiio) { 712e0385b85SDave May ierr = PetscViewerFileClose_BinaryMPIIO(v);CHKERRQ(ierr); 713e0385b85SDave May } else { 714e0385b85SDave May #endif 715e0385b85SDave May ierr = PetscViewerFileClose_Binary(v);CHKERRQ(ierr); 716e0385b85SDave May #if defined(PETSC_HAVE_MPIIO) 7175c6c1daeSBarry Smith } 7185c6c1daeSBarry Smith #endif 719f90597f1SStefano Zampini ierr = PetscFree(vbinary->filename);CHKERRQ(ierr); 720f90597f1SStefano Zampini ierr = PetscFree(vbinary->ogzfilename);CHKERRQ(ierr); 721e0385b85SDave May ierr = PetscFree(vbinary);CHKERRQ(ierr); 722e0385b85SDave May PetscFunctionReturn(0); 723e0385b85SDave May } 7245c6c1daeSBarry Smith 7255c6c1daeSBarry Smith /*@C 7265c6c1daeSBarry Smith PetscViewerBinaryOpen - Opens a file for binary input/output. 7275c6c1daeSBarry Smith 7285c6c1daeSBarry Smith Collective on MPI_Comm 7295c6c1daeSBarry Smith 7305c6c1daeSBarry Smith Input Parameters: 7315c6c1daeSBarry Smith + comm - MPI communicator 7325c6c1daeSBarry Smith . name - name of file 7335c6c1daeSBarry Smith - type - type of file 7345c6c1daeSBarry Smith $ FILE_MODE_WRITE - create new file for binary output 7355c6c1daeSBarry Smith $ FILE_MODE_READ - open existing file for binary input 7365c6c1daeSBarry Smith $ FILE_MODE_APPEND - open existing file for binary output 7375c6c1daeSBarry Smith 7385c6c1daeSBarry Smith Output Parameter: 7395c6c1daeSBarry Smith . binv - PetscViewer for binary input/output to use with the specified file 7405c6c1daeSBarry Smith 7415c6c1daeSBarry Smith Options Database Keys: 742a2d7db39SDave May + -viewer_binary_filename <name> 743a2d7db39SDave May . -viewer_binary_skip_info 7445c6c1daeSBarry Smith . -viewer_binary_skip_options 745a2d7db39SDave May . -viewer_binary_skip_header 746a2d7db39SDave May - -viewer_binary_mpiio 7475c6c1daeSBarry Smith 7485c6c1daeSBarry Smith Level: beginner 7495c6c1daeSBarry Smith 7505c6c1daeSBarry Smith Note: 7515c6c1daeSBarry Smith This PetscViewer should be destroyed with PetscViewerDestroy(). 7525c6c1daeSBarry Smith 7535c6c1daeSBarry Smith For reading files, the filename may begin with ftp:// or http:// and/or 7545c6c1daeSBarry Smith end with .gz; in this case file is brought over and uncompressed. 7555c6c1daeSBarry Smith 7565c6c1daeSBarry Smith For creating files, if the file name ends with .gz it is automatically 7575c6c1daeSBarry Smith compressed when closed. 7585c6c1daeSBarry Smith 7595c6c1daeSBarry Smith For writing files it only opens the file on processor 0 in the communicator. 7605c6c1daeSBarry Smith For readable files it opens the file on all nodes that have the file. If 7615c6c1daeSBarry Smith node 0 does not have the file it generates an error even if other nodes 7625c6c1daeSBarry Smith do have the file. 7635c6c1daeSBarry Smith 7645c6c1daeSBarry Smith Concepts: binary files 7655c6c1daeSBarry Smith Concepts: PetscViewerBinary^creating 7665c6c1daeSBarry Smith Concepts: gzip 7675c6c1daeSBarry Smith Concepts: accessing remote file 7685c6c1daeSBarry Smith Concepts: remote file 7695c6c1daeSBarry Smith 7706a9046bcSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(), 7715c6c1daeSBarry Smith VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(), 77267918a83SBarry Smith PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscViewerBinaryRead(), PetscViewerBinarySetUseMPIIO(), 77367918a83SBarry Smith PetscViewerBinaryGetUseMPIIO(), PetscViewerBinaryGetMPIIOOffset() 7745c6c1daeSBarry Smith @*/ 7755c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryOpen(MPI_Comm comm,const char name[],PetscFileMode type,PetscViewer *binv) 7765c6c1daeSBarry Smith { 7775c6c1daeSBarry Smith PetscErrorCode ierr; 7785c6c1daeSBarry Smith 7795c6c1daeSBarry Smith PetscFunctionBegin; 7805c6c1daeSBarry Smith ierr = PetscViewerCreate(comm,binv);CHKERRQ(ierr); 7815c6c1daeSBarry Smith ierr = PetscViewerSetType(*binv,PETSCVIEWERBINARY);CHKERRQ(ierr); 7825c6c1daeSBarry Smith ierr = PetscViewerFileSetMode(*binv,type);CHKERRQ(ierr); 7835c6c1daeSBarry Smith ierr = PetscViewerFileSetName(*binv,name);CHKERRQ(ierr); 78403a1d59fSDave May ierr = PetscViewerSetFromOptions(*binv);CHKERRQ(ierr); 7855c6c1daeSBarry Smith PetscFunctionReturn(0); 7865c6c1daeSBarry Smith } 7875c6c1daeSBarry Smith 7885c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO) 789060da220SMatthew G. Knepley static PetscErrorCode PetscViewerBinaryWriteReadMPIIO(PetscViewer viewer,void *data,PetscInt num,PetscInt *count,PetscDataType dtype,PetscBool write) 7905c6c1daeSBarry Smith { 7915c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 7925c6c1daeSBarry Smith PetscErrorCode ierr; 7935c6c1daeSBarry Smith MPI_Datatype mdtype; 794e597bc1dSJed Brown PetscMPIInt cnt; 7955c6c1daeSBarry Smith MPI_Status status; 7965c6c1daeSBarry Smith MPI_Aint ul,dsize; 7975c6c1daeSBarry Smith 7985c6c1daeSBarry Smith PetscFunctionBegin; 799060da220SMatthew G. Knepley ierr = PetscMPIIntCast(num,&cnt);CHKERRQ(ierr); 8005c6c1daeSBarry Smith ierr = PetscDataTypeToMPIDataType(dtype,&mdtype);CHKERRQ(ierr); 8015c6c1daeSBarry Smith ierr = MPI_File_set_view(vbinary->mfdes,vbinary->moff,mdtype,mdtype,(char*)"native",MPI_INFO_NULL);CHKERRQ(ierr); 8025c6c1daeSBarry Smith if (write) { 8035c6c1daeSBarry Smith ierr = MPIU_File_write_all(vbinary->mfdes,data,cnt,mdtype,&status);CHKERRQ(ierr); 8045c6c1daeSBarry Smith } else { 8055c6c1daeSBarry Smith ierr = MPIU_File_read_all(vbinary->mfdes,data,cnt,mdtype,&status);CHKERRQ(ierr); 8065c6c1daeSBarry Smith } 8075c6c1daeSBarry Smith ierr = MPI_Type_get_extent(mdtype,&ul,&dsize);CHKERRQ(ierr); 808a297a907SKarl Rupp 8095c6c1daeSBarry Smith vbinary->moff += dsize*cnt; 810060da220SMatthew G. Knepley if (count) *count = num; 8115c6c1daeSBarry Smith PetscFunctionReturn(0); 8125c6c1daeSBarry Smith } 8135c6c1daeSBarry Smith #endif 8145c6c1daeSBarry Smith 8155c6c1daeSBarry Smith /*@C 8165c6c1daeSBarry Smith PetscViewerBinaryRead - Reads from a binary file, all processors get the same result 8175c6c1daeSBarry Smith 8185c6c1daeSBarry Smith Collective on MPI_Comm 8195c6c1daeSBarry Smith 8205c6c1daeSBarry Smith Input Parameters: 8215c6c1daeSBarry Smith + viewer - the binary viewer 822907376e6SBarry Smith . data - location of the data to be written 823060da220SMatthew G. Knepley . num - number of items of data to read 824907376e6SBarry Smith - dtype - type of data to read 8255c6c1daeSBarry Smith 826f8e4bde8SMatthew G. Knepley Output Parameters: 8270780a867SBarry Smith . count - number of items of data actually read, or NULL. Unless an error is generated this is always set to the input parameter num. 828f8e4bde8SMatthew G. Knepley 8295c6c1daeSBarry Smith Level: beginner 8305c6c1daeSBarry Smith 8315c6c1daeSBarry Smith Concepts: binary files 8325c6c1daeSBarry Smith 8330780a867SBarry Smith Developer Note: Since count is always set to num it is not clear what purpose the output argument count serves. 8340780a867SBarry Smith 8356a9046bcSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(), 8365c6c1daeSBarry Smith VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(), 8375c6c1daeSBarry Smith PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead() 8385c6c1daeSBarry Smith @*/ 839060da220SMatthew G. Knepley PetscErrorCode PetscViewerBinaryRead(PetscViewer viewer,void *data,PetscInt num,PetscInt *count,PetscDataType dtype) 8405c6c1daeSBarry Smith { 8415c6c1daeSBarry Smith PetscErrorCode ierr; 8425c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 8435c6c1daeSBarry Smith 844c98fd787SBarry Smith ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr); 8455c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO) 846bc196f7cSDave May if (vbinary->usempiio) { 847060da220SMatthew G. Knepley ierr = PetscViewerBinaryWriteReadMPIIO(viewer,data,num,count,dtype,PETSC_FALSE);CHKERRQ(ierr); 8485c6c1daeSBarry Smith } else { 8495c6c1daeSBarry Smith #endif 850060da220SMatthew G. Knepley ierr = PetscBinarySynchronizedRead(PetscObjectComm((PetscObject)viewer),vbinary->fdes,data,num,dtype);CHKERRQ(ierr); 8510780a867SBarry Smith if (count) *count = num; 8525c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO) 8535c6c1daeSBarry Smith } 8545c6c1daeSBarry Smith #endif 8555c6c1daeSBarry Smith PetscFunctionReturn(0); 8565c6c1daeSBarry Smith } 8575c6c1daeSBarry Smith 8585c6c1daeSBarry Smith /*@C 8595c6c1daeSBarry Smith PetscViewerBinaryWrite - writes to a binary file, only from the first process 8605c6c1daeSBarry Smith 8615c6c1daeSBarry Smith Collective on MPI_Comm 8625c6c1daeSBarry Smith 8635c6c1daeSBarry Smith Input Parameters: 8645c6c1daeSBarry Smith + viewer - the binary viewer 8655c6c1daeSBarry Smith . data - location of data 8665824c9d0SPatrick Sanan . count - number of items of data to write 8675824c9d0SPatrick Sanan . dtype - type of data to write 8685c6c1daeSBarry Smith - istemp - data may be overwritten 8695c6c1daeSBarry Smith 8705c6c1daeSBarry Smith Level: beginner 8715c6c1daeSBarry Smith 8725c6c1daeSBarry Smith Notes: because byte-swapping may be done on the values in data it cannot be declared const 8735c6c1daeSBarry Smith 8745c6c1daeSBarry Smith Concepts: binary files 8755c6c1daeSBarry Smith 8766a9046bcSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(), 8775c6c1daeSBarry Smith VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(), PetscDataType 8785c6c1daeSBarry Smith PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead() 8795c6c1daeSBarry Smith @*/ 8805c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryWrite(PetscViewer viewer,void *data,PetscInt count,PetscDataType dtype,PetscBool istemp) 8815c6c1daeSBarry Smith { 8825c6c1daeSBarry Smith PetscErrorCode ierr; 8835c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 8845c6c1daeSBarry Smith 8855c6c1daeSBarry Smith PetscFunctionBegin; 886c98fd787SBarry Smith ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr); 8875c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO) 888bc196f7cSDave May if (vbinary->usempiio) { 889060da220SMatthew G. Knepley ierr = PetscViewerBinaryWriteReadMPIIO(viewer,data,count,NULL,dtype,PETSC_TRUE);CHKERRQ(ierr); 8905c6c1daeSBarry Smith } else { 8915c6c1daeSBarry Smith #endif 892ce94432eSBarry Smith ierr = PetscBinarySynchronizedWrite(PetscObjectComm((PetscObject)viewer),vbinary->fdes,data,count,dtype,istemp);CHKERRQ(ierr); 8935c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO) 8945c6c1daeSBarry Smith } 8955c6c1daeSBarry Smith #endif 8965c6c1daeSBarry Smith PetscFunctionReturn(0); 8975c6c1daeSBarry Smith } 8985c6c1daeSBarry Smith 8995c6c1daeSBarry Smith /*@C 9005c6c1daeSBarry Smith PetscViewerBinaryWriteStringArray - writes to a binary file, only from the first process an array of strings 9015c6c1daeSBarry Smith 9025c6c1daeSBarry Smith Collective on MPI_Comm 9035c6c1daeSBarry Smith 9045c6c1daeSBarry Smith Input Parameters: 9055c6c1daeSBarry Smith + viewer - the binary viewer 9065c6c1daeSBarry Smith - data - location of the array of strings 9075c6c1daeSBarry Smith 9085c6c1daeSBarry Smith 9095c6c1daeSBarry Smith Level: intermediate 9105c6c1daeSBarry Smith 9115c6c1daeSBarry Smith Concepts: binary files 9125c6c1daeSBarry Smith 9135c6c1daeSBarry Smith Notes: array of strings is null terminated 9145c6c1daeSBarry Smith 9156a9046bcSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(), 9165c6c1daeSBarry Smith VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(), 9175c6c1daeSBarry Smith PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead() 9185c6c1daeSBarry Smith @*/ 91978fbdcc8SBarry Smith PetscErrorCode PetscViewerBinaryWriteStringArray(PetscViewer viewer,const char * const *data) 9205c6c1daeSBarry Smith { 9215c6c1daeSBarry Smith PetscErrorCode ierr; 9225c6c1daeSBarry Smith PetscInt i,n = 0,*sizes; 9235c6c1daeSBarry Smith 924c98fd787SBarry Smith ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr); 9255c6c1daeSBarry Smith /* count number of strings */ 9265c6c1daeSBarry Smith while (data[n++]) ; 9275c6c1daeSBarry Smith n--; 928854ce69bSBarry Smith ierr = PetscMalloc1(n+1,&sizes);CHKERRQ(ierr); 9295c6c1daeSBarry Smith sizes[0] = n; 9305c6c1daeSBarry Smith for (i=0; i<n; i++) { 9315c6c1daeSBarry Smith size_t tmp; 9325c6c1daeSBarry Smith ierr = PetscStrlen(data[i],&tmp);CHKERRQ(ierr); 9335c6c1daeSBarry Smith sizes[i+1] = tmp + 1; /* size includes space for the null terminator */ 9345c6c1daeSBarry Smith } 9355c6c1daeSBarry Smith ierr = PetscViewerBinaryWrite(viewer,sizes,n+1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 9365c6c1daeSBarry Smith for (i=0; i<n; i++) { 93778fbdcc8SBarry Smith ierr = PetscViewerBinaryWrite(viewer,(void*)data[i],sizes[i+1],PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 9385c6c1daeSBarry Smith } 9395c6c1daeSBarry Smith ierr = PetscFree(sizes);CHKERRQ(ierr); 9405c6c1daeSBarry Smith PetscFunctionReturn(0); 9415c6c1daeSBarry Smith } 9425c6c1daeSBarry Smith 9435c6c1daeSBarry Smith /*@C 9445c6c1daeSBarry Smith PetscViewerBinaryReadStringArray - reads a binary file an array of strings 9455c6c1daeSBarry Smith 9465c6c1daeSBarry Smith Collective on MPI_Comm 9475c6c1daeSBarry Smith 9485c6c1daeSBarry Smith Input Parameter: 9495c6c1daeSBarry Smith . viewer - the binary viewer 9505c6c1daeSBarry Smith 9515c6c1daeSBarry Smith Output Parameter: 9525c6c1daeSBarry Smith . data - location of the array of strings 9535c6c1daeSBarry Smith 9545c6c1daeSBarry Smith Level: intermediate 9555c6c1daeSBarry Smith 9565c6c1daeSBarry Smith Concepts: binary files 9575c6c1daeSBarry Smith 9585c6c1daeSBarry Smith Notes: array of strings is null terminated 9595c6c1daeSBarry Smith 9606a9046bcSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(), 9615c6c1daeSBarry Smith VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(), 9625c6c1daeSBarry Smith PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead() 9635c6c1daeSBarry Smith @*/ 9645c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryReadStringArray(PetscViewer viewer,char ***data) 9655c6c1daeSBarry Smith { 9665c6c1daeSBarry Smith PetscErrorCode ierr; 967060da220SMatthew G. Knepley PetscInt i,n,*sizes,N = 0; 9685c6c1daeSBarry Smith 969c98fd787SBarry Smith ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr); 9705c6c1daeSBarry Smith /* count number of strings */ 971060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,&n,1,NULL,PETSC_INT);CHKERRQ(ierr); 972785e854fSJed Brown ierr = PetscMalloc1(n,&sizes);CHKERRQ(ierr); 973060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,sizes,n,NULL,PETSC_INT);CHKERRQ(ierr); 974a297a907SKarl Rupp for (i=0; i<n; i++) N += sizes[i]; 975854ce69bSBarry Smith ierr = PetscMalloc((n+1)*sizeof(char*) + N*sizeof(char),data);CHKERRQ(ierr); 9765c6c1daeSBarry Smith (*data)[0] = (char*)((*data) + n + 1); 977a297a907SKarl Rupp for (i=1; i<n; i++) (*data)[i] = (*data)[i-1] + sizes[i-1]; 978060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,(*data)[0],N,NULL,PETSC_CHAR);CHKERRQ(ierr); 979a297a907SKarl Rupp 9805c6c1daeSBarry Smith (*data)[n] = 0; 981a297a907SKarl Rupp 9825c6c1daeSBarry Smith ierr = PetscFree(sizes);CHKERRQ(ierr); 9835c6c1daeSBarry Smith PetscFunctionReturn(0); 9845c6c1daeSBarry Smith } 9855c6c1daeSBarry Smith 98681f0254dSBarry Smith static PetscErrorCode PetscViewerFileGetName_Binary(PetscViewer viewer,const char **name) 9875c6c1daeSBarry Smith { 9885c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 9895c6c1daeSBarry Smith 9905c6c1daeSBarry Smith PetscFunctionBegin; 9915c6c1daeSBarry Smith *name = vbinary->filename; 9925c6c1daeSBarry Smith PetscFunctionReturn(0); 9935c6c1daeSBarry Smith } 9945c6c1daeSBarry Smith 9955c6c1daeSBarry Smith /*@C 9965c6c1daeSBarry Smith PetscViewerFileGetMode - Gets the type of file to be open 9975c6c1daeSBarry Smith 9985c6c1daeSBarry Smith Not Collective 9995c6c1daeSBarry Smith 10005c6c1daeSBarry Smith Input Parameter: 1001232ac771SBarry Smith . viewer - the PetscViewer; must be a PETSCVIEWERBINARY, PETSCVIEWERMATLAB, PETSCVIEWERHDF5, or PETSCVIEWERASCII PetscViewer 10025c6c1daeSBarry Smith 10035c6c1daeSBarry Smith Output Parameter: 10045c6c1daeSBarry Smith . type - type of file 10055c6c1daeSBarry Smith $ FILE_MODE_WRITE - create new file for binary output 10065c6c1daeSBarry Smith $ FILE_MODE_READ - open existing file for binary input 10075c6c1daeSBarry Smith $ FILE_MODE_APPEND - open existing file for binary output 10085c6c1daeSBarry Smith 10095c6c1daeSBarry Smith Level: advanced 10105c6c1daeSBarry Smith 10115c6c1daeSBarry Smith .seealso: PetscViewerFileSetMode(), PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinaryOpen() 10125c6c1daeSBarry Smith 10135c6c1daeSBarry Smith @*/ 10145c6c1daeSBarry Smith PetscErrorCode PetscViewerFileGetMode(PetscViewer viewer,PetscFileMode *type) 10155c6c1daeSBarry Smith { 10165c6c1daeSBarry Smith PetscErrorCode ierr; 10175c6c1daeSBarry Smith 10185c6c1daeSBarry Smith PetscFunctionBegin; 10195c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 10205c6c1daeSBarry Smith PetscValidPointer(type,2); 10215c6c1daeSBarry Smith ierr = PetscUseMethod(viewer,"PetscViewerFileGetMode_C",(PetscViewer,PetscFileMode*),(viewer,type));CHKERRQ(ierr); 10225c6c1daeSBarry Smith PetscFunctionReturn(0); 10235c6c1daeSBarry Smith } 10245c6c1daeSBarry Smith 10255c6c1daeSBarry Smith /*@ 1026bc196f7cSDave May PetscViewerBinarySetUseMPIIO - Sets a binary viewer to use MPI-IO for reading/writing. Must be called 10275c6c1daeSBarry Smith before PetscViewerFileSetName() 10285c6c1daeSBarry Smith 10295c6c1daeSBarry Smith Logically Collective on PetscViewer 10305c6c1daeSBarry Smith 10315c6c1daeSBarry Smith Input Parameters: 1032bc196f7cSDave May + viewer - the PetscViewer; must be a binary 1033bc196f7cSDave May - flg - PETSC_TRUE means MPI-IO will be used 10345c6c1daeSBarry Smith 10355c6c1daeSBarry Smith Options Database: 10365c6c1daeSBarry Smith -viewer_binary_mpiio : Flag for using MPI-IO 10375c6c1daeSBarry Smith 10385c6c1daeSBarry Smith Level: advanced 10395c6c1daeSBarry Smith 1040bc196f7cSDave May .seealso: PetscViewerFileSetMode(), PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinaryOpen(), 1041bc196f7cSDave May PetscViewerBinaryGetUseMPIIO() 10425c6c1daeSBarry Smith 10435c6c1daeSBarry Smith @*/ 1044bc196f7cSDave May PetscErrorCode PetscViewerBinarySetUseMPIIO(PetscViewer viewer,PetscBool flg) 10455c6c1daeSBarry Smith { 10465c6c1daeSBarry Smith PetscErrorCode ierr; 10475c6c1daeSBarry Smith 10485c6c1daeSBarry Smith PetscFunctionBegin; 10495c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 1050bc196f7cSDave May ierr = PetscTryMethod(viewer,"PetscViewerBinarySetUseMPIIO_C",(PetscViewer,PetscBool),(viewer,flg));CHKERRQ(ierr); 10515c6c1daeSBarry Smith PetscFunctionReturn(0); 10525c6c1daeSBarry Smith } 10535c6c1daeSBarry Smith 10545c6c1daeSBarry Smith /*@C 10555c6c1daeSBarry Smith PetscViewerFileSetMode - Sets the type of file to be open 10565c6c1daeSBarry Smith 10575c6c1daeSBarry Smith Logically Collective on PetscViewer 10585c6c1daeSBarry Smith 10595c6c1daeSBarry Smith Input Parameters: 1060232ac771SBarry Smith + viewer - the PetscViewer; must be a a PETSCVIEWERBINARY, PETSCVIEWERMATLAB, PETSCVIEWERHDF5, or PETSCVIEWERASCII PetscViewer 10615c6c1daeSBarry Smith - type - type of file 10625c6c1daeSBarry Smith $ FILE_MODE_WRITE - create new file for binary output 10635c6c1daeSBarry Smith $ FILE_MODE_READ - open existing file for binary input 10645c6c1daeSBarry Smith $ FILE_MODE_APPEND - open existing file for binary output 10655c6c1daeSBarry Smith 10665c6c1daeSBarry Smith Level: advanced 10675c6c1daeSBarry Smith 10685c6c1daeSBarry Smith .seealso: PetscViewerFileSetMode(), PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinaryOpen() 10695c6c1daeSBarry Smith 10705c6c1daeSBarry Smith @*/ 10715c6c1daeSBarry Smith PetscErrorCode PetscViewerFileSetMode(PetscViewer viewer,PetscFileMode type) 10725c6c1daeSBarry Smith { 10735c6c1daeSBarry Smith PetscErrorCode ierr; 10745c6c1daeSBarry Smith 10755c6c1daeSBarry Smith PetscFunctionBegin; 10765c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 10775c6c1daeSBarry Smith PetscValidLogicalCollectiveEnum(viewer,type,2); 10785c6c1daeSBarry Smith ierr = PetscTryMethod(viewer,"PetscViewerFileSetMode_C",(PetscViewer,PetscFileMode),(viewer,type));CHKERRQ(ierr); 10795c6c1daeSBarry Smith PetscFunctionReturn(0); 10805c6c1daeSBarry Smith } 10815c6c1daeSBarry Smith 108281f0254dSBarry Smith static PetscErrorCode PetscViewerFileGetMode_Binary(PetscViewer viewer,PetscFileMode *type) 10835c6c1daeSBarry Smith { 10845c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 10855c6c1daeSBarry Smith 10865c6c1daeSBarry Smith PetscFunctionBegin; 10875c6c1daeSBarry Smith *type = vbinary->btype; 10885c6c1daeSBarry Smith PetscFunctionReturn(0); 10895c6c1daeSBarry Smith } 10905c6c1daeSBarry Smith 109181f0254dSBarry Smith static PetscErrorCode PetscViewerFileSetMode_Binary(PetscViewer viewer,PetscFileMode type) 10925c6c1daeSBarry Smith { 10935c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 10945c6c1daeSBarry Smith 10955c6c1daeSBarry Smith PetscFunctionBegin; 10965c6c1daeSBarry Smith vbinary->btype = type; 10975c6c1daeSBarry Smith PetscFunctionReturn(0); 10985c6c1daeSBarry Smith } 10995c6c1daeSBarry Smith 110081f0254dSBarry Smith static PetscErrorCode PetscViewerFileSetName_Binary(PetscViewer viewer,const char name[]) 1101e0385b85SDave May { 1102e0385b85SDave May PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 1103e0385b85SDave May PetscErrorCode ierr; 1104e0385b85SDave May 1105e0385b85SDave May PetscFunctionBegin; 1106f90597f1SStefano Zampini if (vbinary->filename) { 1107f90597f1SStefano Zampini /* gzip can be run after the file with the previous filename has been closed */ 1108f90597f1SStefano Zampini ierr = PetscFree(vbinary->ogzfilename);CHKERRQ(ierr); 1109f90597f1SStefano Zampini ierr = PetscStrallocpy(vbinary->filename,&vbinary->ogzfilename);CHKERRQ(ierr); 1110f90597f1SStefano Zampini ierr = PetscFree(vbinary->filename);CHKERRQ(ierr); 1111f90597f1SStefano Zampini viewer->setupcalled = PETSC_FALSE; 1112f90597f1SStefano Zampini } 1113e0385b85SDave May ierr = PetscStrallocpy(name,&vbinary->filename);CHKERRQ(ierr); 1114e0385b85SDave May PetscFunctionReturn(0); 1115e0385b85SDave May } 11165c6c1daeSBarry Smith /* 11175c6c1daeSBarry Smith Actually opens the file 11185c6c1daeSBarry Smith */ 1119e0877f53SBarry Smith static PetscErrorCode PetscViewerFileSetUp_Binary(PetscViewer viewer) 11205c6c1daeSBarry Smith { 11215c6c1daeSBarry Smith PetscMPIInt rank; 11225c6c1daeSBarry Smith PetscErrorCode ierr; 11235c6c1daeSBarry Smith size_t len; 11245c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 11255c6c1daeSBarry Smith const char *fname; 11262259a519SDave May char bname[PETSC_MAX_PATH_LEN],*gz; 11275c6c1daeSBarry Smith PetscBool found; 11285c6c1daeSBarry Smith PetscFileMode type = vbinary->btype; 11295c6c1daeSBarry Smith 11305c6c1daeSBarry Smith PetscFunctionBegin; 1131e0385b85SDave May if (type == (PetscFileMode) -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetMode()"); 11322259a519SDave May if (!vbinary->filename) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetName()"); 11335c6c1daeSBarry Smith ierr = PetscViewerFileClose_Binary(viewer);CHKERRQ(ierr); 11345c6c1daeSBarry Smith 1135ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr); 11365c6c1daeSBarry Smith 11375c6c1daeSBarry Smith /* if ends in .gz strip that off and note user wants file compressed */ 11385c6c1daeSBarry Smith vbinary->storecompressed = PETSC_FALSE; 11395c6c1daeSBarry Smith if (!rank && type == FILE_MODE_WRITE) { 11405c6c1daeSBarry Smith /* remove .gz if it ends library name */ 11415c6c1daeSBarry Smith ierr = PetscStrstr(vbinary->filename,".gz",&gz);CHKERRQ(ierr); 11425c6c1daeSBarry Smith if (gz) { 11435c6c1daeSBarry Smith ierr = PetscStrlen(gz,&len);CHKERRQ(ierr); 11445c6c1daeSBarry Smith if (len == 3) { 11455c6c1daeSBarry Smith *gz = 0; 11465c6c1daeSBarry Smith vbinary->storecompressed = PETSC_TRUE; 11475c6c1daeSBarry Smith } 11485c6c1daeSBarry Smith } 11495c6c1daeSBarry Smith } 11505c6c1daeSBarry Smith 11515c6c1daeSBarry Smith /* only first processor opens file if writeable */ 11525c6c1daeSBarry Smith if (!rank || type == FILE_MODE_READ) { 11535c6c1daeSBarry Smith 11545c6c1daeSBarry Smith if (type == FILE_MODE_READ) { 11555c6c1daeSBarry Smith /* possibly get the file from remote site or compressed file */ 1156ce94432eSBarry Smith ierr = PetscFileRetrieve(PetscObjectComm((PetscObject)viewer),vbinary->filename,bname,PETSC_MAX_PATH_LEN,&found);CHKERRQ(ierr); 11575c6c1daeSBarry Smith fname = bname; 1158*41c4be4aSVaclav Hapla /* comm below may be global as all ranks go here for FILE_MODE_READ and output 'found' of PetscFileRetrieve() is valid on all processes */ 1159*41c4be4aSVaclav Hapla if (!found) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_FILE_OPEN,"Cannot locate file: %s on node zero",vbinary->filename); 1160a297a907SKarl Rupp } else fname = vbinary->filename; 11615c6c1daeSBarry Smith 11625c6c1daeSBarry Smith #if defined(PETSC_HAVE_O_BINARY) 11635c6c1daeSBarry Smith if (type == FILE_MODE_WRITE) { 11645c6c1daeSBarry 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); 11655c6c1daeSBarry Smith } else if (type == FILE_MODE_READ && fname) { 11665c6c1daeSBarry 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); 11675c6c1daeSBarry Smith } else if (type == FILE_MODE_APPEND) { 11685c6c1daeSBarry 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); 11695c6c1daeSBarry Smith } else if (fname) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown file type"); 11705c6c1daeSBarry Smith #else 11715c6c1daeSBarry Smith if (type == FILE_MODE_WRITE) { 11725c6c1daeSBarry Smith if ((vbinary->fdes = creat(fname,0666)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot create file %s for writing",fname); 11735c6c1daeSBarry Smith } else if (type == FILE_MODE_READ && fname) { 11745c6c1daeSBarry 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); 11755c6c1daeSBarry Smith } else if (type == FILE_MODE_APPEND) { 11765c6c1daeSBarry 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); 11775c6c1daeSBarry Smith } else if (fname) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown file type"); 11785c6c1daeSBarry Smith #endif 11795c6c1daeSBarry Smith } else vbinary->fdes = -1; 11805c6c1daeSBarry Smith 11815c6c1daeSBarry Smith /* 11825c6c1daeSBarry Smith try to open info file: all processors open this file if read only 11835c6c1daeSBarry Smith */ 11845c6c1daeSBarry Smith if (!vbinary->skipinfo && (!rank || type == FILE_MODE_READ)) { 11855c6c1daeSBarry Smith char infoname[PETSC_MAX_PATH_LEN],iname[PETSC_MAX_PATH_LEN]; 11865c6c1daeSBarry Smith 1187a126751eSBarry Smith ierr = PetscStrncpy(infoname,vbinary->filename,sizeof(infoname));CHKERRQ(ierr); 11885c6c1daeSBarry Smith /* remove .gz if it ends library name */ 11895c6c1daeSBarry Smith ierr = PetscStrstr(infoname,".gz",&gz);CHKERRQ(ierr); 11905c6c1daeSBarry Smith if (gz) { 11915c6c1daeSBarry Smith ierr = PetscStrlen(gz,&len);CHKERRQ(ierr); 1192a297a907SKarl Rupp if (len == 3) *gz = 0; 11935c6c1daeSBarry Smith } 11945c6c1daeSBarry Smith 1195a126751eSBarry Smith ierr = PetscStrlcat(infoname,".info",sizeof(infoname));CHKERRQ(ierr); 11965c6c1daeSBarry Smith ierr = PetscFixFilename(infoname,iname);CHKERRQ(ierr); 11975c6c1daeSBarry Smith if (type == FILE_MODE_READ) { 1198ce94432eSBarry Smith ierr = PetscFileRetrieve(PetscObjectComm((PetscObject)viewer),iname,infoname,PETSC_MAX_PATH_LEN,&found);CHKERRQ(ierr); 1199c5929fdfSBarry Smith ierr = PetscOptionsInsertFile(PetscObjectComm((PetscObject)viewer),((PetscObject)viewer)->options,infoname,PETSC_FALSE);CHKERRQ(ierr); 12005c6c1daeSBarry Smith } else { 12015c6c1daeSBarry Smith vbinary->fdes_info = fopen(infoname,"w"); 12025c6c1daeSBarry Smith if (!vbinary->fdes_info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open .info file %s for writing",infoname); 12035c6c1daeSBarry Smith } 12045c6c1daeSBarry Smith } 12055c6c1daeSBarry Smith #if defined(PETSC_USE_LOG) 1206e0385b85SDave May PetscLogObjectState((PetscObject)viewer,"File: %s",vbinary->filename); 12075c6c1daeSBarry Smith #endif 12085c6c1daeSBarry Smith PetscFunctionReturn(0); 12095c6c1daeSBarry Smith } 12105c6c1daeSBarry Smith 12115c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO) 1212e0385b85SDave May static PetscErrorCode PetscViewerFileSetUp_BinaryMPIIO(PetscViewer viewer) 12135c6c1daeSBarry Smith { 12145c6c1daeSBarry Smith PetscMPIInt rank; 12155c6c1daeSBarry Smith PetscErrorCode ierr; 12165c6c1daeSBarry Smith size_t len; 12175c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 12182259a519SDave May char *gz; 12195c6c1daeSBarry Smith PetscBool found; 12205c6c1daeSBarry Smith PetscFileMode type = vbinary->btype; 12215c6c1daeSBarry Smith 12225c6c1daeSBarry Smith PetscFunctionBegin; 1223e0385b85SDave May if (type == (PetscFileMode) -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetMode()"); 12242259a519SDave May if (!vbinary->filename) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetName()"); 1225e0385b85SDave May ierr = PetscViewerFileClose_BinaryMPIIO(viewer);CHKERRQ(ierr); 12265c6c1daeSBarry Smith 1227ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr); 12285c6c1daeSBarry Smith 1229a297a907SKarl Rupp vbinary->storecompressed = PETSC_FALSE; 12305c6c1daeSBarry Smith 12315c6c1daeSBarry Smith /* only first processor opens file if writeable */ 12325c6c1daeSBarry Smith if (type == FILE_MODE_READ) { 1233ce94432eSBarry Smith MPI_File_open(PetscObjectComm((PetscObject)viewer),vbinary->filename,MPI_MODE_RDONLY,MPI_INFO_NULL,&vbinary->mfdes);CHKERRQ(ierr); 12345c6c1daeSBarry Smith } else if (type == FILE_MODE_WRITE) { 1235ce94432eSBarry Smith MPI_File_open(PetscObjectComm((PetscObject)viewer),vbinary->filename,MPI_MODE_WRONLY | MPI_MODE_CREATE,MPI_INFO_NULL,&vbinary->mfdes);CHKERRQ(ierr); 12365c6c1daeSBarry Smith } 12375c6c1daeSBarry Smith 12385c6c1daeSBarry Smith /* 12395c6c1daeSBarry Smith try to open info file: all processors open this file if read only 12405c6c1daeSBarry Smith 1241bebe2cf6SSatish Balay Below is identical code to the code for Binary above, should be put in separate routine 12425c6c1daeSBarry Smith */ 12435c6c1daeSBarry Smith if (!vbinary->skipinfo && (!rank || type == FILE_MODE_READ)) { 12445c6c1daeSBarry Smith char infoname[PETSC_MAX_PATH_LEN],iname[PETSC_MAX_PATH_LEN]; 12455c6c1daeSBarry Smith 1246a126751eSBarry Smith ierr = PetscStrncpy(infoname,vbinary->filename,sizeof(infoname));CHKERRQ(ierr); 12475c6c1daeSBarry Smith /* remove .gz if it ends library name */ 12485c6c1daeSBarry Smith ierr = PetscStrstr(infoname,".gz",&gz);CHKERRQ(ierr); 12495c6c1daeSBarry Smith if (gz) { 12505c6c1daeSBarry Smith ierr = PetscStrlen(gz,&len);CHKERRQ(ierr); 1251a297a907SKarl Rupp if (len == 3) *gz = 0; 12525c6c1daeSBarry Smith } 12535c6c1daeSBarry Smith 1254a126751eSBarry Smith ierr = PetscStrlcat(infoname,".info",sizeof(infoname));CHKERRQ(ierr); 12555c6c1daeSBarry Smith ierr = PetscFixFilename(infoname,iname);CHKERRQ(ierr); 12565c6c1daeSBarry Smith if (type == FILE_MODE_READ) { 1257ce94432eSBarry Smith ierr = PetscFileRetrieve(PetscObjectComm((PetscObject)viewer),iname,infoname,PETSC_MAX_PATH_LEN,&found);CHKERRQ(ierr); 1258c5929fdfSBarry Smith ierr = PetscOptionsInsertFile(PetscObjectComm((PetscObject)viewer),((PetscObject)viewer)->options,infoname,PETSC_FALSE);CHKERRQ(ierr); 12595c6c1daeSBarry Smith } else { 12605c6c1daeSBarry Smith vbinary->fdes_info = fopen(infoname,"w"); 12615c6c1daeSBarry Smith if (!vbinary->fdes_info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open .info file %s for writing",infoname); 12625c6c1daeSBarry Smith } 12635c6c1daeSBarry Smith } 12645c6c1daeSBarry Smith #if defined(PETSC_USE_LOG) 1265e0385b85SDave May PetscLogObjectState((PetscObject)viewer,"File: %s",vbinary->filename); 12665c6c1daeSBarry Smith #endif 12675c6c1daeSBarry Smith PetscFunctionReturn(0); 12685c6c1daeSBarry Smith } 12695c6c1daeSBarry Smith 127081f0254dSBarry Smith static PetscErrorCode PetscViewerBinarySetUseMPIIO_Binary(PetscViewer viewer,PetscBool flg) 12715c6c1daeSBarry Smith { 12725c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 12735c6c1daeSBarry Smith PetscFunctionBegin; 1274bc196f7cSDave May vbinary->usempiio = flg; 12755c6c1daeSBarry Smith PetscFunctionReturn(0); 12765c6c1daeSBarry Smith } 12775c6c1daeSBarry Smith #endif 12785c6c1daeSBarry Smith 127981f0254dSBarry Smith static PetscErrorCode PetscViewerView_Binary(PetscViewer v,PetscViewer viewer) 12802bf49c77SBarry Smith { 12812bf49c77SBarry Smith PetscErrorCode ierr; 12822bf49c77SBarry Smith PetscViewer_Binary *binary = (PetscViewer_Binary*)v->data; 12832bf49c77SBarry Smith 12842bf49c77SBarry Smith PetscFunctionBegin; 12852bf49c77SBarry Smith if (binary->filename) { 12862bf49c77SBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Filename: %s\n",binary->filename);CHKERRQ(ierr); 12872bf49c77SBarry Smith } 12882bf49c77SBarry Smith PetscFunctionReturn(0); 12892bf49c77SBarry Smith } 12902bf49c77SBarry Smith 129103a1d59fSDave May static PetscErrorCode PetscViewerSetUp_Binary(PetscViewer v) 129203a1d59fSDave May { 129303a1d59fSDave May PetscErrorCode ierr; 129403a1d59fSDave May PetscViewer_Binary *binary = (PetscViewer_Binary*)v->data; 1295e0385b85SDave May 129603a1d59fSDave May PetscFunctionBegin; 1297da07bb01SDave May if (!binary->setfromoptionscalled) { ierr = PetscViewerSetFromOptions(v);CHKERRQ(ierr); } 129803a1d59fSDave May 1299e0385b85SDave May #if defined(PETSC_HAVE_MPIIO) 1300bc196f7cSDave May if (binary->usempiio) { 1301e0385b85SDave May ierr = PetscViewerFileSetUp_BinaryMPIIO(v);CHKERRQ(ierr); 1302e0385b85SDave May } else { 1303e0385b85SDave May #endif 1304e0385b85SDave May ierr = PetscViewerFileSetUp_Binary(v);CHKERRQ(ierr); 1305e0385b85SDave May #if defined(PETSC_HAVE_MPIIO) 1306e0385b85SDave May } 1307e0385b85SDave May #endif 130803a1d59fSDave May PetscFunctionReturn(0); 130903a1d59fSDave May } 131003a1d59fSDave May 13114416b707SBarry Smith static PetscErrorCode PetscViewerSetFromOptions_Binary(PetscOptionItems *PetscOptionsObject,PetscViewer v) 131203a1d59fSDave May { 131303a1d59fSDave May PetscErrorCode ierr; 131403a1d59fSDave May PetscViewer_Binary *binary = (PetscViewer_Binary*)v->data; 1315d22fd6bcSDave May char defaultname[PETSC_MAX_PATH_LEN]; 131603a1d59fSDave May PetscBool flg; 1317e0385b85SDave May 131803a1d59fSDave May PetscFunctionBegin; 131903a1d59fSDave May ierr = PetscOptionsHead(PetscOptionsObject,"Binary PetscViewer Options");CHKERRQ(ierr); 1320d22fd6bcSDave May ierr = PetscSNPrintf(defaultname,PETSC_MAX_PATH_LEN-1,"binaryoutput");CHKERRQ(ierr); 1321d22fd6bcSDave May ierr = PetscOptionsString("-viewer_binary_filename","Specify filename","PetscViewerFileSetName",defaultname,defaultname,PETSC_MAX_PATH_LEN-1,&flg);CHKERRQ(ierr); 1322d22fd6bcSDave May if (flg) { ierr = PetscViewerFileSetName_Binary(v,defaultname);CHKERRQ(ierr); } 132334a9cc2cSBarry Smith ierr = PetscOptionsBool("-viewer_binary_skip_info","Skip writing/reading .info file","PetscViewerBinarySetSkipInfo",PETSC_FALSE,&binary->skipinfo,NULL);CHKERRQ(ierr); 132434a9cc2cSBarry Smith ierr = PetscOptionsBool("-viewer_binary_skip_options","Skip parsing vec load options","PetscViewerBinarySetSkipOptions",PETSC_TRUE,&binary->skipoptions,NULL);CHKERRQ(ierr); 132534a9cc2cSBarry Smith ierr = PetscOptionsBool("-viewer_binary_skip_header","Skip writing/reading header information","PetscViewerBinarySetSkipHeader",PETSC_FALSE,&binary->skipheader,NULL);CHKERRQ(ierr); 132603a1d59fSDave May #if defined(PETSC_HAVE_MPIIO) 132734a9cc2cSBarry Smith ierr = PetscOptionsBool("-viewer_binary_mpiio","Use MPI-IO functionality to write/read binary file","PetscViewerBinarySetUseMPIIO",PETSC_FALSE,&binary->usempiio,NULL);CHKERRQ(ierr); 132834a9cc2cSBarry Smith #elif defined(PETSC_HAVE_MPIUNI) 132934a9cc2cSBarry Smith ierr = PetscOptionsBool("-viewer_binary_mpiio","Use MPI-IO functionality to write/read binary file","PetscViewerBinarySetUseMPIIO",PETSC_FALSE,NULL,NULL);CHKERRQ(ierr); 133003a1d59fSDave May #endif 133103a1d59fSDave May ierr = PetscOptionsTail();CHKERRQ(ierr); 1332bc196f7cSDave May binary->setfromoptionscalled = PETSC_TRUE; 133303a1d59fSDave May PetscFunctionReturn(0); 133403a1d59fSDave May } 133503a1d59fSDave May 13368556b5ebSBarry Smith /*MC 13378556b5ebSBarry Smith PETSCVIEWERBINARY - A viewer that saves to binary files 13388556b5ebSBarry Smith 13398556b5ebSBarry Smith 13408556b5ebSBarry Smith .seealso: PetscViewerBinaryOpen(), PETSC_VIEWER_STDOUT_(),PETSC_VIEWER_STDOUT_SELF, PETSC_VIEWER_STDOUT_WORLD, PetscViewerCreate(), PetscViewerASCIIOpen(), 13418556b5ebSBarry Smith PetscViewerMatlabOpen(), VecView(), DMView(), PetscViewerMatlabPutArray(), PETSCVIEWERASCII, PETSCVIEWERMATLAB, PETSCVIEWERDRAW, 134267918a83SBarry Smith PetscViewerFileSetName(), PetscViewerFileSetMode(), PetscViewerFormat, PetscViewerType, PetscViewerSetType(), 134367918a83SBarry Smith PetscViewerBinaryGetUseMPIIO(), PetscViewerBinarySetUseMPIIO() 13448556b5ebSBarry Smith 13451b266c99SBarry Smith Level: beginner 13461b266c99SBarry Smith 13478556b5ebSBarry Smith M*/ 13488556b5ebSBarry Smith 13498cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscViewerCreate_Binary(PetscViewer v) 13505c6c1daeSBarry Smith { 13515c6c1daeSBarry Smith PetscErrorCode ierr; 13525c6c1daeSBarry Smith PetscViewer_Binary *vbinary; 13535c6c1daeSBarry Smith 13545c6c1daeSBarry Smith PetscFunctionBegin; 1355b00a9115SJed Brown ierr = PetscNewLog(v,&vbinary);CHKERRQ(ierr); 13565c6c1daeSBarry Smith v->data = (void*)vbinary; 135703a1d59fSDave May v->ops->setfromoptions = PetscViewerSetFromOptions_Binary; 13585c6c1daeSBarry Smith v->ops->destroy = PetscViewerDestroy_Binary; 13592bf49c77SBarry Smith v->ops->view = PetscViewerView_Binary; 1360c98fd787SBarry Smith v->ops->setup = PetscViewerSetUp_Binary; 1361c98fd787SBarry Smith v->ops->flush = NULL; 13625c6c1daeSBarry Smith vbinary->fdes_info = 0; 13635c6c1daeSBarry Smith vbinary->fdes = 0; 13645c6c1daeSBarry Smith vbinary->skipinfo = PETSC_FALSE; 13655c6c1daeSBarry Smith vbinary->skipoptions = PETSC_TRUE; 13665c6c1daeSBarry Smith vbinary->skipheader = PETSC_FALSE; 1367da07bb01SDave May vbinary->setfromoptionscalled = PETSC_FALSE; 1368559f443fSBarry Smith v->ops->getsubviewer = PetscViewerGetSubViewer_Binary; 1369559f443fSBarry Smith v->ops->restoresubviewer = PetscViewerRestoreSubViewer_Binary; 13701d641e7bSMichael Lange v->ops->read = PetscViewerBinaryRead; 13715c6c1daeSBarry Smith vbinary->btype = (PetscFileMode) -1; 13725c6c1daeSBarry Smith vbinary->storecompressed = PETSC_FALSE; 1373f90597f1SStefano Zampini vbinary->filename = NULL; 1374f90597f1SStefano Zampini vbinary->ogzfilename = NULL; 13755c6c1daeSBarry Smith vbinary->flowcontrol = 256; /* seems a good number for Cray XT-5 */ 13765c6c1daeSBarry Smith 1377bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetFlowControl_C",PetscViewerBinaryGetFlowControl_Binary);CHKERRQ(ierr); 1378bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetFlowControl_C",PetscViewerBinarySetFlowControl_Binary);CHKERRQ(ierr); 1379bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetSkipHeader_C",PetscViewerBinarySetSkipHeader_Binary);CHKERRQ(ierr); 1380bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetSkipHeader_C",PetscViewerBinaryGetSkipHeader_Binary);CHKERRQ(ierr); 1381807ea322SDave May ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetSkipOptions_C",PetscViewerBinaryGetSkipOptions_Binary);CHKERRQ(ierr); 1382807ea322SDave May ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetSkipOptions_C",PetscViewerBinarySetSkipOptions_Binary);CHKERRQ(ierr); 1383807ea322SDave May ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetSkipInfo_C",PetscViewerBinaryGetSkipInfo_Binary);CHKERRQ(ierr); 1384807ea322SDave May ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetSkipInfo_C",PetscViewerBinarySetSkipInfo_Binary);CHKERRQ(ierr); 1385bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetInfoPointer_C",PetscViewerBinaryGetInfoPointer_Binary);CHKERRQ(ierr); 1386bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetName_C",PetscViewerFileSetName_Binary);CHKERRQ(ierr); 1387bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetMode_C",PetscViewerFileSetMode_Binary);CHKERRQ(ierr); 1388bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileGetMode_C",PetscViewerFileGetMode_Binary);CHKERRQ(ierr); 1389bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileGetName_C",PetscViewerFileGetName_Binary);CHKERRQ(ierr); 13905c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO) 1391bc196f7cSDave May ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetUseMPIIO_C",PetscViewerBinaryGetUseMPIIO_Binary);CHKERRQ(ierr); 1392bc196f7cSDave May ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetUseMPIIO_C",PetscViewerBinarySetUseMPIIO_Binary);CHKERRQ(ierr); 13935c6c1daeSBarry Smith #endif 13945c6c1daeSBarry Smith PetscFunctionReturn(0); 13955c6c1daeSBarry Smith } 13965c6c1daeSBarry Smith 13975c6c1daeSBarry Smith /* ---------------------------------------------------------------------*/ 13985c6c1daeSBarry Smith /* 13995c6c1daeSBarry Smith The variable Petsc_Viewer_Binary_keyval is used to indicate an MPI attribute that 14005c6c1daeSBarry Smith is attached to a communicator, in this case the attribute is a PetscViewer. 14015c6c1daeSBarry Smith */ 1402d4c7638eSBarry Smith PetscMPIInt Petsc_Viewer_Binary_keyval = MPI_KEYVAL_INVALID; 14035c6c1daeSBarry Smith 14045c6c1daeSBarry Smith /*@C 14055c6c1daeSBarry Smith PETSC_VIEWER_BINARY_ - Creates a binary PetscViewer shared by all processors 14065c6c1daeSBarry Smith in a communicator. 14075c6c1daeSBarry Smith 14085c6c1daeSBarry Smith Collective on MPI_Comm 14095c6c1daeSBarry Smith 14105c6c1daeSBarry Smith Input Parameter: 14115c6c1daeSBarry Smith . comm - the MPI communicator to share the binary PetscViewer 14125c6c1daeSBarry Smith 14135c6c1daeSBarry Smith Level: intermediate 14145c6c1daeSBarry Smith 14155c6c1daeSBarry Smith Options Database Keys: 14165c6c1daeSBarry Smith + -viewer_binary_filename <name> 14175c6c1daeSBarry Smith . -viewer_binary_skip_info 1418a2d7db39SDave May . -viewer_binary_skip_options 1419a2d7db39SDave May . -viewer_binary_skip_header 1420a2d7db39SDave May - -viewer_binary_mpiio 14215c6c1daeSBarry Smith 14225c6c1daeSBarry Smith Environmental variables: 14235c6c1daeSBarry Smith - PETSC_VIEWER_BINARY_FILENAME 14245c6c1daeSBarry Smith 14255c6c1daeSBarry Smith Notes: 14265c6c1daeSBarry Smith Unlike almost all other PETSc routines, PETSC_VIEWER_BINARY_ does not return 14275c6c1daeSBarry Smith an error code. The binary PetscViewer is usually used in the form 14285c6c1daeSBarry Smith $ XXXView(XXX object,PETSC_VIEWER_BINARY_(comm)); 14295c6c1daeSBarry Smith 14305c6c1daeSBarry Smith .seealso: PETSC_VIEWER_BINARY_WORLD, PETSC_VIEWER_BINARY_SELF, PetscViewerBinaryOpen(), PetscViewerCreate(), 14315c6c1daeSBarry Smith PetscViewerDestroy() 14325c6c1daeSBarry Smith @*/ 14335c6c1daeSBarry Smith PetscViewer PETSC_VIEWER_BINARY_(MPI_Comm comm) 14345c6c1daeSBarry Smith { 14355c6c1daeSBarry Smith PetscErrorCode ierr; 14365c6c1daeSBarry Smith PetscBool flg; 14375c6c1daeSBarry Smith PetscViewer viewer; 14385c6c1daeSBarry Smith char fname[PETSC_MAX_PATH_LEN]; 14395c6c1daeSBarry Smith MPI_Comm ncomm; 14405c6c1daeSBarry Smith 14415c6c1daeSBarry Smith PetscFunctionBegin; 1442efca3c55SSatish 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);} 14435c6c1daeSBarry Smith if (Petsc_Viewer_Binary_keyval == MPI_KEYVAL_INVALID) { 144412801b39SBarry Smith ierr = MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN,MPI_COMM_NULL_DELETE_FN,&Petsc_Viewer_Binary_keyval,0); 1445efca3c55SSatish Balay if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);} 14465c6c1daeSBarry Smith } 144747435625SJed Brown ierr = MPI_Comm_get_attr(ncomm,Petsc_Viewer_Binary_keyval,(void**)&viewer,(int*)&flg); 1448efca3c55SSatish Balay if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);} 14495c6c1daeSBarry Smith if (!flg) { /* PetscViewer not yet created */ 14505c6c1daeSBarry Smith ierr = PetscOptionsGetenv(ncomm,"PETSC_VIEWER_BINARY_FILENAME",fname,PETSC_MAX_PATH_LEN,&flg); 1451efca3c55SSatish Balay if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);} 14525c6c1daeSBarry Smith if (!flg) { 14535c6c1daeSBarry Smith ierr = PetscStrcpy(fname,"binaryoutput"); 1454efca3c55SSatish Balay if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);} 14555c6c1daeSBarry Smith } 14565c6c1daeSBarry Smith ierr = PetscViewerBinaryOpen(ncomm,fname,FILE_MODE_WRITE,&viewer); 1457efca3c55SSatish Balay if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);} 14585c6c1daeSBarry Smith ierr = PetscObjectRegisterDestroy((PetscObject)viewer); 1459efca3c55SSatish Balay if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);} 146047435625SJed Brown ierr = MPI_Comm_set_attr(ncomm,Petsc_Viewer_Binary_keyval,(void*)viewer); 1461efca3c55SSatish Balay if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);} 14625c6c1daeSBarry Smith } 14635c6c1daeSBarry Smith ierr = PetscCommDestroy(&ncomm); 1464efca3c55SSatish Balay if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(0);} 14655c6c1daeSBarry Smith PetscFunctionReturn(viewer); 14665c6c1daeSBarry Smith } 1467