python 通过 pybind11 使用Eigen加速代码的步骤


Posted in Python onDecember 07, 2020

python是很容易上手的编程语言,但是有些时候使用python编写的程序并不能保证其运行速度(例如:while 和 for),这个时候我们就需要借助c++等为我们的代码提速。下面是我使用pybind11调用c++的Eigen库的简单介绍:

第一步:准备系统和IDE:

Windows 10 
vs2015 (用于调试c++代码)
vscode (调试python代码)

第二步:python虚拟环境:

1.创建虚拟python虚拟环境: 在vscode的terminal中执行 

python -m venv env

2.下载 Eigen: 将Eigen解压到当前目录命名为 eigen-3.3.8
3.在vscode的terminal中激活虚拟环境:

 ./env/Scripts/Activate.ps1

python 通过 pybind11 使用Eigen加速代码的步骤

4.安装pybind11:

pip install pybind11

5.安装numpy==1.19.3(使用1.19.4可能会有问题)

pip install numpy==1.19.3 

第三步:使用vs2015编写cpp_python.cpp, 并保证没有bug

#include <Eigen/Dense>
using namespace std
using namespace Eigen
MatrixXd add_mat(MatrixXd A_mat, MatrixXd B_mat)
{
  return A_mat + B_mat;
}

第四步:使用pybind11为cpp_python.cpp添加python接口

// cpp_python.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <pybind11/pybind11.h>
#include <pybind11/eigen.h>
#include<pybind11/numpy.h>
#include<fstream>
#include<iostream>
#include <Eigen/Dense>
using namespace std;
using namespace Eigen;
 
MatrixXd add_mat(MatrixXd A_mat, MatrixXd B_mat)
{
    return A_mat + B_mat;
}
 
namespace py = pybind11;
PYBIND11_MODULE(add_mat_moudle, m)
{
    m.doc() = "Matrix add";//解释说明
    m.def("mat_add_py"/*在pyhon中使用的函数名*/, &add_mat);
}

第五步:设置setup.py用来编译c++代码

from setuptools import setup
from setuptools import Extension

add_mat_module = Extension(name='add_mat_moudle', # 模块名称
              sources=['cpp_python.cpp'],  # 源码
              include_dirs=[r'.\eigen-3.3.8',
                    r'.\env\Scripts',   # 依赖的第三方库的头文件
                     r'.\env\Lib\site-packages\pybind11\include']
              )

setup(ext_modules=[add_mat_module])

第六步:编译测试

python 通过 pybind11 使用Eigen加速代码的步骤

注意:我的cpp_python.cpp和setup.py是在同一个文件夹下。

执行: "python .\setup.py build_ext --inplace"就会得下面的结果,生成.pyd文件表明我们已经编译成功。

python 通过 pybind11 使用Eigen加速代码的步骤

运行测试:

python 通过 pybind11 使用Eigen加速代码的步骤

以上就是python 通过 pybind11 使用Eigen加速代码的步骤的详细内容,更多关于python 加速代码的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
python处理json数据中的中文
Mar 06 Python
Python随机生成数模块random使用实例
Apr 13 Python
Python发送email的3种方法
Apr 28 Python
Python 对象中的数据类型
May 13 Python
利用python实现简单的循环购物车功能示例代码
Jul 05 Python
python虚拟环境virtualenv的安装与使用
Sep 21 Python
深入理解Django的自定义过滤器
Oct 17 Python
Python 学习教程之networkx
Apr 15 Python
将python文件打包成EXE应用程序的方法
May 22 Python
python os模块简单应用示例
May 23 Python
Python多线程爬取豆瓣影评API接口
Oct 22 Python
Python字典实现伪切片功能
Oct 28 Python
Python中BeautifulSoup通过查找Id获取元素信息
Dec 07 #Python
BeautifulSoup中find和find_all的使用详解
Dec 07 #Python
python爬虫beautifulsoup解析html方法
Dec 07 #Python
python可视化 matplotlib画图使用colorbar工具自定义颜色
Dec 07 #Python
用ldap作为django后端用户登录验证的实现
Dec 07 #Python
Django中使用Celery的方法步骤
Dec 07 #Python
python集合的新增元素方法整理
Dec 07 #Python
You might like
在PHP中利用XML技术构造远程服务(下)
2006/10/09 PHP
php array_flip() 删除数组重复元素
2009/01/14 PHP
如何用C语言编写PHP扩展的详解
2013/06/13 PHP
php格式化金额函数分享
2015/02/02 PHP
PHP获取一年中每个星期的开始和结束日期的方法
2015/02/12 PHP
用php来限制每个ip每天浏览页面数量的实现思路
2015/02/24 PHP
php中使用in_array() foreach array_search() 查找数组是否包含时的性能对比
2015/04/14 PHP
PDO::getAttribute讲解
2019/01/28 PHP
EXTJS内使用ACTIVEX控件引起崩溃问题的解决方法
2010/03/31 Javascript
快速获取/设置iframe内对象元素的几种js实现方法
2016/05/20 Javascript
React服务端渲染(总结)
2017/07/01 Javascript
微信小程序promsie.all和promise顺序执行
2017/10/27 Javascript
javascript连接mysql与php通过odbc连接任意数据库的实例
2017/12/27 Javascript
vuex直接赋值的三种方法总结
2018/09/16 Javascript
jQuery实现的简单歌词滚动功能示例
2019/01/07 jQuery
在vue项目中promise解决回调地狱和并发请求的问题
2020/11/09 Javascript
用Python操作字符串之rindex()方法的使用
2015/05/19 Python
Python实现的弹球小游戏示例
2017/08/01 Python
python中numpy的矩阵、多维数组的用法
2018/02/05 Python
python3操作微信itchat实现发送图片
2018/02/24 Python
Python和Go语言的区别总结
2019/02/20 Python
python登录WeChat 实现自动回复实例详解
2019/05/28 Python
Python OpenCV中的resize()函数的使用
2019/06/20 Python
python3.5 cv2 获取视频特定帧生成jpg图片
2019/08/28 Python
浅谈Django+Gunicorn+Nginx部署之路
2019/09/11 Python
Python3.7基于hashlib和Crypto实现加签验签功能(实例代码)
2019/12/04 Python
Sperry官网:帆船鞋创始品牌
2016/09/07 全球购物
庆元旦迎新年广播稿
2014/02/18 职场文书
演讲稿格式
2014/04/30 职场文书
餐饮服务食品安全责任书
2014/07/25 职场文书
幼儿园教师的自我评价范文
2014/09/17 职场文书
小学公民道德宣传日活动总结
2015/03/23 职场文书
2015年护士节活动策划方案
2015/05/04 职场文书
社区党务工作总结2015
2015/05/19 职场文书
Python使用pyecharts控件绘制图表
2022/06/05 Python
分享很少见很有用的SQL功能CORRESPONDING
2022/08/05 MySQL