https://mooseframework.inl.gov
Loading...
Searching...
No Matches
MFEMDistributedGraph.h
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
12#pragma once
13
15
16#include <algorithm>
17#include <cstdint>
18#include <numeric>
19#include <vector>
20
21#include <mpi.h>
22
23namespace Moose::MFEM
24{
25
32{
33public:
34 BlockDistribution(std::int64_t n, int nprocs);
35
37 int owner(std::int64_t g) const;
38 std::int64_t begin(int rank) const;
39 std::int64_t end(int rank) const;
40 std::int64_t size() const { return _n; }
41
42private:
43 std::int64_t _n;
46};
47
52template <typename T, typename DestinationFn>
53std::vector<T>
54groupByDestination(const std::vector<T> & items,
55 int nprocs,
56 DestinationFn destination,
57 std::vector<int> & counts)
58{
59 // Evaluate the destination once per item and remember it: it can be a binary
60 // search over the splitters, which the placement pass would otherwise repeat.
61 std::vector<int> destinations(items.size());
62 counts.assign(nprocs, 0);
63 for (const auto i : index_range(items))
64 counts[destinations[i] = destination(items[i])]++;
65
66 std::vector<int> offset(nprocs, 0);
67 std::partial_sum(counts.begin(), counts.end() - 1, offset.begin() + 1);
68
69 std::vector<T> grouped(items.size());
70 for (const auto i : index_range(items))
71 grouped[offset[destinations[i]]++] = items[i];
72 return grouped;
73}
74
80template <typename T>
81std::vector<T>
82allToAll(const std::vector<T> & send, const std::vector<int> & send_counts, MPI_Comm comm)
83{
84 int nprocs;
85 MPI_Comm_size(comm, &nprocs);
86
87 std::vector<int> recv_counts(nprocs);
88 MPI_Alltoall(send_counts.data(), 1, MPI_INT, recv_counts.data(), 1, MPI_INT, comm);
89
90 std::vector<int> send_displs(nprocs, 0), recv_displs(nprocs, 0);
91 std::partial_sum(send_counts.begin(), send_counts.end() - 1, send_displs.begin() + 1);
92 std::partial_sum(recv_counts.begin(), recv_counts.end() - 1, recv_displs.begin() + 1);
93
94 std::vector<T> recv(static_cast<std::size_t>(recv_displs.back()) + recv_counts.back());
95
96 // A contiguous datatype of sizeof(T) bytes keeps the MPI counts in elements
97 // rather than bytes, so a large exchange cannot overflow the int counts.
98 MPI_Datatype item_type;
99 MPI_Type_contiguous(sizeof(T), MPI_BYTE, &item_type);
100 MPI_Type_commit(&item_type);
101 MPI_Alltoallv(send.data(),
102 send_counts.data(),
103 send_displs.data(),
104 item_type,
105 recv.data(),
106 recv_counts.data(),
107 recv_displs.data(),
108 item_type,
109 comm);
110 MPI_Type_free(&item_type);
111
112 return recv;
113}
114
124{
126 std::int64_t wu, wv;
128 std::int64_t u, v;
132
134 bool operator<(const DistributedEdge & o) const { return (wu != o.wu) ? wu < o.wu : wv < o.wv; }
135};
136
156void distributedSpanningForest(std::vector<DistributedEdge> & edges,
157 std::int64_t num_labels,
158 MPI_Comm comm,
159 std::vector<int> * selected,
160 std::vector<std::int64_t> * owned_labels = nullptr);
161
167std::vector<std::int64_t> fetchLabels(const std::vector<std::int64_t> & queries,
168 const std::vector<std::int64_t> & owned_labels,
169 const BlockDistribution & dist,
170 MPI_Comm comm);
171}
172
173#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).
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...
bool operator<(const DistributedEdge &o) const
Total order on the (unique) weights, giving Kruskal's processing order.
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.