https://mooseframework.inl.gov
Loading...
Searching...
No Matches
MFEMDistributedGraph.C
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9
10#ifdef MOOSE_MFEM_ENABLED
11
13#include "MooseError.h"
15
16namespace
17{
18
21struct Proposal
22{
24 std::int64_t component;
26 std::int64_t wu, wv, partner;
27 int origin_rank, origin_index;
28};
29
31struct LabelQuery
32{
33 std::int64_t id;
34 std::int64_t label;
37 int origin_rank, origin_index;
38};
39}
40
41namespace Moose::MFEM
42{
43
44BlockDistribution::BlockDistribution(std::int64_t n, int nprocs) : _n(n), _nprocs(nprocs) {}
45
46int
47BlockDistribution::owner(std::int64_t g) const
48{
49 mooseAssert(g >= 0 && g < _n, "Global id out of range");
50 return static_cast<int>(((g + 1) * _nprocs - 1) / _n);
51}
52
53std::int64_t
55{
56 return rank * _n / _nprocs;
57}
58
59std::int64_t
61{
62 return (rank + 1) * _n / _nprocs;
63}
64
65std::vector<std::int64_t>
66fetchLabels(const std::vector<std::int64_t> & queries,
67 const std::vector<std::int64_t> & owned_labels,
68 const BlockDistribution & dist,
69 MPI_Comm comm)
70{
71 int nprocs, rank;
72 MPI_Comm_size(comm, &nprocs);
73 MPI_Comm_rank(comm, &rank);
74
75 std::vector<LabelQuery> outgoing(queries.size());
76 for (const auto i : index_range(queries))
77 outgoing[i] = {queries[i], 0, rank, static_cast<int>(i)};
78
79 std::vector<int> counts;
80 const auto grouped = groupByDestination(
81 outgoing, nprocs, [&](const LabelQuery & q) { return dist.owner(q.id); }, counts);
82
83 auto received = allToAll(grouped, counts, comm);
84
85 // Answer from the owned slice, then send each answer straight back to the rank
86 // that asked for it.
87 const std::int64_t owned_begin = dist.begin(rank);
88 for (auto & q : received)
89 q.label = owned_labels[q.id - owned_begin];
90
91 std::vector<int> answer_counts;
92 const auto answers = groupByDestination(
93 received, nprocs, [](const LabelQuery & q) { return q.origin_rank; }, answer_counts);
94
95 std::vector<std::int64_t> labels(queries.size());
96 for (const auto & q : allToAll(answers, answer_counts, comm))
97 labels[q.origin_index] = q.label;
98 return labels;
99}
100
101void
102distributedSpanningForest(std::vector<DistributedEdge> & edges,
103 std::int64_t num_labels,
104 MPI_Comm comm,
105 std::vector<int> * selected,
106 std::vector<std::int64_t> * owned_labels)
107{
108 int nprocs, rank;
109 MPI_Comm_size(comm, &nprocs);
110 MPI_Comm_rank(comm, &rank);
111
112 if (num_labels == 0)
113 {
114 if (owned_labels)
115 owned_labels->clear();
116 return;
117 }
118
119 const BlockDistribution dist(num_labels, nprocs);
120 const std::int64_t owned_begin = dist.begin(rank);
121 const auto num_owned = static_cast<std::size_t>(dist.end(rank) - owned_begin);
122
123 // Global id of an owned slot.
124 auto id_of = [&](std::size_t slot) { return owned_begin + static_cast<std::int64_t>(slot); };
125
126 // Representative of every id this rank owns. Edge endpoints always name a
127 // current representative, but ids absorbed in an earlier round do not, so this
128 // has to be carried forward separately from the per-round hooks below. It costs
129 // an extra exchange per round, so it is only maintained when asked for.
130 std::vector<std::int64_t> label;
131 if (owned_labels)
132 {
133 label.resize(num_owned);
134 std::iota(label.begin(), label.end(), owned_begin);
135 }
136
137 while (true)
138 {
139 // Drop edges whose endpoints have already merged; they are interior to a
140 // component and can never join the forest.
141 edges.erase(std::remove_if(edges.begin(),
142 edges.end(),
143 [](const DistributedEdge & e) { return e.u == e.v; }),
144 edges.end());
145
146 std::int64_t local_active = static_cast<std::int64_t>(edges.size()), global_active = 0;
147 MPI_Allreduce(&local_active, &global_active, 1, MPI_INT64_T, MPI_SUM, comm);
148 if (global_active == 0)
149 break;
150
151 // Offer every remaining edge to the components at both of its ends.
152 std::vector<Proposal> proposals;
153 proposals.reserve(2 * edges.size());
154 for (const auto & e : edges)
155 {
156 proposals.push_back({e.u, e.wu, e.wv, e.v, e.origin_rank, e.origin_index});
157 proposals.push_back({e.v, e.wu, e.wv, e.u, e.origin_rank, e.origin_index});
158 }
159
160 std::vector<int> counts;
161 auto grouped = groupByDestination(
162 proposals, nprocs, [&](const Proposal & p) { return dist.owner(p.component); }, counts);
163 // The ungrouped copy is dead once grouped; release it before the exchange
164 // allocates its own buffers, since this is the largest allocation per round.
165 proposals.clear();
166 proposals.shrink_to_fit();
167 const auto received = allToAll(grouped, counts, comm);
168 grouped.clear();
169 grouped.shrink_to_fit();
170
171 // Each component keeps its lightest incident edge. Distinct weights make the
172 // choice unique, so the forest matches a serial Kruskal pass exactly.
173 std::vector<const Proposal *> best(num_owned, nullptr);
174 for (const auto & p : received)
175 {
176 const auto slot = static_cast<std::size_t>(p.component - owned_begin);
177 if (!best[slot] || p.wu < best[slot]->wu || (p.wu == best[slot]->wu && p.wv < best[slot]->wv))
178 best[slot] = &p;
179 }
180
181 // Tell the owning rank about every edge that was picked. An edge chosen by
182 // both of its endpoints arrives twice; marking it is idempotent.
183 if (selected)
184 {
185 std::vector<Proposal> picks;
186 picks.reserve(num_owned);
187 for (const auto slot : index_range(best))
188 if (best[slot])
189 picks.push_back(*best[slot]);
190
191 std::vector<int> pick_counts;
192 const auto pick_grouped = groupByDestination(
193 picks, nprocs, [](const Proposal & p) { return p.origin_rank; }, pick_counts);
194 for (const auto & p : allToAll(pick_grouped, pick_counts, comm))
195 selected->push_back(p.origin_index);
196 }
197
198 // Hook each component onto the component at the far end of its chosen edge.
199 // A component that received no offer is already a root and hooks to itself.
200 std::vector<std::int64_t> hook(num_owned);
201 for (const auto slot : index_range(best))
202 hook[slot] = best[slot] ? best[slot]->partner : id_of(slot);
203
204 // Two components that chose the same edge point at each other. Root that pair
205 // at the smaller id; every other chain terminates at such a pair, because the
206 // chosen weights strictly decrease along a chain.
207 const auto partner_hook = fetchLabels(hook, hook, dist, comm);
208 for (const auto slot : index_range(hook))
209 if (partner_hook[slot] == id_of(slot))
210 hook[slot] = std::min(id_of(slot), hook[slot]);
211
212 // Pointer-jump until every component points directly at its root.
213 while (true)
214 {
215 const auto grandparent = fetchLabels(hook, hook, dist, comm);
216 std::int64_t local_changed = 0;
217 for (const auto slot : index_range(hook))
218 if (hook[slot] != grandparent[slot])
219 {
220 hook[slot] = grandparent[slot];
221 local_changed = 1;
222 }
223
224 std::int64_t global_changed = 0;
225 MPI_Allreduce(&local_changed, &global_changed, 1, MPI_INT64_T, MPI_MAX, comm);
226 if (global_changed == 0)
227 break;
228 }
229
230 // Carry every id's representative through this round's merge.
231 if (owned_labels)
232 label = fetchLabels(label, hook, dist, comm);
233
234 // Relabel this rank's edges onto the merged components.
235 std::vector<std::int64_t> endpoints;
236 endpoints.reserve(2 * edges.size());
237 for (const auto & e : edges)
238 {
239 endpoints.push_back(e.u);
240 endpoints.push_back(e.v);
241 }
242 const auto relabelled = fetchLabels(endpoints, hook, dist, comm);
243 for (const auto i : index_range(edges))
244 {
245 edges[i].u = relabelled[2 * i];
246 edges[i].v = relabelled[2 * i + 1];
247 }
248 }
249
250 if (owned_labels)
251 *owned_labels = std::move(label);
252}
253}
254
255#endif
Block distribution of a dense global index range [0, n) over the ranks of a communicator: rank r owns...
std::int64_t begin(int rank) const
int _nprocs
Number of ranks the ids are distributed over.
std::int64_t end(int rank) const
int owner(std::int64_t g) const
Owning rank of global id g, which must lie in [0, n).
BlockDistribution(std::int64_t n, int nprocs)
Utilities for converting between vector(s) of libMesh Points and MFEM Vector(s).
void distributedSpanningForest(std::vector< DistributedEdge > &edges, std::int64_t num_labels, MPI_Comm comm, std::vector< int > *selected, std::vector< std::int64_t > *owned_labels=nullptr)
Distributed Boruvka over the edges held collectively by the ranks of comm.
std::vector< std::int64_t > fetchLabels(const std::vector< std::int64_t > &queries, const std::vector< std::int64_t > &owned_labels, const BlockDistribution &dist, MPI_Comm comm)
Final component representative of each id in queries, looked up from the distributed label array.
std::vector< T > groupByDestination(const std::vector< T > &items, int nprocs, DestinationFn destination, std::vector< int > &counts)
Group items by the destination rank returned by destination, so they can be handed to allToAll.
std::vector< T > allToAll(const std::vector< T > &send, const std::vector< int > &send_counts, MPI_Comm comm)
MPI_Alltoallv over a trivially copyable payload.
An undirected edge of the distributed vertex graph.
int origin_rank
Rank that owns this edge and its index in that rank's edge array, so a selected edge can be routed ba...
std::int64_t u
Current component label of each endpoint.
std::int64_t wu
Weight: original canonical endpoint ids, wu < wv. Unique across the graph.