# HG changeset patch # User paulb # Date 1189811784 0 # Node ID d78115b759e5b54523d88b9a0ff50b99662748fb # Parent 31021014ccc7aed521cc485c116eb6aec5fcdb3b [project @ 2007-09-14 23:16:24 by paulb] Made the simple example focus on direct message exchange usage. Added a sequential version of the simple example, along with separate managed callable and pmap versions. diff -r 31021014ccc7 -r d78115b759e5 examples/simple.py --- a/examples/simple.py Fri Sep 14 23:15:37 2007 +0000 +++ b/examples/simple.py Fri Sep 14 23:16:24 2007 +0000 @@ -1,9 +1,7 @@ #!/usr/bin/env python """ -A simple example of parallel computation using message exchanges directly, -exchanges and managed callables, and map-style processing with parallel-enabled -functions. +A simple example of parallel computation using message exchanges directly. """ import pprocess @@ -15,12 +13,6 @@ limit = 10 delay = 1 -def make_array(n): - - "Make an 'n' * 'n' array initialised with zeros." - - return [list(x) for x in [(0,) * n] * n] - # Work function and monitoring class. def calculate(ch, i, j): @@ -39,24 +31,7 @@ def store_data(self, ch): i, j, result = ch.receive() - self.D[i][j] = result - -# Alternative work function. - -def calculate_seq(i, j): - - """ - A version of 'calculate' with a simpler result suitable for sequential - collection. It accepts no channel, pretending to be a function used in a - non-parallel context. - """ - - time.sleep(delay) - return i * N + j - -# The alternative work function converted into a parallel-enabled version. - -calculate2 = pprocess.MakeParallel(calculate_seq) + self.D[i*N+j] = result # Main program. @@ -66,11 +41,12 @@ # 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 = make_array(N) + exchange.D = [0] * N * N # The parallel computation. @@ -78,50 +54,16 @@ for i in range(0, N): for j in range(0, N): exchange.start(calculate, i, j) - print i, j print "Finishing..." exchange.finish() # Show the result. - print - for row in exchange.D: - print row - - # Try again with managed callables. - - exchange.D = make_array(N) - calc = exchange.manage(calculate) - - print "Calculating..." + print "Time taken:", time.time() - t for i in range(0, N): - for j in range(0, N): - calc(i, j) - print i, j - - print "Finishing..." - exchange.finish() - - print - for row in exchange.D: - print row - - # Try again with a map, building the results sequentially. - - mymap = pprocess.pmap - results = [] - - print "Calculating..." - for i in range(0, N): - sequence = [] - for j in range(0, N): - sequence.append((i, j)) - results.append(mymap(calculate2, sequence)) - print i - - print - for row in results: - print row + for result in exchange.D[i*N:i*N+N]: + print result, + print # vim: tabstop=4 expandtab shiftwidth=4 diff -r 31021014ccc7 -r d78115b759e5 examples/simple_managed.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/simple_managed.py Fri Sep 14 23:16:24 2007 +0000 @@ -0,0 +1,66 @@ +#!/usr/bin/env python + +""" +A simple example of parallel computation using exchanges 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(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 + calc = exchange.manage(calculate) + + print "Calculating..." + for i in range(0, N): + for j in range(0, N): + calc(i, j) + + print "Finishing..." + exchange.finish() + + 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 31021014ccc7 -r d78115b759e5 examples/simple_map.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/simple_map.py Fri Sep 14 23:16:24 2007 +0000 @@ -0,0 +1,44 @@ +#!/usr/bin/env python + +""" +A simple example of serial computation using map-style processing. +See the simple_pmap.py program for the corresponding parallel version. +""" + +import time + +# Array size. + +N = 10 +delay = 1 + +# Work function. + +def calculate(t): + + "A supposedly time-consuming calculation on 't'." + + i, j = t + time.sleep(delay) + return i * N + j + +# Main program. + +if __name__ == "__main__": + + t = time.time() + + sequence = [] + for i in range(0, N): + for j in range(0, N): + sequence.append((i, j)) + + results = map(calculate, sequence) + + 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 31021014ccc7 -r d78115b759e5 examples/simple_pmap.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/simple_pmap.py Fri Sep 14 23:16:24 2007 +0000 @@ -0,0 +1,45 @@ +#!/usr/bin/env python + +""" +A simple example of parallel computation using map-style processing. +""" + +import pprocess +import time + +# Array size and a limit on the number of processes. + +N = 10 +limit = 20 +delay = 1 + +# Work function. + +def calculate(t): + + "A supposedly time-consuming calculation on 't'." + + i, j = t + time.sleep(delay) + return i * N + j + +# Main program. + +if __name__ == "__main__": + + t = time.time() + + sequence = [] + for i in range(0, N): + for j in range(0, N): + sequence.append((i, j)) + + results = pprocess.pmap(calculate, sequence, limit=limit) + + 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