Boost Python bindings

Hi all!

I’m using Python packages relying on Boost Python bindings, such as Jiminy, to perform some physics computations. I want to not some simple methods calling Boost Python methods and attribute getters returning np.ndarray or native immutable types and perform some basic linear algebra operations on it not involving scipy or whatsoever. It is possible to JIT such code ? Currently obviously it is not working as is, since I get an error saying number does not know how to JIT whose Boost Python methods, but I get the intuition it should be possible just to tell to the compiler those are already compiled and calling them directly should be fine. What do you guys think ? I didn’t manage to find any information about numba JIT with boost Python.

Best

Hi @duburcqa,

Numba can call functions in existing compiled libraries through bindings using either ctypes or cffi, or cython extensions if they are available. Docs:

The basic idea in all these methods is to get hold of the address of the function in the library, declare the argument and return type(s) and then call it from Numba.

Quick example using ctypes binding to libm's cos function:

In [1]: import ctypes as ct

In [2]: libm = ct.CDLL('libm.so')

In [3]: libm_cos = libm.cos

In [4]: libm_cos.argtypes = [ct.c_double,]

In [5]: libm_cos.restype = ct.c_double

In [6]: libm_cos(3.14)
Out[6]: -0.9999987317275395

In [7]: from numba import njit

In [8]: @njit
   ...: def foo(x):
   ...:     return libm_cos(x)
   ...: 

In [9]: foo(3.14)
Out[9]: -0.9999987317275395

Hope this helps.

1 Like

Thank you, I will have a look, but I’m afraid it is not what I’m looking for, since boost Python uses Python native types in argument rather than ctypes :confused:

If you cannot (or do not!) want to bind to a compiled library directly and the calls have to be made using the CPython API, then I think the only thing Numba can offer is perhaps the object mode block. There’s a cost of jumping back into the python interpreter from JIT compiled code, but it might be worth it if there’s a lot of work to do in the block. There’s also numba-passthru · PyPI if you need to ship objects through to the call site in an object mode block.