source: GenericIO.cxx @ eeacdad

Revision eeacdad, 55.8 KB checked in by Hal Finkel <hfinkel@…>, 6 years ago (diff)

initial SZ integration - it compiles

  • Property mode set to 100644
Line 
1/*
2 *                    Copyright (C) 2015, UChicago Argonne, LLC
3 *                               All Rights Reserved
4 *
5 *                               Generic IO (ANL-15-066)
6 *                     Hal Finkel, Argonne National Laboratory
7 *
8 *                              OPEN SOURCE LICENSE
9 *
10 * Under the terms of Contract No. DE-AC02-06CH11357 with UChicago Argonne,
11 * LLC, the U.S. Government retains certain rights in this software.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are met:
15 *
16 *   1. Redistributions of source code must retain the above copyright notice,
17 *      this list of conditions and the following disclaimer.
18 *
19 *   2. Redistributions in binary form must reproduce the above copyright
20 *      notice, this list of conditions and the following disclaimer in the
21 *      documentation and/or other materials provided with the distribution.
22 *
23 *   3. Neither the names of UChicago Argonne, LLC or the Department of Energy
24 *      nor the names of its contributors may be used to endorse or promote
25 *      products derived from this software without specific prior written
26 *      permission.
27 *
28 * *****************************************************************************
29 *
30 *                                  DISCLAIMER
31 * THE SOFTWARE IS SUPPLIED “AS IS” WITHOUT WARRANTY OF ANY KIND.  NEITHER THE
32 * UNTED STATES GOVERNMENT, NOR THE UNITED STATES DEPARTMENT OF ENERGY, NOR
33 * UCHICAGO ARGONNE, LLC, NOR ANY OF THEIR EMPLOYEES, MAKES ANY WARRANTY,
34 * EXPRESS OR IMPLIED, OR ASSUMES ANY LEGAL LIABILITY OR RESPONSIBILITY FOR THE
35 * ACCURACY, COMPLETENESS, OR USEFULNESS OF ANY INFORMATION, DATA, APPARATUS,
36 * PRODUCT, OR PROCESS DISCLOSED, OR REPRESENTS THAT ITS USE WOULD NOT INFRINGE
37 * PRIVATELY OWNED RIGHTS.
38 *
39 * *****************************************************************************
40 */
41
42#define _XOPEN_SOURCE 600
43#include "CRC64.h"
44#include "GenericIO.h"
45
46extern "C" {
47#include "blosc.h"
48}
49#include "sz.h"
50
51#include <sstream>
52#include <fstream>
53#include <stdexcept>
54#include <iterator>
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__
70#include <mpix.h>
71#endif
72
73#ifndef MPI_UINT64_T
74#define MPI_UINT64_T (sizeof(long) == 8 ? MPI_LONG : MPI_LONG_LONG)
75#endif
76
77using namespace std;
78
79namespace gio {
80
81
82#ifndef GENERICIO_NO_MPI
83GenericFileIO_MPI::~GenericFileIO_MPI() {
84  (void) MPI_File_close(&FH);
85}
86
87void 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
98void 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
103void 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
119void 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
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);
130
131    count -= scount;
132    buf = ((char *) buf) + scount;
133    offset += scount;
134  }
135}
136
137void 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
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);
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
160void 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
182GenericFileIO_POSIX::~GenericFileIO_POSIX() {
183  if (FH != -1) close(FH);
184}
185
186void 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
198void 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
203void 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
212      throw runtime_error("Unable to read " + D + " from file: " + FileName +
213                          ": " + strerror(errno));
214    }
215
216    count -= scount;
217    buf = ((char *) buf) + scount;
218    offset += scount;
219  }
220}
221
222void 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
231      throw runtime_error("Unable to write " + D + " to file: " + FileName +
232                          ": " + strerror(errno));
233    }
234
235    count -= scount;
236    buf = ((char *) buf) + scount;
237    offset += scount;
238  }
239}
240
241static bool isBigEndian() {
242  const uint32_t one = 1;
243  return !(*((char *)(&one)));
244}
245
246static 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
257template <typename T, bool IsBigEndian>
258struct 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
285private:
286  T value;
287};
288
289static const size_t CRCSize = 8;
290
291static const size_t MagicSize = 8;
292static const char *MagicBE = "HACC01B";
293static const char *MagicLE = "HACC01L";
294
295template <bool IsBigEndian>
296struct GlobalHeader {
297  char Magic[MagicSize];
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};
313
314enum {
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
323static const size_t NameSize = 256;
324template <bool IsBigEndian>
325struct VariableHeader {
326  char Name[NameSize];
327  endian_specific_value<uint64_t, IsBigEndian> Flags;
328  endian_specific_value<uint64_t, IsBigEndian> Size;
329  endian_specific_value<uint64_t, IsBigEndian> ElementSize;
330};
331
332template <bool IsBigEndian>
333struct RankHeader {
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};
339
340static const size_t FilterNameSize = 8;
341static const size_t MaxFilters = 4;
342template <bool IsBigEndian>
343struct BlockHeader {
344  char Filters[MaxFilters][FilterNameSize];
345  endian_specific_value<uint64_t, IsBigEndian> Start;
346  endian_specific_value<uint64_t, IsBigEndian> Size;
347};
348
349template <bool IsBigEndian>
350struct CompressHeader {
351  endian_specific_value<uint64_t, IsBigEndian> OrigCRC;
352};
353const char *CompressName = "BLOSC";
354
355const char *LossyCompressName = "SZ";
356
357#pragma pack()
358
359unsigned GenericIO::DefaultFileIOType = FileIOPOSIX;
360int GenericIO::DefaultPartition = 0;
361bool GenericIO::DefaultShouldCompress = false;
362
363#ifndef GENERICIO_NO_MPI
364std::size_t GenericIO::CollectiveMPIIOThreshold = 0;
365#endif
366
367static bool blosc_initialized = false;
368static bool sz_initialized = false;
369
370static 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
395#ifndef GENERICIO_NO_MPI
396void GenericIO::write() {
397  if (isBigEndian())
398    write<true>();
399  else
400    write<false>();
401}
402
403// Note: writing errors are not currently recoverable (one rank may fail
404// while the others don't).
405template <bool IsBigEndian>
406void GenericIO::write() {
407  const char *Magic = IsBigEndian ? MagicBE : MagicLE;
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
478  RankHeader<IsBigEndian> RHLocal;
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
512  vector<BlockHeader<IsBigEndian> > LocalBlockHeaders;
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).
526      memset(&LocalBlockHeaders[i], 0, sizeof(BlockHeader<IsBigEndian>));
527      if (ShouldCompress) {
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        }
576nosz:
577
578        LocalCData[i].resize(sizeof(CompressHeader<IsBigEndian>));
579
580        CompressHeader<IsBigEndian> *CH = (CompressHeader<IsBigEndian>*) &LocalCData[i][0];
581        CH->OrigCRC = crc64_omp(OrigData, OrigDataSize);
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
593       if (!sz_initialized) {
594         SZ_Init(NULL);
595         sz_initialized = true;
596       }
597
598#ifdef _OPENMP
599       blosc_set_nthreads(omp_get_max_threads());
600  }
601#endif
602
603        LocalCData[i].resize(LocalCData[i].size() + OrigDataSize);
604        if (blosc_compress(9, 1, OrigUnitSize, OrigDataSize, OrigData,
605                           &LocalCData[i][0] + sizeof(CompressHeader<IsBigEndian>),
606                           OrigDataSize) <= 0) {
607          if (FreeOrigData)
608            free(OrigData);
609
610          goto nocomp;
611        }
612
613        if (FreeOrigData)
614          free(OrigData);
615
616        strncpy(LocalBlockHeaders[i].Filters[FilterIdx++], CompressName, FilterNameSize);
617        size_t CNBytes, CCBytes, CBlockSize;
618        blosc_cbuffer_sizes(&LocalCData[i][0] + sizeof(CompressHeader<IsBigEndian>),
619                            &CNBytes, &CCBytes, &CBlockSize);
620        LocalCData[i].resize(CCBytes + sizeof(CompressHeader<IsBigEndian>));
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 {
627nocomp:
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) {
638    uint64_t HeaderSize = sizeof(GlobalHeader<IsBigEndian>) + Vars.size()*sizeof(VariableHeader<IsBigEndian>) +
639                          SplitNRanks*sizeof(RankHeader<IsBigEndian>) + CRCSize;
640    if (NeedsBlockHeaders)
641      HeaderSize += SplitNRanks*Vars.size()*sizeof(BlockHeader<IsBigEndian>);
642
643    vector<char> Header(HeaderSize, 0);
644    GlobalHeader<IsBigEndian> *GH = (GlobalHeader<IsBigEndian> *) &Header[0];
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();
650    GH->VarsSize = sizeof(VariableHeader<IsBigEndian>);
651    GH->VarsStart = sizeof(GlobalHeader<IsBigEndian>);
652    GH->NRanks = SplitNRanks;
653    GH->RanksSize = sizeof(RankHeader<IsBigEndian>);
654    GH->RanksStart = GH->VarsStart + Vars.size()*sizeof(VariableHeader<IsBigEndian>);
655    GH->GlobalHeaderSize = sizeof(GlobalHeader<IsBigEndian>);
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 {
661      GH->BlocksSize = sizeof(BlockHeader<IsBigEndian>);
662      GH->BlocksStart = GH->RanksStart + SplitNRanks*sizeof(RankHeader<IsBigEndian>);
663    }
664
665    uint64_t RecordSize = 0;
666    VariableHeader<IsBigEndian> *VH = (VariableHeader<IsBigEndian> *) &Header[GH->VarsStart];
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);
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;
680      RecordSize += VH->Size = Vars[i].Size;
681      VH->ElementSize = Vars[i].ElementSize;
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],
690                 Vars.size()*sizeof(BlockHeader<IsBigEndian>), MPI_BYTE,
691                 &Header[GH->BlocksStart],
692                 Vars.size()*sizeof(BlockHeader<IsBigEndian>), MPI_BYTE,
693                 0, SplitComm);
694
695      BlockHeader<IsBigEndian> *BH = (BlockHeader<IsBigEndian> *) &Header[GH->BlocksStart];
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
704      RankHeader<IsBigEndian> *RH = (RankHeader<IsBigEndian> *) &Header[GH->RanksStart];
705      RH->Start = HeaderSize; ++RH;
706      for (int i = 1; i < SplitNRanks; ++i, ++RH) {
707        RH->Start =
708          ((BlockHeader<IsBigEndian> *) &Header[GH->BlocksStart])[i*Vars.size()].Start;
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 {
716      RankHeader<IsBigEndian> *RH = (RankHeader<IsBigEndian> *) &Header[GH->RanksStart];
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],
738                  sizeof(BlockHeader<IsBigEndian>)*Vars.size(), MPI_BYTE,
739                  &LocalBlockHeaders[0],
740                  sizeof(BlockHeader<IsBigEndian>)*Vars.size(), MPI_BYTE,
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)
761      MPI_Gather(&LocalBlockHeaders[0], Vars.size()*sizeof(BlockHeader<IsBigEndian>),
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)
765      MPI_Scatter(0, 0, MPI_BYTE, &LocalBlockHeaders[0], sizeof(BlockHeader<IsBigEndian>)*Vars.size(),
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.);
828    std::cout << "Wrote " << Vars.size() << " variables to " << FileName <<
829                  " (" << FileSize << " bytes) in " << MaxTotalTime << "s: " <<
830                  Rate << " MB/s" << std::endl;
831  }
832
833  MPI_Comm_free(&SplitComm);
834  SplitComm = MPI_COMM_NULL;
835}
836#endif // GENERICIO_NO_MPI
837
838template <bool IsBigEndian>
839void GenericIO::readHeaderLeader(void *GHPtr, MismatchBehavior MB, int NRanks,
840                                 int Rank, int SplitNRanks,
841                                 string &LocalFileName, uint64_t &HeaderSize,
842                                 vector<char> &Header) {
843  GlobalHeader<IsBigEndian> &GH = *(GlobalHeader<IsBigEndian> *) GHPtr;
844
845  if (MB == MismatchDisallowed) {
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
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    }
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
921// Note: Errors from this function should be recoverable. This means that if
922// one rank throws an exception, then all ranks should.
923void GenericIO::openAndReadHeader(MismatchBehavior MB, int EffRank, bool CheckPartMap) {
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)
934    EffRank = MB == MismatchRedistribute ? 0 : Rank;
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
946        GIO.openAndReadHeader(MismatchDisallowed, 0, false);
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
978    MPI_Comm_dup(MB == MismatchRedistribute ? MPI_COMM_SELF : Comm, &SplitComm);
979#endif
980  } else {
981    stringstream ss;
982    ss << FileName << "#" << RankMap[EffRank];
983    LocalFileName = ss.str();
984#ifndef GENERICIO_NO_MPI
985    if (MB == MismatchRedistribute) {
986      MPI_Comm_dup(MPI_COMM_SELF, &SplitComm);
987    } else {
988#ifdef __bgq__
989      MPI_Barrier(Comm);
990#endif
991      MPI_Comm_split(Comm, RankMap[EffRank], Rank, &SplitComm);
992    }
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
1029      GlobalHeader<false> GH; // endianness does not matter yet...
1030      FH.get()->read(&GH, sizeof(GlobalHeader<false>), 0, "global header");
1031
1032      if (string(GH.Magic, GH.Magic + MagicSize - 1) == MagicLE) {
1033        readHeaderLeader<false>(&GH, MB, NRanks, Rank, SplitNRanks, LocalFileName,
1034                                HeaderSize, Header);
1035      } else if (string(GH.Magic, GH.Magic + MagicSize - 1) == MagicBE) {
1036        readHeaderLeader<true>(&GH, MB, NRanks, Rank, SplitNRanks, LocalFileName,
1037                               HeaderSize, Header);
1038      } else {
1039        string Error = "invalid file-type identifier";
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();
1073
1074  GlobalHeader<false> *GH = (GlobalHeader<false> *) &Header[0];
1075  FH.setIsBigEndian(string(GH->Magic, GH->Magic + MagicSize - 1) == MagicBE);
1076
1077  FH.getHeaderCache().swap(Header);
1078  OpenFileName = LocalFileName;
1079
1080#ifndef GENERICIO_NO_MPI
1081  if (!DisableCollErrChecking)
1082    MPI_Barrier(Comm);
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);
1094    MPI_Allreduce(&OpenErr, &TotOpenErr, 1, MPI_INT, MPI_SUM,
1095                  DisableCollErrChecking ? MPI_COMM_SELF : Comm);
1096  } catch (...) {
1097    OpenErr = 1;
1098    MPI_Allreduce(&OpenErr, &TotOpenErr, 1, MPI_INT, MPI_SUM,
1099                  DisableCollErrChecking ? MPI_COMM_SELF : Comm);
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
1111int GenericIO::readNRanks() {
1112  if (FH.isBigEndian())
1113    return readNRanks<true>();
1114  return readNRanks<false>();
1115}
1116
1117template <bool IsBigEndian>
1118int GenericIO::readNRanks() {
1119  if (RankMap.size())
1120    return RankMap.size();
1121
1122  assert(FH.getHeaderCache().size() && "HeaderCache must not be empty");
1123  GlobalHeader<IsBigEndian> *GH = (GlobalHeader<IsBigEndian> *) &FH.getHeaderCache()[0];
1124  return (int) GH->NRanks;
1125}
1126
1127void GenericIO::readDims(int Dims[3]) {
1128  if (FH.isBigEndian())
1129    readDims<true>(Dims);
1130  else
1131    readDims<false>(Dims);
1132}
1133
1134template <bool IsBigEndian>
1135void GenericIO::readDims(int Dims[3]) {
1136  assert(FH.getHeaderCache().size() && "HeaderCache must not be empty");
1137  GlobalHeader<IsBigEndian> *GH = (GlobalHeader<IsBigEndian> *) &FH.getHeaderCache()[0];
1138  std::copy(GH->Dims, GH->Dims + 3, Dims);
1139}
1140
1141uint64_t GenericIO::readTotalNumElems() {
1142  if (FH.isBigEndian())
1143    return readTotalNumElems<true>();
1144  return readTotalNumElems<false>();
1145}
1146
1147template <bool IsBigEndian>
1148uint64_t GenericIO::readTotalNumElems() {
1149  if (RankMap.size())
1150    return (uint64_t) -1;
1151
1152  assert(FH.getHeaderCache().size() && "HeaderCache must not be empty");
1153  GlobalHeader<IsBigEndian> *GH = (GlobalHeader<IsBigEndian> *) &FH.getHeaderCache()[0];
1154  return GH->NElems;
1155}
1156
1157void GenericIO::readPhysOrigin(double Origin[3]) {
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
1168template <bool IsBigEndian>
1169void GenericIO::readPhysOrigin(double Origin[3]) {
1170  assert(FH.getHeaderCache().size() && "HeaderCache must not be empty");
1171  GlobalHeader<IsBigEndian> *GH = (GlobalHeader<IsBigEndian> *) &FH.getHeaderCache()[0];
1172  if (offsetof_safe(GH, PhysOrigin) >= GH->GlobalHeaderSize) {
1173    std::fill(Origin, Origin + 3, 0.0);
1174    return;
1175  }
1176
1177  std::copy(GH->PhysOrigin, GH->PhysOrigin + 3, Origin);
1178}
1179
1180void GenericIO::readPhysScale(double Scale[3]) {
1181  if (FH.isBigEndian())
1182    readPhysScale<true>(Scale);
1183  else
1184    readPhysScale<false>(Scale);
1185}
1186
1187template <bool IsBigEndian>
1188void GenericIO::readPhysScale(double Scale[3]) {
1189  assert(FH.getHeaderCache().size() && "HeaderCache must not be empty");
1190  GlobalHeader<IsBigEndian> *GH = (GlobalHeader<IsBigEndian> *) &FH.getHeaderCache()[0];
1191  if (offsetof_safe(GH, PhysScale) >= GH->GlobalHeaderSize) {
1192    std::fill(Scale, Scale + 3, 0.0);
1193    return;
1194  }
1195
1196  std::copy(GH->PhysScale, GH->PhysScale + 3, Scale);
1197}
1198
1199template <bool IsBigEndian>
1200static size_t getRankIndex(int EffRank, GlobalHeader<IsBigEndian> *GH,
1201                           vector<int> &RankMap, vector<char> &HeaderCache) {
1202  if (RankMap.empty())
1203    return EffRank;
1204
1205  for (size_t i = 0; i < GH->NRanks; ++i) {
1206    RankHeader<IsBigEndian> *RH = (RankHeader<IsBigEndian> *) &HeaderCache[GH->RanksStart +
1207                                                 i*GH->RanksSize];
1208    if (offsetof_safe(RH, GlobalRank) >= GH->RanksSize)
1209      return EffRank;
1210
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
1219int GenericIO::readGlobalRankNumber(int EffRank) {
1220  if (FH.isBigEndian())
1221    return readGlobalRankNumber<true>(EffRank);
1222  return readGlobalRankNumber<false>(EffRank);
1223}
1224
1225template <bool IsBigEndian>
1226int GenericIO::readGlobalRankNumber(int EffRank) {
1227  if (EffRank == -1) {
1228#ifndef GENERICIO_NO_MPI
1229    MPI_Comm_rank(Comm, &EffRank);
1230#else
1231    EffRank = 0;
1232#endif
1233  }
1234
1235  openAndReadHeader(MismatchAllowed, EffRank, false);
1236
1237  assert(FH.getHeaderCache().size() && "HeaderCache must not be empty");
1238
1239  GlobalHeader<IsBigEndian> *GH = (GlobalHeader<IsBigEndian> *) &FH.getHeaderCache()[0];
1240  size_t RankIndex = getRankIndex<IsBigEndian>(EffRank, GH, RankMap, FH.getHeaderCache());
1241
1242  assert(RankIndex < GH->NRanks && "Invalid rank specified");
1243
1244  RankHeader<IsBigEndian> *RH = (RankHeader<IsBigEndian> *) &FH.getHeaderCache()[GH->RanksStart +
1245                                               RankIndex*GH->RanksSize];
1246
1247  if (offsetof_safe(RH, GlobalRank) >= GH->RanksSize)
1248    return EffRank;
1249
1250  return (int) RH->GlobalRank;
1251}
1252
1253void 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
1271size_t GenericIO::readNumElems(int EffRank) {
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
1283  if (FH.isBigEndian())
1284    return readNumElems<true>(EffRank);
1285  return readNumElems<false>(EffRank);
1286}
1287
1288template <bool IsBigEndian>
1289size_t GenericIO::readNumElems(int EffRank) {
1290  if (EffRank == -1) {
1291#ifndef GENERICIO_NO_MPI
1292    MPI_Comm_rank(Comm, &EffRank);
1293#else
1294    EffRank = 0;
1295#endif
1296  }
1297
1298  openAndReadHeader(Redistributing ? MismatchRedistribute : MismatchAllowed,
1299                    EffRank, false);
1300
1301  assert(FH.getHeaderCache().size() && "HeaderCache must not be empty");
1302
1303  GlobalHeader<IsBigEndian> *GH = (GlobalHeader<IsBigEndian> *) &FH.getHeaderCache()[0];
1304  size_t RankIndex = getRankIndex<IsBigEndian>(EffRank, GH, RankMap, FH.getHeaderCache());
1305
1306  assert(RankIndex < GH->NRanks && "Invalid rank specified");
1307
1308  RankHeader<IsBigEndian> *RH = (RankHeader<IsBigEndian> *) &FH.getHeaderCache()[GH->RanksStart +
1309                                               RankIndex*GH->RanksSize];
1310  return (size_t) RH->NElems;
1311}
1312
1313void GenericIO::readCoords(int Coords[3], int EffRank) {
1314  if (EffRank == -1 && Redistributing) {
1315    std::fill(Coords, Coords + 3, 0);
1316    return;
1317  }
1318
1319  if (FH.isBigEndian())
1320    readCoords<true>(Coords, EffRank);
1321  else
1322    readCoords<false>(Coords, EffRank);
1323}
1324
1325template <bool IsBigEndian>
1326void GenericIO::readCoords(int Coords[3], int EffRank) {
1327  if (EffRank == -1) {
1328#ifndef GENERICIO_NO_MPI
1329    MPI_Comm_rank(Comm, &EffRank);
1330#else
1331    EffRank = 0;
1332#endif
1333  }
1334
1335  openAndReadHeader(MismatchAllowed, EffRank, false);
1336
1337  assert(FH.getHeaderCache().size() && "HeaderCache must not be empty");
1338
1339  GlobalHeader<IsBigEndian> *GH = (GlobalHeader<IsBigEndian> *) &FH.getHeaderCache()[0];
1340  size_t RankIndex = getRankIndex<IsBigEndian>(EffRank, GH, RankMap, FH.getHeaderCache());
1341
1342  assert(RankIndex < GH->NRanks && "Invalid rank specified");
1343
1344  RankHeader<IsBigEndian> *RH = (RankHeader<IsBigEndian> *) &FH.getHeaderCache()[GH->RanksStart +
1345                                               RankIndex*GH->RanksSize];
1346
1347  std::copy(RH->Coords, RH->Coords + 3, Coords);
1348}
1349
1350void GenericIO::readData(int EffRank, bool PrintStats, bool CollStats) {
1351  int Rank;
1352#ifndef GENERICIO_NO_MPI
1353  MPI_Comm_rank(Comm, &Rank);
1354#else
1355  Rank = 0;
1356#endif
1357
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.);
1425    std::cout << "Read " << Vars.size() << " variables from " << FileName <<
1426                 " (" << AllTotalReadSize << " bytes) in " << MaxTotalTime << "s: " <<
1427                 Rate << " MB/s [excluding header read]" << std::endl;
1428  }
1429}
1430
1431void 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.
1441template <bool IsBigEndian>
1442void GenericIO::readData(int EffRank, size_t RowOffset, int Rank,
1443                         uint64_t &TotalReadSize, int NErrs[3]) {
1444  openAndReadHeader(Redistributing ? MismatchRedistribute : MismatchAllowed,
1445                    EffRank, false);
1446
1447  assert(FH.getHeaderCache().size() && "HeaderCache must not be empty");
1448
1449  if (EffRank == -1)
1450    EffRank = Rank;
1451
1452  GlobalHeader<IsBigEndian> *GH = (GlobalHeader<IsBigEndian> *) &FH.getHeaderCache()[0];
1453  size_t RankIndex = getRankIndex<IsBigEndian>(EffRank, GH, RankMap, FH.getHeaderCache());
1454
1455  assert(RankIndex < GH->NRanks && "Invalid rank specified");
1456
1457  RankHeader<IsBigEndian> *RH = (RankHeader<IsBigEndian> *) &FH.getHeaderCache()[GH->RanksStart +
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) {
1464      VariableHeader<IsBigEndian> *VH = (VariableHeader<IsBigEndian> *) &FH.getHeaderCache()[GH->VarsStart +
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
1478      size_t ElementSize = VH->Size;
1479      if (offsetof_safe(VH, ElementSize) < GH->VarsSize)
1480        ElementSize = VH->ElementSize;
1481
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());
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());
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
1515      size_t VarOffset = RowOffset*Vars[i].Size;
1516      void *VarData = ((char *) Vars[i].Data) + VarOffset;
1517
1518      vector<unsigned char> LData;
1519      bool HasSZ = false;
1520      void *Data = VarData;
1521      bool HasExtraSpace = Vars[i].HasExtraSpace;
1522      if (offsetof_safe(GH, BlocksStart) < GH->GlobalHeaderSize &&
1523          GH->BlocksSize > 0) {
1524        BlockHeader<IsBigEndian> *BH = (BlockHeader<IsBigEndian> *)
1525          &FH.getHeaderCache()[GH->BlocksStart +
1526                               (RankIndex*GH->NVars + j)*GH->BlocksSize];
1527        ReadSize = BH->Size + CRCSize;
1528        Offset = BH->Start;
1529
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) {
1538          LData.resize(ReadSize);
1539          Data = &LData[0];
1540          HasExtraSpace = true;
1541        } else if (BH->Filters[FilterIdx][0] != '\0') {
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();
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";
1647        break;
1648      }
1649
1650      if (HasExtraSpace)
1651        std::copy(CRCSave, CRCSave + CRCSize, CRCLoc);
1652
1653      if (LData.size()) {
1654        CompressHeader<IsBigEndian> *CH = (CompressHeader<IsBigEndian>*) &LData[0];
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
1666       if (!sz_initialized) {
1667         SZ_Init(NULL);
1668         sz_initialized = true;
1669       }
1670
1671#ifdef _OPENMP
1672       blosc_set_nthreads(omp_get_max_threads());
1673  }
1674#endif
1675
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
1688        blosc_decompress(&LData[0] + sizeof(CompressHeader<IsBigEndian>),
1689                         OrigData, OrigDataSize);
1690
1691        if (CH->OrigCRC != crc64_omp(OrigData, OrigDataSize)) {
1692          ++NErrs[2];
1693          break;
1694        }
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        }
1706      }
1707
1708      // Byte swap the data if necessary.
1709      if (IsBigEndian != isBigEndian() && !HasSZ)
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);
1714        }
1715
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
1751void GenericIO::getVariableInfo(vector<VariableInfo> &VI) {
1752  if (FH.isBigEndian())
1753    getVariableInfo<true>(VI);
1754  else
1755    getVariableInfo<false>(VI);
1756}
1757
1758template <bool IsBigEndian>
1759void GenericIO::getVariableInfo(vector<VariableInfo> &VI) {
1760  assert(FH.getHeaderCache().size() && "HeaderCache must not be empty");
1761
1762  GlobalHeader<IsBigEndian> *GH = (GlobalHeader<IsBigEndian> *) &FH.getHeaderCache()[0];
1763  for (uint64_t j = 0; j < GH->NVars; ++j) {
1764    VariableHeader<IsBigEndian> *VH = (VariableHeader<IsBigEndian> *) &FH.getHeaderCache()[GH->VarsStart +
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
1772    size_t ElementSize = VH->Size;
1773    if (offsetof_safe(VH, ElementSize) < GH->VarsSize)
1774      ElementSize = VH->ElementSize;
1775
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,
1784                              MaybePhysGhost, ElementSize));
1785  }
1786}
1787
1788void GenericIO::setNaturalDefaultPartition() {
1789#ifdef __bgq__
1790  DefaultPartition = MPIX_IO_link_id();
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 */
Note: See TracBrowser for help on using the repository browser.