source: GenericIOPrint.cxx @ da65757

Revision da65757, 7.9 KB checked in by Hal Finkel <hfinkel@…>, 9 years ago (diff)

add license to all files

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