Verified Commit 0334425f authored by Justin Carpentier's avatar Justin Carpentier
Browse files

core: add the possibility to share or not the memory between Numpy and Eigen

parent 374746d3
...@@ -40,12 +40,19 @@ namespace eigenpy ...@@ -40,12 +40,19 @@ namespace eigenpy
typedef typename SimilarMatrixType::Scalar Scalar; typedef typename SimilarMatrixType::Scalar Scalar;
enum { NPY_ARRAY_MEMORY_CONTIGUOUS = SimilarMatrixType::IsRowMajor ? NPY_ARRAY_CARRAY : NPY_ARRAY_FARRAY }; enum { NPY_ARRAY_MEMORY_CONTIGUOUS = SimilarMatrixType::IsRowMajor ? NPY_ARRAY_CARRAY : NPY_ARRAY_FARRAY };
PyArrayObject * pyArray = (PyArrayObject*) call_PyArray_New(nd, shape, if(NumpyType::sharedMemory())
NumpyEquivalentType<Scalar>::type_code, {
mat.data(), PyArrayObject * pyArray = (PyArrayObject*) call_PyArray_New(nd, shape,
NPY_ARRAY_MEMORY_CONTIGUOUS | NPY_ARRAY_ALIGNED); NumpyEquivalentType<Scalar>::type_code,
mat.data(),
return pyArray; NPY_ARRAY_MEMORY_CONTIGUOUS | NPY_ARRAY_ALIGNED);
return pyArray;
}
else
{
return NumpyAllocator<MatType>::allocate(mat.derived(),nd,shape);
}
} }
}; };
...@@ -68,12 +75,19 @@ namespace eigenpy ...@@ -68,12 +75,19 @@ namespace eigenpy
typedef typename SimilarMatrixType::Scalar Scalar; typedef typename SimilarMatrixType::Scalar Scalar;
enum { NPY_ARRAY_MEMORY_CONTIGUOUS_RO = SimilarMatrixType::IsRowMajor ? NPY_ARRAY_CARRAY_RO : NPY_ARRAY_FARRAY_RO }; enum { NPY_ARRAY_MEMORY_CONTIGUOUS_RO = SimilarMatrixType::IsRowMajor ? NPY_ARRAY_CARRAY_RO : NPY_ARRAY_FARRAY_RO };
PyArrayObject * pyArray = (PyArrayObject*) call_PyArray_New(nd, shape, if(NumpyType::sharedMemory())
NumpyEquivalentType<Scalar>::type_code, {
const_cast<SimilarMatrixType &>(mat.derived()).data(), PyArrayObject * pyArray = (PyArrayObject*) call_PyArray_New(nd, shape,
NPY_ARRAY_MEMORY_CONTIGUOUS_RO | NPY_ARRAY_ALIGNED); NumpyEquivalentType<Scalar>::type_code,
const_cast<SimilarMatrixType &>(mat.derived()).data(),
return pyArray; 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 ...@@ -96,6 +96,16 @@ namespace eigenpy
switchToNumpyArray(); switchToNumpyArray();
} }
static void sharedMemory(const bool value)
{
getInstance().shared_memory = value;
}
static bool sharedMemory()
{
return getInstance().shared_memory;
}
static void switchToNumpyArray() static void switchToNumpyArray()
{ {
getInstance().CurrentNumpyType = getInstance().NumpyArrayObject; getInstance().CurrentNumpyType = getInstance().NumpyArrayObject;
...@@ -162,6 +172,8 @@ namespace eigenpy ...@@ -162,6 +172,8 @@ namespace eigenpy
CurrentNumpyType = NumpyArrayObject; // default conversion CurrentNumpyType = NumpyArrayObject; // default conversion
np_type = ARRAY_TYPE; np_type = ARRAY_TYPE;
shared_memory = true;
} }
bp::object CurrentNumpyType; bp::object CurrentNumpyType;
...@@ -173,6 +185,8 @@ namespace eigenpy ...@@ -173,6 +185,8 @@ namespace eigenpy
bp::object NumpyArrayObject; PyTypeObject * NumpyArrayType; bp::object NumpyArrayObject; PyTypeObject * NumpyArrayType;
NP_TYPE np_type; NP_TYPE np_type;
bool shared_memory;
}; };
} }
......
...@@ -45,6 +45,17 @@ namespace eigenpy ...@@ -45,6 +45,17 @@ namespace eigenpy
bp::def("switchToNumpyMatrix",&NumpyType::switchToNumpyMatrix, bp::def("switchToNumpyMatrix",&NumpyType::switchToNumpyMatrix,
"Set the conversion from Eigen::Matrix to numpy.matrix."); "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 Eigen::Matrix to numpy.array.");
bp::def("sharedMemory",
(bool (*)())NumpyType::sharedMemory,
"Status of the shared memory when converting Eigen::Matrix to numpy.array.\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"), bp::def("seed",&seed,bp::arg("seed_value"),
"Initialize the pseudo-random number generator with the argument seed_value."); "Initialize the pseudo-random number generator with the argument seed_value.");
......
Supports Markdown
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