[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 | #define _XOPEN_SOURCE 600 |
---|
| 43 | #include "CRC64.h" |
---|
| 44 | #include "GenericIO.h" |
---|
| 45 | |
---|
| 46 | extern "C" { |
---|
| 47 | #include "blosc.h" |
---|
| 48 | } |
---|
[6461f57] | 49 | #include "sz.h" |
---|
[00587dc] | 50 | |
---|
| 51 | #include <sstream> |
---|
| 52 | #include <fstream> |
---|
| 53 | #include <stdexcept> |
---|
[14e73bb] | 54 | #include <iterator> |
---|
[00587dc] | 55 | #include <algorithm> |
---|
| 56 | #include <cassert> |
---|
| 57 | #include <cstddef> |
---|
| 58 | #include <cstring> |
---|
| 59 | |
---|
| 60 | #ifndef GENERICIO_NO_MPI |
---|
| 61 | #include <ctime> |
---|
| 62 | #endif |
---|
| 63 | |
---|
| 64 | #include <sys/types.h> |
---|
| 65 | #include <sys/stat.h> |
---|
| 66 | #include <fcntl.h> |
---|
| 67 | #include <errno.h> |
---|
| 68 | |
---|
| 69 | #ifdef __bgq__ |
---|
[14e73bb] | 70 | #include <mpix.h> |
---|
[00587dc] | 71 | #endif |
---|
| 72 | |
---|
| 73 | #ifndef MPI_UINT64_T |
---|
| 74 | #define MPI_UINT64_T (sizeof(long) == 8 ? MPI_LONG : MPI_LONG_LONG) |
---|
| 75 | #endif |
---|
| 76 | |
---|
| 77 | using namespace std; |
---|
| 78 | |
---|
| 79 | namespace gio { |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | #ifndef GENERICIO_NO_MPI |
---|
| 83 | GenericFileIO_MPI::~GenericFileIO_MPI() { |
---|
| 84 | (void) MPI_File_close(&FH); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | void GenericFileIO_MPI::open(const std::string &FN, bool ForReading) { |
---|
| 88 | FileName = FN; |
---|
| 89 | |
---|
| 90 | int amode = ForReading ? MPI_MODE_RDONLY : (MPI_MODE_WRONLY | MPI_MODE_CREATE); |
---|
| 91 | if (MPI_File_open(Comm, const_cast<char *>(FileName.c_str()), amode, |
---|
| 92 | MPI_INFO_NULL, &FH) != MPI_SUCCESS) |
---|
| 93 | throw runtime_error((!ForReading ? "Unable to create the file: " : |
---|
| 94 | "Unable to open the file: ") + |
---|
| 95 | FileName); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | void GenericFileIO_MPI::setSize(size_t sz) { |
---|
| 99 | if (MPI_File_set_size(FH, sz) != MPI_SUCCESS) |
---|
| 100 | throw runtime_error("Unable to set size for file: " + FileName); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | void GenericFileIO_MPI::read(void *buf, size_t count, off_t offset, |
---|
| 104 | const std::string &D) { |
---|
| 105 | while (count > 0) { |
---|
| 106 | MPI_Status status; |
---|
| 107 | if (MPI_File_read_at(FH, offset, buf, count, MPI_BYTE, &status) != MPI_SUCCESS) |
---|
| 108 | throw runtime_error("Unable to read " + D + " from file: " + FileName); |
---|
| 109 | |
---|
| 110 | int scount; |
---|
| 111 | (void) MPI_Get_count(&status, MPI_BYTE, &scount); |
---|
| 112 | |
---|
| 113 | count -= scount; |
---|
| 114 | buf = ((char *) buf) + scount; |
---|
| 115 | offset += scount; |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | void GenericFileIO_MPI::write(const void *buf, size_t count, off_t offset, |
---|
| 120 | const std::string &D) { |
---|
| 121 | while (count > 0) { |
---|
| 122 | MPI_Status status; |
---|
| 123 | if (MPI_File_write_at(FH, offset, (void *) buf, count, MPI_BYTE, &status) != MPI_SUCCESS) |
---|
| 124 | throw runtime_error("Unable to write " + D + " to file: " + FileName); |
---|
| 125 | |
---|
[56b997e] | 126 | int scount = 0; |
---|
| 127 | // On some systems, MPI_Get_count will not return zero even when count is zero. |
---|
| 128 | if (count > 0) |
---|
| 129 | (void) MPI_Get_count(&status, MPI_BYTE, &scount); |
---|
[00587dc] | 130 | |
---|
| 131 | count -= scount; |
---|
| 132 | buf = ((char *) buf) + scount; |
---|
| 133 | offset += scount; |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | void GenericFileIO_MPICollective::read(void *buf, size_t count, off_t offset, |
---|
| 138 | const std::string &D) { |
---|
| 139 | int Continue = 0; |
---|
| 140 | |
---|
| 141 | do { |
---|
| 142 | MPI_Status status; |
---|
| 143 | if (MPI_File_read_at_all(FH, offset, buf, count, MPI_BYTE, &status) != MPI_SUCCESS) |
---|
| 144 | throw runtime_error("Unable to read " + D + " from file: " + FileName); |
---|
| 145 | |
---|
[56b997e] | 146 | int scount = 0; |
---|
| 147 | // On some systems, MPI_Get_count will not return zero even when count is zero. |
---|
| 148 | if (count > 0) |
---|
| 149 | (void) MPI_Get_count(&status, MPI_BYTE, &scount); |
---|
[00587dc] | 150 | |
---|
| 151 | count -= scount; |
---|
| 152 | buf = ((char *) buf) + scount; |
---|
| 153 | offset += scount; |
---|
| 154 | |
---|
| 155 | int NeedContinue = (count > 0); |
---|
| 156 | MPI_Allreduce(&NeedContinue, &Continue, 1, MPI_INT, MPI_SUM, Comm); |
---|
| 157 | } while (Continue); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | void GenericFileIO_MPICollective::write(const void *buf, size_t count, off_t offset, |
---|
| 161 | const std::string &D) { |
---|
| 162 | int Continue = 0; |
---|
| 163 | |
---|
| 164 | do { |
---|
| 165 | MPI_Status status; |
---|
| 166 | if (MPI_File_write_at_all(FH, offset, (void *) buf, count, MPI_BYTE, &status) != MPI_SUCCESS) |
---|
| 167 | throw runtime_error("Unable to write " + D + " to file: " + FileName); |
---|
| 168 | |
---|
| 169 | int scount; |
---|
| 170 | (void) MPI_Get_count(&status, MPI_BYTE, &scount); |
---|
| 171 | |
---|
| 172 | count -= scount; |
---|
| 173 | buf = ((char *) buf) + scount; |
---|
| 174 | offset += scount; |
---|
| 175 | |
---|
| 176 | int NeedContinue = (count > 0); |
---|
| 177 | MPI_Allreduce(&NeedContinue, &Continue, 1, MPI_INT, MPI_SUM, Comm); |
---|
| 178 | } while (Continue); |
---|
| 179 | } |
---|
| 180 | #endif |
---|
| 181 | |
---|
| 182 | GenericFileIO_POSIX::~GenericFileIO_POSIX() { |
---|
| 183 | if (FH != -1) close(FH); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | void GenericFileIO_POSIX::open(const std::string &FN, bool ForReading) { |
---|
| 187 | FileName = FN; |
---|
| 188 | |
---|
| 189 | int flags = ForReading ? O_RDONLY : (O_WRONLY | O_CREAT); |
---|
| 190 | int mode = S_IRUSR | S_IWUSR | S_IRGRP; |
---|
| 191 | errno = 0; |
---|
| 192 | if ((FH = ::open(FileName.c_str(), flags, mode)) == -1) |
---|
| 193 | throw runtime_error((!ForReading ? "Unable to create the file: " : |
---|
| 194 | "Unable to open the file: ") + |
---|
| 195 | FileName + ": " + strerror(errno)); |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | void GenericFileIO_POSIX::setSize(size_t sz) { |
---|
| 199 | if (ftruncate(FH, sz) == -1) |
---|
| 200 | throw runtime_error("Unable to set size for file: " + FileName); |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | void GenericFileIO_POSIX::read(void *buf, size_t count, off_t offset, |
---|
| 204 | const std::string &D) { |
---|
| 205 | while (count > 0) { |
---|
| 206 | ssize_t scount; |
---|
| 207 | errno = 0; |
---|
| 208 | if ((scount = pread(FH, buf, count, offset)) == -1) { |
---|
| 209 | if (errno == EINTR) |
---|
| 210 | continue; |
---|
| 211 | |
---|
[14e73bb] | 212 | throw runtime_error("Unable to read " + D + " from file: " + FileName + |
---|
| 213 | ": " + strerror(errno)); |
---|
[00587dc] | 214 | } |
---|
| 215 | |
---|
| 216 | count -= scount; |
---|
| 217 | buf = ((char *) buf) + scount; |
---|
| 218 | offset += scount; |
---|
| 219 | } |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | void GenericFileIO_POSIX::write(const void *buf, size_t count, off_t offset, |
---|
| 223 | const std::string &D) { |
---|
| 224 | while (count > 0) { |
---|
| 225 | ssize_t scount; |
---|
| 226 | errno = 0; |
---|
| 227 | if ((scount = pwrite(FH, buf, count, offset)) == -1) { |
---|
| 228 | if (errno == EINTR) |
---|
| 229 | continue; |
---|
| 230 | |
---|
[14e73bb] | 231 | throw runtime_error("Unable to write " + D + " to file: " + FileName + |
---|
| 232 | ": " + strerror(errno)); |
---|
[00587dc] | 233 | } |
---|
| 234 | |
---|
| 235 | count -= scount; |
---|
| 236 | buf = ((char *) buf) + scount; |
---|
| 237 | offset += scount; |
---|
| 238 | } |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | static bool isBigEndian() { |
---|
| 242 | const uint32_t one = 1; |
---|
| 243 | return !(*((char *)(&one))); |
---|
| 244 | } |
---|
| 245 | |
---|
[a4fee13] | 246 | static void bswap(void *v, size_t s) { |
---|
| 247 | char *p = (char *) v; |
---|
| 248 | for (size_t i = 0; i < s/2; ++i) |
---|
| 249 | std::swap(p[i], p[s - (i+1)]); |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | // Using #pragma pack here, instead of __attribute__((packed)) because xlc, at |
---|
| 253 | // least as of v12.1, won't take __attribute__((packed)) on non-POD and/or |
---|
| 254 | // templated types. |
---|
| 255 | #pragma pack(1) |
---|
| 256 | |
---|
| 257 | template <typename T, bool IsBigEndian> |
---|
| 258 | struct endian_specific_value { |
---|
| 259 | operator T() const { |
---|
| 260 | T rvalue = value; |
---|
| 261 | if (IsBigEndian != isBigEndian()) |
---|
| 262 | bswap(&rvalue, sizeof(T)); |
---|
| 263 | |
---|
| 264 | return rvalue; |
---|
| 265 | }; |
---|
| 266 | |
---|
| 267 | endian_specific_value &operator = (T nvalue) { |
---|
| 268 | if (IsBigEndian != isBigEndian()) |
---|
| 269 | bswap(&nvalue, sizeof(T)); |
---|
| 270 | |
---|
| 271 | value = nvalue; |
---|
| 272 | return *this; |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | endian_specific_value &operator += (T nvalue) { |
---|
| 276 | *this = *this + nvalue; |
---|
| 277 | return *this; |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | endian_specific_value &operator -= (T nvalue) { |
---|
| 281 | *this = *this - nvalue; |
---|
| 282 | return *this; |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | private: |
---|
| 286 | T value; |
---|
| 287 | }; |
---|
| 288 | |
---|
[00587dc] | 289 | static const size_t CRCSize = 8; |
---|
| 290 | |
---|
| 291 | static const size_t MagicSize = 8; |
---|
| 292 | static const char *MagicBE = "HACC01B"; |
---|
| 293 | static const char *MagicLE = "HACC01L"; |
---|
| 294 | |
---|
[a4fee13] | 295 | template <bool IsBigEndian> |
---|
[00587dc] | 296 | struct GlobalHeader { |
---|
| 297 | char Magic[MagicSize]; |
---|
[a4fee13] | 298 | endian_specific_value<uint64_t, IsBigEndian> HeaderSize; |
---|
| 299 | endian_specific_value<uint64_t, IsBigEndian> NElems; // The global total |
---|
| 300 | endian_specific_value<uint64_t, IsBigEndian> Dims[3]; |
---|
| 301 | endian_specific_value<uint64_t, IsBigEndian> NVars; |
---|
| 302 | endian_specific_value<uint64_t, IsBigEndian> VarsSize; |
---|
| 303 | endian_specific_value<uint64_t, IsBigEndian> VarsStart; |
---|
| 304 | endian_specific_value<uint64_t, IsBigEndian> NRanks; |
---|
| 305 | endian_specific_value<uint64_t, IsBigEndian> RanksSize; |
---|
| 306 | endian_specific_value<uint64_t, IsBigEndian> RanksStart; |
---|
| 307 | endian_specific_value<uint64_t, IsBigEndian> GlobalHeaderSize; |
---|
| 308 | endian_specific_value<double, IsBigEndian> PhysOrigin[3]; |
---|
| 309 | endian_specific_value<double, IsBigEndian> PhysScale[3]; |
---|
| 310 | endian_specific_value<uint64_t, IsBigEndian> BlocksSize; |
---|
| 311 | endian_specific_value<uint64_t, IsBigEndian> BlocksStart; |
---|
| 312 | }; |
---|
[00587dc] | 313 | |
---|
| 314 | enum { |
---|
| 315 | FloatValue = (1 << 0), |
---|
| 316 | SignedValue = (1 << 1), |
---|
| 317 | ValueIsPhysCoordX = (1 << 2), |
---|
| 318 | ValueIsPhysCoordY = (1 << 3), |
---|
| 319 | ValueIsPhysCoordZ = (1 << 4), |
---|
| 320 | ValueMaybePhysGhost = (1 << 5) |
---|
| 321 | }; |
---|
| 322 | |
---|
| 323 | static const size_t NameSize = 256; |
---|
[a4fee13] | 324 | template <bool IsBigEndian> |
---|
[00587dc] | 325 | struct VariableHeader { |
---|
| 326 | char Name[NameSize]; |
---|
[a4fee13] | 327 | endian_specific_value<uint64_t, IsBigEndian> Flags; |
---|
| 328 | endian_specific_value<uint64_t, IsBigEndian> Size; |
---|
[5d57155] | 329 | endian_specific_value<uint64_t, IsBigEndian> ElementSize; |
---|
[a4fee13] | 330 | }; |
---|
[00587dc] | 331 | |
---|
[a4fee13] | 332 | template <bool IsBigEndian> |
---|
[00587dc] | 333 | struct RankHeader { |
---|
[a4fee13] | 334 | endian_specific_value<uint64_t, IsBigEndian> Coords[3]; |
---|
| 335 | endian_specific_value<uint64_t, IsBigEndian> NElems; |
---|
| 336 | endian_specific_value<uint64_t, IsBigEndian> Start; |
---|
| 337 | endian_specific_value<uint64_t, IsBigEndian> GlobalRank; |
---|
| 338 | }; |
---|
[00587dc] | 339 | |
---|
| 340 | static const size_t FilterNameSize = 8; |
---|
| 341 | static const size_t MaxFilters = 4; |
---|
[a4fee13] | 342 | template <bool IsBigEndian> |
---|
[00587dc] | 343 | struct BlockHeader { |
---|
| 344 | char Filters[MaxFilters][FilterNameSize]; |
---|
[a4fee13] | 345 | endian_specific_value<uint64_t, IsBigEndian> Start; |
---|
| 346 | endian_specific_value<uint64_t, IsBigEndian> Size; |
---|
| 347 | }; |
---|
[00587dc] | 348 | |
---|
[a4fee13] | 349 | template <bool IsBigEndian> |
---|
[00587dc] | 350 | struct CompressHeader { |
---|
[a4fee13] | 351 | endian_specific_value<uint64_t, IsBigEndian> OrigCRC; |
---|
| 352 | }; |
---|
[00587dc] | 353 | const char *CompressName = "BLOSC"; |
---|
| 354 | |
---|
[6461f57] | 355 | const char *LossyCompressName = "SZ"; |
---|
| 356 | |
---|
[a4fee13] | 357 | #pragma pack() |
---|
| 358 | |
---|
[00587dc] | 359 | unsigned GenericIO::DefaultFileIOType = FileIOPOSIX; |
---|
| 360 | int GenericIO::DefaultPartition = 0; |
---|
| 361 | bool GenericIO::DefaultShouldCompress = false; |
---|
| 362 | |
---|
| 363 | #ifndef GENERICIO_NO_MPI |
---|
| 364 | std::size_t GenericIO::CollectiveMPIIOThreshold = 0; |
---|
| 365 | #endif |
---|
| 366 | |
---|
| 367 | static bool blosc_initialized = false; |
---|
[6461f57] | 368 | static bool sz_initialized = false; |
---|
[00587dc] | 369 | |
---|
[eeacdad] | 370 | static int GetSZDT(GenericIO::Variable &Var) { |
---|
| 371 | if (Var.hasElementType<float>()) |
---|
| 372 | return SZ_FLOAT; |
---|
| 373 | else if (Var.hasElementType<double>()) |
---|
| 374 | return SZ_DOUBLE; |
---|
| 375 | else if (Var.hasElementType<uint8_t>()) |
---|
| 376 | return SZ_UINT8; |
---|
| 377 | else if (Var.hasElementType<int8_t>()) |
---|
| 378 | return SZ_INT8; |
---|
| 379 | else if (Var.hasElementType<uint16_t>()) |
---|
| 380 | return SZ_UINT16; |
---|
| 381 | else if (Var.hasElementType<int16_t>()) |
---|
| 382 | return SZ_INT16; |
---|
| 383 | else if (Var.hasElementType<uint32_t>()) |
---|
| 384 | return SZ_UINT32; |
---|
| 385 | else if (Var.hasElementType<int32_t>()) |
---|
| 386 | return SZ_INT32; |
---|
| 387 | else if (Var.hasElementType<uint64_t>()) |
---|
| 388 | return SZ_UINT64; |
---|
| 389 | else if (Var.hasElementType<int64_t>()) |
---|
| 390 | return SZ_INT64; |
---|
| 391 | else |
---|
| 392 | return -1; |
---|
| 393 | } |
---|
| 394 | |
---|
[00587dc] | 395 | #ifndef GENERICIO_NO_MPI |
---|
[a4fee13] | 396 | void GenericIO::write() { |
---|
| 397 | if (isBigEndian()) |
---|
| 398 | write<true>(); |
---|
| 399 | else |
---|
| 400 | write<false>(); |
---|
| 401 | } |
---|
| 402 | |
---|
[00587dc] | 403 | // Note: writing errors are not currently recoverable (one rank may fail |
---|
| 404 | // while the others don't). |
---|
[a4fee13] | 405 | template <bool IsBigEndian> |
---|
[00587dc] | 406 | void GenericIO::write() { |
---|
[a4fee13] | 407 | const char *Magic = IsBigEndian ? MagicBE : MagicLE; |
---|
[00587dc] | 408 | |
---|
| 409 | uint64_t FileSize = 0; |
---|
| 410 | |
---|
| 411 | int NRanks, Rank; |
---|
| 412 | MPI_Comm_rank(Comm, &Rank); |
---|
| 413 | MPI_Comm_size(Comm, &NRanks); |
---|
| 414 | |
---|
| 415 | #ifdef __bgq__ |
---|
| 416 | MPI_Barrier(Comm); |
---|
| 417 | #endif |
---|
| 418 | MPI_Comm_split(Comm, Partition, Rank, &SplitComm); |
---|
| 419 | |
---|
| 420 | int SplitNRanks, SplitRank; |
---|
| 421 | MPI_Comm_rank(SplitComm, &SplitRank); |
---|
| 422 | MPI_Comm_size(SplitComm, &SplitNRanks); |
---|
| 423 | |
---|
| 424 | string LocalFileName; |
---|
| 425 | if (SplitNRanks != NRanks) { |
---|
| 426 | if (Rank == 0) { |
---|
| 427 | // In split mode, the specified file becomes the rank map, and the real |
---|
| 428 | // data is partitioned. |
---|
| 429 | |
---|
| 430 | vector<int> MapRank, MapPartition; |
---|
| 431 | MapRank.resize(NRanks); |
---|
| 432 | for (int i = 0; i < NRanks; ++i) MapRank[i] = i; |
---|
| 433 | |
---|
| 434 | MapPartition.resize(NRanks); |
---|
| 435 | MPI_Gather(&Partition, 1, MPI_INT, &MapPartition[0], 1, MPI_INT, 0, Comm); |
---|
| 436 | |
---|
| 437 | GenericIO GIO(MPI_COMM_SELF, FileName, FileIOType); |
---|
| 438 | GIO.setNumElems(NRanks); |
---|
| 439 | GIO.addVariable("$rank", MapRank); /* this is for use by humans; the reading |
---|
| 440 | code assumes that the partitions are in |
---|
| 441 | rank order */ |
---|
| 442 | GIO.addVariable("$partition", MapPartition); |
---|
| 443 | |
---|
| 444 | vector<int> CX, CY, CZ; |
---|
| 445 | int TopoStatus; |
---|
| 446 | MPI_Topo_test(Comm, &TopoStatus); |
---|
| 447 | if (TopoStatus == MPI_CART) { |
---|
| 448 | CX.resize(NRanks); |
---|
| 449 | CY.resize(NRanks); |
---|
| 450 | CZ.resize(NRanks); |
---|
| 451 | |
---|
| 452 | for (int i = 0; i < NRanks; ++i) { |
---|
| 453 | int C[3]; |
---|
| 454 | MPI_Cart_coords(Comm, i, 3, C); |
---|
| 455 | |
---|
| 456 | CX[i] = C[0]; |
---|
| 457 | CY[i] = C[1]; |
---|
| 458 | CZ[i] = C[2]; |
---|
| 459 | } |
---|
| 460 | |
---|
| 461 | GIO.addVariable("$x", CX); |
---|
| 462 | GIO.addVariable("$y", CY); |
---|
| 463 | GIO.addVariable("$z", CZ); |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | GIO.write(); |
---|
| 467 | } else { |
---|
| 468 | MPI_Gather(&Partition, 1, MPI_INT, 0, 0, MPI_INT, 0, Comm); |
---|
| 469 | } |
---|
| 470 | |
---|
| 471 | stringstream ss; |
---|
| 472 | ss << FileName << "#" << Partition; |
---|
| 473 | LocalFileName = ss.str(); |
---|
| 474 | } else { |
---|
| 475 | LocalFileName = FileName; |
---|
| 476 | } |
---|
| 477 | |
---|
[a4fee13] | 478 | RankHeader<IsBigEndian> RHLocal; |
---|
[00587dc] | 479 | int Dims[3], Periods[3], Coords[3]; |
---|
| 480 | |
---|
| 481 | int TopoStatus; |
---|
| 482 | MPI_Topo_test(Comm, &TopoStatus); |
---|
| 483 | if (TopoStatus == MPI_CART) { |
---|
| 484 | MPI_Cart_get(Comm, 3, Dims, Periods, Coords); |
---|
| 485 | } else { |
---|
| 486 | Dims[0] = NRanks; |
---|
| 487 | std::fill(Dims + 1, Dims + 3, 1); |
---|
| 488 | std::fill(Periods, Periods + 3, 0); |
---|
| 489 | Coords[0] = Rank; |
---|
| 490 | std::fill(Coords + 1, Coords + 3, 0); |
---|
| 491 | } |
---|
| 492 | |
---|
| 493 | std::copy(Coords, Coords + 3, RHLocal.Coords); |
---|
| 494 | RHLocal.NElems = NElems; |
---|
| 495 | RHLocal.Start = 0; |
---|
| 496 | RHLocal.GlobalRank = Rank; |
---|
| 497 | |
---|
| 498 | bool ShouldCompress = DefaultShouldCompress; |
---|
| 499 | const char *EnvStr = getenv("GENERICIO_COMPRESS"); |
---|
| 500 | if (EnvStr) { |
---|
| 501 | int Mod = atoi(EnvStr); |
---|
| 502 | ShouldCompress = (Mod > 0); |
---|
| 503 | } |
---|
| 504 | |
---|
| 505 | bool NeedsBlockHeaders = ShouldCompress; |
---|
| 506 | EnvStr = getenv("GENERICIO_FORCE_BLOCKS"); |
---|
| 507 | if (!NeedsBlockHeaders && EnvStr) { |
---|
| 508 | int Mod = atoi(EnvStr); |
---|
| 509 | NeedsBlockHeaders = (Mod > 0); |
---|
| 510 | } |
---|
| 511 | |
---|
[a4fee13] | 512 | vector<BlockHeader<IsBigEndian> > LocalBlockHeaders; |
---|
[00587dc] | 513 | vector<void *> LocalData; |
---|
| 514 | vector<bool> LocalHasExtraSpace; |
---|
| 515 | vector<vector<unsigned char> > LocalCData; |
---|
| 516 | if (NeedsBlockHeaders) { |
---|
| 517 | LocalBlockHeaders.resize(Vars.size()); |
---|
| 518 | LocalData.resize(Vars.size()); |
---|
| 519 | LocalHasExtraSpace.resize(Vars.size()); |
---|
| 520 | if (ShouldCompress) |
---|
| 521 | LocalCData.resize(Vars.size()); |
---|
| 522 | |
---|
| 523 | for (size_t i = 0; i < Vars.size(); ++i) { |
---|
| 524 | // Filters null by default, leave null starting address (needs to be |
---|
| 525 | // calculated by the header-writing rank). |
---|
[a4fee13] | 526 | memset(&LocalBlockHeaders[i], 0, sizeof(BlockHeader<IsBigEndian>)); |
---|
[00587dc] | 527 | if (ShouldCompress) { |
---|
[eeacdad] | 528 | void *OrigData = Vars[i].Data; |
---|
| 529 | bool FreeOrigData = false; |
---|
| 530 | size_t OrigUnitSize = Vars[i].Size; |
---|
| 531 | size_t OrigDataSize = NElems*Vars[i].Size; |
---|
| 532 | |
---|
| 533 | int FilterIdx = 0; |
---|
| 534 | if (Vars[i].LCI.Mode != LossyCompressionInfo::LCModeNone) { |
---|
| 535 | int SZDT = GetSZDT(Vars[i]); |
---|
| 536 | if (SZDT == -1) |
---|
| 537 | goto nosz; |
---|
| 538 | |
---|
| 539 | int EBM; |
---|
| 540 | switch (Vars[i].LCI.Mode) { |
---|
| 541 | case LossyCompressionInfo::LCModeAbs: |
---|
| 542 | EBM = ABS; |
---|
| 543 | break; |
---|
| 544 | case LossyCompressionInfo::LCModeRel: |
---|
| 545 | EBM = REL; |
---|
| 546 | break; |
---|
| 547 | case LossyCompressionInfo::LCModeAbsAndRel: |
---|
| 548 | EBM = ABS_AND_REL; |
---|
| 549 | break; |
---|
| 550 | case LossyCompressionInfo::LCModeAbsOrRel: |
---|
| 551 | EBM = ABS_OR_REL; |
---|
| 552 | break; |
---|
| 553 | case LossyCompressionInfo::LCModePSNR: |
---|
| 554 | EBM = PSNR; |
---|
| 555 | break; |
---|
| 556 | } |
---|
| 557 | |
---|
| 558 | size_t LOutSize; |
---|
| 559 | unsigned char *LCompressedData = SZ_compress_args(SZDT, Vars[i].Data, &LOutSize, EBM, |
---|
| 560 | Vars[i].LCI.AbsErrThreshold, Vars[i].LCI.RelErrThreshold, |
---|
| 561 | Vars[i].LCI.PSNRThreshold, 0, 0, 0, 0, NElems); |
---|
| 562 | if (!LCompressedData) |
---|
| 563 | goto nosz; |
---|
| 564 | if (LOutSize >= NElems*Vars[i].Size) { |
---|
| 565 | free(LCompressedData); |
---|
| 566 | goto nosz; |
---|
| 567 | } |
---|
| 568 | |
---|
| 569 | OrigData = LCompressedData; |
---|
| 570 | FreeOrigData = true; |
---|
| 571 | OrigUnitSize = 1; |
---|
| 572 | OrigDataSize = LOutSize; |
---|
| 573 | |
---|
| 574 | strncpy(LocalBlockHeaders[i].Filters[FilterIdx++], LossyCompressName, FilterNameSize); |
---|
| 575 | } |
---|
| 576 | nosz: |
---|
| 577 | |
---|
[a4fee13] | 578 | LocalCData[i].resize(sizeof(CompressHeader<IsBigEndian>)); |
---|
[00587dc] | 579 | |
---|
[a4fee13] | 580 | CompressHeader<IsBigEndian> *CH = (CompressHeader<IsBigEndian>*) &LocalCData[i][0]; |
---|
[eeacdad] | 581 | CH->OrigCRC = crc64_omp(OrigData, OrigDataSize); |
---|
[00587dc] | 582 | |
---|
| 583 | #ifdef _OPENMP |
---|
| 584 | #pragma omp master |
---|
| 585 | { |
---|
| 586 | #endif |
---|
| 587 | |
---|
| 588 | if (!blosc_initialized) { |
---|
| 589 | blosc_init(); |
---|
| 590 | blosc_initialized = true; |
---|
| 591 | } |
---|
| 592 | |
---|
[2c47b73] | 593 | if (!sz_initialized) { |
---|
| 594 | SZ_Init(NULL); |
---|
| 595 | sz_initialized = true; |
---|
| 596 | } |
---|
| 597 | |
---|
[00587dc] | 598 | #ifdef _OPENMP |
---|
| 599 | blosc_set_nthreads(omp_get_max_threads()); |
---|
| 600 | } |
---|
| 601 | #endif |
---|
| 602 | |
---|
[eeacdad] | 603 | LocalCData[i].resize(LocalCData[i].size() + OrigDataSize); |
---|
| 604 | if (blosc_compress(9, 1, OrigUnitSize, OrigDataSize, OrigData, |
---|
[a4fee13] | 605 | &LocalCData[i][0] + sizeof(CompressHeader<IsBigEndian>), |
---|
[eeacdad] | 606 | OrigDataSize) <= 0) { |
---|
| 607 | if (FreeOrigData) |
---|
| 608 | free(OrigData); |
---|
| 609 | |
---|
[00587dc] | 610 | goto nocomp; |
---|
[eeacdad] | 611 | } |
---|
[00587dc] | 612 | |
---|
[eeacdad] | 613 | if (FreeOrigData) |
---|
| 614 | free(OrigData); |
---|
| 615 | |
---|
| 616 | strncpy(LocalBlockHeaders[i].Filters[FilterIdx++], CompressName, FilterNameSize); |
---|
[00587dc] | 617 | size_t CNBytes, CCBytes, CBlockSize; |
---|
[a4fee13] | 618 | blosc_cbuffer_sizes(&LocalCData[i][0] + sizeof(CompressHeader<IsBigEndian>), |
---|
[00587dc] | 619 | &CNBytes, &CCBytes, &CBlockSize); |
---|
[a4fee13] | 620 | LocalCData[i].resize(CCBytes + sizeof(CompressHeader<IsBigEndian>)); |
---|
[00587dc] | 621 | |
---|
| 622 | LocalBlockHeaders[i].Size = LocalCData[i].size(); |
---|
| 623 | LocalCData[i].resize(LocalCData[i].size() + CRCSize); |
---|
| 624 | LocalData[i] = &LocalCData[i][0]; |
---|
| 625 | LocalHasExtraSpace[i] = true; |
---|
| 626 | } else { |
---|
| 627 | nocomp: |
---|
| 628 | LocalBlockHeaders[i].Size = NElems*Vars[i].Size; |
---|
| 629 | LocalData[i] = Vars[i].Data; |
---|
| 630 | LocalHasExtraSpace[i] = Vars[i].HasExtraSpace; |
---|
| 631 | } |
---|
| 632 | } |
---|
| 633 | } |
---|
| 634 | |
---|
| 635 | double StartTime = MPI_Wtime(); |
---|
| 636 | |
---|
| 637 | if (SplitRank == 0) { |
---|
[a4fee13] | 638 | uint64_t HeaderSize = sizeof(GlobalHeader<IsBigEndian>) + Vars.size()*sizeof(VariableHeader<IsBigEndian>) + |
---|
| 639 | SplitNRanks*sizeof(RankHeader<IsBigEndian>) + CRCSize; |
---|
[00587dc] | 640 | if (NeedsBlockHeaders) |
---|
[a4fee13] | 641 | HeaderSize += SplitNRanks*Vars.size()*sizeof(BlockHeader<IsBigEndian>); |
---|
[00587dc] | 642 | |
---|
| 643 | vector<char> Header(HeaderSize, 0); |
---|
[a4fee13] | 644 | GlobalHeader<IsBigEndian> *GH = (GlobalHeader<IsBigEndian> *) &Header[0]; |
---|
[00587dc] | 645 | std::copy(Magic, Magic + MagicSize, GH->Magic); |
---|
| 646 | GH->HeaderSize = HeaderSize - CRCSize; |
---|
| 647 | GH->NElems = NElems; // This will be updated later |
---|
| 648 | std::copy(Dims, Dims + 3, GH->Dims); |
---|
| 649 | GH->NVars = Vars.size(); |
---|
[a4fee13] | 650 | GH->VarsSize = sizeof(VariableHeader<IsBigEndian>); |
---|
| 651 | GH->VarsStart = sizeof(GlobalHeader<IsBigEndian>); |
---|
[00587dc] | 652 | GH->NRanks = SplitNRanks; |
---|
[a4fee13] | 653 | GH->RanksSize = sizeof(RankHeader<IsBigEndian>); |
---|
| 654 | GH->RanksStart = GH->VarsStart + Vars.size()*sizeof(VariableHeader<IsBigEndian>); |
---|
| 655 | GH->GlobalHeaderSize = sizeof(GlobalHeader<IsBigEndian>); |
---|
[00587dc] | 656 | std::copy(PhysOrigin, PhysOrigin + 3, GH->PhysOrigin); |
---|
| 657 | std::copy(PhysScale, PhysScale + 3, GH->PhysScale); |
---|
| 658 | if (!NeedsBlockHeaders) { |
---|
| 659 | GH->BlocksSize = GH->BlocksStart = 0; |
---|
| 660 | } else { |
---|
[a4fee13] | 661 | GH->BlocksSize = sizeof(BlockHeader<IsBigEndian>); |
---|
| 662 | GH->BlocksStart = GH->RanksStart + SplitNRanks*sizeof(RankHeader<IsBigEndian>); |
---|
[00587dc] | 663 | } |
---|
| 664 | |
---|
| 665 | uint64_t RecordSize = 0; |
---|
[a4fee13] | 666 | VariableHeader<IsBigEndian> *VH = (VariableHeader<IsBigEndian> *) &Header[GH->VarsStart]; |
---|
[00587dc] | 667 | for (size_t i = 0; i < Vars.size(); ++i, ++VH) { |
---|
| 668 | string VName(Vars[i].Name); |
---|
| 669 | VName.resize(NameSize); |
---|
| 670 | |
---|
| 671 | std::copy(VName.begin(), VName.end(), VH->Name); |
---|
[a4fee13] | 672 | uint64_t VFlags = 0; |
---|
| 673 | if (Vars[i].IsFloat) VFlags |= FloatValue; |
---|
| 674 | if (Vars[i].IsSigned) VFlags |= SignedValue; |
---|
| 675 | if (Vars[i].IsPhysCoordX) VFlags |= ValueIsPhysCoordX; |
---|
| 676 | if (Vars[i].IsPhysCoordY) VFlags |= ValueIsPhysCoordY; |
---|
| 677 | if (Vars[i].IsPhysCoordZ) VFlags |= ValueIsPhysCoordZ; |
---|
| 678 | if (Vars[i].MaybePhysGhost) VFlags |= ValueMaybePhysGhost; |
---|
| 679 | VH->Flags = VFlags; |
---|
[00587dc] | 680 | RecordSize += VH->Size = Vars[i].Size; |
---|
[5d57155] | 681 | VH->ElementSize = Vars[i].ElementSize; |
---|
[00587dc] | 682 | } |
---|
| 683 | |
---|
| 684 | MPI_Gather(&RHLocal, sizeof(RHLocal), MPI_BYTE, |
---|
| 685 | &Header[GH->RanksStart], sizeof(RHLocal), |
---|
| 686 | MPI_BYTE, 0, SplitComm); |
---|
| 687 | |
---|
| 688 | if (NeedsBlockHeaders) { |
---|
| 689 | MPI_Gather(&LocalBlockHeaders[0], |
---|
[a4fee13] | 690 | Vars.size()*sizeof(BlockHeader<IsBigEndian>), MPI_BYTE, |
---|
[00587dc] | 691 | &Header[GH->BlocksStart], |
---|
[a4fee13] | 692 | Vars.size()*sizeof(BlockHeader<IsBigEndian>), MPI_BYTE, |
---|
[00587dc] | 693 | 0, SplitComm); |
---|
| 694 | |
---|
[a4fee13] | 695 | BlockHeader<IsBigEndian> *BH = (BlockHeader<IsBigEndian> *) &Header[GH->BlocksStart]; |
---|
[00587dc] | 696 | for (int i = 0; i < SplitNRanks; ++i) |
---|
| 697 | for (size_t j = 0; j < Vars.size(); ++j, ++BH) { |
---|
| 698 | if (i == 0 && j == 0) |
---|
| 699 | BH->Start = HeaderSize; |
---|
| 700 | else |
---|
| 701 | BH->Start = BH[-1].Start + BH[-1].Size + CRCSize; |
---|
| 702 | } |
---|
| 703 | |
---|
[a4fee13] | 704 | RankHeader<IsBigEndian> *RH = (RankHeader<IsBigEndian> *) &Header[GH->RanksStart]; |
---|
[00587dc] | 705 | RH->Start = HeaderSize; ++RH; |
---|
| 706 | for (int i = 1; i < SplitNRanks; ++i, ++RH) { |
---|
| 707 | RH->Start = |
---|
[a4fee13] | 708 | ((BlockHeader<IsBigEndian> *) &Header[GH->BlocksStart])[i*Vars.size()].Start; |
---|
[00587dc] | 709 | GH->NElems += RH->NElems; |
---|
| 710 | } |
---|
| 711 | |
---|
| 712 | // Compute the total file size. |
---|
| 713 | uint64_t LastData = BH[-1].Size + CRCSize; |
---|
| 714 | FileSize = BH[-1].Start + LastData; |
---|
| 715 | } else { |
---|
[a4fee13] | 716 | RankHeader<IsBigEndian> *RH = (RankHeader<IsBigEndian> *) &Header[GH->RanksStart]; |
---|
[00587dc] | 717 | RH->Start = HeaderSize; ++RH; |
---|
| 718 | for (int i = 1; i < SplitNRanks; ++i, ++RH) { |
---|
| 719 | uint64_t PrevNElems = RH[-1].NElems; |
---|
| 720 | uint64_t PrevData = PrevNElems*RecordSize + CRCSize*Vars.size(); |
---|
| 721 | RH->Start = RH[-1].Start + PrevData; |
---|
| 722 | GH->NElems += RH->NElems; |
---|
| 723 | } |
---|
| 724 | |
---|
| 725 | // Compute the total file size. |
---|
| 726 | uint64_t LastNElems = RH[-1].NElems; |
---|
| 727 | uint64_t LastData = LastNElems*RecordSize + CRCSize*Vars.size(); |
---|
| 728 | FileSize = RH[-1].Start + LastData; |
---|
| 729 | } |
---|
| 730 | |
---|
| 731 | // Now that the starting offset has been computed, send it back to each rank. |
---|
| 732 | MPI_Scatter(&Header[GH->RanksStart], sizeof(RHLocal), |
---|
| 733 | MPI_BYTE, &RHLocal, sizeof(RHLocal), |
---|
| 734 | MPI_BYTE, 0, SplitComm); |
---|
| 735 | |
---|
| 736 | if (NeedsBlockHeaders) |
---|
| 737 | MPI_Scatter(&Header[GH->BlocksStart], |
---|
[a4fee13] | 738 | sizeof(BlockHeader<IsBigEndian>)*Vars.size(), MPI_BYTE, |
---|
[00587dc] | 739 | &LocalBlockHeaders[0], |
---|
[a4fee13] | 740 | sizeof(BlockHeader<IsBigEndian>)*Vars.size(), MPI_BYTE, |
---|
[00587dc] | 741 | 0, SplitComm); |
---|
| 742 | |
---|
| 743 | uint64_t HeaderCRC = crc64_omp(&Header[0], HeaderSize - CRCSize); |
---|
| 744 | crc64_invert(HeaderCRC, &Header[HeaderSize - CRCSize]); |
---|
| 745 | |
---|
| 746 | if (FileIOType == FileIOMPI) |
---|
| 747 | FH.get() = new GenericFileIO_MPI(MPI_COMM_SELF); |
---|
| 748 | else if (FileIOType == FileIOMPICollective) |
---|
| 749 | FH.get() = new GenericFileIO_MPICollective(MPI_COMM_SELF); |
---|
| 750 | else |
---|
| 751 | FH.get() = new GenericFileIO_POSIX(); |
---|
| 752 | |
---|
| 753 | FH.get()->open(LocalFileName); |
---|
| 754 | FH.get()->setSize(FileSize); |
---|
| 755 | FH.get()->write(&Header[0], HeaderSize, 0, "header"); |
---|
| 756 | |
---|
| 757 | close(); |
---|
| 758 | } else { |
---|
| 759 | MPI_Gather(&RHLocal, sizeof(RHLocal), MPI_BYTE, 0, 0, MPI_BYTE, 0, SplitComm); |
---|
| 760 | if (NeedsBlockHeaders) |
---|
[a4fee13] | 761 | MPI_Gather(&LocalBlockHeaders[0], Vars.size()*sizeof(BlockHeader<IsBigEndian>), |
---|
[00587dc] | 762 | MPI_BYTE, 0, 0, MPI_BYTE, 0, SplitComm); |
---|
| 763 | MPI_Scatter(0, 0, MPI_BYTE, &RHLocal, sizeof(RHLocal), MPI_BYTE, 0, SplitComm); |
---|
| 764 | if (NeedsBlockHeaders) |
---|
[a4fee13] | 765 | MPI_Scatter(0, 0, MPI_BYTE, &LocalBlockHeaders[0], sizeof(BlockHeader<IsBigEndian>)*Vars.size(), |
---|
[00587dc] | 766 | MPI_BYTE, 0, SplitComm); |
---|
| 767 | } |
---|
| 768 | |
---|
| 769 | MPI_Barrier(SplitComm); |
---|
| 770 | |
---|
| 771 | if (FileIOType == FileIOMPI) |
---|
| 772 | FH.get() = new GenericFileIO_MPI(SplitComm); |
---|
| 773 | else if (FileIOType == FileIOMPICollective) |
---|
| 774 | FH.get() = new GenericFileIO_MPICollective(SplitComm); |
---|
| 775 | else |
---|
| 776 | FH.get() = new GenericFileIO_POSIX(); |
---|
| 777 | |
---|
| 778 | FH.get()->open(LocalFileName); |
---|
| 779 | |
---|
| 780 | uint64_t Offset = RHLocal.Start; |
---|
| 781 | for (size_t i = 0; i < Vars.size(); ++i) { |
---|
| 782 | uint64_t WriteSize = NeedsBlockHeaders ? |
---|
| 783 | LocalBlockHeaders[i].Size : NElems*Vars[i].Size; |
---|
| 784 | void *Data = NeedsBlockHeaders ? LocalData[i] : Vars[i].Data; |
---|
| 785 | uint64_t CRC = crc64_omp(Data, WriteSize); |
---|
| 786 | bool HasExtraSpace = NeedsBlockHeaders ? |
---|
| 787 | LocalHasExtraSpace[i] : Vars[i].HasExtraSpace; |
---|
| 788 | char *CRCLoc = HasExtraSpace ? ((char *) Data) + WriteSize : (char *) &CRC; |
---|
| 789 | |
---|
| 790 | if (NeedsBlockHeaders) |
---|
| 791 | Offset = LocalBlockHeaders[i].Start; |
---|
| 792 | |
---|
| 793 | // When using extra space for the CRC write, preserve the original contents. |
---|
| 794 | char CRCSave[CRCSize]; |
---|
| 795 | if (HasExtraSpace) |
---|
| 796 | std::copy(CRCLoc, CRCLoc + CRCSize, CRCSave); |
---|
| 797 | |
---|
| 798 | crc64_invert(CRC, CRCLoc); |
---|
| 799 | |
---|
| 800 | if (HasExtraSpace) { |
---|
| 801 | FH.get()->write(Data, WriteSize + CRCSize, Offset, Vars[i].Name + " with CRC"); |
---|
| 802 | } else { |
---|
| 803 | FH.get()->write(Data, WriteSize, Offset, Vars[i].Name); |
---|
| 804 | FH.get()->write(CRCLoc, CRCSize, Offset + WriteSize, Vars[i].Name + " CRC"); |
---|
| 805 | } |
---|
| 806 | |
---|
| 807 | if (HasExtraSpace) |
---|
| 808 | std::copy(CRCSave, CRCSave + CRCSize, CRCLoc); |
---|
| 809 | |
---|
| 810 | Offset += WriteSize + CRCSize; |
---|
| 811 | } |
---|
| 812 | |
---|
| 813 | close(); |
---|
| 814 | MPI_Barrier(Comm); |
---|
| 815 | |
---|
| 816 | double EndTime = MPI_Wtime(); |
---|
| 817 | double TotalTime = EndTime - StartTime; |
---|
| 818 | double MaxTotalTime; |
---|
| 819 | MPI_Reduce(&TotalTime, &MaxTotalTime, 1, MPI_DOUBLE, MPI_MAX, 0, Comm); |
---|
| 820 | |
---|
| 821 | if (SplitNRanks != NRanks) { |
---|
| 822 | uint64_t ContribFileSize = (SplitRank == 0) ? FileSize : 0; |
---|
| 823 | MPI_Reduce(&ContribFileSize, &FileSize, 1, MPI_UINT64_T, MPI_SUM, 0, Comm); |
---|
| 824 | } |
---|
| 825 | |
---|
| 826 | if (Rank == 0) { |
---|
| 827 | double Rate = ((double) FileSize) / MaxTotalTime / (1024.*1024.); |
---|
[d6628a6] | 828 | std::cout << "Wrote " << Vars.size() << " variables to " << FileName << |
---|
| 829 | " (" << FileSize << " bytes) in " << MaxTotalTime << "s: " << |
---|
| 830 | Rate << " MB/s" << std::endl; |
---|
[00587dc] | 831 | } |
---|
| 832 | |
---|
| 833 | MPI_Comm_free(&SplitComm); |
---|
| 834 | SplitComm = MPI_COMM_NULL; |
---|
| 835 | } |
---|
| 836 | #endif // GENERICIO_NO_MPI |
---|
| 837 | |
---|
[a4fee13] | 838 | template <bool IsBigEndian> |
---|
[8f0a211] | 839 | void GenericIO::readHeaderLeader(void *GHPtr, MismatchBehavior MB, int NRanks, |
---|
| 840 | int Rank, int SplitNRanks, |
---|
| 841 | string &LocalFileName, uint64_t &HeaderSize, |
---|
| 842 | vector<char> &Header) { |
---|
[a4fee13] | 843 | GlobalHeader<IsBigEndian> &GH = *(GlobalHeader<IsBigEndian> *) GHPtr; |
---|
| 844 | |
---|
[8f0a211] | 845 | if (MB == MismatchDisallowed) { |
---|
[a4fee13] | 846 | if (SplitNRanks != (int) GH.NRanks) { |
---|
| 847 | stringstream ss; |
---|
| 848 | ss << "Won't read " << LocalFileName << ": communicator-size mismatch: " << |
---|
| 849 | "current: " << SplitNRanks << ", file: " << GH.NRanks; |
---|
| 850 | throw runtime_error(ss.str()); |
---|
| 851 | } |
---|
| 852 | |
---|
| 853 | #ifndef GENERICIO_NO_MPI |
---|
| 854 | int TopoStatus; |
---|
| 855 | MPI_Topo_test(Comm, &TopoStatus); |
---|
| 856 | if (TopoStatus == MPI_CART) { |
---|
| 857 | int Dims[3], Periods[3], Coords[3]; |
---|
| 858 | MPI_Cart_get(Comm, 3, Dims, Periods, Coords); |
---|
| 859 | |
---|
| 860 | bool DimsMatch = true; |
---|
| 861 | for (int i = 0; i < 3; ++i) { |
---|
| 862 | if ((uint64_t) Dims[i] != GH.Dims[i]) { |
---|
| 863 | DimsMatch = false; |
---|
| 864 | break; |
---|
| 865 | } |
---|
| 866 | } |
---|
| 867 | |
---|
| 868 | if (!DimsMatch) { |
---|
| 869 | stringstream ss; |
---|
| 870 | ss << "Won't read " << LocalFileName << |
---|
| 871 | ": communicator-decomposition mismatch: " << |
---|
| 872 | "current: " << Dims[0] << "x" << Dims[1] << "x" << Dims[2] << |
---|
| 873 | ", file: " << GH.Dims[0] << "x" << GH.Dims[1] << "x" << |
---|
| 874 | GH.Dims[2]; |
---|
| 875 | throw runtime_error(ss.str()); |
---|
| 876 | } |
---|
| 877 | } |
---|
| 878 | #endif |
---|
[8f0a211] | 879 | } else if (MB == MismatchRedistribute && !Redistributing) { |
---|
| 880 | Redistributing = true; |
---|
| 881 | |
---|
| 882 | int NFileRanks = RankMap.empty() ? (int) GH.NRanks : (int) RankMap.size(); |
---|
| 883 | int NFileRanksPerRank = NFileRanks/NRanks; |
---|
| 884 | int NRemFileRank = NFileRanks % NRanks; |
---|
| 885 | |
---|
| 886 | if (!NFileRanksPerRank) { |
---|
| 887 | // We have only the remainder, so the last NRemFileRank ranks get one |
---|
| 888 | // file rank, and the others don't. |
---|
| 889 | if (NRemFileRank && NRanks - Rank <= NRemFileRank) |
---|
| 890 | SourceRanks.push_back(NRanks - (Rank + 1)); |
---|
| 891 | } else { |
---|
| 892 | // Since NRemFileRank < NRanks, and we don't want to put any extra memory |
---|
| 893 | // load on rank 0 (because rank 0's memory load is normally higher than |
---|
| 894 | // the other ranks anyway), the last NRemFileRank will each take |
---|
| 895 | // (NFileRanksPerRank+1) file ranks. |
---|
| 896 | |
---|
| 897 | int FirstFileRank = 0, LastFileRank = NFileRanksPerRank - 1; |
---|
| 898 | for (int i = 1; i <= Rank; ++i) { |
---|
| 899 | FirstFileRank = LastFileRank + 1; |
---|
| 900 | LastFileRank = FirstFileRank + NFileRanksPerRank - 1; |
---|
| 901 | |
---|
| 902 | if (NRemFileRank && NRanks - i <= NRemFileRank) |
---|
| 903 | ++LastFileRank; |
---|
| 904 | } |
---|
| 905 | |
---|
| 906 | for (int i = FirstFileRank; i <= LastFileRank; ++i) |
---|
| 907 | SourceRanks.push_back(i); |
---|
| 908 | } |
---|
[a4fee13] | 909 | } |
---|
| 910 | |
---|
| 911 | HeaderSize = GH.HeaderSize; |
---|
| 912 | Header.resize(HeaderSize + CRCSize, 0xFE /* poison */); |
---|
| 913 | FH.get()->read(&Header[0], HeaderSize + CRCSize, 0, "header"); |
---|
| 914 | |
---|
| 915 | uint64_t CRC = crc64_omp(&Header[0], HeaderSize + CRCSize); |
---|
| 916 | if (CRC != (uint64_t) -1) { |
---|
| 917 | throw runtime_error("Header CRC check failed: " + LocalFileName); |
---|
| 918 | } |
---|
| 919 | } |
---|
| 920 | |
---|
[00587dc] | 921 | // Note: Errors from this function should be recoverable. This means that if |
---|
| 922 | // one rank throws an exception, then all ranks should. |
---|
[8f0a211] | 923 | void GenericIO::openAndReadHeader(MismatchBehavior MB, int EffRank, bool CheckPartMap) { |
---|
[00587dc] | 924 | int NRanks, Rank; |
---|
| 925 | #ifndef GENERICIO_NO_MPI |
---|
| 926 | MPI_Comm_rank(Comm, &Rank); |
---|
| 927 | MPI_Comm_size(Comm, &NRanks); |
---|
| 928 | #else |
---|
| 929 | Rank = 0; |
---|
| 930 | NRanks = 1; |
---|
| 931 | #endif |
---|
| 932 | |
---|
| 933 | if (EffRank == -1) |
---|
[8f0a211] | 934 | EffRank = MB == MismatchRedistribute ? 0 : Rank; |
---|
[00587dc] | 935 | |
---|
| 936 | if (RankMap.empty() && CheckPartMap) { |
---|
| 937 | // First, check to see if the file is a rank map. |
---|
| 938 | unsigned long RanksInMap = 0; |
---|
| 939 | if (Rank == 0) { |
---|
| 940 | try { |
---|
| 941 | #ifndef GENERICIO_NO_MPI |
---|
| 942 | GenericIO GIO(MPI_COMM_SELF, FileName, FileIOType); |
---|
| 943 | #else |
---|
| 944 | GenericIO GIO(FileName, FileIOType); |
---|
| 945 | #endif |
---|
[8f0a211] | 946 | GIO.openAndReadHeader(MismatchDisallowed, 0, false); |
---|
[00587dc] | 947 | RanksInMap = GIO.readNumElems(); |
---|
| 948 | |
---|
| 949 | RankMap.resize(RanksInMap + GIO.requestedExtraSpace()/sizeof(int)); |
---|
| 950 | GIO.addVariable("$partition", RankMap, true); |
---|
| 951 | |
---|
| 952 | GIO.readData(0, false); |
---|
| 953 | RankMap.resize(RanksInMap); |
---|
| 954 | } catch (...) { |
---|
| 955 | RankMap.clear(); |
---|
| 956 | RanksInMap = 0; |
---|
| 957 | } |
---|
| 958 | } |
---|
| 959 | |
---|
| 960 | #ifndef GENERICIO_NO_MPI |
---|
| 961 | MPI_Bcast(&RanksInMap, 1, MPI_UNSIGNED_LONG, 0, Comm); |
---|
| 962 | if (RanksInMap > 0) { |
---|
| 963 | RankMap.resize(RanksInMap); |
---|
| 964 | MPI_Bcast(&RankMap[0], RanksInMap, MPI_INT, 0, Comm); |
---|
| 965 | } |
---|
| 966 | #endif |
---|
| 967 | } |
---|
| 968 | |
---|
| 969 | #ifndef GENERICIO_NO_MPI |
---|
| 970 | if (SplitComm != MPI_COMM_NULL) |
---|
| 971 | MPI_Comm_free(&SplitComm); |
---|
| 972 | #endif |
---|
| 973 | |
---|
| 974 | string LocalFileName; |
---|
| 975 | if (RankMap.empty()) { |
---|
| 976 | LocalFileName = FileName; |
---|
| 977 | #ifndef GENERICIO_NO_MPI |
---|
[8f0a211] | 978 | MPI_Comm_dup(MB == MismatchRedistribute ? MPI_COMM_SELF : Comm, &SplitComm); |
---|
[00587dc] | 979 | #endif |
---|
| 980 | } else { |
---|
| 981 | stringstream ss; |
---|
| 982 | ss << FileName << "#" << RankMap[EffRank]; |
---|
| 983 | LocalFileName = ss.str(); |
---|
| 984 | #ifndef GENERICIO_NO_MPI |
---|
[8f0a211] | 985 | if (MB == MismatchRedistribute) { |
---|
| 986 | MPI_Comm_dup(MPI_COMM_SELF, &SplitComm); |
---|
| 987 | } else { |
---|
[00587dc] | 988 | #ifdef __bgq__ |
---|
[8f0a211] | 989 | MPI_Barrier(Comm); |
---|
[00587dc] | 990 | #endif |
---|
[8f0a211] | 991 | MPI_Comm_split(Comm, RankMap[EffRank], Rank, &SplitComm); |
---|
| 992 | } |
---|
[00587dc] | 993 | #endif |
---|
| 994 | } |
---|
| 995 | |
---|
| 996 | if (LocalFileName == OpenFileName) |
---|
| 997 | return; |
---|
| 998 | FH.close(); |
---|
| 999 | |
---|
| 1000 | int SplitNRanks, SplitRank; |
---|
| 1001 | #ifndef GENERICIO_NO_MPI |
---|
| 1002 | MPI_Comm_rank(SplitComm, &SplitRank); |
---|
| 1003 | MPI_Comm_size(SplitComm, &SplitNRanks); |
---|
| 1004 | #else |
---|
| 1005 | SplitRank = 0; |
---|
| 1006 | SplitNRanks = 1; |
---|
| 1007 | #endif |
---|
| 1008 | |
---|
| 1009 | uint64_t HeaderSize; |
---|
| 1010 | vector<char> Header; |
---|
| 1011 | |
---|
| 1012 | if (SplitRank == 0) { |
---|
| 1013 | #ifndef GENERICIO_NO_MPI |
---|
| 1014 | if (FileIOType == FileIOMPI) |
---|
| 1015 | FH.get() = new GenericFileIO_MPI(MPI_COMM_SELF); |
---|
| 1016 | else if (FileIOType == FileIOMPICollective) |
---|
| 1017 | FH.get() = new GenericFileIO_MPICollective(MPI_COMM_SELF); |
---|
| 1018 | else |
---|
| 1019 | #endif |
---|
| 1020 | FH.get() = new GenericFileIO_POSIX(); |
---|
| 1021 | |
---|
| 1022 | #ifndef GENERICIO_NO_MPI |
---|
| 1023 | char True = 1, False = 0; |
---|
| 1024 | #endif |
---|
| 1025 | |
---|
| 1026 | try { |
---|
| 1027 | FH.get()->open(LocalFileName, true); |
---|
| 1028 | |
---|
[a4fee13] | 1029 | GlobalHeader<false> GH; // endianness does not matter yet... |
---|
| 1030 | FH.get()->read(&GH, sizeof(GlobalHeader<false>), 0, "global header"); |
---|
[00587dc] | 1031 | |
---|
[a4fee13] | 1032 | if (string(GH.Magic, GH.Magic + MagicSize - 1) == MagicLE) { |
---|
[8f0a211] | 1033 | readHeaderLeader<false>(&GH, MB, NRanks, Rank, SplitNRanks, LocalFileName, |
---|
[a4fee13] | 1034 | HeaderSize, Header); |
---|
| 1035 | } else if (string(GH.Magic, GH.Magic + MagicSize - 1) == MagicBE) { |
---|
[8f0a211] | 1036 | readHeaderLeader<true>(&GH, MB, NRanks, Rank, SplitNRanks, LocalFileName, |
---|
[a4fee13] | 1037 | HeaderSize, Header); |
---|
| 1038 | } else { |
---|
| 1039 | string Error = "invalid file-type identifier"; |
---|
[00587dc] | 1040 | throw runtime_error("Won't read " + LocalFileName + ": " + Error); |
---|
| 1041 | } |
---|
| 1042 | |
---|
| 1043 | #ifndef GENERICIO_NO_MPI |
---|
| 1044 | close(); |
---|
| 1045 | MPI_Bcast(&True, 1, MPI_BYTE, 0, SplitComm); |
---|
| 1046 | #endif |
---|
| 1047 | } catch (...) { |
---|
| 1048 | #ifndef GENERICIO_NO_MPI |
---|
| 1049 | MPI_Bcast(&False, 1, MPI_BYTE, 0, SplitComm); |
---|
| 1050 | #endif |
---|
| 1051 | close(); |
---|
| 1052 | throw; |
---|
| 1053 | } |
---|
| 1054 | } else { |
---|
| 1055 | #ifndef GENERICIO_NO_MPI |
---|
| 1056 | char Okay; |
---|
| 1057 | MPI_Bcast(&Okay, 1, MPI_BYTE, 0, SplitComm); |
---|
| 1058 | if (!Okay) |
---|
| 1059 | throw runtime_error("Failure broadcast from rank 0"); |
---|
| 1060 | #endif |
---|
| 1061 | } |
---|
| 1062 | |
---|
| 1063 | #ifndef GENERICIO_NO_MPI |
---|
| 1064 | MPI_Bcast(&HeaderSize, 1, MPI_UINT64_T, 0, SplitComm); |
---|
| 1065 | #endif |
---|
| 1066 | |
---|
| 1067 | Header.resize(HeaderSize, 0xFD /* poison */); |
---|
| 1068 | #ifndef GENERICIO_NO_MPI |
---|
| 1069 | MPI_Bcast(&Header[0], HeaderSize, MPI_BYTE, 0, SplitComm); |
---|
| 1070 | #endif |
---|
| 1071 | |
---|
| 1072 | FH.getHeaderCache().clear(); |
---|
[a4fee13] | 1073 | |
---|
| 1074 | GlobalHeader<false> *GH = (GlobalHeader<false> *) &Header[0]; |
---|
| 1075 | FH.setIsBigEndian(string(GH->Magic, GH->Magic + MagicSize - 1) == MagicBE); |
---|
| 1076 | |
---|
[00587dc] | 1077 | FH.getHeaderCache().swap(Header); |
---|
| 1078 | OpenFileName = LocalFileName; |
---|
| 1079 | |
---|
| 1080 | #ifndef GENERICIO_NO_MPI |
---|
[8f0a211] | 1081 | if (!DisableCollErrChecking) |
---|
| 1082 | MPI_Barrier(Comm); |
---|
[00587dc] | 1083 | |
---|
| 1084 | if (FileIOType == FileIOMPI) |
---|
| 1085 | FH.get() = new GenericFileIO_MPI(SplitComm); |
---|
| 1086 | else if (FileIOType == FileIOMPICollective) |
---|
| 1087 | FH.get() = new GenericFileIO_MPICollective(SplitComm); |
---|
| 1088 | else |
---|
| 1089 | FH.get() = new GenericFileIO_POSIX(); |
---|
| 1090 | |
---|
| 1091 | int OpenErr = 0, TotOpenErr; |
---|
| 1092 | try { |
---|
| 1093 | FH.get()->open(LocalFileName, true); |
---|
[8f0a211] | 1094 | MPI_Allreduce(&OpenErr, &TotOpenErr, 1, MPI_INT, MPI_SUM, |
---|
| 1095 | DisableCollErrChecking ? MPI_COMM_SELF : Comm); |
---|
[00587dc] | 1096 | } catch (...) { |
---|
| 1097 | OpenErr = 1; |
---|
[8f0a211] | 1098 | MPI_Allreduce(&OpenErr, &TotOpenErr, 1, MPI_INT, MPI_SUM, |
---|
| 1099 | DisableCollErrChecking ? MPI_COMM_SELF : Comm); |
---|
[00587dc] | 1100 | throw; |
---|
| 1101 | } |
---|
| 1102 | |
---|
| 1103 | if (TotOpenErr > 0) { |
---|
| 1104 | stringstream ss; |
---|
| 1105 | ss << TotOpenErr << " ranks failed to open file: " << LocalFileName; |
---|
| 1106 | throw runtime_error(ss.str()); |
---|
| 1107 | } |
---|
| 1108 | #endif |
---|
| 1109 | } |
---|
| 1110 | |
---|
| 1111 | int GenericIO::readNRanks() { |
---|
[a4fee13] | 1112 | if (FH.isBigEndian()) |
---|
| 1113 | return readNRanks<true>(); |
---|
| 1114 | return readNRanks<false>(); |
---|
| 1115 | } |
---|
| 1116 | |
---|
| 1117 | template <bool IsBigEndian> |
---|
| 1118 | int GenericIO::readNRanks() { |
---|
[00587dc] | 1119 | if (RankMap.size()) |
---|
| 1120 | return RankMap.size(); |
---|
| 1121 | |
---|
| 1122 | assert(FH.getHeaderCache().size() && "HeaderCache must not be empty"); |
---|
[a4fee13] | 1123 | GlobalHeader<IsBigEndian> *GH = (GlobalHeader<IsBigEndian> *) &FH.getHeaderCache()[0]; |
---|
[00587dc] | 1124 | return (int) GH->NRanks; |
---|
| 1125 | } |
---|
| 1126 | |
---|
| 1127 | void GenericIO::readDims(int Dims[3]) { |
---|
[a4fee13] | 1128 | if (FH.isBigEndian()) |
---|
| 1129 | readDims<true>(Dims); |
---|
| 1130 | else |
---|
| 1131 | readDims<false>(Dims); |
---|
| 1132 | } |
---|
| 1133 | |
---|
| 1134 | template <bool IsBigEndian> |
---|
| 1135 | void GenericIO::readDims(int Dims[3]) { |
---|
[00587dc] | 1136 | assert(FH.getHeaderCache().size() && "HeaderCache must not be empty"); |
---|
[a4fee13] | 1137 | GlobalHeader<IsBigEndian> *GH = (GlobalHeader<IsBigEndian> *) &FH.getHeaderCache()[0]; |
---|
[00587dc] | 1138 | std::copy(GH->Dims, GH->Dims + 3, Dims); |
---|
| 1139 | } |
---|
| 1140 | |
---|
| 1141 | uint64_t GenericIO::readTotalNumElems() { |
---|
[a4fee13] | 1142 | if (FH.isBigEndian()) |
---|
| 1143 | return readTotalNumElems<true>(); |
---|
| 1144 | return readTotalNumElems<false>(); |
---|
| 1145 | } |
---|
| 1146 | |
---|
| 1147 | template <bool IsBigEndian> |
---|
| 1148 | uint64_t GenericIO::readTotalNumElems() { |
---|
[00587dc] | 1149 | if (RankMap.size()) |
---|
| 1150 | return (uint64_t) -1; |
---|
| 1151 | |
---|
| 1152 | assert(FH.getHeaderCache().size() && "HeaderCache must not be empty"); |
---|
[a4fee13] | 1153 | GlobalHeader<IsBigEndian> *GH = (GlobalHeader<IsBigEndian> *) &FH.getHeaderCache()[0]; |
---|
[00587dc] | 1154 | return GH->NElems; |
---|
| 1155 | } |
---|
| 1156 | |
---|
| 1157 | void GenericIO::readPhysOrigin(double Origin[3]) { |
---|
[a4fee13] | 1158 | if (FH.isBigEndian()) |
---|
| 1159 | readPhysOrigin<true>(Origin); |
---|
| 1160 | else |
---|
| 1161 | readPhysOrigin<false>(Origin); |
---|
| 1162 | } |
---|
| 1163 | |
---|
| 1164 | // Define a "safe" version of offsetof (offsetof itself might not work for |
---|
| 1165 | // non-POD types, and at least xlC v12.1 will complain about this if you try). |
---|
| 1166 | #define offsetof_safe(S, F) (size_t(&(S)->F) - size_t(S)) |
---|
| 1167 | |
---|
| 1168 | template <bool IsBigEndian> |
---|
| 1169 | void GenericIO::readPhysOrigin(double Origin[3]) { |
---|
[00587dc] | 1170 | assert(FH.getHeaderCache().size() && "HeaderCache must not be empty"); |
---|
[a4fee13] | 1171 | GlobalHeader<IsBigEndian> *GH = (GlobalHeader<IsBigEndian> *) &FH.getHeaderCache()[0]; |
---|
| 1172 | if (offsetof_safe(GH, PhysOrigin) >= GH->GlobalHeaderSize) { |
---|
[00587dc] | 1173 | std::fill(Origin, Origin + 3, 0.0); |
---|
| 1174 | return; |
---|
| 1175 | } |
---|
| 1176 | |
---|
| 1177 | std::copy(GH->PhysOrigin, GH->PhysOrigin + 3, Origin); |
---|
| 1178 | } |
---|
| 1179 | |
---|
| 1180 | void GenericIO::readPhysScale(double Scale[3]) { |
---|
[a4fee13] | 1181 | if (FH.isBigEndian()) |
---|
| 1182 | readPhysScale<true>(Scale); |
---|
| 1183 | else |
---|
| 1184 | readPhysScale<false>(Scale); |
---|
| 1185 | } |
---|
| 1186 | |
---|
| 1187 | template <bool IsBigEndian> |
---|
| 1188 | void GenericIO::readPhysScale(double Scale[3]) { |
---|
[00587dc] | 1189 | assert(FH.getHeaderCache().size() && "HeaderCache must not be empty"); |
---|
[a4fee13] | 1190 | GlobalHeader<IsBigEndian> *GH = (GlobalHeader<IsBigEndian> *) &FH.getHeaderCache()[0]; |
---|
| 1191 | if (offsetof_safe(GH, PhysScale) >= GH->GlobalHeaderSize) { |
---|
[00587dc] | 1192 | std::fill(Scale, Scale + 3, 0.0); |
---|
| 1193 | return; |
---|
| 1194 | } |
---|
| 1195 | |
---|
| 1196 | std::copy(GH->PhysScale, GH->PhysScale + 3, Scale); |
---|
| 1197 | } |
---|
| 1198 | |
---|
[a4fee13] | 1199 | template <bool IsBigEndian> |
---|
| 1200 | static size_t getRankIndex(int EffRank, GlobalHeader<IsBigEndian> *GH, |
---|
[00587dc] | 1201 | vector<int> &RankMap, vector<char> &HeaderCache) { |
---|
[a4fee13] | 1202 | if (RankMap.empty()) |
---|
[00587dc] | 1203 | return EffRank; |
---|
| 1204 | |
---|
| 1205 | for (size_t i = 0; i < GH->NRanks; ++i) { |
---|
[a4fee13] | 1206 | RankHeader<IsBigEndian> *RH = (RankHeader<IsBigEndian> *) &HeaderCache[GH->RanksStart + |
---|
[00587dc] | 1207 | i*GH->RanksSize]; |
---|
[a4fee13] | 1208 | if (offsetof_safe(RH, GlobalRank) >= GH->RanksSize) |
---|
| 1209 | return EffRank; |
---|
| 1210 | |
---|
[00587dc] | 1211 | if ((int) RH->GlobalRank == EffRank) |
---|
| 1212 | return i; |
---|
| 1213 | } |
---|
| 1214 | |
---|
| 1215 | assert(false && "Index requested of an invalid rank"); |
---|
| 1216 | return (size_t) -1; |
---|
| 1217 | } |
---|
| 1218 | |
---|
| 1219 | int GenericIO::readGlobalRankNumber(int EffRank) { |
---|
[a4fee13] | 1220 | if (FH.isBigEndian()) |
---|
| 1221 | return readGlobalRankNumber<true>(EffRank); |
---|
| 1222 | return readGlobalRankNumber<false>(EffRank); |
---|
| 1223 | } |
---|
| 1224 | |
---|
| 1225 | template <bool IsBigEndian> |
---|
| 1226 | int GenericIO::readGlobalRankNumber(int EffRank) { |
---|
[00587dc] | 1227 | if (EffRank == -1) { |
---|
| 1228 | #ifndef GENERICIO_NO_MPI |
---|
| 1229 | MPI_Comm_rank(Comm, &EffRank); |
---|
| 1230 | #else |
---|
| 1231 | EffRank = 0; |
---|
| 1232 | #endif |
---|
| 1233 | } |
---|
| 1234 | |
---|
[8f0a211] | 1235 | openAndReadHeader(MismatchAllowed, EffRank, false); |
---|
[00587dc] | 1236 | |
---|
| 1237 | assert(FH.getHeaderCache().size() && "HeaderCache must not be empty"); |
---|
| 1238 | |
---|
[a4fee13] | 1239 | GlobalHeader<IsBigEndian> *GH = (GlobalHeader<IsBigEndian> *) &FH.getHeaderCache()[0]; |
---|
| 1240 | size_t RankIndex = getRankIndex<IsBigEndian>(EffRank, GH, RankMap, FH.getHeaderCache()); |
---|
[00587dc] | 1241 | |
---|
| 1242 | assert(RankIndex < GH->NRanks && "Invalid rank specified"); |
---|
| 1243 | |
---|
[a4fee13] | 1244 | RankHeader<IsBigEndian> *RH = (RankHeader<IsBigEndian> *) &FH.getHeaderCache()[GH->RanksStart + |
---|
[00587dc] | 1245 | RankIndex*GH->RanksSize]; |
---|
| 1246 | |
---|
[a4fee13] | 1247 | if (offsetof_safe(RH, GlobalRank) >= GH->RanksSize) |
---|
| 1248 | return EffRank; |
---|
| 1249 | |
---|
[00587dc] | 1250 | return (int) RH->GlobalRank; |
---|
| 1251 | } |
---|
| 1252 | |
---|
[14e73bb] | 1253 | void GenericIO::getSourceRanks(vector<int> &SR) { |
---|
| 1254 | SR.clear(); |
---|
| 1255 | |
---|
| 1256 | if (Redistributing) { |
---|
| 1257 | std::copy(SourceRanks.begin(), SourceRanks.end(), std::back_inserter(SR)); |
---|
| 1258 | return; |
---|
| 1259 | } |
---|
| 1260 | |
---|
| 1261 | int Rank; |
---|
| 1262 | #ifndef GENERICIO_NO_MPI |
---|
| 1263 | MPI_Comm_rank(Comm, &Rank); |
---|
| 1264 | #else |
---|
| 1265 | Rank = 0; |
---|
| 1266 | #endif |
---|
| 1267 | |
---|
| 1268 | SR.push_back(Rank); |
---|
| 1269 | } |
---|
| 1270 | |
---|
[00587dc] | 1271 | size_t GenericIO::readNumElems(int EffRank) { |
---|
[8f0a211] | 1272 | if (EffRank == -1 && Redistributing) { |
---|
| 1273 | DisableCollErrChecking = true; |
---|
| 1274 | |
---|
| 1275 | size_t TotalSize = 0; |
---|
| 1276 | for (int i = 0, ie = SourceRanks.size(); i != ie; ++i) |
---|
| 1277 | TotalSize += readNumElems(SourceRanks[i]); |
---|
| 1278 | |
---|
| 1279 | DisableCollErrChecking = false; |
---|
| 1280 | return TotalSize; |
---|
| 1281 | } |
---|
| 1282 | |
---|
[a4fee13] | 1283 | if (FH.isBigEndian()) |
---|
| 1284 | return readNumElems<true>(EffRank); |
---|
| 1285 | return readNumElems<false>(EffRank); |
---|
| 1286 | } |
---|
| 1287 | |
---|
| 1288 | template <bool IsBigEndian> |
---|
| 1289 | size_t GenericIO::readNumElems(int EffRank) { |
---|
[00587dc] | 1290 | if (EffRank == -1) { |
---|
| 1291 | #ifndef GENERICIO_NO_MPI |
---|
| 1292 | MPI_Comm_rank(Comm, &EffRank); |
---|
| 1293 | #else |
---|
| 1294 | EffRank = 0; |
---|
| 1295 | #endif |
---|
| 1296 | } |
---|
| 1297 | |
---|
[8f0a211] | 1298 | openAndReadHeader(Redistributing ? MismatchRedistribute : MismatchAllowed, |
---|
| 1299 | EffRank, false); |
---|
[00587dc] | 1300 | |
---|
| 1301 | assert(FH.getHeaderCache().size() && "HeaderCache must not be empty"); |
---|
| 1302 | |
---|
[a4fee13] | 1303 | GlobalHeader<IsBigEndian> *GH = (GlobalHeader<IsBigEndian> *) &FH.getHeaderCache()[0]; |
---|
| 1304 | size_t RankIndex = getRankIndex<IsBigEndian>(EffRank, GH, RankMap, FH.getHeaderCache()); |
---|
[00587dc] | 1305 | |
---|
| 1306 | assert(RankIndex < GH->NRanks && "Invalid rank specified"); |
---|
| 1307 | |
---|
[a4fee13] | 1308 | RankHeader<IsBigEndian> *RH = (RankHeader<IsBigEndian> *) &FH.getHeaderCache()[GH->RanksStart + |
---|
[00587dc] | 1309 | RankIndex*GH->RanksSize]; |
---|
| 1310 | return (size_t) RH->NElems; |
---|
| 1311 | } |
---|
| 1312 | |
---|
| 1313 | void GenericIO::readCoords(int Coords[3], int EffRank) { |
---|
[8f0a211] | 1314 | if (EffRank == -1 && Redistributing) { |
---|
| 1315 | std::fill(Coords, Coords + 3, 0); |
---|
| 1316 | return; |
---|
| 1317 | } |
---|
| 1318 | |
---|
[a4fee13] | 1319 | if (FH.isBigEndian()) |
---|
| 1320 | readCoords<true>(Coords, EffRank); |
---|
| 1321 | else |
---|
| 1322 | readCoords<false>(Coords, EffRank); |
---|
| 1323 | } |
---|
| 1324 | |
---|
| 1325 | template <bool IsBigEndian> |
---|
| 1326 | void GenericIO::readCoords(int Coords[3], int EffRank) { |
---|
[00587dc] | 1327 | if (EffRank == -1) { |
---|
| 1328 | #ifndef GENERICIO_NO_MPI |
---|
| 1329 | MPI_Comm_rank(Comm, &EffRank); |
---|
| 1330 | #else |
---|
| 1331 | EffRank = 0; |
---|
| 1332 | #endif |
---|
| 1333 | } |
---|
| 1334 | |
---|
[8f0a211] | 1335 | openAndReadHeader(MismatchAllowed, EffRank, false); |
---|
[00587dc] | 1336 | |
---|
| 1337 | assert(FH.getHeaderCache().size() && "HeaderCache must not be empty"); |
---|
| 1338 | |
---|
[a4fee13] | 1339 | GlobalHeader<IsBigEndian> *GH = (GlobalHeader<IsBigEndian> *) &FH.getHeaderCache()[0]; |
---|
| 1340 | size_t RankIndex = getRankIndex<IsBigEndian>(EffRank, GH, RankMap, FH.getHeaderCache()); |
---|
[00587dc] | 1341 | |
---|
| 1342 | assert(RankIndex < GH->NRanks && "Invalid rank specified"); |
---|
| 1343 | |
---|
[a4fee13] | 1344 | RankHeader<IsBigEndian> *RH = (RankHeader<IsBigEndian> *) &FH.getHeaderCache()[GH->RanksStart + |
---|
[00587dc] | 1345 | RankIndex*GH->RanksSize]; |
---|
| 1346 | |
---|
| 1347 | std::copy(RH->Coords, RH->Coords + 3, Coords); |
---|
| 1348 | } |
---|
| 1349 | |
---|
[a4fee13] | 1350 | void GenericIO::readData(int EffRank, bool PrintStats, bool CollStats) { |
---|
[00587dc] | 1351 | int Rank; |
---|
| 1352 | #ifndef GENERICIO_NO_MPI |
---|
| 1353 | MPI_Comm_rank(Comm, &Rank); |
---|
| 1354 | #else |
---|
| 1355 | Rank = 0; |
---|
| 1356 | #endif |
---|
| 1357 | |
---|
[8f0a211] | 1358 | uint64_t TotalReadSize = 0; |
---|
| 1359 | #ifndef GENERICIO_NO_MPI |
---|
| 1360 | double StartTime = MPI_Wtime(); |
---|
| 1361 | #else |
---|
| 1362 | double StartTime = double(clock())/CLOCKS_PER_SEC; |
---|
| 1363 | #endif |
---|
| 1364 | |
---|
| 1365 | int NErrs[3] = { 0, 0, 0 }; |
---|
| 1366 | |
---|
| 1367 | if (EffRank == -1 && Redistributing) { |
---|
| 1368 | DisableCollErrChecking = true; |
---|
| 1369 | |
---|
| 1370 | size_t RowOffset = 0; |
---|
| 1371 | for (int i = 0, ie = SourceRanks.size(); i != ie; ++i) { |
---|
| 1372 | readData(SourceRanks[i], RowOffset, Rank, TotalReadSize, NErrs); |
---|
| 1373 | RowOffset += readNumElems(SourceRanks[i]); |
---|
| 1374 | } |
---|
| 1375 | |
---|
| 1376 | DisableCollErrChecking = false; |
---|
| 1377 | } else { |
---|
| 1378 | readData(EffRank, 0, Rank, TotalReadSize, NErrs); |
---|
| 1379 | } |
---|
| 1380 | |
---|
| 1381 | int AllNErrs[3]; |
---|
| 1382 | #ifndef GENERICIO_NO_MPI |
---|
| 1383 | MPI_Allreduce(NErrs, AllNErrs, 3, MPI_INT, MPI_SUM, Comm); |
---|
| 1384 | #else |
---|
| 1385 | AllNErrs[0] = NErrs[0]; AllNErrs[1] = NErrs[1]; AllNErrs[2] = NErrs[2]; |
---|
| 1386 | #endif |
---|
| 1387 | |
---|
| 1388 | if (AllNErrs[0] > 0 || AllNErrs[1] > 0 || AllNErrs[2] > 0) { |
---|
| 1389 | stringstream ss; |
---|
| 1390 | ss << "Experienced " << AllNErrs[0] << " I/O error(s), " << |
---|
| 1391 | AllNErrs[1] << " CRC error(s) and " << AllNErrs[2] << |
---|
| 1392 | " decompression CRC error(s) reading: " << OpenFileName; |
---|
| 1393 | throw runtime_error(ss.str()); |
---|
| 1394 | } |
---|
| 1395 | |
---|
| 1396 | #ifndef GENERICIO_NO_MPI |
---|
| 1397 | MPI_Barrier(Comm); |
---|
| 1398 | #endif |
---|
| 1399 | |
---|
| 1400 | #ifndef GENERICIO_NO_MPI |
---|
| 1401 | double EndTime = MPI_Wtime(); |
---|
| 1402 | #else |
---|
| 1403 | double EndTime = double(clock())/CLOCKS_PER_SEC; |
---|
| 1404 | #endif |
---|
| 1405 | |
---|
| 1406 | double TotalTime = EndTime - StartTime; |
---|
| 1407 | double MaxTotalTime; |
---|
| 1408 | #ifndef GENERICIO_NO_MPI |
---|
| 1409 | if (CollStats) |
---|
| 1410 | MPI_Reduce(&TotalTime, &MaxTotalTime, 1, MPI_DOUBLE, MPI_MAX, 0, Comm); |
---|
| 1411 | else |
---|
| 1412 | #endif |
---|
| 1413 | MaxTotalTime = TotalTime; |
---|
| 1414 | |
---|
| 1415 | uint64_t AllTotalReadSize; |
---|
| 1416 | #ifndef GENERICIO_NO_MPI |
---|
| 1417 | if (CollStats) |
---|
| 1418 | MPI_Reduce(&TotalReadSize, &AllTotalReadSize, 1, MPI_UINT64_T, MPI_SUM, 0, Comm); |
---|
| 1419 | else |
---|
| 1420 | #endif |
---|
| 1421 | AllTotalReadSize = TotalReadSize; |
---|
| 1422 | |
---|
| 1423 | if (Rank == 0 && PrintStats) { |
---|
| 1424 | double Rate = ((double) AllTotalReadSize) / MaxTotalTime / (1024.*1024.); |
---|
[d6628a6] | 1425 | std::cout << "Read " << Vars.size() << " variables from " << FileName << |
---|
| 1426 | " (" << AllTotalReadSize << " bytes) in " << MaxTotalTime << "s: " << |
---|
| 1427 | Rate << " MB/s [excluding header read]" << std::endl; |
---|
[8f0a211] | 1428 | } |
---|
| 1429 | } |
---|
| 1430 | |
---|
| 1431 | void GenericIO::readData(int EffRank, size_t RowOffset, int Rank, |
---|
| 1432 | uint64_t &TotalReadSize, int NErrs[3]) { |
---|
| 1433 | if (FH.isBigEndian()) |
---|
| 1434 | readData<true>(EffRank, RowOffset, Rank, TotalReadSize, NErrs); |
---|
| 1435 | else |
---|
| 1436 | readData<false>(EffRank, RowOffset, Rank, TotalReadSize, NErrs); |
---|
| 1437 | } |
---|
| 1438 | |
---|
| 1439 | // Note: Errors from this function should be recoverable. This means that if |
---|
| 1440 | // one rank throws an exception, then all ranks should. |
---|
| 1441 | template <bool IsBigEndian> |
---|
| 1442 | void GenericIO::readData(int EffRank, size_t RowOffset, int Rank, |
---|
| 1443 | uint64_t &TotalReadSize, int NErrs[3]) { |
---|
| 1444 | openAndReadHeader(Redistributing ? MismatchRedistribute : MismatchAllowed, |
---|
| 1445 | EffRank, false); |
---|
[00587dc] | 1446 | |
---|
| 1447 | assert(FH.getHeaderCache().size() && "HeaderCache must not be empty"); |
---|
| 1448 | |
---|
| 1449 | if (EffRank == -1) |
---|
| 1450 | EffRank = Rank; |
---|
| 1451 | |
---|
[a4fee13] | 1452 | GlobalHeader<IsBigEndian> *GH = (GlobalHeader<IsBigEndian> *) &FH.getHeaderCache()[0]; |
---|
| 1453 | size_t RankIndex = getRankIndex<IsBigEndian>(EffRank, GH, RankMap, FH.getHeaderCache()); |
---|
[00587dc] | 1454 | |
---|
| 1455 | assert(RankIndex < GH->NRanks && "Invalid rank specified"); |
---|
| 1456 | |
---|
[a4fee13] | 1457 | RankHeader<IsBigEndian> *RH = (RankHeader<IsBigEndian> *) &FH.getHeaderCache()[GH->RanksStart + |
---|
[00587dc] | 1458 | RankIndex*GH->RanksSize]; |
---|
| 1459 | |
---|
| 1460 | for (size_t i = 0; i < Vars.size(); ++i) { |
---|
| 1461 | uint64_t Offset = RH->Start; |
---|
| 1462 | bool VarFound = false; |
---|
| 1463 | for (uint64_t j = 0; j < GH->NVars; ++j) { |
---|
[a4fee13] | 1464 | VariableHeader<IsBigEndian> *VH = (VariableHeader<IsBigEndian> *) &FH.getHeaderCache()[GH->VarsStart + |
---|
[00587dc] | 1465 | j*GH->VarsSize]; |
---|
| 1466 | |
---|
| 1467 | string VName(VH->Name, VH->Name + NameSize); |
---|
| 1468 | size_t VNameNull = VName.find('\0'); |
---|
| 1469 | if (VNameNull < NameSize) |
---|
| 1470 | VName.resize(VNameNull); |
---|
| 1471 | |
---|
| 1472 | uint64_t ReadSize = RH->NElems*VH->Size + CRCSize; |
---|
| 1473 | if (VName != Vars[i].Name) { |
---|
| 1474 | Offset += ReadSize; |
---|
| 1475 | continue; |
---|
| 1476 | } |
---|
| 1477 | |
---|
[5d57155] | 1478 | size_t ElementSize = VH->Size; |
---|
| 1479 | if (offsetof_safe(VH, ElementSize) < GH->VarsSize) |
---|
| 1480 | ElementSize = VH->ElementSize; |
---|
| 1481 | |
---|
[00587dc] | 1482 | VarFound = true; |
---|
| 1483 | bool IsFloat = (bool) (VH->Flags & FloatValue), |
---|
| 1484 | IsSigned = (bool) (VH->Flags & SignedValue); |
---|
| 1485 | if (VH->Size != Vars[i].Size) { |
---|
| 1486 | stringstream ss; |
---|
| 1487 | ss << "Size mismatch for variable " << Vars[i].Name << |
---|
| 1488 | " in: " << OpenFileName << ": current: " << Vars[i].Size << |
---|
| 1489 | ", file: " << VH->Size; |
---|
| 1490 | throw runtime_error(ss.str()); |
---|
[5d57155] | 1491 | } else if (ElementSize != Vars[i].ElementSize) { |
---|
| 1492 | stringstream ss; |
---|
| 1493 | ss << "Element size mismatch for variable " << Vars[i].Name << |
---|
| 1494 | " in: " << OpenFileName << ": current: " << Vars[i].ElementSize << |
---|
| 1495 | ", file: " << ElementSize; |
---|
| 1496 | throw runtime_error(ss.str()); |
---|
[00587dc] | 1497 | } else if (IsFloat != Vars[i].IsFloat) { |
---|
| 1498 | string Float("float"), Int("integer"); |
---|
| 1499 | stringstream ss; |
---|
| 1500 | ss << "Type mismatch for variable " << Vars[i].Name << |
---|
| 1501 | " in: " << OpenFileName << ": current: " << |
---|
| 1502 | (Vars[i].IsFloat ? Float : Int) << |
---|
| 1503 | ", file: " << (IsFloat ? Float : Int); |
---|
| 1504 | throw runtime_error(ss.str()); |
---|
| 1505 | } else if (IsSigned != Vars[i].IsSigned) { |
---|
| 1506 | string Signed("signed"), Uns("unsigned"); |
---|
| 1507 | stringstream ss; |
---|
| 1508 | ss << "Type mismatch for variable " << Vars[i].Name << |
---|
| 1509 | " in: " << OpenFileName << ": current: " << |
---|
| 1510 | (Vars[i].IsSigned ? Signed : Uns) << |
---|
| 1511 | ", file: " << (IsSigned ? Signed : Uns); |
---|
| 1512 | throw runtime_error(ss.str()); |
---|
| 1513 | } |
---|
| 1514 | |
---|
[8f0a211] | 1515 | size_t VarOffset = RowOffset*Vars[i].Size; |
---|
| 1516 | void *VarData = ((char *) Vars[i].Data) + VarOffset; |
---|
| 1517 | |
---|
[00587dc] | 1518 | vector<unsigned char> LData; |
---|
[eeacdad] | 1519 | bool HasSZ = false; |
---|
[8f0a211] | 1520 | void *Data = VarData; |
---|
[00587dc] | 1521 | bool HasExtraSpace = Vars[i].HasExtraSpace; |
---|
[a4fee13] | 1522 | if (offsetof_safe(GH, BlocksStart) < GH->GlobalHeaderSize && |
---|
[00587dc] | 1523 | GH->BlocksSize > 0) { |
---|
[a4fee13] | 1524 | BlockHeader<IsBigEndian> *BH = (BlockHeader<IsBigEndian> *) |
---|
[00587dc] | 1525 | &FH.getHeaderCache()[GH->BlocksStart + |
---|
| 1526 | (RankIndex*GH->NVars + j)*GH->BlocksSize]; |
---|
| 1527 | ReadSize = BH->Size + CRCSize; |
---|
| 1528 | Offset = BH->Start; |
---|
| 1529 | |
---|
[eeacdad] | 1530 | int FilterIdx = 0; |
---|
| 1531 | |
---|
| 1532 | if (strncmp(BH->Filters[FilterIdx], LossyCompressName, FilterNameSize) == 0) { |
---|
| 1533 | ++FilterIdx; |
---|
| 1534 | HasSZ = true; |
---|
| 1535 | } |
---|
| 1536 | |
---|
| 1537 | if (strncmp(BH->Filters[FilterIdx], CompressName, FilterNameSize) == 0) { |
---|
[00587dc] | 1538 | LData.resize(ReadSize); |
---|
| 1539 | Data = &LData[0]; |
---|
| 1540 | HasExtraSpace = true; |
---|
[eeacdad] | 1541 | } else if (BH->Filters[FilterIdx][0] != '\0') { |
---|
[00587dc] | 1542 | stringstream ss; |
---|
| 1543 | ss << "Unknown filter \"" << BH->Filters[0] << "\" on variable " << Vars[i].Name; |
---|
| 1544 | throw runtime_error(ss.str()); |
---|
| 1545 | } |
---|
| 1546 | } |
---|
| 1547 | |
---|
| 1548 | assert(HasExtraSpace && "Extra space required for reading"); |
---|
| 1549 | |
---|
| 1550 | char CRCSave[CRCSize]; |
---|
| 1551 | char *CRCLoc = ((char *) Data) + ReadSize - CRCSize; |
---|
| 1552 | if (HasExtraSpace) |
---|
| 1553 | std::copy(CRCLoc, CRCLoc + CRCSize, CRCSave); |
---|
| 1554 | |
---|
| 1555 | int Retry = 0; |
---|
| 1556 | { |
---|
| 1557 | int RetryCount = 300; |
---|
| 1558 | const char *EnvStr = getenv("GENERICIO_RETRY_COUNT"); |
---|
| 1559 | if (EnvStr) |
---|
| 1560 | RetryCount = atoi(EnvStr); |
---|
| 1561 | |
---|
| 1562 | int RetrySleep = 100; // ms |
---|
| 1563 | EnvStr = getenv("GENERICIO_RETRY_SLEEP"); |
---|
| 1564 | if (EnvStr) |
---|
| 1565 | RetrySleep = atoi(EnvStr); |
---|
| 1566 | |
---|
| 1567 | for (; Retry < RetryCount; ++Retry) { |
---|
| 1568 | try { |
---|
| 1569 | FH.get()->read(Data, ReadSize, Offset, Vars[i].Name); |
---|
| 1570 | break; |
---|
| 1571 | } catch (...) { } |
---|
| 1572 | |
---|
| 1573 | usleep(1000*RetrySleep); |
---|
| 1574 | } |
---|
| 1575 | |
---|
| 1576 | if (Retry == RetryCount) { |
---|
| 1577 | ++NErrs[0]; |
---|
| 1578 | break; |
---|
| 1579 | } else if (Retry > 0) { |
---|
| 1580 | EnvStr = getenv("GENERICIO_VERBOSE"); |
---|
| 1581 | if (EnvStr) { |
---|
| 1582 | int Mod = atoi(EnvStr); |
---|
| 1583 | if (Mod > 0) { |
---|
| 1584 | int Rank; |
---|
| 1585 | #ifndef GENERICIO_NO_MPI |
---|
| 1586 | MPI_Comm_rank(MPI_COMM_WORLD, &Rank); |
---|
| 1587 | #else |
---|
| 1588 | Rank = 0; |
---|
| 1589 | #endif |
---|
| 1590 | |
---|
| 1591 | std::cerr << "Rank " << Rank << ": " << Retry << |
---|
| 1592 | " I/O retries were necessary for reading " << |
---|
| 1593 | Vars[i].Name << " from: " << OpenFileName << "\n"; |
---|
| 1594 | |
---|
| 1595 | std::cerr.flush(); |
---|
| 1596 | } |
---|
| 1597 | } |
---|
| 1598 | } |
---|
| 1599 | } |
---|
| 1600 | |
---|
| 1601 | TotalReadSize += ReadSize; |
---|
| 1602 | |
---|
| 1603 | uint64_t CRC = crc64_omp(Data, ReadSize); |
---|
| 1604 | if (CRC != (uint64_t) -1) { |
---|
| 1605 | ++NErrs[1]; |
---|
| 1606 | |
---|
| 1607 | int Rank; |
---|
| 1608 | #ifndef GENERICIO_NO_MPI |
---|
| 1609 | MPI_Comm_rank(MPI_COMM_WORLD, &Rank); |
---|
| 1610 | #else |
---|
| 1611 | Rank = 0; |
---|
| 1612 | #endif |
---|
| 1613 | |
---|
| 1614 | // All ranks will do this and have a good time! |
---|
| 1615 | string dn = "gio_crc_errors"; |
---|
| 1616 | mkdir(dn.c_str(), 0777); |
---|
| 1617 | |
---|
| 1618 | srand(time(0)); |
---|
| 1619 | int DumpNum = rand(); |
---|
| 1620 | stringstream ssd; |
---|
| 1621 | ssd << dn << "/gio_crc_error_dump." << Rank << "." << DumpNum << ".bin"; |
---|
| 1622 | |
---|
| 1623 | stringstream ss; |
---|
| 1624 | ss << dn << "/gio_crc_error_log." << Rank << ".txt"; |
---|
| 1625 | |
---|
| 1626 | ofstream ofs(ss.str().c_str(), ofstream::out | ofstream::app); |
---|
| 1627 | ofs << "On-Disk CRC Error Report:\n"; |
---|
| 1628 | ofs << "Variable: " << Vars[i].Name << "\n"; |
---|
| 1629 | ofs << "File: " << OpenFileName << "\n"; |
---|
| 1630 | ofs << "I/O Retries: " << Retry << "\n"; |
---|
| 1631 | ofs << "Size: " << ReadSize << " bytes\n"; |
---|
| 1632 | ofs << "Offset: " << Offset << " bytes\n"; |
---|
| 1633 | ofs << "CRC: " << CRC << " (expected is -1)\n"; |
---|
| 1634 | ofs << "Dump file: " << ssd.str() << "\n"; |
---|
| 1635 | ofs << "\n"; |
---|
| 1636 | ofs.close(); |
---|
| 1637 | |
---|
| 1638 | ofstream dofs(ssd.str().c_str(), ofstream::out); |
---|
| 1639 | dofs.write((const char *) Data, ReadSize); |
---|
| 1640 | dofs.close(); |
---|
[14e73bb] | 1641 | |
---|
| 1642 | uint64_t RawCRC = crc64_omp(Data, ReadSize - CRCSize); |
---|
| 1643 | unsigned char *UData = (unsigned char *) Data; |
---|
| 1644 | crc64_invert(RawCRC, &UData[ReadSize - CRCSize]); |
---|
| 1645 | uint64_t NewCRC = crc64_omp(Data, ReadSize); |
---|
| 1646 | std::cerr << "Recalulated CRC: " << NewCRC << ((NewCRC == -1) ? "ok" : "bad") << "\n"; |
---|
[00587dc] | 1647 | break; |
---|
| 1648 | } |
---|
| 1649 | |
---|
| 1650 | if (HasExtraSpace) |
---|
| 1651 | std::copy(CRCSave, CRCSave + CRCSize, CRCLoc); |
---|
| 1652 | |
---|
| 1653 | if (LData.size()) { |
---|
[a4fee13] | 1654 | CompressHeader<IsBigEndian> *CH = (CompressHeader<IsBigEndian>*) &LData[0]; |
---|
[00587dc] | 1655 | |
---|
| 1656 | #ifdef _OPENMP |
---|
| 1657 | #pragma omp master |
---|
| 1658 | { |
---|
| 1659 | #endif |
---|
| 1660 | |
---|
| 1661 | if (!blosc_initialized) { |
---|
| 1662 | blosc_init(); |
---|
| 1663 | blosc_initialized = true; |
---|
| 1664 | } |
---|
| 1665 | |
---|
[eeacdad] | 1666 | if (!sz_initialized) { |
---|
| 1667 | SZ_Init(NULL); |
---|
| 1668 | sz_initialized = true; |
---|
| 1669 | } |
---|
| 1670 | |
---|
[00587dc] | 1671 | #ifdef _OPENMP |
---|
| 1672 | blosc_set_nthreads(omp_get_max_threads()); |
---|
| 1673 | } |
---|
| 1674 | #endif |
---|
| 1675 | |
---|
[eeacdad] | 1676 | void *OrigData = VarData; |
---|
| 1677 | size_t OrigDataSize = Vars[i].Size*RH->NElems; |
---|
| 1678 | |
---|
| 1679 | if (HasSZ) { |
---|
| 1680 | size_t CNBytes, CCBytes, CBlockSize; |
---|
| 1681 | blosc_cbuffer_sizes(&LData[0] + sizeof(CompressHeader<IsBigEndian>), |
---|
| 1682 | &CNBytes, &CCBytes, &CBlockSize); |
---|
| 1683 | |
---|
| 1684 | OrigData = malloc(CNBytes); |
---|
| 1685 | OrigDataSize = CNBytes; |
---|
| 1686 | } |
---|
| 1687 | |
---|
[a4fee13] | 1688 | blosc_decompress(&LData[0] + sizeof(CompressHeader<IsBigEndian>), |
---|
[eeacdad] | 1689 | OrigData, OrigDataSize); |
---|
[00587dc] | 1690 | |
---|
[eeacdad] | 1691 | if (CH->OrigCRC != crc64_omp(OrigData, OrigDataSize)) { |
---|
[00587dc] | 1692 | ++NErrs[2]; |
---|
| 1693 | break; |
---|
| 1694 | } |
---|
[eeacdad] | 1695 | |
---|
| 1696 | if (HasSZ) { |
---|
| 1697 | int SZDT = GetSZDT(Vars[i]); |
---|
| 1698 | size_t LDSz = SZ_decompress_args(SZDT, (unsigned char *)OrigData, OrigDataSize, |
---|
| 1699 | VarData, 0, 0, 0, 0, RH->NElems); |
---|
| 1700 | free(OrigData); |
---|
| 1701 | |
---|
| 1702 | if (LDSz != Vars[i].Size*RH->NElems) |
---|
| 1703 | throw runtime_error("Variable " + Vars[i].Name + |
---|
| 1704 | ": SZ decompression yielded the wrong amount of data"); |
---|
| 1705 | } |
---|
[00587dc] | 1706 | } |
---|
| 1707 | |
---|
[a4fee13] | 1708 | // Byte swap the data if necessary. |
---|
[eeacdad] | 1709 | if (IsBigEndian != isBigEndian() && !HasSZ) |
---|
[5d57155] | 1710 | for (size_t j = 0; |
---|
| 1711 | j < RH->NElems*(Vars[i].Size/Vars[i].ElementSize); ++j) { |
---|
| 1712 | char *Offset = ((char *) VarData) + j*Vars[i].ElementSize; |
---|
| 1713 | bswap(Offset, Vars[i].ElementSize); |
---|
[a4fee13] | 1714 | } |
---|
| 1715 | |
---|
[00587dc] | 1716 | break; |
---|
| 1717 | } |
---|
| 1718 | |
---|
| 1719 | if (!VarFound) |
---|
| 1720 | throw runtime_error("Variable " + Vars[i].Name + |
---|
| 1721 | " not found in: " + OpenFileName); |
---|
| 1722 | |
---|
| 1723 | // This is for debugging. |
---|
| 1724 | if (NErrs[0] || NErrs[1] || NErrs[2]) { |
---|
| 1725 | const char *EnvStr = getenv("GENERICIO_VERBOSE"); |
---|
| 1726 | if (EnvStr) { |
---|
| 1727 | int Mod = atoi(EnvStr); |
---|
| 1728 | if (Mod > 0) { |
---|
| 1729 | int Rank; |
---|
| 1730 | #ifndef GENERICIO_NO_MPI |
---|
| 1731 | MPI_Comm_rank(MPI_COMM_WORLD, &Rank); |
---|
| 1732 | #else |
---|
| 1733 | Rank = 0; |
---|
| 1734 | #endif |
---|
| 1735 | |
---|
| 1736 | std::cerr << "Rank " << Rank << ": " << NErrs[0] << " I/O error(s), " << |
---|
| 1737 | NErrs[1] << " CRC error(s) and " << NErrs[2] << |
---|
| 1738 | " decompression CRC error(s) reading: " << Vars[i].Name << |
---|
| 1739 | " from: " << OpenFileName << "\n"; |
---|
| 1740 | |
---|
| 1741 | std::cerr.flush(); |
---|
| 1742 | } |
---|
| 1743 | } |
---|
| 1744 | } |
---|
| 1745 | |
---|
| 1746 | if (NErrs[0] || NErrs[1] || NErrs[2]) |
---|
| 1747 | break; |
---|
| 1748 | } |
---|
| 1749 | } |
---|
| 1750 | |
---|
| 1751 | void GenericIO::getVariableInfo(vector<VariableInfo> &VI) { |
---|
[a4fee13] | 1752 | if (FH.isBigEndian()) |
---|
| 1753 | getVariableInfo<true>(VI); |
---|
| 1754 | else |
---|
| 1755 | getVariableInfo<false>(VI); |
---|
| 1756 | } |
---|
| 1757 | |
---|
| 1758 | template <bool IsBigEndian> |
---|
| 1759 | void GenericIO::getVariableInfo(vector<VariableInfo> &VI) { |
---|
[00587dc] | 1760 | assert(FH.getHeaderCache().size() && "HeaderCache must not be empty"); |
---|
| 1761 | |
---|
[a4fee13] | 1762 | GlobalHeader<IsBigEndian> *GH = (GlobalHeader<IsBigEndian> *) &FH.getHeaderCache()[0]; |
---|
[00587dc] | 1763 | for (uint64_t j = 0; j < GH->NVars; ++j) { |
---|
[a4fee13] | 1764 | VariableHeader<IsBigEndian> *VH = (VariableHeader<IsBigEndian> *) &FH.getHeaderCache()[GH->VarsStart + |
---|
[00587dc] | 1765 | j*GH->VarsSize]; |
---|
| 1766 | |
---|
| 1767 | string VName(VH->Name, VH->Name + NameSize); |
---|
| 1768 | size_t VNameNull = VName.find('\0'); |
---|
| 1769 | if (VNameNull < NameSize) |
---|
| 1770 | VName.resize(VNameNull); |
---|
| 1771 | |
---|
[5d57155] | 1772 | size_t ElementSize = VH->Size; |
---|
| 1773 | if (offsetof_safe(VH, ElementSize) < GH->VarsSize) |
---|
| 1774 | ElementSize = VH->ElementSize; |
---|
| 1775 | |
---|
[00587dc] | 1776 | bool IsFloat = (bool) (VH->Flags & FloatValue), |
---|
| 1777 | IsSigned = (bool) (VH->Flags & SignedValue), |
---|
| 1778 | IsPhysCoordX = (bool) (VH->Flags & ValueIsPhysCoordX), |
---|
| 1779 | IsPhysCoordY = (bool) (VH->Flags & ValueIsPhysCoordY), |
---|
| 1780 | IsPhysCoordZ = (bool) (VH->Flags & ValueIsPhysCoordZ), |
---|
| 1781 | MaybePhysGhost = (bool) (VH->Flags & ValueMaybePhysGhost); |
---|
| 1782 | VI.push_back(VariableInfo(VName, (size_t) VH->Size, IsFloat, IsSigned, |
---|
| 1783 | IsPhysCoordX, IsPhysCoordY, IsPhysCoordZ, |
---|
[5d57155] | 1784 | MaybePhysGhost, ElementSize)); |
---|
[00587dc] | 1785 | } |
---|
| 1786 | } |
---|
| 1787 | |
---|
| 1788 | void GenericIO::setNaturalDefaultPartition() { |
---|
| 1789 | #ifdef __bgq__ |
---|
[14e73bb] | 1790 | DefaultPartition = MPIX_IO_link_id(); |
---|
[00587dc] | 1791 | #else |
---|
| 1792 | #ifndef GENERICIO_NO_MPI |
---|
| 1793 | bool UseName = true; |
---|
| 1794 | const char *EnvStr = getenv("GENERICIO_PARTITIONS_USE_NAME"); |
---|
| 1795 | if (EnvStr) { |
---|
| 1796 | int Mod = atoi(EnvStr); |
---|
| 1797 | UseName = (Mod != 0); |
---|
| 1798 | } |
---|
| 1799 | |
---|
| 1800 | if (UseName) { |
---|
| 1801 | // This is a heuristic to generate ~256 partitions based on the |
---|
| 1802 | // names of the nodes. |
---|
| 1803 | char Name[MPI_MAX_PROCESSOR_NAME]; |
---|
| 1804 | int Len = 0; |
---|
| 1805 | |
---|
| 1806 | MPI_Get_processor_name(Name, &Len); |
---|
| 1807 | unsigned char color = 0; |
---|
| 1808 | for (int i = 0; i < Len; ++i) |
---|
| 1809 | color += (unsigned char) Name[i]; |
---|
| 1810 | |
---|
| 1811 | DefaultPartition = color; |
---|
| 1812 | } |
---|
| 1813 | |
---|
| 1814 | // This is for debugging. |
---|
| 1815 | EnvStr = getenv("GENERICIO_RANK_PARTITIONS"); |
---|
| 1816 | if (EnvStr) { |
---|
| 1817 | int Mod = atoi(EnvStr); |
---|
| 1818 | if (Mod > 0) { |
---|
| 1819 | int Rank; |
---|
| 1820 | MPI_Comm_rank(MPI_COMM_WORLD, &Rank); |
---|
| 1821 | DefaultPartition += Rank % Mod; |
---|
| 1822 | } |
---|
| 1823 | } |
---|
| 1824 | #endif |
---|
| 1825 | #endif |
---|
| 1826 | } |
---|
| 1827 | |
---|
| 1828 | } /* END namespace cosmotk */ |
---|