numba用户手册
---------------------------------------------------------------------------------------------------------------------
8.1.调试
#脚本 myscript.py
import numba
@numba.jit
def f(x):
return 2 * x
f(42)
# 调试:
C:\Users\Administrator>numba myscript.py --annotate
C:\Users\Administrator>numba myscript.py --annotate-html myscript.html
C:\Users\Administrator> numba myscript.py --dump-llvm
C:\Users\Administrator>numba myscript.py --dump-optimized
C:\Users\Administrator> numba myscript.py --dump-assemb
---------------------------------------------------------------------------------------------------------------------
8.2.诊断# 设置环境变量 NUMBA_PARALLEL_DIAGNOSTICS
@njit(parallel=True)
def test(x):
n = x.shape[0]
a = np.sin(x)
b = np.cos(a * a)
acc = 0
for i in prange(n - 2):
for j in prange(n - 1):
acc += b[i] + b[j + 1]
return acc
test(np.arange(10))
test.parallel_diagnostics(level=4) #诊断信息详细程度1最不详细,4详细
---------------------------------------------------------------------------------------------------------------------
8.3.numba帮助
C:\Users\Administrator>numba --help
C:\Users\Administrator>numba -snumba --sysinfo
IPython或Jupyter查看信息:!numba -s
---------------------------------------------------------------------------------------------------------------------
8.4.故障排除和技巧
1)什么编译:建议只尝试编译关键代码小程序加速不明显;如函数执行时间少于10 µs则保留该函数.例外jitted函数调用该函数(@jit)
2)代码无法编译
(1)类型推断失败
@jit(nopython=True)
def f(x, y): return x + y
f(1, 2) #numba能够正确地推断出类型
f(1, (2,)) #如用元组和数字来调用函数编译错误
(2)不能静态地确定函数的返回类型
@jit(nopython=True)
def f(x):return ((1,) if x>10 else 1)
f(10) #错误
(3)无类型的列表问题 #列表必须包含有相同类型
@jit(nopython=True)
def f(): return [1, 2, 3] #已知类型的列表
@jit(nopython=True)
def f(x):
tmp = [] # defined empty
for i in range(x):tmp.append(i) #使用空列表,但类型可以推断 `i`
return tmp
@jit(nopython=True)
def f(x): tmp = [] ;return (tmp, x) #ERROR: 类型无法推断the type of `tmp` is unknown
#指定空列表类型,或指定参数为nb.typed.List() #the type of `tmp` is known, but it is still empty
@jit(nopython=True)
def f(x):return ([np.float32(x) for x in range(0)],x)
---------------------------------------------------------------------------------------------------------------------
3)编译的代码太慢#编译nopython模式失败
@jit
def f(a, b):
s = a + float(b)
return s
f(1, 2)#3.0
f.inspect_types() #查看类型是否推断成功 f (int64, int64)
f(1, "2")# 3.0
f.inspect_types() #f (int64, unicode_type)...
---------------------------------------------------------------------------------------------------------------------
4)将函数(@jit)作为实参#有额外的开销
@jit(nopython=True)
def f(g, x):return g(x) + g(-x)
result = f(jitted_g_function, 1)
def make_f(g): #可以使用工厂函数来捕获闭包中的函数参数
@jit(nopython=True) #Note: a new f() is created each time make_f() is called!
def f(x):
return g(x) + g(-x)
return f
f = make_f(jitted_g_function)
result = f(1)
---------------------------------------------------------------------------------------------------------------------
5)PyInstaller打包
llvmlite可能会遇到问题.llvmlite需一个非Python DLL才能工作,但打包程序不会自动检测到它.
必将DLL位置告知打包程序:C:\ProgramData\Anaconda3\Lib\site-packages\llvmlite\binding\llvmlite.dll(xx.so)
---------------------------------------------------------------------------------------------------------------------
6)numba语言环境--错误消息:
RuntimeError: Failed at nopython (nopython mode backend)
LLVM will produce incorrect floating-point code in the current locale
遇到LLVM错误(例如matplotlib的Qt后端)会发生这种情况.
解决:将区域设置强制恢复为其默认值locale.setlocale(locale.LC_NUMERIC, 'C')
---------------------------------------------------------------------------------------------------------------------