我想使用NUMBA来加速这个功能:
from numba import jit
@jit
def rownowaga_numba(u, v):
wymiar_x = len(u)
wymiar_y = len(u[1])
f = [[[0 for j in range(wymiar_y)] for i in range(wymiar_x)] for k in range(9)]
cx = [0., 1., 0., -1., 0., 1., -1., -1., 1.]
cy = [0., 0., 1., 0., -1., 1., 1., -1., -1.]
w = [4./9, 1./9, 1./9, 1./9, 1./9, 1./36, 1./36, 1./36, 1./36]
for i in range( wymiar_x):
for j in range (wymiar_y):
for k in range(9):
up = u[i][j]
vp = v[i][j]
udot = (up**2 + vp**2)
cu = up*cx[k] + vp*cy[k]
f[k][i][j] = w[k] + w[k]*(3.0*cu + 4.5*cu**2 - 1.5*udot)
return f
当我用这些数据测试时:
import timeit
import math as m
u = [[m.sin(i) + m.cos(j) for j in range(40)] for i in range(1000)]
y = [[m.sin(i) + m.cos(j) for j in range(40)] for i in range(1000)]
t0 = timeit.default_timer()
for i in range (10):
f = rownowaga_pypy(u,y)
dt = timeit.default_timer() - t0
print('loop time:', dt)
我得到这个错误:
Traceback (most recent call last):
File "C:\Users\Ricevind\Desktop\PyPy\Skrypty\Rownowaga.py", line 29, in <module>
f = rownowaga_pypy(u,y)
File "C:\pyzo2014a\lib\site-packages\numba\dispatcher.py", line 171, in _compile_for_args
return self.compile(sig)
File "C:\pyzo2014a\lib\site-packages\numba\dispatcher.py", line 348, in compile
flags=flags, locals=self.locals)
File "C:\pyzo2014a\lib\site-packages\numba\compiler.py", line 637, in compile_extra
return pipeline.compile_extra(func)
File "C:\pyzo2014a\lib\site-packages\numba\compiler.py", line 356, in compile_extra
raise e
File "C:\pyzo2014a\lib\site-packages\numba\compiler.py", line 351, in compile_extra
bc = self.extract_bytecode(func)
File "C:\pyzo2014a\lib\site-packages\numba\compiler.py", line 343, in extract_bytecode
bc = bytecode.ByteCode(func=self.func)
File "C:\pyzo2014a\lib\site-packages\numba\bytecode.py", line 343, in __init__
raise NotImplementedError("cell vars are not supported")
NotImplementedError: cell vars are not supported
我最感兴趣的是“cell vars is not supported”的含义,因为google不返回任何有意义的结果。
最佳答案
NUBA目前在嵌套列表列表中工作得不好(至少V0.21)。我相信这就是所谓的“cell vars”错误,但我不能百分之百确定。下面,我将一切转换成NUMPY数组,使代码能够通过NUBBA进行优化:
import numpy as np
import numba as nb
import math
def rownowaga(u, v):
wymiar_x = len(u)
wymiar_y = len(u[1])
f = [[[0 for j in range(wymiar_y)] for i in range(wymiar_x)] for k in range(9)]
cx = [0., 1., 0., -1., 0., 1., -1., -1., 1.]
cy = [0., 0., 1., 0., -1., 1., 1., -1., -1.]
w = [4./9, 1./9, 1./9, 1./9, 1./9, 1./36, 1./36, 1./36, 1./36]
for i in range( wymiar_x):
for j in range (wymiar_y):
for k in range(9):
up = u[i][j]
vp = v[i][j]
udot = (up**2 + vp**2)
cu = up*cx[k] + vp*cy[k]
f[k][i][j] = w[k] + w[k]*(3.0*cu + 4.5*cu**2 - 1.5*udot)
return f
# Pull these out so that numba treats them as constant arrays
cx = np.array([0., 1., 0., -1., 0., 1., -1., -1., 1.])
cy = np.array([0., 0., 1., 0., -1., 1., 1., -1., -1.])
w = np.array([4./9, 1./9, 1./9, 1./9, 1./9, 1./36, 1./36, 1./36, 1./36])
@nb.jit(nopython=True)
def rownowaga_numba(u, v):
wymiar_x = u.shape[0]
wymiar_y = u[1].shape[0]
f = np.zeros((9, wymiar_x, wymiar_y))
for i in xrange( wymiar_x):
for j in xrange (wymiar_y):
for k in xrange(9):
up = u[i,j]
vp = v[i,j]
udot = (up*up + vp*vp)
cu = up*cx[k] + vp*cy[k]
f[k,i,j] = w[k] + w[k]*(3.0*cu + 4.5*cu**2 - 1.5*udot)
return f
现在让我们设置一些测试数组:
u = [[math.sin(i) + math.cos(j) for j in range(40)] for i in range(1000)]
y = [[math.sin(i) + math.cos(j) for j in range(40)] for i in range(1000)]
u_np = np.array(u)
y_np = np.array(y)
首先,让我们验证我的NUBA代码与OP代码给出的答案是一样的:
f1 = rownowaga(u, y)
f2 = rownowaga_numba(u_np, y_np)
从ipython笔记本:
In [13]: np.allclose(f2, np.array(f1))
Out[13]:
True
现在让我们把时间放到笔记本电脑上:
In [15] %timeit f1 = rownowaga(u, y)
1 loops, best of 3: 288 ms per loop
In [16] %timeit f2 = rownowaga_numba(u_np, y_np)
1000 loops, best of 3: 973 µs per loop
所以我们得到了一个很好的300倍的速度与最小的代码更改。我要注意的是,我在0.22点之前使用了NUBA的夜间构建:
In [16]: nb.__version__
Out[16]:
'0.21.0+137.gac9929d'