IPython Notebooks for Parallel Computing

This is simply an example notbook illustrating the "cluster" tab of the ipython notebook. You can use this as a starter project for developing simple parallel codes in a notebook.

First start up a cluster using the "Cluster" tab. Then try these cells.


In [1]:
from IPython.parallel import Client
rc=Client()
view = rc[:]
print rc.ids


[0, 1, 2, 3]

Use the %px magic to import methods on the nodes


In [2]:
%px from pylab import arccos, rand
from numpy import array

Next, use the %%px magic to define a function on the nodes


In [3]:
%%px

def f(N):
    y=arccos(1.0-2*rand(N))
    return 2.0*y.sum()/N

In [4]:
x = view.map(lambda N: f(N), 10*[1000000]).result
x


Out[4]:
[3.1405796297219211,
 3.1426586695423637,
 3.1398369967668183,
 3.1420250074438467,
 3.1427624932300482,
 3.1410966404557557,
 3.1431271009553994,
 3.141158465309319,
 3.1426331913328771,
 3.1436420212612775]

In [5]:
array(x).sum()/len(x)


Out[5]:
3.1419520216019627

In [ ]: