Skip to content

Commit 806d2ee

Browse files
committed
verify partition: support metis format
1 parent 4fc508d commit 806d2ee

1 file changed

Lines changed: 73 additions & 35 deletions

File tree

tools/verify_partition.cc

Lines changed: 73 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
#include <string>
3333

3434
#include "mt-kahypar/macros.h"
35+
#include "mt-kahypar/datastructures/static_graph.h"
3536
#include "mt-kahypar/datastructures/static_hypergraph.h"
37+
#include "mt-kahypar/datastructures/partitioned_graph.h"
3638
#include "mt-kahypar/datastructures/partitioned_hypergraph.h"
3739
#include "mt-kahypar/datastructures/connectivity_info.h"
3840
#include "mt-kahypar/partition/context.h"
@@ -46,9 +48,12 @@ using namespace mt_kahypar;
4648
namespace po = boost::program_options;
4749

4850
using Hypergraph = ds::StaticHypergraph;
51+
using StaticGraph = ds::StaticGraph;
4952
using PartitionedHypergraph = ds::PartitionedHypergraph<Hypergraph, ds::ConnectivityInfo>;
53+
using PartitionedGraph = ds::PartitionedGraph<StaticGraph>;
5054

51-
bool readPartitionFile(const std::string& partition_file, PartitionedHypergraph& hypergraph) {
55+
template<typename PHG>
56+
bool readPartitionFile(const std::string& partition_file, PHG& hypergraph) {
5257
bool success = true;
5358
std::vector<PartitionID> partition;
5459
mt_kahypar::io::readPartitionFile(partition_file, hypergraph.initialNumNodes(), partition);
@@ -67,6 +72,44 @@ bool readPartitionFile(const std::string& partition_file, PartitionedHypergraph&
6772
return success;
6873
}
6974

75+
template<typename PHG>
76+
bool computeResults(PHG& phg, Context& context) {
77+
// Setup Context
78+
context.setupPartWeights(phg.totalWeight());
79+
80+
// Read Partition File
81+
bool success = readPartitionFile(context.partition.graph_partition_filename, phg);
82+
83+
for ( PartitionID i = 0; i < context.partition.k; ++i ) {
84+
if ( !(phg.partWeight(i) <= context.partition.max_part_weights[i]) ) {
85+
LOG << RED << "[ERROR]" << END << "Block" << (i + 1) << "has weight"
86+
<< phg.partWeight(i) << ", but maximum allowed block weight is"
87+
<< context.partition.max_part_weights[i] << END;
88+
success = false;
89+
}
90+
}
91+
92+
// Check fixed vertices
93+
if ( phg.hasFixedVertices() ) {
94+
for ( const HypernodeID& hn : phg.nodes() ) {
95+
if ( phg.isFixed(hn) && phg.fixedVertexBlock(hn) != phg.partID(hn) ) {
96+
LOG << RED << "Node" << hn << "is fixed to block" << phg.fixedVertexBlock(hn)
97+
<< ", but assigned to block" << phg.partID(hn) << END;
98+
success = false;
99+
}
100+
}
101+
}
102+
103+
std::cout << "***********************" << context.partition.k
104+
<< "-way Partition Result************************" << std::endl;
105+
std::cout << "cut =" << metrics::quality(phg, Objective::cut) << std::endl;
106+
std::cout << "soed =" << metrics::quality(phg, Objective::soed) << std::endl;
107+
std::cout << "km1 = " << metrics::quality(phg, Objective::km1) << std::endl;
108+
std::cout << "imbalance = " << metrics::imbalance(phg, context) << std::endl;
109+
110+
return success;
111+
}
112+
70113
int main(int argc, char* argv[]) {
71114
Context context;
72115

@@ -78,6 +121,17 @@ int main(int argc, char* argv[]) {
78121
("partition-file,b",
79122
po::value<std::string>(&context.partition.graph_partition_filename)->value_name("<string>")->required(),
80123
"Partition Filename")
124+
("input-file-format",
125+
po::value<std::string>()->value_name("<string>")->notifier([&](const std::string& s) {
126+
if (s == "hmetis") {
127+
context.partition.file_format = FileFormat::hMetis;
128+
} else if (s == "metis") {
129+
context.partition.file_format = FileFormat::Metis;
130+
}
131+
}),
132+
"Input file format: \n"
133+
" - hmetis : hMETIS hypergraph file format \n"
134+
" - metis : METIS graph file format")
81135
("blocks,k",
82136
po::value<PartitionID>(&context.partition.k)->value_name("<int>")->required(),
83137
"Number of Blocks")
@@ -97,48 +151,32 @@ int main(int argc, char* argv[]) {
97151
mt_kahypar::io::readInputFile(
98152
context.partition.graph_filename, PresetType::default_preset,
99153
InstanceType::hypergraph, FileFormat::hMetis, true);
100-
Hypergraph& hg = utils::cast<Hypergraph>(hypergraph);
101-
PartitionedHypergraph phg(context.partition.k, hg, parallel_tag_t());
102154

103-
// Add fixed vertices
104-
if ( context.partition.fixed_vertex_filename != "" ) {
105-
mt_kahypar::io::addFixedVerticesFromFile(
106-
hypergraph, context.partition.fixed_vertex_filename, context.partition.k);
107-
}
155+
bool success;
156+
if (context.partition.file_format == FileFormat::hMetis) {
157+
Hypergraph& hg = utils::cast<Hypergraph>(hypergraph);
158+
PartitionedHypergraph phg(context.partition.k, hg, parallel_tag_t());
108159

109-
// Setup Context
110-
context.setupPartWeights(hg.totalWeight());
160+
// Add fixed vertices
161+
if ( context.partition.fixed_vertex_filename != "" ) {
162+
mt_kahypar::io::addFixedVerticesFromFile(
163+
hypergraph, context.partition.fixed_vertex_filename, context.partition.k);
164+
}
111165

112-
// Read Partition File
113-
bool success = readPartitionFile(context.partition.graph_partition_filename, phg);
166+
success = computeResults(phg, context);
167+
} else {
168+
StaticGraph& hg = utils::cast<StaticGraph>(hypergraph);
169+
PartitionedGraph phg(context.partition.k, hg, parallel_tag_t());
114170

115-
for ( PartitionID i = 0; i < context.partition.k; ++i ) {
116-
if ( !(phg.partWeight(i) <= context.partition.max_part_weights[i]) ) {
117-
LOG << RED << "[ERROR]" << END << "Block" << (i + 1) << "has weight"
118-
<< phg.partWeight(i) << ", but maximum allowed block weight is"
119-
<< context.partition.max_part_weights[i] << END;
120-
success = false;
171+
// Add fixed vertices
172+
if ( context.partition.fixed_vertex_filename != "" ) {
173+
mt_kahypar::io::addFixedVerticesFromFile(
174+
hypergraph, context.partition.fixed_vertex_filename, context.partition.k);
121175
}
122-
}
123176

124-
// Check fixed vertices
125-
if ( phg.hasFixedVertices() ) {
126-
for ( const HypernodeID& hn : phg.nodes() ) {
127-
if ( phg.isFixed(hn) && phg.fixedVertexBlock(hn) != phg.partID(hn) ) {
128-
LOG << RED << "Node" << hn << "is fixed to block" << phg.fixedVertexBlock(hn)
129-
<< ", but assigned to block" << phg.partID(hn) << END;
130-
success = false;
131-
}
132-
}
177+
success = computeResults(phg, context);
133178
}
134179

135-
std::cout << "***********************" << context.partition.k
136-
<< "-way Partition Result************************" << std::endl;
137-
std::cout << "cut =" << metrics::quality(phg, Objective::cut) << std::endl;
138-
std::cout << "soed =" << metrics::quality(phg, Objective::soed) << std::endl;
139-
std::cout << "km1 = " << metrics::quality(phg, Objective::km1) << std::endl;
140-
std::cout << "imbalance = " << metrics::imbalance(phg, context) << std::endl;
141-
142180
utils::delete_hypergraph(hypergraph);
143181

144182
return success ? 0 : -1;

0 commit comments

Comments
 (0)