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.

115 lines
4.1 KiB

  1. /*
  2. example/example7.cpp -- supporting Pythons' buffer protocol
  3. Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.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 "example.h"
  8. class Matrix {
  9. public:
  10. Matrix(size_t rows, size_t cols) : m_rows(rows), m_cols(cols) {
  11. std::cout << "Value constructor: Creating a " << rows << "x" << cols << " matrix " << std::endl;
  12. m_data = new float[rows*cols];
  13. memset(m_data, 0, sizeof(float) * rows * cols);
  14. }
  15. Matrix(const Matrix &s) : m_rows(s.m_rows), m_cols(s.m_cols) {
  16. std::cout << "Copy constructor: Creating a " << m_rows << "x" << m_cols << " matrix " << std::endl;
  17. m_data = new float[m_rows * m_cols];
  18. memcpy(m_data, s.m_data, sizeof(float) * m_rows * m_cols);
  19. }
  20. Matrix(Matrix &&s) : m_rows(s.m_rows), m_cols(s.m_cols), m_data(s.m_data) {
  21. std::cout << "Move constructor: Creating a " << m_rows << "x" << m_cols << " matrix " << std::endl;
  22. s.m_rows = 0;
  23. s.m_cols = 0;
  24. s.m_data = nullptr;
  25. }
  26. ~Matrix() {
  27. std::cout << "Freeing a " << m_rows << "x" << m_cols << " matrix " << std::endl;
  28. delete[] m_data;
  29. }
  30. Matrix &operator=(const Matrix &s) {
  31. std::cout << "Assignment operator : Creating a " << s.m_rows << "x" << s.m_cols << " matrix " << std::endl;
  32. delete[] m_data;
  33. m_rows = s.m_rows;
  34. m_cols = s.m_cols;
  35. m_data = new float[m_rows * m_cols];
  36. memcpy(m_data, s.m_data, sizeof(float) * m_rows * m_cols);
  37. return *this;
  38. }
  39. Matrix &operator=(Matrix &&s) {
  40. std::cout << "Move assignment operator : Creating a " << s.m_rows << "x" << s.m_cols << " matrix " << std::endl;
  41. if (&s != this) {
  42. delete[] m_data;
  43. m_rows = s.m_rows; m_cols = s.m_cols; m_data = s.m_data;
  44. s.m_rows = 0; s.m_cols = 0; s.m_data = nullptr;
  45. }
  46. return *this;
  47. }
  48. float operator()(size_t i, size_t j) const {
  49. return m_data[i*m_cols + j];
  50. }
  51. float &operator()(size_t i, size_t j) {
  52. return m_data[i*m_cols + j];
  53. }
  54. float *data() { return m_data; }
  55. size_t rows() const { return m_rows; }
  56. size_t cols() const { return m_cols; }
  57. private:
  58. size_t m_rows;
  59. size_t m_cols;
  60. float *m_data;
  61. };
  62. void init_ex7(py::module &m) {
  63. py::class_<Matrix> mtx(m, "Matrix");
  64. mtx.def(py::init<size_t, size_t>())
  65. /// Construct from a buffer
  66. .def("__init__", [](Matrix &v, py::buffer b) {
  67. py::buffer_info info = b.request();
  68. if (info.format != py::format_descriptor<float>::value() || info.ndim != 2)
  69. throw std::runtime_error("Incompatible buffer format!");
  70. new (&v) Matrix(info.shape[0], info.shape[1]);
  71. memcpy(v.data(), info.ptr, sizeof(float) * v.rows() * v.cols());
  72. })
  73. .def("rows", &Matrix::rows)
  74. .def("cols", &Matrix::cols)
  75. /// Bare bones interface
  76. .def("__getitem__", [](const Matrix &m, std::pair<size_t, size_t> i) {
  77. if (i.first >= m.rows() || i.second >= m.cols())
  78. throw py::index_error();
  79. return m(i.first, i.second);
  80. })
  81. .def("__setitem__", [](Matrix &m, std::pair<size_t, size_t> i, float v) {
  82. if (i.first >= m.rows() || i.second >= m.cols())
  83. throw py::index_error();
  84. m(i.first, i.second) = v;
  85. })
  86. /// Provide buffer access
  87. .def_buffer([](Matrix &m) -> py::buffer_info {
  88. return py::buffer_info(
  89. m.data(), /* Pointer to buffer */
  90. sizeof(float), /* Size of one scalar */
  91. py::format_descriptor<float>::value(), /* Python struct-style format descriptor */
  92. 2, /* Number of dimensions */
  93. { m.rows(), m.cols() }, /* Buffer dimensions */
  94. { sizeof(float) * m.rows(), /* Strides (in bytes) for each index */
  95. sizeof(float) }
  96. );
  97. });
  98. }