-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
/
nditer_constr.c
3266 lines (2963 loc) · 113 KB
/
nditer_constr.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 the construction, copying, and destruction
* aspects of NumPy's nditer.
*
* 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 "arrayobject.h"
#include "templ_common.h"
#include "array_assign.h"
/* Internal helper functions private to this file */
static int
npyiter_check_global_flags(npy_uint32 flags, npy_uint32* itflags);
static int
npyiter_check_op_axes(int nop, int oa_ndim, int **op_axes,
npy_intp *itershape);
static int
npyiter_calculate_ndim(int nop, PyArrayObject **op_in,
int oa_ndim);
static int
npyiter_check_per_op_flags(npy_uint32 flags, npyiter_opitflags *op_itflags);
static int
npyiter_prepare_one_operand(PyArrayObject **op,
char **op_dataptr,
PyArray_Descr *op_request_dtype,
PyArray_Descr** op_dtype,
npy_uint32 flags,
npy_uint32 op_flags, npyiter_opitflags *op_itflags);
static int
npyiter_prepare_operands(int nop,
PyArrayObject **op_in,
PyArrayObject **op,
char **op_dataptr,
PyArray_Descr **op_request_dtypes,
PyArray_Descr **op_dtype,
npy_uint32 flags,
npy_uint32 *op_flags, npyiter_opitflags *op_itflags,
npy_int8 *out_maskop);
static int
npyiter_check_casting(int nop, PyArrayObject **op,
PyArray_Descr **op_dtype,
NPY_CASTING casting,
npyiter_opitflags *op_itflags);
static int
npyiter_fill_axisdata(NpyIter *iter, npy_uint32 flags, npyiter_opitflags *op_itflags,
char **op_dataptr,
npy_uint32 *op_flags, int **op_axes,
npy_intp *itershape);
static void
npyiter_replace_axisdata(NpyIter *iter, int iop,
PyArrayObject *op,
int op_ndim, char *op_dataptr,
int *op_axes);
static void
npyiter_compute_index_strides(NpyIter *iter, npy_uint32 flags);
static void
npyiter_apply_forced_iteration_order(NpyIter *iter, NPY_ORDER order);
static void
npyiter_flip_negative_strides(NpyIter *iter);
static void
npyiter_reverse_axis_ordering(NpyIter *iter);
static void
npyiter_find_best_axis_ordering(NpyIter *iter);
static PyArray_Descr *
npyiter_get_common_dtype(int nop, PyArrayObject **op,
npyiter_opitflags *op_itflags, PyArray_Descr **op_dtype,
PyArray_Descr **op_request_dtypes,
int only_inputs);
static PyArrayObject *
npyiter_new_temp_array(NpyIter *iter, PyTypeObject *subtype,
npy_uint32 flags, npyiter_opitflags *op_itflags,
int op_ndim, npy_intp *shape,
PyArray_Descr *op_dtype, int *op_axes);
static int
npyiter_allocate_arrays(NpyIter *iter,
npy_uint32 flags,
PyArray_Descr **op_dtype, PyTypeObject *subtype,
npy_uint32 *op_flags, npyiter_opitflags *op_itflags,
int **op_axes);
static void
npyiter_get_priority_subtype(int nop, PyArrayObject **op,
npyiter_opitflags *op_itflags,
double *subtype_priority, PyTypeObject **subtype);
static int
npyiter_allocate_transfer_functions(NpyIter *iter);
/*NUMPY_API
* Allocate a new iterator for multiple array objects, and advanced
* options for controlling the broadcasting, shape, and buffer size.
*/
NPY_NO_EXPORT NpyIter *
NpyIter_AdvancedNew(int nop, PyArrayObject **op_in, npy_uint32 flags,
NPY_ORDER order, NPY_CASTING casting,
npy_uint32 *op_flags,
PyArray_Descr **op_request_dtypes,
int oa_ndim, int **op_axes, npy_intp *itershape,
npy_intp buffersize)
{
npy_uint32 itflags = NPY_ITFLAG_IDENTPERM;
int idim, ndim;
int iop;
/* The iterator being constructed */
NpyIter *iter;
/* Per-operand values */
PyArrayObject **op;
PyArray_Descr **op_dtype;
npyiter_opitflags *op_itflags;
char **op_dataptr;
npy_int8 *perm;
NpyIter_BufferData *bufferdata = NULL;
int any_allocate = 0, any_missing_dtypes = 0, need_subtype = 0;
/* The subtype for automatically allocated outputs */
double subtype_priority = NPY_PRIORITY;
PyTypeObject *subtype = &PyArray_Type;
#if NPY_IT_CONSTRUCTION_TIMING
npy_intp c_temp,
c_start,
c_check_op_axes,
c_check_global_flags,
c_calculate_ndim,
c_malloc,
c_prepare_operands,
c_fill_axisdata,
c_compute_index_strides,
c_apply_forced_iteration_order,
c_find_best_axis_ordering,
c_get_priority_subtype,
c_find_output_common_dtype,
c_check_casting,
c_allocate_arrays,
c_coalesce_axes,
c_prepare_buffers;
#endif
NPY_IT_TIME_POINT(c_start);
if (nop > NPY_MAXARGS) {
PyErr_Format(PyExc_ValueError,
"Cannot construct an iterator with more than %d operands "
"(%d were requested)", (int)NPY_MAXARGS, (int)nop);
return NULL;
}
/*
* Before 1.8, if `oa_ndim == 0`, this meant `op_axes != NULL` was an error.
* With 1.8, `oa_ndim == -1` takes this role, while op_axes in that case
* enforces a 0-d iterator. Using `oa_ndim == 0` with `op_axes == NULL`
* is thus an error in 1.13 after deprecation.
*/
if ((oa_ndim == 0) && (op_axes == NULL)) {
PyErr_Format(PyExc_ValueError,
"Using `oa_ndim == 0` when `op_axes` is NULL. "
"Use `oa_ndim == -1` or the MultiNew "
"iterator for NumPy <1.8 compatibility");
return NULL;
}
/* Error check 'oa_ndim' and 'op_axes', which must be used together */
if (!npyiter_check_op_axes(nop, oa_ndim, op_axes, itershape)) {
return NULL;
}
NPY_IT_TIME_POINT(c_check_op_axes);
/* Check the global iterator flags */
if (!npyiter_check_global_flags(flags, &itflags)) {
return NULL;
}
NPY_IT_TIME_POINT(c_check_global_flags);
/* Calculate how many dimensions the iterator should have */
ndim = npyiter_calculate_ndim(nop, op_in, oa_ndim);
NPY_IT_TIME_POINT(c_calculate_ndim);
/* Allocate memory for the iterator */
iter = (NpyIter*)
PyObject_Malloc(NIT_SIZEOF_ITERATOR(itflags, ndim, nop));
NPY_IT_TIME_POINT(c_malloc);
/* Fill in the basic data */
NIT_ITFLAGS(iter) = itflags;
NIT_NDIM(iter) = ndim;
NIT_NOP(iter) = nop;
NIT_MASKOP(iter) = -1;
NIT_ITERINDEX(iter) = 0;
memset(NIT_BASEOFFSETS(iter), 0, (nop+1)*NPY_SIZEOF_INTP);
op = NIT_OPERANDS(iter);
op_dtype = NIT_DTYPES(iter);
op_itflags = NIT_OPITFLAGS(iter);
op_dataptr = NIT_RESETDATAPTR(iter);
/* Prepare all the operands */
if (!npyiter_prepare_operands(nop, op_in, op, op_dataptr,
op_request_dtypes, op_dtype,
flags,
op_flags, op_itflags,
&NIT_MASKOP(iter))) {
PyObject_Free(iter);
return NULL;
}
/* Set resetindex to zero as well (it's just after the resetdataptr) */
op_dataptr[nop] = 0;
NPY_IT_TIME_POINT(c_prepare_operands);
/*
* Initialize buffer data (must set the buffers and transferdata
* to NULL before we might deallocate the iterator).
*/
if (itflags & NPY_ITFLAG_BUFFER) {
bufferdata = NIT_BUFFERDATA(iter);
NBF_SIZE(bufferdata) = 0;
memset(NBF_BUFFERS(bufferdata), 0, nop*NPY_SIZEOF_INTP);
memset(NBF_PTRS(bufferdata), 0, nop*NPY_SIZEOF_INTP);
memset(NBF_READTRANSFERDATA(bufferdata), 0, nop*NPY_SIZEOF_INTP);
memset(NBF_WRITETRANSFERDATA(bufferdata), 0, nop*NPY_SIZEOF_INTP);
}
/* Fill in the AXISDATA arrays and set the ITERSIZE field */
if (!npyiter_fill_axisdata(iter, flags, op_itflags, op_dataptr,
op_flags, op_axes, itershape)) {
NpyIter_Deallocate(iter);
return NULL;
}
NPY_IT_TIME_POINT(c_fill_axisdata);
if (itflags & NPY_ITFLAG_BUFFER) {
/*
* If buffering is enabled and no buffersize was given, use a default
* chosen to be big enough to get some amortization benefits, but
* small enough to be cache-friendly.
*/
if (buffersize <= 0) {
buffersize = NPY_BUFSIZE;
}
/* No point in a buffer bigger than the iteration size */
if (buffersize > NIT_ITERSIZE(iter)) {
buffersize = NIT_ITERSIZE(iter);
}
NBF_BUFFERSIZE(bufferdata) = buffersize;
/*
* Initialize for use in FirstVisit, which may be called before
* the buffers are filled and the reduce pos is updated.
*/
NBF_REDUCE_POS(bufferdata) = 0;
}
/*
* If an index was requested, compute the strides for it.
* Note that we must do this before changing the order of the
* axes
*/
npyiter_compute_index_strides(iter, flags);
NPY_IT_TIME_POINT(c_compute_index_strides);
/* Initialize the perm to the identity */
perm = NIT_PERM(iter);
for(idim = 0; idim < ndim; ++idim) {
perm[idim] = (npy_int8)idim;
}
/*
* If an iteration order is being forced, apply it.
*/
npyiter_apply_forced_iteration_order(iter, order);
itflags = NIT_ITFLAGS(iter);
NPY_IT_TIME_POINT(c_apply_forced_iteration_order);
/* Set some flags for allocated outputs */
for (iop = 0; iop < nop; ++iop) {
if (op[iop] == NULL) {
/* Flag this so later we can avoid flipping axes */
any_allocate = 1;
/* If a subtype may be used, indicate so */
if (!(op_flags[iop] & NPY_ITER_NO_SUBTYPE)) {
need_subtype = 1;
}
/*
* If the data type wasn't provided, will need to
* calculate it.
*/
if (op_dtype[iop] == NULL) {
any_missing_dtypes = 1;
}
}
}
/*
* If the ordering was not forced, reorder the axes
* and flip negative strides to find the best one.
*/
if (!(itflags & NPY_ITFLAG_FORCEDORDER)) {
if (ndim > 1) {
npyiter_find_best_axis_ordering(iter);
}
/*
* If there's an output being allocated, we must not negate
* any strides.
*/
if (!any_allocate && !(flags & NPY_ITER_DONT_NEGATE_STRIDES)) {
npyiter_flip_negative_strides(iter);
}
itflags = NIT_ITFLAGS(iter);
}
NPY_IT_TIME_POINT(c_find_best_axis_ordering);
if (need_subtype) {
npyiter_get_priority_subtype(nop, op, op_itflags,
&subtype_priority, &subtype);
}
NPY_IT_TIME_POINT(c_get_priority_subtype);
/*
* If an automatically allocated output didn't have a specified
* dtype, we need to figure it out now, before allocating the outputs.
*/
if (any_missing_dtypes || (flags & NPY_ITER_COMMON_DTYPE)) {
PyArray_Descr *dtype;
int only_inputs = !(flags & NPY_ITER_COMMON_DTYPE);
op = NIT_OPERANDS(iter);
op_dtype = NIT_DTYPES(iter);
dtype = npyiter_get_common_dtype(nop, op,
op_itflags, op_dtype,
op_request_dtypes,
only_inputs);
if (dtype == NULL) {
NpyIter_Deallocate(iter);
return NULL;
}
if (flags & NPY_ITER_COMMON_DTYPE) {
NPY_IT_DBG_PRINT("Iterator: Replacing all data types\n");
/* Replace all the data types */
for (iop = 0; iop < nop; ++iop) {
if (op_dtype[iop] != dtype) {
Py_XDECREF(op_dtype[iop]);
Py_INCREF(dtype);
op_dtype[iop] = dtype;
}
}
}
else {
NPY_IT_DBG_PRINT("Iterator: Setting unset output data types\n");
/* Replace the NULL data types */
for (iop = 0; iop < nop; ++iop) {
if (op_dtype[iop] == NULL) {
Py_INCREF(dtype);
op_dtype[iop] = dtype;
}
}
}
Py_DECREF(dtype);
}
NPY_IT_TIME_POINT(c_find_output_common_dtype);
/*
* All of the data types have been settled, so it's time
* to check that data type conversions are following the
* casting rules.
*/
if (!npyiter_check_casting(nop, op, op_dtype, casting, op_itflags)) {
NpyIter_Deallocate(iter);
return NULL;
}
NPY_IT_TIME_POINT(c_check_casting);
/*
* At this point, the iteration order has been finalized. so
* any allocation of ops that were NULL, or any temporary
* copying due to casting/byte order/alignment can be
* done now using a memory layout matching the iterator.
*/
if (!npyiter_allocate_arrays(iter, flags, op_dtype, subtype, op_flags,
op_itflags, op_axes)) {
NpyIter_Deallocate(iter);
return NULL;
}
NPY_IT_TIME_POINT(c_allocate_arrays);
/*
* Finally, if a multi-index wasn't requested,
* it may be possible to coalesce some axes together.
*/
if (ndim > 1 && !(itflags & NPY_ITFLAG_HASMULTIINDEX)) {
npyiter_coalesce_axes(iter);
/*
* The operation may have changed the layout, so we have to
* get the internal pointers again.
*/
itflags = NIT_ITFLAGS(iter);
ndim = NIT_NDIM(iter);
op = NIT_OPERANDS(iter);
op_dtype = NIT_DTYPES(iter);
op_itflags = NIT_OPITFLAGS(iter);
op_dataptr = NIT_RESETDATAPTR(iter);
}
NPY_IT_TIME_POINT(c_coalesce_axes);
/*
* Now that the axes are finished, 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 (itflags & NPY_ITFLAG_EXLOOP) {
if (NIT_ITERSIZE(iter) == NAD_SHAPE(axisdata)) {
NIT_ITFLAGS(iter) |= NPY_ITFLAG_ONEITERATION;
}
}
else if (NIT_ITERSIZE(iter) == 1) {
NIT_ITFLAGS(iter) |= NPY_ITFLAG_ONEITERATION;
}
}
/*
* If REFS_OK was specified, check whether there are any
* reference arrays and flag it if so.
*/
if (flags & NPY_ITER_REFS_OK) {
for (iop = 0; iop < nop; ++iop) {
PyArray_Descr *rdt = op_dtype[iop];
if ((rdt->flags & (NPY_ITEM_REFCOUNT |
NPY_ITEM_IS_POINTER |
NPY_NEEDS_PYAPI)) != 0) {
/* Iteration needs API access */
NIT_ITFLAGS(iter) |= NPY_ITFLAG_NEEDSAPI;
}
}
}
/* If buffering is set without delayed allocation */
if (itflags & NPY_ITFLAG_BUFFER) {
if (!npyiter_allocate_transfer_functions(iter)) {
NpyIter_Deallocate(iter);
return NULL;
}
if (!(itflags & NPY_ITFLAG_DELAYBUF)) {
/* Allocate the buffers */
if (!npyiter_allocate_buffers(iter, NULL)) {
NpyIter_Deallocate(iter);
return NULL;
}
/* Prepare the next buffers and set iterend/size */
npyiter_copy_to_buffers(iter, NULL);
}
}
NPY_IT_TIME_POINT(c_prepare_buffers);
#if NPY_IT_CONSTRUCTION_TIMING
printf("\nIterator construction timing:\n");
NPY_IT_PRINT_TIME_START(c_start);
NPY_IT_PRINT_TIME_VAR(c_check_op_axes);
NPY_IT_PRINT_TIME_VAR(c_check_global_flags);
NPY_IT_PRINT_TIME_VAR(c_calculate_ndim);
NPY_IT_PRINT_TIME_VAR(c_malloc);
NPY_IT_PRINT_TIME_VAR(c_prepare_operands);
NPY_IT_PRINT_TIME_VAR(c_fill_axisdata);
NPY_IT_PRINT_TIME_VAR(c_compute_index_strides);
NPY_IT_PRINT_TIME_VAR(c_apply_forced_iteration_order);
NPY_IT_PRINT_TIME_VAR(c_find_best_axis_ordering);
NPY_IT_PRINT_TIME_VAR(c_get_priority_subtype);
NPY_IT_PRINT_TIME_VAR(c_find_output_common_dtype);
NPY_IT_PRINT_TIME_VAR(c_check_casting);
NPY_IT_PRINT_TIME_VAR(c_allocate_arrays);
NPY_IT_PRINT_TIME_VAR(c_coalesce_axes);
NPY_IT_PRINT_TIME_VAR(c_prepare_buffers);
printf("\n");
#endif
return iter;
}
/*NUMPY_API
* Allocate a new iterator for more than one array object, using
* standard NumPy broadcasting rules and the default buffer size.
*/
NPY_NO_EXPORT NpyIter *
NpyIter_MultiNew(int nop, PyArrayObject **op_in, npy_uint32 flags,
NPY_ORDER order, NPY_CASTING casting,
npy_uint32 *op_flags,
PyArray_Descr **op_request_dtypes)
{
return NpyIter_AdvancedNew(nop, op_in, flags, order, casting,
op_flags, op_request_dtypes,
-1, NULL, NULL, 0);
}
/*NUMPY_API
* Allocate a new iterator for one array object.
*/
NPY_NO_EXPORT NpyIter *
NpyIter_New(PyArrayObject *op, npy_uint32 flags,
NPY_ORDER order, NPY_CASTING casting,
PyArray_Descr* dtype)
{
/* Split the flags into separate global and op flags */
npy_uint32 op_flags = flags & NPY_ITER_PER_OP_FLAGS;
flags &= NPY_ITER_GLOBAL_FLAGS;
return NpyIter_AdvancedNew(1, &op, flags, order, casting,
&op_flags, &dtype,
-1, NULL, NULL, 0);
}
/*NUMPY_API
* Makes a copy of the iterator
*/
NPY_NO_EXPORT NpyIter *
NpyIter_Copy(NpyIter *iter)
{
npy_uint32 itflags = NIT_ITFLAGS(iter);
int ndim = NIT_NDIM(iter);
int iop, nop = NIT_NOP(iter);
int out_of_memory = 0;
npy_intp size;
NpyIter *newiter;
PyArrayObject **objects;
PyArray_Descr **dtypes;
/* Allocate memory for the new iterator */
size = NIT_SIZEOF_ITERATOR(itflags, ndim, nop);
newiter = (NpyIter*)PyObject_Malloc(size);
/* Copy the raw values to the new iterator */
memcpy(newiter, iter, size);
/* Take ownership of references to the operands and dtypes */
objects = NIT_OPERANDS(newiter);
dtypes = NIT_DTYPES(newiter);
for (iop = 0; iop < nop; ++iop) {
Py_INCREF(objects[iop]);
Py_INCREF(dtypes[iop]);
}
/* Allocate buffers and make copies of the transfer data if necessary */
if (itflags & NPY_ITFLAG_BUFFER) {
NpyIter_BufferData *bufferdata;
npy_intp buffersize, itemsize;
char **buffers;
NpyAuxData **readtransferdata, **writetransferdata;
bufferdata = NIT_BUFFERDATA(newiter);
buffers = NBF_BUFFERS(bufferdata);
readtransferdata = NBF_READTRANSFERDATA(bufferdata);
writetransferdata = NBF_WRITETRANSFERDATA(bufferdata);
buffersize = NBF_BUFFERSIZE(bufferdata);
for (iop = 0; iop < nop; ++iop) {
if (buffers[iop] != NULL) {
if (out_of_memory) {
buffers[iop] = NULL;
}
else {
itemsize = dtypes[iop]->elsize;
buffers[iop] = PyArray_malloc(itemsize*buffersize);
if (buffers[iop] == NULL) {
out_of_memory = 1;
}
}
}
if (readtransferdata[iop] != NULL) {
if (out_of_memory) {
readtransferdata[iop] = NULL;
}
else {
readtransferdata[iop] =
NPY_AUXDATA_CLONE(readtransferdata[iop]);
if (readtransferdata[iop] == NULL) {
out_of_memory = 1;
}
}
}
if (writetransferdata[iop] != NULL) {
if (out_of_memory) {
writetransferdata[iop] = NULL;
}
else {
writetransferdata[iop] =
NPY_AUXDATA_CLONE(writetransferdata[iop]);
if (writetransferdata[iop] == NULL) {
out_of_memory = 1;
}
}
}
}
/* Initialize the buffers to the current iterindex */
if (!out_of_memory && NBF_SIZE(bufferdata) > 0) {
npyiter_goto_iterindex(newiter, NIT_ITERINDEX(newiter));
/* Prepare the next buffers and set iterend/size */
npyiter_copy_to_buffers(newiter, NULL);
}
}
if (out_of_memory) {
NpyIter_Deallocate(newiter);
PyErr_NoMemory();
return NULL;
}
return newiter;
}
/*NUMPY_API
* Deallocate an iterator
*/
NPY_NO_EXPORT int
NpyIter_Deallocate(NpyIter *iter)
{
npy_uint32 itflags;
/*int ndim = NIT_NDIM(iter);*/
int iop, nop;
PyArray_Descr **dtype;
PyArrayObject **object;
npyiter_opitflags *op_itflags;
npy_bool resolve = 1;
if (iter == NULL) {
return NPY_SUCCEED;
}
itflags = NIT_ITFLAGS(iter);
nop = NIT_NOP(iter);
dtype = NIT_DTYPES(iter);
object = NIT_OPERANDS(iter);
op_itflags = NIT_OPITFLAGS(iter);
/* Deallocate any buffers and buffering data */
if (itflags & NPY_ITFLAG_BUFFER) {
NpyIter_BufferData *bufferdata = NIT_BUFFERDATA(iter);
char **buffers;
NpyAuxData **transferdata;
/* buffers */
buffers = NBF_BUFFERS(bufferdata);
for(iop = 0; iop < nop; ++iop, ++buffers) {
PyArray_free(*buffers);
}
/* read bufferdata */
transferdata = NBF_READTRANSFERDATA(bufferdata);
for(iop = 0; iop < nop; ++iop, ++transferdata) {
if (*transferdata) {
NPY_AUXDATA_FREE(*transferdata);
}
}
/* write bufferdata */
transferdata = NBF_WRITETRANSFERDATA(bufferdata);
for(iop = 0; iop < nop; ++iop, ++transferdata) {
if (*transferdata) {
NPY_AUXDATA_FREE(*transferdata);
}
}
}
/*
* Deallocate all the dtypes and objects that were iterated and resolve
* any writeback buffers created by the iterator
*/
for(iop = 0; iop < nop; ++iop, ++dtype, ++object) {
if (op_itflags[iop] & NPY_OP_ITFLAG_HAS_WRITEBACK) {
if (resolve && PyArray_ResolveWritebackIfCopy(*object) < 0) {
resolve = 0;
}
else {
PyArray_DiscardWritebackIfCopy(*object);
}
}
Py_XDECREF(*dtype);
Py_XDECREF(*object);
}
/* Deallocate the iterator memory */
PyObject_Free(iter);
if (resolve == 0) {
return NPY_FAIL;
}
return NPY_SUCCEED;
}
/* Checks 'flags' for (C|F)_ORDER_INDEX, MULTI_INDEX, and EXTERNAL_LOOP,
* setting the appropriate internal flags in 'itflags'.
*
* Returns 1 on success, 0 on error.
*/
static int
npyiter_check_global_flags(npy_uint32 flags, npy_uint32* itflags)
{
if ((flags & NPY_ITER_PER_OP_FLAGS) != 0) {
PyErr_SetString(PyExc_ValueError,
"A per-operand flag was passed as a global flag "
"to the iterator constructor");
return 0;
}
/* Check for an index */
if (flags & (NPY_ITER_C_INDEX | NPY_ITER_F_INDEX)) {
if ((flags & (NPY_ITER_C_INDEX | NPY_ITER_F_INDEX)) ==
(NPY_ITER_C_INDEX | NPY_ITER_F_INDEX)) {
PyErr_SetString(PyExc_ValueError,
"Iterator flags C_INDEX and "
"F_INDEX cannot both be specified");
return 0;
}
(*itflags) |= NPY_ITFLAG_HASINDEX;
}
/* Check if a multi-index was requested */
if (flags & NPY_ITER_MULTI_INDEX) {
/*
* This flag primarily disables dimension manipulations that
* would produce an incorrect multi-index.
*/
(*itflags) |= NPY_ITFLAG_HASMULTIINDEX;
}
/* Check if the caller wants to handle inner iteration */
if (flags & NPY_ITER_EXTERNAL_LOOP) {
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 0;
}
(*itflags) |= NPY_ITFLAG_EXLOOP;
}
/* Ranged */
if (flags & NPY_ITER_RANGED) {
(*itflags) |= NPY_ITFLAG_RANGE;
if ((flags & NPY_ITER_EXTERNAL_LOOP) &&
!(flags & NPY_ITER_BUFFERED)) {
PyErr_SetString(PyExc_ValueError,
"Iterator flag RANGED cannot be used with "
"the flag EXTERNAL_LOOP unless "
"BUFFERED is also enabled");
return 0;
}
}
/* Buffering */
if (flags & NPY_ITER_BUFFERED) {
(*itflags) |= NPY_ITFLAG_BUFFER;
if (flags & NPY_ITER_GROWINNER) {
(*itflags) |= NPY_ITFLAG_GROWINNER;
}
if (flags & NPY_ITER_DELAY_BUFALLOC) {
(*itflags) |= NPY_ITFLAG_DELAYBUF;
}
}
return 1;
}
static int
npyiter_check_op_axes(int nop, int oa_ndim, int **op_axes,
npy_intp *itershape)
{
char axes_dupcheck[NPY_MAXDIMS];
int iop, idim;
if (oa_ndim < 0) {
/*
* If `oa_ndim < 0`, `op_axes` and `itershape` are signalled to
* be unused and should be NULL. (Before NumPy 1.8 this was
* signalled by `oa_ndim == 0`.)
*/
if (op_axes != NULL || itershape != NULL) {
PyErr_Format(PyExc_ValueError,
"If 'op_axes' or 'itershape' is not NULL in the iterator "
"constructor, 'oa_ndim' must be zero or greater");
return 0;
}
return 1;
}
if (oa_ndim > NPY_MAXDIMS) {
PyErr_Format(PyExc_ValueError,
"Cannot construct an iterator with more than %d dimensions "
"(%d were requested for op_axes)",
(int)NPY_MAXDIMS, oa_ndim);
return 0;
}
if (op_axes == NULL) {
PyErr_Format(PyExc_ValueError,
"If 'oa_ndim' is zero or greater in the iterator "
"constructor, then op_axes cannot be NULL");
return 0;
}
/* Check that there are no duplicates in op_axes */
for (iop = 0; iop < nop; ++iop) {
int *axes = op_axes[iop];
if (axes != NULL) {
memset(axes_dupcheck, 0, NPY_MAXDIMS);
for (idim = 0; idim < oa_ndim; ++idim) {
npy_intp i = axes[idim];
if (i >= 0) {
if (i >= NPY_MAXDIMS) {
PyErr_Format(PyExc_ValueError,
"The 'op_axes' provided to the iterator "
"constructor for operand %d "
"contained invalid "
"values %d", (int)iop, (int)i);
return 0;
}
else if (axes_dupcheck[i] == 1) {
PyErr_Format(PyExc_ValueError,
"The 'op_axes' provided to the iterator "
"constructor for operand %d "
"contained duplicate "
"value %d", (int)iop, (int)i);
return 0;
}
else {
axes_dupcheck[i] = 1;
}
}
}
}
}
return 1;
}
static int
npyiter_calculate_ndim(int nop, PyArrayObject **op_in,
int oa_ndim)
{
/* If 'op_axes' is being used, force 'ndim' */
if (oa_ndim >= 0 ) {
return oa_ndim;
}
/* Otherwise it's the maximum 'ndim' from the operands */
else {
int ndim = 0, iop;
for (iop = 0; iop < nop; ++iop) {
if (op_in[iop] != NULL) {
int ondim = PyArray_NDIM(op_in[iop]);
if (ondim > ndim) {
ndim = ondim;
}
}
}
return ndim;
}
}
/*
* Checks the per-operand input flags, and fills in op_itflags.
*
* Returns 1 on success, 0 on failure.
*/
static int
npyiter_check_per_op_flags(npy_uint32 op_flags, npyiter_opitflags *op_itflags)
{
if ((op_flags & NPY_ITER_GLOBAL_FLAGS) != 0) {
PyErr_SetString(PyExc_ValueError,
"A global iterator flag was passed as a per-operand flag "
"to the iterator constructor");
return 0;
}
/* Check the read/write flags */
if (op_flags & NPY_ITER_READONLY) {
/* The read/write flags are mutually exclusive */
if (op_flags & (NPY_ITER_READWRITE|NPY_ITER_WRITEONLY)) {
PyErr_SetString(PyExc_ValueError,
"Only one of the iterator flags READWRITE, "
"READONLY, and WRITEONLY may be "
"specified for an operand");
return 0;
}
*op_itflags = NPY_OP_ITFLAG_READ;
}
else if (op_flags & NPY_ITER_READWRITE) {
/* The read/write flags are mutually exclusive */
if (op_flags & NPY_ITER_WRITEONLY) {
PyErr_SetString(PyExc_ValueError,
"Only one of the iterator flags READWRITE, "
"READONLY, and WRITEONLY may be "
"specified for an operand");
return 0;
}
*op_itflags = NPY_OP_ITFLAG_READ|NPY_OP_ITFLAG_WRITE;
}
else if(op_flags & NPY_ITER_WRITEONLY) {
*op_itflags = NPY_OP_ITFLAG_WRITE;
}
else {
PyErr_SetString(PyExc_ValueError,
"None of the iterator flags READWRITE, "
"READONLY, or WRITEONLY were "
"specified for an operand");
return 0;
}
/* Check the flags for temporary copies */
if (((*op_itflags) & NPY_OP_ITFLAG_WRITE) &&
(op_flags & (NPY_ITER_COPY |
NPY_ITER_UPDATEIFCOPY)) == NPY_ITER_COPY) {
PyErr_SetString(PyExc_ValueError,
"If an iterator operand is writeable, must use "
"the flag UPDATEIFCOPY instead of "
"COPY");
return 0;
}
/* Check the flag for a write masked operands */
if (op_flags & NPY_ITER_WRITEMASKED) {
if (!((*op_itflags) & NPY_OP_ITFLAG_WRITE)) {
PyErr_SetString(PyExc_ValueError,
"The iterator flag WRITEMASKED may only "
"be used with READWRITE or WRITEONLY");
return 0;
}
if ((op_flags & NPY_ITER_ARRAYMASK) != 0) {
PyErr_SetString(PyExc_ValueError,
"The iterator flag WRITEMASKED may not "
"be used together with ARRAYMASK");
return 0;
}
*op_itflags |= NPY_OP_ITFLAG_WRITEMASKED;
}
if ((op_flags & NPY_ITER_VIRTUAL) != 0) {
if ((op_flags & NPY_ITER_READWRITE) == 0) {
PyErr_SetString(PyExc_ValueError,
"The iterator flag VIRTUAL should be "
"be used together with READWRITE");
return 0;
}
*op_itflags |= NPY_OP_ITFLAG_VIRTUAL;
}
return 1;
}
/*
* Prepares a a constructor operand. Assumes a reference to 'op'
* is owned, and that 'op' may be replaced. Fills in 'op_dataptr',
* 'op_dtype', and may modify 'op_itflags'.
*
* Returns 1 on success, 0 on failure.
*/
static int
npyiter_prepare_one_operand(PyArrayObject **op,
char **op_dataptr,
PyArray_Descr *op_request_dtype,
PyArray_Descr **op_dtype,
npy_uint32 flags,
npy_uint32 op_flags, npyiter_opitflags *op_itflags)
{
/* NULL operands must be automatically allocated outputs */
if (*op == NULL) {
/* ALLOCATE or VIRTUAL should be enabled */
if ((op_flags & (NPY_ITER_ALLOCATE|NPY_ITER_VIRTUAL)) == 0) {
PyErr_SetString(PyExc_ValueError,
"Iterator operand was NULL, but neither the "
"ALLOCATE nor the VIRTUAL flag was specified");
return 0;
}