In [1]:
    
import numpy as np
from ipyparallel import Client  # IPython.parallel before IPython 4.0
    
In [2]:
    
rc = Client()
    
In [3]:
    
rc.ids
    
    Out[3]:
In [4]:
    
%px import os, time
    
In [5]:
    
%px print(os.getpid())
    
    Out[5]:
In [6]:
    
%%px --targets :-1
print(os.getpid())
    
    Out[6]:
In [7]:
    
view = rc[:-1]
view
    
    Out[7]:
In [8]:
    
v = rc.load_balanced_view()
    
In [9]:
    
def sample(n):
    import numpy as np
    # Random coordinates.
    x, y = np.random.rand(2, n)
    # Square distances to the origin.
    r_square = x ** 2 + y ** 2
    # Number of points in the quarter disc.
    return (r_square <= 1).sum()
    
In [10]:
    
def pi(n_in, n):
    return 4. * float(n_in) / n
    
In [11]:
    
n = 100000000
    
In [12]:
    
pi(sample(n), n)
    
    Out[12]:
In [13]:
    
%timeit pi(sample(n), n)
    
    Out[13]:
In [14]:
    
args = [n // 100] * 100
    
In [15]:
    
ar = v.map(sample, args)
    
In [16]:
    
ar.ready(), ar.progress
    
    Out[16]:
In [17]:
    
ar.elapsed, ar.serial_time
    
    Out[17]:
In [18]:
    
pi(np.sum(ar.result), n)
    
    Out[18]: