FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
flood_fill.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
8#include <kernel/adjacency/adjactor.hpp>
9#include <kernel/adjacency/graph.hpp>
10#include <kernel/util/dist.hpp>
11
12#include <algorithm>
13#include <iterator>
14#include <limits>
15#include <numeric>
16
17namespace FEAT::Global
18{
21 {
22 public:
35 static Adjacency::Graph
36 run(const std::vector<Index>& first_node_of_rank, const Adjacency::Graph& graph, Dist::Comm& comm)
37 {
38 // This distributed flood fill works essentially like a normal flood fill with two exceptions:
39 // - When starting a new "flood" the seed node is selected by the smallest rank with uncolored nodes left
40 // - When encountering a node of another rank, that node is added to that ranks stack
41 // For each seed the flood fill is iterated until all ranks stacks are empty.
42 // The flood fill is done if there are no more uncolored nodes on any rank.
43
44 // NOTE(mmuegge): There is probably an algorithm that scales better,
45 // which performs a normal flood fill on each rank at the same time
46 // and the reconciles the different colors used afterwards.
47
48 // The starting rank for each flood is selected by a bidding proccess,
49 // where each ranks either submits its own rank or max_bid.
50 // The rank matching the smallest bid starts the next flood.
51 static constexpr int max_bid = std::numeric_limits<int>::max();
52
53 // Sentinel value for uncolored nodes.
54 static constexpr Index uncolored(~Index(0));
55
56 const int rank = comm.rank();
57 const Index num_nodes = graph.get_num_nodes_domain();
58
59 // Stack of nodes to be processed on this rank
60 std::vector<Index> stack;
61
62 // Colors for this ranks nodes
63 std::vector<Index> colors(num_nodes, uncolored);
64
65 // Color of current flood.
66 Index current_color = 0;
67
68 while(true)
69 {
70 // Find rank that starts next flood fill
71 // If no such rank exists, break
72 auto first_uncolored = std::find_if(colors.begin(), colors.end(), [](Index c) { return c == uncolored; });
73 bool all_colored = first_uncolored == colors.end();
74
75 // Submit either our rank or max_bid as bid.
76 // Smallest non-max_bid bid starts the next flood.
77 const int bid = all_colored ? max_bid : rank;
78 int winner(0);
79 comm.allreduce(&bid, &winner, 1, Dist::op_min);
80
81 if(winner == max_bid)
82 {
83 // All ranks submitted max_bid.
84 // There is thus no rank remaining that could start a flood fill.
85 // We are done.
86 break;
87 }
88
89 if(winner == rank)
90 {
91 // We won the bid
92 // Initialize our stack with first uncolored node
93 const auto idx = (Index)std::distance(colors.begin(), first_uncolored);
94 stack.push_back(idx);
95 colors[idx] = current_color;
96 }
97
98 while(true)
99 {
100 // Buffers for communicating foreign nodes
101 std::vector<int> ranks;
102 std::vector<Index> nodes;
103
104 // Process local stack
105 while(!stack.empty())
106 {
107 const Index current_node = stack.back();
108 stack.pop_back();
109
110 for(const Index neighbor : Adjacency::ImageRange(graph, current_node))
111 {
112 const int node_rank = get_rank_of_node(first_node_of_rank, neighbor);
113
114 // We assume that the graph gives us "global" indices
115 // We have to convert these to rank-local indices first before we
116 // can do anything useful with them
117 const Index local_neighbor = neighbor - first_node_of_rank[std::size_t(node_rank)];
118
119 if(node_rank == rank)
120 {
121 // We own this node
122 // If it has not already been visited add it to the stack
123 if(colors[local_neighbor] == uncolored)
124 {
125 stack.push_back(local_neighbor);
126 colors[local_neighbor] = current_color;
127 }
128 }
129 else
130 {
131 // Some other rank owns this node.
132 // Communicate that connection to the other rank later
133 ranks.push_back(node_rank);
134 nodes.push_back(local_neighbor);
135 }
136 }
137 }
138
139 // Send and receive from other ranks
140 std::vector<Index> visited_by_others = exchange_nodes(comm, ranks, nodes);
141
142 // Reset buffers for next iteration
143 ranks.clear();
144 nodes.clear();
145
146 // Add newly visited nodes to our stack
147 for(Index node : visited_by_others)
148 {
149 if(colors[node] == uncolored)
150 {
151 // Node has not been visited on this rank
152 stack.push_back(node);
153 colors[node] = current_color;
154 }
155 }
156
157 // Determine if all ranks are done
158 // If so, break
159
160 const int done = stack.empty() ? 1 : 0;
161 int all_done(0);
162 comm.allreduce(&done, &all_done, 1, Dist::op_min);
163
164 if(all_done == 1)
165 {
166 break;
167 }
168 }
169
170 // Go to next color
171 current_color++;
172 }
173
174 // Everything is colored.
175 // Construct output color -> nodes graph
176
177 // current_color is the last color used when all ranks determined they were done
178 const Index num_colors = current_color;
179
180 Adjacency::Graph result(num_colors, num_nodes, num_nodes);
181
182 // Count occurrences of each color to determine non-zeros for each color
183 std::vector<Index> color_counts(num_colors, 0);
184 for(Index color : colors)
185 {
186 color_counts[color]++;
187 }
188
189 // Write domain ptr using color_counts
190 Index* dom_ptr = result.get_domain_ptr();
191 dom_ptr[0] = 0;
192 for(Index i(1); i < num_colors; i++)
193 {
194 dom_ptr[i] = color_counts[i - 1];
195 }
196
197 // Fill in nodes
198 Index* idx_ptr = result.get_image_idx();
199 for(Index color(0); color < num_colors; color++)
200 {
201 for(Index node(0); node < num_nodes; node++)
202 {
203 if(colors[node] == color)
204 {
205 *idx_ptr = node;
206 idx_ptr++;
207 }
208 }
209 }
210
211 return result;
212 }
213
214 private:
216 static std::vector<Index>
217 exchange_nodes(Dist::Comm& comm, const std::vector<int>& ranks, const std::vector<Index>& nodes)
218 {
219 // Build sendcounts and receivecounts
220 std::vector<int> sendcounts(std::size_t(comm.size()), 0);
221 std::vector<int> receivecounts(std::size_t(comm.size()), 0);
222
223 for(int rank : ranks)
224 {
225 sendcounts[std::size_t(rank)]++;
226 }
227
228 comm.alltoall(sendcounts.data(), 1, receivecounts.data(), 1);
229
230 // Build displacements
231 std::vector<int> senddisplacements;
232 std::vector<int> receivedisplacements;
233 std::exclusive_scan(sendcounts.begin(), sendcounts.end(), std::back_inserter(senddisplacements), 0);
234 std::exclusive_scan(receivecounts.begin(), receivecounts.end(), std::back_inserter(receivedisplacements), 0);
235
236 // Build sendbuffer
237 const std::size_t sendbuffer_size(ranks.size());
238 std::vector<Index> sendbuffer(sendbuffer_size, 0);
239
240 std::vector<int> write_offsets = senddisplacements;
241 for(Index i(0); i < ranks.size(); i++)
242 {
243 const auto rank = std::size_t(ranks[i]);
244 sendbuffer[std::size_t(write_offsets[rank])] = nodes[i];
245 write_offsets[rank]++;
246 }
247
248 // Allocate receivebuffer
249 const std::size_t receivebuffer_size = std::size_t(receivedisplacements.back() + receivecounts.back());
250 std::vector<Index> receivebuffer(receivebuffer_size, 0);
251
252 // Exchange nodes between all ranks
253 comm.alltoallv(
254 sendbuffer.data(),
255 sendcounts.data(),
256 senddisplacements.data(),
257 receivebuffer.data(),
258 receivecounts.data(),
259 receivedisplacements.data());
260
261 return receivebuffer;
262 }
263
264 static int get_rank_of_node(const std::vector<Index>& first_of_rank, Index node)
265 {
266 int rank = (int)first_of_rank.size() - 1;
267 for(auto it = first_of_rank.rbegin(); it != first_of_rank.rend(); ++it, --rank)
268 {
269 if(*it <= node)
270 {
271 return rank;
272 }
273 }
274 XABORTM("FloodFill: Could not find rank for node");
275 }
276 };
277
278} // namespace FEAT::Global
#define XABORTM(msg)
Abortion macro definition with custom message.
Definition: assertion.hpp:192
Adjacency Graph implementation.
Definition: graph.hpp:34
Index * get_domain_ptr()
Returns the domain pointer array.
Definition: graph.hpp:359
Index * get_image_idx()
Returns the image node index array.
Definition: graph.hpp:374
Wrapper for using range-based for loops with adjactors.
Definition: adjactor.hpp:517
Communicator class.
Definition: dist.hpp:1657
void allreduce(const void *sendbuf, void *recvbuf, std::size_t count, const Datatype &datatype, const Operation &op) const
Blocking All-Reduce.
Definition: dist.cpp:658
int size() const
Returns the size of this communicator.
Definition: dist.hpp:1814
void alltoallv(const void *sendbuf, const int *sendcounts, const int *sdispls, const Datatype &sendtype, void *recvbuf, const int *recvcounts, const int *rdispls, const Datatype &recvtype) const
Blocking All-to-All Scatter/Gather.
Definition: dist.cpp:636
void alltoall(const void *sendbuf, std::size_t sendcount, const Datatype &sendtype, void *recvbuf, std::size_t recvcount, const Datatype &recvtype) const
Blocking All-to-All Scatter/Gather.
Definition: dist.cpp:618
int rank() const
Returns the rank of this process in this communicator.
Definition: dist.hpp:1802
Distributed flood fill.
Definition: flood_fill.hpp:21
static std::vector< Index > exchange_nodes(Dist::Comm &comm, const std::vector< int > &ranks, const std::vector< Index > &nodes)
alltoallv for exchanging nodes that have been visited in the last flood fill iteration
Definition: flood_fill.hpp:217
static Adjacency::Graph run(const std::vector< Index > &first_node_of_rank, const Adjacency::Graph &graph, Dist::Comm &comm)
Distributed flood fill.
Definition: flood_fill.hpp:36
const Operation op_min(MPI_MIN)
Operation wrapper for MPI_MIN.
Definition: dist.hpp:575
Global linear algebra namespace.
std::uint64_t Index
Index data type.