# HG changeset patch # User paulb # Date 1189871854 0 # Node ID 4044f325e3e7074be5e43b5df43b8cdc248a1232 # Parent e267c093f6884c7af654c3b31b86a9f9d3f8c9e3 [project @ 2007-09-15 15:57:33 by paulb] Extended, renamed and tidied up the examples. diff -r e267c093f688 -r 4044f325e3e7 examples/simple.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/simple.py Sat Sep 15 15:57:34 2007 +0000 @@ -0,0 +1,40 @@ +#!/usr/bin/env python + +""" +A simple example of a computation performed sequentially. +""" + +import time + +# Array size. + +N = 10 +delay = 1 + +# Main program. + +if __name__ == "__main__": + + t = time.time() + + # Initialise an array. + + results = [0] * N * N + + # Perform the work. + + print "Calculating..." + for i in range(0, N): + for j in range(0, N): + time.sleep(delay) + results[i*N+j] = i * N + j + + # Show the results. + + print "Time taken:", time.time() - t + for i in range(0, N): + for result in results[i*N:i*N+N]: + print result, + print + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r e267c093f688 -r 4044f325e3e7 examples/simple_create.py --- a/examples/simple_create.py Sat Sep 15 15:57:18 2007 +0000 +++ b/examples/simple_create.py Sat Sep 15 15:57:34 2007 +0000 @@ -3,6 +3,9 @@ """ A simple example of parallel computation using message exchanges and the create function. + +NOTE: We could use the with statement in the innermost loop to package the +NOTE: try...finally functionality. """ import pprocess @@ -39,9 +42,7 @@ exchange.D = [0] * N * N - # The parallel computation. - # NOTE: Could use the with statement in the loop to package the - # NOTE: try...finally functionality. + # Perform the work. print "Calculating..." for i in range(0, N): @@ -57,10 +58,12 @@ pprocess.exit(ch) + # Wait for the results. + print "Finishing..." exchange.finish() - # Show the result. + # Show the results. print "Time taken:", time.time() - t for i in range(0, N): diff -r e267c093f688 -r 4044f325e3e7 examples/simple_create_queue.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/simple_create_queue.py Sat Sep 15 15:57:34 2007 +0000 @@ -0,0 +1,66 @@ +#!/usr/bin/env python + +""" +A simple example of parallel computation using message queues and the create +function. + +NOTE: We could use the with statement in the innermost loop to package the +NOTE: try...finally functionality. +""" + +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 + + # Perform the work. + # 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) + + # Store the results as they arrive. + + print "Finishing..." + for i, j, result in queue: + D[i*N+j] = result + + # Show the results. + + 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 diff -r e267c093f688 -r 4044f325e3e7 examples/simple_managed.py --- a/examples/simple_managed.py Sat Sep 15 15:57:18 2007 +0000 +++ b/examples/simple_managed.py Sat Sep 15 15:57:34 2007 +0000 @@ -15,15 +15,14 @@ # Work function and monitoring class. -def calculate(ch, i, j): +def calculate(i, j): """ - A time-consuming calculation, using 'ch' to communicate with the parent - process, with 'i' and 'j' as operands. + A supposedly time-consuming calculation on 'i' and 'j'. """ time.sleep(delay) - ch.send((i, j, i * N + j)) + return (i, j, i * N + j) class MyExchange(pprocess.Exchange): @@ -47,16 +46,25 @@ # assignment of values as the data arrives. exchange.D = [0] * N * N - calc = exchange.manage(calculate) + + # Wrap the calculate function and manage it. + + calc = exchange.manage(pprocess.MakeParallel(calculate)) + + # Perform the work. print "Calculating..." for i in range(0, N): for j in range(0, N): calc(i, j) + # Wait for the results. + print "Finishing..." exchange.finish() + # Show the results. + print "Time taken:", time.time() - t for i in range(0, N): for result in exchange.D[i*N:i*N+N]: diff -r e267c093f688 -r 4044f325e3e7 examples/simple_managed_queue.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/simple_managed_queue.py Sat Sep 15 15:57:34 2007 +0000 @@ -0,0 +1,66 @@ +#!/usr/bin/env python + +""" +A simple example of parallel computation using a queue and managed callables. +""" + +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(i, j): + + """ + A supposedly time-consuming calculation on 'i' and 'j'. + """ + + time.sleep(delay) + return (i, j, i * N + j) + +# 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. + + results = [0] * N * N + + # Wrap the calculate function and manage it. + + calc = queue.manage(pprocess.MakeParallel(calculate)) + + # Perform the work. + + print "Calculating..." + for i in range(0, N): + for j in range(0, N): + calc(i, j) + + # Store the results as they arrive. + + print "Finishing..." + for i, j, result in queue: + results[i*N+j] = result + + # Show the results. + + print "Time taken:", time.time() - t + for i in range(0, N): + for result in results[i*N:i*N+N]: + print result, + print + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r e267c093f688 -r 4044f325e3e7 examples/simple_map.py --- a/examples/simple_map.py Sat Sep 15 15:57:18 2007 +0000 +++ b/examples/simple_map.py Sat Sep 15 15:57:34 2007 +0000 @@ -28,13 +28,19 @@ t = time.time() + # Initialise an array. + sequence = [] for i in range(0, N): for j in range(0, N): sequence.append((i, j)) + # Perform the work. + results = map(calculate, sequence) + # Show the results. + print "Time taken:", time.time() - t for i in range(0, N): for result in results[i*N:i*N+N]: diff -r e267c093f688 -r 4044f325e3e7 examples/simple_pmap.py --- a/examples/simple_pmap.py Sat Sep 15 15:57:18 2007 +0000 +++ b/examples/simple_pmap.py Sat Sep 15 15:57:34 2007 +0000 @@ -29,13 +29,19 @@ t = time.time() + # Initialise an array. + sequence = [] for i in range(0, N): for j in range(0, N): sequence.append((i, j)) + # Perform the work. + results = pprocess.pmap(calculate, sequence, limit=limit) + # Show the results. + print "Time taken:", time.time() - t for i in range(0, N): for result in results[i*N:i*N+N]: diff -r e267c093f688 -r 4044f325e3e7 examples/simple_queue.py --- a/examples/simple_queue.py Sat Sep 15 15:57:18 2007 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,61 +0,0 @@ -#!/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 diff -r e267c093f688 -r 4044f325e3e7 examples/simple_start.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/simple_start.py Sat Sep 15 15:57:34 2007 +0000 @@ -0,0 +1,71 @@ +#!/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 + + # Perform the work. + + print "Calculating..." + for i in range(0, N): + for j in range(0, N): + exchange.start(calculate, i, j) + + # Wait for the results. + + print "Finishing..." + exchange.finish() + + # Show the results. + + 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 e267c093f688 -r 4044f325e3e7 examples/simple_start_queue.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/simple_start_queue.py Sat Sep 15 15:57:34 2007 +0000 @@ -0,0 +1,63 @@ +#!/usr/bin/env python + +""" +A simple example of parallel computation using a queue and the start method. +""" + +import pprocess +import time + +# Array size and a limit on the number of processes. + +N = 10 +limit = 10 +delay = 1 + +# Work function. + +def calculate(i, j): + + "A supposedly time-consuming calculation on 'i' and 'j'." + + time.sleep(delay) + return (i, j, i * N + j) + +# 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. + + results = [0] * N * N + + # Wrap the calculate function. + + calc = pprocess.MakeParallel(calculate) + + # Perform the work. + + for i in range(0, N): + for j in range(0, N): + queue.start(calc, *(i, j)) + + # Store the results as they arrive. + + print "Finishing..." + for i, j, result in queue: + results[i*N+j] = result + + # Show the results. + + print "Time taken:", time.time() - t + for i in range(0, N): + for result in results[i*N:i*N+N]: + print result, + print + +# vim: tabstop=4 expandtab shiftwidth=4