I dont have much experience with parallel processing in python. I have a script that takes in several fits files, which are basically images, reads them into 3D numpy arrays and does some calculations on it. I guess the "worst" part is, that I have 2 for loops, iterating over two dimensions of the array. Inside the loop, I bascically get a list, containing the third dimension of the numpy array at the given x and y coordinate. Then I calculate the maximum value, and at which index the maximum value lies. I then write the results into two new, 2D arrays at the same x and y coordinates.
for a fits file with the dimension of about 6000x6000x20, this can take a couple of minutes to finish. I then tried to have this run in parallel, since every 2D line of sight is independent of each other and can therefor be calculated in seperate processes.
I looked at some basic tutorials invoking multiprocessing, but each time I try it, it takes 10 times as long ... I have read here in some questions, that multiprocessing can have a lot of overhead. Is it possible that the processing time needed for the overhead is a lot longer than the actual calculation in the process, and that this is the reason for it beeing much slower?
Thx.
Here is a sample script I put together.
import numpy,time
import multiprocessing as mp
xs = 500
data = numpy.random.rand(100,xs,xs)
data2 = numpy.zeros(shape=(xs,xs))
def calculation(los):
maxindex = numpy.argmax(los)
return maxindex
t0 = time.time()
for x in range(xs):
for y in range(xs):
los = data[:,x,y]
data2[x,y]=calculation(los)
t1 = time.time()
print t1-t0
t0 = time.time()
pool = mp.Pool(processes=4)
results = [pool.apply_async(calculation, args=(data[:,x,y],)) for x in range(xs) for y in range(xs)]
t1 = time.time()
print t1-t0
The first version takes about 1 second, the second version 12 takes seconds on my machine.