繁体   English   中英

使用多重处理来计算图形上的最短路径

[英]Using multiprocessing to compute shortest path on a graph

我目前有一个最短路径算法,该算法接收图和原始节点作为输入,并返回图中所有节点加上树的成本(每个节点的先例)。 该图是一个字典,成本和树也是如此。

由于我必须计算所有节点都具有起点的最短路径树,因此并行执行是很自然的(因为树彼此独立)。

我正在使用使用多重处理的工作人员池,并将结果附加到列表中(因此我想要一个字典列表)。

它运行时没有错误,但是有趣的是,处理时间不会随着工作人员的数量而改变(根本没有改变)。

对于为什么会发生这种情况的任何见解都将受到赞赏。 代码如下。

from LoadData import *
from ShortestPathTree import shortestPath
from time import clock, sleep
from multiprocessing import Pool, Process, cpu_count, Queue


def funcao(G,i):
    costs, pred=shortestPath(G,i)
    return pred


def main():

    #loads the graph
    graph="graph.graph"
    G = load_graph(graph)

    # loads the relevant nodes (CENTROIDS)
    destinations="destinations.graph"
    DEST = load_relevant_nodes(destinations)

    f = open('output_parallel.out','w')
    start=clock()


    pool=Pool()

    resultados=[]
    def adder(value):
        resultados.append(value)


    #for i in range(len(DEST)):
    for i in range(486):
        pool.apply_async(funcao, args=(G,DEST[i]), callback=adder)

    pool.close()
    pool.join()


    print clock()-start
    print >> f, resultados
    print >> f, 'seconds: '+ str(clock()-start)

事实证明,@ abarnert将其钉在头上,他询问每个电话要花多长时间。 输入的数据中存在一个问题,使每个呼叫都非常容易回答,因此为多个工作人员发送任务的开销弥补了性能的提高。

The results I get now that the input data was corrected are:

1核:185.0s 2核:111.2s 3核:96.6s 4核:87.5s

在双核超线程i7 620M Lenovo T410笔记本电脑上运行(Win 7 64位,Python 2.7.3)

感谢@abarnert的深刻见解!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM