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