-
Notifications
You must be signed in to change notification settings - Fork 1
/
GenericIOPrint.cxx
334 lines (291 loc) · 9.39 KB
/
GenericIOPrint.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* Copyright (C) 2015, UChicago Argonne, LLC
* All Rights Reserved
*
* Generic IO (ANL-15-066)
* Hal Finkel, Argonne National Laboratory
*
* OPEN SOURCE LICENSE
*
* Under the terms of Contract No. DE-AC02-06CH11357 with UChicago Argonne,
* LLC, the U.S. Government retains certain rights in this software.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the names of UChicago Argonne, LLC or the Department of Energy
* nor the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* *****************************************************************************
*
* DISCLAIMER
* THE SOFTWARE IS SUPPLIED “AS IS” WITHOUT WARRANTY OF ANY KIND. NEITHER THE
* UNTED STATES GOVERNMENT, NOR THE UNITED STATES DEPARTMENT OF ENERGY, NOR
* UCHICAGO ARGONNE, LLC, NOR ANY OF THEIR EMPLOYEES, MAKES ANY WARRANTY,
* EXPRESS OR IMPLIED, OR ASSUMES ANY LEGAL LIABILITY OR RESPONSIBILITY FOR THE
* ACCURACY, COMPLETENESS, OR USEFULNESS OF ANY INFORMATION, DATA, APPARATUS,
* PRODUCT, OR PROCESS DISCLOSED, OR REPRESENTS THAT ITS USE WOULD NOT INFRINGE
* PRIVATELY OWNED RIGHTS.
*
* *****************************************************************************
*/
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <limits>
#include <stdexcept>
#include <stdint.h>
#include "GenericIO.h"
using namespace std;
using namespace gio;
class PrinterBase {
public:
virtual ~PrinterBase() {}
virtual void print(ostream &os, size_t i) = 0;
};
template <class T>
class Printer : public PrinterBase {
public:
Printer(GenericIO &G, size_t MNE, size_t NE, const string &N)
: NumElements(NE), Data(MNE*NE + G.requestedExtraSpace()/sizeof(T)) {
G.addScalarizedVariable(N, Data, NE, GenericIO::VarHasExtraSpace);
}
virtual void print(ostream &os, size_t i) {
for (size_t j = 0; j < NumElements; ++j) {
os << scientific << setprecision(numeric_limits<T>::digits10) <<
Data[i*NumElements + j];
if (j != NumElements - 1)
os << "\t";
}
}
protected:
size_t NumElements;
vector<T> Data;
};
template <typename T>
PrinterBase *addPrinter(GenericIO::VariableInfo &V,
GenericIO &GIO, size_t MNE) {
if (sizeof(T) != V.ElementSize)
return 0;
if (V.IsFloat == numeric_limits<T>::is_integer)
return 0;
if (V.IsSigned != numeric_limits<T>::is_signed)
return 0;
return new Printer<T>(GIO, MNE, V.Size/V.ElementSize, V.Name);
}
int main(int argc, char *argv[]) {
#ifndef GENERICIO_NO_MPI
MPI_Init(&argc, &argv);
#endif
bool ShowMap = false;
bool NoData = false;
bool PrintRankInfo = true;
int FileNameIdx = 1;
if (argc > 2) {
if (string(argv[1]) == "--no-rank-info") {
PrintRankInfo = false;
++FileNameIdx;
--argc;
} else if (string(argv[1]) == "--no-data") {
NoData = true;
++FileNameIdx;
--argc;
} else if (string(argv[1]) == "--show-map") {
ShowMap = true;
++FileNameIdx;
--argc;
}
}
if (argc != 2) {
cerr << "Usage: " << argv[0] << " [--no-rank-info|--no-data|--show-map] <mpiioName>" << endl;
exit(-1);
}
string FileName(argv[FileNameIdx]);
int Rank, NRanks;
#ifndef GENERICIO_NO_MPI
MPI_Comm_rank(MPI_COMM_WORLD, &Rank);
MPI_Comm_size(MPI_COMM_WORLD, &NRanks);
#else
Rank = 0;
NRanks = 1;
#endif
if (Rank == 0) {
unsigned Method = GenericIO::FileIOPOSIX;
#ifndef GENERICIO_NO_MPI
const char *EnvStr = getenv("GENERICIO_USE_MPIIO");
if (EnvStr && string(EnvStr) == "1")
Method = GenericIO::FileIOMPI;
#endif
#ifndef GENERICIO_NO_MPI
GenericIO GIO(MPI_COMM_SELF, FileName, Method);
#else
GenericIO GIO(FileName, Method);
#endif
GIO.openAndReadHeader(GenericIO::MismatchAllowed, -1, !ShowMap);
int NR = GIO.readNRanks();
vector<GenericIO::VariableInfo> VI;
GIO.getVariableInfo(VI);
size_t MaxNElem = 0;
for (int i = 0; i < NR; ++i) {
size_t NElem = GIO.readNumElems(i);
MaxNElem = max(MaxNElem, NElem);
}
vector<PrinterBase *> Printers;
for (size_t i = 0; i < VI.size(); ++i) {
PrinterBase *P = 0;
#define ADD_PRINTER(T) \
if (!P) P = addPrinter<T>(VI[i], GIO, MaxNElem)
ADD_PRINTER(float);
ADD_PRINTER(double);
ADD_PRINTER(unsigned char);
ADD_PRINTER(signed char);
ADD_PRINTER(int16_t);
ADD_PRINTER(uint16_t);
ADD_PRINTER(int32_t);
ADD_PRINTER(uint32_t);
ADD_PRINTER(int64_t);
ADD_PRINTER(uint64_t);
#undef ADD_PRINTER
if (!P) throw runtime_error("Don't know how to print variable: " + VI[i].Name);
Printers.push_back(P);
}
int Dims[3];
GIO.readDims(Dims);
cout << "# " << FileName << ": " << NR << " rank(s): " <<
Dims[0] << "x" << Dims[1] << "x" << Dims[2];
uint64_t TotalNumElems = GIO.readTotalNumElems();
if (TotalNumElems != (uint64_t) -1)
cout << ": " << GIO.readTotalNumElems() << " row(s)";
cout << endl;
double PhysOrigin[3], PhysScale[3];
GIO.readPhysOrigin(PhysOrigin);
GIO.readPhysScale(PhysScale);
if (PhysScale[0] != 0.0 || PhysScale[1] != 0.0 || PhysScale[2] != 0.0) {
cout << "# physical coordinates: (" << PhysOrigin[0] << "," <<
PhysOrigin[1] << "," << PhysOrigin[2] << ") -> (" <<
PhysScale[0] << "," << PhysScale[1] << "," <<
PhysScale[2] << ")" << endl;
vector<GenericIO::VariableInfo> VIX, VIY, VIZ;
for (size_t i = 0; i < VI.size(); ++i) {
if (VI[i].IsPhysCoordX) VIX.push_back(VI[i]);
if (VI[i].IsPhysCoordY) VIY.push_back(VI[i]);
if (VI[i].IsPhysCoordZ) VIZ.push_back(VI[i]);
}
if (!VIX.empty()) {
cout << "# x variables: ";
for (size_t i = 0; i < VIX.size(); ++i) {
if (i != 0) cout << ", ";
cout << VIX[i].Name;
if (VIX[i].MaybePhysGhost)
cout << " [maybe ghost]";
}
cout << endl;
}
if (!VIY.empty()) {
cout << "# y variables: ";
for (size_t i = 0; i < VIY.size(); ++i) {
if (i != 0) cout << ", ";
cout << VIY[i].Name;
if (VIY[i].MaybePhysGhost)
cout << " [maybe ghost]";
}
cout << endl;
}
if (!VIZ.empty()) {
cout << "# z variables: ";
for (size_t i = 0; i < VIZ.size(); ++i) {
if (i != 0) cout << ", ";
cout << VIZ[i].Name;
if (VIZ[i].MaybePhysGhost)
cout << " [maybe ghost]";
}
cout << endl;
}
}
cout << "# ";
for (size_t i = 0; i < VI.size(); ++i) {
if (VI[i].Size == VI[i].ElementSize) {
cout << VI[i].Name;
} else {
size_t NumElements = VI[i].Size/VI[i].ElementSize;
for (size_t j = 0; j < NumElements; ++j) {
cout << VI[i].Name;
if (j == 0) {
cout << ".x";
} else if (j == 1) {
cout << ".y";
} else if (j == 2) {
cout << ".z";
} else if (j == 3) {
cout << ".w";
} else {
cout << ".w" << (j - 3);
}
if (j != NumElements - 1)
cout << "\t";
}
}
if (i != VI.size() - 1)
cout << "\t";
}
cout << endl;
cout << "# ";
for (size_t i = 0; i < VI.size(); ++i) {
if (VI[i].Size == VI[i].ElementSize) {
cout << "(" << (VI[i].IsFloat ? "f" :
(VI[i].IsSigned ? "s" : "u")) << 8*VI[i].Size << ")";
} else {
size_t NumElements = VI[i].Size/VI[i].ElementSize;
for (size_t j = 0; j < NumElements; ++j) {
cout << "(" << (VI[i].IsFloat ? "f" :
(VI[i].IsSigned ? "s" : "u")) <<
8*VI[i].ElementSize << ")";
if (j != NumElements - 1)
cout << "\t";
}
}
if (i != VI.size() - 1)
cout << "\t";
}
cout << endl;
for (int i = 0; i < NR; ++i) {
size_t NElem = GIO.readNumElems(i);
int Coords[3];
GIO.readCoords(Coords, i);
if (PrintRankInfo)
cout << "# rank " << GIO.readGlobalRankNumber(i) << ": " <<
Coords[0] << "," << Coords[1] << "," <<
Coords[2] << ": " << NElem << " row(s)" << endl;
if (NoData)
continue;
GIO.readData(i, false);
for (size_t j = 0; j < NElem; ++j) {
for (size_t k = 0; k < Printers.size(); ++k) {
Printers[k]->print(cout, j);
if (k != Printers.size() - 1)
cout << "\t";
}
cout << endl;
}
}
for (size_t i = 0; i < Printers.size(); ++i) {
delete Printers[i];
}
}
#ifndef GENERICIO_NO_MPI
MPI_Barrier(MPI_COMM_WORLD);
MPI_Finalize();
#endif
return 0;
}