source: trunk/anuga_core/source/pypar_dist/demos/demo.py @ 8494

Last change on this file since 8494 was 8494, checked in by steve, 13 years ago

We are working on improvements to the parallel code which means we need to
add to the standard pypar distribution.

File size: 1.6 KB
Line 
1#########################################################
2#   
3#  Example: Running Python in parallel using pypar (MPI).
4#   
5#  Author:  Ole Nielsen,  SMS, ANU, Jan. 2002.
6#
7#########################################################
8#
9# The purpose of this code is to demonstrate how Python can be
10# used to communicate among processes using pypar.
11#
12# This demo passes messages on in a ring from processor n-1 to n starting
13# and ending with processor 0.
14# Each processor adds some text to the message before passing it on
15# and writes a log statement to the screen.
16#
17# To execute:
18#
19#   mpirun -np 4 demo.py
20
21
22import pypar    # The Python-MPI interface
23
24numproc = pypar.size()
25myid =    pypar.rank()
26node =    pypar.get_processor_name()
27
28print "I am proc %d of %d on node %s" %(myid, numproc, node)
29
30
31if numproc < 2:
32  print "Demo must run on at least 2 processors to continue"     
33  pypar.abort()
34 
35if myid == 0:
36  msg = "MSGP0" 
37 
38  print 'Processor 0 sending message "%s" to processor %d' %(msg, 1)
39  pypar.send(msg, 1)
40
41  msg, status = pypar.receive(numproc-1, return_status=True)
42  print 'Processor 0 received message "%s" from processor %d' %(msg, numproc-1)
43  print 'Size of msg was %d bytes' %(status.bytes())
44
45else:
46  source = myid-1
47  destination = (myid+1)%numproc
48 
49  msg, status = pypar.receive(source, return_status=True)
50  print 'Processor %d received message "%s" from processor %d'\
51        %(myid, msg, source)
52  print 'Size of msg was %d bytes' %(status.bytes()) 
53
54  msg = msg + '->P' + str(myid) #Update message     
55  print 'Processor %d sending msg "%s" to %d' %(myid, msg, destination)
56  pypar.send(msg, destination)
57
58pypar.finalize()
59
60
61
Note: See TracBrowser for help on using the repository browser.