-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Implement a GenericCloner test module
#47504
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
acf9d6a
Specialize Handle and OrphanHandle for WrapperBase
fwyzard 4ba7794
Implement integration tests for WrapperBase specialisations
fwyzard 31d3ae7
Implement edmtest::GenericCloner
fwyzard 9e2a878
Introduce the TrivialCopyTraits
fwyzard a6452be
Specialise the TrivialCopyTraits for PortableHostObject and PortableH…
fwyzard 4988fc2
Optimize GenericCloner using the TrivialCopyTraits
fwyzard File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| #ifndef DataFormats_Common_interface_AnyBuffer_h | ||
| #define DataFormats_Common_interface_AnyBuffer_h | ||
|
|
||
| #include <type_traits> | ||
| #include <typeinfo> | ||
|
|
||
| #include <boost/container/small_vector.hpp> | ||
|
|
||
| #include "FWCore/Utilities/interface/EDMException.h" | ||
| #include "FWCore/Utilities/interface/TypeDemangler.h" | ||
|
|
||
| namespace edm { | ||
|
|
||
| class AnyBuffer { | ||
| public: | ||
| AnyBuffer() = default; | ||
|
|
||
| template <typename T> | ||
| AnyBuffer(T const& t) | ||
| requires(std::is_trivially_copyable_v<T>) | ||
| : storage_(reinterpret_cast<std::byte const*>(&t), reinterpret_cast<std::byte const*>(&t) + sizeof(T)), | ||
| typeid_(&typeid(std::remove_cv_t<T>)) {} | ||
|
|
||
| template <typename T> | ||
| T& cast_to() | ||
| requires(std::is_trivially_copyable_v<T>) | ||
| { | ||
| if (empty()) { | ||
| throw edm::Exception(edm::errors::LogicError) | ||
| << "Attempt to read an object of type " << edm::typeDemangle(typeid(T).name()) | ||
| << " from an empty AnyBuffer"; | ||
| } | ||
| if (typeid(std::remove_cv_t<T>) != *typeid_) { | ||
| throw edm::Exception(edm::errors::LogicError) | ||
| << "Attempt to read an object of type " << edm::typeDemangle(typeid(T).name()) | ||
| << " from an AnyBuffer holding an object of type " << edm::typeDemangle(typeid_->name()); | ||
| } | ||
| return *reinterpret_cast<T*>(storage_.data()); | ||
| } | ||
|
|
||
| template <typename T> | ||
| T const& cast_to() const | ||
| requires(std::is_trivially_copyable_v<T>) | ||
| { | ||
| if (empty()) { | ||
| throw edm::Exception(edm::errors::LogicError) | ||
| << "Attempt to read an object of type " << edm::typeDemangle(typeid(T).name()) | ||
| << " from an empty AnyBuffer"; | ||
| } | ||
| if (typeid(std::remove_cv_t<T>) != *typeid_) { | ||
| throw edm::Exception(edm::errors::LogicError) | ||
| << "Attempt to read an object of type " << edm::typeDemangle(typeid(T).name()) | ||
| << " from an AnyBuffer holding an object of type " << edm::typeDemangle(typeid_->name()); | ||
| } | ||
| return *reinterpret_cast<T const*>(storage_.data()); | ||
| } | ||
|
|
||
| bool empty() const { return typeid_ == nullptr; } | ||
|
|
||
| std::byte* data() { return storage_.data(); } | ||
|
|
||
| std::byte const* data() const { return storage_.data(); } | ||
|
|
||
| size_t size_bytes() const { return storage_.size(); } | ||
|
|
||
| private: | ||
| boost::container::small_vector<std::byte, 32> storage_; // arbitrary small vector size to fit AnyBuffer in 64 bytes | ||
| std::type_info const* typeid_ = nullptr; | ||
| }; | ||
|
|
||
| } // namespace edm | ||
|
|
||
| #endif // DataFormats_Common_interface_AnyBuffer_h |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| #ifndef DataFormats_Common_interface_TrivialCopyTraits_h | ||
| #define DataFormats_Common_interface_TrivialCopyTraits_h | ||
|
|
||
| #include <cassert> | ||
| #include <span> | ||
| #include <type_traits> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| namespace edm { | ||
|
|
||
| // This struct should be specialised for each type that can be safely memcpy'ed. | ||
| // | ||
| // The specialisation shall have two static methods | ||
| // | ||
| // static std::vector<std::span<std::byte>> regions(T& object); | ||
| // static std::vector<std::span<const std::byte>> regions(T const& object); | ||
| // | ||
| // that return a vector of address, size pairs. | ||
| // A type that supports this interface can be copied by doing a memcpy of all | ||
| // the address, size pairs from a source object to a destination object. | ||
| // | ||
| // | ||
| // A specialisation may implement the type alias Properties to describe the | ||
| // properties of an object that can be queried from an existing object via the | ||
| // properties() method, and used to initialise a newly allocated copy of the | ||
| // object via the initialize() method. | ||
| // | ||
| // If Properties is void, the properties() method should not be implemented, | ||
| // and the initialize() method takes a single argument: | ||
| // | ||
| // using Properties = void; | ||
| // static void initialise(T& object); | ||
| // | ||
| // If Properties is a concrete type, the properties() method should return an | ||
| // instance of Properties, and the initialise() method should take as a second | ||
| // parameter a const reference to a Properties object: | ||
| // | ||
| // using Properties = ...; | ||
| // static Properties properties(T const& object); | ||
| // static void initialise(T& object, Properties const& args); | ||
| // | ||
| // | ||
| // A specialisation can optionally provide a static method | ||
| // | ||
| // static void finalize(T& object); | ||
| // | ||
| // If present, it should be called to restore the object invariants after a | ||
| // memcpy operation. | ||
|
|
||
| template <typename T> | ||
| struct TrivialCopyTraits; | ||
|
|
||
| // Specialisation for arithmetic types | ||
| template <typename T> | ||
| requires std::is_arithmetic_v<T> | ||
| struct TrivialCopyTraits<T> { | ||
| using value_type = T; | ||
|
|
||
| static std::vector<std::span<std::byte>> regions(value_type& object) { | ||
| return {{reinterpret_cast<std::byte*>(&object), sizeof(value_type)}}; | ||
| } | ||
|
|
||
| static std::vector<std::span<const std::byte>> regions(value_type const& object) { | ||
| return {{reinterpret_cast<std::byte const*>(&object), sizeof(value_type)}}; | ||
| } | ||
| }; | ||
|
|
||
| // Specialisation for vectors of arithmetic types | ||
| template <typename T> | ||
| requires(std::is_arithmetic_v<T> and not std::is_same_v<T, bool>) | ||
| struct TrivialCopyTraits<std::vector<T>> { | ||
| using value_type = std::vector<T>; | ||
|
|
||
| using Properties = std::vector<T>::size_type; | ||
|
|
||
| static Properties properties(value_type const& object) { return object.size(); } | ||
|
|
||
| static void initialize(value_type& object, Properties const& size) { object.resize(size); } | ||
|
|
||
| static std::vector<std::span<std::byte>> regions(value_type& object) { | ||
| return {{reinterpret_cast<std::byte*>(object.data()), object.size() * sizeof(T)}}; | ||
| } | ||
|
|
||
| static std::vector<std::span<const std::byte>> regions(value_type const& object) { | ||
| return {{reinterpret_cast<std::byte const*>(object.data()), object.size() * sizeof(T)}}; | ||
| } | ||
| }; | ||
|
|
||
| } // namespace edm | ||
|
|
||
| #endif // DataFormats_Common_interface_TrivialCopyTraits_h |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| <use name="alpaka"/> | ||
| <use name="rootcling"/> | ||
| <use name="DataFormats/Common"/> | ||
| <use name="FWCore/Utilities/" source_only="1"/> | ||
| <use name="HeterogeneousCore/AlpakaInterface"/> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is used by
GenericCloner::produce()to mark a newly constructedWrapper<T>as holding a valid object, before filling that object withmemcpy.Do you see any reasons not to have this method ? Currently I cannot think of any way to avoid it, though.