3 回答
TA贡献1804条经验 获得超8个赞
我找到了一个可行的解决方案。虽然它可能不是最优雅的。我创建了三个新类Ints,它们采用原始容器并通过函数调用公开各自的Doubles向量。使用这三个类,我可以为所有类指定三次缓冲区协议。Doubles2getValues()
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>
#include <pybind11/buffer_info.h>
namespace py = pybind11;
struct ContainerElement
{
uint8_t i;
double d;
double d2;
};
class Container
{
private:
std::vector<uint8_t> ints;
std::vector<double> doubles;
std::vector<double> doubles2;
public:
std::vector<uint8_t>& getInts() { return ints; }
std::vector<double>& getDoubles() { return doubles; }
std::vector<double>& getDoubles2() { return doubles2; }
void addElement(ContainerElement element)
{
ints.emplace_back(element.i);
doubles.emplace_back(element.d);
doubles2.emplace_back(element.d2);
}
};
void fillContainer(Container& container)
{
for (int i = 0; i < 1e6; ++i)
{
container.addElement({ (uint8_t)i, (double)i,(double)i });
}
}
class Ints
{
private:
Container& cont;
public:
Ints(Container& cont) : cont(cont) {}
std::vector<uint8_t>& getValues() { return cont.getInts(); }
};
class Doubles
{
private:
Container& cont;
public:
Doubles(Container& cont) : cont(cont) {}
std::vector<double>& getValues() { return cont.getDoubles(); }
};
class Doubles2
{
private:
Container& cont;
public:
Doubles2(Container& cont) : cont(cont) {}
std::vector<double>& getValues() { return cont.getDoubles2(); }
};
PYBIND11_MODULE(newInterface, m) {
py::class_<Container>(m, "Container")
.def(py::init<>());
py::class_<Ints>(m, "Ints", py::buffer_protocol())
.def(py::init<Container&>(), py::keep_alive<1, 2>())
.def_buffer([](Ints& ints) -> py::buffer_info {
return py::buffer_info(
ints.getValues().data(),
sizeof(uint8_t),
py::format_descriptor<uint8_t>::format(),
ints.getValues().size()
);
});
py::class_<Doubles>(m, "Doubles", py::buffer_protocol())
.def(py::init<Container&>(), py::keep_alive<1, 2>())
.def_buffer([](Doubles& doubles) -> py::buffer_info {
return py::buffer_info(
doubles.getValues().data(),
sizeof(double),
py::format_descriptor<double>::format(),
doubles.getValues().size()
);
});
py::class_<Doubles2>(m, "Doubles2", py::buffer_protocol())
.def(py::init<Container&>(), py::keep_alive<1, 2>())
.def_buffer([](Doubles2& doubles2) -> py::buffer_info {
return py::buffer_info(
doubles2.getValues().data(),
sizeof(double),
py::format_descriptor<double>::format(),
doubles2.getValues().size()
);
});
m.def("fillContainer", &fillContainer);
}
这样我就可以在 Python 中按以下方式使用代码:
import newInterface as ci
import numpy as np
container = ci.Container()
ci.fillContainer(container)
i = np.array(ci.Ints(container), copy=False)
d = np.array(ci.Doubles(container), copy=False)
d2 = np.array(ci.Doubles2(container), copy=False)
一旦fillContainer填充了容器,numpy 数组的构造就不会从这个容器中复制值。
TA贡献1796条经验 获得超4个赞
TA贡献1772条经验 获得超5个赞
这并不能直接解决问题,但仍然允许在不进行复制的情况下返回数组缓冲区。灵感来自这个线程: https ://github.com/pybind/pybind11/issues/1042
基本上,只需向 py::array() 构造函数提供一个 py::capsule。这样,py::array() 构造函数就不会分配单独的缓冲区和副本。例如:
// Use this if the C++ buffer should NOT be deallocated
// once Python no longer has a reference to it
py::capsule buffer_handle([](){});
// Use this if the C++ buffer SHOULD be deallocated
// once the Python no longer has a reference to it
// py::capsule buffer_handle(data_buffer, [](void* p){ free(p); });
return py::array(py::buffer_info(
data_buffer,
element_size,
data_type,
dims_length,
dims,
strides
), buffer_handle);
添加回答
举报