8#include <kernel/adjacency/adjactor.hpp>
9#include <kernel/adjacency/graph.hpp>
10#include <kernel/util/dist.hpp>
51 static constexpr int max_bid = std::numeric_limits<int>::max();
56 const int rank = comm.
rank();
57 const Index num_nodes = graph.get_num_nodes_domain();
60 std::vector<Index> stack;
63 std::vector<Index> colors(num_nodes, uncolored);
66 Index current_color = 0;
72 auto first_uncolored = std::find_if(colors.begin(), colors.end(), [](
Index c) { return c == uncolored; });
73 bool all_colored = first_uncolored == colors.end();
77 const int bid = all_colored ? max_bid : rank;
93 const auto idx = (
Index)std::distance(colors.begin(), first_uncolored);
95 colors[idx] = current_color;
101 std::vector<int> ranks;
102 std::vector<Index> nodes;
105 while(!stack.empty())
107 const Index current_node = stack.back();
112 const int node_rank = get_rank_of_node(first_node_of_rank, neighbor);
117 const Index local_neighbor = neighbor - first_node_of_rank[std::size_t(node_rank)];
119 if(node_rank == rank)
123 if(colors[local_neighbor] == uncolored)
125 stack.push_back(local_neighbor);
126 colors[local_neighbor] = current_color;
133 ranks.push_back(node_rank);
134 nodes.push_back(local_neighbor);
140 std::vector<Index> visited_by_others =
exchange_nodes(comm, ranks, nodes);
147 for(
Index node : visited_by_others)
149 if(colors[node] == uncolored)
152 stack.push_back(node);
153 colors[node] = current_color;
160 const int done = stack.empty() ? 1 : 0;
178 const Index num_colors = current_color;
183 std::vector<Index> color_counts(num_colors, 0);
184 for(
Index color : colors)
186 color_counts[color]++;
192 for(
Index i(1); i < num_colors; i++)
194 dom_ptr[i] = color_counts[i - 1];
199 for(
Index color(0); color < num_colors; color++)
201 for(
Index node(0); node < num_nodes; node++)
203 if(colors[node] == color)
216 static std::vector<Index>
220 std::vector<int> sendcounts(std::size_t(comm.
size()), 0);
221 std::vector<int> receivecounts(std::size_t(comm.
size()), 0);
223 for(
int rank : ranks)
225 sendcounts[std::size_t(rank)]++;
228 comm.
alltoall(sendcounts.data(), 1, receivecounts.data(), 1);
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);
237 const std::size_t sendbuffer_size(ranks.size());
238 std::vector<Index> sendbuffer(sendbuffer_size, 0);
240 std::vector<int> write_offsets = senddisplacements;
241 for(
Index i(0); i < ranks.size(); i++)
243 const auto rank = std::size_t(ranks[i]);
244 sendbuffer[std::size_t(write_offsets[rank])] = nodes[i];
245 write_offsets[rank]++;
249 const std::size_t receivebuffer_size = std::size_t(receivedisplacements.back() + receivecounts.back());
250 std::vector<Index> receivebuffer(receivebuffer_size, 0);
256 senddisplacements.data(),
257 receivebuffer.data(),
258 receivecounts.data(),
259 receivedisplacements.data());
261 return receivebuffer;
264 static int get_rank_of_node(
const std::vector<Index>& first_of_rank,
Index node)
266 int rank = (int)first_of_rank.size() - 1;
267 for(
auto it = first_of_rank.rbegin(); it != first_of_rank.rend(); ++it, --rank)
274 XABORTM(
"FloodFill: Could not find rank for node");
#define XABORTM(msg)
Abortion macro definition with custom message.
Adjacency Graph implementation.
Index * get_domain_ptr()
Returns the domain pointer array.
Index * get_image_idx()
Returns the image node index array.
Wrapper for using range-based for loops with adjactors.
void allreduce(const void *sendbuf, void *recvbuf, std::size_t count, const Datatype &datatype, const Operation &op) const
Blocking All-Reduce.
int size() const
Returns the size of this communicator.
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.
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.
int rank() const
Returns the rank of this process in this communicator.
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
static Adjacency::Graph run(const std::vector< Index > &first_node_of_rank, const Adjacency::Graph &graph, Dist::Comm &comm)
Distributed flood fill.
const Operation op_min(MPI_MIN)
Operation wrapper for MPI_MIN.
Global linear algebra namespace.
std::uint64_t Index
Index data type.