# HG changeset patch # User paulb # Date 1189885344 0 # Node ID ebd8b8e451ec9f7548f64a5bcfdfa5b1cfab88e7 # Parent 6949a574c55c566097ff1a2b551ea0b9e53f0119 [project @ 2007-09-15 19:42:24 by paulb] Tidied up the examples. Added more sequential example programs and a program demonstrating usage of the Map class directly. diff -r 6949a574c55c -r ebd8b8e451ec examples/simple.py --- a/examples/simple.py Sat Sep 15 19:41:49 2007 +0000 +++ b/examples/simple.py Sat Sep 15 19:42:24 2007 +0000 @@ -31,10 +31,11 @@ # 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 + print "Time taken:", time.time() - t + # vim: tabstop=4 expandtab shiftwidth=4 diff -r 6949a574c55c -r ebd8b8e451ec examples/simple1.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/simple1.py Sat Sep 15 19:42:24 2007 +0000 @@ -0,0 +1,52 @@ +#!/usr/bin/env python + +""" +A simple example of sequential computation using a function, appending results +to a list/array. +""" + +import time + +# Array size. + +N = 10 +delay = 1 + +# Work function. + +def calculate(i, j): + + """ + A supposedly time-consuming calculation on 'i' and 'j'. + """ + + time.sleep(delay) + return i * N + j + +# Main program. + +if __name__ == "__main__": + + t = time.time() + + # Initialise an array. + + results = [] + + # Perform the work. + + print "Calculating..." + for i in range(0, N): + for j in range(0, N): + results.append(calculate(i, j)) + + # Show the results. + + for i in range(0, N): + for result in results[i*N:i*N+N]: + print result, + print + + print "Time taken:", time.time() - t + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 6949a574c55c -r ebd8b8e451ec examples/simple2.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/simple2.py Sat Sep 15 19:42:24 2007 +0000 @@ -0,0 +1,51 @@ +#!/usr/bin/env python + +""" +A simple example of sequential computation using a function. +""" + +import time + +# Array size. + +N = 10 +delay = 1 + +# Work function. + +def calculate(i, j): + + """ + A supposedly time-consuming calculation on 'i' and 'j'. + """ + + time.sleep(delay) + return i * N + j + +# 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): + results[i*N+j] = calculate(i, j) + + # Show the results. + + for i in range(0, N): + for result in results[i*N:i*N+N]: + print result, + print + + print "Time taken:", time.time() - t + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 6949a574c55c -r ebd8b8e451ec examples/simple_create.py --- a/examples/simple_create.py Sat Sep 15 19:41:49 2007 +0000 +++ b/examples/simple_create.py Sat Sep 15 19:42:24 2007 +0000 @@ -31,16 +31,17 @@ if __name__ == "__main__": + t = time.time() + # 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 + results = exchange.D = [0] * N * N # Perform the work. @@ -65,10 +66,11 @@ # 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]: + for result in results[i*N:i*N+N]: print result, print + print "Time taken:", time.time() - t + # vim: tabstop=4 expandtab shiftwidth=4 diff -r 6949a574c55c -r ebd8b8e451ec examples/simple_create_queue.py --- a/examples/simple_create_queue.py Sat Sep 15 19:41:49 2007 +0000 +++ b/examples/simple_create_queue.py Sat Sep 15 19:42:24 2007 +0000 @@ -21,15 +21,16 @@ if __name__ == "__main__": + t = time.time() + # 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 + results = [0] * N * N # Perform the work. # NOTE: Could use the with statement in the loop to package the @@ -53,14 +54,15 @@ print "Finishing..." for i, j, result in queue: - D[i*N+j] = result + results[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]: + for result in results[i*N:i*N+N]: print result, print + print "Time taken:", time.time() - t + # vim: tabstop=4 expandtab shiftwidth=4 diff -r 6949a574c55c -r ebd8b8e451ec examples/simple_managed.py --- a/examples/simple_managed.py Sat Sep 15 19:41:49 2007 +0000 +++ b/examples/simple_managed.py Sat Sep 15 19:42:24 2007 +0000 @@ -36,16 +36,17 @@ if __name__ == "__main__": + t = time.time() + # 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 + results = exchange.D = [0] * N * N # Wrap the calculate function and manage it. @@ -65,10 +66,11 @@ # 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]: + for result in results[i*N:i*N+N]: print result, print + print "Time taken:", time.time() - t + # vim: tabstop=4 expandtab shiftwidth=4 diff -r 6949a574c55c -r ebd8b8e451ec examples/simple_managed_map.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/simple_managed_map.py Sat Sep 15 19:42:24 2007 +0000 @@ -0,0 +1,58 @@ +#!/usr/bin/env python + +""" +A simple example of parallel computation using a map 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 * N + j + +# Main program. + +if __name__ == "__main__": + + t = time.time() + + # Initialise the results using map with a limit on the number of + # channels/processes. + + results = pprocess.Map(limit=limit) + + # Wrap the calculate function and manage it. + + calc = results.manage(pprocess.MakeParallel(calculate)) + + # Perform the work. + + print "Calculating..." + for i in range(0, N): + for j in range(0, N): + calc(i, j) + + # Show the results. + + for i in range(0, N): + for result in results[i*N:i*N+N]: + print result, + print + + print "Time taken:", time.time() - t + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 6949a574c55c -r ebd8b8e451ec examples/simple_managed_queue.py --- a/examples/simple_managed_queue.py Sat Sep 15 19:41:49 2007 +0000 +++ b/examples/simple_managed_queue.py Sat Sep 15 19:42:24 2007 +0000 @@ -28,11 +28,12 @@ if __name__ == "__main__": + t = time.time() + # Initialise the communications queue with a limit on the number of # channels/processes. queue = pprocess.Queue(limit=limit) - t = time.time() # Initialise an array. @@ -57,10 +58,11 @@ # 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 + print "Time taken:", time.time() - t + # vim: tabstop=4 expandtab shiftwidth=4 diff -r 6949a574c55c -r ebd8b8e451ec examples/simple_map.py --- a/examples/simple_map.py Sat Sep 15 19:41:49 2007 +0000 +++ b/examples/simple_map.py Sat Sep 15 19:42:24 2007 +0000 @@ -41,10 +41,11 @@ # 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 + print "Time taken:", time.time() - t + # vim: tabstop=4 expandtab shiftwidth=4 diff -r 6949a574c55c -r ebd8b8e451ec examples/simple_pmap.py --- a/examples/simple_pmap.py Sat Sep 15 19:41:49 2007 +0000 +++ b/examples/simple_pmap.py Sat Sep 15 19:42:24 2007 +0000 @@ -42,10 +42,11 @@ # 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 + print "Time taken:", time.time() - t + # vim: tabstop=4 expandtab shiftwidth=4 diff -r 6949a574c55c -r ebd8b8e451ec examples/simple_start.py --- a/examples/simple_start.py Sat Sep 15 19:41:49 2007 +0000 +++ b/examples/simple_start.py Sat Sep 15 19:42:24 2007 +0000 @@ -18,8 +18,8 @@ def calculate(ch, 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', using 'ch' to + communicate with the parent process. """ time.sleep(delay) @@ -37,16 +37,17 @@ if __name__ == "__main__": + t = time.time() + # 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 + results = exchange.D = [0] * N * N # Perform the work. @@ -62,10 +63,11 @@ # 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]: + for result in results[i*N:i*N+N]: print result, print + print "Time taken:", time.time() - t + # vim: tabstop=4 expandtab shiftwidth=4 diff -r 6949a574c55c -r ebd8b8e451ec examples/simple_start_queue.py --- a/examples/simple_start_queue.py Sat Sep 15 19:41:49 2007 +0000 +++ b/examples/simple_start_queue.py Sat Sep 15 19:42:24 2007 +0000 @@ -26,11 +26,12 @@ if __name__ == "__main__": + t = time.time() + # Initialise the communications queue with a limit on the number of # channels/processes. queue = pprocess.Queue(limit=limit) - t = time.time() # Initialise an array. @@ -54,10 +55,11 @@ # 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 + print "Time taken:", time.time() - t + # vim: tabstop=4 expandtab shiftwidth=4