source: GenericIOPrint.cxx @ 00587dc

Revision 00587dc, 6.0 KB checked in by Hal Finkel <hfinkel@…>, 10 years ago (diff)

Initial Commit (gio-base-20150317)

  • Property mode set to 100644
Line 
1#include <cstdlib>
2#include <iostream>
3#include <iomanip>
4#include <string>
5#include <algorithm>
6#include <limits>
7#include <stdexcept>
8#include <stdint.h>
9
10#include "GenericIO.h"
11
12using namespace std;
13using namespace gio;
14
15class PrinterBase {
16public:
17  virtual ~PrinterBase() {}
18  virtual void print(ostream &os, size_t i) = 0;
19};
20
21template <class T>
22class Printer : public PrinterBase {
23public:
24  Printer(GenericIO &G, size_t MNE, const string &N)
25    : Data(MNE + G.requestedExtraSpace()/sizeof(T)) {
26    G.addVariable(N, Data, true);
27  }
28
29  virtual void print(ostream &os, size_t i) {
30    os << scientific << setprecision(numeric_limits<T>::digits10) << Data[i];
31  }
32
33protected:
34  vector<T> Data;
35};
36
37template <typename T>
38PrinterBase *addPrinter(GenericIO::VariableInfo &V,
39                GenericIO &GIO, size_t MNE) {
40  if (sizeof(T) != V.Size)
41    return 0;
42
43  if (V.IsFloat == numeric_limits<T>::is_integer)
44    return 0;
45  if (V.IsSigned != numeric_limits<T>::is_signed)
46    return 0;
47
48  return new Printer<T>(GIO, MNE, V.Name);
49}
50
51int main(int argc, char *argv[]) {
52#ifndef GENERICIO_NO_MPI
53  MPI_Init(&argc, &argv);
54#endif
55
56  bool ShowMap = false;
57  bool NoData = false;
58  bool PrintRankInfo = true;
59  int FileNameIdx = 1;
60  if (argc > 2) {
61    if (string(argv[1]) == "--no-rank-info") {
62      PrintRankInfo = false;
63      ++FileNameIdx;
64      --argc;
65    } else if (string(argv[1]) == "--no-data") {
66      NoData = true;
67      ++FileNameIdx;
68      --argc;
69    } else if (string(argv[1]) == "--show-map") {
70      ShowMap = true;
71      ++FileNameIdx;
72      --argc;
73    }
74  }
75
76  if (argc != 2) {
77    cerr << "Usage: " << argv[0] << " [--no-rank-info|--no-data|--show-map] <mpiioName>" << endl;
78    exit(-1);
79  }
80
81  string FileName(argv[FileNameIdx]);
82
83  int Rank, NRanks;
84#ifndef GENERICIO_NO_MPI
85  MPI_Comm_rank(MPI_COMM_WORLD, &Rank);
86  MPI_Comm_size(MPI_COMM_WORLD, &NRanks);
87#else
88  Rank = 0;
89  NRanks = 1;
90#endif
91
92  if (Rank == 0) {
93    unsigned Method = GenericIO::FileIOPOSIX;
94#ifndef GENERICIO_NO_MPI
95    const char *EnvStr = getenv("GENERICIO_USE_MPIIO");
96    if (EnvStr && string(EnvStr) == "1")
97      Method = GenericIO::FileIOMPI;
98#endif
99
100#ifndef GENERICIO_NO_MPI
101    GenericIO GIO(MPI_COMM_SELF, FileName, Method);
102#else
103    GenericIO GIO(FileName, Method);
104#endif
105    GIO.openAndReadHeader(false, -1, !ShowMap);
106
107    int NR = GIO.readNRanks();
108
109    vector<GenericIO::VariableInfo> VI;
110    GIO.getVariableInfo(VI);
111
112    size_t MaxNElem = 0;
113    for (int i = 0; i < NR; ++i) {
114      size_t NElem = GIO.readNumElems(i);
115      MaxNElem = max(MaxNElem, NElem);
116    }
117
118    vector<PrinterBase *> Printers;
119    for (size_t i = 0; i < VI.size(); ++i) {
120      PrinterBase *P = 0;
121
122#define ADD_PRINTER(T) \
123      if (!P) P = addPrinter<T>(VI[i], GIO, MaxNElem)
124      ADD_PRINTER(float);
125      ADD_PRINTER(double);
126      ADD_PRINTER(unsigned char);
127      ADD_PRINTER(signed char);
128      ADD_PRINTER(int16_t);
129      ADD_PRINTER(uint16_t);
130      ADD_PRINTER(int32_t);
131      ADD_PRINTER(uint32_t);
132      ADD_PRINTER(int64_t);
133      ADD_PRINTER(uint64_t);
134#undef ADD_PRINTER
135
136      if (!P) throw runtime_error("Don't know how to print variable: " + VI[i].Name);
137      Printers.push_back(P);
138    }
139
140    int Dims[3];
141    GIO.readDims(Dims);
142
143    cout << "# " << FileName << ": " << NR << " rank(s): " <<
144            Dims[0] << "x" << Dims[1] << "x" << Dims[2];
145
146    uint64_t TotalNumElems = GIO.readTotalNumElems();
147    if (TotalNumElems != (uint64_t) -1)
148      cout  << ": " << GIO.readTotalNumElems() << " row(s)";
149    cout << endl;
150
151    double PhysOrigin[3], PhysScale[3];
152    GIO.readPhysOrigin(PhysOrigin);
153    GIO.readPhysScale(PhysScale);
154    if (PhysScale[0] != 0.0 || PhysScale[1] != 0.0 || PhysScale[2] != 0.0) {
155      cout << "# physical coordinates: (" << PhysOrigin[0] << "," <<
156              PhysOrigin[1] << "," << PhysOrigin[2] << ") -> (" <<
157              PhysScale[0] << "," << PhysScale[1] << "," <<
158              PhysScale[2] << ")" << endl;
159
160
161      vector<GenericIO::VariableInfo> VIX, VIY, VIZ;
162      for (size_t i = 0; i < VI.size(); ++i) {
163        if (VI[i].IsPhysCoordX) VIX.push_back(VI[i]);
164        if (VI[i].IsPhysCoordY) VIY.push_back(VI[i]);
165        if (VI[i].IsPhysCoordZ) VIZ.push_back(VI[i]);
166      }
167
168      if (!VIX.empty()) {
169        cout << "# x variables: ";
170        for (size_t i = 0; i < VIX.size(); ++i) {
171          if (i != 0) cout << ", ";
172          cout << VIX[i].Name;
173          if (VIX[i].MaybePhysGhost)
174            cout << " [maybe ghost]";
175        }
176        cout << endl;
177      }
178      if (!VIY.empty()) {
179        cout << "# y variables: ";
180        for (size_t i = 0; i < VIY.size(); ++i) {
181          if (i != 0) cout << ", ";
182          cout << VIY[i].Name;
183          if (VIY[i].MaybePhysGhost)
184            cout << " [maybe ghost]";
185        }
186        cout << endl;
187      }
188      if (!VIZ.empty()) {
189        cout << "# z variables: ";
190        for (size_t i = 0; i < VIZ.size(); ++i) {
191          if (i != 0) cout << ", ";
192          cout << VIZ[i].Name;
193          if (VIZ[i].MaybePhysGhost)
194            cout << " [maybe ghost]";
195        }
196        cout << endl;
197      }
198    }
199
200    cout << "# ";
201    for (size_t i = 0; i < VI.size(); ++i) {
202      cout << VI[i].Name;
203      if (i != VI.size() - 1)
204        cout << "\t";
205    }
206    cout << endl;
207
208    for (int i = 0; i < NR; ++i) {
209      size_t NElem = GIO.readNumElems(i);
210      int Coords[3];
211      GIO.readCoords(Coords, i);
212      if (PrintRankInfo)
213        cout << "# rank " << GIO.readGlobalRankNumber(i) << ": " <<
214                Coords[0] << "," << Coords[1] << "," <<
215                Coords[2] << ": " << NElem << " row(s)" << endl;
216      if (NoData)
217        continue;
218
219      GIO.readData(i, false);
220      for (size_t j = 0; j < NElem; ++j) {
221        for (size_t k = 0; k < Printers.size(); ++k) {
222          Printers[k]->print(cout, j);
223          if (k != Printers.size() - 1)
224            cout << "\t";
225        }
226        cout << endl;
227      }
228    }
229
230    for (size_t i = 0; i < Printers.size(); ++i) {
231      delete Printers[i];
232    }
233  }
234
235#ifndef GENERICIO_NO_MPI
236  MPI_Barrier(MPI_COMM_WORLD);
237  MPI_Finalize();
238#endif
239
240  return 0;
241}
242
Note: See TracBrowser for help on using the repository browser.