mirror of
https://github.com/OPM/opm-simulators.git
synced 2025-02-25 18:55:30 -06:00
Merge pull request #2447 from akva2/simplify_serializer
changed: simplify eclmpiserializer
This commit is contained in:
commit
fa158893db
@ -27,23 +27,45 @@ namespace Opm {
|
|||||||
|
|
||||||
class EclMpiSerializer {
|
class EclMpiSerializer {
|
||||||
public:
|
public:
|
||||||
|
enum class Operation {
|
||||||
|
PACKSIZE,
|
||||||
|
PACK,
|
||||||
|
UNPACK
|
||||||
|
};
|
||||||
|
|
||||||
explicit EclMpiSerializer(Dune::CollectiveCommunication<Dune::MPIHelper::MPICommunicator> comm) :
|
explicit EclMpiSerializer(Dune::CollectiveCommunication<Dune::MPIHelper::MPICommunicator> comm) :
|
||||||
m_comm(comm)
|
m_comm(comm)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
std::size_t packSize(const T& data) {
|
void operator()(const T& data)
|
||||||
return Mpi::packSize(data, m_comm);
|
{
|
||||||
|
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>
|
template<class T>
|
||||||
void pack(const T& data, std::vector<char>& buffer, int& pos) {
|
void pack(T& data)
|
||||||
Mpi::pack(data, buffer, pos, m_comm);
|
{
|
||||||
|
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>
|
template<class T>
|
||||||
void unpack(T& data, std::vector<char>& buffer, int& pos) {
|
void unpack(T& data)
|
||||||
Mpi::unpack(data, buffer, pos, m_comm);
|
{
|
||||||
|
m_position = 0;
|
||||||
|
m_op = Operation::UNPACK;
|
||||||
|
data.serializeOp(*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
@ -54,25 +76,30 @@ public:
|
|||||||
|
|
||||||
#if HAVE_MPI
|
#if HAVE_MPI
|
||||||
if (m_comm.rank() == 0) {
|
if (m_comm.rank() == 0) {
|
||||||
size_t size = data.packSize(*this);
|
pack(data);
|
||||||
std::vector<char> buffer(size);
|
m_comm.broadcast(&m_position, 1, 0);
|
||||||
int position = 0;
|
m_comm.broadcast(m_buffer.data(), m_position, 0);
|
||||||
data.pack(buffer, position, *this);
|
|
||||||
m_comm.broadcast(&position, 1, 0);
|
|
||||||
m_comm.broadcast(buffer.data(), position, 0);
|
|
||||||
} else {
|
} else {
|
||||||
int size;
|
m_comm.broadcast(&m_packSize, 1, 0);
|
||||||
m_comm.broadcast(&size, 1, 0);
|
m_buffer.resize(m_packSize);
|
||||||
std::vector<char> buffer(size);
|
m_comm.broadcast(m_buffer.data(), m_packSize, 0);
|
||||||
m_comm.broadcast(buffer.data(), size, 0);
|
unpack(data);
|
||||||
int position = 0;
|
|
||||||
data.unpack(buffer, position, *this);
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
size_t position() const
|
||||||
|
{
|
||||||
|
return m_position;
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Dune::CollectiveCommunication<Dune::MPIHelper::MPICommunicator> m_comm;
|
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;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -137,57 +137,20 @@ ParallelEclipseState::ParallelEclipseState(const Deck& deck)
|
|||||||
|
|
||||||
|
|
||||||
#if HAVE_MPI
|
#if HAVE_MPI
|
||||||
std::size_t ParallelEclipseState::packSize(EclMpiSerializer& serializer) const
|
void ParallelEclipseState::serializeOp(EclMpiSerializer& serializer)
|
||||||
{
|
{
|
||||||
return serializer.packSize(m_tables) +
|
serializer(m_tables);
|
||||||
serializer.packSize(m_runspec) +
|
serializer(m_runspec);
|
||||||
serializer.packSize(m_eclipseConfig) +
|
serializer(m_eclipseConfig);
|
||||||
serializer.packSize(m_deckUnitSystem) +
|
serializer(m_deckUnitSystem);
|
||||||
serializer.packSize(m_inputNnc) +
|
serializer(m_inputNnc);
|
||||||
serializer.packSize(m_inputEditNnc) +
|
serializer(m_inputEditNnc);
|
||||||
serializer.packSize(m_gridDims) +
|
serializer(m_gridDims);
|
||||||
serializer.packSize(m_simulationConfig) +
|
serializer(m_simulationConfig);
|
||||||
serializer.packSize(m_transMult) +
|
serializer(m_transMult);
|
||||||
serializer.packSize(m_faults) +
|
serializer(m_faults);
|
||||||
serializer.packSize(m_title) +
|
serializer(m_title);
|
||||||
serializer.packSize(aquifer_config);
|
serializer(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);
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -116,20 +116,9 @@ public:
|
|||||||
ParallelEclipseState(const Deck& deck);
|
ParallelEclipseState(const Deck& deck);
|
||||||
|
|
||||||
#if HAVE_MPI
|
#if HAVE_MPI
|
||||||
//! \brief Calculates the size of serialized data.
|
//! \brief Perform serialization operation.
|
||||||
//! \param serializer The serializer to use
|
//! \param serializer The serializer to use
|
||||||
std::size_t packSize(EclMpiSerializer& serializer) const;
|
void serializeOp(EclMpiSerializer& serializer);
|
||||||
|
|
||||||
//! \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);
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
//! \brief Switch to global field properties.
|
//! \brief Switch to global field properties.
|
||||||
|
Loading…
Reference in New Issue
Block a user