Skip to content

Commit 2bd9964

Browse files
authored
Merge pull request #172 from jcarpent/devel
Authorize the sharing of memory between Eigen and Numpy
2 parents 952227d + 155d92e commit 2bd9964

File tree

6 files changed

+86
-18
lines changed

6 files changed

+86
-18
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

package.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0"?>
22
<package format="2">
33
<name>eigenpy</name>
4-
<version>2.1.2</version>
4+
<version>2.2.0</version>
55
<description>Bindings between Numpy and Eigen using Boost.Python</description>
66
<maintainer email="[email protected]">Justin Carpentier</maintainer>
77
<maintainer email="[email protected]">Wolfgang Merkt</maintainer>

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 from Eigen to Numpy.");
52+
53+
bp::def("sharedMemory",
54+
(bool (*)())NumpyType::sharedMemory,
55+
"Status of the shared memory when converting from Eigen to Numpy.\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

unittest/python/test_return_by_ref.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import return_by_ref
12
from return_by_ref import Matrix, RowMatrix, Vector
23
import numpy as np
34

4-
def test(mat):
5+
def test_shared(mat):
56

67
m_ref = mat.ref()
78
m_ref.fill(0)
@@ -22,13 +23,41 @@ def test(mat):
2223
except:
2324
assert True
2425

26+
def test_not_shared(mat):
27+
28+
m_ref = mat.ref()
29+
m_ref.fill(100.)
30+
m_copy = mat.copy()
31+
assert not np.array_equal(m_ref,m_copy)
32+
33+
m_const_ref = mat.const_ref()
34+
assert np.array_equal(m_const_ref,m_copy)
35+
assert not np.array_equal(m_const_ref,m_ref)
36+
37+
m_ref.fill(10.)
38+
assert not np.array_equal(m_ref,m_copy)
39+
assert not np.array_equal(m_const_ref,m_ref)
40+
41+
try:
42+
m_const_ref.fill(2)
43+
assert True
44+
except:
45+
assert False
46+
2547
rows = 10
2648
cols = 30
2749

2850
mat = Matrix(rows,cols)
2951
row_mat = RowMatrix(rows,cols)
3052
vec = Vector(rows,1)
3153

32-
test(mat)
33-
test(row_mat)
34-
test(vec)
54+
test_shared(mat)
55+
test_shared(row_mat)
56+
test_shared(vec)
57+
58+
return_by_ref.sharedMemory(False)
59+
test_not_shared(mat)
60+
test_not_shared(row_mat)
61+
test_not_shared(vec)
62+
63+

0 commit comments

Comments
 (0)