Show More
@@ -120,21 +120,19 b' using our :func:`psum` function:' | |||
|
120 | 120 | |
|
121 | 121 | In [1]: from IPython.parallel import Client |
|
122 | 122 | |
|
123 | In [2]: %load_ext parallel_magic | |
|
124 | ||
|
125 | 123 | In [3]: c = Client(profile='mpi') |
|
126 | 124 | |
|
127 | 125 | In [4]: view = c[:] |
|
128 | 126 | |
|
129 | In [5]: view.activate() | |
|
127 | In [5]: view.activate() # enabe magics | |
|
130 | 128 | |
|
131 | 129 | # run the contents of the file on each engine: |
|
132 | 130 | In [6]: view.run('psum.py') |
|
133 | 131 | |
|
134 | In [6]: px a = np.random.rand(100) | |
|
132 | In [6]: %px a = np.random.rand(100) | |
|
135 | 133 | Parallel execution on engines: [0,1,2,3] |
|
136 | 134 | |
|
137 | In [8]: px s = psum(a) | |
|
135 | In [8]: %px s = psum(a) | |
|
138 | 136 | Parallel execution on engines: [0,1,2,3] |
|
139 | 137 | |
|
140 | 138 | In [9]: view['s'] |
@@ -389,11 +389,11 b' Parallel magic commands' | |||
|
389 | 389 | ----------------------- |
|
390 | 390 | |
|
391 | 391 | We provide a few IPython magic commands (``%px``, ``%autopx`` and ``%result``) |
|
392 | that make it more pleasant to execute Python commands on the engines | |
|
393 |
|
|
|
394 | :meth:`get_result` of the :class:`DirectView`. The ``%px`` magic executes a single | |
|
395 | Python command on the engines specified by the :attr:`targets` attribute of the | |
|
396 | :class:`DirectView` instance: | |
|
392 | that make it a bit more pleasant to execute Python commands on the engines interactively. | |
|
393 | These are simply shortcuts to :meth:`.DirectView.execute` | |
|
394 | and :meth:`.AsyncResult.display_outputs` methods repsectively. | |
|
395 | The ``%px`` magic executes a single Python command on the engines | |
|
396 | specified by the :attr:`targets` attribute of the :class:`DirectView` instance: | |
|
397 | 397 | |
|
398 | 398 | .. sourcecode:: ipython |
|
399 | 399 | |
@@ -413,43 +413,127 b' Python command on the engines specified by the :attr:`targets` attribute of the' | |||
|
413 | 413 | In [27]: %px a = numpy.random.rand(2,2) |
|
414 | 414 | Parallel execution on engines: [0, 1, 2, 3] |
|
415 | 415 | |
|
416 |
In [28]: %px |
|
|
416 | In [28]: %px numpy.linalg.eigvals(a) | |
|
417 | 417 | Parallel execution on engines: [0, 1, 2, 3] |
|
418 | [ 0] Out[68]: array([ 0.77120707, -0.19448286]) | |
|
419 | [ 1] Out[68]: array([ 1.10815921, 0.05110369]) | |
|
420 | [ 2] Out[68]: array([ 0.74625527, -0.37475081]) | |
|
421 | [ 3] Out[68]: array([ 0.72931905, 0.07159743]) | |
|
422 | ||
|
423 | In [29]: %px print 'hi' | |
|
424 | Parallel execution on engine(s): [0, 1, 2, 3] | |
|
425 | [stdout: 0] hi | |
|
426 | [stdout: 1] hi | |
|
427 | [stdout: 2] hi | |
|
428 | [stdout: 3] hi | |
|
429 | ||
|
430 | ||
|
431 | Since engines are IPython as well, you can even run magics remotely: | |
|
432 | ||
|
433 | .. sourcecode:: ipython | |
|
434 | ||
|
435 | In [28]: %px %pylab inline | |
|
436 | Parallel execution on engine(s): [0, 1, 2, 3] | |
|
437 | [stdout: 0] | |
|
438 | Welcome to pylab, a matplotlib-based Python environment... | |
|
439 | For more information, type 'help(pylab)'. | |
|
440 | [stdout: 1] | |
|
441 | Welcome to pylab, a matplotlib-based Python environment... | |
|
442 | For more information, type 'help(pylab)'. | |
|
443 | [stdout: 2] | |
|
444 | Welcome to pylab, a matplotlib-based Python environment... | |
|
445 | For more information, type 'help(pylab)'. | |
|
446 | [stdout: 3] | |
|
447 | Welcome to pylab, a matplotlib-based Python environment... | |
|
448 | For more information, type 'help(pylab)'. | |
|
449 | ||
|
450 | And once in pylab mode with the inline backend, | |
|
451 | you can make plots and they will be displayed in your frontend | |
|
452 | if it suports the inline figures (e.g. notebook or qtconsole): | |
|
453 | ||
|
454 | .. sourcecode:: ipython | |
|
455 | ||
|
456 | In [40]: %px plot(rand(100)) | |
|
457 | Parallel execution on engine(s): [0, 1, 2, 3] | |
|
458 | <plot0> | |
|
459 | <plot1> | |
|
460 | <plot2> | |
|
461 | <plot3> | |
|
462 | [ 0] Out[79]: [<matplotlib.lines.Line2D at 0x10a6286d0>] | |
|
463 | [ 1] Out[79]: [<matplotlib.lines.Line2D at 0x10b9476d0>] | |
|
464 | [ 2] Out[79]: [<matplotlib.lines.Line2D at 0x110652750>] | |
|
465 | [ 3] Out[79]: [<matplotlib.lines.Line2D at 0x10c6566d0>] | |
|
418 | 466 | |
|
419 | In [28]: dv['ev'] | |
|
420 | Out[28]: [ array([ 1.09522024, -0.09645227]), | |
|
421 | ....: array([ 1.21435496, -0.35546712]), | |
|
422 | ....: array([ 0.72180653, 0.07133042]), | |
|
423 | ....: array([ 1.46384341, 1.04353244e-04]) | |
|
424 | ....: ] | |
|
425 | 467 | |
|
426 | The ``%result`` magic gets the most recent result, or takes an argument | |
|
427 | specifying the index of the result to be requested. It is simply a shortcut to the | |
|
428 | :meth:`get_result` method: | |
|
468 | ``%%px`` Cell Magic | |
|
469 | ******************* | |
|
470 | ||
|
471 | `%%px` can also be used as a Cell Magic, which accepts ``--[no]block`` flags, | |
|
472 | and a ``--group-outputs`` argument, which adjust how the outputs of multiple | |
|
473 | engines are presented. | |
|
474 | ||
|
475 | .. seealso:: | |
|
476 | ||
|
477 | :meth:`.AsyncResult.display_outputs` for the grouping options. | |
|
429 | 478 | |
|
430 | 479 | .. sourcecode:: ipython |
|
480 | ||
|
481 | In [50]: %%px --block --group-outputs=engine | |
|
482 | ....: import numpy as np | |
|
483 | ....: A = np.random.random((2,2)) | |
|
484 | ....: ev = numpy.linalg.eigvals(A) | |
|
485 | ....: print ev | |
|
486 | ....: ev.max() | |
|
487 | ....: | |
|
488 | Parallel execution on engine(s): [0, 1, 2, 3] | |
|
489 | [stdout: 0] [ 0.60640442 0.95919621] | |
|
490 | [ 0] Out[73]: 0.9591962130899806 | |
|
491 | [stdout: 1] [ 0.38501813 1.29430871] | |
|
492 | [ 1] Out[73]: 1.2943087091452372 | |
|
493 | [stdout: 2] [-0.85925141 0.9387692 ] | |
|
494 | [ 2] Out[73]: 0.93876920456230284 | |
|
495 | [stdout: 3] [ 0.37998269 1.24218246] | |
|
496 | [ 3] Out[73]: 1.2421824618493817 | |
|
497 | ||
|
498 | ``%result`` Magic | |
|
499 | ***************** | |
|
500 | ||
|
501 | If you are using ``%px`` in non-blocking mode, you won't get output. | |
|
502 | You can use ``%result`` to display the outputs of the latest command, | |
|
503 | just as is done when ``%px`` is blocking: | |
|
504 | ||
|
505 | .. sourcecode:: ipython | |
|
506 | ||
|
507 | In [39]: dv.block = False | |
|
431 | 508 | |
|
432 | In [29]: dv.apply_async(lambda : ev) | |
|
509 | In [40]: %px print 'hi' | |
|
510 | Async parallel execution on engine(s): [0, 1, 2, 3] | |
|
433 | 511 | |
|
434 |
In [ |
|
|
435 | Out[30]: [ [ 1.28167017 0.14197338], | |
|
436 | ....: [-0.14093616 1.27877273], | |
|
437 | ....: [-0.37023573 1.06779409], | |
|
438 | ....: [ 0.83664764 -0.25602658] ] | |
|
512 | In [41]: %result | |
|
513 | [stdout: 0] hi | |
|
514 | [stdout: 1] hi | |
|
515 | [stdout: 2] hi | |
|
516 | [stdout: 3] hi | |
|
517 | ||
|
518 | ``%result`` simply calls :meth:`.AsyncResult.display_outputs` on the most recent request. | |
|
519 | You can pass integers as indices if you want a result other than the latest, | |
|
520 | e.g. ``%result -2``, or ``%result 0`` for the first. | |
|
521 | ||
|
522 | ||
|
523 | ``%autopx`` | |
|
524 | *********** | |
|
439 | 525 | |
|
440 | 526 | The ``%autopx`` magic switches to a mode where everything you type is executed |
|
441 | on the engines given by the :attr:`targets` attribute: | |
|
527 | on the engines until you do ``%autopx`` again. | |
|
442 | 528 | |
|
443 | 529 | .. sourcecode:: ipython |
|
444 | 530 | |
|
445 |
In [30]: dv.block= |
|
|
531 | In [30]: dv.block=True | |
|
446 | 532 | |
|
447 | 533 | In [31]: %autopx |
|
448 |
|
|
|
449 | Type %autopx to disable | |
|
534 | %autopx enabled | |
|
450 | 535 | |
|
451 | 536 | In [32]: max_evals = [] |
|
452 | <IPython.parallel.AsyncResult object at 0x17b8a70> | |
|
453 | 537 | |
|
454 | 538 | In [33]: for i in range(100): |
|
455 | 539 | ....: a = numpy.random.rand(10,10) |
@@ -457,22 +541,15 b' on the engines given by the :attr:`targets` attribute:' | |||
|
457 | 541 | ....: evals = numpy.linalg.eigvals(a) |
|
458 | 542 | ....: max_evals.append(evals[0].real) |
|
459 | 543 | ....: |
|
460 | ....: | |
|
461 | <IPython.parallel.AsyncResult object at 0x17af8f0> | |
|
462 | ||
|
463 | In [34]: %autopx | |
|
464 | Auto Parallel Disabled | |
|
465 | 544 | |
|
466 | In [35]: dv.block=True | |
|
467 | ||
|
468 | In [36]: px ans= "Average max eigenvalue is: %f"%(sum(max_evals)/len(max_evals)) | |
|
469 | Parallel execution on engines: [0, 1, 2, 3] | |
|
545 | In [34]: print "Average max eigenvalue is: %f" % (sum(max_evals)/len(max_evals)) | |
|
546 | [stdout: 0] Average max eigenvalue is: 10.193101 | |
|
547 | [stdout: 1] Average max eigenvalue is: 10.064508 | |
|
548 | [stdout: 2] Average max eigenvalue is: 10.055724 | |
|
549 | [stdout: 3] Average max eigenvalue is: 10.086876 | |
|
470 | 550 | |
|
471 |
In [3 |
|
|
472 | Out[37]: [ 'Average max eigenvalue is: 10.1387247332', | |
|
473 | ....: 'Average max eigenvalue is: 10.2076902286', | |
|
474 | ....: 'Average max eigenvalue is: 10.1891484655', | |
|
475 | ....: 'Average max eigenvalue is: 10.1158837784',] | |
|
551 | In [35]: %autopx | |
|
552 | Auto Parallel Disabled | |
|
476 | 553 | |
|
477 | 554 | |
|
478 | 555 | Moving Python objects around |
General Comments 0
You need to be logged in to leave comments.
Login now