Dark theme

Parallelisation and Python


We've now done the first four parts of our algorithm,

  1. Make an local list containing the densities in the landscape on a particular node
  2. Send the local list to node zero
  3. Receive all the local lists on node zero
  4. Add them up on node zero to make a global density list

All we now need to do is...

  1. Send the global list to all the other nodes from node zero
  2. Receive the global density list back from node zero on the other nodes
  3. Move if necessary

Which we add to the end of the model.py run section before the end of the time iteration loop...

# Send out the global densities to the nodes if (node == 0):

    for i in range (len(pipes)):

        pipes[i].send(densities)

else:

    # Receive the global densities from node zero.

    global_densities = pipe_to_zero.recv()

    landscape.setdensities(global_densities)

    # Move the agents if the density is too high.

    for i in range(node_number_of_agents):

        agents[i].step()

Note that having got the global densities back, we temporarily replace the local densities in the landscape with the global ones, using the landscape.setdensities method we wrote earlier. Once we've got our global densities into each node's landscape, we can safely call the agent[i].step method, knowing it will be calculated on global not local densities. Note that the agents[i].step call is now snug within code that only runs on worker nodes, not node zero.


Finally, the last problem is that, in the above, the landscape.setdensities(globalDensities) is done inside a node != 0 section. This means the landscape isn't set up for node == 0. Thus our final adjustment is to alter the reporting section to set this up...

// Report
 
if (node == 0):
    print("time = ", time, " -------------------")
    landscape.setdensities(densities)
    for x in range(width):
        for y in range (height):
            print(landscape.getdensityat(x,y), end=" ")
    print("")

Overall we've only made two communications per worker node (the bottleneck is node zero – but it isn't doing any other processing), rather than each Agent having to talk to all the others to find out where they are. Smashing – job's a good 'un. Here's the finished classes: model.py, landscape.py, agent.py. Other than accessor/mutator methods for the densities array in landscape.py, we've only had to change model.py. Plainly there are other efficiency savings that could be made if we're happy for the code to be even more obscure, but it should run ok.

So, let's do that now in the final section.


  1. Introduction
  2. Building the model
  3. Divide up the agents
  4. Worker node collation
  5. This page
  6. Final model <-- next