This s a simple send/receive program in MPI. Each node prints out hello from it's rank and the size of the current MPI run (total number of nodes). Then, node 0 sends a message to node 1. Finnaly node 1 receives the message.
Example 5-2. example2.py
#!/usr/bin/env python
import Numeric
from Numeric import *
import mpi
import sys
#print "before",len(sys.argv),sys.argv
sys.argv = mpi.mpi_init(len(sys.argv),sys.argv)
#print "after ",len(sys.argv),sys.argv
myid=mpi.mpi_comm_rank(mpi.MPI_COMM_WORLD)
numprocs=mpi.mpi_comm_size(mpi.MPI_COMM_WORLD)
print "hello from ",myid," of ",numprocs
tag=1234
source=0
destination=1
count=1
if myid == source:
buffer=5678
buffer=array(([5678]),"i")
mpi.mpi_send(buffer, count, mpi.MPI_INT,destination,tag, mpi.MPI_COMM_WORLD)
print "processor ",myid," sent ",buffer
if myid == destination:
buffer=mpi.mpi_recv(count, mpi.MPI_INT,source,tag, mpi.MPI_COMM_WORLD)
print "processor ",myid," got ",buffer
mpi.mpi_finalize() |
Sample output using 2 nodes:
hello from 0 of 2 hello from 1 of 2 processor 0 sent [5678] processor 1 got [5678] |