使用Parakeet优化Python函数

时间:2013-11-23 20:36:56

标签: python optimization numpy simulation parakeet

我需要优化此功能,因为我试图让我的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>>

1 个答案:

答案 0 :(得分:4)

Parakeet还很年轻,它的NumPy支持不完整,你的代码涉及几个尚未运行的功能。

1)你正在包装一个方法,而Parakeet到目前为止只知道如何处理函数。常见的解决方法是创建一个@jit包装的辅助函数,并使用所有必需的成员数据调用方法。方法不起作用的原因是为“自我”分配有意义的类型是非常重要的。这并不是不可能的,但足够棘手的是,方法不会进入长尾小鹦鹉,直到下垂的水果被采摘。说到低调的水果......

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 / comprehensions:1秒,Parakeet w / loops:.5秒,Numba w / loops:0.9秒。

希望接下来的几个版本能够更加惯用地使用NumPy库函数,但是现在理解或循环通常都是可行的。