source: pypar/demo4.py @ 317

Last change on this file since 317 was 85, checked in by ole, 19 years ago

Added pypar files

File size: 1.8 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
22
23import pypar    # The Python-MPI interface
24import Numeric
25typ = 'd'  #'i', 'l', 'f', 'd'
26
27numproc = pypar.size()
28myid =    pypar.rank()
29node =    pypar.Get_processor_name()
30
31print "I am proc %d of %d on node %s" %(myid, numproc, node)
32
33if numproc < 2:
34  print "Demo must run on at least 2 processors to continue"     
35  pypar.Abort()
36 
37if myid == 0:
38  msg = Numeric.array([0], typ) 
39 
40  print 'Processor 0 sending message "%s" to processor %d' %(str(msg), 1)
41  pypar.send(msg, 1)
42
43  msg, status = pypar.receive(numproc-1, return_status=True)
44  print 'Processor 0 received message "%s" from processor %d' %(str(msg), numproc-1)
45  print 'Size of msg was %d bytes' %(status.bytes())
46
47else:
48  source = myid-1
49  destination = (myid+1)%numproc
50 
51  msg, status = pypar.receive(source, return_status=True)
52  print 'Processor %d received message "%s" from processor %d'\
53        %(myid, str(msg), source)
54  print 'Size of msg was %d bytes' %(status.bytes()) 
55
56  msg = list(msg)
57  msg.append(myid)
58  print msg
59  msg = Numeric.array(msg, typ)  #Update message     
60  print 'Processor %d sending msg "%s" to %d' %(myid, msg, destination)
61  pypar.send(msg, destination)
62
63pypar.Finalize()
64
65
66
Note: See TracBrowser for help on using the repository browser.