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