source: inundation/parallel/demo.py @ 1855

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