GithubHelp home page GithubHelp logo

Comments (7)

felixhekhorn avatar felixhekhorn commented on July 21, 2024

Cython and numba:

Cython submodules:

myfilea.pyx
# cython: language_level=3

from src2.myfile2 import f2

cpdef api double foo(double x):
    return x * f2(x)
src2/myfile2.pyx
# cython: language_level=3
def f2(x):
    return 2.*x*x
pkgcy/__init__.py
from scipy.integrate import quad
import os

# if os.environ.get("BUILD_WHEEL",False):
#     pass
# else:
#     import pyximport; pyximport.install()

# from myfilea import f3a as g
# # import myfile

import numba as nb
import ctypes
from numba.extending import get_cython_function_address

addr = get_cython_function_address("myfilea", "foo")
functype = ctypes.CFUNCTYPE(ctypes.c_double, ctypes.c_double)
myexp = functype(addr)

@nb.njit
def f(x):
    return x * myexp(x)

def run():
    # print(myfile.__pyx_capi__)
    # return f(10.)
    return quad(f, 0.,1.)

and

  • empty src2/__init__.pyd
  • $ cythonize -X language_level=3 -f -a -i myfilea.pyx src2/myfile2.pyx

from eko.

alecandido avatar alecandido commented on July 21, 2024

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.

from eko.

felixhekhorn avatar felixhekhorn commented on July 21, 2024

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 ...

from eko.

alecandido avatar alecandido commented on July 21, 2024

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).

from eko.

felixhekhorn avatar felixhekhorn commented on July 21, 2024

Cython is the minimal effort in rewriting

well, we would still need to rewrite everything from scratch (using C/Rust syntax)

from eko.

alecandido avatar alecandido commented on July 21, 2024

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)

from eko.

felixhekhorn avatar felixhekhorn commented on July 21, 2024

closed in favour of #189

from eko.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.