# HG changeset patch # User paulb # Date 1189871186 0 # Node ID f6d1181d2bb293a314caae3e28053f4bc277998e # Parent 599439ea0126e35d274ee0046dc7ad5c5ee4207e [project @ 2007-09-15 15:46:26 by paulb] Renamed to simple_start.py. diff -r 599439ea0126 -r f6d1181d2bb2 examples/simple.py --- a/examples/simple.py Sat Sep 15 01:22:25 2007 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,69 +0,0 @@ -#!/usr/bin/env python - -""" -A simple example of parallel computation using message exchanges directly. -""" - -import pprocess -import time - -# Array size and a limit on the number of processes. - -N = 10 -limit = 10 -delay = 1 - -# Work function and monitoring class. - -def calculate(ch, i, j): - - """ - A time-consuming calculation, using 'ch' to communicate with the parent - process, with 'i' and 'j' as operands. - """ - - time.sleep(delay) - ch.send((i, j, i * N + j)) - -class MyExchange(pprocess.Exchange): - - "Parallel convenience class containing the array assignment operation." - - def store_data(self, ch): - i, j, result = ch.receive() - self.D[i*N+j] = result - -# Main program. - -if __name__ == "__main__": - - # Initialise the communications exchange with a limit on the number of - # channels/processes. - - exchange = MyExchange(limit=limit) - t = time.time() - - # Initialise an array - it is stored in the exchange to permit automatic - # assignment of values as the data arrives. - - exchange.D = [0] * N * N - - # The parallel computation. - - print "Calculating..." - for i in range(0, N): - for j in range(0, N): - exchange.start(calculate, i, j) - - print "Finishing..." - exchange.finish() - - # Show the result. - - print "Time taken:", time.time() - t - for i in range(0, N): - for result in exchange.D[i*N:i*N+N]: - print result, - print - -# vim: tabstop=4 expandtab shiftwidth=4