mirror of
https://github.com/OPM/opm-simulators.git
synced 2025-02-25 18:55:30 -06:00
added: support for std::optional in eclmpiserializer
This commit is contained in:
parent
a4ea6e9658
commit
dfd2109665
@ -22,6 +22,7 @@
|
|||||||
#define ECL_MPI_SERIALIZER_HH
|
#define ECL_MPI_SERIALIZER_HH
|
||||||
|
|
||||||
#include <opm/simulators/utils/ParallelRestart.hpp>
|
#include <opm/simulators/utils/ParallelRestart.hpp>
|
||||||
|
#include <optional>
|
||||||
#include <variant>
|
#include <variant>
|
||||||
|
|
||||||
namespace Opm {
|
namespace Opm {
|
||||||
@ -51,6 +52,8 @@ public:
|
|||||||
pair(data);
|
pair(data);
|
||||||
} else if constexpr (is_variant<T>::value) {
|
} else if constexpr (is_variant<T>::value) {
|
||||||
variant(data);
|
variant(data);
|
||||||
|
} else if constexpr (is_optional<T>::value) {
|
||||||
|
optional(data);
|
||||||
} else {
|
} else {
|
||||||
if (m_op == Operation::PACKSIZE)
|
if (m_op == Operation::PACKSIZE)
|
||||||
m_packSize += Mpi::packSize(data, m_comm);
|
m_packSize += Mpi::packSize(data, m_comm);
|
||||||
@ -194,6 +197,45 @@ public:
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//! \brief Handler for std::optional.
|
||||||
|
//! \tparam T Type for data
|
||||||
|
//! \param data The optional to (de-)serialize
|
||||||
|
template<class T>
|
||||||
|
void optional(const std::optional<T>& data)
|
||||||
|
{
|
||||||
|
if (m_op == Operation::PACKSIZE) {
|
||||||
|
m_packSize += Mpi::packSize(data.has_value(), m_comm);
|
||||||
|
if (data.has_value()) {
|
||||||
|
if constexpr (has_serializeOp<T>::value) {
|
||||||
|
const_cast<T&>(*data).serializeOp(*this);
|
||||||
|
} else
|
||||||
|
m_packSize += Mpi::packSize(*data, m_comm);
|
||||||
|
}
|
||||||
|
} else if (m_op == Operation::PACK) {
|
||||||
|
Mpi::pack(data.has_value(), m_buffer, m_position, m_comm);
|
||||||
|
if (data.has_value()) {
|
||||||
|
if constexpr (has_serializeOp<T>::value) {
|
||||||
|
const_cast<T&>(*data).serializeOp(*this);
|
||||||
|
} else {
|
||||||
|
Mpi::pack(*data, m_buffer, m_position, m_comm);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (m_op == Operation::UNPACK) {
|
||||||
|
bool has;
|
||||||
|
Mpi::unpack(has, m_buffer, m_position, m_comm);
|
||||||
|
if (has) {
|
||||||
|
T res;
|
||||||
|
if constexpr (has_serializeOp<T>::value) {
|
||||||
|
res.serializeOp(*this);
|
||||||
|
} else {
|
||||||
|
Mpi::unpack(res, m_buffer, m_position, m_comm);
|
||||||
|
}
|
||||||
|
const_cast<std::optional<T>&>(data) = res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
//! \brief Handler for maps.
|
//! \brief Handler for maps.
|
||||||
//! \tparam Map map type
|
//! \tparam Map map type
|
||||||
//! \tparam complexType Whether or not Data in map is a complex type
|
//! \tparam complexType Whether or not Data in map is a complex type
|
||||||
@ -371,6 +413,17 @@ protected:
|
|||||||
constexpr static bool value = true;
|
constexpr static bool value = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//! \brief Predicate for std::optional.
|
||||||
|
template<class T>
|
||||||
|
struct is_optional {
|
||||||
|
constexpr static bool value = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<class T1>
|
||||||
|
struct is_optional<std::optional<T1>> {
|
||||||
|
constexpr static bool value = true;
|
||||||
|
};
|
||||||
|
|
||||||
//! \brief Handler for pairs.
|
//! \brief Handler for pairs.
|
||||||
//! \details If data is POD or a string, we pass it to the underlying serializer,
|
//! \details If data is POD or a string, we pass it to the underlying serializer,
|
||||||
//! if not we assume a complex type.
|
//! if not we assume a complex type.
|
||||||
@ -404,6 +457,21 @@ protected:
|
|||||||
data->serializeOp(*this);
|
data->serializeOp(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//! \brief Checks if a type has a serializeOp member.
|
||||||
|
//! \detail Ideally we would check for the serializeOp member,
|
||||||
|
//! but this is a member template. For simplicity,
|
||||||
|
//! we use serializeObject as our check for now.
|
||||||
|
template <typename T>
|
||||||
|
class has_serializeOp
|
||||||
|
{
|
||||||
|
using yes_type = char;
|
||||||
|
using no_type = long;
|
||||||
|
template <typename U> static yes_type test(decltype(&U::serializeObject));
|
||||||
|
template <typename U> static no_type test(...);
|
||||||
|
public:
|
||||||
|
static constexpr bool value = sizeof(test<T>(0)) == sizeof(yes_type);
|
||||||
|
};
|
||||||
|
|
||||||
Dune::CollectiveCommunication<Dune::MPIHelper::MPICommunicator> m_comm; //!< Communicator to broadcast using
|
Dune::CollectiveCommunication<Dune::MPIHelper::MPICommunicator> m_comm; //!< Communicator to broadcast using
|
||||||
|
|
||||||
Operation m_op = Operation::PACKSIZE; //!< Current operation
|
Operation m_op = Operation::PACKSIZE; //!< Current operation
|
||||||
|
Loading…
Reference in New Issue
Block a user