Ivan Smirnov | bb4015d | 2016-06-19 15:50:31 +0100 | [diff] [blame] | 1 | /* |
| 2 | example/example20.cpp -- Usage of structured numpy dtypes |
| 3 | |
| 4 | Copyright (c) 2016 Ivan Smirnov |
| 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 "example.h" |
| 11 | |
| 12 | #include <pybind11/numpy.h> |
| 13 | #include <cstdint> |
| 14 | #include <iostream> |
| 15 | |
Ivan Smirnov | f5f75c6 | 2016-07-18 22:47:40 +0100 | [diff] [blame] | 16 | #ifdef __GNUC__ |
| 17 | #define PYBIND11_PACKED(cls) cls __attribute__((__packed__)) |
| 18 | #else |
| 19 | #define PYBIND11_PACKED(cls) __pragma(pack(push, 1)) cls __pragma(pack(pop)) |
| 20 | #endif |
| 21 | |
Ivan Smirnov | bb4015d | 2016-06-19 15:50:31 +0100 | [diff] [blame] | 22 | namespace py = pybind11; |
| 23 | |
Ivan Smirnov | 4f16421 | 2016-06-22 01:07:20 +0100 | [diff] [blame] | 24 | struct SimpleStruct { |
Ivan Smirnov | bb4015d | 2016-06-19 15:50:31 +0100 | [diff] [blame] | 25 | bool x; |
| 26 | uint32_t y; |
| 27 | float z; |
| 28 | }; |
| 29 | |
Ivan Smirnov | 4f16421 | 2016-06-22 01:07:20 +0100 | [diff] [blame] | 30 | std::ostream& operator<<(std::ostream& os, const SimpleStruct& v) { |
| 31 | return os << "s:" << v.x << "," << v.y << "," << v.z; |
Ivan Smirnov | 669e142 | 2016-06-21 21:05:29 +0100 | [diff] [blame] | 32 | } |
| 33 | |
Ivan Smirnov | f5f75c6 | 2016-07-18 22:47:40 +0100 | [diff] [blame] | 34 | PYBIND11_PACKED(struct PackedStruct { |
Ivan Smirnov | bb4015d | 2016-06-19 15:50:31 +0100 | [diff] [blame] | 35 | bool x; |
| 36 | uint32_t y; |
| 37 | float z; |
Ivan Smirnov | f5f75c6 | 2016-07-18 22:47:40 +0100 | [diff] [blame] | 38 | }); |
Ivan Smirnov | bb4015d | 2016-06-19 15:50:31 +0100 | [diff] [blame] | 39 | |
Ivan Smirnov | 669e142 | 2016-06-21 21:05:29 +0100 | [diff] [blame] | 40 | std::ostream& operator<<(std::ostream& os, const PackedStruct& v) { |
Ivan Smirnov | 4f16421 | 2016-06-22 01:07:20 +0100 | [diff] [blame] | 41 | return os << "p:" << v.x << "," << v.y << "," << v.z; |
Ivan Smirnov | 669e142 | 2016-06-21 21:05:29 +0100 | [diff] [blame] | 42 | } |
| 43 | |
Ivan Smirnov | f5f75c6 | 2016-07-18 22:47:40 +0100 | [diff] [blame] | 44 | PYBIND11_PACKED(struct NestedStruct { |
Ivan Smirnov | 4f16421 | 2016-06-22 01:07:20 +0100 | [diff] [blame] | 45 | SimpleStruct a; |
Ivan Smirnov | bb4015d | 2016-06-19 15:50:31 +0100 | [diff] [blame] | 46 | PackedStruct b; |
Ivan Smirnov | f5f75c6 | 2016-07-18 22:47:40 +0100 | [diff] [blame] | 47 | }); |
Ivan Smirnov | 7f913ae | 2016-06-19 16:41:15 +0100 | [diff] [blame] | 48 | |
Ivan Smirnov | 669e142 | 2016-06-21 21:05:29 +0100 | [diff] [blame] | 49 | std::ostream& operator<<(std::ostream& os, const NestedStruct& v) { |
Ivan Smirnov | 4f16421 | 2016-06-22 01:07:20 +0100 | [diff] [blame] | 50 | return os << "n:a=" << v.a << ";b=" << v.b; |
Ivan Smirnov | 669e142 | 2016-06-21 21:05:29 +0100 | [diff] [blame] | 51 | } |
| 52 | |
Ivan Smirnov | 8fa09cb | 2016-07-06 00:28:12 +0100 | [diff] [blame] | 53 | struct PartialStruct { |
| 54 | bool x; |
| 55 | uint32_t y; |
| 56 | float z; |
| 57 | long dummy2; |
| 58 | }; |
| 59 | |
| 60 | struct PartialNestedStruct { |
| 61 | long dummy1; |
| 62 | PartialStruct a; |
| 63 | long dummy2; |
| 64 | }; |
| 65 | |
Ivan Smirnov | d0bafd9 | 2016-06-26 16:35:28 +0100 | [diff] [blame] | 66 | struct UnboundStruct { }; |
| 67 | |
Ivan Smirnov | f9c0def | 2016-07-20 00:19:24 +0100 | [diff] [blame^] | 68 | struct StringStruct { |
| 69 | char a[3]; |
| 70 | std::array<char, 3> b; |
| 71 | }; |
| 72 | |
| 73 | std::ostream& operator<<(std::ostream& os, const StringStruct& v) { |
| 74 | os << "a='"; |
| 75 | for (size_t i = 0; i < 3 && v.a[i]; i++) os << v.a[i]; |
| 76 | os << "',b='"; |
| 77 | for (size_t i = 0; i < 3 && v.b[i]; i++) os << v.b[i]; |
| 78 | return os << "'"; |
| 79 | } |
| 80 | |
Ivan Smirnov | 7f913ae | 2016-06-19 16:41:15 +0100 | [diff] [blame] | 81 | template <typename T> |
| 82 | py::array mkarray_via_buffer(size_t n) { |
| 83 | return py::array(py::buffer_info(nullptr, sizeof(T), |
Ivan Smirnov | 5e71e17 | 2016-06-26 12:42:34 +0100 | [diff] [blame] | 84 | py::format_descriptor<T>::format(), |
Ivan Smirnov | 7f913ae | 2016-06-19 16:41:15 +0100 | [diff] [blame] | 85 | 1, { n }, { sizeof(T) })); |
| 86 | } |
Ivan Smirnov | bb4015d | 2016-06-19 15:50:31 +0100 | [diff] [blame] | 87 | |
| 88 | template <typename S> |
Ivan Smirnov | 8fa09cb | 2016-07-06 00:28:12 +0100 | [diff] [blame] | 89 | py::array_t<S, 0> create_recarray(size_t n) { |
Ivan Smirnov | 7f913ae | 2016-06-19 16:41:15 +0100 | [diff] [blame] | 90 | auto arr = mkarray_via_buffer<S>(n); |
Ivan Smirnov | b51fa02 | 2016-07-18 22:35:50 +0100 | [diff] [blame] | 91 | auto req = arr.request(); |
| 92 | auto ptr = static_cast<S*>(req.ptr); |
Ivan Smirnov | bb4015d | 2016-06-19 15:50:31 +0100 | [diff] [blame] | 93 | for (size_t i = 0; i < n; i++) { |
Ivan Smirnov | 7f913ae | 2016-06-19 16:41:15 +0100 | [diff] [blame] | 94 | ptr[i].x = i % 2; ptr[i].y = (uint32_t) i; ptr[i].z = (float) i * 1.5f; |
Ivan Smirnov | bb4015d | 2016-06-19 15:50:31 +0100 | [diff] [blame] | 95 | } |
| 96 | return arr; |
| 97 | } |
| 98 | |
Ivan Smirnov | d0bafd9 | 2016-06-26 16:35:28 +0100 | [diff] [blame] | 99 | std::string get_format_unbound() { |
| 100 | return py::format_descriptor<UnboundStruct>::format(); |
| 101 | } |
| 102 | |
Ivan Smirnov | 8fa09cb | 2016-07-06 00:28:12 +0100 | [diff] [blame] | 103 | py::array_t<NestedStruct, 0> create_nested(size_t n) { |
Ivan Smirnov | 7f913ae | 2016-06-19 16:41:15 +0100 | [diff] [blame] | 104 | auto arr = mkarray_via_buffer<NestedStruct>(n); |
Ivan Smirnov | b51fa02 | 2016-07-18 22:35:50 +0100 | [diff] [blame] | 105 | auto req = arr.request(); |
| 106 | auto ptr = static_cast<NestedStruct*>(req.ptr); |
Ivan Smirnov | 7f913ae | 2016-06-19 16:41:15 +0100 | [diff] [blame] | 107 | for (size_t i = 0; i < n; i++) { |
| 108 | ptr[i].a.x = i % 2; ptr[i].a.y = (uint32_t) i; ptr[i].a.z = (float) i * 1.5f; |
| 109 | ptr[i].b.x = (i + 1) % 2; ptr[i].b.y = (uint32_t) (i + 1); ptr[i].b.z = (float) (i + 1) * 1.5f; |
| 110 | } |
| 111 | return arr; |
Ivan Smirnov | bdc9902 | 2016-06-19 16:54:07 +0100 | [diff] [blame] | 112 | } |
Ivan Smirnov | 7f913ae | 2016-06-19 16:41:15 +0100 | [diff] [blame] | 113 | |
Ivan Smirnov | 8fa09cb | 2016-07-06 00:28:12 +0100 | [diff] [blame] | 114 | py::array_t<PartialNestedStruct, 0> create_partial_nested(size_t n) { |
| 115 | auto arr = mkarray_via_buffer<PartialNestedStruct>(n); |
Ivan Smirnov | b51fa02 | 2016-07-18 22:35:50 +0100 | [diff] [blame] | 116 | auto req = arr.request(); |
| 117 | auto ptr = static_cast<PartialNestedStruct*>(req.ptr); |
Ivan Smirnov | 8fa09cb | 2016-07-06 00:28:12 +0100 | [diff] [blame] | 118 | for (size_t i = 0; i < n; i++) { |
| 119 | ptr[i].a.x = i % 2; ptr[i].a.y = (uint32_t) i; ptr[i].a.z = (float) i * 1.5f; |
| 120 | } |
| 121 | return arr; |
| 122 | } |
| 123 | |
Ivan Smirnov | f9c0def | 2016-07-20 00:19:24 +0100 | [diff] [blame^] | 124 | py::array_t<StringStruct, 0> create_string_array(bool non_empty) { |
| 125 | auto arr = mkarray_via_buffer<StringStruct>(non_empty ? 4 : 0); |
| 126 | if (non_empty) { |
| 127 | auto req = arr.request(); |
| 128 | auto ptr = static_cast<StringStruct*>(req.ptr); |
| 129 | for (size_t i = 0; i < req.size * req.itemsize; i++) |
| 130 | static_cast<char*>(req.ptr)[i] = 0; |
| 131 | ptr[1].a[0] = 'a'; ptr[1].b[0] = 'a'; |
| 132 | ptr[2].a[0] = 'a'; ptr[2].b[0] = 'a'; |
| 133 | ptr[3].a[0] = 'a'; ptr[3].b[0] = 'a'; |
| 134 | |
| 135 | ptr[2].a[1] = 'b'; ptr[2].b[1] = 'b'; |
| 136 | ptr[3].a[1] = 'b'; ptr[3].b[1] = 'b'; |
| 137 | |
| 138 | ptr[3].a[2] = 'c'; ptr[3].b[2] = 'c'; |
| 139 | } |
| 140 | return arr; |
| 141 | } |
| 142 | |
Ivan Smirnov | 669e142 | 2016-06-21 21:05:29 +0100 | [diff] [blame] | 143 | template <typename S> |
Ivan Smirnov | 8fa09cb | 2016-07-06 00:28:12 +0100 | [diff] [blame] | 144 | void print_recarray(py::array_t<S, 0> arr) { |
Ivan Smirnov | b51fa02 | 2016-07-18 22:35:50 +0100 | [diff] [blame] | 145 | auto req = arr.request(); |
| 146 | auto ptr = static_cast<S*>(req.ptr); |
| 147 | for (size_t i = 0; i < req.size; i++) |
Ivan Smirnov | 669e142 | 2016-06-21 21:05:29 +0100 | [diff] [blame] | 148 | std::cout << ptr[i] << std::endl; |
| 149 | } |
Ivan Smirnov | bdc9902 | 2016-06-19 16:54:07 +0100 | [diff] [blame] | 150 | |
| 151 | void print_format_descriptors() { |
Ivan Smirnov | 5e71e17 | 2016-06-26 12:42:34 +0100 | [diff] [blame] | 152 | std::cout << py::format_descriptor<SimpleStruct>::format() << std::endl; |
| 153 | std::cout << py::format_descriptor<PackedStruct>::format() << std::endl; |
| 154 | std::cout << py::format_descriptor<NestedStruct>::format() << std::endl; |
Ivan Smirnov | 8fa09cb | 2016-07-06 00:28:12 +0100 | [diff] [blame] | 155 | std::cout << py::format_descriptor<PartialStruct>::format() << std::endl; |
| 156 | std::cout << py::format_descriptor<PartialNestedStruct>::format() << std::endl; |
Ivan Smirnov | f9c0def | 2016-07-20 00:19:24 +0100 | [diff] [blame^] | 157 | std::cout << py::format_descriptor<StringStruct>::format() << std::endl; |
Ivan Smirnov | 7f913ae | 2016-06-19 16:41:15 +0100 | [diff] [blame] | 158 | } |
| 159 | |
Ivan Smirnov | 4f16421 | 2016-06-22 01:07:20 +0100 | [diff] [blame] | 160 | void print_dtypes() { |
| 161 | auto to_str = [](py::object obj) { |
| 162 | return (std::string) (py::str) ((py::object) obj.attr("__str__"))(); |
| 163 | }; |
| 164 | std::cout << to_str(py::dtype_of<SimpleStruct>()) << std::endl; |
| 165 | std::cout << to_str(py::dtype_of<PackedStruct>()) << std::endl; |
| 166 | std::cout << to_str(py::dtype_of<NestedStruct>()) << std::endl; |
Ivan Smirnov | 8fa09cb | 2016-07-06 00:28:12 +0100 | [diff] [blame] | 167 | std::cout << to_str(py::dtype_of<PartialStruct>()) << std::endl; |
| 168 | std::cout << to_str(py::dtype_of<PartialNestedStruct>()) << std::endl; |
Ivan Smirnov | f9c0def | 2016-07-20 00:19:24 +0100 | [diff] [blame^] | 169 | std::cout << to_str(py::dtype_of<StringStruct>()) << std::endl; |
Ivan Smirnov | 4f16421 | 2016-06-22 01:07:20 +0100 | [diff] [blame] | 170 | } |
| 171 | |
Ivan Smirnov | bb4015d | 2016-06-19 15:50:31 +0100 | [diff] [blame] | 172 | void init_ex20(py::module &m) { |
Ivan Smirnov | 5412a05 | 2016-07-02 16:18:42 +0100 | [diff] [blame] | 173 | PYBIND11_NUMPY_DTYPE(SimpleStruct, x, y, z); |
| 174 | PYBIND11_NUMPY_DTYPE(PackedStruct, x, y, z); |
| 175 | PYBIND11_NUMPY_DTYPE(NestedStruct, a, b); |
Ivan Smirnov | 8fa09cb | 2016-07-06 00:28:12 +0100 | [diff] [blame] | 176 | PYBIND11_NUMPY_DTYPE(PartialStruct, x, y, z); |
| 177 | PYBIND11_NUMPY_DTYPE(PartialNestedStruct, a); |
Ivan Smirnov | f9c0def | 2016-07-20 00:19:24 +0100 | [diff] [blame^] | 178 | PYBIND11_NUMPY_DTYPE(StringStruct, a, b); |
Ivan Smirnov | bb4015d | 2016-06-19 15:50:31 +0100 | [diff] [blame] | 179 | |
Ivan Smirnov | 4f16421 | 2016-06-22 01:07:20 +0100 | [diff] [blame] | 180 | m.def("create_rec_simple", &create_recarray<SimpleStruct>); |
Ivan Smirnov | bb4015d | 2016-06-19 15:50:31 +0100 | [diff] [blame] | 181 | m.def("create_rec_packed", &create_recarray<PackedStruct>); |
Ivan Smirnov | 7f913ae | 2016-06-19 16:41:15 +0100 | [diff] [blame] | 182 | m.def("create_rec_nested", &create_nested); |
Ivan Smirnov | 8fa09cb | 2016-07-06 00:28:12 +0100 | [diff] [blame] | 183 | m.def("create_rec_partial", &create_recarray<PartialStruct>); |
| 184 | m.def("create_rec_partial_nested", &create_partial_nested); |
Ivan Smirnov | bdc9902 | 2016-06-19 16:54:07 +0100 | [diff] [blame] | 185 | m.def("print_format_descriptors", &print_format_descriptors); |
Ivan Smirnov | 4f16421 | 2016-06-22 01:07:20 +0100 | [diff] [blame] | 186 | m.def("print_rec_simple", &print_recarray<SimpleStruct>); |
Ivan Smirnov | 669e142 | 2016-06-21 21:05:29 +0100 | [diff] [blame] | 187 | m.def("print_rec_packed", &print_recarray<PackedStruct>); |
| 188 | m.def("print_rec_nested", &print_recarray<NestedStruct>); |
Ivan Smirnov | 4f16421 | 2016-06-22 01:07:20 +0100 | [diff] [blame] | 189 | m.def("print_dtypes", &print_dtypes); |
Ivan Smirnov | d0bafd9 | 2016-06-26 16:35:28 +0100 | [diff] [blame] | 190 | m.def("get_format_unbound", &get_format_unbound); |
Ivan Smirnov | f9c0def | 2016-07-20 00:19:24 +0100 | [diff] [blame^] | 191 | m.def("create_string_array", &create_string_array); |
| 192 | m.def("print_string_array", &print_recarray<StringStruct>); |
Ivan Smirnov | bb4015d | 2016-06-19 15:50:31 +0100 | [diff] [blame] | 193 | } |
Ivan Smirnov | f5f75c6 | 2016-07-18 22:47:40 +0100 | [diff] [blame] | 194 | |
| 195 | #undef PYBIND11_PACKED |