1.1 --- a/examples/simple.py Sun Jul 15 23:09:04 2007 +0000
1.2 +++ b/examples/simple.py Sun Jul 15 23:09:29 2007 +0000
1.3 @@ -1,7 +1,9 @@
1.4 #!/usr/bin/env python
1.5
1.6 """
1.7 -A simple example of parallel computation.
1.8 +A simple example of parallel computation using message exchanges directly,
1.9 +exchanges and managed callables, and map-style processing with parallel-enabled
1.10 +functions.
1.11 """
1.12
1.13 import pprocess
1.14 @@ -19,6 +21,8 @@
1.15
1.16 return [list(x) for x in [(0,) * n] * n]
1.17
1.18 +# Work function and monitoring class.
1.19 +
1.20 def calculate(ch, i, j):
1.21
1.22 """
1.23 @@ -37,6 +41,23 @@
1.24 i, j, result = ch.receive()
1.25 self.D[i][j] = result
1.26
1.27 +# Alternative work function.
1.28 +
1.29 +def calculate_seq(i, j):
1.30 +
1.31 + """
1.32 + A version of 'calculate' with a simpler result suitable for sequential
1.33 + collection. It accepts no channel, pretending to be a function used in a
1.34 + non-parallel context.
1.35 + """
1.36 +
1.37 + time.sleep(delay)
1.38 + return i * N + j
1.39 +
1.40 +# The alternative work function converted into a parallel-enabled version.
1.41 +
1.42 +calculate2 = pprocess.MakeParallel(calculate_seq)
1.43 +
1.44 # Main program.
1.45
1.46 if __name__ == "__main__":
1.47 @@ -68,4 +89,39 @@
1.48 for row in exchange.D:
1.49 print row
1.50
1.51 + # Try again with managed callables.
1.52 +
1.53 + exchange.D = make_array(N)
1.54 + calc = exchange.manage(calculate)
1.55 +
1.56 + print "Calculating..."
1.57 + for i in range(0, N):
1.58 + for j in range(0, N):
1.59 + calc(i, j)
1.60 + print i, j
1.61 +
1.62 + print "Finishing..."
1.63 + exchange.finish()
1.64 +
1.65 + print
1.66 + for row in exchange.D:
1.67 + print row
1.68 +
1.69 + # Try again with a map, building the results sequentially.
1.70 +
1.71 + mymap = pprocess.pmap
1.72 + results = []
1.73 +
1.74 + print "Calculating..."
1.75 + for i in range(0, N):
1.76 + sequence = []
1.77 + for j in range(0, N):
1.78 + sequence.append((i, j))
1.79 + results.append(mymap(calculate2, sequence))
1.80 + print i
1.81 +
1.82 + print
1.83 + for row in results:
1.84 + print row
1.85 +
1.86 # vim: tabstop=4 expandtab shiftwidth=4