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
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/examples/simple_managed.py Fri Sep 14 23:16:24 2007 +0000
2.3 @@ -0,0 +1,66 @@
2.4 +#!/usr/bin/env python
2.5 +
2.6 +"""
2.7 +A simple example of parallel computation using exchanges and managed callables.
2.8 +"""
2.9 +
2.10 +import pprocess
2.11 +import time
2.12 +
2.13 +# Array size and a limit on the number of processes.
2.14 +
2.15 +N = 10
2.16 +limit = 10
2.17 +delay = 1
2.18 +
2.19 +# Work function and monitoring class.
2.20 +
2.21 +def calculate(ch, i, j):
2.22 +
2.23 + """
2.24 + A time-consuming calculation, using 'ch' to communicate with the parent
2.25 + process, with 'i' and 'j' as operands.
2.26 + """
2.27 +
2.28 + time.sleep(delay)
2.29 + ch.send((i, j, i * N + j))
2.30 +
2.31 +class MyExchange(pprocess.Exchange):
2.32 +
2.33 + "Parallel convenience class containing the array assignment operation."
2.34 +
2.35 + def store_data(self, ch):
2.36 + i, j, result = ch.receive()
2.37 + self.D[i*N+j] = result
2.38 +
2.39 +# Main program.
2.40 +
2.41 +if __name__ == "__main__":
2.42 +
2.43 + # Initialise the communications exchange with a limit on the number of
2.44 + # channels/processes.
2.45 +
2.46 + exchange = MyExchange(limit=limit)
2.47 + t = time.time()
2.48 +
2.49 + # Initialise an array - it is stored in the exchange to permit automatic
2.50 + # assignment of values as the data arrives.
2.51 +
2.52 + exchange.D = [0] * N * N
2.53 + calc = exchange.manage(calculate)
2.54 +
2.55 + print "Calculating..."
2.56 + for i in range(0, N):
2.57 + for j in range(0, N):
2.58 + calc(i, j)
2.59 +
2.60 + print "Finishing..."
2.61 + exchange.finish()
2.62 +
2.63 + print "Time taken:", time.time() - t
2.64 + for i in range(0, N):
2.65 + for result in exchange.D[i*N:i*N+N]:
2.66 + print result,
2.67 + print
2.68 +
2.69 +# vim: tabstop=4 expandtab shiftwidth=4
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/examples/simple_map.py Fri Sep 14 23:16:24 2007 +0000
3.3 @@ -0,0 +1,44 @@
3.4 +#!/usr/bin/env python
3.5 +
3.6 +"""
3.7 +A simple example of serial computation using map-style processing.
3.8 +See the simple_pmap.py program for the corresponding parallel version.
3.9 +"""
3.10 +
3.11 +import time
3.12 +
3.13 +# Array size.
3.14 +
3.15 +N = 10
3.16 +delay = 1
3.17 +
3.18 +# Work function.
3.19 +
3.20 +def calculate(t):
3.21 +
3.22 + "A supposedly time-consuming calculation on 't'."
3.23 +
3.24 + i, j = t
3.25 + time.sleep(delay)
3.26 + return i * N + j
3.27 +
3.28 +# Main program.
3.29 +
3.30 +if __name__ == "__main__":
3.31 +
3.32 + t = time.time()
3.33 +
3.34 + sequence = []
3.35 + for i in range(0, N):
3.36 + for j in range(0, N):
3.37 + sequence.append((i, j))
3.38 +
3.39 + results = map(calculate, sequence)
3.40 +
3.41 + print "Time taken:", time.time() - t
3.42 + for i in range(0, N):
3.43 + for result in results[i*N:i*N+N]:
3.44 + print result,
3.45 + print
3.46 +
3.47 +# vim: tabstop=4 expandtab shiftwidth=4
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/examples/simple_pmap.py Fri Sep 14 23:16:24 2007 +0000
4.3 @@ -0,0 +1,45 @@
4.4 +#!/usr/bin/env python
4.5 +
4.6 +"""
4.7 +A simple example of parallel computation using map-style processing.
4.8 +"""
4.9 +
4.10 +import pprocess
4.11 +import time
4.12 +
4.13 +# Array size and a limit on the number of processes.
4.14 +
4.15 +N = 10
4.16 +limit = 20
4.17 +delay = 1
4.18 +
4.19 +# Work function.
4.20 +
4.21 +def calculate(t):
4.22 +
4.23 + "A supposedly time-consuming calculation on 't'."
4.24 +
4.25 + i, j = t
4.26 + time.sleep(delay)
4.27 + return i * N + j
4.28 +
4.29 +# Main program.
4.30 +
4.31 +if __name__ == "__main__":
4.32 +
4.33 + t = time.time()
4.34 +
4.35 + sequence = []
4.36 + for i in range(0, N):
4.37 + for j in range(0, N):
4.38 + sequence.append((i, j))
4.39 +
4.40 + results = pprocess.pmap(calculate, sequence, limit=limit)
4.41 +
4.42 + print "Time taken:", time.time() - t
4.43 + for i in range(0, N):
4.44 + for result in results[i*N:i*N+N]:
4.45 + print result,
4.46 + print
4.47 +
4.48 +# vim: tabstop=4 expandtab shiftwidth=4