Skip to content

Commit 07bbcca

Browse files
committed
ADIOS2 logger base impl
1 parent ec449af commit 07bbcca

2 files changed

Lines changed: 128 additions & 0 deletions

File tree

src/IO/ADIOS/ADIOS2File.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,35 @@
5353
}
5454

5555
#if openPMD_HAVE_ADIOS2
56+
57+
/*
58+
* Logging helper for ADIOS2 API calls to enable quick construction of
59+
* reproducers for bug reports. Output is formatted as C++ code.
60+
* Controlled by environment variable OPENPMD_ADIOS2_LOG_API_CALLS
61+
*/
62+
namespace
63+
{
64+
inline bool shouldLogADIOS2ApiCalls()
65+
{
66+
static int cached_result = -1;
67+
if (cached_result == -1)
68+
{
69+
cached_result =
70+
openPMD::auxiliary::getEnvNum("OPENPMD_ADIOS2_LOG_API_CALLS", 0);
71+
}
72+
return cached_result != 0;
73+
}
74+
} // namespace
75+
76+
#define ADIOS2_LOG_API_CALL(...) \
77+
do \
78+
{ \
79+
if (::shouldLogADIOS2ApiCalls()) \
80+
{ \
81+
std::cerr << "[ADIOS2 API] " << __VA_ARGS__ << std::endl; \
82+
} \
83+
} while (false)
84+
5685
namespace openPMD::detail
5786
{
5887
template <typename T>
@@ -81,6 +110,7 @@ void DatasetReader::call(
81110
}
82111
auto ptr = std::static_pointer_cast<T>(bp.param.data).get();
83112
engine.Get(var, ptr);
113+
ADIOS2_LOG_API_CALL("engine.Get(var, ptr);");
84114
}
85115

86116
template <class>
@@ -111,6 +141,7 @@ void WriteDataset::call(ADIOS2File &ba, detail::BufferedPut &bp)
111141
ba.variables());
112142

113143
engine.Put(var, ptr);
144+
ADIOS2_LOG_API_CALL("engine.Put(var, ptr);");
114145
}
115146
else if constexpr (std::is_same_v<
116147
ptr_type,
@@ -182,6 +213,7 @@ struct RunUniquePtrPut
182213
std::nullopt,
183214
ba.variables());
184215
engine.Put(var, ptr);
216+
ADIOS2_LOG_API_CALL("engine.Put(var, ptr);");
185217
}
186218

187219
static constexpr char const *errorMsg = "RunUniquePtrPut";
@@ -262,6 +294,7 @@ void ADIOS2File::finalize()
262294
if (streamStatus == StreamStatus::DuringStep)
263295
{
264296
engine.EndStep();
297+
ADIOS2_LOG_API_CALL("engine.EndStep();");
265298
}
266299
engine.Close();
267300
m_ADIOS.RemoveIO(m_IOName);
@@ -809,6 +842,7 @@ adios2::Engine &ADIOS2File::getEngine()
809842
m_engine =
810843
std::make_optional(adios2::Engine(m_IO.Open(m_file, tempMode)));
811844
m_engine->BeginStep();
845+
ADIOS2_LOG_API_CALL("engine.BeginStep();");
812846
streamStatus = StreamStatus::DuringStep;
813847
break;
814848
}
@@ -833,10 +867,12 @@ adios2::Engine &ADIOS2File::getEngine()
833867
*/
834868
if (m_engine->BeginStep() != adios2::StepStatus::OK)
835869
{
870+
ADIOS2_LOG_API_CALL("engine.BeginStep();");
836871
throw std::runtime_error(
837872
"[ADIOS2] Unexpected step status when "
838873
"opening file/stream.");
839874
}
875+
ADIOS2_LOG_API_CALL("engine.BeginStep();");
840876
openedANewStep = true;
841877
}
842878

@@ -930,10 +966,15 @@ adios2::Engine &ADIOS2File::getEngine()
930966
m_engine.value().BeginStep() !=
931967
adios2::StepStatus::OK)
932968
{
969+
ADIOS2_LOG_API_CALL("engine.BeginStep();");
933970
throw std::runtime_error(
934971
"[ADIOS2] Unexpected step status when "
935972
"opening file/stream.");
936973
}
974+
if (!openedANewStep)
975+
{
976+
ADIOS2_LOG_API_CALL("engine.BeginStep();");
977+
}
937978
streamStatus = StreamStatus::DuringStep;
938979
}
939980
}
@@ -1136,11 +1177,13 @@ void ADIOS2File::flush_impl(ADIOS2FlushParams flushParams, bool writeLatePuts)
11361177
entry.run(*this);
11371178
}
11381179
engine.PerformDataWrite();
1180+
ADIOS2_LOG_API_CALL("engine.PerformDataWrite();");
11391181
m_uniquePtrPuts.clear();
11401182
m_updateSpans.clear();
11411183
break;
11421184
case CleanedFlushTarget::Buffer:
11431185
engine.PerformPuts();
1186+
ADIOS2_LOG_API_CALL("engine.PerformPuts();");
11441187
break;
11451188
case CleanedFlushTarget::Step:
11461189
if (streamStatus != StreamStatus::DuringStep)
@@ -1158,7 +1201,9 @@ void ADIOS2File::flush_impl(ADIOS2FlushParams flushParams, bool writeLatePuts)
11581201
entry.run(*this);
11591202
}
11601203
engine.EndStep();
1204+
ADIOS2_LOG_API_CALL("engine.EndStep();");
11611205
engine.BeginStep();
1206+
ADIOS2_LOG_API_CALL("engine.BeginStep();");
11621207
m_uniquePtrPuts.clear();
11631208
uncommittedAttributes.clear();
11641209
m_updateSpans.clear();
@@ -1182,6 +1227,7 @@ void ADIOS2File::flush_impl(ADIOS2FlushParams flushParams, bool writeLatePuts)
11821227
else
11831228
{
11841229
eng.PerformGets();
1230+
ADIOS2_LOG_API_CALL("engine.PerformGets();");
11851231
}
11861232
},
11871233
writeLatePuts,

src/IO/ADIOS/ADIOS2IOHandler.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,76 @@ namespace openPMD
8181

8282
#if openPMD_HAVE_ADIOS2
8383

84+
/*
85+
* Logging helper for ADIOS2 API calls to enable quick construction of
86+
* reproducers for bug reports. Output is formatted as C++ code.
87+
* Controlled by environment variable OPENPMD_ADIOS2_LOG_API_CALLS
88+
*/
89+
namespace
90+
{
91+
inline bool shouldLogADIOS2ApiCalls()
92+
{
93+
static int cached_result = -1;
94+
if (cached_result == -1)
95+
{
96+
cached_result =
97+
auxiliary::getEnvNum("OPENPMD_ADIOS2_LOG_API_CALLS", 0);
98+
}
99+
return cached_result != 0;
100+
}
101+
102+
template <typename T>
103+
inline std::string formatValue(T const &value);
104+
105+
// Specialization for strings
106+
template <>
107+
inline std::string formatValue(std::string const &value)
108+
{
109+
return "\"" + value + "\"";
110+
}
111+
112+
// Specialization for basic types
113+
template <>
114+
inline std::string formatValue(int const &value)
115+
{
116+
return std::to_string(value);
117+
}
118+
119+
template <>
120+
inline std::string formatValue(size_t const &value)
121+
{
122+
return std::to_string(value) + "UL";
123+
}
124+
125+
// template <>
126+
// inline std::string formatValue(uint64_t const &value)
127+
// {
128+
// return std::to_string(value) + "ULL";
129+
// }
130+
131+
template <>
132+
inline std::string formatValue(bool const &value)
133+
{
134+
return value ? "true" : "false";
135+
}
136+
137+
// Generic fallback (will use operator<<)
138+
template <typename T>
139+
inline std::string formatValue(T const &)
140+
{
141+
return "/*value*/";
142+
}
143+
} // namespace
144+
145+
#define ADIOS2_LOG_API_CALL(...) \
146+
do \
147+
{ \
148+
if (::openPMD::shouldLogADIOS2ApiCalls()) \
149+
{ \
150+
std::cerr << "[ADIOS2 API] " << __VA_ARGS__ << std::endl; \
151+
} \
152+
} while (false)
153+
84154
std::optional<size_t> joinedDimension(adios2::Dims const &dims)
85155
{
86156
for (size_t i = 0; i < dims.size(); ++i)
@@ -731,6 +801,9 @@ void ADIOS2IOHandlerImpl::createFile(
731801
fileData.m_IO.DefineAttribute(
732802
adios_defaults::str_groupBasedWarning,
733803
std::string(warningADIOS2NoGroupbasedEncoding));
804+
ADIOS2_LOG_API_CALL(
805+
"IO.DefineAttribute(\"" << adios_defaults::str_groupBasedWarning
806+
<< "\", ...);");
734807
}
735808
}
736809
}
@@ -1226,7 +1299,9 @@ namespace detail
12261299
adios2::Dims offset(params.offset.begin(), params.offset.end());
12271300
adios2::Dims extent(params.extent.begin(), params.extent.end());
12281301
variable.SetSelection({std::move(offset), std::move(extent)});
1302+
ADIOS2_LOG_API_CALL("var.SetSelection({offset, extent});");
12291303
typename adios2::Variable<T>::Span span = engine.Put(variable);
1304+
ADIOS2_LOG_API_CALL("var_span = engine.Put(var);");
12301305
params.out->backendManagedBuffer = true;
12311306
/*
12321307
* SIC!
@@ -2366,6 +2441,8 @@ namespace detail
23662441
/* variableName = */ "",
23672442
/* separator = */ "/",
23682443
/* allowModification = */ modifiable);
2444+
ADIOS2_LOG_API_CALL(
2445+
"IO.DefineAttribute(\"" << fullName << "\", ...);");
23692446

23702447
if (!attr)
23712448
{
@@ -2480,13 +2557,18 @@ namespace detail
24802557
if (!var)
24812558
{
24822559
var = IO.DefineVariable<T>(name, shape, start, count, constantDims);
2560+
ADIOS2_LOG_API_CALL(
2561+
"IO.DefineVariable<T>(\""
2562+
<< name << "\", shape, start, count, constantDims);");
24832563
}
24842564
else
24852565
{
24862566
var.SetShape(shape);
2567+
ADIOS2_LOG_API_CALL("var.SetShape(shape);");
24872568
if (count.size() > 0)
24882569
{
24892570
var.SetSelection({start, count});
2571+
ADIOS2_LOG_API_CALL("var.SetSelection({start, count});");
24902572
}
24912573
// don't add compression operators multiple times
24922574
return;

0 commit comments

Comments
 (0)