1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/examples/simple_pmap_iter.py Thu May 21 19:24:06 2009 +0200
1.3 @@ -0,0 +1,54 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +A simple example of parallel computation using map-style processing.
1.8 +"""
1.9 +
1.10 +import pprocess
1.11 +import time
1.12 +#import random
1.13 +
1.14 +# Array size and a limit on the number of processes.
1.15 +
1.16 +N = 10
1.17 +limit = 10
1.18 +delay = 1
1.19 +
1.20 +# Work function.
1.21 +
1.22 +def calculate(t):
1.23 +
1.24 + "A supposedly time-consuming calculation on 't'."
1.25 +
1.26 + i, j = t
1.27 + #time.sleep(delay * random.random())
1.28 + time.sleep(delay)
1.29 + return i * N + j
1.30 +
1.31 +# Main program.
1.32 +
1.33 +if __name__ == "__main__":
1.34 +
1.35 + t = time.time()
1.36 +
1.37 + # Initialise an array.
1.38 +
1.39 + sequence = []
1.40 + for i in range(0, N):
1.41 + for j in range(0, N):
1.42 + sequence.append((i, j))
1.43 +
1.44 + # Perform the work.
1.45 +
1.46 + results = pprocess.pmap(calculate, sequence, limit=limit)
1.47 +
1.48 + # Show the results.
1.49 +
1.50 + for i, result in enumerate(results):
1.51 + print result,
1.52 + if i % N == N - 1:
1.53 + print
1.54 +
1.55 + print "Time taken:", time.time() - t
1.56 +
1.57 +# vim: tabstop=4 expandtab shiftwidth=4