Newer
Older
* Distributed under the OSI-approved Apache License, Version 2.0. See
* accompanying file Copyright.txt for details.
*
* BP1Writer.cpp
*
* Created on: Feb 1, 2017
* Author: wfg
*/
/// \cond EXCLUDE_FROM_DOXYGEN
#include "utilities/format/bp1/BP1Writer.h"
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
/// \endcond
namespace adios
{
namespace format
{
std::size_t BP1Writer::GetProcessGroupIndexSize(
const std::string name, const std::string timeStepName,
const std::size_t numberOfTransports) const noexcept
{
// pgIndex + list of methods (transports)
return (name.length() + timeStepName.length() + 23) +
(3 + numberOfTransports); // should be sufficient for data and metadata
// pgindices
}
void BP1Writer::WriteProcessGroupIndex(
const bool isFortran, const std::string name, const std::uint32_t processID,
const std::vector<std::shared_ptr<Transport>> &transports,
capsule::STLVector &heap, BP1MetadataSet &metadataSet) const noexcept
{
std::vector<char> &metadataBuffer = metadataSet.PGIndex.Buffer;
std::vector<char> &dataBuffer = heap.m_Data;
metadataSet.DataPGLengthPosition = dataBuffer.size();
dataBuffer.insert(dataBuffer.end(), 8, 0); // skip pg length (8)
const std::size_t metadataPGLengthPosition = metadataBuffer.size();
metadataBuffer.insert(metadataBuffer.end(), 2, 0); // skip pg length (2)
// write name to metadata
WriteNameRecord(name, metadataBuffer);
// write if host language Fortran in metadata and data
const char hostFortran =
(isFortran) ? 'y' : 'n'; // if host language is fortran
CopyToBuffer(metadataBuffer, &hostFortran);
CopyToBuffer(dataBuffer, &hostFortran);
// write name in data
WriteNameRecord(name, dataBuffer);
// processID in metadata,
CopyToBuffer(metadataBuffer, &processID);
// skip coordination var in data ....what is coordination var?
dataBuffer.insert(dataBuffer.end(), 4, 0);
// time step name to metadata and data
const std::string timeStepName(std::to_string(metadataSet.TimeStep));
WriteNameRecord(timeStepName, metadataBuffer);
WriteNameRecord(timeStepName, dataBuffer);
// time step to metadata and data
CopyToBuffer(metadataBuffer, &metadataSet.TimeStep);
CopyToBuffer(dataBuffer, &metadataSet.TimeStep);
// offset to pg in data in metadata which is the current absolute position
CopyToBuffer(metadataBuffer,
reinterpret_cast<std::uint64_t *>(&heap.m_DataAbsolutePosition));
// Back to writing metadata pg index length (length of group)
const std::uint16_t metadataPGIndexLength =
metadataBuffer.size() - metadataPGLengthPosition -
2; // without length of group record
CopyToBuffer(metadataBuffer, metadataPGLengthPosition,
&metadataPGIndexLength);
// DONE With metadataBuffer
// here write method in data
const std::vector<std::uint8_t> methodIDs = GetMethodIDs(transports);
const std::uint8_t methodsCount = methodIDs.size();
CopyToBuffer(dataBuffer, &methodsCount); // count
const std::uint16_t methodsLength =
methodIDs.size() *
3; // methodID (1) + method params length(2), no parameters for now
CopyToBuffer(dataBuffer, &methodsLength); // length
for (const auto methodID : methodIDs)
{
CopyToBuffer(dataBuffer, &methodID); // method ID,
dataBuffer.insert(dataBuffer.end(), 2,
0); // skip method params length = 0 (2 bytes) for now
}
// update absolute position
heap.m_DataAbsolutePosition +=
dataBuffer.size() - metadataSet.DataPGLengthPosition;
// pg vars count and position
metadataSet.DataPGVarsCount = 0;
metadataSet.DataPGVarsCountPosition = dataBuffer.size();
// add vars count and length
dataBuffer.insert(dataBuffer.end(), 12, 0);
heap.m_DataAbsolutePosition += 12; // add vars count and length
++metadataSet.DataPGCount;
metadataSet.DataPGIsOpen = true;
}
void BP1Writer::Advance(BP1MetadataSet &metadataSet, capsule::STLVector &buffer)
{
FlattenData(metadataSet, buffer);
}
void BP1Writer::Close(BP1MetadataSet &metadataSet, capsule::STLVector &heap,
Transport &transport, bool &isFirstClose,
const bool doAggregation) const noexcept
{
if (metadataSet.Log.IsActive == true)
metadataSet.Log.Timers[0].SetInitialTime();
if (isFirstClose == true)
{
if (metadataSet.DataPGIsOpen == true)
FlattenData(metadataSet, heap);
FlattenMetadata(metadataSet, heap);
if (metadataSet.Log.IsActive == true)
metadataSet.Log.Timers[0].SetInitialTime();
if (doAggregation == true) // N-to-M where 1 <= M <= N-1, might need a new
// Log metadataSet.Log.m_Timers just for
// aggregation
{
// here call aggregator
}
isFirstClose = false;
}
if (doAggregation == true) // N-to-M where 1 <= M <= N-1
{
// here call aggregator to select transports for Write and Close
}
else // N-to-N
{
transport.Write(heap.m_Data.data(), heap.m_Data.size()); // single write
transport.Close();
}
}
std::string BP1Writer::GetRankProfilingLog(
const int rank, const BP1MetadataSet &metadataSet,
const std::vector<std::shared_ptr<Transport>> &transports) const noexcept
{
auto lf_WriterTimer = [](std::string &rankLog,
const profiling::Timer &timer) {
rankLog += "'" + timer.m_Process + "_" + timer.GetUnits() + "': " +
std::to_string(timer.m_ProcessTime) + ", ";
};
// prepare string dictionary per rank
std::string rankLog("'rank_" + std::to_string(rank) + "': { ");
auto &profiler = metadataSet.Log;
rankLog += "'bytes': " + std::to_string(profiler.TotalBytes[0]) + ", ";
lf_WriterTimer(rankLog, profiler.Timers[0]);
for (unsigned int t = 0; t < transports.size(); ++t)
{
auto &timers = transports[t]->m_Profiler.Timers;
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
rankLog += "'transport_" + std::to_string(t) + "': { ";
rankLog += "'lib': " + transports[t]->m_Type + ", ";
for (unsigned int i = 0; i < 3; ++i)
lf_WriterTimer(rankLog, timers[i]);
rankLog += "}, ";
}
rankLog += "}, ";
return rankLog;
}
// PRIVATE FUNCTIONS
void BP1Writer::WriteDimensionsRecord(
std::vector<char> &buffer, const std::vector<std::size_t> &localDimensions,
const std::vector<std::size_t> &globalDimensions,
const std::vector<std::size_t> &globalOffsets, const unsigned int skip,
const bool addType) const noexcept
{
auto lf_WriteFlaggedDim = [](std::vector<char> &buffer, const char no,
const std::size_t dimension) {
CopyToBuffer(buffer, &no);
CopyToBuffer(buffer, reinterpret_cast<const std::uint64_t *>(&dimension));
};
// BODY Starts here
if (globalDimensions.empty())
{
if (addType == true)
{
constexpr char no = 'n'; // dimension format unsigned int value (not using
// memberID for now)
for (const auto &localDimension : localDimensions)
{
lf_WriteFlaggedDim(buffer, no, localDimension);
buffer.insert(buffer.end(), skip, 0);
}
}
else
{
for (const auto &localDimension : localDimensions)
{
CopyToBuffer(buffer,
reinterpret_cast<const std::uint64_t *>(&localDimension));
buffer.insert(buffer.end(), skip, 0);
}
}
}
else
{
if (addType == true)
{
constexpr char no = 'n'; // dimension format unsigned int value for now
for (unsigned int d = 0; d < localDimensions.size(); ++d)
{
lf_WriteFlaggedDim(buffer, no, localDimensions[d]);
lf_WriteFlaggedDim(buffer, no, globalDimensions[d]);
lf_WriteFlaggedDim(buffer, no, globalOffsets[d]);
}
}
else
{
for (unsigned int d = 0; d < localDimensions.size(); ++d)
{
CopyToBuffer(buffer, reinterpret_cast<const std::uint64_t *>(
&localDimensions[d]));
CopyToBuffer(buffer, reinterpret_cast<const std::uint64_t *>(
&globalDimensions[d]));
CopyToBuffer(
buffer, reinterpret_cast<const std::uint64_t *>(&globalOffsets[d]));
}
}
}
}
void BP1Writer::WriteNameRecord(const std::string name,
std::vector<char> &buffer) const noexcept
{
const std::uint16_t length = name.length();
CopyToBuffer(buffer, &length);
CopyToBuffer(buffer, name.c_str(), length);
}
BP1Index &
BP1Writer::GetBP1Index(const std::string name,
std::unordered_map<std::string, BP1Index> &indices,
bool &isNew) const noexcept
{
auto itName = indices.find(name);
if (itName == indices.end())
{
indices.emplace(name, BP1Index(indices.size()));
isNew = true;
return indices.at(name);
}
isNew = false;
return itName->second;
}
void BP1Writer::FlattenData(BP1MetadataSet &metadataSet,
capsule::STLVector &heap) const noexcept
{
auto &buffer = heap.m_Data;
// vars count and Length (only for PG)
CopyToBuffer(buffer, metadataSet.DataPGVarsCountPosition,
&metadataSet.DataPGVarsCount);
const std::uint64_t varsLength = buffer.size() -
metadataSet.DataPGVarsCountPosition - 8 -
4; // without record itself and vars count
CopyToBuffer(buffer, metadataSet.DataPGVarsCountPosition + 4, &varsLength);
// attributes (empty for now) count (4) and length (8) are zero by moving
// positions in time step zero
buffer.insert(buffer.end(), 12, 0);
heap.m_DataAbsolutePosition += 12;
// Finish writing pg group length
const std::uint64_t dataPGLength =
buffer.size() - metadataSet.DataPGLengthPosition -
8; // without record itself, 12 due to empty attributes
CopyToBuffer(buffer, metadataSet.DataPGLengthPosition, &dataPGLength);
++metadataSet.TimeStep;
metadataSet.DataPGIsOpen = false;
}
void BP1Writer::FlattenMetadata(BP1MetadataSet &metadataSet,
capsule::STLVector &heap) const noexcept
{
auto lf_IndexCountLength =
[](std::unordered_map<std::string, BP1Index> &indices,
std::uint32_t &count, std::uint64_t &length) {
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
count = indices.size();
length = 0;
for (auto &indexPair : indices) // set each index length
{
auto &indexBuffer = indexPair.second.Buffer;
const std::uint32_t indexLength = indexBuffer.size() - 4;
CopyToBuffer(indexBuffer, 0, &indexLength);
length += indexBuffer.size(); // overall length
}
};
auto lf_FlattenIndices =
[](const std::uint32_t count, const std::uint64_t length,
const std::unordered_map<std::string, BP1Index> &indices,
std::vector<char> &buffer) {
CopyToBuffer(buffer, &count);
CopyToBuffer(buffer, &length);
for (const auto &indexPair : indices) // set each index length
{
const auto &indexBuffer = indexPair.second.Buffer;
CopyToBuffer(buffer, indexBuffer.data(), indexBuffer.size());
}
};
// Finish writing metadata counts and lengths
// PG Index
const std::uint64_t pgCount = metadataSet.DataPGCount;
const std::uint64_t pgLength = metadataSet.PGIndex.Buffer.size();
// var index count and length (total), and each index length
std::uint32_t varsCount;
std::uint64_t varsLength;
lf_IndexCountLength(metadataSet.VarsIndices, varsCount, varsLength);
// attribute index count and length, and each index length
std::uint32_t attributesCount;
std::uint64_t attributesLength;
lf_IndexCountLength(metadataSet.AttributesIndices, attributesCount,
attributesLength);
const std::size_t footerSize = (pgLength + 16) + (varsLength + 12) +
(attributesLength + 12) +
metadataSet.MiniFooterSize;
auto &buffer = heap.m_Data;
buffer.reserve(buffer.size() + footerSize); // reserve data to fit metadata,
// must replace with growth buffer
// strategy
// write pg index
CopyToBuffer(buffer, &pgCount);
CopyToBuffer(buffer, &pgLength);
CopyToBuffer(buffer, metadataSet.PGIndex.Buffer.data(), pgLength);
// Vars indices
lf_FlattenIndices(varsCount, varsLength, metadataSet.VarsIndices, buffer);
// Attribute indices
lf_FlattenIndices(attributesCount, attributesLength,
metadataSet.AttributesIndices, buffer);
// getting absolute offsets, minifooter is 28 bytes for now
const std::uint64_t offsetPGIndex = heap.m_DataAbsolutePosition;
const std::uint64_t offsetVarsIndex = offsetPGIndex + (pgLength + 16);
const std::uint64_t offsetAttributeIndex =
offsetVarsIndex + (varsLength + 12);
CopyToBuffer(buffer, &offsetPGIndex);
CopyToBuffer(buffer, &offsetVarsIndex);
CopyToBuffer(buffer, &offsetAttributeIndex);
// version
if (IsLittleEndian())
{
const std::uint8_t endian = 0;
CopyToBuffer(buffer, &endian);
buffer.insert(buffer.end(), 2, 0);
CopyToBuffer(buffer, &m_Version);
}
else
{
}
heap.m_DataAbsolutePosition += footerSize;
if (metadataSet.Log.IsActive == true)
metadataSet.Log.TotalBytes.push_back(heap.m_DataAbsolutePosition);
}
} // end namespace format
} // end namespace adios