Skip to content

Commit

Permalink
Code fixes from szhorvat when migrating to igraph_0.10.x
Browse files Browse the repository at this point in the history
  • Loading branch information
joern274 committed Apr 25, 2024
1 parent 3e38cd8 commit 683dca5
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 91 deletions.
7 changes: 6 additions & 1 deletion deps/igraph-0.10.x/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ set (HAVE_IGRAPH TRUE)

message("-- IGRAPH version 0.10.11 : download and build libigraph${CMAKE_SHARED_LIBRARY_SUFFIX} in ${IGRAPH_BASE}")

#Avoid warning about DOWNLOAD_EXTRACT_TIMESTAMP in CMake 3.24
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
cmake_policy(SET CMP0135 NEW)
endif()

ExternalProject_Add(igraph_0_10
PREFIX "${IGRAPH_INSTALL}"
URL "https://github.com/igraph/igraph/releases/download/0.10.11/igraph-0.10.11.tar.gz"
SOURCE_DIR "${IGRAPH_DOWNLOAD}"
CMAKE_ARGS "-DCMAKE_INSTALL_PREFIX=${IGRAPH_INSTALL}" "-DBUILD_SHARED_LIBS=ON" "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}" "-Wno-deprecated" "-DIGRAPH_VERSION=0.9.10"
BINARY_DIR "${IGRAPH_BUILD}"
PATCH_COMMAND echo PATCH
PATCH_COMMAND echo "No patch for igraph-0.10.x required"
INSTALL_DIR "${IGRAPH_INSTALL}"
BUILD_BYPRODUCTS "${IGRAPH_LIB}"
)
Expand Down
26 changes: 0 additions & 26 deletions deps/igraph-0.10.x/patch.txt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,6 @@ namespace hal
* @param[in] vertex_to_gate - map from node ID in igraph to HAL gate
* @returns map from membership id to set of gates that have the membership.
*/
std::map<int, std::set<Gate*>> get_memberships_for_hal(igraph_t* graph, igraph_vector_t membership, std::map<int, Gate*> vertex_to_gate);
std::map<int, std::set<Gate*>> get_memberships_for_hal(igraph_t* graph, igraph_vector_int_t *membership, std::map<int, Gate*> vertex_to_gate);
};
} // namespace hal
} // namespace hal
20 changes: 8 additions & 12 deletions plugins/graph_algorithm/src/clustering/communities_fast_greedy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,29 @@ namespace hal
igraph_t graph;
std::map<int, Gate*> vertex_to_gate = get_igraph_directed(nl, &graph);

igraph_vector_t membership, modularity;
igraph_vector_int_t membership, modularity;
igraph_matrix_t merges;

igraph_to_undirected(&graph, IGRAPH_TO_UNDIRECTED_MUTUAL, 0);

igraph_vector_init(&membership, 1);
igraph_vector_init(&modularity, 1);
igraph_matrix_init(&merges, 1, 1);
igraph_vector_int_init(&membership, 1);

igraph_community_fastgreedy(&graph,
0, /* no weights */
&merges,
&modularity,
nullptr, /* no weights */
nullptr,
nullptr,
&membership);

// map back to HAL structures
std::map<int, std::set<Gate*>> community_sets;
for (int i = 0; i < igraph_vector_size(&membership); i++)
for (igraph_integer_t i = 0; i < igraph_vector_int_size(&membership); i++)
{
community_sets[(int)VECTOR(membership)[i]].insert(vertex_to_gate[i]);
community_sets[VECTOR(membership)[i]].insert(vertex_to_gate[i]);
}
//igraph_vector_destroy(&membership);

igraph_destroy(&graph);
igraph_vector_destroy(&membership);
igraph_vector_destroy(&modularity);
igraph_matrix_destroy(&merges);
igraph_vector_int_destroy(&membership);

return community_sets;
}
Expand Down
20 changes: 10 additions & 10 deletions plugins/graph_algorithm/src/clustering/communities_spinglass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ namespace hal


igraph_real_t modularity, temperature;
igraph_vector_t membership, csize;
igraph_vector_int_t membership, csize;

igraph_vector_init(&membership, 0);
igraph_vector_init(&csize, 0);
igraph_vector_int_init(&membership, 0);
igraph_vector_int_init(&csize, 0);
igraph_community_spinglass(&graph,
0, /* no weights */
nullptr, /* no weights */
&modularity,
&temperature,
&membership,
&csize,
(igraph_integer_t)spins, /* no of spins */
0, /* parallel update */
spins, /* no of spins */
false, /* parallel update */
1.0, /* start temperature */
0.01, /* stop temperature */
0.99, /* cooling factor */
Expand All @@ -51,17 +51,17 @@ namespace hal

log("\tTemperature: {}", temperature);
log("\tCluster sizes: ");
for (long int i = 0; i < igraph_vector_size(&csize); i++)
for (igraph_integer_t i = 0; i < igraph_vector_int_size(&csize); i++)
{
log("\t\t{}", (long int)VECTOR(csize)[i]);
}

// map back to HAL structures
auto community_sets = get_memberships_for_hal(&graph, membership, vertex_to_gate);
auto community_sets = get_memberships_for_hal(&graph, &membership, vertex_to_gate);

igraph_destroy(&graph);
igraph_vector_destroy(&membership);
igraph_vector_destroy(&csize);
igraph_vector_int_destroy(&membership);
igraph_vector_int_destroy(&csize);

return community_sets;
}
Expand Down
31 changes: 13 additions & 18 deletions plugins/graph_algorithm/src/clustering/community_detection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ namespace hal
}

/* transform all nets to igraph_real_t */
igraph_real_t* edges = new igraph_real_t[edge_counter];
u32 edge_vertice_counter = 0;
igraph_integer_t* edges = new igraph_integer_t[edge_counter];
u32 edge_vertex_counter = 0;
for (auto net : nl->get_nets())
{
assert(net->get_sources().size() == 1);
Expand All @@ -83,40 +83,35 @@ namespace hal
if (successor->get_gate() == nullptr)
continue;
auto successor_id = nl_igraph_id_match[successor->get_gate()->get_id()];
edges[edge_vertice_counter++] = (igraph_real_t)predecessor_id;
edges[edge_vertice_counter++] = (igraph_real_t)successor_id;
edges[edge_vertex_counter++] = predecessor_id;
edges[edge_vertex_counter++] = successor_id;
}
}

/* create and add edges to the graph */
igraph_t graph;
igraph_vector_t netlist_edges;
igraph_vector_init_copy(&netlist_edges, edges, edge_counter);
igraph_vector_int_t netlist_edges;
igraph_vector_int_init_array(&netlist_edges, edges, edge_counter);
delete[] edges;
igraph_create(&graph, &netlist_edges, nl->get_gates().size(), IGRAPH_UNDIRECTED);
igraph_vector_destroy(&netlist_edges);
igraph_vector_int_destroy(&netlist_edges);

/* remove double edges */
igraph_simplify(&graph, true, false, 0);

/* Louvain method without weights */
igraph_vector_t membership, modularity;
igraph_matrix_t merges;
igraph_vector_init(&membership, 1);
igraph_vector_init(&modularity, 1);
igraph_matrix_init(&merges, 1, 1);
igraph_community_fastgreedy(&graph, nullptr, &merges, &modularity, &membership);
igraph_vector_destroy(&modularity);
igraph_matrix_destroy(&merges);
igraph_vector_int_t membership;
igraph_vector_int_init(&membership, 0);
igraph_community_fastgreedy(&graph, nullptr, nullptr, nullptr, &membership);
igraph_destroy(&graph);

/* group gates by community membership */
std::map<int, std::set<Gate*>> community_sets;
for (int i = 0; i < igraph_vector_size(&membership); i++)
for (igraph_integer_t i = 0; i < igraph_vector_int_size(&membership); i++)
{
community_sets[(int)VECTOR(membership)[i]].insert(nl->get_gate_by_id(igraph_nl_id_match[i]));
community_sets[VECTOR(membership)[i]].insert(nl->get_gate_by_id(igraph_nl_id_match[i]));
}
igraph_vector_destroy(&membership);
igraph_vector_int_destroy(&membership);

return community_sets;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@ namespace hal
igraph_t graph;
std::map<int, Gate*> vertex_to_gate = get_igraph_directed(nl, &graph);

igraph_vector_t membership, csize;
igraph_vector_int_t membership;
igraph_integer_t number_of_clusters;
igraph_vector_init(&membership, 0);
igraph_vector_init(&csize, 0);
igraph_vector_int_init(&membership, 0);

// run scc
igraph_clusters(&graph, &membership, &csize, &number_of_clusters, IGRAPH_STRONG);
igraph_connected_components(&graph, &membership, nullptr, &number_of_clusters, IGRAPH_STRONG);

// map back to HAL structures
std::map<int, std::set<Gate*>> ssc_membership = get_memberships_for_hal(&graph, membership, vertex_to_gate);
std::map<int, std::set<Gate*>> ssc_membership = get_memberships_for_hal(&graph, &membership, vertex_to_gate);

// convert to set
std::vector<std::vector<Gate*>> sccs;
Expand All @@ -47,8 +46,7 @@ namespace hal

// cleanup
igraph_destroy(&graph);
igraph_vector_destroy(&membership);
igraph_vector_destroy(&csize);
igraph_vector_int_destroy(&membership);

return sccs;
}
Expand Down
30 changes: 15 additions & 15 deletions plugins/graph_algorithm/src/igraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ namespace hal
{
std::map<int, Gate*> GraphAlgorithmPlugin::get_igraph_directed(Netlist* const nl, igraph_t* graph)
{
//igraph_t graph;

// count all edges, remember in HAL one net(edge) has multiple sinks
u32 edge_counter = 0;
Expand Down Expand Up @@ -60,12 +59,12 @@ namespace hal
log_debug("graph_algorithm", "nets: {}, edge_counter: {}", nl->get_nets().size(), edge_counter);

// initialize edge vector
igraph_vector_t edges;
igraph_vector_init(&edges, 2 * edge_counter);
igraph_vector_int_t edges;
igraph_vector_int_init(&edges, 2 * edge_counter);

// we need dummy gates for input/outputs
u32 dummy_gate_counter = nl->get_gates().size() - 1;
u32 edge_vertice_counter = 0;
u32 edge_vertex_counter = 0;

for (auto net : nl->get_nets())
{
Expand All @@ -91,17 +90,17 @@ namespace hal
u32 dummy_gate = ++dummy_gate_counter;
for (const auto& dst_gate : dst_gates)
{
VECTOR(edges)[edge_vertice_counter++] = dummy_gate;
VECTOR(edges)[edge_vertice_counter++] = dst_gate->get_id() - 1;
VECTOR(edges)[edge_vertex_counter++] = dummy_gate;
VECTOR(edges)[edge_vertex_counter++] = dst_gate->get_id() - 1;

log_debug("graph_algorithm", "input_gate: {} --> {}: {}", dummy_gate, dst_gate->get_id() - 1, dst_gate->get_name().c_str());
}
}
// if gate has no dsts --> add dummy node
else if (dst_gates.size() == 0)
{
VECTOR(edges)[edge_vertice_counter++] = src_gate->get_id() - 1;
VECTOR(edges)[edge_vertice_counter++] = ++dummy_gate_counter;
VECTOR(edges)[edge_vertex_counter++] = src_gate->get_id() - 1;
VECTOR(edges)[edge_vertex_counter++] = ++dummy_gate_counter;

log_debug("graph_algorithm", "{}: {} --> {} output\n", src_gate->get_name().c_str(), src_gate->get_id() - 1, dummy_gate_counter);
}
Expand All @@ -110,27 +109,28 @@ namespace hal
{
for (const auto& dst_gate : dst_gates)
{
VECTOR(edges)[edge_vertice_counter++] = src_gate->get_id() - 1;
VECTOR(edges)[edge_vertice_counter++] = dst_gate->get_id() - 1;
VECTOR(edges)[edge_vertex_counter++] = src_gate->get_id() - 1;
VECTOR(edges)[edge_vertex_counter++] = dst_gate->get_id() - 1;

log_debug("graph_algorithm", "{}: {} --> {}: {}", src_gate->get_name().c_str(), src_gate->get_id() - 1, dst_gate->get_id() - 1, dst_gate->get_name().c_str());
}
}
}

igraph_create(graph, &edges, 0, IGRAPH_DIRECTED);
igraph_vector_int_destroy(&edges);

// map with vertice id to hal-gate
std::map<int, Gate*> vertice_to_gate;
std::map<int, Gate*> vertex_to_gate;
for (auto const& gate : nl->get_gates())
{
vertice_to_gate[gate->get_id() - 1] = gate;
vertex_to_gate[gate->get_id() - 1] = gate;
}

return vertice_to_gate;
return vertex_to_gate;
}

std::map<int, std::set<Gate*>> GraphAlgorithmPlugin::get_memberships_for_hal(igraph_t* graph, igraph_vector_t membership, std::map<int, Gate*> vertex_to_gate)
std::map<int, std::set<Gate*>> GraphAlgorithmPlugin::get_memberships_for_hal(igraph_t* graph, igraph_vector_int_t *membership, std::map<int, Gate*> vertex_to_gate)
{
// map back to HAL structures
int vertices_num = (int)igraph_vcount(graph);
Expand All @@ -141,7 +141,7 @@ namespace hal
auto gate = vertex_to_gate[i];
if (gate == nullptr)
continue;
community_sets[VECTOR(membership)[i]].insert(gate);
community_sets[VECTOR(*membership)[i]].insert(gate);
}
return community_sets;
}
Expand Down

0 comments on commit 683dca5

Please sign in to comment.