C-API implementation to use cython functions in pythran extensions
Created originally on Bitbucket by avmo (Ashwin Vishnu)
Turns out, cython only produces the __pyx_capi__
dictionary with api
modifier only when simple functions are defined, for eg.
cdef api int foo(int f):
return f
Even if I use the api
modifier in class methods, no C API is exported. Then one would need to write wrapper functions which take an instance of the class as an argument. Like this (a modified version of the example in http://docs.cython.org/en/latest/src/userguide/wrapping_CPlusPlus.html)
cdef api class PyRectangle [object cPyRectangle, type cPyRectangle_t]:
cdef Rectangle c_rect # hold a C++ instance which we're wrapping
def __cinit__(self, int x0, int y0, int x1, int y1):
self.c_rect = Rectangle(x0, y0, x1, y1)
cpdef api int get_area(self):
return self.c_rect.getArea()
def get_size(self):
cdef int width, height
self.c_rect.getSize(&width, &height)
return width, height
def move(self, dx, dy):
self.c_rect.move(dx, dy)
cdef api int getArea(Rectangle c_rect):
return c_rect.getArea()
cdef api int get_area_cy(PyRectangle py_rect):
return py_rect.get_area()
The problem with this method is that Pythran won't accept a class as a function parameter.
- Show closed items
Activity
-
Newest first Oldest first
-
Show all activity Show comments only Show history only
- Author Maintainer
Created originally on Bitbucket by avmo (Ashwin Vishnu)
I have managed to sort this out by manually creating
PyCapsule
and thus a__pyx_capi__
attribute forfft3d
classes. Everything compiles, butPythranCython throws aTypeError during runtime.====================================================================== ERROR: test_operator3d_fft3d.with_fftw3d_even (fluidfft.fft3d.tests3d.Tests3D) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/avmo/src/fluidmeta3/fluidfft/fluidfft/fft3d/tests3d.py", line 85, in test op.vgradv_from_v(a, a, a) File "/home/avmo/src/fluidmeta3/fluidfft/fluidfft/fft3d/operators.py", line 208, in vgradv_from_v vy * ifft3d(py_vx_fft) + TypeError: unsupported operand type(s) for *: 'fluidfft.fft3d.with_fftw3d._memoryviewslice' and 'fluidfft.fft3d.with_fftw3d._memoryviewslice' ====================================================================== ERROR: test_operator3d_fft3d.with_fftw3d_odd (fluidfft.fft3d.tests3d.Tests3D) ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/avmo/src/fluidmeta3/fluidfft/fluidfft/fft3d/tests3d.py", line 85, in test op.vgradv_from_v(a, a, a) File "/home/avmo/src/fluidmeta3/fluidfft/fluidfft/fft3d/operators.py", line 208, in vgradv_from_v vy * ifft3d(py_vx_fft) + TypeError: unsupported operand type(s) for *: 'fluidfft.fft3d.with_fftw3d._memoryviewslice' and 'fluidfft.fft3d.with_fftw3d._memoryviewslice'
Due to this change enforcing the type.
cpdef DTYPEf_t[:, :, ::1] ifft(self, DTYPEc_t[:, :, ::1] fieldK): # cpdef ifft(self, DTYPc_t[:, :, ::1] fieldK):
- Author Maintainer
Created originally on Bitbucket by avmo (Ashwin Vishnu)
Now that is reverted, this happens when the tests are run
-------- N0 = 4 ; N1 = 4 ; N2 = 4 Initialization (FFT3DWithFFTW3D) done in 0.018831 s [kthxps:10076] *** Process received signal *** [kthxps:10076] Signal: Segmentation fault (11) [kthxps:10076] Signal code: Address not mapped (1) [kthxps:10076] Failing at address: 0x28 [kthxps:10076] [ 0] /usr/lib/libpthread.so.0(+0x11da0)[0x7f4d215dbda0] [kthxps:10076] [ 1] /home/avmo/src/fluidmeta3/fluidfft/fluidfft/fft3d/with_fftw3d.cpython-36m-x86_64-linux-gnu.so(+0x2c7c1)[0x7f4cfcc3d7c1] [kthxps:10076] [ 2] /home/avmo/src/fluidmeta3/fluidfft/fluidfft/fft3d/dream_pythran.cpython-36m-x86_64-linux-gnu.so(+0xd079)[0x7f4cfce7e079] [kthxps:10076] [ 3] /home/avmo/src/fluidmeta3/fluidfft/fluidfft/fft3d/dream_pythran.cpython-36m-x86_64-linux-gnu.so(+0x12e1f)[0x7f4cfce83e1f] [kthxps:10076] [ 4] /home/avmo/src/fluidmeta3/fluidfft/fluidfft/fft3d/dream_pythran.cpython-36m-x86_64-linux-gnu.so(+0xc26e)[0x7f4cfce7d26e] [kthxps:10076] [ 5] /home/avmo/src/fluidmeta3/fluidfft/fluidfft/fft3d/dream_pythran.cpython-36m-x86_64-linux-gnu.so(+0xc87c)[0x7f4cfce7d87c] [kthxps:10076] [ 6] /home/avmo/src/fluidmeta3/fluidfft/fluidfft/fft3d/dream_pythran.cpython-36m-x86_64-linux-gnu.so(+0xca31)[0x7f4cfce7da31] [kthxps:10076] [ 7] /usr/lib/libpython3.6m.so.1.0(_PyCFunction_FastCallDict+0x154)[0x7f4d20e28bb4] [kthxps:10076] [ 8] /usr/lib/libpython3.6m.so.1.0(+0x12308b)[0x7f4d20dd808b] [kthxps:10076] [ 9] /usr/lib/libpython3.6m.so.1.0(_PyEval_EvalFrameDefault+0x30a)[0x7f4d20dbdb9a] [kthxps:10076] [10] /usr/lib/libpython3.6m.so.1.0(+0x1062b4)[0x7f4d20dbb2b4] [kthxps:10076] [11] /usr/lib/libpython3.6m.so.1.0(+0x122c4f)[0x7f4d20dd7c4f] [kthxps:10076] [12] /usr/lib/libpython3.6m.so.1.0(+0x12314e)[0x7f4d20dd814e] [kthxps:10076] [13] /usr/lib/libpython3.6m.so.1.0(_PyEval_EvalFrameDefault+0x30a)[0x7f4d20dbdb9a] [kthxps:10076] [14] /usr/lib/libpython3.6m.so.1.0(+0x106496)[0x7f4d20dbb496] [kthxps:10076] [15] /usr/lib/libpython3.6m.so.1.0(+0x122c4f)[0x7f4d20dd7c4f] [kthxps:10076] [16] /usr/lib/libpython3.6m.so.1.0(+0x12314e)[0x7f4d20dd814e] [kthxps:10076] [17] /usr/lib/libpython3.6m.so.1.0(_PyEval_EvalFrameDefault+0x30a)[0x7f4d20dbdb9a] [kthxps:10076] [18] /usr/lib/libpython3.6m.so.1.0(+0x1062b4)[0x7f4d20dbb2b4] [kthxps:10076] [19] /usr/lib/libpython3.6m.so.1.0(_PyFunction_FastCallDict+0x41d)[0x7f4d20dd782d] [kthxps:10076] [20] /usr/lib/libpython3.6m.so.1.0(_PyObject_FastCallDict+0x26f)[0x7f4d20e16f5f] [kthxps:10076] [21] /usr/lib/libpython3.6m.so.1.0(_PyObject_Call_Prepend+0x63)[0x7f4d20e17cd3] [kthxps:10076] [22] /usr/lib/libpython3.6m.so.1.0(PyObject_Call+0x4b)[0x7f4d20e17dbb] [kthxps:10076] [23] /usr/lib/libpython3.6m.so.1.0(_PyEval_EvalFrameDefault+0x192f)[0x7f4d20dbf1bf] [kthxps:10076] [24] /usr/lib/libpython3.6m.so.1.0(+0x1062b4)[0x7f4d20dbb2b4] [kthxps:10076] [25] /usr/lib/libpython3.6m.so.1.0(_PyFunction_FastCallDict+0x1bc)[0x7f4d20dd75cc] [kthxps:10076] [26] /usr/lib/libpython3.6m.so.1.0(_PyObject_FastCallDict+0x26f)[0x7f4d20e16f5f] [kthxps:10076] [27] /usr/lib/libpython3.6m.so.1.0(_PyObject_Call_Prepend+0x63)[0x7f4d20e17cd3] [kthxps:10076] [28] /usr/lib/libpython3.6m.so.1.0(PyObject_Call+0x4b)[0x7f4d20e17dbb] [kthxps:10076] [29] /usr/lib/libpython3.6m.so.1.0(+0x16aef7)[0x7f4d20e1fef7] [kthxps:10076] *** End of error message *** make: *** [Makefile:28: tests] Segmentation fault (core dumped)
More work needs to be done to properly initialize PyCapsules manually. However I doubt that this would be successful, as seen from this attempt.
https://github.com/ashwinvis/cython_capi/tree/master/using_cpython_pycapsule
- Author Maintainer
Created originally on Bitbucket by avmo (Ashwin Vishnu)
The C-API feature will be disabled by default for now. To try it out: set
use_capi = True
in fluidfft/util.py, clean all and install again. - Author Maintainer
Created originally on Bitbucket by avmo (Ashwin Vishnu)
C API implementation (see #5)
→ <<cset 154a792f97e9>>
- Author Maintainer
Created originally on Bitbucket by avmo (Ashwin Vishnu)
C API continued (see #5). Less scary segfaults by passing reference to the cython function in PyCapsule
→ <<cset 74f92853c4f1>>
- Author Maintainer
Created originally on Bitbucket by avmo (Ashwin Vishnu)
With cset 74f928, the error messages look slightly better
N0 = 4 ; N1 = 4 ; N2 = 4 Initialization (FFT3DWithFFTW3D) done in 0.021407 s [kthxps:11998] *** Process received signal *** [kthxps:11998] Signal: Segmentation fault (11) [kthxps:11998] Signal code: Invalid permissions (2) [kthxps:11998] Failing at address: 0x7f2bededa5c0 [kthxps:11998] [ 0] /usr/lib/libpthread.so.0(+0x11da0)[0x7f2c12637da0] [kthxps:11998] [ 1] /home/avmo/src/fluidmeta3/fluidfft/fluidfft/fft3d/with_fftw3d.cpython-36m-x86_64-linux-gnu.so(+0x25f5c0)[0x7f2bededa5c0] [kthxps:11998] *** End of error message *** make: *** [Makefile:28: tests] Segmentation fault (core dumped)
- Author Maintainer
Created originally on Bitbucket by avmo (Ashwin Vishnu)
Now the error message goes back to
-------- N0 = 4 ; N1 = 4 ; N2 = 4 Initialization (FFT3DWithFFTW3D) done in 0.015148 s [kthxps:29201] *** Process received signal *** [kthxps:29201] Signal: Segmentation fault (11) [kthxps:29201] Signal code: Address not mapped (1) [kthxps:29201] Failing at address: 0x50 [kthxps:29201] [ 0] /usr/lib/libpthread.so.0(+0x11da0)[0x7f0ce33dbda0] [kthxps:29201] [ 1] /usr/lib/libpython3.6m.so.1.0(PyErr_Restore+0x20)[0x7f0ce2c5ffb0] [kthxps:29201] [ 2] /usr/lib/libpython3.6m.so.1.0(PyErr_FormatV+0x13)[0x7f0ce2c606e3] [kthxps:29201] [ 3] /usr/lib/libpython3.6m.so.1.0(PyErr_Format+0x95)[0x7f0ce2c607b5] [kthxps:29201] [ 4] /usr/lib/libpython3.6m.so.1.0(PyObject_GetBuffer+0x3b)[0x7f0ce2c7c0db] [kthxps:29201] [ 5] /home/avmo/src/fluidmeta3/fluidfft/fluidfft/fft3d/with_fftw3d.cpython-36m-x86_64-linux-gnu.so(+0x1684a)[0x7f0cbea0984a] [kthxps:29201] [ 6] /home/avmo/src/fluidmeta3/fluidfft/fluidfft/fft3d/with_fftw3d.cpython-36m-x86_64-linux-gnu.so(+0x2c780)[0x7f0cbea1f780] [kthxps:29201] [ 7] /home/avmo/src/fluidmeta3/fluidfft/fluidfft/fft3d/dream_pythran.cpython-36m-x86_64-linux-gnu.so(+0xd079)[0x7f0cbec60079] [kthxps:29201] [ 8] /home/avmo/src/fluidmeta3/fluidfft/fluidfft/fft3d/dream_pythran.cpython-36m-x86_64-linux-gnu.so(+0x12e1f)[0x7f0cbec65e1f] [kthxps:29201] [ 9] /home/avmo/src/fluidmeta3/fluidfft/fluidfft/fft3d/dream_pythran.cpython-36m-x86_64-linux-gnu.so(+0xc26e)[0x7f0cbec5f26e] [kthxps:29201] [10] /home/avmo/src/fluidmeta3/fluidfft/fluidfft/fft3d/dream_pythran.cpython-36m-x86_64-linux-gnu.so(+0xc87c)[0x7f0cbec5f87c] [kthxps:29201] [11] /home/avmo/src/fluidmeta3/fluidfft/fluidfft/fft3d/dream_pythran.cpython-36m-x86_64-linux-gnu.so(+0xca31)[0x7f0cbec5fa31] [kthxps:29201] [12] /usr/lib/libpython3.6m.so.1.0(_PyCFunction_FastCallDict+0x154)[0x7f0ce2c28bb4] [kthxps:29201] [13] /usr/lib/libpython3.6m.so.1.0(+0x12308b)[0x7f0ce2bd808b] [kthxps:29201] [14] /usr/lib/libpython3.6m.so.1.0(_PyEval_EvalFrameDefault+0x30a)[0x7f0ce2bbdb9a] [kthxps:29201] [15] /usr/lib/libpython3.6m.so.1.0(+0x1062b4)[0x7f0ce2bbb2b4] [kthxps:29201] [16] /usr/lib/libpython3.6m.so.1.0(+0x122c4f)[0x7f0ce2bd7c4f] [kthxps:29201] [17] /usr/lib/libpython3.6m.so.1.0(+0x12314e)[0x7f0ce2bd814e] [kthxps:29201] [18] /usr/lib/libpython3.6m.so.1.0(_PyEval_EvalFrameDefault+0x30a)[0x7f0ce2bbdb9a] [kthxps:29201] [19] /usr/lib/libpython3.6m.so.1.0(+0x106496)[0x7f0ce2bbb496] [kthxps:29201] [20] /usr/lib/libpython3.6m.so.1.0(+0x122c4f)[0x7f0ce2bd7c4f] [kthxps:29201] [21] /usr/lib/libpython3.6m.so.1.0(+0x12314e)[0x7f0ce2bd814e] [kthxps:29201] [22] /usr/lib/libpython3.6m.so.1.0(_PyEval_EvalFrameDefault+0x30a)[0x7f0ce2bbdb9a] [kthxps:29201] [23] /usr/lib/libpython3.6m.so.1.0(+0x1062b4)[0x7f0ce2bbb2b4] [kthxps:29201] [24] /usr/lib/libpython3.6m.so.1.0(_PyFunction_FastCallDict+0x41d)[0x7f0ce2bd782d] [kthxps:29201] [25] /usr/lib/libpython3.6m.so.1.0(_PyObject_FastCallDict+0x26f)[0x7f0ce2c16f5f] [kthxps:29201] [26] /usr/lib/libpython3.6m.so.1.0(_PyObject_Call_Prepend+0x63)[0x7f0ce2c17cd3] [kthxps:29201] [27] /usr/lib/libpython3.6m.so.1.0(PyObject_Call+0x4b)[0x7f0ce2c17dbb] [kthxps:29201] [28] /usr/lib/libpython3.6m.so.1.0(_PyEval_EvalFrameDefault+0x192f)[0x7f0ce2bbf1bf] [kthxps:29201] [29] /usr/lib/libpython3.6m.so.1.0(+0x1062b4)[0x7f0ce2bbb2b4] [kthxps:29201] *** End of error message *** make: *** [Makefile:28: tests] Segmentation fault (core dumped)
- Author Maintainer
Created originally on Bitbucket by avmo (Ashwin Vishnu)
Until we find a way for safely exporting PyCapsule pointing to a bound Cython method.