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