Nektar++
Static Public Member Functions | List of all members
NekMatrixToPython< T > Struct Template Reference

Static Public Member Functions

static PyObject * convert (std::shared_ptr< NekMatrix< T, StandardMatrixTag >> const &mat)
 

Detailed Description

template<typename T>
struct NekMatrixToPython< T >

Definition at line 58 of file NekMatrix.cpp.

Member Function Documentation

◆ convert()

template<typename T >
static PyObject* NekMatrixToPython< T >::convert ( std::shared_ptr< NekMatrix< T, StandardMatrixTag >> const &  mat)
inlinestatic

Definition at line 60 of file NekMatrix.cpp.

62  {
63  // Create a Python capsule to hold a pointer that contains a lightweight
64  // copy of arr. That way we guarantee Python will still have access to
65  // the memory allocated inside arr even if arr is deallocated in C++.
66 #if PY_MAJOR_VERSION == 2
67  py::object capsule(py::handle<>(PyCObject_FromVoidPtr(
68  new std::shared_ptr<NekMatrix<T, StandardMatrixTag>>(mat),
69  NekMatrixCapsuleDestructor<T, StandardMatrixTag>)));
70 #else
71  py::object capsule(py::handle<>(PyCapsule_New(
72  (void *)new std::shared_ptr<NekMatrix<T, StandardMatrixTag>>(mat),
73  NULL,
74  (PyCapsule_Destructor)&NekMatrixCapsuleDestructor<
75  T, StandardMatrixTag>)));
76 #endif
77 
78  int nRows = mat->GetRows(), nCols = mat->GetColumns();
79  MatrixStorage storage = mat->GetStorageType();
80 
81  ASSERTL0(storage == eFULL,
82  "Only full storage matrices are currently supported.");
83 
84  return py::incref(
85  np::from_data(mat->GetRawPtr(), np::dtype::get_builtin<T>(),
86  py::make_tuple(nRows, nCols),
87  py::make_tuple(sizeof(T), nRows * sizeof(T)), capsule)
88  .ptr());
89  }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:215
void NekMatrixCapsuleDestructor(PyObject *ptr)
Definition: NekMatrix.cpp:50

References ASSERTL0, Nektar::eFULL, and NekMatrixCapsuleDestructor().