ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
iall_gatherv.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-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#pragma once
21
22#include <boost/mpi/communicator.hpp>
23#include <boost/mpi/request.hpp>
24
25#include <algorithm>
26#include <cstddef>
27#include <span>
28#include <vector>
29
30namespace Utils {
31namespace Mpi {
32namespace detail {
33
34inline std::vector<int> displacements(std::span<int const> sizes) {
35 std::vector<int> displ(sizes.size());
36
37 int offset = 0;
38 for (std::size_t i = 0u; i < displ.size(); i++) {
39 displ[i] = offset;
40 offset += sizes[i];
41 }
42
43 return displ;
44}
45
46template <typename T>
47std::vector<boost::mpi::request>
48iall_gatherv_impl(boost::mpi::communicator const &comm, T const *in_values,
49 int in_size, T *out_values, int const *sizes,
50 int const *displs) {
51 auto const n_nodes = comm.size();
52 auto const rank = comm.rank();
53
54 /* not in-place */
55 if (in_values != out_values) {
56 std::copy_n(in_values, in_size, out_values + displs[rank]);
57 }
58
59 std::vector<boost::mpi::request> req;
60 for (int i = 0; i < n_nodes; i++) {
61 if (i != rank) {
62 req.emplace_back(comm.isend(i, 42, out_values + displs[rank], in_size));
63 req.emplace_back(comm.irecv(i, 42, out_values + displs[i], sizes[i]));
64 }
65 }
66
67 return req;
68}
69
70} // namespace detail
71
72template <typename T>
73auto iall_gatherv(boost::mpi::communicator const &comm, T const *in_values,
74 int in_size, T *out_values, int const *sizes) {
75 auto const displ =
76 detail::displacements({sizes, static_cast<size_t>(comm.size())});
77
78 return detail::iall_gatherv_impl(comm, in_values, in_size, out_values, sizes,
79 displ.data());
80}
81} // namespace Mpi
82} // namespace Utils
auto iall_gatherv(boost::mpi::communicator const &comm, T const *in_values, int in_size, T *out_values, int const *sizes)