##// END OF EJS Templates
parallel magics get their own doc file
MinRK -
Show More
@@ -0,0 +1,389 b''
1 .. _parallel_magics:
2
3 =======================
4 Parallel Magic Commands
5 =======================
6
7 We provide a few IPython magic commands
8 that make it a bit more pleasant to execute Python commands on the engines interactively.
9 These are mainly shortcuts to :meth:`.DirectView.execute`
10 and :meth:`.AsyncResult.display_outputs` methods repsectively.
11
12 These magics will automatically become available when you create a Client:
13
14 .. sourcecode:: ipython
15
16 In [2]: rc = parallel.Client()
17
18 The initially active View will have attributes ``targets='all', block=True``,
19 which is a blocking view of all engines, evaluated at request time
20 (adding/removing engines will change where this view's tasks will run).
21
22 The Magics
23 ==========
24
25 %px
26 ---
27
28 The %px magic executes a single Python command on the engines
29 specified by the :attr:`targets` attribute of the :class:`DirectView` instance:
30
31 .. sourcecode:: ipython
32
33 # import numpy here and everywhere
34 In [25]: with rc[:].sync_imports():
35 ....: import numpy
36 importing numpy on engine(s)
37
38 In [27]: %px a = numpy.random.rand(2,2)
39 Parallel execution on engines: [0, 1, 2, 3]
40
41 In [28]: %px numpy.linalg.eigvals(a)
42 Parallel execution on engines: [0, 1, 2, 3]
43 Out [0:68]: array([ 0.77120707, -0.19448286])
44 Out [1:68]: array([ 1.10815921, 0.05110369])
45 Out [2:68]: array([ 0.74625527, -0.37475081])
46 Out [3:68]: array([ 0.72931905, 0.07159743])
47
48 In [29]: %px print 'hi'
49 Parallel execution on engine(s): all
50 [stdout:0] hi
51 [stdout:1] hi
52 [stdout:2] hi
53 [stdout:3] hi
54
55
56 Since engines are IPython as well, you can even run magics remotely:
57
58 .. sourcecode:: ipython
59
60 In [28]: %px %pylab inline
61 Parallel execution on engine(s): all
62 [stdout:0]
63 Welcome to pylab, a matplotlib-based Python environment...
64 For more information, type 'help(pylab)'.
65 [stdout:1]
66 Welcome to pylab, a matplotlib-based Python environment...
67 For more information, type 'help(pylab)'.
68 [stdout:2]
69 Welcome to pylab, a matplotlib-based Python environment...
70 For more information, type 'help(pylab)'.
71 [stdout:3]
72 Welcome to pylab, a matplotlib-based Python environment...
73 For more information, type 'help(pylab)'.
74
75 And once in pylab mode with the inline backend,
76 you can make plots and they will be displayed in your frontend
77 if it suports the inline figures (e.g. notebook or qtconsole):
78
79 .. sourcecode:: ipython
80
81 In [40]: %px plot(rand(100))
82 Parallel execution on engine(s): all
83 <plot0>
84 <plot1>
85 <plot2>
86 <plot3>
87 Out[0:79]: [<matplotlib.lines.Line2D at 0x10a6286d0>]
88 Out[1:79]: [<matplotlib.lines.Line2D at 0x10b9476d0>]
89 Out[2:79]: [<matplotlib.lines.Line2D at 0x110652750>]
90 Out[3:79]: [<matplotlib.lines.Line2D at 0x10c6566d0>]
91
92
93 %%px Cell Magic
94 ---------------
95
96 %%px can be used as a Cell Magic, which accepts some arguments for controlling
97 the execution.
98
99
100 Targets and Blocking
101 ********************
102
103 %%px accepts ``--targets`` for controlling which engines on which to run,
104 and ``--[no]block`` for specifying the blocking behavior of this cell,
105 independent of the defaults for the View.
106
107 .. sourcecode:: ipython
108
109 In [6]: %%px --targets ::2
110 ...: print "I am even"
111 ...:
112 Parallel execution on engine(s): [0, 2]
113 [stdout:0] I am even
114 [stdout:2] I am even
115
116 In [7]: %%px --targets 1
117 ...: print "I am number 1"
118 ...:
119 Parallel execution on engine(s): 1
120 I am number 1
121
122 In [8]: %%px
123 ...: print "still 'all' by default"
124 ...:
125 Parallel execution on engine(s): all
126 [stdout:0] still 'all' by default
127 [stdout:1] still 'all' by default
128 [stdout:2] still 'all' by default
129 [stdout:3] still 'all' by default
130
131 In [9]: %%px --noblock
132 ...: import time
133 ...: time.sleep(1)
134 ...: time.time()
135 ...:
136 Async parallel execution on engine(s): all
137 Out[9]: <AsyncResult: execute>
138
139 In [10]: %pxresult
140 Out[0:12]: 1339454561.069116
141 Out[1:10]: 1339454561.076752
142 Out[2:12]: 1339454561.072837
143 Out[3:10]: 1339454561.066665
144
145
146 .. seealso::
147
148 :ref:`%pxconfig` accepts these same arguments for changing the *default*
149 values of targets/blocking for the active View.
150
151
152 Output Display
153 **************
154
155
156 %%px also accepts a ``--group-outputs`` argument,
157 which adjusts how the outputs of multiple engines are presented.
158
159 .. seealso::
160
161 :meth:`.AsyncResult.display_outputs` for the grouping options.
162
163 .. sourcecode:: ipython
164
165 In [50]: %%px --block --group-outputs=engine
166 ....: import numpy as np
167 ....: A = np.random.random((2,2))
168 ....: ev = numpy.linalg.eigvals(A)
169 ....: print ev
170 ....: ev.max()
171 ....:
172 Parallel execution on engine(s): all
173 [stdout:0] [ 0.60640442 0.95919621]
174 Out [0:73]: 0.9591962130899806
175 [stdout:1] [ 0.38501813 1.29430871]
176 Out [1:73]: 1.2943087091452372
177 [stdout:2] [-0.85925141 0.9387692 ]
178 Out [2:73]: 0.93876920456230284
179 [stdout:3] [ 0.37998269 1.24218246]
180 Out [3:73]: 1.2421824618493817
181
182
183 %pxresult
184 ---------
185
186 If you are using %px in non-blocking mode, you won't get output.
187 You can use %pxresult to display the outputs of the latest command,
188 just as is done when %px is blocking:
189
190 .. sourcecode:: ipython
191
192 In [39]: dv.block = False
193
194 In [40]: %px print 'hi'
195 Async parallel execution on engine(s): all
196
197 In [41]: %pxresult
198 [stdout:0] hi
199 [stdout:1] hi
200 [stdout:2] hi
201 [stdout:3] hi
202
203 %pxresult simply calls :meth:`.AsyncResult.display_outputs` on the most recent request.
204 It accepts the same output-grouping arguments as %%px, so you can use it to view
205 a result in different ways.
206
207
208 %autopx
209 -------
210
211 The %autopx magic switches to a mode where everything you type is executed
212 on the engines until you do %autopx again.
213
214 .. sourcecode:: ipython
215
216 In [30]: dv.block=True
217
218 In [31]: %autopx
219 %autopx enabled
220
221 In [32]: max_evals = []
222
223 In [33]: for i in range(100):
224 ....: a = numpy.random.rand(10,10)
225 ....: a = a+a.transpose()
226 ....: evals = numpy.linalg.eigvals(a)
227 ....: max_evals.append(evals[0].real)
228 ....:
229
230 In [34]: print "Average max eigenvalue is: %f" % (sum(max_evals)/len(max_evals))
231 [stdout:0] Average max eigenvalue is: 10.193101
232 [stdout:1] Average max eigenvalue is: 10.064508
233 [stdout:2] Average max eigenvalue is: 10.055724
234 [stdout:3] Average max eigenvalue is: 10.086876
235
236 In [35]: %autopx
237 Auto Parallel Disabled
238
239 %pxconfig
240 ---------
241
242 The default targets and blocking behavior for the magics are governed by the :attr:`block`
243 and :attr:`targets` attribute of the active View. If you have a handle for the view,
244 you can set these attributes directly, but if you don't, you can change them with
245 the %pxconfig magic:
246
247 .. sourcecode:: ipython
248
249 In [3]: %pxconfig --block
250
251 In [5]: %px print 'hi'
252 Parallel execution on engine(s): all
253 [stdout:0] hi
254 [stdout:1] hi
255 [stdout:2] hi
256 [stdout:3] hi
257
258 In [6]: %pxconfig --targets ::2
259
260 In [7]: %px print 'hi'
261 Parallel execution on engine(s): [0, 2]
262 [stdout:0] hi
263 [stdout:2] hi
264
265 In [8]: %pxconfig --noblock
266
267 In [9]: %px print 'are you there?'
268 Async parallel execution on engine(s): [0, 2]
269 Out[9]: <AsyncResult: execute>
270
271 In [10]: %pxresult
272 [stdout:0] are you there?
273 [stdout:2] are you there?
274
275
276 Multiple Active Views
277 =====================
278
279 The parallel magics are associated with a particular :class:`~.DirectView` object.
280 You can change the active view by calling the :meth:`~.DirectView.activate` method
281 on any view.
282
283 .. sourcecode:: ipython
284
285 In [11]: even = rc[::2]
286
287 In [12]: even.activate()
288
289 In [13]: %px print 'hi'
290 Async parallel execution on engine(s): [0, 2]
291 Out[13]: <AsyncResult: execute>
292
293 In [14]: even.block = True
294
295 In [15]: %px print 'hi'
296 Parallel execution on engine(s): [0, 2]
297 [stdout:0] hi
298 [stdout:2] hi
299
300 When activating a View, you can also specify a *suffix*, so that a whole different
301 set of magics are associated with that view, without replacing the existing ones.
302
303 .. sourcecode:: ipython
304
305 # restore the original DirecView to the base %px magics
306 In [16]: rc.activate()
307 Out[16]: <DirectView all>
308
309 In [17]: even.activate('_even')
310
311 In [18]: %px print 'hi all'
312 Parallel execution on engine(s): all
313 [stdout:0] hi all
314 [stdout:1] hi all
315 [stdout:2] hi all
316 [stdout:3] hi all
317
318 In [19]: %px_even print "We aren't odd!"
319 Parallel execution on engine(s): [0, 2]
320 [stdout:0] We aren't odd!
321 [stdout:2] We aren't odd!
322
323 This suffix is applied to the end of all magics, e.g. %autopx_even, %pxresult_even, etc.
324
325 For convenience, the :class:`~.Client` has a :meth:`~.Client.activate` method as well,
326 which creates a DirectView with block=True, activates it, and returns the new View.
327
328 The initial magics registered when you create a client are the result of a call to
329 :meth:`rc.activate` with default args.
330
331
332 Engines as Kernels
333 ==================
334
335 Engines are really the same object as the Kernels used elsewhere in IPython,
336 with the minor exception that engines connect to a controller, while regular kernels
337 bind their sockets, listening for connections from a QtConsole or other frontends.
338
339 Sometimes for debugging or inspection purposes, you would like a QtConsole connected
340 to an engine for more direct interaction. You can do this by first instructing
341 the Engine to *also* bind its kernel, to listen for connections:
342
343 .. sourcecode:: ipython
344
345 In [50]: %px from IPython.parallel import bind_kernel; bind_kernel()
346
347 Then, if your engines are local, you can start a qtconsole right on the engine(s):
348
349 .. sourcecode:: ipython
350
351 In [51]: %px %qtconsole
352
353 Careful with this one, because if your view is of 16 engines it will start 16 QtConsoles!
354
355 Or you can view just the connection info, and work out the right way to connect to the engines,
356 depending on where they live and where you are:
357
358 .. sourcecode:: ipython
359
360 In [51]: %px %connect_info
361 Parallel execution on engine(s): all
362 [stdout:0]
363 {
364 "stdin_port": 60387,
365 "ip": "127.0.0.1",
366 "hb_port": 50835,
367 "key": "eee2dd69-7dd3-4340-bf3e-7e2e22a62542",
368 "shell_port": 55328,
369 "iopub_port": 58264
370 }
371
372 Paste the above JSON into a file, and connect with:
373 $> ipython <app> --existing <file>
374 or, if you are local, you can connect with just:
375 $> ipython <app> --existing kernel-60125.json
376 or even just:
377 $> ipython <app> --existing
378 if this is the most recent IPython session you have started.
379 [stdout:1]
380 {
381 "stdin_port": 61869,
382 ...
383
384 .. note::
385
386 ``%qtconsole`` will call :func:`bind_kernel` on an engine if it hasn't been done already,
387 so you can often skip that first step.
388
389
@@ -10,6 +10,7 b' Using IPython for parallel computing'
10 parallel_intro.txt
10 parallel_intro.txt
11 parallel_process.txt
11 parallel_process.txt
12 parallel_multiengine.txt
12 parallel_multiengine.txt
13 magics.txt
13 parallel_task.txt
14 parallel_task.txt
14 asyncresult.txt
15 asyncresult.txt
15 parallel_mpi.txt
16 parallel_mpi.txt
@@ -387,229 +387,9 b' The following examples demonstrate how to use the instance attributes:'
387 The :attr:`block` and :attr:`targets` instance attributes of the
387 The :attr:`block` and :attr:`targets` instance attributes of the
388 :class:`.DirectView` also determine the behavior of the parallel magic commands.
388 :class:`.DirectView` also determine the behavior of the parallel magic commands.
389
389
390 Parallel magic commands
391 -----------------------
392
393 We provide a few IPython magic commands (``%px``, ``%autopx`` and ``%result``)
394 that make it a bit more pleasant to execute Python commands on the engines interactively.
395 These are simply shortcuts to :meth:`.DirectView.execute`
396 and :meth:`.AsyncResult.display_outputs` methods repsectively.
397 The ``%px`` magic executes a single Python command on the engines
398 specified by the :attr:`targets` attribute of the :class:`DirectView` instance:
399
400 .. sourcecode:: ipython
401
402 # Create a DirectView for all targets
403 In [22]: dv = rc[:]
404
405 # Make this DirectView active for parallel magic commands
406 In [23]: dv.activate()
407
408 In [24]: dv.block=True
409
410 # import numpy here and everywhere
411 In [25]: with dv.sync_imports():
412 ....: import numpy
413 importing numpy on engine(s)
414
415 In [27]: %px a = numpy.random.rand(2,2)
416 Parallel execution on engines: [0, 1, 2, 3]
417
418 In [28]: %px numpy.linalg.eigvals(a)
419 Parallel execution on engines: [0, 1, 2, 3]
420 [0] Out[68]: array([ 0.77120707, -0.19448286])
421 [1] Out[68]: array([ 1.10815921, 0.05110369])
422 [2] Out[68]: array([ 0.74625527, -0.37475081])
423 [3] Out[68]: array([ 0.72931905, 0.07159743])
424
425 In [29]: %px print 'hi'
426 Parallel execution on engine(s): [0, 1, 2, 3]
427 [stdout:0] hi
428 [stdout:1] hi
429 [stdout:2] hi
430 [stdout:3] hi
431
432
433 Since engines are IPython as well, you can even run magics remotely:
434
435 .. sourcecode:: ipython
436
437 In [28]: %px %pylab inline
438 Parallel execution on engine(s): [0, 1, 2, 3]
439 [stdout:0]
440 Welcome to pylab, a matplotlib-based Python environment...
441 For more information, type 'help(pylab)'.
442 [stdout:1]
443 Welcome to pylab, a matplotlib-based Python environment...
444 For more information, type 'help(pylab)'.
445 [stdout:2]
446 Welcome to pylab, a matplotlib-based Python environment...
447 For more information, type 'help(pylab)'.
448 [stdout:3]
449 Welcome to pylab, a matplotlib-based Python environment...
450 For more information, type 'help(pylab)'.
451
452 And once in pylab mode with the inline backend,
453 you can make plots and they will be displayed in your frontend
454 if it suports the inline figures (e.g. notebook or qtconsole):
455
456 .. sourcecode:: ipython
457
458 In [40]: %px plot(rand(100))
459 Parallel execution on engine(s): [0, 1, 2, 3]
460 <plot0>
461 <plot1>
462 <plot2>
463 <plot3>
464 [0] Out[79]: [<matplotlib.lines.Line2D at 0x10a6286d0>]
465 [1] Out[79]: [<matplotlib.lines.Line2D at 0x10b9476d0>]
466 [2] Out[79]: [<matplotlib.lines.Line2D at 0x110652750>]
467 [3] Out[79]: [<matplotlib.lines.Line2D at 0x10c6566d0>]
468
469
470 ``%%px`` Cell Magic
471 *******************
472
473 `%%px` can also be used as a Cell Magic, which accepts ``--[no]block`` flags,
474 and a ``--group-outputs`` argument, which adjust how the outputs of multiple
475 engines are presented.
476
477 .. seealso::
390 .. seealso::
478
391
479 :meth:`.AsyncResult.display_outputs` for the grouping options.
392 See the documentation of the :ref:`Parallel Magics <parallel_magics>`.
480
481 .. sourcecode:: ipython
482
483 In [50]: %%px --block --group-outputs=engine
484 ....: import numpy as np
485 ....: A = np.random.random((2,2))
486 ....: ev = numpy.linalg.eigvals(A)
487 ....: print ev
488 ....: ev.max()
489 ....:
490 Parallel execution on engine(s): [0, 1, 2, 3]
491 [stdout:0] [ 0.60640442 0.95919621]
492 [0] Out[73]: 0.9591962130899806
493 [stdout:1] [ 0.38501813 1.29430871]
494 [1] Out[73]: 1.2943087091452372
495 [stdout:2] [-0.85925141 0.9387692 ]
496 [2] Out[73]: 0.93876920456230284
497 [stdout:3] [ 0.37998269 1.24218246]
498 [3] Out[73]: 1.2421824618493817
499
500 ``%result`` Magic
501 *****************
502
503 If you are using ``%px`` in non-blocking mode, you won't get output.
504 You can use ``%result`` to display the outputs of the latest command,
505 just as is done when ``%px`` is blocking:
506
507 .. sourcecode:: ipython
508
509 In [39]: dv.block = False
510
511 In [40]: %px print 'hi'
512 Async parallel execution on engine(s): [0, 1, 2, 3]
513
514 In [41]: %result
515 [stdout:0] hi
516 [stdout:1] hi
517 [stdout:2] hi
518 [stdout:3] hi
519
520 ``%result`` simply calls :meth:`.AsyncResult.display_outputs` on the most recent request.
521 You can pass integers as indices if you want a result other than the latest,
522 e.g. ``%result -2``, or ``%result 0`` for the first.
523
524
525 ``%autopx``
526 ***********
527
528 The ``%autopx`` magic switches to a mode where everything you type is executed
529 on the engines until you do ``%autopx`` again.
530
531 .. sourcecode:: ipython
532
533 In [30]: dv.block=True
534
535 In [31]: %autopx
536 %autopx enabled
537
538 In [32]: max_evals = []
539
540 In [33]: for i in range(100):
541 ....: a = numpy.random.rand(10,10)
542 ....: a = a+a.transpose()
543 ....: evals = numpy.linalg.eigvals(a)
544 ....: max_evals.append(evals[0].real)
545 ....:
546
547 In [34]: print "Average max eigenvalue is: %f" % (sum(max_evals)/len(max_evals))
548 [stdout:0] Average max eigenvalue is: 10.193101
549 [stdout:1] Average max eigenvalue is: 10.064508
550 [stdout:2] Average max eigenvalue is: 10.055724
551 [stdout:3] Average max eigenvalue is: 10.086876
552
553 In [35]: %autopx
554 Auto Parallel Disabled
555
556
557 Engines as Kernels
558 ******************
559
560 Engines are really the same object as the Kernels used elsewhere in IPython,
561 with the minor exception that engines connect to a controller, while regular kernels
562 bind their sockets, listening for connections from a QtConsole or other frontends.
563
564 Sometimes for debugging or inspection purposes, you would like a QtConsole connected
565 to an engine for more direct interaction. You can do this by first instructing
566 the Engine to *also* bind its kernel, to listen for connections:
567
568 .. sourcecode:: ipython
569
570 In [50]: %px from IPython.parallel import bind_kernel; bind_kernel()
571
572 Then, if your engines are local, you can start a qtconsole right on the engine(s):
573
574 .. sourcecode:: ipython
575
576 In [51]: %px %qtconsole
577
578 Careful with this one, because if your view is of 16 engines it will start 16 QtConsoles!
579
580 Or you can view just the connection info, and work out the right way to connect to the engines,
581 depending on where they live and where you are:
582
583 .. sourcecode:: ipython
584
585 In [51]: %px %connect_info
586 Parallel execution on engine(s): [0, 1, 2, 3]
587 [stdout:0]
588 {
589 "stdin_port": 60387,
590 "ip": "127.0.0.1",
591 "hb_port": 50835,
592 "key": "eee2dd69-7dd3-4340-bf3e-7e2e22a62542",
593 "shell_port": 55328,
594 "iopub_port": 58264
595 }
596
597 Paste the above JSON into a file, and connect with:
598 $> ipython <app> --existing <file>
599 or, if you are local, you can connect with just:
600 $> ipython <app> --existing kernel-60125.json
601 or even just:
602 $> ipython <app> --existing
603 if this is the most recent IPython session you have started.
604 [stdout:1]
605 {
606 "stdin_port": 61869,
607 ...
608
609 .. note::
610
611 ``%qtconsole`` will call :func:`bind_kernel` on an engine if it hasn't been done already,
612 so you can often skip that first step.
613
393
614
394
615 Moving Python objects around
395 Moving Python objects around
General Comments 0
You need to be logged in to leave comments. Login now