1.1 --- a/examples/simple.py Fri Sep 14 23:15:37 2007 +0000
1.2 +++ b/examples/simple.py Fri Sep 14 23:16:24 2007 +0000
1.3 @@ -1,9 +1,7 @@
1.4 #!/usr/bin/env python
1.5
1.6 """
1.7 -A simple example of parallel computation using message exchanges directly,
1.8 -exchanges and managed callables, and map-style processing with parallel-enabled
1.9 -functions.
1.10 +A simple example of parallel computation using message exchanges directly.
1.11 """
1.12
1.13 import pprocess
1.14 @@ -15,12 +13,6 @@
1.15 limit = 10
1.16 delay = 1
1.17
1.18 -def make_array(n):
1.19 -
1.20 - "Make an 'n' * 'n' array initialised with zeros."
1.21 -
1.22 - return [list(x) for x in [(0,) * n] * n]
1.23 -
1.24 # Work function and monitoring class.
1.25
1.26 def calculate(ch, i, j):
1.27 @@ -39,24 +31,7 @@
1.28
1.29 def store_data(self, ch):
1.30 i, j, result = ch.receive()
1.31 - self.D[i][j] = result
1.32 -
1.33 -# Alternative work function.
1.34 -
1.35 -def calculate_seq(i, j):
1.36 -
1.37 - """
1.38 - A version of 'calculate' with a simpler result suitable for sequential
1.39 - collection. It accepts no channel, pretending to be a function used in a
1.40 - non-parallel context.
1.41 - """
1.42 -
1.43 - time.sleep(delay)
1.44 - return i * N + j
1.45 -
1.46 -# The alternative work function converted into a parallel-enabled version.
1.47 -
1.48 -calculate2 = pprocess.MakeParallel(calculate_seq)
1.49 + self.D[i*N+j] = result
1.50
1.51 # Main program.
1.52
1.53 @@ -66,11 +41,12 @@
1.54 # channels/processes.
1.55
1.56 exchange = MyExchange(limit=limit)
1.57 + t = time.time()
1.58
1.59 # Initialise an array - it is stored in the exchange to permit automatic
1.60 # assignment of values as the data arrives.
1.61
1.62 - exchange.D = make_array(N)
1.63 + exchange.D = [0] * N * N
1.64
1.65 # The parallel computation.
1.66
1.67 @@ -78,50 +54,16 @@
1.68 for i in range(0, N):
1.69 for j in range(0, N):
1.70 exchange.start(calculate, i, j)
1.71 - print i, j
1.72
1.73 print "Finishing..."
1.74 exchange.finish()
1.75
1.76 # Show the result.
1.77
1.78 - print
1.79 - for row in exchange.D:
1.80 - print row
1.81 -
1.82 - # Try again with managed callables.
1.83 -
1.84 - exchange.D = make_array(N)
1.85 - calc = exchange.manage(calculate)
1.86 -
1.87 - print "Calculating..."
1.88 + print "Time taken:", time.time() - t
1.89 for i in range(0, N):
1.90 - for j in range(0, N):
1.91 - calc(i, j)
1.92 - print i, j
1.93 -
1.94 - print "Finishing..."
1.95 - exchange.finish()
1.96 -
1.97 - print
1.98 - for row in exchange.D:
1.99 - print row
1.100 -
1.101 - # Try again with a map, building the results sequentially.
1.102 -
1.103 - mymap = pprocess.pmap
1.104 - results = []
1.105 -
1.106 - print "Calculating..."
1.107 - for i in range(0, N):
1.108 - sequence = []
1.109 - for j in range(0, N):
1.110 - sequence.append((i, j))
1.111 - results.append(mymap(calculate2, sequence))
1.112 - print i
1.113 -
1.114 - print
1.115 - for row in results:
1.116 - print row
1.117 + for result in exchange.D[i*N:i*N+N]:
1.118 + print result,
1.119 + print
1.120
1.121 # vim: tabstop=4 expandtab shiftwidth=4