67 const std::vector<std::int64_t> & owned_labels,
72 MPI_Comm_size(comm, &nprocs);
73 MPI_Comm_rank(comm, &rank);
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)};
79 std::vector<int> counts;
81 outgoing, nprocs, [&](
const LabelQuery & q) {
return dist.
owner(q.id); }, counts);
83 auto received =
allToAll(grouped, counts, comm);
87 const std::int64_t owned_begin = dist.
begin(rank);
88 for (
auto & q : received)
89 q.label = owned_labels[q.id - owned_begin];
91 std::vector<int> answer_counts;
93 received, nprocs, [](
const LabelQuery & q) {
return q.origin_rank; }, answer_counts);
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;
103 std::int64_t num_labels,
105 std::vector<int> * selected,
106 std::vector<std::int64_t> * owned_labels)
109 MPI_Comm_size(comm, &nprocs);
110 MPI_Comm_rank(comm, &rank);
115 owned_labels->clear();
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);
124 auto id_of = [&](std::size_t slot) {
return owned_begin +
static_cast<std::int64_t
>(slot); };
130 std::vector<std::int64_t> label;
133 label.resize(num_owned);
134 std::iota(label.begin(), label.end(), owned_begin);
141 edges.erase(std::remove_if(edges.begin(),
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)
152 std::vector<Proposal> proposals;
153 proposals.reserve(2 * edges.size());
154 for (
const auto & e : edges)
160 std::vector<int> counts;
162 proposals, nprocs, [&](
const Proposal & p) {
return dist.
owner(p.component); }, counts);
166 proposals.shrink_to_fit();
167 const auto received =
allToAll(grouped, counts, comm);
169 grouped.shrink_to_fit();
173 std::vector<const Proposal *> best(num_owned,
nullptr);
174 for (
const auto & p : received)
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))
185 std::vector<Proposal> picks;
186 picks.reserve(num_owned);
187 for (
const auto slot : index_range(best))
189 picks.push_back(*best[slot]);
191 std::vector<int> pick_counts;
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);
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);
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]);
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])
220 hook[slot] = grandparent[slot];
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)
235 std::vector<std::int64_t> endpoints;
236 endpoints.reserve(2 * edges.size());
237 for (
const auto & e : edges)
239 endpoints.push_back(e.
u);
240 endpoints.push_back(e.
v);
242 const auto relabelled =
fetchLabels(endpoints, hook, dist, comm);
243 for (
const auto i : index_range(edges))
245 edges[i].u = relabelled[2 * i];
246 edges[i].v = relabelled[2 * i + 1];
251 *owned_labels = std::move(label);