1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/examples/simple.py Sat Sep 15 15:57:34 2007 +0000
1.3 @@ -0,0 +1,40 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +A simple example of a computation performed sequentially.
1.8 +"""
1.9 +
1.10 +import time
1.11 +
1.12 +# Array size.
1.13 +
1.14 +N = 10
1.15 +delay = 1
1.16 +
1.17 +# Main program.
1.18 +
1.19 +if __name__ == "__main__":
1.20 +
1.21 + t = time.time()
1.22 +
1.23 + # Initialise an array.
1.24 +
1.25 + results = [0] * N * N
1.26 +
1.27 + # Perform the work.
1.28 +
1.29 + print "Calculating..."
1.30 + for i in range(0, N):
1.31 + for j in range(0, N):
1.32 + time.sleep(delay)
1.33 + results[i*N+j] = i * N + j
1.34 +
1.35 + # Show the results.
1.36 +
1.37 + print "Time taken:", time.time() - t
1.38 + for i in range(0, N):
1.39 + for result in results[i*N:i*N+N]:
1.40 + print result,
1.41 + print
1.42 +
1.43 +# vim: tabstop=4 expandtab shiftwidth=4
2.1 --- a/examples/simple_create.py Sat Sep 15 15:57:18 2007 +0000
2.2 +++ b/examples/simple_create.py Sat Sep 15 15:57:34 2007 +0000
2.3 @@ -3,6 +3,9 @@
2.4 """
2.5 A simple example of parallel computation using message exchanges and the create
2.6 function.
2.7 +
2.8 +NOTE: We could use the with statement in the innermost loop to package the
2.9 +NOTE: try...finally functionality.
2.10 """
2.11
2.12 import pprocess
2.13 @@ -39,9 +42,7 @@
2.14
2.15 exchange.D = [0] * N * N
2.16
2.17 - # The parallel computation.
2.18 - # NOTE: Could use the with statement in the loop to package the
2.19 - # NOTE: try...finally functionality.
2.20 + # Perform the work.
2.21
2.22 print "Calculating..."
2.23 for i in range(0, N):
2.24 @@ -57,10 +58,12 @@
2.25
2.26 pprocess.exit(ch)
2.27
2.28 + # Wait for the results.
2.29 +
2.30 print "Finishing..."
2.31 exchange.finish()
2.32
2.33 - # Show the result.
2.34 + # Show the results.
2.35
2.36 print "Time taken:", time.time() - t
2.37 for i in range(0, N):
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/examples/simple_create_queue.py Sat Sep 15 15:57:34 2007 +0000
3.3 @@ -0,0 +1,66 @@
3.4 +#!/usr/bin/env python
3.5 +
3.6 +"""
3.7 +A simple example of parallel computation using message queues and the create
3.8 +function.
3.9 +
3.10 +NOTE: We could use the with statement in the innermost loop to package the
3.11 +NOTE: try...finally functionality.
3.12 +"""
3.13 +
3.14 +import pprocess
3.15 +import time
3.16 +
3.17 +# Array size and a limit on the number of processes.
3.18 +
3.19 +N = 10
3.20 +limit = 10
3.21 +delay = 1
3.22 +
3.23 +# Main program.
3.24 +
3.25 +if __name__ == "__main__":
3.26 +
3.27 + # Initialise the communications queue with a limit on the number of
3.28 + # channels/processes.
3.29 +
3.30 + queue = pprocess.Queue(limit=limit)
3.31 + t = time.time()
3.32 +
3.33 + # Initialise an array.
3.34 +
3.35 + D = [0] * N * N
3.36 +
3.37 + # Perform the work.
3.38 + # NOTE: Could use the with statement in the loop to package the
3.39 + # NOTE: try...finally functionality.
3.40 +
3.41 + print "Calculating..."
3.42 + for i in range(0, N):
3.43 + for j in range(0, N):
3.44 + ch = queue.create()
3.45 + if ch:
3.46 + try: # Calculation work.
3.47 +
3.48 + time.sleep(delay)
3.49 + ch.send((i, j, i * N + j))
3.50 +
3.51 + finally: # Important finalisation.
3.52 +
3.53 + pprocess.exit(ch)
3.54 +
3.55 + # Store the results as they arrive.
3.56 +
3.57 + print "Finishing..."
3.58 + for i, j, result in queue:
3.59 + D[i*N+j] = result
3.60 +
3.61 + # Show the results.
3.62 +
3.63 + print "Time taken:", time.time() - t
3.64 + for i in range(0, N):
3.65 + for result in D[i*N:i*N+N]:
3.66 + print result,
3.67 + print
3.68 +
3.69 +# vim: tabstop=4 expandtab shiftwidth=4
4.1 --- a/examples/simple_managed.py Sat Sep 15 15:57:18 2007 +0000
4.2 +++ b/examples/simple_managed.py Sat Sep 15 15:57:34 2007 +0000
4.3 @@ -15,15 +15,14 @@
4.4
4.5 # Work function and monitoring class.
4.6
4.7 -def calculate(ch, i, j):
4.8 +def calculate(i, j):
4.9
4.10 """
4.11 - A time-consuming calculation, using 'ch' to communicate with the parent
4.12 - process, with 'i' and 'j' as operands.
4.13 + A supposedly time-consuming calculation on 'i' and 'j'.
4.14 """
4.15
4.16 time.sleep(delay)
4.17 - ch.send((i, j, i * N + j))
4.18 + return (i, j, i * N + j)
4.19
4.20 class MyExchange(pprocess.Exchange):
4.21
4.22 @@ -47,16 +46,25 @@
4.23 # assignment of values as the data arrives.
4.24
4.25 exchange.D = [0] * N * N
4.26 - calc = exchange.manage(calculate)
4.27 +
4.28 + # Wrap the calculate function and manage it.
4.29 +
4.30 + calc = exchange.manage(pprocess.MakeParallel(calculate))
4.31 +
4.32 + # Perform the work.
4.33
4.34 print "Calculating..."
4.35 for i in range(0, N):
4.36 for j in range(0, N):
4.37 calc(i, j)
4.38
4.39 + # Wait for the results.
4.40 +
4.41 print "Finishing..."
4.42 exchange.finish()
4.43
4.44 + # Show the results.
4.45 +
4.46 print "Time taken:", time.time() - t
4.47 for i in range(0, N):
4.48 for result in exchange.D[i*N:i*N+N]:
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/examples/simple_managed_queue.py Sat Sep 15 15:57:34 2007 +0000
5.3 @@ -0,0 +1,66 @@
5.4 +#!/usr/bin/env python
5.5 +
5.6 +"""
5.7 +A simple example of parallel computation using a queue and managed callables.
5.8 +"""
5.9 +
5.10 +import pprocess
5.11 +import time
5.12 +
5.13 +# Array size and a limit on the number of processes.
5.14 +
5.15 +N = 10
5.16 +limit = 10
5.17 +delay = 1
5.18 +
5.19 +# Work function and monitoring class.
5.20 +
5.21 +def calculate(i, j):
5.22 +
5.23 + """
5.24 + A supposedly time-consuming calculation on 'i' and 'j'.
5.25 + """
5.26 +
5.27 + time.sleep(delay)
5.28 + return (i, j, i * N + j)
5.29 +
5.30 +# Main program.
5.31 +
5.32 +if __name__ == "__main__":
5.33 +
5.34 + # Initialise the communications queue with a limit on the number of
5.35 + # channels/processes.
5.36 +
5.37 + queue = pprocess.Queue(limit=limit)
5.38 + t = time.time()
5.39 +
5.40 + # Initialise an array.
5.41 +
5.42 + results = [0] * N * N
5.43 +
5.44 + # Wrap the calculate function and manage it.
5.45 +
5.46 + calc = queue.manage(pprocess.MakeParallel(calculate))
5.47 +
5.48 + # Perform the work.
5.49 +
5.50 + print "Calculating..."
5.51 + for i in range(0, N):
5.52 + for j in range(0, N):
5.53 + calc(i, j)
5.54 +
5.55 + # Store the results as they arrive.
5.56 +
5.57 + print "Finishing..."
5.58 + for i, j, result in queue:
5.59 + results[i*N+j] = result
5.60 +
5.61 + # Show the results.
5.62 +
5.63 + print "Time taken:", time.time() - t
5.64 + for i in range(0, N):
5.65 + for result in results[i*N:i*N+N]:
5.66 + print result,
5.67 + print
5.68 +
5.69 +# vim: tabstop=4 expandtab shiftwidth=4
6.1 --- a/examples/simple_map.py Sat Sep 15 15:57:18 2007 +0000
6.2 +++ b/examples/simple_map.py Sat Sep 15 15:57:34 2007 +0000
6.3 @@ -28,13 +28,19 @@
6.4
6.5 t = time.time()
6.6
6.7 + # Initialise an array.
6.8 +
6.9 sequence = []
6.10 for i in range(0, N):
6.11 for j in range(0, N):
6.12 sequence.append((i, j))
6.13
6.14 + # Perform the work.
6.15 +
6.16 results = map(calculate, sequence)
6.17
6.18 + # Show the results.
6.19 +
6.20 print "Time taken:", time.time() - t
6.21 for i in range(0, N):
6.22 for result in results[i*N:i*N+N]:
7.1 --- a/examples/simple_pmap.py Sat Sep 15 15:57:18 2007 +0000
7.2 +++ b/examples/simple_pmap.py Sat Sep 15 15:57:34 2007 +0000
7.3 @@ -29,13 +29,19 @@
7.4
7.5 t = time.time()
7.6
7.7 + # Initialise an array.
7.8 +
7.9 sequence = []
7.10 for i in range(0, N):
7.11 for j in range(0, N):
7.12 sequence.append((i, j))
7.13
7.14 + # Perform the work.
7.15 +
7.16 results = pprocess.pmap(calculate, sequence, limit=limit)
7.17
7.18 + # Show the results.
7.19 +
7.20 print "Time taken:", time.time() - t
7.21 for i in range(0, N):
7.22 for result in results[i*N:i*N+N]:
8.1 --- a/examples/simple_queue.py Sat Sep 15 15:57:18 2007 +0000
8.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
8.3 @@ -1,61 +0,0 @@
8.4 -#!/usr/bin/env python
8.5 -
8.6 -"""
8.7 -A simple example of parallel computation using message queues and the create
8.8 -function.
8.9 -"""
8.10 -
8.11 -import pprocess
8.12 -import time
8.13 -
8.14 -# Array size and a limit on the number of processes.
8.15 -
8.16 -N = 10
8.17 -limit = 10
8.18 -delay = 1
8.19 -
8.20 -# Main program.
8.21 -
8.22 -if __name__ == "__main__":
8.23 -
8.24 - # Initialise the communications queue with a limit on the number of
8.25 - # channels/processes.
8.26 -
8.27 - queue = pprocess.Queue(limit=limit)
8.28 - t = time.time()
8.29 -
8.30 - # Initialise an array.
8.31 -
8.32 - D = [0] * N * N
8.33 -
8.34 - # The parallel computation.
8.35 - # NOTE: Could use the with statement in the loop to package the
8.36 - # NOTE: try...finally functionality.
8.37 -
8.38 - print "Calculating..."
8.39 - for i in range(0, N):
8.40 - for j in range(0, N):
8.41 - ch = queue.create()
8.42 - if ch:
8.43 - try: # Calculation work.
8.44 -
8.45 - time.sleep(delay)
8.46 - ch.send((i, j, i * N + j))
8.47 -
8.48 - finally: # Important finalisation.
8.49 -
8.50 - pprocess.exit(ch)
8.51 -
8.52 - print "Finishing..."
8.53 - for i, j, result in queue:
8.54 - D[i*N+j] = result
8.55 -
8.56 - # Show the result.
8.57 -
8.58 - print "Time taken:", time.time() - t
8.59 - for i in range(0, N):
8.60 - for result in D[i*N:i*N+N]:
8.61 - print result,
8.62 - print
8.63 -
8.64 -# vim: tabstop=4 expandtab shiftwidth=4
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
9.2 +++ b/examples/simple_start.py Sat Sep 15 15:57:34 2007 +0000
9.3 @@ -0,0 +1,71 @@
9.4 +#!/usr/bin/env python
9.5 +
9.6 +"""
9.7 +A simple example of parallel computation using message exchanges directly.
9.8 +"""
9.9 +
9.10 +import pprocess
9.11 +import time
9.12 +
9.13 +# Array size and a limit on the number of processes.
9.14 +
9.15 +N = 10
9.16 +limit = 10
9.17 +delay = 1
9.18 +
9.19 +# Work function and monitoring class.
9.20 +
9.21 +def calculate(ch, i, j):
9.22 +
9.23 + """
9.24 + A time-consuming calculation, using 'ch' to communicate with the parent
9.25 + process, with 'i' and 'j' as operands.
9.26 + """
9.27 +
9.28 + time.sleep(delay)
9.29 + ch.send((i, j, i * N + j))
9.30 +
9.31 +class MyExchange(pprocess.Exchange):
9.32 +
9.33 + "Parallel convenience class containing the array assignment operation."
9.34 +
9.35 + def store_data(self, ch):
9.36 + i, j, result = ch.receive()
9.37 + self.D[i*N+j] = result
9.38 +
9.39 +# Main program.
9.40 +
9.41 +if __name__ == "__main__":
9.42 +
9.43 + # Initialise the communications exchange with a limit on the number of
9.44 + # channels/processes.
9.45 +
9.46 + exchange = MyExchange(limit=limit)
9.47 + t = time.time()
9.48 +
9.49 + # Initialise an array - it is stored in the exchange to permit automatic
9.50 + # assignment of values as the data arrives.
9.51 +
9.52 + exchange.D = [0] * N * N
9.53 +
9.54 + # Perform the work.
9.55 +
9.56 + print "Calculating..."
9.57 + for i in range(0, N):
9.58 + for j in range(0, N):
9.59 + exchange.start(calculate, i, j)
9.60 +
9.61 + # Wait for the results.
9.62 +
9.63 + print "Finishing..."
9.64 + exchange.finish()
9.65 +
9.66 + # Show the results.
9.67 +
9.68 + print "Time taken:", time.time() - t
9.69 + for i in range(0, N):
9.70 + for result in exchange.D[i*N:i*N+N]:
9.71 + print result,
9.72 + print
9.73 +
9.74 +# vim: tabstop=4 expandtab shiftwidth=4
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
10.2 +++ b/examples/simple_start_queue.py Sat Sep 15 15:57:34 2007 +0000
10.3 @@ -0,0 +1,63 @@
10.4 +#!/usr/bin/env python
10.5 +
10.6 +"""
10.7 +A simple example of parallel computation using a queue and the start method.
10.8 +"""
10.9 +
10.10 +import pprocess
10.11 +import time
10.12 +
10.13 +# Array size and a limit on the number of processes.
10.14 +
10.15 +N = 10
10.16 +limit = 10
10.17 +delay = 1
10.18 +
10.19 +# Work function.
10.20 +
10.21 +def calculate(i, j):
10.22 +
10.23 + "A supposedly time-consuming calculation on 'i' and 'j'."
10.24 +
10.25 + time.sleep(delay)
10.26 + return (i, j, i * N + j)
10.27 +
10.28 +# Main program.
10.29 +
10.30 +if __name__ == "__main__":
10.31 +
10.32 + # Initialise the communications queue with a limit on the number of
10.33 + # channels/processes.
10.34 +
10.35 + queue = pprocess.Queue(limit=limit)
10.36 + t = time.time()
10.37 +
10.38 + # Initialise an array.
10.39 +
10.40 + results = [0] * N * N
10.41 +
10.42 + # Wrap the calculate function.
10.43 +
10.44 + calc = pprocess.MakeParallel(calculate)
10.45 +
10.46 + # Perform the work.
10.47 +
10.48 + for i in range(0, N):
10.49 + for j in range(0, N):
10.50 + queue.start(calc, *(i, j))
10.51 +
10.52 + # Store the results as they arrive.
10.53 +
10.54 + print "Finishing..."
10.55 + for i, j, result in queue:
10.56 + results[i*N+j] = result
10.57 +
10.58 + # Show the results.
10.59 +
10.60 + print "Time taken:", time.time() - t
10.61 + for i in range(0, N):
10.62 + for result in results[i*N:i*N+N]:
10.63 + print result,
10.64 + print
10.65 +
10.66 +# vim: tabstop=4 expandtab shiftwidth=4