forked from pydata/numexpr
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinterpreter.cpp
More file actions
1517 lines (1349 loc) · 47 KB
/
interpreter.cpp
File metadata and controls
1517 lines (1349 loc) · 47 KB
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
/*********************************************************************
Numexpr - Fast numerical array expression evaluator for NumPy.
License: MIT
Author: See AUTHORS.txt
See LICENSE.txt for details about copyright and rights to use.
**********************************************************************/
#include "module.hpp"
#include <numpy/npy_cpu.h>
#include <math.h>
#include <string.h>
#include <assert.h>
#include <vector>
#include "numexpr_config.hpp"
#include "complex_functions.hpp"
#include "interpreter.hpp"
#include "numexpr_object.hpp"
#ifdef _MSC_VER
/* Some missing symbols and functions for Win */
#define fmax max
#define fmin min
#define NE_INFINITY (DBL_MAX+DBL_MAX)
#define NE_NAN (INFINITY-INFINITY)
#else
#define NE_INFINITY INFINITY
#define NE_NAN NAN
#endif
#ifndef SIZE_MAX
#define SIZE_MAX ((size_t)-1)
#endif
#define RETURN_TYPE char*
// AVAILABLE(Haystack, Haystack_Len, J, Needle_Len)
// A macro that returns nonzero if there are at least Needle_Len
// bytes left starting at Haystack[J].
// Haystack is 'unsigned char *', Haystack_Len, J, and Needle_Len
// are 'size_t'; Haystack_Len is an lvalue. For NUL-terminated
// searches, Haystack_Len can be modified each iteration to avoid
// having to compute the end of Haystack up front.
#define AVAILABLE(Haystack, Haystack_Len, J, Needle_Len) \
((Haystack_Len) >= (J) + (Needle_Len))
#include "str-two-way.hpp"
#ifdef DEBUG
#define DEBUG_TEST 1
#else
#define DEBUG_TEST 0
#endif
using namespace std;
// Global state
thread_data th_params;
/* This file and interp_body should really be generated from a description of
the opcodes -- there's too much repetition here for manually editing */
/* bit of a misnomer; includes the return value. */
#define NUMEXPR_MAX_ARGS 4
static char op_signature_table[][NUMEXPR_MAX_ARGS] = {
#define Tb 'b'
#define Ti 'i'
#define Tl 'l'
#define Tf 'f'
#define Td 'd'
#define Tc 'c'
#define Ts 's'
#define Tn 'n'
#define T0 0
#define OPCODE(n, e, ex, rt, a1, a2, a3) {rt, a1, a2, a3},
#include "opcodes.hpp"
#undef OPCODE
#undef Tb
#undef Ti
#undef Tl
#undef Tf
#undef Td
#undef Tc
#undef Ts
#undef Tn
#undef T0
};
/* returns the sig of the nth op, '\0' if no more ops -1 on failure */
static int
op_signature(int op, unsigned int n) {
if (n >= NUMEXPR_MAX_ARGS) {
return 0;
}
if (op < 0 || op > OP_END) {
return -1;
}
return op_signature_table[op][n];
}
/*
To add a function to the lookup table, add to FUNC_CODES (first
group is 1-arg functions, second is 2-arg functions), also to
functions_f or functions_ff as appropriate. Finally, use add_func
down below to add to funccodes. Functions with more arguments
aren't implemented at present, but should be easy; just copy the 1-
or 2-arg case.
Some functions (for example, sqrt) are repeated in this table that
are opcodes, but there's no problem with that as the compiler
selects opcodes over functions, and this makes it easier to compare
opcode vs. function speeds.
*/
typedef float (*FuncFFPtr)(float);
#ifdef _WIN32
FuncFFPtr functions_ff[] = {
#define FUNC_FF(fop, s, f, f_win32, ...) f_win32,
#include "functions.hpp"
#undef FUNC_FF
};
#else
FuncFFPtr functions_ff[] = {
#define FUNC_FF(fop, s, f, ...) f,
#include "functions.hpp"
#undef FUNC_FF
};
#endif
#ifdef USE_VML
/* Fake vsConj function just for casting purposes inside numexpr */
static void vsConj(MKL_INT n, const float* x1, float* dest)
{
MKL_INT j;
for (j=0; j<n; j++) {
dest[j] = x1[j];
};
};
#endif
#ifdef USE_VML
typedef void (*FuncFFPtr_vml)(MKL_INT, const float*, float*);
FuncFFPtr_vml functions_ff_vml[] = {
#define FUNC_FF(fop, s, f, f_win32, f_vml) f_vml,
#include "functions.hpp"
#undef FUNC_FF
};
#endif
typedef float (*FuncFFFPtr)(float, float);
#ifdef _WIN32
FuncFFFPtr functions_fff[] = {
#define FUNC_FFF(fop, s, f, f_win32, ...) f_win32,
#include "functions.hpp"
#undef FUNC_FFF
};
#else
FuncFFFPtr functions_fff[] = {
#define FUNC_FFF(fop, s, f, ...) f,
#include "functions.hpp"
#undef FUNC_FFF
};
#endif
#ifdef USE_VML
/* fmod not available in VML */
static void vsfmod(MKL_INT n, const float* x1, const float* x2, float* dest)
{
MKL_INT j;
for(j=0; j < n; j++) {
dest[j] = fmod(x1[j], x2[j]);
};
};
typedef void (*FuncFFFPtr_vml)(MKL_INT, const float*, const float*, float*);
FuncFFFPtr_vml functions_fff_vml[] = {
#define FUNC_FFF(fop, s, f, f_win32, f_vml) f_vml,
#include "functions.hpp"
#undef FUNC_FFF
};
#endif
typedef double (*FuncDDPtr)(double);
FuncDDPtr functions_dd[] = {
#define FUNC_DD(fop, s, f, ...) f,
#include "functions.hpp"
#undef FUNC_DD
};
#ifdef USE_VML
/* Fake vdConj function just for casting purposes inside numexpr */
static void vdConj(MKL_INT n, const double* x1, double* dest)
{
MKL_INT j;
for (j=0; j<n; j++) {
dest[j] = x1[j];
};
};
#endif
#ifdef USE_VML
typedef void (*FuncDDPtr_vml)(MKL_INT, const double*, double*);
FuncDDPtr_vml functions_dd_vml[] = {
#define FUNC_DD(fop, s, f, f_vml) f_vml,
#include "functions.hpp"
#undef FUNC_DD
};
#endif
typedef double (*FuncDDDPtr)(double, double);
FuncDDDPtr functions_ddd[] = {
#define FUNC_DDD(fop, s, f, ...) f,
#include "functions.hpp"
#undef FUNC_DDD
};
#ifdef USE_VML
/* fmod not available in VML */
static void vdfmod(MKL_INT n, const double* x1, const double* x2, double* dest)
{
MKL_INT j;
for(j=0; j < n; j++) {
dest[j] = fmod(x1[j], x2[j]);
};
};
typedef void (*FuncDDDPtr_vml)(MKL_INT, const double*, const double*, double*);
FuncDDDPtr_vml functions_ddd_vml[] = {
#define FUNC_DDD(fop, s, f, f_vml) f_vml,
#include "functions.hpp"
#undef FUNC_DDD
};
#endif
typedef void (*FuncCCPtr)(npy_cdouble*, npy_cdouble*);
FuncCCPtr functions_cc[] = {
#define FUNC_CC(fop, s, f, ...) f,
#include "functions.hpp"
#undef FUNC_CC
};
#ifdef USE_VML
/* complex expm1 not available in VML */
static void vzExpm1(MKL_INT n, const MKL_Complex16* x1, MKL_Complex16* dest)
{
MKL_INT j;
vzExp(n, x1, dest);
for (j=0; j<n; j++) {
dest[j].real -= 1.0;
};
};
static void vzLog1p(MKL_INT n, const MKL_Complex16* x1, MKL_Complex16* dest)
{
MKL_INT j;
for (j=0; j<n; j++) {
dest[j].real = x1[j].real + 1;
dest[j].imag = x1[j].imag;
};
vzLn(n, dest, dest);
};
/* Use this instead of native vzAbs in VML as it seems to work badly */
static void vzAbs_(MKL_INT n, const MKL_Complex16* x1, MKL_Complex16* dest)
{
MKL_INT j;
for (j=0; j<n; j++) {
dest[j].real = sqrt(x1[j].real*x1[j].real + x1[j].imag*x1[j].imag);
dest[j].imag = 0;
};
};
typedef void (*FuncCCPtr_vml)(MKL_INT, const MKL_Complex16[], MKL_Complex16[]);
FuncCCPtr_vml functions_cc_vml[] = {
#define FUNC_CC(fop, s, f, f_vml) f_vml,
#include "functions.hpp"
#undef FUNC_CC
};
#endif
typedef void (*FuncCCCPtr)(npy_cdouble*, npy_cdouble*, npy_cdouble*);
FuncCCCPtr functions_ccc[] = {
#define FUNC_CCC(fop, s, f) f,
#include "functions.hpp"
#undef FUNC_CCC
};
char
get_return_sig(PyObject* program)
{
int sig;
char last_opcode;
Py_ssize_t end = PyBytes_Size(program);
char *program_str = PyBytes_AS_STRING(program);
do {
end -= 4;
if (end < 0) return 'X';
last_opcode = program_str[end];
}
while (last_opcode == OP_NOOP);
sig = op_signature(last_opcode, 0);
if (sig <= 0) {
return 'X';
} else {
return (char)sig;
}
}
static int
typecode_from_char(char c)
{
switch (c) {
case 'b': return NPY_BOOL;
case 'i': return NPY_INT;
case 'l': return NPY_LONGLONG;
case 'f': return NPY_FLOAT;
case 'd': return NPY_DOUBLE;
case 'c': return NPY_CDOUBLE;
case 's': return NPY_STRING;
default:
PyErr_SetString(PyExc_TypeError, "signature value not in 'bilfdcs'");
return -1;
}
}
static int
last_opcode(PyObject *program_object) {
Py_ssize_t n;
unsigned char *program;
PyBytes_AsStringAndSize(program_object, (char **)&program, &n);
return program[n-4];
}
static int
get_reduction_axis(PyObject* program) {
Py_ssize_t end = PyBytes_Size(program);
int axis = ((unsigned char *)PyBytes_AS_STRING(program))[end-1];
if (axis != 255 && axis >= NPY_MAXDIMS)
axis = NPY_MAXDIMS - axis;
return axis;
}
int
check_program(NumExprObject *self)
{
unsigned char *program;
Py_ssize_t prog_len, n_buffers, n_inputs;
int pc, arg, argloc, argno, sig;
char *fullsig, *signature;
if (PyBytes_AsStringAndSize(self->program, (char **)&program,
&prog_len) < 0) {
PyErr_Format(PyExc_RuntimeError, "invalid program: can't read program");
return -1;
}
if (prog_len % 4 != 0) {
PyErr_Format(PyExc_RuntimeError, "invalid program: prog_len mod 4 != 0");
return -1;
}
if (PyBytes_AsStringAndSize(self->fullsig, (char **)&fullsig,
&n_buffers) < 0) {
PyErr_Format(PyExc_RuntimeError, "invalid program: can't read fullsig");
return -1;
}
if (PyBytes_AsStringAndSize(self->signature, (char **)&signature,
&n_inputs) < 0) {
PyErr_Format(PyExc_RuntimeError, "invalid program: can't read signature");
return -1;
}
if (n_buffers > 255) {
PyErr_Format(PyExc_RuntimeError, "invalid program: too many buffers");
return -1;
}
for (pc = 0; pc < prog_len; pc += 4) {
unsigned int op = program[pc];
if (op == OP_NOOP) {
continue;
}
if ((op >= OP_REDUCTION) && pc != prog_len-4) {
PyErr_Format(PyExc_RuntimeError,
"invalid program: reduction operations must occur last");
return -1;
}
for (argno = 0; ; argno++) {
sig = op_signature(op, argno);
if (sig == -1) {
PyErr_Format(PyExc_RuntimeError, "invalid program: illegal opcode at %i (%d)", pc, op);
return -1;
}
if (sig == 0) break;
if (argno < 3) {
argloc = pc+argno+1;
}
if (argno >= 3) {
if (pc + 1 >= prog_len) {
PyErr_Format(PyExc_RuntimeError, "invalid program: double opcode (%c) at end (%i)", pc, sig);
return -1;
}
argloc = pc+argno+2;
}
arg = program[argloc];
if (sig != 'n' && ((arg >= n_buffers) || (arg < 0))) {
PyErr_Format(PyExc_RuntimeError, "invalid program: buffer out of range (%i) at %i", arg, argloc);
return -1;
}
if (sig == 'n') {
if (op == OP_FUNC_FFN) {
if (arg < 0 || arg >= FUNC_FF_LAST) {
PyErr_Format(PyExc_RuntimeError, "invalid program: funccode out of range (%i) at %i", arg, argloc);
return -1;
}
} else if (op == OP_FUNC_FFFN) {
if (arg < 0 || arg >= FUNC_FFF_LAST) {
PyErr_Format(PyExc_RuntimeError, "invalid program: funccode out of range (%i) at %i", arg, argloc);
return -1;
}
} else if (op == OP_FUNC_DDN) {
if (arg < 0 || arg >= FUNC_DD_LAST) {
PyErr_Format(PyExc_RuntimeError, "invalid program: funccode out of range (%i) at %i", arg, argloc);
return -1;
}
} else if (op == OP_FUNC_DDDN) {
if (arg < 0 || arg >= FUNC_DDD_LAST) {
PyErr_Format(PyExc_RuntimeError, "invalid program: funccode out of range (%i) at %i", arg, argloc);
return -1;
}
} else if (op == OP_FUNC_CCN) {
if (arg < 0 || arg >= FUNC_CC_LAST) {
PyErr_Format(PyExc_RuntimeError, "invalid program: funccode out of range (%i) at %i", arg, argloc);
return -1;
}
} else if (op == OP_FUNC_CCCN) {
if (arg < 0 || arg >= FUNC_CCC_LAST) {
PyErr_Format(PyExc_RuntimeError, "invalid program: funccode out of range (%i) at %i", arg, argloc);
return -1;
}
} else if (op >= OP_REDUCTION) {
;
} else {
PyErr_Format(PyExc_RuntimeError, "invalid program: internal checker error processing %i", argloc);
return -1;
}
/* The next is to avoid problems with the ('i','l') duality,
specially in 64-bit platforms */
} else if (((sig == 'l') && (fullsig[arg] == 'i')) ||
((sig == 'i') && (fullsig[arg] == 'l'))) {
;
} else if (sig != fullsig[arg]) {
PyErr_Format(PyExc_RuntimeError,
"invalid : opcode signature doesn't match buffer (%c vs %c) at %i", sig, fullsig[arg], argloc);
return -1;
}
}
}
return 0;
}
struct index_data {
int count;
int size;
int findex;
npy_intp *shape;
npy_intp *strides;
int *index;
char *buffer;
};
// BOUNDS_CHECK is used in interp_body.cpp
#define DO_BOUNDS_CHECK 1
#if DO_BOUNDS_CHECK
#define BOUNDS_CHECK(arg) if ((arg) >= params.r_end) { \
*pc_error = pc; \
return -2; \
}
#else
#define BOUNDS_CHECK(arg)
#endif
int
stringcmp(const char *s1, const char *s2, npy_intp maxlen1, npy_intp maxlen2)
{
npy_intp maxlen, nextpos;
/* Point to this when the end of a string is found,
to simulate infinte trailing NULL characters. */
const char null = 0;
// First check if some of the operands is the empty string and if so,
// just check that the first char of the other is the NULL one.
// Fixes #121
if (maxlen2 == 0) return *s1 != null;
if (maxlen1 == 0) return *s2 != null;
maxlen = (maxlen1 > maxlen2) ? maxlen1 : maxlen2;
for (nextpos = 1; nextpos <= maxlen; nextpos++) {
if (*s1 < *s2)
return -1;
if (*s1 > *s2)
return +1;
s1 = (nextpos >= maxlen1) ? &null : s1+1;
s2 = (nextpos >= maxlen2) ? &null : s2+1;
}
return 0;
}
/* contains(str1, str2) function for string columns.
Based on Newlib/strstr.c. */
int
stringcontains(const char *haystack_start, const char *needle_start, npy_intp max_haystack_len, npy_intp max_needle_len)
{
// needle_len - Length of needle.
// haystack_len - Known minimum length of haystack.
size_t needle_len = (size_t)max_needle_len;
size_t haystack_len = (size_t)max_haystack_len;
const char *haystack = haystack_start;
const char *needle = needle_start;
bool ok = true; /* needle is prefix of haystack. */
char *res;
size_t si = 0;
size_t min_len = min(needle_len, haystack_len);
while (*haystack && *needle && si < min_len)
{
ok &= *haystack++ == *needle++;
si++;
}
/* check needle is prefix of haystack and calc needle length */
if (si == needle_len || *needle == 0) {
if (ok)
return 1;
needle_len = si;
} else {
/* haystack less needle */
return 0;
}
/* calc haystack length */
while (*haystack && si < haystack_len) {
haystack++;
si++;
}
haystack_len = si;
if (needle_len < LONG_NEEDLE_THRESHOLD)
{
res = two_way_short_needle((const unsigned char *)haystack_start, haystack_len,
(const unsigned char *)needle_start, needle_len);
} else {
res = two_way_long_needle((const unsigned char *)haystack_start, haystack_len,
(const unsigned char *)needle_start, needle_len);
}
return res != NULL ? 1 : 0;
}
/* Get space for VM temporary registers */
int get_temps_space(const vm_params& params, char **mem, size_t block_size)
{
int r, k = 1 + params.n_inputs + params.n_constants;
for (r = k; r < k + params.n_temps; r++) {
mem[r] = (char *)malloc(block_size * params.memsizes[r]);
if (mem[r] == NULL) {
return -1;
}
}
return 0;
}
/* Free space for VM temporary registers */
void free_temps_space(const vm_params& params, char **mem)
{
int r, k = 1 + params.n_inputs + params.n_constants;
for (r = k; r < k + params.n_temps; r++) {
free(mem[r]);
}
}
/* Serial/parallel task iterator version of the VM engine */
int vm_engine_iter_task(NpyIter *iter, npy_intp *memsteps,
const vm_params& params,
int *pc_error, char **errmsg)
{
char **mem = params.mem;
NpyIter_IterNextFunc *iternext;
npy_intp block_size, *size_ptr;
char **iter_dataptr;
npy_intp *iter_strides;
iternext = NpyIter_GetIterNext(iter, errmsg);
if (iternext == NULL) {
return -1;
}
size_ptr = NpyIter_GetInnerLoopSizePtr(iter);
iter_dataptr = NpyIter_GetDataPtrArray(iter);
iter_strides = NpyIter_GetInnerStrideArray(iter);
/*
* First do all the blocks with a compile-time fixed size.
* This makes a big difference (30-50% on some tests).
*/
block_size = *size_ptr;
while (block_size == BLOCK_SIZE1) {
#define REDUCTION_INNER_LOOP
#define BLOCK_SIZE BLOCK_SIZE1
#include "interp_body.cpp"
#undef BLOCK_SIZE
#undef REDUCTION_INNER_LOOP
iternext(iter);
block_size = *size_ptr;
}
/* Then finish off the rest */
if (block_size > 0) do {
#define REDUCTION_INNER_LOOP
#define BLOCK_SIZE block_size
#include "interp_body.cpp"
#undef BLOCK_SIZE
#undef REDUCTION_INNER_LOOP
} while (iternext(iter));
return 0;
}
static int
vm_engine_iter_outer_reduce_task(NpyIter *iter, npy_intp *memsteps,
const vm_params& params, int *pc_error, char **errmsg)
{
char **mem = params.mem;
NpyIter_IterNextFunc *iternext;
npy_intp block_size, *size_ptr;
char **iter_dataptr;
npy_intp *iter_strides;
iternext = NpyIter_GetIterNext(iter, errmsg);
if (iternext == NULL) {
return -1;
}
size_ptr = NpyIter_GetInnerLoopSizePtr(iter);
iter_dataptr = NpyIter_GetDataPtrArray(iter);
iter_strides = NpyIter_GetInnerStrideArray(iter);
/*
* First do all the blocks with a compile-time fixed size.
* This makes a big difference (30-50% on some tests).
*/
block_size = *size_ptr;
while (block_size == BLOCK_SIZE1) {
#define BLOCK_SIZE BLOCK_SIZE1
#define NO_OUTPUT_BUFFERING // Because it's a reduction
#include "interp_body.cpp"
#undef NO_OUTPUT_BUFFERING
#undef BLOCK_SIZE
iternext(iter);
block_size = *size_ptr;
}
/* Then finish off the rest */
if (block_size > 0) do {
#define BLOCK_SIZE block_size
#define NO_OUTPUT_BUFFERING // Because it's a reduction
#include "interp_body.cpp"
#undef NO_OUTPUT_BUFFERING
#undef BLOCK_SIZE
} while (iternext(iter));
return 0;
}
/* Parallel iterator version of VM engine */
static int
vm_engine_iter_parallel(NpyIter *iter, const vm_params& params,
bool need_output_buffering, int *pc_error,
char **errmsg)
{
int i, ret = -1;
npy_intp numblocks, taskfactor;
if (errmsg == NULL) {
return -1;
}
/* Ensure only one parallel job is running at a time (otherwise
the global th_params get corrupted). */
Py_BEGIN_ALLOW_THREADS;
pthread_mutex_lock(&gs.parallel_mutex);
Py_END_ALLOW_THREADS;
/* Populate parameters for worker threads */
NpyIter_GetIterIndexRange(iter, &th_params.start, &th_params.vlen);
/*
* Try to make it so each thread gets 16 tasks. This is a compromise
* between 1 task per thread and one block per task.
*/
taskfactor = 16*BLOCK_SIZE1*gs.nthreads;
numblocks = (th_params.vlen - th_params.start + taskfactor - 1) /
taskfactor;
th_params.block_size = numblocks * BLOCK_SIZE1;
th_params.params = params;
th_params.need_output_buffering = need_output_buffering;
th_params.ret_code = 0;
th_params.pc_error = pc_error;
th_params.errmsg = errmsg;
th_params.iter[0] = iter;
/* Make one copy for each additional thread */
for (i = 1; i < gs.nthreads; ++i) {
th_params.iter[i] = NpyIter_Copy(iter);
if (th_params.iter[i] == NULL) {
--i;
for (; i > 0; --i) {
NpyIter_Deallocate(th_params.iter[i]);
}
goto end;
}
}
th_params.memsteps[0] = params.memsteps;
/* Make one copy of memsteps for each additional thread */
for (i = 1; i < gs.nthreads; ++i) {
th_params.memsteps[i] = PyMem_New(npy_intp,
1 + params.n_inputs + params.n_constants + params.n_temps);
if (th_params.memsteps[i] == NULL) {
--i;
for (; i > 0; --i) {
PyMem_Del(th_params.memsteps[i]);
}
for (i = 0; i < gs.nthreads; ++i) {
NpyIter_Deallocate(th_params.iter[i]);
}
goto end;
}
memcpy(th_params.memsteps[i], th_params.memsteps[0],
sizeof(npy_intp) *
(1 + params.n_inputs + params.n_constants + params.n_temps));
}
Py_BEGIN_ALLOW_THREADS;
/* Synchronization point for all threads (wait for initialization) */
pthread_mutex_lock(&gs.count_threads_mutex);
if (gs.count_threads < gs.nthreads) {
gs.count_threads++;
/* Beware of spurious wakeups. See issue pydata/numexpr#306. */
do {
pthread_cond_wait(&gs.count_threads_cv, &gs.count_threads_mutex);
} while (!gs.barrier_passed);
}
else {
gs.barrier_passed = 1;
pthread_cond_broadcast(&gs.count_threads_cv);
}
pthread_mutex_unlock(&gs.count_threads_mutex);
/* Synchronization point for all threads (wait for finalization) */
pthread_mutex_lock(&gs.count_threads_mutex);
if (gs.count_threads > 0) {
gs.count_threads--;
do {
pthread_cond_wait(&gs.count_threads_cv, &gs.count_threads_mutex);
} while (gs.barrier_passed);
}
else {
gs.barrier_passed = 0;
pthread_cond_broadcast(&gs.count_threads_cv);
}
pthread_mutex_unlock(&gs.count_threads_mutex);
Py_END_ALLOW_THREADS;
/* Deallocate all the iterator and memsteps copies */
for (i = 1; i < gs.nthreads; ++i) {
NpyIter_Deallocate(th_params.iter[i]);
PyMem_Del(th_params.memsteps[i]);
}
ret = th_params.ret_code;
end:
pthread_mutex_unlock(&gs.parallel_mutex);
return ret;
}
static int
run_interpreter(NumExprObject *self, NpyIter *iter, NpyIter *reduce_iter,
bool reduction_outer_loop, bool need_output_buffering,
int *pc_error)
{
int r;
Py_ssize_t plen;
vm_params params;
char *errmsg = NULL;
*pc_error = -1;
if (PyBytes_AsStringAndSize(self->program, (char **)&(params.program),
&plen) < 0) {
return -1;
}
params.prog_len = (int)plen;
params.output = NULL;
params.inputs = NULL;
params.index_data = NULL;
params.n_inputs = self->n_inputs;
params.n_constants = self->n_constants;
params.n_temps = self->n_temps;
params.mem = self->mem;
params.memsteps = self->memsteps;
params.memsizes = self->memsizes;
params.r_end = (int)PyBytes_Size(self->fullsig);
params.out_buffer = NULL;
if ((gs.nthreads == 1) || gs.force_serial) {
// Can do it as one "task"
if (reduce_iter == NULL) {
// Allocate memory for output buffering if needed
vector<char> out_buffer(need_output_buffering ?
(self->memsizes[0] * BLOCK_SIZE1) : 0);
params.out_buffer = need_output_buffering ? &out_buffer[0] : NULL;
// Reset the iterator to allocate its buffers
if(NpyIter_Reset(iter, NULL) != NPY_SUCCEED) {
return -1;
}
get_temps_space(params, params.mem, BLOCK_SIZE1);
Py_BEGIN_ALLOW_THREADS;
r = vm_engine_iter_task(iter, params.memsteps,
params, pc_error, &errmsg);
Py_END_ALLOW_THREADS;
free_temps_space(params, params.mem);
}
else {
if (reduction_outer_loop) {
char **dataptr;
NpyIter_IterNextFunc *iternext;
dataptr = NpyIter_GetDataPtrArray(reduce_iter);
iternext = NpyIter_GetIterNext(reduce_iter, NULL);
if (iternext == NULL) {
return -1;
}
get_temps_space(params, params.mem, BLOCK_SIZE1);
Py_BEGIN_ALLOW_THREADS;
do {
r = NpyIter_ResetBasePointers(iter, dataptr, &errmsg);
if (r >= 0) {
r = vm_engine_iter_outer_reduce_task(iter,
params.memsteps, params,
pc_error, &errmsg);
}
if (r < 0) {
break;
}
} while (iternext(reduce_iter));
Py_END_ALLOW_THREADS;
free_temps_space(params, params.mem);
}
else {
char **dataptr;
NpyIter_IterNextFunc *iternext;
dataptr = NpyIter_GetDataPtrArray(iter);
iternext = NpyIter_GetIterNext(iter, NULL);
if (iternext == NULL) {
return -1;
}
get_temps_space(params, params.mem, BLOCK_SIZE1);
Py_BEGIN_ALLOW_THREADS;
do {
r = NpyIter_ResetBasePointers(reduce_iter, dataptr,
&errmsg);
if (r >= 0) {
r = vm_engine_iter_task(reduce_iter, params.memsteps,
params, pc_error, &errmsg);
}
if (r < 0) {
break;
}
} while (iternext(iter));
Py_END_ALLOW_THREADS;
free_temps_space(params, params.mem);
}
}
}
else {
if (reduce_iter == NULL) {
r = vm_engine_iter_parallel(iter, params, need_output_buffering,
pc_error, &errmsg);
}
else {
errmsg = (char *) "Parallel engine doesn't support reduction yet";
r = -1;
}
}
if (r < 0 && errmsg != NULL) {
PyErr_SetString(PyExc_RuntimeError, errmsg);
}
return 0;
}
static int
run_interpreter_const(NumExprObject *self, char *output, int *pc_error)
{
vm_params params;
Py_ssize_t plen;
char **mem;
npy_intp *memsteps;
*pc_error = -1;
if (PyBytes_AsStringAndSize(self->program, (char **)&(params.program),
&plen) < 0) {
return -1;
}
if (self->n_inputs != 0) {
return -1;
}
params.prog_len = (int)plen;
params.output = output;
params.inputs = NULL;
params.index_data = NULL;
params.n_inputs = self->n_inputs;
params.n_constants = self->n_constants;
params.n_temps = self->n_temps;
params.mem = self->mem;
memsteps = self->memsteps;
params.memsizes = self->memsizes;
params.r_end = (int)PyBytes_Size(self->fullsig);
mem = params.mem;
get_temps_space(params, mem, 1);
#define SINGLE_ITEM_CONST_LOOP
#define BLOCK_SIZE 1
#define NO_OUTPUT_BUFFERING // Because it's constant
#include "interp_body.cpp"
#undef NO_OUTPUT_BUFFERING
#undef BLOCK_SIZE
#undef SINGLE_ITEM_CONST_LOOP
free_temps_space(params, mem);
return 0;
}
PyObject *
NumExpr_run(NumExprObject *self, PyObject *args, PyObject *kwds)
{
PyArrayObject *operands[NPY_MAXARGS];
PyArray_Descr *dtypes[NPY_MAXARGS], **dtypes_tmp;
PyObject *tmp, *ret;
npy_uint32 op_flags[NPY_MAXARGS];
NPY_CASTING casting = NPY_SAFE_CASTING;
NPY_ORDER order = NPY_KEEPORDER;
unsigned int i, n_inputs;
int r, pc_error = 0;
int reduction_axis = -1;
npy_intp reduction_size = -1; // For #277 change this 1 -> -1 to be in-line with NumPy 1.8,
int ex_uses_vml = 0, is_reduction = 0;
bool reduction_outer_loop = false, need_output_buffering = false, full_reduction = false;
// To specify axes when doing a reduction
int op_axes_values[NPY_MAXARGS][NPY_MAXDIMS],
op_axes_reduction_values[NPY_MAXARGS];
int *op_axes_ptrs[NPY_MAXDIMS];
int oa_ndim = 0;