Skip to content

Commit

Permalink
fix: missing nodes[] guard when rearranging the graph
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSeemaier committed Apr 29, 2024
1 parent 5f87e87 commit 768d70e
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 24 deletions.
9 changes: 5 additions & 4 deletions kaminpar-shm/coarsening/clustering/legacy_lp_clusterer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ class LegacyLPClusteringImpl final
}

void compute_clustering(StaticArray<NodeID> &clustering, const CSRGraph &graph, bool) {
allocate(graph.n(), graph.n());
allocate_cluster_weights(graph.n());
KASSERT(clustering.size() >= graph.n(), "preallocated clustering array is too small");

init_clusters_ref(clustering);
initialize(&graph, graph.n());
Base::allocate(graph.n(), graph.n());
LegacyOwnedRelaxedClusterWeightVector::allocate_cluster_weights(graph.n());
LegacyNonatomicClusterVectorRef::init_clusters_ref(clustering);
Base::initialize(&graph, graph.n());

for (int iteration = 0; iteration < _lp_ctx.num_iterations; ++iteration) {
SCOPED_TIMER("Iteration", std::to_string(iteration));
Expand Down
27 changes: 21 additions & 6 deletions kaminpar-shm/datastructures/csr_graph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,34 @@ bool validate_graph(
}

for (const NodeID u : graph.nodes()) {
for (const auto [e, v] : graph.neighbors(u)) {
for (EdgeID e = graph.first_edge(u); e < graph.first_invalid_edge(u); ++e) {
if (e >= graph.m()) {
LOG_WARNING << "Edge " << e << " of " << u << " is out-of-graph";
return false;
}

const NodeID v = graph.edge_target(e);

if (v >= graph.n()) {
LOG_WARNING << "Neighbor " << v << " of " << u << " is out-of-graph";
return false;
}

if (u == v) {
LOG_WARNING << "Self-loop at " << u;
LOG_WARNING << "Self-loop at " << u << ": " << e << " --> " << v;
return false;
}

bool found_reverse = false;
for (const auto [e_prime, u_prime] : graph.neighbors(v)) {
for (EdgeID e_prime = graph.first_edge(v); e_prime < graph.first_invalid_edge(v); ++e_prime) {
if (e_prime >= graph.m()) {
LOG_WARNING << "Edge " << e_prime << " of " << v << " is out-of-graph";
std::exit(1);
return false;
}

const NodeID u_prime = graph.edge_target(e_prime);

if (u_prime >= graph.n()) {
LOG_WARNING << "Neighbor " << u_prime << " of neighbor " << v << " of " << u
<< " is out-of-graph";
Expand All @@ -78,9 +93,9 @@ bool validate_graph(
}

if (graph.edge_weight(e) != graph.edge_weight(e_prime)) {
LOG_WARNING << "Weight of edge " << e << " (" << graph.edge_weight(e)
<< ") differs from the weight of its reverse edge " << e_prime << " ("
<< graph.edge_weight(e_prime) << ")";
LOG_WARNING << "Weight of edge " << e << " (" << graph.edge_weight(e) << ") " //
<< "differs from the weight of its reverse edge " << e_prime << " " //
<< "(" << graph.edge_weight(e_prime) << ")"; //
return false;
}

Expand Down
1 change: 0 additions & 1 deletion kaminpar-shm/graphutils/permutator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ Graph rearrange_by_degree_buckets(CSRGraph &old_graph) {
std::move(nodes), std::move(edges), std::move(node_weights), std::move(edge_weights), true
));
new_graph.set_permutation(std::move(node_permutations.old_to_new));

return new_graph;
}

Expand Down
2 changes: 2 additions & 0 deletions kaminpar-shm/graphutils/permutator.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ void build_permuted_graph(
new_node_weights[u] = old_node_weights[old_u];
}
});

parallel::prefix_sum(new_nodes.begin(), new_nodes.end(), new_nodes.begin());
new_nodes.back() = n > 0 ? new_nodes[n - 1] : 0;

// Build p_edges, p_edge_weights
tbb::parallel_for<GraphNodeID>(0, n, [&](const GraphNodeID u) {
Expand Down
22 changes: 13 additions & 9 deletions kaminpar-shm/kaminpar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,19 @@ void KaMinPar::borrow_and_mutate_graph(
StaticArray<EdgeWeight> edge_weights =
(adjwgt == nullptr) ? StaticArray<EdgeWeight>(0) : StaticArray<EdgeWeight>(m, adjwgt);

_was_rearranged = false;
_graph_ptr = std::make_unique<Graph>(std::make_unique<CSRGraph>(
auto csr_graph = std::make_unique<CSRGraph>(
std::move(nodes), std::move(edges), std::move(node_weights), std::move(edge_weights), false
));
);
KASSERT(shm::debug::validate_graph(*csr_graph), "invalid input graph", assert::heavy);
set_graph(Graph(std::move(csr_graph)));
}

void KaMinPar::copy_graph(
const NodeID n, EdgeID *xadj, NodeID *adjncy, NodeWeight *vwgt, EdgeWeight *adjwgt
const NodeID n,
const EdgeID *const xadj,
const NodeID *const adjncy,
const NodeWeight *const vwgt,
const EdgeWeight *const adjwgt
) {
SCOPED_HEAP_PROFILER("Copy graph");
SCOPED_TIMER("IO");
Expand Down Expand Up @@ -160,10 +165,11 @@ void KaMinPar::copy_graph(
}
});

_was_rearranged = false;
_graph_ptr = std::make_unique<Graph>(std::make_unique<CSRGraph>(
auto csr_graph = std::make_unique<CSRGraph>(
std::move(nodes), std::move(edges), std::move(node_weights), std::move(edge_weights), false
));
);
KASSERT(shm::debug::validate_graph(*csr_graph), "invalid input graph", assert::heavy);
set_graph(Graph(std::move(csr_graph)));
}

void KaMinPar::set_graph(Graph graph) {
Expand All @@ -176,8 +182,6 @@ void KaMinPar::reseed(int seed) {
}

EdgeWeight KaMinPar::compute_partition(const BlockID k, BlockID *partition) {
Logger::set_quiet_mode(_output_level == OutputLevel::QUIET);

cio::print_kaminpar_banner();
cio::print_build_identifier();
cio::print_build_datatypes<NodeID, EdgeID, NodeWeight, EdgeWeight>();
Expand Down
8 changes: 4 additions & 4 deletions kaminpar-shm/kaminpar.h
Original file line number Diff line number Diff line change
Expand Up @@ -525,10 +525,10 @@ class KaMinPar {
*/
void copy_graph(
shm::NodeID n,
shm::EdgeID *const xadj,
shm::NodeID *const adjncy,
shm::NodeWeight *const vwgt,
shm::EdgeWeight *const adjwgt
const shm::EdgeID *const xadj,
const shm::NodeID *const adjncy,
const shm::NodeWeight *const vwgt,
const shm::EdgeWeight *const adjwgt
);

/*!
Expand Down

0 comments on commit 768d70e

Please sign in to comment.