Skip to content

Commit

Permalink
kahypar coarsening
Browse files Browse the repository at this point in the history
  • Loading branch information
konpklr committed Jul 1, 2024
1 parent ce4fcca commit ebd2c69
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 98 deletions.
9 changes: 4 additions & 5 deletions mt-kahypar/datastructures/static_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ namespace mt_kahypar::ds {
* \param communities Community structure that should be contracted
*/
StaticGraph StaticGraph::contract(parallel::scalable_vector<HypernodeID>& communities, bool /*deterministic*/) {
std::cout << "contraction\n";
ASSERT(communities.size() == _num_nodes);

if ( !_tmp_contraction_buffer ) {
Expand Down Expand Up @@ -90,11 +89,11 @@ namespace mt_kahypar::ds {

// Remap community ids
tbb::parallel_for(ID(0), _num_nodes, [&](const HypernodeID& node) {
/*if ( nodeIsEnabled(node) ) {
if ( nodeIsEnabled(node) ) {
communities[node] = mapping_prefix_sum[communities[node]];
} else {
communities[node] = kInvalidHypernode;
}*/
}

// Reset tmp contraction buffer
if ( node < coarsened_num_nodes ) {
Expand All @@ -117,7 +116,7 @@ namespace mt_kahypar::ds {
const HypernodeID coarse_node = map_to_coarse_graph(node);
ASSERT(coarse_node < coarsened_num_nodes, V(coarse_node) << V(coarsened_num_nodes));
// Weight vector is atomic => thread-safe
node_weights[coarse_node].add_fetch(nodeWeight(node), std::memory_order_relaxed);
node_weights[coarse_node] += nodeWeight(node);
// Aggregate upper bound for number of incident nets of the contracted vertex
tmp_num_incident_edges[coarse_node] += nodeDegree(node);
});
Expand Down Expand Up @@ -508,4 +507,4 @@ namespace mt_kahypar::ds {
}, std::plus<>());
}

} // namespace
} // namespace
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
namespace mt_kahypar {

template<typename TypeTraits>
bool DeterministicMultilevelCoarsener<TypeTraits>::coarseningPassImpl(std::string input) {
bool DeterministicMultilevelCoarsener<TypeTraits>::coarseningPassImpl() {
auto& timer = utils::Utilities::instance().getTimer(_context.utility_id);
const auto pass_start_time = std::chrono::high_resolution_clock::now();
timer.start_timer("coarsening_pass", "Clustering");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ class DeterministicMultilevelCoarsener : public ICoarsener,
}
}

bool coarseningPassImpl(std::string input) override;
bool coarseningPassImpl() override;

bool shouldNotTerminateImpl() const override {
return Base::currentNumNodes() > _context.coarsening.contraction_limit;
Expand Down
26 changes: 7 additions & 19 deletions mt-kahypar/partition/coarsening/i_coarsener.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#pragma once

#include <string>
#include <fstream>

#include "include/libmtkahypartypes.h"

Expand All @@ -45,30 +44,19 @@ class ICoarsener {
ICoarsener & operator= (const ICoarsener &) = delete;
ICoarsener & operator= (ICoarsener &&) = delete;

void coarsen(std::string ipf) {
void coarsen() {
initialize();
bool should_continue = true;
int pass_nr = 0;
std::ifstream myfile;
myfile.open(ipf);
HypernodeID hn = 0;
while(myfile){
std::string nextline;
std::getline(myfile, nextline);
if(nextline.rfind("IP") == 0 || nextline.rfind(" ") == -1) break;
coarseningPassImpl(nextline);
}
myfile.close();

// Coarsening algorithms proceed in passes where each pass computes a clustering
// of the nodes and subsequently contracts it. Each pass induces one level of the
// hierarchy. The coarsening algorithms proceeds until the number of nodes equals
// a predefined contraction limit (!shouldNotTerminate) or the number of nodes could
// not be significantly reduced within one coarsening pass (should_continue).
/*while ( shouldNotTerminate() && should_continue ) {
while ( shouldNotTerminate() && should_continue ) {
/*timer.start_timer("coarsening pass " + pass_nr, "CoarseningPass" + pass_nr);*/
/*should_continue = coarseningPass();
}*/
should_continue = coarseningPass();
}
terminate();
}

Expand All @@ -77,11 +65,11 @@ class ICoarsener {
}

bool shouldNotTerminate() const {
return false/*shouldNotTerminateImpl()*/;
return shouldNotTerminateImpl();
}

bool coarseningPass() {
return coarseningPassImpl("");
return coarseningPassImpl();
}

void terminate() {
Expand Down Expand Up @@ -109,7 +97,7 @@ class ICoarsener {
private:
virtual void initializeImpl() = 0;
virtual bool shouldNotTerminateImpl() const = 0;
virtual bool coarseningPassImpl(std::string input) = 0;
virtual bool coarseningPassImpl() = 0;
virtual void terminateImpl() = 0;
virtual HypernodeID currentNumberOfNodesImpl() const = 0;
virtual mt_kahypar_hypergraph_t coarsestHypergraphImpl() = 0;
Expand Down
40 changes: 5 additions & 35 deletions mt-kahypar/partition/coarsening/multilevel_coarsener.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class MultilevelCoarsener : public ICoarsener,
return Base::currentNumNodes() > _context.coarsening.contraction_limit;
}

bool coarseningPassImpl(std::string input) override {
bool coarseningPassImpl() override {
HighResClockTimepoint round_start = std::chrono::high_resolution_clock::now();
Hypergraph& current_hg = Base::currentHypergraph();
DBG << V(_pass_nr)
Expand All @@ -150,15 +150,7 @@ class MultilevelCoarsener : public ICoarsener,
// Random shuffle vertices of current hypergraph
_current_vertices.resize(current_hg.initialNumNodes());
parallel::scalable_vector<HypernodeID> cluster_ids(current_hg.initialNumNodes());
std::vector<HypernodeID> ctmp(current_hg.initialNumNodes());
std::stringstream ss(input);
std::string word;
for(HypernodeID hn = 0; hn < current_hg.initialNumNodes(); hn++){
ss >> word;
cluster_ids[hn] = stoi(word);
ctmp[hn] = stoi(word);
}
/*tbb::parallel_for(ID(0), current_hg.initialNumNodes(), [&](const HypernodeID hn) {
tbb::parallel_for(ID(0), current_hg.initialNumNodes(), [&](const HypernodeID hn) {
ASSERT(hn < _current_vertices.size());
// Reset clustering
_current_vertices[hn] = hn;
Expand Down Expand Up @@ -218,32 +210,10 @@ class MultilevelCoarsener : public ICoarsener,
return false;
}
_progress_bar += (num_hns_before_pass - current_num_nodes);
*/

_timer.start_timer("contraction", "Contraction");
// Perform parallel contraction
std::cout << "c0: \n"; /*current_hg.nodeWeight(0).weights[0] << " " << current_hg.nodeWeight(0).weights[1] << "\n";*/
for(int i = 0; i < cluster_ids.size(); i++){
std::cout << cluster_ids[i] << " ";
}
_uncoarseningData.performMultilevelContraction(std::move(cluster_ids), true /* deterministic */, round_start);
Hypergraph& chg = Base::currentHypergraph();
//if(chg.initialNumNodes() == current_hg.initialNumNodes()) std::cout << 1/0 << "\n";
std::vector<HypernodeWeight> tmp(chg.initialNumNodes(), 0);
std::cout << "sizes: " << current_hg.initialNumNodes() << " " << chg.initialNumNodes() << "\n";
for(HypernodeID hn : current_hg.nodes()){
//if(current_hg.nodeWeight(hn).weights[1] == 0) std::cout << 1/0;
}
std::cout << "c0: \n"; /*current_hg.nodeWeight(0).weights[0] << " " << current_hg.nodeWeight(0).weights[1] << "\n";*/
/*for(int i = 0; i < cluster_ids.size(); i++){
std::cout << cluster_ids[i] << " ";
if(cluster_ids[i] != ctmp[i]) std::cout << 1/0 << "\n";
tmp[cluster_ids[i]] += current_hg.nodeWeight(i);
}
for(int i = 0; i < tmp.size(); i++){
if(tmp[i] != chg.nodeWeight(i)){
std::cout << "difference:" << i << " " << tmp[i].weights[0] << " " << tmp[i].weights[1] << " " << chg.nodeWeight(i).weights[0] << " " << chg.nodeWeight(i).weights[1] << "\n";
while(true){}}
}*/
_uncoarseningData.performMultilevelContraction(std::move(cluster_ids), false /* deterministic */, round_start);
_timer.stop_timer("contraction");

++_pass_nr;
Expand Down Expand Up @@ -534,4 +504,4 @@ class MultilevelCoarsener : public ICoarsener,
bool _enable_randomization;
};

} // namespace mt_kahypar
} // namespace mt_kahypar
1 change: 0 additions & 1 deletion mt-kahypar/partition/coarsening/nlevel_uncoarsener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ namespace mt_kahypar {
void NLevelUncoarsener<TypeTraits>::rebalancingImpl() {
// If we reach the top-level hypergraph and the partition is still imbalanced,
// we use a rebalancing algorithm to restore balance.
std::cout << "test we are here\n";
if ( _context.type == ContextType::main && !metrics::isBalanced(*_uncoarseningData.partitioned_hg, _context)) {
const HyperedgeWeight quality_before = _current_metrics.quality;
if ( _context.partition.verbose_output ) {
Expand Down
2 changes: 1 addition & 1 deletion mt-kahypar/partition/coarsening/nlevel_uncoarsener.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,4 @@ class NLevelUncoarsener : public IUncoarsener<TypeTraits>,
bool _is_timer_disabled;
bool _force_measure_timings;
};
}
}
48 changes: 13 additions & 35 deletions mt-kahypar/partition/multilevel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ namespace {
const bool is_vcycle) {
using Hypergraph = typename TypeTraits::Hypergraph;
using PartitionedHypergraph = typename TypeTraits::PartitionedHypergraph;
PartitionedHypergraph partitioned_hg;

// ################## COARSENING ##################
mt_kahypar::io::printCoarseningBanner(context);
Expand All @@ -96,7 +97,7 @@ namespace {
context.coarsening.algorithm, utils::hypergraph_cast(hypergraph),
context, uncoarsening::to_pointer(uncoarseningData));

coarsener->coarsen(context.partition.contraction_input_file);
coarsener->coarsen();

if (context.partition.verbose_output) {
mt_kahypar_hypergraph_t coarsestHypergraph = coarsener->coarsestHypergraph();
Expand All @@ -106,39 +107,16 @@ namespace {
}
}
timer.stop_timer("coarsening");
std::unique_ptr<IUncoarsener<TypeTraits>> uncoarsener(nullptr);
/*std::unique_ptr<IUncoarsener<TypeTraits>> uncoarsener(nullptr);
if (uncoarseningData.nlevel) {
/*uncoarsener = std::make_unique<NLevelUncoarsener<TypeTraits>>(
hypergraph, context, uncoarseningData, target_graph);*/
uncoarsener = std::make_unique<NLevelUncoarsener<TypeTraits>>(
hypergraph, context, uncoarseningData, target_graph);
} else {
uncoarsener = std::make_unique<MultilevelUncoarsener<TypeTraits>>(
hypergraph, context, uncoarseningData, target_graph);
}
/*PartitionedHypergraph partitioned_hg;
int initial_num = hypergraph.initialNumEdges();
partitioned_hg = PartitionedHypergraph(context.partition.k, hypergraph);
std::ifstream myfile;
myfile.open(context.partition.partition_input_file);
HypernodeID hn = 0;
while(myfile){
std::string nextline;
std::getline(myfile, nextline);
if(nextline != ""){
ASSERT(stoi(nextline) < partitioned_hg.k());
partitioned_hg.setOnlyNodePart(hn, stoi(nextline));
}
hn++;
}
myfile.close();
partitioned_hg.initializePartition();*/

/*for(HypernodeID hn : partitioned_hg.nodes()){
std::cout << partitioned_hg.partID(hn) << "\n";
}*/
PartitionedHypergraph& partitioned_hg = uncoarseningData.coarsestPartitionedHypergraph();
std::ifstream myfile;
/*std::ifstream myfile;
myfile.open(context.partition.contraction_input_file);
HypernodeID hn = 0;
bool is_partfile = false;
Expand Down Expand Up @@ -176,11 +154,11 @@ namespace {
PartitionedHypergraph back = uncoarsener->uncoarsen();
/*partitioned_hg = uncoarsener->doLastRefine(&partitioned_hg);*/

io::printPartitioningResults(back, context, "Local Search Results:");
return back;
/*io::printPartitioningResults(back, context, "Local Search Results:");
return back;*/


/*// ################## INITIAL PARTITIONING ##################
// ################## INITIAL PARTITIONING ##################
io::printInitialPartitioningBanner(context);
timer.start_timer("initial_partitioning", "Initial Partitioning");
PartitionedHypergraph& phg = uncoarseningData.coarsestPartitionedHypergraph();
Expand Down Expand Up @@ -267,17 +245,17 @@ namespace {
timer.start_timer("refinement", "Refinement");
std::unique_ptr<IUncoarsener<TypeTraits>> uncoarsener(nullptr);
if (uncoarseningData.nlevel) {
uncoarsener = std::make_unique<NLevelUncoarsener<TypeTraits>>(
hypergraph, context, uncoarseningData, target_graph);
/*uncoarsener = std::make_unique<NLevelUncoarsener<TypeTraits>>(
hypergraph, context, uncoarseningData, target_graph);*/
} else {
uncoarsener = std::make_unique<MultilevelUncoarsener<TypeTraits>>(
hypergraph, context, uncoarseningData, target_graph);
}
partitioned_hg = uncoarsener->uncoarsen();
partitioned_hg = uncoarsener->doLastRefine();
/*partitioned_hg = uncoarsener->doLastRefine();*/
io::printPartitioningResults(partitioned_hg, context, "Local Search Results:");
timer.stop_timer("refinement");
return partitioned_hg;*/
return partitioned_hg;
}
}

Expand Down

0 comments on commit ebd2c69

Please sign in to comment.