paulb@96 | 1 | #!/usr/bin/env python |
paulb@96 | 2 | |
paulb@96 | 3 | """ |
paulb@96 | 4 | A simple example of serial computation using map-style processing. |
paulb@96 | 5 | See the simple_pmap.py program for the corresponding parallel version. |
paulb@96 | 6 | """ |
paulb@96 | 7 | |
paulb@96 | 8 | import time |
paulb@96 | 9 | |
paulb@96 | 10 | # Array size. |
paulb@96 | 11 | |
paulb@96 | 12 | N = 10 |
paulb@96 | 13 | delay = 1 |
paulb@96 | 14 | |
paulb@96 | 15 | # Work function. |
paulb@96 | 16 | |
paulb@96 | 17 | def calculate(t): |
paulb@96 | 18 | |
paulb@96 | 19 | "A supposedly time-consuming calculation on 't'." |
paulb@96 | 20 | |
paulb@96 | 21 | i, j = t |
paulb@96 | 22 | time.sleep(delay) |
paulb@96 | 23 | return i * N + j |
paulb@96 | 24 | |
paulb@96 | 25 | # Main program. |
paulb@96 | 26 | |
paulb@96 | 27 | if __name__ == "__main__": |
paulb@96 | 28 | |
paulb@96 | 29 | t = time.time() |
paulb@96 | 30 | |
paulb@106 | 31 | # Initialise an array. |
paulb@106 | 32 | |
paulb@96 | 33 | sequence = [] |
paulb@96 | 34 | for i in range(0, N): |
paulb@96 | 35 | for j in range(0, N): |
paulb@96 | 36 | sequence.append((i, j)) |
paulb@96 | 37 | |
paulb@106 | 38 | # Perform the work. |
paulb@106 | 39 | |
paulb@96 | 40 | results = map(calculate, sequence) |
paulb@96 | 41 | |
paulb@106 | 42 | # Show the results. |
paulb@106 | 43 | |
paulb@96 | 44 | print "Time taken:", time.time() - t |
paulb@96 | 45 | for i in range(0, N): |
paulb@96 | 46 | for result in results[i*N:i*N+N]: |
paulb@96 | 47 | print result, |
paulb@96 | 48 | print |
paulb@96 | 49 | |
paulb@96 | 50 | # vim: tabstop=4 expandtab shiftwidth=4 |