Newer
Older
* Distributed under the OSI-approved Apache License, Version 2.0. See
* accompanying file Copyright.txt for details.
*
* BPFileWriter.cpp
*
* Created on: Dec 19, 2016
* Author: wfg
*/
#include "engine/bp/BPFileWriter.h"
#include "ADIOS.h"
// supported transports
#include "transport/file/FStream.h"
#include "transport/file/FileDescriptor.h"
#include "transport/file/FilePointer.h"
namespace adios
{
BPFileWriter::BPFileWriter(ADIOS &adios, const std::string name,
const std::string accessMode, MPI_Comm mpiComm,
const Method &method, const IOMode iomode,
const float timeout_sec, const bool debugMode,
const unsigned int nthreads)
: Engine(adios, "BPFileWriter", name, accessMode, mpiComm, method, debugMode,
nthreads, " BPFileWriter constructor (or call to ADIOS Open).\n"),
m_Buffer{capsule::STLVector(accessMode, m_RankMPI, m_DebugMode)},
m_BP1Aggregator{format::BP1Aggregator(m_MPIComm, debugMode)},
m_MaxBufferSize{m_Buffer.m_Data.max_size()}
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
{
m_MetadataSet.TimeStep = 1; // starting at one to be compatible with ADIOS1.x
Init();
}
BPFileWriter::~BPFileWriter() {}
void BPFileWriter::Init()
{
InitParameters();
InitTransports();
InitProcessGroup();
}
void BPFileWriter::Write(Variable<char> &variable, const char *values)
{
WriteVariableCommon(variable, values);
}
void BPFileWriter::Write(Variable<unsigned char> &variable,
const unsigned char *values)
{
WriteVariableCommon(variable, values);
}
void BPFileWriter::Write(Variable<short> &variable, const short *values)
{
WriteVariableCommon(variable, values);
}
void BPFileWriter::Write(Variable<unsigned short> &variable,
const unsigned short *values)
{
WriteVariableCommon(variable, values);
}
void BPFileWriter::Write(Variable<int> &variable, const int *values)
{
WriteVariableCommon(variable, values);
}
void BPFileWriter::Write(Variable<unsigned int> &variable,
const unsigned int *values)
{
WriteVariableCommon(variable, values);
}
void BPFileWriter::Write(Variable<long int> &variable, const long int *values)
{
WriteVariableCommon(variable, values);
}
void BPFileWriter::Write(Variable<unsigned long int> &variable,
const unsigned long int *values)
{
WriteVariableCommon(variable, values);
}
void BPFileWriter::Write(Variable<long long int> &variable,
const long long int *values)
{
WriteVariableCommon(variable, values);
}
void BPFileWriter::Write(Variable<unsigned long long int> &variable,
const unsigned long long int *values)
{
WriteVariableCommon(variable, values);
}
void BPFileWriter::Write(Variable<float> &variable, const float *values)
{
WriteVariableCommon(variable, values);
}
void BPFileWriter::Write(Variable<double> &variable, const double *values)
{
WriteVariableCommon(variable, values);
}
void BPFileWriter::Write(Variable<long double> &variable,
const long double *values)
{
WriteVariableCommon(variable, values);
}
void BPFileWriter::Write(Variable<std::complex<float>> &variable,
const std::complex<float> *values)
{
WriteVariableCommon(variable, values);
}
void BPFileWriter::Write(Variable<std::complex<double>> &variable,
const std::complex<double> *values)
{
WriteVariableCommon(variable, values);
}
void BPFileWriter::Write(Variable<std::complex<long double>> &variable,
const std::complex<long double> *values)
{
WriteVariableCommon(variable, values);
}
void BPFileWriter::Write(VariableCompound &variable, const void *values) {}
// String version
void BPFileWriter::Write(const std::string variableName, const char *values)
{
WriteVariableCommon(m_ADIOS.GetVariable<char>(variableName), values);
}
void BPFileWriter::Write(const std::string variableName,
const unsigned char *values)
{
WriteVariableCommon(m_ADIOS.GetVariable<unsigned char>(variableName), values);
}
void BPFileWriter::Write(const std::string variableName, const short *values)
{
WriteVariableCommon(m_ADIOS.GetVariable<short>(variableName), values);
}
void BPFileWriter::Write(const std::string variableName,
const unsigned short *values)
{
WriteVariableCommon(m_ADIOS.GetVariable<unsigned short>(variableName),
values);
}
void BPFileWriter::Write(const std::string variableName, const int *values)
{
WriteVariableCommon(m_ADIOS.GetVariable<int>(variableName), values);
}
void BPFileWriter::Write(const std::string variableName,
const unsigned int *values)
{
WriteVariableCommon(m_ADIOS.GetVariable<unsigned int>(variableName), values);
}
void BPFileWriter::Write(const std::string variableName, const long int *values)
{
WriteVariableCommon(m_ADIOS.GetVariable<long int>(variableName), values);
}
void BPFileWriter::Write(const std::string variableName,
const unsigned long int *values)
{
WriteVariableCommon(m_ADIOS.GetVariable<unsigned long int>(variableName),
values);
}
void BPFileWriter::Write(const std::string variableName,
const long long int *values)
{
WriteVariableCommon(m_ADIOS.GetVariable<long long int>(variableName), values);
}
void BPFileWriter::Write(const std::string variableName,
const unsigned long long int *values)
{
WriteVariableCommon(m_ADIOS.GetVariable<unsigned long long int>(variableName),
values);
}
void BPFileWriter::Write(const std::string variableName, const float *values)
{
WriteVariableCommon(m_ADIOS.GetVariable<float>(variableName), values);
}
void BPFileWriter::Write(const std::string variableName, const double *values)
{
WriteVariableCommon(m_ADIOS.GetVariable<double>(variableName), values);
}
void BPFileWriter::Write(const std::string variableName,
const long double *values)
{
WriteVariableCommon(m_ADIOS.GetVariable<long double>(variableName), values);
}
void BPFileWriter::Write(const std::string variableName,
const std::complex<float> *values)
{
WriteVariableCommon(m_ADIOS.GetVariable<std::complex<float>>(variableName),
values);
}
void BPFileWriter::Write(const std::string variableName,
const std::complex<double> *values)
{
WriteVariableCommon(m_ADIOS.GetVariable<std::complex<double>>(variableName),
values);
}
void BPFileWriter::Write(const std::string variableName,
const std::complex<long double> *values)
{
WriteVariableCommon(
m_ADIOS.GetVariable<std::complex<long double>>(variableName), values);
}
void BPFileWriter::Write(const std::string variableName,
const void *values) // Compound type
{
}
void BPFileWriter::Advance(float timeout_sec)
{
m_BP1Writer.Advance(m_MetadataSet, m_Buffer);
}
void BPFileWriter::Close(const int transportIndex)
{
CheckTransportIndex(transportIndex);
if (transportIndex == -1)
{
for (auto &transport :
m_Transports) // by reference or value or it doesn't matter?
m_BP1Writer.Close(m_MetadataSet, m_Buffer, *transport, m_IsFirstClose,
false); // false: not using aggregation for now
}
else
{
m_BP1Writer.Close(m_MetadataSet, m_Buffer, *m_Transports[transportIndex],
m_IsFirstClose,
false); // false: not using aggregation for now
}
if (m_MetadataSet.Log.m_IsActive == true)
{
bool allClose = true;
for (auto &transport : m_Transports)
{
if (transport->m_IsOpen == true)
{
allClose = false;
break;
}
}
if (allClose == true) // aggregate and write profiling.log
{
const std::string rankLog = m_BP1Writer.GetRankProfilingLog(
m_RankMPI, m_MetadataSet, m_Transports);
const std::string fileName(m_BP1Writer.GetDirectoryName(m_Name) +
"/profiling.log");
m_BP1Aggregator.WriteProfilingLog(fileName, rankLog);
}
}
}
// PRIVATE FUNCTIONS
void BPFileWriter::InitParameters()
{
auto itGrowthFactor = m_Method.m_Parameters.find("buffer_growth");
if (itGrowthFactor != m_Method.m_Parameters.end())
{
const float growthFactor = std::stof(itGrowthFactor->second);
if (m_DebugMode == true)
{
if (growthFactor == 1.f)
throw std::invalid_argument(
"ERROR: buffer_growth argument can't be less of equal than 1, in " +
m_EndMessage + "\n");
}
m_BP1Writer.m_GrowthFactor = growthFactor;
m_GrowthFactor = growthFactor; // float
}
auto itMaxBufferSize = m_Method.m_Parameters.find("max_size_MB");
if (itMaxBufferSize != m_Method.m_Parameters.end())
{
if (m_DebugMode == true)
{
if (m_GrowthFactor <= 1.f)
throw std::invalid_argument("ERROR: Method buffer_growth argument "
"can't be less of equal than 1, in " +
m_EndMessage + "\n");
}
m_MaxBufferSize = std::stoul(itMaxBufferSize->second) *
1048576; // convert from MB to bytes
}
auto itVerbosity = m_Method.m_Parameters.find("verbose");
if (itVerbosity != m_Method.m_Parameters.end())
{
int verbosity = std::stoi(itVerbosity->second);
if (m_DebugMode == true)
{
if (verbosity < 0 || verbosity > 5)
throw std::invalid_argument("ERROR: Method verbose argument must be an "
"integer in the range [0,5], in call to "
"Open or Engine constructor\n");
}
m_BP1Writer.m_Verbosity = verbosity;
}
auto itProfile = m_Method.m_Parameters.find("profile_units");
if (itProfile != m_Method.m_Parameters.end())
{
auto &profiler = m_MetadataSet.Log;
if (itProfile->second == "mus" || itProfile->second == "microseconds")
profiler.m_Timers.emplace_back("buffering", Support::Resolutions::mus);
else if (itProfile->second == "ms" || itProfile->second == "milliseconds")
profiler.m_Timers.emplace_back("buffering", Support::Resolutions::ms);
else if (itProfile->second == "s" || itProfile->second == "seconds")
profiler.m_Timers.emplace_back("buffering", Support::Resolutions::s);
else if (itProfile->second == "min" || itProfile->second == "minutes")
profiler.m_Timers.emplace_back("buffering", Support::Resolutions::m);
else if (itProfile->second == "h" || itProfile->second == "hours")
profiler.m_Timers.emplace_back("buffering", Support::Resolutions::h);
else
{
if (m_DebugMode == true)
throw std::invalid_argument("ERROR: Method profile_buffering_units "
"argument must be mus, ms, s, min or h, in "
"call to Open or Engine constructor\n");
}
profiler.m_IsActive = true;
}
}
void BPFileWriter::InitTransports()
{
if (m_DebugMode == true)
{
if (TransportNamesUniqueness() == false)
{
throw std::invalid_argument(
"ERROR: two transports of the same kind (e.g file IO) "
"can't have the same name, modify with name= in Method "
"AddTransport\n");
}
}
for (const auto ¶meters : m_Method.m_TransportParameters)
{
auto itProfile = parameters.find("profile_units");
bool doProfiling = false;
Support::Resolutions resolution =
Support::Resolutions::s; // default is seconds
if (itProfile != parameters.end())
{
if (itProfile->second == "mus" || itProfile->second == "microseconds")
resolution = Support::Resolutions::mus;
else if (itProfile->second == "ms" || itProfile->second == "milliseconds")
resolution = Support::Resolutions::ms;
else if (itProfile->second == "s" || itProfile->second == "seconds")
resolution = Support::Resolutions::s;
else if (itProfile->second == "min" || itProfile->second == "minutes")
resolution = Support::Resolutions::m;
else if (itProfile->second == "h" || itProfile->second == "hours")
resolution = Support::Resolutions::h;
else
{
if (m_DebugMode == true)
throw std::invalid_argument("ERROR: Transport profile_units argument "
"must be mus, ms, s, min or h " +
m_EndMessage);
}
doProfiling = true;
}
auto itTransport = parameters.find("transport");
if (itTransport->second == "file" || itTransport->second == "File")
{
auto itLibrary = parameters.find("library");
if (itLibrary == parameters.end() ||
itLibrary->second == "POSIX") // use default POSIX
{
auto file =
std::make_shared<transport::FileDescriptor>(m_MPIComm, m_DebugMode);
if (doProfiling == true)
file->InitProfiler(m_AccessMode, resolution);
m_BP1Writer.OpenRankFiles(m_Name, m_AccessMode, *file);
m_Transports.push_back(std::move(file));
}
else if (itLibrary->second == "FILE*" || itLibrary->second == "stdio")
{
auto file =
std::make_shared<transport::FilePointer>(m_MPIComm, m_DebugMode);
if (doProfiling == true)
file->InitProfiler(m_AccessMode, resolution);
m_BP1Writer.OpenRankFiles(m_Name, m_AccessMode, *file);
m_Transports.push_back(std::move(file));
}
else if (itLibrary->second == "fstream" ||
itLibrary->second == "std::fstream")
{
auto file =
std::make_shared<transport::FStream>(m_MPIComm, m_DebugMode);
if (doProfiling == true)
file->InitProfiler(m_AccessMode, resolution);
m_BP1Writer.OpenRankFiles(m_Name, m_AccessMode, *file);
m_Transports.push_back(std::move(file));
}
else if (itLibrary->second == "MPI_File" || itLibrary->second == "MPI-IO")
{
}
else
{
if (m_DebugMode == true)
throw std::invalid_argument(
"ERROR: file transport library " + itLibrary->second +
" not supported, in " + m_Name + m_EndMessage);
}
}
else
{
if (m_DebugMode == true)
throw std::invalid_argument("ERROR: transport " + itTransport->second +
" (you mean File?) not supported, in " +
m_Name + m_EndMessage);
}
}
}
void BPFileWriter::InitProcessGroup()
{
if (m_MetadataSet.Log.m_IsActive == true)
m_MetadataSet.Log.m_Timers[0].SetInitialTime();
if (m_AccessMode == "a")
{
// Get last pg timestep and update timestep counter in
// format::BP1MetadataSet
}
WriteProcessGroupIndex();
if (m_MetadataSet.Log.m_IsActive == true)
m_MetadataSet.Log.m_Timers[0].SetTime();
}
void BPFileWriter::WriteProcessGroupIndex()
{
// pg = process group
// const std::size_t pgIndexSize = m_BP1Writer.GetProcessGroupIndexSize(
// std::to_string( m_RankMPI ),
// std::to_string(
// m_MetadataSet.TimeStep
// ),
// m_Transports.size()
// );
// metadata
// GrowBuffer( pgIndexSize, m_GrowthFactor, m_MetadataSet.PGIndex );
// data? Need to be careful, maybe add some trailing tolerance in variable
// ????
// GrowBuffer( pgIndexSize, m_GrowthFactor, m_Buffer.m_Data );
const bool isFortran = (m_HostLanguage == "Fortran") ? true : false;
m_BP1Writer.WriteProcessGroupIndex(isFortran, std::to_string(m_RankMPI),
static_cast<std::uint32_t>(m_RankMPI),
m_Transports, m_Buffer, m_MetadataSet);
}
} // end namespace adios