forked from serge-sans-paille/pythran-stories
-
Notifications
You must be signed in to change notification settings - Fork 0
/
costless-abstraction-with-pythran-broadcasting.html
613 lines (518 loc) · 41.5 KB
/
costless-abstraction-with-pythran-broadcasting.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Costless Abstraction with Pythran: Broadcasting</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="serge-sans-paille and other pythraners">
<!-- Le styles -->
<link rel="stylesheet" href="./theme/css/bootstrap.min.css" type="text/css" />
<style type="text/css">
body {
padding-top: 60px;
padding-bottom: 40px;
}
.sidebar-nav {
padding: 9px 0;
}
.tag-1 {
font-size: 13pt;
}
.tag-2 {
font-size: 10pt;
}
.tag-2 {
font-size: 8pt;
}
.tag-4 {
font-size: 6pt;
}
</style>
<link href="./theme/css/bootstrap-responsive.min.css" rel="stylesheet">
<link href="./theme/css/font-awesome.css" rel="stylesheet">
<link href="./theme/css/pygments.css" rel="stylesheet">
<!-- Le HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="//html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<!-- Le fav and touch icons -->
<link rel="shortcut icon" href="./theme/images/favicon.ico">
<link rel="apple-touch-icon" href="./theme/images/apple-touch-icon.png">
<link rel="apple-touch-icon" sizes="72x72" href="./theme/images/apple-touch-icon-72x72.png">
<link rel="apple-touch-icon" sizes="114x114" href="./theme/images/apple-touch-icon-114x114.png">
<link href="./" type="application/atom+xml" rel="alternate" title="Pythran stories ATOM Feed" />
</head>
<body>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container-fluid">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="./index.html">Pythran stories </a>
<div class="nav-collapse">
<ul class="nav">
<li class="divider-vertical"></li>
<li >
<a href="./category/benchmark.html">
<i class="icon-folder-open icon-large"></i>benchmark
</a>
</li>
<li >
<a href="./category/compilation.html">
<i class="icon-folder-open icon-large"></i>compilation
</a>
</li>
<li >
<a href="./category/cython.html">
<i class="icon-folder-open icon-large"></i>cython
</a>
</li>
<li >
<a href="./category/engineering.html">
<i class="icon-folder-open icon-large"></i>engineering
</a>
</li>
<li >
<a href="./category/examples.html">
<i class="icon-folder-open icon-large"></i>examples
</a>
</li>
<li class="active">
<a href="./category/optimisation.html">
<i class="icon-folder-open icon-large"></i>optimisation
</a>
</li>
<li >
<a href="./category/release.html">
<i class="icon-folder-open icon-large"></i>release
</a>
</li>
<ul class="nav pull-right">
<li><a href="./archives.html"><i class="icon-th-list"></i>Archives</a></li>
</ul>
</ul>
<!--<p class="navbar-text pull-right">Logged in as <a href="#">username</a></p>-->
</div><!--/.nav-collapse -->
</div>
</div>
</div>
<div class="container-fluid">
<div class="row">
<div class="span9" id="content">
<section id="content">
<article>
<header>
<h1>
<a href=""
rel="bookmark"
title="Permalink to Costless Abstraction with Pythran: Broadcasting">
Costless Abstraction with Pythran: Broadcasting
</a>
</h1>
</header>
<div class="entry-content">
<div class="well">
<footer class="post-info">
<span class="label">Date</span>
<abbr class="published" title="2016-05-25T00:00:00+02:00">
<i class="icon-calendar"></i>Wed 25 May 2016
</abbr>
<span class="label">By</span>
<a href="./author/serge-sans-paille.html"><i class="icon-user"></i>serge-sans-paille</a>
<span class="label">Category</span>
<a href="./category/optimisation.html"><i class="icon-folder-open"></i>optimisation</a>.
</footer><!-- /.post-info --> </div>
<p>This blogpost originally was a Jupyter Notebook. You can <a href="notebooks/broadcasting.ipynb">download it</a> if you want. The conversion was done using <code>nbconvert</code> and a <a href="notebooks/nbmarkdown.tpl">custom template</a> to match the style of the other part of the blog.</p>
<h1>Numpy's Broadcasting</h1>
<p>Broadcasting is a neat feature of Numpy (and other similar array-oriented languages like Matlab). It makes it possible to avoid explicit loops on arrays (they are particularly inefficient in Numpy), and improves the abstraction level of your code, which is a good thing if you share the same abstraction.</p>
<p>For instance, the addition between two 1D array when one of them only holds a single element is well defined: the single element is repeated along the axis:</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
</pre></div>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">10</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">10</span><span class="p">])</span>
<span class="o">>>></span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span>
</pre></div>
<div class="highlight"><pre><span></span>array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])
</pre></div>
<p>Which is very similar to the addition between an array and a scalar, <em>btw</em>.</p>
<p>So to store all the possible multiplication between two 1D arrays, one can create a new axis and turn them into 2D arrays, then use this broadcasting facility:</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">8</span><span class="p">]),</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">9</span><span class="p">])</span>
<span class="o">>>></span> <span class="n">a</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">,</span> <span class="p">:]</span> <span class="o">*</span> <span class="n">b</span><span class="p">[:,</span> <span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">]</span>
</pre></div>
<div class="highlight"><pre><span></span>array([[ 1, 2, 4, 8],
[ 3, 6, 12, 24],
[ 7, 14, 28, 56],
[ 9, 18, 36, 72]])
</pre></div>
<h1>Broadcasting and Pythran</h1>
<p>Pythran uses <a href="https://en.wikipedia.org/wiki/Expression_templates">expression templates</a> to optimize array expression, and end up with something that is similar to <a href="https://github.com/pydata/numexpr">numexpr</a> performance wise.</p>
<p>It's relatively easy for Pythran's expression template to broadcast between array and scalars, or between two arrays that don't have the same dimension, as the information required to perform the broadcasting is part of the type, thus it's known at compile time.</p>
<p>But the broadcasting described above only depends on the size, and Pythran generally does not have access to it at compile time. So a dynamic behavior is needed. Roughly speaking, instead of explicitly iterating over the expression template, iterators parametrized by a step are used. This step is equal to one for regular operands, and to zero for broadcast operands, which results in part of the operator always repeating itself.</p>
<p>What's its cost? Let's benchmark :-)</p>
<h2>Numpy implementation</h2>
<p>The original code performs a reduction over a broadcast multiplication. When doing so Numpy creates a temporary 2D array, then computes the sum. Using <code>None</code> for indexing is similar to <code>np.newaxis</code>.</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="k">def</span> <span class="nf">broadcast_numpy</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="o">...</span> <span class="k">return</span> <span class="p">(</span><span class="n">x</span><span class="p">[:,</span> <span class="bp">None</span><span class="p">]</span> <span class="o">*</span> <span class="n">y</span><span class="p">[</span><span class="bp">None</span><span class="p">,</span> <span class="p">:])</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>
</pre></div>
<h2>Pythran Implementation</h2>
<p>The Pythran implementation is straight-forward: just add the right annotation.</p>
<p><em>Note: The pythran magic is not available as is in pythran 0.7.4 or lower</em></p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="o">%</span><span class="n">load_ext</span> <span class="n">pythran</span><span class="o">.</span><span class="n">magic</span>
</pre></div>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="o">%%</span><span class="n">pythran</span> <span class="o">-</span><span class="n">O3</span>
<span class="o">>>></span> <span class="c1">#pythran export broadcast_pythran(float64[], float64[])</span>
<span class="o">>>></span> <span class="k">def</span> <span class="nf">broadcast_pythran</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="o">...</span> <span class="k">return</span> <span class="p">(</span><span class="n">x</span><span class="p">[:,</span> <span class="bp">None</span><span class="p">]</span> <span class="o">*</span> <span class="n">y</span><span class="p">[</span><span class="bp">None</span><span class="p">,</span> <span class="p">:])</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>
</pre></div>
<h2>Cython Implementation</h2>
<p>The Cython implementation makes the looping explicit. We use all the tricks we know to get a fast version: <code>@cython.boundscheck(False)</code>, <code>@cython.wraparound(False)</code> and a manual look at the output of <code>cython -a</code>.</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="o">%</span><span class="n">load_ext</span> <span class="n">Cython</span>
</pre></div>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="o">%%</span><span class="n">cython</span> <span class="o">--</span><span class="nb">compile</span><span class="o">-</span><span class="n">args</span><span class="o">=-</span><span class="n">O3</span>
<span class="o">>>></span>
<span class="o">>>></span> <span class="kn">import</span> <span class="nn">cython</span>
<span class="o">>>></span> <span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="o">>>></span> <span class="n">cimport</span> <span class="n">numpy</span> <span class="k">as</span> <span class="n">np</span>
<span class="o">>>></span>
<span class="o">>>></span> <span class="nd">@cython.boundscheck</span><span class="p">(</span><span class="bp">False</span><span class="p">)</span>
<span class="o">>>></span> <span class="nd">@cython.wraparound</span><span class="p">(</span><span class="bp">False</span><span class="p">)</span>
<span class="o">>>></span> <span class="k">def</span> <span class="nf">broadcast_cython</span><span class="p">(</span><span class="n">double</span><span class="p">[::</span><span class="mi">1</span><span class="p">]</span> <span class="n">x</span><span class="p">,</span> <span class="n">double</span><span class="p">[::</span><span class="mi">1</span><span class="p">]</span> <span class="n">y</span><span class="p">):</span>
<span class="o">...</span> <span class="n">cdef</span> <span class="nb">int</span> <span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="o">...</span> <span class="n">cdef</span> <span class="nb">int</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span>
<span class="o">...</span> <span class="n">cdef</span> <span class="n">double</span> <span class="n">res</span> <span class="o">=</span> <span class="mi">0</span>
<span class="o">...</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
<span class="o">...</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
<span class="o">...</span> <span class="n">res</span> <span class="o">+=</span> <span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="n">y</span><span class="p">[</span><span class="n">j</span><span class="p">]</span>
<span class="o">...</span> <span class="k">return</span> <span class="n">res</span>
</pre></div>
<h2>Numba Implementation</h2>
<p>The Numba version is very similar to the Cython one, without the need of declaring the actual types.</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="kn">import</span> <span class="nn">numba</span>
<span class="o">>>></span> <span class="nd">@numba.jit</span>
<span class="o">>>></span> <span class="k">def</span> <span class="nf">broadcast_numba</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="o">...</span> <span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="o">...</span> <span class="n">res</span> <span class="o">=</span> <span class="mi">0</span>
<span class="o">...</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
<span class="o">...</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
<span class="o">...</span> <span class="n">res</span> <span class="o">+=</span> <span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="n">y</span><span class="p">[</span><span class="n">j</span><span class="p">]</span>
<span class="o">...</span> <span class="k">return</span> <span class="n">res</span>
</pre></div>
<h2>Sanity Check</h2>
<p>Just to be sure all versions yield the same value :-)</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">OrderedDict</span>
<span class="o">>>></span> <span class="n">functions</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">()</span>
<span class="o">>>></span> <span class="n">functions</span><span class="p">[</span><span class="s1">'numpy'</span><span class="p">]</span> <span class="o">=</span> <span class="n">broadcast_numpy</span>
<span class="o">>>></span> <span class="n">functions</span><span class="p">[</span><span class="s1">'cython'</span><span class="p">]</span> <span class="o">=</span> <span class="n">broadcast_cython</span>
<span class="o">>>></span> <span class="n">functions</span><span class="p">[</span><span class="s1">'pythran'</span><span class="p">]</span> <span class="o">=</span> <span class="n">broadcast_pythran</span>
<span class="o">>>></span> <span class="n">functions</span><span class="p">[</span><span class="s1">'numba'</span><span class="p">]</span> <span class="o">=</span> <span class="n">broadcast_numba</span>
</pre></div>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">'float64'</span><span class="p">)</span>
<span class="o">>>></span> <span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">'float64'</span><span class="p">)</span>
<span class="o">>>></span> <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">function</span> <span class="ow">in</span> <span class="n">functions</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
<span class="o">...</span> <span class="k">print</span> <span class="n">name</span><span class="p">,</span> <span class="n">function</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
</pre></div>
<div class="highlight"><pre><span></span>numpy 19.3255679156
cython 19.3255679156
pythran 19.3255679156
numba 19.3255679156
</pre></div>
<h1>Benchmark</h1>
<p>The actual benchmark just runs each function through <code>timeit</code> for various array sizes.</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="kn">import</span> <span class="nn">timeit</span>
<span class="o">>>></span> <span class="n">sizes</span> <span class="o">=</span> <span class="p">[</span><span class="mf">1e3</span><span class="p">,</span> <span class="mf">5e3</span><span class="p">,</span> <span class="mf">1e4</span><span class="p">]</span>
<span class="o">>>></span> <span class="kn">import</span> <span class="nn">pandas</span>
<span class="o">>>></span> <span class="n">scores</span> <span class="o">=</span> <span class="n">pandas</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="n">functions</span><span class="o">.</span><span class="n">keys</span><span class="p">(),</span> <span class="n">index</span><span class="o">=</span><span class="n">sizes</span><span class="p">)</span>
<span class="o">>>></span> <span class="k">for</span> <span class="n">size</span> <span class="ow">in</span> <span class="n">sizes</span><span class="p">:</span>
<span class="o">...</span> <span class="n">size</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">size</span><span class="p">)</span>
<span class="o">...</span> <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">function</span> <span class="ow">in</span> <span class="n">functions</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
<span class="o">...</span> <span class="k">print</span> <span class="n">name</span><span class="p">,</span> <span class="s2">" "</span><span class="p">,</span>
<span class="o">...</span> <span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">(</span><span class="n">size</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">'float64'</span><span class="p">)</span>
<span class="o">...</span> <span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">(</span><span class="n">size</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">'float64'</span><span class="p">)</span>
<span class="o">...</span> <span class="n">result</span> <span class="o">=</span> <span class="o">%</span><span class="n">timeit</span> <span class="o">-</span><span class="n">o</span> <span class="n">function</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="o">...</span> <span class="n">scores</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="n">size</span><span class="p">,</span> <span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">result</span><span class="o">.</span><span class="n">best</span>
</pre></div>
<div class="highlight"><pre><span></span>numpy 100 loops, best of 3: 2 ms per loop
cython 1000 loops, best of 3: 875 µs per loop
pythran 1000 loops, best of 3: 852 µs per loop
numba 1000 loops, best of 3: 859 µs per loop
numpy 10 loops, best of 3: 82 ms per loop
cython 10 loops, best of 3: 21.9 ms per loop
pythran 10 loops, best of 3: 22 ms per loop
numba 10 loops, best of 3: 22.2 ms per loop
numpy 1 loop, best of 3: 253 ms per loop
cython 10 loops, best of 3: 85.4 ms per loop
pythran 10 loops, best of 3: 84.8 ms per loop
numba 10 loops, best of 3: 84.6 ms per loop
</pre></div>
<h2>Results (time in seconds, lower is better)</h2>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="n">scores</span>
</pre></div>
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>numpy</th>
<th>cython</th>
<th>pythran</th>
<th>numba</th>
</tr>
</thead>
<tbody>
<tr>
<th>1000.0</th>
<td>0.002001</td>
<td>0.000875</td>
<td>0.000852</td>
<td>0.000859</td>
</tr>
<tr>
<th>5000.0</th>
<td>0.082013</td>
<td>0.021908</td>
<td>0.021978</td>
<td>0.022195</td>
</tr>
<tr>
<th>10000.0</th>
<td>0.252877</td>
<td>0.085423</td>
<td>0.084839</td>
<td>0.084629</td>
</tr>
</tbody>
</table>
</div>
<h2>Comparison to Numpy time (lower is better)</h2>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="n">normalized_scores</span> <span class="o">=</span> <span class="n">scores</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
<span class="o">>>></span> <span class="k">for</span> <span class="n">column</span> <span class="ow">in</span> <span class="n">normalized_scores</span><span class="o">.</span><span class="n">columns</span><span class="p">:</span>
<span class="o">...</span> <span class="n">normalized_scores</span><span class="p">[</span><span class="n">column</span><span class="p">]</span> <span class="o">/=</span> <span class="n">scores</span><span class="p">[</span><span class="s1">'numpy'</span><span class="p">]</span>
</pre></div>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="n">normalized_scores</span>
</pre></div>
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>numpy</th>
<th>cython</th>
<th>pythran</th>
<th>numba</th>
</tr>
</thead>
<tbody>
<tr>
<th>1000.0</th>
<td>1.0</td>
<td>0.437434</td>
<td>0.425680</td>
<td>0.429456</td>
</tr>
<tr>
<th>5000.0</th>
<td>1.0</td>
<td>0.267123</td>
<td>0.267988</td>
<td>0.270626</td>
</tr>
<tr>
<th>10000.0</th>
<td>1.0</td>
<td>0.337803</td>
<td>0.335494</td>
<td>0.334665</td>
</tr>
</tbody>
</table>
</div>
<h2>Partial Conclusion</h2>
<p>At first glance, Cython, Pythran and Numba all manage to get a decent speedup over the Numpy version. So what's the point?</p>
<ol>
<li>Cython requires extra annotations, and explicit loops;</li>
<li>Numba only requires a decorator, but still explicit loops;</li>
<li>Pythran still requires a type annotation, but it keeps the Numpy abstraction.</li>
</ol>
<p>That's Pythran Leitmotiv: keep the Numpy abstraction, but try hard to make it run faster!</p>
<h1>Round Two: Using the compiler</h1>
<p>GCC (and Clang, and…) provide two flags that can be useful in this situation: <code>-Ofast</code> and <code>-march=native</code>. The former is generally equivalent to <code>-O3</code> with a few extra flags, most notably <code>-ffast-math</code> that disregards standard compliance with respect to floating point operation; In our case it makes it possible to reorder the operations to perform the final reduction using SIMD instructions. And with <code>-march=native</code>, the code gets specialized for the host architecture. In the case of this post (and the machine used to run the tests), it means it can use the <a href="https://en.wikipedia.org/wiki/Advanced_Vector_Extensions">AVX</a> instruction set and its 256bits vector register than can store four double precision floating!</p>
<p>In the Pythran case, vectorization is currently activated through the (somehow experimental) <code>-DUSE_BOOST_SIMD</code> flag.</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="o">%%</span><span class="n">pythran</span> <span class="o">-</span><span class="n">O3</span> <span class="o">-</span><span class="n">march</span><span class="o">=</span><span class="n">native</span> <span class="o">-</span><span class="n">DUSE_BOOST_SIMD</span>
<span class="o">>>></span> <span class="c1">#pythran export broadcast_pythran_simd(float64[], float64[])</span>
<span class="o">>>></span> <span class="k">def</span> <span class="nf">broadcast_pythran_simd</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="o">...</span> <span class="k">return</span> <span class="p">(</span><span class="n">x</span><span class="p">[:,</span> <span class="bp">None</span><span class="p">]</span> <span class="o">*</span> <span class="n">y</span><span class="p">[</span><span class="bp">None</span><span class="p">,</span> <span class="p">:])</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>
<span class="o">>>></span>
</pre></div>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="o">%%</span><span class="n">cython</span> <span class="o">-</span><span class="n">c</span><span class="o">=-</span><span class="n">Ofast</span> <span class="o">-</span><span class="n">c</span><span class="o">=-</span><span class="n">march</span><span class="o">=</span><span class="n">native</span>
<span class="o">>>></span>
<span class="o">>>></span> <span class="kn">import</span> <span class="nn">cython</span>
<span class="o">>>></span> <span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="o">>>></span> <span class="n">cimport</span> <span class="n">numpy</span> <span class="k">as</span> <span class="n">np</span>
<span class="o">>>></span>
<span class="o">>>></span> <span class="nd">@cython.boundscheck</span><span class="p">(</span><span class="bp">False</span><span class="p">)</span>
<span class="o">>>></span> <span class="nd">@cython.wraparound</span><span class="p">(</span><span class="bp">False</span><span class="p">)</span>
<span class="o">>>></span> <span class="k">def</span> <span class="nf">broadcast_cython_simd</span><span class="p">(</span><span class="n">double</span><span class="p">[::</span><span class="mi">1</span><span class="p">]</span> <span class="n">x</span><span class="p">,</span> <span class="n">double</span><span class="p">[::</span><span class="mi">1</span><span class="p">]</span> <span class="n">y</span><span class="p">):</span>
<span class="o">...</span> <span class="n">cdef</span> <span class="nb">int</span> <span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="o">...</span> <span class="n">cdef</span> <span class="nb">int</span> <span class="n">i</span><span class="p">,</span> <span class="n">j</span>
<span class="o">...</span> <span class="n">cdef</span> <span class="n">double</span> <span class="n">res</span> <span class="o">=</span> <span class="mi">0</span>
<span class="o">...</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
<span class="o">...</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
<span class="o">...</span> <span class="n">res</span> <span class="o">+=</span> <span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">*</span> <span class="n">y</span><span class="p">[</span><span class="n">j</span><span class="p">]</span>
<span class="o">...</span> <span class="k">return</span> <span class="n">res</span>
</pre></div>
<p>We can then rerun the previous benchmark, with these two functions:</p>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="n">simd_functions</span> <span class="o">=</span> <span class="n">OrderedDict</span><span class="p">()</span>
<span class="o">>>></span> <span class="n">simd_functions</span><span class="p">[</span><span class="s1">'numpy'</span><span class="p">]</span> <span class="o">=</span> <span class="n">broadcast_numpy</span>
<span class="o">>>></span> <span class="n">simd_functions</span><span class="p">[</span><span class="s1">'cython+simd'</span><span class="p">]</span> <span class="o">=</span> <span class="n">broadcast_cython_simd</span>
<span class="o">>>></span> <span class="n">simd_functions</span><span class="p">[</span><span class="s1">'pythran+simd'</span><span class="p">]</span> <span class="o">=</span> <span class="n">broadcast_pythran_simd</span>
<span class="o">>>></span> <span class="n">simd_scores</span> <span class="o">=</span> <span class="n">pandas</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="n">simd_functions</span><span class="o">.</span><span class="n">keys</span><span class="p">(),</span> <span class="n">index</span><span class="o">=</span><span class="n">sizes</span><span class="p">)</span>
<span class="o">>>></span> <span class="k">for</span> <span class="n">size</span> <span class="ow">in</span> <span class="n">sizes</span><span class="p">:</span>
<span class="o">...</span> <span class="n">size</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">size</span><span class="p">)</span>
<span class="o">...</span> <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">function</span> <span class="ow">in</span> <span class="n">simd_functions</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
<span class="o">...</span> <span class="k">print</span> <span class="n">name</span><span class="p">,</span> <span class="s2">" "</span><span class="p">,</span>
<span class="o">...</span> <span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">(</span><span class="n">size</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">'float64'</span><span class="p">)</span>
<span class="o">...</span> <span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">random</span><span class="p">(</span><span class="n">size</span><span class="p">)</span><span class="o">.</span><span class="n">astype</span><span class="p">(</span><span class="s1">'float64'</span><span class="p">)</span>
<span class="o">...</span> <span class="n">result</span> <span class="o">=</span> <span class="o">%</span><span class="n">timeit</span> <span class="o">-</span><span class="n">o</span> <span class="n">function</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="o">...</span> <span class="n">simd_scores</span><span class="o">.</span><span class="n">loc</span><span class="p">[</span><span class="n">size</span><span class="p">,</span> <span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">result</span><span class="o">.</span><span class="n">best</span>
</pre></div>
<div class="highlight"><pre><span></span>numpy 100 loops, best of 3: 1.86 ms per loop
cython+simd 1000 loops, best of 3: 207 µs per loop
pythran+simd 1000 loops, best of 3: 246 µs per loop
numpy 10 loops, best of 3: 80.7 ms per loop
cython+simd 100 loops, best of 3: 5.36 ms per loop
pythran+simd 100 loops, best of 3: 5.96 ms per loop
numpy 1 loop, best of 3: 250 ms per loop
cython+simd 10 loops, best of 3: 21.4 ms per loop
pythran+simd 10 loops, best of 3: 21.5 ms per loop
</pre></div>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="n">simd_scores</span>
</pre></div>
<div>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>numpy</th>
<th>cython+simd</th>
<th>pythran+simd</th>
</tr>
</thead>
<tbody>
<tr>
<th>1000.0</th>
<td>0.001864</td>
<td>0.000207</td>
<td>0.000246</td>
</tr>
<tr>
<th>5000.0</th>
<td>0.080706</td>
<td>0.005360</td>
<td>0.005961</td>
</tr>
<tr>
<th>10000.0</th>
<td>0.249898</td>
<td>0.021382</td>
<td>0.021472</td>
</tr>
</tbody>
</table>
</div>
<h2>Conclusion</h2>
<p>What happens there is that the underlying compiler is capable, on our simple case, to vectorize the loops and takes advantage of the vector register to speedup the computation. Although there's still a small overhead, Pythran is almost on par with Cython, even when vectorization is enabled, which means that the abstraction is still valid, even for complex feature like Numpy's broadcasting.</p>
<p>Under the hood though, the approach is totally different: Pythran vectorizes the expression template and generates calls to <a href="https://github.com/NumScale/boost.simd">boost.simd</a>, while Cython fully relies on GCC/clang auto-vectorizer, which proves to be a good approach until one meets a code compilers cannot vectorize!</p>
<h3>Technical info</h3>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="n">np</span><span class="o">.</span><span class="n">__version__</span>
</pre></div>
<div class="highlight"><pre><span></span>'1.11.0'
</pre></div>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="kn">import</span> <span class="nn">cython</span> <span class="p">;</span> <span class="n">cython</span><span class="o">.</span><span class="n">__version__</span>
</pre></div>
<div class="highlight"><pre><span></span>'0.24'
</pre></div>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="kn">import</span> <span class="nn">pythran</span><span class="p">;</span> <span class="n">pythran</span><span class="o">.</span><span class="n">__version__</span>
</pre></div>
<div class="highlight"><pre><span></span>'0.7.4.post1'
</pre></div>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="n">numba</span><span class="o">.</span><span class="n">__version__</span>
</pre></div>
<div class="highlight"><pre><span></span>'0.25.0'
</pre></div>
<div class="highlight"><pre><span></span><span class="o">>>></span> <span class="err">!</span><span class="n">g</span><span class="o">++</span> <span class="o">--</span><span class="n">version</span>
</pre></div>
<div class="highlight"><pre><span></span>g++-5.real (Debian 5.3.1-19) 5.3.1 20160509
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
</pre></div>
</div><!-- /.entry-content -->
</article>
</section>
</div><!--/span-->
<div class="span3 well sidebar-nav" id="sidebar">
<ul class="nav nav-list">
<li class="nav-header"><h4><i class="icon-external-link"></i>blogroll</h4></li>
<li><a href="http://pythonhosted.org/pythran"><i class="icon-external-link"></i>Pythran Doc</a></li>
<li><a href="https://pypi.python.org/pypi/pythran"><i class="icon-external-link"></i>Pythran on PyPI</a></li>
<li class="nav-header"><h4><i class="icon-home icon-large"></i> social</h4></li>
<li><a href="./feeds/all.atom.xml" rel="alternate"><i class="icon-bookmark icon-large"></i>atom feed</a></li>
<li><a href="https://github.com/serge-sans-paille/pythran"><i class="icon-github-sign icon-large"></i>github</a></li>
<li class="nav-header"><h4><i class="icon-folder-close icon-large"></i>Categories</h4></li>
<li>
<a href="./category/benchmark.html">
<i class="icon-folder-open icon-large"></i>benchmark
</a>
</li>
<li>
<a href="./category/compilation.html">
<i class="icon-folder-open icon-large"></i>compilation
</a>
</li>
<li>
<a href="./category/cython.html">
<i class="icon-folder-open icon-large"></i>cython
</a>
</li>
<li>
<a href="./category/engineering.html">
<i class="icon-folder-open icon-large"></i>engineering
</a>
</li>
<li>
<a href="./category/examples.html">
<i class="icon-folder-open icon-large"></i>examples
</a>
</li>
<li>
<a href="./category/optimisation.html">
<i class="icon-folder-open icon-large"></i>optimisation
</a>
</li>
<li>
<a href="./category/release.html">
<i class="icon-folder-open icon-large"></i>release
</a>
</li>
<li class="nav-header"><h4><i class="icon-tags icon-large"></i>Tags</h4></li>
</ul> </div><!--/.well -->
</div><!--/row-->
<hr>
<footer>
<address id="about">
Proudly powered by <a href="http://pelican.notmyidea.org/">Pelican <i class="icon-external-link"></i></a>,
which takes great advantage of <a href="http://python.org">Python <i class="icon-external-link"></i></a>.
</address><!-- /#about -->
<p>The theme is from <a href="http://twitter.github.com/bootstrap/">Bootstrap from Twitter <i class="icon-external-link"></i></a>,
and <a href="http://fortawesome.github.com/Font-Awesome/">Font-Awesome <i class="icon-external-link"></i></a>, thanks!</p>
</footer>
</div><!--/.fluid-container-->
<!-- Le javascript -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="./theme/js/jquery-1.7.2.min.js"></script>
<script src="./theme/js/bootstrap.min.js"></script>
</body>
</html>