Show More
@@ -0,0 +1,228 b'' | |||||
|
1 | { | |||
|
2 | "metadata": { | |||
|
3 | "name": "Parallel Magics" | |||
|
4 | }, | |||
|
5 | "nbformat": 3, | |||
|
6 | "worksheets": [ | |||
|
7 | { | |||
|
8 | "cells": [ | |||
|
9 | { | |||
|
10 | "cell_type": "heading", | |||
|
11 | "level": 1, | |||
|
12 | "source": [ | |||
|
13 | "Using Parallel Magics" | |||
|
14 | ] | |||
|
15 | }, | |||
|
16 | { | |||
|
17 | "cell_type": "markdown", | |||
|
18 | "source": [ | |||
|
19 | "IPython has a few magics for working with your engines.", | |||
|
20 | "", | |||
|
21 | "This assumes you have started an IPython cluster, either with the notebook interface,", | |||
|
22 | "or the `ipcluster/controller/engine` commands." | |||
|
23 | ] | |||
|
24 | }, | |||
|
25 | { | |||
|
26 | "cell_type": "code", | |||
|
27 | "collapsed": false, | |||
|
28 | "input": [ | |||
|
29 | "from IPython import parallel", | |||
|
30 | "rc = parallel.Client()", | |||
|
31 | "dv = rc[:]", | |||
|
32 | "dv.block = True", | |||
|
33 | "dv" | |||
|
34 | ], | |||
|
35 | "language": "python", | |||
|
36 | "outputs": [] | |||
|
37 | }, | |||
|
38 | { | |||
|
39 | "cell_type": "markdown", | |||
|
40 | "source": [ | |||
|
41 | "The parallel magics come from the `parallelmagics` IPython extension.", | |||
|
42 | "The magics are set to work with a particular View object,", | |||
|
43 | "so to activate them, you call the `activate()` method on a particular view:" | |||
|
44 | ] | |||
|
45 | }, | |||
|
46 | { | |||
|
47 | "cell_type": "code", | |||
|
48 | "collapsed": true, | |||
|
49 | "input": [ | |||
|
50 | "dv.activate()" | |||
|
51 | ], | |||
|
52 | "language": "python", | |||
|
53 | "outputs": [] | |||
|
54 | }, | |||
|
55 | { | |||
|
56 | "cell_type": "markdown", | |||
|
57 | "source": [ | |||
|
58 | "Now we can execute code remotely with `%px`:" | |||
|
59 | ] | |||
|
60 | }, | |||
|
61 | { | |||
|
62 | "cell_type": "code", | |||
|
63 | "collapsed": false, | |||
|
64 | "input": [ | |||
|
65 | "%px a=5" | |||
|
66 | ], | |||
|
67 | "language": "python", | |||
|
68 | "outputs": [] | |||
|
69 | }, | |||
|
70 | { | |||
|
71 | "cell_type": "code", | |||
|
72 | "collapsed": false, | |||
|
73 | "input": [ | |||
|
74 | "%px print a" | |||
|
75 | ], | |||
|
76 | "language": "python", | |||
|
77 | "outputs": [] | |||
|
78 | }, | |||
|
79 | { | |||
|
80 | "cell_type": "code", | |||
|
81 | "collapsed": false, | |||
|
82 | "input": [ | |||
|
83 | "%px a" | |||
|
84 | ], | |||
|
85 | "language": "python", | |||
|
86 | "outputs": [] | |||
|
87 | }, | |||
|
88 | { | |||
|
89 | "cell_type": "markdown", | |||
|
90 | "source": [ | |||
|
91 | "You don't have to wait for results:" | |||
|
92 | ] | |||
|
93 | }, | |||
|
94 | { | |||
|
95 | "cell_type": "code", | |||
|
96 | "collapsed": true, | |||
|
97 | "input": [ | |||
|
98 | "dv.block = False" | |||
|
99 | ], | |||
|
100 | "language": "python", | |||
|
101 | "outputs": [] | |||
|
102 | }, | |||
|
103 | { | |||
|
104 | "cell_type": "code", | |||
|
105 | "collapsed": false, | |||
|
106 | "input": [ | |||
|
107 | "%px import time", | |||
|
108 | "%px time.sleep(5)", | |||
|
109 | "%px time.time()" | |||
|
110 | ], | |||
|
111 | "language": "python", | |||
|
112 | "outputs": [] | |||
|
113 | }, | |||
|
114 | { | |||
|
115 | "cell_type": "markdown", | |||
|
116 | "source": [ | |||
|
117 | "But you will notice that this didn't output the result of the last command.", | |||
|
118 | "For this, we have `%result`, which displays the output of the latest request:" | |||
|
119 | ] | |||
|
120 | }, | |||
|
121 | { | |||
|
122 | "cell_type": "code", | |||
|
123 | "collapsed": false, | |||
|
124 | "input": [ | |||
|
125 | "%result" | |||
|
126 | ], | |||
|
127 | "language": "python", | |||
|
128 | "outputs": [] | |||
|
129 | }, | |||
|
130 | { | |||
|
131 | "cell_type": "markdown", | |||
|
132 | "source": [ | |||
|
133 | "Remember, an IPython engine is IPython, so you can do magics remotely as well!" | |||
|
134 | ] | |||
|
135 | }, | |||
|
136 | { | |||
|
137 | "cell_type": "code", | |||
|
138 | "collapsed": false, | |||
|
139 | "input": [ | |||
|
140 | "dv.block = True", | |||
|
141 | "%px %pylab inline" | |||
|
142 | ], | |||
|
143 | "language": "python", | |||
|
144 | "outputs": [] | |||
|
145 | }, | |||
|
146 | { | |||
|
147 | "cell_type": "markdown", | |||
|
148 | "source": [ | |||
|
149 | "`%%px` can also be used as a cell magic, for submitting whole blocks.", | |||
|
150 | "This one acceps `--block` and `--noblock` flags to specify", | |||
|
151 | "the blocking behavior, though the default is unchanged.", | |||
|
152 | "" | |||
|
153 | ] | |||
|
154 | }, | |||
|
155 | { | |||
|
156 | "cell_type": "code", | |||
|
157 | "collapsed": true, | |||
|
158 | "input": [ | |||
|
159 | "dv.scatter('id', dv.targets, flatten=True)", | |||
|
160 | "dv['stride'] = len(dv)" | |||
|
161 | ], | |||
|
162 | "language": "python", | |||
|
163 | "outputs": [] | |||
|
164 | }, | |||
|
165 | { | |||
|
166 | "cell_type": "code", | |||
|
167 | "collapsed": false, | |||
|
168 | "input": [ | |||
|
169 | "%%px --noblock", | |||
|
170 | "x = linspace(0,pi,1000)", | |||
|
171 | "for n in range(id,12, stride):", | |||
|
172 | " print n", | |||
|
173 | " plt.plot(x,sin(n*x))", | |||
|
174 | "plt.title(\"Plot %i\" % id)" | |||
|
175 | ], | |||
|
176 | "language": "python", | |||
|
177 | "outputs": [] | |||
|
178 | }, | |||
|
179 | { | |||
|
180 | "cell_type": "code", | |||
|
181 | "collapsed": false, | |||
|
182 | "input": [ | |||
|
183 | "%result" | |||
|
184 | ], | |||
|
185 | "language": "python", | |||
|
186 | "outputs": [] | |||
|
187 | }, | |||
|
188 | { | |||
|
189 | "cell_type": "markdown", | |||
|
190 | "source": [ | |||
|
191 | "It also lets you choose some amount of the grouping of the outputs with `--group-outputs`:", | |||
|
192 | "", | |||
|
193 | "The choices are:", | |||
|
194 | "", | |||
|
195 | "* `engine` - all of an engine's output is collected together", | |||
|
196 | "* `type` - where stdout of each engine is grouped, etc. (the default)", | |||
|
197 | "* `order` - same as `type`, but individual displaypub outputs are interleaved.", | |||
|
198 | " That is, it will output the first plot from each engine, then the second from each,", | |||
|
199 | " etc." | |||
|
200 | ] | |||
|
201 | }, | |||
|
202 | { | |||
|
203 | "cell_type": "code", | |||
|
204 | "collapsed": false, | |||
|
205 | "input": [ | |||
|
206 | "%%px --group-outputs=engine", | |||
|
207 | "x = linspace(0,pi,1000)", | |||
|
208 | "for n in range(id,12, stride):", | |||
|
209 | " print n", | |||
|
210 | " plt.plot(x,sin(n*x))", | |||
|
211 | "plt.title(\"Plot %i\" % id)" | |||
|
212 | ], | |||
|
213 | "language": "python", | |||
|
214 | "outputs": [] | |||
|
215 | }, | |||
|
216 | { | |||
|
217 | "cell_type": "code", | |||
|
218 | "collapsed": true, | |||
|
219 | "input": [ | |||
|
220 | "" | |||
|
221 | ], | |||
|
222 | "language": "python", | |||
|
223 | "outputs": [] | |||
|
224 | } | |||
|
225 | ] | |||
|
226 | } | |||
|
227 | ] | |||
|
228 | } No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now