对于C++,我们可以使用OpenMP进行并行编程; 但是,OpenMP不适用于Python.如果我想并行我的python程序的某些部分,我该怎么办?
代码的结构可以被认为是:
solve1(A)
solve2(B)
Run Code Online (Sandbox Code Playgroud)
哪里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
Run Code Online (Sandbox Code Playgroud)
setinner和setouter是两个独立的函数.这就是我要平行的地方......
我正在尝试使用scipy.integrate.ode或scipy.integrate.odeint求解大量(超过一千个)初始条件的 ODE 系统,但是执行循环的速度非常慢,并且 scipy 似乎没有提供用于输入 2D 数组(由一组指定初始条件的一维数组),并且选项vectorized似乎scipy.integrate.solve_ivp并不意味着它接受初始条件的二维数组(https://docs.scipy.org/doc/scipy/reference/ generated/scipy.integrate .solve_ivp.html)。我读过一个线程询问类似的问题(Vectorized SciPy odesolver),其中一个答案建议使用scipy.integrate.odeint,但是它似乎也不接受多维数组,所以它根本不明白如何实现它。有什么解决方案可以加快进程吗?除了矢量化之外,我还考虑过使用并行计算技术,但我对此并不熟悉,而且我认为它并没有真正像矢量化那样显着地加速程序?