Since the main idea of MPI is to send and receive messages, I think it'd be a good idea to go ahead and cover that.
There are many ways to send and receive data, we'll cover the most direct method first, in its most basic form. There are many ways to send and receive data, however:
from mpi4py import MPI import time comm = MPI.COMM_WORLD rank = comm.rank size = comm.size name = MPI.Get_processor_name() shared = (rank+1)*5 if rank == 0: data = shared comm.send(data, dest=1) comm.send(data, dest=2) print 'From rank',name,'we sent',data time.sleep(5) elif rank == 1: data = comm.recv(source=0) print 'on node',name, 'we received:',data elif rank == 2: data = comm.recv(source=0) print 'on node',name, 'we received:',data
Sending and receiving data in the basic form is pretty simple using comm.send and comm.recv (short for receive).
We're using two parameters in comm.send. The first parameter is the data that we want to send, which we're calling data. The next parameter is the destination, or the node number.
To receive, we're just using one parameter, which is source, or what node are we expecting this data to come from.
After that, we're printing to console any data that we have.
mpirun.openmpi -np 3 -machinefile /home/pi/mpi_testing/machinefile python ~/Desktop/sct/sct5.py