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 | #ifndef GENERICIO_H |
---|
43 | #define GENERICIO_H |
---|
44 | |
---|
45 | #include <cstdlib> |
---|
46 | #include <vector> |
---|
47 | #include <string> |
---|
48 | #include <iostream> |
---|
49 | #include <limits> |
---|
50 | #include <stdint.h> |
---|
51 | |
---|
52 | #ifndef GENERICIO_NO_MPI |
---|
53 | #include <mpi.h> |
---|
54 | #else |
---|
55 | #include <fstream> |
---|
56 | #endif |
---|
57 | |
---|
58 | #include <unistd.h> |
---|
59 | |
---|
60 | namespace gio { |
---|
61 | |
---|
62 | class GenericFileIO { |
---|
63 | public: |
---|
64 | virtual ~GenericFileIO() {} |
---|
65 | |
---|
66 | public: |
---|
67 | virtual void open(const std::string &FN, bool ForReading = false) = 0; |
---|
68 | virtual void setSize(size_t sz) = 0; |
---|
69 | virtual void read(void *buf, size_t count, off_t offset, |
---|
70 | const std::string &D) = 0; |
---|
71 | virtual void write(const void *buf, size_t count, off_t offset, |
---|
72 | const std::string &D) = 0; |
---|
73 | |
---|
74 | protected: |
---|
75 | std::string FileName; |
---|
76 | }; |
---|
77 | |
---|
78 | #ifndef GENERICIO_NO_MPI |
---|
79 | class GenericFileIO_MPI : public GenericFileIO { |
---|
80 | public: |
---|
81 | GenericFileIO_MPI(const MPI_Comm &C) : FH(MPI_FILE_NULL), Comm(C) {} |
---|
82 | virtual ~GenericFileIO_MPI(); |
---|
83 | |
---|
84 | public: |
---|
85 | virtual void open(const std::string &FN, bool ForReading = false); |
---|
86 | virtual void setSize(size_t sz); |
---|
87 | virtual void read(void *buf, size_t count, off_t offset, const std::string &D); |
---|
88 | virtual void write(const void *buf, size_t count, off_t offset, const std::string &D); |
---|
89 | |
---|
90 | protected: |
---|
91 | MPI_File FH; |
---|
92 | MPI_Comm Comm; |
---|
93 | }; |
---|
94 | |
---|
95 | class GenericFileIO_MPICollective : public GenericFileIO_MPI { |
---|
96 | public: |
---|
97 | GenericFileIO_MPICollective(const MPI_Comm &C) : GenericFileIO_MPI(C) {} |
---|
98 | |
---|
99 | public: |
---|
100 | void read(void *buf, size_t count, off_t offset, const std::string &D); |
---|
101 | void write(const void *buf, size_t count, off_t offset, const std::string &D); |
---|
102 | }; |
---|
103 | #endif |
---|
104 | |
---|
105 | class GenericFileIO_POSIX : public GenericFileIO { |
---|
106 | public: |
---|
107 | GenericFileIO_POSIX() : FH(-1) {} |
---|
108 | ~GenericFileIO_POSIX(); |
---|
109 | |
---|
110 | public: |
---|
111 | void open(const std::string &FN, bool ForReading = false); |
---|
112 | void setSize(size_t sz); |
---|
113 | void read(void *buf, size_t count, off_t offset, const std::string &D); |
---|
114 | void write(const void *buf, size_t count, off_t offset, const std::string &D); |
---|
115 | |
---|
116 | protected: |
---|
117 | int FH; |
---|
118 | }; |
---|
119 | |
---|
120 | namespace detail { |
---|
121 | // A standard enable_if idiom (we include our own here for pre-C++11 support). |
---|
122 | template <bool B, typename T = void> |
---|
123 | struct enable_if {}; |
---|
124 | |
---|
125 | template <typename T> |
---|
126 | struct enable_if<true, T> { typedef T type; }; |
---|
127 | |
---|
128 | // A SFINAE-based trait to detect whether a type has a member named x. This is |
---|
129 | // designed to work both with structs/classes and also with OpenCL-style vector |
---|
130 | // types. |
---|
131 | template <typename T> |
---|
132 | class has_x { |
---|
133 | typedef char yes[1]; |
---|
134 | typedef char no[2]; |
---|
135 | |
---|
136 | template <typename C> |
---|
137 | static yes &test(char(*)[sizeof((*((C *) 0)).x)]); |
---|
138 | |
---|
139 | template <typename C> |
---|
140 | static no &test(...); |
---|
141 | |
---|
142 | public: |
---|
143 | enum { value = sizeof(test<T>(0)) == sizeof(yes) }; |
---|
144 | }; |
---|
145 | |
---|
146 | // A SFINAE-based trait to detect whether a type is array-like (i.e. supports |
---|
147 | // the [] operator). |
---|
148 | template <typename T> |
---|
149 | class is_array { |
---|
150 | typedef char yes[1]; |
---|
151 | typedef char no[2]; |
---|
152 | |
---|
153 | template <typename C> |
---|
154 | static yes &test(char(*)[sizeof((*((C *) 0))[0])]); |
---|
155 | |
---|
156 | template <typename C> |
---|
157 | static no &test(...); |
---|
158 | |
---|
159 | public: |
---|
160 | enum { value = sizeof(test<T>(0)) == sizeof(yes) }; |
---|
161 | }; |
---|
162 | } // namespace detail |
---|
163 | |
---|
164 | class GenericIO { |
---|
165 | public: |
---|
166 | enum VariableFlags { |
---|
167 | VarHasExtraSpace = (1 << 0), // Note that this flag indicates that the |
---|
168 | // extra space is available, but the GenericIO |
---|
169 | // implementation is required to |
---|
170 | // preserve its contents. |
---|
171 | VarIsPhysCoordX = (1 << 1), |
---|
172 | VarIsPhysCoordY = (1 << 2), |
---|
173 | VarIsPhysCoordZ = (1 << 3), |
---|
174 | VarMaybePhysGhost = (1 << 4) |
---|
175 | }; |
---|
176 | |
---|
177 | struct VariableInfo { |
---|
178 | VariableInfo(const std::string &N, std::size_t S, bool IF, bool IS, |
---|
179 | bool PCX, bool PCY, bool PCZ, bool PG, std::size_t ES = 0) |
---|
180 | : Name(N), Size(S), IsFloat(IF), IsSigned(IS), |
---|
181 | IsPhysCoordX(PCX), IsPhysCoordY(PCY), IsPhysCoordZ(PCZ), |
---|
182 | MaybePhysGhost(PG), ElementSize(ES ? ES : S) {} |
---|
183 | |
---|
184 | std::string Name; |
---|
185 | std::size_t Size; |
---|
186 | bool IsFloat; |
---|
187 | bool IsSigned; |
---|
188 | bool IsPhysCoordX, IsPhysCoordY, IsPhysCoordZ; |
---|
189 | bool MaybePhysGhost; |
---|
190 | std::size_t ElementSize; |
---|
191 | }; |
---|
192 | |
---|
193 | public: |
---|
194 | struct LossyCompressionInfo { |
---|
195 | enum LCMode { |
---|
196 | LCModeNone, |
---|
197 | LCModeAbs, |
---|
198 | LCModeRel, |
---|
199 | LCModeAbsAndRel, |
---|
200 | LCModeAbsOrRel, |
---|
201 | LCModePSNR |
---|
202 | }; |
---|
203 | |
---|
204 | LCMode Mode; |
---|
205 | double AbsErrThreshold; |
---|
206 | double RelErrThreshold; |
---|
207 | double PSNRThreshold; |
---|
208 | |
---|
209 | LossyCompressionInfo() |
---|
210 | : Mode(LCModeNone), AbsErrThreshold(0.0), |
---|
211 | RelErrThreshold(0.0), PSNRThreshold(0.0) {} |
---|
212 | }; |
---|
213 | |
---|
214 | class Variable { |
---|
215 | private: |
---|
216 | template <typename ET> |
---|
217 | void deduceTypeInfoFromElement(ET *) { |
---|
218 | ElementSize = sizeof(ET); |
---|
219 | IsFloat = !std::numeric_limits<ET>::is_integer; |
---|
220 | IsSigned = std::numeric_limits<ET>::is_signed; |
---|
221 | } |
---|
222 | |
---|
223 | // There are specializations here to handle array types |
---|
224 | // (e.g. typedef float float4[4];), struct types |
---|
225 | // (e.g. struct float4 { float x, y, z, w; };), and scalar types. |
---|
226 | // Builtin vector types |
---|
227 | // (e.g. typedef float float4 __attribute__((ext_vector_type(4)));) should |
---|
228 | // also work. |
---|
229 | template <typename T> |
---|
230 | typename detail::enable_if<detail::is_array<T>::value, void>::type |
---|
231 | deduceTypeInfo(T *D) { |
---|
232 | Size = sizeof(T); |
---|
233 | deduceTypeInfoFromElement(&(*D)[0]); |
---|
234 | } |
---|
235 | |
---|
236 | template <typename T> |
---|
237 | typename detail::enable_if<detail::has_x<T>::value && |
---|
238 | !detail::is_array<T>::value, void>::type |
---|
239 | deduceTypeInfo(T *D) { |
---|
240 | Size = sizeof(T); |
---|
241 | deduceTypeInfoFromElement(&(*D).x); |
---|
242 | } |
---|
243 | |
---|
244 | template <typename T> |
---|
245 | typename detail::enable_if<!detail::has_x<T>::value && |
---|
246 | !detail::is_array<T>::value, void>::type |
---|
247 | deduceTypeInfo(T *D) { |
---|
248 | Size = sizeof(T); |
---|
249 | deduceTypeInfoFromElement(D); |
---|
250 | } |
---|
251 | |
---|
252 | public: |
---|
253 | template <typename T> |
---|
254 | Variable(const std::string &N, T* D, unsigned Flags = 0, |
---|
255 | const LossyCompressionInfo &LCI = LossyCompressionInfo()) |
---|
256 | : Name(N), Data((void *) D), HasExtraSpace(Flags & VarHasExtraSpace), |
---|
257 | IsPhysCoordX(Flags & VarIsPhysCoordX), |
---|
258 | IsPhysCoordY(Flags & VarIsPhysCoordY), |
---|
259 | IsPhysCoordZ(Flags & VarIsPhysCoordZ), |
---|
260 | MaybePhysGhost(Flags & VarMaybePhysGhost), |
---|
261 | LCI(LCI) { |
---|
262 | deduceTypeInfo(D); |
---|
263 | } |
---|
264 | |
---|
265 | template <typename T> |
---|
266 | Variable(const std::string &N, std::size_t NumElements, T* D, |
---|
267 | unsigned Flags = 0, |
---|
268 | const LossyCompressionInfo &LCI = LossyCompressionInfo()) |
---|
269 | : Name(N), Data((void *) D), HasExtraSpace(Flags & VarHasExtraSpace), |
---|
270 | IsPhysCoordX(Flags & VarIsPhysCoordX), |
---|
271 | IsPhysCoordY(Flags & VarIsPhysCoordY), |
---|
272 | IsPhysCoordZ(Flags & VarIsPhysCoordZ), |
---|
273 | MaybePhysGhost(Flags & VarMaybePhysGhost), |
---|
274 | LCI(LCI) { |
---|
275 | deduceTypeInfoFromElement(D); |
---|
276 | Size = ElementSize*NumElements; |
---|
277 | } |
---|
278 | |
---|
279 | Variable(const VariableInfo &VI, void *D, unsigned Flags = 0, |
---|
280 | const LossyCompressionInfo &LCI = LossyCompressionInfo()) |
---|
281 | : Name(VI.Name), Size(VI.Size), IsFloat(VI.IsFloat), |
---|
282 | IsSigned(VI.IsSigned), Data(D), |
---|
283 | HasExtraSpace(Flags & VarHasExtraSpace), |
---|
284 | IsPhysCoordX((Flags & VarIsPhysCoordX) || VI.IsPhysCoordX), |
---|
285 | IsPhysCoordY((Flags & VarIsPhysCoordY) || VI.IsPhysCoordY), |
---|
286 | IsPhysCoordZ((Flags & VarIsPhysCoordZ) || VI.IsPhysCoordZ), |
---|
287 | MaybePhysGhost((Flags & VarMaybePhysGhost) || VI.MaybePhysGhost), |
---|
288 | ElementSize(VI.ElementSize), LCI(LCI) {} |
---|
289 | |
---|
290 | std::string Name; |
---|
291 | std::size_t Size; |
---|
292 | bool IsFloat; |
---|
293 | bool IsSigned; |
---|
294 | void *Data; |
---|
295 | bool HasExtraSpace; |
---|
296 | bool IsPhysCoordX, IsPhysCoordY, IsPhysCoordZ; |
---|
297 | bool MaybePhysGhost; |
---|
298 | std::size_t ElementSize; |
---|
299 | |
---|
300 | LossyCompressionInfo LCI; |
---|
301 | }; |
---|
302 | |
---|
303 | public: |
---|
304 | enum FileIO { |
---|
305 | FileIOMPI, |
---|
306 | FileIOPOSIX, |
---|
307 | FileIOMPICollective |
---|
308 | }; |
---|
309 | |
---|
310 | #ifndef GENERICIO_NO_MPI |
---|
311 | GenericIO(const MPI_Comm &C, const std::string &FN, unsigned FIOT = -1) |
---|
312 | : NElems(0), FileIOType(FIOT == (unsigned) -1 ? DefaultFileIOType : FIOT), |
---|
313 | Partition(DefaultPartition), Comm(C), FileName(FN), Redistributing(false), |
---|
314 | DisableCollErrChecking(false), SplitComm(MPI_COMM_NULL) { |
---|
315 | std::fill(PhysOrigin, PhysOrigin + 3, 0.0); |
---|
316 | std::fill(PhysScale, PhysScale + 3, 0.0); |
---|
317 | } |
---|
318 | #else |
---|
319 | GenericIO(const std::string &FN, unsigned FIOT = -1) |
---|
320 | : NElems(0), FileIOType(FIOT == (unsigned) -1 ? DefaultFileIOType : FIOT), |
---|
321 | Partition(DefaultPartition), FileName(FN), Redistributing(false), |
---|
322 | DisableCollErrChecking(false) { |
---|
323 | std::fill(PhysOrigin, PhysOrigin + 3, 0.0); |
---|
324 | std::fill(PhysScale, PhysScale + 3, 0.0); |
---|
325 | } |
---|
326 | #endif |
---|
327 | |
---|
328 | ~GenericIO() { |
---|
329 | close(); |
---|
330 | |
---|
331 | #ifndef GENERICIO_NO_MPI |
---|
332 | if (SplitComm != MPI_COMM_NULL) |
---|
333 | MPI_Comm_free(&SplitComm); |
---|
334 | #endif |
---|
335 | } |
---|
336 | |
---|
337 | public: |
---|
338 | std::size_t requestedExtraSpace() const { |
---|
339 | return 8; |
---|
340 | } |
---|
341 | |
---|
342 | void setNumElems(std::size_t E) { |
---|
343 | NElems = E; |
---|
344 | |
---|
345 | #ifndef GENERICIO_NO_MPI |
---|
346 | int IsLarge = E >= CollectiveMPIIOThreshold; |
---|
347 | int AllIsLarge; |
---|
348 | MPI_Allreduce(&IsLarge, &AllIsLarge, 1, MPI_INT, MPI_SUM, Comm); |
---|
349 | if (!AllIsLarge) |
---|
350 | FileIOType = FileIOMPICollective; |
---|
351 | #endif |
---|
352 | } |
---|
353 | |
---|
354 | void setPhysOrigin(double O, int Dim = -1) { |
---|
355 | if (Dim >= 0) |
---|
356 | PhysOrigin[Dim] = O; |
---|
357 | else |
---|
358 | std::fill(PhysOrigin, PhysOrigin + 3, O); |
---|
359 | } |
---|
360 | |
---|
361 | void setPhysScale(double S, int Dim = -1) { |
---|
362 | if (Dim >= 0) |
---|
363 | PhysScale[Dim] = S; |
---|
364 | else |
---|
365 | std::fill(PhysScale, PhysScale + 3, S); |
---|
366 | } |
---|
367 | |
---|
368 | template <typename T> |
---|
369 | void addVariable(const std::string &Name, T *Data, |
---|
370 | unsigned Flags = 0, |
---|
371 | const LossyCompressionInfo &LCI = LossyCompressionInfo()) { |
---|
372 | Vars.push_back(Variable(Name, Data, Flags, LCI)); |
---|
373 | } |
---|
374 | |
---|
375 | template <typename T, typename A> |
---|
376 | void addVariable(const std::string &Name, std::vector<T, A> &Data, |
---|
377 | unsigned Flags = 0, |
---|
378 | const LossyCompressionInfo &LCI = LossyCompressionInfo()) { |
---|
379 | T *D = Data.empty() ? 0 : &Data[0]; |
---|
380 | addVariable(Name, D, Flags, LCI); |
---|
381 | } |
---|
382 | |
---|
383 | void addVariable(const VariableInfo &VI, void *Data, |
---|
384 | unsigned Flags = 0, |
---|
385 | const LossyCompressionInfo &LCI = LossyCompressionInfo()) { |
---|
386 | Vars.push_back(Variable(VI, Data, Flags, LCI)); |
---|
387 | } |
---|
388 | |
---|
389 | template <typename T> |
---|
390 | void addScalarizedVariable(const std::string &Name, T *Data, |
---|
391 | std::size_t NumElements, unsigned Flags = 0, |
---|
392 | const LossyCompressionInfo &LCI = LossyCompressionInfo()) { |
---|
393 | Vars.push_back(Variable(Name, NumElements, Data, Flags, LCI)); |
---|
394 | } |
---|
395 | |
---|
396 | template <typename T, typename A> |
---|
397 | void addScalarizedVariable(const std::string &Name, std::vector<T, A> &Data, |
---|
398 | std::size_t NumElements, unsigned Flags = 0, |
---|
399 | const LossyCompressionInfo &LCI = LossyCompressionInfo()) { |
---|
400 | T *D = Data.empty() ? 0 : &Data[0]; |
---|
401 | addScalarizedVariable(Name, D, NumElements, Flags, LCI); |
---|
402 | } |
---|
403 | |
---|
404 | #ifndef GENERICIO_NO_MPI |
---|
405 | // Writing |
---|
406 | void write(); |
---|
407 | #endif |
---|
408 | |
---|
409 | enum MismatchBehavior { |
---|
410 | MismatchAllowed, |
---|
411 | MismatchDisallowed, |
---|
412 | MismatchRedistribute |
---|
413 | }; |
---|
414 | |
---|
415 | // Reading |
---|
416 | void openAndReadHeader(MismatchBehavior MB = MismatchDisallowed, |
---|
417 | int EffRank = -1, bool CheckPartMap = true); |
---|
418 | |
---|
419 | int readNRanks(); |
---|
420 | void readDims(int Dims[3]); |
---|
421 | |
---|
422 | // Note: For partitioned inputs, this returns -1. |
---|
423 | uint64_t readTotalNumElems(); |
---|
424 | |
---|
425 | void readPhysOrigin(double Origin[3]); |
---|
426 | void readPhysScale(double Scale[3]); |
---|
427 | |
---|
428 | void clearVariables() { this->Vars.clear(); }; |
---|
429 | |
---|
430 | int getNumberOfVariables() { return this->Vars.size(); }; |
---|
431 | |
---|
432 | void getVariableInfo(std::vector<VariableInfo> &VI); |
---|
433 | |
---|
434 | std::size_t readNumElems(int EffRank = -1); |
---|
435 | void readCoords(int Coords[3], int EffRank = -1); |
---|
436 | int readGlobalRankNumber(int EffRank = -1); |
---|
437 | |
---|
438 | void readData(int EffRank = -1, bool PrintStats = true, bool CollStats = true); |
---|
439 | |
---|
440 | void getSourceRanks(std::vector<int> &SR); |
---|
441 | |
---|
442 | void close() { |
---|
443 | FH.close(); |
---|
444 | } |
---|
445 | |
---|
446 | void setPartition(int P) { |
---|
447 | Partition = P; |
---|
448 | } |
---|
449 | |
---|
450 | static void setDefaultFileIOType(unsigned FIOT) { |
---|
451 | DefaultFileIOType = FIOT; |
---|
452 | } |
---|
453 | |
---|
454 | static void setDefaultPartition(int P) { |
---|
455 | DefaultPartition = P; |
---|
456 | } |
---|
457 | |
---|
458 | static void setNaturalDefaultPartition(); |
---|
459 | |
---|
460 | static void setDefaultShouldCompress(bool C) { |
---|
461 | DefaultShouldCompress = C; |
---|
462 | } |
---|
463 | |
---|
464 | #ifndef GENERICIO_NO_MPI |
---|
465 | static void setCollectiveMPIIOThreshold(std::size_t T) { |
---|
466 | #ifndef GENERICIO_NO_NEVER_USE_COLLECTIVE_IO |
---|
467 | CollectiveMPIIOThreshold = T; |
---|
468 | #endif |
---|
469 | } |
---|
470 | #endif |
---|
471 | |
---|
472 | private: |
---|
473 | // Implementation functions templated on the Endianness of the underlying |
---|
474 | // data. |
---|
475 | |
---|
476 | #ifndef GENERICIO_NO_MPI |
---|
477 | template <bool IsBigEndian> |
---|
478 | void write(); |
---|
479 | #endif |
---|
480 | |
---|
481 | template <bool IsBigEndian> |
---|
482 | void readHeaderLeader(void *GHPtr, MismatchBehavior MB, int Rank, int NRanks, |
---|
483 | int SplitNRanks, std::string &LocalFileName, |
---|
484 | uint64_t &HeaderSize, std::vector<char> &Header); |
---|
485 | |
---|
486 | template <bool IsBigEndian> |
---|
487 | int readNRanks(); |
---|
488 | |
---|
489 | template <bool IsBigEndian> |
---|
490 | void readDims(int Dims[3]); |
---|
491 | |
---|
492 | template <bool IsBigEndian> |
---|
493 | uint64_t readTotalNumElems(); |
---|
494 | |
---|
495 | template <bool IsBigEndian> |
---|
496 | void readPhysOrigin(double Origin[3]); |
---|
497 | |
---|
498 | template <bool IsBigEndian> |
---|
499 | void readPhysScale(double Scale[3]); |
---|
500 | |
---|
501 | template <bool IsBigEndian> |
---|
502 | int readGlobalRankNumber(int EffRank); |
---|
503 | |
---|
504 | template <bool IsBigEndian> |
---|
505 | size_t readNumElems(int EffRank); |
---|
506 | |
---|
507 | template <bool IsBigEndian> |
---|
508 | void readCoords(int Coords[3], int EffRank); |
---|
509 | |
---|
510 | void readData(int EffRank, size_t RowOffset, int Rank, |
---|
511 | uint64_t &TotalReadSize, int NErrs[3]); |
---|
512 | |
---|
513 | template <bool IsBigEndian> |
---|
514 | void readData(int EffRank, size_t RowOffset, |
---|
515 | int Rank, uint64_t &TotalReadSize, int NErrs[3]); |
---|
516 | |
---|
517 | template <bool IsBigEndian> |
---|
518 | void getVariableInfo(std::vector<VariableInfo> &VI); |
---|
519 | |
---|
520 | protected: |
---|
521 | std::vector<Variable> Vars; |
---|
522 | std::size_t NElems; |
---|
523 | |
---|
524 | double PhysOrigin[3], PhysScale[3]; |
---|
525 | |
---|
526 | unsigned FileIOType; |
---|
527 | int Partition; |
---|
528 | #ifndef GENERICIO_NO_MPI |
---|
529 | MPI_Comm Comm; |
---|
530 | #endif |
---|
531 | std::string FileName; |
---|
532 | |
---|
533 | static unsigned DefaultFileIOType; |
---|
534 | static int DefaultPartition; |
---|
535 | static bool DefaultShouldCompress; |
---|
536 | |
---|
537 | #ifndef GENERICIO_NO_MPI |
---|
538 | static std::size_t CollectiveMPIIOThreshold; |
---|
539 | #endif |
---|
540 | |
---|
541 | // When redistributing, the rank blocks which this process should read. |
---|
542 | bool Redistributing, DisableCollErrChecking; |
---|
543 | std::vector<int> SourceRanks; |
---|
544 | |
---|
545 | std::vector<int> RankMap; |
---|
546 | #ifndef GENERICIO_NO_MPI |
---|
547 | MPI_Comm SplitComm; |
---|
548 | #endif |
---|
549 | std::string OpenFileName; |
---|
550 | |
---|
551 | // This reference counting mechanism allows the the GenericIO class |
---|
552 | // to be used in a cursor mode. To do this, make a copy of the class |
---|
553 | // after reading the header but prior to adding the variables. |
---|
554 | struct FHManager { |
---|
555 | FHManager() : CountedFH(0) { |
---|
556 | allocate(); |
---|
557 | } |
---|
558 | |
---|
559 | FHManager(const FHManager& F) { |
---|
560 | CountedFH = F.CountedFH; |
---|
561 | CountedFH->Cnt += 1; |
---|
562 | } |
---|
563 | |
---|
564 | ~FHManager() { |
---|
565 | close(); |
---|
566 | } |
---|
567 | |
---|
568 | GenericFileIO *&get() { |
---|
569 | if (!CountedFH) |
---|
570 | allocate(); |
---|
571 | |
---|
572 | return CountedFH->GFIO; |
---|
573 | } |
---|
574 | |
---|
575 | std::vector<char> &getHeaderCache() { |
---|
576 | if (!CountedFH) |
---|
577 | allocate(); |
---|
578 | |
---|
579 | return CountedFH->HeaderCache; |
---|
580 | } |
---|
581 | |
---|
582 | bool isBigEndian() { |
---|
583 | return CountedFH ? CountedFH->IsBigEndian : false; |
---|
584 | } |
---|
585 | |
---|
586 | void setIsBigEndian(bool isBE) { |
---|
587 | CountedFH->IsBigEndian = isBE; |
---|
588 | } |
---|
589 | |
---|
590 | void allocate() { |
---|
591 | close(); |
---|
592 | CountedFH = new FHWCnt; |
---|
593 | }; |
---|
594 | |
---|
595 | void close() { |
---|
596 | if (CountedFH && CountedFH->Cnt == 1) |
---|
597 | delete CountedFH; |
---|
598 | else if (CountedFH) |
---|
599 | CountedFH->Cnt -= 1; |
---|
600 | |
---|
601 | CountedFH = 0; |
---|
602 | } |
---|
603 | |
---|
604 | struct FHWCnt { |
---|
605 | FHWCnt() : GFIO(0), Cnt(1), IsBigEndian(false) {} |
---|
606 | |
---|
607 | ~FHWCnt() { |
---|
608 | close(); |
---|
609 | } |
---|
610 | |
---|
611 | protected: |
---|
612 | void close() { |
---|
613 | delete GFIO; |
---|
614 | GFIO = 0; |
---|
615 | } |
---|
616 | |
---|
617 | public: |
---|
618 | GenericFileIO *GFIO; |
---|
619 | size_t Cnt; |
---|
620 | |
---|
621 | // Used for reading |
---|
622 | std::vector<char> HeaderCache; |
---|
623 | bool IsBigEndian; |
---|
624 | }; |
---|
625 | |
---|
626 | FHWCnt *CountedFH; |
---|
627 | } FH; |
---|
628 | }; |
---|
629 | |
---|
630 | } /* END namespace cosmotk */ |
---|
631 | #endif // GENERICIO_H |
---|
632 | |
---|