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-2026 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
30// GCC 16 -fanalyzer has false positives with boost::mpi::communicator::rank()
31#if defined(__GNUC__) and not defined(__clang__)
32#pragma GCC diagnostic push
33#pragma GCC diagnostic ignored "-Wanalyzer-null-dereference"
34#endif
35
36namespace Utils {
37namespace Mpi {
38namespace detail {
39
40inline std::vector<int> displacements(std::span<int const> sizes) {
41 std::vector<int> displ(sizes.size());
42
43 int offset = 0;
44 for (std::size_t i = 0u; i < displ.size(); i++) {
45 displ[i] = offset;
46 offset += sizes[i];
47 }
48
49 return displ;
50}
51
52template <typename T>
53std::vector<boost::mpi::request>
54iall_gatherv_impl(boost::mpi::communicator const &comm, T const *in_values,
55 int in_size, T *out_values, int const *sizes,
56 int const *displs) {
57 auto const n_nodes = comm.size();
58 auto const rank = comm.rank();
59
60 /* not in-place */
61 if (in_values != out_values) {
62 std::copy_n(in_values, in_size, out_values + displs[rank]);
63 }
64
65 std::vector<boost::mpi::request> req;
66 for (int i = 0; i < n_nodes; i++) {
67 if (i != rank) {
68 req.emplace_back(comm.isend(i, 42, out_values + displs[rank], in_size));
69 req.emplace_back(comm.irecv(i, 42, out_values + displs[i], sizes[i]));
70 }
71 }
72
73 return req;
74}
75
76} // namespace detail
77
78template <typename T>
79auto iall_gatherv(boost::mpi::communicator const &comm, T const *in_values,
80 int in_size, T *out_values, int const *sizes) {
81 auto const displ =
82 detail::displacements({sizes, static_cast<std::size_t>(comm.size())});
83
84 return detail::iall_gatherv_impl(comm, in_values, in_size, out_values, sizes,
85 displ.data());
86}
87} // namespace Mpi
88} // namespace Utils
89
90#if defined(__GNUC__) and not defined(__clang__)
91#pragma GCC diagnostic pop
92#endif
auto iall_gatherv(boost::mpi::communicator const &comm, T const *in_values, int in_size, T *out_values, int const *sizes)