forked from xtensor-stack/xtensor-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyarray.hpp
More file actions
561 lines (453 loc) · 17.1 KB
/
Copy pathpyarray.hpp
File metadata and controls
561 lines (453 loc) · 17.1 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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/***************************************************************************
* Copyright (c) 2016, Johan Mabille and Sylvain Corlay *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#ifndef PY_ARRAY_HPP
#define PY_ARRAY_HPP
#include <cstddef>
#include <algorithm>
#include "pybind11/numpy.h"
#include "xtensor/xexpression.hpp"
#include "xtensor/xsemantic.hpp"
#include "xtensor/xiterator.hpp"
namespace xt
{
using pybind_array = pybind11::array;
using buffer_info = pybind11::buffer_info;
/***********************
* pyarray declaration *
***********************/
template <class T, int ExtraFlags>
class pyarray;
template <class T, int ExtraFlags>
struct array_inner_types<pyarray<T, ExtraFlags>>
{
using temporary_type = pyarray<T, ExtraFlags>;
};
template <class A>
class pyarray_backstrides
{
public:
using array_type = A;
using value_type = typename array_type::size_type;
using size_type = typename array_type::size_type;
pyarray_backstrides(const A& a);
value_type operator[](size_type i) const;
private:
const pybind_array* p_a;
};
/**
* @class pyarray
* @brief Wrapper on the Python buffer protocol.
*/
template <class T, int ExtraFlags = pybind_array::forcecast>
class pyarray : public pybind_array,
public xarray_semantic<pyarray<T, ExtraFlags>>
{
public:
using self_type = pyarray<T, ExtraFlags>;
using base_type = pybind_array;
using semantic_base = xarray_semantic<self_type>;
using value_type = T;
using reference = T&;
using const_reference = const T&;
using pointer = T*;
using const_pointer = const T*;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using stepper = xstepper<self_type>;
using const_stepper = xstepper<const self_type>;
using iterator = xiterator<stepper>;
using const_iterator = xiterator<const_stepper>;
using storage_iterator = T*;
using const_storage_iterator = const T*;
using shape_type = xshape<size_type>;
using strides_type = xstrides<size_type>;
using backstrides_type = pyarray_backstrides<self_type>;
using closure_type = const self_type&;
PYBIND11_OBJECT_CVT(pyarray, pybind_array, is_non_null, m_ptr = ensure_(m_ptr));
pyarray();
explicit pyarray(const buffer_info& info);
pyarray(const xshape<size_type>& shape,
const xstrides<size_type>& strides,
const T* ptr = nullptr,
handle base = handle());
explicit pyarray(const xshape<size_type>& shape,
const T* ptr = nullptr,
handle base = handle());
explicit pyarray(size_type count,
const T* ptr = nullptr,
handle base = handle());
size_type dimension() const;
const shape_type& shape() const;
const strides_type& strides() const;
backstrides_type backstrides() const;
void reshape(const shape_type& shape);
void reshape(const shape_type& shape, layout l);
void reshape(const shape_type& shape, const strides_type& strides);
template<typename... Args>
reference operator()(Args... args);
template<typename... Args>
const_reference operator()(Args... args) const;
template<typename... Args>
pointer data(Args... args);
template<typename... Args>
const_pointer data(Args... args) const;
bool broadcast_shape(shape_type& shape) const;
bool is_trivial_broadcast(const strides_type& strides) const;
iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const;
const_iterator cbegin() const;
const_iterator cend() const;
iterator xbegin(const shape_type& shape);
iterator xend(const shape_type& shape);
const_iterator xbegin(const shape_type& shape) const;
const_iterator xend(const shape_type& shape) const;
const_iterator cxbegin(const shape_type& shape) const;
const_iterator cxend(const shape_type& shape) const;
stepper stepper_begin(const shape_type& shape);
stepper stepper_end(const shape_type& shape);
const_stepper stepper_begin(const shape_type& shape) const;
const_stepper stepper_end(const shape_type& shape) const;
storage_iterator storage_begin();
storage_iterator storage_end();
const_storage_iterator storage_begin() const;
const_storage_iterator storage_end() const;
template <class E>
pyarray(const xexpression<E>& e);
template <class E>
pyarray& operator=(const xexpression<E>& e);
private:
template<typename... Args>
auto index_at(Args... args) const -> size_type;
static constexpr auto itemsize() -> size_type;
static bool is_non_null(PyObject* ptr);
static PyObject *ensure_(PyObject* ptr);
mutable shape_type m_shape;
mutable strides_type m_strides;
};
/**************************************
* pyarray_backstrides implementation *
**************************************/
template <class A>
inline pyarray_backstrides<A>::pyarray_backstrides(const A& a)
: p_a(&a)
{
}
template <class A>
inline auto pyarray_backstrides<A>::operator[](size_type i) const -> value_type
{
value_type sh = p_a->shape()[i];
value_type res = sh == 1 ? 0 : (sh - 1) * p_a->strides()[i] / sizeof(typename A::value_type);
return res;
}
/**************************
* pyarray implementation *
**************************/
template <class T, int ExtraFlags>
inline pyarray<T, ExtraFlags>::pyarray()
: pybind_array()
{
}
template <class T, int ExtraFlags>
inline pyarray<T, ExtraFlags>::pyarray(const buffer_info& info)
: pybind_array(info)
{
}
template <class T, int ExtraFlags>
inline pyarray<T, ExtraFlags>::pyarray(const xshape<size_type>& shape,
const xstrides<size_type>& strides,
const T *ptr,
handle base)
: pybind_array(shape, strides, ptr, base)
{
}
template <class T, int ExtraFlags>
inline pyarray<T, ExtraFlags>::pyarray(const xshape<size_type>& shape,
const T* ptr,
handle base)
: pybind_array(shape, ptr, base)
{
}
template <class T, int ExtraFlags>
inline pyarray<T, ExtraFlags>::pyarray(size_type count,
const T* ptr,
handle base)
: pybind_array(count, ptr, base)
{
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::dimension() const -> size_type
{
return pybind_array::ndim();
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::shape() const -> const shape_type&
{
// Until we have the CRTP on shape types, we copy the shape.
m_shape.resize(dimension());
std::copy(pybind_array::shape(), pybind_array::shape() + dimension(), m_shape.begin());
return m_shape;
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::strides() const -> const strides_type&
{
m_strides.resize(dimension());
std::transform(pybind_array::strides(), pybind_array::strides() + dimension(), m_strides.begin(),
[](size_type str) { return str / sizeof(value_type); });
return m_strides;
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::backstrides() const -> backstrides_type
{
backstrides_type tmp(*this);
return tmp;
}
template <class T, int ExtraFlags>
void pyarray<T, ExtraFlags>::reshape(const shape_type& shape)
{
if (!m_ptr || shape.size() != dimension() || !std::equal(shape.begin(), shape.end(), pybind_array::shape()))
{
reshape(shape, layout::row_major);
}
}
template <class T, int ExtraFlags>
void pyarray<T, ExtraFlags>::reshape(const shape_type& shape, layout l)
{
strides_type strides(shape.size());
size_type data_size = sizeof(value_type);
if (l == layout::row_major)
{
for (size_type i = strides.size(); i != 0; --i)
{
strides[i - 1] = data_size;
data_size = strides[i - 1] * shape[i - 1];
if (shape[i - 1] == 1)
{
strides[i - 1] = 0;
}
}
}
else
{
for (size_type i = 0; i < strides.size(); ++i)
{
strides[i] = data_size;
data_size = strides[i] * shape[i];
if (shape[i] == 1)
{
strides[i] = 0;
}
}
}
reshape(shape, strides);
}
template <class T, int ExtraFlags>
void pyarray<T, ExtraFlags>::reshape(const shape_type& shape, const strides_type& strides)
{
self_type tmp(shape, strides);
*this = std::move(tmp);
}
template <class T, int ExtraFlags>
template<typename... Args>
inline auto pyarray<T, ExtraFlags>::operator()(Args... args) -> reference
{
if (sizeof...(args) != dimension())
{
pybind_array::fail_dim_check(sizeof...(args), "index dimension mismatch");
}
// not using pybind_array::offset_at() / index_at() here so as to avoid another dimension check.
return *(static_cast<pointer>(pybind_array::mutable_data()) + pybind_array::get_byte_offset(args...) / itemsize());
}
template <class T, int ExtraFlags>
template<typename... Args>
inline auto pyarray<T, ExtraFlags>::operator()(Args... args) const -> const_reference
{
if (sizeof...(args) != dimension())
{
pybind_array::fail_dim_check(sizeof...(args), "index dimension mismatch");
}
// not using pybind_array::offset_at() / index_at() here so as to avoid another dimension check.
return *(static_cast<const_pointer>(pybind_array::data()) + pybind_array::get_byte_offset(args...) / itemsize());
}
template <class T, int ExtraFlags>
template<typename... Args>
inline auto pyarray<T, ExtraFlags>::data(Args... args) -> pointer
{
return static_cast<pointer>(pybind_array::mutable_data(args...));
}
template <class T, int ExtraFlags>
template<typename... Args>
inline auto pyarray<T, ExtraFlags>::data(Args... args) const -> const_pointer
{
return static_cast<const T*>(pybind_array::data(args...));
}
template <class T, int ExtraFlags>
bool pyarray<T, ExtraFlags>::broadcast_shape(shape_type& shape) const
{
return xt::broadcast_shape(this->shape(), shape);
}
template <class T, int ExtraFlags>
bool pyarray<T, ExtraFlags>::is_trivial_broadcast(const strides_type& strides) const
{
return strides.size() == dimension() &&
std::equal(strides.begin(), strides.end(), this->strides().begin());
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::begin() -> iterator
{
return xbegin(shape());
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::end() -> iterator
{
return xend(shape());
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::begin() const -> const_iterator
{
return xbegin(shape());
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::end() const -> const_iterator
{
return xend(shape());
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::cbegin() const -> const_iterator
{
return begin();
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::cend() const -> const_iterator
{
return end();
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::xbegin(const shape_type& shape) -> iterator
{
return iterator(stepper_begin(shape), shape);
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::xend(const shape_type& shape) -> iterator
{
return iterator(stepper_end(shape), shape);
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::xbegin(const shape_type& shape) const -> const_iterator
{
return const_iterator(stepper_begin(shape), shape);
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::xend(const shape_type& shape) const -> const_iterator
{
return const_iterator(stepper_end(shape), shape);
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::cxbegin(const shape_type& shape) const -> const_iterator
{
return xbegin(shape);
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::cxend(const shape_type& shape) const -> const_iterator
{
return xend(shape);
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::stepper_begin(const shape_type& shape) -> stepper
{
size_type offset = shape.size() - dimension();
return stepper(this, storage_begin(), offset);
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::stepper_end(const shape_type& shape) -> stepper
{
size_type offset = shape.size() - dimension();
return stepper(this, storage_end(), offset);
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::stepper_begin(const shape_type& shape) const -> const_stepper
{
size_type offset = shape.size() - dimension();
return const_stepper(this, storage_begin(), offset);
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::stepper_end(const shape_type& shape) const -> const_stepper
{
size_type offset = shape.size() - dimension();
return const_stepper(this, storage_end(), offset);
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::storage_begin() -> storage_iterator
{
return reinterpret_cast<storage_iterator>(PyArray_GET_(m_ptr, data));
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::storage_end() -> storage_iterator
{
return storage_begin() + pybind_array::size();
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::storage_begin() const -> const_storage_iterator
{
return reinterpret_cast<const_storage_iterator>(PyArray_GET_(m_ptr, data));
}
template <class T, int ExtraFlags>
inline auto pyarray<T, ExtraFlags>::storage_end() const -> const_storage_iterator
{
return storage_begin() + pybind_array::size();
}
template <class T, int ExtraFlags>
template <class E>
inline pyarray<T, ExtraFlags>::pyarray(const xexpression<E>& e)
: pybind_array()
{
semantic_base::assign(e);
}
template <class T, int ExtraFlags>
template <class E>
inline auto pyarray<T, ExtraFlags>::operator=(const xexpression<E>& e) -> self_type&
{
return semantic_base::operator=(e);
}
// Private methods
template <class T, int ExtraFlags>
template<typename... Args>
inline auto pyarray<T, ExtraFlags>::index_at(Args... args) const -> size_type
{
return pybind_array::offset_at(args...) / itemsize();
}
template <class T, int ExtraFlags>
constexpr auto pyarray<T, ExtraFlags>::itemsize() -> size_type
{
return sizeof(value_type);
}
template <class T, int ExtraFlags>
inline bool pyarray<T, ExtraFlags>::is_non_null(PyObject* ptr)
{
return ptr != nullptr;
}
template <class T, int ExtraFlags>
inline PyObject* pyarray<T, ExtraFlags>::ensure_(PyObject* ptr)
{
if (ptr == nullptr)
{
return nullptr;
}
auto& api = pybind11::detail::npy_api::get();
PyObject *result = api.PyArray_FromAny_(ptr, pybind11::dtype::of<T>().release().ptr(), 0, 0,
pybind11::detail::npy_api::NPY_ENSURE_ARRAY_ | ExtraFlags, nullptr);
if (!result)
{
PyErr_Clear();
}
Py_DECREF(ptr);
return result;
}
}
#endif