parallel_mpi.ipynb
223 lines
| 5.4 KiB
| text/plain
|
TextLexer
Fernando Perez
|
r4911 | { | |
MinRK
|
r5981 | "metadata": { | |
"name": "parallel_mpi" | |||
Brian Granger
|
r6035 | }, | |
"nbformat": 3, | |||
MinRK
|
r5981 | "worksheets": [ | |
{ | |||
"cells": [ | |||
{ | |||
Brian Granger
|
r6035 | "cell_type": "markdown", | |
MinRK
|
r5981 | "source": [ | |
Brian Granger
|
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
|
r5981 | "Once the cluster is running, we can connect to it and open a view into it:" | |
] | |||
Brian Granger
|
r6035 | }, | |
MinRK
|
r5981 | { | |
Brian Granger
|
r6035 | "cell_type": "code", | |
"collapsed": true, | |||
MinRK
|
r5981 | "input": [ | |
Brian Granger
|
r6035 | "from IPython.parallel import Client", | |
"c = Client()", | |||
MinRK
|
r5981 | "view = c[:]" | |
Brian Granger
|
r6035 | ], | |
"language": "python", | |||
"outputs": [], | |||
MinRK
|
r5981 | "prompt_number": 21 | |
Brian Granger
|
r6035 | }, | |
MinRK
|
r5981 | { | |
Brian Granger
|
r6035 | "cell_type": "markdown", | |
MinRK
|
r5981 | "source": [ | |
"Let's define a simple function that gets the MPI rank from each engine." | |||
] | |||
Brian Granger
|
r6035 | }, | |
MinRK
|
r5981 | { | |
Brian Granger
|
r6035 | "cell_type": "code", | |
"collapsed": true, | |||
MinRK
|
r5981 | "input": [ | |
Brian Granger
|
r6035 | "@view.remote(block=True)", | |
"def mpi_rank():", | |||
" from mpi4py import MPI", | |||
" comm = MPI.COMM_WORLD", | |||
MinRK
|
r5981 | " return comm.Get_rank()" | |
Brian Granger
|
r6035 | ], | |
"language": "python", | |||
"outputs": [], | |||
MinRK
|
r5981 | "prompt_number": 22 | |
Brian Granger
|
r6035 | }, | |
MinRK
|
r5981 | { | |
Brian Granger
|
r6035 | "cell_type": "code", | |
"collapsed": false, | |||
MinRK
|
r5981 | "input": [ | |
"mpi_rank()" | |||
Brian Granger
|
r6035 | ], | |
"language": "python", | |||
MinRK
|
r5981 | "outputs": [ | |
{ | |||
Brian Granger
|
r6035 | "output_type": "pyout", | |
"prompt_number": 23, | |||
MinRK
|
r5981 | "text": [ | |
"[3, 0, 2, 1]" | |||
] | |||
} | |||
Brian Granger
|
r6035 | ], | |
MinRK
|
r5981 | "prompt_number": 23 | |
Brian Granger
|
r6035 | }, | |
MinRK
|
r5981 | { | |
Brian Granger
|
r6035 | "cell_type": "markdown", | |
MinRK
|
r5981 | "source": [ | |
Brian Granger
|
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
|
r5981 | "within a single notebook here." | |
] | |||
Brian Granger
|
r6035 | }, | |
MinRK
|
r5981 | { | |
Brian Granger
|
r6035 | "cell_type": "code", | |
"collapsed": true, | |||
MinRK
|
r5981 | "input": [ | |
Brian Granger
|
r6035 | "%load_ext parallelmagic", | |
MinRK
|
r5981 | "view.activate()" | |
Brian Granger
|
r6035 | ], | |
"language": "python", | |||
"outputs": [], | |||
MinRK
|
r5981 | "prompt_number": 4 | |
Brian Granger
|
r6035 | }, | |
MinRK
|
r5981 | { | |
Brian Granger
|
r6035 | "cell_type": "markdown", | |
MinRK
|
r5981 | "source": [ | |
Brian Granger
|
r6035 | "Use the autopx magic to make the rest of this cell execute on the engines instead", | |
MinRK
|
r5981 | "of locally" | |
] | |||
Brian Granger
|
r6035 | }, | |
MinRK
|
r5981 | { | |
Brian Granger
|
r6035 | "cell_type": "code", | |
"collapsed": true, | |||
MinRK
|
r5981 | "input": [ | |
"view.block = True" | |||
Brian Granger
|
r6035 | ], | |
"language": "python", | |||
"outputs": [], | |||
MinRK
|
r5981 | "prompt_number": 24 | |
Brian Granger
|
r6035 | }, | |
MinRK
|
r5981 | { | |
Brian Granger
|
r6035 | "cell_type": "code", | |
"collapsed": false, | |||
MinRK
|
r5981 | "input": [ | |
"%autopx" | |||
Brian Granger
|
r6035 | ], | |
"language": "python", | |||
MinRK
|
r5981 | "outputs": [ | |
{ | |||
Brian Granger
|
r6035 | "output_type": "stream", | |
"stream": "stdout", | |||
MinRK
|
r5981 | "text": [ | |
"%autopx enabled" | |||
] | |||
} | |||
Brian Granger
|
r6035 | ], | |
MinRK
|
r5981 | "prompt_number": 32 | |
Brian Granger
|
r6035 | }, | |
MinRK
|
r5981 | { | |
Brian Granger
|
r6035 | "cell_type": "markdown", | |
MinRK
|
r5981 | "source": [ | |
"With autopx enabled, the next cell will actually execute *entirely on each engine*:" | |||
] | |||
Brian Granger
|
r6035 | }, | |
MinRK
|
r5981 | { | |
Brian Granger
|
r6035 | "cell_type": "code", | |
"collapsed": true, | |||
MinRK
|
r5981 | "input": [ | |
Brian Granger
|
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
|
r5981 | "assert data == (rank+1)**2, 'data=%s, rank=%s' % (data, rank)" | |
Brian Granger
|
r6035 | ], | |
"language": "python", | |||
"outputs": [], | |||
MinRK
|
r5981 | "prompt_number": 29 | |
Brian Granger
|
r6035 | }, | |
MinRK
|
r5981 | { | |
Brian Granger
|
r6035 | "cell_type": "markdown", | |
MinRK
|
r5981 | "source": [ | |
Brian Granger
|
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
|
r5981 | "First, don't forget to toggle off `autopx` mode so code runs again in the notebook:" | |
] | |||
Brian Granger
|
r6035 | }, | |
MinRK
|
r5981 | { | |
Brian Granger
|
r6035 | "cell_type": "code", | |
"collapsed": false, | |||
MinRK
|
r5981 | "input": [ | |
"%autopx" | |||
Brian Granger
|
r6035 | ], | |
"language": "python", | |||
MinRK
|
r5981 | "outputs": [ | |
{ | |||
Brian Granger
|
r6035 | "output_type": "stream", | |
"stream": "stdout", | |||
MinRK
|
r5981 | "text": [ | |
"%autopx disabled" | |||
] | |||
} | |||
Brian Granger
|
r6035 | ], | |
MinRK
|
r5981 | "prompt_number": 33 | |
Brian Granger
|
r6035 | }, | |
MinRK
|
r5981 | { | |
Brian Granger
|
r6035 | "cell_type": "code", | |
"collapsed": false, | |||
MinRK
|
r5981 | "input": [ | |
"view['data']" | |||
Brian Granger
|
r6035 | ], | |
"language": "python", | |||
MinRK
|
r5981 | "outputs": [ | |
{ | |||
Brian Granger
|
r6035 | "output_type": "pyout", | |
"prompt_number": 34, | |||
MinRK
|
r5981 | "text": [ | |
"[16, 1, 9, 4]" | |||
] | |||
} | |||
Brian Granger
|
r6035 | ], | |
MinRK
|
r5981 | "prompt_number": 34 | |
Brian Granger
|
r6035 | }, | |
MinRK
|
r5981 | { | |
Brian Granger
|
r6035 | "cell_type": "code", | |
"collapsed": true, | |||
"input": [], | |||
"language": "python", | |||
MinRK
|
r5981 | "outputs": [] | |
} | |||
] | |||
} | |||
] | |||
Fernando Perez
|
r4911 | } |