From f211a11954220c58d206725152e58ab21e85a54b Mon Sep 17 00:00:00 2001 From: Jason Wang Date: Thu, 8 Apr 2021 23:46:25 -0400 Subject: [PATCH 1/5] renamed reserve to resize --- source/adios2/engine/ssc/SscHelper.cpp | 16 ++++++++-------- source/adios2/engine/ssc/SscHelper.h | 6 +++--- source/adios2/engine/ssc/SscReader.cpp | 8 ++++---- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/source/adios2/engine/ssc/SscHelper.cpp b/source/adios2/engine/ssc/SscHelper.cpp index da0b129dc9..56f6d057d4 100644 --- a/source/adios2/engine/ssc/SscHelper.cpp +++ b/source/adios2/engine/ssc/SscHelper.cpp @@ -122,9 +122,9 @@ void SerializeVariables(const BlockVec &input, Buffer &output, const int rank) for (const auto &b : input) { uint64_t pos = output.value(); - if (pos + 1024 > output.capacity()) + if (pos + 1024 > output.size()) { - output.reserve((pos + 1024) * 2); + output.resize((pos + 1024) * 2); } if (pos == 0) { @@ -189,9 +189,9 @@ void SerializeAttributes(IO &input, Buffer &output) for (const auto &attributePair : attributeMap) { uint64_t pos = output.value(); - if (pos + 1024 > output.capacity()) + if (pos + 1024 > output.size()) { - output.reserve((pos + 1024) * 2); + output.resize((pos + 1024) * 2); } if (pos == 0) { @@ -432,7 +432,7 @@ void AggregateMetadata(const Buffer &localBuffer, Buffer &globalBuffer, std::vector localSizes(mpiSize); MPI_Gather(&localSize, 1, MPI_INT, localSizes.data(), 1, MPI_INT, 0, comm); int globalSize = std::accumulate(localSizes.begin(), localSizes.end(), 0); - globalBuffer.reserve(globalSize + 10); + globalBuffer.resize(globalSize + 10); std::vector displs(mpiSize); for (size_t i = 1; i < mpiSize; ++i) @@ -450,11 +450,11 @@ void AggregateMetadata(const Buffer &localBuffer, Buffer &globalBuffer, void BroadcastMetadata(Buffer &globalBuffer, const int root, MPI_Comm comm) { - int globalBufferSize = static_cast(globalBuffer.capacity()); + int globalBufferSize = static_cast(globalBuffer.size()); MPI_Bcast(&globalBufferSize, 1, MPI_INT, root, comm); - if (globalBuffer.capacity() < globalBufferSize) + if (globalBuffer.size() < globalBufferSize) { - globalBuffer.reserve(globalBufferSize); + globalBuffer.resize(globalBufferSize); } MPI_Bcast(globalBuffer.data(), globalBufferSize, MPI_CHAR, root, comm); } diff --git a/source/adios2/engine/ssc/SscHelper.h b/source/adios2/engine/ssc/SscHelper.h index 1c800045b4..49b88e1850 100644 --- a/source/adios2/engine/ssc/SscHelper.h +++ b/source/adios2/engine/ssc/SscHelper.h @@ -34,7 +34,7 @@ class Buffer m_Buffer = reinterpret_cast(malloc(1)); m_Capacity = 1; } - Buffer(size_t capacity) + Buffer(const size_t capacity) { m_Buffer = reinterpret_cast(malloc(capacity)); m_Capacity = capacity; @@ -53,7 +53,7 @@ class Buffer m_Buffer = reinterpret_cast(realloc(m_Buffer, 1)); m_Capacity = 1; } - void reserve(size_t capacity) + void resize(const size_t capacity) { m_Buffer = reinterpret_cast(realloc(m_Buffer, capacity)); m_Capacity = capacity; @@ -94,7 +94,7 @@ class Buffer { return reinterpret_cast(m_Buffer + pos); } - size_t capacity() const { return m_Capacity; } + size_t size() const { return m_Capacity; } uint8_t &operator[](const size_t pos) { return *(m_Buffer + pos); } const uint8_t &operator[](const size_t pos) const { diff --git a/source/adios2/engine/ssc/SscReader.cpp b/source/adios2/engine/ssc/SscReader.cpp index 71206571d2..8cb342f05c 100644 --- a/source/adios2/engine/ssc/SscReader.cpp +++ b/source/adios2/engine/ssc/SscReader.cpp @@ -67,7 +67,7 @@ void SscReader::BeginStepConsequentFixed() void SscReader::BeginStepFlexible(StepStatus &status) { m_AllReceivingWriterRanks.clear(); - m_Buffer.reserve(1); + m_Buffer.resize(1); m_Buffer[0] = 0; m_GlobalWritePattern.clear(); m_GlobalWritePattern.resize(m_StreamSize); @@ -200,7 +200,7 @@ void SscReader::PerformGets() { totalDataSize += i.second.second; } - m_Buffer.reserve(totalDataSize); + m_Buffer.resize(totalDataSize); for (const auto &i : m_AllReceivingWriterRanks) { MPI_Win_lock(MPI_LOCK_SHARED, i.first, 0, m_MpiWin); @@ -278,7 +278,7 @@ void SscReader::EndStepFixed() { MPI_Win_free(&m_MpiWin); SyncReadPattern(); - MPI_Win_create(m_Buffer.data(), m_Buffer.capacity(), 1, MPI_INFO_NULL, + MPI_Win_create(m_Buffer.data(), m_Buffer.size(), 1, MPI_INFO_NULL, m_StreamComm, &m_MpiWin); } if (m_MpiMode == "twosided") @@ -485,7 +485,7 @@ void SscReader::SyncReadPattern() { totalDataSize += i.second.second; } - m_Buffer.reserve(totalDataSize); + m_Buffer.resize(totalDataSize); if (m_Verbosity >= 20) { From c381166e547081fb5220ac578e5f0bbd7779ac9b Mon Sep 17 00:00:00 2001 From: Jason Wang Date: Fri, 9 Apr 2021 00:51:02 -0400 Subject: [PATCH 2/5] merged redundant constructors --- source/adios2/engine/ssc/SscHelper.h | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/source/adios2/engine/ssc/SscHelper.h b/source/adios2/engine/ssc/SscHelper.h index 49b88e1850..88422677c5 100644 --- a/source/adios2/engine/ssc/SscHelper.h +++ b/source/adios2/engine/ssc/SscHelper.h @@ -29,12 +29,7 @@ namespace ssc class Buffer { public: - Buffer() - { - m_Buffer = reinterpret_cast(malloc(1)); - m_Capacity = 1; - } - Buffer(const size_t capacity) + Buffer(const size_t capacity = 1) { m_Buffer = reinterpret_cast(malloc(capacity)); m_Capacity = capacity; @@ -44,8 +39,6 @@ class Buffer if (m_Buffer) { free(m_Buffer); - m_Capacity = 0; - m_Buffer = nullptr; } } void clear() From df62e6fa6d6d5153903167013f97774c9a3ec0ae Mon Sep 17 00:00:00 2001 From: Jason Wang Date: Fri, 9 Apr 2021 01:36:34 -0400 Subject: [PATCH 3/5] set local buffer default pos to 8 --- source/adios2/engine/ssc/SscHelper.cpp | 18 ++---------------- source/adios2/engine/ssc/SscHelper.h | 1 + source/adios2/engine/ssc/SscReader.cpp | 2 +- source/adios2/engine/ssc/SscWriter.cpp | 2 +- 4 files changed, 5 insertions(+), 18 deletions(-) diff --git a/source/adios2/engine/ssc/SscHelper.cpp b/source/adios2/engine/ssc/SscHelper.cpp index 56f6d057d4..1869dfcd86 100644 --- a/source/adios2/engine/ssc/SscHelper.cpp +++ b/source/adios2/engine/ssc/SscHelper.cpp @@ -122,14 +122,7 @@ void SerializeVariables(const BlockVec &input, Buffer &output, const int rank) for (const auto &b : input) { uint64_t pos = output.value(); - if (pos + 1024 > output.size()) - { - output.resize((pos + 1024) * 2); - } - if (pos == 0) - { - pos += 8; - } + output.resize((pos + 1024) * 2); output.value(pos) = static_cast(b.shapeId); ++pos; @@ -189,14 +182,7 @@ void SerializeAttributes(IO &input, Buffer &output) for (const auto &attributePair : attributeMap) { uint64_t pos = output.value(); - if (pos + 1024 > output.size()) - { - output.resize((pos + 1024) * 2); - } - if (pos == 0) - { - pos += 8; - } + output.resize((pos + 1024) * 2); if (attributePair.second->m_Type == DataType::String) { diff --git a/source/adios2/engine/ssc/SscHelper.h b/source/adios2/engine/ssc/SscHelper.h index 88422677c5..ef87818652 100644 --- a/source/adios2/engine/ssc/SscHelper.h +++ b/source/adios2/engine/ssc/SscHelper.h @@ -96,6 +96,7 @@ class Buffer private: size_t m_Capacity = 0; + size_t m_Size = 0; uint8_t *m_Buffer = nullptr; }; diff --git a/source/adios2/engine/ssc/SscReader.cpp b/source/adios2/engine/ssc/SscReader.cpp index 8cb342f05c..0e4f0794d3 100644 --- a/source/adios2/engine/ssc/SscReader.cpp +++ b/source/adios2/engine/ssc/SscReader.cpp @@ -461,7 +461,7 @@ void SscReader::SyncReadPattern() } ssc::Buffer localBuffer(8); - localBuffer.value() = 0; + localBuffer.value() = 8; ssc::SerializeVariables(m_LocalReadPattern, localBuffer, m_StreamRank); diff --git a/source/adios2/engine/ssc/SscWriter.cpp b/source/adios2/engine/ssc/SscWriter.cpp index a11c27e731..1d635e5548 100644 --- a/source/adios2/engine/ssc/SscWriter.cpp +++ b/source/adios2/engine/ssc/SscWriter.cpp @@ -277,7 +277,7 @@ void SscWriter::SyncWritePattern(bool finalStep) } ssc::Buffer localBuffer(8); - localBuffer.value() = 0; + localBuffer.value() = 8; ssc::SerializeVariables(m_GlobalWritePattern[m_StreamRank], localBuffer, m_StreamRank); From 984a59c528d1d74ab3cf01b5f888b0ed96f60dd4 Mon Sep 17 00:00:00 2001 From: Jason Wang Date: Fri, 9 Apr 2021 02:51:28 -0400 Subject: [PATCH 4/5] refined ssc buffer resize --- source/adios2/engine/ssc/SscHelper.cpp | 4 ++-- source/adios2/engine/ssc/SscHelper.h | 20 ++++++++++++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/source/adios2/engine/ssc/SscHelper.cpp b/source/adios2/engine/ssc/SscHelper.cpp index 1869dfcd86..b5cf30a550 100644 --- a/source/adios2/engine/ssc/SscHelper.cpp +++ b/source/adios2/engine/ssc/SscHelper.cpp @@ -122,7 +122,7 @@ void SerializeVariables(const BlockVec &input, Buffer &output, const int rank) for (const auto &b : input) { uint64_t pos = output.value(); - output.resize((pos + 1024) * 2); + output.resize(pos + 1024); output.value(pos) = static_cast(b.shapeId); ++pos; @@ -182,7 +182,7 @@ void SerializeAttributes(IO &input, Buffer &output) for (const auto &attributePair : attributeMap) { uint64_t pos = output.value(); - output.resize((pos + 1024) * 2); + output.resize(pos + 1024); if (attributePair.second->m_Type == DataType::String) { diff --git a/source/adios2/engine/ssc/SscHelper.h b/source/adios2/engine/ssc/SscHelper.h index ef87818652..5107c80c33 100644 --- a/source/adios2/engine/ssc/SscHelper.h +++ b/source/adios2/engine/ssc/SscHelper.h @@ -33,6 +33,7 @@ class Buffer { m_Buffer = reinterpret_cast(malloc(capacity)); m_Capacity = capacity; + m_Size = 0; } ~Buffer() { @@ -45,11 +46,21 @@ class Buffer { m_Buffer = reinterpret_cast(realloc(m_Buffer, 1)); m_Capacity = 1; + m_Size = 0; } - void resize(const size_t capacity) + void resize(const size_t size) { - m_Buffer = reinterpret_cast(realloc(m_Buffer, capacity)); - m_Capacity = capacity; + m_Size = size; + if (size > m_Capacity) + { + m_Capacity = size * m_Factor; + m_Buffer = + reinterpret_cast(realloc(m_Buffer, m_Capacity)); + } + if (m_Buffer == nullptr) + { + throw("ssc buffer realloc failed"); + } } template T &value(const size_t pos = 0) @@ -87,7 +98,7 @@ class Buffer { return reinterpret_cast(m_Buffer + pos); } - size_t size() const { return m_Capacity; } + size_t size() const { return m_Size; } uint8_t &operator[](const size_t pos) { return *(m_Buffer + pos); } const uint8_t &operator[](const size_t pos) const { @@ -97,6 +108,7 @@ class Buffer private: size_t m_Capacity = 0; size_t m_Size = 0; + float m_Factor = 1.5; uint8_t *m_Buffer = nullptr; }; From 3da0310b4876c3b12f1261be066500fcc3fd9c3b Mon Sep 17 00:00:00 2001 From: Jason Wang Date: Fri, 9 Apr 2021 03:08:38 -0400 Subject: [PATCH 5/5] fix warning --- source/adios2/engine/ssc/SscHelper.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/source/adios2/engine/ssc/SscHelper.h b/source/adios2/engine/ssc/SscHelper.h index 5107c80c33..1507323938 100644 --- a/source/adios2/engine/ssc/SscHelper.h +++ b/source/adios2/engine/ssc/SscHelper.h @@ -53,7 +53,7 @@ class Buffer m_Size = size; if (size > m_Capacity) { - m_Capacity = size * m_Factor; + m_Capacity = size * 2; m_Buffer = reinterpret_cast(realloc(m_Buffer, m_Capacity)); } @@ -108,7 +108,6 @@ class Buffer private: size_t m_Capacity = 0; size_t m_Size = 0; - float m_Factor = 1.5; uint8_t *m_Buffer = nullptr; };