-
Notifications
You must be signed in to change notification settings - Fork 6
/
nep-0010-new-iterator-ufunc.html
2616 lines (2355 loc) · 252 KB
/
nep-0010-new-iterator-ufunc.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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en" data-content_root="./" >
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<title>NEP 10 — Optimizing iterator/UFunc performance — NumPy Enhancement Proposals</title>
<script data-cfasync="false">
document.documentElement.dataset.mode = localStorage.getItem("mode") || "";
document.documentElement.dataset.theme = localStorage.getItem("theme") || "";
</script>
<!--
this give us a css class that will be invisible only if js is disabled
-->
<noscript>
<style>
.pst-js-only { display: none !important; }
</style>
</noscript>
<!-- Loaded before other Sphinx assets -->
<link href="_static/styles/theme.css?digest=8878045cc6db502f8baf" rel="stylesheet" />
<link href="_static/styles/pydata-sphinx-theme.css?digest=8878045cc6db502f8baf" rel="stylesheet" />
<link rel="stylesheet" type="text/css" href="_static/pygments.css?v=03e43079" />
<!-- So that users can add custom icons -->
<script src="_static/scripts/fontawesome.js?digest=8878045cc6db502f8baf"></script>
<!-- Pre-loaded scripts that we'll load fully later -->
<link rel="preload" as="script" href="_static/scripts/bootstrap.js?digest=8878045cc6db502f8baf" />
<link rel="preload" as="script" href="_static/scripts/pydata-sphinx-theme.js?digest=8878045cc6db502f8baf" />
<script src="_static/documentation_options.js?v=7f41d439"></script>
<script src="_static/doctools.js?v=888ff710"></script>
<script src="_static/sphinx_highlight.js?v=dc90522c"></script>
<script>DOCUMENTATION_OPTIONS.pagename = 'nep-0010-new-iterator-ufunc';</script>
<link rel="icon" href="_static/favicon.ico"/>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="NEP 13 — A mechanism for overriding Ufuncs" href="nep-0013-ufunc-overrides.html" />
<link rel="prev" title="NEP 7 — A proposal for implementing some date/time types in NumPy" href="nep-0007-datetime-proposal.html" />
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<meta name="docsearch:language" content="en"/>
<meta name="docsearch:version" content="" />
<meta name="docbuild:last-update" content="Jan 10, 2025"/>
</head>
<body data-bs-spy="scroll" data-bs-target=".bd-toc-nav" data-offset="180" data-bs-root-margin="0px 0px -60%" data-default-mode="">
<div id="pst-skip-link" class="skip-link d-print-none"><a href="#main-content">Skip to main content</a></div>
<div id="pst-scroll-pixel-helper"></div>
<button type="button" class="btn rounded-pill" id="pst-back-to-top">
<i class="fa-solid fa-arrow-up"></i>Back to top</button>
<dialog id="pst-search-dialog">
<form class="bd-search d-flex align-items-center"
action="search.html"
method="get">
<i class="fa-solid fa-magnifying-glass"></i>
<input type="search"
class="form-control"
name="q"
placeholder="Search the docs ..."
aria-label="Search the docs ..."
autocomplete="off"
autocorrect="off"
autocapitalize="off"
spellcheck="false"/>
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd>K</kbd></span>
</form>
</dialog>
<div class="pst-async-banner-revealer d-none">
<aside id="bd-header-version-warning" class="d-none d-print-none" aria-label="Version warning"></aside>
</div>
<header class="bd-header navbar navbar-expand-lg bd-navbar d-print-none">
<div class="bd-header__inner bd-page-width">
<button class="pst-navbar-icon sidebar-toggle primary-toggle" aria-label="Site navigation">
<span class="fa-solid fa-bars"></span>
</button>
<div class="col-lg-3 navbar-header-items__start">
<div class="navbar-item">
<a class="navbar-brand logo" href="content.html">
<img src="_static/numpylogo.svg" class="logo__image only-light" alt="NumPy Enhancement Proposals - Home"/>
<img src="_static/numpylogo.svg" class="logo__image only-dark pst-js-only" alt="NumPy Enhancement Proposals - Home"/>
</a></div>
</div>
<div class="col-lg-9 navbar-header-items">
<div class="me-auto navbar-header-items__center">
<div class="navbar-item">
<nav>
<ul class="bd-navbar-elements navbar-nav">
<li class="nav-item current active">
<a class="nav-link nav-internal" href="index.html">
Index
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="scope.html">
The Scope of NumPy
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="roadmap.html">
Current roadmap
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-external" href="https://github.com/numpy/numpy/issues?q=is%3Aopen+is%3Aissue+label%3A%2223+-+Wish+List%22">
Wish list
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-external" href="https://github.com/numpy/numpy/issues?q=is%3Aopen+is%3Aissue+label%3A%2223+-+Wish+List%22">
Wishlist
</a>
</li>
</ul>
</nav></div>
</div>
<div class="navbar-header-items__end">
<div class="navbar-item navbar-persistent--container">
<button class="btn search-button-field search-button__button pst-js-only" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="fa-solid fa-magnifying-glass"></i>
<span class="search-button__default-text">Search</span>
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd class="kbd-shortcut__modifier">K</kbd></span>
</button>
</div>
<div class="navbar-item">
<button class="btn btn-sm nav-link pst-navbar-icon theme-switch-button pst-js-only" aria-label="Color mode" data-bs-title="Color mode" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="theme-switch fa-solid fa-sun fa-lg" data-mode="light" title="Light"></i>
<i class="theme-switch fa-solid fa-moon fa-lg" data-mode="dark" title="Dark"></i>
<i class="theme-switch fa-solid fa-circle-half-stroke fa-lg" data-mode="auto" title="System Settings"></i>
</button></div>
<div class="navbar-item"><ul class="navbar-icon-links"
aria-label="Icon Links">
<li class="nav-item">
<a href="https://github.com/numpy/numpy" title="GitHub" class="nav-link pst-navbar-icon" rel="noopener" target="_blank" data-bs-toggle="tooltip" data-bs-placement="bottom"><i class="fa-brands fa-square-github fa-lg" aria-hidden="true"></i>
<span class="sr-only">GitHub</span></a>
</li>
</ul></div>
</div>
</div>
<div class="navbar-persistent--mobile">
<button class="btn search-button-field search-button__button pst-js-only" title="Search" aria-label="Search" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="fa-solid fa-magnifying-glass"></i>
<span class="search-button__default-text">Search</span>
<span class="search-button__kbd-shortcut"><kbd class="kbd-shortcut__modifier">Ctrl</kbd>+<kbd class="kbd-shortcut__modifier">K</kbd></span>
</button>
</div>
<button class="pst-navbar-icon sidebar-toggle secondary-toggle" aria-label="On this page">
<span class="fa-solid fa-outdent"></span>
</button>
</div>
</header>
<div class="bd-container">
<div class="bd-container__inner bd-page-width">
<dialog id="pst-primary-sidebar-modal"></dialog>
<div id="pst-primary-sidebar" class="bd-sidebar-primary bd-sidebar">
<div class="sidebar-header-items sidebar-primary__section">
<div class="sidebar-header-items__center">
<div class="navbar-item">
<nav>
<ul class="bd-navbar-elements navbar-nav">
<li class="nav-item current active">
<a class="nav-link nav-internal" href="index.html">
Index
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="scope.html">
The Scope of NumPy
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-internal" href="roadmap.html">
Current roadmap
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-external" href="https://github.com/numpy/numpy/issues?q=is%3Aopen+is%3Aissue+label%3A%2223+-+Wish+List%22">
Wish list
</a>
</li>
<li class="nav-item ">
<a class="nav-link nav-external" href="https://github.com/numpy/numpy/issues?q=is%3Aopen+is%3Aissue+label%3A%2223+-+Wish+List%22">
Wishlist
</a>
</li>
</ul>
</nav></div>
</div>
<div class="sidebar-header-items__end">
<div class="navbar-item">
<button class="btn btn-sm nav-link pst-navbar-icon theme-switch-button pst-js-only" aria-label="Color mode" data-bs-title="Color mode" data-bs-placement="bottom" data-bs-toggle="tooltip">
<i class="theme-switch fa-solid fa-sun fa-lg" data-mode="light" title="Light"></i>
<i class="theme-switch fa-solid fa-moon fa-lg" data-mode="dark" title="Dark"></i>
<i class="theme-switch fa-solid fa-circle-half-stroke fa-lg" data-mode="auto" title="System Settings"></i>
</button></div>
<div class="navbar-item"><ul class="navbar-icon-links"
aria-label="Icon Links">
<li class="nav-item">
<a href="https://github.com/numpy/numpy" title="GitHub" class="nav-link pst-navbar-icon" rel="noopener" target="_blank" data-bs-toggle="tooltip" data-bs-placement="bottom"><i class="fa-brands fa-square-github fa-lg" aria-hidden="true"></i>
<span class="sr-only">GitHub</span></a>
</li>
</ul></div>
</div>
</div>
<div class="sidebar-primary-items__start sidebar-primary__section">
<div class="sidebar-primary-item">
<nav class="bd-docs-nav bd-links"
aria-label="Section Navigation">
<p class="bd-links__title" role="heading" aria-level="1">Section Navigation</p>
<div class="bd-toc-item navbar-nav"><ul class="nav bd-sidenav">
<li class="toctree-l1"><a class="reference internal" href="scope.html">The Scope of NumPy</a></li>
<li class="toctree-l1"><a class="reference internal" href="roadmap.html">Current roadmap</a></li>
<li class="toctree-l1"><a class="reference external" href="https://github.com/numpy/numpy/issues?q=is%3Aopen+is%3Aissue+label%3A%2223+-+Wish+List%22">Wish list</a></li>
</ul>
<ul class="current nav bd-sidenav">
<li class="toctree-l1 has-children"><a class="reference internal" href="meta.html">Meta-NEPs (NEPs about NEPs or active Processes)</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l2"><a class="reference internal" href="nep-0000.html">NEP 0 — Purpose and process</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0023-backwards-compatibility.html">NEP 23 — Backwards compatibility and deprecation policy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0036-fair-play.html">NEP 36 — Fair play</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0045-c_style_guide.html">NEP 45 — C style guide</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0046-sponsorship-guidelines.html">NEP 46 — NumPy sponsorship guidelines</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0048-spending-project-funds.html">NEP 48 — Spending NumPy project funds</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-template.html">NEP X — Template and instructions</a></li>
</ul>
</details></li>
<li class="toctree-l1 has-children"><a class="reference internal" href="provisional.html">Provisional NEPs (provisionally accepted; interface may change)</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul class="simple">
</ul>
</details></li>
<li class="toctree-l1 has-children"><a class="reference internal" href="accepted.html">Accepted NEPs (implementation in progress)</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l2"><a class="reference internal" href="nep-0041-improved-dtype-support.html">NEP 41 — First step towards a new datatype system</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0042-new-dtypes.html">NEP 42 — New and extensible DTypes</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0044-restructuring-numpy-docs.html">NEP 44 — Restructuring the NumPy documentation</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0051-scalar-representation.html">NEP 51 — Changing the representation of NumPy scalars</a></li>
</ul>
</details></li>
<li class="toctree-l1 has-children"><a class="reference internal" href="open.html">Open NEPs (under consideration)</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l2"><a class="reference internal" href="nep-0043-extensible-ufuncs.html">NEP 43 — Enhancing the extensibility of UFuncs</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0053-c-abi-evolution.html">NEP 53 — Evolving the NumPy C-API for NumPy 2.0</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0054-simd-cpp-highway.html">NEP 54 — SIMD infrastructure evolution: adopting Google Highway when moving to C++?</a></li>
</ul>
</details></li>
<li class="toctree-l1 current active has-children"><a class="reference internal" href="finished.html">Finished NEPs</a><details open="open"><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="nep-0001-npy-format.html">NEP 1 — A simple file format for NumPy arrays</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0005-generalized-ufuncs.html">NEP 5 — Generalized universal functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0007-datetime-proposal.html">NEP 7 — A proposal for implementing some date/time types in NumPy</a></li>
<li class="toctree-l2 current active"><a class="current reference internal" href="#">NEP 10 — Optimizing iterator/UFunc performance</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0013-ufunc-overrides.html">NEP 13 — A mechanism for overriding Ufuncs</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0014-dropping-python2.7-proposal.html">NEP 14 — Plan for dropping Python 2.7 support</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0015-merge-multiarray-umath.html">NEP 15 — Merging multiarray and umath</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0018-array-function-protocol.html">NEP 18 — A dispatch mechanism for NumPy's high level array functions</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0019-rng-policy.html">NEP 19 — Random number generator policy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0020-gufunc-signature-enhancement.html">NEP 20 — Expansion of generalized universal function signatures</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0022-ndarray-duck-typing-overview.html">NEP 22 — Duck typing for NumPy arrays – high level overview</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0027-zero-rank-arrarys.html">NEP 27 — Zero rank arrays</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0028-website-redesign.html">NEP 28 — numpy.org website redesign</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0029-deprecation_policy.html">NEP 29 — Recommend Python and NumPy version support as a community policy standard</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0032-remove-financial-functions.html">NEP 32 — Remove the financial functions from NumPy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0034-infer-dtype-is-object.html">NEP 34 — Disallow inferring ``dtype=object`` from sequences</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0035-array-creation-dispatch-with-array-function.html">NEP 35 — Array creation dispatching with __array_function__</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0038-SIMD-optimizations.html">NEP 38 — Using SIMD optimization instructions for performance</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0040-legacy-datatype-impl.html">NEP 40 — Legacy datatype implementation in NumPy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0049.html">NEP 49 — Data allocation strategies</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0050-scalar-promotion.html">NEP 50 — Promotion rules for Python scalars</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0052-python-api-cleanup.html">NEP 52 — Python API cleanup for NumPy 2.0</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0055-string_dtype.html">NEP 55 — Add a UTF-8 variable-width string DType to NumPy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0056-array-api-main-namespace.html">NEP 56 — Array API standard support in NumPy's main namespace</a></li>
</ul>
</details></li>
<li class="toctree-l1 has-children"><a class="reference internal" href="deferred.html">Deferred and Superseded NEPs</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l2"><a class="reference internal" href="nep-0002-warnfix.html">NEP 2 — A proposal to build numpy without warning with a big set of warning flags</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0003-math_config_clean.html">NEP 3 — Cleaning the math configuration of numpy.core</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0004-datetime-proposal3.html">NEP 4 — A (third) proposal for implementing some date/time types in NumPy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0006-newbugtracker.html">NEP 6 — Replacing Trac with a different bug tracker</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0008-groupby_additions.html">NEP 8 — A proposal for adding groupby functionality to NumPy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0009-structured_array_extensions.html">NEP 9 — Structured array extensions</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0011-deferred-ufunc-evaluation.html">NEP 11 — Deferred UFunc evaluation</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0012-missing-data.html">NEP 12 — Missing data functionality in NumPy</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0021-advanced-indexing.html">NEP 21 — Simplified and explicit advanced indexing</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0024-missing-data-2.html">NEP 24 — Missing data functionality - alternative 1 to NEP 12</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0025-missing-data-3.html">NEP 25 — NA support via special dtypes</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0026-missing-data-summary.html">NEP 26 — Summary of missing data NEPs and discussion</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0030-duck-array-protocol.html">NEP 30 — Duck typing for NumPy arrays - implementation</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0031-uarray.html">NEP 31 — Context-local and global overrides of the NumPy API</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0037-array-module.html">NEP 37 — A dispatch protocol for NumPy-like modules</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0047-array-api-standard.html">NEP 47 — Adopting the array API standard</a></li>
</ul>
</details></li>
<li class="toctree-l1 has-children"><a class="reference internal" href="rejected.html">Rejected and Withdrawn NEPs</a><details><summary><span class="toctree-toggle" role="presentation"><i class="fa-solid fa-chevron-down"></i></span></summary><ul>
<li class="toctree-l2"><a class="reference internal" href="nep-0016-abstract-array.html">NEP 16 — An abstract base class for identifying "duck arrays"</a></li>
<li class="toctree-l2"><a class="reference internal" href="nep-0017-split-out-maskedarray.html">NEP 17 — Split out masked arrays</a></li>
</ul>
</details></li>
</ul>
</div>
</nav></div>
</div>
<div class="sidebar-primary-items__end sidebar-primary__section">
<div class="sidebar-primary-item">
<div id="ethical-ad-placement"
class="flat"
data-ea-publisher="readthedocs"
data-ea-type="readthedocs-sidebar"
data-ea-manual="true">
</div></div>
</div>
</div>
<main id="main-content" class="bd-main" role="main">
<div class="bd-content">
<div class="bd-article-container">
<div class="bd-header-article d-print-none">
<div class="header-article-items header-article__inner">
<div class="header-article-items__start">
<div class="header-article-item">
<nav aria-label="Breadcrumb" class="d-print-none">
<ul class="bd-breadcrumbs">
<li class="breadcrumb-item breadcrumb-home">
<a href="content.html" class="nav-link" aria-label="Home">
<i class="fa-solid fa-home"></i>
</a>
</li>
<li class="breadcrumb-item"><a href="index.html" class="nav-link">Roadmap & NumPy enhancement proposals</a></li>
<li class="breadcrumb-item"><a href="finished.html" class="nav-link">Finished NEPs</a></li>
<li class="breadcrumb-item active" aria-current="page"><span class="ellipsis">NEP 10 — Optimizing iterator/UFunc performance</span></li>
</ul>
</nav>
</div>
</div>
</div>
</div>
<div id="searchbox"></div>
<article class="bd-article">
<section id="nep-10-optimizing-iterator-ufunc-performance">
<span id="nep10"></span><h1><a class="toc-backref" href="#id1" role="doc-backlink">NEP 10 — Optimizing iterator/UFunc performance</a><a class="headerlink" href="#nep-10-optimizing-iterator-ufunc-performance" title="Link to this heading">#</a></h1>
<dl class="field-list simple">
<dt class="field-odd">Author<span class="colon">:</span></dt>
<dd class="field-odd"><p>Mark Wiebe <<a class="reference external" href="mailto:mwwiebe%40gmail.com">mwwiebe<span>@</span>gmail<span>.</span>com</a>></p>
</dd>
<dt class="field-even">Content-Type<span class="colon">:</span></dt>
<dd class="field-even"><p>text/x-rst</p>
</dd>
<dt class="field-odd">Created<span class="colon">:</span></dt>
<dd class="field-odd"><p>25-Nov-2010</p>
</dd>
<dt class="field-even">Status<span class="colon">:</span></dt>
<dd class="field-even"><p>Final</p>
</dd>
</dl>
<section id="table-of-contents">
<h2><a class="toc-backref" href="#id2" role="doc-backlink">Table of contents</a><a class="headerlink" href="#table-of-contents" title="Link to this heading">#</a></h2>
<nav class="contents" id="contents">
<p class="topic-title">Contents</p>
<ul class="simple">
<li><p><a class="reference internal" href="#nep-10-optimizing-iterator-ufunc-performance" id="id1">NEP 10 — Optimizing iterator/UFunc performance</a></p>
<ul>
<li><p><a class="reference internal" href="#table-of-contents" id="id2">Table of contents</a></p></li>
<li><p><a class="reference internal" href="#abstract" id="id3">Abstract</a></p></li>
<li><p><a class="reference internal" href="#motivation" id="id4">Motivation</a></p>
<ul>
<li><p><a class="reference internal" href="#image-compositing-example" id="id5">Image compositing example</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#improving-cache-coherency" id="id6">Improving cache-coherency</a></p>
<ul>
<li><p><a class="reference internal" href="#output-layout-selection-algorithm" id="id7">Output layout selection algorithm</a></p></li>
</ul>
</li>
<li><p><a class="reference internal" href="#coalescing-dimensions" id="id8">Coalescing dimensions</a></p></li>
<li><p><a class="reference internal" href="#inner-loop-specialization" id="id9">Inner loop specialization</a></p></li>
<li><p><a class="reference internal" href="#implementation-details" id="id10">Implementation details</a></p>
<ul>
<li><p><a class="reference internal" href="#iterator-rewrite" id="id11">Iterator rewrite</a></p></li>
<li><p><a class="reference internal" href="#proposed-iterator-memory-layout" id="id12">Proposed iterator memory layout</a></p></li>
<li><p><a class="reference internal" href="#proposed-iterator-api" id="id13">Proposed iterator API</a></p>
<ul>
<li><p><a class="reference internal" href="#old-new-iterator-api-conversion" id="id14">Old -> new iterator API conversion</a></p></li>
<li><p><a class="reference internal" href="#iterator-pointer-type" id="id15">Iterator pointer type</a></p></li>
<li><p><a class="reference internal" href="#construction-and-destruction" id="id16">Construction and destruction</a></p></li>
<li><p><a class="reference internal" href="#functions-for-iteration" id="id17">Functions for iteration</a></p></li>
<li><p><a class="reference internal" href="#examples" id="id18">Examples</a></p></li>
<li><p><a class="reference internal" href="#python-lambda-ufunc-example" id="id19">Python lambda UFunc example</a></p></li>
<li><p><a class="reference internal" href="#python-addition-example" id="id20">Python addition example</a></p></li>
<li><p><a class="reference internal" href="#image-compositing-example-revisited" id="id21">Image compositing example revisited</a></p></li>
<li><p><a class="reference internal" href="#image-compositing-with-numexpr" id="id22">Image compositing with NumExpr</a></p></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
</nav>
</section>
<section id="abstract">
<h2><a class="toc-backref" href="#id3" role="doc-backlink">Abstract</a><a class="headerlink" href="#abstract" title="Link to this heading">#</a></h2>
<p>This NEP proposes to replace the NumPy iterator and multi-iterator
with a single new iterator, designed to be more flexible and allow for
more cache-friendly data access. The new iterator also subsumes much
of the core ufunc functionality, making it easy to get the current
ufunc benefits in contexts which don’t precisely fit the ufunc mold.
Key benefits include:</p>
<ul class="simple">
<li><p>automatic reordering to find a cache-friendly access pattern</p></li>
<li><p>standard and customizable broadcasting</p></li>
<li><p>automatic type/byte-order/alignment conversions</p></li>
<li><p>optional buffering to minimize conversion memory usage</p></li>
<li><p>optional output arrays, with automatic allocation when unsupplied</p></li>
<li><p>automatic output or common type selection</p></li>
</ul>
<p>A large fraction of this iterator design has already been implemented with
promising results. Construction overhead is slightly greater (a.flat:
0.5 us, nditer(a): 1.4 us and broadcast(a,b): 1.4 us, nditer([a,b]):
2.2 us), but, as shown in an example, it is already possible to improve
on the performance of the built-in NumPy mechanisms in pure Python code
together with the iterator. One example rewrites np.add, getting a
four times improvement with some Fortran-contiguous arrays, and
another improves image compositing code from 1.4s to 180ms.</p>
<p>The implementation attempts to take into account
the design decisions made in the NumPy 2.0 refactor, to make its future
integration into libndarray relatively simple.</p>
</section>
<section id="motivation">
<h2><a class="toc-backref" href="#id4" role="doc-backlink">Motivation</a><a class="headerlink" href="#motivation" title="Link to this heading">#</a></h2>
<p>NumPy defaults to returning C-contiguous arrays from UFuncs. This can
result in extremely poor memory access patterns when dealing with data
that is structured differently. A simple timing example illustrates
this with a more than eight times performance hit from adding
Fortran-contiguous arrays together. All timings are done using NumPy
2.0dev (Nov 22, 2010) on an Athlon 64 X2 4200+, with a 64-bit OS.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">In</span> <span class="p">[</span><span class="mi">1</span><span class="p">]:</span> <span class="kn">import</span><span class="w"> </span><span class="nn">numpy</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">np</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">2</span><span class="p">]:</span> <span class="n">a</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">1000000</span><span class="p">,</span><span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span><span class="o">.</span><span class="n">reshape</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span><span class="mi">10</span><span class="p">,</span><span class="mi">10</span><span class="p">,</span><span class="mi">10</span><span class="p">,</span><span class="mi">10</span><span class="p">,</span><span class="mi">10</span><span class="p">)</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">3</span><span class="p">]:</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">d</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">copy</span><span class="p">(),</span> <span class="n">a</span><span class="o">.</span><span class="n">copy</span><span class="p">(),</span> <span class="n">a</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">4</span><span class="p">]:</span> <span class="n">timeit</span> <span class="n">a</span><span class="o">+</span><span class="n">b</span><span class="o">+</span><span class="n">c</span><span class="o">+</span><span class="n">d</span>
<span class="mi">10</span> <span class="n">loops</span><span class="p">,</span> <span class="n">best</span> <span class="n">of</span> <span class="mi">3</span><span class="p">:</span> <span class="mf">28.5</span> <span class="n">ms</span> <span class="n">per</span> <span class="n">loop</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">5</span><span class="p">]:</span> <span class="n">timeit</span> <span class="n">a</span><span class="o">.</span><span class="n">T</span><span class="o">+</span><span class="n">b</span><span class="o">.</span><span class="n">T</span><span class="o">+</span><span class="n">c</span><span class="o">.</span><span class="n">T</span><span class="o">+</span><span class="n">d</span><span class="o">.</span><span class="n">T</span>
<span class="mi">1</span> <span class="n">loops</span><span class="p">,</span> <span class="n">best</span> <span class="n">of</span> <span class="mi">3</span><span class="p">:</span> <span class="mi">237</span> <span class="n">ms</span> <span class="n">per</span> <span class="n">loop</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">6</span><span class="p">]:</span> <span class="n">timeit</span> <span class="n">a</span><span class="o">.</span><span class="n">T</span><span class="o">.</span><span class="n">ravel</span><span class="p">(</span><span class="s1">'A'</span><span class="p">)</span><span class="o">+</span><span class="n">b</span><span class="o">.</span><span class="n">T</span><span class="o">.</span><span class="n">ravel</span><span class="p">(</span><span class="s1">'A'</span><span class="p">)</span><span class="o">+</span><span class="n">c</span><span class="o">.</span><span class="n">T</span><span class="o">.</span><span class="n">ravel</span><span class="p">(</span><span class="s1">'A'</span><span class="p">)</span><span class="o">+</span><span class="n">d</span><span class="o">.</span><span class="n">T</span><span class="o">.</span><span class="n">ravel</span><span class="p">(</span><span class="s1">'A'</span><span class="p">)</span>
<span class="mi">10</span> <span class="n">loops</span><span class="p">,</span> <span class="n">best</span> <span class="n">of</span> <span class="mi">3</span><span class="p">:</span> <span class="mf">29.6</span> <span class="n">ms</span> <span class="n">per</span> <span class="n">loop</span>
</pre></div>
</div>
<p>In this case, it is simple to recover the performance by switching to
a view of the memory, adding, then reshaping back. To further examine
the problem and see how it isn’t always as trivial to work around,
let’s consider simple code for working with image buffers in NumPy.</p>
<section id="image-compositing-example">
<h3><a class="toc-backref" href="#id5" role="doc-backlink">Image compositing example</a><a class="headerlink" href="#image-compositing-example" title="Link to this heading">#</a></h3>
<p>For a more realistic example, consider an image buffer. Images are
generally stored in a Fortran-contiguous order, and the colour
channel can be treated as either a structured ‘RGB’ type or an extra
dimension of length three. The resulting memory layout is neither C-
nor Fortran-contiguous, but is easy to work with directly in NumPy,
because of the flexibility of the ndarray. This appears ideal, because
it makes the memory layout compatible with typical C or C++ image code,
while simultaneously giving natural access in Python. Getting the color
of pixel (x,y) is just ‘image[x,y]’.</p>
<p>The performance of this layout in NumPy turns out to be very poor.
Here is code which creates two black images, and does an ‘over’
compositing operation on them.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">In</span> <span class="p">[</span><span class="mi">9</span><span class="p">]:</span> <span class="n">image1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">1080</span><span class="p">,</span><span class="mi">1920</span><span class="p">,</span><span class="mi">3</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span><span class="o">.</span><span class="n">swapaxes</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">10</span><span class="p">]:</span> <span class="n">alpha1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">1080</span><span class="p">,</span><span class="mi">1920</span><span class="p">,</span><span class="mi">1</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span><span class="o">.</span><span class="n">swapaxes</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">11</span><span class="p">]:</span> <span class="n">image2</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">1080</span><span class="p">,</span><span class="mi">1920</span><span class="p">,</span><span class="mi">3</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span><span class="o">.</span><span class="n">swapaxes</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">12</span><span class="p">]:</span> <span class="n">alpha2</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">1080</span><span class="p">,</span><span class="mi">1920</span><span class="p">,</span><span class="mi">1</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span><span class="o">.</span><span class="n">swapaxes</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">)</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">13</span><span class="p">]:</span> <span class="k">def</span><span class="w"> </span><span class="nf">composite_over</span><span class="p">(</span><span class="n">im1</span><span class="p">,</span> <span class="n">al1</span><span class="p">,</span> <span class="n">im2</span><span class="p">,</span> <span class="n">al2</span><span class="p">):</span>
<span class="o">....</span><span class="p">:</span> <span class="k">return</span> <span class="p">(</span><span class="n">im1</span> <span class="o">+</span> <span class="p">(</span><span class="mi">1</span><span class="o">-</span><span class="n">al1</span><span class="p">)</span><span class="o">*</span><span class="n">im2</span><span class="p">,</span> <span class="n">al1</span> <span class="o">+</span> <span class="p">(</span><span class="mi">1</span><span class="o">-</span><span class="n">al1</span><span class="p">)</span><span class="o">*</span><span class="n">al2</span><span class="p">)</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">14</span><span class="p">]:</span> <span class="n">timeit</span> <span class="n">composite_over</span><span class="p">(</span><span class="n">image1</span><span class="p">,</span><span class="n">alpha1</span><span class="p">,</span><span class="n">image2</span><span class="p">,</span><span class="n">alpha2</span><span class="p">)</span>
<span class="mi">1</span> <span class="n">loops</span><span class="p">,</span> <span class="n">best</span> <span class="n">of</span> <span class="mi">3</span><span class="p">:</span> <span class="mf">3.51</span> <span class="n">s</span> <span class="n">per</span> <span class="n">loop</span>
</pre></div>
</div>
<p>If we give up the convenient layout, and use the C-contiguous default,
the performance is about seven times better.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">In</span> <span class="p">[</span><span class="mi">16</span><span class="p">]:</span> <span class="n">image1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">1080</span><span class="p">,</span><span class="mi">1920</span><span class="p">,</span><span class="mi">3</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">17</span><span class="p">]:</span> <span class="n">alpha1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">1080</span><span class="p">,</span><span class="mi">1920</span><span class="p">,</span><span class="mi">1</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">18</span><span class="p">]:</span> <span class="n">image2</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">1080</span><span class="p">,</span><span class="mi">1920</span><span class="p">,</span><span class="mi">3</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">19</span><span class="p">]:</span> <span class="n">alpha2</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">1080</span><span class="p">,</span><span class="mi">1920</span><span class="p">,</span><span class="mi">1</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">20</span><span class="p">]:</span> <span class="n">timeit</span> <span class="n">composite_over</span><span class="p">(</span><span class="n">image1</span><span class="p">,</span><span class="n">alpha1</span><span class="p">,</span><span class="n">image2</span><span class="p">,</span><span class="n">alpha2</span><span class="p">)</span>
<span class="mi">1</span> <span class="n">loops</span><span class="p">,</span> <span class="n">best</span> <span class="n">of</span> <span class="mi">3</span><span class="p">:</span> <span class="mi">581</span> <span class="n">ms</span> <span class="n">per</span> <span class="n">loop</span>
</pre></div>
</div>
<p>But this is not all, since it turns out that broadcasting the alpha
channel is exacting a performance price as well. If we use an alpha
channel with 3 values instead of one, we get:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">In</span> <span class="p">[</span><span class="mi">21</span><span class="p">]:</span> <span class="n">image1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">1080</span><span class="p">,</span><span class="mi">1920</span><span class="p">,</span><span class="mi">3</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">22</span><span class="p">]:</span> <span class="n">alpha1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">1080</span><span class="p">,</span><span class="mi">1920</span><span class="p">,</span><span class="mi">3</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">23</span><span class="p">]:</span> <span class="n">image2</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">1080</span><span class="p">,</span><span class="mi">1920</span><span class="p">,</span><span class="mi">3</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">24</span><span class="p">]:</span> <span class="n">alpha2</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">1080</span><span class="p">,</span><span class="mi">1920</span><span class="p">,</span><span class="mi">3</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">25</span><span class="p">]:</span> <span class="n">timeit</span> <span class="n">composite_over</span><span class="p">(</span><span class="n">image1</span><span class="p">,</span><span class="n">alpha1</span><span class="p">,</span><span class="n">image2</span><span class="p">,</span><span class="n">alpha2</span><span class="p">)</span>
<span class="mi">1</span> <span class="n">loops</span><span class="p">,</span> <span class="n">best</span> <span class="n">of</span> <span class="mi">3</span><span class="p">:</span> <span class="mi">313</span> <span class="n">ms</span> <span class="n">per</span> <span class="n">loop</span>
</pre></div>
</div>
<p>For a final comparison, let’s see how it performs when we use
one-dimensional arrays to ensure just a single loop does the
calculation.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">In</span> <span class="p">[</span><span class="mi">26</span><span class="p">]:</span> <span class="n">image1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">1080</span><span class="o">*</span><span class="mi">1920</span><span class="o">*</span><span class="mi">3</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">27</span><span class="p">]:</span> <span class="n">alpha1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">1080</span><span class="o">*</span><span class="mi">1920</span><span class="o">*</span><span class="mi">3</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">28</span><span class="p">]:</span> <span class="n">image2</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">1080</span><span class="o">*</span><span class="mi">1920</span><span class="o">*</span><span class="mi">3</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">29</span><span class="p">]:</span> <span class="n">alpha2</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">1080</span><span class="o">*</span><span class="mi">1920</span><span class="o">*</span><span class="mi">3</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">30</span><span class="p">]:</span> <span class="n">timeit</span> <span class="n">composite_over</span><span class="p">(</span><span class="n">image1</span><span class="p">,</span><span class="n">alpha1</span><span class="p">,</span><span class="n">image2</span><span class="p">,</span><span class="n">alpha2</span><span class="p">)</span>
<span class="mi">1</span> <span class="n">loops</span><span class="p">,</span> <span class="n">best</span> <span class="n">of</span> <span class="mi">3</span><span class="p">:</span> <span class="mi">312</span> <span class="n">ms</span> <span class="n">per</span> <span class="n">loop</span>
</pre></div>
</div>
<p>To get a reference performance number, I implemented this simple operation
straightforwardly in C (careful to use the same compile options as NumPy).
If I emulated the memory allocation and layout of the Python code, the
performance was roughly 0.3 seconds, very much in line with NumPy’s
performance. Combining the operations into one pass reduced the time
to roughly 0.15 seconds.</p>
<p>A slight variation of this example is to use a single memory block
with four channels (1920,1080,4) instead of separate image and alpha.
This is more typical in image processing applications, and here’s how
that looks with a C-contiguous layout.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">In</span> <span class="p">[</span><span class="mi">31</span><span class="p">]:</span> <span class="n">image1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">1080</span><span class="p">,</span><span class="mi">1920</span><span class="p">,</span><span class="mi">4</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">32</span><span class="p">]:</span> <span class="n">image2</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">1080</span><span class="p">,</span><span class="mi">1920</span><span class="p">,</span><span class="mi">4</span><span class="p">),</span> <span class="n">dtype</span><span class="o">=</span><span class="n">np</span><span class="o">.</span><span class="n">float32</span><span class="p">)</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">33</span><span class="p">]:</span> <span class="k">def</span><span class="w"> </span><span class="nf">composite_over</span><span class="p">(</span><span class="n">im1</span><span class="p">,</span> <span class="n">im2</span><span class="p">):</span>
<span class="o">....</span><span class="p">:</span> <span class="n">ret</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span><span class="o">-</span><span class="n">im1</span><span class="p">[:,:,</span><span class="o">-</span><span class="mi">1</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="o">*</span><span class="n">im2</span>
<span class="o">....</span><span class="p">:</span> <span class="n">ret</span> <span class="o">+=</span> <span class="n">im1</span>
<span class="o">....</span><span class="p">:</span> <span class="k">return</span> <span class="n">ret</span>
<span class="n">In</span> <span class="p">[</span><span class="mi">34</span><span class="p">]:</span> <span class="n">timeit</span> <span class="n">composite_over</span><span class="p">(</span><span class="n">image1</span><span class="p">,</span><span class="n">image2</span><span class="p">)</span>
<span class="mi">1</span> <span class="n">loops</span><span class="p">,</span> <span class="n">best</span> <span class="n">of</span> <span class="mi">3</span><span class="p">:</span> <span class="mi">481</span> <span class="n">ms</span> <span class="n">per</span> <span class="n">loop</span>
</pre></div>
</div>
<p>To see the improvements that implementation of the new iterator as
proposed can produce, go to the example continued after the
proposed API, near the bottom of the document.</p>
</section>
</section>
<section id="improving-cache-coherency">
<h2><a class="toc-backref" href="#id6" role="doc-backlink">Improving cache-coherency</a><a class="headerlink" href="#improving-cache-coherency" title="Link to this heading">#</a></h2>
<p>In order to get the best performance from UFunc calls, the pattern of
memory reads should be as regular as possible. Modern CPUs attempt to
predict the memory read/write pattern and fill the cache ahead of time.
The most predictable pattern is for all the inputs and outputs to be
sequentially processed in the same order.</p>
<p>I propose that by default, the memory layout of the UFunc outputs be as
close to that of the inputs as possible. Whenever there is an ambiguity
or a mismatch, it defaults to a C-contiguous layout.</p>
<p>To understand how to accomplish this, we first consider the strides of
all the inputs after the shapes have been normalized for broadcasting.
By determining whether a set of strides are compatible and/or ambiguous,
we can determine an output memory layout which maximizes coherency.</p>
<p>In broadcasting, the input shapes are first transformed to broadcast
shapes by prepending singular dimensions, then the broadcast strides
are created, where any singular dimension’s stride is set to zero.</p>
<p>Strides may be negative as well, and in certain cases this can be
normalized to fit the following discussion. If all the strides for a
particular axis are negative or zero, the strides for that dimension
can be negated after adjusting the base data pointers appropriately.</p>
<p>Here’s an example of how three inputs with C-contiguous layouts result in
broadcast strides. To simplify things, the examples use an itemsize of 1.</p>
<div class="pst-scrollable-table-container"><table class="table">
<tbody>
<tr class="row-odd"><td><p>Input shapes:</p></td>
<td><p>(5,3,7)</p></td>
<td><p>(5,3,1)</p></td>
<td><p>(1,7)</p></td>
</tr>
<tr class="row-even"><td><p>Broadcast shapes:</p></td>
<td><p>(5,3,7)</p></td>
<td><p>(5,3,1)</p></td>
<td><p>(1,1,7)</p></td>
</tr>
<tr class="row-odd"><td><p>Broadcast strides:</p></td>
<td><p>(21,7,1)</p></td>
<td><p>(3,1,0)</p></td>
<td><p>(0,0,1)</p></td>
</tr>
</tbody>
</table>
</div>
<p><em>Compatible Strides</em> - A set of strides are compatible if there exists
a permutation of the axes such that the strides are decreasing for every
stride in the set, excluding entries that are zero.</p>
<p>The example above satisfies the definition with the identity permutation.
In the motivation image example, the strides are slightly different if
we separate the colour and alpha information or not. The permutation
which demonstrates compatibility here is the transposition (0,1).</p>
<div class="pst-scrollable-table-container"><table class="table">
<tbody>
<tr class="row-odd"><td><p>Input/Broadcast shapes:</p></td>
<td><p>Image (1920, 1080, 3)</p></td>
<td><p>Alpha (1920, 1080, 1)</p></td>
</tr>
<tr class="row-even"><td><p>Broadcast strides (separate):</p></td>
<td><p>(3,5760,1)</p></td>
<td><p>(1,1920,0)</p></td>
</tr>
<tr class="row-odd"><td><p>Broadcast strides (together):</p></td>
<td><p>(4,7680,1)</p></td>
<td><p>(4,7680,0)</p></td>
</tr>
</tbody>
</table>
</div>
<p><em>Ambiguous Strides</em> - A set of compatible strides are ambiguous if
more than one permutation of the axes exists such that the strides are
decreasing for every stride in the set, excluding entries that are zero.</p>
<p>This typically occurs when every axis has a 0-stride somewhere in the
set of strides. The simplest example is in two dimensions, as follows.</p>
<div class="pst-scrollable-table-container"><table class="table">
<tbody>
<tr class="row-odd"><td><p>Broadcast shapes:</p></td>
<td><p>(1,3)</p></td>
<td><p>(5,1)</p></td>
</tr>
<tr class="row-even"><td><p>Broadcast strides:</p></td>
<td><p>(0,1)</p></td>
<td><p>(1,0)</p></td>
</tr>
</tbody>
</table>
</div>
<p>There may, however, be unambiguous compatible strides without a single
input forcing the entire layout, as in this example:</p>
<div class="pst-scrollable-table-container"><table class="table">
<tbody>
<tr class="row-odd"><td><p>Broadcast shapes:</p></td>
<td><p>(1,3,4)</p></td>
<td><p>(5,3,1)</p></td>
</tr>
<tr class="row-even"><td><p>Broadcast strides:</p></td>
<td><p>(0,4,1)</p></td>
<td><p>(3,1,0)</p></td>
</tr>
</tbody>
</table>
</div>
<p>In the face of ambiguity, we have a choice to either completely throw away
the fact that the strides are compatible, or try to resolve the ambiguity
by adding an additional constraint. I think the appropriate choice
is to resolve it by picking the memory layout closest to C-contiguous,
but still compatible with the input strides.</p>
<section id="output-layout-selection-algorithm">
<h3><a class="toc-backref" href="#id7" role="doc-backlink">Output layout selection algorithm</a><a class="headerlink" href="#output-layout-selection-algorithm" title="Link to this heading">#</a></h3>
<p>The output ndarray memory layout we would like to produce is as follows:</p>
<div class="pst-scrollable-table-container"><table class="table">
<tbody>
<tr class="row-odd"><td><p>Consistent/Unambiguous strides:</p></td>
<td><p>The single consistent layout</p></td>
</tr>
<tr class="row-even"><td><p>Consistent/Ambiguous strides:</p></td>
<td><p>The consistent layout closest to C-contiguous</p></td>
</tr>
<tr class="row-odd"><td><p>Inconsistent strides:</p></td>
<td><p>C-contiguous</p></td>
</tr>
</tbody>
</table>
</div>
<p>Here is pseudo-code for an algorithm to compute the permutation for the
output layout.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">perm</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="n">ndim</span><span class="p">)</span> <span class="c1"># Identity, i.e. C-contiguous</span>
<span class="c1"># Insertion sort, ignoring 0-strides</span>
<span class="c1"># Note that the sort must be stable, and 0-strides may</span>
<span class="c1"># be reordered if necessary, but should be moved as little</span>
<span class="c1"># as possible.</span>
<span class="k">for</span> <span class="n">i0</span> <span class="o">=</span> <span class="mi">1</span> <span class="n">to</span> <span class="n">ndim</span><span class="o">-</span><span class="mi">1</span><span class="p">:</span>
<span class="c1"># ipos is where perm[i0] will get inserted</span>
<span class="n">ipos</span> <span class="o">=</span> <span class="n">i0</span>
<span class="n">j0</span> <span class="o">=</span> <span class="n">perm</span><span class="p">[</span><span class="n">i0</span><span class="p">]</span>
<span class="k">for</span> <span class="n">i1</span> <span class="o">=</span> <span class="n">i0</span><span class="o">-</span><span class="mi">1</span> <span class="n">to</span> <span class="mi">0</span><span class="p">:</span>
<span class="n">j1</span> <span class="o">=</span> <span class="n">perm</span><span class="p">[</span><span class="n">i1</span><span class="p">]</span>
<span class="n">ambig</span><span class="p">,</span> <span class="n">shouldswap</span> <span class="o">=</span> <span class="kc">True</span><span class="p">,</span> <span class="kc">False</span>
<span class="c1"># Check whether any strides are ordered wrong</span>
<span class="k">for</span> <span class="n">strides</span> <span class="ow">in</span> <span class="n">broadcast_strides</span><span class="p">:</span>
<span class="k">if</span> <span class="n">strides</span><span class="p">[</span><span class="n">j0</span><span class="p">]</span> <span class="o">!=</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">strides</span><span class="p">[</span><span class="n">j1</span><span class="p">]</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">:</span>
<span class="k">if</span> <span class="n">strides</span><span class="p">[</span><span class="n">j0</span><span class="p">]</span> <span class="o">></span> <span class="n">strides</span><span class="p">[</span><span class="n">j1</span><span class="p">]:</span>
<span class="c1"># Only set swap if it's still ambiguous.</span>
<span class="k">if</span> <span class="n">ambig</span><span class="p">:</span>
<span class="n">shouldswap</span> <span class="o">=</span> <span class="kc">True</span>
<span class="k">else</span><span class="p">:</span>
<span class="c1"># Set swap even if it's not ambiguous,</span>
<span class="c1"># because not swapping is the choice</span>
<span class="c1"># for conflicts as well.</span>
<span class="n">shouldswap</span> <span class="o">=</span> <span class="kc">False</span>
<span class="n">ambig</span> <span class="o">=</span> <span class="kc">False</span>
<span class="c1"># If there was an unambiguous comparison, either shift ipos</span>
<span class="c1"># to i1 or stop looking for the comparison</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">ambig</span><span class="p">:</span>
<span class="k">if</span> <span class="n">shouldswap</span><span class="p">:</span>
<span class="n">ipos</span> <span class="o">=</span> <span class="n">i1</span>
<span class="k">else</span><span class="p">:</span>
<span class="k">break</span>
<span class="c1"># Insert perm[i0] into the right place</span>
<span class="k">if</span> <span class="n">ipos</span> <span class="o">!=</span> <span class="n">i0</span><span class="p">:</span>
<span class="k">for</span> <span class="n">i1</span> <span class="o">=</span> <span class="n">i0</span><span class="o">-</span><span class="mi">1</span> <span class="n">to</span> <span class="n">ipos</span><span class="p">:</span>
<span class="n">perm</span><span class="p">[</span><span class="n">i1</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">perm</span><span class="p">[</span><span class="n">i1</span><span class="p">]</span>
<span class="n">perm</span><span class="p">[</span><span class="n">ipos</span><span class="p">]</span> <span class="o">=</span> <span class="n">j0</span>
<span class="c1"># perm is now the closest consistent ordering to C-contiguous</span>
<span class="k">return</span> <span class="n">perm</span>
</pre></div>
</div>
</section>
</section>
<section id="coalescing-dimensions">
<h2><a class="toc-backref" href="#id8" role="doc-backlink">Coalescing dimensions</a><a class="headerlink" href="#coalescing-dimensions" title="Link to this heading">#</a></h2>
<p>In many cases, the memory layout allows for the use of a one-dimensional
loop instead of tracking multiple coordinates within the iterator.
The existing code already exploits this when the data is C-contiguous,
but since we’re reordering the axes, we can apply this optimization
more generally.</p>
<p>Once the iteration strides have been sorted to be monotonically
decreasing, any dimensions which could be coalesced are side by side.
If for all the operands, incrementing by strides[i+1] shape[i+1] times
is the same as incrementing by strides[i], or strides[i+1]*shape[i+1] ==
strides[i], dimensions i and i+1 can be coalesced into a single dimension.</p>
<p>Here is pseudo-code for coalescing.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># Figure out which pairs of dimensions can be coalesced</span>
<span class="n">can_coalesce</span> <span class="o">=</span> <span class="p">[</span><span class="kc">False</span><span class="p">]</span><span class="o">*</span><span class="n">ndim</span>
<span class="k">for</span> <span class="n">strides</span><span class="p">,</span> <span class="n">shape</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">broadcast_strides</span><span class="p">,</span> <span class="n">broadcast_shape</span><span class="p">):</span>
<span class="k">for</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span> <span class="n">to</span> <span class="n">ndim</span><span class="o">-</span><span class="mi">2</span><span class="p">:</span>
<span class="k">if</span> <span class="n">strides</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span><span class="o">*</span><span class="n">shape</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="n">strides</span><span class="p">[</span><span class="n">i</span><span class="p">]:</span>
<span class="n">can_coalesce</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="kc">True</span>
<span class="c1"># Coalesce the types</span>
<span class="n">new_ndim</span> <span class="o">=</span> <span class="n">ndim</span> <span class="o">-</span> <span class="n">count_nonzero</span><span class="p">(</span><span class="n">can_coalesce</span><span class="p">)</span>
<span class="k">for</span> <span class="n">strides</span><span class="p">,</span> <span class="n">shape</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">broadcast_strides</span><span class="p">,</span> <span class="n">broadcast_shape</span><span class="p">):</span>
<span class="n">j</span> <span class="o">=</span> <span class="mi">0</span>
<span class="k">for</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span> <span class="n">to</span> <span class="n">ndim</span><span class="o">-</span><span class="mi">1</span><span class="p">:</span>
<span class="c1"># Note that can_coalesce[ndim-1] is always False, so</span>
<span class="c1"># there is no out-of-bounds access here.</span>
<span class="k">if</span> <span class="n">can_coalesce</span><span class="p">[</span><span class="n">i</span><span class="p">]:</span>
<span class="n">shape</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">shape</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">*</span><span class="n">shape</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">strides</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">strides</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
<span class="n">shape</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">shape</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>
<span class="n">j</span> <span class="o">+=</span> <span class="mi">1</span>
</pre></div>
</div>
</section>
<section id="inner-loop-specialization">
<h2><a class="toc-backref" href="#id9" role="doc-backlink">Inner loop specialization</a><a class="headerlink" href="#inner-loop-specialization" title="Link to this heading">#</a></h2>
<p>Specialization is handled purely by the inner loop function, so this
optimization is independent of the others. Some specialization is
already done, like for the reduce operation. The idea is mentioned in
<a class="reference external" href="http://projects.scipy.org/numpy/wiki/ProjectIdeas">http://projects.scipy.org/numpy/wiki/ProjectIdeas</a>, “use intrinsics
(SSE-instructions) to speed up low-level loops in NumPy.”</p>
<p>Here are some possibilities for two-argument functions,
covering the important cases of add/subtract/multiply/divide.</p>
<ul class="simple">
<li><p>The first or second argument is a single value (i.e. a 0 stride
value) and does not alias the output. arr = arr + 1; arr = 1 + arr</p>
<ul>
<li><p>Can load the constant once instead of reloading it from memory every time</p></li>
</ul>
</li>
<li><p>The strides match the size of the data type. C- or
Fortran-contiguous data, for example</p>
<ul>
<li><p>Can do a simple loop without using strides</p></li>
</ul>
</li>
<li><p>The strides match the size of the data type, and they are
both 16-byte aligned (or differ from 16-byte aligned by the same offset)</p>
<ul>
<li><p>Can use SSE to process multiple values at once</p></li>
</ul>
</li>
<li><p>The first input and the output are the same single value
(i.e. a reduction operation).</p>
<ul>
<li><p>This is already specialized for many UFuncs in the existing code</p></li>
</ul>
</li>
</ul>
<p>The above cases are not generally mutually exclusive, for example a
constant argument may be combined with SSE when the strides match the
data type size, and reductions can be optimized with SSE as well.</p>
</section>
<section id="implementation-details">
<h2><a class="toc-backref" href="#id10" role="doc-backlink">Implementation details</a><a class="headerlink" href="#implementation-details" title="Link to this heading">#</a></h2>
<p>Except for inner loop specialization, the discussed
optimizations significantly affect ufunc_object.c and the
PyArrayIterObject/PyArrayMultiIterObject used to do the broadcasting.
In general, it should be possible to emulate the current behavior where it
is desired, but I believe the default should be to produce and manipulate
memory layouts which will give the best performance.</p>
<p>To support the new cache-friendly behavior, we introduce a new
option ‘K’ (for “keep”) for any <code class="docutils literal notranslate"><span class="pre">order=</span></code> parameter.</p>
<p>The proposed ‘order=’ flags become as follows:</p>
<div class="pst-scrollable-table-container"><table class="table">
<tbody>
<tr class="row-odd"><td><p>‘C’</p></td>
<td><p>C-contiguous layout</p></td>
</tr>
<tr class="row-even"><td><p>‘F’</p></td>
<td><p>Fortran-contiguous layout</p></td>
</tr>
<tr class="row-odd"><td><p>‘A’</p></td>
<td><p>‘F’ if the input(s) have a Fortran-contiguous layout, ‘C’ otherwise (“Any Contiguous”)</p></td>
</tr>
<tr class="row-even"><td><p>‘K’</p></td>
<td><p>a layout equivalent to ‘C’ followed by some permutation of the axes, as close to the layout of the input(s) as possible (“Keep Layout”)</p></td>
</tr>
</tbody>
</table>
</div>
<p>Or as an enum:</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/* For specifying array memory layout or iteration order */</span>
<span class="k">typedef</span><span class="w"> </span><span class="k">enum</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="cm">/* Fortran order if inputs are all Fortran, C otherwise */</span>
<span class="w"> </span><span class="n">NPY_ANYORDER</span><span class="o">=</span><span class="mi">-1</span><span class="p">,</span>
<span class="w"> </span><span class="cm">/* C order */</span>
<span class="w"> </span><span class="n">NPY_CORDER</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>
<span class="w"> </span><span class="cm">/* Fortran order */</span>
<span class="w"> </span><span class="n">NPY_FORTRANORDER</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
<span class="w"> </span><span class="cm">/* An order as close to the inputs as possible */</span>
<span class="w"> </span><span class="n">NPY_KEEPORDER</span><span class="o">=</span><span class="mi">2</span>
<span class="p">}</span><span class="w"> </span><span class="n">NPY_ORDER</span><span class="p">;</span>
</pre></div>
</div>
<p>Perhaps a good strategy is to first implement the capabilities discussed
here without changing the defaults. Once they are implemented and
well-tested, the defaults can change from <code class="docutils literal notranslate"><span class="pre">order='C'</span></code> to <code class="docutils literal notranslate"><span class="pre">order='K'</span></code>
everywhere appropriate. UFuncs additionally should gain an <code class="docutils literal notranslate"><span class="pre">order=</span></code>
parameter to control the layout of their output(s).</p>
<p>The iterator can do automatic casting, and I have created a sequence
of progressively more permissive casting rules. Perhaps for 2.0, NumPy
could adopt this enum as its preferred way of dealing with casting.</p>
<div class="highlight-c notranslate"><div class="highlight"><pre><span></span><span class="cm">/* For specifying allowed casting in operations which support it */</span>
<span class="k">typedef</span><span class="w"> </span><span class="k">enum</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="cm">/* Only allow identical types */</span>
<span class="w"> </span><span class="n">NPY_NO_CASTING</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span>
<span class="w"> </span><span class="cm">/* Allow identical and byte swapped types */</span>
<span class="w"> </span><span class="n">NPY_EQUIV_CASTING</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
<span class="w"> </span><span class="cm">/* Only allow safe casts */</span>
<span class="w"> </span><span class="n">NPY_SAFE_CASTING</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span>
<span class="w"> </span><span class="cm">/* Allow safe casts and casts within the same kind */</span>
<span class="w"> </span><span class="n">NPY_SAME_KIND_CASTING</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span>
<span class="w"> </span><span class="cm">/* Allow any casts */</span>
<span class="w"> </span><span class="n">NPY_UNSAFE_CASTING</span><span class="o">=</span><span class="mi">4</span>
<span class="p">}</span><span class="w"> </span><span class="n">NPY_CASTING</span><span class="p">;</span>
</pre></div>
</div>
<section id="iterator-rewrite">
<h3><a class="toc-backref" href="#id11" role="doc-backlink">Iterator rewrite</a><a class="headerlink" href="#iterator-rewrite" title="Link to this heading">#</a></h3>
<p>Based on an analysis of the code, it appears that refactoring the existing
iteration objects to implement these optimizations is prohibitively
difficult. Additionally, some usage of the iterator requires modifying
internal values or flags, so code using the iterator would have to
change anyway. Thus we propose creating a new iterator object which
subsumes the existing iterator functionality and expands it to account
for the optimizations.</p>
<p>High level goals for the replacement iterator include:</p>
<ul class="simple">
<li><p>Small memory usage and a low number of memory allocations.</p></li>
<li><p>Simple cases (like flat arrays) should have very little overhead.</p></li>