我试图弄清楚如何使用numba
为矢量化数组操作生成numpy
样式的UFUNC。我注意到我的性能非常慢,所以我尝试在代码中按照numba FAQ调用以下代码进行调试:
import llvmlite.binding as llvm
llvm.set_option('', '--debug-only=loop-vectorize')
显然,由于内存冲突,我的循环没有被矢量化
再次从FAQ页面上,我看到“当内存访问模式不平凡时”会发生这种情况。我不清楚这意味着什么,但我尝试矢量化的代码对我来说似乎很平凡:
@guvectorize(['void(f4[:,:], b1[:,:], f8, f4, f4[:,:])'],
'(n,m), (n,m), (), () -> (n,m)', cache=True)
def enforce_cutoff(img, mask, max, nodata, out):
for i in range(img.shape[0]):
for j in range(img.shape[1]):
if mask[i,j]:
out[i,j] = nodata
else:
if img[i,j]<max:
out[i,j] = img[i,j]
else:
out[i,j] = max-0.1
任何关于为什么不能矢量化的线索,以及我如何绕过它的线索,都将不胜感激。我对numba&;我对LLVM一点也不熟悉,所以我对这方面不太了解
LLVM的完整输出如下所示:
LV: Checking a loop in "_ZN7AtmCorr18enforce_cutoff$241E5ArrayIfLi2E1A7mutable7alignedE5ArrayIbLi2E1A7mutable7alignedEdf5ArrayIfLi2E1A7mutable7alignedE" from enforce_cutoff
LV: Loop hints: force=? width=0 unroll=0
LV: Found a loop: B40.us
LV: Found an induction variable.
LV: Found an induction variable.
LV: Can't vectorize due to memory conflicts
LV: Not vectorizing: Cannot prove legality.
LV: Checking a loop in "__gufunc__._ZN7AtmCorr18enforce_cutoff$241E5ArrayIfLi2E1A7mutable7alignedE5ArrayIbLi2E1A7mutable7alignedEdf5ArrayIfLi2E1A7mutable7alignedE" from <numba.npyufunc.wrappers._GufuncWrapper object at 0x0000020A848A6438>
LV: Loop hints: force=? width=0 unroll=0
LV: Not vectorizing: Cannot prove legality.
LV: Checking a loop in "_ZN7AtmCorr18enforce_cutoff$241E5ArrayIfLi2E1A7mutable7alignedE5ArrayIbLi2E1A7mutable7alignedEdf5ArrayIfLi2E1A7mutable7alignedE" from <numba.npyufunc.wrappers._GufuncWrapper object at 0x0000020A848A6438>
LV: Loop hints: force=? width=0 unroll=0
LV: Found a loop: B40.us
LV: Found an induction variable.
LV: Found an induction variable.
LV: Found an induction variable.
LV: Found an induction variable.
LV: Did not find one integer induction var.
LV: Can't vectorize due to memory conflicts
LV: Not vectorizing: Cannot prove legality.
LV: Checking a loop in "_ZN7AtmCorr18enforce_cutoff$241E5ArrayIfLi2E1A7mutable7alignedE5ArrayIbLi2E1A7mutable7alignedEdf5ArrayIfLi2E1A7mutable7alignedE" from <numba.npyufunc.wrappers._GufuncWrapper object at 0x0000020A848A6438>
LV: Loop hints: force=? width=0 unroll=0
LV: Found a loop: B20.us.us
LV: Found an induction variable.
LV: Can't vectorize due to memory conflicts
LV: Not vectorizing: Cannot prove legality.
一个可能的解决方法是确保数组是C连续的。如果它们不是c-contigous,则会被复制
示例
计时
相关问题 更多 >
编程相关推荐