Skip to content

Commit

Permalink
#344 Rework serializer reference, plus some helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew-Whitlock committed Sep 24, 2024
1 parent 218cf0e commit 769b6ce
Show file tree
Hide file tree
Showing 21 changed files with 154 additions and 149 deletions.
11 changes: 6 additions & 5 deletions examples/checkpoint_example_to_file_nonintrusive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,18 +140,19 @@ int main(int, char**) {
// Call the serialization routine for the variable `my_test_inst`
// The output is a unique pointer: `std::unique_ptr<SerializedInfo>`
// (defined in `src/checkpoint_api.h`)
magistrate::serializeToFile(my_test_inst, "hello.txt");
std::string my_filename = "CheckpointExampleToFileNoninstrusive.txt";
magistrate::serializeToFile(my_test_inst, my_filename);

//
// De-serializes from the file an object of type 'MyTestType'
// out will be an object of type 'std::unique_ptr<MyTestType>'
//
auto out = magistrate::deserializeFromFile<MyTestType>("hello.txt");
auto out = magistrate::deserializeFromFile<MyTestType>(my_filename);

if (my_test_inst == *out) {
std::cout << " Serialization / Deserialization from file worked. \n";
} else {
std::cout << " Serialization / Deserialization from file failed. \n";
std::cout << " Serialization / Deserialization from file failed. \n" << std::flush;
assert(false);
}

Expand All @@ -164,7 +165,7 @@ int main(int, char**) {
// Here 'out_2' will contain an empty vector and an integer 'len_' set to 0.
//

magistrate::deserializeInPlaceFromFile<MyTestType>("hello.txt", &out_2);
magistrate::deserializeInPlaceFromFile<MyTestType>(my_filename, &out_2);

//
// Now 'out_2' will contain:
Expand All @@ -175,7 +176,7 @@ int main(int, char**) {
if (my_test_inst == out_2) {
std::cout << " Deserialization in-place from file worked. \n";
} else {
std::cout << " Deserialization in-place from file failed. \n";
std::cout << " Deserialization in-place from file failed. \n" << std::flush;
assert(false);
}

Expand Down
16 changes: 8 additions & 8 deletions examples/checkpoint_example_user_traits.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ int main(int, char**){

//Each invocation will be handled based on the traits attached
auto s_info_a = checkpoint::serialize(obj);
auto s_info_b = checkpoint::serialize<checkpoint_trait>(obj);
auto s_info_c = checkpoint::serialize<checkpoint_trait, checkpoint_trait>(obj);
auto s_info_d = checkpoint::serialize<test::random_trait, checkpoint_trait>(obj);
auto s_info_e = checkpoint::serialize<checkpoint_trait, test::random_trait>(obj);
auto s_info_f = checkpoint::serialize<test::random_trait, test::random_trait>(obj);
auto s_info_g = checkpoint::serialize<shallow_trait>(obj);
auto s_info_h = checkpoint::serialize<misc::namespace_trait>(obj);
auto s_info_i = checkpoint::serialize<misc::hook_all_trait>(obj);
auto s_info_b = checkpoint::serialize<test::TestObj, checkpoint_trait>(obj);
auto s_info_c = checkpoint::serialize<test::TestObj, checkpoint_trait, checkpoint_trait>(obj);
auto s_info_d = checkpoint::serialize<test::TestObj, test::random_trait, checkpoint_trait>(obj);
auto s_info_e = checkpoint::serialize<test::TestObj, checkpoint_trait, test::random_trait>(obj);
auto s_info_f = checkpoint::serialize<test::TestObj, test::random_trait, test::random_trait>(obj);
auto s_info_g = checkpoint::serialize<test::TestObj, shallow_trait>(obj);
auto s_info_h = checkpoint::serialize<test::TestObj, misc::namespace_trait>(obj);
auto s_info_i = checkpoint::serialize<test::TestObj, misc::hook_all_trait>(obj);
}
18 changes: 9 additions & 9 deletions src/checkpoint/checkpoint_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ using SerializedReturnType = std::unique_ptr<SerializedInfo>;
* \return a \c std::unique_ptr to a \c SerializedInfo containing the buffer
* with serialized data and the size of the buffer
*/
template <typename... UserTraits, typename T>
template <typename T, typename... UserTraits>
SerializedReturnType serialize(T& target, BufferCallbackType fn = nullptr);

/**
Expand Down Expand Up @@ -132,7 +132,7 @@ std::unique_ptr<T> deserialize(char* buf);
* \param[in] t a valid pointer to a \c T that has been user-allocated and
* constructed
*/
template <typename... UserTraits, typename T>
template <typename T, typename... UserTraits>
void deserializeInPlace(char* buf, T* t);

/**
Expand All @@ -153,7 +153,7 @@ std::unique_ptr<T> deserialize(SerializedReturnType&& in);
*
* \return number of bytes for the \c target
*/
template <typename... UserTraits, typename T>
template <typename T, typename... UserTraits>
std::size_t getSize(T& target);

/**
Expand All @@ -170,7 +170,7 @@ std::size_t getSize(T& target);
*
* \return memory footprint of the \c target
*/
template <typename... UserTraits, typename T>
template <typename T, typename... UserTraits>
std::size_t getMemoryFootprint(T& target, std::size_t size_offset = 0);

/**
Expand All @@ -184,7 +184,7 @@ std::size_t getMemoryFootprint(T& target, std::size_t size_offset = 0);
* \param[in] target the \c T to serialize
* \param[in] file name of the file to create
*/
template <typename... UserTraits, typename T>
template <typename T, typename... UserTraits>
void serializeToFile(T& target, std::string const& file);

/**
Expand Down Expand Up @@ -214,7 +214,7 @@ std::unique_ptr<T> deserializeFromFile(std::string const& file);
* \param[in] file the filename to read with bytes for \c T
* \param[in] t a valid, constructed \c T to deserialize into
*/
template <typename... UserTraits, typename T>
template <typename T, typename... UserTraits>
void deserializeInPlaceFromFile(std::string const& file, T* buf);

/**
Expand All @@ -227,7 +227,7 @@ void deserializeInPlaceFromFile(std::string const& file, T* buf);
* \param[in] target the \c T to serialize
* \param[in] stream to serialize into, with tellp and write functions.
*/
template <typename... Traits, typename T, typename StreamT>
template <typename T, typename... UserTraits, typename StreamT>
void serializeToStream(T& target, StreamT& stream);

/**
Expand All @@ -243,7 +243,7 @@ void serializeToStream(T& target, StreamT& stream);
*
* \return unique pointer to the new object \c T
*/
template <typename T, typename... Traits, typename StreamT>
template <typename T, typename... UserTraits, typename StreamT>
std::unique_ptr<T> deserializeFromStream(StreamT& stream);

/**
Expand All @@ -257,7 +257,7 @@ std::unique_ptr<T> deserializeFromStream(StreamT& stream);
* \param[in] stream the stream to read with bytes for \c T, with tellg and read functions
* \param[in] t a valid, constructed \c T to deserialize into
*/
template <typename... Traits, typename T, typename StreamT>
template <typename T, typename... UserTraits, typename StreamT>
void deserializeInPlaceFromStream(StreamT& stream, T* buf);


Expand Down
42 changes: 21 additions & 21 deletions src/checkpoint/checkpoint_api.impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@

namespace checkpoint {

template <typename... Traits, typename T>
template <typename T, typename... Traits>
SerializedReturnType serialize(T& target, BufferCallbackType fn) {
auto ret = dispatch::serializeType<T, Traits...>(target, fn);
auto ret = dispatch::serializeType<T, UserTraitHolder<Traits...>>(target, fn);
auto& buf = std::get<0>(ret);
std::unique_ptr<SerializedInfo> base_ptr(
static_cast<SerializedInfo*>(buf.release())
Expand All @@ -65,43 +65,43 @@ SerializedReturnType serialize(T& target, BufferCallbackType fn) {

template <typename T, typename... Traits>
T* deserialize(char* buf, char* object_buf) {
return dispatch::deserializeType<T, Traits...>(buf, object_buf);
return dispatch::deserializeType<T, UserTraitHolder<Traits...>>(buf, object_buf);
}

template <typename T, typename... Traits>
std::unique_ptr<T> deserialize(char* buf) {
auto t = dispatch::deserializeType<T, Traits...>(buf);
auto t = dispatch::deserializeType<T, UserTraitHolder<Traits...>>(buf);
return std::unique_ptr<T>(t);
}

template <typename T, typename... Traits>
std::unique_ptr<T> deserialize(SerializedReturnType&& in) {
auto t = dispatch::deserializeType<T, Traits...>(in->getBuffer());
auto t = dispatch::deserializeType<T, UserTraitHolder<Traits...>>(in->getBuffer());
return std::unique_ptr<T>(t);
}

template <typename... Traits, typename T>
template <typename T, typename... Traits>
void deserializeInPlace(char* buf, T* t) {
return dispatch::deserializeType<T, Traits...>(dispatch::InPlaceTag{}, buf, t);
return dispatch::deserializeType<T, UserTraitHolder<Traits...>>(dispatch::InPlaceTag{}, buf, t);
}

template <typename... Traits, typename T>
template <typename T, typename... Traits>
std::size_t getSize(T& target) {
return dispatch::Standard::size<T, SerializerRef<Sizer, Traits...>>(target);
return dispatch::Standard::size<T, Sizer, UserTraitHolder<Traits...>>(target);
}

template <typename... Traits, typename T>
template <typename T, typename... Traits>
std::size_t getMemoryFootprint(T& target, std::size_t size_offset) {
return size_offset + std::max(
dispatch::Standard::footprint<T, SerializerRef<Footprinter, Traits...>>(target),
dispatch::Standard::footprint<T, Footprinter, UserTraitHolder<Traits...>>(target),
sizeof(target)
);
}

template <typename... Traits, typename T>
template <typename T, typename... Traits>
void serializeToFile(T& target, std::string const& file) {
auto len = getSize<Traits...>(target);
dispatch::Standard::pack<T, SerializerRef<PackerBuffer<buffer::IOBuffer>, Traits...>>(
dispatch::Standard::pack<T, PackerBuffer<buffer::IOBuffer>, UserTraitHolder<Traits...>>(
target, len, buffer::IOBuffer::WriteToFileTag{}, len, file
);
}
Expand All @@ -110,23 +110,23 @@ template <typename T, typename... Traits>
std::unique_ptr<T> deserializeFromFile(std::string const& file) {
auto mem = dispatch::Standard::allocate<T>();
T* t_buf = dispatch::Standard::construct<T>(mem);
auto t = dispatch::Standard::unpack<T, SerializerRef<UnpackerBuffer<buffer::IOBuffer>, Traits...>>(
auto t = dispatch::Standard::unpack<T, UnpackerBuffer<buffer::IOBuffer>, UserTraitHolder<Traits...>>(
t_buf, buffer::IOBuffer::ReadFromFileTag{}, file
);
return std::unique_ptr<T>(t);
}

template <typename... Traits, typename T>
template <typename T, typename... Traits>
void deserializeInPlaceFromFile(std::string const& file, T* t) {
dispatch::Standard::unpack<T, SerializerRef<UnpackerBuffer<buffer::IOBuffer>, Traits...>>(
dispatch::Standard::unpack<T, UnpackerBuffer<buffer::IOBuffer>, UserTraitHolder<Traits...>>(
t, buffer::IOBuffer::ReadFromFileTag{}, file
);
}

template <typename... Traits, typename T, typename StreamT>
template <typename T, typename... Traits, typename StreamT>
void serializeToStream(T& target, StreamT& stream) {
auto len = getSize<T>(target);
dispatch::Standard::pack<T, SerializerRef<StreamPacker<StreamT>, Traits...>>(
dispatch::Standard::pack<T, StreamPacker<StreamT>, UserTraitHolder<Traits...>>(
target, len, stream
);
}
Expand All @@ -135,15 +135,15 @@ template <typename T, typename... Traits, typename StreamT>
std::unique_ptr<T> deserializeFromStream(StreamT& stream) {
auto mem = dispatch::Standard::allocate<T>();
T* t_buf = dispatch::Standard::construct<T>(mem);
auto t = dispatch::Standard::unpack<T, SerializerRef<StreamUnpacker<StreamT>, Traits...>>(
auto t = dispatch::Standard::unpack<T, StreamUnpacker<StreamT>, UserTraitHolder<Traits...>>(
t_buf, stream
);
return std::unique_ptr<T>(t);
}

template <typename... Traits, typename T, typename StreamT>
template <typename T, typename... Traits, typename StreamT>
void deserializeInPlaceFromStream(StreamT& stream, T* t) {
dispatch::Standard::unpack<T, SerializerRef<StreamUnpacker<StreamT>, Traits...>>(
dispatch::Standard::unpack<T, StreamUnpacker<StreamT>, UserTraitHolder<Traits...>>(
t, stream
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/checkpoint/container/atomic_serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ template <
typename SerializerT,
typename T,
typename = std::enable_if_t<
checkpoint::is_footprinter<SerializerT>::value
checkpoint::is_footprinter_v<SerializerT>
>
>
void serialize(SerializerT& s, const std::atomic<T>& atomic) {
Expand Down
4 changes: 1 addition & 3 deletions src/checkpoint/container/function_serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ template <
typename Res,
typename... ArgTypes,
typename = std::enable_if_t<
checkpoint::is_footprinter<
SerializerT
>::value
checkpoint::is_footprinter_v<SerializerT>
>
>
void serializeFunction(SerializerT& s, std::function<Res(ArgTypes...)>& fn) {
Expand Down
4 changes: 2 additions & 2 deletions src/checkpoint/container/kokkos_unordered_map_serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ template <
typename Hasher, typename EqualTo
>
typename std::enable_if_t<
not checkpoint::is_footprinter<SerializerT>::value, void
not checkpoint::is_footprinter_v<SerializerT>, void
> serialize(
SerializerT& s,
Kokkos::UnorderedMap<Key, Value, Device, Hasher, EqualTo>& map
Expand All @@ -140,7 +140,7 @@ template <
typename Hasher, typename EqualTo
>
typename std::enable_if_t<
checkpoint::is_footprinter<SerializerT>::value, void
checkpoint::is_footprinter_v<SerializerT>, void
> serialize(
SerializerT& s,
Kokkos::UnorderedMap<Key, Value, Device, Hasher, EqualTo>& map
Expand Down
6 changes: 3 additions & 3 deletions src/checkpoint/container/list_serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace checkpoint {

template <typename Serializer, typename ContainerT, typename ElmT>
inline typename std::enable_if_t<
not checkpoint::is_footprinter<Serializer>::value, void
not checkpoint::is_footprinter_v<Serializer>, void
>
deserializeOrderedElems(
Serializer& s, ContainerT& cont, typename ContainerT::size_type size,
Expand All @@ -76,7 +76,7 @@ deserializeOrderedElems(

template <typename Serializer, typename ContainerT, typename ElmT>
inline typename std::enable_if_t<
not checkpoint::is_footprinter<Serializer>::value, void
not checkpoint::is_footprinter_v<Serializer>, void
>
deserializeOrderedElems(
Serializer& s, ContainerT& cont, typename ContainerT::size_type size,
Expand All @@ -96,7 +96,7 @@ deserializeOrderedElems(

template <typename Serializer, typename ContainerT, typename ElmT>
inline typename std::enable_if_t<
checkpoint::is_footprinter<Serializer>::value, void
checkpoint::is_footprinter_v<Serializer>, void
>
deserializeOrderedElems(
Serializer&, ContainerT&, typename ContainerT::size_type
Expand Down
4 changes: 2 additions & 2 deletions src/checkpoint/container/map_serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace checkpoint {

template <typename Serializer, typename ContainerT, typename ElmT>
inline typename std::enable_if_t<
not checkpoint::is_footprinter<Serializer>::value,
not checkpoint::is_footprinter_v<Serializer>,
void
> deserializeEmplaceElems(
Serializer& s, ContainerT& cont, typename ContainerT::size_type size
Expand All @@ -77,7 +77,7 @@ inline typename std::enable_if_t<

template <typename Serializer, typename ContainerT, typename ElmT>
inline typename std::enable_if_t<
checkpoint::is_footprinter<Serializer>::value,
checkpoint::is_footprinter_v<Serializer>,
void
> deserializeEmplaceElems(
Serializer&, ContainerT&, typename ContainerT::size_type
Expand Down
2 changes: 1 addition & 1 deletion src/checkpoint/container/queue_serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ template <
typename SerializerT,
typename Q,
typename = std::enable_if_t<
checkpoint::is_footprinter<SerializerT>::value
checkpoint::is_footprinter_v<SerializerT>
>
>
void serializeQueueLikeContainer(SerializerT& s, const Q& q) {
Expand Down
4 changes: 2 additions & 2 deletions src/checkpoint/container/raw_ptr_serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ template <
typename SerializerT,
typename T,
typename = std::enable_if_t<
checkpoint::is_footprinter<SerializerT>::value
checkpoint::is_footprinter_v<SerializerT>
>
>
void serialize(SerializerT& s, T* ptr) {
Expand Down Expand Up @@ -92,7 +92,7 @@ void serializeRawPtr(SerializerT& s, void* ptr) {
#define CHECKPOINT_FOOTPRINT_PIMPL_WITH_SIZEOF_PTR(PIMPL_TYPE) \
template < \
typename SerializerT, \
typename = std::enable_if_t< checkpoint::is_footprinter<SerializerT>::value > \
typename = std::enable_if_t< checkpoint::is_footprinter_v<SerializerT> > \
> \
void serialize(SerializerT &s, PIMPL_TYPE *t) { \
s.countBytes(t); \
Expand Down
2 changes: 1 addition & 1 deletion src/checkpoint/container/shared_ptr_serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ template <
typename SerializerT,
typename T,
typename = std::enable_if_t<
checkpoint::is_footprinter<SerializerT>::value
checkpoint::is_footprinter_v<SerializerT>
>
>
void serialize(SerializerT& s, std::shared_ptr<T>& ptr) {
Expand Down
2 changes: 1 addition & 1 deletion src/checkpoint/container/thread_serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ namespace checkpoint {
template <
typename SerializerT,
typename = std::enable_if_t<
checkpoint::is_footprinter<SerializerT>::value
checkpoint::is_footprinter_v<SerializerT>
>
>
void serialize(SerializerT& s, const std::thread& t) {
Expand Down
Loading

0 comments on commit 769b6ce

Please sign in to comment.