Giter Site home page Giter Site logo

Comments (2)

matsen avatar matsen commented on August 16, 2024

I think the PYBIND11_MAKE_OPAQUE(std::vector<double>); and https://pybind11.readthedocs.io/en/stable/advanced/pycpp/numpy.html approach is going to work, but in case it doesn't here are some code snippets:

From pybind/pybind11#1042:

pybind11::array WrapVector(std::vector<double> *v) { 
  auto capsule = py::capsule(
      &v, [](void *v) { delete reinterpret_cast<std::vector<double> *>(v); });
  return py::array(static_cast<pybind11::ssize_t>(v->size()), v->data(),
                   capsule);
}

which led to double-free problems.

This silliness also worked:

class Vector : public std::vector<double> {
 public:
  Vector(size_t size) : std::vector<double>(size){};
};

from bito.

matsen avatar matsen commented on August 16, 2024

Here's the code I used to time the various versions:

import numpy as np
import sbn
import timeit

v1_c = sbn.make_vector()
v2_c = sbn.make_vector()
v3_c = sbn.make_vector()
a1_c = np.array(v1_c, copy=False)
a2_c = np.array(v2_c, copy=False)
a3_c = np.array(v3_c, copy=False)

print("pybind11 version: ", timeit.timeit("a3_c = a1_c + a2_c", globals=globals()))

a1 = np.random.uniform(size=len(a1_c))
a2 = np.random.uniform(size=len(a1_c))
a3 = np.random.uniform(size=len(a1_c))

print("numpy version: ", timeit.timeit("a3 = a1 + a2", globals=globals()))
class Vector {
 public:
  Vector(size_t size) : m_size(size) { m_data = new double[size]; }
  double *data() { return m_data; }
  size_t size() const { return m_size; }

 private:
  size_t m_size;
  double *m_data;
};

Vector make_vector() {
  std::random_device
      rd;  // Will be used to obtain a seed for the random number engine
  std::mt19937 gen(rd());  // Standard mersenne_twister_engine seeded with rd()
  std::uniform_real_distribution<> dis(0., 1.);
  Vector x(1000);
  for (size_t i = 0; i < x.size(); i++) {
    x.data()[i] = dis(gen);
  }
  return x;
}
...
  py::class_<Vector>(m, "Vector", py::buffer_protocol())
      .def_buffer([](Vector &v) -> py::buffer_info {
        return py::buffer_info(
            v.data(),                                /* Pointer to buffer */
            sizeof(double),                          /* Size of one scalar */
            py::format_descriptor<double>::format(), /* Python
                                                       struct-style format
                                                       descriptor */
            1,                                       /* Number of dimensions */
            {v.size()},                              /* Buffer dimensions */
            {sizeof(double)});
      });
  m.def("make_vector", &make_vector, "test");

from bito.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.