Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend Derived Variable Expressions: Trig functions #4282

Merged
merged 11 commits into from
Aug 20, 2024
15 changes: 15 additions & 0 deletions source/adios2/toolkit/derived/Expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ const std::map<ExpressionOperator, OperatorProperty> op_property = {
{ExpressionOperator::OP_DIV, {"DIV", false}},
{ExpressionOperator::OP_SQRT, {"SQRT", false}},
{ExpressionOperator::OP_POW, {"POW", false}},
{ExpressionOperator::OP_SIN, {"SIN", false}},
{ExpressionOperator::OP_COS, {"COS", false}},
{ExpressionOperator::OP_TAN, {"TAN", false}},
{ExpressionOperator::OP_ASIN, {"ASIN", false}},
{ExpressionOperator::OP_ACOS, {"ACOS", false}},
{ExpressionOperator::OP_ATAN, {"ATAN", false}},
{ExpressionOperator::OP_CURL, {"CURL", false}},
{ExpressionOperator::OP_MAGN, {"MAGNITUDE", false}}};

Expand All @@ -45,6 +51,9 @@ const std::map<std::string, ExpressionOperator> string_to_op = {
{"multiply", ExpressionOperator::OP_MULT}, {"MULTIPLY", ExpressionOperator::OP_MULT},
{"SQRT", ExpressionOperator::OP_SQRT}, {"sqrt", ExpressionOperator::OP_SQRT},
{"pow", ExpressionOperator::OP_POW}, {"POW", ExpressionOperator::OP_POW},
{"sin", ExpressionOperator::OP_SIN}, {"cos", ExpressionOperator::OP_COS},
{"tan", ExpressionOperator::OP_TAN}, {"asin", ExpressionOperator::OP_ASIN},
{"acos", ExpressionOperator::OP_ACOS}, {"atan", ExpressionOperator::OP_ATAN},
{"^", ExpressionOperator::OP_POW}, {"CURL", ExpressionOperator::OP_CURL},
{"curl", ExpressionOperator::OP_CURL}, {"MAGNITUDE", ExpressionOperator::OP_MAGN},
{"magnitude", ExpressionOperator::OP_MAGN}};
Expand Down Expand Up @@ -136,6 +145,12 @@ std::map<adios2::detail::ExpressionOperator, OperatorFunctions> OpFunctions = {
{adios2::detail::ExpressionOperator::OP_DIV, {DivFunc, SameDimsFunc, SameTypeFunc}},
{adios2::detail::ExpressionOperator::OP_POW, {PowFunc, SameDimsFunc, FloatTypeFunc}},
{adios2::detail::ExpressionOperator::OP_SQRT, {SqrtFunc, SameDimsFunc, FloatTypeFunc}},
{adios2::detail::ExpressionOperator::OP_SIN, {SinFunc, SameDimsFunc, FloatTypeFunc}},
{adios2::detail::ExpressionOperator::OP_COS, {CosFunc, SameDimsFunc, FloatTypeFunc}},
{adios2::detail::ExpressionOperator::OP_TAN, {TanFunc, SameDimsFunc, FloatTypeFunc}},
{adios2::detail::ExpressionOperator::OP_ASIN, {AsinFunc, SameDimsFunc, FloatTypeFunc}},
{adios2::detail::ExpressionOperator::OP_ACOS, {AcosFunc, SameDimsFunc, FloatTypeFunc}},
{adios2::detail::ExpressionOperator::OP_ATAN, {AtanFunc, SameDimsFunc, FloatTypeFunc}},
{adios2::detail::ExpressionOperator::OP_CURL, {Curl3DFunc, CurlDimsFunc, SameTypeFunc}},
{adios2::detail::ExpressionOperator::OP_MAGN, {MagnitudeFunc, SameDimsFunc, SameTypeFunc}}};

Expand Down
6 changes: 6 additions & 0 deletions source/adios2/toolkit/derived/Expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ enum ExpressionOperator
OP_DIV,
OP_SQRT,
OP_POW,
OP_SIN,
OP_COS,
OP_TAN,
OP_ASIN,
OP_ACOS,
OP_ATAN,
OP_MAGN,
OP_CURL
};
Expand Down
228 changes: 228 additions & 0 deletions source/adios2/toolkit/derived/Function.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,234 @@ DerivedData PowFunc(std::vector<DerivedData> inputData, DataType type)
return DerivedData();
}

DerivedData SinFunc(std::vector<DerivedData> inputData, DataType type)
{
PERFSTUBS_SCOPED_TIMER("derived::Function::SinFunc");
if (inputData.size() != 1)
{
helper::Throw<std::invalid_argument>("Derived", "Function", "SinFunc",
"Invalid number of arguments passed to SinFunc");
}
size_t dataSize = std::accumulate(std::begin(inputData[0].Count), std::end(inputData[0].Count),
1, std::multiplies<size_t>());
DataType inputType = inputData[0].Type;

if (inputType == DataType::LongDouble)
{
long double *sinValues = (long double *)malloc(dataSize * sizeof(long double));
std::transform(reinterpret_cast<long double *>(inputData[0].Data),
reinterpret_cast<long double *>(inputData[0].Data) + dataSize, sinValues,
[](long double &a) { return std::sin(a); });
return DerivedData({(void *)sinValues, inputData[0].Start, inputData[0].Count});
}
#define declare_type_sin(T) \
if (inputType == helper::GetDataType<T>()) \
{ \
if (inputType != DataType::LongDouble) \
{ \
double *sinValues = (double *)malloc(dataSize * sizeof(double)); \
std::transform(reinterpret_cast<T *>(inputData[0].Data), \
reinterpret_cast<T *>(inputData[0].Data) + dataSize, sinValues, \
[](T &a) { return std::sin(a); }); \
return DerivedData({(void *)sinValues, inputData[0].Start, inputData[0].Count}); \
} \
}
ADIOS2_FOREACH_ATTRIBUTE_PRIMITIVE_STDTYPE_1ARG(declare_type_sin)
helper::Throw<std::invalid_argument>("Derived", "Function", "SinFunc",
"Invalid variable types");
return DerivedData();
}

DerivedData CosFunc(std::vector<DerivedData> inputData, DataType type)
{
PERFSTUBS_SCOPED_TIMER("derived::Function::CosFunc");
if (inputData.size() != 1)
{
helper::Throw<std::invalid_argument>("Derived", "Function", "CosFunc",
"Invalid number of arguments passed to CosFunc");
}
size_t dataSize = std::accumulate(std::begin(inputData[0].Count), std::end(inputData[0].Count),
1, std::multiplies<size_t>());
DataType inputType = inputData[0].Type;

if (inputType == DataType::LongDouble)
{
long double *cosValues = (long double *)malloc(dataSize * sizeof(long double));
std::transform(reinterpret_cast<long double *>(inputData[0].Data),
reinterpret_cast<long double *>(inputData[0].Data) + dataSize, cosValues,
[](long double &a) { return std::cos(a); });
return DerivedData({(void *)cosValues, inputData[0].Start, inputData[0].Count});
}
#define declare_type_cos(T) \
if (inputType == helper::GetDataType<T>()) \
{ \
if (inputType != DataType::LongDouble) \
{ \
double *cosValues = (double *)malloc(dataSize * sizeof(double)); \
std::transform(reinterpret_cast<T *>(inputData[0].Data), \
reinterpret_cast<T *>(inputData[0].Data) + dataSize, cosValues, \
[](T &a) { return std::cos(a); }); \
return DerivedData({(void *)cosValues, inputData[0].Start, inputData[0].Count}); \
} \
}
ADIOS2_FOREACH_ATTRIBUTE_PRIMITIVE_STDTYPE_1ARG(declare_type_cos)
helper::Throw<std::invalid_argument>("Derived", "Function", "CosFunc",
"Invalid variable types");
return DerivedData();
}

DerivedData TanFunc(std::vector<DerivedData> inputData, DataType type)
{
PERFSTUBS_SCOPED_TIMER("derived::Function::TanFunc");
if (inputData.size() != 1)
{
helper::Throw<std::invalid_argument>("Derived", "Function", "TanFunc",
"Invalid number of arguments passed to TanFunc");
}
size_t dataSize = std::accumulate(std::begin(inputData[0].Count), std::end(inputData[0].Count),
1, std::multiplies<size_t>());
DataType inputType = inputData[0].Type;

if (inputType == DataType::LongDouble)
{
long double *tanValues = (long double *)malloc(dataSize * sizeof(long double));
std::transform(reinterpret_cast<long double *>(inputData[0].Data),
reinterpret_cast<long double *>(inputData[0].Data) + dataSize, tanValues,
[](long double &a) { return std::tan(a); });
return DerivedData({(void *)tanValues, inputData[0].Start, inputData[0].Count});
}
#define declare_type_tan(T) \
if (inputType == helper::GetDataType<T>()) \
{ \
if (inputType != DataType::LongDouble) \
{ \
double *tanValues = (double *)malloc(dataSize * sizeof(double)); \
std::transform(reinterpret_cast<T *>(inputData[0].Data), \
reinterpret_cast<T *>(inputData[0].Data) + dataSize, tanValues, \
[](T &a) { return std::tan(a); }); \
return DerivedData({(void *)tanValues, inputData[0].Start, inputData[0].Count}); \
} \
}
ADIOS2_FOREACH_ATTRIBUTE_PRIMITIVE_STDTYPE_1ARG(declare_type_tan)
helper::Throw<std::invalid_argument>("Derived", "Function", "TanFunc",
"Invalid variable types");
return DerivedData();
}

DerivedData AsinFunc(std::vector<DerivedData> inputData, DataType type)
{
PERFSTUBS_SCOPED_TIMER("derived::Function::AsinFunc");
if (inputData.size() != 1)
{
helper::Throw<std::invalid_argument>("Derived", "Function", "AsinFunc",
"Invalid number of arguments passed to AsinFunc");
}
size_t dataSize = std::accumulate(std::begin(inputData[0].Count), std::end(inputData[0].Count),
1, std::multiplies<size_t>());
DataType inputType = inputData[0].Type;

if (inputType == DataType::LongDouble)
{
long double *asinValues = (long double *)malloc(dataSize * sizeof(long double));
std::transform(reinterpret_cast<long double *>(inputData[0].Data),
reinterpret_cast<long double *>(inputData[0].Data) + dataSize, asinValues,
[](long double &a) { return std::asin(a); });
return DerivedData({(void *)asinValues, inputData[0].Start, inputData[0].Count});
}
#define declare_type_asin(T) \
if (inputType == helper::GetDataType<T>()) \
{ \
if (inputType != DataType::LongDouble) \
{ \
double *asinValues = (double *)malloc(dataSize * sizeof(double)); \
std::transform(reinterpret_cast<T *>(inputData[0].Data), \
reinterpret_cast<T *>(inputData[0].Data) + dataSize, asinValues, \
[](T &a) { return std::asin(a); }); \
return DerivedData({(void *)asinValues, inputData[0].Start, inputData[0].Count}); \
} \
}
ADIOS2_FOREACH_ATTRIBUTE_PRIMITIVE_STDTYPE_1ARG(declare_type_asin)
helper::Throw<std::invalid_argument>("Derived", "Function", "AsinFunc",
"Invalid variable types");
return DerivedData();
}

DerivedData AcosFunc(std::vector<DerivedData> inputData, DataType type)
{
PERFSTUBS_SCOPED_TIMER("derived::Function::AcosFunc");
if (inputData.size() != 1)
{
helper::Throw<std::invalid_argument>("Derived", "Function", "AcosFunc",
"Invalid number of arguments passed to AcosFunc");
}
size_t dataSize = std::accumulate(std::begin(inputData[0].Count), std::end(inputData[0].Count),
1, std::multiplies<size_t>());
DataType inputType = inputData[0].Type;

if (inputType == DataType::LongDouble)
{
long double *acosValues = (long double *)malloc(dataSize * sizeof(long double));
std::transform(reinterpret_cast<long double *>(inputData[0].Data),
reinterpret_cast<long double *>(inputData[0].Data) + dataSize, acosValues,
[](long double &a) { return std::acos(a); });
return DerivedData({(void *)acosValues, inputData[0].Start, inputData[0].Count});
}
#define declare_type_acos(T) \
if (inputType == helper::GetDataType<T>()) \
{ \
if (inputType != DataType::LongDouble) \
{ \
double *acosValues = (double *)malloc(dataSize * sizeof(double)); \
std::transform(reinterpret_cast<T *>(inputData[0].Data), \
reinterpret_cast<T *>(inputData[0].Data) + dataSize, acosValues, \
[](T &a) { return std::acos(a); }); \
return DerivedData({(void *)acosValues, inputData[0].Start, inputData[0].Count}); \
} \
}
ADIOS2_FOREACH_ATTRIBUTE_PRIMITIVE_STDTYPE_1ARG(declare_type_acos)
helper::Throw<std::invalid_argument>("Derived", "Function", "AcosFunc",
"Invalid variable types");
return DerivedData();
}

DerivedData AtanFunc(std::vector<DerivedData> inputData, DataType type)
{
PERFSTUBS_SCOPED_TIMER("derived::Function::AtanFunc");
if (inputData.size() != 1)
{
helper::Throw<std::invalid_argument>("Derived", "Function", "AtanFunc",
"Invalid number of arguments passed to AtanFunc");
}
size_t dataSize = std::accumulate(std::begin(inputData[0].Count), std::end(inputData[0].Count),
1, std::multiplies<size_t>());
DataType inputType = inputData[0].Type;

if (inputType == DataType::LongDouble)
{
long double *atanValues = (long double *)malloc(dataSize * sizeof(long double));
std::transform(reinterpret_cast<long double *>(inputData[0].Data),
reinterpret_cast<long double *>(inputData[0].Data) + dataSize, atanValues,
[](long double &a) { return std::atan(a); });
return DerivedData({(void *)atanValues, inputData[0].Start, inputData[0].Count});
}
#define declare_type_atan(T) \
if (inputType == helper::GetDataType<T>()) \
{ \
if (inputType != DataType::LongDouble) \
{ \
double *atanValues = (double *)malloc(dataSize * sizeof(double)); \
std::transform(reinterpret_cast<T *>(inputData[0].Data), \
reinterpret_cast<T *>(inputData[0].Data) + dataSize, atanValues, \
[](T &a) { return std::atan(a); }); \
return DerivedData({(void *)atanValues, inputData[0].Start, inputData[0].Count}); \
} \
}
ADIOS2_FOREACH_ATTRIBUTE_PRIMITIVE_STDTYPE_1ARG(declare_type_atan)
helper::Throw<std::invalid_argument>("Derived", "Function", "AtanFunc",
"Invalid variable types");
return DerivedData();
}

DerivedData MagnitudeFunc(std::vector<DerivedData> inputData, DataType type)
{
PERFSTUBS_SCOPED_TIMER("derived::Function::MagnitudeFunc");
Expand Down
6 changes: 6 additions & 0 deletions source/adios2/toolkit/derived/Function.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ namespace derived
{
DerivedData AddFunc(std::vector<DerivedData> input, DataType type);
DerivedData SubtractFunc(std::vector<DerivedData> input, DataType type);
DerivedData SinFunc(std::vector<DerivedData> input, DataType type);
DerivedData CosFunc(std::vector<DerivedData> input, DataType type);
DerivedData TanFunc(std::vector<DerivedData> input, DataType type);
DerivedData AsinFunc(std::vector<DerivedData> input, DataType type);
DerivedData AcosFunc(std::vector<DerivedData> input, DataType type);
DerivedData AtanFunc(std::vector<DerivedData> input, DataType type);
DerivedData MultFunc(std::vector<DerivedData> input, DataType type);
DerivedData DivFunc(std::vector<DerivedData> input, DataType type);
DerivedData SqrtFunc(std::vector<DerivedData> input, DataType type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1910,3 +1910,4 @@ adios2::detail::ASTDriver::destroy_lex_structures ()
{
yylex_destroy();
}

Loading