From a5fbc19a0ba3dc2288f5e38954b63a605dbd5ad1 Mon Sep 17 00:00:00 2001 From: William F Godoy Date: Tue, 9 Jul 2019 09:20:24 -0400 Subject: [PATCH 1/2] Refactoring Buffer under format Created Buffer base class BufferSTL extends Buffer Accomodate for BP3 and BP4 --- source/adios2/CMakeLists.txt | 3 +- source/adios2/engine/bp3/BP3Writer.cpp | 4 +- source/adios2/engine/bp4/BP4Writer.cpp | 8 ++-- source/adios2/engine/bp4/BP4Writer.h | 8 ++-- .../toolkit/aggregator/mpi/MPIAggregator.cpp | 3 +- .../toolkit/aggregator/mpi/MPIAggregator.h | 10 ++-- .../toolkit/aggregator/mpi/MPIChain.cpp | 20 ++++---- .../adios2/toolkit/aggregator/mpi/MPIChain.h | 16 ++++--- source/adios2/toolkit/format/BufferSTL.h | 42 ----------------- source/adios2/toolkit/format/bp3/BP3Base.h | 2 +- source/adios2/toolkit/format/bp4/BP4Base.h | 2 +- .../toolkit/format/bpOperation/BPOperation.h | 2 +- .../adios2/toolkit/format/buffer/Buffer.cpp | 30 ++++++++++++ source/adios2/toolkit/format/buffer/Buffer.h | 46 +++++++++++++++++++ .../toolkit/format/{ => buffer}/BufferSTL.cpp | 6 +++ .../adios2/toolkit/format/buffer/BufferSTL.h | 37 +++++++++++++++ 16 files changed, 161 insertions(+), 78 deletions(-) delete mode 100644 source/adios2/toolkit/format/BufferSTL.h create mode 100644 source/adios2/toolkit/format/buffer/Buffer.cpp create mode 100644 source/adios2/toolkit/format/buffer/Buffer.h rename source/adios2/toolkit/format/{ => buffer}/BufferSTL.cpp (85%) create mode 100644 source/adios2/toolkit/format/buffer/BufferSTL.h diff --git a/source/adios2/CMakeLists.txt b/source/adios2/CMakeLists.txt index 01b804918c..304b97f607 100644 --- a/source/adios2/CMakeLists.txt +++ b/source/adios2/CMakeLists.txt @@ -59,7 +59,8 @@ add_library(adios2 engine/nullcore/NullCoreWriter.cpp engine/nullcore/NullCoreWriter.tcc #toolkit - toolkit/format/BufferSTL.cpp + toolkit/format/buffer/Buffer.cpp + toolkit/format/buffer/BufferSTL.cpp toolkit/format/bp3/BP3Base.cpp toolkit/format/bp3/BP3Base.tcc toolkit/format/bp3/BP3Serializer.cpp toolkit/format/bp3/BP3Serializer.tcc diff --git a/source/adios2/engine/bp3/BP3Writer.cpp b/source/adios2/engine/bp3/BP3Writer.cpp index e7d2d8762e..2037eae6b9 100644 --- a/source/adios2/engine/bp3/BP3Writer.cpp +++ b/source/adios2/engine/bp3/BP3Writer.cpp @@ -353,7 +353,7 @@ void BP3Writer::AggregateWriteData(const bool isFinal, const int transportIndex) if (m_BP3Serializer.m_Aggregator.m_IsConsumer) { - const BufferSTL &bufferSTL = + const format::BufferSTL &bufferSTL = m_BP3Serializer.m_Aggregator.GetConsumerBuffer( m_BP3Serializer.m_Data); @@ -374,7 +374,7 @@ void BP3Writer::AggregateWriteData(const bool isFinal, const int transportIndex) if (isFinal) // Write metadata footer { - BufferSTL &bufferSTL = m_BP3Serializer.m_Data; + format::BufferSTL &bufferSTL = m_BP3Serializer.m_Data; m_BP3Serializer.ResetBuffer(bufferSTL, false, false); m_BP3Serializer.AggregateCollectiveMetadata( diff --git a/source/adios2/engine/bp4/BP4Writer.cpp b/source/adios2/engine/bp4/BP4Writer.cpp index 05d434ac0e..f77fc3456d 100644 --- a/source/adios2/engine/bp4/BP4Writer.cpp +++ b/source/adios2/engine/bp4/BP4Writer.cpp @@ -221,7 +221,7 @@ void BP4Writer::InitBPBuffer() // throw std::invalid_argument( // "ADIOS2: OpenMode Append hasn't been implemented, yet"); // TODO: Get last pg timestep and update timestep counter in - BufferSTL preMetadataIndex; + format::BufferSTL preMetadataIndex; size_t preMetadataIndexFileSize; if (m_BP4Serializer.m_RankMPI == 0) @@ -411,7 +411,7 @@ void BP4Writer::WriteProfilingJSONFile() /*write the content of metadata index file*/ void BP4Writer::PopulateMetadataIndexFileContent( - BufferSTL &b, const uint64_t currentStep, const uint64_t mpirank, + format::BufferSTL &b, const uint64_t currentStep, const uint64_t mpirank, const uint64_t pgIndexStart, const uint64_t variablesIndexStart, const uint64_t attributesIndexStart, const uint64_t currentStepEndPos, const uint64_t currentTimeStamp) @@ -486,7 +486,7 @@ void BP4Writer::WriteCollectiveMetadataFile(const bool isFinal) m_BP4Serializer.m_Metadata.m_Position + m_BP4Serializer.m_PreMetadataFileLength; - BufferSTL metadataIndex; + format::BufferSTL metadataIndex; metadataIndex.Resize(128, "BP4 Index Table Entry"); uint64_t currentStep; @@ -575,7 +575,7 @@ void BP4Writer::AggregateWriteData(const bool isFinal, const int transportIndex) if (m_BP4Serializer.m_Aggregator.m_IsConsumer) { - const BufferSTL &bufferSTL = + const format::BufferSTL &bufferSTL = m_BP4Serializer.m_Aggregator.GetConsumerBuffer( m_BP4Serializer.m_Data); if (bufferSTL.m_Position > 0) diff --git a/source/adios2/engine/bp4/BP4Writer.h b/source/adios2/engine/bp4/BP4Writer.h index 07b260590d..9ad11dbb6a 100644 --- a/source/adios2/engine/bp4/BP4Writer.h +++ b/source/adios2/engine/bp4/BP4Writer.h @@ -97,10 +97,10 @@ class BP4Writer : public core::Engine void UpdateActiveFlag(const bool active); void PopulateMetadataIndexFileContent( - BufferSTL &buffer, const uint64_t currentStep, const uint64_t mpirank, - const uint64_t pgIndexStart, const uint64_t variablesIndexStart, - const uint64_t attributesIndexStart, const uint64_t currentStepEndPos, - const uint64_t currentTimeStamp); + format::BufferSTL &buffer, const uint64_t currentStep, + const uint64_t mpirank, const uint64_t pgIndexStart, + const uint64_t variablesIndexStart, const uint64_t attributesIndexStart, + const uint64_t currentStepEndPos, const uint64_t currentTimeStamp); void WriteCollectiveMetadataFile(const bool isFinal = false); diff --git a/source/adios2/toolkit/aggregator/mpi/MPIAggregator.cpp b/source/adios2/toolkit/aggregator/mpi/MPIAggregator.cpp index af46b163d0..ee1dea549f 100644 --- a/source/adios2/toolkit/aggregator/mpi/MPIAggregator.cpp +++ b/source/adios2/toolkit/aggregator/mpi/MPIAggregator.cpp @@ -35,7 +35,8 @@ void MPIAggregator::SwapBuffers(const int step) noexcept {} void MPIAggregator::ResetBuffers() noexcept {} -BufferSTL &MPIAggregator::GetConsumerBuffer(BufferSTL &bufferSTL) +format::BufferSTL & +MPIAggregator::GetConsumerBuffer(format::BufferSTL &bufferSTL) { return bufferSTL; } diff --git a/source/adios2/toolkit/aggregator/mpi/MPIAggregator.h b/source/adios2/toolkit/aggregator/mpi/MPIAggregator.h index 2ed3da606b..6e8f2244c7 100644 --- a/source/adios2/toolkit/aggregator/mpi/MPIAggregator.h +++ b/source/adios2/toolkit/aggregator/mpi/MPIAggregator.h @@ -13,7 +13,7 @@ #include "adios2/common/ADIOSMPI.h" #include "adios2/common/ADIOSTypes.h" -#include "adios2/toolkit/format/BufferSTL.h" +#include "adios2/toolkit/format/buffer/BufferSTL.h" namespace adios2 { @@ -58,10 +58,10 @@ class MPIAggregator virtual void Init(const size_t subStreams, MPI_Comm parentComm); virtual std::vector> - IExchange(BufferSTL &bufferSTL, const int step) = 0; + IExchange(format::BufferSTL &bufferSTL, const int step) = 0; virtual std::vector> - IExchangeAbsolutePosition(BufferSTL &bufferSTL, const int step) = 0; + IExchangeAbsolutePosition(format::BufferSTL &bufferSTL, const int step) = 0; virtual void WaitAbsolutePosition(std::vector> &requests, @@ -74,7 +74,7 @@ class MPIAggregator virtual void ResetBuffers() noexcept; - virtual BufferSTL &GetConsumerBuffer(BufferSTL &bufferSTL); + virtual format::BufferSTL &GetConsumerBuffer(format::BufferSTL &bufferSTL); /** closes current aggregator, frees m_Comm */ void Close(); @@ -88,7 +88,7 @@ class MPIAggregator void HandshakeRank(const int rank = 0); /** assigning extra buffers for aggregation */ - std::vector m_Buffers; + std::vector m_Buffers; }; } // end namespace aggregator diff --git a/source/adios2/toolkit/aggregator/mpi/MPIChain.cpp b/source/adios2/toolkit/aggregator/mpi/MPIChain.cpp index 470282f56a..9e32169cd4 100644 --- a/source/adios2/toolkit/aggregator/mpi/MPIChain.cpp +++ b/source/adios2/toolkit/aggregator/mpi/MPIChain.cpp @@ -32,15 +32,15 @@ void MPIChain::Init(const size_t subStreams, MPI_Comm parentComm) } } -std::vector> MPIChain::IExchange(BufferSTL &bufferSTL, - const int step) +std::vector> +MPIChain::IExchange(format::BufferSTL &bufferSTL, const int step) { if (m_Size == 1) { return std::vector>(); } - BufferSTL &sendBuffer = GetSender(bufferSTL); + format::BufferSTL &sendBuffer = GetSender(bufferSTL); const int endRank = m_Size - 1 - step; const bool sender = (m_Rank >= 1 && m_Rank <= endRank) ? true : false; const bool receiver = (m_Rank < endRank) ? true : false; @@ -88,7 +88,7 @@ std::vector> MPIChain::IExchange(BufferSTL &bufferSTL, ", aggregation waiting for receiver size at iteration " + std::to_string(step) + "\n"); - BufferSTL &receiveBuffer = GetReceiver(bufferSTL); + format::BufferSTL &receiveBuffer = GetReceiver(bufferSTL); ResizeUpdateBufferSTL( bufferSize, receiveBuffer, "in aggregation, when resizing receiving buffer to size " + @@ -109,7 +109,8 @@ std::vector> MPIChain::IExchange(BufferSTL &bufferSTL, } std::vector> -MPIChain::IExchangeAbsolutePosition(BufferSTL &bufferSTL, const int step) +MPIChain::IExchangeAbsolutePosition(format::BufferSTL &bufferSTL, + const int step) { if (m_Size == 1) { @@ -235,7 +236,7 @@ void MPIChain::SwapBuffers(const int /*step*/) noexcept void MPIChain::ResetBuffers() noexcept { m_CurrentBufferOrder = 0; } -BufferSTL &MPIChain::GetConsumerBuffer(BufferSTL &bufferSTL) +format::BufferSTL &MPIChain::GetConsumerBuffer(format::BufferSTL &bufferSTL) { return GetSender(bufferSTL); } @@ -276,7 +277,7 @@ void MPIChain::HandshakeLinks() } } -BufferSTL &MPIChain::GetSender(BufferSTL &bufferSTL) +format::BufferSTL &MPIChain::GetSender(format::BufferSTL &bufferSTL) { if (m_CurrentBufferOrder == 0) { @@ -288,7 +289,7 @@ BufferSTL &MPIChain::GetSender(BufferSTL &bufferSTL) } } -BufferSTL &MPIChain::GetReceiver(BufferSTL &bufferSTL) +format::BufferSTL &MPIChain::GetReceiver(format::BufferSTL &bufferSTL) { if (m_CurrentBufferOrder == 0) { @@ -300,7 +301,8 @@ BufferSTL &MPIChain::GetReceiver(BufferSTL &bufferSTL) } } -void MPIChain::ResizeUpdateBufferSTL(const size_t newSize, BufferSTL &bufferSTL, +void MPIChain::ResizeUpdateBufferSTL(const size_t newSize, + format::BufferSTL &bufferSTL, const std::string hint) { bufferSTL.Resize(newSize, hint); diff --git a/source/adios2/toolkit/aggregator/mpi/MPIChain.h b/source/adios2/toolkit/aggregator/mpi/MPIChain.h index e2f107ee2f..25ab7502cf 100644 --- a/source/adios2/toolkit/aggregator/mpi/MPIChain.h +++ b/source/adios2/toolkit/aggregator/mpi/MPIChain.h @@ -28,11 +28,12 @@ class MPIChain : public MPIAggregator void Init(const size_t subStreams, MPI_Comm parentComm) final; - std::vector> IExchange(BufferSTL &bufferSTL, - const int step) final; + std::vector> + IExchange(format::BufferSTL &bufferSTL, const int step) final; std::vector> - IExchangeAbsolutePosition(BufferSTL &bufferSTL, const int step) final; + IExchangeAbsolutePosition(format::BufferSTL &bufferSTL, + const int step) final; void Wait(std::vector> &request, const int step) final; @@ -44,7 +45,7 @@ class MPIChain : public MPIAggregator void ResetBuffers() noexcept final; - BufferSTL &GetConsumerBuffer(BufferSTL &bufferSTL) final; + format::BufferSTL &GetConsumerBuffer(format::BufferSTL &bufferSTL) final; private: bool m_IsInExchangeAbsolutePosition = false; @@ -65,7 +66,7 @@ class MPIChain : public MPIAggregator * @param bufferSTL original buffer from serializer * @return reference to sender buffer */ - BufferSTL &GetSender(BufferSTL &bufferSTL); + format::BufferSTL &GetSender(format::BufferSTL &bufferSTL); /** * Returns a reference to the receiver buffer depending on @@ -74,7 +75,7 @@ class MPIChain : public MPIAggregator * @param bufferSTL original buffer from serializer * @return reference to receiver buffer */ - BufferSTL &GetReceiver(BufferSTL &bufferSTL); + format::BufferSTL &GetReceiver(format::BufferSTL &bufferSTL); /** * Resizes and updates m_Position in a bufferSTL, used for receiving buffers @@ -82,7 +83,8 @@ class MPIChain : public MPIAggregator * @param bufferSTL to be resized * @param hint used in exception error message */ - void ResizeUpdateBufferSTL(const size_t newSize, BufferSTL &bufferSTL, + void ResizeUpdateBufferSTL(const size_t newSize, + format::BufferSTL &bufferSTL, const std::string hint); }; diff --git a/source/adios2/toolkit/format/BufferSTL.h b/source/adios2/toolkit/format/BufferSTL.h deleted file mode 100644 index ab9679f3e3..0000000000 --- a/source/adios2/toolkit/format/BufferSTL.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Distributed under the OSI-approved Apache License, Version 2.0. See - * accompanying file Copyright.txt for details. - * - * BufferSTL.h - * - * Created on: Sep 26, 2017 - * Author: William F Godoy godoywf@ornl.gov - */ - -#ifndef ADIOS2_TOOLKIT_FORMAT_BUFFERSTL_H_ -#define ADIOS2_TOOLKIT_FORMAT_BUFFERSTL_H_ - -#include -#include - -#include "adios2/common/ADIOSTypes.h" - -namespace adios2 -{ - -class BufferSTL -{ -public: - std::vector m_Buffer; - size_t m_Position = 0; - size_t m_AbsolutePosition = 0; - - BufferSTL() = default; - ~BufferSTL() = default; - - void Resize(const size_t size, const std::string hint); - - size_t GetAvailableSize() const; - -private: - const bool m_DebugMode = false; -}; - -} // end namespace adios2 - -#endif /* ADIOS2_TOOLKIT_FORMAT_STLBUFFER_H_ */ diff --git a/source/adios2/toolkit/format/bp3/BP3Base.h b/source/adios2/toolkit/format/bp3/BP3Base.h index 85a1a28828..eb9fcfc4c3 100644 --- a/source/adios2/toolkit/format/bp3/BP3Base.h +++ b/source/adios2/toolkit/format/bp3/BP3Base.h @@ -28,8 +28,8 @@ #include "adios2/core/Engine.h" #include "adios2/core/VariableBase.h" #include "adios2/toolkit/aggregator/mpi/MPIChain.h" -#include "adios2/toolkit/format/BufferSTL.h" #include "adios2/toolkit/format/bpOperation/BPOperation.h" +#include "adios2/toolkit/format/buffer/BufferSTL.h" #include "adios2/toolkit/profiling/iochrono/IOChrono.h" namespace adios2 diff --git a/source/adios2/toolkit/format/bp4/BP4Base.h b/source/adios2/toolkit/format/bp4/BP4Base.h index 2b7c13ef0e..69fea9bda0 100644 --- a/source/adios2/toolkit/format/bp4/BP4Base.h +++ b/source/adios2/toolkit/format/bp4/BP4Base.h @@ -28,8 +28,8 @@ #include "adios2/core/Engine.h" #include "adios2/core/VariableBase.h" #include "adios2/toolkit/aggregator/mpi/MPIChain.h" -#include "adios2/toolkit/format/BufferSTL.h" #include "adios2/toolkit/format/bpOperation/BPOperation.h" +#include "adios2/toolkit/format/buffer/BufferSTL.h" #include "adios2/toolkit/profiling/iochrono/IOChrono.h" namespace adios2 diff --git a/source/adios2/toolkit/format/bpOperation/BPOperation.h b/source/adios2/toolkit/format/bpOperation/BPOperation.h index cfc52a9c36..088d9fcb2a 100644 --- a/source/adios2/toolkit/format/bpOperation/BPOperation.h +++ b/source/adios2/toolkit/format/bpOperation/BPOperation.h @@ -17,7 +17,7 @@ #include "adios2/common/ADIOSMacros.h" #include "adios2/core/Variable.h" #include "adios2/helper/adiosFunctions.h" -#include "adios2/toolkit/format/BufferSTL.h" +#include "adios2/toolkit/format/buffer/BufferSTL.h" namespace adios2 { diff --git a/source/adios2/toolkit/format/buffer/Buffer.cpp b/source/adios2/toolkit/format/buffer/Buffer.cpp new file mode 100644 index 0000000000..dcfe2d8c7d --- /dev/null +++ b/source/adios2/toolkit/format/buffer/Buffer.cpp @@ -0,0 +1,30 @@ +/* + * Distributed under the OSI-approved Apache License, Version 2.0. See + * accompanying file Copyright.txt for details. + * + * Buffer.cpp + * + * Created on: Jul 9, 2019 + * Author: William F Godoy godoywf@ornl.gov + */ + +#include "Buffer.h" + +namespace adios2 +{ +namespace format +{ + +Buffer::Buffer(const std::string type, const bool debugMode) +: m_Type(type), m_DebugMode(debugMode) +{ +} + +void Buffer::Resize(const size_t size, const std::string hint) +{ + throw std::invalid_argument("ERROR: buffer memory of type " + m_Type + + " can't call Resize " + hint + "\n"); +} + +} // end namespace format +} // end namespace adios2 diff --git a/source/adios2/toolkit/format/buffer/Buffer.h b/source/adios2/toolkit/format/buffer/Buffer.h new file mode 100644 index 0000000000..cd1a3b7b86 --- /dev/null +++ b/source/adios2/toolkit/format/buffer/Buffer.h @@ -0,0 +1,46 @@ +/* + * Distributed under the OSI-approved Apache License, Version 2.0. See + * accompanying file Copyright.txt for details. + * + * Buffer.h : abstract class for all buffer memory types + * + * Created on: Jul 9, 2019 + * Author: William F Godoy godoywf@ornl.gov + */ + +#ifndef ADIOS2_TOOLKIT_FORMAT_BUFFER_BUFFER_H_ +#define ADIOS2_TOOLKIT_FORMAT_BUFFER_BUFFER_H_ + +#include "adios2/common/ADIOSConfig.h" +#include "adios2/common/ADIOSTypes.h" + +namespace adios2 +{ +namespace format +{ + +class Buffer +{ +public: + const std::string m_Type; + size_t m_Position = 0; + size_t m_AbsolutePosition = 0; + + /** if 0: buffer can be extended, if >0: buffer has a fixed size */ + size_t m_FixedSize = 0; + + Buffer(const std::string type, const bool debugMode); + virtual ~Buffer() = default; + + virtual void Resize(const size_t size, const std::string hint); + + virtual size_t GetAvailableSize() const = 0; + +private: + const bool m_DebugMode; +}; + +} // end namespace format +} // end namespace adios2 + +#endif /* ADIOS2_TOOLKIT_FORMAT_BUFFER_BUFFER_H_ */ diff --git a/source/adios2/toolkit/format/BufferSTL.cpp b/source/adios2/toolkit/format/buffer/BufferSTL.cpp similarity index 85% rename from source/adios2/toolkit/format/BufferSTL.cpp rename to source/adios2/toolkit/format/buffer/BufferSTL.cpp index 7c6d2caf6d..57939fee8b 100644 --- a/source/adios2/toolkit/format/BufferSTL.cpp +++ b/source/adios2/toolkit/format/buffer/BufferSTL.cpp @@ -12,6 +12,10 @@ namespace adios2 { +namespace format +{ + +BufferSTL::BufferSTL(const bool debugMode) : Buffer("BufferSTL", debugMode) {} void BufferSTL::Resize(const size_t size, const std::string hint) { @@ -25,6 +29,7 @@ void BufferSTL::Resize(const size_t size, const std::string hint) } catch (...) { + // catch a bad_alloc std::throw_with_nested(std::runtime_error( "ERROR: buffer overflow when resizing to " + std::to_string(size) + " bytes, " + hint + "\n")); @@ -36,4 +41,5 @@ size_t BufferSTL::GetAvailableSize() const return m_Buffer.size() - m_Position; } +} // end namespace format } // end namespace adios2 diff --git a/source/adios2/toolkit/format/buffer/BufferSTL.h b/source/adios2/toolkit/format/buffer/BufferSTL.h new file mode 100644 index 0000000000..393ba4cf61 --- /dev/null +++ b/source/adios2/toolkit/format/buffer/BufferSTL.h @@ -0,0 +1,37 @@ +/* + * Distributed under the OSI-approved Apache License, Version 2.0. See + * accompanying file Copyright.txt for details. + * + * BufferSTL.h + * + * Created on: Sep 26, 2017 + * Author: William F Godoy godoywf@ornl.gov + */ + +#ifndef ADIOS2_TOOLKIT_FORMAT_BUFFER_BUFFERSTL_H_ +#define ADIOS2_TOOLKIT_FORMAT_BUFFER_BUFFERSTL_H_ + +#include "adios2/toolkit/format/buffer/Buffer.h" + +namespace adios2 +{ +namespace format +{ + +class BufferSTL : public Buffer +{ +public: + std::vector m_Buffer; + + BufferSTL(const bool debugMode = true); + ~BufferSTL() = default; + + void Resize(const size_t size, const std::string hint) final; + + size_t GetAvailableSize() const final; +}; + +} // end namespace format +} // end namespace adios2 + +#endif /* ADIOS2_TOOLKIT_FORMAT_BUFFERSTL_H_ */ From 3541ef77fbd06263c2f628ef43a013dd60b7cf9e Mon Sep 17 00:00:00 2001 From: William F Godoy Date: Tue, 9 Jul 2019 15:31:48 -0400 Subject: [PATCH 2/2] Added BufferSystemV Refactored MPI Aggregator with base Buffer --- source/adios2/CMakeLists.txt | 5 +- source/adios2/engine/bp3/BP3Writer.cpp | 6 +- source/adios2/engine/bp4/BP4Writer.cpp | 7 +- .../toolkit/aggregator/mpi/MPIAggregator.cpp | 5 +- .../toolkit/aggregator/mpi/MPIAggregator.h | 12 +-- .../toolkit/aggregator/mpi/MPIChain.cpp | 75 +++++++++++-------- .../adios2/toolkit/aggregator/mpi/MPIChain.h | 26 +++---- source/adios2/toolkit/format/bp3/BP3Base.h | 2 +- source/adios2/toolkit/format/bp4/BP4Base.h | 2 +- .../toolkit/format/bpOperation/BPOperation.h | 2 +- .../adios2/toolkit/format/buffer/Buffer.cpp | 17 ++++- source/adios2/toolkit/format/buffer/Buffer.h | 16 ++-- .../format/buffer/{ => heap}/BufferSTL.cpp | 6 +- .../format/buffer/{ => heap}/BufferSTL.h | 11 ++- .../format/buffer/ipc/BufferSystemV.cpp | 66 ++++++++++++++++ .../toolkit/format/buffer/ipc/BufferSystemV.h | 47 ++++++++++++ 16 files changed, 225 insertions(+), 80 deletions(-) rename source/adios2/toolkit/format/buffer/{ => heap}/BufferSTL.cpp (84%) rename source/adios2/toolkit/format/buffer/{ => heap}/BufferSTL.h (68%) create mode 100644 source/adios2/toolkit/format/buffer/ipc/BufferSystemV.cpp create mode 100644 source/adios2/toolkit/format/buffer/ipc/BufferSystemV.h diff --git a/source/adios2/CMakeLists.txt b/source/adios2/CMakeLists.txt index 304b97f607..c4542e8352 100644 --- a/source/adios2/CMakeLists.txt +++ b/source/adios2/CMakeLists.txt @@ -60,7 +60,7 @@ add_library(adios2 engine/nullcore/NullCoreWriter.cpp engine/nullcore/NullCoreWriter.tcc #toolkit toolkit/format/buffer/Buffer.cpp - toolkit/format/buffer/BufferSTL.cpp + toolkit/format/buffer/heap/BufferSTL.cpp toolkit/format/bp3/BP3Base.cpp toolkit/format/bp3/BP3Base.tcc toolkit/format/bp3/BP3Serializer.cpp toolkit/format/bp3/BP3Serializer.tcc @@ -105,6 +105,8 @@ if(UNIX) endif() if(ADIOS2_HAVE_SysVShMem) + target_sources(adios2 PRIVATE toolkit/format/buffer/ipc/BufferSystemV.cpp) + target_sources(adios2 PRIVATE toolkit/transport/shm/ShmSystemV.cpp) endif() @@ -144,7 +146,6 @@ if(ADIOS2_HAVE_SSC) target_link_libraries(adios2 PRIVATE nlohmann_json) endif() - if(ADIOS2_HAVE_SST) add_subdirectory(toolkit/sst) target_sources(adios2 PRIVATE diff --git a/source/adios2/engine/bp3/BP3Writer.cpp b/source/adios2/engine/bp3/BP3Writer.cpp index 2037eae6b9..0935b91246 100644 --- a/source/adios2/engine/bp3/BP3Writer.cpp +++ b/source/adios2/engine/bp3/BP3Writer.cpp @@ -353,12 +353,12 @@ void BP3Writer::AggregateWriteData(const bool isFinal, const int transportIndex) if (m_BP3Serializer.m_Aggregator.m_IsConsumer) { - const format::BufferSTL &bufferSTL = + const format::Buffer &bufferSTL = m_BP3Serializer.m_Aggregator.GetConsumerBuffer( m_BP3Serializer.m_Data); - m_FileDataManager.WriteFiles(bufferSTL.m_Buffer.data(), - bufferSTL.m_Position, transportIndex); + m_FileDataManager.WriteFiles(bufferSTL.Data(), bufferSTL.m_Position, + transportIndex); m_FileDataManager.FlushFiles(transportIndex); } diff --git a/source/adios2/engine/bp4/BP4Writer.cpp b/source/adios2/engine/bp4/BP4Writer.cpp index f77fc3456d..d408436649 100644 --- a/source/adios2/engine/bp4/BP4Writer.cpp +++ b/source/adios2/engine/bp4/BP4Writer.cpp @@ -575,14 +575,13 @@ void BP4Writer::AggregateWriteData(const bool isFinal, const int transportIndex) if (m_BP4Serializer.m_Aggregator.m_IsConsumer) { - const format::BufferSTL &bufferSTL = + const format::Buffer &bufferSTL = m_BP4Serializer.m_Aggregator.GetConsumerBuffer( m_BP4Serializer.m_Data); if (bufferSTL.m_Position > 0) { - m_FileDataManager.WriteFiles(bufferSTL.m_Buffer.data(), - bufferSTL.m_Position, - transportIndex); + m_FileDataManager.WriteFiles( + bufferSTL.Data(), bufferSTL.m_Position, transportIndex); m_FileDataManager.FlushFiles(transportIndex); } diff --git a/source/adios2/toolkit/aggregator/mpi/MPIAggregator.cpp b/source/adios2/toolkit/aggregator/mpi/MPIAggregator.cpp index ee1dea549f..38e45effb4 100644 --- a/source/adios2/toolkit/aggregator/mpi/MPIAggregator.cpp +++ b/source/adios2/toolkit/aggregator/mpi/MPIAggregator.cpp @@ -35,10 +35,9 @@ void MPIAggregator::SwapBuffers(const int step) noexcept {} void MPIAggregator::ResetBuffers() noexcept {} -format::BufferSTL & -MPIAggregator::GetConsumerBuffer(format::BufferSTL &bufferSTL) +format::Buffer &MPIAggregator::GetConsumerBuffer(format::Buffer &buffer) { - return bufferSTL; + return buffer; } void MPIAggregator::Close() diff --git a/source/adios2/toolkit/aggregator/mpi/MPIAggregator.h b/source/adios2/toolkit/aggregator/mpi/MPIAggregator.h index 6e8f2244c7..63b261daf4 100644 --- a/source/adios2/toolkit/aggregator/mpi/MPIAggregator.h +++ b/source/adios2/toolkit/aggregator/mpi/MPIAggregator.h @@ -11,9 +11,11 @@ #ifndef ADIOS2_TOOLKIT_AGGREGATOR_MPI_MPIAGGREGATOR_H_ #define ADIOS2_TOOLKIT_AGGREGATOR_MPI_MPIAGGREGATOR_H_ +#include //std::unique_ptr + #include "adios2/common/ADIOSMPI.h" #include "adios2/common/ADIOSTypes.h" -#include "adios2/toolkit/format/buffer/BufferSTL.h" +#include "adios2/toolkit/format/buffer/Buffer.h" namespace adios2 { @@ -58,10 +60,10 @@ class MPIAggregator virtual void Init(const size_t subStreams, MPI_Comm parentComm); virtual std::vector> - IExchange(format::BufferSTL &bufferSTL, const int step) = 0; + IExchange(format::Buffer &buffer, const int step) = 0; virtual std::vector> - IExchangeAbsolutePosition(format::BufferSTL &bufferSTL, const int step) = 0; + IExchangeAbsolutePosition(format::Buffer &buffer, const int step) = 0; virtual void WaitAbsolutePosition(std::vector> &requests, @@ -74,7 +76,7 @@ class MPIAggregator virtual void ResetBuffers() noexcept; - virtual format::BufferSTL &GetConsumerBuffer(format::BufferSTL &bufferSTL); + virtual format::Buffer &GetConsumerBuffer(format::Buffer &buffer); /** closes current aggregator, frees m_Comm */ void Close(); @@ -88,7 +90,7 @@ class MPIAggregator void HandshakeRank(const int rank = 0); /** assigning extra buffers for aggregation */ - std::vector m_Buffers; + std::vector> m_Buffers; }; } // end namespace aggregator diff --git a/source/adios2/toolkit/aggregator/mpi/MPIChain.cpp b/source/adios2/toolkit/aggregator/mpi/MPIChain.cpp index 9e32169cd4..91a2f4bb51 100644 --- a/source/adios2/toolkit/aggregator/mpi/MPIChain.cpp +++ b/source/adios2/toolkit/aggregator/mpi/MPIChain.cpp @@ -12,6 +12,8 @@ #include "adios2/common/ADIOSMPI.h" #include "adios2/helper/adiosFunctions.h" //helper::CheckMPIReturn +#include "adios2/toolkit/format/buffer/heap/BufferSTL.h" + namespace adios2 { namespace aggregator @@ -28,19 +30,19 @@ void MPIChain::Init(const size_t subStreams, MPI_Comm parentComm) // add a receiving buffer except for the last rank (only sends) if (m_Rank < m_Size) { - m_Buffers.emplace_back(); // just one for now + m_Buffers.emplace_back(new format::BufferSTL()); // just one for now } } std::vector> -MPIChain::IExchange(format::BufferSTL &bufferSTL, const int step) +MPIChain::IExchange(format::Buffer &buffer, const int step) { if (m_Size == 1) { return std::vector>(); } - format::BufferSTL &sendBuffer = GetSender(bufferSTL); + format::Buffer &sendBuffer = GetSender(buffer); const int endRank = m_Size - 1 - step; const bool sender = (m_Rank >= 1 && m_Rank <= endRank) ? true : false; const bool receiver = (m_Rank < endRank) ? true : false; @@ -61,11 +63,10 @@ MPIChain::IExchange(format::BufferSTL &bufferSTL, const int step) if (sendBuffer.m_Position > 0) { - const std::vector requestsISend64 = - helper::Isend64(sendBuffer.m_Buffer.data(), - sendBuffer.m_Position, m_Rank - 1, 1, m_Comm, - ", aggregation Isend64 data at iteration " + - std::to_string(step)); + const std::vector requestsISend64 = helper::Isend64( + sendBuffer.Data(), sendBuffer.m_Position, m_Rank - 1, 1, m_Comm, + ", aggregation Isend64 data at iteration " + + std::to_string(step)); requests[0].insert(requests[0].end(), requestsISend64.begin(), requestsISend64.end()); @@ -88,8 +89,8 @@ MPIChain::IExchange(format::BufferSTL &bufferSTL, const int step) ", aggregation waiting for receiver size at iteration " + std::to_string(step) + "\n"); - format::BufferSTL &receiveBuffer = GetReceiver(bufferSTL); - ResizeUpdateBufferSTL( + format::Buffer &receiveBuffer = GetReceiver(buffer); + ResizeUpdateBuffer( bufferSize, receiveBuffer, "in aggregation, when resizing receiving buffer to size " + std::to_string(bufferSize)); @@ -98,8 +99,8 @@ MPIChain::IExchange(format::BufferSTL &bufferSTL, const int step) if (bufferSize > 0) { requests[1] = - helper::Irecv64(receiveBuffer.m_Buffer.data(), - receiveBuffer.m_Position, m_Rank + 1, 1, m_Comm, + helper::Irecv64(receiveBuffer.Data(), receiveBuffer.m_Position, + m_Rank + 1, 1, m_Comm, ", aggregation Irecv64 data at iteration " + std::to_string(step)); } @@ -109,8 +110,7 @@ MPIChain::IExchange(format::BufferSTL &bufferSTL, const int step) } std::vector> -MPIChain::IExchangeAbsolutePosition(format::BufferSTL &bufferSTL, - const int step) +MPIChain::IExchangeAbsolutePosition(format::Buffer &buffer, const int step) { if (m_Size == 1) { @@ -130,14 +130,13 @@ MPIChain::IExchangeAbsolutePosition(format::BufferSTL &bufferSTL, if (step == 0) { m_SizeSend = - (m_Rank == 0) ? bufferSTL.m_AbsolutePosition : bufferSTL.m_Position; + (m_Rank == 0) ? buffer.m_AbsolutePosition : buffer.m_Position; } if (m_Rank == step) { m_ExchangeAbsolutePosition = - (m_Rank == 0) ? m_SizeSend - : m_SizeSend + bufferSTL.m_AbsolutePosition; + (m_Rank == 0) ? m_SizeSend : m_SizeSend + buffer.m_AbsolutePosition; helper::CheckMPIReturn( MPI_Isend(&m_ExchangeAbsolutePosition, 1, ADIOS2_MPI_SIZE_T, @@ -148,8 +147,8 @@ MPIChain::IExchangeAbsolutePosition(format::BufferSTL &bufferSTL, else if (m_Rank == destination) { helper::CheckMPIReturn( - MPI_Irecv(&bufferSTL.m_AbsolutePosition, 1, ADIOS2_MPI_SIZE_T, step, - 0, m_Comm, &requests[1][0]), + MPI_Irecv(&buffer.m_AbsolutePosition, 1, ADIOS2_MPI_SIZE_T, step, 0, + m_Comm, &requests[1][0]), ", aggregation Irecv absolute position at iteration " + std::to_string(step) + "\n"); } @@ -236,9 +235,9 @@ void MPIChain::SwapBuffers(const int /*step*/) noexcept void MPIChain::ResetBuffers() noexcept { m_CurrentBufferOrder = 0; } -format::BufferSTL &MPIChain::GetConsumerBuffer(format::BufferSTL &bufferSTL) +format::Buffer &MPIChain::GetConsumerBuffer(format::Buffer &buffer) { - return GetSender(bufferSTL); + return GetSender(buffer); } // PRIVATE @@ -277,36 +276,48 @@ void MPIChain::HandshakeLinks() } } -format::BufferSTL &MPIChain::GetSender(format::BufferSTL &bufferSTL) +format::Buffer &MPIChain::GetSender(format::Buffer &buffer) { if (m_CurrentBufferOrder == 0) { - return bufferSTL; + return buffer; } else { - return m_Buffers.front(); + return *m_Buffers.front(); } } -format::BufferSTL &MPIChain::GetReceiver(format::BufferSTL &bufferSTL) +format::Buffer &MPIChain::GetReceiver(format::Buffer &buffer) { if (m_CurrentBufferOrder == 0) { - return m_Buffers.front(); + return *m_Buffers.front(); } else { - return bufferSTL; + return buffer; } } -void MPIChain::ResizeUpdateBufferSTL(const size_t newSize, - format::BufferSTL &bufferSTL, - const std::string hint) +void MPIChain::ResizeUpdateBuffer(const size_t newSize, format::Buffer &buffer, + const std::string hint) { - bufferSTL.Resize(newSize, hint); - bufferSTL.m_Position = bufferSTL.m_Buffer.size(); + if (buffer.m_FixedSize > 0) + { + if (newSize > buffer.m_FixedSize) + { + throw std::invalid_argument( + "ERROR: requesting new size: " + std::to_string(newSize) + + " bytes, for fixed size buffer " + + std::to_string(buffer.m_FixedSize) + " of type " + + buffer.m_Type + ", allocate more memory\n"); + } + return; // do nothing if fixed size is enough + } + + buffer.Resize(newSize, hint); + buffer.m_Position = newSize; } } // end namespace aggregator diff --git a/source/adios2/toolkit/aggregator/mpi/MPIChain.h b/source/adios2/toolkit/aggregator/mpi/MPIChain.h index 25ab7502cf..83a1dd93c5 100644 --- a/source/adios2/toolkit/aggregator/mpi/MPIChain.h +++ b/source/adios2/toolkit/aggregator/mpi/MPIChain.h @@ -28,12 +28,11 @@ class MPIChain : public MPIAggregator void Init(const size_t subStreams, MPI_Comm parentComm) final; - std::vector> - IExchange(format::BufferSTL &bufferSTL, const int step) final; + std::vector> IExchange(format::Buffer &buffer, + const int step) final; std::vector> - IExchangeAbsolutePosition(format::BufferSTL &bufferSTL, - const int step) final; + IExchangeAbsolutePosition(format::Buffer &buffer, const int step) final; void Wait(std::vector> &request, const int step) final; @@ -45,7 +44,7 @@ class MPIChain : public MPIAggregator void ResetBuffers() noexcept final; - format::BufferSTL &GetConsumerBuffer(format::BufferSTL &bufferSTL) final; + format::Buffer &GetConsumerBuffer(format::Buffer &buffer) final; private: bool m_IsInExchangeAbsolutePosition = false; @@ -63,29 +62,28 @@ class MPIChain : public MPIAggregator * Returns a reference to the sender buffer depending on * m_CurrentBufferOrder * flag - * @param bufferSTL original buffer from serializer + * @param buffer original buffer from serializer * @return reference to sender buffer */ - format::BufferSTL &GetSender(format::BufferSTL &bufferSTL); + format::Buffer &GetSender(format::Buffer &buffer); /** * Returns a reference to the receiver buffer depending on * m_CurrentBufferOrder * flag - * @param bufferSTL original buffer from serializer + * @param buffer original buffer from serializer * @return reference to receiver buffer */ - format::BufferSTL &GetReceiver(format::BufferSTL &bufferSTL); + format::Buffer &GetReceiver(format::Buffer &buffer); /** - * Resizes and updates m_Position in a bufferSTL, used for receiving buffers + * Resizes and updates m_Position in a buffer, used for receiving buffers * @param newSize new size for receiving buffer - * @param bufferSTL to be resized + * @param buffer to be resized * @param hint used in exception error message */ - void ResizeUpdateBufferSTL(const size_t newSize, - format::BufferSTL &bufferSTL, - const std::string hint); + void ResizeUpdateBuffer(const size_t newSize, format::Buffer &buffer, + const std::string hint); }; } // end namespace aggregator diff --git a/source/adios2/toolkit/format/bp3/BP3Base.h b/source/adios2/toolkit/format/bp3/BP3Base.h index eb9fcfc4c3..b4cb9bb0c1 100644 --- a/source/adios2/toolkit/format/bp3/BP3Base.h +++ b/source/adios2/toolkit/format/bp3/BP3Base.h @@ -29,7 +29,7 @@ #include "adios2/core/VariableBase.h" #include "adios2/toolkit/aggregator/mpi/MPIChain.h" #include "adios2/toolkit/format/bpOperation/BPOperation.h" -#include "adios2/toolkit/format/buffer/BufferSTL.h" +#include "adios2/toolkit/format/buffer/heap/BufferSTL.h" #include "adios2/toolkit/profiling/iochrono/IOChrono.h" namespace adios2 diff --git a/source/adios2/toolkit/format/bp4/BP4Base.h b/source/adios2/toolkit/format/bp4/BP4Base.h index 69fea9bda0..a56e6c95b7 100644 --- a/source/adios2/toolkit/format/bp4/BP4Base.h +++ b/source/adios2/toolkit/format/bp4/BP4Base.h @@ -29,7 +29,7 @@ #include "adios2/core/VariableBase.h" #include "adios2/toolkit/aggregator/mpi/MPIChain.h" #include "adios2/toolkit/format/bpOperation/BPOperation.h" -#include "adios2/toolkit/format/buffer/BufferSTL.h" +#include "adios2/toolkit/format/buffer/heap/BufferSTL.h" #include "adios2/toolkit/profiling/iochrono/IOChrono.h" namespace adios2 diff --git a/source/adios2/toolkit/format/bpOperation/BPOperation.h b/source/adios2/toolkit/format/bpOperation/BPOperation.h index 088d9fcb2a..fd41988412 100644 --- a/source/adios2/toolkit/format/bpOperation/BPOperation.h +++ b/source/adios2/toolkit/format/bpOperation/BPOperation.h @@ -17,7 +17,7 @@ #include "adios2/common/ADIOSMacros.h" #include "adios2/core/Variable.h" #include "adios2/helper/adiosFunctions.h" -#include "adios2/toolkit/format/buffer/BufferSTL.h" +#include "adios2/toolkit/format/buffer/heap/BufferSTL.h" namespace adios2 { diff --git a/source/adios2/toolkit/format/buffer/Buffer.cpp b/source/adios2/toolkit/format/buffer/Buffer.cpp index dcfe2d8c7d..548eba4ebd 100644 --- a/source/adios2/toolkit/format/buffer/Buffer.cpp +++ b/source/adios2/toolkit/format/buffer/Buffer.cpp @@ -15,8 +15,8 @@ namespace adios2 namespace format { -Buffer::Buffer(const std::string type, const bool debugMode) -: m_Type(type), m_DebugMode(debugMode) +Buffer::Buffer(const std::string type, const size_t fixedSize) +: m_Type(type), m_FixedSize(fixedSize) { } @@ -26,5 +26,18 @@ void Buffer::Resize(const size_t size, const std::string hint) " can't call Resize " + hint + "\n"); } +char *Buffer::Data() noexcept { return nullptr; } + +const char *Buffer::Data() const noexcept { return nullptr; } + +size_t Buffer::GetAvailableSize() const +{ + if (m_FixedSize > 0 && m_FixedSize >= m_Position) + { + return m_FixedSize - m_Position; + } + return 0; +} + } // end namespace format } // end namespace adios2 diff --git a/source/adios2/toolkit/format/buffer/Buffer.h b/source/adios2/toolkit/format/buffer/Buffer.h index cd1a3b7b86..837896614d 100644 --- a/source/adios2/toolkit/format/buffer/Buffer.h +++ b/source/adios2/toolkit/format/buffer/Buffer.h @@ -23,21 +23,23 @@ class Buffer { public: const std::string m_Type; + + /** if 0: buffer can be extended, if >0: buffer has a fixed size */ + const size_t m_FixedSize = 0; + size_t m_Position = 0; size_t m_AbsolutePosition = 0; - /** if 0: buffer can be extended, if >0: buffer has a fixed size */ - size_t m_FixedSize = 0; + Buffer(const std::string type, const size_t fixedSize = 0); - Buffer(const std::string type, const bool debugMode); virtual ~Buffer() = default; - virtual void Resize(const size_t size, const std::string hint); + virtual char *Data() noexcept; + virtual const char *Data() const noexcept; - virtual size_t GetAvailableSize() const = 0; + virtual void Resize(const size_t size, const std::string hint); -private: - const bool m_DebugMode; + virtual size_t GetAvailableSize() const; }; } // end namespace format diff --git a/source/adios2/toolkit/format/buffer/BufferSTL.cpp b/source/adios2/toolkit/format/buffer/heap/BufferSTL.cpp similarity index 84% rename from source/adios2/toolkit/format/buffer/BufferSTL.cpp rename to source/adios2/toolkit/format/buffer/heap/BufferSTL.cpp index 57939fee8b..2967ca56f8 100644 --- a/source/adios2/toolkit/format/buffer/BufferSTL.cpp +++ b/source/adios2/toolkit/format/buffer/heap/BufferSTL.cpp @@ -15,7 +15,11 @@ namespace adios2 namespace format { -BufferSTL::BufferSTL(const bool debugMode) : Buffer("BufferSTL", debugMode) {} +BufferSTL::BufferSTL() : Buffer("BufferSTL") {} + +char *BufferSTL::Data() noexcept { return m_Buffer.data(); } + +const char *BufferSTL::Data() const noexcept { return m_Buffer.data(); } void BufferSTL::Resize(const size_t size, const std::string hint) { diff --git a/source/adios2/toolkit/format/buffer/BufferSTL.h b/source/adios2/toolkit/format/buffer/heap/BufferSTL.h similarity index 68% rename from source/adios2/toolkit/format/buffer/BufferSTL.h rename to source/adios2/toolkit/format/buffer/heap/BufferSTL.h index 393ba4cf61..ef5f373dc2 100644 --- a/source/adios2/toolkit/format/buffer/BufferSTL.h +++ b/source/adios2/toolkit/format/buffer/heap/BufferSTL.h @@ -8,8 +8,8 @@ * Author: William F Godoy godoywf@ornl.gov */ -#ifndef ADIOS2_TOOLKIT_FORMAT_BUFFER_BUFFERSTL_H_ -#define ADIOS2_TOOLKIT_FORMAT_BUFFER_BUFFERSTL_H_ +#ifndef ADIOS2_TOOLKIT_FORMAT_BUFFER_HEAP_BUFFERSTL_H_ +#define ADIOS2_TOOLKIT_FORMAT_BUFFER_HEAP_BUFFERSTL_H_ #include "adios2/toolkit/format/buffer/Buffer.h" @@ -23,9 +23,12 @@ class BufferSTL : public Buffer public: std::vector m_Buffer; - BufferSTL(const bool debugMode = true); + BufferSTL(); ~BufferSTL() = default; + char *Data() noexcept final; + const char *Data() const noexcept final; + void Resize(const size_t size, const std::string hint) final; size_t GetAvailableSize() const final; @@ -34,4 +37,4 @@ class BufferSTL : public Buffer } // end namespace format } // end namespace adios2 -#endif /* ADIOS2_TOOLKIT_FORMAT_BUFFERSTL_H_ */ +#endif /* ADIOS2_TOOLKIT_FORMAT_BUFFER_HEAP_BUFFERSTL_H_ */ diff --git a/source/adios2/toolkit/format/buffer/ipc/BufferSystemV.cpp b/source/adios2/toolkit/format/buffer/ipc/BufferSystemV.cpp new file mode 100644 index 0000000000..1bc442ce02 --- /dev/null +++ b/source/adios2/toolkit/format/buffer/ipc/BufferSystemV.cpp @@ -0,0 +1,66 @@ +/* + * Distributed under the OSI-approved Apache License, Version 2.0. See + * accompanying file Copyright.txt for details. + * + * BufferSystemV.cpp + * + * Created on: Jul 9, 2019 + * Author: William F Godoy godoywf@ornl.gov + */ + +#include "BufferSystemV.h" + +#include +#include //std::memcpy +#include //std::ios_base::failure + +#include //ftok +#include //shmget, shmmat +#include //key_t + +namespace adios2 +{ +namespace format +{ + +BufferSystemV::BufferSystemV(const size_t fixedSize, const std::string &name, + const unsigned int projectID, const bool remove) +: Buffer("BufferSystemV", fixedSize), m_Remove(remove) +{ + assert(projectID > 0); // for the developer + key_t key = ftok(name.c_str(), static_cast(projectID)); + m_ShmID = shmget(key, static_cast(fixedSize), + IPC_CREAT | 0666); + if (m_ShmID == -1) + { + throw std::ios_base::failure( + "ERROR: could not create shared memory buffer of size " + + std::to_string(fixedSize) + " with shmget \n"); + } + + void *data = shmat(m_ShmID, nullptr, 0); + int *status = reinterpret_cast(data); + if (*status == -1) + { + throw std::runtime_error("ERROR: could not attach shared memory buffer " + "to address with shmat\n"); + } + m_Data = static_cast(data); +} + +BufferSystemV::~BufferSystemV() +{ + shmdt(m_Data); + + if (m_Remove) + { + shmctl(m_ShmID, IPC_RMID, NULL); + } +} + +char *BufferSystemV::Data() noexcept { return m_Data; } + +const char *BufferSystemV::Data() const noexcept { return m_Data; } + +} // end namespace format +} // end namespace adios2 diff --git a/source/adios2/toolkit/format/buffer/ipc/BufferSystemV.h b/source/adios2/toolkit/format/buffer/ipc/BufferSystemV.h new file mode 100644 index 0000000000..21da900007 --- /dev/null +++ b/source/adios2/toolkit/format/buffer/ipc/BufferSystemV.h @@ -0,0 +1,47 @@ +/* + * Distributed under the OSI-approved Apache License, Version 2.0. See + * accompanying file Copyright.txt for details. + * + * BufferSystemV.h + * + * Created on: Jul 9, 2019 + * Author: William F Godoy godoywf@ornl.gov + */ + +#ifndef ADIOS2_TOOLKIT_FORMAT_BUFFER_IPC_BUFFERSYSTEMV_H_ +#define ADIOS2_TOOLKIT_FORMAT_BUFFER_IPC_BUFFERSYSTEMV_H_ + +#include "adios2/toolkit/format/buffer/Buffer.h" + +namespace adios2 +{ +namespace format +{ + +class BufferSystemV : public Buffer +{ +public: + BufferSystemV(const size_t fixedSize, const std::string &name, + const unsigned int projectID, const bool remove); + + ~BufferSystemV(); + + char *Data() noexcept final; + + const char *Data() const noexcept final; + +private: + /** shared memory segment ID from shmget */ + int m_ShmID = -1; + + /** pointer to shared memory segment */ + char *m_Data = nullptr; + + /** false: make it persistent, true: remove with destructor */ + const bool m_Remove; +}; + +} // end namespace format +} // end namespace adios2 + +#endif /* ADIOS2_TOOLKIT_FORMAT_BUFFER_IPC_BUFFERSYSTEMV_H_ */