Skip to content

Commit 0334425

Browse files
committed
core: add the possibility to share or not the memory between Numpy and Eigen
1 parent 374746d commit 0334425

File tree

3 files changed

+51
-12
lines changed

3 files changed

+51
-12
lines changed

include/eigenpy/numpy-allocator.hpp

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,19 @@ namespace eigenpy
4040
typedef typename SimilarMatrixType::Scalar Scalar;
4141
enum { NPY_ARRAY_MEMORY_CONTIGUOUS = SimilarMatrixType::IsRowMajor ? NPY_ARRAY_CARRAY : NPY_ARRAY_FARRAY };
4242

43-
PyArrayObject * pyArray = (PyArrayObject*) call_PyArray_New(nd, shape,
44-
NumpyEquivalentType<Scalar>::type_code,
45-
mat.data(),
46-
NPY_ARRAY_MEMORY_CONTIGUOUS | NPY_ARRAY_ALIGNED);
47-
48-
return pyArray;
43+
if(NumpyType::sharedMemory())
44+
{
45+
PyArrayObject * pyArray = (PyArrayObject*) call_PyArray_New(nd, shape,
46+
NumpyEquivalentType<Scalar>::type_code,
47+
mat.data(),
48+
NPY_ARRAY_MEMORY_CONTIGUOUS | NPY_ARRAY_ALIGNED);
49+
50+
return pyArray;
51+
}
52+
else
53+
{
54+
return NumpyAllocator<MatType>::allocate(mat.derived(),nd,shape);
55+
}
4956
}
5057
};
5158

@@ -68,12 +75,19 @@ namespace eigenpy
6875
typedef typename SimilarMatrixType::Scalar Scalar;
6976
enum { NPY_ARRAY_MEMORY_CONTIGUOUS_RO = SimilarMatrixType::IsRowMajor ? NPY_ARRAY_CARRAY_RO : NPY_ARRAY_FARRAY_RO };
7077

71-
PyArrayObject * pyArray = (PyArrayObject*) call_PyArray_New(nd, shape,
72-
NumpyEquivalentType<Scalar>::type_code,
73-
const_cast<SimilarMatrixType &>(mat.derived()).data(),
74-
NPY_ARRAY_MEMORY_CONTIGUOUS_RO | NPY_ARRAY_ALIGNED);
75-
76-
return pyArray;
78+
if(NumpyType::sharedMemory())
79+
{
80+
PyArrayObject * pyArray = (PyArrayObject*) call_PyArray_New(nd, shape,
81+
NumpyEquivalentType<Scalar>::type_code,
82+
const_cast<SimilarMatrixType &>(mat.derived()).data(),
83+
NPY_ARRAY_MEMORY_CONTIGUOUS_RO | NPY_ARRAY_ALIGNED);
84+
85+
return pyArray;
86+
}
87+
else
88+
{
89+
return NumpyAllocator<MatType>::allocate(mat.derived(),nd,shape);
90+
}
7791
}
7892
};
7993

include/eigenpy/numpy-type.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ namespace eigenpy
9696
switchToNumpyArray();
9797
}
9898

99+
static void sharedMemory(const bool value)
100+
{
101+
getInstance().shared_memory = value;
102+
}
103+
104+
static bool sharedMemory()
105+
{
106+
return getInstance().shared_memory;
107+
}
108+
99109
static void switchToNumpyArray()
100110
{
101111
getInstance().CurrentNumpyType = getInstance().NumpyArrayObject;
@@ -162,6 +172,8 @@ namespace eigenpy
162172

163173
CurrentNumpyType = NumpyArrayObject; // default conversion
164174
np_type = ARRAY_TYPE;
175+
176+
shared_memory = true;
165177
}
166178

167179
bp::object CurrentNumpyType;
@@ -173,6 +185,8 @@ namespace eigenpy
173185
bp::object NumpyArrayObject; PyTypeObject * NumpyArrayType;
174186

175187
NP_TYPE np_type;
188+
189+
bool shared_memory;
176190
};
177191
}
178192

src/eigenpy.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ namespace eigenpy
4545
bp::def("switchToNumpyMatrix",&NumpyType::switchToNumpyMatrix,
4646
"Set the conversion from Eigen::Matrix to numpy.matrix.");
4747

48+
bp::def("sharedMemory",
49+
(void (*)(const bool))NumpyType::sharedMemory,
50+
bp::arg("value"),
51+
"Share the memory when converting Eigen::Matrix to numpy.array.");
52+
53+
bp::def("sharedMemory",
54+
(bool (*)())NumpyType::sharedMemory,
55+
"Status of the shared memory when converting Eigen::Matrix to numpy.array.\n"
56+
"If True, the memory is shared when converting an Eigen::Matrix to a numpy.array.\n"
57+
"Otherwise, a deep copy of the Eigen::Matrix is performed");
58+
4859
bp::def("seed",&seed,bp::arg("seed_value"),
4960
"Initialize the pseudo-random number generator with the argument seed_value.");
5061

0 commit comments

Comments
 (0)