Skip to content

Commit

Permalink
try catch in c interface
Browse files Browse the repository at this point in the history
  • Loading branch information
kittobi1992 committed Aug 11, 2023
1 parent aa73caa commit 555493b
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 133 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ int main(int argc, char* argv[]) {
"path/to/hypergraph/file", DEFAULT, HMETIS /* file format */);

// Partition Hypergraph
mt_kahypar_partitioned_hypergraph_t partitioned_hg =
mt_kahypar_partition(hypergraph, context);
mt_kahypar_partitioned_hypergraph_t partitioned_hg;
mt_kahypar_partition(hypergraph, partitioned_hg, context);

// Extract Partition
std::unique_ptr<mt_kahypar_partition_id_t[]> partition =
Expand Down
20 changes: 13 additions & 7 deletions include/libmtkahypar.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,14 @@ MT_KAHYPAR_API bool mt_kahypar_check_compatibility(mt_kahypar_hypergraph_t hyper
/**
* Partitions a (hyper)graph with the configuration specified in the partitioning context.
*
* \return True, on success. Otherwise an internal error occurred (details can be fonnd in the log).
* \note Before partitioning, the number of blocks, imbalance parameter and objective function must be
* set in the partitioning context. This can be done either via mt_kahypar_set_context_parameter(...)
* or mt_kahypar_set_partitioning_parameters(...).
*/
MT_KAHYPAR_API mt_kahypar_partitioned_hypergraph_t mt_kahypar_partition(mt_kahypar_hypergraph_t hypergraph,
mt_kahypar_context_t* context);
MT_KAHYPAR_API bool mt_kahypar_partition(mt_kahypar_hypergraph_t hypergraph,
mt_kahypar_partitioned_hypergraph_t& partitioned_hg,
mt_kahypar_context_t* context);

/**
* Maps a (hyper)graph onto a target graph with the configuration specified in the partitioning context.
Expand All @@ -263,15 +265,17 @@ MT_KAHYPAR_API mt_kahypar_partitioned_hypergraph_t mt_kahypar_partition(mt_kahyp
* is able to acurately model wire-lengths in VLSI design or communication costs in a distributed system where some
* processors do not communicate directly with each other or different speeds.
*
* \return True, on success. Otherwise an internal error occurred (details can be fonnd in the log).
* \note Since computing Steiner trees is an NP-hard problem, we currently restrict the size of the target graph
* to at most 64 nodes. If you want to map hypergraphs onto larger target graphs, you can use recursive multisectioning.
* For example, if the target graph has 4096 nodes, you can first map the hypergraph onto a coarser approximation of the
* target graph with 64 nodes, and subsequently map each block of the mapping to the corresponding subgraph of the
* target graph each having 64 nodes.
*/
MT_KAHYPAR_API mt_kahypar_partitioned_hypergraph_t mt_kahypar_map(mt_kahypar_hypergraph_t hypergraph,
mt_kahypar_target_graph_t* target_graph,
mt_kahypar_context_t* context);
MT_KAHYPAR_API bool mt_kahypar_map(mt_kahypar_hypergraph_t hypergraph,
mt_kahypar_target_graph_t* target_graph,
mt_kahypar_partitioned_hypergraph_t& partitioned_hg,
mt_kahypar_context_t* context);

/**
* Checks whether or not the given partitioned hypergraph can
Expand All @@ -283,22 +287,24 @@ MT_KAHYPAR_API bool mt_kahypar_check_partition_compatibility(mt_kahypar_partitio
/**
* Improves a given partition (using the V-cycle technique).
*
* \return True, on success. Otherwise an internal error occurred (details can be fonnd in the log).
* \note The number of blocks specified in the partitioning context must be equal to the
* number of blocks of the given partition.
* \note There is no guarantee that this call will find an improvement.
*/
MT_KAHYPAR_API void mt_kahypar_improve_partition(mt_kahypar_partitioned_hypergraph_t partitioned_hg,
MT_KAHYPAR_API bool mt_kahypar_improve_partition(mt_kahypar_partitioned_hypergraph_t partitioned_hg,
mt_kahypar_context_t* context,
const size_t num_vcycles);

/**
* Improves a given mapping (using the V-cycle technique).
*
* \return True, on success. Otherwise an internal error occurred (details can be fonnd in the log).
* \note The number of nodes of the target graph must be equal to the
* number of blocks of the given partition.
* \note There is no guarantee that this call will find an improvement.
*/
MT_KAHYPAR_API void mt_kahypar_improve_mapping(mt_kahypar_partitioned_hypergraph_t partitioned_hg,
MT_KAHYPAR_API bool mt_kahypar_improve_mapping(mt_kahypar_partitioned_hypergraph_t partitioned_hg,
mt_kahypar_target_graph_t* target_graph,
mt_kahypar_context_t* context,
const size_t num_vcycles);
Expand Down
8 changes: 4 additions & 4 deletions include/libmtkahypartypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ typedef struct {
} mt_kahypar_hypergraph_t;

typedef struct {
const mt_kahypar_hypergraph_s* hypergraph;
mt_kahypar_hypergraph_type_t type;
const mt_kahypar_hypergraph_s* hypergraph = nullptr;
mt_kahypar_hypergraph_type_t type = NULLPTR_HYPERGRAPH;
} mt_kahypar_hypergraph_const_t;

struct mt_kahypar_partitioned_hypergraph_s;
typedef struct {
mt_kahypar_partitioned_hypergraph_s* partitioned_hg;
mt_kahypar_partition_type_t type;
mt_kahypar_partitioned_hypergraph_s* partitioned_hg = nullptr;
mt_kahypar_partition_type_t type = NULLPTR_PARTITION;
} mt_kahypar_partitioned_hypergraph_t;

typedef struct {
Expand Down
4 changes: 2 additions & 2 deletions lib/examples/map_hypergraph_onto_target_graph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ int main(int argc, char* argv[]) {
mt_kahypar_read_target_graph_from_file("target.graph");

// Map hypergraph onto target graph
mt_kahypar_partitioned_hypergraph_t partitioned_hg =
mt_kahypar_map(hypergraph, target_graph, context);
mt_kahypar_partitioned_hypergraph_t partitioned_hg;
mt_kahypar_map(hypergraph, target_graph, partitioned_hg, context);

// Extract Mapping
std::unique_ptr<mt_kahypar_partition_id_t[]> mapping =
Expand Down
6 changes: 3 additions & 3 deletions lib/examples/partition_graph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ int main(int argc, char* argv[]) {
mt_kahypar_read_hypergraph_from_file("delaunay_n15.graph",
DEFAULT, METIS /* file format */);

// Partition Hypergraph
mt_kahypar_partitioned_hypergraph_t partitioned_graph =
mt_kahypar_partition(graph, context);
// Partition Graph
mt_kahypar_partitioned_hypergraph_t partitioned_graph;
mt_kahypar_partition(graph, partitioned_graph, context);

// Extract Partition
std::unique_ptr<mt_kahypar_partition_id_t[]> partition =
Expand Down
7 changes: 3 additions & 4 deletions lib/examples/partition_hypergraph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ int main(int argc, char* argv[]) {
DEFAULT, HMETIS /* file format */);

// Partition Hypergraph
mt_kahypar_partitioned_hypergraph_t partitioned_hg =
mt_kahypar_partition(hypergraph, context);
mt_kahypar_partitioned_hypergraph_t partitioned_hg;
mt_kahypar_partition(hypergraph, partitioned_hg, context);

// Extract Partition
std::unique_ptr<mt_kahypar_partition_id_t[]> partition =
Expand All @@ -55,8 +55,7 @@ int main(int argc, char* argv[]) {
std::cout << "Km1 = " << km1 << std::endl;
std::cout << "Weight of Block 0 = " << block_weights[0] << std::endl;
std::cout << "Weight of Block 1 = " << block_weights[1] << std::endl;

mt_kahypar_free_context(context);
mt_kahypar_free_hypergraph(hypergraph);
mt_kahypar_free_partitioned_hypergraph(partitioned_hg);
mt_kahypar_free_partitioned_hypergraph(partitioned_hg);
}
4 changes: 2 additions & 2 deletions lib/examples/partition_with_fixed_vertices.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ int main(int argc, char* argv[]) {
// hypergraph, "ibm01.k4.p1.fix", 4 /* number of blocks */);

// Partition Hypergraph
mt_kahypar_partitioned_hypergraph_t partitioned_hg =
mt_kahypar_partition(hypergraph, context);
mt_kahypar_partitioned_hypergraph_t partitioned_hg;
mt_kahypar_partition(hypergraph, partitioned_hg, context);

// Extract Partition
std::unique_ptr<mt_kahypar_partition_id_t[]> partition =
Expand Down
4 changes: 2 additions & 2 deletions lib/examples/partition_with_individual_block_weights.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ int main(int argc, char* argv[]) {
DEFAULT, HMETIS /* file format */);

// Partition Hypergraph
mt_kahypar_partitioned_hypergraph_t partitioned_hg =
mt_kahypar_partition(hypergraph, context);
mt_kahypar_partitioned_hypergraph_t partitioned_hg;
mt_kahypar_partition(hypergraph, partitioned_hg, context);

// Extract Block Weights
std::unique_ptr<mt_kahypar_hypernode_weight_t[]> block_weights =
Expand Down
140 changes: 82 additions & 58 deletions lib/libmtkahypar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,89 +387,113 @@ bool mt_kahypar_check_compatibility(mt_kahypar_hypergraph_t hypergraph,
return lib::check_compatibility(hypergraph, preset);
}

mt_kahypar_partitioned_hypergraph_t mt_kahypar_partition(mt_kahypar_hypergraph_t hypergraph,
mt_kahypar_context_t* context) {
bool mt_kahypar_partition(mt_kahypar_hypergraph_t hypergraph,
mt_kahypar_partitioned_hypergraph_t& partitioned_hg,
mt_kahypar_context_t* context) {
Context& c = *reinterpret_cast<Context*>(context);
if ( lib::check_if_all_relavant_parameters_are_set(c) ) {
if ( mt_kahypar_check_compatibility(hypergraph, lib::get_preset_c_type(c.partition.preset_type)) ) {
c.partition.instance_type = lib::get_instance_type(hypergraph);
c.partition.partition_type = to_partition_c_type(
c.partition.preset_type, c.partition.instance_type);
lib::prepare_context(c);
c.partition.num_vcycles = 0;
return PartitionerFacade::partition(hypergraph, c);
} else {
WARNING(lib::incompatibility_description(hypergraph));
bool success = lib::check_if_all_relavant_parameters_are_set(c);
if ( success && mt_kahypar_check_compatibility(hypergraph, lib::get_preset_c_type(c.partition.preset_type)) ) {
c.partition.instance_type = lib::get_instance_type(hypergraph);
c.partition.partition_type = to_partition_c_type(
c.partition.preset_type, c.partition.instance_type);
lib::prepare_context(c);
c.partition.num_vcycles = 0;
try {
partitioned_hg = PartitionerFacade::partition(hypergraph, c);
} catch ( std::exception& ex ) {
LOG << ex.what();
success = false;
}
} else {
WARNING(lib::incompatibility_description(hypergraph));
success = false;
}
return mt_kahypar_partitioned_hypergraph_t { nullptr, NULLPTR_PARTITION };
return success;
}

mt_kahypar_partitioned_hypergraph_t mt_kahypar_map(mt_kahypar_hypergraph_t hypergraph,
mt_kahypar_target_graph_t* target_graph,
mt_kahypar_context_t* context) {
bool mt_kahypar_map(mt_kahypar_hypergraph_t hypergraph,
mt_kahypar_target_graph_t* target_graph,
mt_kahypar_partitioned_hypergraph_t& partitioned_hg,
mt_kahypar_context_t* context) {
Context& c = *reinterpret_cast<Context*>(context);
if ( lib::check_if_all_relavant_parameters_are_set(c) ) {
if ( mt_kahypar_check_compatibility(hypergraph, lib::get_preset_c_type(c.partition.preset_type)) ) {
c.partition.instance_type = lib::get_instance_type(hypergraph);
c.partition.partition_type = to_partition_c_type(
c.partition.preset_type, c.partition.instance_type);
lib::prepare_context(c);
c.partition.num_vcycles = 0;
c.partition.objective = Objective::steiner_tree;
TargetGraph* target = reinterpret_cast<TargetGraph*>(target_graph);
return PartitionerFacade::partition(hypergraph, c, target);
} else {
WARNING(lib::incompatibility_description(hypergraph));
bool success = lib::check_if_all_relavant_parameters_are_set(c);
if ( success && mt_kahypar_check_compatibility(hypergraph, lib::get_preset_c_type(c.partition.preset_type)) ) {
c.partition.instance_type = lib::get_instance_type(hypergraph);
c.partition.partition_type = to_partition_c_type(
c.partition.preset_type, c.partition.instance_type);
lib::prepare_context(c);
c.partition.num_vcycles = 0;
c.partition.objective = Objective::steiner_tree;
TargetGraph* target = reinterpret_cast<TargetGraph*>(target_graph);
try {
partitioned_hg = PartitionerFacade::partition(hypergraph, c, target);
} catch ( std::exception& ex ) {
LOG << ex.what();
success = false;
}
} else {
WARNING(lib::incompatibility_description(hypergraph));
success = false;
}
return mt_kahypar_partitioned_hypergraph_t { nullptr, NULLPTR_PARTITION };
return success;
}

MT_KAHYPAR_API bool mt_kahypar_check_partition_compatibility(mt_kahypar_partitioned_hypergraph_t partitioned_hg,
mt_kahypar_preset_type_t preset) {
bool mt_kahypar_check_partition_compatibility(mt_kahypar_partitioned_hypergraph_t partitioned_hg,
mt_kahypar_preset_type_t preset) {
return lib::check_compatibility(partitioned_hg, preset);
}

void mt_kahypar_improve_partition(mt_kahypar_partitioned_hypergraph_t partitioned_hg,
bool mt_kahypar_improve_partition(mt_kahypar_partitioned_hypergraph_t partitioned_hg,
mt_kahypar_context_t* context,
const size_t num_vcycles) {
Context& c = *reinterpret_cast<Context*>(context);
if ( lib::check_if_all_relavant_parameters_are_set(c) ) {
if ( mt_kahypar_check_partition_compatibility(
partitioned_hg, lib::get_preset_c_type(c.partition.preset_type)) ) {
c.partition.instance_type = lib::get_instance_type(partitioned_hg);
c.partition.partition_type = to_partition_c_type(
c.partition.preset_type, c.partition.instance_type);
lib::prepare_context(c);
c.partition.num_vcycles = num_vcycles;
bool success = lib::check_if_all_relavant_parameters_are_set(c);
if ( success && mt_kahypar_check_partition_compatibility(
partitioned_hg, lib::get_preset_c_type(c.partition.preset_type)) ) {
c.partition.instance_type = lib::get_instance_type(partitioned_hg);
c.partition.partition_type = to_partition_c_type(
c.partition.preset_type, c.partition.instance_type);
lib::prepare_context(c);
c.partition.num_vcycles = num_vcycles;
try {
PartitionerFacade::improve(partitioned_hg, c);
} else {
WARNING(lib::incompatibility_description(partitioned_hg));
} catch ( std::exception& ex ) {
LOG << ex.what();
success = false;
}
} else {
WARNING(lib::incompatibility_description(partitioned_hg));
success = false;
}
return success;
}

void mt_kahypar_improve_mapping(mt_kahypar_partitioned_hypergraph_t partitioned_hg,
mt_kahypar_target_graph_t* target_graph,
mt_kahypar_context_t* context,
const size_t num_vcycles) {
bool mt_kahypar_improve_mapping(mt_kahypar_partitioned_hypergraph_t partitioned_hg,
mt_kahypar_target_graph_t* target_graph,
mt_kahypar_context_t* context,
const size_t num_vcycles) {
Context& c = *reinterpret_cast<Context*>(context);
if ( lib::check_if_all_relavant_parameters_are_set(c) ) {
if ( mt_kahypar_check_partition_compatibility(
partitioned_hg, lib::get_preset_c_type(c.partition.preset_type)) ) {
c.partition.instance_type = lib::get_instance_type(partitioned_hg);
c.partition.partition_type = to_partition_c_type(
c.partition.preset_type, c.partition.instance_type);
lib::prepare_context(c);
c.partition.num_vcycles = num_vcycles;
c.partition.objective = Objective::steiner_tree;
TargetGraph* target = reinterpret_cast<TargetGraph*>(target_graph);
bool success = lib::check_if_all_relavant_parameters_are_set(c);
if ( success && mt_kahypar_check_partition_compatibility(
partitioned_hg, lib::get_preset_c_type(c.partition.preset_type)) ) {
c.partition.instance_type = lib::get_instance_type(partitioned_hg);
c.partition.partition_type = to_partition_c_type(
c.partition.preset_type, c.partition.instance_type);
lib::prepare_context(c);
c.partition.num_vcycles = num_vcycles;
c.partition.objective = Objective::steiner_tree;
TargetGraph* target = reinterpret_cast<TargetGraph*>(target_graph);
try {
PartitionerFacade::improve(partitioned_hg, c, target);
} else {
WARNING(lib::incompatibility_description(partitioned_hg));
} catch ( std::exception& ex ) {
LOG << ex.what();
success = false;
}
} else {
WARNING(lib::incompatibility_description(partitioned_hg));
success = false;
}
return success;
}

mt_kahypar_partitioned_hypergraph_t mt_kahypar_create_partitioned_hypergraph(mt_kahypar_hypergraph_t hypergraph,
Expand Down
2 changes: 1 addition & 1 deletion mt-kahypar/partition/mapping/target_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace mt_kahypar {
void TargetGraph::precomputeDistances(const size_t max_connectivity) {
const size_t num_entries = std::pow(_k, max_connectivity);
if ( num_entries > MEMORY_LIMIT ) {
throw SystemError(
throw SystemException(
"Too much memory requested for precomputing steiner trees "
"of connectivity sets in the target graph.");
}
Expand Down
Loading

0 comments on commit 555493b

Please sign in to comment.