I have been trying to improve my code (with numba and multiprocessing), but I cannot quite get it, because my function has a lot of arguments.
I have already simplified it with other functions (see below)...
As each agent (a class instance) is independent of each other for these actions, I would like to replace the for
with Pool
.
So I would get a large function pooling()
that I would call and pass the list of agents
from multiprocessing import Pool
p = Pool(4)
p.map(pooling, list(agents))
But, where do I ADD all the arguments that the pooling function will need?
As it is:
def check_demographics(month, my_agents, families, firms, year, mortality_men, mortality_women, fertility, state_id):
dummy = list(my_agents)
d = str(state_id.iloc[0])
# Place where I would like to replace the LOOP. All below would be a function
for agent in dummy:
if agent.get_region_id()[:2] == d:
# Brithday
if month % 12 == agent.month - 1:
agent.update_age()
# Mortality probability
if agent.get_gender() == 'Male':
prob = mortality_men[mortality_men['age'] == agent.get_age()][year].iloc[0]
# When gender is Female
else:
# Extract specific agent data to calculate mortality 'Female'
prob = mortality_women[mortality_women['age'] == agent.get_age()][year].iloc[0]
# Give birth decision
age = agent.get_age()
if 14 < age < 50:
pregnant(agent, fertility, year, families, my_agents)
# Mortality procedures
if fixed_seed.random() < prob:
mortal(my_agents, my_graveyard, families, agent, firms)
It is the most time consuming function in my programme.
And @jit
is not helping much.
Thanks a bunch
Yes, there is a lot of parameters! Consider using a class.
Well, since
Pool.map
support only one iterable argument, you need to group everything in one place. I suggest you to use the "Facade" pattern: an intermediate class used to store all required parameters and having a single method (I call itcheck
) without parameter (it's a method).Remark: my refactoring is really ugly, but I wanted to keep variable names unchanged for clarity.
Then your loop can be something like that:
You said that each agent is independent of each other but, after analysing the loop, I see that you need the complete list of agents (the
my_agents
parameters). It's obvious with theFacade
class. So your agent list must not change and the internal state of each agent must be frozen during looping.