本文主要是介绍numba用户手册 10调试,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
numba用户手册
1.numba基础
2.@jit
3.使用自动并行化@jit
4.性能提升
5.创建ufunc
6.@jitclass
7.@cfunc
8.提前编译代码AOT
9.numba线程
10.调试
---------------------------------------------------------------------------------------------------------------------8.1.调试#脚本 myscript.pyimport numba@numba.jitdef f(x):return 2 * xf(42)# 调试:C:\Users\Administrator>numba myscript.py --annotateC:\Users\Administrator>numba myscript.py --annotate-html myscript.htmlC:\Users\Administrator> numba myscript.py --dump-llvmC:\Users\Administrator>numba myscript.py --dump-optimizedC:\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 = 0for i in prange(n - 2):for j in prange(n - 1):acc += b[i] + b[j + 1]return acctest(np.arange(10))test.parallel_diagnostics(level=4) #诊断信息详细程度1最不详细,4详细---------------------------------------------------------------------------------------------------------------------8.3.numba帮助C:\Users\Administrator>numba --helpC:\Users\Administrator>numba -snumba --sysinfoIPython或Jupyter查看信息:!numba -s---------------------------------------------------------------------------------------------------------------------8.4.故障排除和技巧1)什么编译:建议只尝试编译关键代码小程序加速不明显;如函数执行时间少于10 µs则保留该函数.例外jitted函数调用该函数(@jit)2)代码无法编译(1)类型推断失败@jit(nopython=True)def f(x, y): return x + yf(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 emptyfor 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模式失败@jitdef f(a, b):s = a + float(b)return sf(1, 2)#3.0f.inspect_types() #查看类型是否推断成功 f (int64, int64)f(1, "2")# 3.0f.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 ff = 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')---------------------------------------------------------------------------------------------------------------------
这篇关于numba用户手册 10调试的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!