11 #pragma GCC diagnostic push
12 #pragma GCC diagnostic ignored "-Wshadow"
14 #pragma clang diagnostic ignored "-Wself-assign-overloaded"
15 #pragma clang diagnostic ignored "-Wnested-anon-types"
16 #pragma clang diagnostic ignored "-Wshadow-field-in-constructor-modified"
25 #include "pybind11/operators.h"
26 #include "pybind11/pybind11.h"
27 #include "pybind11/stl.h"
28 #include "pybind11/stl_bind.h"
30 #pragma GCC diagnostic pop
32 namespace py = pybind11;
35 #ifdef PYBIND11_MODULE
36 PYBIND11_MODULE(graph_algorithm, m)
38 m.doc() =
"Graph algorithms based on igraph operating on a netlist graph abstraction.";
42 py::module m(
"graph_algorithm",
"Graph algorithms based on igraph operating on a netlist graph abstraction.");
45 py::class_<GraphAlgorithmPlugin, RawPtrWrapper<GraphAlgorithmPlugin>,
BasePluginInterface> py_graph_algorithm_plugin(m,
"GraphAlgorithmPlugin");
48 The name of the plugin.
54 Get the name of the plugin.
56 :returns: The name of the plugin.
61 The version of the plugin.
67 Get the version of the plugin.
69 :returns: The version of the plugin.
74 The description of the plugin.
80 Get the description of the plugin.
82 :returns: The description of the plugin.
87 A set of plugin names that this plugin depends on.
93 Get a set of plugin names that this plugin depends on.
95 :returns: A set of plugin names that this plugin depends on.
99 py::class_<graph_algorithm::NetlistGraph, RawPtrWrapper<graph_algorithm::NetlistGraph>> py_netlist_graph(m, "NetlistGraph", R
"(
100 Holds a directed graph corresponding to a netlist.
103 py::enum_<graph_algorithm::NetlistGraph::Direction>(py_netlist_graph, "Direction", R
"(
104 The direction of exploration within the graph.
112 py_netlist_graph.def_static(
114 [](
Netlist* nl,
bool create_dummy_vertices =
false,
const std::function<
bool(
const Net*)>& filter =
nullptr) -> std::unique_ptr<graph_algorithm::NetlistGraph> {
122 log_error(
"python_context",
"error encountered while creating a graph from a netlist:\n{}", res.get_error().get());
127 py::arg(
"create_dummy_vertices") =
false,
128 py::arg(
"filter") =
nullptr,
129 R
"(Create a directed graph from a netlist. Optionally create dummy nodes at nets missing a source or destination. An optional filter can be applied to exclude undesired edges.
131 :param hal_py.Netlist nl: The netlist.
132 :param bool create_dummy_vertices: Set ``True`` to create dummy vertices, ``False`` otherwise. Defaults to ``False``.
133 :param lambda filter: An optional filter that is evaluated on every net of the netlist. Defaults to ``None``.
134 :returns: The netlist graph on success, ``None`` otherwise.
135 :rtype: graph_algorithm.NetlistGraph or None
138 py_netlist_graph.def_static(
139 "from_netlist_no_edges",
140 [](
Netlist* nl,
const std::vector<Gate*>& gates = {}) -> std::unique_ptr<graph_algorithm::NetlistGraph> {
148 log_error(
"python_context",
"error encountered while creating a graph from a netlist:\n{}", res.get_error().get());
153 py::arg(
"gates") = std::vector<Gate*>(),
154 R
"(Create an empty directed graph from a netlist, i.e., vertices for all gates are created, but no edges are added.
156 :param hal_py.Netlist nl: The netlist.
157 :param list[hal_py.Gate] gates: The gates to include in the graph. If omitted, all gates of the netlist will be included.
158 :returns: The netlist graph on success, ``None`` otherwise.
159 :rtype: graph_algorithm.NetlistGraph or None
162 py_netlist_graph.def(
165 auto res =
self.copy();
172 log_error(
"python_context",
"error encountered while copying netlist graph:\n{}", res.get_error().get());
177 Create a deep copy of the netlist graph.
179 :returns: The copied netlist graph on success, ``None`` otherwise.
180 :rtype: graph_algorithm.NetlistGraph or None
184 Get the netlist associated with the netlist graph.
186 :returns: The netlist.
187 :rtype: hal_py.Netlist
190 py_netlist_graph.def(
191 "get_gates_from_vertices",
193 auto res =
self.get_gates_from_vertices(vertices);
200 log_error(
"python_context",
"error encountered while getting gates from vertices:\n{}", res.get_error().get());
206 Get the gates corresponding to the specified list of vertices.
207 The result may contain ``None`` for dummy vertices.
209 :param list[int] vertices: A list of vertices.
210 :returns: A list of gates on success, ``None`` otherwise.
211 :rtype: list[hal_py.Gate] or None
214 py_netlist_graph.def(
215 "get_gates_from_vertices",
217 const auto res =
self.get_gates_from_vertices(vertices);
224 log_error(
"python_context",
"error encountered while getting gates from vertices:\n{}", res.get_error().get());
230 Get the gates corresponding to the specified set of vertices.
231 The result may contain ``None`` for dummy vertices.
233 :param set[int] vertices: A set of vertices.
234 :returns: A list of gates on success, ``None`` otherwise.
235 :rtype: list[hal_py.Gate] or None
238 py_netlist_graph.def(
239 "get_gates_set_from_vertices",
241 auto res =
self.get_gates_set_from_vertices(vertices);
248 log_error(
"python_context",
"error encountered while getting gates from vertices:\n{}", res.get_error().get());
254 Get the gates corresponding to the specified list of vertices.
256 :param list[int] vertices: A list of vertices.
257 :returns: A list of gates on success, ``None`` otherwise.
258 :rtype: set[hal_py.Gate] or None
261 py_netlist_graph.def(
262 "get_gates_set_from_vertices",
264 const auto res =
self.get_gates_set_from_vertices(vertices);
271 log_error(
"python_context",
"error encountered while getting gates from vertices:\n{}", res.get_error().get());
277 Get the gates corresponding to the specified set of vertices.
279 :param set[int] vertices: A set of vertices.
280 :returns: A list of gates on success, ``None`` otherwise.
281 :rtype: set[hal_py.Gate] or None
284 py_netlist_graph.def(
285 "get_gate_from_vertex",
287 const auto res =
self.get_gate_from_vertex(vertex);
294 log_error(
"python_context",
"error encountered while getting gate from vertex:\n{}", res.get_error().get());
300 Get the gates corresponding to the specified vertex.
302 :param int vertex: A vertex.
303 :returns: A gate on success, ``None`` otherwise.
304 :rtype: hal_py.Gate or None
307 py_netlist_graph.def(
308 "get_vertices_from_gates",
310 auto res =
self.get_vertices_from_gates(gates);
317 log_error(
"python_context",
"error encountered while getting vertices from gates:\n{}", res.get_error().get());
323 Get the vertices corresponding to the specified list of gates.
325 :param list[hal_py.Gate] gates: A list of gates.
326 :returns: A list of vertices on success, ``None`` otherwise.
327 :rtype: list[int] or None
330 py_netlist_graph.def(
331 "get_vertices_from_gates",
333 auto res =
self.get_vertices_from_gates(gates);
340 log_error(
"python_context",
"error encountered while getting vertices from gates:\n{}", res.get_error().get());
346 Get the vertices corresponding to the specified set of gates.
348 :param set[hal_py.Gate] gates: A set of gates.
349 :returns: A list of vertices on success, ``None`` otherwise.
350 :rtype: list[int] or None
353 py_netlist_graph.def(
354 "get_vertex_from_gate",
356 auto res =
self.get_vertex_from_gate(
g);
363 log_error(
"python_context",
"error encountered while getting vertex from gate:\n{}", res.get_error().get());
369 Get the vertex corresponding to the specified gate.
371 :param hal_py.Gate g: A gate.
372 :returns: A vertex on success, ``None`` otherwise.
377 Get the number of vertices in the netlist graph.
379 :param bool only_connected: Set ``True`` to only count vertices connected to at least one edge, ``False`` otherwise. Defaults to ``False``.
380 :returns: The number of vertices in the netlist graph.
385 Get the number of edges in the netlist graph.
387 :returns: The number of edges in the netlist graph.
391 py_netlist_graph.def(
394 auto res =
self.get_vertices(only_connected);
401 log_error(
"python_context",
"error encountered while getting vertices:\n{}", res.get_error().get());
405 py::arg(
"only_connected") =
false,
407 Get the vertices in the netlist graph.
409 :param bool only_connected: Set ``True`` to only return vertices connected to at least one edge, ``False`` otherwise. Defaults to ``False``.
410 :returns: A list of vertices on success, ``None`` otherwise.
411 :rtype: list[int] or None
414 py_netlist_graph.def(
417 auto res =
self.get_edges();
424 log_error(
"python_context",
"error encountered while getting edges:\n{}", res.get_error().get());
429 Get the edges between vertices in the netlist graph.
431 :returns: A list of edges on success, ``None`` otherwise.
432 :rtype: list[tuple(int,int)] or None
435 py_netlist_graph.def(
436 "get_edges_in_netlist",
438 auto res =
self.get_edges_in_netlist();
445 log_error(
"python_context",
"error encountered while getting edges:\n{}", res.get_error().get());
450 Get the edges between gates in the netlist corresponding to the netlist graph.
452 :returns: A list of edges on success, ``None`` otherwise.
453 :rtype: list[tuple(hal_py.Gate,hal_py.Gate)] or None
456 py_netlist_graph.def(
459 auto res =
self.add_edges(edges);
466 log_error(
"python_context",
"error encountered while adding edges:\n{}", res.get_error().get());
472 Add edges between the specified pairs of source and destination gates to the netlist graph.
473 The gates must already correspond to vertices in the graph.
475 :param list[tuple(hal_py.Gate,hal_py.Gate)] edges: The edges to add as pairs of gates.
476 :returns: ``True`` on success, ``False`` otherwise.
480 py_netlist_graph.def(
483 auto res =
self.add_edges(edges);
490 log_error(
"python_context",
"error encountered while adding edges:\n{}", res.get_error().get());
496 Add edges between the specified pairs of source and destination vertices to the netlist graph.
497 The vertices must already exist in the graph.
499 :param list[tuple(int,int)] edges: The edges to add as pairs of vertices.
500 :returns: ``True`` on success, ``False`` otherwise.
504 py_netlist_graph.def(
507 auto res =
self.add_edges(edges);
514 log_error(
"python_context",
"error encountered while adding edges:\n{}", res.get_error().get());
520 Add edges between the specified pairs of source and destination gates to the netlist graph.
521 The vertices must already exist in the graph.
523 :param dict[hal_py.Gate,set[hal_py.Gate]] edges: The edges to add as a dict from source gate to its destination gates.
524 :returns: ``True`` on success, ``False`` otherwise.
528 py_netlist_graph.def(
531 auto res =
self.delete_edges(edges);
538 log_error(
"python_context",
"error encountered while deleting edges:\n{}", res.get_error().get());
544 Delete edges between the specified pairs of source and destination gates from the netlist graph.
546 :param list[tuple(hal_py.Gate,hal_py.Gate)] edges: The edges to delete as pairs of gates.
547 :returns: ``True`` on success, ``False`` otherwise.
551 py_netlist_graph.def(
554 auto res =
self.delete_edges(edges);
561 log_error(
"python_context",
"error encountered while deleting edges:\n{}", res.get_error().get());
567 Delete edges between the specified pairs of source and destination vertices from the netlist graph.
569 :param list[tuple(int,int)] edges: The edges to delete as pairs of vertices.
570 :returns: ``True`` on success, ``False`` otherwise.
575 Print the edge list of the graph to stdout.
579 "get_connected_components",
588 log_error(
"python_context",
"error encountered while computing connected components:\n{}", res.get_error().get());
594 py::arg(
"min_size") = 0,
596 Compute the (strongly) connected components of the specified graph.
597 Returns each connected component as a list of vertices in the netlist graph.
599 :param graph_algorithm.NetlistGraph graph: The netlist graph.
600 :param bool strong: Set ``True`` to compute strongly connected components, ``False`` otherwise.
601 :param int min_size: Minimal size of a connected component to be part of the result. Set to ``0`` to include all components. Defaults to ``0``.
602 :returns: A list of strongly connected components on success, ``None`` otherwise.
603 :rtype: list[list[int]] or None
609 -> std::optional<std::vector<std::vector<u32>>> {
617 log_error(
"python_context",
"error encountered while computing neighborhood:\n{}", res.get_error().get());
622 py::arg(
"start_gates"),
624 py::arg(
"direction"),
625 py::arg(
"min_dist") = 0,
627 Compute the neighborhood of the given order for each of the specified gates within the given netlist graph.
628 For order 0, only the vertex itself is returned. For order 1, the vertex itself and all vertices that are its direct predecessors and/or successors (depending on the specified direction). For order 2, the neighborhood of order 1 plus all direct predecessors and/or successors of the vertices in order 1 are returned, etc.
629 Returns each neighborhood as a list of vertices in the netlist graph.
631 :param graph_algorithm.NetlistGraph graph: The netlist graph.
632 :param list[hal_py.Gate] start_gates: A list of gates for which to compute the neighborhood.
633 :param int order: The order of the neighborhood to compute.
634 :param graph_algorithm.NetlistGraph.Direction direction: The direction in which the neighborhood should be computed.
635 :param int min_dist: The minimum distance of the vertices to include in the result.
636 :returns: A list of neighborhoods of each of the provided start gates (in order) on success, ``None`` otherwise.
637 :rtype: list[list[int]] or None
643 -> std::optional<std::vector<std::vector<u32>>> {
651 log_error(
"python_context",
"error encountered while computing neighborhood:\n{}", res.get_error().get());
656 py::arg(
"start_vertices"),
658 py::arg(
"direction"),
659 py::arg(
"min_dist") = 0,
661 Compute the neighborhood of the given order for each of the specified vertices within the given netlist graph.
662 For order 0, only the vertex itself is returned. For order 1, the vertex itself and all vertices that are its direct predecessors and/or successors (depending on the specified direction). For order 2, the neighborhood of order 1 plus all direct predecessors and/or successors of the vertices in order 1 are returned, etc.
663 Returns each neighborhood as a list of vertices in the netlist graph.
665 :param graph_algorithm.NetlistGraph graph: The netlist graph.
666 :param list[int] start_vertices: A list of vertices for which to compute the neighborhood.
667 :param int order: The order of the neighborhood to compute.
668 :param graph_algorithm.NetlistGraph.Direction direction: The direction in which the neighborhood should be computed.
669 :param int min_dist: The minimum distance of the vertices to include in the result.
670 :returns: A list of neighborhoods of each of the provided start vertices (in order) on success, ``None`` otherwise.
671 :rtype: list[list[int]] or None
675 "get_shortest_paths",
677 -> std::optional<std::vector<std::vector<u32>>> {
685 log_error(
"python_context",
"error encountered while computing shortest paths:\n{}", res.get_error().get());
690 py::arg(
"from_gate"),
692 py::arg(
"direction"),
694 Compute a shortest path from the specified ``from_gate`` to each of the given ``to_gates`` by traversing in the provided direction.
695 Returns one shortest path for each end gate, even if multiple shortest paths exist.
696 Each shortest path is given as a list of vertices in the order of traversal.
698 :param graph_algorithm.NetlistGraph graph: The netlist graph.
699 :param hal_py.Gate from_gate: The start gate of the shortest path.
700 :param list[hal_py.Gate] to_gates: A list of end gates of the shortest path.
701 :param graph_algorithm.NetlistGraph.Direction direction: The direction in which to compute the shortest paths starting at the ``from_gate``.
702 :returns: The shortest paths in order of the ``to_gates`` on success, an error otherwise.
703 :rtype: list[list[int]] or None
707 "get_shortest_paths",
709 -> std::optional<std::vector<std::vector<u32>>> {
717 log_error(
"python_context",
"error encountered while computing shortest paths:\n{}", res.get_error().get());
722 py::arg(
"from_vertex"),
723 py::arg(
"to_vertices"),
724 py::arg(
"direction"),
726 Compute a shortest path from the specified ``from_vertex`` to each of the given ``to_vertices`` by traversing in the provided direction.
727 Returns one shortest path for each end vertex, even if multiple shortest paths exist.
728 Each shortest path is given as a list of vertices in the order of traversal.
730 :param graph_algorithm.NetlistGraph graph: The netlist graph.
731 :param int from_vertex: The start vertex of the shortest path.
732 :param list[int] to_vertices: A list of end vertices of the shortest path.
733 :param graph_algorithm.NetlistGraph.Direction direction: The direction in which to compute the shortest paths starting at the ``from_vertex``.
734 :returns: The shortest paths in order of the ``to_vertices`` on success, an error otherwise.
735 :rtype: list[list[int]] or None
739 "get_all_shortest_paths",
741 -> std::optional<std::vector<std::vector<u32>>> {
749 log_error(
"python_context",
"error encountered while computing all shortest paths:\n{}", res.get_error().get());
754 py::arg(
"from_gate"),
756 py::arg(
"direction"),
758 Compute shortest paths from the specified ``from_gate`` to each of the given ``to_gates`` by traversing in the provided direction.
759 Returns all shortest paths for each end gate.
760 Each shortest path is given as a list of vertices in the order of traversal.
762 :param graph_algorithm.NetlistGraph graph: The netlist graph.
763 :param hal_py.Gate from_gate: The start gate of the shortest path.
764 :param list[hal_py.Gate] to_gates: A list of end gates of the shortest path.
765 :param graph_algorithm.NetlistGraph.Direction direction: The direction in which to compute the shortest paths starting at the ``from_gate``.
766 :returns: The shortest paths in order of the ``to_gates`` on success, an error otherwise.
767 :rtype: list[list[int]] or None
771 "get_all_shortest_paths",
773 -> std::optional<std::vector<std::vector<u32>>> {
781 log_error(
"python_context",
"error encountered while computing all shortest paths:\n{}", res.get_error().get());
786 py::arg(
"from_vertex"),
787 py::arg(
"to_vertices"),
788 py::arg(
"direction"),
790 Compute shortest paths from the specified ``from_vertex`` to each of the given ``to_vertices`` by traversing in the provided direction.
791 Returns all shortest paths for each end gate.
792 Each shortest path is given as a list of vertices in the order of traversal.
794 :param graph_algorithm.NetlistGraph graph: The netlist graph.
795 :param int from_vertex: The start vertex of the shortest path.
796 :param list[int] to_vertices: A list of end vertices of the shortest path.
797 :param graph_algorithm.NetlistGraph.Direction direction: The direction in which to compute the shortest paths starting at the ``from_vertex``.
798 :returns: The shortest paths in order of the ``to_vertices`` on success, an error otherwise.
799 :rtype: list[list[int]] or None
804 [](
const graph_algorithm::NetlistGraph* graph,
const std::vector<Gate*>& subgraph_gates) -> std::optional<std::unique_ptr<graph_algorithm::NetlistGraph>> {
812 log_error(
"python_context",
"error encountered while computing subgraph:\n{}", res.get_error().get());
817 py::arg(
"subgraph_gates"),
819 Compute the subgraph induced by the specified gates, including all edges between the corresponding vertices.
821 :param graph_algorithm.NetlistGraph graph: The netlist graph.
822 :param list[hal_py.Gate] subgraph_gates: A list of gates that make up the subgraph.
823 :returns: The subgraph as a new netlist graph on success, ``None`` otherwise.
824 :rtype: graph_algorithm.NetlistGraph or None
829 [](
const graph_algorithm::NetlistGraph* graph,
const std::set<Gate*>& subgraph_gates) -> std::optional<std::unique_ptr<graph_algorithm::NetlistGraph>> {
837 log_error(
"python_context",
"error encountered while computing subgraph:\n{}", res.get_error().get());
842 py::arg(
"subgraph_gates"),
844 Compute the subgraph induced by the specified gates, including all edges between the corresponding vertices.
846 :param graph_algorithm.NetlistGraph graph: The netlist graph.
847 :param set[hal_py.Gate] subgraph_gates: A set of gates that make up the subgraph.
848 :returns: The subgraph as a new netlist graph on success, ``None`` otherwise.
849 :rtype: graph_algorithm.NetlistGraph or None
854 [](
const graph_algorithm::NetlistGraph* graph,
const std::vector<u32>& subgraph_vertices) -> std::optional<std::unique_ptr<graph_algorithm::NetlistGraph>> {
862 log_error(
"python_context",
"error encountered while computing subgraph:\n{}", res.get_error().get());
867 py::arg(
"subgraph_vertices"),
869 Compute the subgraph induced by the specified vertices, including all edges between these vertices.
871 :param graph_algorithm.NetlistGraph graph: The netlist graph.
872 :param list[int] subgraph_vertices: A list of vertices that make up the subgraph.
873 :returns: The subgraph as a new netlist graph on success, ``None`` otherwise.
874 :rtype: graph_algorithm.NetlistGraph or None
879 [](
const graph_algorithm::NetlistGraph* graph,
const std::set<u32>& subgraph_vertices) -> std::optional<std::unique_ptr<graph_algorithm::NetlistGraph>> {
887 log_error(
"python_context",
"error encountered while computing subgraph:\n{}", res.get_error().get());
892 py::arg(
"subgraph_vertices"),
894 Compute the subgraph induced by the specified vertices, including all edges between these vertices.
896 :param graph_algorithm.NetlistGraph graph: The netlist graph.
897 :param set[int] subgraph_vertices: A set of vertices that make up the subgraph.
898 :returns: The subgraph as a new netlist graph on success, ``None`` otherwise.
899 :rtype: graph_algorithm.NetlistGraph or None
902 #ifndef PYBIND11_MODULE
std::string get_description() const override
Get a short description of the plugin.
std::string get_version() const override
Get the version of the plugin.
std::set< std::string > get_dependencies() const override
Get the plugin dependencies.
std::string get_name() const override
Get the name of the plugin.
A directed graph corresponding to a netlist.
Netlist * get_netlist() const
Get the netlist associated with the netlist graph.
u32 get_num_vertices(bool only_connected=false) const
Get the number of vertices in the netlist graph.
static Result< std::unique_ptr< NetlistGraph > > from_netlist(Netlist *nl, bool create_dummy_vertices=false, const std::function< bool(const Net *)> &filter=nullptr)
Create a directed graph from a netlist.
Direction
The direction of exploration within the graph.
@ ALL
Explore in both directions, i.e., treat the graph as undirected.
@ NONE
No direction, invalid default setting.
@ IN
Explore through the inputs of the current node, i.e., traverse backwards.
@ OUT
Explore through the outputs of the current node, i.e., traverse forwards.
void print() const
Print the edge list of the graph to stdout.
u32 get_num_edges() const
Get the number of edges in the netlist graph.
static Result< std::unique_ptr< NetlistGraph > > from_netlist_no_edges(Netlist *nl, const std::vector< Gate * > &gates={})
Create an empty directed graph from a netlist.
This file contains functions related to graph components.
#define log_error(channel,...)
const Module * module(const Gate *g, const NodeBoxes &boxes)
Result< std::vector< std::vector< u32 > > > get_all_shortest_paths(NetlistGraph *graph, Gate *from_gate, const std::vector< Gate * > &to_gates, NetlistGraph::Direction direction)
Compute shortest paths from the specified from_gate to each of the given to_gates by traversing in th...
Result< std::vector< std::vector< u32 > > > get_connected_components(const NetlistGraph *graph, bool strong, u32 min_size=0)
Compute the (strongly) connected components of the specified graph.
Result< std::vector< std::vector< u32 > > > get_shortest_paths(NetlistGraph *graph, Gate *from_gate, const std::vector< Gate * > &to_gates, NetlistGraph::Direction direction)
Compute a shortest path from the specified from_gate to each of the given to_gates by traversing in t...
Result< std::unique_ptr< NetlistGraph > > get_subgraph(const NetlistGraph *graph, const std::vector< Gate * > &subgraph_gates)
Compute the subgraph induced by the specified gates, including all edges between the corresponding ve...
Result< std::vector< std::vector< u32 > > > get_neighborhood(NetlistGraph *graph, const std::vector< Gate * > &start_gates, u32 order, NetlistGraph::Direction direction, u32 min_dist=0)
Compute the neighborhood of the given order for each of the specified gates within the given netlist ...
This file contains functions related to neighborhoods in graphs.
This file contains the class that holds a netlist graph.
This file contains all functions related to the HAL plugin API.
This file contains functions related to shortest paths in graphs.
This file contains functions related to subgraphs.