首页 > 其他分享 >pybind11

pybind11

时间:2022-11-30 10:13:20浏览次数:37  
标签:name arra int print pybind11 go sb

pybind11.hpp

pybind11.cpp

#include "pybind11.hpp"
#include <stdio.h>
#include <iostream>

using namespace std;
namespace py = pybind11;

class Animal{
public:
    Animal(const string& name){
        this->name_ = name;
    }

    virtual string go(int repeats) = 0;

protected:
    string name_;
};

class Dog : public Animal{
public:
    Dog(const string& name): Animal(name){
    }

    virtual ~Dog(){
        printf("析构了狗狗.~\n");
    }

    virtual string go(int repeats) override{
        string result;
        for(int i = 0; i < repeats; ++i){
            result += "wang! ";
        }
        return this->name_ + " ::: " + result;
    }
};

string call_animal_go(Animal* animal, int repeats){
    printf("Execute call_animal_go\n");
    return animal->go(repeats);
}

int add(int a, int b){
    printf("add a = %d, b = %d\n", a, b);
    return a + b;
}

// 对numpy的操作
void print_ndarray(const py::array& arra){

    printf("ndim = %d\n", arra.ndim());
    printf("size = %d\n", arra.size());

    for(int i = 0; i < arra.ndim(); ++i)
        printf("arra.shape[%d] = %d\n", i, arra.shape(i));
    
    float* ptr = (float*)arra.data<float>(0);
    for(int i = 0; i < arra.size(); ++i){
        printf("%f ", ptr[i]);
    }
    printf("\n");
}

//PYBIND11_MODULE(name, variable){
//}
PYBIND11_MODULE(sb, m) {

    m.doc() = R"doc(
        这里是介绍 ~~~
    )doc";

    m.attr("name") = "小王";
    m.def("add", &add, "加法函数,实现两个数的加法", py::arg("a"), py::arg("b")=0);

    // 声明基类,基类是抽象类,没有构造函数
    py::class_<Animal>(m, "Animal")
        .def("go", &Animal::go);

    // 声明子类,但是子类有构造函数,并有一个参数
    py::class_<Dog, Animal>(m, "Dog")
        .def(py::init<const string&>());

    // torch._C
    m.def("call_animal_go", &call_animal_go, "调用Animal的go方法");
    m.def("print_ndarray", &print_ndarray, "打印ndarray里边的信息", py::arg("arra"));
}
View Code

pybind11.test

import sb
import numpy as np

print(sb.name)
print(sb.__doc__)

print(sb.add.__doc__)
print(sb.add(5))

dog = sb.Dog("小狗")
print(dog.go(3))
print(sb.call_animal_go(dog, 3))

# shared_ptr   引用计数,  循环引用(导致两个或多个对象都无法达成计数为0,无法卸载),使用弱引用解决
# 引用计数为0时,释放对象
# python的所有对象,都是引用计数的技术实现的
# weak_ptr     弱引用     不占用计数的引用 , 需要用的时候提申请(如果对象没了,就返回false)
a = sb.Dog("别的东西")
b = a
dog = None
print("Step~~~~~~~")

arra = np.arange(25).reshape(5, 5).astype(np.float32)
sb.print_ndarray(arra)
View Code

 

标签:name,arra,int,print,pybind11,go,sb
From: https://www.cnblogs.com/xiaoruirui/p/16937576.html

相关文章

  • python安装报错error: pybind11 2.10+ requires MSVC 2017 or newer
    pip安装paddleocr时报错,提示要2017或更高,c:\users\administrator\appdata\local\temp\pip-build-env-86xs2ijc\overlay\lib\site-packages\pybind11\include\pybind11\det......
  • 使用pybind11 来实现python 调用c++
    参考https://blog.csdn.net/luolinll1212/article/details/1060619431c++实现2编写pybind11的代码#include"pybind11/pybind11.h"#include"pybind11/numpy.h"......
  • pybind11使用(3) 使用stl容器
    假设c++侧读到的数据是一个结构体,定义如下:structAirwayData{intuavId;introuteId;std::vector<int>longitude;std::vector<int>latitude;......
  • Pybind11 揭秘。 Ch 4.1:仅位置参数
    Pybind11揭秘。Ch4.1:仅位置参数Python不支持C++中的函数重载(具有相同函数名称的不同函数签名)。尽管如此,Python确实提供了一组少数语法,包括默认值和kwargs,以允许根......
  • 混合编程:如何用pybind11调用C++
    摘要:在实际开发过程中,免不了涉及到混合编程,比如,对于python这种脚本语言,性能还是有限的,在一些对性能要求高的情景下面,还是需要使用c/c++来完成。本文分享自华为云社区《混......
  • pybind11使用记录---ubuntu下使用cmake编译c++工程为python库
    前言:因为最近c++下的工程需要在python下调用,所以需要把c++编译成可供python调用的库,记录一下具体做法:编译c++有多种方法,因为我的是cmake构建的工程,所以直接在cmake的......
  • Windows10 pybind11 opencv 和numpy相互转换 (tcy)
      利用pybind11实现python和C++图像之间的相互调用。将Mat类引入python中。 图像相互转换通过左值引用及智能指针实现。封装了类操作及8个函数(Mat和numpy......
  • Pybind11实现python调取C++
    1、一些处理矩阵运算,图像处理算法,直接采用python实现可能速度稍微慢,效率不高,或者为了直接在python中调用其他C++第三方库。图像,矩阵在python中通常表示为numpy.ndarray,......