本文介绍了用于 Python 的 Parfor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找 MATLAB 的 Python parfor (Scipy, Numpy) 的明确答案.

I am looking for a definitive answer to MATLAB's parfor for Python (Scipy, Numpy).

有类似parfor的解决方案吗?如果不是,创建一个的复杂性是什么?

Is there a solution similar to parfor? If not, what is the complication for creating one?

更新:这是我需要加速的典型数值计算代码

UPDATE: Here is a typical numerical computation code that I need speeding up

import numpy as np

N = 2000
output = np.zeros([N,N])
for i in range(N):
    for j in range(N):
        output[i,j] = HeavyComputationThatIsThreadSafe(i,j)

一个繁重计算函数的例子是:

An example of a heavy computation function is:

import scipy.optimize

def HeavyComputationThatIsThreadSafe(i,j):
    n = i * j

    return scipy.optimize.anneal(lambda x: np.sum((x-np.arange(n)**2)), np.random.random((n,1)))[0][0,0]

推荐答案

有很多 Python 并行计算框架.我碰巧最喜欢的是 IPython,但我对其他任何一个都不太了解.在 IPython 中,parfor 的一个类似物是 client.MultiEngineClient.map()有关快速简便并行的文档.

There are many Python frameworks for parallel computing. The one I happen to like most is IPython, but I don't know too much about any of the others. In IPython, one analogue to parfor would be client.MultiEngineClient.map() or some of the other constructs in the documentation on quick and easy parallelism.

这篇关于用于 Python 的 Parfor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 18:29