Hide
Siafoo is here to make coding less frustrating and to save you time. Join Siafoo Now or Learn More

PyPar Ring Example Atom Feed 0

In Brief A simple demonstration of creating processes and sending MPI messages using PyPar. The code itself creates a set of processes and passes a message through all of them in a sort of 'ring'.
# 's
 1import pypar                         # The Python-MPI interface 
2
3numproc = pypar.size() # Number of processes as specified by mpirun
4myid = pypar.rank() # Id of of this process (myid in [0, numproc-1])
5node = pypar.get_processor_name() # Host name on which current process is running
6
7print 'I am proc %d of %d on node %s' %(myid, numproc, node)
8
9if myid == 0:
10 # Actions for process 0
11
12 msg = 'P0'
13 pypar.send(msg, destination=1) # Send message to proces 1 (right hand neighbour)
14 msg = pypar.receive(source=numproc-1) # Receive message from last process
15
16 print 'Processor 0 received message "%s" from processor %d' %(msg, numproc-1)
17
18else:
19 # Actions for all other processes
20
21 source = myid-1
22 destination = (myid+1)%numproc
23
24 msg = pypar.receive(source)
25 msg = msg + 'P' + str(myid) # Update message
26
27 pypar.send(msg, destination)
28
29pypar.finalize()

A simple demonstration of creating processes and sending MPI messages using PyPar. The code itself creates a set of processes and passes a message through all of them in a sort of 'ring'.

Comments

over 10 years ago (09 Feb 2010 at 09:34 PM) by David Isaacson
Is there some reason you would want to do this IRL, or is it just to show that message sending works?
over 10 years ago (09 Feb 2010 at 10:00 PM) by Stou S.
This is just an example of how to use PyPar to communicate between processes.

You might use a ring type deal for a fluid flow in a pipe simulation where each process could be a radial slice and you would be passing information about the conditions along the boundaries between the slices.