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 | protected: |
---|
323 | std::vector<Variable> Vars; |
---|
324 | std::size_t NElems; |
---|
325 | |
---|
326 | double PhysOrigin[3], PhysScale[3]; |
---|
327 | |
---|
328 | unsigned FileIOType; |
---|
329 | int Partition; |
---|
330 | #ifndef GENERICIO_NO_MPI |
---|
331 | MPI_Comm Comm; |
---|
332 | #endif |
---|
333 | std::string FileName; |
---|
334 | |
---|
335 | static unsigned DefaultFileIOType; |
---|
336 | static int DefaultPartition; |
---|
337 | static bool DefaultShouldCompress; |
---|
338 | |
---|
339 | #ifndef GENERICIO_NO_MPI |
---|
340 | static std::size_t CollectiveMPIIOThreshold; |
---|
341 | #endif |
---|
342 | |
---|
343 | std::vector<int> RankMap; |
---|
344 | #ifndef GENERICIO_NO_MPI |
---|
345 | MPI_Comm SplitComm; |
---|
346 | #endif |
---|
347 | std::string OpenFileName; |
---|
348 | |
---|
349 | // This reference counting mechanism allows the the GenericIO class |
---|
350 | // to be used in a cursor mode. To do this, make a copy of the class |
---|
351 | // after reading the header but prior to adding the variables. |
---|
352 | struct FHManager { |
---|
353 | FHManager() : CountedFH(0) { |
---|
354 | allocate(); |
---|
355 | } |
---|
356 | |
---|
357 | FHManager(const FHManager& F) { |
---|
358 | CountedFH = F.CountedFH; |
---|
359 | CountedFH->Cnt += 1; |
---|
360 | } |
---|
361 | |
---|
362 | ~FHManager() { |
---|
363 | close(); |
---|
364 | } |
---|
365 | |
---|
366 | GenericFileIO *&get() { |
---|
367 | if (!CountedFH) |
---|
368 | allocate(); |
---|
369 | |
---|
370 | return CountedFH->GFIO; |
---|
371 | } |
---|
372 | |
---|
373 | std::vector<char> &getHeaderCache() { |
---|
374 | if (!CountedFH) |
---|
375 | allocate(); |
---|
376 | |
---|
377 | return CountedFH->HeaderCache; |
---|
378 | } |
---|
379 | |
---|
380 | void allocate() { |
---|
381 | close(); |
---|
382 | CountedFH = new FHWCnt; |
---|
383 | }; |
---|
384 | |
---|
385 | void close() { |
---|
386 | if (CountedFH && CountedFH->Cnt == 1) |
---|
387 | delete CountedFH; |
---|
388 | else if (CountedFH) |
---|
389 | CountedFH->Cnt -= 1; |
---|
390 | |
---|
391 | CountedFH = 0; |
---|
392 | } |
---|
393 | |
---|
394 | struct FHWCnt { |
---|
395 | FHWCnt() : GFIO(0), Cnt(1) {} |
---|
396 | |
---|
397 | ~FHWCnt() { |
---|
398 | close(); |
---|
399 | } |
---|
400 | |
---|
401 | protected: |
---|
402 | void close() { |
---|
403 | delete GFIO; |
---|
404 | GFIO = 0; |
---|
405 | } |
---|
406 | |
---|
407 | public: |
---|
408 | GenericFileIO *GFIO; |
---|
409 | size_t Cnt; |
---|
410 | |
---|
411 | // Used for reading |
---|
412 | std::vector<char> HeaderCache; |
---|
413 | }; |
---|
414 | |
---|
415 | FHWCnt *CountedFH; |
---|
416 | } FH; |
---|
417 | }; |
---|
418 | |
---|
419 | } /* END namespace cosmotk */ |
---|
420 | #endif // GENERICIO_H |
---|
421 | |
---|