ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
sendrecv.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2018-2022 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef UTILS_MPI_SENDRECV_HPP
21#define UTILS_MPI_SENDRECV_HPP
22
23#include <boost/mpi/communicator.hpp>
24#include <boost/mpi/datatype.hpp>
25#include <boost/mpi/exception.hpp>
26#include <boost/mpi/nonblocking.hpp>
27
28#include <array>
29
30namespace Utils {
31namespace Mpi {
32namespace mpi = boost::mpi;
33
34namespace detail {
35template <typename T>
36std::array<mpi::request, 2> isendrecv_impl(mpi::communicator const &comm,
37 int dest, int stag, const T &sval,
38 int src, int rtag, T &rval) {
39 return {{comm.isend(dest, stag, sval), comm.irecv(src, rtag, rval)}};
40}
41
42template <typename T>
43mpi::status sendrecv_impl(mpi::communicator const &comm, int dest, int stag,
44 const T &sval, int src, int rtag, T &rval,
45 boost::mpl::true_) {
46 mpi::status stat;
47 BOOST_MPI_CHECK_RESULT(
48 MPI_Sendrecv, (const_cast<T *>(&sval), 1, mpi::get_mpi_datatype<T>(sval),
49 dest, stag, &rval, 1, mpi::get_mpi_datatype<T>(rval), src,
50 rtag, comm, &stat.m_status));
51 return stat;
52}
53
54template <typename T>
55mpi::status sendrecv_impl(mpi::communicator const &comm, int dest, int stag,
56 const T &sval, int src, int rtag, T &rval,
57 boost::mpl::false_) {
58 auto srrequests = isendrecv_impl(comm, dest, stag, sval, src, rtag, rval);
59 mpi::status srstatuses[2];
60 wait_all(srrequests.begin(), srrequests.end(), srstatuses);
61 return srstatuses[1];
62}
63} // namespace detail
64
65template <typename T>
66mpi::status sendrecv(mpi::communicator const &comm, int dest, int stag,
67 const T &sval, int src, int rtag, T &rval) {
68 return detail::sendrecv_impl(comm, dest, stag, sval, src, rtag, rval,
70}
71
72template <typename T>
73std::array<mpi::request, 2> isendrecv(mpi::communicator const &comm, int dest,
74 int stag, const T &sval, int src,
75 int rtag, T &rval) {
76 return detail::isendrecv_impl(comm, dest, stag, sval, src, rtag, rval);
77}
78
79} // namespace Mpi
80} // namespace Utils
81
82#endif // ESPRESSO_SENDRECV_HPP
std::array< mpi::request, 2 > isendrecv(mpi::communicator const &comm, int dest, int stag, const T &sval, int src, int rtag, T &rval)
Definition sendrecv.hpp:73
mpi::status sendrecv(mpi::communicator const &comm, int dest, int stag, const T &sval, int src, int rtag, T &rval)
Definition sendrecv.hpp:66