##// END OF EJS Templates
update Parallel Magics notebook
MinRK -
Show More
@@ -1,323 +1,334 b''
1 {
1 {
2 "metadata": {
2 "metadata": {
3 "name": "Parallel Magics"
3 "name": "Parallel Magics"
4 },
4 },
5 "nbformat": 3,
5 "nbformat": 3,
6 "worksheets": [
6 "worksheets": [
7 {
7 {
8 "cells": [
8 "cells": [
9 {
9 {
10 "cell_type": "heading",
10 "cell_type": "heading",
11 "level": 1,
11 "level": 1,
12 "source": [
12 "source": [
13 "Using Parallel Magics"
13 "Using Parallel Magics"
14 ]
14 ]
15 },
15 },
16 {
16 {
17 "cell_type": "markdown",
17 "cell_type": "markdown",
18 "source": [
18 "source": [
19 "IPython has a few magics for working with your engines.",
19 "IPython has a few magics for working with your engines.\n",
20 "",
20 "\n",
21 "This assumes you have started an IPython cluster, either with the notebook interface,",
21 "This assumes you have started an IPython cluster, either with the notebook interface,\n",
22 "or the `ipcluster/controller/engine` commands."
22 "or the `ipcluster/controller/engine` commands."
23 ]
23 ]
24 },
24 },
25 {
25 {
26 "cell_type": "code",
26 "cell_type": "code",
27 "collapsed": false,
28 "input": [
27 "input": [
29 "from IPython import parallel",
28 "from IPython import parallel\n",
30 "rc = parallel.Client()",
29 "rc = parallel.Client()\n",
31 "dv = rc[:]",
30 "dv = rc[:]\n",
32 "dv.block = True",
31 "rc.ids"
33 "dv"
34 ],
32 ],
35 "language": "python",
33 "language": "python",
36 "outputs": []
34 "outputs": []
37 },
35 },
38 {
36 {
39 "cell_type": "markdown",
37 "cell_type": "markdown",
40 "source": [
38 "source": [
41 "The parallel magics come from the `parallelmagics` IPython extension.",
39 "Creating a Client registers the parallel magics `%px`, `%%px`, `%pxresult`, and `%autopx`. \n",
42 "The magics are set to work with a particular View object,",
40 "These magics are initially associated with a DirectView always associated with all currently registered engines."
43 "so to activate them, you call the `activate()` method on a particular view:"
44 ]
41 ]
45 },
42 },
46 {
43 {
47 "cell_type": "code",
48 "collapsed": true,
49 "input": [
50 "dv.activate()"
51 ],
52 "language": "python",
53 "outputs": []
54 },
55 {
56 "cell_type": "markdown",
44 "cell_type": "markdown",
57 "source": [
45 "source": [
58 "Now we can execute code remotely with `%px`:"
46 "Now we can execute code remotely with `%px`:"
59 ]
47 ]
60 },
48 },
61 {
49 {
62 "cell_type": "code",
50 "cell_type": "code",
63 "collapsed": false,
64 "input": [
51 "input": [
65 "%px a=5"
52 "%px a=5"
66 ],
53 ],
67 "language": "python",
54 "language": "python",
68 "outputs": []
55 "outputs": []
69 },
56 },
70 {
57 {
71 "cell_type": "code",
58 "cell_type": "code",
72 "collapsed": false,
73 "input": [
59 "input": [
74 "%px print a"
60 "%px print a"
75 ],
61 ],
76 "language": "python",
62 "language": "python",
77 "outputs": []
63 "outputs": []
78 },
64 },
79 {
65 {
80 "cell_type": "code",
66 "cell_type": "code",
81 "collapsed": false,
82 "input": [
67 "input": [
83 "%px a"
68 "%px a"
84 ],
69 ],
85 "language": "python",
70 "language": "python",
86 "outputs": []
71 "outputs": []
87 },
72 },
88 {
73 {
89 "cell_type": "code",
74 "cell_type": "code",
90 "collapsed": false,
91 "input": [
75 "input": [
92 "with dv.sync_imports():",
76 "with dv.sync_imports():\n",
93 " import sys"
77 " import sys"
94 ],
78 ],
95 "language": "python",
79 "language": "python",
96 "outputs": []
80 "outputs": []
97 },
81 },
98 {
82 {
99 "cell_type": "code",
83 "cell_type": "code",
100 "collapsed": false,
101 "input": [
84 "input": [
102 "%px print >> sys.stderr, \"ERROR\""
85 "%px print >> sys.stderr, \"ERROR\""
103 ],
86 ],
104 "language": "python",
87 "language": "python",
105 "outputs": []
88 "outputs": []
106 },
89 },
107 {
90 {
108 "cell_type": "markdown",
91 "cell_type": "markdown",
109 "source": [
92 "source": [
110 "You don't have to wait for results:"
93 "You don't have to wait for results. The `%pxconfig` magic lets you change the default blocking/targets for the `%px` magics:"
111 ]
94 ]
112 },
95 },
113 {
96 {
114 "cell_type": "code",
97 "cell_type": "code",
115 "collapsed": true,
116 "input": [
98 "input": [
117 "dv.block = False"
99 "%pxconfig --noblock"
118 ],
100 ],
119 "language": "python",
101 "language": "python",
120 "outputs": []
102 "outputs": []
121 },
103 },
122 {
104 {
123 "cell_type": "code",
105 "cell_type": "code",
124 "collapsed": false,
125 "input": [
106 "input": [
126 "%px import time",
107 "%px import time\n",
127 "%px time.sleep(5)",
108 "%px time.sleep(5)\n",
128 "%px time.time()"
109 "%px time.time()"
129 ],
110 ],
130 "language": "python",
111 "language": "python",
131 "outputs": []
112 "outputs": []
132 },
113 },
133 {
114 {
134 "cell_type": "markdown",
115 "cell_type": "markdown",
135 "source": [
116 "source": [
136 "But you will notice that this didn't output the result of the last command.",
117 "But you will notice that this didn't output the result of the last command.\n",
137 "For this, we have `%result`, which displays the output of the latest request:"
118 "For this, we have `%result`, which displays the output of the latest request:"
138 ]
119 ]
139 },
120 },
140 {
121 {
141 "cell_type": "code",
122 "cell_type": "code",
142 "collapsed": false,
143 "input": [
123 "input": [
144 "%result"
124 "%result"
145 ],
125 ],
146 "language": "python",
126 "language": "python",
147 "outputs": []
127 "outputs": []
148 },
128 },
149 {
129 {
150 "cell_type": "markdown",
130 "cell_type": "markdown",
151 "source": [
131 "source": [
152 "Remember, an IPython engine is IPython, so you can do magics remotely as well!"
132 "Remember, an IPython engine is IPython, so you can do magics remotely as well!"
153 ]
133 ]
154 },
134 },
155 {
135 {
156 "cell_type": "code",
136 "cell_type": "code",
157 "collapsed": false,
158 "input": [
137 "input": [
159 "dv.block = True",
138 "%pxconfig --block\n",
160 "%px %pylab inline"
139 "%px %pylab inline"
161 ],
140 ],
162 "language": "python",
141 "language": "python",
163 "outputs": []
142 "outputs": []
164 },
143 },
165 {
144 {
166 "cell_type": "markdown",
145 "cell_type": "markdown",
167 "source": [
146 "source": [
168 "`%%px` can also be used as a cell magic, for submitting whole blocks.",
147 "`%%px` can also be used as a cell magic, for submitting whole blocks.\n",
169 "This one acceps `--block` and `--noblock` flags to specify",
148 "This one acceps `--block` and `--noblock` flags to specify\n",
170 "the blocking behavior, though the default is unchanged.",
149 "the blocking behavior, though the default is unchanged.\n"
171 ""
172 ]
150 ]
173 },
151 },
174 {
152 {
175 "cell_type": "code",
153 "cell_type": "code",
176 "collapsed": true,
177 "input": [
154 "input": [
178 "dv.scatter('id', dv.targets, flatten=True)",
155 "dv.scatter('id', dv.targets, flatten=True)\n",
179 "dv['stride'] = len(dv)"
156 "dv['stride'] = len(dv)"
180 ],
157 ],
181 "language": "python",
158 "language": "python",
182 "outputs": []
159 "outputs": []
183 },
160 },
184 {
161 {
185 "cell_type": "code",
162 "cell_type": "code",
186 "collapsed": false,
187 "input": [
163 "input": [
188 "%%px --noblock",
164 "%%px --noblock\n",
189 "x = linspace(0,pi,1000)",
165 "x = linspace(0,pi,1000)\n",
190 "for n in range(id,12, stride):",
166 "for n in range(id,12, stride):\n",
191 " print n",
167 " print n\n",
192 " plt.plot(x,sin(n*x))",
168 " plt.plot(x,sin(n*x))\n",
193 "plt.title(\"Plot %i\" % id)"
169 "plt.title(\"Plot %i\" % id)"
194 ],
170 ],
195 "language": "python",
171 "language": "python",
196 "outputs": []
172 "outputs": []
197 },
173 },
198 {
174 {
199 "cell_type": "code",
175 "cell_type": "code",
200 "collapsed": false,
201 "input": [
176 "input": [
202 "%result"
177 "%result"
203 ],
178 ],
204 "language": "python",
179 "language": "python",
205 "outputs": []
180 "outputs": []
206 },
181 },
207 {
182 {
208 "cell_type": "markdown",
183 "cell_type": "markdown",
209 "source": [
184 "source": [
210 "It also lets you choose some amount of the grouping of the outputs with `--group-outputs`:",
185 "It also lets you choose some amount of the grouping of the outputs with `--group-outputs`:\n",
211 "",
186 "\n",
212 "The choices are:",
187 "The choices are:\n",
213 "",
188 "\n",
214 "* `engine` - all of an engine's output is collected together",
189 "* `engine` - all of an engine's output is collected together\n",
215 "* `type` - where stdout of each engine is grouped, etc. (the default)",
190 "* `type` - where stdout of each engine is grouped, etc. (the default)\n",
216 "* `order` - same as `type`, but individual displaypub outputs are interleaved.",
191 "* `order` - same as `type`, but individual displaypub outputs are interleaved.\n",
217 " That is, it will output the first plot from each engine, then the second from each,",
192 " That is, it will output the first plot from each engine, then the second from each,\n",
218 " etc."
193 " etc."
219 ]
194 ]
220 },
195 },
221 {
196 {
222 "cell_type": "code",
197 "cell_type": "code",
223 "collapsed": false,
224 "input": [
198 "input": [
225 "%%px --group-outputs=engine",
199 "%%px --group-outputs=engine\n",
226 "x = linspace(0,pi,1000)",
200 "x = linspace(0,pi,1000)\n",
227 "for n in range(id,12, stride):",
201 "for n in range(id+1,12, stride):\n",
228 " print n",
202 " print n\n",
229 " plt.figure()",
203 " plt.figure()\n",
230 " plt.plot(x,sin(n*x))",
204 " plt.plot(x,sin(n*x))\n",
231 "plt.title(\"Plot %i\" % id)"
205 " plt.title(\"Plot %i\" % n)"
232 ],
206 ],
233 "language": "python",
207 "language": "python",
234 "outputs": []
208 "outputs": []
235 },
209 },
236 {
210 {
237 "cell_type": "markdown",
211 "cell_type": "markdown",
238 "source": [
212 "source": [
239 "When you specify 'order', then individual display outputs (e.g. plots) will be interleaved:"
213 "When you specify 'order', then individual display outputs (e.g. plots) will be interleaved.\n",
214 "\n",
215 "`%result` takes the same output-ordering arguments as `%%px`, \n",
216 "so you can view the previous result in a variety of different ways with a few sequential calls to `%result`:"
240 ]
217 ]
241 },
218 },
242 {
219 {
243 "cell_type": "code",
220 "cell_type": "code",
244 "collapsed": false,
245 "input": [
221 "input": [
246 "%%px --group-outputs=order",
222 "%result --group-outputs=order"
247 "x = linspace(0,pi,1000)",
248 "for n in range(id,12, stride):",
249 " print n",
250 " plt.figure()",
251 " plt.plot(x,sin(n*x))",
252 "plt.title(\"Plot %i\" % id)"
253 ],
223 ],
254 "language": "python",
224 "language": "python",
255 "outputs": []
225 "outputs": []
256 },
226 },
257 {
227 {
258 "cell_type": "heading",
228 "cell_type": "heading",
259 "level": 2,
229 "level": 2,
260 "source": [
230 "source": [
261 "Single-engine views"
231 "Single-engine views"
262 ]
232 ]
263 },
233 },
264 {
234 {
265 "cell_type": "markdown",
235 "cell_type": "markdown",
266 "source": [
236 "source": [
267 "When a DirectView has a single target, the output is a bit simpler (no prefixes on stdout/err, etc.):"
237 "When a DirectView has a single target, the output is a bit simpler (no prefixes on stdout/err, etc.):"
268 ]
238 ]
269 },
239 },
270 {
240 {
271 "cell_type": "code",
241 "cell_type": "code",
272 "collapsed": true,
273 "input": [
242 "input": [
274 "def generate_output():",
243 "def generate_output():\n",
275 " \"\"\"function for testing output",
244 " \"\"\"function for testing output\n",
276 " ",
245 " \n",
277 " publishes two outputs of each type, and returns something",
246 " publishes two outputs of each type, and returns something\n",
278 " \"\"\"",
247 " \"\"\"\n",
279 " ",
248 " \n",
280 " import sys,os",
249 " import sys,os\n",
281 " from IPython.core.display import display, HTML, Math",
250 " from IPython.core.display import display, HTML, Math\n",
282 " ",
251 " \n",
283 " print \"stdout\"",
252 " print \"stdout\"\n",
284 " print >> sys.stderr, \"stderr\"",
253 " print >> sys.stderr, \"stderr\"\n",
285 " ",
254 " \n",
286 " display(HTML(\"<b>HTML</b>\"))",
255 " display(HTML(\"<b>HTML</b>\"))\n",
287 " ",
256 " \n",
288 " print \"stdout2\"",
257 " print \"stdout2\"\n",
289 " print >> sys.stderr, \"stderr2\"",
258 " print >> sys.stderr, \"stderr2\"\n",
290 " ",
259 " \n",
291 " display(Math(r\"\\alpha=\\beta\"))",
260 " display(Math(r\"\\alpha=\\beta\"))\n",
292 " ",
261 " \n",
293 " return os.getpid()",
262 " return os.getpid()\n",
294 "",
263 "\n",
295 "dv['generate_output'] = generate_output"
264 "dv['generate_output'] = generate_output"
296 ],
265 ],
297 "language": "python",
266 "language": "python",
298 "outputs": []
267 "outputs": []
299 },
268 },
300 {
269 {
270 "cell_type": "markdown",
271 "source": [
272 "You can also have more than one set of parallel magics registered at a time.\n",
273 "\n",
274 "The `View.activate()` method takes a suffix argument, which is added to `'px'`."
275 ]
276 },
277 {
301 "cell_type": "code",
278 "cell_type": "code",
302 "collapsed": true,
303 "input": [
279 "input": [
304 "e0 = rc[-1]",
280 "e0 = rc[-1]\n",
305 "e0.block = True",
281 "e0.block = True\n",
306 "e0.activate()"
282 "e0.activate('0')"
283 ],
284 "language": "python",
285 "outputs": []
286 },
287 {
288 "cell_type": "code",
289 "input": [
290 "%px0 generate_output()"
307 ],
291 ],
308 "language": "python",
292 "language": "python",
309 "outputs": []
293 "outputs": []
310 },
294 },
311 {
295 {
312 "cell_type": "code",
296 "cell_type": "code",
313 "collapsed": false,
314 "input": [
297 "input": [
315 "%px generate_output()"
298 "%px generate_output()"
316 ],
299 ],
317 "language": "python",
300 "language": "python",
318 "outputs": []
301 "outputs": []
302 },
303 {
304 "cell_type": "markdown",
305 "source": [
306 "As mentioned above, we can redisplay those same results with various grouping:"
307 ]
308 },
309 {
310 "cell_type": "code",
311 "input": [
312 "%pxresult --group-outputs order"
313 ],
314 "language": "python",
315 "outputs": []
316 },
317 {
318 "cell_type": "code",
319 "input": [
320 "%pxresult --group-outputs engine"
321 ],
322 "language": "python",
323 "outputs": []
324 },
325 {
326 "cell_type": "code",
327 "input": [],
328 "language": "python",
329 "outputs": []
319 }
330 }
320 ]
331 ]
321 }
332 }
322 ]
333 ]
323 } No newline at end of file
334 }
General Comments 0
You need to be logged in to leave comments. Login now