Skip to content

Commit

Permalink
#161 simplify vector's elements reconstruction
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakub Strzebonski committed Jul 1, 2021
1 parent db73eb4 commit 984b3cd
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 61 deletions.
70 changes: 9 additions & 61 deletions src/checkpoint/container/vector_serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#define INCLUDED_CHECKPOINT_CONTAINER_VECTOR_SERIALIZE_H

#include "checkpoint/common.h"
#include "checkpoint/dispatch/allocator.h"
#include "checkpoint/dispatch/dispatch.h"
#include "checkpoint/dispatch/reconstructor.h"
#include "checkpoint/serializers/serializers_headers.h"
Expand Down Expand Up @@ -78,78 +79,25 @@ serializeVectorMeta(SerializerT& s, std::vector<T, VectorAllocator>& vec) {
}

template <typename T, typename VectorAllocator>
void constructVectorDataWithResize(
void constructVectorData(
SerialSizeType const vec_size, std::vector<T, VectorAllocator>& vec,
typename ReconstructorTraits<T>::template isDefaultConsType<T>* = nullptr
) {
vec.resize(vec_size);
}

template <typename T, typename VectorAllocator>
void constructVectorDataWithResize(
SerialSizeType const, std::vector<T, VectorAllocator>& vec,
typename ReconstructorTraits<T>::template isNotDefaultConsType<T>* = nullptr
) {
static_assert(
SerializableTraits<T, void>::is_tagged_constructible or
SerializableTraits<T, void>::is_reconstructible or
std::is_default_constructible<T>::value,
"Either a default constructor, reconstruct() function, or tagged "
"constructor are required for std::vector de-serialization"
);
}

template <typename T>
struct Allocated {
Allocated() : buf{dispatch::Standard::template allocate<T>()} { }
~Allocated() { std::allocator<T>{}.deallocate(reinterpret_cast<T*>(buf), 1); }

SerialByteType* buf;
};

template <typename T, typename VectorAllocator>
void constructVectorDataReconstruct(
SerialSizeType const vec_size, std::vector<T, VectorAllocator>& vec,
typename ReconstructorTraits<T>::template isReconstructibleType<T>* = nullptr
) {
Allocated<T> const allocated;
auto& t = T::reconstruct(allocated.buf);
vec.resize(vec_size, t);
}

template <typename T, typename VectorAllocator>
void constructVectorDataReconstruct(
SerialSizeType const vec_size, std::vector<T, VectorAllocator>& vec,
typename ReconstructorTraits<T>::template isNonIntReconstructibleType<T>* = nullptr
) {
Allocated<T> const allocated;
T* t = nullptr;
reconstruct(t, allocated.buf);
vec.resize(vec_size, *t);
}

template <typename T, typename VectorAllocator>
void constructVectorDataReconstruct(
SerialSizeType const vec_size, std::vector<T, VectorAllocator>& vec,
typename ReconstructorTraits<T>::template isNotReconstructibleType<T>* = nullptr
) {
constructVectorDataWithResize(vec_size, vec);
}

template <typename T, typename VectorAllocator>
void constructVectorData(
SerialSizeType const vec_size, std::vector<T, VectorAllocator>& vec,
typename ReconstructorTraits<T>::template isTaggedConstructibleType<T>* = nullptr
typename ReconstructorTraits<T>::template isNotDefaultConsType<T>* = nullptr
) {
vec.resize(vec_size, T{SERIALIZE_CONSTRUCT_TAG{}});
}
using Alloc = dispatch::Allocator<T> const;
using Reconstructor =
dispatch::Reconstructor<typename dispatch::CleanType<T>::CleanT>;

template <typename T, typename VectorAllocator>
void constructVectorData(
SerialSizeType const vec_size, std::vector<T, VectorAllocator>& vec,
typename ReconstructorTraits<T>::template isNotTaggedConstructibleType<T>* = nullptr
) {
constructVectorDataReconstruct(vec_size, vec);
Alloc allocated;
auto* reconstructed = Reconstructor::construct(allocated.buf);
vec.resize(vec_size, *reconstructed);
}

template <typename Serializer, typename T, typename VectorAllocator>
Expand Down
64 changes: 64 additions & 0 deletions src/checkpoint/dispatch/allocator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
//@HEADER
// *****************************************************************************
//
// allocator.h
// DARMA Toolkit v. 1.0.0
// DARMA/checkpoint => Serialization Library
//
// Copyright 2019 National Technology & Engineering Solutions of Sandia, LLC
// (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.
// Government retains certain rights in this software.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from this
// software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact [email protected]
//
// *****************************************************************************
//@HEADER
*/

#if !defined INCLUDED_CHECKPOINT_DISPATCH_ALLOCATOR_H
#define INCLUDED_CHECKPOINT_DISPATCH_ALLOCATOR_H

#include "checkpoint/dispatch/dispatch.h"

#include <memory>

namespace checkpoint { namespace dispatch {

template <typename T>
struct Allocator {
Allocator() : buf{Standard::template allocate<T>()} { }
~Allocator() { std::allocator<T>{}.deallocate(reinterpret_cast<T*>(buf), 1); }

SerialByteType* buf;
};

}} // namespace checkpoint::dispatch

#endif /*INCLUDED_CHECKPOINT_DISPATCH_ALLOCATOR_H*/

0 comments on commit 984b3cd

Please sign in to comment.