Python 优化成对子结构

Python 优化成对子结构,python,optimization,Python,Optimization,我必须计算大约1e6个物体的粒子速度的两两差,每个物体大约有1e4个粒子。现在我正在使用itertools.compositions在粒子上循环,但仅仅对于一个对象,我的代码就需要超过30分钟。我想知道我还能做些什么来把它加速到一个可行的速度,因为并行化在python中似乎并没有增加多少功能。赛顿是去的路吗 以下是我针对其中一个对象的代码: def pairwisevel(hist,velj,velk, xj, xk): vlos = (velj - velk) if( (xj

我必须计算大约1e6个物体的粒子速度的两两差,每个物体大约有1e4个粒子。现在我正在使用itertools.compositions在粒子上循环,但仅仅对于一个对象,我的代码就需要超过30分钟。我想知道我还能做些什么来把它加速到一个可行的速度,因为并行化在python中似乎并没有增加多少功能。赛顿是去的路吗

以下是我针对其中一个对象的代码:

def pairwisevel(hist,velj,velk, xj, xk):
    vlos = (velj - velk) 
    if( (xj - xk) < 0.):
        vlos = - vlos
    hist.add_from_value(vlos)


for i in itertools.combinations(np.arange(0,int(particles_per_group[0]),1),2):
    pairwisevel(hist, pvel[i[0]], pvel[i[1]],\
                pcoords[i[0]], pcoords[i[1]])
def成对水平(hist、velj、velk、xj、xk):
vlos=(velj-velk)
如果((xj-xk)<0.):
vlos=-vlos
历史从值添加值(vlos)
对于itertools.组合中的i(np.arange(0,int(粒子/u群[0]),1),2):
成对水平(历史,pvel[i[0]],pvel[i[1]]\
pcoords[i[0]],pcoords[i[1]]

我希望我理解你的问题。在这个例子中,我计算了一个粒子对象的历史图。但如果您不想对所有1e6组(1e4*1e4*1e6=1e14)进行比较,这仍需要几天的时间。 在这个例子中,我用来完成这个任务

代码

import numpy as np
import numba as nb
import time

#From Numba source
#Copyright (c) 2012, Anaconda, Inc.
#All rights reserved.

@nb.njit(fastmath=True)
def digitize(x, bins, right=False):
    # bins are monotonically-increasing
    n = len(bins)
    lo = 0
    hi = n

    if right:
        if np.isnan(x):
            # Find the first nan (i.e. the last from the end of bins,
            # since there shouldn't be many of them in practice)
            for i in range(n, 0, -1):
                if not np.isnan(bins[i - 1]):
                    return i
            return 0
        while hi > lo:
            mid = (lo + hi) >> 1
            if bins[mid] < x:
                # mid is too low => narrow to upper bins
                lo = mid + 1
            else:
                # mid is too high, or is a NaN => narrow to lower bins
                hi = mid
    else:
        if np.isnan(x):
            # NaNs end up in the last bin
            return n
        while hi > lo:
            mid = (lo + hi) >> 1
            if bins[mid] <= x:
                # mid is too low => narrow to upper bins
                lo = mid + 1
            else:
                # mid is too high, or is a NaN => narrow to lower bins
                hi = mid

    return lo

#Variant_1
@nb.njit(fastmath=True,parallel=True)
def bincount_comb_1(pvel,pcoords,bins):
  vlos_binned=np.zeros(bins.shape[0]+1,dtype=np.uint64)
  for i in nb.prange(pvel.shape[0]):
    for j in range(pvel.shape[0]):
      if( (pcoords[i] - pcoords[j]) < 0.):
        vlos = 0.
      else:
        vlos = (pvel[i] - pvel[j])

      dig_vlos=digitize(vlos, bins, right=False)
      vlos_binned[dig_vlos]+=1
  return vlos_binned

#Variant_2
#Is this also working?
@nb.njit(fastmath=True,parallel=True)
def bincount_comb_2(pvel,pcoords,bins):
  vlos_binned=np.zeros(bins.shape[0]+1,dtype=np.uint64)
  for i in nb.prange(pvel.shape[0]):
    for j in range(pvel.shape[0]):
      #only particles which fulfill this condition are counted?
      if( (pcoords[i] - pcoords[j]) < 0.):
        vlos = (pvel[i] - pvel[j])
        dig_vlos=digitize(vlos, bins, right=False)
        vlos_binned[dig_vlos]+=1
  return vlos_binned

#Variant_3
#Only counting once
@nb.njit(fastmath=True,parallel=True)
def bincount_comb_3(pvel,pcoords,bins):
  vlos_binned=np.zeros(bins.shape[0]+1,dtype=np.uint64)
  for i in nb.prange(pvel.shape[0]):
    for j in range(i,pvel.shape[0]):
      #only particles, where this condition is met are counted?
      if( (pcoords[i] - pcoords[j]) < 0.):
        vlos = (pvel[i] - pvel[j])
        dig_vlos=digitize(vlos, bins, right=False)
        vlos_binned[dig_vlos]+=1
  return vlos_binned


#Create some data to test
bins=np.arange(2,32)
pvel=np.random.rand(10_000)*35
pcoords=np.random.rand(10_000)*35

#first call has compilation overhead, we don't measure this
res_1=bincount_comb_1(pvel,pcoords,bins)
res_2=bincount_comb_2(pvel,pcoords,bins)

t1=time.time()
res=bincount_comb_1(pvel,pcoords,bins)
print(time.time()-t1)
t1=time.time()
res=bincount_comb_2(pvel,pcoords,bins)
print(time.time()-t1)
t1=time.time()
res=bincount_comb_3(pvel,pcoords,bins)
print(time.time()-t1)
#Variant_1: 0.5s 5.78d for 1e6 groups of points
#Variant_2: 0.3s 3.25d for 1e6 groups of points
#Variant_3: 0.22s 2.54d for 1e6 groups of points

要明确的是,您想要所有1e6和1e4点的组合?即,按1e10对的顺序?这需要一段时间,不管怎样。。。(尽管其中一个点(即仅1e6或1e4对)的30分钟似乎太长了,
hist.add\u from\u value
到底在做什么?我猜这就是罪魁祸首,因为它可能正在更新直方图(?)中的各种值,这些值只需要在下一点再次更新。你试过收集列表中的点并在最后用所有这些点更新直方图吗?现在我只想要同一个对象中的组合。hist是一个直方图类,所以我只保存属于速度差箱的对数。总的来说,我有30个速度箱,我认为这比存储所有对差异更有效一个对象有多大(有多少个粒子?)你必须使用每个组的粒子,还是你的主要目标是计算pvel的所有组合?hist到底是什么?类似于?它们有大约1e4到1e5个粒子。现在我只想看看每个物体内部的成对速度分布,来推断不同物体特征的重要性。在未来,我还想看看不同物体的粒子成对速度的相关性。(谢谢你的评论顺便说一句)非常感谢!我从来没有听说过,但这个麻木是惊人的:D