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-2026 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#pragma once
22
23#include <algorithm>
24#include <cstddef>
25#include <numeric>
26#include <vector>
27
28#include <boost/mpi/collectives/gather.hpp>
29#include <boost/mpi/communicator.hpp>
30
31namespace Utils::Mpi::detail {
32
33template <typename T>
34int size_and_offset(std::vector<int> &sizes, std::vector<int> &displ,
35 int n_elem, const boost::mpi::communicator &comm,
36 int root = 0) {
37 auto const world_size = static_cast<unsigned int>(comm.size());
38 sizes.resize(world_size);
39 displ.resize(world_size);
40
41 /* Gather sizes */
42 boost::mpi::gather(comm, n_elem, sizes, root);
43
44 auto const total_size = std::accumulate(sizes.begin(), sizes.end(), 0);
45
46 int offset = 0;
47 for (std::size_t i = 0; i < sizes.size(); i++) {
48 displ[i] = offset;
49 offset += sizes[i];
50 }
51
52 return total_size;
53}
54
55inline void size_and_offset(int n_elem, const boost::mpi::communicator &comm,
56 int root = 0) {
57 /* Send local size */
58 boost::mpi::gather(comm, n_elem, root);
59}
60
61} // namespace Utils::Mpi::detail