test_buffers.cpp
Go to the documentation of this file.
1 /*
2  tests/test_buffers.cpp -- supporting Pythons' buffer protocol
3 
4  Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
5 
6  All rights reserved. Use of this source code is governed by a
7  BSD-style license that can be found in the LICENSE file.
8 */
9 
10 #include <pybind11/complex.h>
11 #include <pybind11/stl.h>
12 
13 #include "constructor_stats.h"
14 #include "pybind11_tests.h"
15 
16 TEST_SUBMODULE(buffers, m) {
17  m.attr("long_double_and_double_have_same_size") = (sizeof(long double) == sizeof(double));
18 
19  m.def("format_descriptor_format_buffer_info_equiv",
20  [](const std::string &cpp_name, const py::buffer &buffer) {
21  // https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables
22  static auto *format_table = new std::map<std::string, std::string>;
23  static auto *equiv_table
24  = new std::map<std::string, bool (py::buffer_info::*)() const>;
25  if (format_table->empty()) {
26 #define PYBIND11_ASSIGN_HELPER(...) \
27  (*format_table)[#__VA_ARGS__] = py::format_descriptor<__VA_ARGS__>::format(); \
28  (*equiv_table)[#__VA_ARGS__] = &py::buffer_info::item_type_is_equivalent_to<__VA_ARGS__>;
29  PYBIND11_ASSIGN_HELPER(PyObject *)
41  PYBIND11_ASSIGN_HELPER(long double)
42  PYBIND11_ASSIGN_HELPER(std::complex<float>)
43  PYBIND11_ASSIGN_HELPER(std::complex<double>)
44  PYBIND11_ASSIGN_HELPER(std::complex<long double>)
45 #undef PYBIND11_ASSIGN_HELPER
46  }
47  return std::pair<std::string, bool>(
48  (*format_table)[cpp_name], (buffer.request().*((*equiv_table)[cpp_name]))());
49  });
50 
51  // test_from_python / test_to_python:
52  class Matrix {
53  public:
54  Matrix(py::ssize_t rows, py::ssize_t cols) : m_rows(rows), m_cols(cols) {
55  print_created(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
56  // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
57  m_data = new float[(size_t) (rows * cols)];
58  memset(m_data, 0, sizeof(float) * (size_t) (rows * cols));
59  }
60 
61  Matrix(const Matrix &s) : m_rows(s.m_rows), m_cols(s.m_cols) {
62  print_copy_created(this,
63  std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
64  // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
65  m_data = new float[(size_t) (m_rows * m_cols)];
66  memcpy(m_data, s.m_data, sizeof(float) * (size_t) (m_rows * m_cols));
67  }
68 
69  Matrix(Matrix &&s) noexcept : m_rows(s.m_rows), m_cols(s.m_cols), m_data(s.m_data) {
70  print_move_created(this);
71  s.m_rows = 0;
72  s.m_cols = 0;
73  s.m_data = nullptr;
74  }
75 
76  ~Matrix() {
77  print_destroyed(this,
78  std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
79  delete[] m_data;
80  }
81 
82  Matrix &operator=(const Matrix &s) {
83  if (this == &s) {
84  return *this;
85  }
87  std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
88  delete[] m_data;
89  m_rows = s.m_rows;
90  m_cols = s.m_cols;
91  m_data = new float[(size_t) (m_rows * m_cols)];
92  memcpy(m_data, s.m_data, sizeof(float) * (size_t) (m_rows * m_cols));
93  return *this;
94  }
95 
96  Matrix &operator=(Matrix &&s) noexcept {
98  std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
99  if (&s != this) {
100  delete[] m_data;
101  m_rows = s.m_rows;
102  m_cols = s.m_cols;
103  m_data = s.m_data;
104  s.m_rows = 0;
105  s.m_cols = 0;
106  s.m_data = nullptr;
107  }
108  return *this;
109  }
110 
111  float operator()(py::ssize_t i, py::ssize_t j) const {
112  return m_data[(size_t) (i * m_cols + j)];
113  }
114 
116  return m_data[(size_t) (i * m_cols + j)];
117  }
118 
119  float *data() { return m_data; }
120 
121  py::ssize_t rows() const { return m_rows; }
122  py::ssize_t cols() const { return m_cols; }
123 
124  private:
125  py::ssize_t m_rows;
126  py::ssize_t m_cols;
127  float *m_data;
128  };
129  py::class_<Matrix>(m, "Matrix", py::buffer_protocol())
130  .def(py::init<py::ssize_t, py::ssize_t>())
132  .def(py::init([](const py::buffer &b) {
133  py::buffer_info info = b.request();
134  if (info.format != py::format_descriptor<float>::format() || info.ndim != 2) {
135  throw std::runtime_error("Incompatible buffer format!");
136  }
137 
138  auto *v = new Matrix(info.shape[0], info.shape[1]);
139  memcpy(v->data(), info.ptr, sizeof(float) * (size_t) (v->rows() * v->cols()));
140  return v;
141  }))
142 
143  .def("rows", &Matrix::rows)
144  .def("cols", &Matrix::cols)
145 
147  .def("__getitem__",
148  [](const Matrix &m, std::pair<py::ssize_t, py::ssize_t> i) {
149  if (i.first >= m.rows() || i.second >= m.cols()) {
150  throw py::index_error();
151  }
152  return m(i.first, i.second);
153  })
154  .def("__setitem__",
155  [](Matrix &m, std::pair<py::ssize_t, py::ssize_t> i, float v) {
156  if (i.first >= m.rows() || i.second >= m.cols()) {
157  throw py::index_error();
158  }
159  m(i.first, i.second) = v;
160  })
162  .def_buffer([](Matrix &m) -> py::buffer_info {
163  return py::buffer_info(
164  m.data(), /* Pointer to buffer */
165  {m.rows(), m.cols()}, /* Buffer dimensions */
166  {sizeof(float) * size_t(m.cols()), /* Strides (in bytes) for each index */
167  sizeof(float)});
168  });
169 
170  // test_inherited_protocol
171  class SquareMatrix : public Matrix {
172  public:
173  explicit SquareMatrix(py::ssize_t n) : Matrix(n, n) {}
174  };
175  // Derived classes inherit the buffer protocol and the buffer access function
176  py::class_<SquareMatrix, Matrix>(m, "SquareMatrix").def(py::init<py::ssize_t>());
177 
178  // test_pointer_to_member_fn
179  // Tests that passing a pointer to member to the base class works in
180  // the derived class.
181  struct Buffer {
182  int32_t value = 0;
183 
184  py::buffer_info get_buffer_info() {
185  return py::buffer_info(
187  }
188  };
189  py::class_<Buffer>(m, "Buffer", py::buffer_protocol())
190  .def(py::init<>())
191  .def_readwrite("value", &Buffer::value)
192  .def_buffer(&Buffer::get_buffer_info);
193 
194  class ConstBuffer {
195  std::unique_ptr<int32_t> value;
196 
197  public:
198  int32_t get_value() const { return *value; }
199  void set_value(int32_t v) { *value = v; }
200 
201  py::buffer_info get_buffer_info() const {
202  return py::buffer_info(
203  value.get(), sizeof(*value), py::format_descriptor<int32_t>::format(), 1);
204  }
205 
206  ConstBuffer() : value(new int32_t{0}) {}
207  };
208  py::class_<ConstBuffer>(m, "ConstBuffer", py::buffer_protocol())
209  .def(py::init<>())
210  .def_property("value", &ConstBuffer::get_value, &ConstBuffer::set_value)
211  .def_buffer(&ConstBuffer::get_buffer_info);
212 
213  struct DerivedBuffer : public Buffer {};
214  py::class_<DerivedBuffer>(m, "DerivedBuffer", py::buffer_protocol())
215  .def(py::init<>())
216  .def_readwrite("value", (int32_t DerivedBuffer::*) &DerivedBuffer::value)
217  .def_buffer(&DerivedBuffer::get_buffer_info);
218 
219  struct BufferReadOnly {
220  const uint8_t value = 0;
221  explicit BufferReadOnly(uint8_t value) : value(value) {}
222 
223  py::buffer_info get_buffer_info() { return py::buffer_info(&value, 1); }
224  };
225  py::class_<BufferReadOnly>(m, "BufferReadOnly", py::buffer_protocol())
226  .def(py::init<uint8_t>())
227  .def_buffer(&BufferReadOnly::get_buffer_info);
228 
229  struct BufferReadOnlySelect {
230  uint8_t value = 0;
231  bool readonly = false;
232 
233  py::buffer_info get_buffer_info() { return py::buffer_info(&value, 1, readonly); }
234  };
235  py::class_<BufferReadOnlySelect>(m, "BufferReadOnlySelect", py::buffer_protocol())
236  .def(py::init<>())
237  .def_readwrite("value", &BufferReadOnlySelect::value)
238  .def_readwrite("readonly", &BufferReadOnlySelect::readonly)
239  .def_buffer(&BufferReadOnlySelect::get_buffer_info);
240 
241  // Expose buffer_info for testing.
242  py::class_<py::buffer_info>(m, "buffer_info")
243  .def(py::init<>())
244  .def_readonly("itemsize", &py::buffer_info::itemsize)
245  .def_readonly("size", &py::buffer_info::size)
246  .def_readonly("format", &py::buffer_info::format)
247  .def_readonly("ndim", &py::buffer_info::ndim)
248  .def_readonly("shape", &py::buffer_info::shape)
249  .def_readonly("strides", &py::buffer_info::strides)
250  .def_readonly("readonly", &py::buffer_info::readonly)
251  .def("__repr__", [](py::handle self) {
252  return py::str("itemsize={0.itemsize!r}, size={0.size!r}, format={0.format!r}, "
253  "ndim={0.ndim!r}, shape={0.shape!r}, strides={0.strides!r}, "
254  "readonly={0.readonly!r}")
255  .format(self);
256  });
257 
258  m.def("get_buffer_info", [](const py::buffer &buffer) { return buffer.request(); });
259 }
int16_t
signed short int16_t
Definition: ms_stdint.h:81
format
std::string format(const std::string &str, const std::vector< std::string > &find, const std::vector< std::string > &replace)
Definition: openglsupport.cpp:226
Eigen::internal::strides
EIGEN_ALWAYS_INLINE DSizes< IndexType, NumDims > strides(const DSizes< IndexType, NumDims > &dimensions)
Definition: TensorBlock.h:26
ssize_t
Py_ssize_t ssize_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:489
s
RealScalar s
Definition: level1_cplx_impl.h:126
uint32_t
unsigned int uint32_t
Definition: ms_stdint.h:85
b
Scalar * b
Definition: benchVecAdd.cpp:17
stl.h
buffer
Definition: pytypes.h:2270
constructor_stats.h
rows
int rows
Definition: Tutorial_commainit_02.cpp:1
uint8_t
unsigned char uint8_t
Definition: ms_stdint.h:83
print_copy_created
void print_copy_created(T *inst, Values &&...values)
Definition: constructor_stats.h:282
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
n
int n
Definition: BiCGSTAB_simple.cpp:1
buffer::request
buffer_info request(bool writable=false) const
Definition: pytypes.h:2274
complex.h
ceres::Matrix
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > Matrix
Definition: gtsam/3rdparty/ceres/eigen.h:42
data
int data[]
Definition: Map_placement_new.cpp:1
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
int64_t
signed __int64 int64_t
Definition: ms_stdint.h:94
operator()
internal::enable_if< internal::valid_indexed_view_overload< RowIndices, ColIndices >::value &&internal::traits< typename EIGEN_INDEXED_VIEW_METHOD_TYPE< RowIndices, ColIndices >::type >::ReturnAsIndexedView, typename EIGEN_INDEXED_VIEW_METHOD_TYPE< RowIndices, ColIndices >::type >::type operator()(const RowIndices &rowIndices, const ColIndices &colIndices) EIGEN_INDEXED_VIEW_METHOD_CONST
Definition: IndexedViewMethods.h:73
print_copy_assigned
void print_copy_assigned(T *inst, Values &&...values)
Definition: constructor_stats.h:294
info
else if n * info
Definition: 3rdparty/Eigen/lapack/cholesky.cpp:18
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
int8_t
signed char int8_t
Definition: ms_stdint.h:80
init
detail::initimpl::constructor< Args... > init()
Binds an existing constructor taking arguments Args...
Definition: pybind11.h:2006
TEST_SUBMODULE
TEST_SUBMODULE(buffers, m)
Definition: test_buffers.cpp:16
size_t
std::size_t size_t
Definition: wrap/pybind11/include/pybind11/detail/common.h:490
print_move_created
void print_move_created(T *inst, Values &&...values)
Definition: constructor_stats.h:288
pybind11_tests.h
print_destroyed
void print_destroyed(T *inst, Values &&...values)
Definition: constructor_stats.h:314
uint16_t
unsigned short uint16_t
Definition: ms_stdint.h:84
v
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
PYBIND11_ASSIGN_HELPER
#define PYBIND11_ASSIGN_HELPER(...)
int32_t
signed int int32_t
Definition: ms_stdint.h:82
print_move_assigned
void print_move_assigned(T *inst, Values &&...values)
Definition: constructor_stats.h:299
uint64_t
unsigned __int64 uint64_t
Definition: ms_stdint.h:95
cols
int cols
Definition: Tutorial_commainit_02.cpp:1
gtsam.examples.ShonanAveragingCLI.str
str
Definition: ShonanAveragingCLI.py:115
test_callbacks.value
value
Definition: test_callbacks.py:160
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
print_created
void print_created(T *inst, Values &&...values)
Definition: constructor_stats.h:309


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:07:05