blob: 07849a099d4f1964fd8326dfb5aaaca66121ce9a [file] [log] [blame]
Ivan Smirnovbb4015d2016-06-19 15:50:31 +01001/*
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 Smirnovf5f75c62016-07-18 22:47:40 +010016#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 Smirnovbb4015d2016-06-19 15:50:31 +010022namespace py = pybind11;
23
Ivan Smirnov4f164212016-06-22 01:07:20 +010024struct SimpleStruct {
Ivan Smirnovbb4015d2016-06-19 15:50:31 +010025 bool x;
26 uint32_t y;
27 float z;
28};
29
Ivan Smirnov4f164212016-06-22 01:07:20 +010030std::ostream& operator<<(std::ostream& os, const SimpleStruct& v) {
31 return os << "s:" << v.x << "," << v.y << "," << v.z;
Ivan Smirnov669e1422016-06-21 21:05:29 +010032}
33
Ivan Smirnovf5f75c62016-07-18 22:47:40 +010034PYBIND11_PACKED(struct PackedStruct {
Ivan Smirnovbb4015d2016-06-19 15:50:31 +010035 bool x;
36 uint32_t y;
37 float z;
Ivan Smirnovf5f75c62016-07-18 22:47:40 +010038});
Ivan Smirnovbb4015d2016-06-19 15:50:31 +010039
Ivan Smirnov669e1422016-06-21 21:05:29 +010040std::ostream& operator<<(std::ostream& os, const PackedStruct& v) {
Ivan Smirnov4f164212016-06-22 01:07:20 +010041 return os << "p:" << v.x << "," << v.y << "," << v.z;
Ivan Smirnov669e1422016-06-21 21:05:29 +010042}
43
Ivan Smirnovf5f75c62016-07-18 22:47:40 +010044PYBIND11_PACKED(struct NestedStruct {
Ivan Smirnov4f164212016-06-22 01:07:20 +010045 SimpleStruct a;
Ivan Smirnovbb4015d2016-06-19 15:50:31 +010046 PackedStruct b;
Ivan Smirnovf5f75c62016-07-18 22:47:40 +010047});
Ivan Smirnov7f913ae2016-06-19 16:41:15 +010048
Ivan Smirnov669e1422016-06-21 21:05:29 +010049std::ostream& operator<<(std::ostream& os, const NestedStruct& v) {
Ivan Smirnov4f164212016-06-22 01:07:20 +010050 return os << "n:a=" << v.a << ";b=" << v.b;
Ivan Smirnov669e1422016-06-21 21:05:29 +010051}
52
Ivan Smirnov8fa09cb2016-07-06 00:28:12 +010053struct PartialStruct {
54 bool x;
55 uint32_t y;
56 float z;
57 long dummy2;
58};
59
60struct PartialNestedStruct {
61 long dummy1;
62 PartialStruct a;
63 long dummy2;
64};
65
Ivan Smirnovd0bafd92016-06-26 16:35:28 +010066struct UnboundStruct { };
67
Ivan Smirnovf9c0def2016-07-20 00:19:24 +010068struct StringStruct {
69 char a[3];
70 std::array<char, 3> b;
71};
72
73std::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 Smirnov7f913ae2016-06-19 16:41:15 +010081template <typename T>
82py::array mkarray_via_buffer(size_t n) {
83 return py::array(py::buffer_info(nullptr, sizeof(T),
Ivan Smirnov5e71e172016-06-26 12:42:34 +010084 py::format_descriptor<T>::format(),
Ivan Smirnov7f913ae2016-06-19 16:41:15 +010085 1, { n }, { sizeof(T) }));
86}
Ivan Smirnovbb4015d2016-06-19 15:50:31 +010087
88template <typename S>
Ivan Smirnov8fa09cb2016-07-06 00:28:12 +010089py::array_t<S, 0> create_recarray(size_t n) {
Ivan Smirnov7f913ae2016-06-19 16:41:15 +010090 auto arr = mkarray_via_buffer<S>(n);
Ivan Smirnovb51fa022016-07-18 22:35:50 +010091 auto req = arr.request();
92 auto ptr = static_cast<S*>(req.ptr);
Ivan Smirnovbb4015d2016-06-19 15:50:31 +010093 for (size_t i = 0; i < n; i++) {
Ivan Smirnov7f913ae2016-06-19 16:41:15 +010094 ptr[i].x = i % 2; ptr[i].y = (uint32_t) i; ptr[i].z = (float) i * 1.5f;
Ivan Smirnovbb4015d2016-06-19 15:50:31 +010095 }
96 return arr;
97}
98
Ivan Smirnovd0bafd92016-06-26 16:35:28 +010099std::string get_format_unbound() {
100 return py::format_descriptor<UnboundStruct>::format();
101}
102
Ivan Smirnov8fa09cb2016-07-06 00:28:12 +0100103py::array_t<NestedStruct, 0> create_nested(size_t n) {
Ivan Smirnov7f913ae2016-06-19 16:41:15 +0100104 auto arr = mkarray_via_buffer<NestedStruct>(n);
Ivan Smirnovb51fa022016-07-18 22:35:50 +0100105 auto req = arr.request();
106 auto ptr = static_cast<NestedStruct*>(req.ptr);
Ivan Smirnov7f913ae2016-06-19 16:41:15 +0100107 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 Smirnovbdc99022016-06-19 16:54:07 +0100112}
Ivan Smirnov7f913ae2016-06-19 16:41:15 +0100113
Ivan Smirnov8fa09cb2016-07-06 00:28:12 +0100114py::array_t<PartialNestedStruct, 0> create_partial_nested(size_t n) {
115 auto arr = mkarray_via_buffer<PartialNestedStruct>(n);
Ivan Smirnovb51fa022016-07-18 22:35:50 +0100116 auto req = arr.request();
117 auto ptr = static_cast<PartialNestedStruct*>(req.ptr);
Ivan Smirnov8fa09cb2016-07-06 00:28:12 +0100118 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 Smirnovf9c0def2016-07-20 00:19:24 +0100124py::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 Smirnov669e1422016-06-21 21:05:29 +0100143template <typename S>
Ivan Smirnov8fa09cb2016-07-06 00:28:12 +0100144void print_recarray(py::array_t<S, 0> arr) {
Ivan Smirnovb51fa022016-07-18 22:35:50 +0100145 auto req = arr.request();
146 auto ptr = static_cast<S*>(req.ptr);
147 for (size_t i = 0; i < req.size; i++)
Ivan Smirnov669e1422016-06-21 21:05:29 +0100148 std::cout << ptr[i] << std::endl;
149}
Ivan Smirnovbdc99022016-06-19 16:54:07 +0100150
151void print_format_descriptors() {
Ivan Smirnov5e71e172016-06-26 12:42:34 +0100152 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 Smirnov8fa09cb2016-07-06 00:28:12 +0100155 std::cout << py::format_descriptor<PartialStruct>::format() << std::endl;
156 std::cout << py::format_descriptor<PartialNestedStruct>::format() << std::endl;
Ivan Smirnovf9c0def2016-07-20 00:19:24 +0100157 std::cout << py::format_descriptor<StringStruct>::format() << std::endl;
Ivan Smirnov7f913ae2016-06-19 16:41:15 +0100158}
159
Ivan Smirnov4f164212016-06-22 01:07:20 +0100160void 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 Smirnov8fa09cb2016-07-06 00:28:12 +0100167 std::cout << to_str(py::dtype_of<PartialStruct>()) << std::endl;
168 std::cout << to_str(py::dtype_of<PartialNestedStruct>()) << std::endl;
Ivan Smirnovf9c0def2016-07-20 00:19:24 +0100169 std::cout << to_str(py::dtype_of<StringStruct>()) << std::endl;
Ivan Smirnov4f164212016-06-22 01:07:20 +0100170}
171
Ivan Smirnovbb4015d2016-06-19 15:50:31 +0100172void init_ex20(py::module &m) {
Ivan Smirnov5412a052016-07-02 16:18:42 +0100173 PYBIND11_NUMPY_DTYPE(SimpleStruct, x, y, z);
174 PYBIND11_NUMPY_DTYPE(PackedStruct, x, y, z);
175 PYBIND11_NUMPY_DTYPE(NestedStruct, a, b);
Ivan Smirnov8fa09cb2016-07-06 00:28:12 +0100176 PYBIND11_NUMPY_DTYPE(PartialStruct, x, y, z);
177 PYBIND11_NUMPY_DTYPE(PartialNestedStruct, a);
Ivan Smirnovf9c0def2016-07-20 00:19:24 +0100178 PYBIND11_NUMPY_DTYPE(StringStruct, a, b);
Ivan Smirnovbb4015d2016-06-19 15:50:31 +0100179
Ivan Smirnov4f164212016-06-22 01:07:20 +0100180 m.def("create_rec_simple", &create_recarray<SimpleStruct>);
Ivan Smirnovbb4015d2016-06-19 15:50:31 +0100181 m.def("create_rec_packed", &create_recarray<PackedStruct>);
Ivan Smirnov7f913ae2016-06-19 16:41:15 +0100182 m.def("create_rec_nested", &create_nested);
Ivan Smirnov8fa09cb2016-07-06 00:28:12 +0100183 m.def("create_rec_partial", &create_recarray<PartialStruct>);
184 m.def("create_rec_partial_nested", &create_partial_nested);
Ivan Smirnovbdc99022016-06-19 16:54:07 +0100185 m.def("print_format_descriptors", &print_format_descriptors);
Ivan Smirnov4f164212016-06-22 01:07:20 +0100186 m.def("print_rec_simple", &print_recarray<SimpleStruct>);
Ivan Smirnov669e1422016-06-21 21:05:29 +0100187 m.def("print_rec_packed", &print_recarray<PackedStruct>);
188 m.def("print_rec_nested", &print_recarray<NestedStruct>);
Ivan Smirnov4f164212016-06-22 01:07:20 +0100189 m.def("print_dtypes", &print_dtypes);
Ivan Smirnovd0bafd92016-06-26 16:35:28 +0100190 m.def("get_format_unbound", &get_format_unbound);
Ivan Smirnovf9c0def2016-07-20 00:19:24 +0100191 m.def("create_string_array", &create_string_array);
192 m.def("print_string_array", &print_recarray<StringStruct>);
Ivan Smirnovbb4015d2016-06-19 15:50:31 +0100193}
Ivan Smirnovf5f75c62016-07-18 22:47:40 +0100194
195#undef PYBIND11_PACKED