-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathpybind11.h
More file actions
262 lines (230 loc) · 8 KB
/
Copy pathpybind11.h
File metadata and controls
262 lines (230 loc) · 8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* LSST Data Management System
* Copyright 2008-2016 AURA/LSST.
*
* This product includes software developed by the
* LSST Project (http://www.lsst.org/).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the LSST License Statement and
* the GNU General Public License along with this program. If not,
* see <https://www.lsstcorp.org/LegalNotices/>.
*/
#ifndef NDARRAY_pybind11_h_INCLUDED
#define NDARRAY_pybind11_h_INCLUDED
/**
* @file ndarray/pybind11.h
* @brief Public header file for pybind11-based Python support.
*
* \warning Both the Numpy C-API headers "arrayobject.h" and
* "ufuncobject.h" must be included before ndarray/python.hpp
* or any of the files in ndarray/python.
*
* \note This file is not included by the main "ndarray.h" header file.
*/
/** \defgroup ndarrayPythonGroup Python Support
*
* The ndarray Python support module provides conversion
* functions between ndarray objects, notably Array and
* Vector, and Python Numpy objects.
*/
#include "pybind11.h"
#include "pybind11/numpy.h"
#include "ndarray.h"
#include "ndarray/eigen.h"
namespace ndarray {
namespace detail {
inline void destroyCapsule(PyObject * p) {
void * m = PyCapsule_GetPointer(p, "ndarray.Manager");
Manager::Ptr * b = reinterpret_cast<Manager::Ptr*>(m);
delete b;
}
} // namespace ndarray::detail
inline PyObject* makePyManager(Manager::Ptr const & m) {
return PyCapsule_New(
new Manager::Ptr(m),
"ndarray.Manager",
detail::destroyCapsule
);
}
#if PYBIND11_VERSION_MAJOR == 2 && PYBIND11_VERSION_MINOR <= 1
using pybind11_np_size_t = size_t;
#else
using pybind11_np_size_t = ssize_t;
#endif
template <typename T, int N, int C>
struct
#ifdef __GNUG__
// pybind11 hides all symbols in its namespace only when this is set,
// and in that case we should hide these classes too.
__attribute__((visibility("hidden")))
#endif
Pybind11Helper {
using Element = typename ndarray::Array<T,N,C>::Element;
using Wrapper = pybind11::array_t<typename std::remove_const<Element>::type, 0>; // 0: no ensurecopy
static constexpr bool isConst = std::is_const<Element>::value;
Pybind11Helper() : isNone(false), wrapper() {}
bool init(pybind11::handle src) {
isNone = src.is_none();
if (isNone) {
return true;
}
if (!Wrapper::check_(src)) {
return false;
}
try {
wrapper = pybind11::reinterpret_borrow<Wrapper>(src);
} catch (pybind11::error_already_set & err) {
return false;
}
return true;
}
bool check() const {
if (isNone) {
return true;
}
if (!wrapper) {
return false;
}
if (wrapper.ndim() != N) {
return false;
}
if (!isConst && !wrapper.writeable()) {
return false;
}
pybind11_np_size_t const * shape = wrapper.shape();
pybind11_np_size_t const * strides = wrapper.strides();
pybind11_np_size_t const itemsize = wrapper.itemsize();
if (C > 0) {
// If the shape is zero in any dimension, we don't
// worry about the strides.
for (int i = 0; i < C; ++i) {
if (shape[N-i-1] == 0) {
return true;
}
}
pybind11_np_size_t requiredStride = itemsize;
for (int i = 0; i < C; ++i) {
if (strides[N-i-1] != requiredStride) {
return false;
}
requiredStride *= shape[N-i-1];
}
} else if (C < 0) {
// If the shape is zero in any dimension, we don't
// worry about the strides.
for (int i = 0; i < -C; ++i) {
if (shape[i] == 0) {
return true;
}
}
pybind11_np_size_t requiredStride = itemsize;
for (int i = 0; i < -C; ++i) {
if (strides[i] != requiredStride) {
return false;
}
requiredStride *= shape[i];
}
}
return true;
}
ndarray::Array<T,N,C> convert() const {
if (isNone) {
return ndarray::Array<T,N,C>();
}
if (!pybind11::reinterpret_borrow<pybind11::bool_>(wrapper.dtype().attr("isnative"))) {
PyErr_SetString(PyExc_TypeError, "Only arrays with native byteorder can be converted to C++.");
throw pybind11::error_already_set();
}
Vector<ndarray::Size,N> nShape;
Vector<ndarray::Offset,N> nStrides;
pybind11_np_size_t const * pShape = wrapper.shape();
pybind11_np_size_t const * pStrides = wrapper.strides();
pybind11_np_size_t const itemsize = wrapper.itemsize();
for (int i = 0; i < N; ++i) {
if (pStrides[i] % itemsize != 0) {
PyErr_SetString(
PyExc_TypeError,
"Cannot convert array to C++: strides must be an integer multiple of the element size"
);
throw pybind11::error_already_set();
}
nShape[i] = pShape[i];
nStrides[i] = pStrides[i]/itemsize;
}
return ndarray::Array<T,N,C>(
ndarray::external(const_cast<Element*>(wrapper.data()),
nShape, nStrides, pybind11::object(wrapper))
);
}
static pybind11::handle toPython(ndarray::Array<T,N,C> const & src) {
Vector<Size,N> nShape = src.getShape();
Vector<Offset,N> nStrides = src.getStrides();
std::vector<pybind11_np_size_t> pShape(N);
std::vector<pybind11_np_size_t> pStrides(N);
pybind11_np_size_t const itemsize = sizeof(Element);
for (int i = 0; i < N; ++i) {
pShape[i] = nShape[i];
pStrides[i] = nStrides[i]*itemsize;
}
pybind11::object base;
if (src.getManager()) {
base = pybind11::reinterpret_steal<pybind11::object>(ndarray::makePyManager(src.getManager()));
}
Wrapper result(pShape, pStrides, src.getData(), base);
if (std::is_const<Element>::value) {
result.attr("flags")["WRITEABLE"] = false;
}
return result.release();
}
bool isNone;
Wrapper wrapper;
};
} // namespace ndarray
namespace pybind11 {
namespace detail {
/* @brief A pybind11 type_caster for ndarray::Array
*/
template <typename T, int N, int C>
class type_caster< ndarray::Array<T,N,C> > {
using Helper = ndarray::Pybind11Helper<T,N,C>;
public:
bool load(handle src, bool) {
return _helper.init(src) && _helper.check();
}
void set_value() {
_value = _helper.convert();
}
static handle cast(const ndarray::Array<T,N,C> &src, return_value_policy /* policy */, handle /* parent */) {
return Helper::toPython(src);
}
static constexpr auto name = _("numpy.ndarray");
static handle cast(const ndarray::Array<T,N,C> *src, return_value_policy policy, handle parent) {
return cast(*src, policy, parent);
}
operator ndarray::Array<T,N,C> * () {
if (_helper.isNone) {
return nullptr;
} else {
set_value();
return &_value;
}
}
operator ndarray::Array<T,N,C> & () { set_value(); return _value; }
template <typename _T> using cast_op_type = pybind11::detail::cast_op_type<_T>;
private:
ndarray::Array<T,N,C> _value;
Helper _helper;
};
} // namespace detail
} // namespace pybind11
#endif // !NDARRAY_pybind11_h_INCLUDED