Skip to content

Commit

Permalink
added function in preparation for new NetlistTraversalDecorator funct…
Browse files Browse the repository at this point in the history
…ions to ease creation of a sequential graph abstraction
  • Loading branch information
SJulianS committed May 10, 2024
1 parent ff35022 commit 48a8b6f
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,15 @@ namespace hal
*/
Result<std::monostate> add_edges(const std::vector<std::pair<u32, u32>>& edges);

/**
* Add edges between the specified pairs of source and destination gates to the netlist graph.
* The vertices must already exist in the graph.
*
* @param[in] edges - The edges to add as a map from source gate to its destination gates.
* @returns OK on success, an error otherwise.
*/
Result<std::monostate> add_edges(const std::map<Gate*, std::set<Gate*>>& edges);

/**
* Delete edges between the specified pairs of source and destination gates from the netlist graph.
*
Expand Down
24 changes: 24 additions & 0 deletions plugins/graph_algorithm/python/python_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,30 @@ namespace hal
:rtype: bool
)");

py_netlist_graph.def(
"add_edges",
[](graph_algorithm::NetlistGraph& self, const std::map<Gate*, std::set<Gate*>>& edges) -> bool {
auto res = self.add_edges(edges);
if (res.is_ok())
{
return true;
}
else
{
log_error("python_context", "error encountered while adding edges:\n{}", res.get_error().get());
return false;
}
},
py::arg("edges"),
R"(
Add edges between the specified pairs of source and destination gates to the netlist graph.
The vertices must already exist in the graph.
:param dict[hal_py.Gate,set[hal_py.Gate]] edges: The edges to add as a dict from source gate to its destination gates.
:returns: ``True`` on success, ``False`` otherwise.
:rtype: bool
)");

py_netlist_graph.def(
"delete_edges",
[](graph_algorithm::NetlistGraph& self, const std::vector<std::pair<Gate*, Gate*>>& edges) -> bool {
Expand Down
54 changes: 54 additions & 0 deletions plugins/graph_algorithm/src/netlist_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,60 @@ namespace hal
return OK({});
}

Result<std::monostate> NetlistGraph::add_edges(const std::map<Gate*, std::set<Gate*>>& edges)
{
u32 edge_count = 0;
for (const auto& [_, dst_gates] : edges)
{
edge_count += dst_gates.size();
}

igraph_vector_int_t e_vec;
if (auto err = igraph_vector_int_init(&e_vec, 2 * edge_count); err != IGRAPH_SUCCESS)
{
return ERR(igraph_strerror(err));
}

u32 edge_index = 0;
for (const auto& [src_gate, dst_gates] : edges)
{
u32 src_vertex;
if (auto it = m_gates_to_nodes.find(src_gate); it != m_gates_to_nodes.end())
{
src_vertex = it->second;
}
else
{
igraph_vector_int_destroy(&e_vec);
return ERR("no node for gate '" + src_gate->get_name() + "' with ID " + std::to_string(src_gate->get_id()) + " exists in graph for netlist with ID "
+ std::to_string(m_nl->get_id()));
}

for (auto* dst_gate : dst_gates)
{
if (auto it = m_gates_to_nodes.find(dst_gate); it != m_gates_to_nodes.end())
{
VECTOR(e_vec)[edge_index++] = src_vertex;
VECTOR(e_vec)[edge_index++] = it->second;
}
else
{
igraph_vector_int_destroy(&e_vec);
return ERR("no node for gate '" + dst_gate->get_name() + "' with ID " + std::to_string(dst_gate->get_id()) + " exists in graph for netlist with ID "
+ std::to_string(m_nl->get_id()));
}
}
}

if (auto err = igraph_add_edges(&m_graph, &e_vec, nullptr); err != IGRAPH_SUCCESS)
{
igraph_vector_int_destroy(&e_vec);
return ERR(igraph_strerror(err));
}

return OK({});
}

Result<std::monostate> NetlistGraph::delete_edges(const std::vector<std::pair<Gate*, Gate*>>& edges)
{
igraph_vector_int_t e_vec;
Expand Down

0 comments on commit 48a8b6f

Please sign in to comment.