Nektar++
NekMatrix.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: NekMatrix.cpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: Python wrapper for NekMatrix.
32//
33///////////////////////////////////////////////////////////////////////////////
34
38
39using namespace Nektar;
40using namespace Nektar::LibUtilities;
41
42#if PY_MAJOR_VERSION == 2
43template <typename T, typename F> void NekMatrixCapsuleDestructor(void *ptr)
44{
45 std::shared_ptr<NekMatrix<T, F>> *mat =
46 (std::shared_ptr<NekMatrix<T, F>> *)ptr;
47 delete mat;
48}
49#else
50template <typename T, typename F> void NekMatrixCapsuleDestructor(PyObject *ptr)
51{
52 std::shared_ptr<NekMatrix<T, F>> *mat =
53 (std::shared_ptr<NekMatrix<T, F>> *)PyCapsule_GetPointer(ptr, nullptr);
54 delete mat;
55}
56#endif
57
58template <typename T> struct NekMatrixToPython
59{
60 static PyObject *convert(
61 std::shared_ptr<NekMatrix<T, StandardMatrixTag>> const &mat)
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 nullptr,
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 }
90};
91
92template <typename T> void export_NekMatrix()
93{
94 py::to_python_converter<std::shared_ptr<NekMatrix<T, StandardMatrixTag>>,
96}
97
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208
void NekMatrixCapsuleDestructor(PyObject *ptr)
Definition: NekMatrix.cpp:50
void export_NekMatrix()
Definition: NekMatrix.cpp:92
template void export_NekMatrix< double >()
static PyObject * convert(std::shared_ptr< NekMatrix< T, StandardMatrixTag > > const &mat)
Definition: NekMatrix.cpp:60