2020-01-06 03:25:59 -06:00
|
|
|
/*
|
|
|
|
This file is part of the Open Porous Media project (OPM).
|
|
|
|
|
|
|
|
OPM is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
OPM is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with OPM. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
Consult the COPYING file in the top-level source directory of this
|
|
|
|
module for the precise wording of the license and the list of
|
|
|
|
copyright holders.
|
|
|
|
*/
|
|
|
|
#ifndef ECL_MPI_SERIALIZER_HH
|
|
|
|
#define ECL_MPI_SERIALIZER_HH
|
|
|
|
|
2022-09-07 04:21:35 -05:00
|
|
|
#include <opm/simulators/utils/MPIPacker.hpp>
|
2022-08-29 07:03:52 -05:00
|
|
|
#include <opm/simulators/utils/ParallelCommunication.hpp>
|
2021-09-29 04:45:45 -05:00
|
|
|
|
2022-09-08 02:16:07 -05:00
|
|
|
#include <algorithm>
|
|
|
|
#include <functional>
|
2022-09-07 05:03:10 -05:00
|
|
|
#include <map>
|
2020-09-23 05:23:18 -05:00
|
|
|
#include <optional>
|
2022-09-07 05:03:10 -05:00
|
|
|
#include <set>
|
2021-09-29 04:45:45 -05:00
|
|
|
#include <type_traits>
|
|
|
|
#include <utility>
|
2022-09-07 05:03:10 -05:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
2020-06-09 04:25:47 -05:00
|
|
|
#include <variant>
|
2022-09-07 05:03:10 -05:00
|
|
|
#include <vector>
|
2020-01-06 03:25:59 -06:00
|
|
|
|
2022-09-07 06:43:23 -05:00
|
|
|
namespace Opm {
|
2022-09-08 02:16:07 -05:00
|
|
|
namespace detail {
|
2022-09-01 05:23:24 -05:00
|
|
|
|
|
|
|
template<typename ...Ts>
|
|
|
|
struct MakeVariantImpl
|
|
|
|
{
|
|
|
|
|
|
|
|
template<std::size_t Index, typename, typename ...Rest>
|
|
|
|
static decltype(auto) make_variant(std::size_t index)
|
|
|
|
{
|
|
|
|
if(Index == index)
|
|
|
|
return std::variant<Ts...>{std::in_place_index_t<Index>{}};
|
|
|
|
|
|
|
|
if constexpr(sizeof...(Rest) != 0)
|
|
|
|
return make_variant<Index + 1, Rest...>(index);
|
|
|
|
else
|
|
|
|
throw std::runtime_error("Invalid variant index");
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename ...Ts>
|
|
|
|
decltype(auto) make_variant(std::size_t index)
|
|
|
|
{
|
|
|
|
return detail::MakeVariantImpl<Ts...>::template make_variant<0, Ts...>(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T>
|
2022-09-13 06:38:30 -05:00
|
|
|
using remove_cvr_t = std::remove_cv_t<std::remove_reference_t<T>>;
|
2022-09-01 05:23:24 -05:00
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
2020-03-19 03:09:58 -05:00
|
|
|
/*! \brief Class for (de-)serializing and broadcasting data in parallel.
|
2022-09-02 06:02:35 -05:00
|
|
|
*! \details If the class has a serializeOp member this is used,
|
|
|
|
* if not it is passed on to the underlying primitive serializer.
|
2020-03-19 03:09:58 -05:00
|
|
|
*/
|
|
|
|
|
2020-01-06 03:25:59 -06:00
|
|
|
class EclMpiSerializer {
|
|
|
|
public:
|
2020-03-19 03:09:58 -05:00
|
|
|
//! \brief Constructor.
|
|
|
|
//! \param comm The global communicator to broadcast using
|
2021-05-25 05:57:11 -05:00
|
|
|
explicit EclMpiSerializer(Opm::Parallel::Communication comm) :
|
2020-01-06 03:25:59 -06:00
|
|
|
m_comm(comm)
|
|
|
|
{}
|
|
|
|
|
2022-09-08 02:16:07 -05:00
|
|
|
//! \brief Applies current serialization op to the passed data.
|
2020-01-06 03:25:59 -06:00
|
|
|
template<class T>
|
2020-03-09 08:20:51 -05:00
|
|
|
void operator()(const T& data)
|
|
|
|
{
|
2020-03-18 03:17:09 -05:00
|
|
|
if constexpr (is_ptr<T>::value) {
|
|
|
|
ptr(data);
|
2022-09-13 09:11:44 -05:00
|
|
|
} else if constexpr (is_pair_or_tuple<T>::value) {
|
|
|
|
tuple(data);
|
2020-06-09 04:25:47 -05:00
|
|
|
} else if constexpr (is_variant<T>::value) {
|
|
|
|
variant(data);
|
2020-09-23 05:23:18 -05:00
|
|
|
} else if constexpr (is_optional<T>::value) {
|
2022-09-07 05:03:10 -05:00
|
|
|
optional(data);
|
2022-09-07 05:03:10 -05:00
|
|
|
} else if constexpr (is_vector<T>::value) {
|
2022-09-08 03:21:55 -05:00
|
|
|
vector(data);
|
2022-09-07 05:03:10 -05:00
|
|
|
} else if constexpr (is_map<T>::value) {
|
2022-09-08 03:21:55 -05:00
|
|
|
map(data);
|
2022-09-07 05:03:10 -05:00
|
|
|
} else if constexpr (is_array<T>::value) {
|
2022-09-08 03:21:55 -05:00
|
|
|
array(data);
|
2022-09-07 05:03:10 -05:00
|
|
|
} else if constexpr (is_set<T>::value) {
|
2022-09-08 03:21:55 -05:00
|
|
|
set(data);
|
2022-09-07 05:03:10 -05:00
|
|
|
} else if constexpr (has_serializeOp<detail::remove_cvr_t<T>>::value) {
|
2022-09-08 02:16:07 -05:00
|
|
|
const_cast<T&>(data).serializeOp(*this);
|
2020-03-16 08:35:50 -05:00
|
|
|
} else {
|
2022-09-08 02:16:07 -05:00
|
|
|
if (m_op == Operation::PACKSIZE)
|
|
|
|
m_packSize += Mpi::Packer::packSize(data, m_comm);
|
|
|
|
else if (m_op == Operation::PACK)
|
|
|
|
Mpi::Packer::pack(data, m_buffer, m_position, m_comm);
|
|
|
|
else if (m_op == Operation::UNPACK)
|
|
|
|
Mpi::Packer::unpack(const_cast<T&>(data), m_buffer, m_position, m_comm);
|
2020-03-16 08:35:50 -05:00
|
|
|
}
|
2020-01-06 03:25:59 -06:00
|
|
|
}
|
|
|
|
|
2022-09-08 04:32:23 -05:00
|
|
|
//! \brief Call this to serialize data.
|
|
|
|
//! \tparam T Type of class to serialize
|
|
|
|
//! \param data Class to serialize
|
|
|
|
template<class T>
|
|
|
|
void pack(const T& data)
|
|
|
|
{
|
|
|
|
m_op = Operation::PACKSIZE;
|
|
|
|
m_packSize = 0;
|
|
|
|
(*this)(data);
|
|
|
|
m_position = 0;
|
|
|
|
m_buffer.resize(m_packSize);
|
|
|
|
m_op = Operation::PACK;
|
|
|
|
(*this)(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
//! \brief Call this to de-serialize data.
|
|
|
|
//! \tparam T Type of class to de-serialize
|
|
|
|
//! \param data Class to de-serialize
|
|
|
|
template<class T>
|
|
|
|
void unpack(T& data)
|
|
|
|
{
|
|
|
|
m_position = 0;
|
|
|
|
m_op = Operation::UNPACK;
|
|
|
|
(*this)(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
//! \brief Serialize and broadcast on root process, de-serialize on
|
|
|
|
//! others.
|
|
|
|
//!
|
|
|
|
//! \tparam T Type of class to broadcast
|
|
|
|
//! \param data Class to broadcast
|
|
|
|
//! \param root Process to broadcast from
|
|
|
|
template<class T>
|
|
|
|
void broadcast(T& data, int root = 0)
|
|
|
|
{
|
|
|
|
if (m_comm.size() == 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (m_comm.rank() == root) {
|
|
|
|
try {
|
|
|
|
pack(data);
|
|
|
|
m_packSize = m_position;
|
|
|
|
m_comm.broadcast(&m_packSize, 1, root);
|
|
|
|
m_comm.broadcast(m_buffer.data(), m_position, root);
|
|
|
|
} catch (...) {
|
|
|
|
m_packSize = std::numeric_limits<size_t>::max();
|
|
|
|
m_comm.broadcast(&m_packSize, 1, root);
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
m_comm.broadcast(&m_packSize, 1, root);
|
|
|
|
if (m_packSize == std::numeric_limits<size_t>::max()) {
|
|
|
|
throw std::runtime_error("Error detected in parallel serialization");
|
|
|
|
}
|
|
|
|
|
|
|
|
m_buffer.resize(m_packSize);
|
|
|
|
m_comm.broadcast(m_buffer.data(), m_packSize, root);
|
|
|
|
unpack(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
void broadcast(int root, Args&&... args)
|
|
|
|
{
|
|
|
|
if (m_comm.size() == 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (m_comm.rank() == root) {
|
|
|
|
try {
|
|
|
|
m_op = Operation::PACKSIZE;
|
|
|
|
m_packSize = 0;
|
|
|
|
variadic_call(args...);
|
|
|
|
m_position = 0;
|
|
|
|
m_buffer.resize(m_packSize);
|
|
|
|
m_op = Operation::PACK;
|
|
|
|
variadic_call(args...);
|
|
|
|
m_packSize = m_position;
|
|
|
|
m_comm.broadcast(&m_packSize, 1, root);
|
|
|
|
m_comm.broadcast(m_buffer.data(), m_position, root);
|
|
|
|
} catch (...) {
|
|
|
|
m_packSize = std::numeric_limits<size_t>::max();
|
|
|
|
m_comm.broadcast(&m_packSize, 1, root);
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
m_comm.broadcast(&m_packSize, 1, root);
|
|
|
|
if (m_packSize == std::numeric_limits<size_t>::max()) {
|
|
|
|
throw std::runtime_error("Error detected in parallel serialization");
|
|
|
|
}
|
|
|
|
m_buffer.resize(m_packSize);
|
|
|
|
m_comm.broadcast(m_buffer.data(), m_packSize, root);
|
|
|
|
m_position = 0;
|
|
|
|
m_op = Operation::UNPACK;
|
|
|
|
variadic_call(std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//! \brief Serialize and broadcast on root process, de-serialize and append on
|
|
|
|
//! others.
|
|
|
|
//!
|
|
|
|
//! \tparam T Type of class to broadcast
|
|
|
|
//! \param data Class to broadcast
|
|
|
|
//! \param root Process to broadcast from
|
|
|
|
template<class T>
|
|
|
|
void append(T& data, int root = 0)
|
|
|
|
{
|
|
|
|
if (m_comm.size() == 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
T tmp;
|
|
|
|
T& bcast = m_comm.rank() == root ? data : tmp;
|
|
|
|
broadcast(bcast);
|
|
|
|
|
|
|
|
if (m_comm.rank() != root)
|
|
|
|
data.append(tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
//! \brief Returns current position in buffer.
|
|
|
|
size_t position() const
|
|
|
|
{
|
|
|
|
return m_position;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! \brief Returns true if we are currently doing a serialization operation.
|
|
|
|
bool isSerializing() const
|
|
|
|
{
|
|
|
|
return m_op != Operation::UNPACK;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2020-03-19 03:09:58 -05:00
|
|
|
//! \brief Handler for vectors.
|
|
|
|
//! \tparam T Type for vector elements
|
|
|
|
//! \param data The vector to (de-)serialize
|
2022-09-02 06:02:35 -05:00
|
|
|
template <typename T>
|
2022-09-08 03:21:55 -05:00
|
|
|
void vector(const std::vector<T>& data)
|
2020-03-10 08:28:00 -05:00
|
|
|
{
|
2022-09-08 02:16:07 -05:00
|
|
|
if constexpr (std::is_pod_v<T>) {
|
|
|
|
if (m_op == Operation::PACKSIZE) {
|
|
|
|
(*this)(data.size());
|
|
|
|
m_packSize += Mpi::Packer::packSize(data.data(), data.size(), m_comm);
|
|
|
|
} else if (m_op == Operation::PACK) {
|
|
|
|
(*this)(data.size());
|
|
|
|
Mpi::Packer::pack(data.data(), data.size(), m_buffer, m_position, m_comm);
|
|
|
|
} else if (m_op == Operation::UNPACK) {
|
|
|
|
std::size_t size = 0;
|
|
|
|
(*this)(size);
|
2022-09-08 03:21:55 -05:00
|
|
|
auto& data_mut = const_cast<std::vector<T>&>(data);
|
|
|
|
data_mut.resize(size);
|
|
|
|
Mpi::Packer::unpack(data_mut.data(), size, m_buffer, m_position, m_comm);
|
2022-09-08 02:16:07 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (m_op == Operation::UNPACK) {
|
|
|
|
std::size_t size = 0;
|
|
|
|
(*this)(size);
|
2022-09-08 03:21:55 -05:00
|
|
|
auto& data_mut = const_cast<std::vector<T>&>(data);
|
|
|
|
data_mut.resize(size);
|
|
|
|
std::for_each(data_mut.begin(), data_mut.end(), std::ref(*this));
|
2022-09-08 02:16:07 -05:00
|
|
|
} else {
|
|
|
|
(*this)(data.size());
|
|
|
|
std::for_each(data.begin(), data.end(), std::ref(*this));
|
2020-03-10 08:28:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-07 06:42:22 -05:00
|
|
|
//! \brief Handler for bool vectors.
|
|
|
|
//! \param data The vector to (de-)serialize
|
2022-09-08 03:21:55 -05:00
|
|
|
void vector(const std::vector<bool>& data)
|
2022-09-07 06:42:22 -05:00
|
|
|
{
|
2022-09-08 02:16:07 -05:00
|
|
|
if (m_op == Operation::UNPACK) {
|
|
|
|
std::size_t size = 0;
|
2022-09-07 06:42:22 -05:00
|
|
|
(*this)(size);
|
2022-09-08 03:21:55 -05:00
|
|
|
auto& data_mut = const_cast<std::vector<bool>&>(data);
|
|
|
|
data_mut.clear();
|
|
|
|
data_mut.reserve(size);
|
2022-09-07 06:42:22 -05:00
|
|
|
for (size_t i = 0; i < size; ++i) {
|
2022-09-08 02:16:07 -05:00
|
|
|
bool entry = false;
|
2022-09-07 06:42:22 -05:00
|
|
|
(*this)(entry);
|
2022-09-08 03:21:55 -05:00
|
|
|
data_mut.push_back(entry);
|
2022-09-07 06:42:22 -05:00
|
|
|
}
|
2022-09-08 02:16:07 -05:00
|
|
|
} else {
|
|
|
|
(*this)(data.size());
|
|
|
|
for (const auto entry : data) { // Not a reference: vector<bool> range
|
|
|
|
bool b = entry;
|
|
|
|
(*this)(b);
|
|
|
|
}
|
2022-09-07 06:42:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-07 05:03:10 -05:00
|
|
|
//! \brief Handler for arrays.
|
|
|
|
//! \param data The array to (de-)serialize
|
2022-09-02 06:02:35 -05:00
|
|
|
template <class Array>
|
2022-09-08 03:21:55 -05:00
|
|
|
void array(const Array& data)
|
2021-10-22 01:23:59 -05:00
|
|
|
{
|
|
|
|
using T = typename Array::value_type;
|
|
|
|
|
2022-09-08 02:16:07 -05:00
|
|
|
if constexpr (std::is_pod_v<T>) {
|
|
|
|
if (m_op == Operation::PACKSIZE)
|
2022-09-13 08:32:37 -05:00
|
|
|
m_packSize += Mpi::Packer::packSize(data.data(), data.size(), m_comm);
|
2022-09-08 02:16:07 -05:00
|
|
|
else if (m_op == Operation::PACK)
|
2022-09-13 08:32:37 -05:00
|
|
|
Mpi::Packer::pack(data.data(), data.size(), m_buffer, m_position, m_comm);
|
2022-09-08 03:21:55 -05:00
|
|
|
else if (m_op == Operation::UNPACK) {
|
|
|
|
auto& data_mut = const_cast<Array&>(data);
|
|
|
|
Mpi::Packer::unpack(data_mut.data(), data_mut.size(), m_buffer, m_position, m_comm);
|
|
|
|
}
|
2022-09-08 02:16:07 -05:00
|
|
|
} else {
|
|
|
|
std::for_each(data.begin(), data.end(), std::ref(*this));
|
2021-10-22 01:23:59 -05:00
|
|
|
}
|
|
|
|
}
|
2020-06-09 04:25:47 -05:00
|
|
|
|
2022-09-01 05:23:24 -05:00
|
|
|
//! \brief Handler for std::variant.
|
|
|
|
//! \param data The variant to (de-)serialize
|
|
|
|
template<class... Args>
|
|
|
|
void variant(const std::variant<Args...>& data)
|
2020-06-09 04:25:47 -05:00
|
|
|
{
|
2022-09-08 02:16:07 -05:00
|
|
|
if (m_op == Operation::UNPACK) {
|
|
|
|
std::size_t index = 0;
|
|
|
|
(*this)(index);
|
2022-09-01 05:23:24 -05:00
|
|
|
auto& data_mut = const_cast<std::variant<Args...>&>(data);
|
|
|
|
data_mut = detail::make_variant<Args...>(index);
|
2022-09-08 02:16:07 -05:00
|
|
|
std::visit(std::ref(*this), data_mut);
|
|
|
|
} else {
|
|
|
|
(*this)(data.index());
|
|
|
|
std::visit(std::ref(*this), data);
|
2020-06-09 04:25:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-23 05:23:18 -05:00
|
|
|
//! \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)
|
|
|
|
{
|
2022-09-08 02:16:07 -05:00
|
|
|
if (m_op == Operation::UNPACK) {
|
|
|
|
bool has = false;
|
|
|
|
(*this)(has);
|
2020-09-23 05:23:18 -05:00
|
|
|
if (has) {
|
|
|
|
T res;
|
2022-09-07 05:03:10 -05:00
|
|
|
(*this)(res);
|
2020-09-23 05:23:18 -05:00
|
|
|
const_cast<std::optional<T>&>(data) = res;
|
|
|
|
}
|
2022-09-08 02:16:07 -05:00
|
|
|
} else {
|
|
|
|
(*this)(data.has_value());
|
|
|
|
if (data.has_value()) {
|
|
|
|
(*this)(*data);
|
|
|
|
}
|
2020-09-23 05:23:18 -05:00
|
|
|
}
|
2022-09-07 05:03:10 -05:00
|
|
|
}
|
2020-09-23 05:23:18 -05:00
|
|
|
|
2022-09-07 05:03:10 -05:00
|
|
|
//! \brief Handler for std::tuple.
|
|
|
|
//! \param data The tuple to (de-)serialize
|
2022-09-13 09:11:44 -05:00
|
|
|
template<class Tuple>
|
|
|
|
void tuple(const Tuple& data)
|
2022-09-07 05:03:10 -05:00
|
|
|
{
|
2022-09-13 09:11:44 -05:00
|
|
|
tuple_call(data);
|
2020-09-23 05:23:18 -05:00
|
|
|
}
|
|
|
|
|
2020-03-19 03:09:58 -05:00
|
|
|
//! \brief Handler for maps.
|
|
|
|
//! \tparam Map map type
|
|
|
|
//! \param map The map to (de-)serialize
|
2022-09-02 06:02:35 -05:00
|
|
|
template<class Map>
|
2022-09-08 03:21:55 -05:00
|
|
|
void map(const Map& data)
|
2020-03-16 08:39:10 -05:00
|
|
|
{
|
2022-09-08 02:16:07 -05:00
|
|
|
if (m_op == Operation::UNPACK) {
|
|
|
|
std::size_t size = 0;
|
|
|
|
(*this)(size);
|
2022-09-08 03:21:55 -05:00
|
|
|
auto& data_mut = const_cast<Map&>(data);
|
2020-03-16 08:39:10 -05:00
|
|
|
for (size_t i = 0; i < size; ++i) {
|
2022-09-08 02:16:07 -05:00
|
|
|
typename Map::value_type entry;
|
|
|
|
(*this)(entry);
|
2022-09-08 03:21:55 -05:00
|
|
|
data_mut.insert(entry);
|
2020-03-16 08:39:10 -05:00
|
|
|
}
|
2022-09-08 02:16:07 -05:00
|
|
|
} else {
|
|
|
|
(*this)(data.size());
|
|
|
|
std::for_each(data.begin(), data.end(), std::ref(*this));
|
2020-03-16 08:39:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-07 05:03:10 -05:00
|
|
|
//! \brief Handler for sets.
|
|
|
|
//! \tparam Set set type
|
|
|
|
//! \param data The set to (de-)serialize
|
2022-09-02 06:02:35 -05:00
|
|
|
template<class Set>
|
2022-09-08 03:21:55 -05:00
|
|
|
void set(const Set& data)
|
2021-09-24 07:48:38 -05:00
|
|
|
{
|
2022-09-08 02:16:07 -05:00
|
|
|
if (m_op == Operation::UNPACK) {
|
|
|
|
std::size_t size = 0;
|
|
|
|
(*this)(size);
|
2022-09-08 03:21:55 -05:00
|
|
|
auto& data_mut = const_cast<Set&>(data);
|
2021-09-24 07:48:38 -05:00
|
|
|
for (size_t i = 0; i < size; ++i) {
|
2022-09-08 02:16:07 -05:00
|
|
|
typename Set::value_type entry;
|
|
|
|
(*this)(entry);
|
2022-09-08 03:21:55 -05:00
|
|
|
data_mut.insert(entry);
|
2021-09-24 07:48:38 -05:00
|
|
|
}
|
2022-09-08 02:16:07 -05:00
|
|
|
} else {
|
|
|
|
(*this)(data.size());
|
|
|
|
std::for_each(data.begin(), data.end(), std::ref(*this));
|
2021-09-24 07:48:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-07 06:43:23 -05:00
|
|
|
template<typename T, typename... Args>
|
|
|
|
void variadic_call(T& first,
|
|
|
|
Args&&... args)
|
|
|
|
{
|
|
|
|
(*this)(first);
|
|
|
|
if constexpr (sizeof...(args) > 0)
|
|
|
|
variadic_call(std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2022-09-07 05:03:10 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-03-19 03:09:58 -05:00
|
|
|
//! \brief Enumeration of operations.
|
|
|
|
enum class Operation {
|
|
|
|
PACKSIZE, //!< Calculating serialization buffer size
|
|
|
|
PACK, //!< Performing serialization
|
|
|
|
UNPACK //!< Performing de-serialization
|
|
|
|
};
|
|
|
|
|
|
|
|
//! \brief Predicate for detecting vectors.
|
2020-03-16 08:39:10 -05:00
|
|
|
template<class T>
|
|
|
|
struct is_vector {
|
|
|
|
constexpr static bool value = false;
|
|
|
|
};
|
|
|
|
|
2022-09-07 04:22:24 -05:00
|
|
|
template<class T1, class Allocator>
|
|
|
|
struct is_vector<std::vector<T1,Allocator>> {
|
2020-03-16 08:39:10 -05:00
|
|
|
constexpr static bool value = true;
|
|
|
|
};
|
|
|
|
|
2020-06-09 04:25:47 -05:00
|
|
|
//! \brief Predicate for detecting variants.
|
|
|
|
template<class T>
|
|
|
|
struct is_variant {
|
|
|
|
constexpr static bool value = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class... Ts>
|
|
|
|
struct is_variant<std::variant<Ts...>> {
|
|
|
|
constexpr static bool value = true;
|
|
|
|
};
|
|
|
|
|
2022-09-13 09:11:44 -05:00
|
|
|
//! \brief Predicate for detecting pairs and tuples.
|
2022-09-07 05:03:10 -05:00
|
|
|
template<class T>
|
2022-09-13 09:11:44 -05:00
|
|
|
struct is_pair_or_tuple {
|
2022-09-07 05:03:10 -05:00
|
|
|
constexpr static bool value = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class... Ts>
|
2022-09-13 09:11:44 -05:00
|
|
|
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>> {
|
2022-09-07 05:03:10 -05:00
|
|
|
constexpr static bool value = true;
|
|
|
|
};
|
|
|
|
|
2020-03-19 03:09:58 -05:00
|
|
|
//! \brief Predicate for smart pointers.
|
2020-03-16 08:35:50 -05:00
|
|
|
template<class T>
|
2020-03-18 03:17:09 -05:00
|
|
|
struct is_ptr {
|
2020-03-16 08:35:50 -05:00
|
|
|
constexpr static bool value = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T1>
|
2020-03-18 03:17:09 -05:00
|
|
|
struct is_ptr<std::shared_ptr<T1>> {
|
|
|
|
constexpr static bool value = true;
|
|
|
|
};
|
|
|
|
|
2020-03-23 07:36:07 -05:00
|
|
|
template<class T1, class Deleter>
|
|
|
|
struct is_ptr<std::unique_ptr<T1, Deleter>> {
|
2020-03-16 08:35:50 -05:00
|
|
|
constexpr static bool value = true;
|
|
|
|
};
|
2020-03-18 09:43:27 -05:00
|
|
|
|
2020-09-23 05:23:18 -05:00
|
|
|
//! \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;
|
|
|
|
};
|
|
|
|
|
2022-09-06 02:35:12 -05:00
|
|
|
//! \brief Predicate for maps
|
|
|
|
template<class T>
|
|
|
|
struct is_map {
|
|
|
|
constexpr static bool value = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class Key, class T, class Compare, class Allocator>
|
|
|
|
struct is_map<std::map<Key,T,Compare,Allocator>> {
|
|
|
|
constexpr static bool value = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class Key, class T, class Hash, class KeyEqual, class Allocator>
|
|
|
|
struct is_map<std::unordered_map<Key,T,Hash,KeyEqual,Allocator>> {
|
|
|
|
constexpr static bool value = true;
|
|
|
|
};
|
|
|
|
|
2022-09-07 05:03:10 -05:00
|
|
|
//! \brief Predicate for sets
|
|
|
|
template<class T>
|
|
|
|
struct is_set {
|
|
|
|
constexpr static bool value = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class Key, class Compare, class Allocator>
|
|
|
|
struct is_set<std::set<Key,Compare,Allocator>> {
|
|
|
|
constexpr static bool value = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class Key, class Hash, class KeyEqual, class Allocator>
|
|
|
|
struct is_set<std::unordered_set<Key,Hash,KeyEqual,Allocator>> {
|
|
|
|
constexpr static bool value = true;
|
|
|
|
};
|
|
|
|
|
2022-09-07 05:03:10 -05:00
|
|
|
//! \brief Predicate for arrays
|
|
|
|
template<class T>
|
|
|
|
struct is_array {
|
|
|
|
constexpr static bool value = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T, std::size_t N>
|
|
|
|
struct is_array<std::array<T,N>> {
|
|
|
|
constexpr static bool value = true;
|
|
|
|
};
|
|
|
|
|
2021-09-29 04:45:45 -05:00
|
|
|
//! Detect existence of \c serializeOp member function
|
|
|
|
//!
|
|
|
|
//! Base case (no \c serializeOp member function)
|
|
|
|
template <typename, class = void>
|
|
|
|
struct has_serializeOp : public std::false_type {};
|
|
|
|
|
|
|
|
//! Detect existence of \c serializeOp member function
|
|
|
|
//!
|
|
|
|
//! Non-default, albeit common, case (type has \c serializeOp member
|
|
|
|
//! function)
|
|
|
|
template <typename T>
|
|
|
|
struct has_serializeOp<
|
|
|
|
T, std::void_t<decltype(std::declval<T>().serializeOp(std::declval<EclMpiSerializer&>()))>
|
|
|
|
> : public std::true_type {};
|
|
|
|
|
2020-03-19 03:09:58 -05:00
|
|
|
//! \brief Handler for smart pointers.
|
2020-03-23 05:55:45 -05:00
|
|
|
template<class PtrType>
|
|
|
|
void ptr(const PtrType& data)
|
2020-03-16 08:35:50 -05:00
|
|
|
{
|
2020-03-23 05:55:45 -05:00
|
|
|
using T1 = typename PtrType::element_type;
|
2020-03-16 08:35:50 -05:00
|
|
|
bool value = data ? true : false;
|
|
|
|
(*this)(value);
|
|
|
|
if (m_op == Operation::UNPACK && value) {
|
2020-03-23 05:55:45 -05:00
|
|
|
const_cast<PtrType&>(data).reset(new T1);
|
2020-03-16 08:35:50 -05:00
|
|
|
}
|
2022-09-02 06:02:35 -05:00
|
|
|
if (data) {
|
2022-09-08 02:16:07 -05:00
|
|
|
(*this)(*data);
|
2022-09-02 06:02:35 -05:00
|
|
|
}
|
2020-03-16 08:35:50 -05:00
|
|
|
}
|
|
|
|
|
2022-08-29 07:03:52 -05:00
|
|
|
Parallel::Communication m_comm; //!< Communicator to broadcast using
|
2020-03-09 08:20:51 -05:00
|
|
|
|
2020-03-19 03:09:58 -05:00
|
|
|
Operation m_op = Operation::PACKSIZE; //!< Current operation
|
|
|
|
size_t m_packSize = 0; //!< Required buffer size after PACKSIZE has been done
|
|
|
|
int m_position = 0; //!< Current position in buffer
|
|
|
|
std::vector<char> m_buffer; //!< Buffer for serialized data
|
2020-01-06 03:25:59 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|