Search code examples
pythonarraysnumpymultiprocessingpool

Python - multiprocessing pool with functions with 2 inputs arrays


I m trying to use multiprocess in order to decrease the time of calculation of functions whose depend of 2D arrays whose shape is 2000x2000. I have 2 inputs arrays for the function but with p.map it doesnt work...(with one it s ok). How can i do to enable that?

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

and i get an error : "The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()"


Solution

  • A way to do solve that is to zip the input 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))