example20.cpp 4.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
  example/example20.cpp -- Usage of structured numpy dtypes

  Copyright (c) 2016 Ivan Smirnov

  All rights reserved. Use of this source code is governed by a
  BSD-style license that can be found in the LICENSE file.
*/

#include "example.h"

#include <pybind11/numpy.h>
#include <cstdint>
#include <iostream>

16
17
18
19
20
21
#ifdef __GNUC__
#define PYBIND11_PACKED(cls) cls __attribute__((__packed__))
#else
#define PYBIND11_PACKED(cls) __pragma(pack(push, 1)) cls __pragma(pack(pop))
#endif

22
23
namespace py = pybind11;

24
struct SimpleStruct {
25
26
27
28
29
    bool x;
    uint32_t y;
    float z;
};

30
31
std::ostream& operator<<(std::ostream& os, const SimpleStruct& v) {
    return os << "s:" << v.x << "," << v.y << "," << v.z;
32
33
}

34
PYBIND11_PACKED(struct PackedStruct {
35
36
37
    bool x;
    uint32_t y;
    float z;
38
});
39

40
std::ostream& operator<<(std::ostream& os, const PackedStruct& v) {
41
    return os << "p:" << v.x << "," << v.y << "," << v.z;
42
43
}

44
PYBIND11_PACKED(struct NestedStruct {
45
    SimpleStruct a;
46
    PackedStruct b;
47
});
Ivan Smirnov's avatar
Ivan Smirnov committed
48

49
std::ostream& operator<<(std::ostream& os, const NestedStruct& v) {
50
    return os << "n:a=" << v.a << ";b=" << v.b;
51
52
}

53
54
55
56
57
58
59
60
61
62
63
64
65
struct PartialStruct {
    bool x;
    uint32_t y;
    float z;
    long dummy2;
};

struct PartialNestedStruct {
    long dummy1;
    PartialStruct a;
    long dummy2;
};

66
67
struct UnboundStruct { };

Ivan Smirnov's avatar
Ivan Smirnov committed
68
69
70
template <typename T>
py::array mkarray_via_buffer(size_t n) {
    return py::array(py::buffer_info(nullptr, sizeof(T),
71
                                     py::format_descriptor<T>::format(),
Ivan Smirnov's avatar
Ivan Smirnov committed
72
73
                                     1, { n }, { sizeof(T) }));
}
74
75

template <typename S>
76
py::array_t<S, 0> create_recarray(size_t n) {
Ivan Smirnov's avatar
Ivan Smirnov committed
77
    auto arr = mkarray_via_buffer<S>(n);
78
79
    auto req = arr.request();
    auto ptr = static_cast<S*>(req.ptr);
Ivan Smirnov's avatar
Ivan Smirnov committed
80
81
82
83
84
85
    for (size_t i = 0; i < n; i++) {
        ptr[i].x = i % 2; ptr[i].y = (uint32_t) i; ptr[i].z = (float) i * 1.5f;
    }
    return arr;
}

86
87
88
89
std::string get_format_unbound() {
    return py::format_descriptor<UnboundStruct>::format();
}

90
py::array_t<NestedStruct, 0> create_nested(size_t n) {
Ivan Smirnov's avatar
Ivan Smirnov committed
91
    auto arr = mkarray_via_buffer<NestedStruct>(n);
92
93
    auto req = arr.request();
    auto ptr = static_cast<NestedStruct*>(req.ptr);
94
    for (size_t i = 0; i < n; i++) {
Ivan Smirnov's avatar
Ivan Smirnov committed
95
96
        ptr[i].a.x = i % 2; ptr[i].a.y = (uint32_t) i; ptr[i].a.z = (float) i * 1.5f;
        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;
99
100
}

101
102
py::array_t<PartialNestedStruct, 0> create_partial_nested(size_t n) {
    auto arr = mkarray_via_buffer<PartialNestedStruct>(n);
103
104
    auto req = arr.request();
    auto ptr = static_cast<PartialNestedStruct*>(req.ptr);
105
106
107
108
109
110
    for (size_t i = 0; i < n; i++) {
        ptr[i].a.x = i % 2; ptr[i].a.y = (uint32_t) i; ptr[i].a.z = (float) i * 1.5f;
    }
    return arr;
}

111
template <typename S>
112
void print_recarray(py::array_t<S, 0> arr) {
113
114
115
    auto req = arr.request();
    auto ptr = static_cast<S*>(req.ptr);
    for (size_t i = 0; i < req.size; i++)
116
117
        std::cout << ptr[i] << std::endl;
}
Ivan Smirnov's avatar
Ivan Smirnov committed
118

119
void print_format_descriptors() {
120
121
122
    std::cout << py::format_descriptor<SimpleStruct>::format() << std::endl;
    std::cout << py::format_descriptor<PackedStruct>::format() << std::endl;
    std::cout << py::format_descriptor<NestedStruct>::format() << std::endl;
123
124
    std::cout << py::format_descriptor<PartialStruct>::format() << std::endl;
    std::cout << py::format_descriptor<PartialNestedStruct>::format() << std::endl;
125
126
}

127
128
129
130
131
132
133
void print_dtypes() {
    auto to_str = [](py::object obj) {
        return (std::string) (py::str) ((py::object) obj.attr("__str__"))();
    };
    std::cout << to_str(py::dtype_of<SimpleStruct>()) << std::endl;
    std::cout << to_str(py::dtype_of<PackedStruct>()) << std::endl;
    std::cout << to_str(py::dtype_of<NestedStruct>()) << std::endl;
134
135
    std::cout << to_str(py::dtype_of<PartialStruct>()) << std::endl;
    std::cout << to_str(py::dtype_of<PartialNestedStruct>()) << std::endl;
136
137
}

138
void init_ex20(py::module &m) {
139
140
141
    PYBIND11_NUMPY_DTYPE(SimpleStruct, x, y, z);
    PYBIND11_NUMPY_DTYPE(PackedStruct, x, y, z);
    PYBIND11_NUMPY_DTYPE(NestedStruct, a, b);
142
143
    PYBIND11_NUMPY_DTYPE(PartialStruct, x, y, z);
    PYBIND11_NUMPY_DTYPE(PartialNestedStruct, a);
144

145
    m.def("create_rec_simple", &create_recarray<SimpleStruct>);
146
    m.def("create_rec_packed", &create_recarray<PackedStruct>);
Ivan Smirnov's avatar
Ivan Smirnov committed
147
    m.def("create_rec_nested", &create_nested);
148
149
    m.def("create_rec_partial", &create_recarray<PartialStruct>);
    m.def("create_rec_partial_nested", &create_partial_nested);
150
    m.def("print_format_descriptors", &print_format_descriptors);
151
    m.def("print_rec_simple", &print_recarray<SimpleStruct>);
152
153
    m.def("print_rec_packed", &print_recarray<PackedStruct>);
    m.def("print_rec_nested", &print_recarray<NestedStruct>);
154
    m.def("print_dtypes", &print_dtypes);
155
    m.def("get_format_unbound", &get_format_unbound);
156
}
157
158

#undef PYBIND11_PACKED