# HG changeset patch # User paulb # Date 1189900106 0 # Node ID 6a3923cf82609ff0564dca1888459e5c3e3efc37 # Parent 7022b1c3ec9e8325af06d34f7c2ef33f962945cd [project @ 2007-09-15 23:48:26 by paulb] Added a combination of Map and create. Added commented-out statements for random delays. Changed simple2.py to use "positional" results. Other minor fixes. diff -r 7022b1c3ec9e -r 6a3923cf8260 examples/simple.py --- a/examples/simple.py Sat Sep 15 23:47:05 2007 +0000 +++ b/examples/simple.py Sat Sep 15 23:48:26 2007 +0000 @@ -5,6 +5,7 @@ """ import time +#import random # Array size. @@ -26,6 +27,7 @@ print "Calculating..." for i in range(0, N): for j in range(0, N): + #time.sleep(delay * random.random()) time.sleep(delay) results[i*N+j] = i * N + j diff -r 7022b1c3ec9e -r 6a3923cf8260 examples/simple1.py --- a/examples/simple1.py Sat Sep 15 23:47:05 2007 +0000 +++ b/examples/simple1.py Sat Sep 15 23:48:26 2007 +0000 @@ -6,6 +6,7 @@ """ import time +#import random # Array size. @@ -20,6 +21,7 @@ A supposedly time-consuming calculation on 'i' and 'j'. """ + #time.sleep(delay * random.random()) time.sleep(delay) return i * N + j diff -r 7022b1c3ec9e -r 6a3923cf8260 examples/simple2.py --- a/examples/simple2.py Sat Sep 15 23:47:05 2007 +0000 +++ b/examples/simple2.py Sat Sep 15 23:48:26 2007 +0000 @@ -5,6 +5,7 @@ """ import time +#import random # Array size. @@ -19,8 +20,9 @@ A supposedly time-consuming calculation on 'i' and 'j'. """ + #time.sleep(delay * random.random()) time.sleep(delay) - return i * N + j + return (i, j, i * N + j) # Main program. @@ -37,7 +39,8 @@ print "Calculating..." for i in range(0, N): for j in range(0, N): - results[i*N+j] = calculate(i, j) + i2, j2, result = calculate(i, j) + results[i2*N+j2] = result # Show the results. diff -r 7022b1c3ec9e -r 6a3923cf8260 examples/simple_create.py --- a/examples/simple_create.py Sat Sep 15 23:47:05 2007 +0000 +++ b/examples/simple_create.py Sat Sep 15 23:48:26 2007 +0000 @@ -10,6 +10,7 @@ import pprocess import time +#import random # Array size and a limit on the number of processes. @@ -52,6 +53,7 @@ if ch: try: # Calculation work. + #time.sleep(delay * random.random()) time.sleep(delay) ch.send((i, j, i * N + j)) diff -r 7022b1c3ec9e -r 6a3923cf8260 examples/simple_create_map.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/examples/simple_create_map.py Sat Sep 15 23:48:26 2007 +0000 @@ -0,0 +1,59 @@ +#!/usr/bin/env python + +""" +A simple example of parallel computation using a map and the create function. + +NOTE: We could use the with statement in the innermost loop to package the +NOTE: try...finally functionality. +""" + +import pprocess +import time +#import random + +# Array size and a limit on the number of processes. + +N = 10 +limit = 10 +delay = 1 + +# Main program. + +if __name__ == "__main__": + + t = time.time() + + # Initialise the results using a map with a limit on the number of + # channels/processes. + + results = pprocess.Map(limit=limit) + + # Perform the work. + # NOTE: Could use the with statement in the loop to package the + # NOTE: try...finally functionality. + + print "Calculating..." + for i in range(0, N): + for j in range(0, N): + ch = results.create() + if ch: + try: # Calculation work. + + #time.sleep(delay * random.random()) + time.sleep(delay) + ch.send(i * N + j) + + finally: # Important finalisation. + + pprocess.exit(ch) + + # Show the results. + + for i in range(0, N): + for result in results[i*N:i*N+N]: + print result, + print + + print "Time taken:", time.time() - t + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 7022b1c3ec9e -r 6a3923cf8260 examples/simple_create_queue.py --- a/examples/simple_create_queue.py Sat Sep 15 23:47:05 2007 +0000 +++ b/examples/simple_create_queue.py Sat Sep 15 23:48:26 2007 +0000 @@ -10,6 +10,7 @@ import pprocess import time +#import random # Array size and a limit on the number of processes. @@ -43,6 +44,7 @@ if ch: try: # Calculation work. + #time.sleep(delay * random.random()) time.sleep(delay) ch.send((i, j, i * N + j)) diff -r 7022b1c3ec9e -r 6a3923cf8260 examples/simple_managed.py --- a/examples/simple_managed.py Sat Sep 15 23:47:05 2007 +0000 +++ b/examples/simple_managed.py Sat Sep 15 23:48:26 2007 +0000 @@ -6,6 +6,7 @@ import pprocess import time +#import random # Array size and a limit on the number of processes. @@ -21,6 +22,7 @@ A supposedly time-consuming calculation on 'i' and 'j'. """ + #time.sleep(delay * random.random()) time.sleep(delay) return (i, j, i * N + j) diff -r 7022b1c3ec9e -r 6a3923cf8260 examples/simple_managed_map.py --- a/examples/simple_managed_map.py Sat Sep 15 23:47:05 2007 +0000 +++ b/examples/simple_managed_map.py Sat Sep 15 23:48:26 2007 +0000 @@ -6,6 +6,7 @@ import pprocess import time +#import random # Array size and a limit on the number of processes. @@ -21,6 +22,7 @@ A supposedly time-consuming calculation on 'i' and 'j'. """ + #time.sleep(delay * random.random()) time.sleep(delay) return i * N + j @@ -30,7 +32,7 @@ t = time.time() - # Initialise the results using map with a limit on the number of + # Initialise the results using a map with a limit on the number of # channels/processes. results = pprocess.Map(limit=limit) diff -r 7022b1c3ec9e -r 6a3923cf8260 examples/simple_managed_queue.py --- a/examples/simple_managed_queue.py Sat Sep 15 23:47:05 2007 +0000 +++ b/examples/simple_managed_queue.py Sat Sep 15 23:48:26 2007 +0000 @@ -6,6 +6,7 @@ import pprocess import time +#import random # Array size and a limit on the number of processes. @@ -21,6 +22,7 @@ A supposedly time-consuming calculation on 'i' and 'j'. """ + #time.sleep(delay * random.random()) time.sleep(delay) return (i, j, i * N + j) diff -r 7022b1c3ec9e -r 6a3923cf8260 examples/simple_map.py --- a/examples/simple_map.py Sat Sep 15 23:47:05 2007 +0000 +++ b/examples/simple_map.py Sat Sep 15 23:48:26 2007 +0000 @@ -6,6 +6,7 @@ """ import time +#import random # Array size. @@ -19,6 +20,7 @@ "A supposedly time-consuming calculation on 't'." i, j = t + #time.sleep(delay * random.random()) time.sleep(delay) return i * N + j diff -r 7022b1c3ec9e -r 6a3923cf8260 examples/simple_pmap.py --- a/examples/simple_pmap.py Sat Sep 15 23:47:05 2007 +0000 +++ b/examples/simple_pmap.py Sat Sep 15 23:48:26 2007 +0000 @@ -6,6 +6,7 @@ import pprocess import time +#import random # Array size and a limit on the number of processes. @@ -20,6 +21,7 @@ "A supposedly time-consuming calculation on 't'." i, j = t + #time.sleep(delay * random.random()) time.sleep(delay) return i * N + j diff -r 7022b1c3ec9e -r 6a3923cf8260 examples/simple_start.py --- a/examples/simple_start.py Sat Sep 15 23:47:05 2007 +0000 +++ b/examples/simple_start.py Sat Sep 15 23:48:26 2007 +0000 @@ -6,6 +6,7 @@ import pprocess import time +#import random # Array size and a limit on the number of processes. @@ -22,6 +23,7 @@ communicate with the parent process. """ + #time.sleep(delay * random.random()) time.sleep(delay) ch.send((i, j, i * N + j)) diff -r 7022b1c3ec9e -r 6a3923cf8260 examples/simple_start_queue.py --- a/examples/simple_start_queue.py Sat Sep 15 23:47:05 2007 +0000 +++ b/examples/simple_start_queue.py Sat Sep 15 23:48:26 2007 +0000 @@ -6,6 +6,7 @@ import pprocess import time +#import random # Array size and a limit on the number of processes. @@ -19,6 +20,7 @@ "A supposedly time-consuming calculation on 'i' and 'j'." + #time.sleep(delay * random.random()) time.sleep(delay) return (i, j, i * N + j)