Merge pull request #4106 from akva2/serializer_handle_tuples

changed: handle tuples in eclmpiserializer
This commit is contained in:
Arne Morten Kvarving 2022-09-15 12:59:13 +02:00 committed by GitHub
commit 3a19ab31bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 112 deletions

View File

@ -89,8 +89,8 @@ public:
{
if constexpr (is_ptr<T>::value) {
ptr(data);
} else if constexpr (is_pair<T>::value) {
pair(data);
} else if constexpr (is_pair_or_tuple<T>::value) {
tuple(data);
} else if constexpr (is_variant<T>::value) {
variant(data);
} else if constexpr (is_optional<T>::value) {
@ -124,8 +124,8 @@ public:
auto handle = [&](auto& d)
{
for (auto& it : d) {
if constexpr (is_pair<T>::value)
pair(it);
if constexpr (is_pair_or_tuple<T>::value)
tuple(it);
else if constexpr (is_ptr<T>::value)
ptr(it);
else if constexpr (is_vector<T>::value)
@ -177,6 +177,8 @@ public:
}
}
//! \brief Handler for arrays.
//! \param data The array to (de-)serialize
template <class Array>
void array(Array& data)
{
@ -184,8 +186,8 @@ public:
auto handle = [&](auto& d) {
for (auto& it : d) {
if constexpr (is_pair<T>::value)
pair(it);
if constexpr (is_pair_or_tuple<T>::value)
tuple(it);
else if constexpr (is_ptr<T>::value)
ptr(it);
else if constexpr (has_serializeOp<T>::value)
@ -260,7 +262,14 @@ public:
const_cast<std::optional<T>&>(data) = res;
}
}
}
//! \brief Handler for std::tuple.
//! \param data The tuple to (de-)serialize
template<class Tuple>
void tuple(const Tuple& data)
{
tuple_call(data);
}
//! \brief Handler for maps.
@ -288,8 +297,8 @@ public:
auto keyHandle = [&](auto& d)
{
if constexpr (is_pair<Key>::value)
pair(d);
if constexpr (is_pair_or_tuple<Key>::value)
tuple(d);
else if constexpr (has_serializeOp<Key>::value)
d.serializeOp(*this);
else
@ -321,6 +330,9 @@ public:
}
}
//! \brief Handler for sets.
//! \tparam Set set type
//! \param data The set to (de-)serialize
template<class Set>
void set(Set& data)
{
@ -498,6 +510,20 @@ protected:
variadic_call(std::forward<Args>(args)...);
}
template<std::size_t I = 0, typename Tuple>
typename std::enable_if<I == std::tuple_size<Tuple>::value, void>::type
tuple_call(const Tuple&)
{
}
template<std::size_t I = 0, typename Tuple>
typename std::enable_if<I != std::tuple_size<Tuple>::value, void>::type
tuple_call(const Tuple& tuple)
{
(*this)(std::get<I>(tuple));
tuple_call<I+1>(tuple);
}
//! \brief Enumeration of operations.
enum class Operation {
PACKSIZE, //!< Calculating serialization buffer size
@ -505,17 +531,6 @@ protected:
UNPACK //!< Performing de-serialization
};
//! \brief Predicate for detecting pairs.
template<class T>
struct is_pair {
constexpr static bool value = false;
};
template<class T1, class T2>
struct is_pair<std::pair<T1,T2>> {
constexpr static bool value = true;
};
//! \brief Predicate for detecting vectors.
template<class T>
struct is_vector {
@ -538,6 +553,22 @@ protected:
constexpr static bool value = true;
};
//! \brief Predicate for detecting pairs and tuples.
template<class T>
struct is_pair_or_tuple {
constexpr static bool value = false;
};
template<class... Ts>
struct is_pair_or_tuple<std::tuple<Ts...>> {
constexpr static bool value = true;
};
template<class T1, class T2>
struct is_pair_or_tuple<std::pair<T1,T2>> {
constexpr static bool value = true;
};
//! \brief Predicate for smart pointers.
template<class T>
struct is_ptr {
@ -623,21 +654,6 @@ protected:
T, std::void_t<decltype(std::declval<T>().serializeOp(std::declval<EclMpiSerializer&>()))>
> : public std::true_type {};
//! \brief Handler for pairs.
template<class T1, class T2>
void pair(const std::pair<T1,T2>& data)
{
if constexpr (has_serializeOp<T1>::value)
const_cast<T1&>(data.first).serializeOp(*this);
else
(*this)(data.first);
if constexpr (has_serializeOp<T2>::value)
const_cast<T2&>(data.second).serializeOp(*this);
else
(*this)(data.second);
}
//! \brief Handler for smart pointers.
template<class PtrType>
void ptr(const PtrType& data)

View File

@ -61,26 +61,6 @@ std::size_t packSize(const T* data, std::size_t l, Opm::Parallel::MPIComm comm)
return packSize(data, l, comm, typename std::is_pod<T>::type());
}
template<std::size_t I = 0, typename Tuple>
typename std::enable_if<I == std::tuple_size<Tuple>::value, std::size_t>::type
pack_size_tuple_entry(const Tuple&, Opm::Parallel::MPIComm)
{
return 0;
}
template<std::size_t I = 0, typename Tuple>
typename std::enable_if<I != std::tuple_size<Tuple>::value, std::size_t>::type
pack_size_tuple_entry(const Tuple& tuple, Opm::Parallel::MPIComm comm)
{
return packSize(std::get<I>(tuple), comm) + pack_size_tuple_entry<I+1>(tuple, comm);
}
template<class... Ts>
std::size_t packSize(const std::tuple<Ts...>& data, Opm::Parallel::MPIComm comm)
{
return pack_size_tuple_entry(data, comm);
}
std::size_t packSize(const char* str, Opm::Parallel::MPIComm comm)
{
#if HAVE_MPI
@ -175,29 +155,6 @@ void pack(const T* data, std::size_t l, std::vector<char>& buffer, int& position
pack(data, l, buffer, position, comm, typename std::is_pod<T>::type());
}
template<std::size_t I = 0, typename Tuple>
typename std::enable_if<I == std::tuple_size<Tuple>::value, void>::type
pack_tuple_entry(const Tuple&, std::vector<char>&, int&,
Opm::Parallel::MPIComm)
{
}
template<std::size_t I = 0, typename Tuple>
typename std::enable_if<I != std::tuple_size<Tuple>::value, void>::type
pack_tuple_entry(const Tuple& tuple, std::vector<char>& buffer,
int& position, Opm::Parallel::MPIComm comm)
{
pack(std::get<I>(tuple), buffer, position, comm);
pack_tuple_entry<I+1>(tuple, buffer, position, comm);
}
template<class... Ts>
void pack(const std::tuple<Ts...>& data, std::vector<char>& buffer,
int& position, Opm::Parallel::MPIComm comm)
{
pack_tuple_entry(data, buffer, position, comm);
}
void pack(const char* str, std::vector<char>& buffer, int& position,
Opm::Parallel::MPIComm comm)
{
@ -268,29 +225,6 @@ void unpack(T* data, const std::size_t& l, std::vector<char>& buffer, int& posit
unpack(data, l, buffer, position, comm, typename std::is_pod<T>::type());
}
template<std::size_t I = 0, typename Tuple>
typename std::enable_if<I == std::tuple_size<Tuple>::value, void>::type
unpack_tuple_entry(Tuple&, std::vector<char>&, int&,
Opm::Parallel::MPIComm)
{
}
template<std::size_t I = 0, typename Tuple>
typename std::enable_if<I != std::tuple_size<Tuple>::value, void>::type
unpack_tuple_entry(Tuple& tuple, std::vector<char>& buffer,
int& position, Opm::Parallel::MPIComm comm)
{
unpack(std::get<I>(tuple), buffer, position, comm);
unpack_tuple_entry<I+1>(tuple, buffer, position, comm);
}
template<class... Ts>
void unpack(std::tuple<Ts...>& data, std::vector<char>& buffer,
int& position, Opm::Parallel::MPIComm comm)
{
unpack_tuple_entry(data, buffer, position, comm);
}
void unpack(char* str, std::size_t length, std::vector<char>& buffer, int& position,
Opm::Parallel::MPIComm comm)
{

View File

@ -27,7 +27,6 @@
#include <bitset>
#include <cstddef>
#include <string>
#include <tuple>
#include <typeinfo>
namespace Opm
@ -74,9 +73,6 @@ std::size_t packSize(const T& data, Opm::Parallel::MPIComm comm)
return packSize(data, comm, typename std::is_pod<T>::type());
}
template<class... Ts>
std::size_t packSize(const std::tuple<Ts...>& data, Opm::Parallel::MPIComm comm);
std::size_t packSize(const char* str, Opm::Parallel::MPIComm comm);
template<std::size_t Size>
@ -125,10 +121,6 @@ void pack(const T& data, std::vector<char>& buffer, int& position,
pack(data, buffer, position, comm, typename std::is_pod<T>::type());
}
template<class... Ts>
void pack(const std::tuple<Ts...>& data, std::vector<char>& buffer,
int& position, Opm::Parallel::MPIComm comm);
void pack(const char* str, std::vector<char>& buffer, int& position,
Opm::Parallel::MPIComm comm);
@ -180,10 +172,6 @@ void unpack(T& data, std::vector<char>& buffer, int& position,
unpack(data, buffer, position, comm, typename std::is_pod<T>::type());
}
template<class... Ts>
void unpack(std::tuple<Ts...>& data, std::vector<char>& buffer,
int& position, Opm::Parallel::MPIComm comm);
void unpack(char* str, std::size_t length, std::vector<char>& buffer, int& position,
Opm::Parallel::MPIComm comm);