import pypar    # The Python-MPI interface                                                                                                                                                                                         

numproc = pypar.size()
myid =    pypar.rank()
node =    pypar.get_processor_name()

print "I am proc %d of %d on node %s" %(myid, numproc, node)


if numproc < 2:
  print "Demo must run on at least 2 processors to continue"
  pypar.abort()

if myid == 0:
  msg = "MSGP0"

  print 'Processor 0 sending message "%s" to processor %d' %(msg, 1)
  pypar.send(msg, 1)

  msg, status = pypar.receive(numproc-1, return_status=True)
  print 'Processor 0 received message "%s" from processor %d' %(msg, numproc-1)
  print 'Size of msg was %d bytes' %(status.bytes())

else:
  source = myid-1
  destination = (myid+1)%numproc

  msg, status = pypar.receive(source, return_status=True)
  print 'Processor %d received message "%s" from processor %d'\
        %(myid, msg, source)
  print 'Size of msg was %d bytes' %(status.bytes())

  msg = msg + '->P' + str(myid) #Update message                                                                                                                                                                                    
  print 'Processor %d sending msg "%s" to %d' %(myid, msg, destination)
  pypar.send(msg, destination)

pypar.finalize()