1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

JITコンパイルしたPythonの関数をC言語から呼び出す

Last updated at Posted at 2024-07-19

タイトル通りです。numba.cfuncを利用するとJITコンパイルした関数のアドレスが取れるので、それをC言語で書かれたライブラリの関数に渡します。

C言語側

sample.c
int apply(int (*callback)(int, int))
{
    return callback(1, 2);
}

Linuxであれば、これを

gcc -shared -fPIC -o libsample.so sample.c

でビルドして共有ライブラリを作ります。

Python側

main.py
from numba import cfunc
import ctypes

# ここではmain.pyとlibsample.soを同じディレクトリに置く想定
lib = ctypes.cdll.LoadLibrary("./libsample.so")
callbackType = ctypes.CFUNCTYPE(ctypes.c_int32, ctypes.c_int32, ctypes.c_int32) # (戻り値, 引数1, ...)
lib.apply.argtypes = (callbackType,)
lib.apply.restype = ctypes.c_int32

# この関数をCに渡す
@cfunc("int32 (int32, int32)")
def add(x, y):
    return x + y

if (__name__ == "__main__"):
    print(lib.apply(add.ctypes))

main.pyを実行すれば、add関数がapply関数からコールバックされ、3と表示されます。

参考文献

環境

Ubuntu 22.04 on WSL2(Windows11)
Python 3.10.12
Numba 0.60.0
GCC 11.4.0

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?