|
@@
-116,10
+116,10
b' using IPython by following these steps:'
|
|
116
|
1. Copy the text files with the digits of pi
|
|
116
|
1. Copy the text files with the digits of pi
|
|
117
|
(ftp://pi.super-computing.org/.2/pi200m/) to the working directory of the
|
|
117
|
(ftp://pi.super-computing.org/.2/pi200m/) to the working directory of the
|
|
118
|
engines on the compute nodes.
|
|
118
|
engines on the compute nodes.
|
|
119
|
2. Use :command:`ipcluster` to start 15 engines. We used an 8 core cluster
|
|
119
|
2. Use :command:`ipcluster` to start 15 engines. We used an 8 core (2 quad
|
|
120
|
with hyperthreading enabled which makes the 8 cores looks like 16 (1
|
|
120
|
core CPUs) cluster with hyperthreading enabled which makes the 8 cores
|
|
121
|
controller + 15 engines) in the OS. However, the maximum speedup we can
|
|
121
|
looks like 16 (1 controller + 15 engines) in the OS. However, the maximum
|
|
122
|
observe is still only 8x.
|
|
122
|
speedup we can observe is still only 8x.
|
|
123
|
3. With the file :file:`parallelpi.py` in your current working directory, open
|
|
123
|
3. With the file :file:`parallelpi.py` in your current working directory, open
|
|
124
|
up IPython in pylab mode and type ``run parallelpi.py``.
|
|
124
|
up IPython in pylab mode and type ``run parallelpi.py``.
|
|
125
|
|
|
125
|
|
|
@@
-136,7
+136,9
b' calculation can also be run by simply typing the commands from'
|
|
136
|
In [1]: from IPython.kernel import client
|
|
136
|
In [1]: from IPython.kernel import client
|
|
137
|
2009-11-19 11:32:38-0800 [-] Log opened.
|
|
137
|
2009-11-19 11:32:38-0800 [-] Log opened.
|
|
138
|
|
|
138
|
|
|
139
|
# The MultiEngineClient allows us to use the engines interactively
|
|
139
|
# The MultiEngineClient allows us to use the engines interactively.
|
|
|
|
|
140
|
# We simply pass MultiEngineClient the name of the cluster profile we
|
|
|
|
|
141
|
# are using.
|
|
140
|
In [2]: mec = client.MultiEngineClient(profile='mycluster')
|
|
142
|
In [2]: mec = client.MultiEngineClient(profile='mycluster')
|
|
141
|
2009-11-19 11:32:44-0800 [-] Connecting [0]
|
|
143
|
2009-11-19 11:32:44-0800 [-] Connecting [0]
|
|
142
|
2009-11-19 11:32:44-0800 [Negotiation,client] Connected: ./ipcontroller-mec.furl
|
|
144
|
2009-11-19 11:32:44-0800 [Negotiation,client] Connected: ./ipcontroller-mec.furl
|
|
@@
-148,6
+150,7
b' calculation can also be run by simply typing the commands from'
|
|
148
|
|
|
150
|
|
|
149
|
In [5]: filestring = 'pi200m-ascii-%(i)02dof20.txt'
|
|
151
|
In [5]: filestring = 'pi200m-ascii-%(i)02dof20.txt'
|
|
150
|
|
|
152
|
|
|
|
|
|
153
|
# Create the list of files to process.
|
|
151
|
In [6]: files = [filestring % {'i':i} for i in range(1,16)]
|
|
154
|
In [6]: files = [filestring % {'i':i} for i in range(1,16)]
|
|
152
|
|
|
155
|
|
|
153
|
In [7]: files
|
|
156
|
In [7]: files
|
|
@@
-190,18
+193,6
b' compared to the 10,000 digit calculation.'
|
|
190
|
|
|
193
|
|
|
191
|
.. image:: two_digit_counts.*
|
|
194
|
.. image:: two_digit_counts.*
|
|
192
|
|
|
195
|
|
|
193
|
To conclude this example, we summarize the key features of IPython's parallel
|
|
|
|
|
194
|
architecture that this example demonstrates:
|
|
|
|
|
195
|
|
|
|
|
|
196
|
* Serial code can be parallelized often with only a few extra lines of code.
|
|
|
|
|
197
|
In this case we have used :meth:`MultiEngineClient.map`; the
|
|
|
|
|
198
|
:class:`MultiEngineClient` class has a number of other methods that provide
|
|
|
|
|
199
|
more fine grained control of the IPython cluster.
|
|
|
|
|
200
|
* The resulting parallel code can be run without ever leaving the IPython's
|
|
|
|
|
201
|
interactive shell.
|
|
|
|
|
202
|
* Any data computed in parallel can be explored interactively through
|
|
|
|
|
203
|
visualization or further numerical calculations.
|
|
|
|
|
204
|
|
|
|
|
|
205
|
|
|
196
|
|
|
206
|
Parallel options pricing
|
|
197
|
Parallel options pricing
|
|
207
|
========================
|
|
198
|
========================
|
|
@@
-217,33
+208,34
b' purposes: hedging against risk, speculation, etc.'
|
|
217
|
Much of modern finance is driven by the need to price these contracts
|
|
208
|
Much of modern finance is driven by the need to price these contracts
|
|
218
|
accurately based on what is known about the properties (such as volatility) of
|
|
209
|
accurately based on what is known about the properties (such as volatility) of
|
|
219
|
the underlying asset. One method of pricing options is to use a Monte Carlo
|
|
210
|
the underlying asset. One method of pricing options is to use a Monte Carlo
|
|
220
|
simulation of the underlying assets. In this example we use this approach to
|
|
211
|
simulation of the underlying asset price. In this example we use this approach
|
|
221
|
price both European and Asian (path dependent) options for various strike
|
|
212
|
to price both European and Asian (path dependent) options for various strike
|
|
222
|
prices and volatilities.
|
|
213
|
prices and volatilities.
|
|
223
|
|
|
214
|
|
|
224
|
The code for this example can be found in the :file:`docs/examples/kernel`
|
|
215
|
The code for this example can be found in the :file:`docs/examples/kernel`
|
|
225
|
directory of the IPython source.
|
|
216
|
directory of the IPython source. The function :func:`price_options` in
|
|
226
|
|
|
217
|
:file:`mcpricer.py` implements the basic Monte Carlo pricing algorithm using
|
|
227
|
The function :func:`price_options`, calculates the option prices for a single
|
|
218
|
the NumPy package and is shown here:
|
|
228
|
option (:file:`mcpricer.py`):
|
|
|
|
|
229
|
|
|
219
|
|
|
230
|
.. literalinclude:: ../../examples/kernel/mcpricer.py
|
|
220
|
.. literalinclude:: ../../examples/kernel/mcpricer.py
|
|
231
|
:language: python
|
|
221
|
:language: python
|
|
232
|
|
|
222
|
|
|
233
|
To run this code in parallel, we will use IPython's :class:`TaskClient`, which
|
|
223
|
To run this code in parallel, we will use IPython's :class:`TaskClient` class,
|
|
234
|
distributes work to the engines using dynamic load balancing. This client
|
|
224
|
which distributes work to the engines using dynamic load balancing. This
|
|
235
|
can be used along side the :class:`MultiEngineClient` shown in the previous
|
|
225
|
client can be used along side the :class:`MultiEngineClient` class shown in
|
|
236
|
example.
|
|
226
|
the previous example. The parallel calculation using :class:`TaskClient` can
|
|
237
|
|
|
227
|
be found in the file :file:`mcpricer.py`. The code in this file creates a
|
|
238
|
Here is the code that calls :func:`price_options` for a number of different
|
|
228
|
:class:`TaskClient` instance and then submits a set of tasks using
|
|
239
|
volatilities and strike prices in parallel:
|
|
229
|
:meth:`TaskClient.run` that calculate the option prices for different
|
|
|
|
|
230
|
volatilities and strike prices. The results are then plotted as a 2D contour
|
|
|
|
|
231
|
plot using Matplotlib.
|
|
240
|
|
|
232
|
|
|
241
|
.. literalinclude:: ../../examples/kernel/mcdriver.py
|
|
233
|
.. literalinclude:: ../../examples/kernel/mcdriver.py
|
|
242
|
:language: python
|
|
234
|
:language: python
|
|
243
|
|
|
235
|
|
|
244
|
To run this code in parallel, start an IPython cluster using
|
|
236
|
To use this code, start an IPython cluster using :command:`ipcluster`, open
|
|
245
|
:command:`ipcluster`, open IPython in the pylab mode with the file
|
|
237
|
IPython in the pylab mode with the file :file:`mcdriver.py` in your current
|
|
246
|
:file:`mcdriver.py` in your current working directory and then type:
|
|
238
|
working directory and then type:
|
|
247
|
|
|
239
|
|
|
248
|
.. sourcecode:: ipython
|
|
240
|
.. sourcecode:: ipython
|
|
249
|
|
|
241
|
|
|
@@
-252,7
+244,7
b' To run this code in parallel, start an IPython cluster using'
|
|
252
|
|
|
244
|
|
|
253
|
Once all the tasks have finished, the results can be plotted using the
|
|
245
|
Once all the tasks have finished, the results can be plotted using the
|
|
254
|
:func:`plot_options` function. Here we make contour plots of the Asian
|
|
246
|
:func:`plot_options` function. Here we make contour plots of the Asian
|
|
255
|
call and Asian put as function of the volatility and strike price:
|
|
247
|
call and Asian put options as function of the volatility and strike price:
|
|
256
|
|
|
248
|
|
|
257
|
.. sourcecode:: ipython
|
|
249
|
.. sourcecode:: ipython
|
|
258
|
|
|
250
|
|
|
@@
-263,8
+255,28
b' call and Asian put as function of the volatility and strike price:'
|
|
263
|
|
|
255
|
|
|
264
|
In [10]: plot_options(sigma_vals, K_vals, prices['aput'])
|
|
256
|
In [10]: plot_options(sigma_vals, K_vals, prices['aput'])
|
|
265
|
|
|
257
|
|
|
266
|
The plots generated by Matplotlib will look like this:
|
|
258
|
These results are shown in the two figures below. On a 8 core cluster the
|
|
|
|
|
259
|
entire calculation (10 strike prices, 10 volatilities, 100,000 paths for each)
|
|
|
|
|
260
|
took 30 seconds in parallel, giving a speedup of 7.7x, which is comparable
|
|
|
|
|
261
|
to the speedup observed in our previous example.
|
|
267
|
|
|
262
|
|
|
268
|
.. image:: asian_call.*
|
|
263
|
.. image:: asian_call.*
|
|
269
|
|
|
264
|
|
|
270
|
.. image:: asian_put.*
|
|
265
|
.. image:: asian_put.*
|
|
|
|
|
266
|
|
|
|
|
|
267
|
Conclusion
|
|
|
|
|
268
|
==========
|
|
|
|
|
269
|
|
|
|
|
|
270
|
To conclude these examples, we summarize the key features of IPython's
|
|
|
|
|
271
|
parallel architecture that have been demonstrated:
|
|
|
|
|
272
|
|
|
|
|
|
273
|
* Serial code can be parallelized often with only a few extra lines of code.
|
|
|
|
|
274
|
We have used the :class:`MultiEngineClient` and :class:`TaskClient` classes
|
|
|
|
|
275
|
for this purpose.
|
|
|
|
|
276
|
* The resulting parallel code can be run without ever leaving the IPython's
|
|
|
|
|
277
|
interactive shell.
|
|
|
|
|
278
|
* Any data computed in parallel can be explored interactively through
|
|
|
|
|
279
|
visualization or further numerical calculations.
|
|
|
|
|
280
|
* We have run these examples on a cluster running Windows HPC Server 2008.
|
|
|
|
|
281
|
IPython's built in support for the Windows HPC job scheduler makes it
|
|
|
|
|
282
|
easy to get started with IPython's parallel capabilities.
|