Skip to content

Commit def25d6

Browse files
authored
Merge pull request #5001 from vicentebolea/backport-from-master
backport from master
2 parents 0c7a8d7 + 66a13b2 commit def25d6

49 files changed

Lines changed: 3014 additions & 1369 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

REUSE.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ precedence = "aggregate"
102102
SPDX-FileCopyrightText = "mingw-w64 contributors"
103103
SPDX-License-Identifier = "LicenseRef-public-domain"
104104

105+
[[annotations]]
106+
path = "source/adios2/toolkit/derived/parser/pregen-source/*"
107+
precedence = "aggregate"
108+
SPDX-FileCopyrightText = "Oak Ridge National Laboratory and Contributors"
109+
SPDX-License-Identifier = "Apache-2.0"
110+
105111
[[annotations]]
106112
path = ["thirdparty/CMakeLists.txt", "thirdparty/atl/CMakeLists.txt", "thirdparty/dill/CMakeLists.txt", "thirdparty/enet/CMakeLists.txt", "thirdparty/EVPath/CMakeLists.txt", "thirdparty/ffs/CMakeLists.txt", "thirdparty/GTest/CMakeLists.txt", "thirdparty/KWSys/CMakeLists.txt", "thirdparty/mingw-w64/CMakeLists.txt", "thirdparty/nanobind/CMakeLists.txt", "thirdparty/nlohmann_json/CMakeLists.txt", "thirdparty/perfstubs/CMakeLists.txt", "thirdparty/pugixml/CMakeLists.txt", "thirdparty/yaml-cpp/CMakeLists.txt"]
107113
precedence = "aggregate"

source/adios2/CMakeLists.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ set(maybe_adios2_core_derived)
149149
if(ADIOS2_HAVE_Derived_Variable)
150150
target_sources(adios2_core PRIVATE
151151
core/VariableDerived.cpp
152-
toolkit/derived/Expression.cpp
152+
toolkit/derived/ExprCodeStream.cpp
153153
toolkit/derived/Function.cpp)
154154
set_target_properties(adios2_core PROPERTIES
155155
INCLUDE_DIRECTORIES "$<BUILD_INTERFACE:${ADIOS2_SOURCE_DIR}/source/adios2/toolkit/derived/parser>;$<BUILD_INTERFACE:${ADIOS2_BINARY_DIR}/source/adios2>")
@@ -182,17 +182,18 @@ if(ADIOS2_HAVE_Derived_Variable)
182182
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "IntelLLVM")
183183
SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_BINARY_DIR}/parser.cpp PROPERTIES COMPILE_FLAGS -Wno-unused-but-set-variable)
184184
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
185-
SET_SOURCE_FILES_PROPERTIES(toolkit/derived/Expression.cpp toolkit/derived/Function.cpp PROPERTIES COMPILE_FLAGS "/wd4005 /wd4065 /wd4267 -DYY_NO_UNISTD_H")
185+
SET_SOURCE_FILES_PROPERTIES(toolkit/derived/ExprCodeStream.cpp toolkit/derived/Function.cpp PROPERTIES COMPILE_FLAGS "/wd4005 /wd4065 /wd4267 -DYY_NO_UNISTD_H")
186186
endif()
187187

188188
add_library(adios2_core_derived
189189
${CMAKE_CURRENT_BINARY_DIR}/lexer.cpp
190190
${CMAKE_CURRENT_BINARY_DIR}/parser.cpp
191191
toolkit/derived/parser/ASTDriver.cpp
192-
toolkit/derived/parser/ASTNode.cpp)
192+
toolkit/derived/parser/ASTNode.cpp
193+
toolkit/derived/parser/ASTToExprNode.cpp)
193194
set_target_properties(adios2_core_derived PROPERTIES
194195
VISIBILITY_INLINES_HIDDEN ON
195-
INCLUDE_DIRECTORIES "$<BUILD_INTERFACE:${ADIOS2_SOURCE_DIR}/source/adios2/toolkit/derived/parser>;$<BUILD_INTERFACE:${ADIOS2_BINARY_DIR}/source/adios2>"
196+
INCLUDE_DIRECTORIES "$<BUILD_INTERFACE:${ADIOS2_SOURCE_DIR}/source>;$<BUILD_INTERFACE:${ADIOS2_SOURCE_DIR}/source/adios2/toolkit/derived/parser>;$<BUILD_INTERFACE:${ADIOS2_BINARY_DIR}/source/adios2>"
196197
EXPORT_NAME core_derived
197198
OUTPUT_NAME adios2${ADIOS2_LIBRARY_SUFFIX}_core_derived)
198199
target_link_libraries(adios2_core PRIVATE adios2_core_derived)

source/adios2/core/IO.cpp

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include "IO.h"
88
#include "IO.tcc"
99

10+
#include <cstdlib>
11+
#include <iostream>
1012
#include <memory>
1113
#include <mutex>
1214
#include <sstream>
@@ -948,9 +950,9 @@ VariableDerived &IO::DefineDerivedVariable(const std::string &name, const std::s
948950
}
949951
}
950952

951-
derived::Expression derived_exp(exp_string);
952-
std::vector<std::string> var_list = derived_exp.VariableNameList();
953-
bool isConstant = true;
953+
// Parse expression string into ExprNode tree
954+
derived::ExprNode exprTree = detail::ParseToExprNode(exp_string);
955+
std::vector<std::string> var_list = derived::VariableNameList(exprTree);
954956
std::map<std::string, DataType> name_to_type;
955957
std::map<std::string, std::tuple<Dims, Dims, Dims>> name_to_dims;
956958
// check correctness for the variable names and types within the expression
@@ -963,22 +965,32 @@ VariableDerived &IO::DefineDerivedVariable(const std::string &name, const std::s
963965
" in defining the derived variable " + name);
964966
DataType var_type = InquireVariableType(var_name);
965967
name_to_type.insert({var_name, var_type});
966-
if ((itVariable->second)->IsConstantDims() == false)
967-
isConstant = false;
968968
name_to_dims.insert({var_name,
969969
{(itVariable->second)->m_Start, (itVariable->second)->m_Count,
970970
(itVariable->second)->m_Shape}});
971971
}
972-
// set the type of the expression and check correcness
973-
DataType expressionType = derived_exp.GetType(name_to_type);
974-
// set the initial shape of the expression and check correcness
975-
derived_exp.SetDims(name_to_dims);
976-
// std::cout << "Derived variable " << name << ": PASS : initial variable dimensions are valid"
977-
// << std::endl;
978-
979-
// create derived variable with the expression
972+
973+
// Build the expression code stream: GenerateCode -> ResolveTypes -> ConstantFold -> PlanBuffers
974+
derived::ExprCodeStream codeStream = derived::GenerateCode(exprTree);
975+
derived::ResolveTypes(codeStream, name_to_type);
976+
derived::ConstantFold(codeStream);
977+
derived::PlanBuffers(codeStream);
978+
codeStream.ExprString = exp_string;
979+
DataType expressionType = codeStream.OutputType;
980+
981+
{
982+
static bool verbose = (getenv("DerivedVerbose") != nullptr);
983+
if (verbose)
984+
std::cerr << derived::DumpCodeStream(codeStream);
985+
}
986+
987+
// Compute output dims before construction (VariableBase validates in InitShapeType)
988+
auto outDims = derived::GetDims(codeStream, name_to_dims);
989+
980990
auto itVariablePair = m_VariablesDerived.emplace(
981-
name, std::make_unique<VariableDerived>(name, derived_exp, expressionType, isConstant,
991+
name, std::make_unique<VariableDerived>(name, std::move(exprTree), std::move(codeStream),
992+
exp_string, expressionType, std::get<2>(outDims),
993+
std::get<0>(outDims), std::get<1>(outDims), false,
982994
varType, name_to_type));
983995
VariableDerived &variable = static_cast<VariableDerived &>(*itVariablePair.first->second);
984996

source/adios2/core/VariableDerived.cpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,32 @@ namespace adios2
1212
namespace core
1313
{
1414

15-
VariableDerived::VariableDerived(const std::string &name, adios2::derived::Expression expr,
16-
const DataType exprType, const bool isConstant,
17-
const DerivedVarType varType,
15+
VariableDerived::VariableDerived(const std::string &name, adios2::derived::ExprNode exprTree,
16+
adios2::derived::ExprCodeStream codeStream,
17+
const std::string &exprString, const DataType exprType,
18+
const Dims &shape, const Dims &start, const Dims &count,
19+
const bool isConstant, const DerivedVarType varType,
1820
const std::map<std::string, DataType> nameToType)
19-
: VariableBase(name, exprType, helper::GetDataTypeSize(exprType), expr.GetShape(), expr.GetStart(),
20-
expr.GetCount(), isConstant),
21-
m_DerivedType(varType), m_NameToType(nameToType), m_Expr(expr)
21+
: VariableBase(name, exprType, helper::GetDataTypeSize(exprType), shape, start, count, isConstant),
22+
m_DerivedType(varType), m_NameToType(nameToType), m_ExprTree(std::move(exprTree)),
23+
m_CodeStream(std::move(codeStream)), m_ExprString(exprString)
2224
{
2325
if (varType != DerivedVarType::StoreData)
2426
m_WriteData = false;
2527
}
2628

2729
DerivedVarType VariableDerived::GetDerivedType() { return m_DerivedType; }
2830

29-
std::vector<std::string> VariableDerived::VariableNameList() { return m_Expr.VariableNameList(); }
31+
std::vector<std::string> VariableDerived::VariableNameList() { return m_CodeStream.InputVarNames; }
32+
3033
void VariableDerived::UpdateExprDim(std::map<std::string, std::tuple<Dims, Dims, Dims>> NameToDims)
3134
{
32-
m_Expr.SetDims(NameToDims);
33-
m_Shape = m_Expr.GetShape();
34-
m_Start = m_Expr.GetStart();
35-
m_Count = m_Expr.GetCount();
35+
auto outDims = adios2::derived::GetDims(m_CodeStream, NameToDims);
36+
m_Shape = std::get<2>(outDims);
37+
m_Start = std::get<0>(outDims);
38+
m_Count = std::get<1>(outDims);
39+
if (!m_Shape.empty())
40+
m_ShapeID = ShapeID::GlobalArray;
3641
}
3742

3843
std::vector<std::tuple<void *, Dims, Dims>>
@@ -80,7 +85,7 @@ VariableDerived::ApplyExpression(std::map<std::string, std::unique_ptr<MinVarInf
8085
inputData.insert({variable.first, varData});
8186
}
8287
std::vector<adios2::derived::DerivedData> outputData =
83-
m_Expr.ApplyExpression(numBlocks, inputData);
88+
adios2::derived::Execute(m_CodeStream, numBlocks, inputData);
8489

8590
std::vector<std::tuple<void *, Dims, Dims>> blockData;
8691
for (size_t i = 0; i < numBlocks; i++)
@@ -115,7 +120,7 @@ VariableDerived::CreateEmptyData(std::map<std::string, std::unique_ptr<MinVarInf
115120
nameToDims.insert({variable.first, varDims});
116121
}
117122

118-
auto outputDims = m_Expr.GetDims(nameToDims);
123+
auto outputDims = adios2::derived::GetDims(m_CodeStream, nameToDims);
119124

120125
blockData.push_back({nullptr, std::get<0>(outputDims), std::get<1>(outputDims)});
121126
}

source/adios2/core/VariableDerived.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
#define ADIOS2_CORE_VARIABLE_DERIVED_H_
99

1010
#include "adios2/core/VariableBase.h"
11-
#include "adios2/toolkit/derived/Expression.h"
11+
#include "adios2/toolkit/derived/ExprCodeStream.h"
12+
#include "adios2/toolkit/derived/ExprNode.h"
1213

1314
namespace adios2
1415
{
@@ -28,9 +29,14 @@ class VariableDerived : public VariableBase
2829
size_t numBlocks);
2930

3031
public:
31-
adios2::derived::Expression m_Expr;
32-
VariableDerived(const std::string &name, adios2::derived::Expression expr,
33-
const DataType exprType, const bool isConstant, const DerivedVarType varType,
32+
adios2::derived::ExprNode m_ExprTree;
33+
adios2::derived::ExprCodeStream m_CodeStream;
34+
std::string m_ExprString;
35+
36+
VariableDerived(const std::string &name, adios2::derived::ExprNode exprTree,
37+
adios2::derived::ExprCodeStream codeStream, const std::string &exprString,
38+
const DataType exprType, const Dims &shape, const Dims &start,
39+
const Dims &count, const bool isConstant, const DerivedVarType varType,
3440
const std::map<std::string, DataType> nameToType);
3541
~VariableDerived() = default;
3642

source/adios2/engine/bp5/BP5Reader.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include "BP5Reader.h"
2727
#include "BP5Reader.tcc"
28+
#include "adios2/toolkit/derived/ExprNode.h"
2829

2930
using TP = std::chrono::high_resolution_clock::time_point;
3031
#define NOW() std::chrono::high_resolution_clock::now();
@@ -1508,8 +1509,8 @@ std::string BP5Reader::VariableExprStr(const VariableBase &Var)
15081509
char *expPtr = m_BP5Deserializer->VariableExprStr(Var);
15091510
if (expPtr != nullptr)
15101511
{
1511-
derived::Expression expr(expPtr);
1512-
return expr.toStringExpr();
1512+
derived::ExprNode node = detail::ParseToExprNode(expPtr);
1513+
return derived::ToStringExpr(node);
15131514
}
15141515
#endif
15151516
std::string noDerive("");

source/adios2/toolkit/derived/DerivedData.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@ struct DerivedData
1919
Dims Start;
2020
Dims Count;
2121
DataType Type;
22+
bool IsScalar = false; // true = Data points to one element, broadcast by compute functions
2223
};
2324

2425
struct ExprData
2526
{
2627
std::vector<DerivedData> Data;
27-
std::vector<std::string> Const;
2828
DataType OutType;
29+
void *Output = nullptr; // pre-allocated output buffer
30+
size_t OutputSize = 0; // number of elements in output
2931
};
3032
}
3133
}

0 commit comments

Comments
 (0)