From 14ad0e4c2a0bc072dd4635822231fcc9c953cfff Mon Sep 17 00:00:00 2001 From: Greg Eisenhauer Date: Wed, 14 Dec 2022 07:52:02 -0500 Subject: [PATCH] Rename some portions of Struct interface --- bindings/CXX11/adios2/cxx11/VariableNT.cpp | 54 +++++++-------- bindings/CXX11/adios2/cxx11/VariableNT.h | 18 ++--- source/adios2/common/ADIOSTypes.cpp | 38 +++++++++++ source/adios2/common/ADIOSTypes.h | 6 ++ source/adios2/core/IO.cpp | 6 +- source/adios2/core/VariableStruct.cpp | 24 +++---- source/adios2/core/VariableStruct.h | 16 ++--- source/adios2/engine/ssc/SscHelper.cpp | 10 +-- source/adios2/engine/ssc/SscWriterGeneric.cpp | 2 +- source/adios2/engine/ssc/SscWriterNaive.cpp | 2 +- .../toolkit/format/bp5/BP5Deserializer.cpp | 22 +++++-- .../toolkit/format/bp5/BP5Serializer.cpp | 19 ++++-- testing/adios2/engine/ssc/TestSscStruct.cpp | 23 ++++--- .../engine/staging-common/TestStructRead.cpp | 12 ++-- .../engine/staging-common/TestStructWrite.cpp | 8 ++- .../interface/TestADIOSDefineVariable.cpp | 65 +++++++++++-------- 16 files changed, 207 insertions(+), 118 deletions(-) diff --git a/bindings/CXX11/adios2/cxx11/VariableNT.cpp b/bindings/CXX11/adios2/cxx11/VariableNT.cpp index e3ef4485e6..5956d00231 100644 --- a/bindings/CXX11/adios2/cxx11/VariableNT.cpp +++ b/bindings/CXX11/adios2/cxx11/VariableNT.cpp @@ -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 @@ -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 { @@ -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) {} @@ -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( - "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(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( - "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(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( - "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(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( - "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(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( - "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(m_Variable) - ->m_StructDefinition.Size(index); + ->m_StructDefinition.ElementCount(index); } std::pair diff --git a/bindings/CXX11/adios2/cxx11/VariableNT.h b/bindings/CXX11/adios2/cxx11/VariableNT.h index ec373a6191..85b172876c 100644 --- a/bindings/CXX11/adios2/cxx11/VariableNT.h +++ b/bindings/CXX11/adios2/cxx11/VariableNT.h @@ -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; @@ -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 { diff --git a/source/adios2/common/ADIOSTypes.cpp b/source/adios2/common/ADIOSTypes.cpp index 5361f198ca..6bf3c2198b 100644 --- a/source/adios2/common/ADIOSTypes.cpp +++ b/source/adios2/common/ADIOSTypes.cpp @@ -367,4 +367,42 @@ void MinMaxStruct::Dump(DataType Type) break; } } + +int TypeElementSize(DataType adiosvartype) +{ + 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 diff --git a/source/adios2/common/ADIOSTypes.h b/source/adios2/common/ADIOSTypes.h index 4a7d7af5e7..86b51626b1 100644 --- a/source/adios2/common/ADIOSTypes.h +++ b/source/adios2/common/ADIOSTypes.h @@ -295,6 +295,12 @@ using Box = std::pair; template 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 diff --git a/source/adios2/core/IO.cpp b/source/adios2/core/IO.cpp index d86eb9dfe4..931e29a9b5 100644 --- a/source/adios2/core/IO.cpp +++ b/source/adios2/core/IO.cpp @@ -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)) { @@ -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; } diff --git a/source/adios2/core/VariableStruct.cpp b/source/adios2/core/VariableStruct.cpp index 215fc64a23..1eeb545cdc 100644 --- a/source/adios2/core/VariableStruct.cpp +++ b/source/adios2/core/VariableStruct.cpp @@ -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( - "core", "VariableStruct::StructDefinition", "AddItem", + "core", "VariableStruct::StructDefinition", "AddField", "struct definition already frozen"); } if (type == DataType::None || type == DataType::Struct) { helper::Throw("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("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 { @@ -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("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, diff --git a/source/adios2/core/VariableStruct.h b/source/adios2/core/VariableStruct.h index 480bb7e4ee..b943c2e963 100644 --- a/source/adios2/core/VariableStruct.h +++ b/source/adios2/core/VariableStruct.h @@ -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 m_Definition; + std::vector m_Definition; bool m_Frozen = false; std::string m_Name; size_t m_StructSize; diff --git a/source/adios2/engine/ssc/SscHelper.cpp b/source/adios2/engine/ssc/SscHelper.cpp index 69fc8d32ac..2efb58e80d 100644 --- a/source/adios2/engine/ssc/SscHelper.cpp +++ b/source/adios2/engine/ssc/SscHelper.cpp @@ -453,9 +453,9 @@ void SerializeStructDefinitions( pos += p.first.size(); output.value(pos) = p.second.StructSize(); pos += 8; - output[pos] = static_cast(p.second.Items()); + output[pos] = static_cast(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(p.second.Name(i).size()); ++pos; @@ -466,7 +466,7 @@ void SerializeStructDefinitions( pos += 8; output.value(pos) = p.second.Type(i); pos += sizeof(DataType); - output.value(pos) = p.second.Size(i); + output.value(pos) = p.second.ElementCount(i); pos += 8; } } @@ -517,8 +517,8 @@ void DeserializeStructDefinitions( pos += 8; if (structDefinition != nullptr) { - structDefinition->AddItem(itemName, itemOffset, itemType, - itemSize); + structDefinition->AddField(itemName, itemOffset, itemType, + itemSize); } } } diff --git a/source/adios2/engine/ssc/SscWriterGeneric.cpp b/source/adios2/engine/ssc/SscWriterGeneric.cpp index 342439b01f..9671515a5b 100644 --- a/source/adios2/engine/ssc/SscWriterGeneric.cpp +++ b/source/adios2/engine/ssc/SscWriterGeneric.cpp @@ -257,7 +257,7 @@ void SscWriterGeneric::PutDeferred(VariableBase &variable, const void *data) if (variable.m_Type == DataType::Struct) { b.structDef = reinterpret_cast(&variable) - ->m_StructDefinition.Name(); + ->m_StructDefinition.StructName(); } } else diff --git a/source/adios2/engine/ssc/SscWriterNaive.cpp b/source/adios2/engine/ssc/SscWriterNaive.cpp index 88cf58dddc..de3ebd73ec 100644 --- a/source/adios2/engine/ssc/SscWriterNaive.cpp +++ b/source/adios2/engine/ssc/SscWriterNaive.cpp @@ -178,7 +178,7 @@ void SscWriterNaive::PutDeferred(VariableBase &variable, const void *data) if (variable.m_Type == DataType::Struct) { b.structDef = reinterpret_cast(&variable) - ->m_StructDefinition.Name(); + ->m_StructDefinition.StructName(); } } } diff --git a/source/adios2/toolkit/format/bp5/BP5Deserializer.cpp b/source/adios2/toolkit/format/bp5/BP5Deserializer.cpp index 9725a3c798..118f4e55e1 100644 --- a/source/adios2/toolkit/format/bp5/BP5Deserializer.cpp +++ b/source/adios2/toolkit/format/bp5/BP5Deserializer.cpp @@ -386,10 +386,24 @@ BP5Deserializer::ControlInfo *BP5Deserializer::BuildControl(FMFormat Format) FMFieldList List = StructList[0].field_list; while (List->field_name != NULL) { - DataType Type = TranslateFFSType2ADIOS( - List->field_type, List->field_size); - Def->AddItem(List->field_name, List->field_offset, Type, - List->field_size); + char *ind = (char *)index(List->field_type, '['); + if (!ind) + { + DataType Type = TranslateFFSType2ADIOS( + List->field_type, List->field_size); + Def->AddField(List->field_name, List->field_offset, + Type); + } + else + { + *ind = 0; // terminate string temporarily + DataType Type = TranslateFFSType2ADIOS( + List->field_type, List->field_size); + size_t Count = strtol(ind + 1, NULL, 10); + Def->AddField(List->field_name, List->field_offset, + Type, Count); + *ind = '['; // restore string + } List++; } VarRec->Def = Def; diff --git a/source/adios2/toolkit/format/bp5/BP5Serializer.cpp b/source/adios2/toolkit/format/bp5/BP5Serializer.cpp index 1045479c3d..96ffebbde9 100644 --- a/source/adios2/toolkit/format/bp5/BP5Serializer.cpp +++ b/source/adios2/toolkit/format/bp5/BP5Serializer.cpp @@ -423,22 +423,31 @@ BP5Serializer::CreateWriterRec(void *Variable, const char *Name, DataType Type, core::VariableStruct *VS = static_cast(Variable); const core::StructDefinition *SD = &VS->m_StructDefinition; - FMField *List = (FMField *)malloc((SD->Items() + 1) * sizeof(List[0])); - for (size_t i = 0; i < SD->Items(); i++) + FMField *List = (FMField *)malloc((SD->Fields() + 1) * sizeof(List[0])); + for (size_t i = 0; i < SD->Fields(); i++) { List[i].field_name = strdup(SD->Name(i).c_str()); List[i].field_type = TranslateADIOS2Type2FFS(SD->Type(i)); - List[i].field_size = SD->Size(i); + List[i].field_size = TypeElementSize(SD->Type(i)); List[i].field_offset = SD->Offset(i); + if (SD->ElementCount(i) != 1) + { + size_t Len = strlen(List[i].field_type) + 10; + char *Tmp = (char *)malloc(Len); + snprintf(Tmp, Len, "%s[%d]", List[i].field_type, + (int)SD->ElementCount(i)); + free((void *)List[i].field_type); + List[i].field_type = Tmp; + } } - List[SD->Items()] = {NULL, NULL, 0, 0}; + List[SD->Fields()] = {NULL, NULL, 0, 0}; FMStructDescRec struct_list[4] = { {NULL, NULL, 0, NULL}, {"complex4", fcomplex_field_list, sizeof(fcomplex_struct), NULL}, {"complex8", dcomplex_field_list, sizeof(dcomplex_struct), NULL}, {NULL, NULL, 0, NULL}}; - struct_list[0].format_name = strdup(SD->Name().c_str()); + struct_list[0].format_name = strdup(SD->StructName().c_str()); struct_list[0].field_list = List; struct_list[0].struct_size = SD->StructSize(); diff --git a/testing/adios2/engine/ssc/TestSscStruct.cpp b/testing/adios2/engine/ssc/TestSscStruct.cpp index 788e9a3ab8..e62929c0f6 100644 --- a/testing/adios2/engine/ssc/TestSscStruct.cpp +++ b/testing/adios2/engine/ssc/TestSscStruct.cpp @@ -69,11 +69,13 @@ void Writer(const Dims &shape, const Dims &start, const Dims &count, int b[4]; }; auto particleDef = adios.DefineStruct("particle", sizeof(particle)); - particleDef.AddItem("a", 0, adios2::DataType::Int8, 1); - particleDef.AddItem("b", 4, adios2::DataType::Int32, 4); + particleDef.AddField("a", offsetof(struct particle, a), + adios2::DataType::Int8); // simple field + particleDef.AddField("b", offsetof(struct particle, b), + adios2::DataType::Int32, sizeof(int)); // static array auto varStruct = io.DefineStructVariable("particles", particleDef, shape, start, count); - EXPECT_THROW(particleDef.AddItem("c", 4, adios2::DataType::Int32, 4), + EXPECT_THROW(particleDef.AddField("c", 12, adios2::DataType::Int32), std::runtime_error); std::vector myParticles(datasize); for (size_t i = 0; i < datasize; ++i) @@ -153,13 +155,18 @@ void Reader(const Dims &shape, const Dims &start, const Dims &count, engine.LockReaderSelections(); auto particleDef1 = adios.DefineStruct("particle1", sizeof(particle)); - particleDef1.AddItem("a", 0, adios2::DataType::Int8, 1); - particleDef1.AddItem("b", 4, adios2::DataType::Int32, 4); + particleDef1.AddField("a", offsetof(struct particle, a), + adios2::DataType::Int8); + particleDef1.AddField("b", offsetof(struct particle, b), + adios2::DataType::Int32, 4); auto particleDef2 = adios.DefineStruct("particle2", sizeof(particle) + 4); - particleDef2.AddItem("a", 0, adios2::DataType::Int8, 1); - particleDef2.AddItem("b", 4, adios2::DataType::Int32, 4); - particleDef2.AddItem("c", 20, adios2::DataType::Int32, 1); + particleDef2.AddField("a", offsetof(struct particle, a), + adios2::DataType::Int8, 1); + particleDef2.AddField("b", offsetof(struct particle, b), + adios2::DataType::Int32, 4); + particleDef2.AddField("c", sizeof(particle) /* OK offset */, + adios2::DataType::Int32); while (true) { diff --git a/testing/adios2/engine/staging-common/TestStructRead.cpp b/testing/adios2/engine/staging-common/TestStructRead.cpp index f11336647f..acad02f65e 100644 --- a/testing/adios2/engine/staging-common/TestStructRead.cpp +++ b/testing/adios2/engine/staging-common/TestStructRead.cpp @@ -78,13 +78,15 @@ TEST_F(StructReadTest, ADIOS2StructRead) engine.LockReaderSelections(); auto particleDef1 = adios.DefineStruct("particle1", sizeof(particle)); - particleDef1.AddItem("a", 0, adios2::DataType::Int8, 1); - particleDef1.AddItem("b", 4, adios2::DataType::Int32, 4); + particleDef1.AddField("a", offsetof(particle, a), adios2::DataType::Int8); + particleDef1.AddField("b", offsetof(particle, b), adios2::DataType::Int32, + 4); auto particleDef2 = adios.DefineStruct("particle2", sizeof(particle) + 4); - particleDef2.AddItem("a", 0, adios2::DataType::Int8, 1); - particleDef2.AddItem("b", 4, adios2::DataType::Int32, 4); - particleDef2.AddItem("c", 20, adios2::DataType::Int32, 1); + particleDef2.AddField("a", offsetof(particle, a), adios2::DataType::Int8); + particleDef2.AddField("b", offsetof(particle, b), adios2::DataType::Int32, + 4); + particleDef2.AddField("c", 20, adios2::DataType::Int32); // arbitrary offset while (true) { diff --git a/testing/adios2/engine/staging-common/TestStructWrite.cpp b/testing/adios2/engine/staging-common/TestStructWrite.cpp index a000ce2b78..c111fbc974 100644 --- a/testing/adios2/engine/staging-common/TestStructWrite.cpp +++ b/testing/adios2/engine/staging-common/TestStructWrite.cpp @@ -93,11 +93,13 @@ TEST_F(StructWriteTest, ADIOS2CommonWrite) int b[4]; }; auto particleDef = adios.DefineStruct("particle", sizeof(particle)); - particleDef.AddItem("a", 0, adios2::DataType::Int8, 1); - particleDef.AddItem("b", 4, adios2::DataType::Int32, 4); + particleDef.AddField("a", offsetof(struct particle, a), + adios2::DataType::Int8); + particleDef.AddField("b", offsetof(struct particle, b), + adios2::DataType::Int32, 4); auto varStruct = io.DefineStructVariable("particles", particleDef, shape, start, count); - EXPECT_THROW(particleDef.AddItem("c", 4, adios2::DataType::Int32, 4), + EXPECT_THROW(particleDef.AddField("c", 4, adios2::DataType::Int32), std::runtime_error); std::vector myParticles(datasize); for (size_t i = 0; i < datasize; ++i) diff --git a/testing/adios2/interface/TestADIOSDefineVariable.cpp b/testing/adios2/interface/TestADIOSDefineVariable.cpp index bf08e98a74..8bea974933 100644 --- a/testing/adios2/interface/TestADIOSDefineVariable.cpp +++ b/testing/adios2/interface/TestADIOSDefineVariable.cpp @@ -661,22 +661,33 @@ TEST_F(ADIOSDefineVariableTest, DefineStructVariable) const adios2::Dims start = {0}; const adios2::Dims count = {10}; - auto def1 = adios.DefineStruct("def1", 24); - def1.AddItem("a", 0, adios2::DataType::Int8, 1); - def1.AddItem("b", 4, adios2::DataType::Int32, 5); - def1.Freeze(); - EXPECT_THROW(def1.AddItem("c", 0, adios2::DataType::Int32), + typedef struct def1 + { + int8_t a; + int32_t b[5]; + } def1; + auto struct1 = adios.DefineStruct("def1", sizeof(def1)); + struct1.AddField("a", offsetof(def1, a), adios2::DataType::Int8); + struct1.AddField("b", offsetof(def1, b), adios2::DataType::Int32, 5); + struct1.Freeze(); + EXPECT_THROW(struct1.AddField("c", 0, adios2::DataType::Int32), std::runtime_error); - auto def2 = adios.DefineStruct("def2", 28); - def2.AddItem("a", 0, adios2::DataType::Int8, 1); - def2.AddItem("b", 4, adios2::DataType::Int32, 5); - def2.AddItem("c", 24, adios2::DataType::Int32); - EXPECT_THROW(def2.AddItem("c", 27, adios2::DataType::Int32), + typedef struct def2 + { + int8_t a; + int32_t b[5]; + int32_t c; + } def2; + auto struct2 = adios.DefineStruct("def2", sizeof(def2)); + struct2.AddField("a", offsetof(def2, a), adios2::DataType::Int8); + struct2.AddField("b", offsetof(def2, b), adios2::DataType::Int32, 5); + struct2.AddField("c", 24, adios2::DataType::Int32); + EXPECT_THROW(struct2.AddField("c", 27, adios2::DataType::Int32), std::runtime_error); auto structVar = - io.DefineStructVariable("particle", def1, shape, start, count); + io.DefineStructVariable("particle", struct1, shape, start, count); EXPECT_EQ(structVar.Shape().size(), 1); EXPECT_EQ(structVar.Start().size(), 1); @@ -684,20 +695,20 @@ TEST_F(ADIOSDefineVariableTest, DefineStructVariable) EXPECT_EQ(structVar.Name(), "particle"); EXPECT_EQ(structVar.Type(), "struct"); EXPECT_EQ(structVar.Sizeof(), 24); - EXPECT_EQ(structVar.StructItems(), 2); - EXPECT_EQ(structVar.StructItemName(0), "a"); - EXPECT_EQ(structVar.StructItemName(1), "b"); - EXPECT_EQ(structVar.StructItemOffset(0), 0); - EXPECT_EQ(structVar.StructItemOffset(1), 4); - EXPECT_EQ(structVar.StructItemType(0), adios2::DataType::Int8); - EXPECT_EQ(structVar.StructItemType(1), adios2::DataType::Int32); - EXPECT_EQ(structVar.StructItemSize(0), 1); - EXPECT_EQ(structVar.StructItemSize(1), 5); - - EXPECT_THROW(structVar.StructItemName(2), std::invalid_argument); - EXPECT_THROW(structVar.StructItemOffset(2), std::invalid_argument); - EXPECT_THROW(structVar.StructItemType(2), std::invalid_argument); - EXPECT_THROW(structVar.StructItemSize(2), std::invalid_argument); + EXPECT_EQ(structVar.StructFields(), 2); + EXPECT_EQ(structVar.StructFieldName(0), "a"); + EXPECT_EQ(structVar.StructFieldName(1), "b"); + EXPECT_EQ(structVar.StructFieldOffset(0), 0); + EXPECT_EQ(structVar.StructFieldOffset(1), offsetof(def1, b)); + EXPECT_EQ(structVar.StructFieldType(0), adios2::DataType::Int8); + EXPECT_EQ(structVar.StructFieldType(1), adios2::DataType::Int32); + EXPECT_EQ(structVar.StructFieldElementCount(0), 1); + EXPECT_EQ(structVar.StructFieldElementCount(1), 5); + + EXPECT_THROW(structVar.StructFieldName(2), std::invalid_argument); + EXPECT_THROW(structVar.StructFieldOffset(2), std::invalid_argument); + EXPECT_THROW(structVar.StructFieldType(2), std::invalid_argument); + EXPECT_THROW(structVar.StructFieldElementCount(2), std::invalid_argument); auto inquire1 = io.InquireVariable("particle"); EXPECT_TRUE(inquire1); @@ -705,10 +716,10 @@ TEST_F(ADIOSDefineVariableTest, DefineStructVariable) auto inquire2 = io.InquireStructVariable("particle"); EXPECT_TRUE(inquire2); - auto inquire3 = io.InquireStructVariable("particle", def1); + auto inquire3 = io.InquireStructVariable("particle", struct1); EXPECT_TRUE(inquire3); - auto inquire4 = io.InquireStructVariable("particle", def2); + auto inquire4 = io.InquireStructVariable("particle", struct2); EXPECT_FALSE(inquire4); }