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 
39 using namespace Nektar;
40 using namespace Nektar::LibUtilities;
41 
42 #if PY_MAJOR_VERSION == 2
43 template<typename T, typename F>
44 void NekMatrixCapsuleDestructor(void *ptr)
45 {
46  std::shared_ptr<NekMatrix<T, F>> *mat =
47  (std::shared_ptr<NekMatrix<T, F>> *)ptr;
48  delete mat;
49 }
50 #else
51 template<typename T, typename F>
52 void NekMatrixCapsuleDestructor(PyObject *ptr)
53 {
54  std::shared_ptr<NekMatrix<T, F>> *mat =
55  (std::shared_ptr<NekMatrix<T, F>> *)PyCapsule_GetPointer(ptr, 0);
56  delete mat;
57 }
58 #endif
59 
60 template<typename T>
62 {
63  static PyObject *convert(
64  std::shared_ptr<NekMatrix<T, StandardMatrixTag>> const &mat)
65  {
66  // Create a Python capsule to hold a pointer that contains a lightweight
67  // copy of arr. That way we guarantee Python will still have access to
68  // the memory allocated inside arr even if arr is deallocated in C++.
69 #if PY_MAJOR_VERSION == 2
70  py::object capsule(
71  py::handle<>(PyCObject_FromVoidPtr(
72  new std::shared_ptr<NekMatrix<T, StandardMatrixTag>>(mat),
73  NekMatrixCapsuleDestructor<T, StandardMatrixTag>)));
74 #else
75  py::object capsule(
76  py::handle<>(PyCapsule_New(
77  (void *)new std::shared_ptr<NekMatrix<T, StandardMatrixTag>>(mat), NULL,
78  (PyCapsule_Destructor)&NekMatrixCapsuleDestructor<T, StandardMatrixTag>)));
79 #endif
80 
81  int nRows = mat->GetRows(), nCols = mat->GetColumns();
82  MatrixStorage storage = mat->GetStorageType();
83 
84  ASSERTL0(storage == eFULL, "Only full storage matrices are currently supported.");
85 
86  return py::incref(
87  np::from_data(
88  mat->GetRawPtr(), np::dtype::get_builtin<T>(),
89  py::make_tuple(nRows, nCols),
90  py::make_tuple(sizeof(T), nRows * sizeof(T)),
91  capsule).ptr());
92  }
93 };
94 
95 template<typename T>
97 {
98  py::to_python_converter<std::shared_ptr<NekMatrix<T, StandardMatrixTag>>,
100 }
101 
102 template void export_NekMatrix<double>();
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
void NekMatrixCapsuleDestructor(PyObject *ptr)
Definition: NekMatrix.cpp:52
void export_NekMatrix()
Definition: NekMatrix.cpp:96
template void export_NekMatrix< double >()
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:1
static PyObject * convert(std::shared_ptr< NekMatrix< T, StandardMatrixTag >> const &mat)
Definition: NekMatrix.cpp:63