-
Notifications
You must be signed in to change notification settings - Fork 128
/
HDFMixerWriter.cpp
451 lines (377 loc) · 12.8 KB
/
HDFMixerWriter.cpp
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
/*
* Distributed under the OSI-approved Apache License, Version 2.0. See
* accompanying file Copyright.txt for details.
*
* HDFMixer.h
*
* Created on: Aug 16 2017
* Author: Junmin GU
*/
#include <iostream>
#include "HDFMixerWriter.h"
#include "adios2/helper/adiosFunctions.h"
//
// class HDFSerialWriter
//
namespace adios2
{
namespace core
{
namespace engine
{
HDFVDSWriter::HDFVDSWriter(helper::Comm const &comm)
: m_SubfileComm(comm), m_VDSFile(), m_Rank(-1)
{
m_NumSubFiles = m_SubfileComm.Size();
m_Rank = m_SubfileComm.Rank();
}
void HDFVDSWriter::Init(const std::string &name)
{
if (m_Rank > 0)
{
return;
}
//
// VDS can only operate on one process. So let rank = 0 handle it
//
std::string h5Name = adios2::helper::AddExtension(name, ".h5");
m_VDSFile.Init(h5Name, helper::Comm(), true);
// m_FileName = h5Name;
m_FileName = name;
}
void HDFVDSWriter::GetVarInfo(const VariableBase &var,
std::vector<hsize_t> &dimsf, int nDims,
std::vector<hsize_t> &start,
std::vector<hsize_t> &count,
std::vector<hsize_t> &one)
{ // interop::HDF5Common summaryFile(true);
// std::vector<hsize_t> dimsf, start, one, count;
// int nDims = std::max(var.m_Shape.size(), var.m_Count.size());
for (int i = 0; i < nDims; i++)
{
if (var.m_Shape.size() > 0)
{
dimsf.push_back(var.m_Shape[i]);
}
else
{
dimsf.push_back(var.m_Count[i]);
}
if (var.m_Start.size() > 0)
{
start.push_back(var.m_Start[i]);
}
else
{
start.push_back(0);
}
if (var.m_Count.size() > 0)
{
count.push_back(var.m_Count[i]);
}
else if (var.m_Shape.size() > 0)
{
count.push_back(var.m_Shape[i]);
}
else
{
count.push_back(0);
}
one.push_back(1);
}
}
void HDFVDSWriter::AddVar(const VariableBase &var, hid_t h5Type)
{
hid_t space;
/* Create VDS dataspace. */
int nDims = std::max(var.m_Shape.size(), var.m_Count.size());
if (nDims == 0)
{
if (m_Rank == 0)
{
/*
std::cout<<" will deal with scalar later?"<<var.m_Name<<std::endl;
hid_t filespaceID = H5Screate(H5S_SCALAR);
hid_t dsetID = H5Dcreate(m_VDSFile.m_GroupId, var.m_Name.c_str(),
h5Type, filespaceID, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
//hid_t plistID = H5Pcreate(H5P_DATASET_XFER);
//H5Pset_dxpl_mpio(plistID, H5FD_MPIO_COLLECTIVE);
herr_t status = H5Dwrite(dsetID, h5Type, H5S_ALL, H5S_ALL,
H5P_DEFAULT, values);
//herr_t status = H5Dwrite(dsetID, h5Type, H5S_ALL, H5S_ALL,
plistID, values);
H5Sclose(filespaceID);
H5Dclose(dsetID);
*/
}
return; //
}
/* Initialize hyperslab values. */
size_t all_starts[m_NumSubFiles][nDims];
size_t all_counts[m_NumSubFiles][nDims];
//
std::vector<hsize_t> dimsf, start, one, count;
GetVarInfo(var, dimsf, nDims, start, count, one);
//
m_SubfileComm.Gather(start.data(), nDims, all_starts[0], nDims, 0);
m_SubfileComm.Gather(count.data(), nDims, all_counts[0], nDims, 0);
herr_t status;
if (m_Rank == 0)
{
m_VDSFile.CheckWriteGroup();
/* Set VDS creation property. */
hid_t dcpl = H5Pcreate(H5P_DATASET_CREATE);
// status = H5Pset_fill_value(dcpl, ADIOS2_MPI_SIZE_T, 0);
space = H5Screate_simple(nDims, dimsf.data(), NULL);
// summaryFile.Init(fileName.c_str(), MPI_COMM_SELF, true);
hsize_t currCount[nDims], currStart[nDims];
// std::string subfileVarName="TimeStep0/"+var.m_Name; // need full
// path? NEED TO GET the RIGHT SUBFILE VAR NAME RELATED to TIMESTEP!!
std::string subfileVarName;
interop::HDF5Common::StaticGetAdiosStepString(
subfileVarName, m_VDSFile.m_CurrentAdiosStep);
subfileVarName += "/" + var.m_Name;
for (int i = 0; i < m_NumSubFiles; i++)
{
for (int j = 0; j < nDims; j++)
{
currCount[j] = all_counts[i][j];
currStart[j] = all_starts[i][j];
// std::cout<<i<<"th: subfile, "<<j<<"th dirmention:
// count:"<<currCount[j] <<" start:"<<currStart[j]<<std::endl;
}
hid_t src_space = H5Screate_simple(
nDims, currCount,
NULL); // with factor=1, we do not flatten the data
status = H5Sselect_hyperslab(space, H5S_SELECT_SET, currStart, NULL,
one.data(), currCount);
std::string path, root, subfileName;
HDFSerialWriter::StaticCreateName(
path, root, subfileName, m_FileName,
i); // for each core, get the subfile name
// std::cout<<" subfileName="<<subfileName<<",
// var="<<subfileVarName<<std::endl;
status = H5Pset_virtual(dcpl, space, subfileName.c_str(),
subfileVarName.c_str(), src_space);
status = H5Sclose(src_space);
}
/* Create a virtual dataset. */
// hid_t dset = H5Dcreate2 (m_VDSFile.m_FileId, subfileVarName.c_str(),
// h5Type, space, H5P_DEFAULT, dcpl, H5P_DEFAULT);
hid_t dset = H5Dcreate2(m_VDSFile.m_GroupId, var.m_Name.c_str(), h5Type,
space, H5P_DEFAULT, dcpl, H5P_DEFAULT);
status = H5Sclose(space);
status = H5Dclose(dset);
status = H5Pclose(dcpl);
}
// status is never checked so this silences the warning about it not
// being used. TODO: check the status and handle the error conditions.
static_cast<void>(status);
// m_VDSFile.Close();
m_SubfileComm.Barrier();
#if defined(__PGI)
// The above cast to void is not enough to quiet PGI. "Check" status.
if (status < 0)
{
return;
}
#endif
}
void HDFVDSWriter::Advance(const float timeoutSeconds)
{
if (m_Rank > 0)
{
return;
}
m_VDSFile.Advance();
}
void HDFVDSWriter::Close(const int transportIndex)
{
if (m_Rank > 0)
{
return;
}
m_VDSFile.Close();
}
//
// class HDFSerialWriter
//
HDFSerialWriter::HDFSerialWriter(helper::Comm const &comm)
: m_LocalComm(comm), m_H5File()
{
}
void HDFSerialWriter::Advance(const float timeoutSeconds)
{
m_H5File.Advance();
}
void HDFSerialWriter::Close(const int transportIndex) { m_H5File.Close(); };
void HDFSerialWriter::StaticCreateName(std::string &pathName,
std::string &rootName,
std::string &fullH5Name,
const std::string &input, int rank)
{
auto lf_GetBaseName = [](const std::string &name) -> std::string {
const std::string baseName(adios2::helper::AddExtension(name, ".h5") +
".dir");
return baseName;
};
auto lf_GetRootTag = [](const std::string &userTag) -> std::string {
std::string h5RootName = userTag;
const auto lastPathSeparator(userTag.find_last_of(PathSeparator));
if (lastPathSeparator != std::string::npos)
{
h5RootName = userTag.substr(lastPathSeparator);
}
return h5RootName;
};
pathName = lf_GetBaseName(input);
rootName = lf_GetRootTag(input);
fullH5Name =
(pathName + "/" + rootName + "_" + std::to_string(rank) + ".h5");
}
void HDFSerialWriter::Init(const std::string &name, int rank)
{
/*
auto lf_GetBaseName = [](const std::string &name) -> std::string {
const std::string baseName(AddExtension(name, ".h5") + ".dir");
return baseName;
};
auto lf_GetRootTag = [] (const std::string &userTag) -> std::string {
std::string h5RootName = userTag;
const auto lastPathSeparator(userTag.find_last_of(PathSeparator));
if (lastPathSeparator != std::string::npos)
{
h5RootName = userTag.substr(lastPathSeparator);
}
return h5RootName;
};
std::string baseName=lf_GetBaseName(name);
auto rootTag = lf_GetRootTag(name);
const std::string h5Name(baseName + "/" +
rootTag+"_"+std::to_string(rank)+".h5");
*/
std::string baseName, rootTag, h5Name;
StaticCreateName(baseName, rootTag, h5Name, name, rank);
// std::cout<<"rank="<<rank<<" name="<<h5Name<<std::endl;
adios2::helper::CreateDirectory(baseName);
m_H5File.Init(h5Name, m_LocalComm, true);
m_FileName = h5Name;
m_Rank = rank;
// m_H5File.Init(h5Name, m_, true);
}
/*
std::vector<std::string>
GetBaseNames(const std::vector<std::string> &names) const noexcept
{
auto lf_GetBaseName = [](const std::string &name) -> std::string {
const std::string baseName(AddExtension(name, ".h5") + ".dir");
return baseName;
};
std::vector<std::string> baseNames;
baseNames.reserve(names.size());
for (const auto &name : names)
{
baseNames.push_back(lf_GetBaseName(name));
}
return baseNames;
}
std::vector<std::string>
GetLocalFileNames(const std::vector<std::string> &baseNames,
const std::string &userTag) const noexcept
{
// e.g. /some/where/xy.h5.dir
// e.g. xy
auto lf_GetH5Name = [](const std::string &baseName,
const std::string &userTag,
const int rank) -> std::string {
#ifdef NEVER
const std::string h5BaseName = AddExtension(baseName, ".h5");
std::string h5RootName = h5BaseName;
const auto lastPathSeparator(h5BaseName.find_last_of(PathSeparator));
if (lastPathSeparator != std::string::npos)
{
h5RootName = h5BaseName.substr(lastPathSeparator);
}
const std::string h5Name(h5BaseName + ".dir/" + h5RootName + "." +
std::to_string(rank));
#else
const std::string h5Name(baseName + "/" +
userTag+"_"+std::to_string(rank)+".h5"); #endif return h5Name;
};
auto lf_GetRootTag = [] (const std::string &userTag) -> std::string {
std::string h5RootName = userTag;
const auto lastPathSeparator(userTag.find_last_of(PathSeparator));
if (lastPathSeparator != std::string::npos)
{
h5RootName = userTag.substr(lastPathSeparator);
}
return h5RootName;
};
std::vector<std::string> h5Names;
h5Names.reserve(baseNames.size());
auto rootTag = lf_GetRootTag(userTag);
for (const auto &baseName : baseNames)
{
h5Names.push_back(lf_GetH5Name(baseName, rootTag, m_RankMPI));
}
return h5Names;
}
enum class ResizeResult
{
Failure, //!< FAILURE, caught a std::bad_alloc
Unchanged, //!< UNCHANGED, no need to resize (sufficient capacity)
Success, //!< SUCCESS, resize was successful
Flush //!< FLUSH, need to flush to transports for current variable
};
template <class T>
ResizeResult ResizeBuffer(const Variable<T> &variable)
{ std::cout<<"ResizeBuffer() Forcing Flush for now."<<std::endl;
return HDFSerialWriter::ResizeResult::Flush;};
capsule::STLVector m_HeapBuffer;
profiling::IOChrono m_Profiler;
void InitParameters(const Params ¶meters)
{
std::cout<<"InitParameters(), empty for now. "<<std::endl;
};
//
// from H51Writer
//
std::string GetRankProfilingJSON(
const std::vector<std::string>
&transportsTypes, const std::vector<profiling::IOChrono *> &transportsProfilers)
noexcept
{
std::cout<<"GetRankProfilingJSON() returns empty string now "<<std::endl;
return "";
}
std::string
AggregateProfilingJSON(const std::string &rankProfilingJSON) noexcept
{
std::cout<<"AggregateProfilingJSON() to hdf5"<<std::endl;
return "agg.hd5";
}
void WriteProcessGroupIndex(
const std::string hostLanguage,
const std::vector<std::string> &transportsTypes)
noexcept
{
std::cout<<"WriteProcessGroupIndex() to hdf5"<<std::endl;
}
void Flush() {
std::cout<<"Flush() out hdf5"<<std::endl;
}
template <class T>
void WriteVariableMetadata(const Variable<T> &variable) noexcept
{
std::cout<<"WriteVariableMetadata() to hdf5"<<std::endl;
}
template <class T>
void WriteVariablePayload(const Variable<T> &variable) noexcept
{
std::cout<<"WriteVariablePayload() to hdf5"<<std::endl;
}
};
*/
} // end namespace engine
} // end namespace core
} // end namespace adios2