Closed felixhekhorn closed 7 months ago
Cython and numba:
Cython submodules:
and
src2/__init__.pyd
$ cythonize -X language_level=3 -f -a -i myfilea.pyx src2/myfile2.pyx
We can call more or less in the same way any C library from Numba:
https://numba.readthedocs.io/en/stable/user/cfunc.html#calling-c-code-from-numba
At that point, I'd write the code directly in C/C++, or in Rust and providing a C API. Moreover, providing the C library for external users was the goal since the beginning. If EKO consumes the same one, at least during the transition, it's not bad (eventually, when the top-level infrastructure will move out of Numba, we could also consume a different kind of API). In any case, we aim to drop Numba.
yes, but the main advantage of using Cython is the minimal effort in rewriting, because I'm still afraid of doing all in one gigantic step ... this way we could, maybe, do step by step, though I'm not sure still ...
this way we could, maybe, do step by step, though I'm not sure still...
This is exactly what I was pointing out: the same feature that allows Cython to be introduced one step at a time could be used with whatever C library (because it's based on ctypes
).
Cython is the minimal effort in rewriting
well, we would still need to rewrite everything from scratch (using C/Rust syntax)
well, we would still need to rewrite everything from scratch (using C/Rust syntax)
For as long as it's just math, the C/Rust syntax is mostly Python syntax. It was Fortran in the first place... (and doing it incrementally should make this a fully negligible concern)
closed in favour of #189
related to #185 - for the moment I use this issue to save my work on a different place than just my computer ... eventually, I think, I can turn this into an PR.
at the moment it seems doing just
poetry build
is not enough, but doing just before acythonize -X language_level=3 -a -i myfile.pyx
seems to be sufficientcc @scarrazza
Resources
Files
`pyproject.toml`
```toml [tool.poetry] name = "pkgcy" version = "0.0.0" description = "Blubbes" authors = ["Lorem Ipsum`pkgcy/__init__.py`
```py from scipy.integrate import quad import os if os.environ.get("BUILD_WHEEL",False): pass else: import pyximport; pyximport.install() from myfile import f as g def f(x): return x def run(): return quad(g, 0.,1.) ````mybuild.py`
```py import os # See if Cython is installed try: from Cython.Build import cythonize # Do nothing if Cython is not available except ImportError: # Got to provide this function. Otherwise, poetry will fail def build(setup_kwargs): pass # Cython is installed. Compile else: # from setuptools import Extension # from setuptools.dist import Distribution from distutils.command.build_ext import build_ext # This function will be executed in setup.py: def build(setup_kwargs): # The file you want to compile extensions = [ "myfile.pyx" ] # gcc arguments hack: enable optimizations os.environ['CFLAGS'] = '-O3' # Build setup_kwargs.update({ 'ext_modules': cythonize( extensions, language_level=3, compiler_directives={'linetrace': True}, ), 'cmdclass': {'build_ext': build_ext} }) ````myfile.pyx`
```cython # cython: language_level=3 def f(x): return x*x*x ```