-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
/
nditer_api.c
2811 lines (2522 loc) · 96.5 KB
/
nditer_api.c
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
/*
* This file implements most of the main API functions of NumPy's nditer.
* This excludes functions specialized using the templating system.
*
* Copyright (c) 2010-2011 by Mark Wiebe (mwwiebe@gmail.com)
* The University of British Columbia
*
* Copyright (c) 2011 Enthought, Inc
*
* See LICENSE.txt for the license.
*/
#define NPY_NO_DEPRECATED_API NPY_API_VERSION
/* Indicate that this .c file is allowed to include the header */
#define NPY_ITERATOR_IMPLEMENTATION_CODE
#include "nditer_impl.h"
#include "templ_common.h"
#include "ctors.h"
/* Internal helper functions private to this file */
static npy_intp
npyiter_checkreducesize(NpyIter *iter, npy_intp count,
npy_intp *reduce_innersize,
npy_intp *reduce_outerdim);
/*NUMPY_API
* Removes an axis from iteration. This requires that NPY_ITER_MULTI_INDEX
* was set for iterator creation, and does not work if buffering is
* enabled. This function also resets the iterator to its initial state.
*
* Returns NPY_SUCCEED or NPY_FAIL.
*/
NPY_NO_EXPORT int
NpyIter_RemoveAxis(NpyIter *iter, int axis)
{
npy_uint32 itflags = NIT_ITFLAGS(iter);
int idim, ndim = NIT_NDIM(iter);
int iop, nop = NIT_NOP(iter);
int xdim = 0;
npy_int8 *perm = NIT_PERM(iter);
NpyIter_AxisData *axisdata_del = NIT_AXISDATA(iter), *axisdata;
npy_intp sizeof_axisdata = NIT_AXISDATA_SIZEOF(itflags, ndim, nop);
npy_intp *baseoffsets = NIT_BASEOFFSETS(iter);
char **resetdataptr = NIT_RESETDATAPTR(iter);
if (!(itflags&NPY_ITFLAG_HASMULTIINDEX)) {
PyErr_SetString(PyExc_RuntimeError,
"Iterator RemoveAxis may only be called "
"if a multi-index is being tracked");
return NPY_FAIL;
}
else if (itflags&NPY_ITFLAG_HASINDEX) {
PyErr_SetString(PyExc_RuntimeError,
"Iterator RemoveAxis may not be called on "
"an index is being tracked");
return NPY_FAIL;
}
else if (itflags&NPY_ITFLAG_BUFFER) {
PyErr_SetString(PyExc_RuntimeError,
"Iterator RemoveAxis may not be called on "
"a buffered iterator");
return NPY_FAIL;
}
else if (axis < 0 || axis >= ndim) {
PyErr_SetString(PyExc_ValueError,
"axis out of bounds in iterator RemoveAxis");
return NPY_FAIL;
}
/* Reverse axis, since the iterator treats them that way */
axis = ndim - 1 - axis;
/* First find the axis in question */
for (idim = 0; idim < ndim; ++idim) {
/* If this is it, and it's iterated forward, done */
if (perm[idim] == axis) {
xdim = idim;
break;
}
/* If this is it, but it's iterated backward, must reverse the axis */
else if (-1 - perm[idim] == axis) {
npy_intp *strides = NAD_STRIDES(axisdata_del);
npy_intp shape = NAD_SHAPE(axisdata_del), offset;
xdim = idim;
/*
* Adjust baseoffsets and resetbaseptr back to the start of
* this axis.
*/
for (iop = 0; iop < nop; ++iop) {
offset = (shape-1)*strides[iop];
baseoffsets[iop] += offset;
resetdataptr[iop] += offset;
}
break;
}
NIT_ADVANCE_AXISDATA(axisdata_del, 1);
}
if (idim == ndim) {
PyErr_SetString(PyExc_RuntimeError,
"internal error in iterator perm");
return NPY_FAIL;
}
/* Adjust the permutation */
for (idim = 0; idim < ndim-1; ++idim) {
npy_int8 p = (idim < xdim) ? perm[idim] : perm[idim+1];
if (p >= 0) {
if (p > axis) {
--p;
}
}
else if (p <= 0) {
if (p < -1-axis) {
++p;
}
}
perm[idim] = p;
}
/* Shift all the axisdata structures by one */
axisdata = NIT_INDEX_AXISDATA(axisdata_del, 1);
memmove(axisdata_del, axisdata, (ndim-1-xdim)*sizeof_axisdata);
/* Adjust the iteration size and reset iterend */
NIT_ITERSIZE(iter) = 1;
axisdata = NIT_AXISDATA(iter);
for (idim = 0; idim < ndim-1; ++idim) {
if (npy_mul_with_overflow_intp(&NIT_ITERSIZE(iter),
NIT_ITERSIZE(iter), NAD_SHAPE(axisdata))) {
NIT_ITERSIZE(iter) = -1;
break;
}
NIT_ADVANCE_AXISDATA(axisdata, 1);
}
NIT_ITEREND(iter) = NIT_ITERSIZE(iter);
/* Shrink the iterator */
NIT_NDIM(iter) = ndim - 1;
/* If it is now 0-d fill the singleton dimension */
if (ndim == 1) {
npy_intp *strides = NAD_STRIDES(axisdata_del);
NAD_SHAPE(axisdata_del) = 1;
for (iop = 0; iop < nop; ++iop) {
strides[iop] = 0;
}
NIT_ITFLAGS(iter) |= NPY_ITFLAG_ONEITERATION;
}
return NpyIter_Reset(iter, NULL);
}
/*NUMPY_API
* Removes multi-index support from an iterator.
*
* Returns NPY_SUCCEED or NPY_FAIL.
*/
NPY_NO_EXPORT int
NpyIter_RemoveMultiIndex(NpyIter *iter)
{
npy_uint32 itflags;
/* Make sure the iterator is reset */
if (NpyIter_Reset(iter, NULL) != NPY_SUCCEED) {
return NPY_FAIL;
}
itflags = NIT_ITFLAGS(iter);
if (itflags&NPY_ITFLAG_HASMULTIINDEX) {
if (NIT_ITERSIZE(iter) < 0) {
PyErr_SetString(PyExc_ValueError, "iterator is too large");
return NPY_FAIL;
}
NIT_ITFLAGS(iter) = itflags & ~NPY_ITFLAG_HASMULTIINDEX;
npyiter_coalesce_axes(iter);
}
return NPY_SUCCEED;
}
/*NUMPY_API
* Removes the inner loop handling (so HasExternalLoop returns true)
*/
NPY_NO_EXPORT int
NpyIter_EnableExternalLoop(NpyIter *iter)
{
npy_uint32 itflags = NIT_ITFLAGS(iter);
/*int ndim = NIT_NDIM(iter);*/
int nop = NIT_NOP(iter);
/* Check conditions under which this can be done */
if (itflags&(NPY_ITFLAG_HASINDEX|NPY_ITFLAG_HASMULTIINDEX)) {
PyErr_SetString(PyExc_ValueError,
"Iterator flag EXTERNAL_LOOP cannot be used "
"if an index or multi-index is being tracked");
return NPY_FAIL;
}
if ((itflags&(NPY_ITFLAG_BUFFER|NPY_ITFLAG_RANGE|NPY_ITFLAG_EXLOOP))
== (NPY_ITFLAG_RANGE|NPY_ITFLAG_EXLOOP)) {
PyErr_SetString(PyExc_ValueError,
"Iterator flag EXTERNAL_LOOP cannot be used "
"with ranged iteration unless buffering is also enabled");
return NPY_FAIL;
}
/* Set the flag */
if (!(itflags&NPY_ITFLAG_EXLOOP)) {
itflags |= NPY_ITFLAG_EXLOOP;
NIT_ITFLAGS(iter) = itflags;
/*
* Check whether we can apply the single iteration
* optimization to the iternext function.
*/
if (!(itflags&NPY_ITFLAG_BUFFER)) {
NpyIter_AxisData *axisdata = NIT_AXISDATA(iter);
if (NIT_ITERSIZE(iter) == NAD_SHAPE(axisdata)) {
NIT_ITFLAGS(iter) |= NPY_ITFLAG_ONEITERATION;
}
}
}
/* Reset the iterator */
return NpyIter_Reset(iter, NULL);
}
/*NUMPY_API
* Resets the iterator to its initial state
*
* If errmsg is non-NULL, it should point to a variable which will
* receive the error message, and no Python exception will be set.
* This is so that the function can be called from code not holding
* the GIL.
*/
NPY_NO_EXPORT int
NpyIter_Reset(NpyIter *iter, char **errmsg)
{
npy_uint32 itflags = NIT_ITFLAGS(iter);
/*int ndim = NIT_NDIM(iter);*/
int nop = NIT_NOP(iter);
if (itflags&NPY_ITFLAG_BUFFER) {
NpyIter_BufferData *bufferdata;
/* If buffer allocation was delayed, do it now */
if (itflags&NPY_ITFLAG_DELAYBUF) {
if (!npyiter_allocate_buffers(iter, errmsg)) {
return NPY_FAIL;
}
NIT_ITFLAGS(iter) &= ~NPY_ITFLAG_DELAYBUF;
}
else {
/*
* If the iterindex is already right, no need to
* do anything
*/
bufferdata = NIT_BUFFERDATA(iter);
if (NIT_ITERINDEX(iter) == NIT_ITERSTART(iter) &&
NBF_BUFITEREND(bufferdata) <= NIT_ITEREND(iter) &&
NBF_SIZE(bufferdata) > 0) {
return NPY_SUCCEED;
}
/* Copy any data from the buffers back to the arrays */
npyiter_copy_from_buffers(iter);
}
}
npyiter_goto_iterindex(iter, NIT_ITERSTART(iter));
if (itflags&NPY_ITFLAG_BUFFER) {
/* Prepare the next buffers and set iterend/size */
npyiter_copy_to_buffers(iter, NULL);
}
return NPY_SUCCEED;
}
/*NUMPY_API
* Resets the iterator to its initial state, with new base data pointers.
* This function requires great caution.
*
* If errmsg is non-NULL, it should point to a variable which will
* receive the error message, and no Python exception will be set.
* This is so that the function can be called from code not holding
* the GIL.
*/
NPY_NO_EXPORT int
NpyIter_ResetBasePointers(NpyIter *iter, char **baseptrs, char **errmsg)
{
npy_uint32 itflags = NIT_ITFLAGS(iter);
/*int ndim = NIT_NDIM(iter);*/
int iop, nop = NIT_NOP(iter);
char **resetdataptr = NIT_RESETDATAPTR(iter);
npy_intp *baseoffsets = NIT_BASEOFFSETS(iter);
if (itflags&NPY_ITFLAG_BUFFER) {
/* If buffer allocation was delayed, do it now */
if (itflags&NPY_ITFLAG_DELAYBUF) {
if (!npyiter_allocate_buffers(iter, errmsg)) {
return NPY_FAIL;
}
NIT_ITFLAGS(iter) &= ~NPY_ITFLAG_DELAYBUF;
}
else {
/* Copy any data from the buffers back to the arrays */
npyiter_copy_from_buffers(iter);
}
}
/* The new data pointers for resetting */
for (iop = 0; iop < nop; ++iop) {
resetdataptr[iop] = baseptrs[iop] + baseoffsets[iop];
}
npyiter_goto_iterindex(iter, NIT_ITERSTART(iter));
if (itflags&NPY_ITFLAG_BUFFER) {
/* Prepare the next buffers and set iterend/size */
npyiter_copy_to_buffers(iter, NULL);
}
return NPY_SUCCEED;
}
/*NUMPY_API
* Resets the iterator to a new iterator index range
*
* If errmsg is non-NULL, it should point to a variable which will
* receive the error message, and no Python exception will be set.
* This is so that the function can be called from code not holding
* the GIL.
*/
NPY_NO_EXPORT int
NpyIter_ResetToIterIndexRange(NpyIter *iter,
npy_intp istart, npy_intp iend, char **errmsg)
{
npy_uint32 itflags = NIT_ITFLAGS(iter);
/*int ndim = NIT_NDIM(iter);*/
/*int nop = NIT_NOP(iter);*/
if (!(itflags&NPY_ITFLAG_RANGE)) {
if (errmsg == NULL) {
PyErr_SetString(PyExc_ValueError,
"Cannot call ResetToIterIndexRange on an iterator without "
"requesting ranged iteration support in the constructor");
}
else {
*errmsg = "Cannot call ResetToIterIndexRange on an iterator "
"without requesting ranged iteration support in the "
"constructor";
}
return NPY_FAIL;
}
if (istart < 0 || iend > NIT_ITERSIZE(iter)) {
if (NIT_ITERSIZE(iter) < 0) {
if (errmsg == NULL) {
PyErr_SetString(PyExc_ValueError, "iterator is too large");
}
else {
*errmsg = "iterator is too large";
}
return NPY_FAIL;
}
if (errmsg == NULL) {
PyErr_Format(PyExc_ValueError,
"Out-of-bounds range [%d, %d) passed to "
"ResetToIterIndexRange", (int)istart, (int)iend);
}
else {
*errmsg = "Out-of-bounds range passed to ResetToIterIndexRange";
}
return NPY_FAIL;
}
else if (iend < istart) {
if (errmsg == NULL) {
PyErr_Format(PyExc_ValueError,
"Invalid range [%d, %d) passed to ResetToIterIndexRange",
(int)istart, (int)iend);
}
else {
*errmsg = "Invalid range passed to ResetToIterIndexRange";
}
return NPY_FAIL;
}
NIT_ITERSTART(iter) = istart;
NIT_ITEREND(iter) = iend;
return NpyIter_Reset(iter, errmsg);
}
/*NUMPY_API
* Sets the iterator to the specified multi-index, which must have the
* correct number of entries for 'ndim'. It is only valid
* when NPY_ITER_MULTI_INDEX was passed to the constructor. This operation
* fails if the multi-index is out of bounds.
*
* Returns NPY_SUCCEED on success, NPY_FAIL on failure.
*/
NPY_NO_EXPORT int
NpyIter_GotoMultiIndex(NpyIter *iter, npy_intp *multi_index)
{
npy_uint32 itflags = NIT_ITFLAGS(iter);
int idim, ndim = NIT_NDIM(iter);
int nop = NIT_NOP(iter);
npy_intp iterindex, factor;
NpyIter_AxisData *axisdata;
npy_intp sizeof_axisdata;
npy_int8 *perm;
if (!(itflags&NPY_ITFLAG_HASMULTIINDEX)) {
PyErr_SetString(PyExc_ValueError,
"Cannot call GotoMultiIndex on an iterator without "
"requesting a multi-index in the constructor");
return NPY_FAIL;
}
if (itflags&NPY_ITFLAG_BUFFER) {
PyErr_SetString(PyExc_ValueError,
"Cannot call GotoMultiIndex on an iterator which "
"is buffered");
return NPY_FAIL;
}
if (itflags&NPY_ITFLAG_EXLOOP) {
PyErr_SetString(PyExc_ValueError,
"Cannot call GotoMultiIndex on an iterator which "
"has the flag EXTERNAL_LOOP");
return NPY_FAIL;
}
perm = NIT_PERM(iter);
axisdata = NIT_AXISDATA(iter);
sizeof_axisdata = NIT_AXISDATA_SIZEOF(itflags, ndim, nop);
/* Compute the iterindex corresponding to the multi-index */
iterindex = 0;
factor = 1;
for (idim = 0; idim < ndim; ++idim) {
npy_int8 p = perm[idim];
npy_intp i, shape;
shape = NAD_SHAPE(axisdata);
if (p < 0) {
/* If the perm entry is negative, reverse the index */
i = shape - multi_index[ndim+p] - 1;
}
else {
i = multi_index[ndim-p-1];
}
/* Bounds-check this index */
if (i >= 0 && i < shape) {
iterindex += factor * i;
factor *= shape;
}
else {
PyErr_SetString(PyExc_IndexError,
"Iterator GotoMultiIndex called with an out-of-bounds "
"multi-index");
return NPY_FAIL;
}
NIT_ADVANCE_AXISDATA(axisdata, 1);
}
if (iterindex < NIT_ITERSTART(iter) || iterindex >= NIT_ITEREND(iter)) {
if (NIT_ITERSIZE(iter) < 0) {
PyErr_SetString(PyExc_ValueError, "iterator is too large");
return NPY_FAIL;
}
PyErr_SetString(PyExc_IndexError,
"Iterator GotoMultiIndex called with a multi-index outside the "
"restricted iteration range");
return NPY_FAIL;
}
npyiter_goto_iterindex(iter, iterindex);
return NPY_SUCCEED;
}
/*NUMPY_API
* If the iterator is tracking an index, sets the iterator
* to the specified index.
*
* Returns NPY_SUCCEED on success, NPY_FAIL on failure.
*/
NPY_NO_EXPORT int
NpyIter_GotoIndex(NpyIter *iter, npy_intp flat_index)
{
npy_uint32 itflags = NIT_ITFLAGS(iter);
int idim, ndim = NIT_NDIM(iter);
int nop = NIT_NOP(iter);
npy_intp iterindex, factor;
NpyIter_AxisData *axisdata;
npy_intp sizeof_axisdata;
if (!(itflags&NPY_ITFLAG_HASINDEX)) {
PyErr_SetString(PyExc_ValueError,
"Cannot call GotoIndex on an iterator without "
"requesting a C or Fortran index in the constructor");
return NPY_FAIL;
}
if (itflags&NPY_ITFLAG_BUFFER) {
PyErr_SetString(PyExc_ValueError,
"Cannot call GotoIndex on an iterator which "
"is buffered");
return NPY_FAIL;
}
if (itflags&NPY_ITFLAG_EXLOOP) {
PyErr_SetString(PyExc_ValueError,
"Cannot call GotoIndex on an iterator which "
"has the flag EXTERNAL_LOOP");
return NPY_FAIL;
}
if (flat_index < 0 || flat_index >= NIT_ITERSIZE(iter)) {
PyErr_SetString(PyExc_IndexError,
"Iterator GotoIndex called with an out-of-bounds "
"index");
return NPY_FAIL;
}
axisdata = NIT_AXISDATA(iter);
sizeof_axisdata = NIT_AXISDATA_SIZEOF(itflags, ndim, nop);
/* Compute the iterindex corresponding to the flat_index */
iterindex = 0;
factor = 1;
for (idim = 0; idim < ndim; ++idim) {
npy_intp i, shape, iterstride;
iterstride = NAD_STRIDES(axisdata)[nop];
shape = NAD_SHAPE(axisdata);
/* Extract the index from the flat_index */
if (iterstride == 0) {
i = 0;
}
else if (iterstride < 0) {
i = shape - (flat_index/(-iterstride))%shape - 1;
}
else {
i = (flat_index/iterstride)%shape;
}
/* Add its contribution to iterindex */
iterindex += factor * i;
factor *= shape;
NIT_ADVANCE_AXISDATA(axisdata, 1);
}
if (iterindex < NIT_ITERSTART(iter) || iterindex >= NIT_ITEREND(iter)) {
PyErr_SetString(PyExc_IndexError,
"Iterator GotoIndex called with an index outside the "
"restricted iteration range.");
return NPY_FAIL;
}
npyiter_goto_iterindex(iter, iterindex);
return NPY_SUCCEED;
}
/*NUMPY_API
* Sets the iterator position to the specified iterindex,
* which matches the iteration order of the iterator.
*
* Returns NPY_SUCCEED on success, NPY_FAIL on failure.
*/
NPY_NO_EXPORT int
NpyIter_GotoIterIndex(NpyIter *iter, npy_intp iterindex)
{
npy_uint32 itflags = NIT_ITFLAGS(iter);
/*int ndim = NIT_NDIM(iter);*/
int iop, nop = NIT_NOP(iter);
if (itflags&NPY_ITFLAG_EXLOOP) {
PyErr_SetString(PyExc_ValueError,
"Cannot call GotoIterIndex on an iterator which "
"has the flag EXTERNAL_LOOP");
return NPY_FAIL;
}
if (iterindex < NIT_ITERSTART(iter) || iterindex >= NIT_ITEREND(iter)) {
if (NIT_ITERSIZE(iter) < 0) {
PyErr_SetString(PyExc_ValueError, "iterator is too large");
return NPY_FAIL;
}
PyErr_SetString(PyExc_IndexError,
"Iterator GotoIterIndex called with an iterindex outside the "
"iteration range.");
return NPY_FAIL;
}
if (itflags&NPY_ITFLAG_BUFFER) {
NpyIter_BufferData *bufferdata = NIT_BUFFERDATA(iter);
npy_intp bufiterend, size;
size = NBF_SIZE(bufferdata);
bufiterend = NBF_BUFITEREND(bufferdata);
/* Check if the new iterindex is already within the buffer */
if (!(itflags&NPY_ITFLAG_REDUCE) && iterindex < bufiterend &&
iterindex >= bufiterend - size) {
npy_intp *strides, delta;
char **ptrs;
strides = NBF_STRIDES(bufferdata);
ptrs = NBF_PTRS(bufferdata);
delta = iterindex - NIT_ITERINDEX(iter);
for (iop = 0; iop < nop; ++iop) {
ptrs[iop] += delta * strides[iop];
}
NIT_ITERINDEX(iter) = iterindex;
}
/* Start the buffer at the provided iterindex */
else {
/* Write back to the arrays */
npyiter_copy_from_buffers(iter);
npyiter_goto_iterindex(iter, iterindex);
/* Prepare the next buffers and set iterend/size */
npyiter_copy_to_buffers(iter, NULL);
}
}
else {
npyiter_goto_iterindex(iter, iterindex);
}
return NPY_SUCCEED;
}
/*NUMPY_API
* Gets the current iteration index
*/
NPY_NO_EXPORT npy_intp
NpyIter_GetIterIndex(NpyIter *iter)
{
npy_uint32 itflags = NIT_ITFLAGS(iter);
int idim, ndim = NIT_NDIM(iter);
int nop = NIT_NOP(iter);
/* iterindex is only used if NPY_ITER_RANGED or NPY_ITER_BUFFERED was set */
if (itflags&(NPY_ITFLAG_RANGE|NPY_ITFLAG_BUFFER)) {
return NIT_ITERINDEX(iter);
}
else {
npy_intp iterindex;
NpyIter_AxisData *axisdata;
npy_intp sizeof_axisdata;
iterindex = 0;
if (ndim == 0) {
return 0;
}
sizeof_axisdata = NIT_AXISDATA_SIZEOF(itflags, ndim, nop);
axisdata = NIT_INDEX_AXISDATA(NIT_AXISDATA(iter), ndim-1);
for (idim = ndim-2; idim >= 0; --idim) {
iterindex += NAD_INDEX(axisdata);
NIT_ADVANCE_AXISDATA(axisdata, -1);
iterindex *= NAD_SHAPE(axisdata);
}
iterindex += NAD_INDEX(axisdata);
return iterindex;
}
}
/*NUMPY_API
* Whether the buffer allocation is being delayed
*/
NPY_NO_EXPORT npy_bool
NpyIter_HasDelayedBufAlloc(NpyIter *iter)
{
return (NIT_ITFLAGS(iter)&NPY_ITFLAG_DELAYBUF) != 0;
}
/*NUMPY_API
* Whether the iterator handles the inner loop
*/
NPY_NO_EXPORT npy_bool
NpyIter_HasExternalLoop(NpyIter *iter)
{
return (NIT_ITFLAGS(iter)&NPY_ITFLAG_EXLOOP) != 0;
}
/*NUMPY_API
* Whether the iterator is tracking a multi-index
*/
NPY_NO_EXPORT npy_bool
NpyIter_HasMultiIndex(NpyIter *iter)
{
return (NIT_ITFLAGS(iter)&NPY_ITFLAG_HASMULTIINDEX) != 0;
}
/*NUMPY_API
* Whether the iterator is tracking an index
*/
NPY_NO_EXPORT npy_bool
NpyIter_HasIndex(NpyIter *iter)
{
return (NIT_ITFLAGS(iter)&NPY_ITFLAG_HASINDEX) != 0;
}
/*NUMPY_API
* Checks to see whether this is the first time the elements
* of the specified reduction operand which the iterator points at are
* being seen for the first time. The function returns
* a reasonable answer for reduction operands and when buffering is
* disabled. The answer may be incorrect for buffered non-reduction
* operands.
*
* This function is intended to be used in EXTERNAL_LOOP mode only,
* and will produce some wrong answers when that mode is not enabled.
*
* If this function returns true, the caller should also
* check the inner loop stride of the operand, because if
* that stride is 0, then only the first element of the innermost
* external loop is being visited for the first time.
*
* WARNING: For performance reasons, 'iop' is not bounds-checked,
* it is not confirmed that 'iop' is actually a reduction
* operand, and it is not confirmed that EXTERNAL_LOOP
* mode is enabled. These checks are the responsibility of
* the caller, and should be done outside of any inner loops.
*/
NPY_NO_EXPORT npy_bool
NpyIter_IsFirstVisit(NpyIter *iter, int iop)
{
npy_uint32 itflags = NIT_ITFLAGS(iter);
int idim, ndim = NIT_NDIM(iter);
int nop = NIT_NOP(iter);
NpyIter_AxisData *axisdata;
npy_intp sizeof_axisdata;
sizeof_axisdata = NIT_AXISDATA_SIZEOF(itflags, ndim, nop);
axisdata = NIT_AXISDATA(iter);
for (idim = 0; idim < ndim; ++idim) {
npy_intp coord = NAD_INDEX(axisdata);
npy_intp stride = NAD_STRIDES(axisdata)[iop];
/*
* If this is a reduction dimension and the coordinate
* is not at the start, it's definitely not the first visit
*/
if (stride == 0 && coord != 0) {
return 0;
}
NIT_ADVANCE_AXISDATA(axisdata, 1);
}
/*
* In reduction buffering mode, there's a double loop being
* tracked in the buffer part of the iterator data structure.
* We only need to check the outer level of this two-level loop,
* because of the requirement that EXTERNAL_LOOP be enabled.
*/
if (itflags&NPY_ITFLAG_BUFFER) {
NpyIter_BufferData *bufferdata = NIT_BUFFERDATA(iter);
/* The outer reduce loop */
if (NBF_REDUCE_POS(bufferdata) != 0 &&
NBF_REDUCE_OUTERSTRIDES(bufferdata)[iop] == 0) {
return 0;
}
}
return 1;
}
/*NUMPY_API
* Whether the iteration could be done with no buffering.
*/
NPY_NO_EXPORT npy_bool
NpyIter_RequiresBuffering(NpyIter *iter)
{
npy_uint32 itflags = NIT_ITFLAGS(iter);
/*int ndim = NIT_NDIM(iter);*/
int iop, nop = NIT_NOP(iter);
npyiter_opitflags *op_itflags;
if (!(itflags&NPY_ITFLAG_BUFFER)) {
return 0;
}
op_itflags = NIT_OPITFLAGS(iter);
/* If any operand requires a cast, buffering is mandatory */
for (iop = 0; iop < nop; ++iop) {
if (op_itflags[iop]&NPY_OP_ITFLAG_CAST) {
return 1;
}
}
return 0;
}
/*NUMPY_API
* Whether the iteration loop, and in particular the iternext()
* function, needs API access. If this is true, the GIL must
* be retained while iterating.
*/
NPY_NO_EXPORT npy_bool
NpyIter_IterationNeedsAPI(NpyIter *iter)
{
return (NIT_ITFLAGS(iter)&NPY_ITFLAG_NEEDSAPI) != 0;
}
/*NUMPY_API
* Gets the number of dimensions being iterated
*/
NPY_NO_EXPORT int
NpyIter_GetNDim(NpyIter *iter)
{
return NIT_NDIM(iter);
}
/*NUMPY_API
* Gets the number of operands being iterated
*/
NPY_NO_EXPORT int
NpyIter_GetNOp(NpyIter *iter)
{
return NIT_NOP(iter);
}
/*NUMPY_API
* Gets the number of elements being iterated
*/
NPY_NO_EXPORT npy_intp
NpyIter_GetIterSize(NpyIter *iter)
{
return NIT_ITERSIZE(iter);
}
/*NUMPY_API
* Whether the iterator is buffered
*/
NPY_NO_EXPORT npy_bool
NpyIter_IsBuffered(NpyIter *iter)
{
return (NIT_ITFLAGS(iter)&NPY_ITFLAG_BUFFER) != 0;
}
/*NUMPY_API
* Whether the inner loop can grow if buffering is unneeded
*/
NPY_NO_EXPORT npy_bool
NpyIter_IsGrowInner(NpyIter *iter)
{
return (NIT_ITFLAGS(iter)&NPY_ITFLAG_GROWINNER) != 0;
}
/*NUMPY_API
* Gets the size of the buffer, or 0 if buffering is not enabled
*/
NPY_NO_EXPORT npy_intp
NpyIter_GetBufferSize(NpyIter *iter)
{
npy_uint32 itflags = NIT_ITFLAGS(iter);
/*int ndim = NIT_NDIM(iter);*/
int nop = NIT_NOP(iter);
if (itflags&NPY_ITFLAG_BUFFER) {
NpyIter_BufferData *bufferdata = NIT_BUFFERDATA(iter);
return NBF_BUFFERSIZE(bufferdata);
}
else {
return 0;
}
}
/*NUMPY_API
* Gets the range of iteration indices being iterated
*/
NPY_NO_EXPORT void
NpyIter_GetIterIndexRange(NpyIter *iter,
npy_intp *istart, npy_intp *iend)
{
*istart = NIT_ITERSTART(iter);
*iend = NIT_ITEREND(iter);
}
/*NUMPY_API
* Gets the broadcast shape if a multi-index is being tracked by the iterator,
* otherwise gets the shape of the iteration as Fortran-order
* (fastest-changing index first).
*
* The reason Fortran-order is returned when a multi-index
* is not enabled is that this is providing a direct view into how
* the iterator traverses the n-dimensional space. The iterator organizes
* its memory from fastest index to slowest index, and when
* a multi-index is enabled, it uses a permutation to recover the original
* order.
*
* Returns NPY_SUCCEED or NPY_FAIL.
*/
NPY_NO_EXPORT int
NpyIter_GetShape(NpyIter *iter, npy_intp *outshape)
{
npy_uint32 itflags = NIT_ITFLAGS(iter);
int ndim = NIT_NDIM(iter);
int nop = NIT_NOP(iter);
int idim, sizeof_axisdata;
NpyIter_AxisData *axisdata;
npy_int8 *perm;
axisdata = NIT_AXISDATA(iter);
sizeof_axisdata = NIT_AXISDATA_SIZEOF(itflags, ndim, nop);
if (itflags&NPY_ITFLAG_HASMULTIINDEX) {
perm = NIT_PERM(iter);
for(idim = 0; idim < ndim; ++idim) {
npy_int8 p = perm[idim];
if (p < 0) {
outshape[ndim+p] = NAD_SHAPE(axisdata);
}
else {
outshape[ndim-p-1] = NAD_SHAPE(axisdata);
}
NIT_ADVANCE_AXISDATA(axisdata, 1);
}
}
else {
for(idim = 0; idim < ndim; ++idim) {
outshape[idim] = NAD_SHAPE(axisdata);
NIT_ADVANCE_AXISDATA(axisdata, 1);
}
}
return NPY_SUCCEED;
}
/*NUMPY_API
* Builds a set of strides which are the same as the strides of an
* output array created using the NPY_ITER_ALLOCATE flag, where NULL
* was passed for op_axes. This is for data packed contiguously,
* but not necessarily in C or Fortran order. This should be used
* together with NpyIter_GetShape and NpyIter_GetNDim.
*
* A use case for this function is to match the shape and layout of
* the iterator and tack on one or more dimensions. For example,
* in order to generate a vector per input value for a numerical gradient,
* you pass in ndim*itemsize for itemsize, then add another dimension to
* the end with size ndim and stride itemsize. To do the Hessian matrix,
* you do the same thing but add two dimensions, or take advantage of
* the symmetry and pack it into 1 dimension with a particular encoding.
*
* This function may only be called if the iterator is tracking a multi-index
* and if NPY_ITER_DONT_NEGATE_STRIDES was used to prevent an axis from
* being iterated in reverse order.
*
* If an array is created with this method, simply adding 'itemsize'
* for each iteration will traverse the new array matching the
* iterator.
*
* Returns NPY_SUCCEED or NPY_FAIL.
*/
NPY_NO_EXPORT int
NpyIter_CreateCompatibleStrides(NpyIter *iter,
npy_intp itemsize, npy_intp *outstrides)
{
npy_uint32 itflags = NIT_ITFLAGS(iter);
int idim, ndim = NIT_NDIM(iter);
int nop = NIT_NOP(iter);
npy_intp sizeof_axisdata;
NpyIter_AxisData *axisdata;
npy_int8 *perm;
if (!(itflags&NPY_ITFLAG_HASMULTIINDEX)) {
PyErr_SetString(PyExc_RuntimeError,
"Iterator CreateCompatibleStrides may only be called "
"if a multi-index is being tracked");
return NPY_FAIL;