forked from ornladios/ADIOS2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIO.cpp
More file actions
1168 lines (1036 loc) · 38.8 KB
/
Copy pathIO.cpp
File metadata and controls
1168 lines (1036 loc) · 38.8 KB
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* SPDX-FileCopyrightText: 2026 Oak Ridge National Laboratory and Contributors
*
* SPDX-License-Identifier: Apache-2.0
*/
#include "IO.h"
#include "IO.tcc"
#include <cstdlib>
#include <iostream>
#include <memory>
#include <mutex>
#include <sstream>
#include <stdexcept>
#include <utility> // std::pair
#include <vector>
#include "adios2/common/ADIOSMacros.h"
#include "adios2/engine/bp3/BP3Reader.h"
#include "adios2/engine/bp3/BP3Writer.h"
#include "adios2/engine/bp4/BP4Reader.h"
#include "adios2/engine/bp4/BP4Writer.h"
#include "adios2/engine/bp5/BP5Reader.h"
#include "adios2/engine/bp5/BP5Writer.h"
#include "adios2/engine/inline/InlineReader.h"
#include "adios2/engine/inline/InlineWriter.h"
#include "adios2/engine/mhs/MhsReader.h"
#include "adios2/engine/mhs/MhsWriter.h"
#include "adios2/engine/null/NullReader.h"
#include "adios2/engine/null/NullWriter.h"
#include "adios2/engine/plugin/PluginEngine.h"
#include "adios2/engine/skeleton/SkeletonReader.h"
#include "adios2/engine/skeleton/SkeletonWriter.h"
#include "adios2/engine/timeseries/TimeSeriesReader.h"
#include "adios2/helper/adiosComm.h"
#include "adios2/helper/adiosCommDummy.h"
#include "adios2/helper/adiosFunctions.h" //BuildParametersMap
#include "adios2/helper/adiosString.h"
#include <adios2sys/SystemTools.hxx> // FileIsDirectory()
#ifdef ADIOS2_HAVE_DATAMAN // external dependencies
#include "adios2/engine/dataman/DataManReader.h"
#include "adios2/engine/dataman/DataManWriter.h"
#endif
#ifdef ADIOS2_HAVE_SST // external dependencies
#include "adios2/engine/sst/SstReader.h"
#include "adios2/engine/sst/SstWriter.h"
#endif
#ifdef ADIOS2_HAVE_DAOS // external dependencies
#include "adios2/engine/daos/DaosReader.h"
#include "adios2/engine/daos/DaosWriter.h"
#endif
#ifdef ADIOS2_HAVE_CAMPAIGN // external dependencies
#include "adios2/engine/campaign/CampaignReader.h"
#endif
namespace adios2
{
namespace core
{
IO::EngineFactoryEntry IO_MakeEngine_HDF5();
namespace
{
std::unordered_map<std::string, IO::EngineFactoryEntry> Factory = {
{"bp3", {IO::MakeEngine<engine::BP3Reader>, IO::MakeEngine<engine::BP3Writer>}},
{"bp4",
{IO::MakeEngine<engine::BP4Reader>, IO::MakeEngine<engine::BP4Writer>,
IO::MakeEngineWithMD<engine::BP4Reader>}},
{"bp5",
#ifdef ADIOS2_HAVE_BP5
{IO::MakeEngine<engine::BP5Reader>, IO::MakeEngine<engine::BP5Writer>,
IO::MakeEngineWithMD<engine::BP5Reader>}
#else
IO::NoEngineEntry("ERROR: this version didn't compile with "
"BP5 library, can't use BP5 engine\n")
#endif
},
{"dataman",
#ifdef ADIOS2_HAVE_DATAMAN
{IO::MakeEngine<engine::DataManReader>, IO::MakeEngine<engine::DataManWriter>}
#else
IO::NoEngineEntry("ERROR: this version didn't compile with "
"DataMan library, can't use DataMan engine\n")
#endif
},
{"ssc", IO::NoEngineEntry("ERROR: this version didn't compile with "
"SSC library, can't use SSC engine\n")},
{"mhs",
#ifdef ADIOS2_HAVE_MHS
{IO::MakeEngine<engine::MhsReader>, IO::MakeEngine<engine::MhsWriter>}
#else
IO::NoEngineEntry("ERROR: this version didn't compile with "
"MHS library, can't use MHS engine\n")
#endif
},
{"sst",
#ifdef ADIOS2_HAVE_SST
{IO::MakeEngine<engine::SstReader>, IO::MakeEngine<engine::SstWriter>}
#else
IO::NoEngineEntry("ERROR: this version didn't compile with "
"Sst library, can't use Sst engine\n")
#endif
},
{"daos",
#ifdef ADIOS2_HAVE_DAOS
{IO::MakeEngine<engine::DaosReader>, IO::MakeEngine<engine::DaosWriter>}
#else
IO::NoEngineEntry("ERROR: this version didn't compile with "
"DAOS library, can't use DAOS engine\n")
#endif
},
{"effis",
#ifdef ADIOS2_HAVE_SST
{IO::MakeEngine<engine::SstReader>, IO::MakeEngine<engine::SstWriter>}
#else
IO::NoEngineEntry("ERROR: this version didn't compile with "
"Sst library, can't use Sst engine\n")
#endif
},
{"dataspaces", IO::NoEngineEntry("ERROR: this version didn't compile with "
"DataSpaces library, can't use DataSpaces engine\n")},
{"hdf5",
#ifdef ADIOS2_HAVE_HDF5
IO_MakeEngine_HDF5()
#else
IO::NoEngineEntry("ERROR: this version didn't compile with "
"HDF5 library, can't use HDF5 engine\n")
#endif
},
{"skeleton", {IO::MakeEngine<engine::SkeletonReader>, IO::MakeEngine<engine::SkeletonWriter>}},
{"inline", {IO::MakeEngine<engine::InlineReader>, IO::MakeEngine<engine::InlineWriter>}},
{"null", {IO::MakeEngine<engine::NullReader>, IO::MakeEngine<engine::NullWriter>}},
{"nullcore",
{IO::NoEngine("ERROR: nullcore engine does not support read mode"),
IO::MakeEngine<engine::NullWriter>}},
{"plugin", {IO::MakeEngine<plugin::PluginEngine>, IO::MakeEngine<plugin::PluginEngine>}},
{"timeseries",
{IO::MakeEngine<engine::TimeSeriesReader>,
IO::NoEngine("ERROR: campaign engine does not support write mode")}},
{"campaign",
#ifdef ADIOS2_HAVE_CAMPAIGN
{IO::MakeEngine<engine::CampaignReader>,
IO::NoEngine("ERROR: campaign engine does not support write mode")}
#else
IO::NoEngineEntry("ERROR: this version didn't compile with "
"support for campaign management, can't use Campaign engine\n")
#endif
},
};
const std::unordered_map<std::string, bool> ReadRandomAccess_Supported = {
{"bp3", false}, {"bp4", false}, {"bp5", true}, {"dataman", false},
{"ssc", false}, {"mhs", false}, {"sst", false}, {"daos", false},
{"effis", false}, {"dataspaces", false}, {"hdf5", false}, {"skeleton", true},
{"inline", false}, {"null", true}, {"nullcore", true}, {"plugin", false},
{"campaign", true}, {"timeseries", true},
};
// Synchronize access to the factory in case one thread is
// looking up while another registers additional entries.
std::mutex FactoryMutex;
std::unordered_map<std::string, IO::EngineFactoryEntry>::const_iterator
FactoryLookup(std::string const &name)
{
std::lock_guard<std::mutex> factoryGuard(FactoryMutex);
(void)factoryGuard;
return Factory.find(name);
}
struct ThrowError
{
std::shared_ptr<Engine> operator()(IO &, const std::string &, const Mode, helper::Comm) const
{
helper::Throw<std::invalid_argument>("Core", "IO", "Operator", Err);
return nullptr;
}
std::string Err;
};
} // end anonymous namespace
IO::MakeEngineFunc IO::NoEngine(std::string e) { return ThrowError{e}; }
IO::EngineFactoryEntry IO::NoEngineEntry(std::string e) { return {NoEngine(e), NoEngine(e)}; }
void IO::RegisterEngine(const std::string &engineType, EngineFactoryEntry entry)
{
std::lock_guard<std::mutex> factoryGuard(FactoryMutex);
Factory[engineType] = std::move(entry);
}
IO::IO(ADIOS &adios, const std::string name, const bool inConfigFile,
const std::string hostLanguage)
: m_ADIOS(adios), m_Name(name), m_HostLanguage(hostLanguage), m_InConfigFile(inConfigFile)
{
}
IO::~IO() = default;
void IO::SetEngine(const std::string engineType) noexcept
{
auto lf_InsertParam = [&](const std::string &key, const std::string &value) {
m_Parameters.insert(std::pair<std::string, std::string>(key, value));
};
/* First step in handling virtual engine names */
std::string finalEngineType;
std::string engineTypeLC = engineType;
std::transform(engineTypeLC.begin(), engineTypeLC.end(), engineTypeLC.begin(), ::tolower);
if (engineTypeLC == "insituviz" || engineTypeLC == "insituvisualization")
{
finalEngineType = "SST";
lf_InsertParam("FirstTimestepPrecious", "true");
lf_InsertParam("RendezvousReaderCount", "0");
lf_InsertParam("QueueLimit", "3");
lf_InsertParam("QueueFullPolicy", "Discard");
lf_InsertParam("AlwaysProvideLatestTimestep", "false");
}
else if (engineTypeLC == "insituanalysis")
{
finalEngineType = "SST";
lf_InsertParam("FirstTimestepPrecious", "false");
lf_InsertParam("RendezvousReaderCount", "1");
lf_InsertParam("QueueLimit", "1");
lf_InsertParam("QueueFullPolicy", "Block");
lf_InsertParam("AlwaysProvideLatestTimestep", "false");
}
else if (engineTypeLC == "codecoupling")
{
finalEngineType = "SST";
lf_InsertParam("FirstTimestepPrecious", "false");
lf_InsertParam("RendezvousReaderCount", "1");
lf_InsertParam("QueueLimit", "1");
lf_InsertParam("QueueFullPolicy", "Block");
lf_InsertParam("AlwaysProvideLatestTimestep", "false");
}
else if (engineTypeLC == "filestream")
{
finalEngineType = "filestream";
lf_InsertParam("OpenTimeoutSecs", "3600");
lf_InsertParam("StreamReader", "true");
}
/* "file" is handled entirely in IO::Open() as it needs the name */
else
{
finalEngineType = engineType;
}
m_EngineType = finalEngineType;
}
void IO::SetIOMode(const IOMode ioMode) { m_IOMode = ioMode; }
void IO::SetParameters(const Params ¶meters) noexcept
{
PERFSTUBS_SCOPED_TIMER("IO::other");
for (const auto ¶meter : parameters)
{
m_Parameters[parameter.first] = parameter.second;
}
}
void IO::SetParameters(const std::string ¶meters)
{
PERFSTUBS_SCOPED_TIMER("IO::other");
adios2::Params parameterMap = adios2::helper::BuildParametersMap(parameters, '=', ',');
SetParameters(parameterMap);
}
void IO::SetParameter(const std::string key, const std::string value) noexcept
{
PERFSTUBS_SCOPED_TIMER("IO::other");
m_Parameters[key] = value;
}
Params &IO::GetParameters() noexcept { return m_Parameters; }
void IO::ClearParameters() noexcept
{
PERFSTUBS_SCOPED_TIMER("IO::other");
m_Parameters.clear();
}
size_t IO::AddTransport(const std::string type, const Params ¶meters)
{
PERFSTUBS_SCOPED_TIMER("IO::other");
Params parametersMap(parameters);
if (parameters.count("transport") == 1 || parameters.count("Transport") == 1)
{
helper::Throw<std::invalid_argument>(
"Core", "IO", "AddTransport",
"key Transport (or transport) is not allowed in transport "
"parameters");
}
CheckTransportType(type);
parametersMap["transport"] = type;
m_TransportsParameters.push_back(parametersMap);
return m_TransportsParameters.size() - 1;
}
void IO::SetTransportParameter(const size_t transportIndex, const std::string key,
const std::string value)
{
PERFSTUBS_SCOPED_TIMER("IO::other");
if (transportIndex >= m_TransportsParameters.size())
{
helper::Throw<std::invalid_argument>("Core", "IO", "SetTransportParameter",
"transport Index " + std::to_string(transportIndex) +
" does not exist");
}
m_TransportsParameters[transportIndex][key] = value;
}
const VarMap &IO::GetVariables() const noexcept { return m_Variables; }
#ifdef ADIOS2_HAVE_DERIVED_VARIABLE
const VarMap &IO::GetDerivedVariables() const noexcept { return m_VariablesDerived; }
#endif
const AttrMap &IO::GetAttributes() const noexcept { return m_Attributes; }
bool IO::InConfigFile() const noexcept { return m_InConfigFile; }
void IO::SetDeclared() noexcept { m_IsDeclared = true; }
void IO::SetArrayOrder(const ArrayOrdering ArrayOrder) noexcept
{
if (ArrayOrder == ArrayOrdering::Auto)
{
if (helper::IsRowMajor(m_HostLanguage))
m_ArrayOrder = ArrayOrdering::RowMajor;
else
m_ArrayOrder = ArrayOrdering::ColumnMajor;
}
else
m_ArrayOrder = ArrayOrder;
}
bool IO::IsDeclared() const noexcept { return m_IsDeclared; }
bool IO::RemoveVariable(const std::string &name) noexcept
{
PERFSTUBS_SCOPED_TIMER("IO::RemoveVariable");
bool isRemoved = false;
auto itVariable = m_Variables.find(name);
// variable exists
if (itVariable != m_Variables.end())
{
m_Variables.erase(itVariable);
isRemoved = true;
}
return isRemoved;
}
#ifdef ADIOS2_HAVE_DERIVED_VARIABLE
bool IO::RemoveDerivedVariable(const std::string &name) noexcept
{
PERFSTUBS_SCOPED_TIMER("IO::RemoveVariable");
bool isRemoved = false;
auto itVariable = m_VariablesDerived.find(name);
// variable exists
if (itVariable != m_VariablesDerived.end())
{
m_VariablesDerived.erase(itVariable);
isRemoved = true;
}
return isRemoved;
}
#endif
void IO::RemoveAllVariables() noexcept
{
PERFSTUBS_SCOPED_TIMER("IO::RemoveAllVariables");
m_Variables.clear();
}
bool IO::RemoveAttribute(const std::string &name) noexcept
{
PERFSTUBS_SCOPED_TIMER("IO::RemoveAttribute");
bool isRemoved = false;
auto itAttribute = m_Attributes.find(name);
// attribute exists
if (itAttribute != m_Attributes.end())
{
// first remove the Attribute object
const DataType type(itAttribute->second->m_Type);
if (type == DataType::None)
{
// nothing to do
}
else
{
m_Attributes.erase(itAttribute);
isRemoved = true;
}
}
return isRemoved;
}
void IO::RemoveAllAttributes() noexcept
{
PERFSTUBS_SCOPED_TIMER("IO::RemoveAllAttributes");
m_Attributes.clear();
}
std::map<std::string, Params> IO::GetAvailableVariables(const std::set<std::string> &keys) noexcept
{
PERFSTUBS_SCOPED_TIMER("IO::GetAvailableVariables");
std::map<std::string, Params> variablesInfo;
for (const auto &variablePair : m_Variables)
{
const std::string variableName = variablePair.first;
const DataType type = InquireVariableType(variableName);
if (type == DataType::Struct)
{
}
#define declare_template_instantiation(T) \
else if (type == helper::GetDataType<T>()) \
{ \
variablesInfo[variableName] = GetVariableInfo<T>(variableName, keys); \
}
ADIOS2_FOREACH_STDTYPE_1ARG(declare_template_instantiation)
#undef declare_template_instantiation
}
return variablesInfo;
}
std::map<std::string, Params> IO::GetAvailableAttributes(const std::string &variableName,
const std::string separator,
const bool fullNameKeys) noexcept
{
PERFSTUBS_SCOPED_TIMER("IO::GetAvailableAttributes");
std::map<std::string, Params> attributesInfo;
if (!variableName.empty())
{
auto itVariable = m_Variables.find(variableName);
const DataType type = InquireVariableType(itVariable);
if (type == DataType::Struct)
{
}
else
{
attributesInfo = itVariable->second->GetAttributesInfo(*this, separator, fullNameKeys);
}
return attributesInfo;
}
// return all attributes if variable name is empty
for (const auto &attributePair : m_Attributes)
{
const std::string &name = attributePair.first;
if (attributePair.second->m_Type == DataType::Struct)
{
}
else
{
attributesInfo[name] = attributePair.second->GetInfo();
}
}
return attributesInfo;
}
DataType IO::InquireVariableType(const std::string &name) const noexcept
{
PERFSTUBS_SCOPED_TIMER("IO::other");
auto itVariable = m_Variables.find(name);
return InquireVariableType(itVariable);
}
DataType IO::InquireVariableType(const VarMap::const_iterator itVariable) const noexcept
{
if (itVariable == m_Variables.end())
{
return DataType::None;
}
const DataType type = itVariable->second->m_Type;
if (m_ReadStreaming)
{
if (type == DataType::Struct)
{
}
else
{
if (!itVariable->second->IsValidStep(m_EngineStep + 1))
{
return DataType::None;
}
}
}
return type;
}
DataType IO::InquireAttributeType(const std::string &name, const std::string &variableName,
const std::string separator) const noexcept
{
PERFSTUBS_SCOPED_TIMER("IO::other");
const std::string globalName = helper::GlobalName(name, variableName, separator);
auto itAttribute = m_Attributes.find(globalName);
if (itAttribute == m_Attributes.end())
{
return DataType::None;
}
return itAttribute->second->m_Type;
}
void IO::AddOperation(const std::string &variable, const std::string &operatorType,
const Params ¶meters) noexcept
{
PERFSTUBS_SCOPED_TIMER("IO::other");
m_VarOpsPlaceholder[variable].push_back({operatorType, parameters});
}
Engine &IO::Open(const std::string &name, const Mode mode, helper::Comm comm, const char *md,
const size_t mdsize)
{
auto lf_GuessEngineFromTAR = [&](const std::string tarinfo) -> std::string {
std::string engineType;
if (tarinfo.find("mmd.0") != std::string::npos)
engineType = "bp5";
else if (tarinfo.find("md.idx") != std::string::npos)
engineType = "bp4";
else if (tarinfo.find(".h5") != std::string::npos ||
tarinfo.find(".hdf5") != std::string::npos ||
tarinfo.find(".nc") != std::string::npos)
engineType = "h5";
else
helper::Throw<std::runtime_error>(
"Core", "IO", "Open",
"Cannot guess engine type from TAR info. Set the engine manually");
return engineType;
};
PERFSTUBS_SCOPED_TIMER("IO::Open");
auto itEngineFound = m_Engines.find(name);
const bool isEngineFound = (itEngineFound != m_Engines.end());
bool isEngineActive = false;
Mode mode_to_use = mode;
if (isEngineFound)
{
if (*itEngineFound->second)
{
isEngineActive = true;
}
}
if (isEngineFound)
{
if (isEngineActive) // check if active
{
helper::Throw<std::invalid_argument>("Core", "IO", "Open",
"Engine " + name + " is opened twice");
}
}
if (isEngineFound)
{
if (!isEngineActive)
{
m_Engines.erase(name);
}
}
std::shared_ptr<Engine> engine;
const bool isDefaultEngine = m_EngineType.empty() ? true : false;
std::string engineTypeLC = m_EngineType;
if (!isDefaultEngine)
{
std::transform(engineTypeLC.begin(), engineTypeLC.end(), engineTypeLC.begin(), ::tolower);
}
// Engine selection probes the filesystem on rank 0 only and broadcasts the
// chosen engine type, so a missing target fails on every rank instead of a
// lone rank-0 throw that hangs the others.
const bool isVirtualFile = (engineTypeLC == "file" || engineTypeLC == "bpfile" ||
engineTypeLC == "bp" || engineTypeLC == "filestream");
if (isDefaultEngine || isVirtualFile)
{
if (engineTypeLC == "filestream")
{
// FileStream streams BP4/BP5 file output only (not DAOS, timeseries,
// etc.). A not-yet-created stream is normal, so BPVersionLocal returns
// '4' for a missing path and the engine waits rather than erroring.
char version = '4';
if (comm.Rank() == 0)
{
version = helper::BPVersionLocal(name);
}
engineTypeLC = std::string("bp") + comm.BroadcastValue(version);
}
else if (helper::EndsWith(name, ".h5", false))
{
engineTypeLC = "hdf5";
}
else if (helper::EndsWith(name, ".aca", false))
{
engineTypeLC = "campaign";
}
else if (helper::EndsWith(name, ".ats", false))
{
engineTypeLC = "timeseries";
}
else if ((mode_to_use == Mode::Read) || (mode_to_use == Mode::ReadRandomAccess))
{
auto it = m_Parameters.find("TarInfo");
if (it != m_Parameters.end())
{
// TarInfo is a parameter (same on every rank), so guess directly.
engineTypeLC = lf_GuessEngineFromTAR(it->second);
}
else
{
// Rank 0 picks the engine from the on-disk dataset; an empty
// result means nothing exists at the location.
std::vector<char> selected;
if (comm.Rank() == 0)
{
std::string sel;
if (adios2sys::SystemTools::PathExists(name) ||
adios2sys::SystemTools::PathExists(name + ".tier0"))
{
if (adios2sys::SystemTools::FileIsDirectory(name))
{
sel = std::string("bp") + helper::BPVersionLocal(name);
}
else if (adios2sys::SystemTools::FileIsDirectory(name + ".tier0"))
{
sel = "mhs";
}
else if (helper::EndsWith(name, ".bp", false))
{
sel = "bp3";
}
else
{
sel = helper::IsHDF5FileLocal(name, *this, comm, m_TransportsParameters)
? "hdf5"
: "bp3";
}
}
selected.assign(sel.begin(), sel.end());
}
comm.BroadcastVector(selected);
if (selected.empty())
{
// Built identically on every rank from 'name', so all ranks
// throw together without broadcasting the message.
std::string err = "Cannot determine the engine for reading \"" + name +
"\": nothing exists at that location.";
if (mode_to_use == Mode::Read)
{
err += " If it is a stream that has not been created yet, select "
"the engine explicitly with IO::SetEngine().";
}
helper::Throw<std::runtime_error>("Core", "IO", "Open", err);
}
engineTypeLC.assign(selected.begin(), selected.end());
}
}
else
{
// File default for writing: BP5
engineTypeLC = "bp5";
}
}
// For the inline engine, there must be exactly 1 reader, and exactly 1
// writer.
if (engineTypeLC == "inline")
{
if (mode_to_use == Mode::Append)
{
helper::Throw<std::runtime_error>("Core", "IO", "Open",
"Append mode is not supported in the inline engine.");
}
// See inline.rst:44
if (mode_to_use == Mode::Sync)
{
helper::Throw<std::runtime_error>("Core", "IO", "Open",
"Sync mode is not supported in the inline engine.");
}
if (m_Engines.size() >= 2)
{
std::string msg = "Failed to add engine " + name + " to IO \'" + m_Name + "\'. ";
msg += "An inline engine must have exactly one writer, and one "
"reader. ";
msg += "There are already two engines declared, so no more can be "
"added.";
helper::Throw<std::runtime_error>("Core", "IO", "Open", msg);
}
// Now protect against declaration of two writers, or declaration of
// two readers:
if (m_Engines.size() == 1)
{
auto engine_ptr = m_Engines.begin()->second;
if (engine_ptr->OpenMode() == mode_to_use)
{
std::string msg = "The previously added engine " + engine_ptr->m_Name +
" is already opened in same mode requested for " + name + ". ";
msg += "The inline engine requires exactly one writer and one "
"reader.";
helper::Throw<std::runtime_error>("Core", "IO", "Open", msg);
}
}
}
if (mode_to_use == Mode::ReadRandomAccess)
{
// older engines don't know about ReadRandomAccess Mode
auto it = ReadRandomAccess_Supported.find(engineTypeLC);
if (it != ReadRandomAccess_Supported.end())
{
if (!it->second)
{
mode_to_use = Mode::Read;
}
}
}
auto f = FactoryLookup(engineTypeLC);
if (f != Factory.end())
{
if (md && (mode == Mode::ReadRandomAccess || mode_to_use == Mode::Read))
{
engine =
f->second.MakeReaderWithMD(*this, name, mode_to_use, std::move(comm), md, mdsize);
}
else if ((mode_to_use == Mode::Read) || (mode_to_use == Mode::ReadRandomAccess))
{
engine = f->second.MakeReader(*this, name, mode_to_use, std::move(comm));
}
else
{
engine = f->second.MakeWriter(*this, name, mode_to_use, std::move(comm));
}
}
else
{
helper::Throw<std::invalid_argument>("Core", "IO", "Open",
"Engine type " + m_EngineType + " is not valid");
}
auto itEngine = m_Engines.emplace(name, std::move(engine));
if (!itEngine.second)
{
helper::Throw<std::invalid_argument>("Core", "IO", "Open",
"failed to create Engine " + m_EngineType);
}
// return a reference
return *itEngine.first->second.get();
}
Engine &IO::Open(const std::string &name, const Mode mode)
{
return Open(name, mode, m_ADIOS.GetComm().Duplicate());
}
Engine &IO::Open(const std::string &name, const char *md, const size_t mdsize)
{
const Mode mode = Mode::ReadRandomAccess;
// helper::Comm comm;
// std::cout << "Open comm rank = " << comm.Rank();
return Open(name, mode, helper::CommDummy(), md, mdsize);
}
Group &IO::CreateGroup(char delimiter)
{
m_Gr = std::make_shared<Group>("", delimiter, *this);
m_Gr->BuildTree();
return *m_Gr;
}
Engine &IO::GetEngine(const std::string &name)
{
PERFSTUBS_SCOPED_TIMER("IO::other");
auto itEngine = m_Engines.find(name);
if (itEngine == m_Engines.end())
{
helper::Throw<std::invalid_argument>("Core", "IO", "GetEngine",
"Engine " + name + " not found");
}
// return a reference
return *itEngine->second.get();
}
void IO::RemoveEngine(const std::string &name)
{
auto itEngine = m_Engines.find(name);
if (itEngine != m_Engines.end())
{
m_Engines.erase(itEngine);
}
}
void IO::EnterComputationBlock() noexcept
{
for (auto &enginePair : m_Engines)
{
auto &engine = enginePair.second;
if (engine->OpenMode() != Mode::Read)
{
enginePair.second->EnterComputationBlock();
}
}
}
void IO::ExitComputationBlock() noexcept
{
for (auto &enginePair : m_Engines)
{
auto &engine = enginePair.second;
if (engine->OpenMode() != Mode::Read)
{
enginePair.second->ExitComputationBlock();
}
}
}
void IO::FlushAll()
{
PERFSTUBS_SCOPED_TIMER("IO::FlushAll");
for (auto &enginePair : m_Engines)
{
auto &engine = enginePair.second;
if (engine->OpenMode() != Mode::Read)
{
enginePair.second->Flush();
}
}
}
void IO::ResetVariablesStepSelection(const bool zeroStart, const std::string hint)
{
PERFSTUBS_SCOPED_TIMER("IO::other");
for (auto itVariable = m_Variables.begin(); itVariable != m_Variables.end(); ++itVariable)
{
const DataType type = InquireVariableType(itVariable);
if (type == DataType::None)
{
continue;
}
if (type == DataType::Struct)
{
}
else
{
VariableBase &variable = *itVariable->second;
variable.CheckRandomAccessConflict(hint);
variable.ResetStepsSelection(zeroStart);
variable.m_RandomAccess = false;
}
}
}
void IO::SetPrefixedNames(const bool isStep) noexcept
{
const std::set<std::string> attributes = helper::KeysToSet(m_Attributes);
const std::set<std::string> variables = helper::KeysToSet(m_Variables);
for (auto itVariable = m_Variables.begin(); itVariable != m_Variables.end(); ++itVariable)
{
// if for each step (BP4), check if variable type is not empty
// (means variable exist in that step)
const DataType type = isStep ? InquireVariableType(itVariable) : itVariable->second->m_Type;
if (type == DataType::None)
{
continue;
}
if (type == DataType::Struct)
{
}
else
{
VariableBase &variable = *itVariable->second;
variable.m_PrefixedVariables = helper::PrefixMatches(variable.m_Name, variables);
variable.m_PrefixedAttributes = helper::PrefixMatches(variable.m_Name, attributes);
}
}
m_IsPrefixedNames = true;
}
// PRIVATE
void IO::CheckAttributeCommon(const std::string &name) const
{
auto itAttribute = m_Attributes.find(name);
if (itAttribute != m_Attributes.end())
{
helper::Throw<std::invalid_argument>("Core", "IO", "CheckAttributeCommon",
"Attribute " + name + " exists in IO " + m_Name +
", in call to DefineAttribute");
}
}
void IO::CheckTransportType(const std::string type) const
{
if (type.empty() || type.find("=") != type.npos)
{
helper::Throw<std::invalid_argument>(
"Core", "IO", "CheckTransportType",
"wrong first argument " + type +
", must be a single word for a supported transport type, in "
"call to IO AddTransport");
}
}
#ifdef ADIOS2_HAVE_DERIVED_VARIABLE
VariableDerived &IO::DefineDerivedVariable(const std::string &name, const std::string &exp_string,
const DerivedVarType varType)
{
PERFSTUBS_SCOPED_TIMER("IO::DefineDerivedVariable");
{
auto itVariable = m_VariablesDerived.find(name);
if (itVariable != m_VariablesDerived.end())
{
helper::Throw<std::invalid_argument>("Core", "IO", "DefineDerivedVariable",
"derived variable " + name +
" already defined in IO " + m_Name);
}
else
{
auto itVariable = m_Variables.find(name);
if (itVariable != m_Variables.end())
{
helper::Throw<std::invalid_argument>(
"Core", "IO", "DefineDerivedVariable",
"derived variable " + name +
" trying to use an already defined variable name in IO " + m_Name);
}
}
}
// Parse expression string into ExprNode tree
derived::ExprNode exprTree = detail::ParseToExprNode(exp_string);
std::vector<std::string> var_list = derived::VariableNameList(exprTree);
std::map<std::string, DataType> name_to_type;
std::map<std::string, std::tuple<Dims, Dims, Dims>> name_to_dims;
// check correctness for the variable names and types within the expression
for (auto var_name : var_list)
{
auto itVariable = m_Variables.find(var_name);
if (itVariable == m_Variables.end())
helper::Throw<std::invalid_argument>("Core", "IO", "DefineDerivedVariable",
"using undefine variable " + var_name +
" in defining the derived variable " + name);
DataType var_type = InquireVariableType(var_name);
name_to_type.insert({var_name, var_type});
name_to_dims.insert({var_name,
{(itVariable->second)->m_Start, (itVariable->second)->m_Count,
(itVariable->second)->m_Shape}});
}
// Build the expression code stream: GenerateCode -> ResolveTypes -> ConstantFold -> PlanBuffers
derived::ExprCodeStream codeStream = derived::GenerateCode(exprTree);
derived::ResolveTypes(codeStream, name_to_type);
derived::ConstantFold(codeStream);
derived::PlanBuffers(codeStream);
codeStream.ExprString = exp_string;
DataType expressionType = codeStream.OutputType;
{
static bool verbose = (getenv("DerivedVerbose") != nullptr);
if (verbose)
std::cerr << derived::DumpCodeStream(codeStream);
}
// Compute output dims before construction (VariableBase validates in InitShapeType)