Merge pull request #2447 from akva2/simplify_serializer

changed: simplify eclmpiserializer
This commit is contained in:
Markus Blatt 2020-03-10 11:21:50 +01:00 committed by GitHub
commit fa158893db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 81 deletions

View File

@ -27,23 +27,45 @@ 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>
std::size_t packSize(const T& data) {
return Mpi::packSize(data, m_comm);
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 pack(const T& data, std::vector<char>& buffer, int& pos) {
Mpi::pack(data, buffer, pos, m_comm);
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, std::vector<char>& buffer, int& pos) {
Mpi::unpack(data, buffer, pos, m_comm);
void unpack(T& data)
{
m_position = 0;
m_op = Operation::UNPACK;
data.serializeOp(*this);
}
template<class T>
@ -54,25 +76,30 @@ public:
#if HAVE_MPI
if (m_comm.rank() == 0) {
size_t size = data.packSize(*this);
std::vector<char> buffer(size);
int position = 0;
data.pack(buffer, position, *this);
m_comm.broadcast(&position, 1, 0);
m_comm.broadcast(buffer.data(), position, 0);
pack(data);
m_comm.broadcast(&m_position, 1, 0);
m_comm.broadcast(m_buffer.data(), m_position, 0);
} else {
int size;
m_comm.broadcast(&size, 1, 0);
std::vector<char> buffer(size);
m_comm.broadcast(buffer.data(), size, 0);
int position = 0;
data.unpack(buffer, position, *this);
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;
};
}

View File

@ -137,57 +137,20 @@ ParallelEclipseState::ParallelEclipseState(const Deck& deck)
#if HAVE_MPI
std::size_t ParallelEclipseState::packSize(EclMpiSerializer& serializer) const
void ParallelEclipseState::serializeOp(EclMpiSerializer& serializer)
{
return serializer.packSize(m_tables) +
serializer.packSize(m_runspec) +
serializer.packSize(m_eclipseConfig) +
serializer.packSize(m_deckUnitSystem) +
serializer.packSize(m_inputNnc) +
serializer.packSize(m_inputEditNnc) +
serializer.packSize(m_gridDims) +
serializer.packSize(m_simulationConfig) +
serializer.packSize(m_transMult) +
serializer.packSize(m_faults) +
serializer.packSize(m_title) +
serializer.packSize(aquifer_config);
}
void ParallelEclipseState::pack(std::vector<char>& buffer, int& position,
EclMpiSerializer& serializer) const
{
serializer.pack(m_tables, buffer, position);
serializer.pack(m_runspec, buffer, position);
serializer.pack(m_eclipseConfig, buffer, position);
serializer.pack(m_deckUnitSystem, buffer, position);
serializer.pack(m_inputNnc, buffer, position);
serializer.pack(m_inputEditNnc, buffer, position);
serializer.pack(m_gridDims, buffer, position);
serializer.pack(m_simulationConfig, buffer, position);
serializer.pack(m_transMult, buffer, position);
serializer.pack(m_faults, buffer, position);
serializer.pack(m_title, buffer, position);
serializer.pack(aquifer_config, buffer, position);
}
void ParallelEclipseState::unpack(std::vector<char>& buffer, int& position,
EclMpiSerializer& serializer)
{
serializer.unpack(m_tables, buffer, position);
serializer.unpack(m_runspec, buffer, position);
serializer.unpack(m_eclipseConfig, buffer, position);
serializer.unpack(m_deckUnitSystem, buffer, position);
serializer.unpack(m_inputNnc, buffer, position);
serializer.unpack(m_inputEditNnc, buffer, position);
serializer.unpack(m_gridDims, buffer, position);
serializer.unpack(m_simulationConfig, buffer, position);
serializer.unpack(m_transMult, buffer, position);
serializer.unpack(m_faults, buffer, position);
serializer.unpack(m_title, buffer, position);
serializer.unpack(aquifer_config, buffer, position);
serializer(m_tables);
serializer(m_runspec);
serializer(m_eclipseConfig);
serializer(m_deckUnitSystem);
serializer(m_inputNnc);
serializer(m_inputEditNnc);
serializer(m_gridDims);
serializer(m_simulationConfig);
serializer(m_transMult);
serializer(m_faults);
serializer(m_title);
serializer(aquifer_config);
}
#endif

View File

@ -116,20 +116,9 @@ public:
ParallelEclipseState(const Deck& deck);
#if HAVE_MPI
//! \brief Calculates the size of serialized data.
//! \brief Perform serialization operation.
//! \param serializer The serializer to use
std::size_t packSize(EclMpiSerializer& serializer) const;
//! \brief Serialize data.
//! \param buffer Buffer to write serialized data into
//! \param Position in buffer
//! \param serializer The serializer to use
void pack(std::vector<char>& buffer, int& position, EclMpiSerializer& serializer) const;
//! \brief Deserialize data.
//! \param buffer Buffer to read serialized data from
//! \param Position in buffer
//! \param serializer The serializer to use
void unpack(std::vector<char>& buffer, int& position, EclMpiSerializer& serializer);
void serializeOp(EclMpiSerializer& serializer);
#endif
//! \brief Switch to global field properties.