1.1 --- a/examples/simple.py Sat Sep 15 19:41:49 2007 +0000
1.2 +++ b/examples/simple.py Sat Sep 15 19:42:24 2007 +0000
1.3 @@ -31,10 +31,11 @@
1.4
1.5 # Show the results.
1.6
1.7 - print "Time taken:", time.time() - t
1.8 for i in range(0, N):
1.9 for result in results[i*N:i*N+N]:
1.10 print result,
1.11 print
1.12
1.13 + print "Time taken:", time.time() - t
1.14 +
1.15 # vim: tabstop=4 expandtab shiftwidth=4
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/examples/simple1.py Sat Sep 15 19:42:24 2007 +0000
2.3 @@ -0,0 +1,52 @@
2.4 +#!/usr/bin/env python
2.5 +
2.6 +"""
2.7 +A simple example of sequential computation using a function, appending results
2.8 +to a list/array.
2.9 +"""
2.10 +
2.11 +import time
2.12 +
2.13 +# Array size.
2.14 +
2.15 +N = 10
2.16 +delay = 1
2.17 +
2.18 +# Work function.
2.19 +
2.20 +def calculate(i, j):
2.21 +
2.22 + """
2.23 + A supposedly time-consuming calculation on 'i' and 'j'.
2.24 + """
2.25 +
2.26 + time.sleep(delay)
2.27 + return i * N + j
2.28 +
2.29 +# Main program.
2.30 +
2.31 +if __name__ == "__main__":
2.32 +
2.33 + t = time.time()
2.34 +
2.35 + # Initialise an array.
2.36 +
2.37 + results = []
2.38 +
2.39 + # Perform the work.
2.40 +
2.41 + print "Calculating..."
2.42 + for i in range(0, N):
2.43 + for j in range(0, N):
2.44 + results.append(calculate(i, j))
2.45 +
2.46 + # Show the results.
2.47 +
2.48 + for i in range(0, N):
2.49 + for result in results[i*N:i*N+N]:
2.50 + print result,
2.51 + print
2.52 +
2.53 + print "Time taken:", time.time() - t
2.54 +
2.55 +# vim: tabstop=4 expandtab shiftwidth=4
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/examples/simple2.py Sat Sep 15 19:42:24 2007 +0000
3.3 @@ -0,0 +1,51 @@
3.4 +#!/usr/bin/env python
3.5 +
3.6 +"""
3.7 +A simple example of sequential computation using a function.
3.8 +"""
3.9 +
3.10 +import time
3.11 +
3.12 +# Array size.
3.13 +
3.14 +N = 10
3.15 +delay = 1
3.16 +
3.17 +# Work function.
3.18 +
3.19 +def calculate(i, j):
3.20 +
3.21 + """
3.22 + A supposedly time-consuming calculation on 'i' and 'j'.
3.23 + """
3.24 +
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 + # Initialise an array.
3.35 +
3.36 + results = [0] * N * N
3.37 +
3.38 + # Perform the work.
3.39 +
3.40 + print "Calculating..."
3.41 + for i in range(0, N):
3.42 + for j in range(0, N):
3.43 + results[i*N+j] = calculate(i, j)
3.44 +
3.45 + # Show the results.
3.46 +
3.47 + for i in range(0, N):
3.48 + for result in results[i*N:i*N+N]:
3.49 + print result,
3.50 + print
3.51 +
3.52 + print "Time taken:", time.time() - t
3.53 +
3.54 +# vim: tabstop=4 expandtab shiftwidth=4
4.1 --- a/examples/simple_create.py Sat Sep 15 19:41:49 2007 +0000
4.2 +++ b/examples/simple_create.py Sat Sep 15 19:42:24 2007 +0000
4.3 @@ -31,16 +31,17 @@
4.4
4.5 if __name__ == "__main__":
4.6
4.7 + t = time.time()
4.8 +
4.9 # Initialise the communications exchange with a limit on the number of
4.10 # channels/processes.
4.11
4.12 exchange = MyExchange(limit=limit)
4.13 - t = time.time()
4.14
4.15 # Initialise an array - it is stored in the exchange to permit automatic
4.16 # assignment of values as the data arrives.
4.17
4.18 - exchange.D = [0] * N * N
4.19 + results = exchange.D = [0] * N * N
4.20
4.21 # Perform the work.
4.22
4.23 @@ -65,10 +66,11 @@
4.24
4.25 # Show the results.
4.26
4.27 - print "Time taken:", time.time() - t
4.28 for i in range(0, N):
4.29 - for result in exchange.D[i*N:i*N+N]:
4.30 + for result in results[i*N:i*N+N]:
4.31 print result,
4.32 print
4.33
4.34 + print "Time taken:", time.time() - t
4.35 +
4.36 # vim: tabstop=4 expandtab shiftwidth=4
5.1 --- a/examples/simple_create_queue.py Sat Sep 15 19:41:49 2007 +0000
5.2 +++ b/examples/simple_create_queue.py Sat Sep 15 19:42:24 2007 +0000
5.3 @@ -21,15 +21,16 @@
5.4
5.5 if __name__ == "__main__":
5.6
5.7 + t = time.time()
5.8 +
5.9 # Initialise the communications queue with a limit on the number of
5.10 # channels/processes.
5.11
5.12 queue = pprocess.Queue(limit=limit)
5.13 - t = time.time()
5.14
5.15 # Initialise an array.
5.16
5.17 - D = [0] * N * N
5.18 + results = [0] * N * N
5.19
5.20 # Perform the work.
5.21 # NOTE: Could use the with statement in the loop to package the
5.22 @@ -53,14 +54,15 @@
5.23
5.24 print "Finishing..."
5.25 for i, j, result in queue:
5.26 - D[i*N+j] = result
5.27 + results[i*N+j] = result
5.28
5.29 # Show the results.
5.30
5.31 - print "Time taken:", time.time() - t
5.32 for i in range(0, N):
5.33 - for result in D[i*N:i*N+N]:
5.34 + for result in results[i*N:i*N+N]:
5.35 print result,
5.36 print
5.37
5.38 + print "Time taken:", time.time() - t
5.39 +
5.40 # vim: tabstop=4 expandtab shiftwidth=4
6.1 --- a/examples/simple_managed.py Sat Sep 15 19:41:49 2007 +0000
6.2 +++ b/examples/simple_managed.py Sat Sep 15 19:42:24 2007 +0000
6.3 @@ -36,16 +36,17 @@
6.4
6.5 if __name__ == "__main__":
6.6
6.7 + t = time.time()
6.8 +
6.9 # Initialise the communications exchange with a limit on the number of
6.10 # channels/processes.
6.11
6.12 exchange = MyExchange(limit=limit)
6.13 - t = time.time()
6.14
6.15 # Initialise an array - it is stored in the exchange to permit automatic
6.16 # assignment of values as the data arrives.
6.17
6.18 - exchange.D = [0] * N * N
6.19 + results = exchange.D = [0] * N * N
6.20
6.21 # Wrap the calculate function and manage it.
6.22
6.23 @@ -65,10 +66,11 @@
6.24
6.25 # Show the results.
6.26
6.27 - print "Time taken:", time.time() - t
6.28 for i in range(0, N):
6.29 - for result in exchange.D[i*N:i*N+N]:
6.30 + for result in results[i*N:i*N+N]:
6.31 print result,
6.32 print
6.33
6.34 + print "Time taken:", time.time() - t
6.35 +
6.36 # vim: tabstop=4 expandtab shiftwidth=4
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
7.2 +++ b/examples/simple_managed_map.py Sat Sep 15 19:42:24 2007 +0000
7.3 @@ -0,0 +1,58 @@
7.4 +#!/usr/bin/env python
7.5 +
7.6 +"""
7.7 +A simple example of parallel computation using a map and managed callables.
7.8 +"""
7.9 +
7.10 +import pprocess
7.11 +import time
7.12 +
7.13 +# Array size and a limit on the number of processes.
7.14 +
7.15 +N = 10
7.16 +limit = 10
7.17 +delay = 1
7.18 +
7.19 +# Work function and monitoring class.
7.20 +
7.21 +def calculate(i, j):
7.22 +
7.23 + """
7.24 + A supposedly time-consuming calculation on 'i' and 'j'.
7.25 + """
7.26 +
7.27 + time.sleep(delay)
7.28 + return i * N + j
7.29 +
7.30 +# Main program.
7.31 +
7.32 +if __name__ == "__main__":
7.33 +
7.34 + t = time.time()
7.35 +
7.36 + # Initialise the results using map with a limit on the number of
7.37 + # channels/processes.
7.38 +
7.39 + results = pprocess.Map(limit=limit)
7.40 +
7.41 + # Wrap the calculate function and manage it.
7.42 +
7.43 + calc = results.manage(pprocess.MakeParallel(calculate))
7.44 +
7.45 + # Perform the work.
7.46 +
7.47 + print "Calculating..."
7.48 + for i in range(0, N):
7.49 + for j in range(0, N):
7.50 + calc(i, j)
7.51 +
7.52 + # Show the results.
7.53 +
7.54 + for i in range(0, N):
7.55 + for result in results[i*N:i*N+N]:
7.56 + print result,
7.57 + print
7.58 +
7.59 + print "Time taken:", time.time() - t
7.60 +
7.61 +# vim: tabstop=4 expandtab shiftwidth=4
8.1 --- a/examples/simple_managed_queue.py Sat Sep 15 19:41:49 2007 +0000
8.2 +++ b/examples/simple_managed_queue.py Sat Sep 15 19:42:24 2007 +0000
8.3 @@ -28,11 +28,12 @@
8.4
8.5 if __name__ == "__main__":
8.6
8.7 + t = time.time()
8.8 +
8.9 # Initialise the communications queue with a limit on the number of
8.10 # channels/processes.
8.11
8.12 queue = pprocess.Queue(limit=limit)
8.13 - t = time.time()
8.14
8.15 # Initialise an array.
8.16
8.17 @@ -57,10 +58,11 @@
8.18
8.19 # Show the results.
8.20
8.21 - print "Time taken:", time.time() - t
8.22 for i in range(0, N):
8.23 for result in results[i*N:i*N+N]:
8.24 print result,
8.25 print
8.26
8.27 + print "Time taken:", time.time() - t
8.28 +
8.29 # vim: tabstop=4 expandtab shiftwidth=4
9.1 --- a/examples/simple_map.py Sat Sep 15 19:41:49 2007 +0000
9.2 +++ b/examples/simple_map.py Sat Sep 15 19:42:24 2007 +0000
9.3 @@ -41,10 +41,11 @@
9.4
9.5 # Show the results.
9.6
9.7 - print "Time taken:", time.time() - t
9.8 for i in range(0, N):
9.9 for result in results[i*N:i*N+N]:
9.10 print result,
9.11 print
9.12
9.13 + print "Time taken:", time.time() - t
9.14 +
9.15 # vim: tabstop=4 expandtab shiftwidth=4
10.1 --- a/examples/simple_pmap.py Sat Sep 15 19:41:49 2007 +0000
10.2 +++ b/examples/simple_pmap.py Sat Sep 15 19:42:24 2007 +0000
10.3 @@ -42,10 +42,11 @@
10.4
10.5 # Show the results.
10.6
10.7 - print "Time taken:", time.time() - t
10.8 for i in range(0, N):
10.9 for result in results[i*N:i*N+N]:
10.10 print result,
10.11 print
10.12
10.13 + print "Time taken:", time.time() - t
10.14 +
10.15 # vim: tabstop=4 expandtab shiftwidth=4
11.1 --- a/examples/simple_start.py Sat Sep 15 19:41:49 2007 +0000
11.2 +++ b/examples/simple_start.py Sat Sep 15 19:42:24 2007 +0000
11.3 @@ -18,8 +18,8 @@
11.4 def calculate(ch, i, j):
11.5
11.6 """
11.7 - A time-consuming calculation, using 'ch' to communicate with the parent
11.8 - process, with 'i' and 'j' as operands.
11.9 + A supposedly time-consuming calculation on 'i' and 'j', using 'ch' to
11.10 + communicate with the parent process.
11.11 """
11.12
11.13 time.sleep(delay)
11.14 @@ -37,16 +37,17 @@
11.15
11.16 if __name__ == "__main__":
11.17
11.18 + t = time.time()
11.19 +
11.20 # Initialise the communications exchange with a limit on the number of
11.21 # channels/processes.
11.22
11.23 exchange = MyExchange(limit=limit)
11.24 - t = time.time()
11.25
11.26 # Initialise an array - it is stored in the exchange to permit automatic
11.27 # assignment of values as the data arrives.
11.28
11.29 - exchange.D = [0] * N * N
11.30 + results = exchange.D = [0] * N * N
11.31
11.32 # Perform the work.
11.33
11.34 @@ -62,10 +63,11 @@
11.35
11.36 # Show the results.
11.37
11.38 - print "Time taken:", time.time() - t
11.39 for i in range(0, N):
11.40 - for result in exchange.D[i*N:i*N+N]:
11.41 + for result in results[i*N:i*N+N]:
11.42 print result,
11.43 print
11.44
11.45 + print "Time taken:", time.time() - t
11.46 +
11.47 # vim: tabstop=4 expandtab shiftwidth=4
12.1 --- a/examples/simple_start_queue.py Sat Sep 15 19:41:49 2007 +0000
12.2 +++ b/examples/simple_start_queue.py Sat Sep 15 19:42:24 2007 +0000
12.3 @@ -26,11 +26,12 @@
12.4
12.5 if __name__ == "__main__":
12.6
12.7 + t = time.time()
12.8 +
12.9 # Initialise the communications queue with a limit on the number of
12.10 # channels/processes.
12.11
12.12 queue = pprocess.Queue(limit=limit)
12.13 - t = time.time()
12.14
12.15 # Initialise an array.
12.16
12.17 @@ -54,10 +55,11 @@
12.18
12.19 # Show the results.
12.20
12.21 - print "Time taken:", time.time() - t
12.22 for i in range(0, N):
12.23 for result in results[i*N:i*N+N]:
12.24 print result,
12.25 print
12.26
12.27 + print "Time taken:", time.time() - t
12.28 +
12.29 # vim: tabstop=4 expandtab shiftwidth=4