[da65757] | 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 | |
---|
[00587dc] | 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 | |
---|
| 53 | using namespace std; |
---|
| 54 | using namespace gio; |
---|
| 55 | |
---|
| 56 | class PrinterBase { |
---|
| 57 | public: |
---|
| 58 | virtual ~PrinterBase() {} |
---|
| 59 | virtual void print(ostream &os, size_t i) = 0; |
---|
| 60 | }; |
---|
| 61 | |
---|
| 62 | template <class T> |
---|
| 63 | class Printer : public PrinterBase { |
---|
| 64 | public: |
---|
[5d57155] | 65 | Printer(GenericIO &G, size_t MNE, size_t NE, const string &N) |
---|
| 66 | : NumElements(NE), Data(MNE*NE + G.requestedExtraSpace()/sizeof(T)) { |
---|
| 67 | G.addScalarizedVariable(N, Data, NE, GenericIO::VarHasExtraSpace); |
---|
[00587dc] | 68 | } |
---|
| 69 | |
---|
| 70 | virtual void print(ostream &os, size_t i) { |
---|
[5d57155] | 71 | for (size_t j = 0; j < NumElements; ++j) { |
---|
| 72 | os << scientific << setprecision(numeric_limits<T>::digits10) << |
---|
| 73 | Data[i*NumElements + j]; |
---|
| 74 | |
---|
| 75 | if (j != NumElements - 1) |
---|
| 76 | os << "\t"; |
---|
| 77 | } |
---|
[00587dc] | 78 | } |
---|
| 79 | |
---|
| 80 | protected: |
---|
[5d57155] | 81 | size_t NumElements; |
---|
[00587dc] | 82 | vector<T> Data; |
---|
| 83 | }; |
---|
| 84 | |
---|
| 85 | template <typename T> |
---|
| 86 | PrinterBase *addPrinter(GenericIO::VariableInfo &V, |
---|
| 87 | GenericIO &GIO, size_t MNE) { |
---|
[5d57155] | 88 | if (sizeof(T) != V.ElementSize) |
---|
[00587dc] | 89 | return 0; |
---|
| 90 | |
---|
| 91 | if (V.IsFloat == numeric_limits<T>::is_integer) |
---|
| 92 | return 0; |
---|
| 93 | if (V.IsSigned != numeric_limits<T>::is_signed) |
---|
| 94 | return 0; |
---|
| 95 | |
---|
[5d57155] | 96 | return new Printer<T>(GIO, MNE, V.Size/V.ElementSize, V.Name); |
---|
[00587dc] | 97 | } |
---|
| 98 | |
---|
| 99 | int main(int argc, char *argv[]) { |
---|
| 100 | #ifndef GENERICIO_NO_MPI |
---|
| 101 | MPI_Init(&argc, &argv); |
---|
| 102 | #endif |
---|
| 103 | |
---|
| 104 | bool ShowMap = false; |
---|
| 105 | bool NoData = false; |
---|
| 106 | bool PrintRankInfo = true; |
---|
| 107 | int FileNameIdx = 1; |
---|
| 108 | if (argc > 2) { |
---|
| 109 | if (string(argv[1]) == "--no-rank-info") { |
---|
| 110 | PrintRankInfo = false; |
---|
| 111 | ++FileNameIdx; |
---|
| 112 | --argc; |
---|
| 113 | } else if (string(argv[1]) == "--no-data") { |
---|
| 114 | NoData = true; |
---|
| 115 | ++FileNameIdx; |
---|
| 116 | --argc; |
---|
| 117 | } else if (string(argv[1]) == "--show-map") { |
---|
| 118 | ShowMap = true; |
---|
| 119 | ++FileNameIdx; |
---|
| 120 | --argc; |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | if (argc != 2) { |
---|
| 125 | cerr << "Usage: " << argv[0] << " [--no-rank-info|--no-data|--show-map] <mpiioName>" << endl; |
---|
| 126 | exit(-1); |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | string FileName(argv[FileNameIdx]); |
---|
| 130 | |
---|
| 131 | int Rank, NRanks; |
---|
| 132 | #ifndef GENERICIO_NO_MPI |
---|
| 133 | MPI_Comm_rank(MPI_COMM_WORLD, &Rank); |
---|
| 134 | MPI_Comm_size(MPI_COMM_WORLD, &NRanks); |
---|
| 135 | #else |
---|
| 136 | Rank = 0; |
---|
| 137 | NRanks = 1; |
---|
| 138 | #endif |
---|
| 139 | |
---|
| 140 | if (Rank == 0) { |
---|
| 141 | unsigned Method = GenericIO::FileIOPOSIX; |
---|
| 142 | #ifndef GENERICIO_NO_MPI |
---|
| 143 | const char *EnvStr = getenv("GENERICIO_USE_MPIIO"); |
---|
| 144 | if (EnvStr && string(EnvStr) == "1") |
---|
| 145 | Method = GenericIO::FileIOMPI; |
---|
| 146 | #endif |
---|
| 147 | |
---|
| 148 | #ifndef GENERICIO_NO_MPI |
---|
| 149 | GenericIO GIO(MPI_COMM_SELF, FileName, Method); |
---|
| 150 | #else |
---|
| 151 | GenericIO GIO(FileName, Method); |
---|
| 152 | #endif |
---|
[8f0a211] | 153 | GIO.openAndReadHeader(GenericIO::MismatchAllowed, -1, !ShowMap); |
---|
[00587dc] | 154 | |
---|
| 155 | int NR = GIO.readNRanks(); |
---|
| 156 | |
---|
| 157 | vector<GenericIO::VariableInfo> VI; |
---|
| 158 | GIO.getVariableInfo(VI); |
---|
| 159 | |
---|
| 160 | size_t MaxNElem = 0; |
---|
| 161 | for (int i = 0; i < NR; ++i) { |
---|
| 162 | size_t NElem = GIO.readNumElems(i); |
---|
| 163 | MaxNElem = max(MaxNElem, NElem); |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | vector<PrinterBase *> Printers; |
---|
| 167 | for (size_t i = 0; i < VI.size(); ++i) { |
---|
| 168 | PrinterBase *P = 0; |
---|
| 169 | |
---|
| 170 | #define ADD_PRINTER(T) \ |
---|
| 171 | if (!P) P = addPrinter<T>(VI[i], GIO, MaxNElem) |
---|
| 172 | ADD_PRINTER(float); |
---|
| 173 | ADD_PRINTER(double); |
---|
| 174 | ADD_PRINTER(unsigned char); |
---|
| 175 | ADD_PRINTER(signed char); |
---|
| 176 | ADD_PRINTER(int16_t); |
---|
| 177 | ADD_PRINTER(uint16_t); |
---|
| 178 | ADD_PRINTER(int32_t); |
---|
| 179 | ADD_PRINTER(uint32_t); |
---|
| 180 | ADD_PRINTER(int64_t); |
---|
| 181 | ADD_PRINTER(uint64_t); |
---|
| 182 | #undef ADD_PRINTER |
---|
| 183 | |
---|
| 184 | if (!P) throw runtime_error("Don't know how to print variable: " + VI[i].Name); |
---|
| 185 | Printers.push_back(P); |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | int Dims[3]; |
---|
| 189 | GIO.readDims(Dims); |
---|
| 190 | |
---|
| 191 | cout << "# " << FileName << ": " << NR << " rank(s): " << |
---|
| 192 | Dims[0] << "x" << Dims[1] << "x" << Dims[2]; |
---|
| 193 | |
---|
| 194 | uint64_t TotalNumElems = GIO.readTotalNumElems(); |
---|
| 195 | if (TotalNumElems != (uint64_t) -1) |
---|
| 196 | cout << ": " << GIO.readTotalNumElems() << " row(s)"; |
---|
| 197 | cout << endl; |
---|
| 198 | |
---|
| 199 | double PhysOrigin[3], PhysScale[3]; |
---|
| 200 | GIO.readPhysOrigin(PhysOrigin); |
---|
| 201 | GIO.readPhysScale(PhysScale); |
---|
| 202 | if (PhysScale[0] != 0.0 || PhysScale[1] != 0.0 || PhysScale[2] != 0.0) { |
---|
| 203 | cout << "# physical coordinates: (" << PhysOrigin[0] << "," << |
---|
| 204 | PhysOrigin[1] << "," << PhysOrigin[2] << ") -> (" << |
---|
| 205 | PhysScale[0] << "," << PhysScale[1] << "," << |
---|
| 206 | PhysScale[2] << ")" << endl; |
---|
| 207 | |
---|
| 208 | |
---|
| 209 | vector<GenericIO::VariableInfo> VIX, VIY, VIZ; |
---|
| 210 | for (size_t i = 0; i < VI.size(); ++i) { |
---|
| 211 | if (VI[i].IsPhysCoordX) VIX.push_back(VI[i]); |
---|
| 212 | if (VI[i].IsPhysCoordY) VIY.push_back(VI[i]); |
---|
| 213 | if (VI[i].IsPhysCoordZ) VIZ.push_back(VI[i]); |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | if (!VIX.empty()) { |
---|
| 217 | cout << "# x variables: "; |
---|
| 218 | for (size_t i = 0; i < VIX.size(); ++i) { |
---|
| 219 | if (i != 0) cout << ", "; |
---|
| 220 | cout << VIX[i].Name; |
---|
| 221 | if (VIX[i].MaybePhysGhost) |
---|
| 222 | cout << " [maybe ghost]"; |
---|
| 223 | } |
---|
| 224 | cout << endl; |
---|
| 225 | } |
---|
| 226 | if (!VIY.empty()) { |
---|
| 227 | cout << "# y variables: "; |
---|
| 228 | for (size_t i = 0; i < VIY.size(); ++i) { |
---|
| 229 | if (i != 0) cout << ", "; |
---|
| 230 | cout << VIY[i].Name; |
---|
| 231 | if (VIY[i].MaybePhysGhost) |
---|
| 232 | cout << " [maybe ghost]"; |
---|
| 233 | } |
---|
| 234 | cout << endl; |
---|
| 235 | } |
---|
| 236 | if (!VIZ.empty()) { |
---|
| 237 | cout << "# z variables: "; |
---|
| 238 | for (size_t i = 0; i < VIZ.size(); ++i) { |
---|
| 239 | if (i != 0) cout << ", "; |
---|
| 240 | cout << VIZ[i].Name; |
---|
| 241 | if (VIZ[i].MaybePhysGhost) |
---|
| 242 | cout << " [maybe ghost]"; |
---|
| 243 | } |
---|
| 244 | cout << endl; |
---|
| 245 | } |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | cout << "# "; |
---|
| 249 | for (size_t i = 0; i < VI.size(); ++i) { |
---|
[5d57155] | 250 | if (VI[i].Size == VI[i].ElementSize) { |
---|
| 251 | cout << VI[i].Name; |
---|
| 252 | } else { |
---|
| 253 | size_t NumElements = VI[i].Size/VI[i].ElementSize; |
---|
| 254 | for (size_t j = 0; j < NumElements; ++j) { |
---|
| 255 | cout << VI[i].Name; |
---|
| 256 | if (j == 0) { |
---|
| 257 | cout << ".x"; |
---|
| 258 | } else if (j == 1) { |
---|
| 259 | cout << ".y"; |
---|
| 260 | } else if (j == 2) { |
---|
| 261 | cout << ".z"; |
---|
| 262 | } else if (j == 3) { |
---|
| 263 | cout << ".w"; |
---|
| 264 | } else { |
---|
| 265 | cout << ".w" << (j - 3); |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | if (j != NumElements - 1) |
---|
| 269 | cout << "\t"; |
---|
| 270 | } |
---|
| 271 | } |
---|
| 272 | |
---|
[00587dc] | 273 | if (i != VI.size() - 1) |
---|
| 274 | cout << "\t"; |
---|
| 275 | } |
---|
| 276 | cout << endl; |
---|
| 277 | |
---|
[b02d091] | 278 | cout << "# "; |
---|
| 279 | for (size_t i = 0; i < VI.size(); ++i) { |
---|
| 280 | if (VI[i].Size == VI[i].ElementSize) { |
---|
| 281 | cout << "(" << (VI[i].IsFloat ? "f" : |
---|
| 282 | (VI[i].IsSigned ? "s" : "u")) << 8*VI[i].Size << ")"; |
---|
| 283 | } else { |
---|
| 284 | size_t NumElements = VI[i].Size/VI[i].ElementSize; |
---|
| 285 | for (size_t j = 0; j < NumElements; ++j) { |
---|
| 286 | cout << "(" << (VI[i].IsFloat ? "f" : |
---|
| 287 | (VI[i].IsSigned ? "s" : "u")) << |
---|
| 288 | 8*VI[i].ElementSize << ")"; |
---|
| 289 | |
---|
| 290 | if (j != NumElements - 1) |
---|
| 291 | cout << "\t"; |
---|
| 292 | } |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | if (i != VI.size() - 1) |
---|
| 296 | cout << "\t"; |
---|
| 297 | } |
---|
| 298 | cout << endl; |
---|
| 299 | |
---|
[00587dc] | 300 | for (int i = 0; i < NR; ++i) { |
---|
| 301 | size_t NElem = GIO.readNumElems(i); |
---|
| 302 | int Coords[3]; |
---|
| 303 | GIO.readCoords(Coords, i); |
---|
| 304 | if (PrintRankInfo) |
---|
| 305 | cout << "# rank " << GIO.readGlobalRankNumber(i) << ": " << |
---|
| 306 | Coords[0] << "," << Coords[1] << "," << |
---|
| 307 | Coords[2] << ": " << NElem << " row(s)" << endl; |
---|
| 308 | if (NoData) |
---|
| 309 | continue; |
---|
| 310 | |
---|
| 311 | GIO.readData(i, false); |
---|
| 312 | for (size_t j = 0; j < NElem; ++j) { |
---|
| 313 | for (size_t k = 0; k < Printers.size(); ++k) { |
---|
| 314 | Printers[k]->print(cout, j); |
---|
| 315 | if (k != Printers.size() - 1) |
---|
| 316 | cout << "\t"; |
---|
| 317 | } |
---|
| 318 | cout << endl; |
---|
| 319 | } |
---|
| 320 | } |
---|
| 321 | |
---|
| 322 | for (size_t i = 0; i < Printers.size(); ++i) { |
---|
| 323 | delete Printers[i]; |
---|
| 324 | } |
---|
| 325 | } |
---|
| 326 | |
---|
| 327 | #ifndef GENERICIO_NO_MPI |
---|
| 328 | MPI_Barrier(MPI_COMM_WORLD); |
---|
| 329 | MPI_Finalize(); |
---|
| 330 | #endif |
---|
| 331 | |
---|
| 332 | return 0; |
---|
| 333 | } |
---|
| 334 | |
---|