1 static char help[] = "Tests binary viewers.\n\n"; 2 3 #include <petscsys.h> 4 #include <petscviewer.h> 5 6 static PetscErrorCode TestOpen(PetscFileMode mode,PetscViewer *viewer) 7 { 8 const char *name; 9 PetscBool skipinfo,skipheader,skipoptions; 10 11 PetscFunctionBegin; 12 PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD,"binary.dat",mode,viewer)); 13 PetscCall(PetscViewerBinarySkipInfo(*viewer)); 14 PetscCall(PetscViewerBinarySetSkipInfo(*viewer,PETSC_FALSE)); 15 PetscCall(PetscViewerBinarySetSkipHeader(*viewer,PETSC_FALSE)); 16 PetscCall(PetscViewerBinarySetSkipOptions(*viewer,PETSC_FALSE)); 17 PetscCall(PetscViewerSetUp(*viewer)); 18 PetscCall(PetscViewerFileGetName(*viewer,&name)); 19 PetscCall(PetscViewerFileGetMode(*viewer,&mode)); 20 PetscCall(PetscViewerBinaryGetSkipInfo(*viewer,&skipinfo)); 21 PetscCall(PetscViewerBinaryGetSkipHeader(*viewer,&skipheader)); 22 PetscCall(PetscViewerBinaryGetSkipOptions(*viewer,&skipoptions)); 23 PetscFunctionReturn(0); 24 } 25 26 static PetscErrorCode TestWrite(PetscViewer viewer) 27 { 28 PetscInt idata = 42; 29 PetscReal rdata = 42; 30 PetscInt s = PETSC_DETERMINE, t = PETSC_DETERMINE; 31 PetscViewer subviewer; 32 33 PetscFunctionBegin; 34 PetscCall(PetscViewerBinaryWrite(viewer,&idata,1,PETSC_INT)); 35 PetscCall(PetscViewerBinaryWrite(viewer,&rdata,1,PETSC_REAL)); 36 37 PetscCall(PetscViewerGetSubViewer(viewer,PETSC_COMM_SELF,&subviewer)); 38 if (subviewer) { 39 PetscCall(PetscViewerBinaryWrite(subviewer,&idata,1,PETSC_INT)); 40 PetscCall(PetscViewerBinaryWrite(subviewer,&rdata,1,PETSC_REAL)); 41 } 42 PetscCall(PetscViewerRestoreSubViewer(viewer,PETSC_COMM_SELF,&subviewer)); 43 44 PetscCall(PetscViewerBinaryWriteAll(viewer,&idata,1,s,t,PETSC_INT)); 45 PetscCall(PetscViewerBinaryWriteAll(viewer,&rdata,1,s,t,PETSC_REAL)); 46 47 PetscCall(PetscViewerGetSubViewer(viewer,PETSC_COMM_SELF,&subviewer)); 48 if (subviewer) { 49 PetscCall(PetscViewerBinaryWrite(subviewer,&idata,1,PETSC_INT)); 50 PetscCall(PetscViewerBinaryWrite(subviewer,&rdata,1,PETSC_REAL)); 51 } 52 PetscCall(PetscViewerRestoreSubViewer(viewer,PETSC_COMM_SELF,&subviewer)); 53 54 PetscCall(PetscViewerBinaryWrite(viewer,&idata,1,PETSC_INT)); 55 PetscCall(PetscViewerBinaryWrite(viewer,&rdata,1,PETSC_REAL)); 56 PetscFunctionReturn(0); 57 } 58 59 static PetscErrorCode TestRead(PetscViewer viewer) 60 { 61 PetscInt idata = 0; 62 PetscReal rdata = 0; 63 PetscInt s = PETSC_DETERMINE, t = PETSC_DETERMINE; 64 PetscViewer subviewer; 65 MPI_Comm comm = PetscObjectComm((PetscObject)viewer); 66 67 PetscFunctionBegin; 68 PetscCall(PetscViewerBinaryRead(viewer,&idata,1,NULL,PETSC_INT)); 69 PetscCall(PetscViewerBinaryRead(viewer,&rdata,1,NULL,PETSC_REAL)); 70 PetscCheck(idata == 42,comm,PETSC_ERR_FILE_UNEXPECTED,"Unexpected idata=%" PetscInt_FMT,idata); 71 PetscCheck(rdata == 42,comm,PETSC_ERR_FILE_UNEXPECTED,"Unexpected rdata=%g",(double)rdata); 72 73 PetscCall(PetscViewerGetSubViewer(viewer,PETSC_COMM_SELF,&subviewer)); 74 if (subviewer) { 75 MPI_Comm subcomm = PetscObjectComm((PetscObject)subviewer); 76 PetscCall(PetscViewerBinaryRead(subviewer,&idata,1,NULL,PETSC_INT)); 77 PetscCall(PetscViewerBinaryRead(subviewer,&rdata,1,NULL,PETSC_REAL)); 78 PetscCheck(idata == 42,subcomm,PETSC_ERR_FILE_UNEXPECTED,"Unexpected idata=%" PetscInt_FMT,idata); 79 PetscCheck(rdata == 42,subcomm,PETSC_ERR_FILE_UNEXPECTED,"Unexpected rdata=%g",(double)rdata); 80 } 81 PetscCall(PetscViewerRestoreSubViewer(viewer,PETSC_COMM_SELF,&subviewer)); 82 83 PetscCall(PetscViewerBinaryReadAll(viewer,&idata,1,s,t,PETSC_INT)); 84 PetscCall(PetscViewerBinaryReadAll(viewer,&rdata,1,s,t,PETSC_REAL)); 85 PetscCheck(idata == 42,comm,PETSC_ERR_FILE_UNEXPECTED,"Unexpected idata=%" PetscInt_FMT,idata); 86 PetscCheck(rdata == 42,comm,PETSC_ERR_FILE_UNEXPECTED,"Unexpected rdata=%g",(double)rdata); 87 88 PetscCall(PetscViewerGetSubViewer(viewer,PETSC_COMM_SELF,&subviewer)); 89 if (subviewer) { 90 MPI_Comm subcomm = PetscObjectComm((PetscObject)subviewer); 91 PetscCall(PetscViewerBinaryRead(subviewer,&idata,1,NULL,PETSC_INT)); 92 PetscCall(PetscViewerBinaryRead(subviewer,&rdata,1,NULL,PETSC_REAL)); 93 PetscCheck(idata == 42,subcomm,PETSC_ERR_FILE_UNEXPECTED,"Unexpected idata=%" PetscInt_FMT,idata); 94 PetscCheck(rdata == 42,subcomm,PETSC_ERR_FILE_UNEXPECTED,"Unexpected rdata=%g",(double)rdata); 95 } 96 PetscCall(PetscViewerRestoreSubViewer(viewer,PETSC_COMM_SELF,&subviewer)); 97 98 PetscCall(PetscViewerBinaryRead(viewer,&idata,1,NULL,PETSC_INT)); 99 PetscCall(PetscViewerBinaryRead(viewer,&rdata,1,NULL,PETSC_REAL)); 100 PetscCheck(idata == 42,comm,PETSC_ERR_FILE_UNEXPECTED,"Unexpected idata=%" PetscInt_FMT,idata); 101 PetscCheck(rdata == 42,comm,PETSC_ERR_FILE_UNEXPECTED,"Unexpected rdata=%g",(double)rdata); 102 PetscFunctionReturn(0); 103 } 104 105 static PetscErrorCode TestEOF(PetscViewer viewer) 106 { 107 char data; 108 PetscInt count = PETSC_MAX_INT; 109 MPI_Comm comm = PetscObjectComm((PetscObject)viewer); 110 111 PetscFunctionBegin; 112 PetscCall(PetscViewerRead(viewer,&data,1,&count,PETSC_CHAR)); 113 PetscCheck(!count,comm,PETSC_ERR_FILE_UNEXPECTED,"Expected EOF"); 114 PetscFunctionReturn(0); 115 } 116 117 static PetscErrorCode TestClose(PetscViewer *viewer) 118 { 119 PetscFileMode mode; 120 121 PetscFunctionBegin; 122 PetscCall(PetscViewerFileGetMode(*viewer,&mode)); 123 if (mode == FILE_MODE_READ) PetscCall(TestEOF(*viewer)); 124 PetscCall(PetscViewerDestroy(viewer)); 125 PetscFunctionReturn(0); 126 } 127 128 int main(int argc,char **args) 129 { 130 PetscViewer viewer; 131 132 PetscCall(PetscInitialize(&argc,&args,NULL,help)); 133 134 PetscCall(TestOpen(FILE_MODE_WRITE,&viewer)); 135 PetscCall(TestWrite(viewer)); 136 PetscCall(TestClose(&viewer)); 137 138 PetscCall(TestOpen(FILE_MODE_READ,&viewer)); 139 PetscCall(TestRead(viewer)); 140 PetscCall(TestClose(&viewer)); 141 142 PetscCall(TestOpen(FILE_MODE_APPEND,&viewer)); 143 PetscCall(TestWrite(viewer)); 144 PetscCall(TestClose(&viewer)); 145 146 PetscCall(TestOpen(FILE_MODE_READ,&viewer)); 147 PetscCall(TestRead(viewer)); 148 PetscCall(TestRead(viewer)); 149 PetscCall(TestClose(&viewer)); 150 151 PetscCall(TestOpen(FILE_MODE_APPEND,&viewer)); 152 PetscCall(TestWrite(viewer)); 153 PetscCall(TestClose(&viewer)); 154 155 PetscCall(TestOpen(FILE_MODE_READ,&viewer)); 156 PetscCall(TestRead(viewer)); 157 PetscCall(TestRead(viewer)); 158 PetscCall(TestRead(viewer)); 159 PetscCall(TestClose(&viewer)); 160 161 PetscCall(TestOpen(FILE_MODE_WRITE,&viewer)); 162 PetscCall(TestWrite(viewer)); 163 PetscCall(TestClose(&viewer)); 164 165 PetscCall(TestOpen(FILE_MODE_READ,&viewer)); 166 PetscCall(TestRead(viewer)); 167 PetscCall(TestClose(&viewer)); 168 169 PetscCall(TestOpen(FILE_MODE_WRITE,&viewer)); 170 PetscCall(TestClose(&viewer)); 171 PetscCall(TestOpen(FILE_MODE_READ,&viewer)); 172 PetscCall(TestClose(&viewer)); 173 PetscCall(TestOpen(FILE_MODE_APPEND,&viewer)); 174 PetscCall(TestClose(&viewer)); 175 PetscCall(TestOpen(FILE_MODE_READ,&viewer)); 176 PetscCall(TestClose(&viewer)); 177 178 { 179 FILE *info; 180 PetscMPIInt rank; 181 182 PetscCall(TestOpen(FILE_MODE_WRITE,&viewer)); 183 PetscCall(PetscViewerPushFormat(viewer,PETSC_VIEWER_BINARY_MATLAB)); 184 PetscCall(PetscViewerBinaryGetInfoPointer(viewer,&info)); 185 PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer),&rank)); 186 PetscCheckFalse(rank == 0 && !info,PETSC_COMM_SELF,PETSC_ERR_PLIB,"Missing info pointer"); 187 PetscCall(TestClose(&viewer)); 188 } 189 190 PetscCall(PetscFinalize()); 191 return 0; 192 } 193 194 /*TEST 195 196 testset: 197 nsize: {{1 2 3}separate_output} 198 args: -viewer_view 199 test: 200 suffix: stdio 201 args: -viewer_binary_mpiio 0 202 test: 203 requires: mpiio 204 suffix: mpiio 205 args: -viewer_binary_mpiio 1 206 207 TEST*/ 208