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

Optimize processing attributes, by using a new AttributeBase.Equals()… #3299

Merged
merged 1 commit into from Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions source/adios2/core/Attribute.h
Expand Up @@ -67,6 +67,8 @@ class Attribute : public AttributeBase

private:
std::string DoGetInfoValue() const noexcept override;
bool DoEqual(const void *values, const size_t elements) const
noexcept override;
};

} // end namespace core
Expand Down
29 changes: 29 additions & 0 deletions source/adios2/core/Attribute.tcc
Expand Up @@ -145,6 +145,35 @@ std::string Attribute<T>::DoGetInfoValue() const noexcept
return value;
}

template <typename T>
bool Attribute<T>::DoEqual(const void *values, const size_t elements) const
noexcept
{
if (m_Elements != elements)
{
return false;
}

const T *data = reinterpret_cast<const T *>(values);

if (m_IsSingleValue)
{
return (*data == m_DataSingleValue);
}
else
{
for (size_t i = 0; i < elements; ++i)
{
if (data[i] != m_DataArray[i])
{
return false;
}
}
}

return true;
}

} // end namespace core
} // end namespace adios2

Expand Down
6 changes: 6 additions & 0 deletions source/adios2/core/AttributeBase.cpp
Expand Up @@ -40,5 +40,11 @@ Params AttributeBase::GetInfo() const noexcept
return info;
}

bool AttributeBase::Equals(const void *values, const size_t elements) const
noexcept
{
return this->DoEqual(values, elements);
}

} // end namespace core
} // end namespace adios2
8 changes: 8 additions & 0 deletions source/adios2/core/AttributeBase.h
Expand Up @@ -54,8 +54,16 @@ class AttributeBase

Params GetInfo() const noexcept;

/**
* Compare Attribute's current value with input
* @return true if they equal, false otherwise
*/
bool Equals(const void *values, const size_t elements) const noexcept;

private:
virtual std::string DoGetInfoValue() const noexcept = 0;
virtual bool DoEqual(const void *values, const size_t elements) const
noexcept = 0;
};

} // end namespace core
Expand Down
58 changes: 28 additions & 30 deletions source/adios2/core/IO.tcc
Expand Up @@ -126,11 +126,12 @@ Attribute<T> &IO::DefineAttribute(const std::string &name, const T &value,
auto itExistingAttribute = m_Attributes.find(globalName);
if (itExistingAttribute != m_Attributes.end())
{
if (helper::ValueToString(value) !=
itExistingAttribute->second->GetInfo()["Value"])
if (itExistingAttribute->second->m_Type == helper::GetDataType<T>())
{
if (itExistingAttribute->second->m_Type == helper::GetDataType<T>())
if (!itExistingAttribute->second->Equals(
static_cast<const void *>(&value), 1))
{

Attribute<T> &a =
static_cast<Attribute<T> &>(*itExistingAttribute->second);
a.Modify(value);
Expand All @@ -140,16 +141,16 @@ Attribute<T> &IO::DefineAttribute(const std::string &name, const T &value,
globalName, itExistingAttribute->second->m_Type);
}
}
else
{
helper::Throw<std::invalid_argument>(
"Core", "IO", "DefineAttribute",
"modifiable attribute " + globalName +
" has been defined with type " +
ToString(itExistingAttribute->second->m_Type) +
". Type cannot be changed to " +
ToString(helper::GetDataType<T>()));
}
}
else
{
helper::Throw<std::invalid_argument>(
"Core", "IO", "DefineAttribute",
"modifiable attribute " + globalName +
" has been defined with type " +
ToString(itExistingAttribute->second->m_Type) +
". Type cannot be changed to " +
ToString(helper::GetDataType<T>()));
}
return static_cast<Attribute<T> &>(*itExistingAttribute->second);
}
Expand Down Expand Up @@ -190,15 +191,12 @@ IO::DefineAttribute(const std::string &name, const T *array,
auto itExistingAttribute = m_Attributes.find(globalName);
if (itExistingAttribute != m_Attributes.end())
{
const std::string arrayValues(
"{ " +
helper::VectorToCSV(std::vector<T>(array, array + elements)) +
" }");

if (itExistingAttribute->second->GetInfo()["Value"] != arrayValues)
if (itExistingAttribute->second->m_Type == helper::GetDataType<T>())
{
if (itExistingAttribute->second->m_Type == helper::GetDataType<T>())
if (!itExistingAttribute->second->Equals(
static_cast<const void *>(array), elements))
{

Attribute<T> &a =
static_cast<Attribute<T> &>(*itExistingAttribute->second);
a.Modify(array, elements);
Expand All @@ -208,16 +206,16 @@ IO::DefineAttribute(const std::string &name, const T *array,
globalName, itExistingAttribute->second->m_Type);
}
}
else
{
helper::Throw<std::invalid_argument>(
"Core", "IO", "DefineAttribute",
"modifiable attribute " + globalName +
" has been defined with type " +
ToString(itExistingAttribute->second->m_Type) +
". Type cannot be changed to " +
ToString(helper::GetDataType<T>()));
}
}
else
{
helper::Throw<std::invalid_argument>(
"Core", "IO", "DefineAttribute",
"modifiable attribute " + globalName +
" has been defined with type " +
ToString(itExistingAttribute->second->m_Type) +
". Type cannot be changed to " +
ToString(helper::GetDataType<T>()));
}
return static_cast<Attribute<T> &>(*itExistingAttribute->second);
}
Expand Down
44 changes: 25 additions & 19 deletions source/adios2/toolkit/format/bp5/BP5Deserializer.cpp
Expand Up @@ -158,17 +158,22 @@ DataType BP5Deserializer::TranslateFFSType2ADIOS(const char *Type, int size)
return DataType::None;
}

void BP5Deserializer::BreakdownVarName(const char *Name, char **base_name_p,
DataType *type_p, int *element_size_p)
const char *BP5Deserializer::BreakdownVarName(const char *Name,
DataType *type_p,
int *element_size_p)
{
int Type;
int ElementSize;
const char *NameStart = strchr(strchr(Name + 4, '_') + 1, '_') + 1;
// + 4 to skip BP5_ or bp5_ prefix
sscanf(Name + 4, "%d_%d_", &ElementSize, &Type);
*element_size_p = ElementSize;
// const char *NameStart = strchr(strchr(Name + 4, '_') + 1, '_') + 1;
// sscanf(Name + 4, "%d_%d", &ElementSize, &Type);
/* string formatted as bp5_%d_%d_actualname */
char *p;
// + 3 to skip BP5_ or bp5_ prefix
long n = strtol(Name + 4, &p, 10);
*element_size_p = static_cast<int>(n);
++p; // skip '_'
long Type = strtol(p, &p, 10);
*type_p = (DataType)Type;
*base_name_p = strdup(NameStart);
++p; // skip '_'
return p;
}

void BP5Deserializer::BreakdownFieldType(const char *FieldType, bool &Operator,
Expand Down Expand Up @@ -232,14 +237,16 @@ void BP5Deserializer::BreakdownV1ArrayName(const char *Name, char **base_name_p,
void BP5Deserializer::BreakdownArrayName(const char *Name, char **base_name_p,
DataType *type_p, int *element_size_p)
{
int Type;
int ElementSize;
const char *NameStart = strchr(strchr(Name + 4, '_') + 1, '_') + 1;
/* string formatted as bp5_%d_%d_actualname */
char *p;
// + 3 to skip BP5_ or bp5_ prefix
sscanf(Name + 4, "%d_%d", &ElementSize, &Type);
*element_size_p = ElementSize;
long n = strtol(Name + 4, &p, 10);
*element_size_p = static_cast<int>(n);
++p; // skip '_'
long Type = strtol(p, &p, 10);
*type_p = (DataType)Type;
*base_name_p = strdup(NameStart);
++p; // skip '_'
*base_name_p = strdup(p);
}

BP5Deserializer::BP5VarRec *BP5Deserializer::LookupVarByKey(void *Key) const
Expand Down Expand Up @@ -826,15 +833,15 @@ void BP5Deserializer::InstallAttributeData(void *AttributeBlock,
int i = 0;
while (FieldList[i].field_name)
{
char *FieldName;

void *field_data = (char *)BaseData + FieldList[i].field_offset;

if (!NameIndicatesAttrArray(FieldList[i].field_name))
{
DataType Type;
int ElemSize;
BreakdownVarName(FieldList[i].field_name, &FieldName, &Type,
&ElemSize);
const char *FieldName =
BreakdownVarName(FieldList[i].field_name, &Type, &ElemSize);
if (Type == adios2::DataType::Struct)
{
return;
Expand All @@ -858,7 +865,6 @@ void BP5Deserializer::InstallAttributeData(void *AttributeBlock,
std::cout << "Loading attribute matched no type "
<< ToString(Type) << std::endl;
}
free(FieldName);
i++;
}
else
Expand Down
4 changes: 2 additions & 2 deletions source/adios2/toolkit/format/bp5/BP5Deserializer.h
Expand Up @@ -180,8 +180,8 @@ class BP5Deserializer : virtual public BP5Base
BP5VarRec *LookupVarByName(const char *Name);
BP5VarRec *CreateVarRec(const char *ArrayName);
void ReverseDimensions(size_t *Dimensions, int count, int times);
void BreakdownVarName(const char *Name, char **base_name_p,
DataType *type_p, int *element_size_p);
const char *BreakdownVarName(const char *Name, DataType *type_p,
int *element_size_p);
void BreakdownFieldType(const char *FieldType, bool &Operator,
bool &MinMax);
void BreakdownArrayName(const char *Name, char **base_name_p,
Expand Down