Scatter with MPI tutorial with mpi4py



In this tutorial, we're going to be talking about scatter within MPI using Python and mpi4py. Scatter is a way that we can take a bunch of elements, like those in a list, and "scatter" those elements around to the processing nodes.

from mpi4py import MPI

comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()

if rank == 0:
   data = [(x+1)**x for x in range(size)]
   print 'we will be scattering:',data
else:
   data = None
   
data = comm.scatter(data, root=0)
print 'rank',rank,'has data:',data
		

Create the information that we want to scatter about.

What we've done above is created a quick 1-liner for loop that creates a list of elements that is as long as there are processors, and generates a unique number based on a simple equation (x+1)**x which translates to the processor number, plus one, to the power of that processor number.

Now we actually scatter the data from the root processor of 0. Then we're printing out the rank, and the data that rank received from the scatter.

mpirun.openmpi -np 5 -machinefile /home/pi/mpi_testing/machinefile python ~/Desktop/sct/sct9.py
MPI with MPI4py and Python tutorial

Keep in mind one of the restrictions to scatter, and that is that you can only scatter as many elements as you have processors. If you attempt to scatter more elements than you have processors, you will get an error like:

MPI with MPI4py and Python tutorial

Also, if you are running this on a Raspberry Pi 3, you may be experiencing issues. One of our users, dgrawr, discovered this, and here's what they found:

About a week ago i posted that i was having issues with persistent hangs on scatter calls... I have an answer to that and thought i'd share in case others run into it... the Raspberry Pi 3 has wifi and bluetooth, and that was apparently interfering with the communication between nodes (address conflicts or something similar). i sudo nano'd the "/etc/modprode.d/raspi-blacklist.conf" file as described in https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=138610. Now the sample code is running and returning the expected results without issue.

The next tutorial:





  • Build a Supercomputer with Raspberry Pis
  • Intro
  • Supplies
  • Installing Operating System
  • Downloading and installing MPI
  • Testing Supercomputer
  • MPI with MPI4py Introduction
  • Installing mpi4py for use with Python and MPI
  • First basic MPI script with mpi4py
  • Using conditional, Python, statements alongside MPI commands example
  • Getting network processor size with the size command
  • Sending and Receiving data using send and recv commands with MPI
  • Dynamically sending messages to and from processors with MPI and mpi4py
  • Message and data tagging for send and recv MPI commands tutorial
  • MPI broadcasting tutorial with Python, mpi4py, and bcast
  • Scatter with MPI tutorial with mpi4py
  • Gather command with MPI, mpi4py, and Python