Newer
Older
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
#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, const string &N)
: Data(MNE + G.requestedExtraSpace()/sizeof(T)) {
G.addVariable(N, Data, true);
}
virtual void print(ostream &os, size_t i) {
os << scientific << setprecision(numeric_limits<T>::digits10) << Data[i];
}
protected:
vector<T> Data;
};
template <typename T>
PrinterBase *addPrinter(GenericIO::VariableInfo &V,
GenericIO &GIO, size_t MNE) {
if (sizeof(T) != V.Size)
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.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(false, -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) {
cout << VI[i].Name;
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;
}