Python - 具有 2 个输入数组的函数的多处理池
Python - multiprocessing pool with functions with 2 inputs arrays
我正在尝试使用多进程来减少依赖于形状为 2000x2000 的二维数组的函数的计算时间。我有 2 个输入数组用于该函数,但使用 p.map 它不起作用...(使用一个就可以)。我该怎么做才能启用它?
from multiprocessing import Pool
from numpy import *
import time
tic=time.clock()
Y=(arange(2000.))
X=(arange(2000.))
(xx,yy)=meshgrid(X,Y)
r = sqrt((xx)**2 + (yy)**2)
theta = (arctan2((yy),(xx)))
def f(theta,r):
return 1.*r**(-3/2.)*cos(-3/2.*theta)
p = Pool(4)
print p.map(f, theta,r)
toc=time.clock()
print 'Temps=', toc-tic
我得到一个错误:"The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()"
解决这个问题的一种方法是 zip
输入 arrays
def f(values):
return 1.*values[1]**(-3/2.)*cos(-3/2.*values[0])
p = Pool(4)
print p.map(f, zip(theta, r))
我正在尝试使用多进程来减少依赖于形状为 2000x2000 的二维数组的函数的计算时间。我有 2 个输入数组用于该函数,但使用 p.map 它不起作用...(使用一个就可以)。我该怎么做才能启用它?
from multiprocessing import Pool
from numpy import *
import time
tic=time.clock()
Y=(arange(2000.))
X=(arange(2000.))
(xx,yy)=meshgrid(X,Y)
r = sqrt((xx)**2 + (yy)**2)
theta = (arctan2((yy),(xx)))
def f(theta,r):
return 1.*r**(-3/2.)*cos(-3/2.*theta)
p = Pool(4)
print p.map(f, theta,r)
toc=time.clock()
print 'Temps=', toc-tic
我得到一个错误:"The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()"
解决这个问题的一种方法是 zip
输入 arrays
def f(values):
return 1.*values[1]**(-3/2.)*cos(-3/2.*values[0])
p = Pool(4)
print p.map(f, zip(theta, r))