IPython Documentation

Table Of Contents

Previous topic

IPython’s Direct interface

Next topic

The IPython task interface

This Page

Multiplexing Exercises

Monte Carlo π

A simple toy problem to get a handle on multiple engines is a Monte Carlo approximation of π.

Let’s say we have a dartboard with a round target inscribed on a square board. If you threw darts randomly, and they land evenly distributed on the square board, how many darts would you expect to hit the target?

_images/darts.png
from random import random
from math import pi

def mcpi(nsamples):
    s = 0
    for i in xrange(nsamples):
        x = random()
        y = random()
        if x*x + y*y <= 1:
            s+=1
    return 4.*s/nsamples

def multi_mcpi(dview, nsamples):
    raise NotImplementedError("you write this")

It takes a lot of samples to get a good approximation. Can you write a function that will use your engines to break up the work?

mcpi solution

Remote Iterators

Can you create an object that iterates through a remote iterable?

In [10]: dview['a'] = range(5)

In [11]: ra = remote_iterator(dview, 'a')

remote iter solution