提交 8f4d7b1f authored 作者: Brendan Murphy's avatar Brendan Murphy 提交者: Ricardo Vieira

Updated lazylinker C code

Some macros were removed from npy_3k_compat.h. Following numpy, I updated the affected functions to the Python 3 names, and removed support for Python 2. Also updated lazylinker_c version to indicate substantial changes to the C code.
上级 ca089790
...@@ -5,9 +5,6 @@ ...@@ -5,9 +5,6 @@
#if PY_VERSION_HEX >= 0x03000000 #if PY_VERSION_HEX >= 0x03000000
#include "numpy/npy_3kcompat.h" #include "numpy/npy_3kcompat.h"
#define PyCObject_AsVoidPtr NpyCapsule_AsVoidPtr
#define PyCObject_GetDesc NpyCapsule_GetDesc
#define PyCObject_Check NpyCapsule_Check
#endif #endif
#ifndef Py_TYPE #ifndef Py_TYPE
...@@ -323,9 +320,9 @@ static int CLazyLinker_init(CLazyLinker *self, PyObject *args, PyObject *kwds) { ...@@ -323,9 +320,9 @@ static int CLazyLinker_init(CLazyLinker *self, PyObject *args, PyObject *kwds) {
if (PyObject_HasAttrString(thunk, "cthunk")) { if (PyObject_HasAttrString(thunk, "cthunk")) {
PyObject *cthunk = PyObject_GetAttrString(thunk, "cthunk"); PyObject *cthunk = PyObject_GetAttrString(thunk, "cthunk");
// new reference // new reference
assert(cthunk && PyCObject_Check(cthunk)); assert(cthunk && NpyCapsule_Check(cthunk));
self->thunk_cptr_fn[i] = PyCObject_AsVoidPtr(cthunk); self->thunk_cptr_fn[i] = NpyCapsule_AsVoidPtr(cthunk);
self->thunk_cptr_data[i] = PyCObject_GetDesc(cthunk); self->thunk_cptr_data[i] = NpyCapsule_GetDesc(cthunk);
Py_DECREF(cthunk); Py_DECREF(cthunk);
// cthunk is kept alive by membership in self->thunks // cthunk is kept alive by membership in self->thunks
} }
...@@ -487,8 +484,8 @@ static PyObject *pycall(CLazyLinker *self, Py_ssize_t node_idx, int verbose) { ...@@ -487,8 +484,8 @@ static PyObject *pycall(CLazyLinker *self, Py_ssize_t node_idx, int verbose) {
PyList_SetItem(self->call_times, node_idx, PyList_SetItem(self->call_times, node_idx,
PyFloat_FromDouble(t1 - t0 + ti)); PyFloat_FromDouble(t1 - t0 + ti));
PyObject *count = PyList_GetItem(self->call_counts, node_idx); PyObject *count = PyList_GetItem(self->call_counts, node_idx);
long icount = PyInt_AsLong(count); long icount = PyLong_AsLong(count);
PyList_SetItem(self->call_counts, node_idx, PyInt_FromLong(icount + 1)); PyList_SetItem(self->call_counts, node_idx, PyLong_FromLong(icount + 1));
} }
} else { } else {
if (verbose) { if (verbose) {
...@@ -512,8 +509,8 @@ static int c_call(CLazyLinker *self, Py_ssize_t node_idx, int verbose) { ...@@ -512,8 +509,8 @@ static int c_call(CLazyLinker *self, Py_ssize_t node_idx, int verbose) {
PyList_SetItem(self->call_times, node_idx, PyList_SetItem(self->call_times, node_idx,
PyFloat_FromDouble(t1 - t0 + ti)); PyFloat_FromDouble(t1 - t0 + ti));
PyObject *count = PyList_GetItem(self->call_counts, node_idx); PyObject *count = PyList_GetItem(self->call_counts, node_idx);
long icount = PyInt_AsLong(count); long icount = PyLong_AsLong(count);
PyList_SetItem(self->call_counts, node_idx, PyInt_FromLong(icount + 1)); PyList_SetItem(self->call_counts, node_idx, PyLong_FromLong(icount + 1));
} else { } else {
err = fn(self->thunk_cptr_data[node_idx]); err = fn(self->thunk_cptr_data[node_idx]);
} }
...@@ -774,20 +771,20 @@ static PyObject *CLazyLinker_call(PyObject *_self, PyObject *args, ...@@ -774,20 +771,20 @@ static PyObject *CLazyLinker_call(PyObject *_self, PyObject *args,
output_subset = (char *)calloc(self->n_output_vars, sizeof(char)); output_subset = (char *)calloc(self->n_output_vars, sizeof(char));
for (int it = 0; it < output_subset_size; ++it) { for (int it = 0; it < output_subset_size; ++it) {
PyObject *elem = PyList_GetItem(output_subset_ptr, it); PyObject *elem = PyList_GetItem(output_subset_ptr, it);
if (!PyInt_Check(elem)) { if (!PyLong_Check(elem)) {
err = 1; err = 1;
PyErr_SetString(PyExc_RuntimeError, PyErr_SetString(PyExc_RuntimeError,
"Some elements of output_subset list are not int"); "Some elements of output_subset list are not int");
} }
output_subset[PyInt_AsLong(elem)] = 1; output_subset[PyLong_AsLong(elem)] = 1;
} }
} }
} }
self->position_of_error = -1; self->position_of_error = -1;
// create constants used to fill the var_compute_cells // create constants used to fill the var_compute_cells
PyObject *one = PyInt_FromLong(1); PyObject *one = PyLong_FromLong(1);
PyObject *zero = PyInt_FromLong(0); PyObject *zero = PyLong_FromLong(0);
// pre-allocate our return value // pre-allocate our return value
Py_INCREF(Py_None); Py_INCREF(Py_None);
...@@ -942,11 +939,8 @@ static PyMemberDef CLazyLinker_members[] = { ...@@ -942,11 +939,8 @@ static PyMemberDef CLazyLinker_members[] = {
}; };
static PyTypeObject lazylinker_ext_CLazyLinkerType = { static PyTypeObject lazylinker_ext_CLazyLinkerType = {
#if defined(NPY_PY3K)
PyVarObject_HEAD_INIT(NULL, 0) PyVarObject_HEAD_INIT(NULL, 0)
#else
PyObject_HEAD_INIT(NULL) 0, /*ob_size*/
#endif
"lazylinker_ext.CLazyLinker", /*tp_name*/ "lazylinker_ext.CLazyLinker", /*tp_name*/
sizeof(CLazyLinker), /*tp_basicsize*/ sizeof(CLazyLinker), /*tp_basicsize*/
0, /*tp_itemsize*/ 0, /*tp_itemsize*/
...@@ -987,7 +981,7 @@ static PyTypeObject lazylinker_ext_CLazyLinkerType = { ...@@ -987,7 +981,7 @@ static PyTypeObject lazylinker_ext_CLazyLinkerType = {
}; };
static PyObject *get_version(PyObject *dummy, PyObject *args) { static PyObject *get_version(PyObject *dummy, PyObject *args) {
PyObject *result = PyFloat_FromDouble(0.212); PyObject *result = PyFloat_FromDouble(0.3);
return result; return result;
} }
...@@ -996,7 +990,7 @@ static PyMethodDef lazylinker_ext_methods[] = { ...@@ -996,7 +990,7 @@ static PyMethodDef lazylinker_ext_methods[] = {
{NULL, NULL, 0, NULL} /* Sentinel */ {NULL, NULL, 0, NULL} /* Sentinel */
}; };
#if defined(NPY_PY3K)
static struct PyModuleDef moduledef = {PyModuleDef_HEAD_INIT, static struct PyModuleDef moduledef = {PyModuleDef_HEAD_INIT,
"lazylinker_ext", "lazylinker_ext",
NULL, NULL,
...@@ -1006,28 +1000,19 @@ static struct PyModuleDef moduledef = {PyModuleDef_HEAD_INIT, ...@@ -1006,28 +1000,19 @@ static struct PyModuleDef moduledef = {PyModuleDef_HEAD_INIT,
NULL, NULL,
NULL, NULL,
NULL}; NULL};
#endif
#if defined(NPY_PY3K)
#define RETVAL m
PyMODINIT_FUNC PyInit_lazylinker_ext(void) { PyMODINIT_FUNC PyInit_lazylinker_ext(void) {
#else
#define RETVAL
PyMODINIT_FUNC initlazylinker_ext(void) {
#endif
PyObject *m; PyObject *m;
lazylinker_ext_CLazyLinkerType.tp_new = PyType_GenericNew; lazylinker_ext_CLazyLinkerType.tp_new = PyType_GenericNew;
if (PyType_Ready(&lazylinker_ext_CLazyLinkerType) < 0) if (PyType_Ready(&lazylinker_ext_CLazyLinkerType) < 0)
return RETVAL; return NULL;
#if defined(NPY_PY3K)
m = PyModule_Create(&moduledef); m = PyModule_Create(&moduledef);
#else
m = Py_InitModule3("lazylinker_ext", lazylinker_ext_methods,
"Example module that creates an extension type.");
#endif
Py_INCREF(&lazylinker_ext_CLazyLinkerType); Py_INCREF(&lazylinker_ext_CLazyLinkerType);
PyModule_AddObject(m, "CLazyLinker", PyModule_AddObject(m, "CLazyLinker",
(PyObject *)&lazylinker_ext_CLazyLinkerType); (PyObject *)&lazylinker_ext_CLazyLinkerType);
return RETVAL; return m;
} }
...@@ -18,14 +18,8 @@ ...@@ -18,14 +18,8 @@
#define PYTENSOR_EXTERN #define PYTENSOR_EXTERN
#endif #endif
#if PY_MAJOR_VERSION < 3
#define PYTENSOR_RTYPE void
#else
#define PYTENSOR_RTYPE PyObject *
#endif
/* We need to redefine PyMODINIT_FUNC to add MOD_PUBLIC in the middle */ /* We need to redefine PyMODINIT_FUNC to add MOD_PUBLIC in the middle */
#undef PyMODINIT_FUNC #undef PyMODINIT_FUNC
#define PyMODINIT_FUNC PYTENSOR_EXTERN MOD_PUBLIC PYTENSOR_RTYPE #define PyMODINIT_FUNC PYTENSOR_EXTERN MOD_PUBLIC PyObject *
#endif #endif
...@@ -14,7 +14,7 @@ from pytensor.link.c.cmodule import GCC_compiler ...@@ -14,7 +14,7 @@ from pytensor.link.c.cmodule import GCC_compiler
_logger = logging.getLogger(__file__) _logger = logging.getLogger(__file__)
force_compile = False force_compile = False
version = 0.212 # must match constant returned in function get_version() version = 0.3 # must match constant returned in function get_version()
lazylinker_ext: ModuleType | None = None lazylinker_ext: ModuleType | None = None
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论