paulb@96 | 1 | #!/usr/bin/env python |
paulb@96 | 2 | |
paulb@96 | 3 | """ |
paulb@96 | 4 | A simple example of parallel computation using exchanges and managed callables. |
paulb@96 | 5 | """ |
paulb@96 | 6 | |
paulb@96 | 7 | import pprocess |
paulb@96 | 8 | import time |
paulb@112 | 9 | #import random |
paulb@96 | 10 | |
paulb@96 | 11 | # Array size and a limit on the number of processes. |
paulb@96 | 12 | |
paulb@96 | 13 | N = 10 |
paulb@96 | 14 | limit = 10 |
paulb@96 | 15 | delay = 1 |
paulb@96 | 16 | |
paulb@96 | 17 | # Work function and monitoring class. |
paulb@96 | 18 | |
paulb@106 | 19 | def calculate(i, j): |
paulb@96 | 20 | |
paulb@96 | 21 | """ |
paulb@106 | 22 | A supposedly time-consuming calculation on 'i' and 'j'. |
paulb@96 | 23 | """ |
paulb@96 | 24 | |
paulb@112 | 25 | #time.sleep(delay * random.random()) |
paulb@96 | 26 | time.sleep(delay) |
paulb@106 | 27 | return (i, j, i * N + j) |
paulb@96 | 28 | |
paulb@96 | 29 | class MyExchange(pprocess.Exchange): |
paulb@96 | 30 | |
paulb@96 | 31 | "Parallel convenience class containing the array assignment operation." |
paulb@96 | 32 | |
paulb@96 | 33 | def store_data(self, ch): |
paulb@96 | 34 | i, j, result = ch.receive() |
paulb@96 | 35 | self.D[i*N+j] = result |
paulb@96 | 36 | |
paulb@96 | 37 | # Main program. |
paulb@96 | 38 | |
paulb@96 | 39 | if __name__ == "__main__": |
paulb@96 | 40 | |
paulb@109 | 41 | t = time.time() |
paulb@109 | 42 | |
paulb@96 | 43 | # Initialise the communications exchange with a limit on the number of |
paulb@96 | 44 | # channels/processes. |
paulb@96 | 45 | |
paulb@96 | 46 | exchange = MyExchange(limit=limit) |
paulb@96 | 47 | |
paulb@96 | 48 | # Initialise an array - it is stored in the exchange to permit automatic |
paulb@96 | 49 | # assignment of values as the data arrives. |
paulb@96 | 50 | |
paulb@109 | 51 | results = exchange.D = [0] * N * N |
paulb@106 | 52 | |
paulb@106 | 53 | # Wrap the calculate function and manage it. |
paulb@106 | 54 | |
paulb@106 | 55 | calc = exchange.manage(pprocess.MakeParallel(calculate)) |
paulb@106 | 56 | |
paulb@106 | 57 | # Perform the work. |
paulb@96 | 58 | |
paulb@96 | 59 | print "Calculating..." |
paulb@96 | 60 | for i in range(0, N): |
paulb@96 | 61 | for j in range(0, N): |
paulb@96 | 62 | calc(i, j) |
paulb@96 | 63 | |
paulb@106 | 64 | # Wait for the results. |
paulb@106 | 65 | |
paulb@96 | 66 | print "Finishing..." |
paulb@96 | 67 | exchange.finish() |
paulb@96 | 68 | |
paulb@106 | 69 | # Show the results. |
paulb@106 | 70 | |
paulb@96 | 71 | for i in range(0, N): |
paulb@109 | 72 | for result in results[i*N:i*N+N]: |
paulb@96 | 73 | print result, |
paulb@96 | 74 | print |
paulb@96 | 75 | |
paulb@109 | 76 | print "Time taken:", time.time() - t |
paulb@109 | 77 | |
paulb@96 | 78 | # vim: tabstop=4 expandtab shiftwidth=4 |