I am trying to parallelize a function that takes multiple constant arguments. So far I have been able to run it, but it is not parallelizing the process. How should I approach it?
I tried to do the following:
import numpy as np
import multiprocessing
def optm(hstep,astep,time_ERA):
#this is a secondary function where I get arrays from a dataset
data = checkdate(time_ERA,2,4)
zlevels=data[0]
pottemp=data[1]
for z1 in np.linspace(0,zlevels[-1],hstep):
for z2 in np.linspace(0,zlevels[-1],hstep):
for a1 in np.linspace(0,0.01,astep): # max angle
for a2 in np.linspace(0,0.01,astep):
for a3 in np.linspace(0,0.01,astep):
result_array=another_function(zlevels,pottemp,z1,z2,a1,a2,a3) # this function is the one that does all the math in the code. Therefore, it take a lot of time to compute it.
return result_array
Then I parallelized the function this way:
input_list = [(hstep,astep,time_ERA)] #creat a tuple for the necessary startmap
pool = multiprocessing.Pool()
result = pool.starmap(optm, input_list)
pool.close()
When I run it, it takes longer than without the parallelization. It is my first time trying to parallelize a code so I am still not sure if I should use map or starmap and how to parallelize it.