ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
size_and_offset.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2017-2022 The ESPResSo project
3 * Max-Planck-Institute for Polymer Research, Theory Group
4 *
5 * This file is part of ESPResSo.
6 *
7 * ESPResSo is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * ESPResSo is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#ifndef UTILS_MPI_DETAIL_SIZE_AND_OFFSET_HPP
22#define UTILS_MPI_DETAIL_SIZE_AND_OFFSET_HPP
23
24#include <algorithm>
25#include <numeric>
26#include <vector>
27
28#include <boost/mpi/collectives.hpp>
29#include <boost/mpi/communicator.hpp>
30
31namespace Utils {
32namespace Mpi {
33namespace detail {
34
35template <typename T>
36int size_and_offset(std::vector<int> &sizes, std::vector<int> &displ,
37 int n_elem, const boost::mpi::communicator &comm,
38 int root = 0) {
39 auto const world_size = static_cast<unsigned int>(comm.size());
40 sizes.resize(world_size);
41 displ.resize(world_size);
42
43 /* Gather sizes */
44 boost::mpi::gather(comm, n_elem, sizes, root);
45
46 auto const total_size = std::accumulate(sizes.begin(), sizes.end(), 0);
47
48 int offset = 0;
49 for (unsigned int i = 0; i < sizes.size(); i++) {
50 displ[i] = offset;
51 offset += sizes[i];
52 }
53
54 return total_size;
55}
56
57inline void size_and_offset(int n_elem, const boost::mpi::communicator &comm,
58 int root = 0) {
59 /* Send local size */
60 boost::mpi::gather(comm, n_elem, root);
61}
62
63} // namespace detail
64} // namespace Mpi
65} // namespace Utils
66
67#endif