1.1 --- a/examples/simple.py Sat Sep 15 01:22:25 2007 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,69 +0,0 @@
1.4 -#!/usr/bin/env python
1.5 -
1.6 -"""
1.7 -A simple example of parallel computation using message exchanges directly.
1.8 -"""
1.9 -
1.10 -import pprocess
1.11 -import time
1.12 -
1.13 -# Array size and a limit on the number of processes.
1.14 -
1.15 -N = 10
1.16 -limit = 10
1.17 -delay = 1
1.18 -
1.19 -# Work function and monitoring class.
1.20 -
1.21 -def calculate(ch, i, j):
1.22 -
1.23 - """
1.24 - A time-consuming calculation, using 'ch' to communicate with the parent
1.25 - process, with 'i' and 'j' as operands.
1.26 - """
1.27 -
1.28 - time.sleep(delay)
1.29 - ch.send((i, j, i * N + j))
1.30 -
1.31 -class MyExchange(pprocess.Exchange):
1.32 -
1.33 - "Parallel convenience class containing the array assignment operation."
1.34 -
1.35 - def store_data(self, ch):
1.36 - i, j, result = ch.receive()
1.37 - self.D[i*N+j] = result
1.38 -
1.39 -# Main program.
1.40 -
1.41 -if __name__ == "__main__":
1.42 -
1.43 - # Initialise the communications exchange with a limit on the number of
1.44 - # channels/processes.
1.45 -
1.46 - exchange = MyExchange(limit=limit)
1.47 - t = time.time()
1.48 -
1.49 - # Initialise an array - it is stored in the exchange to permit automatic
1.50 - # assignment of values as the data arrives.
1.51 -
1.52 - exchange.D = [0] * N * N
1.53 -
1.54 - # The parallel computation.
1.55 -
1.56 - print "Calculating..."
1.57 - for i in range(0, N):
1.58 - for j in range(0, N):
1.59 - exchange.start(calculate, i, j)
1.60 -
1.61 - print "Finishing..."
1.62 - exchange.finish()
1.63 -
1.64 - # Show the result.
1.65 -
1.66 - print "Time taken:", time.time() - t
1.67 - for i in range(0, N):
1.68 - for result in exchange.D[i*N:i*N+N]:
1.69 - print result,
1.70 - print
1.71 -
1.72 -# vim: tabstop=4 expandtab shiftwidth=4