OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
PyPolynomialCoefficient.cpp
Go to the documentation of this file.
1#include <Python.h>
2#include <structmember.h>
3
5#include "PyOpal/Globals.h"
7
9/*
10 PolynomialCoefficient(std::vector<int> inVariablesByVector,
11 int outVariable,
12 double coefficient)
13*/
14int _init(PyObject* self, PyObject *args, PyObject *kwds) {
15 PyCoefficient* py_coeff = reinterpret_cast<PyCoefficient*>(self);
16 // failed to cast or self was not initialised - something horrible happened
17 if (py_coeff == NULL) {
18 PyErr_SetString(PyExc_TypeError,
19 "Failed to resolve self as PolynomialCoefficient in __init__");
20 return -1;
21 }
22 // legal python to call initialised_object.__init__() to reinitialise, so
23 // handle this case
24 if (py_coeff->coeff != NULL) {
25 delete py_coeff->coeff;
26 py_coeff->coeff = NULL;
27 }
28 // read in arguments
29
30 PyObject* py_index;
31 int value_axis;
32 double coefficient;
33 static char *kwlist[] = {const_cast<char*>("index_by_vector"),
34 const_cast<char*>("output_axis"),
35 const_cast<char*>("coefficient_value"),
36 NULL};
37 if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oid", kwlist,
38 &py_index, &value_axis, &coefficient)) {
39 return -1;
40 }
41
42 // convert from list to std::vector<int>
43 // first check validity of coefficients
44 if (!PyList_Check(py_index)) {
45 PyErr_SetString(PyExc_TypeError,
46 "Failed to resolve index as a list");
47 return -1;
48 }
49 size_t list_size = PyList_Size(py_index); // nb: size 0 is legal
50 std::vector<int> index(list_size);
51 // now loop over the rows
52 for (size_t i = 0; i < list_size; ++i) {
53 PyObject* py_value = PyList_GetItem(py_index, i);
54 index[i] = int(PyLong_AsLong(py_value));
55 if (PyErr_Occurred() != NULL) { // not an int
56 return -1;
57 }
58 }
59 // now initialise the internal coeff
60 try {
61 py_coeff->coeff = new interpolation::PolynomialCoefficient(index, value_axis, coefficient);
62 } catch (std::exception& exc) {
63 PyErr_SetString(PyExc_RuntimeError, (&exc)->what());
64 return -1;
65 }
66 return 0;
67}
68
69PyObject *_alloc(PyTypeObject *type, Py_ssize_t nitems) {
70 void* void_coeff = malloc(sizeof(PyCoefficient));
71 PyCoefficient* coeff = reinterpret_cast<PyCoefficient*>(void_coeff);
72 coeff->coeff = NULL;
73 Py_REFCNT(coeff) = 1;
74 Py_TYPE(coeff) = type;
75 return reinterpret_cast<PyObject*>(coeff);
76}
77
78PyObject *_new(PyTypeObject *type, Py_ssize_t nitems) {
79 return _alloc(type, nitems);
80}
81
82void _free(PyCoefficient * self) {
83 if (self != NULL) {
84 if (self->coeff != NULL)
85 delete self->coeff;
86 free(self);
87 }
88}
89
90void _dealloc(PyCoefficient * self) {
91 _free(self);
92}
93
94static PyMemberDef _members[] = {
95{NULL}
96};
97
98static PyMethodDef _methods[] = {
99{NULL}
100};
101
102std::string class_docstring =
103std::string("PolynomialCoefficient docstring\n");
104
105static PyTypeObject PyCoefficientType = {
106 PyObject_HEAD_INIT(NULL)
107 "polynomial_coefficient.PolynomialCoefficient", /*tp_name*/
108 sizeof(PyCoefficient), /*tp_basicsize*/
109 0, /*tp_itemsize*/
110 (destructor)_dealloc, /*tp_dealloc*/
111 0, /*tp_print*/
112 0, /*tp_getattr*/
113 0, /*tp_setattr*/
114 0, /*tp_compare*/
115 0, /*tp_repr*/
116 0, /*tp_as_number*/
117 0, /*tp_as_sequence*/
118 0, /*tp_as_mapping*/
119 0, /*tp_hash */
120 0, /*tp_call*/
121 0, /*tp_str*/
122 0, /*tp_getattro*/
123 0, /*tp_setattro*/
124 0, /*tp_as_buffer*/
125 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
126 class_docstring.c_str(), /* tp_doc */
127 0, /* tp_traverse */
128 0, /* tp_clear */
129 0, /* tp_richcompare */
130 0, /* tp_weaklistoffset */
131 0, /* tp_iter */
132 0, /* tp_iternext */
133 _methods, /* tp_methods */
134 _members, /* tp_members */
135 0, /* tp_getset */
136 0, /* tp_base */
137 0, /* tp_dict */
138 0, /* tp_descr_get */
139 0, /* tp_descr_set */
140 0, /* tp_dictoffset */
141 (initproc)_init, /* tp_init */
142 (allocfunc)_alloc, /* tp_alloc, called by new */
143 0, // (newfunc)_new, /* tp_new */
144 (freefunc)_free, /* tp_free, called by dealloc */
145};
146
147} // namespace PyPolynomialCoefficient
148
149const char* module_docstring =
150 "polynomial_coefficient module contains the PolynomialCoefficient class";
151
152static struct PyModuleDef polynomial_coefficient_def = {
153 PyModuleDef_HEAD_INIT,
154 "polynomial_coefficient", /* m_name */
155 module_docstring, /* m_doc */
156 -1, /* m_size */
157 NULL, /* m_methods */
158 NULL, /* m_reload */
159 NULL, /* m_traverse */
160 NULL, /* m_clear */
161 NULL, /* m_free */
162};
163
164PyMODINIT_FUNC PyInit_polynomial_coefficient(void) {
166 PyPolynomialCoefficient::PyCoefficientType.tp_new = PyType_GenericNew;
167 if (PyType_Ready(&PyPolynomialCoefficient::PyCoefficientType) < 0)
168 return NULL;
169
170 PyObject* module = PyModule_Create(&polynomial_coefficient_def);
171 if (module == NULL)
172 return NULL;
173
174 PyTypeObject* polynomial_coeff_type =
175 &PyPolynomialCoefficient::PyCoefficientType;
176 Py_INCREF(polynomial_coeff_type);
177 PyModule_AddObject(module, "PolynomialCoefficient",
178 reinterpret_cast<PyObject*>(polynomial_coeff_type));
179 return module;
180}
181
182
void Initialise()
Definition: Globals.cpp:78
int _init(PyObject *self, PyObject *args, PyObject *kwds)
PyObject * _alloc(PyTypeObject *type, Py_ssize_t nitems)
void _free(PyCoefficient *self)
void _dealloc(PyCoefficient *self)
PyObject * _new(PyTypeObject *type, Py_ssize_t nitems)
boost::function< boost::tuple< double, bool >(arguments_t)> type
Definition: function.hpp:21
PolynomialCoefficient represents a coefficient in a multi-dimensional polynomial.
PyMODINIT_FUNC PyInit_polynomial_coefficient(void)
const char * module_docstring