-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add example coarsener, configure with --c-algorithm=example
- Loading branch information
1 parent
c40cdb7
commit bb46b81
Showing
5 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include "kaminpar-shm/coarsening/example_coarsener.h" | ||
|
||
#include "kaminpar-shm/coarsening/contraction/cluster_contraction.h" | ||
#include "kaminpar-shm/coarsening/max_cluster_weights.h" | ||
#include "kaminpar-shm/factories.h" | ||
#include "kaminpar-shm/kaminpar.h" | ||
|
||
#include "kaminpar-common/assert.h" | ||
|
||
namespace kaminpar::shm { | ||
ExampleCoarsener::ExampleCoarsener(const Context &ctx, const PartitionContext &p_ctx) | ||
: _clustering_algorithm(factory::create_clusterer(ctx)), | ||
_c_ctx(ctx.coarsening), | ||
_p_ctx(p_ctx) {} | ||
|
||
void ExampleCoarsener::initialize(const Graph *graph) { | ||
_hierarchy.clear(); | ||
_input_graph = graph; | ||
} | ||
|
||
bool ExampleCoarsener::coarsen() { | ||
const NodeWeight total_node_weight = current().total_node_weight(); | ||
const NodeID prev_n = current().n(); | ||
|
||
// Compute the graph clustering via label propagation | ||
StaticArray<NodeID> clustering(prev_n, static_array::noinit); | ||
_clustering_algorithm->set_max_cluster_weight( | ||
compute_max_cluster_weight<NodeWeight>(_c_ctx, _p_ctx, prev_n, total_node_weight) | ||
); | ||
_clustering_algorithm->compute_clustering(clustering, current(), false); | ||
|
||
// Construct the coarse graph by contracting the current graph | ||
std::unique_ptr<CoarseGraph> coarsened = | ||
contract_clustering(current(), std::move(clustering), _c_ctx.contraction); | ||
_hierarchy.push_back(std::move(coarsened)); | ||
// Coarse graph: coarsened->get() | ||
|
||
// Determine whether the coarsening process should continue | ||
const NodeID next_n = current().n(); | ||
const bool converged = (1.0 - 1.0 * next_n / prev_n) <= _c_ctx.convergence_threshold; | ||
return !converged; | ||
} | ||
|
||
PartitionedGraph ExampleCoarsener::uncoarsen(PartitionedGraph &&p_graph) { | ||
// Graph partition of the coarse graph that must be projected onto the finer graph: | ||
const BlockID p_graph_k = p_graph.k(); | ||
const StaticArray<BlockID> p_graph_partition = p_graph.take_raw_partition(); | ||
|
||
// Current coarse graph | ||
std::unique_ptr<CoarseGraph> coarsened = std::move(_hierarchy.back()); | ||
KASSERT(&coarsened->get() == &p_graph.graph()); | ||
|
||
// Pop coarse graph from the hierarchy: this destroys the graph wrapped in p_graph, i.e., p_graph | ||
// may no longer be used | ||
_hierarchy.pop_back(); | ||
const NodeID next_n = current().n(); | ||
|
||
StaticArray<BlockID> partition(next_n); | ||
coarsened->project(p_graph_partition, partition); | ||
return {current(), p_graph_k, std::move(partition)}; | ||
} | ||
} // namespace kaminpar::shm | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#pragma once | ||
|
||
#include "kaminpar-shm/coarsening/clusterer.h" | ||
#include "kaminpar-shm/coarsening/coarsener.h" | ||
#include "kaminpar-shm/coarsening/contraction/cluster_contraction.h" | ||
#include "kaminpar-shm/datastructures/graph.h" | ||
#include "kaminpar-shm/datastructures/partitioned_graph.h" | ||
#include "kaminpar-shm/kaminpar.h" | ||
|
||
namespace kaminpar::shm { | ||
class ExampleCoarsener : public Coarsener { | ||
public: | ||
ExampleCoarsener(const Context &ctx, const PartitionContext &p_ctx); | ||
|
||
ExampleCoarsener(const ExampleCoarsener &) = delete; | ||
ExampleCoarsener &operator=(const ExampleCoarsener) = delete; | ||
|
||
ExampleCoarsener(ExampleCoarsener &&) = delete; | ||
ExampleCoarsener &operator=(ExampleCoarsener &&) = delete; | ||
|
||
void initialize(const Graph *graph) final; | ||
|
||
bool coarsen() final; | ||
PartitionedGraph uncoarsen(PartitionedGraph &&p_graph) final; | ||
|
||
[[nodiscard]] const Graph ¤t() const final { | ||
return _hierarchy.empty() ? *_input_graph : _hierarchy.back()->get(); | ||
} | ||
|
||
[[nodiscard]] std::size_t level() const final { | ||
return _hierarchy.size(); | ||
} | ||
|
||
private: | ||
std::unique_ptr<CoarseGraph> pop_hierarchy(PartitionedGraph &&p_graph); | ||
|
||
const CoarseningContext &_c_ctx; | ||
const PartitionContext &_p_ctx; | ||
|
||
const Graph *_input_graph; | ||
std::vector<std::unique_ptr<CoarseGraph>> _hierarchy; | ||
|
||
std::unique_ptr<Clusterer> _clustering_algorithm; | ||
}; | ||
} // namespace kaminpar::shm | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters