I'm trying to use numba to speed up a slow calculation. It works great with the @njit
decorator but I really need it to work as a precompiled ahead-of-time(AOT) module. Sadly I haven't been able to get it to work. Here is the code I use to compile the AOT-module:
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()
When I compile the module it creates the window_cloud_scores.so
file but gives the following warning:
/Users/.../lib/python3.6/site-packages/numba/pycc/../runtime/_nrt_python.c:234:55: warning: incompatible pointer types passing 'PyTypeObject *' (aka 'struct _typeobject *') to parameter of type 'PyObject *' (aka 'struct _object ') [-Wincompatible-pointer-types] mi = (MemInfoObject)PyObject_CallFunctionObjArgs(&MemInfoType, addr, NULL); ^~~~~~~~~~~~ /Users/.../python3.6m/abstract.h:425:68: note: passing argument to parameter 'callable' here PyAPI_FUNC(PyObject *) PyObject_CallFunctionObjArgs(PyObject *callable,
And then when I try to run
import window_cloud_scores as wcs
wcs.run(...)
I get a segmentation fault: 11
in the python console and it a jupyter notebook the kernel dies.
And again,
@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)
Works great. Thoughts?