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