15c6c1daeSBarry Smith 2af0996ceSBarry Smith #include <petsc/private/viewerimpl.h> /*I "petscviewer.h" I*/ 35c6c1daeSBarry Smith #include <fcntl.h> 45c6c1daeSBarry Smith #if defined(PETSC_HAVE_UNISTD_H) 55c6c1daeSBarry Smith #include <unistd.h> 65c6c1daeSBarry Smith #endif 75c6c1daeSBarry Smith #if defined(PETSC_HAVE_IO_H) 85c6c1daeSBarry Smith #include <io.h> 95c6c1daeSBarry Smith #endif 105c6c1daeSBarry Smith 115c6c1daeSBarry Smith typedef struct { 125c6c1daeSBarry Smith int fdes; /* file descriptor, ignored if using MPI IO */ 135c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO) 14bc196f7cSDave May PetscBool usempiio; 155c6c1daeSBarry Smith MPI_File mfdes; /* ignored unless using MPI IO */ 16e8a65b7dSLisandro Dalcin MPI_File mfsub; /* subviewer support */ 175c6c1daeSBarry Smith MPI_Offset moff; 185c6c1daeSBarry Smith #endif 195c6c1daeSBarry Smith PetscFileMode btype; /* read or write? */ 205c6c1daeSBarry Smith FILE *fdes_info; /* optional file containing info on binary file*/ 215c6c1daeSBarry Smith PetscBool storecompressed; /* gzip the write binary file when closing it*/ 225c6c1daeSBarry Smith char *filename; 23f90597f1SStefano Zampini char *ogzfilename; /* gzip can be run after the filename has been updated */ 245c6c1daeSBarry Smith PetscBool skipinfo; /* Don't create info file for writing; don't use for reading */ 255c6c1daeSBarry Smith PetscBool skipoptions; /* don't use PETSc options database when loading */ 265c6c1daeSBarry Smith PetscInt flowcontrol; /* allow only <flowcontrol> messages outstanding at a time while doing IO */ 275c6c1daeSBarry Smith PetscBool skipheader; /* don't write header, only raw data */ 28a261c58fSBarry Smith PetscBool matlabheaderwritten; /* if format is PETSC_VIEWER_BINARY_MATLAB has the MATLAB .info header been written yet */ 29c98fd787SBarry Smith PetscBool setfromoptionscalled; 305c6c1daeSBarry Smith } PetscViewer_Binary; 315c6c1daeSBarry Smith 3281f0254dSBarry Smith static PetscErrorCode PetscViewerGetSubViewer_Binary(PetscViewer viewer,MPI_Comm comm,PetscViewer *outviewer) 335c6c1daeSBarry Smith { 345c6c1daeSBarry Smith int rank; 355c6c1daeSBarry Smith PetscErrorCode ierr; 36e8a65b7dSLisandro Dalcin PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 375c6c1daeSBarry Smith 385c6c1daeSBarry Smith PetscFunctionBegin; 39c98fd787SBarry Smith ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr); 40e8a65b7dSLisandro Dalcin 41e8a65b7dSLisandro Dalcin /* Return subviewer in process zero */ 42ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr); 435c6c1daeSBarry Smith if (!rank) { 44e5afcf28SBarry Smith PetscMPIInt flg; 45e5afcf28SBarry Smith 46e5afcf28SBarry Smith ierr = MPI_Comm_compare(PETSC_COMM_SELF,comm,&flg);CHKERRQ(ierr); 47e5afcf28SBarry Smith if (flg != MPI_IDENT && flg != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"PetscViewerGetSubViewer() for PETSCVIEWERBINARY requires a singleton MPI_Comm"); 48e5afcf28SBarry Smith ierr = PetscViewerCreate(comm,outviewer);CHKERRQ(ierr); 495c6c1daeSBarry Smith ierr = PetscViewerSetType(*outviewer,PETSCVIEWERBINARY);CHKERRQ(ierr); 50e8a65b7dSLisandro Dalcin ierr = PetscMemcpy((*outviewer)->data,vbinary,sizeof(PetscViewer_Binary));CHKERRQ(ierr); 5112f4c3a9SLisandro Dalcin (*outviewer)->setupcalled = PETSC_TRUE; 5296509d17SLisandro Dalcin } else { 5396509d17SLisandro Dalcin *outviewer = NULL; 5496509d17SLisandro Dalcin } 55e8a65b7dSLisandro Dalcin 56e8a65b7dSLisandro Dalcin #if defined(PETSC_HAVE_MPIIO) 57e8a65b7dSLisandro Dalcin if (vbinary->usempiio && *outviewer) { 58e8a65b7dSLisandro Dalcin PetscViewer_Binary *obinary = (PetscViewer_Binary*)(*outviewer)->data; 59e8a65b7dSLisandro Dalcin /* Parent viewer opens a new MPI file handle on PETSC_COMM_SELF and keeps track of it for future reuse */ 60e8a65b7dSLisandro Dalcin if (vbinary->mfsub == MPI_FILE_NULL) { 61e8a65b7dSLisandro Dalcin int amode; 62e8a65b7dSLisandro Dalcin switch (vbinary->btype) { 63e8a65b7dSLisandro Dalcin case FILE_MODE_READ: amode = MPI_MODE_RDONLY; break; 64e8a65b7dSLisandro Dalcin case FILE_MODE_WRITE: amode = MPI_MODE_WRONLY; break; 6522a8f86cSLisandro Dalcin case FILE_MODE_APPEND: amode = MPI_MODE_WRONLY; break; 66e8a65b7dSLisandro Dalcin default: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Unsupported file mode %s",PetscFileModes[vbinary->btype]); 67e8a65b7dSLisandro Dalcin } 68e8a65b7dSLisandro Dalcin ierr = MPI_File_open(PETSC_COMM_SELF,vbinary->filename,amode,MPI_INFO_NULL,&vbinary->mfsub);CHKERRQ(ierr); 69e8a65b7dSLisandro Dalcin } 70e8a65b7dSLisandro Dalcin /* Subviewer gets the MPI file handle on PETSC_COMM_SELF */ 71e8a65b7dSLisandro Dalcin obinary->mfdes = vbinary->mfsub; 72e8a65b7dSLisandro Dalcin obinary->mfsub = MPI_FILE_NULL; 73e8a65b7dSLisandro Dalcin obinary->moff = vbinary->moff; 74e8a65b7dSLisandro Dalcin } 75e8a65b7dSLisandro Dalcin #endif 765c6c1daeSBarry Smith PetscFunctionReturn(0); 775c6c1daeSBarry Smith } 785c6c1daeSBarry Smith 7981f0254dSBarry Smith static PetscErrorCode PetscViewerRestoreSubViewer_Binary(PetscViewer viewer,MPI_Comm comm,PetscViewer *outviewer) 805c6c1daeSBarry Smith { 81e5afcf28SBarry Smith PetscMPIInt rank; 82e8a65b7dSLisandro Dalcin PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 83e8a65b7dSLisandro Dalcin PetscErrorCode ierr; 84e8a65b7dSLisandro Dalcin #if defined(PETSC_HAVE_MPIIO) 85e8a65b7dSLisandro Dalcin MPI_Offset moff = 0; 86e8a65b7dSLisandro Dalcin #endif 875c6c1daeSBarry Smith 885c6c1daeSBarry Smith PetscFunctionBegin; 89ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr); 90e8a65b7dSLisandro Dalcin if (rank && *outviewer) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Subviewer not obtained from viewer"); 91e8a65b7dSLisandro Dalcin 92e8a65b7dSLisandro Dalcin #if defined(PETSC_HAVE_MPIIO) 93e8a65b7dSLisandro Dalcin if (vbinary->usempiio && *outviewer) { 94e8a65b7dSLisandro Dalcin PetscViewer_Binary *obinary = (PetscViewer_Binary*)(*outviewer)->data; 95e8a65b7dSLisandro Dalcin if (obinary->mfdes != vbinary->mfsub) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Subviewer not obtained from viewer"); 96e8a65b7dSLisandro Dalcin moff = obinary->moff; 97e8a65b7dSLisandro Dalcin } 98e8a65b7dSLisandro Dalcin #endif 99e8a65b7dSLisandro Dalcin 100e8a65b7dSLisandro Dalcin if (*outviewer) { 101e8a65b7dSLisandro Dalcin PetscViewer_Binary *obinary = (PetscViewer_Binary*)(*outviewer)->data; 102e8a65b7dSLisandro Dalcin if (obinary->fdes != vbinary->fdes) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Subviewer not obtained from viewer"); 1035c6c1daeSBarry Smith ierr = PetscFree((*outviewer)->data);CHKERRQ(ierr); 1045c6c1daeSBarry Smith ierr = PetscHeaderDestroy(outviewer);CHKERRQ(ierr); 1055c6c1daeSBarry Smith } 106e8a65b7dSLisandro Dalcin 107e8a65b7dSLisandro Dalcin #if defined(PETSC_HAVE_MPIIO) 108e8a65b7dSLisandro Dalcin if (vbinary->usempiio) { 109e8a65b7dSLisandro Dalcin PetscInt64 ioff = (PetscInt64)moff; /* We could use MPI_OFFSET datatype (requires MPI 2.2) */ 110e8a65b7dSLisandro Dalcin ierr = MPI_Bcast(&ioff,1,MPIU_INT64,0,PetscObjectComm((PetscObject)viewer));CHKERRQ(ierr); 111e8a65b7dSLisandro Dalcin vbinary->moff = (MPI_Offset)ioff; 112e8a65b7dSLisandro Dalcin } 113e8a65b7dSLisandro Dalcin #endif 1145c6c1daeSBarry Smith PetscFunctionReturn(0); 1155c6c1daeSBarry Smith } 1165c6c1daeSBarry Smith 1175c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO) 1185c6c1daeSBarry Smith /*@C 11922a8f86cSLisandro Dalcin PetscViewerBinaryGetMPIIOOffset - Gets the current global offset that should be passed to MPI_File_set_view() or MPI_File_{write|read}_at[_all]() 1205c6c1daeSBarry Smith 1215c6c1daeSBarry Smith Not Collective 1225c6c1daeSBarry Smith 1235c6c1daeSBarry Smith Input Parameter: 1245c6c1daeSBarry Smith . viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 1255c6c1daeSBarry Smith 1265c6c1daeSBarry Smith Output Parameter: 12722a8f86cSLisandro Dalcin . off - the current global offset 1285c6c1daeSBarry Smith 1295c6c1daeSBarry Smith Level: advanced 1305c6c1daeSBarry Smith 1315c6c1daeSBarry Smith Fortran Note: 1325c6c1daeSBarry Smith This routine is not supported in Fortran. 1335c6c1daeSBarry Smith 1345c6c1daeSBarry Smith Use PetscViewerBinaryAddMPIIOOffset() to increase this value after you have written a view. 1355c6c1daeSBarry Smith 1365c6c1daeSBarry Smith 13767918a83SBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetInfoPointer(), PetscViewerBinaryGetUseMPIIO(), PetscViewerBinarySetUseMPIIO(), PetscViewerBinaryAddMPIIOOffset() 1385c6c1daeSBarry Smith @*/ 1395c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetMPIIOOffset(PetscViewer viewer,MPI_Offset *off) 1405c6c1daeSBarry Smith { 14122a8f86cSLisandro Dalcin PetscViewer_Binary *vbinary; 1425c6c1daeSBarry Smith 1435c6c1daeSBarry Smith PetscFunctionBegin; 14422a8f86cSLisandro Dalcin PetscValidHeaderSpecificType(viewer,PETSC_VIEWER_CLASSID,1,PETSCVIEWERBINARY); 14522a8f86cSLisandro Dalcin PetscValidPointer(off,2); 14622a8f86cSLisandro Dalcin vbinary = (PetscViewer_Binary*)viewer->data; 1475c6c1daeSBarry Smith *off = vbinary->moff; 1485c6c1daeSBarry Smith PetscFunctionReturn(0); 1495c6c1daeSBarry Smith } 1505c6c1daeSBarry Smith 1515c6c1daeSBarry Smith /*@C 15222a8f86cSLisandro Dalcin PetscViewerBinaryAddMPIIOOffset - Adds to the current global offset 1535c6c1daeSBarry Smith 15422a8f86cSLisandro Dalcin Logically Collective 1555c6c1daeSBarry Smith 1565c6c1daeSBarry Smith Input Parameters: 1575c6c1daeSBarry Smith + viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 15822a8f86cSLisandro Dalcin - off - the addition to the global offset 1595c6c1daeSBarry Smith 1605c6c1daeSBarry Smith Level: advanced 1615c6c1daeSBarry Smith 1625c6c1daeSBarry Smith Fortran Note: 1635c6c1daeSBarry Smith This routine is not supported in Fortran. 1645c6c1daeSBarry Smith 16522a8f86cSLisandro Dalcin Use PetscViewerBinaryGetMPIIOOffset() to get the value that you should pass to MPI_File_set_view() or MPI_File_{write|read}_at[_all]() 1665c6c1daeSBarry Smith 1675c6c1daeSBarry Smith 16867918a83SBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer(), PetscViewerBinaryGetUseMPIIO(), PetscViewerBinarySetUseMPIIO(), PetscViewerBinaryGetMPIIOOffset() 1695c6c1daeSBarry Smith @*/ 1705c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryAddMPIIOOffset(PetscViewer viewer,MPI_Offset off) 1715c6c1daeSBarry Smith { 17222a8f86cSLisandro Dalcin PetscViewer_Binary *vbinary; 1735c6c1daeSBarry Smith 1745c6c1daeSBarry Smith PetscFunctionBegin; 17522a8f86cSLisandro Dalcin PetscValidHeaderSpecificType(viewer,PETSC_VIEWER_CLASSID,1,PETSCVIEWERBINARY); 17622a8f86cSLisandro Dalcin PetscValidLogicalCollectiveInt(viewer,(PetscInt)off,2); 17722a8f86cSLisandro Dalcin vbinary = (PetscViewer_Binary*)viewer->data; 1785c6c1daeSBarry Smith vbinary->moff += off; 1795c6c1daeSBarry Smith PetscFunctionReturn(0); 1805c6c1daeSBarry Smith } 1815c6c1daeSBarry Smith 1825c6c1daeSBarry Smith /*@C 1835c6c1daeSBarry Smith PetscViewerBinaryGetMPIIODescriptor - Extracts the MPI IO file descriptor from a PetscViewer. 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: 1915c6c1daeSBarry Smith . fdes - file descriptor 1925c6c1daeSBarry Smith 1935c6c1daeSBarry Smith Level: advanced 1945c6c1daeSBarry Smith 1955c6c1daeSBarry Smith Fortran Note: 1965c6c1daeSBarry Smith This routine is not supported in Fortran. 1975c6c1daeSBarry Smith 1985c6c1daeSBarry Smith 19967918a83SBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer(), PetscViewerBinaryGetUseMPIIO(), PetscViewerBinarySetUseMPIIO(), PetscViewerBinaryGetMPIIOOffset() 2005c6c1daeSBarry Smith @*/ 2015c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetMPIIODescriptor(PetscViewer viewer,MPI_File *fdes) 2025c6c1daeSBarry Smith { 20303a1d59fSDave May PetscErrorCode ierr; 20422a8f86cSLisandro Dalcin PetscViewer_Binary *vbinary; 2055c6c1daeSBarry Smith 2065c6c1daeSBarry Smith PetscFunctionBegin; 20722a8f86cSLisandro Dalcin PetscValidHeaderSpecificType(viewer,PETSC_VIEWER_CLASSID,1,PETSCVIEWERBINARY); 20822a8f86cSLisandro Dalcin PetscValidPointer(fdes,2); 209c98fd787SBarry Smith ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr); 21022a8f86cSLisandro Dalcin vbinary = (PetscViewer_Binary*)viewer->data; 2115c6c1daeSBarry Smith *fdes = vbinary->mfdes; 2125c6c1daeSBarry Smith PetscFunctionReturn(0); 2135c6c1daeSBarry Smith } 2145c6c1daeSBarry Smith 21581f0254dSBarry Smith static PetscErrorCode PetscViewerBinaryGetUseMPIIO_Binary(PetscViewer viewer,PetscBool *flg) 216a8aae444SDave May { 217a8aae444SDave May PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 218a8aae444SDave May 219a8aae444SDave May PetscFunctionBegin; 220bc196f7cSDave May *flg = vbinary->usempiio; 221a8aae444SDave May PetscFunctionReturn(0); 222a8aae444SDave May } 223a8aae444SDave May #endif 224a8aae444SDave May 225a8aae444SDave May 2265c6c1daeSBarry Smith /*@C 227bc196f7cSDave May PetscViewerBinaryGetUseMPIIO - Returns PETSC_TRUE if the binary viewer uses MPI-IO. 2285c6c1daeSBarry Smith 2295c6c1daeSBarry Smith Not Collective 2305c6c1daeSBarry Smith 2315c6c1daeSBarry Smith Input Parameter: 2325c6c1daeSBarry Smith . viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 2335c6c1daeSBarry Smith 2345c6c1daeSBarry Smith Output Parameter: 235bc196f7cSDave May - flg - PETSC_TRUE if MPI-IO is being used 2365c6c1daeSBarry Smith 2375c6c1daeSBarry Smith Options Database: 2385c6c1daeSBarry Smith -viewer_binary_mpiio : Flag for using MPI-IO 2395c6c1daeSBarry Smith 2405c6c1daeSBarry Smith Level: advanced 2415c6c1daeSBarry Smith 242bc196f7cSDave May Note: 243bc196f7cSDave May If MPI-IO is not available, this function will always return PETSC_FALSE 244bc196f7cSDave May 2455c6c1daeSBarry Smith Fortran Note: 2465c6c1daeSBarry Smith This routine is not supported in Fortran. 2475c6c1daeSBarry Smith 2485c6c1daeSBarry Smith 24967918a83SBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetInfoPointer(), PetscViewerBinarySetUseMPIIO(), PetscViewerBinaryGetMPIIOOffset() 2505c6c1daeSBarry Smith @*/ 251bc196f7cSDave May PetscErrorCode PetscViewerBinaryGetUseMPIIO(PetscViewer viewer,PetscBool *flg) 2525c6c1daeSBarry Smith { 253a8aae444SDave May PetscErrorCode ierr; 2545c6c1daeSBarry Smith 2555c6c1daeSBarry Smith PetscFunctionBegin; 256a8aae444SDave May *flg = PETSC_FALSE; 257bc196f7cSDave May ierr = PetscTryMethod(viewer,"PetscViewerBinaryGetUseMPIIO_C",(PetscViewer,PetscBool*),(viewer,flg));CHKERRQ(ierr); 2585c6c1daeSBarry Smith PetscFunctionReturn(0); 2595c6c1daeSBarry Smith } 2605c6c1daeSBarry Smith 26181f0254dSBarry Smith static PetscErrorCode PetscViewerBinaryGetFlowControl_Binary(PetscViewer viewer,PetscInt *fc) 2625c6c1daeSBarry Smith { 2635c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 2645c6c1daeSBarry Smith 2655c6c1daeSBarry Smith PetscFunctionBegin; 2665c6c1daeSBarry Smith *fc = vbinary->flowcontrol; 2675c6c1daeSBarry Smith PetscFunctionReturn(0); 2685c6c1daeSBarry Smith } 2695c6c1daeSBarry Smith 2705c6c1daeSBarry Smith /*@C 2715c6c1daeSBarry Smith PetscViewerBinaryGetFlowControl - Returns how many messages are allowed to outstanding at the same time during parallel IO reads/writes 2725c6c1daeSBarry Smith 2735c6c1daeSBarry Smith Not Collective 2745c6c1daeSBarry Smith 2755c6c1daeSBarry Smith Input Parameter: 2765c6c1daeSBarry Smith . viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 2775c6c1daeSBarry Smith 2785c6c1daeSBarry Smith Output Parameter: 2795c6c1daeSBarry Smith . fc - the number of messages 2805c6c1daeSBarry Smith 2815c6c1daeSBarry Smith Level: advanced 2825c6c1daeSBarry Smith 2835c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer(), PetscViewerBinarySetFlowControl() 2845c6c1daeSBarry Smith 2855c6c1daeSBarry Smith @*/ 2865c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetFlowControl(PetscViewer viewer,PetscInt *fc) 2875c6c1daeSBarry Smith { 2885c6c1daeSBarry Smith PetscErrorCode ierr; 2895c6c1daeSBarry Smith 2905c6c1daeSBarry Smith PetscFunctionBegin; 291163d334eSBarry Smith ierr = PetscUseMethod(viewer,"PetscViewerBinaryGetFlowControl_C",(PetscViewer,PetscInt*),(viewer,fc));CHKERRQ(ierr); 2925c6c1daeSBarry Smith PetscFunctionReturn(0); 2935c6c1daeSBarry Smith } 2945c6c1daeSBarry Smith 29581f0254dSBarry Smith static PetscErrorCode PetscViewerBinarySetFlowControl_Binary(PetscViewer viewer,PetscInt fc) 2965c6c1daeSBarry Smith { 2975c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 2985c6c1daeSBarry Smith 2995c6c1daeSBarry Smith PetscFunctionBegin; 300ce94432eSBarry Smith if (fc <= 1) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ARG_OUTOFRANGE,"Flow control count must be greater than 1, %D was set",fc); 3015c6c1daeSBarry Smith vbinary->flowcontrol = fc; 3025c6c1daeSBarry Smith PetscFunctionReturn(0); 3035c6c1daeSBarry Smith } 3045c6c1daeSBarry Smith 3055c6c1daeSBarry Smith /*@C 306639ff905SBarry Smith PetscViewerBinarySetFlowControl - Sets how many messages are allowed to outstanding at the same time during parallel IO reads/writes 3075c6c1daeSBarry Smith 3085c6c1daeSBarry Smith Not Collective 3095c6c1daeSBarry Smith 3105c6c1daeSBarry Smith Input Parameter: 3115c6c1daeSBarry Smith + viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 312639ff905SBarry Smith - fc - the number of messages, defaults to 256 if this function was not called 3135c6c1daeSBarry Smith 3145c6c1daeSBarry Smith Level: advanced 3155c6c1daeSBarry Smith 3165c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer(), PetscViewerBinaryGetFlowControl() 3175c6c1daeSBarry Smith 3185c6c1daeSBarry Smith @*/ 3195c6c1daeSBarry Smith PetscErrorCode PetscViewerBinarySetFlowControl(PetscViewer viewer,PetscInt fc) 3205c6c1daeSBarry Smith { 3215c6c1daeSBarry Smith PetscErrorCode ierr; 3225c6c1daeSBarry Smith 3235c6c1daeSBarry Smith PetscFunctionBegin; 3245c6c1daeSBarry Smith ierr = PetscUseMethod(viewer,"PetscViewerBinarySetFlowControl_C",(PetscViewer,PetscInt),(viewer,fc));CHKERRQ(ierr); 3255c6c1daeSBarry Smith PetscFunctionReturn(0); 3265c6c1daeSBarry Smith } 3275c6c1daeSBarry Smith 3285c6c1daeSBarry Smith /*@C 3295c6c1daeSBarry Smith PetscViewerBinaryGetDescriptor - Extracts the file descriptor from a PetscViewer. 3305c6c1daeSBarry Smith 3315872f025SBarry Smith Collective On PetscViewer 3325c6c1daeSBarry Smith 3335c6c1daeSBarry Smith Input Parameter: 3345c6c1daeSBarry Smith . viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 3355c6c1daeSBarry Smith 3365c6c1daeSBarry Smith Output Parameter: 3375c6c1daeSBarry Smith . fdes - file descriptor 3385c6c1daeSBarry Smith 3395c6c1daeSBarry Smith Level: advanced 3405c6c1daeSBarry Smith 3415c6c1daeSBarry Smith Notes: 3425c6c1daeSBarry Smith For writable binary PetscViewers, the descriptor will only be valid for the 3435c6c1daeSBarry Smith first processor in the communicator that shares the PetscViewer. For readable 3445c6c1daeSBarry Smith files it will only be valid on nodes that have the file. If node 0 does not 3455c6c1daeSBarry Smith have the file it generates an error even if another node does have the file. 3465c6c1daeSBarry Smith 3475c6c1daeSBarry Smith Fortran Note: 3485c6c1daeSBarry Smith This routine is not supported in Fortran. 3495c6c1daeSBarry Smith 35095452b02SPatrick Sanan Developer Notes: 35195452b02SPatrick Sanan This must be called on all processes because Dave May changed 3525872f025SBarry Smith the source code that this may be trigger a PetscViewerSetUp() call if it was not previously triggered. 3535872f025SBarry Smith 3545872f025SBarry Smith 3555c6c1daeSBarry Smith 3565c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetInfoPointer() 3575c6c1daeSBarry Smith @*/ 3585c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetDescriptor(PetscViewer viewer,int *fdes) 3595c6c1daeSBarry Smith { 36003a1d59fSDave May PetscErrorCode ierr; 36122a8f86cSLisandro Dalcin PetscViewer_Binary *vbinary; 3625c6c1daeSBarry Smith 3635c6c1daeSBarry Smith PetscFunctionBegin; 36422a8f86cSLisandro Dalcin PetscValidHeaderSpecificType(viewer,PETSC_VIEWER_CLASSID,1,PETSCVIEWERBINARY); 36522a8f86cSLisandro Dalcin PetscValidPointer(fdes,2); 366c98fd787SBarry Smith ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr); 36722a8f86cSLisandro Dalcin vbinary = (PetscViewer_Binary*)viewer->data; 3685c6c1daeSBarry Smith *fdes = vbinary->fdes; 3695c6c1daeSBarry Smith PetscFunctionReturn(0); 3705c6c1daeSBarry Smith } 3715c6c1daeSBarry Smith 3725c6c1daeSBarry Smith /*@ 3735c6c1daeSBarry Smith PetscViewerBinarySkipInfo - Binary file will not have .info file created with it 3745c6c1daeSBarry Smith 3755c6c1daeSBarry Smith Not Collective 3765c6c1daeSBarry Smith 377fd292e60Sprj- Input Parameter: 3785c6c1daeSBarry Smith . viewer - PetscViewer context, obtained from PetscViewerCreate() 3795c6c1daeSBarry Smith 3805c6c1daeSBarry Smith Options Database Key: 3815c6c1daeSBarry Smith . -viewer_binary_skip_info 3825c6c1daeSBarry Smith 3835c6c1daeSBarry Smith Level: advanced 3845c6c1daeSBarry Smith 38595452b02SPatrick Sanan Notes: 38695452b02SPatrick Sanan This must be called after PetscViewerSetType(). If you use PetscViewerBinaryOpen() then 3875c6c1daeSBarry Smith you can only skip the info file with the -viewer_binary_skip_info flag. To use the function you must open the 388a2d7db39SDave May viewer with PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinarySkipInfo(). 3895c6c1daeSBarry Smith 3905c6c1daeSBarry Smith The .info contains meta information about the data in the binary file, for example the block size if it was 3915c6c1daeSBarry Smith set for a vector or matrix. 3925c6c1daeSBarry Smith 3935c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySetSkipOptions(), 394807ea322SDave May PetscViewerBinaryGetSkipOptions(), PetscViewerBinaryGetSkipInfo() 3955c6c1daeSBarry Smith @*/ 3965c6c1daeSBarry Smith PetscErrorCode PetscViewerBinarySkipInfo(PetscViewer viewer) 3975c6c1daeSBarry Smith { 39822a8f86cSLisandro Dalcin PetscViewer_Binary *vbinary; 3995c6c1daeSBarry Smith 4005c6c1daeSBarry Smith PetscFunctionBegin; 40122a8f86cSLisandro Dalcin PetscValidHeaderSpecificType(viewer,PETSC_VIEWER_CLASSID,1,PETSCVIEWERBINARY); 40222a8f86cSLisandro Dalcin vbinary = (PetscViewer_Binary*)viewer->data; 4035c6c1daeSBarry Smith vbinary->skipinfo = PETSC_TRUE; 4045c6c1daeSBarry Smith PetscFunctionReturn(0); 4055c6c1daeSBarry Smith } 4065c6c1daeSBarry Smith 40781f0254dSBarry Smith static PetscErrorCode PetscViewerBinarySetSkipInfo_Binary(PetscViewer viewer,PetscBool skip) 408807ea322SDave May { 409807ea322SDave May PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 410807ea322SDave May 411807ea322SDave May PetscFunctionBegin; 412807ea322SDave May vbinary->skipinfo = skip; 413807ea322SDave May PetscFunctionReturn(0); 414807ea322SDave May } 415807ea322SDave May 416807ea322SDave May /*@ 417807ea322SDave May PetscViewerBinarySetSkipInfo - Binary file will not have .info file created with it 418807ea322SDave May 419807ea322SDave May Not Collective 420807ea322SDave May 421fd292e60Sprj- Input Parameter: 422807ea322SDave May . viewer - PetscViewer context, obtained from PetscViewerCreate() 423807ea322SDave May 424807ea322SDave May Options Database Key: 425807ea322SDave May . -viewer_binary_skip_info 426807ea322SDave May 427807ea322SDave May Level: advanced 428807ea322SDave May 429807ea322SDave May .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySetSkipOptions(), 430807ea322SDave May PetscViewerBinaryGetSkipOptions(), PetscViewerBinaryGetSkipInfo() 431807ea322SDave May @*/ 432807ea322SDave May PetscErrorCode PetscViewerBinarySetSkipInfo(PetscViewer viewer,PetscBool skip) 433807ea322SDave May { 434807ea322SDave May PetscErrorCode ierr; 435807ea322SDave May 436807ea322SDave May PetscFunctionBegin; 437807ea322SDave May ierr = PetscUseMethod(viewer,"PetscViewerBinarySetSkipInfo_C",(PetscViewer,PetscBool),(viewer,skip));CHKERRQ(ierr); 438807ea322SDave May PetscFunctionReturn(0); 439807ea322SDave May } 440807ea322SDave May 44181f0254dSBarry Smith static PetscErrorCode PetscViewerBinaryGetSkipInfo_Binary(PetscViewer viewer,PetscBool *skip) 442807ea322SDave May { 443807ea322SDave May PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 444807ea322SDave May 445807ea322SDave May PetscFunctionBegin; 446807ea322SDave May *skip = vbinary->skipinfo; 447807ea322SDave May PetscFunctionReturn(0); 448807ea322SDave May } 449807ea322SDave May 450807ea322SDave May /*@ 451807ea322SDave May PetscViewerBinaryGetSkipInfo - check if viewer wrote a .info file 452807ea322SDave May 453807ea322SDave May Not Collective 454807ea322SDave May 455807ea322SDave May Input Parameter: 456807ea322SDave May . viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 457807ea322SDave May 458807ea322SDave May Output Parameter: 459807ea322SDave May . skip - PETSC_TRUE implies the .info file was not generated 460807ea322SDave May 461807ea322SDave May Level: advanced 462807ea322SDave May 46395452b02SPatrick Sanan Notes: 46495452b02SPatrick Sanan This must be called after PetscViewerSetType() 465807ea322SDave May 466807ea322SDave May .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(), 467807ea322SDave May PetscViewerBinarySetSkipOptions(), PetscViewerBinarySetSkipInfo() 468807ea322SDave May @*/ 469807ea322SDave May PetscErrorCode PetscViewerBinaryGetSkipInfo(PetscViewer viewer,PetscBool *skip) 470807ea322SDave May { 471807ea322SDave May PetscErrorCode ierr; 472807ea322SDave May 473807ea322SDave May PetscFunctionBegin; 474807ea322SDave May ierr = PetscUseMethod(viewer,"PetscViewerBinaryGetSkipInfo_C",(PetscViewer,PetscBool*),(viewer,skip));CHKERRQ(ierr); 475807ea322SDave May PetscFunctionReturn(0); 476807ea322SDave May } 477807ea322SDave May 47881f0254dSBarry Smith static PetscErrorCode PetscViewerBinarySetSkipOptions_Binary(PetscViewer viewer,PetscBool skip) 479807ea322SDave May { 480807ea322SDave May PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 481807ea322SDave May 482807ea322SDave May PetscFunctionBegin; 483807ea322SDave May vbinary->skipoptions = skip; 484807ea322SDave May PetscFunctionReturn(0); 485807ea322SDave May } 486807ea322SDave May 4875c6c1daeSBarry Smith /*@ 4885c6c1daeSBarry Smith PetscViewerBinarySetSkipOptions - do not use the PETSc options database when loading objects 4895c6c1daeSBarry Smith 4905c6c1daeSBarry Smith Not Collective 4915c6c1daeSBarry Smith 4925c6c1daeSBarry Smith Input Parameters: 4935c6c1daeSBarry Smith + viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 4945c6c1daeSBarry Smith - skip - PETSC_TRUE means do not use 4955c6c1daeSBarry Smith 4965c6c1daeSBarry Smith Options Database Key: 4975c6c1daeSBarry Smith . -viewer_binary_skip_options 4985c6c1daeSBarry Smith 4995c6c1daeSBarry Smith Level: advanced 5005c6c1daeSBarry Smith 50195452b02SPatrick Sanan Notes: 50295452b02SPatrick Sanan This must be called after PetscViewerSetType() 5035c6c1daeSBarry Smith 5045c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(), 5055c6c1daeSBarry Smith PetscViewerBinaryGetSkipOptions() 5065c6c1daeSBarry Smith @*/ 5075c6c1daeSBarry Smith PetscErrorCode PetscViewerBinarySetSkipOptions(PetscViewer viewer,PetscBool skip) 5085c6c1daeSBarry Smith { 509807ea322SDave May PetscErrorCode ierr; 5105c6c1daeSBarry Smith 5115c6c1daeSBarry Smith PetscFunctionBegin; 512807ea322SDave May ierr = PetscUseMethod(viewer,"PetscViewerBinarySetSkipOptions_C",(PetscViewer,PetscBool),(viewer,skip));CHKERRQ(ierr); 513807ea322SDave May PetscFunctionReturn(0); 514807ea322SDave May } 515807ea322SDave May 51681f0254dSBarry Smith static PetscErrorCode PetscViewerBinaryGetSkipOptions_Binary(PetscViewer viewer,PetscBool *skip) 517807ea322SDave May { 51822a8f86cSLisandro Dalcin PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data;; 519807ea322SDave May 520807ea322SDave May PetscFunctionBegin; 521807ea322SDave May *skip = vbinary->skipoptions; 5225c6c1daeSBarry Smith PetscFunctionReturn(0); 5235c6c1daeSBarry Smith } 5245c6c1daeSBarry Smith 5255c6c1daeSBarry Smith /*@ 5265c6c1daeSBarry Smith PetscViewerBinaryGetSkipOptions - checks if viewer uses the PETSc options database when loading objects 5275c6c1daeSBarry Smith 5285c6c1daeSBarry Smith Not Collective 5295c6c1daeSBarry Smith 5305c6c1daeSBarry Smith Input Parameter: 5315c6c1daeSBarry Smith . viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 5325c6c1daeSBarry Smith 5335c6c1daeSBarry Smith Output Parameter: 5345c6c1daeSBarry Smith . skip - PETSC_TRUE means do not use 5355c6c1daeSBarry Smith 5365c6c1daeSBarry Smith Level: advanced 5375c6c1daeSBarry Smith 53895452b02SPatrick Sanan Notes: 53995452b02SPatrick Sanan This must be called after PetscViewerSetType() 5405c6c1daeSBarry Smith 5415c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(), 5425c6c1daeSBarry Smith PetscViewerBinarySetSkipOptions() 5435c6c1daeSBarry Smith @*/ 5445c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetSkipOptions(PetscViewer viewer,PetscBool *skip) 5455c6c1daeSBarry Smith { 546807ea322SDave May PetscErrorCode ierr; 5475c6c1daeSBarry Smith 5485c6c1daeSBarry Smith PetscFunctionBegin; 549807ea322SDave May ierr = PetscUseMethod(viewer,"PetscViewerBinaryGetSkipOptions_C",(PetscViewer,PetscBool*),(viewer,skip));CHKERRQ(ierr); 5505c6c1daeSBarry Smith PetscFunctionReturn(0); 5515c6c1daeSBarry Smith } 5525c6c1daeSBarry Smith 55381f0254dSBarry Smith static PetscErrorCode PetscViewerBinarySetSkipHeader_Binary(PetscViewer viewer,PetscBool skip) 5545c6c1daeSBarry Smith { 5555c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 5565c6c1daeSBarry Smith 5575c6c1daeSBarry Smith PetscFunctionBegin; 5585c6c1daeSBarry Smith vbinary->skipheader = skip; 5595c6c1daeSBarry Smith PetscFunctionReturn(0); 5605c6c1daeSBarry Smith } 5615c6c1daeSBarry Smith 5625c6c1daeSBarry Smith /*@ 5635c6c1daeSBarry Smith PetscViewerBinarySetSkipHeader - do not write a header with size information on output, just raw data 5645c6c1daeSBarry Smith 5655c6c1daeSBarry Smith Not Collective 5665c6c1daeSBarry Smith 5675c6c1daeSBarry Smith Input Parameters: 5685c6c1daeSBarry Smith + viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 5695c6c1daeSBarry Smith - skip - PETSC_TRUE means do not write header 5705c6c1daeSBarry Smith 5715c6c1daeSBarry Smith Options Database Key: 5725c6c1daeSBarry Smith . -viewer_binary_skip_header 5735c6c1daeSBarry Smith 5745c6c1daeSBarry Smith Level: advanced 5755c6c1daeSBarry Smith 57695452b02SPatrick Sanan Notes: 57795452b02SPatrick Sanan This must be called after PetscViewerSetType() 5785c6c1daeSBarry Smith 5795c6c1daeSBarry Smith Can ONLY be called on a binary viewer 5805c6c1daeSBarry Smith 5815c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(), 5825c6c1daeSBarry Smith PetscViewerBinaryGetSkipHeader() 5835c6c1daeSBarry Smith @*/ 5845c6c1daeSBarry Smith PetscErrorCode PetscViewerBinarySetSkipHeader(PetscViewer viewer,PetscBool skip) 5855c6c1daeSBarry Smith { 5865c6c1daeSBarry Smith PetscErrorCode ierr; 5875c6c1daeSBarry Smith 5885c6c1daeSBarry Smith PetscFunctionBegin; 5895c6c1daeSBarry Smith ierr = PetscUseMethod(viewer,"PetscViewerBinarySetSkipHeader_C",(PetscViewer,PetscBool),(viewer,skip));CHKERRQ(ierr); 5905c6c1daeSBarry Smith PetscFunctionReturn(0); 5915c6c1daeSBarry Smith } 5925c6c1daeSBarry Smith 59381f0254dSBarry Smith static PetscErrorCode PetscViewerBinaryGetSkipHeader_Binary(PetscViewer viewer,PetscBool *skip) 5945c6c1daeSBarry Smith { 5955c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 5965c6c1daeSBarry Smith 5975c6c1daeSBarry Smith PetscFunctionBegin; 5985c6c1daeSBarry Smith *skip = vbinary->skipheader; 5995c6c1daeSBarry Smith PetscFunctionReturn(0); 6005c6c1daeSBarry Smith } 6015c6c1daeSBarry Smith 6025c6c1daeSBarry Smith /*@ 6035c6c1daeSBarry Smith PetscViewerBinaryGetSkipHeader - checks whether to write a header with size information on output, or just raw data 6045c6c1daeSBarry Smith 6055c6c1daeSBarry Smith Not Collective 6065c6c1daeSBarry Smith 6075c6c1daeSBarry Smith Input Parameter: 6085c6c1daeSBarry Smith . viewer - PetscViewer context, obtained from PetscViewerBinaryOpen() 6095c6c1daeSBarry Smith 6105c6c1daeSBarry Smith Output Parameter: 6115c6c1daeSBarry Smith . skip - PETSC_TRUE means do not write header 6125c6c1daeSBarry Smith 6135c6c1daeSBarry Smith Level: advanced 6145c6c1daeSBarry Smith 61595452b02SPatrick Sanan Notes: 61695452b02SPatrick Sanan This must be called after PetscViewerSetType() 6175c6c1daeSBarry Smith 6185c6c1daeSBarry Smith Returns false for PETSCSOCKETVIEWER, you cannot skip the header for it. 6195c6c1daeSBarry Smith 6205c6c1daeSBarry Smith .seealso: PetscViewerBinaryOpen(), PetscViewerBinaryGetDescriptor(), PetscViewerBinarySkipInfo(), 6215c6c1daeSBarry Smith PetscViewerBinarySetSkipHeader() 6225c6c1daeSBarry Smith @*/ 6235c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetSkipHeader(PetscViewer viewer,PetscBool *skip) 6245c6c1daeSBarry Smith { 6255c6c1daeSBarry Smith PetscErrorCode ierr; 6265c6c1daeSBarry Smith 6275c6c1daeSBarry Smith PetscFunctionBegin; 6285c6c1daeSBarry Smith *skip = PETSC_FALSE; 629163d334eSBarry Smith ierr = PetscUseMethod(viewer,"PetscViewerBinaryGetSkipHeader_C",(PetscViewer,PetscBool*),(viewer,skip));CHKERRQ(ierr); 6305c6c1daeSBarry Smith PetscFunctionReturn(0); 6315c6c1daeSBarry Smith } 6325c6c1daeSBarry Smith 63381f0254dSBarry Smith static PetscErrorCode PetscViewerBinaryGetInfoPointer_Binary(PetscViewer viewer,FILE **file) 6345c6c1daeSBarry Smith { 635f3b211e4SSatish Balay PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 636a261c58fSBarry Smith PetscErrorCode ierr; 637a261c58fSBarry Smith MPI_Comm comm; 6385c6c1daeSBarry Smith 6395c6c1daeSBarry Smith PetscFunctionBegin; 640c98fd787SBarry Smith ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr); 6415c6c1daeSBarry Smith *file = vbinary->fdes_info; 642a261c58fSBarry Smith if (viewer->format == PETSC_VIEWER_BINARY_MATLAB && !vbinary->matlabheaderwritten) { 643a261c58fSBarry Smith vbinary->matlabheaderwritten = PETSC_TRUE; 644a261c58fSBarry Smith ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr); 645da88d4d4SJed Brown ierr = PetscFPrintf(comm,*file,"#--- begin code written by PetscViewerBinary for MATLAB format ---#\n");CHKERRQ(ierr); 646da88d4d4SJed Brown ierr = PetscFPrintf(comm,*file,"#$$ Set.filename = '%s';\n",vbinary->filename);CHKERRQ(ierr); 647da88d4d4SJed Brown ierr = PetscFPrintf(comm,*file,"#$$ fd = PetscOpenFile(Set.filename);\n");CHKERRQ(ierr); 648da88d4d4SJed Brown ierr = PetscFPrintf(comm,*file,"#--- end code written by PetscViewerBinary for MATLAB format ---#\n\n");CHKERRQ(ierr); 649a261c58fSBarry Smith } 6505c6c1daeSBarry Smith PetscFunctionReturn(0); 6515c6c1daeSBarry Smith } 6525c6c1daeSBarry Smith 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 .seealso: PetscViewerBinaryOpen(),PetscViewerBinaryGetDescriptor() 6755c6c1daeSBarry Smith @*/ 6765c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryGetInfoPointer(PetscViewer viewer,FILE **file) 6775c6c1daeSBarry Smith { 6785c6c1daeSBarry Smith PetscErrorCode ierr; 6795c6c1daeSBarry Smith 6805c6c1daeSBarry Smith PetscFunctionBegin; 6810298fd71SBarry Smith *file = NULL; 6825c6c1daeSBarry Smith ierr = PetscTryMethod(viewer,"PetscViewerBinaryGetInfoPointer_C",(PetscViewer,FILE **),(viewer,file));CHKERRQ(ierr); 6835c6c1daeSBarry Smith PetscFunctionReturn(0); 6845c6c1daeSBarry Smith } 6855c6c1daeSBarry Smith 6865c6c1daeSBarry Smith static PetscErrorCode PetscViewerFileClose_Binary(PetscViewer v) 6875c6c1daeSBarry Smith { 6885c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data; 6895c6c1daeSBarry Smith PetscErrorCode ierr; 6905c6c1daeSBarry Smith PetscMPIInt rank; 6915c6c1daeSBarry Smith int err; 6925c6c1daeSBarry Smith 6935c6c1daeSBarry Smith PetscFunctionBegin; 694ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)v),&rank);CHKERRQ(ierr); 6955c6c1daeSBarry Smith if ((!rank || vbinary->btype == FILE_MODE_READ) && vbinary->fdes) { 6965c6c1daeSBarry Smith close(vbinary->fdes); 6975c6c1daeSBarry Smith if (!rank && vbinary->storecompressed) { 6985c6c1daeSBarry Smith char par[PETSC_MAX_PATH_LEN],buf[PETSC_MAX_PATH_LEN]; 6995c6c1daeSBarry Smith FILE *fp; 7005c6c1daeSBarry Smith /* compress the file */ 701a126751eSBarry Smith ierr = PetscStrncpy(par,"gzip -f ",sizeof(par));CHKERRQ(ierr); 702a126751eSBarry Smith ierr = PetscStrlcat(par,vbinary->ogzfilename ? vbinary->ogzfilename : vbinary->filename,sizeof(par));CHKERRQ(ierr); 703f90597f1SStefano Zampini ierr = PetscFree(vbinary->ogzfilename);CHKERRQ(ierr); 7045c6c1daeSBarry Smith #if defined(PETSC_HAVE_POPEN) 7050298fd71SBarry Smith ierr = PetscPOpen(PETSC_COMM_SELF,NULL,par,"r",&fp);CHKERRQ(ierr); 7065c6c1daeSBarry Smith if (fgets(buf,1024,fp)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_LIB,"Error from command %s\n%s",par,buf); 707016831caSBarry Smith ierr = PetscPClose(PETSC_COMM_SELF,fp);CHKERRQ(ierr); 7085c6c1daeSBarry Smith #else 7095c6c1daeSBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine"); 7105c6c1daeSBarry Smith #endif 7115c6c1daeSBarry Smith } 7125c6c1daeSBarry Smith } 7135c6c1daeSBarry Smith if (vbinary->fdes_info) { 7145c6c1daeSBarry Smith err = fclose(vbinary->fdes_info); 7155c6c1daeSBarry Smith if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file"); 7165c6c1daeSBarry Smith } 7175c6c1daeSBarry Smith PetscFunctionReturn(0); 7185c6c1daeSBarry Smith } 7195c6c1daeSBarry Smith 720e0385b85SDave May #if defined(PETSC_HAVE_MPIIO) 721e0385b85SDave May static PetscErrorCode PetscViewerFileClose_BinaryMPIIO(PetscViewer v) 722e0385b85SDave May { 723e0385b85SDave May PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data; 724e0385b85SDave May int err; 725e0385b85SDave May PetscErrorCode ierr; 726e0385b85SDave May 727e0385b85SDave May PetscFunctionBegin; 728e8a65b7dSLisandro Dalcin if (vbinary->mfdes != MPI_FILE_NULL) { 729e0385b85SDave May ierr = MPI_File_close(&vbinary->mfdes);CHKERRQ(ierr); 730e0385b85SDave May } 731e8a65b7dSLisandro Dalcin if (vbinary->mfsub != MPI_FILE_NULL) { 732e8a65b7dSLisandro Dalcin ierr = MPI_File_close(&vbinary->mfsub);CHKERRQ(ierr); 733e8a65b7dSLisandro Dalcin } 734e0385b85SDave May if (vbinary->fdes_info) { 735e0385b85SDave May err = fclose(vbinary->fdes_info); 736e0385b85SDave May if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file"); 737e0385b85SDave May } 738e0385b85SDave May PetscFunctionReturn(0); 739e0385b85SDave May } 740e0385b85SDave May #endif 741e0385b85SDave May 74281f0254dSBarry Smith static PetscErrorCode PetscViewerDestroy_Binary(PetscViewer v) 7435c6c1daeSBarry Smith { 7445c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data; 7455c6c1daeSBarry Smith PetscErrorCode ierr; 7465c6c1daeSBarry Smith 7475c6c1daeSBarry Smith PetscFunctionBegin; 748a261c58fSBarry Smith if (v->format == PETSC_VIEWER_BINARY_MATLAB) { 749a261c58fSBarry Smith MPI_Comm comm; 750a261c58fSBarry Smith FILE *info; 751a261c58fSBarry Smith 752a261c58fSBarry Smith ierr = PetscObjectGetComm((PetscObject)v,&comm);CHKERRQ(ierr); 753a261c58fSBarry Smith ierr = PetscViewerBinaryGetInfoPointer(v,&info);CHKERRQ(ierr); 754da88d4d4SJed Brown ierr = PetscFPrintf(comm,info,"#--- begin code written by PetscViewerBinary for MATLAB format ---#\n");CHKERRQ(ierr); 755da88d4d4SJed Brown ierr = PetscFPrintf(comm,info,"#$$ close(fd);\n");CHKERRQ(ierr); 756da88d4d4SJed Brown ierr = PetscFPrintf(comm,info,"#--- end code written by PetscViewerBinary for MATLAB format ---#\n\n");CHKERRQ(ierr); 757a261c58fSBarry Smith } 7585c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO) 759bc196f7cSDave May if (vbinary->usempiio) { 760e0385b85SDave May ierr = PetscViewerFileClose_BinaryMPIIO(v);CHKERRQ(ierr); 761e0385b85SDave May } else { 762e0385b85SDave May #endif 763e0385b85SDave May ierr = PetscViewerFileClose_Binary(v);CHKERRQ(ierr); 764e0385b85SDave May #if defined(PETSC_HAVE_MPIIO) 7655c6c1daeSBarry Smith } 7665c6c1daeSBarry Smith #endif 767f90597f1SStefano Zampini ierr = PetscFree(vbinary->filename);CHKERRQ(ierr); 768f90597f1SStefano Zampini ierr = PetscFree(vbinary->ogzfilename);CHKERRQ(ierr); 769e0385b85SDave May ierr = PetscFree(vbinary);CHKERRQ(ierr); 77022a8f86cSLisandro Dalcin 77122a8f86cSLisandro Dalcin ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetFlowControl_C",NULL);CHKERRQ(ierr); 77222a8f86cSLisandro Dalcin ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetFlowControl_C",NULL);CHKERRQ(ierr); 77322a8f86cSLisandro Dalcin ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetSkipHeader_C",NULL);CHKERRQ(ierr); 77422a8f86cSLisandro Dalcin ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetSkipHeader_C",NULL);CHKERRQ(ierr); 77522a8f86cSLisandro Dalcin ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetSkipOptions_C",NULL);CHKERRQ(ierr); 77622a8f86cSLisandro Dalcin ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetSkipOptions_C",NULL);CHKERRQ(ierr); 77722a8f86cSLisandro Dalcin ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetSkipInfo_C",NULL);CHKERRQ(ierr); 77822a8f86cSLisandro Dalcin ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetSkipInfo_C",NULL);CHKERRQ(ierr); 77922a8f86cSLisandro Dalcin ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetInfoPointer_C",NULL);CHKERRQ(ierr); 78022a8f86cSLisandro Dalcin ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetName_C",NULL);CHKERRQ(ierr); 78122a8f86cSLisandro Dalcin ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetMode_C",NULL);CHKERRQ(ierr); 78222a8f86cSLisandro Dalcin ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileGetMode_C",NULL);CHKERRQ(ierr); 78322a8f86cSLisandro Dalcin ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileGetName_C",NULL);CHKERRQ(ierr); 78422a8f86cSLisandro Dalcin #if defined(PETSC_HAVE_MPIIO) 78522a8f86cSLisandro Dalcin ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetUseMPIIO_C",NULL);CHKERRQ(ierr); 78622a8f86cSLisandro Dalcin ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetUseMPIIO_C",NULL);CHKERRQ(ierr); 78722a8f86cSLisandro Dalcin #endif 788e0385b85SDave May PetscFunctionReturn(0); 789e0385b85SDave May } 7905c6c1daeSBarry Smith 7915c6c1daeSBarry Smith /*@C 7925c6c1daeSBarry Smith PetscViewerBinaryOpen - Opens a file for binary input/output. 7935c6c1daeSBarry Smith 794d083f849SBarry Smith Collective 7955c6c1daeSBarry Smith 7965c6c1daeSBarry Smith Input Parameters: 7975c6c1daeSBarry Smith + comm - MPI communicator 7985c6c1daeSBarry Smith . name - name of file 7995c6c1daeSBarry Smith - type - type of file 8005c6c1daeSBarry Smith $ FILE_MODE_WRITE - create new file for binary output 8015c6c1daeSBarry Smith $ FILE_MODE_READ - open existing file for binary input 8025c6c1daeSBarry Smith $ FILE_MODE_APPEND - open existing file for binary output 8035c6c1daeSBarry Smith 8045c6c1daeSBarry Smith Output Parameter: 8055c6c1daeSBarry Smith . binv - PetscViewer for binary input/output to use with the specified file 8065c6c1daeSBarry Smith 8075c6c1daeSBarry Smith Options Database Keys: 80863c55180SPatrick Sanan + -viewer_binary_filename <name> - 80963c55180SPatrick Sanan . -viewer_binary_skip_info - 81063c55180SPatrick Sanan . -viewer_binary_skip_options - 81163c55180SPatrick Sanan . -viewer_binary_skip_header - 81263c55180SPatrick Sanan - -viewer_binary_mpiio - 8135c6c1daeSBarry Smith 8145c6c1daeSBarry Smith Level: beginner 8155c6c1daeSBarry Smith 8165c6c1daeSBarry Smith Note: 8175c6c1daeSBarry Smith This PetscViewer should be destroyed with PetscViewerDestroy(). 8185c6c1daeSBarry Smith 8195c6c1daeSBarry Smith For reading files, the filename may begin with ftp:// or http:// and/or 8205c6c1daeSBarry Smith end with .gz; in this case file is brought over and uncompressed. 8215c6c1daeSBarry Smith 8225c6c1daeSBarry Smith For creating files, if the file name ends with .gz it is automatically 8235c6c1daeSBarry Smith compressed when closed. 8245c6c1daeSBarry Smith 8255c6c1daeSBarry Smith For writing files it only opens the file on processor 0 in the communicator. 8265c6c1daeSBarry Smith For readable files it opens the file on all nodes that have the file. If 8275c6c1daeSBarry Smith node 0 does not have the file it generates an error even if other nodes 8285c6c1daeSBarry Smith do have the file. 8295c6c1daeSBarry Smith 8306a9046bcSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(), 8315c6c1daeSBarry Smith VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(), 83267918a83SBarry Smith PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscViewerBinaryRead(), PetscViewerBinarySetUseMPIIO(), 83367918a83SBarry Smith PetscViewerBinaryGetUseMPIIO(), PetscViewerBinaryGetMPIIOOffset() 8345c6c1daeSBarry Smith @*/ 8355c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryOpen(MPI_Comm comm,const char name[],PetscFileMode type,PetscViewer *binv) 8365c6c1daeSBarry Smith { 8375c6c1daeSBarry Smith PetscErrorCode ierr; 8385c6c1daeSBarry Smith 8395c6c1daeSBarry Smith PetscFunctionBegin; 8405c6c1daeSBarry Smith ierr = PetscViewerCreate(comm,binv);CHKERRQ(ierr); 8415c6c1daeSBarry Smith ierr = PetscViewerSetType(*binv,PETSCVIEWERBINARY);CHKERRQ(ierr); 8425c6c1daeSBarry Smith ierr = PetscViewerFileSetMode(*binv,type);CHKERRQ(ierr); 8435c6c1daeSBarry Smith ierr = PetscViewerFileSetName(*binv,name);CHKERRQ(ierr); 84403a1d59fSDave May ierr = PetscViewerSetFromOptions(*binv);CHKERRQ(ierr); 8455c6c1daeSBarry Smith PetscFunctionReturn(0); 8465c6c1daeSBarry Smith } 8475c6c1daeSBarry Smith 8485c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO) 849060da220SMatthew G. Knepley static PetscErrorCode PetscViewerBinaryWriteReadMPIIO(PetscViewer viewer,void *data,PetscInt num,PetscInt *count,PetscDataType dtype,PetscBool write) 8505c6c1daeSBarry Smith { 85122a8f86cSLisandro Dalcin MPI_Comm comm = PetscObjectComm((PetscObject)viewer); 8525c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 85322a8f86cSLisandro Dalcin MPI_File mfdes = vbinary->mfdes; 8545c6c1daeSBarry Smith PetscErrorCode ierr; 8555c6c1daeSBarry Smith MPI_Datatype mdtype; 85669e10bbaSLisandro Dalcin PetscMPIInt rank,cnt; 8575c6c1daeSBarry Smith MPI_Status status; 8585c6c1daeSBarry Smith MPI_Aint ul,dsize; 8595c6c1daeSBarry Smith 8605c6c1daeSBarry Smith PetscFunctionBegin; 86122a8f86cSLisandro Dalcin ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 862060da220SMatthew G. Knepley ierr = PetscMPIIntCast(num,&cnt);CHKERRQ(ierr); 8635c6c1daeSBarry Smith ierr = PetscDataTypeToMPIDataType(dtype,&mdtype);CHKERRQ(ierr); 8645c6c1daeSBarry Smith if (write) { 86569e10bbaSLisandro Dalcin if (!rank) { 86669e10bbaSLisandro Dalcin ierr = MPIU_File_write_at(mfdes,vbinary->moff,data,cnt,mdtype,&status);CHKERRQ(ierr); 86769e10bbaSLisandro Dalcin } 8685c6c1daeSBarry Smith } else { 86969e10bbaSLisandro Dalcin if (!rank) { 87069e10bbaSLisandro Dalcin ierr = MPIU_File_read_at(mfdes,vbinary->moff,data,cnt,mdtype,&status);CHKERRQ(ierr); 8719860990eSLisandro Dalcin if (cnt > 0) {ierr = MPI_Get_count(&status,mdtype,&cnt);CHKERRQ(ierr);} 8725c6c1daeSBarry Smith } 87322a8f86cSLisandro Dalcin ierr = MPI_Bcast(&cnt,1,MPI_INT,0,comm);CHKERRQ(ierr); 87422a8f86cSLisandro Dalcin ierr = MPI_Bcast(data,cnt,mdtype,0,comm);CHKERRQ(ierr); 87569e10bbaSLisandro Dalcin } 8765c6c1daeSBarry Smith ierr = MPI_Type_get_extent(mdtype,&ul,&dsize);CHKERRQ(ierr); 8775c6c1daeSBarry Smith vbinary->moff += dsize*cnt; 8789860990eSLisandro Dalcin if (count) *count = cnt; 8795c6c1daeSBarry Smith PetscFunctionReturn(0); 8805c6c1daeSBarry Smith } 8815c6c1daeSBarry Smith #endif 8825c6c1daeSBarry Smith 8835c6c1daeSBarry Smith /*@C 8845c6c1daeSBarry Smith PetscViewerBinaryRead - Reads from a binary file, all processors get the same result 8855c6c1daeSBarry Smith 886d083f849SBarry Smith Collective 8875c6c1daeSBarry Smith 8885c6c1daeSBarry Smith Input Parameters: 8895c6c1daeSBarry Smith + viewer - the binary viewer 890907376e6SBarry Smith . data - location of the data to be written 891060da220SMatthew G. Knepley . num - number of items of data to read 892907376e6SBarry Smith - dtype - type of data to read 8935c6c1daeSBarry Smith 894f8e4bde8SMatthew G. Knepley Output Parameters: 895*5972f5f3SLisandro Dalcin . count - number of items of data actually read, or NULL. 896f8e4bde8SMatthew G. Knepley 8975c6c1daeSBarry Smith Level: beginner 8985c6c1daeSBarry Smith 8996a9046bcSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(), 9005c6c1daeSBarry Smith VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(), 9015c6c1daeSBarry Smith PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead() 9025c6c1daeSBarry Smith @*/ 903060da220SMatthew G. Knepley PetscErrorCode PetscViewerBinaryRead(PetscViewer viewer,void *data,PetscInt num,PetscInt *count,PetscDataType dtype) 9045c6c1daeSBarry Smith { 9055c6c1daeSBarry Smith PetscErrorCode ierr; 90622a8f86cSLisandro Dalcin PetscViewer_Binary *vbinary; 9075c6c1daeSBarry Smith 90822a8f86cSLisandro Dalcin PetscFunctionBegin; 90922a8f86cSLisandro Dalcin PetscValidHeaderSpecificType(viewer,PETSC_VIEWER_CLASSID,1,PETSCVIEWERBINARY); 91022a8f86cSLisandro Dalcin PetscValidLogicalCollectiveInt(viewer,num,3); 911c98fd787SBarry Smith ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr); 91222a8f86cSLisandro Dalcin vbinary = (PetscViewer_Binary*)viewer->data; 9135c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO) 914bc196f7cSDave May if (vbinary->usempiio) { 915060da220SMatthew G. Knepley ierr = PetscViewerBinaryWriteReadMPIIO(viewer,data,num,count,dtype,PETSC_FALSE);CHKERRQ(ierr); 9165c6c1daeSBarry Smith } else { 9175c6c1daeSBarry Smith #endif 9189860990eSLisandro Dalcin ierr = PetscBinarySynchronizedRead(PetscObjectComm((PetscObject)viewer),vbinary->fdes,data,num,count,dtype);CHKERRQ(ierr); 9195c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO) 9205c6c1daeSBarry Smith } 9215c6c1daeSBarry Smith #endif 9225c6c1daeSBarry Smith PetscFunctionReturn(0); 9235c6c1daeSBarry Smith } 9245c6c1daeSBarry Smith 9255c6c1daeSBarry Smith /*@C 9265c6c1daeSBarry Smith PetscViewerBinaryWrite - writes to a binary file, only from the first process 9275c6c1daeSBarry Smith 928d083f849SBarry Smith Collective 9295c6c1daeSBarry Smith 9305c6c1daeSBarry Smith Input Parameters: 9315c6c1daeSBarry Smith + viewer - the binary viewer 9325c6c1daeSBarry Smith . data - location of data 9335824c9d0SPatrick Sanan . count - number of items of data to write 934f253e43cSLisandro Dalcin - dtype - type of data to write 9355c6c1daeSBarry Smith 9365c6c1daeSBarry Smith Level: beginner 9375c6c1daeSBarry Smith 9386a9046bcSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(), 9395c6c1daeSBarry Smith VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(), PetscDataType 9405c6c1daeSBarry Smith PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead() 9415c6c1daeSBarry Smith @*/ 942f253e43cSLisandro Dalcin PetscErrorCode PetscViewerBinaryWrite(PetscViewer viewer,const void *data,PetscInt count,PetscDataType dtype) 9435c6c1daeSBarry Smith { 9445c6c1daeSBarry Smith PetscErrorCode ierr; 94522a8f86cSLisandro Dalcin PetscViewer_Binary *vbinary; 9465c6c1daeSBarry Smith 9475c6c1daeSBarry Smith PetscFunctionBegin; 94822a8f86cSLisandro Dalcin PetscValidHeaderSpecificType(viewer,PETSC_VIEWER_CLASSID,1,PETSCVIEWERBINARY); 94922a8f86cSLisandro Dalcin PetscValidLogicalCollectiveInt(viewer,count,3); 950c98fd787SBarry Smith ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr); 95122a8f86cSLisandro Dalcin vbinary = (PetscViewer_Binary*)viewer->data; 9525c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO) 953bc196f7cSDave May if (vbinary->usempiio) { 954f253e43cSLisandro Dalcin ierr = PetscViewerBinaryWriteReadMPIIO(viewer,(void*)data,count,NULL,dtype,PETSC_TRUE);CHKERRQ(ierr); 9555c6c1daeSBarry Smith } else { 9565c6c1daeSBarry Smith #endif 957f253e43cSLisandro Dalcin ierr = PetscBinarySynchronizedWrite(PetscObjectComm((PetscObject)viewer),vbinary->fdes,data,count,dtype);CHKERRQ(ierr); 9585c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO) 9595c6c1daeSBarry Smith } 9605c6c1daeSBarry Smith #endif 9615c6c1daeSBarry Smith PetscFunctionReturn(0); 9625c6c1daeSBarry Smith } 9635c6c1daeSBarry Smith 964*5972f5f3SLisandro Dalcin static PetscErrorCode PetscViewerBinaryWriteReadAll(PetscViewer viewer,PetscBool write,void *data,PetscInt count,PetscInt start,PetscInt total,PetscDataType dtype) 965*5972f5f3SLisandro Dalcin { 966*5972f5f3SLisandro Dalcin MPI_Comm comm = PetscObjectComm((PetscObject)viewer); 967*5972f5f3SLisandro Dalcin PetscMPIInt size,rank; 968*5972f5f3SLisandro Dalcin MPI_Datatype mdtype; 969*5972f5f3SLisandro Dalcin MPI_Aint lb,dsize; 970*5972f5f3SLisandro Dalcin PetscBool useMPIIO; 971*5972f5f3SLisandro Dalcin PetscErrorCode ierr; 972*5972f5f3SLisandro Dalcin 973*5972f5f3SLisandro Dalcin PetscFunctionBegin; 974*5972f5f3SLisandro Dalcin PetscValidHeaderSpecificType(viewer,PETSC_VIEWER_CLASSID,1,PETSCVIEWERBINARY); 975*5972f5f3SLisandro Dalcin PetscValidLogicalCollectiveBool(viewer,((start>=0)||(start==PETSC_DETERMINE)),4); 976*5972f5f3SLisandro Dalcin PetscValidLogicalCollectiveBool(viewer,((total>=0)||(total==PETSC_DETERMINE)),5); 977*5972f5f3SLisandro Dalcin PetscValidLogicalCollectiveInt(viewer,total,5); 978*5972f5f3SLisandro Dalcin ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr); 979*5972f5f3SLisandro Dalcin 980*5972f5f3SLisandro Dalcin ierr = PetscDataTypeToMPIDataType(dtype,&mdtype);CHKERRQ(ierr); 981*5972f5f3SLisandro Dalcin ierr = MPI_Type_get_extent(mdtype,&lb,&dsize);CHKERRQ(ierr); 982*5972f5f3SLisandro Dalcin ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 983*5972f5f3SLisandro Dalcin ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 984*5972f5f3SLisandro Dalcin 985*5972f5f3SLisandro Dalcin ierr = PetscViewerBinaryGetUseMPIIO(viewer,&useMPIIO);CHKERRQ(ierr); 986*5972f5f3SLisandro Dalcin #if defined(PETSC_HAVE_MPIIO) 987*5972f5f3SLisandro Dalcin if (useMPIIO) { 988*5972f5f3SLisandro Dalcin MPI_File mfdes; 989*5972f5f3SLisandro Dalcin MPI_Offset off; 990*5972f5f3SLisandro Dalcin PetscMPIInt cnt; 991*5972f5f3SLisandro Dalcin 992*5972f5f3SLisandro Dalcin if (start == PETSC_DETERMINE) { 993*5972f5f3SLisandro Dalcin ierr = MPI_Scan(&count,&start,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 994*5972f5f3SLisandro Dalcin start -= count; 995*5972f5f3SLisandro Dalcin } 996*5972f5f3SLisandro Dalcin if (total == PETSC_DETERMINE) { 997*5972f5f3SLisandro Dalcin total = start + count; 998*5972f5f3SLisandro Dalcin ierr = MPI_Bcast(&total,1,MPIU_INT,size-1,comm);CHKERRQ(ierr); 999*5972f5f3SLisandro Dalcin } 1000*5972f5f3SLisandro Dalcin ierr = PetscMPIIntCast(count,&cnt);CHKERRQ(ierr); 1001*5972f5f3SLisandro Dalcin ierr = PetscViewerBinaryGetMPIIODescriptor(viewer,&mfdes);CHKERRQ(ierr); 1002*5972f5f3SLisandro Dalcin ierr = PetscViewerBinaryGetMPIIOOffset(viewer,&off);CHKERRQ(ierr); 1003*5972f5f3SLisandro Dalcin off += (MPI_Offset)(start*dsize); 1004*5972f5f3SLisandro Dalcin if (write) { 1005*5972f5f3SLisandro Dalcin ierr = MPIU_File_write_at_all(mfdes,off,data,cnt,mdtype,MPI_STATUS_IGNORE);CHKERRQ(ierr); 1006*5972f5f3SLisandro Dalcin } else { 1007*5972f5f3SLisandro Dalcin ierr = MPIU_File_read_at_all(mfdes,off,data,cnt,mdtype,MPI_STATUS_IGNORE);CHKERRQ(ierr); 1008*5972f5f3SLisandro Dalcin } 1009*5972f5f3SLisandro Dalcin off = (MPI_Offset)(total*dsize); 1010*5972f5f3SLisandro Dalcin ierr = PetscViewerBinaryAddMPIIOOffset(viewer,off);CHKERRQ(ierr); 1011*5972f5f3SLisandro Dalcin PetscFunctionReturn(0); 1012*5972f5f3SLisandro Dalcin } 1013*5972f5f3SLisandro Dalcin #endif 1014*5972f5f3SLisandro Dalcin { 1015*5972f5f3SLisandro Dalcin int fdes; 1016*5972f5f3SLisandro Dalcin char *workbuf = NULL; 1017*5972f5f3SLisandro Dalcin PetscInt maxcount=0,message_count,flowcontrolcount; 1018*5972f5f3SLisandro Dalcin PetscMPIInt tag,cnt,maxcnt,scnt=0,rcnt=0,j; 1019*5972f5f3SLisandro Dalcin MPI_Status status; 1020*5972f5f3SLisandro Dalcin 1021*5972f5f3SLisandro Dalcin ierr = PetscCommGetNewTag(comm,&tag);CHKERRQ(ierr); 1022*5972f5f3SLisandro Dalcin ierr = MPI_Reduce(&count,&maxcount,1,MPIU_INT,MPI_MAX,0,comm);CHKERRQ(ierr); 1023*5972f5f3SLisandro Dalcin ierr = PetscMPIIntCast(maxcount,&maxcnt);CHKERRQ(ierr); 1024*5972f5f3SLisandro Dalcin ierr = PetscMPIIntCast(count,&cnt);CHKERRQ(ierr); 1025*5972f5f3SLisandro Dalcin 1026*5972f5f3SLisandro Dalcin ierr = PetscViewerBinaryGetDescriptor(viewer,&fdes);CHKERRQ(ierr); 1027*5972f5f3SLisandro Dalcin ierr = PetscViewerFlowControlStart(viewer,&message_count,&flowcontrolcount);CHKERRQ(ierr); 1028*5972f5f3SLisandro Dalcin if (!rank) { 1029*5972f5f3SLisandro Dalcin ierr = PetscMalloc(maxcnt*dsize,&workbuf);CHKERRQ(ierr); 1030*5972f5f3SLisandro Dalcin if (write) { 1031*5972f5f3SLisandro Dalcin ierr = PetscBinaryWrite(fdes,data,cnt,dtype);CHKERRQ(ierr); 1032*5972f5f3SLisandro Dalcin } else { 1033*5972f5f3SLisandro Dalcin ierr = PetscBinaryRead(fdes,data,cnt,NULL,dtype);CHKERRQ(ierr); 1034*5972f5f3SLisandro Dalcin } 1035*5972f5f3SLisandro Dalcin for (j=1; j<size; j++) { 1036*5972f5f3SLisandro Dalcin ierr = PetscViewerFlowControlStepMaster(viewer,j,&message_count,flowcontrolcount);CHKERRQ(ierr); 1037*5972f5f3SLisandro Dalcin if (write) { 1038*5972f5f3SLisandro Dalcin ierr = MPI_Recv(workbuf,maxcnt,mdtype,j,tag,comm,&status);CHKERRQ(ierr); 1039*5972f5f3SLisandro Dalcin ierr = MPI_Get_count(&status,mdtype,&rcnt);CHKERRQ(ierr); 1040*5972f5f3SLisandro Dalcin ierr = PetscBinaryWrite(fdes,workbuf,rcnt,dtype);CHKERRQ(ierr); 1041*5972f5f3SLisandro Dalcin } else { 1042*5972f5f3SLisandro Dalcin ierr = MPI_Recv(&scnt,1,MPI_INT,j,tag,comm,MPI_STATUS_IGNORE);CHKERRQ(ierr); 1043*5972f5f3SLisandro Dalcin ierr = PetscBinaryRead(fdes,workbuf,scnt,NULL,dtype);CHKERRQ(ierr); 1044*5972f5f3SLisandro Dalcin ierr = MPI_Send(workbuf,scnt,mdtype,j,tag,comm);CHKERRQ(ierr); 1045*5972f5f3SLisandro Dalcin } 1046*5972f5f3SLisandro Dalcin } 1047*5972f5f3SLisandro Dalcin ierr = PetscFree(workbuf);CHKERRQ(ierr); 1048*5972f5f3SLisandro Dalcin ierr = PetscViewerFlowControlEndMaster(viewer,&message_count);CHKERRQ(ierr); 1049*5972f5f3SLisandro Dalcin } else { 1050*5972f5f3SLisandro Dalcin ierr = PetscViewerFlowControlStepWorker(viewer,rank,&message_count);CHKERRQ(ierr); 1051*5972f5f3SLisandro Dalcin if (write) { 1052*5972f5f3SLisandro Dalcin ierr = MPI_Send(data,cnt,mdtype,0,tag,comm);CHKERRQ(ierr); 1053*5972f5f3SLisandro Dalcin } else { 1054*5972f5f3SLisandro Dalcin ierr = MPI_Send(&cnt,1,MPI_INT,0,tag,comm);CHKERRQ(ierr); 1055*5972f5f3SLisandro Dalcin ierr = MPI_Recv(data,cnt,mdtype,0,tag,comm,MPI_STATUS_IGNORE);CHKERRQ(ierr); 1056*5972f5f3SLisandro Dalcin } 1057*5972f5f3SLisandro Dalcin ierr = PetscViewerFlowControlEndWorker(viewer,&message_count);CHKERRQ(ierr); 1058*5972f5f3SLisandro Dalcin } 1059*5972f5f3SLisandro Dalcin } 1060*5972f5f3SLisandro Dalcin PetscFunctionReturn(0); 1061*5972f5f3SLisandro Dalcin } 1062*5972f5f3SLisandro Dalcin 1063*5972f5f3SLisandro Dalcin 1064*5972f5f3SLisandro Dalcin /*@C 1065*5972f5f3SLisandro Dalcin PetscViewerBinaryReadAll - reads from a binary file from all processes 1066*5972f5f3SLisandro Dalcin 1067*5972f5f3SLisandro Dalcin Collective 1068*5972f5f3SLisandro Dalcin 1069*5972f5f3SLisandro Dalcin Input Parameters: 1070*5972f5f3SLisandro Dalcin + viewer - the binary viewer 1071*5972f5f3SLisandro Dalcin . data - location of data 1072*5972f5f3SLisandro Dalcin . count - local number of items of data to read 1073*5972f5f3SLisandro Dalcin . start - local start, can be PETSC_DETERMINE 1074*5972f5f3SLisandro Dalcin . total - global number of items of data to read, can be PETSC_DETERMINE 1075*5972f5f3SLisandro Dalcin - dtype - type of data to read 1076*5972f5f3SLisandro Dalcin 1077*5972f5f3SLisandro Dalcin Level: advanced 1078*5972f5f3SLisandro Dalcin 1079*5972f5f3SLisandro Dalcin .seealso: PetscViewerBinaryOpen(), PetscViewerBinarySetUseMPIIO(), PetscBinaryViewerRead(), PetscBinaryViewerWriteAll() 1080*5972f5f3SLisandro Dalcin @*/ 1081*5972f5f3SLisandro Dalcin PetscErrorCode PetscViewerBinaryReadAll(PetscViewer viewer,void *data,PetscInt count,PetscInt start,PetscInt total,PetscDataType dtype) 1082*5972f5f3SLisandro Dalcin { 1083*5972f5f3SLisandro Dalcin PetscErrorCode ierr; 1084*5972f5f3SLisandro Dalcin PetscFunctionBegin; 1085*5972f5f3SLisandro Dalcin ierr = PetscViewerBinaryWriteReadAll(viewer,PETSC_FALSE,data,count,start,total,dtype);CHKERRQ(ierr); 1086*5972f5f3SLisandro Dalcin PetscFunctionReturn(0); 1087*5972f5f3SLisandro Dalcin } 1088*5972f5f3SLisandro Dalcin 1089*5972f5f3SLisandro Dalcin /*@C 1090*5972f5f3SLisandro Dalcin PetscViewerBinaryWriteAll - writes to a binary file from all processes 1091*5972f5f3SLisandro Dalcin 1092*5972f5f3SLisandro Dalcin Collective 1093*5972f5f3SLisandro Dalcin 1094*5972f5f3SLisandro Dalcin Input Parameters: 1095*5972f5f3SLisandro Dalcin + viewer - the binary viewer 1096*5972f5f3SLisandro Dalcin . data - location of data 1097*5972f5f3SLisandro Dalcin . count - local number of items of data to write 1098*5972f5f3SLisandro Dalcin . start - local start, can be PETSC_DETERMINE 1099*5972f5f3SLisandro Dalcin . total - global number of items of data to write, can be PETSC_DETERMINE 1100*5972f5f3SLisandro Dalcin - dtype - type of data to write 1101*5972f5f3SLisandro Dalcin 1102*5972f5f3SLisandro Dalcin Level: advanced 1103*5972f5f3SLisandro Dalcin 1104*5972f5f3SLisandro Dalcin .seealso: PetscViewerBinaryOpen(), PetscViewerBinarySetUseMPIIO(), PetscBinaryViewerWriteAll(), PetscBinaryViewerReadAll() 1105*5972f5f3SLisandro Dalcin @*/ 1106*5972f5f3SLisandro Dalcin PetscErrorCode PetscViewerBinaryWriteAll(PetscViewer viewer,const void *data,PetscInt count,PetscInt start,PetscInt total,PetscDataType dtype) 1107*5972f5f3SLisandro Dalcin { 1108*5972f5f3SLisandro Dalcin PetscErrorCode ierr; 1109*5972f5f3SLisandro Dalcin PetscFunctionBegin; 1110*5972f5f3SLisandro Dalcin ierr = PetscViewerBinaryWriteReadAll(viewer,PETSC_TRUE,(void*)data,count,start,total,dtype);CHKERRQ(ierr); 1111*5972f5f3SLisandro Dalcin PetscFunctionReturn(0); 1112*5972f5f3SLisandro Dalcin } 1113*5972f5f3SLisandro Dalcin 11145c6c1daeSBarry Smith /*@C 11155c6c1daeSBarry Smith PetscViewerBinaryWriteStringArray - writes to a binary file, only from the first process an array of strings 11165c6c1daeSBarry Smith 1117d083f849SBarry Smith Collective 11185c6c1daeSBarry Smith 11195c6c1daeSBarry Smith Input Parameters: 11205c6c1daeSBarry Smith + viewer - the binary viewer 11215c6c1daeSBarry Smith - data - location of the array of strings 11225c6c1daeSBarry Smith 11235c6c1daeSBarry Smith 11245c6c1daeSBarry Smith Level: intermediate 11255c6c1daeSBarry Smith 112695452b02SPatrick Sanan Notes: 112795452b02SPatrick Sanan array of strings is null terminated 11285c6c1daeSBarry Smith 11296a9046bcSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(), 11305c6c1daeSBarry Smith VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(), 11315c6c1daeSBarry Smith PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead() 11325c6c1daeSBarry Smith @*/ 113378fbdcc8SBarry Smith PetscErrorCode PetscViewerBinaryWriteStringArray(PetscViewer viewer,const char * const *data) 11345c6c1daeSBarry Smith { 11355c6c1daeSBarry Smith PetscErrorCode ierr; 11365c6c1daeSBarry Smith PetscInt i,n = 0,*sizes; 11375c6c1daeSBarry Smith 113822a8f86cSLisandro Dalcin PetscFunctionBegin; 1139c98fd787SBarry Smith ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr); 11405c6c1daeSBarry Smith /* count number of strings */ 11415c6c1daeSBarry Smith while (data[n++]) ; 11425c6c1daeSBarry Smith n--; 1143854ce69bSBarry Smith ierr = PetscMalloc1(n+1,&sizes);CHKERRQ(ierr); 11445c6c1daeSBarry Smith sizes[0] = n; 11455c6c1daeSBarry Smith for (i=0; i<n; i++) { 11465c6c1daeSBarry Smith size_t tmp; 11475c6c1daeSBarry Smith ierr = PetscStrlen(data[i],&tmp);CHKERRQ(ierr); 11485c6c1daeSBarry Smith sizes[i+1] = tmp + 1; /* size includes space for the null terminator */ 11495c6c1daeSBarry Smith } 1150f253e43cSLisandro Dalcin ierr = PetscViewerBinaryWrite(viewer,sizes,n+1,PETSC_INT);CHKERRQ(ierr); 11515c6c1daeSBarry Smith for (i=0; i<n; i++) { 1152f253e43cSLisandro Dalcin ierr = PetscViewerBinaryWrite(viewer,(void*)data[i],sizes[i+1],PETSC_CHAR);CHKERRQ(ierr); 11535c6c1daeSBarry Smith } 11545c6c1daeSBarry Smith ierr = PetscFree(sizes);CHKERRQ(ierr); 11555c6c1daeSBarry Smith PetscFunctionReturn(0); 11565c6c1daeSBarry Smith } 11575c6c1daeSBarry Smith 11585c6c1daeSBarry Smith /*@C 11595c6c1daeSBarry Smith PetscViewerBinaryReadStringArray - reads a binary file an array of strings 11605c6c1daeSBarry Smith 1161d083f849SBarry Smith Collective 11625c6c1daeSBarry Smith 11635c6c1daeSBarry Smith Input Parameter: 11645c6c1daeSBarry Smith . viewer - the binary viewer 11655c6c1daeSBarry Smith 11665c6c1daeSBarry Smith Output Parameter: 11675c6c1daeSBarry Smith . data - location of the array of strings 11685c6c1daeSBarry Smith 11695c6c1daeSBarry Smith Level: intermediate 11705c6c1daeSBarry Smith 117195452b02SPatrick Sanan Notes: 117295452b02SPatrick Sanan array of strings is null terminated 11735c6c1daeSBarry Smith 11746a9046bcSBarry Smith .seealso: PetscViewerASCIIOpen(), PetscViewerPushFormat(), PetscViewerDestroy(), 11755c6c1daeSBarry Smith VecView(), MatView(), VecLoad(), MatLoad(), PetscViewerBinaryGetDescriptor(), 11765c6c1daeSBarry Smith PetscViewerBinaryGetInfoPointer(), PetscFileMode, PetscViewer, PetscBinaryViewerRead() 11775c6c1daeSBarry Smith @*/ 11785c6c1daeSBarry Smith PetscErrorCode PetscViewerBinaryReadStringArray(PetscViewer viewer,char ***data) 11795c6c1daeSBarry Smith { 11805c6c1daeSBarry Smith PetscErrorCode ierr; 1181060da220SMatthew G. Knepley PetscInt i,n,*sizes,N = 0; 11825c6c1daeSBarry Smith 118322a8f86cSLisandro Dalcin PetscFunctionBegin; 1184c98fd787SBarry Smith ierr = PetscViewerSetUp(viewer);CHKERRQ(ierr); 11855c6c1daeSBarry Smith /* count number of strings */ 1186060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,&n,1,NULL,PETSC_INT);CHKERRQ(ierr); 1187785e854fSJed Brown ierr = PetscMalloc1(n,&sizes);CHKERRQ(ierr); 1188060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,sizes,n,NULL,PETSC_INT);CHKERRQ(ierr); 1189a297a907SKarl Rupp for (i=0; i<n; i++) N += sizes[i]; 1190854ce69bSBarry Smith ierr = PetscMalloc((n+1)*sizeof(char*) + N*sizeof(char),data);CHKERRQ(ierr); 11915c6c1daeSBarry Smith (*data)[0] = (char*)((*data) + n + 1); 1192a297a907SKarl Rupp for (i=1; i<n; i++) (*data)[i] = (*data)[i-1] + sizes[i-1]; 1193060da220SMatthew G. Knepley ierr = PetscViewerBinaryRead(viewer,(*data)[0],N,NULL,PETSC_CHAR);CHKERRQ(ierr); 1194a297a907SKarl Rupp 119502c9f0b5SLisandro Dalcin (*data)[n] = NULL; 1196a297a907SKarl Rupp 11975c6c1daeSBarry Smith ierr = PetscFree(sizes);CHKERRQ(ierr); 11985c6c1daeSBarry Smith PetscFunctionReturn(0); 11995c6c1daeSBarry Smith } 12005c6c1daeSBarry Smith 120181f0254dSBarry Smith static PetscErrorCode PetscViewerFileGetName_Binary(PetscViewer viewer,const char **name) 12025c6c1daeSBarry Smith { 12035c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 12045c6c1daeSBarry Smith 12055c6c1daeSBarry Smith PetscFunctionBegin; 12065c6c1daeSBarry Smith *name = vbinary->filename; 12075c6c1daeSBarry Smith PetscFunctionReturn(0); 12085c6c1daeSBarry Smith } 12095c6c1daeSBarry Smith 12105c6c1daeSBarry Smith /*@C 12115c6c1daeSBarry Smith PetscViewerFileGetMode - Gets the type of file to be open 12125c6c1daeSBarry Smith 12135c6c1daeSBarry Smith Not Collective 12145c6c1daeSBarry Smith 12155c6c1daeSBarry Smith Input Parameter: 1216232ac771SBarry Smith . viewer - the PetscViewer; must be a PETSCVIEWERBINARY, PETSCVIEWERMATLAB, PETSCVIEWERHDF5, or PETSCVIEWERASCII PetscViewer 12175c6c1daeSBarry Smith 12185c6c1daeSBarry Smith Output Parameter: 12195c6c1daeSBarry Smith . type - type of file 12205c6c1daeSBarry Smith $ FILE_MODE_WRITE - create new file for binary output 12215c6c1daeSBarry Smith $ FILE_MODE_READ - open existing file for binary input 12225c6c1daeSBarry Smith $ FILE_MODE_APPEND - open existing file for binary output 12235c6c1daeSBarry Smith 12245c6c1daeSBarry Smith Level: advanced 12255c6c1daeSBarry Smith 12265c6c1daeSBarry Smith .seealso: PetscViewerFileSetMode(), PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinaryOpen() 12275c6c1daeSBarry Smith 12285c6c1daeSBarry Smith @*/ 12295c6c1daeSBarry Smith PetscErrorCode PetscViewerFileGetMode(PetscViewer viewer,PetscFileMode *type) 12305c6c1daeSBarry Smith { 12315c6c1daeSBarry Smith PetscErrorCode ierr; 12325c6c1daeSBarry Smith 12335c6c1daeSBarry Smith PetscFunctionBegin; 12345c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 12355c6c1daeSBarry Smith PetscValidPointer(type,2); 12365c6c1daeSBarry Smith ierr = PetscUseMethod(viewer,"PetscViewerFileGetMode_C",(PetscViewer,PetscFileMode*),(viewer,type));CHKERRQ(ierr); 12375c6c1daeSBarry Smith PetscFunctionReturn(0); 12385c6c1daeSBarry Smith } 12395c6c1daeSBarry Smith 12405c6c1daeSBarry Smith /*@ 1241bc196f7cSDave May PetscViewerBinarySetUseMPIIO - Sets a binary viewer to use MPI-IO for reading/writing. Must be called 12425c6c1daeSBarry Smith before PetscViewerFileSetName() 12435c6c1daeSBarry Smith 12445c6c1daeSBarry Smith Logically Collective on PetscViewer 12455c6c1daeSBarry Smith 12465c6c1daeSBarry Smith Input Parameters: 1247bc196f7cSDave May + viewer - the PetscViewer; must be a binary 1248bc196f7cSDave May - flg - PETSC_TRUE means MPI-IO will be used 12495c6c1daeSBarry Smith 12505c6c1daeSBarry Smith Options Database: 12515c6c1daeSBarry Smith -viewer_binary_mpiio : Flag for using MPI-IO 12525c6c1daeSBarry Smith 12535c6c1daeSBarry Smith Level: advanced 12545c6c1daeSBarry Smith 1255bc196f7cSDave May .seealso: PetscViewerFileSetMode(), PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinaryOpen(), 1256bc196f7cSDave May PetscViewerBinaryGetUseMPIIO() 12575c6c1daeSBarry Smith 12585c6c1daeSBarry Smith @*/ 1259bc196f7cSDave May PetscErrorCode PetscViewerBinarySetUseMPIIO(PetscViewer viewer,PetscBool flg) 12605c6c1daeSBarry Smith { 12615c6c1daeSBarry Smith PetscErrorCode ierr; 12625c6c1daeSBarry Smith 12635c6c1daeSBarry Smith PetscFunctionBegin; 12645c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 126522a8f86cSLisandro Dalcin PetscValidLogicalCollectiveBool(viewer,flg,2); 1266bc196f7cSDave May ierr = PetscTryMethod(viewer,"PetscViewerBinarySetUseMPIIO_C",(PetscViewer,PetscBool),(viewer,flg));CHKERRQ(ierr); 12675c6c1daeSBarry Smith PetscFunctionReturn(0); 12685c6c1daeSBarry Smith } 12695c6c1daeSBarry Smith 12705c6c1daeSBarry Smith /*@C 12715c6c1daeSBarry Smith PetscViewerFileSetMode - Sets the type of file to be open 12725c6c1daeSBarry Smith 12735c6c1daeSBarry Smith Logically Collective on PetscViewer 12745c6c1daeSBarry Smith 12755c6c1daeSBarry Smith Input Parameters: 1276232ac771SBarry Smith + viewer - the PetscViewer; must be a a PETSCVIEWERBINARY, PETSCVIEWERMATLAB, PETSCVIEWERHDF5, or PETSCVIEWERASCII PetscViewer 12775c6c1daeSBarry Smith - type - type of file 1278f8859db6SBarry Smith $ FILE_MODE_WRITE - create new file for output 1279f8859db6SBarry Smith $ FILE_MODE_READ - open existing file for input 1280f8859db6SBarry Smith $ FILE_MODE_APPEND - open existing file for output 12815c6c1daeSBarry Smith 12825c6c1daeSBarry Smith Level: advanced 12835c6c1daeSBarry Smith 12845c6c1daeSBarry Smith .seealso: PetscViewerFileSetMode(), PetscViewerCreate(), PetscViewerSetType(), PetscViewerBinaryOpen() 12855c6c1daeSBarry Smith 12865c6c1daeSBarry Smith @*/ 12875c6c1daeSBarry Smith PetscErrorCode PetscViewerFileSetMode(PetscViewer viewer,PetscFileMode type) 12885c6c1daeSBarry Smith { 12895c6c1daeSBarry Smith PetscErrorCode ierr; 12905c6c1daeSBarry Smith 12915c6c1daeSBarry Smith PetscFunctionBegin; 12925c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,1); 12935c6c1daeSBarry Smith PetscValidLogicalCollectiveEnum(viewer,type,2); 12945c6c1daeSBarry Smith ierr = PetscTryMethod(viewer,"PetscViewerFileSetMode_C",(PetscViewer,PetscFileMode),(viewer,type));CHKERRQ(ierr); 12955c6c1daeSBarry Smith PetscFunctionReturn(0); 12965c6c1daeSBarry Smith } 12975c6c1daeSBarry Smith 129881f0254dSBarry Smith static PetscErrorCode PetscViewerFileGetMode_Binary(PetscViewer viewer,PetscFileMode *type) 12995c6c1daeSBarry Smith { 13005c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 13015c6c1daeSBarry Smith 13025c6c1daeSBarry Smith PetscFunctionBegin; 13035c6c1daeSBarry Smith *type = vbinary->btype; 13045c6c1daeSBarry Smith PetscFunctionReturn(0); 13055c6c1daeSBarry Smith } 13065c6c1daeSBarry Smith 130781f0254dSBarry Smith static PetscErrorCode PetscViewerFileSetMode_Binary(PetscViewer viewer,PetscFileMode type) 13085c6c1daeSBarry Smith { 13095c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 13105c6c1daeSBarry Smith 13115c6c1daeSBarry Smith PetscFunctionBegin; 131222a8f86cSLisandro Dalcin if (viewer->setupcalled && vbinary->btype != type) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ORDER,"Cannot change mode to %s after setup",PetscFileModes[type]); 13135c6c1daeSBarry Smith vbinary->btype = type; 13145c6c1daeSBarry Smith PetscFunctionReturn(0); 13155c6c1daeSBarry Smith } 13165c6c1daeSBarry Smith 131781f0254dSBarry Smith static PetscErrorCode PetscViewerFileSetName_Binary(PetscViewer viewer,const char name[]) 1318e0385b85SDave May { 1319e0385b85SDave May PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 1320e0385b85SDave May PetscErrorCode ierr; 1321e0385b85SDave May 1322e0385b85SDave May PetscFunctionBegin; 1323f90597f1SStefano Zampini if (vbinary->filename) { 1324f90597f1SStefano Zampini /* gzip can be run after the file with the previous filename has been closed */ 1325f90597f1SStefano Zampini ierr = PetscFree(vbinary->ogzfilename);CHKERRQ(ierr); 1326f90597f1SStefano Zampini ierr = PetscStrallocpy(vbinary->filename,&vbinary->ogzfilename);CHKERRQ(ierr); 1327f90597f1SStefano Zampini ierr = PetscFree(vbinary->filename);CHKERRQ(ierr); 1328f90597f1SStefano Zampini viewer->setupcalled = PETSC_FALSE; 1329f90597f1SStefano Zampini } 1330e0385b85SDave May ierr = PetscStrallocpy(name,&vbinary->filename);CHKERRQ(ierr); 1331e0385b85SDave May PetscFunctionReturn(0); 1332e0385b85SDave May } 13335c6c1daeSBarry Smith /* 13345c6c1daeSBarry Smith Actually opens the file 13355c6c1daeSBarry Smith */ 1336e0877f53SBarry Smith static PetscErrorCode PetscViewerFileSetUp_Binary(PetscViewer viewer) 13375c6c1daeSBarry Smith { 13385c6c1daeSBarry Smith PetscMPIInt rank; 13395c6c1daeSBarry Smith PetscErrorCode ierr; 13405c6c1daeSBarry Smith size_t len; 13415c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 13425c6c1daeSBarry Smith const char *fname; 13432259a519SDave May char bname[PETSC_MAX_PATH_LEN],*gz; 13445c6c1daeSBarry Smith PetscBool found; 13455c6c1daeSBarry Smith PetscFileMode type = vbinary->btype; 13465c6c1daeSBarry Smith 13475c6c1daeSBarry Smith PetscFunctionBegin; 1348e0385b85SDave May if (type == (PetscFileMode) -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetMode()"); 13492259a519SDave May if (!vbinary->filename) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetName()"); 13505c6c1daeSBarry Smith ierr = PetscViewerFileClose_Binary(viewer);CHKERRQ(ierr); 13515c6c1daeSBarry Smith 1352ce94432eSBarry Smith ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr); 13535c6c1daeSBarry Smith 13545c6c1daeSBarry Smith /* if ends in .gz strip that off and note user wants file compressed */ 13555c6c1daeSBarry Smith vbinary->storecompressed = PETSC_FALSE; 13565c6c1daeSBarry Smith if (!rank && type == FILE_MODE_WRITE) { 13575c6c1daeSBarry Smith /* remove .gz if it ends library name */ 13585c6c1daeSBarry Smith ierr = PetscStrstr(vbinary->filename,".gz",&gz);CHKERRQ(ierr); 13595c6c1daeSBarry Smith if (gz) { 13605c6c1daeSBarry Smith ierr = PetscStrlen(gz,&len);CHKERRQ(ierr); 13615c6c1daeSBarry Smith if (len == 3) { 13625c6c1daeSBarry Smith *gz = 0; 13635c6c1daeSBarry Smith vbinary->storecompressed = PETSC_TRUE; 13645c6c1daeSBarry Smith } 13655c6c1daeSBarry Smith } 13665c6c1daeSBarry Smith } 13675c6c1daeSBarry Smith 13685c6c1daeSBarry Smith /* only first processor opens file if writeable */ 13695c6c1daeSBarry Smith if (!rank || type == FILE_MODE_READ) { 13705c6c1daeSBarry Smith 13715c6c1daeSBarry Smith if (type == FILE_MODE_READ) { 13725c6c1daeSBarry Smith /* possibly get the file from remote site or compressed file */ 1373ce94432eSBarry Smith ierr = PetscFileRetrieve(PetscObjectComm((PetscObject)viewer),vbinary->filename,bname,PETSC_MAX_PATH_LEN,&found);CHKERRQ(ierr); 13745c6c1daeSBarry Smith fname = bname; 137541c4be4aSVaclav 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 */ 137641c4be4aSVaclav Hapla if (!found) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_FILE_OPEN,"Cannot locate file: %s on node zero",vbinary->filename); 1377a297a907SKarl Rupp } else fname = vbinary->filename; 137855819941SStefano Zampini if (type == FILE_MODE_APPEND) { /* check if asked to append to a non-existing file */ 137955819941SStefano Zampini ierr = PetscTestFile(fname,'\0',&found);CHKERRQ(ierr); 138055819941SStefano Zampini } 13815c6c1daeSBarry Smith 13825c6c1daeSBarry Smith #if defined(PETSC_HAVE_O_BINARY) 138355819941SStefano Zampini if (type == FILE_MODE_WRITE || (type == FILE_MODE_APPEND && !found) ) { 13845c6c1daeSBarry 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); 13855c6c1daeSBarry Smith } else if (type == FILE_MODE_READ && fname) { 13865c6c1daeSBarry 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); 13875c6c1daeSBarry Smith } else if (type == FILE_MODE_APPEND) { 13885c6c1daeSBarry 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); 13895c6c1daeSBarry Smith } else if (fname) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown file type"); 13905c6c1daeSBarry Smith #else 139155819941SStefano Zampini if (type == FILE_MODE_WRITE || (type == FILE_MODE_APPEND && !found) ) { 13925c6c1daeSBarry Smith if ((vbinary->fdes = creat(fname,0666)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot create file %s for writing",fname); 13935c6c1daeSBarry Smith } else if (type == FILE_MODE_READ && fname) { 13945c6c1daeSBarry 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); 13955c6c1daeSBarry Smith } else if (type == FILE_MODE_APPEND) { 13965c6c1daeSBarry 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); 13975c6c1daeSBarry Smith } else if (fname) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Unknown file type"); 13985c6c1daeSBarry Smith #endif 13995c6c1daeSBarry Smith } else vbinary->fdes = -1; 14005c6c1daeSBarry Smith 14015c6c1daeSBarry Smith /* 14025c6c1daeSBarry Smith try to open info file: all processors open this file if read only 14035c6c1daeSBarry Smith */ 14045c6c1daeSBarry Smith if (!vbinary->skipinfo && (!rank || type == FILE_MODE_READ)) { 14055c6c1daeSBarry Smith char infoname[PETSC_MAX_PATH_LEN],iname[PETSC_MAX_PATH_LEN]; 14065c6c1daeSBarry Smith 1407a126751eSBarry Smith ierr = PetscStrncpy(infoname,vbinary->filename,sizeof(infoname));CHKERRQ(ierr); 14085c6c1daeSBarry Smith /* remove .gz if it ends library name */ 14095c6c1daeSBarry Smith ierr = PetscStrstr(infoname,".gz",&gz);CHKERRQ(ierr); 14105c6c1daeSBarry Smith if (gz) { 14115c6c1daeSBarry Smith ierr = PetscStrlen(gz,&len);CHKERRQ(ierr); 1412a297a907SKarl Rupp if (len == 3) *gz = 0; 14135c6c1daeSBarry Smith } 14145c6c1daeSBarry Smith 1415a126751eSBarry Smith ierr = PetscStrlcat(infoname,".info",sizeof(infoname));CHKERRQ(ierr); 14165c6c1daeSBarry Smith ierr = PetscFixFilename(infoname,iname);CHKERRQ(ierr); 14175c6c1daeSBarry Smith if (type == FILE_MODE_READ) { 1418ce94432eSBarry Smith ierr = PetscFileRetrieve(PetscObjectComm((PetscObject)viewer),iname,infoname,PETSC_MAX_PATH_LEN,&found);CHKERRQ(ierr); 14191f5e1f59SVaclav Hapla if (found) { 1420c5929fdfSBarry Smith ierr = PetscOptionsInsertFile(PetscObjectComm((PetscObject)viewer),((PetscObject)viewer)->options,infoname,PETSC_FALSE);CHKERRQ(ierr); 14211f5e1f59SVaclav Hapla } 14225c6c1daeSBarry Smith } else { 14235c6c1daeSBarry Smith vbinary->fdes_info = fopen(infoname,"w"); 14245c6c1daeSBarry Smith if (!vbinary->fdes_info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open .info file %s for writing",infoname); 14255c6c1daeSBarry Smith } 14265c6c1daeSBarry Smith } 14275c6c1daeSBarry Smith #if defined(PETSC_USE_LOG) 1428e0385b85SDave May PetscLogObjectState((PetscObject)viewer,"File: %s",vbinary->filename); 14295c6c1daeSBarry Smith #endif 14305c6c1daeSBarry Smith PetscFunctionReturn(0); 14315c6c1daeSBarry Smith } 14325c6c1daeSBarry Smith 14335c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO) 1434e0385b85SDave May static PetscErrorCode PetscViewerFileSetUp_BinaryMPIIO(PetscViewer viewer) 14355c6c1daeSBarry Smith { 14365c6c1daeSBarry Smith PetscMPIInt rank; 14375c6c1daeSBarry Smith PetscErrorCode ierr; 14385c6c1daeSBarry Smith size_t len; 14395c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 14402259a519SDave May char *gz; 14415c6c1daeSBarry Smith PetscBool found; 14425c6c1daeSBarry Smith PetscFileMode type = vbinary->btype; 1443e8a65b7dSLisandro Dalcin int amode; 14445c6c1daeSBarry Smith 14455c6c1daeSBarry Smith PetscFunctionBegin; 1446e0385b85SDave May if (type == (PetscFileMode) -1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetMode()"); 14472259a519SDave May if (!vbinary->filename) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call PetscViewerFileSetName()"); 1448e0385b85SDave May ierr = PetscViewerFileClose_BinaryMPIIO(viewer);CHKERRQ(ierr); 14495c6c1daeSBarry Smith 1450a297a907SKarl Rupp vbinary->storecompressed = PETSC_FALSE; 14515c6c1daeSBarry Smith 1452e8a65b7dSLisandro Dalcin switch (type) { 1453e8a65b7dSLisandro Dalcin case FILE_MODE_READ: amode = MPI_MODE_RDONLY; break; 1454e8a65b7dSLisandro Dalcin case FILE_MODE_WRITE: amode = MPI_MODE_WRONLY | MPI_MODE_CREATE; break; 145522a8f86cSLisandro Dalcin case FILE_MODE_APPEND: amode = MPI_MODE_WRONLY | MPI_MODE_CREATE | MPI_MODE_APPEND; break; 1456e8a65b7dSLisandro Dalcin default: SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Unsupported file mode %s",PetscFileModes[type]); 14575c6c1daeSBarry Smith } 1458e8a65b7dSLisandro Dalcin ierr = MPI_File_open(PetscObjectComm((PetscObject)viewer),vbinary->filename,amode,MPI_INFO_NULL,&vbinary->mfdes);CHKERRQ(ierr); 145922a8f86cSLisandro Dalcin /* 146022a8f86cSLisandro Dalcin The MPI standard does not have MPI_MODE_TRUNCATE. We emulate this behavior by setting the file size to zero. 146122a8f86cSLisandro Dalcin */ 146222a8f86cSLisandro Dalcin if (type == FILE_MODE_WRITE) {ierr = MPI_File_set_size(vbinary->mfdes,0);CHKERRQ(ierr);} 146322a8f86cSLisandro Dalcin /* 146422a8f86cSLisandro Dalcin Initially, all processes view the file as a linear byte stream. Therefore, for files opened with MPI_MODE_APPEND, 146522a8f86cSLisandro Dalcin MPI_File_get_position[_shared](fh, &offset) returns the absolute byte position at the end of file. 146622a8f86cSLisandro Dalcin Otherwise, we would need to call MPI_File_get_byte_offset(fh, offset, &byte_offset) to convert 146722a8f86cSLisandro Dalcin the offset in etype units to an absolute byte position. 146822a8f86cSLisandro Dalcin */ 146922a8f86cSLisandro Dalcin if (type == FILE_MODE_APPEND) {ierr = MPI_File_get_position(vbinary->mfdes,&vbinary->moff);CHKERRQ(ierr);} 14705c6c1daeSBarry Smith 14715c6c1daeSBarry Smith /* 14725c6c1daeSBarry Smith try to open info file: all processors open this file if read only 14735c6c1daeSBarry Smith 1474bebe2cf6SSatish Balay Below is identical code to the code for Binary above, should be put in separate routine 14755c6c1daeSBarry Smith */ 1476e8a65b7dSLisandro Dalcin ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank);CHKERRQ(ierr); 14775c6c1daeSBarry Smith if (!vbinary->skipinfo && (!rank || type == FILE_MODE_READ)) { 14785c6c1daeSBarry Smith char infoname[PETSC_MAX_PATH_LEN],iname[PETSC_MAX_PATH_LEN]; 14795c6c1daeSBarry Smith 1480a126751eSBarry Smith ierr = PetscStrncpy(infoname,vbinary->filename,sizeof(infoname));CHKERRQ(ierr); 14815c6c1daeSBarry Smith /* remove .gz if it ends library name */ 14825c6c1daeSBarry Smith ierr = PetscStrstr(infoname,".gz",&gz);CHKERRQ(ierr); 14835c6c1daeSBarry Smith if (gz) { 14845c6c1daeSBarry Smith ierr = PetscStrlen(gz,&len);CHKERRQ(ierr); 1485a297a907SKarl Rupp if (len == 3) *gz = 0; 14865c6c1daeSBarry Smith } 14875c6c1daeSBarry Smith 1488a126751eSBarry Smith ierr = PetscStrlcat(infoname,".info",sizeof(infoname));CHKERRQ(ierr); 14895c6c1daeSBarry Smith ierr = PetscFixFilename(infoname,iname);CHKERRQ(ierr); 14905c6c1daeSBarry Smith if (type == FILE_MODE_READ) { 1491ce94432eSBarry Smith ierr = PetscFileRetrieve(PetscObjectComm((PetscObject)viewer),iname,infoname,PETSC_MAX_PATH_LEN,&found);CHKERRQ(ierr); 1492c5929fdfSBarry Smith ierr = PetscOptionsInsertFile(PetscObjectComm((PetscObject)viewer),((PetscObject)viewer)->options,infoname,PETSC_FALSE);CHKERRQ(ierr); 14935c6c1daeSBarry Smith } else { 14945c6c1daeSBarry Smith vbinary->fdes_info = fopen(infoname,"w"); 14955c6c1daeSBarry Smith if (!vbinary->fdes_info) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Cannot open .info file %s for writing",infoname); 14965c6c1daeSBarry Smith } 14975c6c1daeSBarry Smith } 14985c6c1daeSBarry Smith #if defined(PETSC_USE_LOG) 1499e0385b85SDave May PetscLogObjectState((PetscObject)viewer,"File: %s",vbinary->filename); 15005c6c1daeSBarry Smith #endif 15015c6c1daeSBarry Smith PetscFunctionReturn(0); 15025c6c1daeSBarry Smith } 15035c6c1daeSBarry Smith 150481f0254dSBarry Smith static PetscErrorCode PetscViewerBinarySetUseMPIIO_Binary(PetscViewer viewer,PetscBool flg) 15055c6c1daeSBarry Smith { 15065c6c1daeSBarry Smith PetscViewer_Binary *vbinary = (PetscViewer_Binary*)viewer->data; 15075c6c1daeSBarry Smith PetscFunctionBegin; 150822a8f86cSLisandro Dalcin if (viewer->setupcalled && vbinary->usempiio != flg) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_ORDER,"Cannot change MPIIO to %s after setup",PetscBools[flg]); 1509bc196f7cSDave May vbinary->usempiio = flg; 15105c6c1daeSBarry Smith PetscFunctionReturn(0); 15115c6c1daeSBarry Smith } 15125c6c1daeSBarry Smith #endif 15135c6c1daeSBarry Smith 151481f0254dSBarry Smith static PetscErrorCode PetscViewerView_Binary(PetscViewer v,PetscViewer viewer) 15152bf49c77SBarry Smith { 15162bf49c77SBarry Smith PetscErrorCode ierr; 1517cb6ad94fSLisandro Dalcin PetscViewer_Binary *vbinary = (PetscViewer_Binary*)v->data; 1518cb6ad94fSLisandro Dalcin const char *fname = vbinary->filename ? vbinary->filename : "not yet set"; 1519cb6ad94fSLisandro Dalcin const char *fmode = vbinary->btype != (PetscFileMode) -1 ? PetscFileModes[vbinary->btype] : "not yet set"; 1520cb6ad94fSLisandro Dalcin PetscBool usempiio; 15212bf49c77SBarry Smith 15222bf49c77SBarry Smith PetscFunctionBegin; 1523cb6ad94fSLisandro Dalcin ierr = PetscViewerBinaryGetUseMPIIO(v,&usempiio);CHKERRQ(ierr); 1524cb6ad94fSLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer,"Filename: %s\n",fname);CHKERRQ(ierr); 1525cb6ad94fSLisandro Dalcin ierr = PetscViewerASCIIPrintf(viewer,"Mode: %s (%s)\n",fmode,usempiio ? "mpiio" : "stdio");CHKERRQ(ierr); 15262bf49c77SBarry Smith PetscFunctionReturn(0); 15272bf49c77SBarry Smith } 15282bf49c77SBarry Smith 152903a1d59fSDave May static PetscErrorCode PetscViewerSetUp_Binary(PetscViewer v) 153003a1d59fSDave May { 153103a1d59fSDave May PetscErrorCode ierr; 153203a1d59fSDave May PetscViewer_Binary *binary = (PetscViewer_Binary*)v->data; 1533e0385b85SDave May 153403a1d59fSDave May PetscFunctionBegin; 1535da07bb01SDave May if (!binary->setfromoptionscalled) { ierr = PetscViewerSetFromOptions(v);CHKERRQ(ierr); } 153603a1d59fSDave May 1537e0385b85SDave May #if defined(PETSC_HAVE_MPIIO) 1538bc196f7cSDave May if (binary->usempiio) { 1539e0385b85SDave May ierr = PetscViewerFileSetUp_BinaryMPIIO(v);CHKERRQ(ierr); 1540e0385b85SDave May } else { 1541e0385b85SDave May #endif 1542e0385b85SDave May ierr = PetscViewerFileSetUp_Binary(v);CHKERRQ(ierr); 1543e0385b85SDave May #if defined(PETSC_HAVE_MPIIO) 1544e0385b85SDave May } 1545e0385b85SDave May #endif 154603a1d59fSDave May PetscFunctionReturn(0); 154703a1d59fSDave May } 154803a1d59fSDave May 154922a8f86cSLisandro Dalcin static PetscErrorCode PetscViewerSetFromOptions_Binary(PetscOptionItems *PetscOptionsObject,PetscViewer viewer) 155003a1d59fSDave May { 155103a1d59fSDave May PetscErrorCode ierr; 155222a8f86cSLisandro Dalcin PetscViewer_Binary *binary = (PetscViewer_Binary*)viewer->data; 1553d22fd6bcSDave May char defaultname[PETSC_MAX_PATH_LEN]; 155403a1d59fSDave May PetscBool flg; 1555e0385b85SDave May 155603a1d59fSDave May PetscFunctionBegin; 155722a8f86cSLisandro Dalcin if (viewer->setupcalled) PetscFunctionReturn(0); 155803a1d59fSDave May ierr = PetscOptionsHead(PetscOptionsObject,"Binary PetscViewer Options");CHKERRQ(ierr); 1559d22fd6bcSDave May ierr = PetscSNPrintf(defaultname,PETSC_MAX_PATH_LEN-1,"binaryoutput");CHKERRQ(ierr); 1560d22fd6bcSDave May ierr = PetscOptionsString("-viewer_binary_filename","Specify filename","PetscViewerFileSetName",defaultname,defaultname,PETSC_MAX_PATH_LEN-1,&flg);CHKERRQ(ierr); 156122a8f86cSLisandro Dalcin if (flg) { ierr = PetscViewerFileSetName_Binary(viewer,defaultname);CHKERRQ(ierr); } 156222a8f86cSLisandro Dalcin ierr = PetscOptionsBool("-viewer_binary_skip_info","Skip writing/reading .info file","PetscViewerBinarySetSkipInfo",binary->skipinfo,&binary->skipinfo,NULL);CHKERRQ(ierr); 156322a8f86cSLisandro Dalcin ierr = PetscOptionsBool("-viewer_binary_skip_options","Skip parsing vec load options","PetscViewerBinarySetSkipOptions",binary->skipoptions,&binary->skipoptions,NULL);CHKERRQ(ierr); 156422a8f86cSLisandro Dalcin ierr = PetscOptionsBool("-viewer_binary_skip_header","Skip writing/reading header information","PetscViewerBinarySetSkipHeader",binary->skipheader,&binary->skipheader,NULL);CHKERRQ(ierr); 156503a1d59fSDave May #if defined(PETSC_HAVE_MPIIO) 156622a8f86cSLisandro Dalcin ierr = PetscOptionsBool("-viewer_binary_mpiio","Use MPI-IO functionality to write/read binary file","PetscViewerBinarySetUseMPIIO",binary->usempiio,&binary->usempiio,NULL);CHKERRQ(ierr); 1567*5972f5f3SLisandro Dalcin #else 1568*5972f5f3SLisandro Dalcin ierr = PetscOptionsBool("-viewer_binary_mpiio","Use MPI-IO functionality to write/read binary file (NOT AVAILABLE)","PetscViewerBinarySetUseMPIIO",PETSC_FALSE,NULL,NULL);CHKERRQ(ierr); 156903a1d59fSDave May #endif 157003a1d59fSDave May ierr = PetscOptionsTail();CHKERRQ(ierr); 1571bc196f7cSDave May binary->setfromoptionscalled = PETSC_TRUE; 157203a1d59fSDave May PetscFunctionReturn(0); 157303a1d59fSDave May } 157403a1d59fSDave May 15758556b5ebSBarry Smith /*MC 15768556b5ebSBarry Smith PETSCVIEWERBINARY - A viewer that saves to binary files 15778556b5ebSBarry Smith 15788556b5ebSBarry Smith 15798556b5ebSBarry Smith .seealso: PetscViewerBinaryOpen(), PETSC_VIEWER_STDOUT_(),PETSC_VIEWER_STDOUT_SELF, PETSC_VIEWER_STDOUT_WORLD, PetscViewerCreate(), PetscViewerASCIIOpen(), 15808556b5ebSBarry Smith PetscViewerMatlabOpen(), VecView(), DMView(), PetscViewerMatlabPutArray(), PETSCVIEWERASCII, PETSCVIEWERMATLAB, PETSCVIEWERDRAW, 158167918a83SBarry Smith PetscViewerFileSetName(), PetscViewerFileSetMode(), PetscViewerFormat, PetscViewerType, PetscViewerSetType(), 158267918a83SBarry Smith PetscViewerBinaryGetUseMPIIO(), PetscViewerBinarySetUseMPIIO() 15838556b5ebSBarry Smith 15841b266c99SBarry Smith Level: beginner 15851b266c99SBarry Smith 15868556b5ebSBarry Smith M*/ 15878556b5ebSBarry Smith 15888cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscViewerCreate_Binary(PetscViewer v) 15895c6c1daeSBarry Smith { 15905c6c1daeSBarry Smith PetscErrorCode ierr; 15915c6c1daeSBarry Smith PetscViewer_Binary *vbinary; 15925c6c1daeSBarry Smith 15935c6c1daeSBarry Smith PetscFunctionBegin; 1594b00a9115SJed Brown ierr = PetscNewLog(v,&vbinary);CHKERRQ(ierr); 15955c6c1daeSBarry Smith v->data = (void*)vbinary; 159603a1d59fSDave May v->ops->setfromoptions = PetscViewerSetFromOptions_Binary; 15975c6c1daeSBarry Smith v->ops->destroy = PetscViewerDestroy_Binary; 15982bf49c77SBarry Smith v->ops->view = PetscViewerView_Binary; 1599c98fd787SBarry Smith v->ops->setup = PetscViewerSetUp_Binary; 1600c98fd787SBarry Smith v->ops->flush = NULL; 16015c6c1daeSBarry Smith vbinary->fdes = 0; 1602e8a65b7dSLisandro Dalcin #if defined(PETSC_HAVE_MPIIO) 1603e8a65b7dSLisandro Dalcin vbinary->mfdes = MPI_FILE_NULL; 1604e8a65b7dSLisandro Dalcin vbinary->mfsub = MPI_FILE_NULL; 1605e8a65b7dSLisandro Dalcin #endif 160602c9f0b5SLisandro Dalcin vbinary->fdes_info = NULL; 16075c6c1daeSBarry Smith vbinary->skipinfo = PETSC_FALSE; 16085c6c1daeSBarry Smith vbinary->skipoptions = PETSC_TRUE; 16095c6c1daeSBarry Smith vbinary->skipheader = PETSC_FALSE; 1610da07bb01SDave May vbinary->setfromoptionscalled = PETSC_FALSE; 1611559f443fSBarry Smith v->ops->getsubviewer = PetscViewerGetSubViewer_Binary; 1612559f443fSBarry Smith v->ops->restoresubviewer = PetscViewerRestoreSubViewer_Binary; 16131d641e7bSMichael Lange v->ops->read = PetscViewerBinaryRead; 16145c6c1daeSBarry Smith vbinary->btype = (PetscFileMode) -1; 16155c6c1daeSBarry Smith vbinary->storecompressed = PETSC_FALSE; 1616f90597f1SStefano Zampini vbinary->filename = NULL; 1617f90597f1SStefano Zampini vbinary->ogzfilename = NULL; 16185c6c1daeSBarry Smith vbinary->flowcontrol = 256; /* seems a good number for Cray XT-5 */ 16195c6c1daeSBarry Smith 1620bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetFlowControl_C",PetscViewerBinaryGetFlowControl_Binary);CHKERRQ(ierr); 1621bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetFlowControl_C",PetscViewerBinarySetFlowControl_Binary);CHKERRQ(ierr); 1622bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetSkipHeader_C",PetscViewerBinarySetSkipHeader_Binary);CHKERRQ(ierr); 1623bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetSkipHeader_C",PetscViewerBinaryGetSkipHeader_Binary);CHKERRQ(ierr); 1624807ea322SDave May ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetSkipOptions_C",PetscViewerBinaryGetSkipOptions_Binary);CHKERRQ(ierr); 1625807ea322SDave May ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetSkipOptions_C",PetscViewerBinarySetSkipOptions_Binary);CHKERRQ(ierr); 1626807ea322SDave May ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetSkipInfo_C",PetscViewerBinaryGetSkipInfo_Binary);CHKERRQ(ierr); 1627807ea322SDave May ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetSkipInfo_C",PetscViewerBinarySetSkipInfo_Binary);CHKERRQ(ierr); 1628bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetInfoPointer_C",PetscViewerBinaryGetInfoPointer_Binary);CHKERRQ(ierr); 1629bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetName_C",PetscViewerFileSetName_Binary);CHKERRQ(ierr); 1630bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileSetMode_C",PetscViewerFileSetMode_Binary);CHKERRQ(ierr); 1631bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileGetMode_C",PetscViewerFileGetMode_Binary);CHKERRQ(ierr); 1632bdf89e91SBarry Smith ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerFileGetName_C",PetscViewerFileGetName_Binary);CHKERRQ(ierr); 16335c6c1daeSBarry Smith #if defined(PETSC_HAVE_MPIIO) 1634bc196f7cSDave May ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinaryGetUseMPIIO_C",PetscViewerBinaryGetUseMPIIO_Binary);CHKERRQ(ierr); 1635bc196f7cSDave May ierr = PetscObjectComposeFunction((PetscObject)v,"PetscViewerBinarySetUseMPIIO_C",PetscViewerBinarySetUseMPIIO_Binary);CHKERRQ(ierr); 16365c6c1daeSBarry Smith #endif 16375c6c1daeSBarry Smith PetscFunctionReturn(0); 16385c6c1daeSBarry Smith } 16395c6c1daeSBarry Smith 16405c6c1daeSBarry Smith /* ---------------------------------------------------------------------*/ 16415c6c1daeSBarry Smith /* 16425c6c1daeSBarry Smith The variable Petsc_Viewer_Binary_keyval is used to indicate an MPI attribute that 16435c6c1daeSBarry Smith is attached to a communicator, in this case the attribute is a PetscViewer. 16445c6c1daeSBarry Smith */ 1645d4c7638eSBarry Smith PetscMPIInt Petsc_Viewer_Binary_keyval = MPI_KEYVAL_INVALID; 16465c6c1daeSBarry Smith 16475c6c1daeSBarry Smith /*@C 16485c6c1daeSBarry Smith PETSC_VIEWER_BINARY_ - Creates a binary PetscViewer shared by all processors 16495c6c1daeSBarry Smith in a communicator. 16505c6c1daeSBarry Smith 1651d083f849SBarry Smith Collective 16525c6c1daeSBarry Smith 16535c6c1daeSBarry Smith Input Parameter: 16545c6c1daeSBarry Smith . comm - the MPI communicator to share the binary PetscViewer 16555c6c1daeSBarry Smith 16565c6c1daeSBarry Smith Level: intermediate 16575c6c1daeSBarry Smith 16585c6c1daeSBarry Smith Options Database Keys: 16595c6c1daeSBarry Smith + -viewer_binary_filename <name> 16605c6c1daeSBarry Smith . -viewer_binary_skip_info 1661a2d7db39SDave May . -viewer_binary_skip_options 1662a2d7db39SDave May . -viewer_binary_skip_header 1663a2d7db39SDave May - -viewer_binary_mpiio 16645c6c1daeSBarry Smith 16655c6c1daeSBarry Smith Environmental variables: 16665c6c1daeSBarry Smith - PETSC_VIEWER_BINARY_FILENAME 16675c6c1daeSBarry Smith 16685c6c1daeSBarry Smith Notes: 16695c6c1daeSBarry Smith Unlike almost all other PETSc routines, PETSC_VIEWER_BINARY_ does not return 16705c6c1daeSBarry Smith an error code. The binary PetscViewer is usually used in the form 16715c6c1daeSBarry Smith $ XXXView(XXX object,PETSC_VIEWER_BINARY_(comm)); 16725c6c1daeSBarry Smith 16735c6c1daeSBarry Smith .seealso: PETSC_VIEWER_BINARY_WORLD, PETSC_VIEWER_BINARY_SELF, PetscViewerBinaryOpen(), PetscViewerCreate(), 16745c6c1daeSBarry Smith PetscViewerDestroy() 16755c6c1daeSBarry Smith @*/ 16765c6c1daeSBarry Smith PetscViewer PETSC_VIEWER_BINARY_(MPI_Comm comm) 16775c6c1daeSBarry Smith { 16785c6c1daeSBarry Smith PetscErrorCode ierr; 16795c6c1daeSBarry Smith PetscBool flg; 16805c6c1daeSBarry Smith PetscViewer viewer; 16815c6c1daeSBarry Smith char fname[PETSC_MAX_PATH_LEN]; 16825c6c1daeSBarry Smith MPI_Comm ncomm; 16835c6c1daeSBarry Smith 16845c6c1daeSBarry Smith PetscFunctionBegin; 168502c9f0b5SLisandro Dalcin ierr = PetscCommDuplicate(comm,&ncomm,NULL);if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(NULL);} 16865c6c1daeSBarry Smith if (Petsc_Viewer_Binary_keyval == MPI_KEYVAL_INVALID) { 168702c9f0b5SLisandro Dalcin ierr = MPI_Comm_create_keyval(MPI_COMM_NULL_COPY_FN,MPI_COMM_NULL_DELETE_FN,&Petsc_Viewer_Binary_keyval,NULL); 168802c9f0b5SLisandro Dalcin if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(NULL);} 16895c6c1daeSBarry Smith } 169047435625SJed Brown ierr = MPI_Comm_get_attr(ncomm,Petsc_Viewer_Binary_keyval,(void**)&viewer,(int*)&flg); 169102c9f0b5SLisandro Dalcin if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(NULL);} 16925c6c1daeSBarry Smith if (!flg) { /* PetscViewer not yet created */ 16935c6c1daeSBarry Smith ierr = PetscOptionsGetenv(ncomm,"PETSC_VIEWER_BINARY_FILENAME",fname,PETSC_MAX_PATH_LEN,&flg); 169402c9f0b5SLisandro Dalcin if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(NULL);} 16955c6c1daeSBarry Smith if (!flg) { 16965c6c1daeSBarry Smith ierr = PetscStrcpy(fname,"binaryoutput"); 169702c9f0b5SLisandro Dalcin if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(NULL);} 16985c6c1daeSBarry Smith } 16995c6c1daeSBarry Smith ierr = PetscViewerBinaryOpen(ncomm,fname,FILE_MODE_WRITE,&viewer); 170002c9f0b5SLisandro Dalcin if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(NULL);} 17015c6c1daeSBarry Smith ierr = PetscObjectRegisterDestroy((PetscObject)viewer); 170202c9f0b5SLisandro Dalcin if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(NULL);} 170347435625SJed Brown ierr = MPI_Comm_set_attr(ncomm,Petsc_Viewer_Binary_keyval,(void*)viewer); 170402c9f0b5SLisandro Dalcin if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(NULL);} 17055c6c1daeSBarry Smith } 17065c6c1daeSBarry Smith ierr = PetscCommDestroy(&ncomm); 170702c9f0b5SLisandro Dalcin if (ierr) {PetscError(PETSC_COMM_SELF,__LINE__,"PETSC_VIEWER_BINARY_",__FILE__,PETSC_ERR_PLIB,PETSC_ERROR_INITIAL," ");PetscFunctionReturn(NULL);} 17085c6c1daeSBarry Smith PetscFunctionReturn(viewer); 17095c6c1daeSBarry Smith } 1710