【发布时间】:2018-08-24 15:51:50
【问题描述】:
使用
threading库加速计算点云中每个点的邻域。通过在帖子底部调用函数CalculateAllPointsNeighbors。
该函数接收搜索半径、最大邻居数和拆分工作的线程数。没有对任何点进行任何更改。每个点将数据存储在其自己的np.ndarray单元格中,由其自己的索引访问。-
以下函数计算
N线程数完成计算所有点邻域所需的时间:def TimeFuncThreads(classObj, uptothreads): listTimers = [] startNum = 1 EndNum = uptothreads + 1 for i in range(startNum, EndNum): print("Current Number of Threads to Test: ", i) tempT = time.time() classObj.CalculateAllPointsNeighbors(searchRadius=0.05, maxNN=25, maxThreads=i) tempT = time.time() - tempT listTimers.append(tempT) PlotXY(np.arange(startNum, EndNum), listTimers)
我现在很困惑我是否使用了 threading 库错误以及我得到的这种行为是什么?
-
处理线程的函数和从每个线程调用的函数:
def CalculateAllPointsNeighbors(self, searchRadius=0.20, maxNN=50, maxThreads=8):
threadsList = [] pointsIndices = np.arange(self.numberOfPoints) splitIndices = np.array_split(pointsIndices, maxThreads) for i in range(maxThreads): threadsList.append(threading.Thread(target=self.GetPointsNeighborsByID, args=(splitIndices[i], searchRadius, maxNN))) [t.start() for t in threadsList] [t.join() for t in threadsList] def GetPointsNeighborsByID(self, idx, searchRadius=0.05, maxNN=20): if isinstance(idx, int): idx = [idx] for currentPointIndex in idx: currentPoint = self.pointsOpen3D.points[currentPointIndex] pointNeighborhoodObject = self.GetPointNeighborsByCoordinates(currentPoint, searchRadius, maxNN) self.pointsNeighborsArray[currentPointIndex] = pointNeighborhoodObject self.__RotatePointNeighborhood(currentPointIndex)
【问题讨论】:
-
您确定这不是系统上其他因素(如防病毒或其他软件)影响运行时间的结果吗?
-
附带说明:不要将列表推导用于副作用,仅用于构建列表。如果你这样做是为了节省空间,你可以写一个简单的
for语句作为单行语句,它更短,而不是更长。如果您这样做是因为您听说 listcomps 更快,那么它们比在每个值上调用append更快,但它们比根本不构建列表要慢。 -
时间都花在了哪里?一眼看去,它可能在您没有向我们展示的
__RotatePointNeighborhood方法中。无论它在哪里,每个线程是否都坚持自己的共享数组段,或者它们都在尝试读取(或更糟糕的是,写入)重叠的段? -
@abarnert 感谢您提供列表理解提示。所有点都写入相同的
np.ndarray。虽然每个点只写入自己的索引。例如,p1写信给array[1]。而且由于线程之间没有重叠点,并且每个点的邻域都计算一次,我认为这不是问题。
标签: python python-3.x multithreading