Skip to content

Commit b62ffc9

Browse files
committed
[example] add python GRegion demo
1 parent 82b8d34 commit b62ffc9

File tree

4 files changed

+61
-6
lines changed

4 files changed

+61
-6
lines changed

python/PyCGraph.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,15 @@ PYBIND11_MODULE(PyCGraph, m) {
5656
.def("setLoop", &GElement::setLoop);
5757

5858
py::class_<GNode, PywGNode, GElement, std::unique_ptr<GNode, py::nodelete> >(m, "GNode")
59-
.def(py::init<>());
59+
.def(py::init<const std::string&>(),
60+
py::arg("name"))
61+
.def(py::init<const std::string&, int>(),
62+
py::arg("name"),
63+
py::arg("loop"))
64+
.def(py::init<const GElementPtrSet&, const std::string&, int>(),
65+
py::arg("depends") = GElementPtrSet{},
66+
py::arg("name") = CGRAPH_EMPTY,
67+
py::arg("loop") = CGRAPH_DEFAULT_LOOP_TIMES);
6068

6169
py::class_<PyGCluster, GElement, std::unique_ptr<PyGCluster, py::nodelete> >(m, "GCluster")
6270
.def(py::init<>())

python/tutorial/T02-Cluster.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@
1212
from MyPyGNode.MyPyNode2 import MyPyNode2
1313

1414
def tutorial_cluster():
15-
b1, b2, b3 = MyPyNode1(), MyPyNode1(), MyPyNode2()
16-
b1.setName("nodeB1")
17-
b2.setName("nodeB2")
18-
b2.setLoop(3)
19-
b3.setName("nodeB3")
15+
b1, b2, b3 = MyPyNode1("nodeB1"), MyPyNode1("nodeB2", 3), MyPyNode2("nodeB3")
2016
b_cluster = GCluster()
2117
b_cluster.addGElements([b1, b2, b3])
2218

python/tutorial/T03-Region.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
@Author: Chunel
3+
@Contact: chunel@foxmail.com
4+
@File: T03-Region
5+
@Time: 2025/2/24 22:52
6+
@Desc:
7+
"""
8+
9+
from PyCGraph import GNode, GPipeline, GRegion, CStatus
10+
11+
from MyPyGNode.MyPyNode1 import MyPyNode1
12+
from MyPyGNode.MyPyNode2 import MyPyNode2
13+
14+
def tutorial_region():
15+
b1 = MyPyNode1("nodeB1")
16+
b2 = MyPyNode2({b1}, "nodeB2", 2)
17+
b3 = MyPyNode1({b1}, "nodeB3", 1)
18+
b4 = MyPyNode1({b2, b3}, "nodeB4", 1)
19+
b_region = GRegion()
20+
b_region.addGElements([b1, b2, b3, b4])
21+
22+
pipeline = GPipeline()
23+
a, c, d = MyPyNode1(), MyPyNode2(), MyPyNode1()
24+
25+
pipeline.registerGElement(a, set(), "nodeA")
26+
pipeline.registerGElement(b_region, {a}, "regionB", 2)
27+
pipeline.registerGElement(c, {b_region}, "nodeC")
28+
29+
pipeline.process()
30+
31+
32+
if __name__ == '__main__':
33+
tutorial_region()

python/wrapper/PywGNode.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,24 @@
1616
namespace py = pybind11;
1717

1818
class PywGNode : public CGraph::GNode {
19+
public:
20+
explicit PywGNode(const std::string& name) {
21+
setName(name);
22+
}
23+
24+
explicit PywGNode(const std::string& name, int loop) {
25+
setName(name);
26+
setLoop(loop);
27+
}
28+
29+
explicit PywGNode(const CGraph::GElementPtrSet& depends = CGraph::GElementPtrSet{},
30+
const std::string& name = CGraph::CGRAPH_EMPTY,
31+
int loop = CGraph::CGRAPH_DEFAULT_LOOP_TIMES) {
32+
addDependGElements(depends);
33+
setLoop(loop);
34+
setName(name);
35+
}
36+
1937
protected:
2038
CStatus init() override {
2139
PYBIND11_OVERLOAD(CStatus, GNode, init);

0 commit comments

Comments
 (0)