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

Python wrapper: prepare wrapped python objects for numpy usage #870

Closed
akuesters opened this issue Sep 24, 2019 · 2 comments
Closed

Python wrapper: prepare wrapped python objects for numpy usage #870

akuesters opened this issue Sep 24, 2019 · 2 comments
Assignees
Labels

Comments

@akuesters
Copy link
Contributor

see https://pybind11.readthedocs.io/en/stable/advanced/pycpp/numpy.html?highlight=numpy

Add buffer_info to all wrapped python objects returning lists (e.g. stdandard vector, list, array, deque)

@akuesters akuesters added this to the Python Interface milestone Sep 24, 2019
@akuesters akuesters changed the title prepare wrapped python objects for numpy usage Python wrapper: prepare wrapped python objects for numpy usage Sep 25, 2019
@brenthuisman
Copy link
Contributor

Probes are already covered.

@thorstenhater
Copy link
Contributor

buffer_info is only needed where we define an object that is buffer-like (eg matrices), example
from https://pybind11.readthedocs.io/en/stable/advanced/pycpp/numpy.html#buffer-protocol

py::class_<Matrix>(m, "Matrix", py::buffer_protocol())
   .def_buffer([](Matrix &m) -> py::buffer_info {
        return py::buffer_info(m.raw,                                  /* Pointer to buffer */
                               sizeof(float),                          /* Size of one scalar */
                               py::format_descriptor<float>::format(), /* Python struct-style format descriptor */
                               2,                                      /* Number of dimensions */
                               {m.rows(), m.cols()},                   /* Buffer dimensions */
                               {sizeof(float)*m.cols(),                /* Strides (in bytes) for each index */
                                sizeof(float) });
    });

Everywhere else, we need return a pybind11::array or pybind11::array_t<T> for numpy
types and pybind11::buffer for more general buffer-enabled classes.

The 0-copy way of doing this is the following

// Get a pointer to the vector object (this is copy-free, since move-constructed)
auto v = new std::vector<int>(some_func());
// pybind11 smart-pointer any hybrid with explicit deleter
auto capsule = py::capsule(v, [](void *v) { delete reinterpret_cast<std::vector<int>*>(v); });
// Create an array from the raw data and capsule, where the capsule's lifetime is bound
// to that of the array and in turn will (C++) delete the vector object.
return py::array(v->size(), v->data(), capsule);

if we can live with a copy, this should be used to be less stressful on my sanity (no lifetime management madness)

auto v = some_func();
return py::array(v.size(), v.data());

See pybind/pybind11#1042

As @halfflat say, the probe API is covered already and with that the main usecase. After trawling the Python
interface I could not find more, so I am closing this with the request to re-open, if I missed something.
The notes above can serve as reference on how to achieve numpy compatibility where needed.

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

No branches or pull requests

3 participants