我正在尝试使用numba来加快计算速度。它可以与@njit
装饰器一起很好地工作,但是我真的需要它作为预编译的ahead-of-time(AOT)模块来工作。可悲的是,我无法使其正常工作。这是我用来编译AOT模块的代码:
from numba.pycc import CC
import numpy as np
cc = CC('window_cloud_scores')
cc.verbose = True
cc.output_dir='/cache'
cc.output_file='window_cloud_scores.so'
@cc.export('run', 'f8[:,:](u1[:,:], i4)')
def run(clouds,window):
r=int(window/2)
assert clouds.ndim==2
assert clouds.shape[0]==clouds.shape[1]
rows,cols=clouds.shape
score_map=np.full(clouds.shape,-1)
scores=[]
for j in range(r,rows-r):
score_cols=[]
for i in range(r,cols-r):
clouds_window=clouds[j-r:j+r+1,i-r:i+r+1]
score_cols.append(clouds_window.mean())
scores.append(score_cols)
return np.array(scores)
if __name__ == "__main__":
cc.compile()
我在编译模块时会创建window_cloud_scores.so
文件,但会发出以下警告:
/ Users /.../ lib / python3.6 / site-packages / numba / pycc /../ runtime / _nrt_python.c:234:55:警告:传递“ PyTypeObject *”的不兼容指针类型(又名“ struct _typeobject *') 'PyObject *'类型的参数(又名'struct _object ')[-Wincompatible-pointer-types] mi =(MemInfoObject )PyObject_CallFunctionObjArgs(&MemInfoType,addr,NULL); ^ ~~~~~~~~~~~~ /Users/.../python3.6m/abstract.h:425:68:注意:此处将参数传递给参数“ callable” PyAPI_FUNC(PyObject *)PyObject_CallFunctionObjArgs(PyObject * callable,
然后当我尝试运行
import window_cloud_scores as wcs
wcs.run(...)
我在python控制台中得到一个segmentation fault: 11
,它是内核死去的jupyter笔记本。
再次
@njit
def run(clouds,window):
r=int(window/2)
assert clouds.ndim==2
assert clouds.shape[0]==clouds.shape[1]
rows,cols=clouds.shape
score_map=np.full(clouds.shape,-1)
scores=[]
for j in range(r,rows-r):
score_cols=[]
for i in range(r,cols-r):
clouds_window=clouds[j-r:j+r+1,i-r:i+r+1]
score_cols.append(clouds_window.mean())
scores.append(score_cols)
return np.array(scores)
效果很好。有想法吗?