KASKADE 7 development version
Loading...
Searching...
No Matches
bddcMpiRouting.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the library KASKADE 7 */
4/* https://www.zib.de/research/projects/kaskade7-finite-element-toolbox */
5/* */
6/* Copyright (C) 2026 Zuse Institute Berlin */
7/* */
8/* KASKADE 7 is distributed under the terms of the ZIB Academic License. */
9/* see $KASKADE/academic.txt */
10/* */
11/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13#ifndef KASKADE_BDDC_MPI_ROUTING_HH
14#define KASKADE_BDDC_MPI_ROUTING_HH
15
16#include <algorithm>
17#include <array>
18#include <cstdint>
19#include <iostream>
20#include <limits>
21#include <numeric>
22#include <stdexcept>
23#include <vector>
24
25#ifdef KASKADE_HAVE_MPI
26#include <mpi.h>
27#endif
28
29namespace Kaskade::BDDC
30{
35 inline int mpiRouteClass(int from, int to, std::vector<int> const& leaders)
36 {
37 if (from == to) { (void)leaders.at(from); return 0; }
38 return leaders.at(from) == leaders.at(to) ? 1 : 2;
39 }
40
47 {
48 public:
49 void configure()
50 {
51#ifdef KASKADE_HAVE_MPI
52 int initialized = 0;
53 MPI_Initialized(&initialized);
54 if (!initialized) throw std::runtime_error("BDDC topology requires MPI_Init");
55 int rank = 0, size = 1;
56 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
57 MPI_Comm_size(MPI_COMM_WORLD, &size);
58 MPI_Comm shared = MPI_COMM_NULL;
59 MPI_Comm_split_type(MPI_COMM_WORLD, MPI_COMM_TYPE_SHARED, rank, MPI_INFO_NULL, &shared);
60 int leader = rank;
61 MPI_Allreduce(&rank, &leader, 1, MPI_INT, MPI_MIN, shared);
62 MPI_Comm_free(&shared);
63 leaders_.resize(size);
64 MPI_Allgather(&leader, 1, MPI_INT, leaders_.data(), 1, MPI_INT, MPI_COMM_WORLD);
65#else
66 throw std::runtime_error("BDDC topology requires an MPI build");
67#endif
68 }
69
70 int route(int from, int to) const { return mpiRouteClass(from, to, leaders_); }
71 std::vector<int> const& leaders() const { return leaders_; }
72 int nodes() const
73 {
74 auto unique = leaders_;
75 std::sort(unique.begin(), unique.end());
76 return std::unique(unique.begin(), unique.end()) - unique.begin();
77 }
78
79 private:
80 std::vector<int> leaders_;
81 };
82
89 {
90 std::array<std::uint64_t,6> bytes{};
91 std::array<std::uint64_t,6> packets{};
92
93 void add(bool restriction, int route, std::uint64_t payload)
94 {
95 if (route < 0 || route > 2) throw std::invalid_argument("Invalid MPI route class");
96 auto index = (restriction ? 0 : 3) + route;
97 bytes.at(index) += payload;
98 ++packets.at(index);
99 }
100
101 void add(MpiRoutingTraffic const& other)
102 {
103 for (std::size_t i=0; i<bytes.size(); ++i)
104 {
105 bytes[i] += other.bytes[i];
106 packets[i] += other.packets[i];
107 }
108 }
109
111 void printGlobal() const
112 {
113#ifdef KASKADE_HAVE_MPI
114 MpiRoutingTraffic total;
115 MPI_Reduce(bytes.data(), total.bytes.data(), 6, MPI_UINT64_T, MPI_SUM, 0, MPI_COMM_WORLD);
116 MPI_Reduce(packets.data(), total.packets.data(), 6, MPI_UINT64_T, MPI_SUM, 0, MPI_COMM_WORLD);
117 int rank = 0;
118 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
119 if (rank != 0) return;
120 char const* routes[] = {"same-rank", "same-node", "inter-node"};
121 for (int i=0; i<6; ++i)
122 std::cout << "BDDC routed payload: phase=" << (i<3 ? "restrict" : "prolongate")
123 << " route=" << routes[i%3] << " bytes=" << total.bytes[i]
124 << " packets=" << total.packets[i] << '\n';
125#endif
126 }
127 };
128
129 namespace detail
130 {
132 inline bool mpiByteLayout(std::vector<std::size_t> const& sizes,
133 std::vector<int>& counts, std::vector<int>& offsets, int& total)
134 {
135 counts.resize(sizes.size());
136 offsets.resize(sizes.size());
137 total = 0;
138 for (std::size_t i=0; i<sizes.size(); ++i)
139 {
140 if (sizes[i] > static_cast<std::size_t>(std::numeric_limits<int>::max() - total))
141 return false;
142 offsets[i] = total;
143 counts[i] = static_cast<int>(sizes[i]);
144 total += counts[i];
145 }
146 return true;
147 }
148
149#ifdef KASKADE_HAVE_MPI
150 struct MpiRoutedBytes
151 {
152 std::vector<unsigned char> bytes;
153 std::vector<int> counts, offsets;
154 };
155
162 inline MpiRoutedBytes mpiExchangeByDestination(
163 std::vector<std::vector<unsigned char>> const& outgoing)
164 {
165 int size = 1;
166 MPI_Comm_size(MPI_COMM_WORLD, &size);
167 std::vector<std::size_t> sizes;
168 for (auto const& bucket : outgoing) sizes.push_back(bucket.size());
169 std::vector<int> sendCounts, sendOffsets;
170 int sendTotal = 0;
171 int invalid = outgoing.size() != static_cast<std::size_t>(size) ||
172 !mpiByteLayout(sizes, sendCounts, sendOffsets, sendTotal);
173 int anyInvalid = 0;
174 MPI_Allreduce(&invalid, &anyInvalid, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
175 if (anyInvalid) throw std::runtime_error("BDDC routed send count/size exceeds MPI limits");
176
177 MpiRoutedBytes received;
178 received.counts.resize(size);
179 MPI_Alltoall(sendCounts.data(), 1, MPI_INT, received.counts.data(), 1, MPI_INT,
180 MPI_COMM_WORLD);
181 sizes.assign(received.counts.begin(), received.counts.end());
182 int receiveTotal = 0;
183 invalid = !mpiByteLayout(sizes, received.counts, received.offsets, receiveTotal);
184 MPI_Allreduce(&invalid, &anyInvalid, 1, MPI_INT, MPI_MAX, MPI_COMM_WORLD);
185 if (anyInvalid) throw std::runtime_error("BDDC routed receive size exceeds MPI limits");
186
187 std::vector<unsigned char> send(static_cast<std::size_t>(sendTotal));
188 for (int destination=0; destination<size; ++destination)
189 std::copy(outgoing[destination].begin(), outgoing[destination].end(),
190 send.begin()+sendOffsets[destination]);
191 received.bytes.resize(static_cast<std::size_t>(receiveTotal));
192 unsigned char empty = 0;
193 MPI_Alltoallv(send.empty() ? &empty : send.data(), sendCounts.data(), sendOffsets.data(),
194 MPI_BYTE, received.bytes.empty() ? &empty : received.bytes.data(),
195 received.counts.data(), received.offsets.data(), MPI_BYTE, MPI_COMM_WORLD);
196 return received;
197 }
198#endif
199 }
200}
201#endif
Cache the MPI_COMM_TYPE_SHARED partition without retaining communicators. configure() is collective o...
std::vector< int > const & leaders() const
int route(int from, int to) const
constexpr int rank
Reports the rank of vector, matrix, and tensor types of static size.
Definition scalar.hh:153
bool mpiByteLayout(std::vector< std::size_t > const &sizes, std::vector< int > &counts, std::vector< int > &offsets, int &total)
Validate legacy MPI int counts/displacements without narrowing overflow.
int mpiRouteClass(int from, int to, std::vector< int > const &leaders)
Classify an endpoint pair using MPI shared-memory group leaders.
Rank-local sent payload accounting for R/P and the three endpoint classes. Count each directed interf...
void add(MpiRoutingTraffic const &other)
void add(bool restriction, int route, std::uint64_t payload)
std::array< std::uint64_t, 6 > packets
void printGlobal() const
Collectively print producer-counted totals once on rank zero.
std::array< std::uint64_t, 6 > bytes