mirror of
https://github.com/OPM/opm-simulators.git
synced 2025-01-26 09:36:25 -06:00
91e9d952d8
this shall be used by users when they have a vector of types with a serializeOp template. it cannot (at least not right now) be part of the regular operator() as some vectors should go directly to the underlying serializer (trivial types). we can possibly find some traits magic for this later.
133 lines
3.5 KiB
C++
133 lines
3.5 KiB
C++
/*
|
|
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
|
|
|
|
#include <opm/simulators/utils/ParallelRestart.hpp>
|
|
|
|
namespace Opm {
|
|
|
|
class EclMpiSerializer {
|
|
public:
|
|
enum class Operation {
|
|
PACKSIZE,
|
|
PACK,
|
|
UNPACK
|
|
};
|
|
|
|
explicit EclMpiSerializer(Dune::CollectiveCommunication<Dune::MPIHelper::MPICommunicator> comm) :
|
|
m_comm(comm)
|
|
{}
|
|
|
|
template<class T>
|
|
void operator()(const T& data)
|
|
{
|
|
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);
|
|
}
|
|
|
|
template<class T>
|
|
void vector(std::vector<T>& data)
|
|
{
|
|
static_assert(!std::is_pod<T>::value, "Do not call this for POD vectors");
|
|
auto handle = [&](auto& d)
|
|
{
|
|
for (auto& it : d) {
|
|
it.serializeOp(*this);
|
|
}
|
|
};
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
template<class T>
|
|
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);
|
|
}
|
|
|
|
template<class T>
|
|
void unpack(T& data)
|
|
{
|
|
m_position = 0;
|
|
m_op = Operation::UNPACK;
|
|
data.serializeOp(*this);
|
|
}
|
|
|
|
template<class T>
|
|
void broadcast(T& data)
|
|
{
|
|
if (m_comm.size() == 1)
|
|
return;
|
|
|
|
#if HAVE_MPI
|
|
if (m_comm.rank() == 0) {
|
|
pack(data);
|
|
m_comm.broadcast(&m_position, 1, 0);
|
|
m_comm.broadcast(m_buffer.data(), m_position, 0);
|
|
} else {
|
|
m_comm.broadcast(&m_packSize, 1, 0);
|
|
m_buffer.resize(m_packSize);
|
|
m_comm.broadcast(m_buffer.data(), m_packSize, 0);
|
|
unpack(data);
|
|
}
|
|
#endif
|
|
}
|
|
|
|
size_t position() const
|
|
{
|
|
return m_position;
|
|
}
|
|
|
|
protected:
|
|
Dune::CollectiveCommunication<Dune::MPIHelper::MPICommunicator> m_comm;
|
|
|
|
Operation m_op = Operation::PACKSIZE;
|
|
size_t m_packSize = 0;
|
|
int m_position = 0;
|
|
std::vector<char> m_buffer;
|
|
};
|
|
|
|
}
|
|
|
|
#endif
|