KaMPIng 0.1.1
Flexible and (near) zero-overhead C++ bindings for MPI
Loading...
Searching...
No Matches
topology_communicator.hpp
1// This file is part of KaMPIng.
2//
3// Copyright 2024 The KaMPIng Authors
4//
5// KaMPIng is free software : you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
6// License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later
7// version. KaMPIng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
8// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
9// for more details.
10//
11// You should have received a copy of the GNU Lesser General Public License along with KaMPIng. If not, see
12// <https://www.gnu.org/licenses/>.
13
14#pragma once
15
16#include <kamping/collectives/collectives_helpers.hpp>
17#include <kamping/communicator.hpp>
18
19namespace kamping {
20/// @brief A \ref Communicator which possesses an additional virtual topology and supports neighborhood collectives (on
21/// the topology). A virtual topology can be defined by a communication graph: each MPI rank corresponds to a vertex in
22/// the graph and an edge (i,j) defines a (directed) communication link from rank i to rank j. Such a topolgy can be
23/// used to model frequent (sparse) communication patterns and there are specialized (neighborhood) collective
24/// operations, e.g., MPI_Neighbor_alltoall etc., exploiting this structure.
25///
26/// @tparam DefaultContainerType The default container type to use for containers created by KaMPIng. Defaults to
27/// std::vector.
28/// @tparam Plugins Plugins adding functionality to KaMPIng. Plugins should be classes taking a ``Communicator``
29/// template parameter and can assume that they are castable to `Communicator` from which they can
30/// call any function of `kamping::Communicator`. See `test/plugin_tests.cpp` for examples.
31template <
32 template <typename...> typename DefaultContainerType = std::vector,
33 template <typename, template <typename...> typename>
34 typename... Plugins>
36 : public Communicator<DefaultContainerType>,
37 public Plugins<TopologyCommunicator<DefaultContainerType, Plugins...>, DefaultContainerType>... {
38public:
39 /// @brief Type of the default container type to use for containers created inside operations of this communicator.
40 /// @tparam Args Arguments to the container type.
41 template <typename... Args>
43
44 /// @brief Returns the in degree of the process' rank, i.e. the number of in-going edges/communication links towards
45 /// the rank.
46 /// @return Number of in-going edges as unsigned integer.
47 size_t in_degree() const {
48 return _in_degree;
49 }
50 /// @brief Returns the in degree of the process' rank, i.e. the number of in-going edges/communication links towards
51 /// the rank.
52 /// @return Number of in-going edges as signed integer.
53 int in_degree_signed() const {
54 return asserting_cast<int>(_in_degree);
55 }
56
57 /// @brief Returns the out degree of the process' rank, i.e. the number of out-going edges/communication links
58 /// starting at the rank.
59 /// @return Number of out-going edges as unsigned integer.
60 size_t out_degree() const {
61 return _out_degree;
62 }
63
64 /// @brief Returns the out degree of the process' rank, i.e. the number of out-going edges/communication links
65 /// starting at the rank.
66 /// @return Number of out-going edges as signed integer.
67 int out_degree_signed() const {
68 return asserting_cast<int>(_out_degree);
69 }
70
71 template <typename... Args>
72 auto neighbor_alltoall(Args... args) const;
73
74protected:
76
77 /// @brief topolgy constructor using \c MPI_COMM_WORLD by default.
78 ///
79 /// @param in_degree In degree of the process' rank in the underlying communication graph.
80 /// @param out_degree Out degree of the process' rank in the underlying communication graph.
83
84 /// @brief topolgy constructor where an MPI communicator has to be specified.
85 ///
86 /// @param in_degree In degree of the process' rank in the underlying communication graph.
87 /// @param out_degree Out degree of the process' rank in the underlying communication graph.
88 /// @param comm MPI communicator that is wrapped by this \c Communicator.
89 /// @param take_ownership Whether the Communicator should take ownership of comm, i.e. free it in the destructor.
92
93 /// @brief topolgy constructor where an MPI communicator and the default root rank have to be specified.
94 ///
95 /// @param in_degree In degree of the process' rank in the underlying communication graph.
96 /// @param out_degree Out degree of the process' rank in the underlying communication graph.
97 /// @param comm MPI communicator that is wrapped by this \c Communicator.
98 /// @param root Default root that is used by MPI operations requiring a root.
99 /// @param take_ownership Whether the Communicator should take ownership of comm, i.e. free it in the destructor.
101 size_t in_degree, size_t out_degree, MPI_Comm comm, int root, bool take_ownership = false
102 )
104 _in_degree{in_degree},
105 _out_degree{out_degree} {}
106
107private:
108 size_t _in_degree; ///< In degree of the underlying communication graph.
109 size_t _out_degree; ///< Out degree of the underlying communication graph.
110};
111} // namespace kamping
Wrapper for MPI communicator providing access to rank() and size() of the communicator....
Definition communicator.hpp:49
Communicator()
Default constructor not specifying any MPI communicator and using MPI_COMM_WORLD by default.
Definition communicator.hpp:57
size_t root() const
Default root for MPI operations that require a root as size_t.
Definition communicator.hpp:239
STL-compatible allocator for requesting memory using the builtin MPI allocator.
Definition allocator.hpp:32
A Communicator which possesses an additional virtual topology and supports neighborhood collectives (...
Definition topology_communicator.hpp:37
int out_degree_signed() const
Returns the out degree of the process' rank, i.e. the number of out-going edges/communication links s...
Definition topology_communicator.hpp:67
size_t in_degree() const
Returns the in degree of the process' rank, i.e. the number of in-going edges/communication links tow...
Definition topology_communicator.hpp:47
TopologyCommunicator(size_t in_degree, size_t out_degree)
topolgy constructor using MPI_COMM_WORLD by default.
Definition topology_communicator.hpp:81
TopologyCommunicator(size_t in_degree, size_t out_degree, MPI_Comm comm, int root, bool take_ownership=false)
topolgy constructor where an MPI communicator and the default root rank have to be specified.
Definition topology_communicator.hpp:100
TopologyCommunicator(size_t in_degree, size_t out_degree, MPI_Comm comm, bool take_ownership=false)
topolgy constructor where an MPI communicator has to be specified.
Definition topology_communicator.hpp:90
size_t out_degree() const
Returns the out degree of the process' rank, i.e. the number of out-going edges/communication links s...
Definition topology_communicator.hpp:60
int in_degree_signed() const
Returns the in degree of the process' rank, i.e. the number of in-going edges/communication links tow...
Definition topology_communicator.hpp:53
auto neighbor_alltoall(Args... args) const
Wrapper for MPI_Neighbor_alltoall.
Definition alltoall.hpp:87