2022-09-07 04:21:35 -05:00
|
|
|
/*
|
|
|
|
Copyright 2019 Equinor AS.
|
|
|
|
|
|
|
|
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 3 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/>.
|
|
|
|
*/
|
2022-09-14 03:11:00 -05:00
|
|
|
#ifndef MPI_PACKER_HPP
|
|
|
|
#define MPI_PACKER_HPP
|
2022-09-07 04:21:35 -05:00
|
|
|
|
|
|
|
#include <opm/common/utility/TimeService.hpp>
|
|
|
|
#include <opm/simulators/utils/ParallelCommunication.hpp>
|
|
|
|
|
2022-09-07 06:54:15 -05:00
|
|
|
#include <dune/common/parallel/mpitraits.hh>
|
|
|
|
|
2022-09-07 04:21:35 -05:00
|
|
|
#include <bitset>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <string>
|
|
|
|
|
2022-09-07 06:54:15 -05:00
|
|
|
namespace Opm {
|
|
|
|
namespace Mpi {
|
|
|
|
namespace detail {
|
2022-09-07 04:21:35 -05:00
|
|
|
|
2022-09-07 06:54:15 -05:00
|
|
|
//! \brief Abstract struct for packing which is (partially) specialized for specific types.
|
|
|
|
template <bool pod, class T>
|
|
|
|
struct Packing
|
2022-09-07 04:21:35 -05:00
|
|
|
{
|
2022-09-07 06:54:15 -05:00
|
|
|
static std::size_t packSize(const T&, Parallel::MPIComm);
|
|
|
|
static void pack(const T&, std::vector<char>&, int&, Parallel::MPIComm);
|
|
|
|
static void unpack(T&, std::vector<char>&, int&, Parallel::MPIComm);
|
|
|
|
};
|
2022-09-07 04:21:35 -05:00
|
|
|
|
2022-09-07 06:54:15 -05:00
|
|
|
//! \brief Packaging for pod data.
|
2022-09-07 04:21:35 -05:00
|
|
|
template<class T>
|
2022-09-07 06:54:15 -05:00
|
|
|
struct Packing<true,T>
|
2022-09-07 04:21:35 -05:00
|
|
|
{
|
2022-09-07 06:54:15 -05:00
|
|
|
//! \brief Calculates the pack size for a POD.
|
|
|
|
//! \param data The data to pack
|
|
|
|
//! \param comm The communicator to use
|
|
|
|
static std::size_t packSize(const T& data, Parallel::MPIComm comm)
|
|
|
|
{
|
|
|
|
return packSize(&data, 1, comm);
|
|
|
|
}
|
|
|
|
|
|
|
|
//! \brief Calculates the pack size for an array of POD.
|
|
|
|
//! \param data The array to pack
|
|
|
|
//! \param n Length of array
|
|
|
|
//! \param comm The communicator to use
|
|
|
|
static std::size_t packSize(const T*, std::size_t n, Parallel::MPIComm comm)
|
|
|
|
{
|
|
|
|
int size = 0;
|
|
|
|
MPI_Pack_size(n, Dune::MPITraits<T>::getType(), comm, &size);
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! \brief Pack a POD.
|
|
|
|
//! \param data The variable to pack
|
|
|
|
//! \param buffer Buffer to pack into
|
|
|
|
//! \param position Position in buffer to use
|
|
|
|
//! \param comm The communicator to use
|
|
|
|
static void pack(const T& data,
|
|
|
|
std::vector<char>& buffer,
|
|
|
|
int& position,
|
|
|
|
Parallel::MPIComm comm)
|
|
|
|
{
|
|
|
|
pack(&data, 1, buffer, position, comm);
|
|
|
|
}
|
|
|
|
|
|
|
|
//! \brief Pack an array of POD.
|
|
|
|
//! \param data The array to pack
|
|
|
|
//! \param n Length of array
|
|
|
|
//! \param buffer Buffer to pack into
|
|
|
|
//! \param position Position in buffer to use
|
|
|
|
//! \param comm The communicator to use
|
|
|
|
static void pack(const T* data,
|
|
|
|
std::size_t n,
|
|
|
|
std::vector<char>& buffer,
|
|
|
|
int& position,
|
|
|
|
Parallel::MPIComm comm)
|
|
|
|
{
|
|
|
|
MPI_Pack(data, n, Dune::MPITraits<T>::getType(), buffer.data(),
|
|
|
|
buffer.size(), &position, comm);
|
|
|
|
}
|
|
|
|
|
|
|
|
//! \brief Unpack a POD.
|
|
|
|
//! \param data The variable to unpack
|
|
|
|
//! \param buffer Buffer to unpack from
|
|
|
|
//! \param position Position in buffer to use
|
|
|
|
//! \param comm The communicator to use
|
|
|
|
static void unpack(T& data,
|
|
|
|
std::vector<char>& buffer,
|
|
|
|
int& position,
|
|
|
|
Parallel::MPIComm comm)
|
|
|
|
{
|
|
|
|
unpack(&data, 1, buffer, position, comm);
|
|
|
|
}
|
|
|
|
|
|
|
|
//! \brief Unpack an array of POD.
|
|
|
|
//! \param data The array to unpack
|
|
|
|
//! \param n Length of array
|
|
|
|
//! \param buffer Buffer to unpack from
|
|
|
|
//! \param position Position in buffer to use
|
|
|
|
//! \param comm The communicator to use
|
|
|
|
static void unpack(T* data,
|
|
|
|
std::size_t n,
|
|
|
|
std::vector<char>& buffer,
|
|
|
|
int& position,
|
|
|
|
Parallel::MPIComm comm)
|
|
|
|
{
|
|
|
|
MPI_Unpack(buffer.data(), buffer.size(), &position, data, n,
|
|
|
|
Dune::MPITraits<T>::getType(), comm);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//! \brief Default handling for unsupported types.
|
2022-09-07 04:21:35 -05:00
|
|
|
template<class T>
|
2022-09-07 06:54:15 -05:00
|
|
|
struct Packing<false,T>
|
2022-09-07 04:21:35 -05:00
|
|
|
{
|
2022-09-07 06:54:15 -05:00
|
|
|
static std::size_t packSize(const T&, Parallel::MPIComm)
|
|
|
|
{
|
|
|
|
static_assert(!std::is_same_v<T,T>, "Packing not supported for type");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pack(const T&, std::vector<char>&, int&,
|
|
|
|
Parallel::MPIComm)
|
|
|
|
{
|
|
|
|
static_assert(!std::is_same_v<T,T>, "Packing not supported for type");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void unpack(T&, std::vector<char>&, int&,
|
|
|
|
Parallel::MPIComm)
|
|
|
|
{
|
|
|
|
static_assert(!std::is_same_v<T,T>, "Packing not supported for type");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//! \brief Specialization for std::bitset
|
|
|
|
template <std::size_t Size>
|
|
|
|
struct Packing<false,std::bitset<Size>>
|
2022-09-07 04:21:35 -05:00
|
|
|
{
|
2022-09-07 06:54:15 -05:00
|
|
|
static std::size_t packSize(const std::bitset<Size>&, Opm::Parallel::MPIComm);
|
|
|
|
static void pack(const std::bitset<Size>&, std::vector<char>&, int&, Opm::Parallel::MPIComm);
|
|
|
|
static void unpack(std::bitset<Size>&, std::vector<char>&, int&, Opm::Parallel::MPIComm);
|
|
|
|
};
|
|
|
|
|
|
|
|
#define ADD_PACK_SPECIALIZATION(T) \
|
|
|
|
template<> \
|
|
|
|
struct Packing<false,T> \
|
|
|
|
{ \
|
|
|
|
static std::size_t packSize(const T&, Parallel::MPIComm); \
|
|
|
|
static void pack(const T&, std::vector<char>&, int&, Parallel::MPIComm); \
|
|
|
|
static void unpack(T&, std::vector<char>&, int&, Parallel::MPIComm); \
|
|
|
|
};
|
|
|
|
|
|
|
|
ADD_PACK_SPECIALIZATION(std::string)
|
|
|
|
ADD_PACK_SPECIALIZATION(time_point)
|
2022-09-07 04:21:35 -05:00
|
|
|
|
2022-09-14 03:11:00 -05:00
|
|
|
#undef ADD_PACK_SPECIALIZATION
|
|
|
|
|
2022-09-07 04:21:35 -05:00
|
|
|
}
|
|
|
|
|
2022-09-07 06:54:15 -05:00
|
|
|
//! \brief Struct handling packing of serialization for MPI communication.
|
|
|
|
struct Packer {
|
2022-09-14 03:11:00 -05:00
|
|
|
//! \brief Constructor.
|
|
|
|
//! \param comm The communicator to use
|
|
|
|
Packer(Parallel::Communication comm)
|
|
|
|
: m_comm(comm)
|
|
|
|
{}
|
|
|
|
|
2022-09-07 06:54:15 -05:00
|
|
|
//! \brief Calculates the pack size for a variable.
|
|
|
|
//! \tparam T The type of the data to be packed
|
|
|
|
//! \param data The data to pack
|
|
|
|
template<class T>
|
2022-09-14 03:11:00 -05:00
|
|
|
std::size_t packSize(const T& data) const
|
2022-09-07 06:54:15 -05:00
|
|
|
{
|
2022-09-14 03:11:00 -05:00
|
|
|
return detail::Packing<std::is_pod_v<T>,T>::packSize(data, m_comm);
|
2022-09-07 06:54:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//! \brief Calculates the pack size for an array.
|
|
|
|
//! \tparam T The type of the data to be packed
|
|
|
|
//! \param data The array to pack
|
|
|
|
//! \param n Length of array
|
|
|
|
template<class T>
|
2022-09-14 03:11:00 -05:00
|
|
|
std::size_t packSize(const T* data, std::size_t n) const
|
2022-09-07 06:54:15 -05:00
|
|
|
{
|
|
|
|
static_assert(std::is_pod_v<T>, "Array packing not supported for non-pod data");
|
2022-09-14 03:11:00 -05:00
|
|
|
return detail::Packing<true,T>::packSize(data, n, m_comm);
|
2022-09-07 06:54:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//! \brief Pack a variable.
|
|
|
|
//! \tparam T The type of the data to be packed
|
|
|
|
//! \param data The variable to pack
|
|
|
|
//! \param buffer Buffer to pack into
|
|
|
|
//! \param position Position in buffer to use
|
|
|
|
template<class T>
|
2022-09-14 03:11:00 -05:00
|
|
|
void pack(const T& data,
|
|
|
|
std::vector<char>& buffer,
|
|
|
|
int& position) const
|
2022-09-07 06:54:15 -05:00
|
|
|
{
|
2022-09-14 03:11:00 -05:00
|
|
|
detail::Packing<std::is_pod_v<T>,T>::pack(data, buffer, position, m_comm);
|
2022-09-07 06:54:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//! \brief Pack an array.
|
|
|
|
//! \tparam T The type of the data to be packed
|
|
|
|
//! \param data The array to pack
|
|
|
|
//! \param n Length of array
|
|
|
|
//! \param buffer Buffer to pack into
|
|
|
|
//! \param position Position in buffer to use
|
|
|
|
template<class T>
|
2022-09-14 03:11:00 -05:00
|
|
|
void pack(const T* data,
|
|
|
|
std::size_t n,
|
|
|
|
std::vector<char>& buffer,
|
|
|
|
int& position) const
|
2022-09-07 06:54:15 -05:00
|
|
|
{
|
|
|
|
static_assert(std::is_pod_v<T>, "Array packing not supported for non-pod data");
|
2022-09-14 03:11:00 -05:00
|
|
|
detail::Packing<true,T>::pack(data, n, buffer, position, m_comm);
|
2022-09-07 06:54:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//! \brief Unpack a variable.
|
|
|
|
//! \tparam T The type of the data to be unpacked
|
|
|
|
//! \param data The variable to unpack
|
|
|
|
//! \param buffer Buffer to unpack from
|
|
|
|
//! \param position Position in buffer to use
|
|
|
|
template<class T>
|
2022-09-14 03:11:00 -05:00
|
|
|
void unpack(T& data,
|
|
|
|
std::vector<char>& buffer,
|
|
|
|
int& position) const
|
2022-09-07 06:54:15 -05:00
|
|
|
{
|
2022-09-14 03:11:00 -05:00
|
|
|
detail::Packing<std::is_pod_v<T>,T>::unpack(data, buffer, position, m_comm);
|
2022-09-07 06:54:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//! \brief Unpack an array.
|
|
|
|
//! \tparam T The type of the data to be unpacked
|
|
|
|
//! \param data The array to unpack
|
|
|
|
//! \param n Length of array
|
|
|
|
//! \param buffer Buffer to unpack from
|
|
|
|
//! \param position Position in buffer to use
|
|
|
|
template<class T>
|
2022-09-14 03:11:00 -05:00
|
|
|
void unpack(T* data,
|
|
|
|
std::size_t n,
|
|
|
|
std::vector<char>& buffer,
|
|
|
|
int& position) const
|
2022-09-07 06:54:15 -05:00
|
|
|
{
|
|
|
|
static_assert(std::is_pod_v<T>, "Array packing not supported for non-pod data");
|
2022-09-14 03:11:00 -05:00
|
|
|
detail::Packing<true,T>::unpack(data, n, buffer, position, m_comm);
|
2022-09-07 06:54:15 -05:00
|
|
|
}
|
2022-09-14 03:11:00 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
Parallel::Communication m_comm; //!< Communicator to use
|
2022-09-07 06:54:15 -05:00
|
|
|
};
|
2022-09-07 04:21:35 -05:00
|
|
|
|
|
|
|
} // end namespace Mpi
|
|
|
|
} // end namespace Opm
|
2022-09-07 06:54:15 -05:00
|
|
|
|
2022-09-14 03:11:00 -05:00
|
|
|
#endif // MPI_PACKER_HPP
|