Pybind11:在C++中使用Pybind11转换numpy数组的问题
Pybind11: Problem of conversion of numpy array in C++ using Pybind11
我正试图从C++中获得一个Python数组,并将其放回Python函数中,同时使用带有Pybind11的C++。首先,我访问一个numpy数组的字典,然后将每个numpy数组放入一个Python函数中,该函数只计算数组中元素的总和。给我带来麻烦的代码的主要部分是:
py::array item = list_items.attr("__getitem__")(i);
//equivalent to dict.__getitem__[i]
//code runs if removing the following line
boost_mod.attr("sum_of_dict")(item).cast<int>();
//calls the function sum_of_dict in the module boost_mod having as argument a 1D numpy array
//this function computes the sum of the elements in the numpy array, which are all integers
我想我有一个转换问题,我不知道如何克服它。我看过这个文档链接,但它没有帮助。我还研究了不同的cast
可能性,以及其他类似于这篇或另一篇的帖子,但它们不适用于我的问题,因为它们不使用pybind。对于转换和使用numpy数组对象的任何帮助或提示,我们将不胜感激。非常感谢
完整代码如下:
C++代码
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <chrono>
#include <thread>
#include <Python.h>
namespace py = pybind11;
py::module boost_mod = py::module::import("boost_mod");
int parallelize(py::dict list_items, py::object q_in, py::object q_out){
unsigned int sum = 0;
unsigned int len = list_items.attr("__len__")().cast<int>();
for( unsigned int i = 0; i < len ; i++){
//PROBLEM IS HERE//
py::array item = list_items.attr("__getitem__")(i);
sum+= boost_mod.attr("sum_of_dict")(item).cast<int>();
std::this_thread::sleep_for(std::chrono::milliseconds(5));
}
return sum;
}
PYBIND11_MODULE(cpp_parallel, m) {
m.doc() = "pybind11 plugin";
m.def("parallelize", ¶llelize,
"a function");
}
Python模块,包含函数
import numpy as np
import cpp_parallel as cpp
class child(object):
def __init__(self, index):
self.nb = index
self.total = None
def sum(self, i, j):
return self.nb + self.nb
def create_dict(self):
self.list_items = {}
for i in range(self.nb):
lth = np.random.randint(1,10)
a = np.random.binomial(size=lth, n=1, p =0.6)
self.list_items[i] = a
return self.list_items
def sum_of_dict(self, element): # fonction comme eval function
a = np.sum(element)
return a
def sub(self, q_in, q_out):
//FUNCTION CALLED FROM C++//
return cpp.parallelize(self.list_items, q_in, q_out)
Python代码
from multiprocessing import Process, Queue
import time
import boost_mod as test
import cpp_parallel as cpp
q_in = Queue()
q_out = Queue()
q_in.put(True)
dict_evaluated = test.child(1000)
dict_evaluated.create_dict()
dict_evaluated.sub(q_in, q_out)
实际上,我没有正确实例化调用函数的模块。学分归Wim Lavrijsen所有。答案如下:
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
#include <omp.h>
#include <chrono>
#include <thread>
#include <Python.h>
namespace py = pybind11;
py::module boost_mod = py::module::import("boost_mod");
int parallelize(py::object child, py::object q_in, py::object q_out){
unsigned int sum = 0;
py::object items = child.attr("list_items");
unsigned int len = items.attr("__len__")().cast<int>();
#pragma omp simd reduction (+:sum)
for( unsigned int i = 0; i < len ; i++){
py::array item = items.attr("__getitem__")(i);
sum += child.attr("sum_of_dict")(item).cast<int>();
bool accord = q_in.attr("get")().cast<bool>();
if (accord == true){
q_out.attr("put")(sum);
}
accord = false;
std::this_thread::sleep_for(std::chrono::milliseconds(5));
}
return sum;
}
PYBIND11_MODULE(cpp_parallel, m) {
m.doc() = "pybind11 example plugin";
m.def("parallelize", ¶llelize,
"the function which parallelizes the evaluation");
}
儿童班:
from multiprocessing import Process, Event, Lock, Queue, Pipe
import time
import numpy as np
import cpp_parallel as cpp
class child(object):
def __init__(self, index):
self.nb = index
self.total = None
def sum(self, i, j):
return self.nb + self.nb
def create_dict(self):
self.list_items = {}
for i in range(self.nb):
lth = np.random.randint(1,10)
a = np.random.binomial(size=lth, n=1, p =0.6)
self.list_items[i] = a
def sum_of_dict(self, element): # fonction comme eval function
a = np.sum(element)
return a
def sub(self, q_in, q_out):
return cpp.parallelize(self, q_in, q_out)
相关文章:
- Pybind11:将元组列表从Python传递到C++
- Windows/Cygwin - 不能使用 pybind11 - 犯错误
- 用pybind11包装C++抽象类时出错
- 使用Pybind11向Python公开Eigen::张量
- 如何在C++中使用pybind11加载一个pickle python列表
- 使用JsonCpp将数据返回到带有pybind11的python会在python调用中产生Symbol not foun
- C++返回 Numpy 数组的 Python 扩展模块
- 如何将 Eigen::Ref 与 pybind11 一起使用?
- 相当于 pybind11 中的 boost::p ython py::scope().attr()
- Python 3.8 不能与 Pybind11 一起导入 [Windows]
- numpy C API 中的import_array如何工作?
- 如何通过 pybind11 从 python 中的C++中捕获异常?
- Pybind11 错误未定义对"Py_GetVersion"的引用
- Pybind11 Class Definition
- Pybind11 默认参数 numpy 数组或 None
- Pybind11:在C++中使用Pybind11转换numpy数组的问题
- pybind11 返回 numpy 对象数组
- 通过 Pybind11 返回 numpy 数组
- Pybind11:创建数据的numpy视图
- 使用pybind11将numpy阵列铸造到自定义C 矩阵级