1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/docs/tutorial.xhtml Fri Sep 14 23:15:37 2007 +0000
1.3 @@ -0,0 +1,72 @@
1.4 +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
1.5 +<html xmlns="http://www.w3.org/1999/xhtml" lang="en-gb">
1.6 +<head>
1.7 + <meta content="text/html; charset=UTF-8" http-equiv="content-type" />
1.8 + <title>pprocess - Tutorial</title>
1.9 +</head>
1.10 +<body>
1.11 +
1.12 +<h1>pprocess - Tutorial</h1>
1.13 +
1.14 +<p>The <code>pprocess</code>
1.15 +module provides several mechanisms for running Python code concurrently
1.16 +in several processes. The most straightforward way of making a program
1.17 +parallel-aware - that is, where the program can take advantage of more
1.18 +than one processor to simultaneously process data - is to use the
1.19 +<code>pmap</code> function. Consider the following Python code:</p>
1.20 +
1.21 +<pre>
1.22 + t = time.time()
1.23 +
1.24 + sequence = []
1.25 + for i in range(0, N):
1.26 + for j in range(0, N):
1.27 + sequence.append((i, j))
1.28 +
1.29 + results = map(calculate, sequence)
1.30 +
1.31 + print "Time taken:", time.time() - t
1.32 + for i in range(0, N):
1.33 + for result in results[i*N:i*N+N]:
1.34 + print result,
1.35 + print
1.36 +</pre>
1.37 +
1.38 +<p>(This code in context with <code>import</code> statements and functions is found in the <code>examples/simple_map.py</code> file.)</p>
1.39 +
1.40 +<p>Here, we initialise a sequence of inputs for an N by N grid, perform a
1.41 +calculation on each element, then print the resulting sequence as a
1.42 +grid. Since the <code>map</code> function performs the calculations sequentially, even if the <code>calculate</code>
1.43 +function could be invoked independently for each input value, we have
1.44 +to wait for each calculation to complete before initiating a new
1.45 +one.</p>
1.46 +
1.47 +<p>In order to reduce the processing time - to speed the code up,
1.48 +in other words - we can make this code use several processes instead of
1.49 +just one. Here is the modified code:</p>
1.50 +
1.51 +<pre>
1.52 + t = time.time()
1.53 +
1.54 + sequence = []
1.55 + for i in range(0, N):
1.56 + for j in range(0, N):
1.57 + sequence.append((i, j))
1.58 +
1.59 + results = <strong>pprocess.pmap</strong>(calculate, sequence<strong>, limit=limit</strong>)
1.60 +
1.61 + print "Time taken:", time.time() - t
1.62 + for i in range(0, N):
1.63 + for result in results[i*N:i*N+N]:
1.64 + print result,
1.65 + print
1.66 +</pre>
1.67 +
1.68 +<p>(This code in context with <code>import</code> statements and functions is found in the <code>examples/simple_pmap.py</code> file.)</p>
1.69 +
1.70 +<p>By replacing usage of the <code>map</code> function with the <code>pprocess.pmap</code>
1.71 +function, and specifying the limit on the number of processes to be active at any
1.72 +given time, several calculations can now be performed in parallel.</p>
1.73 +
1.74 +</body>
1.75 +</html>