# HG changeset patch # User paulb # Date 1189815125 0 # Node ID df9f7a4e712b47e5dfdf09add8391c8a0b05f055 # Parent 36f4afc7748a127b82f4dc622d89fe77a796c830 [project @ 2007-09-15 00:12:05 by paulb] Added examples using the create method on exchanges and the Queue class. Fixed the limit in the pmap example. diff -r 36f4afc7748a -r df9f7a4e712b examples/simple_create.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/simple_create.py Sat Sep 15 00:12:05 2007 +0000 @@ -0,0 +1,71 @@ +#!/usr/bin/env python + +""" +A simple example of parallel computation using message exchanges and the create +function. +""" + +import pprocess +import time + +# Array size and a limit on the number of processes. + +N = 10 +limit = 10 +delay = 1 + +# Monitoring class. + +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. + # NOTE: Could use the with statement in the loop to package the + # NOTE: try...finally functionality. + + print "Calculating..." + for i in range(0, N): + for j in range(0, N): + ch = exchange.create() + if ch: + try: # Calculation work. + + time.sleep(delay) + ch.send((i, j, i * N + j)) + + finally: # Important finalisation. + + pprocess.exit(ch) + + 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 diff -r 36f4afc7748a -r df9f7a4e712b examples/simple_pmap.py --- a/examples/simple_pmap.py Sat Sep 15 00:11:23 2007 +0000 +++ b/examples/simple_pmap.py Sat Sep 15 00:12:05 2007 +0000 @@ -10,7 +10,7 @@ # Array size and a limit on the number of processes. N = 10 -limit = 20 +limit = 10 delay = 1 # Work function. diff -r 36f4afc7748a -r df9f7a4e712b examples/simple_queue.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/simple_queue.py Sat Sep 15 00:12:05 2007 +0000 @@ -0,0 +1,61 @@ +#!/usr/bin/env python + +""" +A simple example of parallel computation using message queues and the create +function. +""" + +import pprocess +import time + +# Array size and a limit on the number of processes. + +N = 10 +limit = 10 +delay = 1 + +# Main program. + +if __name__ == "__main__": + + # Initialise the communications queue with a limit on the number of + # channels/processes. + + queue = pprocess.Queue(limit=limit) + t = time.time() + + # Initialise an array. + + D = [0] * N * N + + # The parallel computation. + # NOTE: Could use the with statement in the loop to package the + # NOTE: try...finally functionality. + + print "Calculating..." + for i in range(0, N): + for j in range(0, N): + ch = queue.create() + if ch: + try: # Calculation work. + + time.sleep(delay) + ch.send((i, j, i * N + j)) + + finally: # Important finalisation. + + pprocess.exit(ch) + + print "Finishing..." + for i, j, result in queue: + D[i*N+j] = result + + # Show the result. + + print "Time taken:", time.time() - t + for i in range(0, N): + for result in D[i*N:i*N+N]: + print result, + print + +# vim: tabstop=4 expandtab shiftwidth=4