blob: 8b18c05d47d20f7ecb8b9089fd320030e9cc5540 [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 Smirnov7f913ae2016-06-19 16:41:15 +010068template <typename T>
69py::array mkarray_via_buffer(size_t n) {
70 return py::array(py::buffer_info(nullptr, sizeof(T),
Ivan Smirnov5e71e172016-06-26 12:42:34 +010071 py::format_descriptor<T>::format(),
Ivan Smirnov7f913ae2016-06-19 16:41:15 +010072 1, { n }, { sizeof(T) }));
73}
Ivan Smirnovbb4015d2016-06-19 15:50:31 +010074
75template <typename S>
Ivan Smirnov8fa09cb2016-07-06 00:28:12 +010076py::array_t<S, 0> create_recarray(size_t n) {
Ivan Smirnov7f913ae2016-06-19 16:41:15 +010077 auto arr = mkarray_via_buffer<S>(n);
Ivan Smirnovb51fa022016-07-18 22:35:50 +010078 auto req = arr.request();
79 auto ptr = static_cast<S*>(req.ptr);
Ivan Smirnovbb4015d2016-06-19 15:50:31 +010080 for (size_t i = 0; i < n; i++) {
Ivan Smirnov7f913ae2016-06-19 16:41:15 +010081 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 +010082 }
83 return arr;
84}
85
Ivan Smirnovd0bafd92016-06-26 16:35:28 +010086std::string get_format_unbound() {
87 return py::format_descriptor<UnboundStruct>::format();
88}
89
Ivan Smirnov8fa09cb2016-07-06 00:28:12 +010090py::array_t<NestedStruct, 0> create_nested(size_t n) {
Ivan Smirnov7f913ae2016-06-19 16:41:15 +010091 auto arr = mkarray_via_buffer<NestedStruct>(n);
Ivan Smirnovb51fa022016-07-18 22:35:50 +010092 auto req = arr.request();
93 auto ptr = static_cast<NestedStruct*>(req.ptr);
Ivan Smirnov7f913ae2016-06-19 16:41:15 +010094 for (size_t i = 0; i < n; i++) {
95 ptr[i].a.x = i % 2; ptr[i].a.y = (uint32_t) i; ptr[i].a.z = (float) i * 1.5f;
96 ptr[i].b.x = (i + 1) % 2; ptr[i].b.y = (uint32_t) (i + 1); ptr[i].b.z = (float) (i + 1) * 1.5f;
97 }
98 return arr;
Ivan Smirnovbdc99022016-06-19 16:54:07 +010099}
Ivan Smirnov7f913ae2016-06-19 16:41:15 +0100100
Ivan Smirnov8fa09cb2016-07-06 00:28:12 +0100101py::array_t<PartialNestedStruct, 0> create_partial_nested(size_t n) {
102 auto arr = mkarray_via_buffer<PartialNestedStruct>(n);
Ivan Smirnovb51fa022016-07-18 22:35:50 +0100103 auto req = arr.request();
104 auto ptr = static_cast<PartialNestedStruct*>(req.ptr);
Ivan Smirnov8fa09cb2016-07-06 00:28:12 +0100105 for (size_t i = 0; i < n; i++) {
106 ptr[i].a.x = i % 2; ptr[i].a.y = (uint32_t) i; ptr[i].a.z = (float) i * 1.5f;
107 }
108 return arr;
109}
110
Ivan Smirnov669e1422016-06-21 21:05:29 +0100111template <typename S>
Ivan Smirnov8fa09cb2016-07-06 00:28:12 +0100112void print_recarray(py::array_t<S, 0> arr) {
Ivan Smirnovb51fa022016-07-18 22:35:50 +0100113 auto req = arr.request();
114 auto ptr = static_cast<S*>(req.ptr);
115 for (size_t i = 0; i < req.size; i++)
Ivan Smirnov669e1422016-06-21 21:05:29 +0100116 std::cout << ptr[i] << std::endl;
117}
Ivan Smirnovbdc99022016-06-19 16:54:07 +0100118
119void print_format_descriptors() {
Ivan Smirnov5e71e172016-06-26 12:42:34 +0100120 std::cout << py::format_descriptor<SimpleStruct>::format() << std::endl;
121 std::cout << py::format_descriptor<PackedStruct>::format() << std::endl;
122 std::cout << py::format_descriptor<NestedStruct>::format() << std::endl;
Ivan Smirnov8fa09cb2016-07-06 00:28:12 +0100123 std::cout << py::format_descriptor<PartialStruct>::format() << std::endl;
124 std::cout << py::format_descriptor<PartialNestedStruct>::format() << std::endl;
Ivan Smirnov7f913ae2016-06-19 16:41:15 +0100125}
126
Ivan Smirnov4f164212016-06-22 01:07:20 +0100127void print_dtypes() {
128 auto to_str = [](py::object obj) {
129 return (std::string) (py::str) ((py::object) obj.attr("__str__"))();
130 };
131 std::cout << to_str(py::dtype_of<SimpleStruct>()) << std::endl;
132 std::cout << to_str(py::dtype_of<PackedStruct>()) << std::endl;
133 std::cout << to_str(py::dtype_of<NestedStruct>()) << std::endl;
Ivan Smirnov8fa09cb2016-07-06 00:28:12 +0100134 std::cout << to_str(py::dtype_of<PartialStruct>()) << std::endl;
135 std::cout << to_str(py::dtype_of<PartialNestedStruct>()) << std::endl;
Ivan Smirnov4f164212016-06-22 01:07:20 +0100136}
137
Ivan Smirnovbb4015d2016-06-19 15:50:31 +0100138void init_ex20(py::module &m) {
Ivan Smirnov5412a052016-07-02 16:18:42 +0100139 PYBIND11_NUMPY_DTYPE(SimpleStruct, x, y, z);
140 PYBIND11_NUMPY_DTYPE(PackedStruct, x, y, z);
141 PYBIND11_NUMPY_DTYPE(NestedStruct, a, b);
Ivan Smirnov8fa09cb2016-07-06 00:28:12 +0100142 PYBIND11_NUMPY_DTYPE(PartialStruct, x, y, z);
143 PYBIND11_NUMPY_DTYPE(PartialNestedStruct, a);
Ivan Smirnovbb4015d2016-06-19 15:50:31 +0100144
Ivan Smirnov4f164212016-06-22 01:07:20 +0100145 m.def("create_rec_simple", &create_recarray<SimpleStruct>);
Ivan Smirnovbb4015d2016-06-19 15:50:31 +0100146 m.def("create_rec_packed", &create_recarray<PackedStruct>);
Ivan Smirnov7f913ae2016-06-19 16:41:15 +0100147 m.def("create_rec_nested", &create_nested);
Ivan Smirnov8fa09cb2016-07-06 00:28:12 +0100148 m.def("create_rec_partial", &create_recarray<PartialStruct>);
149 m.def("create_rec_partial_nested", &create_partial_nested);
Ivan Smirnovbdc99022016-06-19 16:54:07 +0100150 m.def("print_format_descriptors", &print_format_descriptors);
Ivan Smirnov4f164212016-06-22 01:07:20 +0100151 m.def("print_rec_simple", &print_recarray<SimpleStruct>);
Ivan Smirnov669e1422016-06-21 21:05:29 +0100152 m.def("print_rec_packed", &print_recarray<PackedStruct>);
153 m.def("print_rec_nested", &print_recarray<NestedStruct>);
Ivan Smirnov4f164212016-06-22 01:07:20 +0100154 m.def("print_dtypes", &print_dtypes);
Ivan Smirnovd0bafd92016-06-26 16:35:28 +0100155 m.def("get_format_unbound", &get_format_unbound);
Ivan Smirnovbb4015d2016-06-19 15:50:31 +0100156}
Ivan Smirnovf5f75c62016-07-18 22:47:40 +0100157
158#undef PYBIND11_PACKED