Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pybind11 demo #468

Open
ChunelFeng opened this issue Jan 24, 2025 · 3 comments
Open

pybind11 demo #468

ChunelFeng opened this issue Jan 24, 2025 · 3 comments
Labels
good first issue Good for newcomers

Comments

@ChunelFeng
Copy link
Owner

cmake_minimum_required(VERSION 3.12)
project(example)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

find_package(PythonLibs REQUIRED)
find_package(pybind11 CONFIG REQUIRED)

include_directories(my_module PUBLIC ${pybind11_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS})
pybind11_add_module(my_module SHARED main.cpp)

target_link_libraries(my_module PRIVATE pybind11::pybind11 ${PYTHON_LIBRARIES})
#include <pybind11/pybind11.h>
namespace py = pybind11;

PYBIND11_EXPORT void pnt() {
    printf("hello world \n");
}

PYBIND11_EXPORT int add(int i, int j) {
    return i + j;
}

PYBIND11_MODULE(my_module, m) {
    m.def("add", &add, "A function that adds two numbers");
    m.def("pnt", &pnt, "my print");
}
@ChunelFeng ChunelFeng reopened this Jan 24, 2025
@ChunelFeng ChunelFeng changed the title pyCGraph pybind11 demo Jan 25, 2025
@ChunelFeng
Copy link
Owner Author

ChunelFeng commented Jan 26, 2025

from datetime import datetime
import time

from PyCGraph import GNode, GPipeline, CStatus

class MyGNode(GNode):
    def __init__(self):
        super().__init__()
        print("new")

    def init(self):
        print("init....")
        return CStatus()

    def run(self):
        print("[{0}] run {1}".format(datetime.now(), self.getName()))
        time.sleep(1)
        return CStatus()

    def __del__(self):
        print("delete")


if __name__ == '__main__':
    ppln = GPipeline()

    a = MyGNode()
    b = MyGNode()

    ppln.registerGElement(a, set(), "nodeA", 1)
    ppln.registerGElement(b, {a}, "nodeB", 1)

    ppln.process()

@ChunelFeng ChunelFeng added the good first issue Good for newcomers label Jan 27, 2025
@ChunelFeng
Copy link
Owner Author

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include "CGraph.h"
#include "wrapper/PyWrapperInclude.h"

using namespace CGraph;
namespace py = pybind11;

class MyGParamCpp : public GParam {
public:
    explicit MyGParamCpp() : GParam() {};
    ~MyGParamCpp() override {};

    CStatus setup() override {
        printf("enter setup .. \n");
        return CStatus();
    }

    CVoid reset(const CStatus& curSts) override {
        printf("enter reset .. \n");
        return;
    }

    int prm1 {1};
    int prm2 {2};
};

PYBIND11_MODULE(PyCGraph, m) {
    py::class_<CStatus>(m, "CStatus")
        .def(py::init<>())
        .def(py::init<int, const std::string&>())
        .def("getCode", &CStatus::getCode)
        .def("getInfo", &CStatus::getInfo)
        .def("isOK", &CStatus::isOK);

    py::enum_<GMultiConditionType>(m, "GMultiConditionType")
        .value("SERIAL", GMultiConditionType::SERIAL)
        .value("PARALLEL", GMultiConditionType::PARALLEL)
        .export_values();

    py::class_<GParam, PywGParam, std::unique_ptr<GParam, py::nodelete> >(m, "GParam")
        .def(py::init<>());

    py::class_<MyGParamCpp, GParam, std::unique_ptr<MyGParamCpp, py::nodelete> >(m, "GParamCpp")
         .def(py::init<>())
         .def_readwrite("prm1", &MyGParamCpp::prm1)
         .def_readwrite("prm2", &MyGParamCpp::prm2)
         ;

    py::class_<PyGPipeline, std::unique_ptr<PyGPipeline, py::nodelete> >(m, "GPipeline")
        .def(py::init<>())
        .def("init", &PyGPipeline::init)
        .def("run", &PyGPipeline::run, py::call_guard<py::gil_scoped_release>())
        .def("process", &PyGPipeline::process, py::call_guard<py::gil_scoped_release>(),
             py::arg("runTimes") = 1)
        .def("destroy", &PyGPipeline::destroy)
        .def("registerGElement", &PyGPipeline::registerGElement,
            py::arg("element"),
            py::arg("depends") = GElementPtrSet{},
            py::arg("name") = CGRAPH_EMPTY,
            py::arg("loop") = CGRAPH_DEFAULT_LOOP_TIMES,
            "register a GElement with dependencies, name, and loop count.");

    py::class_<GElement, PywGElement, std::unique_ptr<GElement, py::nodelete> >(m, "GElement")
        .def(py::init<>())
        .def("createGParam", &GElement::__createGParam_4py)
        .def("getGParam", &GElement::__getGParam_4py)
        .def("getName", &GElement::getName)
        .def("setName", &GElement::setName)
        .def("addDependGElements", &GElement::addDependGElements,
            py::arg("elements"))
        .def("setLoop", &GElement::setLoop);

    py::class_<GNode, PywGNode, GElement, std::unique_ptr<GNode, py::nodelete> >(m, "GNode")
        .def(py::init<>());

    py::class_<PyGCluster, GElement, std::unique_ptr<PyGCluster, py::nodelete> >(m, "GCluster")
        .def(py::init<>())
        .def("addGElements", &PyGCluster::addGElements,
            py::arg("elements"));

    py::class_<PyGRegion, GElement, std::unique_ptr<PyGRegion, py::nodelete> >(m, "GRegion")
        .def(py::init<>())
        .def("addGElements", &PyGRegion::addGElements,
            py::arg("elements"));

    py::class_<PywGCondition, GElement, std::unique_ptr<PywGCondition, py::nodelete> >(m, "GCondition")
        .def(py::init<>())
        .def("addGElements", &PywGCondition::addGElements,
            py::arg("elements"));

    py::class_<PyGMultiCondition<CGraph::GMultiConditionType::SERIAL>, GElement>(m, "GSerialMultiCondition")
        .def(py::init<>())
        .def("addGElements", &PyGMultiCondition<CGraph::GMultiConditionType::SERIAL>::addGElements,
            py::arg("elements"));

    py::class_<PyGMultiCondition<CGraph::GMultiConditionType::PARALLEL>, GElement>(m, "GParallelMultiCondition")
        .def(py::init<>())
        .def("addGElements", &PyGMultiCondition<CGraph::GMultiConditionType::PARALLEL>::addGElements,
            py::arg("elements"));
}

@ChunelFeng
Copy link
Owner Author

搞一个包含纯虚的中间基类试试看

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant