1

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()"

11
  • 1
    what version of python are you using? the answer can depend on it. Commented Apr 28, 2015 at 15:23
  • I m using ipython with python 2.7.8 Commented Apr 28, 2015 at 15:27
  • 1
    possible duplicate stackoverflow.com/questions/5442910/… Commented Apr 28, 2015 at 15:29
  • 1
    Also, map is going to iterate over every item in the iterable you pass to it, and call f on each individual element. Is that actually what you want here? It looks like you want to pass both arrays in their entirety to the child process, and have f work on the whole array. If that's the case, multiprocessing.Pool.map won't help improve performance at all. Commented Apr 28, 2015 at 16:02
  • 2
    I actually don't think that multiprocessing will help at all for what you need to do with the arrays. numpy already does a good job with that. Commented Apr 28, 2015 at 16:03

1 Answer 1

2

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))
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks it works, just one little problem i get a list of array... : "array([ -2.23942628e-05, -2.23605957e-05, -2.23268448e-05, ..., 7.27439741e-06, 7.27185262e-06, 7.26930745e-06]), array..." How can i get an array with same shape as r or theta?
Oh, I see. You want the function to take the whole array instead of element by element. Then multiprocessing probably won't speed up much at all. In that case do print p.map(f, [[theta, r]])
In that case you could also have done. from functools import partial; partial_f = partial(f, theta); res = p.map(partial_f, [r])
thanks for your help! :) i will try that and i tell you if i won time! ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.