Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework library interfaces #201

Open
wants to merge 48 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
190803f
use exceptions where possible
N-Maas Dec 6, 2024
7012b22
use exceptions in python interface and remove duplications
N-Maas Dec 6, 2024
b1b824e
setMappingParameters function for python interface
N-Maas Dec 6, 2024
42c3a2a
NonSupported -> Unsupported
N-Maas Dec 6, 2024
650c565
direct context initialization
N-Maas Dec 6, 2024
94ed733
follow python naming conventions
N-Maas Dec 6, 2024
689c863
library interfaces: disable logging by default
N-Maas Dec 9, 2024
70fea0f
error handling in C interface
N-Maas Dec 9, 2024
4338f98
checks for read partition file
N-Maas Dec 12, 2024
bed7b6f
python fix for imbalance
N-Maas Dec 12, 2024
2fcbc36
add test
N-Maas Dec 12, 2024
3fb0db4
fix imbalance in python tests
N-Maas Dec 12, 2024
8b095f6
deduplication for python module
N-Maas Dec 12, 2024
88c29d8
new initialization for python interface
N-Maas Dec 13, 2024
8686931
keep alive for PHG
N-Maas Dec 13, 2024
46f1f0b
block weight handling in python API
N-Maas Dec 13, 2024
bd3878e
fix/improve C interface
N-Maas Dec 13, 2024
2a98465
handle boost targets
N-Maas Dec 13, 2024
2f5829c
C interface: require context for graph construction
N-Maas Dec 13, 2024
7424fa4
renaming
N-Maas Dec 13, 2024
927d1ef
Python interface: require context for creating graph
N-Maas Dec 13, 2024
90c6ff8
typo
N-Maas Dec 13, 2024
dc01f1b
switch to fully type erased python interface
N-Maas Dec 13, 2024
a00e02d
further improvements and tests
N-Maas Dec 16, 2024
2f2350d
python iterators
N-Maas Dec 16, 2024
a591605
fix interface test
N-Maas Dec 16, 2024
e21739d
support for more hypergraph properties
N-Maas Dec 16, 2024
0b152b1
further refactoring
N-Maas Dec 17, 2024
40a50f6
get partition properties in C interface
N-Maas Dec 17, 2024
84d52d3
reorganize files
N-Maas Dec 17, 2024
5659588
input format handling
N-Maas Dec 17, 2024
23d8560
remove some todos
N-Maas Dec 17, 2024
e83f120
implement iteration and source/target
N-Maas Dec 17, 2024
0a9c7c4
python tests highest quality preset
N-Maas Dec 18, 2024
ccec2b1
minor
N-Maas Dec 18, 2024
ac16860
make c interface actually c compatible
N-Maas Dec 18, 2024
abbecca
allow error when creating partitioned hypergraph
N-Maas Dec 18, 2024
9d8e53e
add missing check
N-Maas Dec 18, 2024
bc43d6f
update c interface examples
N-Maas Dec 19, 2024
bbb0444
bounds checking for python interface
N-Maas Dec 19, 2024
67d865e
minor
N-Maas Dec 19, 2024
bb79e7a
python: add 'blocks' iterator
N-Maas Dec 19, 2024
1199b8b
update python examples
N-Maas Dec 19, 2024
dc01576
fixes
N-Maas Dec 19, 2024
83e1a8f
update readme
N-Maas Dec 19, 2024
55947a2
checks for number of blocks
N-Maas Dec 19, 2024
91f8817
don't copy target graph in C interface
N-Maas Dec 19, 2024
ce2e7ba
fix tools
N-Maas Dec 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
get partition properties in C interface
  • Loading branch information
N-Maas committed Dec 17, 2024
commit 40a50f69fce6feed080e3a2b2c6ed1b9d8e4b6a1
45 changes: 36 additions & 9 deletions include/mtkahypar.h
Original file line number Diff line number Diff line change
Expand Up @@ -407,17 +407,44 @@ MT_KAHYPAR_API mt_kahypar_status_t mt_kahypar_write_partition_to_file(const mt_k
// ####################### Partitioning Results #######################

// edge and pin iterators?? (+ num pins)
// node degree, node weight, edge weight, edge source, edge target
// edge source, edge target
// compute block weights

// "num_blocks"
// "block_weight"
// "block_id", "Block to which the corresponding node is assigned"
// "fixed_vertex_block" "Block to which the node is fixed (-1 if not fixed)"
// "num_incident_cut_edges" "Number of incident cut hyperedges of the corresponding node"
// "num_pins_in_block", pinCountInPart
// "connectivity"
/**
* Number of blocks of the partition.
*/
MT_KAHYPAR_API mt_kahypar_partition_id_t mt_kahypar_num_blocks(const mt_kahypar_partitioned_hypergraph_t partitioned_hg);

// compute block weights
/**
* Weight of the corresponding block.
*/
MT_KAHYPAR_API mt_kahypar_hypernode_weight_t mt_kahypar_block_weight(const mt_kahypar_partitioned_hypergraph_t partitioned_hg,
const mt_kahypar_partition_id_t block);

/**
* Block to which the corresponding hypernode is assigned.
*/
MT_KAHYPAR_API mt_kahypar_partition_id_t mt_kahypar_block_id(const mt_kahypar_partitioned_hypergraph_t partitioned_hg,
const mt_kahypar_hypernode_id_t node);

/**
* Number of incident cut hyperedges of the corresponding node.
*/
MT_KAHYPAR_API mt_kahypar_hyperedge_id_t mt_kahypar_num_incident_cut_hyperedges(const mt_kahypar_partitioned_hypergraph_t partitioned_hg,
const mt_kahypar_hypernode_id_t node);

/**
* Number of distinct blocks to which the pins of the corresponding hyperedge are assigned.
*/
MT_KAHYPAR_API mt_kahypar_partition_id_t mt_kahypar_connectivity(const mt_kahypar_partitioned_hypergraph_t partitioned_hg,
const mt_kahypar_hyperedge_id_t edge);

/**
* Number of pins assigned to the corresponding block in the given hyperedge.
*/
MT_KAHYPAR_API mt_kahypar_hypernode_id_t mt_kahypar_num_pins_in_block(const mt_kahypar_partitioned_hypergraph_t partitioned_hg,
const mt_kahypar_hyperedge_id_t edge,
const mt_kahypar_partition_id_t block);

/**
* Extracts a partition from a partitioned (hyper)graph. The size of the provided array must be at least the number of nodes.
Expand Down
31 changes: 31 additions & 0 deletions lib/mtkahypar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,37 @@ mt_kahypar_status_t mt_kahypar_write_partition_to_file(const mt_kahypar_partitio
}
}


mt_kahypar_partition_id_t mt_kahypar_num_blocks(const mt_kahypar_partitioned_hypergraph_t partitioned_hg) {
return lib::num_blocks<false>(partitioned_hg);
}

mt_kahypar_hypernode_weight_t mt_kahypar_block_weight(const mt_kahypar_partitioned_hypergraph_t partitioned_hg,
const mt_kahypar_partition_id_t block) {
return lib::block_weight<false>(partitioned_hg, block);
}

mt_kahypar_partition_id_t mt_kahypar_block_id(const mt_kahypar_partitioned_hypergraph_t partitioned_hg,
const mt_kahypar_hypernode_id_t node) {
return lib::block_id<false>(partitioned_hg, node);
}

mt_kahypar_hyperedge_id_t mt_kahypar_num_incident_cut_hyperedges(const mt_kahypar_partitioned_hypergraph_t partitioned_hg,
const mt_kahypar_hypernode_id_t node) {
return lib::num_incident_cut_edges<false>(partitioned_hg, node);
}

mt_kahypar_partition_id_t mt_kahypar_connectivity(const mt_kahypar_partitioned_hypergraph_t partitioned_hg,
const mt_kahypar_hyperedge_id_t edge) {
return lib::connectivity<false>(partitioned_hg, edge);
}

mt_kahypar_hypernode_id_t mt_kahypar_num_pins_in_block(const mt_kahypar_partitioned_hypergraph_t partitioned_hg,
const mt_kahypar_hyperedge_id_t edge,
const mt_kahypar_partition_id_t block) {
return lib::num_pins_in_block<false>(partitioned_hg, edge, block);
}

void mt_kahypar_get_partition(const mt_kahypar_partitioned_hypergraph_t partitioned_hg, mt_kahypar_partition_id_t* partition) {
lib::get_partition<false>(partitioned_hg, partition);
}
Expand Down
4 changes: 2 additions & 2 deletions python/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ Construct a partitioned hypergraph from this hypergraph.
// ####################### Partitioned Hypergraph #######################

phg_class
.def("num_blocks", &lib::num_blocks<true>, "Number of blocks")
.def("num_blocks", &lib::num_blocks<true>, "Number of blocks of the partition")
.def("block_weight", &lib::block_weight<true>,
"Weight of the corresponding block", py::arg("block"))
.def("block_id", &lib::block_id<true>,
Expand All @@ -621,7 +621,7 @@ Construct a partitioned hypergraph from this hypergraph.
.def("connectivity", &lib::connectivity<true>,
"Number of distinct blocks to which the pins of the corresponding hyperedge are assigned", py::arg("hyperedge"))
.def("num_pins_in_block", &lib::num_pins_in_block<true>,
"Number of nodes part of the corresponding block in the given hyperedge", py::arg("hyperedge"), py::arg("block_id"))
"Number of pins assigned to the corresponding block in the given hyperedge", py::arg("hyperedge"), py::arg("block_id"))
.def("imbalance", &lib::imbalance<true>,
"Computes the imbalance of the partition", py::arg("context"))
.def("cut", &lib::cut<true>,
Expand Down
55 changes: 55 additions & 0 deletions tests/interface/interface_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,61 @@ namespace mt_kahypar {
mt_kahypar_free_partitioned_hypergraph(partitioned_hg_2);
}

TEST(MtKaHyPar, ReportsPropertiesOfHypergraphPartition) {
mt_kahypar_error_t error;
mt_kahypar_context_t* context = mt_kahypar_context_from_preset(DEFAULT);
const mt_kahypar_hypernode_id_t num_vertices = 7;
const mt_kahypar_hyperedge_id_t num_hyperedges = 4;

std::unique_ptr<size_t[]> hyperedge_indices = std::make_unique<size_t[]>(5);
hyperedge_indices[0] = 0; hyperedge_indices[1] = 2; hyperedge_indices[2] = 6;
hyperedge_indices[3] = 9; hyperedge_indices[4] = 12;

std::unique_ptr<mt_kahypar_hyperedge_id_t[]> hyperedges = std::make_unique<mt_kahypar_hyperedge_id_t[]>(12);
hyperedges[0] = 0; hyperedges[1] = 2; // Hyperedge 0
hyperedges[2] = 0; hyperedges[3] = 1; hyperedges[4] = 3; hyperedges[5] = 4; // Hyperedge 1
hyperedges[6] = 3; hyperedges[7] = 4; hyperedges[8] = 6; // Hyperedge 2
hyperedges[9] = 2; hyperedges[10] = 5; hyperedges[11] = 6; // Hyperedge 3

mt_kahypar_hypergraph_t hypergraph = mt_kahypar_create_hypergraph(
context, num_vertices, num_hyperedges, hyperedge_indices.get(), hyperedges.get(), nullptr, nullptr, &error);

std::unique_ptr<mt_kahypar_partition_id_t[]> partition = std::make_unique<mt_kahypar_partition_id_t[]>(7);
partition[0] = 0; partition[1] = 0; partition[2] = 0;
partition[3] = 1; partition[4] = 1; partition[5] = 1; partition[6] = 1;

mt_kahypar_partitioned_hypergraph_t partitioned_hg =
mt_kahypar_create_partitioned_hypergraph(hypergraph, context, 2, partition.get());

ASSERT_EQ(2, mt_kahypar_km1(partitioned_hg));
ASSERT_EQ(2, mt_kahypar_num_blocks(partitioned_hg));
ASSERT_EQ(3, mt_kahypar_block_weight(partitioned_hg, 0));
ASSERT_EQ(4, mt_kahypar_block_weight(partitioned_hg, 1));
for ( mt_kahypar_hypernode_id_t hn = 0; hn < 7; ++hn ) {
ASSERT_EQ(partition[hn], mt_kahypar_block_id(partitioned_hg, hn));
}

ASSERT_EQ(1, mt_kahypar_num_incident_cut_hyperedges(partitioned_hg, 0));
ASSERT_EQ(1, mt_kahypar_num_incident_cut_hyperedges(partitioned_hg, 6));

ASSERT_EQ(1, mt_kahypar_connectivity(partitioned_hg, 0));
ASSERT_EQ(2, mt_kahypar_connectivity(partitioned_hg, 1));
ASSERT_EQ(1, mt_kahypar_connectivity(partitioned_hg, 2));
ASSERT_EQ(2, mt_kahypar_connectivity(partitioned_hg, 3));

ASSERT_EQ(2, mt_kahypar_num_pins_in_block(partitioned_hg, 0, 0));
ASSERT_EQ(0, mt_kahypar_num_pins_in_block(partitioned_hg, 0, 1));
ASSERT_EQ(2, mt_kahypar_num_pins_in_block(partitioned_hg, 1, 0));
ASSERT_EQ(2, mt_kahypar_num_pins_in_block(partitioned_hg, 1, 1));
ASSERT_EQ(0, mt_kahypar_num_pins_in_block(partitioned_hg, 2, 0));
ASSERT_EQ(3, mt_kahypar_num_pins_in_block(partitioned_hg, 2, 1));
ASSERT_EQ(1, mt_kahypar_num_pins_in_block(partitioned_hg, 3, 0));
ASSERT_EQ(2, mt_kahypar_num_pins_in_block(partitioned_hg, 3, 1));

mt_kahypar_free_hypergraph(hypergraph);
mt_kahypar_free_partitioned_hypergraph(partitioned_hg);
}

TEST(MtKaHyPar, WritesAndLoadsGraphPartitionFile) {
mt_kahypar_error_t error;
mt_kahypar_context_t* context = mt_kahypar_context_from_preset(DEFAULT);
Expand Down