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
|
|
|
|
2020-09-23 05:23:18 -05:00
|
|
|
#include <optional>
|
2021-09-29 04:45:45 -05:00
|
|
|
#include <type_traits>
|
|
|
|
#include <utility>
|
2020-06-09 04:25:47 -05:00
|
|
|
#include <variant>
|
2020-01-06 03:25:59 -06:00
|
|
|
|
2022-09-01 05:23:24 -05:00
|
|
|
namespace detail
|
|
|
|
{
|
|
|
|
|
|
|
|
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>
|
|
|
|
using remove_cvr_t = std::remove_const_t<std::remove_reference_t<T>>;
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
2020-01-06 03:25:59 -06:00
|
|
|
namespace Opm {
|
|
|
|
|
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)
|
|
|
|
{}
|
|
|
|
|
2020-03-19 03:09:58 -05:00
|
|
|
//! \brief (De-)serialization for simple types.
|
|
|
|
//! \details The data handled by this depends on the underlying serialization used.
|
|
|
|
//! Currently you can call this for scalars, and stl containers with scalars.
|
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);
|
2020-03-18 09:43:27 -05:00
|
|
|
} else if constexpr (is_pair<T>::value) {
|
|
|
|
pair(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);
|
|
|
|
} else if constexpr (is_map<T>::value) {
|
|
|
|
map(const_cast<T&>(data));
|
2022-09-07 05:03:10 -05:00
|
|
|
} else if constexpr (is_array<T>::value) {
|
|
|
|
array(const_cast<T&>(data));
|
2020-03-16 08:35:50 -05:00
|
|
|
} else {
|
|
|
|
if (m_op == Operation::PACKSIZE)
|
|
|
|
m_packSize += Mpi::packSize(data, m_comm);
|
|
|
|
else if (m_op == Operation::PACK)
|
|
|
|
Mpi::pack(data, m_buffer, m_position, m_comm);
|
|
|
|
else if (m_op == Operation::UNPACK)
|
|
|
|
Mpi::unpack(const_cast<T&>(data), m_buffer, m_position, m_comm);
|
|
|
|
}
|
2020-01-06 03:25:59 -06:00
|
|
|
}
|
|
|
|
|
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>
|
2020-03-10 08:28:00 -05:00
|
|
|
void vector(std::vector<T>& data)
|
|
|
|
{
|
|
|
|
auto handle = [&](auto& d)
|
|
|
|
{
|
|
|
|
for (auto& it : d) {
|
2020-03-12 09:06:45 -05:00
|
|
|
if constexpr (is_pair<T>::value)
|
|
|
|
pair(it);
|
2020-03-18 03:17:09 -05:00
|
|
|
else if constexpr (is_ptr<T>::value)
|
|
|
|
ptr(it);
|
2022-09-02 06:02:35 -05:00
|
|
|
else if constexpr (has_serializeOp<T>::value)
|
2020-03-12 09:06:45 -05:00
|
|
|
it.serializeOp(*this);
|
2022-09-02 06:02:35 -05:00
|
|
|
else
|
|
|
|
(*this)(it);
|
2020-03-10 08:28:00 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (m_op == Operation::PACKSIZE) {
|
|
|
|
m_packSize += Mpi::packSize(data.size(), m_comm);
|
|
|
|
handle(data);
|
|
|
|
} else if (m_op == Operation::PACK) {
|
|
|
|
Mpi::pack(data.size(), m_buffer, m_position, m_comm);
|
|
|
|
handle(data);
|
|
|
|
} else if (m_op == Operation::UNPACK) {
|
|
|
|
size_t size;
|
|
|
|
Mpi::unpack(size, m_buffer, m_position, m_comm);
|
|
|
|
data.resize(size);
|
|
|
|
handle(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-02 06:02:35 -05:00
|
|
|
template <class Array>
|
2021-10-22 01:23:59 -05:00
|
|
|
void array(Array& data)
|
|
|
|
{
|
|
|
|
using T = typename Array::value_type;
|
|
|
|
|
|
|
|
auto handle = [&](auto& d) {
|
|
|
|
for (auto& it : d) {
|
|
|
|
if constexpr (is_pair<T>::value)
|
|
|
|
pair(it);
|
|
|
|
else if constexpr (is_ptr<T>::value)
|
|
|
|
ptr(it);
|
2022-09-02 06:02:35 -05:00
|
|
|
else if constexpr (has_serializeOp<T>::value)
|
2021-10-22 01:23:59 -05:00
|
|
|
it.serializeOp(*this);
|
2022-09-02 06:02:35 -05:00
|
|
|
else
|
|
|
|
(*this)(it);
|
2021-10-22 01:23:59 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (m_op == Operation::PACKSIZE) {
|
|
|
|
m_packSize += Mpi::packSize(data.size(), m_comm);
|
|
|
|
handle(data);
|
|
|
|
} else if (m_op == Operation::PACK) {
|
|
|
|
Mpi::pack(data.size(), m_buffer, m_position, m_comm);
|
|
|
|
handle(data);
|
|
|
|
} else if (m_op == Operation::UNPACK) {
|
|
|
|
size_t size;
|
|
|
|
Mpi::unpack(size, m_buffer, m_position, m_comm);
|
|
|
|
handle(data);
|
|
|
|
}
|
|
|
|
}
|
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-01 05:23:24 -05:00
|
|
|
auto visitor = [&](auto& d)
|
|
|
|
{
|
|
|
|
if constexpr (has_serializeOp<detail::remove_cvr_t<decltype(d)>>::value)
|
|
|
|
const_cast<detail::remove_cvr_t<decltype(d)>&>(d).serializeOp(*this);
|
|
|
|
else
|
|
|
|
(*this)(d);
|
2020-06-09 04:25:47 -05:00
|
|
|
};
|
|
|
|
if (m_op == Operation::PACKSIZE) {
|
|
|
|
m_packSize += Mpi::packSize(data.index(), m_comm);
|
2022-09-01 05:23:24 -05:00
|
|
|
std::visit(visitor, data);
|
2020-06-09 04:25:47 -05:00
|
|
|
} else if (m_op == Operation::PACK) {
|
|
|
|
Mpi::pack(data.index(), m_buffer, m_position, m_comm);
|
2022-09-01 05:23:24 -05:00
|
|
|
std::visit(visitor, data);
|
2020-06-09 04:25:47 -05:00
|
|
|
} else if (m_op == Operation::UNPACK) {
|
|
|
|
size_t index;
|
|
|
|
Mpi::unpack(index, m_buffer, m_position, m_comm);
|
2022-09-01 05:23:24 -05:00
|
|
|
auto& data_mut = const_cast<std::variant<Args...>&>(data);
|
|
|
|
data_mut = detail::make_variant<Args...>(index);
|
|
|
|
std::visit(visitor, data_mut);
|
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)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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>
|
2020-03-17 07:56:12 -05:00
|
|
|
void map(Map& data)
|
2020-03-16 08:39:10 -05:00
|
|
|
{
|
2020-03-17 07:56:12 -05:00
|
|
|
using Key = typename Map::key_type;
|
|
|
|
using Data = typename Map::mapped_type;
|
|
|
|
|
2020-03-16 08:39:10 -05:00
|
|
|
auto handle = [&](auto& d)
|
|
|
|
{
|
|
|
|
if constexpr (is_vector<Data>::value)
|
2022-09-07 05:03:10 -05:00
|
|
|
vector(d);
|
2020-03-18 03:17:09 -05:00
|
|
|
else if constexpr (is_ptr<Data>::value)
|
2022-09-07 05:03:10 -05:00
|
|
|
ptr(d);
|
2022-09-06 02:35:12 -05:00
|
|
|
else if constexpr (is_map<Data>::value)
|
2022-09-07 05:03:10 -05:00
|
|
|
map(d);
|
2022-09-02 06:02:35 -05:00
|
|
|
else if constexpr (has_serializeOp<Data>::value)
|
2020-03-16 08:39:10 -05:00
|
|
|
d.serializeOp(*this);
|
2020-03-18 09:43:27 -05:00
|
|
|
else
|
|
|
|
(*this)(d);
|
2020-03-16 08:39:10 -05:00
|
|
|
};
|
|
|
|
|
2021-03-10 07:26:04 -06:00
|
|
|
auto keyHandle = [&](auto& d)
|
|
|
|
{
|
|
|
|
if constexpr (is_pair<Key>::value)
|
|
|
|
pair(d);
|
2022-09-02 06:02:35 -05:00
|
|
|
else if constexpr (has_serializeOp<Key>::value)
|
|
|
|
d.serializeOp(*this);
|
2021-03-10 07:26:04 -06:00
|
|
|
else
|
|
|
|
(*this)(d);
|
|
|
|
};
|
|
|
|
|
2020-03-16 08:39:10 -05:00
|
|
|
if (m_op == Operation::PACKSIZE) {
|
|
|
|
m_packSize += Mpi::packSize(data.size(), m_comm);
|
|
|
|
for (auto& it : data) {
|
2021-03-10 07:26:04 -06:00
|
|
|
keyHandle(it.first);
|
2020-03-16 08:39:10 -05:00
|
|
|
handle(it.second);
|
|
|
|
}
|
|
|
|
} else if (m_op == Operation::PACK) {
|
|
|
|
Mpi::pack(data.size(), m_buffer, m_position, m_comm);
|
|
|
|
for (auto& it : data) {
|
2021-03-10 07:26:04 -06:00
|
|
|
keyHandle(it.first);
|
2020-03-16 08:39:10 -05:00
|
|
|
handle(it.second);
|
|
|
|
}
|
|
|
|
} else if (m_op == Operation::UNPACK) {
|
|
|
|
size_t size;
|
|
|
|
Mpi::unpack(size, m_buffer, m_position, m_comm);
|
|
|
|
for (size_t i = 0; i < size; ++i) {
|
|
|
|
Key key;
|
2021-03-10 07:26:04 -06:00
|
|
|
keyHandle(key);
|
2020-03-16 08:39:10 -05:00
|
|
|
Data entry;
|
|
|
|
handle(entry);
|
|
|
|
data.insert(std::make_pair(key, entry));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-02 06:02:35 -05:00
|
|
|
template<class Set>
|
2021-09-24 07:48:38 -05:00
|
|
|
void set(Set& data)
|
|
|
|
{
|
|
|
|
using Data = typename Set::value_type;
|
|
|
|
|
|
|
|
auto handle = [&](auto& d)
|
|
|
|
{
|
|
|
|
if constexpr (is_vector<Data>::value)
|
2022-09-02 06:02:35 -05:00
|
|
|
this->vector(d);
|
2021-09-24 07:48:38 -05:00
|
|
|
else if constexpr (is_ptr<Data>::value)
|
|
|
|
ptr(d);
|
2022-09-02 06:02:35 -05:00
|
|
|
else if constexpr (has_serializeOp<Data>::value)
|
2021-09-24 07:48:38 -05:00
|
|
|
d.serializeOp(*this);
|
|
|
|
else
|
|
|
|
(*this)(d);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (m_op == Operation::PACKSIZE) {
|
|
|
|
m_packSize += Mpi::packSize(data.size(), m_comm);
|
|
|
|
for (auto& it : data) {
|
|
|
|
handle(it);
|
|
|
|
}
|
|
|
|
} else if (m_op == Operation::PACK) {
|
|
|
|
Mpi::pack(data.size(), m_buffer, m_position, m_comm);
|
|
|
|
for (auto& it : data) {
|
|
|
|
handle(it);
|
|
|
|
}
|
|
|
|
} else if (m_op == Operation::UNPACK) {
|
|
|
|
size_t size;
|
|
|
|
Mpi::unpack(size, m_buffer, m_position, m_comm);
|
|
|
|
for (size_t i = 0; i < size; ++i) {
|
|
|
|
Data entry;
|
|
|
|
handle(entry);
|
|
|
|
data.insert(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-19 03:09:58 -05:00
|
|
|
//! \brief Call this to serialize data.
|
|
|
|
//! \tparam T Type of class to serialize
|
|
|
|
//! \param data Class to serialize
|
2020-01-06 03:25:59 -06:00
|
|
|
template<class T>
|
2020-03-09 08:20:51 -05:00
|
|
|
void pack(T& data)
|
|
|
|
{
|
|
|
|
m_op = Operation::PACKSIZE;
|
|
|
|
m_packSize = 0;
|
|
|
|
data.serializeOp(*this);
|
|
|
|
m_position = 0;
|
|
|
|
m_buffer.resize(m_packSize);
|
|
|
|
m_op = Operation::PACK;
|
|
|
|
data.serializeOp(*this);
|
2020-01-06 03:25:59 -06:00
|
|
|
}
|
|
|
|
|
2020-03-19 03:09:58 -05:00
|
|
|
//! \brief Call this to de-serialize data.
|
|
|
|
//! \tparam T Type of class to de-serialize
|
|
|
|
//! \param data Class to de-serialize
|
2020-01-06 03:25:59 -06:00
|
|
|
template<class T>
|
2020-03-09 08:20:51 -05:00
|
|
|
void unpack(T& data)
|
|
|
|
{
|
|
|
|
m_position = 0;
|
|
|
|
m_op = Operation::UNPACK;
|
|
|
|
data.serializeOp(*this);
|
2020-01-06 03:25:59 -06:00
|
|
|
}
|
|
|
|
|
2021-09-29 04:45:45 -05:00
|
|
|
//! \brief Serialize and broadcast on root process, de-serialize on
|
|
|
|
//! others.
|
|
|
|
//!
|
2020-03-19 03:09:58 -05:00
|
|
|
//! \tparam T Type of class to broadcast
|
|
|
|
//! \param data Class to broadcast
|
2020-01-22 04:22:06 -06:00
|
|
|
template<class T>
|
|
|
|
void broadcast(T& data)
|
|
|
|
{
|
|
|
|
if (m_comm.size() == 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (m_comm.rank() == 0) {
|
2020-09-24 04:00:37 -05:00
|
|
|
try {
|
|
|
|
pack(data);
|
|
|
|
m_packSize = m_position;
|
|
|
|
m_comm.broadcast(&m_packSize, 1, 0);
|
|
|
|
m_comm.broadcast(m_buffer.data(), m_position, 0);
|
|
|
|
} catch (...) {
|
|
|
|
m_packSize = std::numeric_limits<size_t>::max();
|
|
|
|
m_comm.broadcast(&m_packSize, 1, 0);
|
|
|
|
throw;
|
|
|
|
}
|
2020-01-22 04:22:06 -06:00
|
|
|
} else {
|
2020-03-09 08:20:51 -05:00
|
|
|
m_comm.broadcast(&m_packSize, 1, 0);
|
2020-09-24 04:00:37 -05:00
|
|
|
if (m_packSize == std::numeric_limits<size_t>::max()) {
|
|
|
|
throw std::runtime_error("Error detected in parallel serialization");
|
|
|
|
}
|
2020-03-09 08:20:51 -05:00
|
|
|
m_buffer.resize(m_packSize);
|
|
|
|
m_comm.broadcast(m_buffer.data(), m_packSize, 0);
|
|
|
|
unpack(data);
|
2020-01-22 04:22:06 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 08:36:30 -05:00
|
|
|
//! \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
|
|
|
|
template<class T>
|
|
|
|
void append(T& data)
|
|
|
|
{
|
|
|
|
if (m_comm.size() == 1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
T tmp;
|
|
|
|
T& bcast = m_comm.rank() == 0 ? data : tmp;
|
|
|
|
broadcast(bcast);
|
|
|
|
|
|
|
|
if (m_comm.rank() != 0)
|
|
|
|
data.append(tmp);
|
|
|
|
}
|
|
|
|
|
2020-03-19 03:09:58 -05:00
|
|
|
//! \brief Returns current position in buffer.
|
2020-03-09 08:20:51 -05:00
|
|
|
size_t position() const
|
|
|
|
{
|
|
|
|
return m_position;
|
|
|
|
}
|
|
|
|
|
2020-03-19 03:09:58 -05:00
|
|
|
//! \brief Returns true if we are currently doing a serialization operation.
|
2020-03-17 02:12:15 -05:00
|
|
|
bool isSerializing() const
|
|
|
|
{
|
|
|
|
return m_op != Operation::UNPACK;
|
|
|
|
}
|
|
|
|
|
2020-01-06 03:25:59 -06:00
|
|
|
protected:
|
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 pairs.
|
2020-03-12 09:06:45 -05:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2020-03-19 03:09:58 -05:00
|
|
|
//! \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;
|
|
|
|
};
|
|
|
|
|
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 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 pairs.
|
2020-03-12 09:06:45 -05:00
|
|
|
template<class T1, class T2>
|
|
|
|
void pair(const std::pair<T1,T2>& data)
|
|
|
|
{
|
2022-09-02 05:57:30 -05:00
|
|
|
if constexpr (has_serializeOp<T1>::value)
|
|
|
|
const_cast<T1&>(data.first).serializeOp(*this);
|
2020-03-12 09:06:45 -05:00
|
|
|
else
|
2022-09-02 05:57:30 -05:00
|
|
|
(*this)(data.first);
|
2020-03-12 09:06:45 -05:00
|
|
|
|
2022-09-02 05:57:30 -05:00
|
|
|
if constexpr (has_serializeOp<T2>::value)
|
2020-03-12 09:06:45 -05:00
|
|
|
const_cast<T2&>(data.second).serializeOp(*this);
|
2022-09-02 05:57:30 -05:00
|
|
|
else
|
|
|
|
(*this)(data.second);
|
2020-03-12 09:06:45 -05:00
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
if constexpr (has_serializeOp<T1>::value)
|
|
|
|
data->serializeOp(*this);
|
|
|
|
else
|
|
|
|
(*this)(*data);
|
|
|
|
}
|
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
|