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