##// END OF EJS Templates
Embed image as default...
Embed image as default Embed image as default to work in qtconsole unless url kwarg is set explicitely fixes #1614

File last commit:

r6035:3077781f
r6529:19cfeefa
Show More
parallel_mpi.ipynb
223 lines | 5.4 KiB | text/plain | TextLexer
Fernando Perez
Add simple MPI parallel example notebook.
r4911 {
MinRK
regenerate example notebooks to remove transformed output
r5981 "metadata": {
"name": "parallel_mpi"
Brian Granger
Updating example notebooks to v3 format.
r6035 },
"nbformat": 3,
MinRK
regenerate example notebooks to remove transformed output
r5981 "worksheets": [
{
"cells": [
{
Brian Granger
Updating example notebooks to v3 format.
r6035 "cell_type": "markdown",
MinRK
regenerate example notebooks to remove transformed output
r5981 "source": [
Brian Granger
Updating example notebooks to v3 format.
r6035 "# Simple usage of a set of MPI engines",
"",
"This example assumes you've started a cluster of N engines (4 in this example) as part",
"of an MPI world. ",
"",
"Our documentation describes [how to create an MPI profile](http://ipython.org/ipython-doc/dev/parallel/parallel_process.html#using-ipcluster-in-mpiexec-mpirun-mode)",
"and explains [basic MPI usage of the IPython cluster](http://ipython.org/ipython-doc/dev/parallel/parallel_mpi.html).",
"",
"",
"For the simplest possible way to start 4 engines that belong to the same MPI world, ",
"you can run this in a terminal or antoher notebook:",
"",
"<pre>",
"ipcluster start --engines=MPI -n 4",
"</pre>",
"",
"Note: to run the above in a notebook, use a *new* notebook and prepend the command with `!`, but do not run",
"it in *this* notebook, as this command will block until you shut down the cluster. To stop the cluster, use ",
"the 'Interrupt' button on the left, which is the equivalent of sending `Ctrl-C` to the kernel.",
"",
MinRK
regenerate example notebooks to remove transformed output
r5981 "Once the cluster is running, we can connect to it and open a view into it:"
]
Brian Granger
Updating example notebooks to v3 format.
r6035 },
MinRK
regenerate example notebooks to remove transformed output
r5981 {
Brian Granger
Updating example notebooks to v3 format.
r6035 "cell_type": "code",
"collapsed": true,
MinRK
regenerate example notebooks to remove transformed output
r5981 "input": [
Brian Granger
Updating example notebooks to v3 format.
r6035 "from IPython.parallel import Client",
"c = Client()",
MinRK
regenerate example notebooks to remove transformed output
r5981 "view = c[:]"
Brian Granger
Updating example notebooks to v3 format.
r6035 ],
"language": "python",
"outputs": [],
MinRK
regenerate example notebooks to remove transformed output
r5981 "prompt_number": 21
Brian Granger
Updating example notebooks to v3 format.
r6035 },
MinRK
regenerate example notebooks to remove transformed output
r5981 {
Brian Granger
Updating example notebooks to v3 format.
r6035 "cell_type": "markdown",
MinRK
regenerate example notebooks to remove transformed output
r5981 "source": [
"Let's define a simple function that gets the MPI rank from each engine."
]
Brian Granger
Updating example notebooks to v3 format.
r6035 },
MinRK
regenerate example notebooks to remove transformed output
r5981 {
Brian Granger
Updating example notebooks to v3 format.
r6035 "cell_type": "code",
"collapsed": true,
MinRK
regenerate example notebooks to remove transformed output
r5981 "input": [
Brian Granger
Updating example notebooks to v3 format.
r6035 "@view.remote(block=True)",
"def mpi_rank():",
" from mpi4py import MPI",
" comm = MPI.COMM_WORLD",
MinRK
regenerate example notebooks to remove transformed output
r5981 " return comm.Get_rank()"
Brian Granger
Updating example notebooks to v3 format.
r6035 ],
"language": "python",
"outputs": [],
MinRK
regenerate example notebooks to remove transformed output
r5981 "prompt_number": 22
Brian Granger
Updating example notebooks to v3 format.
r6035 },
MinRK
regenerate example notebooks to remove transformed output
r5981 {
Brian Granger
Updating example notebooks to v3 format.
r6035 "cell_type": "code",
"collapsed": false,
MinRK
regenerate example notebooks to remove transformed output
r5981 "input": [
"mpi_rank()"
Brian Granger
Updating example notebooks to v3 format.
r6035 ],
"language": "python",
MinRK
regenerate example notebooks to remove transformed output
r5981 "outputs": [
{
Brian Granger
Updating example notebooks to v3 format.
r6035 "output_type": "pyout",
"prompt_number": 23,
MinRK
regenerate example notebooks to remove transformed output
r5981 "text": [
"[3, 0, 2, 1]"
]
}
Brian Granger
Updating example notebooks to v3 format.
r6035 ],
MinRK
regenerate example notebooks to remove transformed output
r5981 "prompt_number": 23
Brian Granger
Updating example notebooks to v3 format.
r6035 },
MinRK
regenerate example notebooks to remove transformed output
r5981 {
Brian Granger
Updating example notebooks to v3 format.
r6035 "cell_type": "markdown",
MinRK
regenerate example notebooks to remove transformed output
r5981 "source": [
Brian Granger
Updating example notebooks to v3 format.
r6035 "For interactive convenience, we load the parallel magic extensions and make this view",
"the active one for the automatic parallelism magics.",
"",
"This is not necessary and in production codes likely won't be used, as the engines will ",
"load their own MPI codes separately. But it makes it easy to illustrate everything from",
MinRK
regenerate example notebooks to remove transformed output
r5981 "within a single notebook here."
]
Brian Granger
Updating example notebooks to v3 format.
r6035 },
MinRK
regenerate example notebooks to remove transformed output
r5981 {
Brian Granger
Updating example notebooks to v3 format.
r6035 "cell_type": "code",
"collapsed": true,
MinRK
regenerate example notebooks to remove transformed output
r5981 "input": [
Brian Granger
Updating example notebooks to v3 format.
r6035 "%load_ext parallelmagic",
MinRK
regenerate example notebooks to remove transformed output
r5981 "view.activate()"
Brian Granger
Updating example notebooks to v3 format.
r6035 ],
"language": "python",
"outputs": [],
MinRK
regenerate example notebooks to remove transformed output
r5981 "prompt_number": 4
Brian Granger
Updating example notebooks to v3 format.
r6035 },
MinRK
regenerate example notebooks to remove transformed output
r5981 {
Brian Granger
Updating example notebooks to v3 format.
r6035 "cell_type": "markdown",
MinRK
regenerate example notebooks to remove transformed output
r5981 "source": [
Brian Granger
Updating example notebooks to v3 format.
r6035 "Use the autopx magic to make the rest of this cell execute on the engines instead",
MinRK
regenerate example notebooks to remove transformed output
r5981 "of locally"
]
Brian Granger
Updating example notebooks to v3 format.
r6035 },
MinRK
regenerate example notebooks to remove transformed output
r5981 {
Brian Granger
Updating example notebooks to v3 format.
r6035 "cell_type": "code",
"collapsed": true,
MinRK
regenerate example notebooks to remove transformed output
r5981 "input": [
"view.block = True"
Brian Granger
Updating example notebooks to v3 format.
r6035 ],
"language": "python",
"outputs": [],
MinRK
regenerate example notebooks to remove transformed output
r5981 "prompt_number": 24
Brian Granger
Updating example notebooks to v3 format.
r6035 },
MinRK
regenerate example notebooks to remove transformed output
r5981 {
Brian Granger
Updating example notebooks to v3 format.
r6035 "cell_type": "code",
"collapsed": false,
MinRK
regenerate example notebooks to remove transformed output
r5981 "input": [
"%autopx"
Brian Granger
Updating example notebooks to v3 format.
r6035 ],
"language": "python",
MinRK
regenerate example notebooks to remove transformed output
r5981 "outputs": [
{
Brian Granger
Updating example notebooks to v3 format.
r6035 "output_type": "stream",
"stream": "stdout",
MinRK
regenerate example notebooks to remove transformed output
r5981 "text": [
"%autopx enabled"
]
}
Brian Granger
Updating example notebooks to v3 format.
r6035 ],
MinRK
regenerate example notebooks to remove transformed output
r5981 "prompt_number": 32
Brian Granger
Updating example notebooks to v3 format.
r6035 },
MinRK
regenerate example notebooks to remove transformed output
r5981 {
Brian Granger
Updating example notebooks to v3 format.
r6035 "cell_type": "markdown",
MinRK
regenerate example notebooks to remove transformed output
r5981 "source": [
"With autopx enabled, the next cell will actually execute *entirely on each engine*:"
]
Brian Granger
Updating example notebooks to v3 format.
r6035 },
MinRK
regenerate example notebooks to remove transformed output
r5981 {
Brian Granger
Updating example notebooks to v3 format.
r6035 "cell_type": "code",
"collapsed": true,
MinRK
regenerate example notebooks to remove transformed output
r5981 "input": [
Brian Granger
Updating example notebooks to v3 format.
r6035 "from mpi4py import MPI",
"",
"comm = MPI.COMM_WORLD",
"size = comm.Get_size()",
"rank = comm.Get_rank()",
"",
"if rank == 0:",
" data = [(i+1)**2 for i in range(size)]",
"else:",
" data = None",
"data = comm.scatter(data, root=0)",
"",
MinRK
regenerate example notebooks to remove transformed output
r5981 "assert data == (rank+1)**2, 'data=%s, rank=%s' % (data, rank)"
Brian Granger
Updating example notebooks to v3 format.
r6035 ],
"language": "python",
"outputs": [],
MinRK
regenerate example notebooks to remove transformed output
r5981 "prompt_number": 29
Brian Granger
Updating example notebooks to v3 format.
r6035 },
MinRK
regenerate example notebooks to remove transformed output
r5981 {
Brian Granger
Updating example notebooks to v3 format.
r6035 "cell_type": "markdown",
MinRK
regenerate example notebooks to remove transformed output
r5981 "source": [
Brian Granger
Updating example notebooks to v3 format.
r6035 "Though the assertion at the end of the previous block validated the code, we can now ",
"pull the 'data' variable from all the nodes for local inspection.",
MinRK
regenerate example notebooks to remove transformed output
r5981 "First, don't forget to toggle off `autopx` mode so code runs again in the notebook:"
]
Brian Granger
Updating example notebooks to v3 format.
r6035 },
MinRK
regenerate example notebooks to remove transformed output
r5981 {
Brian Granger
Updating example notebooks to v3 format.
r6035 "cell_type": "code",
"collapsed": false,
MinRK
regenerate example notebooks to remove transformed output
r5981 "input": [
"%autopx"
Brian Granger
Updating example notebooks to v3 format.
r6035 ],
"language": "python",
MinRK
regenerate example notebooks to remove transformed output
r5981 "outputs": [
{
Brian Granger
Updating example notebooks to v3 format.
r6035 "output_type": "stream",
"stream": "stdout",
MinRK
regenerate example notebooks to remove transformed output
r5981 "text": [
"%autopx disabled"
]
}
Brian Granger
Updating example notebooks to v3 format.
r6035 ],
MinRK
regenerate example notebooks to remove transformed output
r5981 "prompt_number": 33
Brian Granger
Updating example notebooks to v3 format.
r6035 },
MinRK
regenerate example notebooks to remove transformed output
r5981 {
Brian Granger
Updating example notebooks to v3 format.
r6035 "cell_type": "code",
"collapsed": false,
MinRK
regenerate example notebooks to remove transformed output
r5981 "input": [
"view['data']"
Brian Granger
Updating example notebooks to v3 format.
r6035 ],
"language": "python",
MinRK
regenerate example notebooks to remove transformed output
r5981 "outputs": [
{
Brian Granger
Updating example notebooks to v3 format.
r6035 "output_type": "pyout",
"prompt_number": 34,
MinRK
regenerate example notebooks to remove transformed output
r5981 "text": [
"[16, 1, 9, 4]"
]
}
Brian Granger
Updating example notebooks to v3 format.
r6035 ],
MinRK
regenerate example notebooks to remove transformed output
r5981 "prompt_number": 34
Brian Granger
Updating example notebooks to v3 format.
r6035 },
MinRK
regenerate example notebooks to remove transformed output
r5981 {
Brian Granger
Updating example notebooks to v3 format.
r6035 "cell_type": "code",
"collapsed": true,
"input": [],
"language": "python",
MinRK
regenerate example notebooks to remove transformed output
r5981 "outputs": []
}
]
}
]
Fernando Perez
Add simple MPI parallel example notebook.
r4911 }