##// END OF EJS Templates
add missing sys import
MinRK -
Show More
@@ -1,313 +1,323 b''
1 {
1 {
2 "metadata": {
2 "metadata": {
3 "name": "Parallel Magics"
3 "name": "Parallel Magics"
4 },
4 },
5 "nbformat": 3,
5 "nbformat": 3,
6 "worksheets": [
6 "worksheets": [
7 {
7 {
8 "cells": [
8 "cells": [
9 {
9 {
10 "cell_type": "heading",
10 "cell_type": "heading",
11 "level": 1,
11 "level": 1,
12 "source": [
12 "source": [
13 "Using Parallel Magics"
13 "Using Parallel Magics"
14 ]
14 ]
15 },
15 },
16 {
16 {
17 "cell_type": "markdown",
17 "cell_type": "markdown",
18 "source": [
18 "source": [
19 "IPython has a few magics for working with your engines.",
19 "IPython has a few magics for working with your engines.",
20 "",
20 "",
21 "This assumes you have started an IPython cluster, either with the notebook interface,",
21 "This assumes you have started an IPython cluster, either with the notebook interface,",
22 "or the `ipcluster/controller/engine` commands."
22 "or the `ipcluster/controller/engine` commands."
23 ]
23 ]
24 },
24 },
25 {
25 {
26 "cell_type": "code",
26 "cell_type": "code",
27 "collapsed": false,
27 "collapsed": false,
28 "input": [
28 "input": [
29 "from IPython import parallel",
29 "from IPython import parallel",
30 "rc = parallel.Client()",
30 "rc = parallel.Client()",
31 "dv = rc[:]",
31 "dv = rc[:]",
32 "dv.block = True",
32 "dv.block = True",
33 "dv"
33 "dv"
34 ],
34 ],
35 "language": "python",
35 "language": "python",
36 "outputs": []
36 "outputs": []
37 },
37 },
38 {
38 {
39 "cell_type": "markdown",
39 "cell_type": "markdown",
40 "source": [
40 "source": [
41 "The parallel magics come from the `parallelmagics` IPython extension.",
41 "The parallel magics come from the `parallelmagics` IPython extension.",
42 "The magics are set to work with a particular View object,",
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:"
43 "so to activate them, you call the `activate()` method on a particular view:"
44 ]
44 ]
45 },
45 },
46 {
46 {
47 "cell_type": "code",
47 "cell_type": "code",
48 "collapsed": true,
48 "collapsed": true,
49 "input": [
49 "input": [
50 "dv.activate()"
50 "dv.activate()"
51 ],
51 ],
52 "language": "python",
52 "language": "python",
53 "outputs": []
53 "outputs": []
54 },
54 },
55 {
55 {
56 "cell_type": "markdown",
56 "cell_type": "markdown",
57 "source": [
57 "source": [
58 "Now we can execute code remotely with `%px`:"
58 "Now we can execute code remotely with `%px`:"
59 ]
59 ]
60 },
60 },
61 {
61 {
62 "cell_type": "code",
62 "cell_type": "code",
63 "collapsed": false,
63 "collapsed": false,
64 "input": [
64 "input": [
65 "%px a=5"
65 "%px a=5"
66 ],
66 ],
67 "language": "python",
67 "language": "python",
68 "outputs": []
68 "outputs": []
69 },
69 },
70 {
70 {
71 "cell_type": "code",
71 "cell_type": "code",
72 "collapsed": false,
72 "collapsed": false,
73 "input": [
73 "input": [
74 "%px print a"
74 "%px print a"
75 ],
75 ],
76 "language": "python",
76 "language": "python",
77 "outputs": []
77 "outputs": []
78 },
78 },
79 {
79 {
80 "cell_type": "code",
80 "cell_type": "code",
81 "collapsed": false,
81 "collapsed": false,
82 "input": [
82 "input": [
83 "%px a"
83 "%px a"
84 ],
84 ],
85 "language": "python",
85 "language": "python",
86 "outputs": []
86 "outputs": []
87 },
87 },
88 {
88 {
89 "cell_type": "code",
89 "cell_type": "code",
90 "collapsed": false,
90 "collapsed": false,
91 "input": [
91 "input": [
92 "with dv.sync_imports():",
93 " import sys"
94 ],
95 "language": "python",
96 "outputs": []
97 },
98 {
99 "cell_type": "code",
100 "collapsed": false,
101 "input": [
92 "%px print >> sys.stderr, \"ERROR\""
102 "%px print >> sys.stderr, \"ERROR\""
93 ],
103 ],
94 "language": "python",
104 "language": "python",
95 "outputs": []
105 "outputs": []
96 },
106 },
97 {
107 {
98 "cell_type": "markdown",
108 "cell_type": "markdown",
99 "source": [
109 "source": [
100 "You don't have to wait for results:"
110 "You don't have to wait for results:"
101 ]
111 ]
102 },
112 },
103 {
113 {
104 "cell_type": "code",
114 "cell_type": "code",
105 "collapsed": true,
115 "collapsed": true,
106 "input": [
116 "input": [
107 "dv.block = False"
117 "dv.block = False"
108 ],
118 ],
109 "language": "python",
119 "language": "python",
110 "outputs": []
120 "outputs": []
111 },
121 },
112 {
122 {
113 "cell_type": "code",
123 "cell_type": "code",
114 "collapsed": false,
124 "collapsed": false,
115 "input": [
125 "input": [
116 "%px import time",
126 "%px import time",
117 "%px time.sleep(5)",
127 "%px time.sleep(5)",
118 "%px time.time()"
128 "%px time.time()"
119 ],
129 ],
120 "language": "python",
130 "language": "python",
121 "outputs": []
131 "outputs": []
122 },
132 },
123 {
133 {
124 "cell_type": "markdown",
134 "cell_type": "markdown",
125 "source": [
135 "source": [
126 "But you will notice that this didn't output the result of the last command.",
136 "But you will notice that this didn't output the result of the last command.",
127 "For this, we have `%result`, which displays the output of the latest request:"
137 "For this, we have `%result`, which displays the output of the latest request:"
128 ]
138 ]
129 },
139 },
130 {
140 {
131 "cell_type": "code",
141 "cell_type": "code",
132 "collapsed": false,
142 "collapsed": false,
133 "input": [
143 "input": [
134 "%result"
144 "%result"
135 ],
145 ],
136 "language": "python",
146 "language": "python",
137 "outputs": []
147 "outputs": []
138 },
148 },
139 {
149 {
140 "cell_type": "markdown",
150 "cell_type": "markdown",
141 "source": [
151 "source": [
142 "Remember, an IPython engine is IPython, so you can do magics remotely as well!"
152 "Remember, an IPython engine is IPython, so you can do magics remotely as well!"
143 ]
153 ]
144 },
154 },
145 {
155 {
146 "cell_type": "code",
156 "cell_type": "code",
147 "collapsed": false,
157 "collapsed": false,
148 "input": [
158 "input": [
149 "dv.block = True",
159 "dv.block = True",
150 "%px %pylab inline"
160 "%px %pylab inline"
151 ],
161 ],
152 "language": "python",
162 "language": "python",
153 "outputs": []
163 "outputs": []
154 },
164 },
155 {
165 {
156 "cell_type": "markdown",
166 "cell_type": "markdown",
157 "source": [
167 "source": [
158 "`%%px` can also be used as a cell magic, for submitting whole blocks.",
168 "`%%px` can also be used as a cell magic, for submitting whole blocks.",
159 "This one acceps `--block` and `--noblock` flags to specify",
169 "This one acceps `--block` and `--noblock` flags to specify",
160 "the blocking behavior, though the default is unchanged.",
170 "the blocking behavior, though the default is unchanged.",
161 ""
171 ""
162 ]
172 ]
163 },
173 },
164 {
174 {
165 "cell_type": "code",
175 "cell_type": "code",
166 "collapsed": true,
176 "collapsed": true,
167 "input": [
177 "input": [
168 "dv.scatter('id', dv.targets, flatten=True)",
178 "dv.scatter('id', dv.targets, flatten=True)",
169 "dv['stride'] = len(dv)"
179 "dv['stride'] = len(dv)"
170 ],
180 ],
171 "language": "python",
181 "language": "python",
172 "outputs": []
182 "outputs": []
173 },
183 },
174 {
184 {
175 "cell_type": "code",
185 "cell_type": "code",
176 "collapsed": false,
186 "collapsed": false,
177 "input": [
187 "input": [
178 "%%px --noblock",
188 "%%px --noblock",
179 "x = linspace(0,pi,1000)",
189 "x = linspace(0,pi,1000)",
180 "for n in range(id,12, stride):",
190 "for n in range(id,12, stride):",
181 " print n",
191 " print n",
182 " plt.plot(x,sin(n*x))",
192 " plt.plot(x,sin(n*x))",
183 "plt.title(\"Plot %i\" % id)"
193 "plt.title(\"Plot %i\" % id)"
184 ],
194 ],
185 "language": "python",
195 "language": "python",
186 "outputs": []
196 "outputs": []
187 },
197 },
188 {
198 {
189 "cell_type": "code",
199 "cell_type": "code",
190 "collapsed": false,
200 "collapsed": false,
191 "input": [
201 "input": [
192 "%result"
202 "%result"
193 ],
203 ],
194 "language": "python",
204 "language": "python",
195 "outputs": []
205 "outputs": []
196 },
206 },
197 {
207 {
198 "cell_type": "markdown",
208 "cell_type": "markdown",
199 "source": [
209 "source": [
200 "It also lets you choose some amount of the grouping of the outputs with `--group-outputs`:",
210 "It also lets you choose some amount of the grouping of the outputs with `--group-outputs`:",
201 "",
211 "",
202 "The choices are:",
212 "The choices are:",
203 "",
213 "",
204 "* `engine` - all of an engine's output is collected together",
214 "* `engine` - all of an engine's output is collected together",
205 "* `type` - where stdout of each engine is grouped, etc. (the default)",
215 "* `type` - where stdout of each engine is grouped, etc. (the default)",
206 "* `order` - same as `type`, but individual displaypub outputs are interleaved.",
216 "* `order` - same as `type`, but individual displaypub outputs are interleaved.",
207 " That is, it will output the first plot from each engine, then the second from each,",
217 " That is, it will output the first plot from each engine, then the second from each,",
208 " etc."
218 " etc."
209 ]
219 ]
210 },
220 },
211 {
221 {
212 "cell_type": "code",
222 "cell_type": "code",
213 "collapsed": false,
223 "collapsed": false,
214 "input": [
224 "input": [
215 "%%px --group-outputs=engine",
225 "%%px --group-outputs=engine",
216 "x = linspace(0,pi,1000)",
226 "x = linspace(0,pi,1000)",
217 "for n in range(id,12, stride):",
227 "for n in range(id,12, stride):",
218 " print n",
228 " print n",
219 " plt.figure()",
229 " plt.figure()",
220 " plt.plot(x,sin(n*x))",
230 " plt.plot(x,sin(n*x))",
221 "plt.title(\"Plot %i\" % id)"
231 "plt.title(\"Plot %i\" % id)"
222 ],
232 ],
223 "language": "python",
233 "language": "python",
224 "outputs": []
234 "outputs": []
225 },
235 },
226 {
236 {
227 "cell_type": "markdown",
237 "cell_type": "markdown",
228 "source": [
238 "source": [
229 "When you specify 'order', then individual display outputs (e.g. plots) will be interleaved:"
239 "When you specify 'order', then individual display outputs (e.g. plots) will be interleaved:"
230 ]
240 ]
231 },
241 },
232 {
242 {
233 "cell_type": "code",
243 "cell_type": "code",
234 "collapsed": false,
244 "collapsed": false,
235 "input": [
245 "input": [
236 "%%px --group-outputs=order",
246 "%%px --group-outputs=order",
237 "x = linspace(0,pi,1000)",
247 "x = linspace(0,pi,1000)",
238 "for n in range(id,12, stride):",
248 "for n in range(id,12, stride):",
239 " print n",
249 " print n",
240 " plt.figure()",
250 " plt.figure()",
241 " plt.plot(x,sin(n*x))",
251 " plt.plot(x,sin(n*x))",
242 "plt.title(\"Plot %i\" % id)"
252 "plt.title(\"Plot %i\" % id)"
243 ],
253 ],
244 "language": "python",
254 "language": "python",
245 "outputs": []
255 "outputs": []
246 },
256 },
247 {
257 {
248 "cell_type": "heading",
258 "cell_type": "heading",
249 "level": 2,
259 "level": 2,
250 "source": [
260 "source": [
251 "Single-engine views"
261 "Single-engine views"
252 ]
262 ]
253 },
263 },
254 {
264 {
255 "cell_type": "markdown",
265 "cell_type": "markdown",
256 "source": [
266 "source": [
257 "When a DirectView has a single target, the output is a bit simpler (no prefixes on stdout/err, etc.):"
267 "When a DirectView has a single target, the output is a bit simpler (no prefixes on stdout/err, etc.):"
258 ]
268 ]
259 },
269 },
260 {
270 {
261 "cell_type": "code",
271 "cell_type": "code",
262 "collapsed": true,
272 "collapsed": true,
263 "input": [
273 "input": [
264 "def generate_output():",
274 "def generate_output():",
265 " \"\"\"function for testing output",
275 " \"\"\"function for testing output",
266 " ",
276 " ",
267 " publishes two outputs of each type, and returns something",
277 " publishes two outputs of each type, and returns something",
268 " \"\"\"",
278 " \"\"\"",
269 " ",
279 " ",
270 " import sys,os",
280 " import sys,os",
271 " from IPython.core.display import display, HTML, Math",
281 " from IPython.core.display import display, HTML, Math",
272 " ",
282 " ",
273 " print \"stdout\"",
283 " print \"stdout\"",
274 " print >> sys.stderr, \"stderr\"",
284 " print >> sys.stderr, \"stderr\"",
275 " ",
285 " ",
276 " display(HTML(\"<b>HTML</b>\"))",
286 " display(HTML(\"<b>HTML</b>\"))",
277 " ",
287 " ",
278 " print \"stdout2\"",
288 " print \"stdout2\"",
279 " print >> sys.stderr, \"stderr2\"",
289 " print >> sys.stderr, \"stderr2\"",
280 " ",
290 " ",
281 " display(Math(r\"\\alpha=\\beta\"))",
291 " display(Math(r\"\\alpha=\\beta\"))",
282 " ",
292 " ",
283 " return os.getpid()",
293 " return os.getpid()",
284 "",
294 "",
285 "dv['generate_output'] = generate_output"
295 "dv['generate_output'] = generate_output"
286 ],
296 ],
287 "language": "python",
297 "language": "python",
288 "outputs": []
298 "outputs": []
289 },
299 },
290 {
300 {
291 "cell_type": "code",
301 "cell_type": "code",
292 "collapsed": true,
302 "collapsed": true,
293 "input": [
303 "input": [
294 "e0 = rc[-1]",
304 "e0 = rc[-1]",
295 "e0.block = True",
305 "e0.block = True",
296 "e0.activate()"
306 "e0.activate()"
297 ],
307 ],
298 "language": "python",
308 "language": "python",
299 "outputs": []
309 "outputs": []
300 },
310 },
301 {
311 {
302 "cell_type": "code",
312 "cell_type": "code",
303 "collapsed": false,
313 "collapsed": false,
304 "input": [
314 "input": [
305 "%px generate_output()"
315 "%px generate_output()"
306 ],
316 ],
307 "language": "python",
317 "language": "python",
308 "outputs": []
318 "outputs": []
309 }
319 }
310 ]
320 ]
311 }
321 }
312 ]
322 ]
313 } No newline at end of file
323 }
General Comments 0
You need to be logged in to leave comments. Login now