I am trying to run a simple test using multiprocessing. The test works well until I import numpy (even though it is not used in the program). Here is the code:
from multiprocessing import Pool
import time
import numpy as np #this is the problematic line
def CostlyFunc(N):
""""""
tstart = time.time()
x = 0
for i in xrange(N):
for j in xrange(N):
if i % 2: x += 2
else: x -= 2
print "CostlyFunc : elapsed time %f s" % (time.time() - tstart)
return x
#serial application
ResultList0 = []
StartTime = time.time()
for i in xrange(3):
ResultList0.append(CostlyFunc(5000))
print "Elapsed time (serial) : ", time.time() - StartTime
#multiprocessing application
StartTime = time.time()
pool = Pool()
asyncResult = pool.map_async(CostlyFunc, [5000, 5000, 5000])
ResultList1 = asyncResult.get()
print "Elapsed time (multiporcessing) : ", time.time() - StartTime
If I don't import numpy the result is:
CostlyFunc : elapsed time 2.866265 s
CostlyFunc : elapsed time 2.793213 s
CostlyFunc : elapsed time 2.794936 s
Elapsed time (serial) : 8.45455098152
CostlyFunc : elapsed time 2.889815 s
CostlyFunc : elapsed time 2.891556 s
CostlyFunc : elapsed time 2.898898 s
Elapsed time (multiporcessing) : 2.91595196724
The total elapsed time is similar to the time required for 1 process, meaning that the computation has been parallelized. If I do import numpy the result becomes :
CostlyFunc : elapsed time 2.877116 s
CostlyFunc : elapsed time 2.866778 s
CostlyFunc : elapsed time 2.860894 s
Elapsed time (serial) : 8.60492110252
CostlyFunc : elapsed time 8.450145 s
CostlyFunc : elapsed time 8.473006 s
CostlyFunc : elapsed time 8.506402 s
Elapsed time (multiporcessing) : 8.55398178101
The total time elapsed is the same for both serial and multiprocessing methods because only one core is used. It is clear that the problem comes from numpy. Is it possible that I have an incompatibility between my versions of multiprocessing and NumPy?
I am currently using Python2.7, NumPy 1.6.2 and multiprocessing 0.70a1 on linux