Commit 2e964683 authored by Dave Moxey's avatar Dave Moxey
Browse files

Add initial wrapper for NekMatrix

parent 8964b331
......@@ -30,7 +30,7 @@ IF (NOT BOOST_NUMPY_LIB)
INCLUDE_DIRECTORIES(SYSTEM ${TPDIST}/include)
# Add this directory to the RPATH.
#SET(CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_RPATH} ${TPDIST}/lib64)
SET(CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_RPATH} ${TPDIST}/lib64)
ELSE()
ADD_CUSTOM_TARGET(boost-numpy ALL)
ADD_DEFINITIONS(-DBOOST_HAS_NUMPY)
......
......@@ -5,6 +5,7 @@ SET(LIBUTILITIES_SOURCES
BasicUtils/ShapeType.cpp
Foundations/Basis.cpp
Foundations/Points.cpp
LinearAlgebra/NekMatrix.cpp
)
ADD_NEKPY_LIBRARY(LibUtilities LIBUTILITIES_SOURCES)
......@@ -8,6 +8,9 @@ void export_ShapeType();
template<typename T>
void export_SharedArray();
template<typename T>
void export_NekMatrix();
BOOST_PYTHON_FUNCTION_OVERLOADS(Points_GetD_overloads, GetD, 1, 1);
BOOST_PYTHON_MODULE(_LibUtilities)
......@@ -19,4 +22,5 @@ BOOST_PYTHON_MODULE(_LibUtilities)
export_SessionReader();
export_ShapeType();
export_SharedArray<double>();
export_NekMatrix<double>();
}
#include <LibUtilities/LinearAlgebra/NekMatrix.hpp>
#include <LibUtilities/LinearAlgebra/MatrixStorageType.h>
#include <NekPyConfig.hpp>
using namespace Nektar;
using namespace Nektar::LibUtilities;
#if PY_MAJOR_VERSION == 2
template<typename T, typename F>
void NekMatrixCapsuleDestructor(void *ptr)
{
std::shared_ptr<NekMatrix<T, F>> *mat =
(std::shared_ptr<NekMatrix<T, F>> *)ptr;
delete mat;
}
#else
template<typename T, typename F>
void NekMatrixCapsuleDestructor(PyObject *ptr)
{
std::shared_ptr<NekMatrix<T, F>> *mat =
(std::shared_ptr<NekMatrix<T, F>> *)PyCapsule_GetPointer(ptr, 0);
delete mat;
}
#endif
template<typename T>
struct NekMatrixToPython
{
static PyObject *convert(
std::shared_ptr<NekMatrix<T, StandardMatrixTag>> const &mat)
{
// Create a Python capsule to hold a pointer that contains a lightweight
// copy of arr. That way we guarantee Python will still have access to
// the memory allocated inside arr even if arr is deallocated in C++.
#if PY_MAJOR_VERSION == 2
py::object capsule(
py::handle<>(PyCObject_FromVoidPtr(
new std::shared_ptr<NekMatrix<T, StandardMatrixTag>>(mat),
NekMatrixCapsuleDestructor<T, StandardMatrixTag>)));
#else
py::object capsule(
py::handle<>(PyCapsule_New(
(void *)new std::shared_ptr<NekMatrix<T, StandardMatrixTag>>(mat),
(PyCapsule_Destructor)&CapsuleDestructor<T, StandardMatrixTag>)));
#endif
int nRows = mat->GetRows(), nCols = mat->GetColumns();
//StorageType storage = mat->GetStorageType();
// if (storage != eFULL)
// {
// // Only support full storage types at the moment - ignore symmetric,
// // banded, etc.
// }
return py::incref(
np::from_data(
mat->GetRawPtr(), np::dtype::get_builtin<T>(),
py::make_tuple(nRows, nCols),
py::make_tuple(nRows * sizeof(T), sizeof(T)),
capsule).ptr());
}
};
template<typename T>
void export_NekMatrix()
{
py::to_python_converter<std::shared_ptr<NekMatrix<T, StandardMatrixTag>>,
NekMatrixToPython<T>>();
}
template void export_NekMatrix<double>();
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