当我试图使我的OpenGL仿真运行更快时,我需要优化此功能.我想使用Parakeet,但是我不太明白我需要以哪种方式修改下面的代码.你知道我该怎么办吗?
def distanceMatrix(self,x,y,z):
" ""Computes distances between all particles and places the result in a matrix such that the ij th matrix entry corresponds to the distance between particle i and j"" "
xtemp = tile(x,(self.N,1))
dx = xtemp - xtemp.T
ytemp = tile(y,(self.N,1))
dy = ytemp - ytemp.T
ztemp = tile(z,(self.N,1))
dz = ztemp - ztemp.T
# Particles 'feel' each other across the periodic boundaries
if self.periodicX:
dx[dx>self.L/2]=dx[dx > self.L/2]-self.L
dx[dx<-self.L/2]=dx[dx < -self.L/2]+self.L
if self.periodicY:
dy[dy>self.L/2]=dy[dy>self.L/2]-self.L
dy[dy<-self.L/2]=dy[dy<-self.L/2]+self.L
if self.periodicZ:
dz[dz>self.L/2]=dz[dz>self.L/2]-self.L
dz[dz<-self.L/2]=dz[dz<-self.L/2]+self.L
# Total Distances
d = sqrt(dx**2+dy**2+dz**2)
# Mark zero entries with negative 1 to avoid divergences
d[d==0] = -1
return d, dx, dy, dz
据我所知,Parakeet应该能够不加修改地使用上述功能-它仅使用Numpy和数学.但是,当从Parakeet jit包装器调用函数时,总是出现以下错误:
AssertionError: Unsupported function: <bound method Particles.distanceMatrix of <particles.Particles instance at 0x04CD8E90>>
解决方法:
Parakeet仍然很年轻,对NumPy的支持还不完善,您的代码涉及了一些尚无法使用的功能.
1)您正在包装一个方法,而Parakeet到目前为止只知道如何处理函数.常见的解决方法是制作一个@jit包装的辅助函数,并使用所有必需的成员数据调用您的方法.方法不起作用的原因是,将有意义的类型分配给“自身”并不容易.这不是不可能的,但是足够棘手,以至于在摘下垂悬的果实之前,方法不会进入Parakeet.说到低落的水果…
2)布尔索引.尚未实现,但将在下一版本中发布.
3)np.tile:也不起作用,可能还会在下一个版本中发布.如果您想查看哪些内置函数和NumPy库函数将起作用,请查看Parakeet的mappings模块.
我重写了您的代码,使其对Parakeet更加友好:
@jit
def parakeet_dist(x, y, z, L, periodicX, periodicY, periodicZ):
# perform all-pairs computations more explicitly
# instead of tile + broadcasting
def periodic_diff(x1, x2, periodic):
diff = x1 - x2
if periodic:
if diff > (L / 2): diff -= L
if diff < (-L/2): diff += L
return diff
dx = np.array([[periodic_diff(x1, x2, periodicX) for x1 in x] for x2 in x])
dy = np.array([[periodic_diff(y1, y2, periodicY) for y1 in y] for y2 in y])
dz = np.array([[periodic_diff(z1, z2, periodicZ) for z1 in z] for z2 in z])
d= np.sqrt(dx**2 + dy**2 + dz**2)
# since we can't yet use boolean indexing for masking out zero distances
# have to fall back on explicit loops instead
for i in xrange(len(x)):
for j in xrange(len(x)):
if d[i,j] == 0: d[i,j] = -1
return d, dx, dy, dz
在我的机器上,对于N = 2000,它的运行速度仅比NumPy快3倍(NumPy为0.39s,而Parakeet为0.14s).如果我重写数组遍历以更明确地使用循环,那么性能将比NumPy快约6倍(Parakeet的运行时间约为0.06s):
@jit
def loopy_dist(x, y, z, L, periodicX, periodicY, periodicZ):
N = len(x)
dx = np.zeros((N,N))
dy = np.zeros( (N,N) )
dz = np.zeros( (N,N) )
d = np.zeros( (N,N) )
def periodic_diff(x1, x2, periodic):
diff = x1 - x2
if periodic:
if diff > (L / 2): diff -= L
if diff < (-L/2): diff += L
return diff
for i in xrange(N):
for j in xrange(N):
dx[i,j] = periodic_diff(x[j], x[i], periodicX)
dy[i,j] = periodic_diff(y[j], y[i], periodicY)
dz[i,j] = periodic_diff(z[j], z[i], periodicZ)
d[i,j] = dx[i,j] ** 2 + dy[i,j] ** 2 + dz[i,j] ** 2
if d[i,j] == 0: d[i,j] = -1
else: d[i,j] = np.sqrt(d[i,j])
return d, dx, dy, dz
通过一些创造性的重写,您还可以使以上代码在Numba中运行,但是仅比NumPy(0.25秒)快约1.5倍.编译时间为Parakeet w /理解:1秒,Parakeet w /循环:.5秒,Numba w /循环:0.9秒.
希望接下来的几个发行版将使NumPy库函数更加惯用,但就目前而言,理解或循环通常是要走的路.