##// END OF EJS Templates
Add simple MPI parallel example notebook.
Fernando Perez -
Show More
@@ -0,0 +1,139 b''
1 {
2 "worksheets": [
3 {
4 "cells": [
5 {
6 "source": "# Simple usage of a set of MPI engines\n\nThis example assumes you've started a cluster of N engines (4 in this example) as part\nof an MPI world. \n\nOur documentation describes [how to create an MPI profile](http://ipython.org/ipython-doc/dev/parallel/parallel_process.html#using-ipcluster-in-mpiexec-mpirun-mode)\nand explains [basic MPI usage of the IPython cluster](http://ipython.org/ipython-doc/dev/parallel/parallel_mpi.html).\n\n\nFor the simplest possible way to start 4 engines that belong to the same MPI world, \nyou can run this in a terminal or antoher notebook:\n\n<pre>\nipcluster start --engines=MPIExecEngineSetLauncher -n 4\n</pre>\n\nNote: to run the above in a notebook, use a *new* notebook and prepend the command with `!`, but do not run\nit in *this* notebook, as this command will block until you shut down the cluster. To stop the cluster, use \nthe 'Interrupt' button on the left, which is the equivalent of sending `Ctrl-C` to the kernel.\n\nOnce the cluster is running, we can connect to it and open a view into it:",
7 "cell_type": "markdown"
8 },
9 {
10 "cell_type": "code",
11 "language": "python",
12 "outputs": [],
13 "collapsed": true,
14 "prompt_number": 21,
15 "input": "from IPython.parallel import Client\nc = Client()\nview = c[:]"
16 },
17 {
18 "source": "Let's define a simple function that ",
19 "cell_type": "markdown"
20 },
21 {
22 "cell_type": "code",
23 "language": "python",
24 "outputs": [],
25 "collapsed": true,
26 "prompt_number": 22,
27 "input": "@view.remote(block=True)\ndef mpi_rank():\n from mpi4py import MPI\n comm = MPI.COMM_WORLD\n return comm.Get_rank()"
28 },
29 {
30 "cell_type": "code",
31 "language": "python",
32 "outputs": [
33 {
34 "output_type": "pyout",
35 "prompt_number": 23,
36 "text": "[3, 0, 2, 1]"
37 }
38 ],
39 "collapsed": false,
40 "prompt_number": 23,
41 "input": "mpi_rank()"
42 },
43 {
44 "source": "For interactive convenience, we load the parallel magic extensions and make this view\nthe active one for the automatic parallelism magics.\n\nThis is not necessary and in production codes likely won't be used, as the engines will \nload their own MPI codes separately. But it makes it easy to illustrate everything from\nwithin a single notebook here.",
45 "cell_type": "markdown"
46 },
47 {
48 "cell_type": "code",
49 "language": "python",
50 "outputs": [],
51 "collapsed": true,
52 "prompt_number": 4,
53 "input": "%load_ext parallelmagic\nview.activate()"
54 },
55 {
56 "source": "Use the autopx magic to make the rest of this cell execute on the engines instead\nof locally",
57 "cell_type": "markdown"
58 },
59 {
60 "cell_type": "code",
61 "language": "python",
62 "outputs": [],
63 "collapsed": true,
64 "prompt_number": 24,
65 "input": "view.block = True"
66 },
67 {
68 "cell_type": "code",
69 "language": "python",
70 "outputs": [
71 {
72 "output_type": "stream",
73 "stream": "stdout",
74 "text": "%autopx enabled\n\n"
75 }
76 ],
77 "collapsed": false,
78 "prompt_number": 32,
79 "input": "%autopx"
80 },
81 {
82 "source": "With autopx enabled, the next cell will actually execute *entirely on each engine*:",
83 "cell_type": "markdown"
84 },
85 {
86 "cell_type": "code",
87 "language": "python",
88 "outputs": [],
89 "collapsed": true,
90 "prompt_number": 29,
91 "input": "from mpi4py import MPI\n\ncomm = MPI.COMM_WORLD\nsize = comm.Get_size()\nrank = comm.Get_rank()\n\nif rank == 0:\n data = [(i+1)**2 for i in range(size)]\nelse:\n data = None\ndata = comm.scatter(data, root=0)\n\nassert data == (rank+1)**2, 'data=%s, rank=%s' % (data, rank)"
92 },
93 {
94 "source": "Though the assertion at the end of the previous block validated the code, we can now \npull the 'data' variable from all the nodes for local inspection.\nFirst, don't forget to toggle off `autopx` mode so code runs again in the notebook:\n",
95 "cell_type": "markdown"
96 },
97 {
98 "cell_type": "code",
99 "language": "python",
100 "outputs": [
101 {
102 "output_type": "stream",
103 "stream": "stdout",
104 "text": "%autopx disabled\n\n"
105 }
106 ],
107 "collapsed": false,
108 "prompt_number": 33,
109 "input": "%autopx"
110 },
111 {
112 "cell_type": "code",
113 "language": "python",
114 "outputs": [
115 {
116 "output_type": "pyout",
117 "prompt_number": 34,
118 "text": "[16, 1, 9, 4]"
119 }
120 ],
121 "collapsed": false,
122 "prompt_number": 34,
123 "input": "view['data']"
124 },
125 {
126 "input": "",
127 "cell_type": "code",
128 "collapsed": true,
129 "language": "python",
130 "outputs": []
131 }
132 ]
133 }
134 ],
135 "metadata": {
136 "name": "parallel_mpi"
137 },
138 "nbformat": 2
139 } No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now