eigenpy 3.13.0
Bindings between Numpy and Eigen using Boost.Python
Loading...
Searching...
No Matches
eigen-allocator.hpp
1//
2// Copyright (c) 2014-2023 CNRS INRIA
3//
4
5#ifndef __eigenpy_eigen_allocator_hpp__
6#define __eigenpy_eigen_allocator_hpp__
7
8#include "eigenpy/fwd.hpp"
9#include "eigenpy/numpy-map.hpp"
10#include "eigenpy/register.hpp"
11#include "eigenpy/scalar-conversion.hpp"
12#include "eigenpy/utils/is-aligned.hpp"
13
14namespace eigenpy {
15
16namespace details {
17template <typename MatType,
18 bool IsVectorAtCompileTime = MatType::IsVectorAtCompileTime>
20 static MatType* run(int rows, int cols, void* storage) {
21 if (storage)
22 return new (storage) MatType(rows, cols);
23 else
24 return new MatType(rows, cols);
25 }
26
27 static MatType* run(PyArrayObject* pyArray, void* storage = NULL) {
28 assert(PyArray_NDIM(pyArray) == 1 || PyArray_NDIM(pyArray) == 2);
29
30 int rows = -1, cols = -1;
31 const int ndim = PyArray_NDIM(pyArray);
32 if (ndim == 2) {
33 rows = (int)PyArray_DIMS(pyArray)[0];
34 cols = (int)PyArray_DIMS(pyArray)[1];
35 } else if (ndim == 1) {
36 rows = (int)PyArray_DIMS(pyArray)[0];
37 cols = 1;
38 }
39
40 return run(rows, cols, storage);
41 }
42};
43
44template <typename MatType>
45struct init_matrix_or_array<MatType, true> {
46 static MatType* run(int rows, int cols, void* storage) {
47 if (storage)
48 return new (storage) MatType(rows, cols);
49 else
50 return new MatType(rows, cols);
51 }
52
53 static MatType* run(int size, void* storage) {
54 if (storage)
55 return new (storage) MatType(size);
56 else
57 return new MatType(size);
58 }
59
60 static MatType* run(PyArrayObject* pyArray, void* storage = NULL) {
61 const int ndim = PyArray_NDIM(pyArray);
62 if (ndim == 1) {
63 const int size = (int)PyArray_DIMS(pyArray)[0];
64 return run(size, storage);
65 } else {
66 const int rows = (int)PyArray_DIMS(pyArray)[0];
67 const int cols = (int)PyArray_DIMS(pyArray)[1];
68 return run(rows, cols, storage);
69 }
70 }
71};
72
73#ifdef EIGENPY_WITH_TENSOR_SUPPORT
74template <typename Tensor>
75struct init_tensor {
76 static Tensor* run(PyArrayObject* pyArray, void* storage = NULL) {
77 enum { Rank = Tensor::NumDimensions };
78 assert(PyArray_NDIM(pyArray) == Rank);
79 typedef typename Tensor::Index Index;
80
81 Eigen::array<Index, Rank> dimensions;
82 for (int k = 0; k < PyArray_NDIM(pyArray); ++k)
83 dimensions[k] = PyArray_DIMS(pyArray)[k];
84
85 if (storage)
86 return new (storage) Tensor(dimensions);
87 else
88 return new Tensor(dimensions);
89 }
90};
91#endif
92
93template <typename MatType>
95
96template <typename EigenType,
97 typename BaseType = typename get_eigen_base_type<EigenType>::type>
99
100template <typename MatType>
101struct check_swap_impl<MatType, Eigen::MatrixBase<MatType>>
102 : check_swap_impl_matrix<MatType> {};
103
104template <typename MatType>
106 static bool run(PyArrayObject* pyArray,
107 const Eigen::MatrixBase<MatType>& mat) {
108 if (PyArray_NDIM(pyArray) == 0) return false;
109 if (mat.rows() == PyArray_DIMS(pyArray)[0])
110 return false;
111 else
112 return true;
113 }
114};
115
116template <typename EigenType>
117bool check_swap(PyArrayObject* pyArray, const EigenType& mat) {
118 return check_swap_impl<EigenType>::run(pyArray, mat);
119}
120
121#ifdef EIGENPY_WITH_TENSOR_SUPPORT
122template <typename TensorType>
123struct check_swap_impl_tensor {
124 static bool run(PyArrayObject* /*pyArray*/, const TensorType& /*tensor*/) {
125 return false;
126 }
127};
128
129template <typename TensorType>
130struct check_swap_impl<TensorType, Eigen::TensorBase<TensorType>>
131 : check_swap_impl_tensor<TensorType> {};
132#endif
133
134// template <typename MatType>
135// struct cast_impl_matrix;
136//
137// template <typename EigenType,
138// typename BaseType = typename get_eigen_base_type<EigenType>::type>
139// struct cast_impl;
140//
141// template <typename MatType>
142// struct cast_impl<MatType, Eigen::MatrixBase<MatType> >
143// : cast_impl_matrix<MatType> {};
144//
145// template <typename MatType>
146// struct cast_impl_matrix
147//{
148// template <typename NewScalar, typename MatrixIn, typename MatrixOut>
149// static void run(const Eigen::MatrixBase<MatrixIn> &input,
150// const Eigen::MatrixBase<MatrixOut> &dest) {
151// dest.const_cast_derived() = input.template cast<NewScalar>();
152// }
153// };
154
155template <typename Scalar, typename NewScalar,
156 template <typename D> class EigenBase = Eigen::MatrixBase,
157 bool cast_is_valid = FromTypeToType<Scalar, NewScalar>::value>
158struct cast {
159 template <typename MatrixIn, typename MatrixOut>
160 static void run(const Eigen::MatrixBase<MatrixIn>& input,
161 const Eigen::MatrixBase<MatrixOut>& dest) {
162 dest.const_cast_derived() = input.template cast<NewScalar>();
163 }
164};
165
166#ifdef EIGENPY_WITH_TENSOR_SUPPORT
167template <typename Scalar, typename NewScalar>
168struct cast<Scalar, NewScalar, Eigen::TensorRef, true> {
169 template <typename TensorIn, typename TensorOut>
170 static void run(const TensorIn& input, TensorOut& dest) {
171 dest = input.template cast<NewScalar>();
172 }
173};
174#endif
175
176template <typename Scalar, typename NewScalar,
177 template <typename D> class EigenBase>
178struct cast<Scalar, NewScalar, EigenBase, false> {
179 template <typename MatrixIn, typename MatrixOut>
180 static void run(const MatrixIn /*input*/, const MatrixOut /*dest*/) {
181 // do nothing
182 assert(false && "Must never happened");
183 }
184};
185
186} // namespace details
187
188#define EIGENPY_CAST_FROM_PYARRAY_TO_EIGEN_MATRIX(MatType, Scalar, NewScalar, \
189 pyArray, mat) \
190 details::cast<Scalar, NewScalar>::run( \
191 NumpyMap<MatType, Scalar>::map(pyArray, \
192 details::check_swap(pyArray, mat)), \
193 mat)
194
195#define EIGENPY_CAST_FROM_EIGEN_MATRIX_TO_PYARRAY(MatType, Scalar, NewScalar, \
196 mat, pyArray) \
197 details::cast<Scalar, NewScalar>::run( \
198 mat, NumpyMap<MatType, NewScalar>::map( \
199 pyArray, details::check_swap(pyArray, mat)))
200
201// Define specific cast for Windows and Mac
202#if defined _WIN32 || defined __CYGWIN__
203// Manage NPY_INT on Windows (NPY_INT32 is NPY_LONG).
204// See https://github.com/stack-of-tasks/eigenpy/pull/455
205#define EIGENPY_CAST_FROM_NUMPY_TO_EIGEN_SWITCH_OS_SPECIFIC( \
206 MatType, Scalar, pyArray, mat, CAST_MACRO) \
207 case NPY_INT: \
208 CAST_MACRO(MatType, int32_t, Scalar, pyArray, mat); \
209 break; \
210 case NPY_UINT: \
211 CAST_MACRO(MatType, uint32_t, Scalar, pyArray, mat); \
212 break;
213#elif defined __APPLE__
214// Manage NPY_LONGLONG on Mac (NPY_INT64 is NPY_LONG).
215// long long and long are both the same type
216// but NPY_LONGLONG and NPY_LONGĀ are different dtype.
217// See https://github.com/stack-of-tasks/eigenpy/pull/455
218#define EIGENPY_CAST_FROM_NUMPY_TO_EIGEN_SWITCH_OS_SPECIFIC( \
219 MatType, Scalar, pyArray, mat, CAST_MACRO) \
220 case NPY_LONGLONG: \
221 CAST_MACRO(MatType, int64_t, Scalar, pyArray, mat); \
222 break; \
223 case NPY_ULONGLONG: \
224 CAST_MACRO(MatType, uint64_t, Scalar, pyArray, mat); \
225 break;
226#else
227#define EIGENPY_CAST_FROM_NUMPY_TO_EIGEN_SWITCH_OS_SPECIFIC( \
228 MatType, Scalar, pyArray, mat, CAST_MACRO)
229#endif
230
232#define EIGENPY_CAST_FROM_NUMPY_TO_EIGEN_SWITCH( \
233 pyArray_type_code, MatType, Scalar, pyArray, mat, CAST_MACRO) \
234 switch (pyArray_type_code) { \
235 case NPY_BOOL: \
236 CAST_MACRO(MatType, bool, Scalar, pyArray, mat); \
237 break; \
238 case NPY_INT8: \
239 CAST_MACRO(MatType, int8_t, Scalar, pyArray, mat); \
240 break; \
241 case NPY_INT16: \
242 CAST_MACRO(MatType, int16_t, Scalar, pyArray, mat); \
243 break; \
244 case NPY_INT32: \
245 CAST_MACRO(MatType, int32_t, Scalar, pyArray, mat); \
246 break; \
247 case NPY_INT64: \
248 CAST_MACRO(MatType, int64_t, Scalar, pyArray, mat); \
249 break; \
250 case NPY_UINT8: \
251 CAST_MACRO(MatType, uint8_t, Scalar, pyArray, mat); \
252 break; \
253 case NPY_UINT16: \
254 CAST_MACRO(MatType, uint16_t, Scalar, pyArray, mat); \
255 break; \
256 case NPY_UINT32: \
257 CAST_MACRO(MatType, uint32_t, Scalar, pyArray, mat); \
258 break; \
259 case NPY_UINT64: \
260 CAST_MACRO(MatType, uint64_t, Scalar, pyArray, mat); \
261 break; \
262 case NPY_FLOAT: \
263 CAST_MACRO(MatType, float, Scalar, pyArray, mat); \
264 break; \
265 case NPY_CFLOAT: \
266 CAST_MACRO(MatType, std::complex<float>, Scalar, pyArray, mat); \
267 break; \
268 case NPY_DOUBLE: \
269 CAST_MACRO(MatType, double, Scalar, pyArray, mat); \
270 break; \
271 case NPY_CDOUBLE: \
272 CAST_MACRO(MatType, std::complex<double>, Scalar, pyArray, mat); \
273 break; \
274 case NPY_LONGDOUBLE: \
275 CAST_MACRO(MatType, long double, Scalar, pyArray, mat); \
276 break; \
277 case NPY_CLONGDOUBLE: \
278 CAST_MACRO(MatType, std::complex<long double>, Scalar, pyArray, mat); \
279 break; \
280 EIGENPY_CAST_FROM_NUMPY_TO_EIGEN_SWITCH_OS_SPECIFIC( \
281 MatType, Scalar, pyArray, mat, CAST_MACRO) \
282 default: \
283 throw Exception("You asked for a conversion which is not implemented."); \
284 }
285
286template <typename EigenType>
287struct EigenAllocator;
288
289template <typename EigenType,
290 typename BaseType = typename get_eigen_base_type<EigenType>::type>
292
293template <typename MatType>
295
296template <typename MatType>
297struct eigen_allocator_impl<MatType, Eigen::MatrixBase<MatType>>
298 : eigen_allocator_impl_matrix<MatType> {};
299
300template <typename MatType>
301struct eigen_allocator_impl<const MatType, const Eigen::MatrixBase<MatType>>
302 : eigen_allocator_impl_matrix<const MatType> {};
303
304template <typename MatType>
306 typedef MatType Type;
307 typedef typename MatType::Scalar Scalar;
308
309 static void allocate(
310 PyArrayObject* pyArray,
311 boost::python::converter::rvalue_from_python_storage<MatType>* storage) {
312 void* raw_ptr = storage->storage.bytes;
313 assert(is_aligned(raw_ptr, EIGENPY_DEFAULT_ALIGN_BYTES) &&
314 "The pointer is not aligned.");
315
316 Type* mat_ptr = details::init_matrix_or_array<Type>::run(pyArray, raw_ptr);
317 Type& mat = *mat_ptr;
318
319 copy(pyArray, mat);
320 }
321
323 template <typename MatrixDerived>
324 static void copy(PyArrayObject* pyArray,
325 const Eigen::MatrixBase<MatrixDerived>& mat_) {
326 MatrixDerived& mat = mat_.const_cast_derived();
327 const int pyArray_type_code = EIGENPY_GET_PY_ARRAY_TYPE(pyArray);
328 const int Scalar_type_code = Register::getTypeCode<Scalar>();
329
330 if (pyArray_type_code == Scalar_type_code) {
332 pyArray, details::check_swap(pyArray, mat)); // avoid useless cast
333 return;
334 }
335 EIGENPY_CAST_FROM_NUMPY_TO_EIGEN_SWITCH(
336 pyArray_type_code, MatType, Scalar, pyArray, mat,
337 EIGENPY_CAST_FROM_PYARRAY_TO_EIGEN_MATRIX);
338 }
339
341 template <typename MatrixDerived>
342 static void copy(const Eigen::MatrixBase<MatrixDerived>& mat_,
343 PyArrayObject* pyArray) {
344 const MatrixDerived& mat = const_cast<const MatrixDerived&>(mat_.derived());
345 const int pyArray_type_code = EIGENPY_GET_PY_ARRAY_TYPE(pyArray);
346 const int Scalar_type_code = Register::getTypeCode<Scalar>();
347
348 if (pyArray_type_code == Scalar_type_code) // no cast needed
349 {
351 details::check_swap(pyArray, mat)) = mat;
352 return;
353 }
354 throw Exception(
355 "Scalar conversion from Eigen to Numpy is not implemented.");
356 }
357};
358
359#ifdef EIGENPY_WITH_TENSOR_SUPPORT
360template <typename TensorType>
361struct eigen_allocator_impl_tensor;
362
363template <typename TensorType>
364struct eigen_allocator_impl<TensorType, Eigen::TensorBase<TensorType>>
365 : eigen_allocator_impl_tensor<TensorType> {};
366
367template <typename TensorType>
368struct eigen_allocator_impl<const TensorType,
369 const Eigen::TensorBase<TensorType>>
370 : eigen_allocator_impl_tensor<const TensorType> {};
371
372template <typename TensorType>
373struct eigen_allocator_impl_tensor {
374 typedef typename TensorType::Scalar Scalar;
375 static void allocate(
376 PyArrayObject* pyArray,
377 boost::python::converter::rvalue_from_python_storage<TensorType>*
378 storage) {
379 void* raw_ptr = storage->storage.bytes;
380 assert(is_aligned(raw_ptr, EIGENPY_DEFAULT_ALIGN_BYTES) &&
381 "The pointer is not aligned.");
382
383 TensorType* tensor_ptr =
384 details::init_tensor<TensorType>::run(pyArray, raw_ptr);
385 TensorType& tensor = *tensor_ptr;
386
387 copy(pyArray, tensor);
388 }
389
390#define EIGENPY_CAST_FROM_PYARRAY_TO_EIGEN_TENSOR(TensorType, Scalar, \
391 NewScalar, pyArray, tensor) \
392 { \
393 typename NumpyMap<TensorType, Scalar>::EigenMap pyArray_map = \
394 NumpyMap<TensorType, Scalar>::map( \
395 pyArray, details::check_swap(pyArray, tensor)); \
396 details::cast<Scalar, NewScalar, Eigen::TensorRef>::run(pyArray_map, \
397 tensor); \
398 }
399
401 template <typename TensorDerived>
402 static void copy(PyArrayObject* pyArray, TensorDerived& tensor) {
403 const int pyArray_type_code = EIGENPY_GET_PY_ARRAY_TYPE(pyArray);
404 const int Scalar_type_code = Register::getTypeCode<Scalar>();
405
406 if (pyArray_type_code == Scalar_type_code) {
407 tensor = NumpyMap<TensorType, Scalar>::map(
408 pyArray, details::check_swap(pyArray, tensor)); // avoid useless cast
409 return;
410 }
411
412 EIGENPY_CAST_FROM_NUMPY_TO_EIGEN_SWITCH(
413 pyArray_type_code, TensorType, Scalar, pyArray, tensor,
414 EIGENPY_CAST_FROM_PYARRAY_TO_EIGEN_TENSOR);
415 }
416
417#define EIGENPY_CAST_FROM_EIGEN_TENSOR_TO_PYARRAY(TensorType, Scalar, \
418 NewScalar, tensor, pyArray) \
419 { \
420 typename NumpyMap<TensorType, NewScalar>::EigenMap pyArray_map = \
421 NumpyMap<TensorType, NewScalar>::map( \
422 pyArray, details::check_swap(pyArray, tensor)); \
423 details::cast<Scalar, NewScalar, Eigen::TensorRef>::run(tensor, \
424 pyArray_map); \
425 }
426
428 static void copy(const TensorType& tensor, PyArrayObject* pyArray) {
429 const int pyArray_type_code = EIGENPY_GET_PY_ARRAY_TYPE(pyArray);
430 const int Scalar_type_code = Register::getTypeCode<Scalar>();
431
432 if (pyArray_type_code == Scalar_type_code) // no cast needed
433 {
434 NumpyMap<TensorType, Scalar>::map(
435 pyArray, details::check_swap(pyArray, tensor)) = tensor;
436 return;
437 }
438
439 throw Exception(
440 "Scalar conversion from Eigen to Numpy is not implemented.");
441 }
442};
443#endif
444
445#if EIGEN_VERSION_AT_LEAST(3, 2, 0)
453template <typename MatType>
454inline bool is_arr_layout_compatible_with_mat_type(PyArrayObject* pyArray) {
455 bool is_array_C_cont = PyArray_IS_C_CONTIGUOUS(pyArray);
456 bool is_array_F_cont = PyArray_IS_F_CONTIGUOUS(pyArray);
457 return (MatType::IsRowMajor && is_array_C_cont) ||
458 (!MatType::IsRowMajor && is_array_F_cont) ||
459 (MatType::IsVectorAtCompileTime &&
460 (is_array_C_cont || is_array_F_cont));
461}
462
463template <typename MatType, int Options, typename Stride>
464struct eigen_allocator_impl_matrix<Eigen::Ref<MatType, Options, Stride>> {
465 typedef Eigen::Ref<MatType, Options, Stride> RefType;
466 typedef typename MatType::Scalar Scalar;
467
468 typedef
469 typename ::boost::python::detail::referent_storage<RefType&>::StorageType
470 StorageType;
471
472 static void allocate(
473 PyArrayObject* pyArray,
474 ::boost::python::converter::rvalue_from_python_storage<RefType>*
475 storage) {
476 typedef typename StrideType<
477 MatType,
478 Eigen::internal::traits<RefType>::StrideType::InnerStrideAtCompileTime,
479 Eigen::internal::traits<RefType>::StrideType::
480 OuterStrideAtCompileTime>::type NumpyMapStride;
481
482 bool need_to_allocate = false;
483 const int pyArray_type_code = EIGENPY_GET_PY_ARRAY_TYPE(pyArray);
484 const int Scalar_type_code = Register::getTypeCode<Scalar>();
485 if (pyArray_type_code != Scalar_type_code) need_to_allocate |= true;
486 bool incompatible_layout =
487 !is_arr_layout_compatible_with_mat_type<MatType>(pyArray);
488 need_to_allocate |= incompatible_layout;
489 if (Options !=
490 Eigen::Unaligned) // we need to check whether the memory is correctly
491 // aligned and composed of a continuous segment
492 {
493 void* data_ptr = PyArray_DATA(pyArray);
494 if (!PyArray_ISONESEGMENT(pyArray) || !is_aligned(data_ptr, Options))
495 need_to_allocate |= true;
496 }
497
498 void* raw_ptr = storage->storage.bytes;
499 if (need_to_allocate) {
500 MatType* mat_ptr;
501 mat_ptr = details::init_matrix_or_array<MatType>::run(pyArray);
502 RefType mat_ref(*mat_ptr);
503
504 new (raw_ptr) StorageType(mat_ref, pyArray, mat_ptr);
505
506 RefType& mat = *reinterpret_cast<RefType*>(raw_ptr);
507 EigenAllocator<MatType>::copy(pyArray, mat);
508 } else {
509 assert(pyArray_type_code == Scalar_type_code);
510 typename NumpyMap<MatType, Scalar, Options, NumpyMapStride>::EigenMap
511 numpyMap =
512 NumpyMap<MatType, Scalar, Options, NumpyMapStride>::map(pyArray);
513 RefType mat_ref(numpyMap);
514 new (raw_ptr) StorageType(mat_ref, pyArray);
515 }
516 }
517
518 static void copy(RefType const& ref, PyArrayObject* pyArray) {
519 EigenAllocator<MatType>::copy(ref, pyArray);
520 }
521};
522
523template <typename MatType, int Options, typename Stride>
525 const Eigen::Ref<const MatType, Options, Stride>> {
526 typedef const Eigen::Ref<const MatType, Options, Stride> RefType;
527 typedef typename MatType::Scalar Scalar;
528
529 typedef
530 typename ::boost::python::detail::referent_storage<RefType&>::StorageType
531 StorageType;
532
533 static void allocate(
534 PyArrayObject* pyArray,
535 ::boost::python::converter::rvalue_from_python_storage<RefType>*
536 storage) {
537 typedef typename StrideType<
538 MatType,
539 Eigen::internal::traits<RefType>::StrideType::InnerStrideAtCompileTime,
540 Eigen::internal::traits<RefType>::StrideType::
541 OuterStrideAtCompileTime>::type NumpyMapStride;
542
543 bool need_to_allocate = false;
544 const int pyArray_type_code = EIGENPY_GET_PY_ARRAY_TYPE(pyArray);
545 const int Scalar_type_code = Register::getTypeCode<Scalar>();
546
547 if (pyArray_type_code != Scalar_type_code) need_to_allocate |= true;
548 bool incompatible_layout =
549 !is_arr_layout_compatible_with_mat_type<MatType>(pyArray);
550 need_to_allocate |= incompatible_layout;
551 if (Options !=
552 Eigen::Unaligned) // we need to check whether the memory is correctly
553 // aligned and composed of a continuous segment
554 {
555 void* data_ptr = PyArray_DATA(pyArray);
556 if (!PyArray_ISONESEGMENT(pyArray) || !is_aligned(data_ptr, Options))
557 need_to_allocate |= true;
558 }
559
560 void* raw_ptr = storage->storage.bytes;
561 if (need_to_allocate) {
562 MatType* mat_ptr;
563 mat_ptr = details::init_matrix_or_array<MatType>::run(pyArray);
564 RefType mat_ref(*mat_ptr);
565
566 new (raw_ptr) StorageType(mat_ref, pyArray, mat_ptr);
567
568 MatType& mat = *mat_ptr;
569 EigenAllocator<MatType>::copy(pyArray, mat);
570 } else {
571 assert(pyArray_type_code == Scalar_type_code);
572 typename NumpyMap<MatType, Scalar, Options, NumpyMapStride>::EigenMap
573 numpyMap =
574 NumpyMap<MatType, Scalar, Options, NumpyMapStride>::map(pyArray);
575 RefType mat_ref(numpyMap);
576 new (raw_ptr) StorageType(mat_ref, pyArray);
577 }
578 }
579
580 static void copy(RefType const& ref, PyArrayObject* pyArray) {
581 EigenAllocator<MatType>::copy(ref, pyArray);
582 }
583};
584#endif
585
586#ifdef EIGENPY_WITH_TENSOR_SUPPORT
587
588template <typename TensorType, typename TensorRef>
589struct eigen_allocator_impl_tensor_ref;
590
591template <typename TensorType>
592struct eigen_allocator_impl_tensor<Eigen::TensorRef<TensorType>>
593 : eigen_allocator_impl_tensor_ref<TensorType,
594 Eigen::TensorRef<TensorType>> {};
595
596template <typename TensorType>
597struct eigen_allocator_impl_tensor<const Eigen::TensorRef<const TensorType>>
598 : eigen_allocator_impl_tensor_ref<
599 const TensorType, const Eigen::TensorRef<const TensorType>> {};
600
601template <typename TensorType, typename RefType>
602struct eigen_allocator_impl_tensor_ref {
603 typedef typename TensorType::Scalar Scalar;
604
605 typedef
606 typename ::boost::python::detail::referent_storage<RefType&>::StorageType
607 StorageType;
608
609 static void allocate(
610 PyArrayObject* pyArray,
611 ::boost::python::converter::rvalue_from_python_storage<RefType>*
612 storage) {
613 // typedef typename StrideType<
614 // MatType,
615 // Eigen::internal::traits<RefType>::StrideType::InnerStrideAtCompileTime,
616 // Eigen::internal::traits<RefType>::StrideType::
617 // OuterStrideAtCompileTime>::type NumpyMapStride;
618
619 static const int Options = Eigen::internal::traits<TensorType>::Options;
620
621 bool need_to_allocate = false;
622 const int pyArray_type_code = EIGENPY_GET_PY_ARRAY_TYPE(pyArray);
623 const int Scalar_type_code = Register::getTypeCode<Scalar>();
624 if (pyArray_type_code != Scalar_type_code) need_to_allocate |= true;
625 // bool incompatible_layout =
626 // !is_arr_layout_compatible_with_mat_type<MatType>(pyArray);
627 // need_to_allocate |= incompatible_layout;
628 // if (Options !=
629 // Eigen::Unaligned) // we need to check whether the memory is
630 // correctly
631 // // aligned and composed of a continuous segment
632 // {
633 // void *data_ptr = PyArray_DATA(pyArray);
634 // if (!PyArray_ISONESEGMENT(pyArray) || !is_aligned(data_ptr,
635 // Options))
636 // need_to_allocate |= true;
637 // }
638
639 void* raw_ptr = storage->storage.bytes;
640 if (need_to_allocate) {
641 typedef typename boost::remove_const<TensorType>::type TensorTypeNonConst;
642 TensorTypeNonConst* tensor_ptr;
643 tensor_ptr = details::init_tensor<TensorTypeNonConst>::run(pyArray);
644 RefType tensor_ref(*tensor_ptr);
645
646 new (raw_ptr) StorageType(tensor_ref, pyArray, tensor_ptr);
647
648 TensorTypeNonConst& tensor = *tensor_ptr;
649 EigenAllocator<TensorTypeNonConst>::copy(pyArray, tensor);
650 } else {
651 assert(pyArray_type_code == Scalar_type_code);
652 typename NumpyMap<TensorType, Scalar, Options>::EigenMap numpyMap =
653 NumpyMap<TensorType, Scalar, Options>::map(pyArray);
654 RefType tensor_ref(numpyMap);
655 new (raw_ptr) StorageType(tensor_ref, pyArray);
656 }
657 }
658
659 static void copy(RefType const& ref, PyArrayObject* pyArray) {
660 EigenAllocator<TensorType>::copy(ref, pyArray);
661 }
662};
663
664#endif
665
666template <typename EigenType>
667struct EigenAllocator : eigen_allocator_impl<EigenType> {};
668
669} // namespace eigenpy
670
671#endif // __eigenpy_eigen_allocator_hpp__
static void copy(PyArrayObject *pyArray, const Eigen::MatrixBase< MatrixDerived > &mat_)
Copy Python array into the input matrix mat.
static void copy(const Eigen::MatrixBase< MatrixDerived > &mat_, PyArrayObject *pyArray)
Copy mat into the Python array using Eigen::Map.