Unverified Commit 2bd99645 authored by Justin Carpentier's avatar Justin Carpentier Committed by GitHub
Browse files

Merge pull request #172 from jcarpent/devel

Authorize the sharing of memory between Eigen and Numpy
parents 952227d8 155d92ec
Subproject commit 93ec987ecdc016039c8731e203943e5a83eb96d5
Subproject commit 321eb1ccf1d94570eb564f3659b13ef3ef82239e
......@@ -40,12 +40,19 @@ namespace eigenpy
typedef typename SimilarMatrixType::Scalar Scalar;
enum { NPY_ARRAY_MEMORY_CONTIGUOUS = SimilarMatrixType::IsRowMajor ? NPY_ARRAY_CARRAY : NPY_ARRAY_FARRAY };
PyArrayObject * pyArray = (PyArrayObject*) call_PyArray_New(nd, shape,
NumpyEquivalentType<Scalar>::type_code,
mat.data(),
NPY_ARRAY_MEMORY_CONTIGUOUS | NPY_ARRAY_ALIGNED);
return pyArray;
if(NumpyType::sharedMemory())
{
PyArrayObject * pyArray = (PyArrayObject*) call_PyArray_New(nd, shape,
NumpyEquivalentType<Scalar>::type_code,
mat.data(),
NPY_ARRAY_MEMORY_CONTIGUOUS | NPY_ARRAY_ALIGNED);
return pyArray;
}
else
{
return NumpyAllocator<MatType>::allocate(mat.derived(),nd,shape);
}
}
};
......@@ -68,12 +75,19 @@ namespace eigenpy
typedef typename SimilarMatrixType::Scalar Scalar;
enum { NPY_ARRAY_MEMORY_CONTIGUOUS_RO = SimilarMatrixType::IsRowMajor ? NPY_ARRAY_CARRAY_RO : NPY_ARRAY_FARRAY_RO };
PyArrayObject * pyArray = (PyArrayObject*) call_PyArray_New(nd, shape,
NumpyEquivalentType<Scalar>::type_code,
const_cast<SimilarMatrixType &>(mat.derived()).data(),
NPY_ARRAY_MEMORY_CONTIGUOUS_RO | NPY_ARRAY_ALIGNED);
return pyArray;
if(NumpyType::sharedMemory())
{
PyArrayObject * pyArray = (PyArrayObject*) call_PyArray_New(nd, shape,
NumpyEquivalentType<Scalar>::type_code,
const_cast<SimilarMatrixType &>(mat.derived()).data(),
NPY_ARRAY_MEMORY_CONTIGUOUS_RO | NPY_ARRAY_ALIGNED);
return pyArray;
}
else
{
return NumpyAllocator<MatType>::allocate(mat.derived(),nd,shape);
}
}
};
......
......@@ -96,6 +96,16 @@ namespace eigenpy
switchToNumpyArray();
}
static void sharedMemory(const bool value)
{
getInstance().shared_memory = value;
}
static bool sharedMemory()
{
return getInstance().shared_memory;
}
static void switchToNumpyArray()
{
getInstance().CurrentNumpyType = getInstance().NumpyArrayObject;
......@@ -162,6 +172,8 @@ namespace eigenpy
CurrentNumpyType = NumpyArrayObject; // default conversion
np_type = ARRAY_TYPE;
shared_memory = true;
}
bp::object CurrentNumpyType;
......@@ -173,6 +185,8 @@ namespace eigenpy
bp::object NumpyArrayObject; PyTypeObject * NumpyArrayType;
NP_TYPE np_type;
bool shared_memory;
};
}
......
<?xml version="1.0"?>
<package format="2">
<name>eigenpy</name>
<version>2.1.2</version>
<version>2.2.0</version>
<description>Bindings between Numpy and Eigen using Boost.Python</description>
<maintainer email="justin.carpentier@inria.fr">Justin Carpentier</maintainer>
<maintainer email="wolfgang.merkt@ed.ac.uk">Wolfgang Merkt</maintainer>
......
......@@ -45,6 +45,17 @@ namespace eigenpy
bp::def("switchToNumpyMatrix",&NumpyType::switchToNumpyMatrix,
"Set the conversion from Eigen::Matrix to numpy.matrix.");
bp::def("sharedMemory",
(void (*)(const bool))NumpyType::sharedMemory,
bp::arg("value"),
"Share the memory when converting from Eigen to Numpy.");
bp::def("sharedMemory",
(bool (*)())NumpyType::sharedMemory,
"Status of the shared memory when converting from Eigen to Numpy.\n"
"If True, the memory is shared when converting an Eigen::Matrix to a numpy.array.\n"
"Otherwise, a deep copy of the Eigen::Matrix is performed.");
bp::def("seed",&seed,bp::arg("seed_value"),
"Initialize the pseudo-random number generator with the argument seed_value.");
......
import return_by_ref
from return_by_ref import Matrix, RowMatrix, Vector
import numpy as np
def test(mat):
def test_shared(mat):
m_ref = mat.ref()
m_ref.fill(0)
......@@ -22,6 +23,27 @@ def test(mat):
except:
assert True
def test_not_shared(mat):
m_ref = mat.ref()
m_ref.fill(100.)
m_copy = mat.copy()
assert not np.array_equal(m_ref,m_copy)
m_const_ref = mat.const_ref()
assert np.array_equal(m_const_ref,m_copy)
assert not np.array_equal(m_const_ref,m_ref)
m_ref.fill(10.)
assert not np.array_equal(m_ref,m_copy)
assert not np.array_equal(m_const_ref,m_ref)
try:
m_const_ref.fill(2)
assert True
except:
assert False
rows = 10
cols = 30
......@@ -29,6 +51,13 @@ mat = Matrix(rows,cols)
row_mat = RowMatrix(rows,cols)
vec = Vector(rows,1)
test(mat)
test(row_mat)
test(vec)
test_shared(mat)
test_shared(row_mat)
test_shared(vec)
return_by_ref.sharedMemory(False)
test_not_shared(mat)
test_not_shared(row_mat)
test_not_shared(vec)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment