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

Rename some portions of Struct interface #3409

Merged
merged 1 commit into from
Dec 15, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 27 additions & 27 deletions bindings/CXX11/adios2/cxx11/VariableNT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ StructDefinition::StructDefinition(core::StructDefinition *ptr)
{
}

void StructDefinition::AddItem(const std::string &name, const size_t offset,
const DataType type, const size_t size)
void StructDefinition::AddField(const std::string &name, const size_t offset,
const DataType type, const size_t size)
{
helper::CheckForNullptr(m_StructDefinition,
"in call to StructDefinition::AddItem");
m_StructDefinition->AddItem(name, offset, type, size);
"in call to StructDefinition::AddField");
m_StructDefinition->AddField(name, offset, type, size);
}

void StructDefinition::Freeze() noexcept
Expand All @@ -43,11 +43,11 @@ size_t StructDefinition::StructSize() const noexcept
return m_StructDefinition->StructSize();
}

size_t StructDefinition::Items() const noexcept
size_t StructDefinition::Fields() const noexcept
{
helper::CheckForNullptr(m_StructDefinition,
"in call to StructDefinition::Items");
return m_StructDefinition->Items();
"in call to StructDefinition::Fields");
return m_StructDefinition->Fields();
}
std::string StructDefinition::Name(const size_t index) const
{
Expand All @@ -67,11 +67,11 @@ DataType StructDefinition::Type(const size_t index) const
"in call to StructDefinition::Type");
return m_StructDefinition->Type(index);
}
size_t StructDefinition::Size(const size_t index) const
size_t StructDefinition::ElementCount(const size_t index) const
{
helper::CheckForNullptr(m_StructDefinition,
"in call to StructDefinition::Size");
return m_StructDefinition->Size(index);
"in call to StructDefinition::ElementCount");
return m_StructDefinition->ElementCount(index);
}

VariableNT::VariableNT(core::VariableBase *variable) : m_Variable(variable) {}
Expand Down Expand Up @@ -253,74 +253,74 @@ void VariableNT::RemoveOperations()
m_Variable->RemoveOperations();
}

size_t VariableNT::StructItems() const
size_t VariableNT::StructFields() const
{
helper::CheckForNullptr(m_Variable, "in call to VariableNT::StructItems");
helper::CheckForNullptr(m_Variable, "in call to VariableNT::StructFields");
if (m_Variable->m_Type != DataType::Struct)
{
helper::Throw<std::runtime_error>(
"bindings::CXX11", "VariableNT", "StructItems",
"bindings::CXX11", "VariableNT", "StructFields",
"invalid data type " + ToString(m_Variable->m_Type) +
", only Struct type supports this API");
}
return reinterpret_cast<core::VariableStruct *>(m_Variable)
->m_StructDefinition.Items();
->m_StructDefinition.Fields();
}
std::string VariableNT::StructItemName(const size_t index) const
std::string VariableNT::StructFieldName(const size_t index) const
{
helper::CheckForNullptr(m_Variable,
"in call to VariableNT::StructItemName");
"in call to VariableNT::StructFieldName");
if (m_Variable->m_Type != DataType::Struct)
{
helper::Throw<std::runtime_error>(
"bindings::CXX11", "VariableNT", "StructItemName",
"bindings::CXX11", "VariableNT", "StructFieldName",
"invalid data type " + ToString(m_Variable->m_Type) +
", only Struct type supports this API");
}
return reinterpret_cast<core::VariableStruct *>(m_Variable)
->m_StructDefinition.Name(index);
}
size_t VariableNT::StructItemOffset(const size_t index) const
size_t VariableNT::StructFieldOffset(const size_t index) const
{
helper::CheckForNullptr(m_Variable,
"in call to VariableNT::StructItemOffset");
"in call to VariableNT::StructFieldOffset");
if (m_Variable->m_Type != DataType::Struct)
{
helper::Throw<std::runtime_error>(
"bindings::CXX11", "VariableNT", "StructItemOffset",
"bindings::CXX11", "VariableNT", "StructFieldOffset",
"invalid data type " + ToString(m_Variable->m_Type) +
", only Struct type supports this API");
}
return reinterpret_cast<core::VariableStruct *>(m_Variable)
->m_StructDefinition.Offset(index);
}
DataType VariableNT::StructItemType(const size_t index) const
DataType VariableNT::StructFieldType(const size_t index) const
{
helper::CheckForNullptr(m_Variable,
"in call to VariableNT::StructItemType");
"in call to VariableNT::StructFieldType");
if (m_Variable->m_Type != DataType::Struct)
{
helper::Throw<std::runtime_error>(
"bindings::CXX11", "VariableNT", "StructItemType",
"bindings::CXX11", "VariableNT", "StructFieldType",
"invalid data type " + ToString(m_Variable->m_Type) +
", only Struct type supports this API");
}
return reinterpret_cast<core::VariableStruct *>(m_Variable)
->m_StructDefinition.Type(index);
}
size_t VariableNT::StructItemSize(const size_t index) const
size_t VariableNT::StructFieldElementCount(const size_t index) const
{
helper::CheckForNullptr(m_Variable,
"in call to VariableNT::StructItemSize");
"in call to VariableNT::StructFieldElementCount");
if (m_Variable->m_Type != DataType::Struct)
{
helper::Throw<std::runtime_error>(
"bindings::CXX11", "VariableNT", "StructItemSize",
"bindings::CXX11", "VariableNT", "StructFieldElementCount",
"invalid data type " + ToString(m_Variable->m_Type) +
", only Struct type supports this API");
}
return reinterpret_cast<core::VariableStruct *>(m_Variable)
->m_StructDefinition.Size(index);
->m_StructDefinition.ElementCount(index);
}

std::pair<VariableNT::T, VariableNT::T>
Expand Down
18 changes: 9 additions & 9 deletions bindings/CXX11/adios2/cxx11/VariableNT.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ class StructDefinition
{

public:
void AddItem(const std::string &name, const size_t offset,
const DataType type, const size_t size = 1);
void AddField(const std::string &name, const size_t offset,
const DataType type, const size_t size = 1);
void Freeze() noexcept;
size_t StructSize() const noexcept;
size_t Items() const noexcept;
size_t Fields() const noexcept;
std::string Name(const size_t index) const;
size_t Offset(const size_t index) const;
DataType Type(const size_t index) const;
size_t Size(const size_t index) const;
size_t ElementCount(const size_t index) const;

private:
friend class ADIOS;
Expand Down Expand Up @@ -209,11 +209,11 @@ class VariableNT
*/
void RemoveOperations();

size_t StructItems() const;
std::string StructItemName(const size_t index) const;
size_t StructItemOffset(const size_t index) const;
DataType StructItemType(const size_t index) const;
size_t StructItemSize(const size_t index) const;
size_t StructFields() const;
std::string StructFieldName(const size_t index) const;
size_t StructFieldOffset(const size_t index) const;
DataType StructFieldType(const size_t index) const;
size_t StructFieldElementCount(const size_t index) const;

union T
{
Expand Down
38 changes: 38 additions & 0 deletions source/adios2/common/ADIOSTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,4 +367,42 @@ void MinMaxStruct::Dump(DataType Type)
break;
}
}

int TypeElementSize(DataType adiosvartype)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why we are hardcoding this and not using sizeof(uint8_t) etc?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No great reason, except it was shorter to type. ADIOS wouldn't do well if the size implications of any of the types (including float and double) ever changed. (Which kind of generically excludes machines like the old KSR-1 where float was 8 bytes and double was 16.) Looking at the level of type flexibility that would let us move data between machines with different underlying data representations is an interesting topic though...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is not an issue for everywhere we run ADIOS2 now, but it would be good to think about it before we are forced to do it :) (not part of this PR)

{
switch (adiosvartype)
{
case DataType::UInt8:
return 1;
case DataType::Int8:
return 1;
case DataType::String:
return -1;
case DataType::UInt16:
return 2;
case DataType::Int16:
return 2;
case DataType::UInt32:
return 4;
case DataType::Int32:
return 4;
case DataType::UInt64:
return 8;
case DataType::Int64:
return 8;
case DataType::Float:
return 4;
case DataType::Double:
return 8;
case DataType::FloatComplex:
return 8;
case DataType::DoubleComplex:
return 16;
case DataType::LongDouble:
return 16;
default:
return -1;
}
}

} // end namespace adios2
6 changes: 6 additions & 0 deletions source/adios2/common/ADIOSTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,12 @@ using Box = std::pair<T, T>;
template <typename T, typename Enable = void>
struct TypeInfo;

/**
* Return the actual size in bytes of elements of the given type. Returns -1
* for strings.
*/
int TypeElementSize(DataType adiosvartype);

/**
* ToString
* makes a string from an enum class like ShapeID etc, for debugging etc
Expand Down
6 changes: 3 additions & 3 deletions source/adios2/core/IO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -939,12 +939,12 @@ VariableStruct *IO::InquireStructVariable(const std::string &name,
return nullptr;
}

if (ret->m_StructDefinition.Items() != def.Items())
if (ret->m_StructDefinition.Fields() != def.Fields())
{
return nullptr;
}

for (size_t i = 0; i < def.Items(); ++i)
for (size_t i = 0; i < def.Fields(); ++i)
{
if (ret->m_StructDefinition.Name(i) != def.Name(i))
{
Expand All @@ -959,7 +959,7 @@ VariableStruct *IO::InquireStructVariable(const std::string &name,
{
return nullptr;
}
if (ret->m_StructDefinition.Size(i) != def.Size(i))
if (ret->m_StructDefinition.ElementCount(i) != def.ElementCount(i))
{
return nullptr;
}
Expand Down
24 changes: 12 additions & 12 deletions source/adios2/core/VariableStruct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,42 +22,42 @@ StructDefinition::StructDefinition(const std::string &name, const size_t size)
{
}

void StructDefinition::AddItem(const std::string &name, const size_t offset,
const DataType type, const size_t size)
void StructDefinition::AddField(const std::string &name, const size_t offset,
const DataType type, const size_t elementcount)
{
if (m_Frozen)
{
helper::Throw<std::runtime_error>(
"core", "VariableStruct::StructDefinition", "AddItem",
"core", "VariableStruct::StructDefinition", "AddField",
"struct definition already frozen");
}
if (type == DataType::None || type == DataType::Struct)
{
helper::Throw<std::invalid_argument>("core",
"VariableStruct::StructDefinition",
"AddItem", "invalid data type");
"AddField", "invalid data type");
}
if (offset + helper::GetDataTypeSize(type) * size > m_StructSize)
if (offset + helper::GetDataTypeSize(type) * elementcount > m_StructSize)
{
helper::Throw<std::runtime_error>("core",
"VariableStruct::StructDefinition",
"AddItem", "exceeded struct size");
"AddField", "exceeded struct size");
}
m_Definition.emplace_back();
auto &d = m_Definition.back();
d.Name = name;
d.Offset = offset;
d.Type = type;
d.Size = size;
d.ElementCount = elementcount;
}

void StructDefinition::Freeze() noexcept { m_Frozen = true; }

size_t StructDefinition::StructSize() const noexcept { return m_StructSize; }

size_t StructDefinition::Items() const noexcept { return m_Definition.size(); }
size_t StructDefinition::Fields() const noexcept { return m_Definition.size(); }

std::string StructDefinition::Name() const noexcept { return m_Name; }
std::string StructDefinition::StructName() const noexcept { return m_Name; }

std::string StructDefinition::Name(const size_t index) const
{
Expand Down Expand Up @@ -92,15 +92,15 @@ DataType StructDefinition::Type(const size_t index) const
return m_Definition[index].Type;
}

size_t StructDefinition::Size(const size_t index) const
size_t StructDefinition::ElementCount(const size_t index) const
{
if (index >= m_Definition.size())
{
helper::Throw<std::invalid_argument>("core",
"VariableStruct::StructDefinition",
"Size", "invalid index");
"ElementCount", "invalid index");
}
return m_Definition[index].Size;
return m_Definition[index].ElementCount;
}

VariableStruct::VariableStruct(const std::string &name,
Expand Down
16 changes: 8 additions & 8 deletions source/adios2/core/VariableStruct.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,28 @@ namespace core
class StructDefinition
{
public:
struct StructItemDefinition
struct StructFieldDefinition
{
std::string Name;
size_t Offset;
DataType Type;
size_t Size;
size_t ElementCount;
};

StructDefinition(const std::string &name, const size_t size);
void AddItem(const std::string &name, const size_t offset,
const DataType type, const size_t size = 1);
void AddField(const std::string &name, const size_t offset,
const DataType type, const size_t size = 1);
void Freeze() noexcept;
size_t StructSize() const noexcept;
size_t Items() const noexcept;
std::string StructName() const noexcept;
size_t Fields() const noexcept;
std::string Name(const size_t index) const;
std::string Name() const noexcept;
size_t Offset(const size_t index) const;
DataType Type(const size_t index) const;
size_t Size(const size_t index) const;
size_t ElementCount(const size_t index) const;

private:
std::vector<StructItemDefinition> m_Definition;
std::vector<StructFieldDefinition> m_Definition;
bool m_Frozen = false;
std::string m_Name;
size_t m_StructSize;
Expand Down
10 changes: 5 additions & 5 deletions source/adios2/engine/ssc/SscHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,9 @@ void SerializeStructDefinitions(
pos += p.first.size();
output.value<uint64_t>(pos) = p.second.StructSize();
pos += 8;
output[pos] = static_cast<uint8_t>(p.second.Items());
output[pos] = static_cast<uint8_t>(p.second.Fields());
++pos;
for (size_t i = 0; i < p.second.Items(); ++i)
for (size_t i = 0; i < p.second.Fields(); ++i)
{
output[pos] = static_cast<uint8_t>(p.second.Name(i).size());
++pos;
Expand All @@ -466,7 +466,7 @@ void SerializeStructDefinitions(
pos += 8;
output.value<DataType>(pos) = p.second.Type(i);
pos += sizeof(DataType);
output.value<uint64_t>(pos) = p.second.Size(i);
output.value<uint64_t>(pos) = p.second.ElementCount(i);
pos += 8;
}
}
Expand Down Expand Up @@ -517,8 +517,8 @@ void DeserializeStructDefinitions(
pos += 8;
if (structDefinition != nullptr)
{
structDefinition->AddItem(itemName, itemOffset, itemType,
itemSize);
structDefinition->AddField(itemName, itemOffset, itemType,
itemSize);
}
}
}
Expand Down
Loading