##// END OF EJS Templates
Merge pull request #3172 from ogrisel/missing-import...
Min RK -
r10209:25beeeab merge
parent child Browse files
Show More
@@ -1,476 +1,475 b''
1 {
1 {
2 "metadata": {
2 "metadata": {
3 "name": "Parallel Magics"
3 "name": "Parallel Magics"
4 },
4 },
5 "nbformat": 3,
5 "nbformat": 3,
6 "nbformat_minor": 0,
6 "nbformat_minor": 0,
7 "worksheets": [
7 "worksheets": [
8 {
8 {
9 "cells": [
9 "cells": [
10 {
10 {
11 "cell_type": "heading",
11 "cell_type": "heading",
12 "level": 1,
12 "level": 1,
13 "metadata": {},
13 "metadata": {},
14 "source": [
14 "source": [
15 "Using Parallel Magics"
15 "Using Parallel Magics"
16 ]
16 ]
17 },
17 },
18 {
18 {
19 "cell_type": "markdown",
19 "cell_type": "markdown",
20 "metadata": {},
20 "metadata": {},
21 "source": [
21 "source": [
22 "IPython has a few magics for working with your engines.\n",
22 "IPython has a few magics for working with your engines.\n",
23 "\n",
23 "\n",
24 "This assumes you have started an IPython cluster, either with the notebook interface,\n",
24 "This assumes you have started an IPython cluster, either with the notebook interface,\n",
25 "or the `ipcluster/controller/engine` commands."
25 "or the `ipcluster/controller/engine` commands."
26 ]
26 ]
27 },
27 },
28 {
28 {
29 "cell_type": "code",
29 "cell_type": "code",
30 "collapsed": false,
30 "collapsed": false,
31 "input": [
31 "input": [
32 "from IPython import parallel\n",
32 "from IPython import parallel\n",
33 "rc = parallel.Client()\n",
33 "rc = parallel.Client()\n",
34 "dv = rc[:]\n",
34 "dv = rc[:]\n",
35 "rc.ids"
35 "rc.ids"
36 ],
36 ],
37 "language": "python",
37 "language": "python",
38 "metadata": {},
38 "metadata": {},
39 "outputs": [],
39 "outputs": []
40 "prompt_number": 2
41 },
40 },
42 {
41 {
43 "cell_type": "markdown",
42 "cell_type": "markdown",
44 "metadata": {},
43 "metadata": {},
45 "source": [
44 "source": [
46 "Creating a Client registers the parallel magics `%px`, `%%px`, `%pxresult`, `pxconfig`, and `%autopx`. \n",
45 "Creating a Client registers the parallel magics `%px`, `%%px`, `%pxresult`, `pxconfig`, and `%autopx`. \n",
47 "These magics are initially associated with a DirectView always associated with all currently registered engines."
46 "These magics are initially associated with a DirectView always associated with all currently registered engines."
48 ]
47 ]
49 },
48 },
50 {
49 {
51 "cell_type": "markdown",
50 "cell_type": "markdown",
52 "metadata": {},
51 "metadata": {},
53 "source": [
52 "source": [
54 "Now we can execute code remotely with `%px`:"
53 "Now we can execute code remotely with `%px`:"
55 ]
54 ]
56 },
55 },
57 {
56 {
58 "cell_type": "code",
57 "cell_type": "code",
59 "collapsed": false,
58 "collapsed": false,
60 "input": [
59 "input": [
61 "%px a=5"
60 "%px a=5"
62 ],
61 ],
63 "language": "python",
62 "language": "python",
64 "metadata": {},
63 "metadata": {},
65 "outputs": []
64 "outputs": []
66 },
65 },
67 {
66 {
68 "cell_type": "code",
67 "cell_type": "code",
69 "collapsed": false,
68 "collapsed": false,
70 "input": [
69 "input": [
71 "%px print a"
70 "%px print a"
72 ],
71 ],
73 "language": "python",
72 "language": "python",
74 "metadata": {},
73 "metadata": {},
75 "outputs": []
74 "outputs": []
76 },
75 },
77 {
76 {
78 "cell_type": "code",
77 "cell_type": "code",
79 "collapsed": false,
78 "collapsed": false,
80 "input": [
79 "input": [
81 "%px a"
80 "%px a"
82 ],
81 ],
83 "language": "python",
82 "language": "python",
84 "metadata": {},
83 "metadata": {},
85 "outputs": []
84 "outputs": []
86 },
85 },
87 {
86 {
88 "cell_type": "code",
87 "cell_type": "code",
89 "collapsed": false,
88 "collapsed": false,
90 "input": [
89 "input": [
91 "with dv.sync_imports():\n",
90 "with dv.sync_imports():\n",
92 " import sys"
91 " import sys"
93 ],
92 ],
94 "language": "python",
93 "language": "python",
95 "metadata": {},
94 "metadata": {},
96 "outputs": []
95 "outputs": []
97 },
96 },
98 {
97 {
99 "cell_type": "code",
98 "cell_type": "code",
100 "collapsed": false,
99 "collapsed": false,
101 "input": [
100 "input": [
102 "%px print >> sys.stderr, \"ERROR\""
101 "%px print >> sys.stderr, \"ERROR\""
103 ],
102 ],
104 "language": "python",
103 "language": "python",
105 "metadata": {},
104 "metadata": {},
106 "outputs": []
105 "outputs": []
107 },
106 },
108 {
107 {
109 "cell_type": "markdown",
108 "cell_type": "markdown",
110 "metadata": {},
109 "metadata": {},
111 "source": [
110 "source": [
112 "You don't have to wait for results. The `%pxconfig` magic lets you change the default blocking/targets for the `%px` magics:"
111 "You don't have to wait for results. The `%pxconfig` magic lets you change the default blocking/targets for the `%px` magics:"
113 ]
112 ]
114 },
113 },
115 {
114 {
116 "cell_type": "code",
115 "cell_type": "code",
117 "collapsed": false,
116 "collapsed": false,
118 "input": [
117 "input": [
119 "%pxconfig --noblock"
118 "%pxconfig --noblock"
120 ],
119 ],
121 "language": "python",
120 "language": "python",
122 "metadata": {},
121 "metadata": {},
123 "outputs": []
122 "outputs": []
124 },
123 },
125 {
124 {
126 "cell_type": "code",
125 "cell_type": "code",
127 "collapsed": false,
126 "collapsed": false,
128 "input": [
127 "input": [
129 "%px import time\n",
128 "%px import time\n",
130 "%px time.sleep(5)\n",
129 "%px time.sleep(5)\n",
131 "%px time.time()"
130 "%px time.time()"
132 ],
131 ],
133 "language": "python",
132 "language": "python",
134 "metadata": {},
133 "metadata": {},
135 "outputs": []
134 "outputs": []
136 },
135 },
137 {
136 {
138 "cell_type": "markdown",
137 "cell_type": "markdown",
139 "metadata": {},
138 "metadata": {},
140 "source": [
139 "source": [
141 "But you will notice that this didn't output the result of the last command.\n",
140 "But you will notice that this didn't output the result of the last command.\n",
142 "For this, we have `%pxresult`, which displays the output of the latest request:"
141 "For this, we have `%pxresult`, which displays the output of the latest request:"
143 ]
142 ]
144 },
143 },
145 {
144 {
146 "cell_type": "code",
145 "cell_type": "code",
147 "collapsed": false,
146 "collapsed": false,
148 "input": [
147 "input": [
149 "%pxresult"
148 "%pxresult"
150 ],
149 ],
151 "language": "python",
150 "language": "python",
152 "metadata": {},
151 "metadata": {},
153 "outputs": []
152 "outputs": []
154 },
153 },
155 {
154 {
156 "cell_type": "markdown",
155 "cell_type": "markdown",
157 "metadata": {},
156 "metadata": {},
158 "source": [
157 "source": [
159 "Remember, an IPython engine is IPython, so you can do magics remotely as well!"
158 "Remember, an IPython engine is IPython, so you can do magics remotely as well!"
160 ]
159 ]
161 },
160 },
162 {
161 {
163 "cell_type": "code",
162 "cell_type": "code",
164 "collapsed": false,
163 "collapsed": false,
165 "input": [
164 "input": [
166 "%pxconfig --block\n",
165 "%pxconfig --block\n",
167 "%px %pylab inline"
166 "%px %pylab inline"
168 ],
167 ],
169 "language": "python",
168 "language": "python",
170 "metadata": {},
169 "metadata": {},
171 "outputs": []
170 "outputs": []
172 },
171 },
173 {
172 {
174 "cell_type": "markdown",
173 "cell_type": "markdown",
175 "metadata": {},
174 "metadata": {},
176 "source": [
175 "source": [
177 "`%%px` can also be used as a cell magic, for submitting whole blocks.\n",
176 "`%%px` can also be used as a cell magic, for submitting whole blocks.\n",
178 "This one acceps `--block` and `--noblock` flags to specify\n",
177 "This one acceps `--block` and `--noblock` flags to specify\n",
179 "the blocking behavior, though the default is unchanged.\n"
178 "the blocking behavior, though the default is unchanged.\n"
180 ]
179 ]
181 },
180 },
182 {
181 {
183 "cell_type": "code",
182 "cell_type": "code",
184 "collapsed": false,
183 "collapsed": false,
185 "input": [
184 "input": [
186 "dv.scatter('id', dv.targets, flatten=True)\n",
185 "dv.scatter('id', dv.targets, flatten=True)\n",
187 "dv['stride'] = len(dv)"
186 "dv['stride'] = len(dv)"
188 ],
187 ],
189 "language": "python",
188 "language": "python",
190 "metadata": {},
189 "metadata": {},
191 "outputs": []
190 "outputs": []
192 },
191 },
193 {
192 {
194 "cell_type": "code",
193 "cell_type": "code",
195 "collapsed": false,
194 "collapsed": false,
196 "input": [
195 "input": [
197 "%%px --noblock\n",
196 "%%px --noblock\n",
198 "x = linspace(0,pi,1000)\n",
197 "x = linspace(0,pi,1000)\n",
199 "for n in range(id,12, stride):\n",
198 "for n in range(id,12, stride):\n",
200 " print n\n",
199 " print n\n",
201 " plt.plot(x,sin(n*x))\n",
200 " plt.plot(x,sin(n*x))\n",
202 "plt.title(\"Plot %i\" % id)"
201 "plt.title(\"Plot %i\" % id)"
203 ],
202 ],
204 "language": "python",
203 "language": "python",
205 "metadata": {},
204 "metadata": {},
206 "outputs": []
205 "outputs": []
207 },
206 },
208 {
207 {
209 "cell_type": "code",
208 "cell_type": "code",
210 "collapsed": false,
209 "collapsed": false,
211 "input": [
210 "input": [
212 "%pxresult"
211 "%pxresult"
213 ],
212 ],
214 "language": "python",
213 "language": "python",
215 "metadata": {},
214 "metadata": {},
216 "outputs": []
215 "outputs": []
217 },
216 },
218 {
217 {
219 "cell_type": "markdown",
218 "cell_type": "markdown",
220 "metadata": {},
219 "metadata": {},
221 "source": [
220 "source": [
222 "It also lets you choose some amount of the grouping of the outputs with `--group-outputs`:\n",
221 "It also lets you choose some amount of the grouping of the outputs with `--group-outputs`:\n",
223 "\n",
222 "\n",
224 "The choices are:\n",
223 "The choices are:\n",
225 "\n",
224 "\n",
226 "* `engine` - all of an engine's output is collected together\n",
225 "* `engine` - all of an engine's output is collected together\n",
227 "* `type` - where stdout of each engine is grouped, etc. (the default)\n",
226 "* `type` - where stdout of each engine is grouped, etc. (the default)\n",
228 "* `order` - same as `type`, but individual displaypub outputs are interleaved.\n",
227 "* `order` - same as `type`, but individual displaypub outputs are interleaved.\n",
229 " That is, it will output the first plot from each engine, then the second from each,\n",
228 " That is, it will output the first plot from each engine, then the second from each,\n",
230 " etc."
229 " etc."
231 ]
230 ]
232 },
231 },
233 {
232 {
234 "cell_type": "code",
233 "cell_type": "code",
235 "collapsed": false,
234 "collapsed": false,
236 "input": [
235 "input": [
237 "%%px --group-outputs=engine\n",
236 "%%px --group-outputs=engine\n",
238 "x = linspace(0,pi,1000)\n",
237 "x = linspace(0,pi,1000)\n",
239 "for n in range(id+1,12, stride):\n",
238 "for n in range(id+1,12, stride):\n",
240 " print n\n",
239 " print n\n",
241 " plt.figure()\n",
240 " plt.figure()\n",
242 " plt.plot(x,sin(n*x))\n",
241 " plt.plot(x,sin(n*x))\n",
243 " plt.title(\"Plot %i\" % n)"
242 " plt.title(\"Plot %i\" % n)"
244 ],
243 ],
245 "language": "python",
244 "language": "python",
246 "metadata": {},
245 "metadata": {},
247 "outputs": []
246 "outputs": []
248 },
247 },
249 {
248 {
250 "cell_type": "markdown",
249 "cell_type": "markdown",
251 "metadata": {},
250 "metadata": {},
252 "source": [
251 "source": [
253 "When you specify 'order', then individual display outputs (e.g. plots) will be interleaved.\n",
252 "When you specify 'order', then individual display outputs (e.g. plots) will be interleaved.\n",
254 "\n",
253 "\n",
255 "`%pxresult` takes the same output-ordering arguments as `%%px`, \n",
254 "`%pxresult` takes the same output-ordering arguments as `%%px`, \n",
256 "so you can view the previous result in a variety of different ways with a few sequential calls to `%pxresult`:"
255 "so you can view the previous result in a variety of different ways with a few sequential calls to `%pxresult`:"
257 ]
256 ]
258 },
257 },
259 {
258 {
260 "cell_type": "code",
259 "cell_type": "code",
261 "collapsed": false,
260 "collapsed": false,
262 "input": [
261 "input": [
263 "%pxresult --group-outputs=order"
262 "%pxresult --group-outputs=order"
264 ],
263 ],
265 "language": "python",
264 "language": "python",
266 "metadata": {},
265 "metadata": {},
267 "outputs": []
266 "outputs": []
268 },
267 },
269 {
268 {
270 "cell_type": "heading",
269 "cell_type": "heading",
271 "level": 2,
270 "level": 2,
272 "metadata": {},
271 "metadata": {},
273 "source": [
272 "source": [
274 "Single-engine views"
273 "Single-engine views"
275 ]
274 ]
276 },
275 },
277 {
276 {
278 "cell_type": "markdown",
277 "cell_type": "markdown",
279 "metadata": {},
278 "metadata": {},
280 "source": [
279 "source": [
281 "When a DirectView has a single target, the output is a bit simpler (no prefixes on stdout/err, etc.):"
280 "When a DirectView has a single target, the output is a bit simpler (no prefixes on stdout/err, etc.):"
282 ]
281 ]
283 },
282 },
284 {
283 {
285 "cell_type": "code",
284 "cell_type": "code",
286 "collapsed": false,
285 "collapsed": false,
287 "input": [
286 "input": [
288 "def generate_output():\n",
287 "def generate_output():\n",
289 " \"\"\"function for testing output\n",
288 " \"\"\"function for testing output\n",
290 " \n",
289 " \n",
291 " publishes two outputs of each type, and returns something\n",
290 " publishes two outputs of each type, and returns something\n",
292 " \"\"\"\n",
291 " \"\"\"\n",
293 " \n",
292 " \n",
294 " import sys,os\n",
293 " import sys,os\n",
295 " from IPython.core.display import display, HTML, Math\n",
294 " from IPython.core.display import display, HTML, Math\n",
296 " \n",
295 " \n",
297 " print \"stdout\"\n",
296 " print \"stdout\"\n",
298 " print >> sys.stderr, \"stderr\"\n",
297 " print >> sys.stderr, \"stderr\"\n",
299 " \n",
298 " \n",
300 " display(HTML(\"<b>HTML</b>\"))\n",
299 " display(HTML(\"<b>HTML</b>\"))\n",
301 " \n",
300 " \n",
302 " print \"stdout2\"\n",
301 " print \"stdout2\"\n",
303 " print >> sys.stderr, \"stderr2\"\n",
302 " print >> sys.stderr, \"stderr2\"\n",
304 " \n",
303 " \n",
305 " display(Math(r\"\\alpha=\\beta\"))\n",
304 " display(Math(r\"\\alpha=\\beta\"))\n",
306 " \n",
305 " \n",
307 " return os.getpid()\n",
306 " return os.getpid()\n",
308 "\n",
307 "\n",
309 "dv['generate_output'] = generate_output"
308 "dv['generate_output'] = generate_output"
310 ],
309 ],
311 "language": "python",
310 "language": "python",
312 "metadata": {},
311 "metadata": {},
313 "outputs": []
312 "outputs": []
314 },
313 },
315 {
314 {
316 "cell_type": "markdown",
315 "cell_type": "markdown",
317 "metadata": {},
316 "metadata": {},
318 "source": [
317 "source": [
319 "You can also have more than one set of parallel magics registered at a time.\n",
318 "You can also have more than one set of parallel magics registered at a time.\n",
320 "\n",
319 "\n",
321 "The `View.activate()` method takes a suffix argument, which is added to `'px'`."
320 "The `View.activate()` method takes a suffix argument, which is added to `'px'`."
322 ]
321 ]
323 },
322 },
324 {
323 {
325 "cell_type": "code",
324 "cell_type": "code",
326 "collapsed": false,
325 "collapsed": false,
327 "input": [
326 "input": [
328 "e0 = rc[-1]\n",
327 "e0 = rc[-1]\n",
329 "e0.block = True\n",
328 "e0.block = True\n",
330 "e0.activate('0')"
329 "e0.activate('0')"
331 ],
330 ],
332 "language": "python",
331 "language": "python",
333 "metadata": {},
332 "metadata": {},
334 "outputs": []
333 "outputs": []
335 },
334 },
336 {
335 {
337 "cell_type": "code",
336 "cell_type": "code",
338 "collapsed": false,
337 "collapsed": false,
339 "input": [
338 "input": [
340 "%px0 generate_output()"
339 "%px0 generate_output()"
341 ],
340 ],
342 "language": "python",
341 "language": "python",
343 "metadata": {},
342 "metadata": {},
344 "outputs": []
343 "outputs": []
345 },
344 },
346 {
345 {
347 "cell_type": "code",
346 "cell_type": "code",
348 "collapsed": false,
347 "collapsed": false,
349 "input": [
348 "input": [
350 "%px generate_output()"
349 "%px generate_output()"
351 ],
350 ],
352 "language": "python",
351 "language": "python",
353 "metadata": {},
352 "metadata": {},
354 "outputs": []
353 "outputs": []
355 },
354 },
356 {
355 {
357 "cell_type": "markdown",
356 "cell_type": "markdown",
358 "metadata": {},
357 "metadata": {},
359 "source": [
358 "source": [
360 "As mentioned above, we can redisplay those same results with various grouping:"
359 "As mentioned above, we can redisplay those same results with various grouping:"
361 ]
360 ]
362 },
361 },
363 {
362 {
364 "cell_type": "code",
363 "cell_type": "code",
365 "collapsed": false,
364 "collapsed": false,
366 "input": [
365 "input": [
367 "%pxresult --group-outputs order"
366 "%pxresult --group-outputs order"
368 ],
367 ],
369 "language": "python",
368 "language": "python",
370 "metadata": {},
369 "metadata": {},
371 "outputs": []
370 "outputs": []
372 },
371 },
373 {
372 {
374 "cell_type": "code",
373 "cell_type": "code",
375 "collapsed": false,
374 "collapsed": false,
376 "input": [
375 "input": [
377 "%pxresult --group-outputs engine"
376 "%pxresult --group-outputs engine"
378 ],
377 ],
379 "language": "python",
378 "language": "python",
380 "metadata": {},
379 "metadata": {},
381 "outputs": []
380 "outputs": []
382 },
381 },
383 {
382 {
384 "cell_type": "heading",
383 "cell_type": "heading",
385 "level": 2,
384 "level": 2,
386 "metadata": {},
385 "metadata": {},
387 "source": [
386 "source": [
388 "Parallel Exceptions"
387 "Parallel Exceptions"
389 ]
388 ]
390 },
389 },
391 {
390 {
392 "cell_type": "markdown",
391 "cell_type": "markdown",
393 "metadata": {},
392 "metadata": {},
394 "source": [
393 "source": [
395 "When you raise exceptions with the parallel exception,\n",
394 "When you raise exceptions with the parallel exception,\n",
396 "the CompositeError raised locally will display your remote traceback."
395 "the CompositeError raised locally will display your remote traceback."
397 ]
396 ]
398 },
397 },
399 {
398 {
400 "cell_type": "code",
399 "cell_type": "code",
401 "collapsed": false,
400 "collapsed": false,
402 "input": [
401 "input": [
403 "%%px\n",
402 "%%px\n",
404 "from numpy.random import random\n",
403 "from numpy.random import random\n",
405 "A = random((100,100,'invalid shape'))"
404 "A = random((100,100,'invalid shape'))"
406 ],
405 ],
407 "language": "python",
406 "language": "python",
408 "metadata": {},
407 "metadata": {},
409 "outputs": []
408 "outputs": []
410 },
409 },
411 {
410 {
412 "cell_type": "heading",
411 "cell_type": "heading",
413 "level": 2,
412 "level": 2,
414 "metadata": {},
413 "metadata": {},
415 "source": [
414 "source": [
416 "Remote Cell Magics"
415 "Remote Cell Magics"
417 ]
416 ]
418 },
417 },
419 {
418 {
420 "cell_type": "markdown",
419 "cell_type": "markdown",
421 "metadata": {},
420 "metadata": {},
422 "source": [
421 "source": [
423 "Remember, Engines are IPython too, so the cell that is run remotely by %%px can in turn use a cell magic."
422 "Remember, Engines are IPython too, so the cell that is run remotely by %%px can in turn use a cell magic."
424 ]
423 ]
425 },
424 },
426 {
425 {
427 "cell_type": "code",
426 "cell_type": "code",
428 "collapsed": false,
427 "collapsed": false,
429 "input": [
428 "input": [
430 "%%px\n",
429 "%%px\n",
431 "%%timeit\n",
430 "%%timeit\n",
432 "from numpy.random import random\n",
431 "from numpy.random import random\n",
433 "from numpy.linalg import norm\n",
432 "from numpy.linalg import norm\n",
434 "A = random((100,100))\n",
433 "A = random((100,100))\n",
435 "norm(A, 2) "
434 "norm(A, 2) "
436 ],
435 ],
437 "language": "python",
436 "language": "python",
438 "metadata": {},
437 "metadata": {},
439 "outputs": []
438 "outputs": []
440 },
439 },
441 {
440 {
442 "cell_type": "heading",
441 "cell_type": "heading",
443 "level": 2,
442 "level": 2,
444 "metadata": {},
443 "metadata": {},
445 "source": [
444 "source": [
446 "Local Execution"
445 "Local Execution"
447 ]
446 ]
448 },
447 },
449 {
448 {
450 "cell_type": "markdown",
449 "cell_type": "markdown",
451 "metadata": {},
450 "metadata": {},
452 "source": [
451 "source": [
453 "As of IPython 0.14, you can instruct `%%px` to also execute the cell locally.\n",
452 "As of IPython 0.14, you can instruct `%%px` to also execute the cell locally.\n",
454 "This is useful for interactive definitions,\n",
453 "This is useful for interactive definitions,\n",
455 "or if you want to load a data source everywhere,\n",
454 "or if you want to load a data source everywhere,\n",
456 "not just on the engines."
455 "not just on the engines."
457 ]
456 ]
458 },
457 },
459 {
458 {
460 "cell_type": "code",
459 "cell_type": "code",
461 "collapsed": false,
460 "collapsed": false,
462 "input": [
461 "input": [
463 "%%px --local\n",
462 "%%px --local\n",
463 "import os\n",
464 "thispid = os.getpid()\n",
464 "thispid = os.getpid()\n",
465 "print thispid"
465 "print thispid"
466 ],
466 ],
467 "language": "python",
467 "language": "python",
468 "metadata": {},
468 "metadata": {},
469 "outputs": [],
469 "outputs": []
470 "prompt_number": 5
471 }
470 }
472 ],
471 ],
473 "metadata": {}
472 "metadata": {}
474 }
473 }
475 ]
474 ]
476 } No newline at end of file
475 }
General Comments 0
You need to be logged in to leave comments. Login now