Skip to content

Instantly share code, notes, and snippets.

@swerwath
Last active August 29, 2016 22:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swerwath/8108369623e54f0f21306fa8342029e1 to your computer and use it in GitHub Desktop.
Save swerwath/8108369623e54f0f21306fa8342029e1 to your computer and use it in GitHub Desktop.
from mpi4py import MPI
comm = MPI.COMM_WORLD # All our nodes in this cluster
rank = comm.Get_rank() # Our node's "ID"
if rank == 0: # If we're the root/base node, we'll be the one distributing the data
data = [1,2,3,4,5]
else:
data = []
#Scatter our list elements evenly across the node in comm
data = comm.scatter(data, root=0)
print "rank", rank, "recieved", data
data = data + 2 # Perform our mapping function
#Gather
data = comm.gather(data, root=0) # Send all the mapped data back to our root node
comm.Barrier() # Wait until all processes reach this point
if rank == 0: # Only our root node had the data gathered to it
print data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment