Newer
Older
/*
* Distributed under the OSI-approved Apache License, Version 2.0. See
* accompanying file Copyright.txt for details.
*
* BP1Writer.tcc
*
* Created on: Apr 11, 2017
* Author: wfg
*/
#include "utilities/format/bp1/BP1Writer.h"
namespace adios
{
namespace format
{
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
// PUBLIC
template <class T>
std::size_t BP1Writer::GetVariableIndexSize(const Variable<T> &variable) const
noexcept
{
// size_t indexSize = varEntryLength + memberID + lengthGroupName +
// groupName + lengthVariableName + lengthOfPath + path + datatype
std::size_t indexSize = 23; // without characteristics
indexSize += variable.m_Name.size();
// characteristics 3 and 4, check variable number of dimensions
const std::size_t dimensions =
variable.DimensionsSize(); // commas in CSV + 1
indexSize += 28 * dimensions; // 28 bytes per dimension
indexSize += 1; // id
// characteristics, offset + payload offset in data
indexSize += 2 * (1 + 8);
// characteristic 0, if scalar add value, for now only allowing string
if (dimensions == 1)
{
indexSize += sizeof(T);
indexSize += 1; // id
// must have an if here
indexSize += 2 + variable.m_Name.size();
indexSize += 1; // id
}
// characteristic statistics
if (m_Verbosity == 0) // default, only min and max
{
indexSize += 2 * (sizeof(T) + 1);
indexSize += 1 + 1; // id
}
return indexSize + 12; // extra 12 bytes in case of attributes
// need to add transform characteristics
}
template <class T>
void BP1Writer::WriteVariableMetadata(const Variable<T> &variable,
capsule::STLVector &heap,
BP1MetadataSet &metadataSet) const
noexcept
{
Stats<T> stats = GetStats(variable);
WriteVariableMetadataCommon(variable, stats, heap, metadataSet);
}
template <class T>
void BP1Writer::WriteVariableMetadata(const Variable<std::complex<T>> &variable,
capsule::STLVector &heap,
BP1MetadataSet &metadataSet) const
noexcept
{
Stats<T> stats = GetStats(variable);
WriteVariableMetadataCommon(variable, stats, heap, metadataSet);
}
template <class T>
void BP1Writer::WriteVariablePayload(const Variable<T> &variable,
capsule::STLVector &heap,
const unsigned int nthreads) const noexcept
{
// EXPENSIVE part, might want to use threads if large, serial for now
InsertToBuffer(heap.m_Data, variable.m_AppValues, variable.TotalSize());
heap.m_DataAbsolutePosition += variable.PayLoadSize();
}
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
template <class T>
BP1Writer::Stats<T> BP1Writer::GetStats(const Variable<T> &variable) const
noexcept
{
Stats<T> stats;
const std::size_t valuesSize = variable.TotalSize();
if (m_Verbosity == 0)
{
if (valuesSize >= 10000000) // ten million? this needs actual results
// //we can make decisions for threads
// based on valuesSize
GetMinMax(variable.m_AppValues, valuesSize, stats.Min, stats.Max,
m_Threads); // here we can add cores from constructor
else
GetMinMax(variable.m_AppValues, valuesSize, stats.Min, stats.Max);
}
return stats;
}
template <class T>
BP1Writer::Stats<T>
BP1Writer::GetStats(const Variable<std::complex<T>> &variable) const noexcept
{
Stats<T> stats;
const std::size_t valuesSize = variable.TotalSize();
if (m_Verbosity == 0)
{
// ten million? this needs actual results
// to make decisions for threads usage
if (valuesSize >= 10000000)
{
GetMinMax(variable.m_AppValues, valuesSize, stats.Min, stats.Max,
m_Threads);
}
else
{
GetMinMax(variable.m_AppValues, valuesSize, stats.Min, stats.Max);
}
}
return stats;
}
template <class T>
void BP1Writer::WriteBoundsRecord(const bool isScalar, const Stats<T> &stats,
std::vector<char> &buffer,
std::uint8_t &characteristicsCounter,
const bool addLength) const noexcept
{
if (isScalar == true)
{
WriteCharacteristicRecord(characteristic_value, stats.Min, buffer,
characteristicsCounter,
addLength); // stats.min = stats.max = value
return;
}
if (m_Verbosity == 0) // default verbose
{
WriteCharacteristicRecord(characteristic_min, stats.Min, buffer,
characteristicsCounter, addLength);
WriteCharacteristicRecord(characteristic_max, stats.Max, buffer,
characteristicsCounter, addLength);
}
}
template <class T>
void BP1Writer::WriteCharacteristicRecord(const std::uint8_t characteristicID,
const T &value,
std::vector<char> &buffer,
std::uint8_t &characteristicsCounter,
const bool addLength) const noexcept
{
const std::uint8_t id = characteristicID;
InsertToBuffer(buffer, &id);
if (addLength == true)
{
const std::uint16_t lengthOfCharacteristic = sizeof(T); // id
InsertToBuffer(buffer, &lengthOfCharacteristic);
}
InsertToBuffer(buffer, &value);
++characteristicsCounter;
}
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
template <class T, class U>
void BP1Writer::WriteVariableMetadataCommon(const Variable<T> &variable,
Stats<U> &stats,
capsule::STLVector &heap,
BP1MetadataSet &metadataSet) const
noexcept
{
stats.TimeIndex = metadataSet.TimeStep;
// Get new Index or point to existing index
bool isNew = true; // flag to check if variable is new
BP1Index &varIndex =
GetBP1Index(variable.m_Name, metadataSet.VarsIndices, isNew);
stats.MemberID = varIndex.MemberID;
// write metadata header in data and extract offsets
stats.Offset = heap.m_DataAbsolutePosition;
WriteVariableMetadataInData(variable, stats, heap);
stats.PayloadOffset = heap.m_DataAbsolutePosition;
// write to metadata index
WriteVariableMetadataInIndex(variable, stats, isNew, varIndex);
++metadataSet.DataPGVarsCount;
}
template <class T, class U>
void BP1Writer::WriteVariableMetadataInData(const Variable<T> &variable,
const Stats<U> &stats,
capsule::STLVector &heap) const
noexcept
{
auto &buffer = heap.m_Data;
// for writing length at the end
const std::size_t varLengthPosition = buffer.size();
buffer.insert(buffer.end(), 8, 0); // skip var length (8)
InsertToBuffer(buffer, &stats.MemberID); // memberID
WriteNameRecord(variable.m_Name, buffer); // variable name
buffer.insert(buffer.end(), 2, 0); // skip path
const std::uint8_t dataType = GetDataType<T>(); // dataType
InsertToBuffer(buffer, &dataType);
constexpr char no = 'n'; // isDimension
InsertToBuffer(buffer, &no);
// write variable dimensions
const std::uint8_t dimensions = variable.m_LocalDimensions.size();
InsertToBuffer(buffer, &dimensions); // count
// 27 is from 9 bytes for each: var y/n + local, var y/n + global dimension,
// var y/n + global offset, changed for characteristic
std::uint16_t dimensionsLength = 27 * dimensions;
InsertToBuffer(buffer, &dimensionsLength); // length
WriteDimensionsRecord(buffer, variable.m_LocalDimensions,
variable.m_GlobalDimensions, variable.m_Offsets, 18,
true);
// CHARACTERISTICS
WriteVariableCharacteristics(variable, stats, buffer, true);
// Back to varLength including payload size
const std::uint64_t varLength = buffer.size() - varLengthPosition +
variable.PayLoadSize() -
8; // remove its own size
CopyToBufferPosition(buffer, varLengthPosition, &varLength); // length
heap.m_DataAbsolutePosition +=
buffer.size() - varLengthPosition; // update absolute position to be
// used as payload position
}
template <class T, class U>
void BP1Writer::WriteVariableMetadataInIndex(const Variable<T> &variable,
const Stats<U> &stats,
const bool isNew,
BP1Index &index) const noexcept
{
auto &buffer = index.Buffer;
if (isNew == true) // write variable header (might be shared with
// attributes index)
{
buffer.insert(buffer.end(), 4, 0); // skip var length (4)
InsertToBuffer(buffer, &stats.MemberID);
buffer.insert(buffer.end(), 2, 0); // skip group name
WriteNameRecord(variable.m_Name, buffer);
buffer.insert(buffer.end(), 2, 0); // skip path
const std::uint8_t dataType = GetDataType<T>();
InsertToBuffer(buffer, &dataType);
// Characteristics Sets Count in Metadata
index.Count = 1;
InsertToBuffer(buffer, &index.Count);
}
else // update characteristics sets count
{
const std::size_t characteristicsSetsCountPosition =
15 + variable.m_Name.size();
++index.Count;
CopyToBufferPosition(buffer, characteristicsSetsCountPosition,
&index.Count); // test
}
WriteVariableCharacteristics(variable, stats, buffer);
}
template <class T, class U>
void BP1Writer::WriteVariableCharacteristics(const Variable<T> &variable,
const Stats<U> &stats,
std::vector<char> &buffer,
const bool addLength) const
noexcept
{
const std::size_t characteristicsCountPosition =
buffer.size(); // very important to track as writer is going back to
// this position
buffer.insert(buffer.end(), 5,
0); // skip characteristics count(1) + length (4)
std::uint8_t characteristicsCounter = 0;
// DIMENSIONS
std::uint8_t characteristicID = characteristic_dimensions;
InsertToBuffer(buffer, &characteristicID);
const std::uint8_t dimensions = variable.m_LocalDimensions.size();
if (addLength == true)
{
const std::int16_t lengthOfDimensionsCharacteristic =
24 * dimensions + 3; // 24 = 3 local, global, offset x 8 bytes/each
InsertToBuffer(buffer, &lengthOfDimensionsCharacteristic);
}
InsertToBuffer(buffer, &dimensions); // count
const std::uint16_t dimensionsLength = 24 * dimensions;
InsertToBuffer(buffer, &dimensionsLength); // length
WriteDimensionsRecord(buffer, variable.m_LocalDimensions,
variable.m_GlobalDimensions, variable.m_Offsets, 16,
addLength);
++characteristicsCounter;
// VALUE for SCALAR or STAT min, max for ARRAY
WriteBoundsRecord(variable.m_IsScalar, stats, buffer,
characteristicsCounter, addLength);
// TIME INDEX
WriteCharacteristicRecord(characteristic_time_index, stats.TimeIndex,
buffer, characteristicsCounter, addLength);
if (addLength == false) // only in metadata offset and payload offset
{
WriteCharacteristicRecord(characteristic_offset, stats.Offset, buffer,
characteristicsCounter);
WriteCharacteristicRecord(characteristic_payload_offset,
stats.PayloadOffset, buffer,
characteristicsCounter);
}
// END OF CHARACTERISTICS
// Back to characteristics count and length
CopyToBufferPosition(buffer, characteristicsCountPosition,
&characteristicsCounter); // count (1)
const std::uint32_t characteristicsLength =
buffer.size() - characteristicsCountPosition - 4 -
1; // remove its own length (4) + characteristic counter (1)
CopyToBufferPosition(buffer, characteristicsCountPosition + 1,
&characteristicsLength); // length
}
} // end namespace format
} // end namespace