You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
4.1 KiB

  1. /*
  2. tests/test_buffers.cpp -- supporting Pythons' buffer protocol
  3. Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
  4. All rights reserved. Use of this source code is governed by a
  5. BSD-style license that can be found in the LICENSE file.
  6. */
  7. #include "pybind11_tests.h"
  8. #include "constructor_stats.h"
  9. class Matrix {
  10. public:
  11. Matrix(size_t rows, size_t cols) : m_rows(rows), m_cols(cols) {
  12. print_created(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
  13. m_data = new float[rows*cols];
  14. memset(m_data, 0, sizeof(float) * rows * cols);
  15. }
  16. Matrix(const Matrix &s) : m_rows(s.m_rows), m_cols(s.m_cols) {
  17. print_copy_created(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
  18. m_data = new float[m_rows * m_cols];
  19. memcpy(m_data, s.m_data, sizeof(float) * m_rows * m_cols);
  20. }
  21. Matrix(Matrix &&s) : m_rows(s.m_rows), m_cols(s.m_cols), m_data(s.m_data) {
  22. print_move_created(this);
  23. s.m_rows = 0;
  24. s.m_cols = 0;
  25. s.m_data = nullptr;
  26. }
  27. ~Matrix() {
  28. print_destroyed(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
  29. delete[] m_data;
  30. }
  31. Matrix &operator=(const Matrix &s) {
  32. print_copy_assigned(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
  33. delete[] m_data;
  34. m_rows = s.m_rows;
  35. m_cols = s.m_cols;
  36. m_data = new float[m_rows * m_cols];
  37. memcpy(m_data, s.m_data, sizeof(float) * m_rows * m_cols);
  38. return *this;
  39. }
  40. Matrix &operator=(Matrix &&s) {
  41. print_move_assigned(this, std::to_string(m_rows) + "x" + std::to_string(m_cols) + " matrix");
  42. if (&s != this) {
  43. delete[] m_data;
  44. m_rows = s.m_rows; m_cols = s.m_cols; m_data = s.m_data;
  45. s.m_rows = 0; s.m_cols = 0; s.m_data = nullptr;
  46. }
  47. return *this;
  48. }
  49. float operator()(size_t i, size_t j) const {
  50. return m_data[i*m_cols + j];
  51. }
  52. float &operator()(size_t i, size_t j) {
  53. return m_data[i*m_cols + j];
  54. }
  55. float *data() { return m_data; }
  56. size_t rows() const { return m_rows; }
  57. size_t cols() const { return m_cols; }
  58. private:
  59. size_t m_rows;
  60. size_t m_cols;
  61. float *m_data;
  62. };
  63. test_initializer buffers([](py::module &m) {
  64. py::class_<Matrix> mtx(m, "Matrix");
  65. mtx.def(py::init<size_t, size_t>())
  66. /// Construct from a buffer
  67. .def("__init__", [](Matrix &v, py::buffer b) {
  68. py::buffer_info info = b.request();
  69. if (info.format != py::format_descriptor<float>::format() || info.ndim != 2)
  70. throw std::runtime_error("Incompatible buffer format!");
  71. new (&v) Matrix(info.shape[0], info.shape[1]);
  72. memcpy(v.data(), info.ptr, sizeof(float) * v.rows() * v.cols());
  73. })
  74. .def("rows", &Matrix::rows)
  75. .def("cols", &Matrix::cols)
  76. /// Bare bones interface
  77. .def("__getitem__", [](const Matrix &m, std::pair<size_t, size_t> i) {
  78. if (i.first >= m.rows() || i.second >= m.cols())
  79. throw py::index_error();
  80. return m(i.first, i.second);
  81. })
  82. .def("__setitem__", [](Matrix &m, std::pair<size_t, size_t> i, float v) {
  83. if (i.first >= m.rows() || i.second >= m.cols())
  84. throw py::index_error();
  85. m(i.first, i.second) = v;
  86. })
  87. /// Provide buffer access
  88. .def_buffer([](Matrix &m) -> py::buffer_info {
  89. return py::buffer_info(
  90. m.data(), /* Pointer to buffer */
  91. sizeof(float), /* Size of one scalar */
  92. py::format_descriptor<float>::format(), /* Python struct-style format descriptor */
  93. 2, /* Number of dimensions */
  94. { m.rows(), m.cols() }, /* Buffer dimensions */
  95. { sizeof(float) * m.rows(), /* Strides (in bytes) for each index */
  96. sizeof(float) }
  97. );
  98. })
  99. ;
  100. });