对于 C + + ,我们可以使用 OpenMP 进行并行编程; 但是,OpenMP 不适用于 Python。如果我想并行 Python 程序的某些部分,我应该做什么?
守则的结构可视为:
solve1(A)
solve2(B)
其中 solve1
和 solve2
是两个独立的函数。如何以并行代替顺序运行这类代码以减少运行时间?
密码是:
def solve(Q, G, n):
i = 0
tol = 10 ** -4
while i < 1000:
inneropt, partition, x = setinner(Q, G, n)
outeropt = setouter(Q, G, n)
if (outeropt - inneropt) / (1 + abs(outeropt) + abs(inneropt)) < tol:
break
node1 = partition[0]
node2 = partition[1]
G = updateGraph(G, node1, node2)
if i == 999:
print "Maximum iteration reaches"
print inneropt
其中 setinner
和 setouter
是两个独立的函数。这就是我想要并行的地方..。