xref: /petsc/src/sys/classes/viewer/tests/ex6.c (revision 0baf8eba40dbc839082666f9f7396a225d6f663c)
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(PETSC_SUCCESS);
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(PETSC_SUCCESS);
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(PETSC_SUCCESS);
103 }
104 
105 static PetscErrorCode TestEOF(PetscViewer viewer)
106 {
107   char     data;
108   PetscInt count = PETSC_INT_MAX;
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(PETSC_SUCCESS);
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(PETSC_SUCCESS);
126 }
127 
128 int main(int argc, char **args)
129 {
130   PetscViewer viewer;
131 
132   PetscFunctionBeginUser;
133   PetscCall(PetscInitialize(&argc, &args, NULL, help));
134 
135   PetscCall(TestOpen(FILE_MODE_WRITE, &viewer));
136   PetscCall(TestWrite(viewer));
137   PetscCall(TestClose(&viewer));
138 
139   PetscCall(TestOpen(FILE_MODE_READ, &viewer));
140   PetscCall(TestRead(viewer));
141   PetscCall(TestClose(&viewer));
142 
143   PetscCall(TestOpen(FILE_MODE_APPEND, &viewer));
144   PetscCall(TestWrite(viewer));
145   PetscCall(TestClose(&viewer));
146 
147   PetscCall(TestOpen(FILE_MODE_READ, &viewer));
148   PetscCall(TestRead(viewer));
149   PetscCall(TestRead(viewer));
150   PetscCall(TestClose(&viewer));
151 
152   PetscCall(TestOpen(FILE_MODE_APPEND, &viewer));
153   PetscCall(TestWrite(viewer));
154   PetscCall(TestClose(&viewer));
155 
156   PetscCall(TestOpen(FILE_MODE_READ, &viewer));
157   PetscCall(TestRead(viewer));
158   PetscCall(TestRead(viewer));
159   PetscCall(TestRead(viewer));
160   PetscCall(TestClose(&viewer));
161 
162   PetscCall(TestOpen(FILE_MODE_WRITE, &viewer));
163   PetscCall(TestWrite(viewer));
164   PetscCall(TestClose(&viewer));
165 
166   PetscCall(TestOpen(FILE_MODE_READ, &viewer));
167   PetscCall(TestRead(viewer));
168   PetscCall(TestClose(&viewer));
169 
170   PetscCall(TestOpen(FILE_MODE_WRITE, &viewer));
171   PetscCall(TestClose(&viewer));
172   PetscCall(TestOpen(FILE_MODE_READ, &viewer));
173   PetscCall(TestClose(&viewer));
174   PetscCall(TestOpen(FILE_MODE_APPEND, &viewer));
175   PetscCall(TestClose(&viewer));
176   PetscCall(TestOpen(FILE_MODE_READ, &viewer));
177   PetscCall(TestClose(&viewer));
178 
179   {
180     FILE       *info;
181     PetscMPIInt rank;
182 
183     PetscCall(TestOpen(FILE_MODE_WRITE, &viewer));
184     PetscCall(PetscViewerPushFormat(viewer, PETSC_VIEWER_BINARY_MATLAB));
185     PetscCall(PetscViewerBinaryGetInfoPointer(viewer, &info));
186     PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)viewer), &rank));
187     PetscCheck(rank != 0 || info, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Missing info pointer");
188     PetscCall(TestClose(&viewer));
189   }
190 
191   PetscCall(PetscFinalize());
192   return 0;
193 }
194 
195 /*TEST
196 
197    testset:
198      nsize: {{1 2 3}separate_output}
199      args: -viewer_view
200      test:
201        suffix: stdio
202        args: -viewer_binary_mpiio 0
203      test:
204        requires: mpiio
205        suffix: mpiio
206        args: -viewer_binary_mpiio 1
207 
208 TEST*/
209