ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
cart_comm.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#ifndef UTILS_MPI_CART_COMM_HPP
20#define UTILS_MPI_CART_COMM_HPP
21
22#include <utils/Vector.hpp>
23
24#include <boost/mpi/communicator.hpp>
25#include <boost/mpi/exception.hpp>
26
27#include <mpi.h>
28
29#include <cstddef>
30#include <utility>
31
32namespace Utils {
33namespace Mpi {
34
35/**
36 * @brief Wrapper around MPI_Dims_create.
37 *
38 * @tparam dim Number of dimensions
39 */
40template <std::size_t dim> Vector<int, dim> dims_create(int nodes) {
41 Vector<int, dim> dims{};
42 BOOST_MPI_CHECK_RESULT(MPI_Dims_create,
43 (nodes, static_cast<int>(dim), dims.data()))
44
45 return dims;
46}
47
48/**
49 * @brief Wrapper around MPI_Cart_create.
50 *
51 * @tparam dim Number of dimensions
52 */
53template <std::size_t dim>
54boost::mpi::communicator cart_create(
55 boost::mpi::communicator const &comm, Vector<int, dim> const &dims,
56 bool reorder = true,
57 Vector<int, dim> const &periodicity = Vector<int, dim>::broadcast(1)) {
58 MPI_Comm temp_comm;
59 BOOST_MPI_CHECK_RESULT(MPI_Cart_create,
60 (comm, dim, dims.data(), periodicity.data(),
61 static_cast<int>(reorder), &temp_comm))
62
63 return {temp_comm, boost::mpi::comm_take_ownership};
64}
65
66/**
67 * @brief Wrapper around MPI_Cart_coords.
68 *
69 * @tparam dims Number of dimensions
70 */
71template <std::size_t dims>
72Vector3i cart_coords(boost::mpi::communicator const &comm, int rank) {
73 Vector3i pos;
74 BOOST_MPI_CHECK_RESULT(MPI_Cart_coords, (comm, rank, dims, pos.data()))
75 return pos;
76}
77
78/**
79 * @brief Wrapper around MPI_Cart_rank.
80 *
81 * @tparam dims Number of dimensions
82 */
83template <std::size_t dims>
84int cart_rank(boost::mpi::communicator const &comm,
85 const Vector<int, dims> &pos) {
86 int rank;
87 BOOST_MPI_CHECK_RESULT(MPI_Cart_rank, (comm, pos.data(), &rank))
88 return rank;
89}
90
91/**
92 * @brief Wrapper around MPI_Cart_shift.
93 *
94 * @return pair of source and destination rank.
95 */
96inline std::pair<int, int> cart_shift(boost::mpi::communicator const &comm,
97 int direction, int displacement) {
98 int src = -1, dst = -1;
99 BOOST_MPI_CHECK_RESULT(MPI_Cart_shift,
100 (comm, direction, displacement, &src, &dst))
101
102 return {src, dst};
103}
104
105/**
106 * @brief Calculates the numbers of the nearest neighbors for a node.
107 *
108 * @tparam dim Dimension of the communicator
109 * @param comm Cartesian communicator
110 *
111 * @return Ranks of 2*dim neighbors
112 */
113template <std::size_t dim>
115cart_neighbors(const boost::mpi::communicator &comm) {
116
118
119 for (std::size_t i = 0; i < dim; i++) {
120 ret[2 * i + 0] = std::get<1>(cart_shift(comm, static_cast<int>(i), -1));
121 ret[2 * i + 1] = std::get<1>(cart_shift(comm, static_cast<int>(i), +1));
122 }
123
124 return ret;
125}
126
127/**
128 * @brief Information about a cartesian communicator.
129 *
130 * Members correspond to the output arguments of
131 * MPI_Cart_get.
132 *
133 * @tparam dim Number of dimensions.
134 */
140
141/**
142 * @brief Wrapper around MPI_Cart_get.
143 *
144 * @tparam dim Number of dimensions.
145 * @param comm Communicator with cartesian topology.
146 *
147 * @return Struct with information about the communicator.
148 */
149template <std::size_t dim>
150CartInfo<dim> cart_get(const boost::mpi::communicator &comm) {
151 CartInfo<dim> ret{};
152
153 BOOST_MPI_CHECK_RESULT(MPI_Cart_get, (comm, dim, ret.dims.data(),
154 ret.periods.data(), ret.coords.data()));
155
156 return ret;
157}
158
159} // namespace Mpi
160} // namespace Utils
161
162#endif
Vector implementation and trait types for boost qvm interoperability.
int cart_rank(boost::mpi::communicator const &comm, const Vector< int, dims > &pos)
Wrapper around MPI_Cart_rank.
Definition cart_comm.hpp:84
std::pair< int, int > cart_shift(boost::mpi::communicator const &comm, int direction, int displacement)
Wrapper around MPI_Cart_shift.
Definition cart_comm.hpp:96
Vector3i cart_coords(boost::mpi::communicator const &comm, int rank)
Wrapper around MPI_Cart_coords.
Definition cart_comm.hpp:72
Vector< int, dim > dims_create(int nodes)
Wrapper around MPI_Dims_create.
Definition cart_comm.hpp:40
CartInfo< dim > cart_get(const boost::mpi::communicator &comm)
Wrapper around MPI_Cart_get.
Utils::Vector< int, 2 *dim > cart_neighbors(const boost::mpi::communicator &comm)
Calculates the numbers of the nearest neighbors for a node.
boost::mpi::communicator cart_create(boost::mpi::communicator const &comm, Vector< int, dim > const &dims, bool reorder=true, Vector< int, dim > const &periodicity=Vector< int, dim >::broadcast(1))
Wrapper around MPI_Cart_create.
Definition cart_comm.hpp:54
DEVICE_QUALIFIER constexpr pointer data() noexcept
Definition Array.hpp:120
Information about a cartesian communicator.
Utils::Vector< int, dim > dims
Utils::Vector< int, dim > periods
Utils::Vector< int, dim > coords