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 | class GenericIO { |
---|
121 | public: |
---|
122 | enum VariableFlags { |
---|
123 | VarHasExtraSpace = (1 << 0), // Note that this flag indicates that the |
---|
124 | // extra space is available, but the GenericIO |
---|
125 | // implementation is required to |
---|
126 | // preserve its contents. |
---|
127 | VarIsPhysCoordX = (1 << 1), |
---|
128 | VarIsPhysCoordY = (1 << 2), |
---|
129 | VarIsPhysCoordZ = (1 << 3), |
---|
130 | VarMaybePhysGhost = (1 << 4) |
---|
131 | }; |
---|
132 | |
---|
133 | struct VariableInfo { |
---|
134 | VariableInfo(const std::string &N, std::size_t S, bool IF, bool IS, |
---|
135 | bool PCX, bool PCY, bool PCZ, bool PG) |
---|
136 | : Name(N), Size(S), IsFloat(IF), IsSigned(IS), |
---|
137 | IsPhysCoordX(PCX), IsPhysCoordY(PCY), IsPhysCoordZ(PCZ), |
---|
138 | MaybePhysGhost(PG) {} |
---|
139 | |
---|
140 | std::string Name; |
---|
141 | std::size_t Size; |
---|
142 | bool IsFloat; |
---|
143 | bool IsSigned; |
---|
144 | bool IsPhysCoordX, IsPhysCoordY, IsPhysCoordZ; |
---|
145 | bool MaybePhysGhost; |
---|
146 | }; |
---|
147 | |
---|
148 | public: |
---|
149 | struct Variable { |
---|
150 | template <typename T> |
---|
151 | Variable(const std::string &N, T* D, unsigned Flags = 0) |
---|
152 | : Name(N), Size(sizeof(T)), |
---|
153 | IsFloat(!std::numeric_limits<T>::is_integer), |
---|
154 | IsSigned(std::numeric_limits<T>::is_signed), |
---|
155 | Data((void *) D), HasExtraSpace(Flags & VarHasExtraSpace), |
---|
156 | IsPhysCoordX(Flags & VarIsPhysCoordX), |
---|
157 | IsPhysCoordY(Flags & VarIsPhysCoordY), |
---|
158 | IsPhysCoordZ(Flags & VarIsPhysCoordZ), |
---|
159 | MaybePhysGhost(Flags & VarMaybePhysGhost) {} |
---|
160 | |
---|
161 | Variable(const VariableInfo &VI, void *D, unsigned Flags = 0) |
---|
162 | : Name(VI.Name), Size(VI.Size), IsFloat(VI.IsFloat), |
---|
163 | IsSigned(VI.IsSigned), Data(D), |
---|
164 | HasExtraSpace(Flags & VarHasExtraSpace), |
---|
165 | IsPhysCoordX((Flags & VarIsPhysCoordX) || VI.IsPhysCoordX), |
---|
166 | IsPhysCoordY((Flags & VarIsPhysCoordY) || VI.IsPhysCoordY), |
---|
167 | IsPhysCoordZ((Flags & VarIsPhysCoordZ) || VI.IsPhysCoordZ), |
---|
168 | MaybePhysGhost((Flags & VarMaybePhysGhost) || VI.MaybePhysGhost) {} |
---|
169 | |
---|
170 | std::string Name; |
---|
171 | std::size_t Size; |
---|
172 | bool IsFloat; |
---|
173 | bool IsSigned; |
---|
174 | void *Data; |
---|
175 | bool HasExtraSpace; |
---|
176 | bool IsPhysCoordX, IsPhysCoordY, IsPhysCoordZ; |
---|
177 | bool MaybePhysGhost; |
---|
178 | }; |
---|
179 | |
---|
180 | public: |
---|
181 | enum FileIO { |
---|
182 | FileIOMPI, |
---|
183 | FileIOPOSIX, |
---|
184 | FileIOMPICollective |
---|
185 | }; |
---|
186 | |
---|
187 | #ifndef GENERICIO_NO_MPI |
---|
188 | GenericIO(const MPI_Comm &C, const std::string &FN, unsigned FIOT = -1) |
---|
189 | : NElems(0), FileIOType(FIOT == (unsigned) -1 ? DefaultFileIOType : FIOT), |
---|
190 | Partition(DefaultPartition), Comm(C), FileName(FN), SplitComm(MPI_COMM_NULL) { |
---|
191 | std::fill(PhysOrigin, PhysOrigin + 3, 0.0); |
---|
192 | std::fill(PhysScale, PhysScale + 3, 0.0); |
---|
193 | } |
---|
194 | #else |
---|
195 | GenericIO(const std::string &FN, unsigned FIOT = -1) |
---|
196 | : NElems(0), FileIOType(FIOT == (unsigned) -1 ? DefaultFileIOType : FIOT), |
---|
197 | Partition(DefaultPartition), FileName(FN) { |
---|
198 | std::fill(PhysOrigin, PhysOrigin + 3, 0.0); |
---|
199 | std::fill(PhysScale, PhysScale + 3, 0.0); |
---|
200 | } |
---|
201 | #endif |
---|
202 | |
---|
203 | ~GenericIO() { |
---|
204 | close(); |
---|
205 | |
---|
206 | #ifndef GENERICIO_NO_MPI |
---|
207 | if (SplitComm != MPI_COMM_NULL) |
---|
208 | MPI_Comm_free(&SplitComm); |
---|
209 | #endif |
---|
210 | } |
---|
211 | |
---|
212 | public: |
---|
213 | std::size_t requestedExtraSpace() const { |
---|
214 | return 8; |
---|
215 | } |
---|
216 | |
---|
217 | void setNumElems(std::size_t E) { |
---|
218 | NElems = E; |
---|
219 | |
---|
220 | #ifndef GENERICIO_NO_MPI |
---|
221 | int IsLarge = E >= CollectiveMPIIOThreshold; |
---|
222 | int AllIsLarge; |
---|
223 | MPI_Allreduce(&IsLarge, &AllIsLarge, 1, MPI_INT, MPI_SUM, Comm); |
---|
224 | if (!AllIsLarge) |
---|
225 | FileIOType = FileIOMPICollective; |
---|
226 | #endif |
---|
227 | } |
---|
228 | |
---|
229 | void setPhysOrigin(double O, int Dim = -1) { |
---|
230 | if (Dim >= 0) |
---|
231 | PhysOrigin[Dim] = O; |
---|
232 | else |
---|
233 | std::fill(PhysOrigin, PhysOrigin + 3, O); |
---|
234 | } |
---|
235 | |
---|
236 | void setPhysScale(double S, int Dim = -1) { |
---|
237 | if (Dim >= 0) |
---|
238 | PhysScale[Dim] = S; |
---|
239 | else |
---|
240 | std::fill(PhysScale, PhysScale + 3, S); |
---|
241 | } |
---|
242 | |
---|
243 | template <typename T> |
---|
244 | void addVariable(const std::string &Name, T *Data, |
---|
245 | unsigned Flags = 0) { |
---|
246 | Vars.push_back(Variable(Name, Data, Flags)); |
---|
247 | } |
---|
248 | |
---|
249 | template <typename T, typename A> |
---|
250 | void addVariable(const std::string &Name, std::vector<T, A> &Data, |
---|
251 | unsigned Flags = 0) { |
---|
252 | T *D = Data.empty() ? 0 : &Data[0]; |
---|
253 | addVariable(Name, D, Flags); |
---|
254 | } |
---|
255 | |
---|
256 | void addVariable(const VariableInfo &VI, void *Data, |
---|
257 | unsigned Flags = 0) { |
---|
258 | Vars.push_back(Variable(VI, Data, Flags)); |
---|
259 | } |
---|
260 | |
---|
261 | #ifndef GENERICIO_NO_MPI |
---|
262 | // Writing |
---|
263 | void write(); |
---|
264 | #endif |
---|
265 | |
---|
266 | // Reading |
---|
267 | void openAndReadHeader(bool MustMatch = true, int EffRank = -1, |
---|
268 | bool CheckPartMap = true); |
---|
269 | |
---|
270 | int readNRanks(); |
---|
271 | void readDims(int Dims[3]); |
---|
272 | |
---|
273 | // Note: For partitioned inputs, this returns -1. |
---|
274 | uint64_t readTotalNumElems(); |
---|
275 | |
---|
276 | void readPhysOrigin(double Origin[3]); |
---|
277 | void readPhysScale(double Scale[3]); |
---|
278 | |
---|
279 | void clearVariables() { this->Vars.clear(); }; |
---|
280 | |
---|
281 | int getNumberOfVariables() { return this->Vars.size(); }; |
---|
282 | |
---|
283 | |
---|
284 | void getVariableInfo(std::vector<VariableInfo> &VI); |
---|
285 | |
---|
286 | std::size_t readNumElems(int EffRank = -1); |
---|
287 | void readCoords(int Coords[3], int EffRank = -1); |
---|
288 | int readGlobalRankNumber(int EffRank = -1); |
---|
289 | |
---|
290 | void readData(int EffRank = -1, bool PrintStats = true, bool CollStats = true); |
---|
291 | |
---|
292 | void close() { |
---|
293 | FH.close(); |
---|
294 | } |
---|
295 | |
---|
296 | void setPartition(int P) { |
---|
297 | Partition = P; |
---|
298 | } |
---|
299 | |
---|
300 | static void setDefaultFileIOType(unsigned FIOT) { |
---|
301 | DefaultFileIOType = FIOT; |
---|
302 | } |
---|
303 | |
---|
304 | static void setDefaultPartition(int P) { |
---|
305 | DefaultPartition = P; |
---|
306 | } |
---|
307 | |
---|
308 | static void setNaturalDefaultPartition(); |
---|
309 | |
---|
310 | static void setDefaultShouldCompress(bool C) { |
---|
311 | DefaultShouldCompress = C; |
---|
312 | } |
---|
313 | |
---|
314 | #ifndef GENERICIO_NO_MPI |
---|
315 | static void setCollectiveMPIIOThreshold(std::size_t T) { |
---|
316 | #ifndef GENERICIO_NO_NEVER_USE_COLLECTIVE_IO |
---|
317 | CollectiveMPIIOThreshold = T; |
---|
318 | #endif |
---|
319 | } |
---|
320 | #endif |
---|
321 | |
---|
322 | private: |
---|
323 | // Implementation functions templated on the Endianness of the underlying |
---|
324 | // data. |
---|
325 | |
---|
326 | #ifndef GENERICIO_NO_MPI |
---|
327 | template <bool IsBigEndian> |
---|
328 | void write(); |
---|
329 | #endif |
---|
330 | |
---|
331 | template <bool IsBigEndian> |
---|
332 | void readHeaderLeader(void *GHPtr, bool MustMatch, int SplitNRanks, |
---|
333 | std::string &LocalFileName, uint64_t &HeaderSize, |
---|
334 | std::vector<char> &Header); |
---|
335 | |
---|
336 | template <bool IsBigEndian> |
---|
337 | int readNRanks(); |
---|
338 | |
---|
339 | template <bool IsBigEndian> |
---|
340 | void readDims(int Dims[3]); |
---|
341 | |
---|
342 | template <bool IsBigEndian> |
---|
343 | uint64_t readTotalNumElems(); |
---|
344 | |
---|
345 | template <bool IsBigEndian> |
---|
346 | void readPhysOrigin(double Origin[3]); |
---|
347 | |
---|
348 | template <bool IsBigEndian> |
---|
349 | void readPhysScale(double Scale[3]); |
---|
350 | |
---|
351 | template <bool IsBigEndian> |
---|
352 | int readGlobalRankNumber(int EffRank); |
---|
353 | |
---|
354 | template <bool IsBigEndian> |
---|
355 | size_t readNumElems(int EffRank); |
---|
356 | |
---|
357 | template <bool IsBigEndian> |
---|
358 | void readCoords(int Coords[3], int EffRank); |
---|
359 | |
---|
360 | template <bool IsBigEndian> |
---|
361 | void readData(int EffRank, bool PrintStats, bool CollStats); |
---|
362 | |
---|
363 | template <bool IsBigEndian> |
---|
364 | void getVariableInfo(std::vector<VariableInfo> &VI); |
---|
365 | |
---|
366 | protected: |
---|
367 | std::vector<Variable> Vars; |
---|
368 | std::size_t NElems; |
---|
369 | |
---|
370 | double PhysOrigin[3], PhysScale[3]; |
---|
371 | |
---|
372 | unsigned FileIOType; |
---|
373 | int Partition; |
---|
374 | #ifndef GENERICIO_NO_MPI |
---|
375 | MPI_Comm Comm; |
---|
376 | #endif |
---|
377 | std::string FileName; |
---|
378 | |
---|
379 | static unsigned DefaultFileIOType; |
---|
380 | static int DefaultPartition; |
---|
381 | static bool DefaultShouldCompress; |
---|
382 | |
---|
383 | #ifndef GENERICIO_NO_MPI |
---|
384 | static std::size_t CollectiveMPIIOThreshold; |
---|
385 | #endif |
---|
386 | |
---|
387 | std::vector<int> RankMap; |
---|
388 | #ifndef GENERICIO_NO_MPI |
---|
389 | MPI_Comm SplitComm; |
---|
390 | #endif |
---|
391 | std::string OpenFileName; |
---|
392 | |
---|
393 | // This reference counting mechanism allows the the GenericIO class |
---|
394 | // to be used in a cursor mode. To do this, make a copy of the class |
---|
395 | // after reading the header but prior to adding the variables. |
---|
396 | struct FHManager { |
---|
397 | FHManager() : CountedFH(0) { |
---|
398 | allocate(); |
---|
399 | } |
---|
400 | |
---|
401 | FHManager(const FHManager& F) { |
---|
402 | CountedFH = F.CountedFH; |
---|
403 | CountedFH->Cnt += 1; |
---|
404 | } |
---|
405 | |
---|
406 | ~FHManager() { |
---|
407 | close(); |
---|
408 | } |
---|
409 | |
---|
410 | GenericFileIO *&get() { |
---|
411 | if (!CountedFH) |
---|
412 | allocate(); |
---|
413 | |
---|
414 | return CountedFH->GFIO; |
---|
415 | } |
---|
416 | |
---|
417 | std::vector<char> &getHeaderCache() { |
---|
418 | if (!CountedFH) |
---|
419 | allocate(); |
---|
420 | |
---|
421 | return CountedFH->HeaderCache; |
---|
422 | } |
---|
423 | |
---|
424 | bool isBigEndian() { |
---|
425 | return CountedFH ? CountedFH->IsBigEndian : false; |
---|
426 | } |
---|
427 | |
---|
428 | void setIsBigEndian(bool isBE) { |
---|
429 | CountedFH->IsBigEndian = isBE; |
---|
430 | } |
---|
431 | |
---|
432 | void allocate() { |
---|
433 | close(); |
---|
434 | CountedFH = new FHWCnt; |
---|
435 | }; |
---|
436 | |
---|
437 | void close() { |
---|
438 | if (CountedFH && CountedFH->Cnt == 1) |
---|
439 | delete CountedFH; |
---|
440 | else if (CountedFH) |
---|
441 | CountedFH->Cnt -= 1; |
---|
442 | |
---|
443 | CountedFH = 0; |
---|
444 | } |
---|
445 | |
---|
446 | struct FHWCnt { |
---|
447 | FHWCnt() : GFIO(0), Cnt(1), IsBigEndian(false) {} |
---|
448 | |
---|
449 | ~FHWCnt() { |
---|
450 | close(); |
---|
451 | } |
---|
452 | |
---|
453 | protected: |
---|
454 | void close() { |
---|
455 | delete GFIO; |
---|
456 | GFIO = 0; |
---|
457 | } |
---|
458 | |
---|
459 | public: |
---|
460 | GenericFileIO *GFIO; |
---|
461 | size_t Cnt; |
---|
462 | |
---|
463 | // Used for reading |
---|
464 | std::vector<char> HeaderCache; |
---|
465 | bool IsBigEndian; |
---|
466 | }; |
---|
467 | |
---|
468 | FHWCnt *CountedFH; |
---|
469 | } FH; |
---|
470 | }; |
---|
471 | |
---|
472 | } /* END namespace cosmotk */ |
---|
473 | #endif // GENERICIO_H |
---|
474 | |
---|