diff options
-rw-r--r-- | numpy/core/include/numpy/ndarraytypes.h | 4 | ||||
-rw-r--r-- | numpy/core/src/multiarray/arraytypes.c.src | 13 | ||||
-rw-r--r-- | numpy/core/src/multiarray/common.c | 2 | ||||
-rw-r--r-- | numpy/core/src/multiarray/common.h | 10 | ||||
-rw-r--r-- | numpy/core/src/multiarray/ctors.c | 6 | ||||
-rw-r--r-- | numpy/core/src/multiarray/item_selection.c | 15 | ||||
-rw-r--r-- | numpy/core/src/multiarray/iterators.c | 12 | ||||
-rw-r--r-- | numpy/core/src/multiarray/lowlevel_strided_loops.c.src | 8 | ||||
-rw-r--r-- | numpy/core/src/multiarray/mapping.c | 9 | ||||
-rw-r--r-- | numpy/core/src/multiarray/methods.c | 4 |
10 files changed, 54 insertions, 29 deletions
diff --git a/numpy/core/include/numpy/ndarraytypes.h b/numpy/core/include/numpy/ndarraytypes.h index 27970ff77..cc0206d44 100644 --- a/numpy/core/include/numpy/ndarraytypes.h +++ b/numpy/core/include/numpy/ndarraytypes.h @@ -928,10 +928,11 @@ typedef int (PyArray_FinalizeFunc)(PyArrayObject *, PyObject *); #define PyArray_IS_C_CONTIGUOUS(m) PyArray_CHKFLAGS(m, NPY_ARRAY_C_CONTIGUOUS) #define PyArray_IS_F_CONTIGUOUS(m) PyArray_CHKFLAGS(m, NPY_ARRAY_F_CONTIGUOUS) +/* the variable is used in some places, so always define it */ +#define NPY_BEGIN_THREADS_DEF PyThreadState *_save=NULL; #if NPY_ALLOW_THREADS #define NPY_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS #define NPY_END_ALLOW_THREADS Py_END_ALLOW_THREADS -#define NPY_BEGIN_THREADS_DEF PyThreadState *_save=NULL; #define NPY_BEGIN_THREADS do {_save = PyEval_SaveThread();} while (0); #define NPY_END_THREADS do {if (_save) PyEval_RestoreThread(_save);} while (0); #define NPY_BEGIN_THREADS_THRESHOLDED(loop_size) do { if (loop_size > 500) \ @@ -951,7 +952,6 @@ typedef int (PyArray_FinalizeFunc)(PyArrayObject *, PyObject *); #else #define NPY_BEGIN_ALLOW_THREADS #define NPY_END_ALLOW_THREADS -#define NPY_BEGIN_THREADS_DEF #define NPY_BEGIN_THREADS #define NPY_END_THREADS #define NPY_BEGIN_THREADS_THRESHOLDED(loop_size) diff --git a/numpy/core/src/multiarray/arraytypes.c.src b/numpy/core/src/multiarray/arraytypes.c.src index 0f75c8aa5..a430e1ee4 100644 --- a/numpy/core/src/multiarray/arraytypes.c.src +++ b/numpy/core/src/multiarray/arraytypes.c.src @@ -3501,15 +3501,22 @@ static int NPY_CLIPMODE clipmode) { npy_intp i, j, k, tmp; + NPY_BEGIN_THREADS_DEF; + + NPY_BEGIN_THREADS; switch(clipmode) { case NPY_RAISE: for (i = 0; i < n_outer; i++) { for (j = 0; j < m_middle; j++) { tmp = indarray[j]; - /* We don't know what axis we're operating on, so don't report it in case of an error. */ - if (check_and_adjust_index(&tmp, nindarray, -1) < 0) + /* + * We don't know what axis we're operating on, + * so don't report it in case of an error. + */ + if (check_and_adjust_index(&tmp, nindarray, -1, _save) < 0) { return 1; + } if (NPY_LIKELY(nelem == 1)) { *dest++ = *(src + tmp); } @@ -3571,6 +3578,8 @@ static int } break; } + + NPY_END_THREADS; return 0; } /**end repeat**/ diff --git a/numpy/core/src/multiarray/common.c b/numpy/core/src/multiarray/common.c index 48d13cd6e..0e8a21394 100644 --- a/numpy/core/src/multiarray/common.c +++ b/numpy/core/src/multiarray/common.c @@ -618,7 +618,7 @@ index2ptr(PyArrayObject *mp, npy_intp i) return NULL; } dim0 = PyArray_DIMS(mp)[0]; - if (check_and_adjust_index(&i, dim0, 0) < 0) + if (check_and_adjust_index(&i, dim0, 0, NULL) < 0) return NULL; if (i == 0) { return PyArray_DATA(mp); diff --git a/numpy/core/src/multiarray/common.h b/numpy/core/src/multiarray/common.h index 8510a90b5..d64411115 100644 --- a/numpy/core/src/multiarray/common.h +++ b/numpy/core/src/multiarray/common.h @@ -82,12 +82,17 @@ convert_shape_to_string(npy_intp n, npy_intp *vals, char *ending); * 0 <= *index < max_item, and returns 0. * 'axis' should be the array axis that is being indexed over, if known. If * unknown, use -1. + * If _save is NULL it is assumed the GIL is taken + * If _save is not NULL it is assumed the GIL is not taken and it + * is acquired in the case of an error */ static NPY_INLINE int -check_and_adjust_index(npy_intp *index, npy_intp max_item, int axis) +check_and_adjust_index(npy_intp *index, npy_intp max_item, int axis, + PyThreadState * _save) { /* Check that index is valid, taking into account negative indices */ if (NPY_UNLIKELY((*index < -max_item) || (*index >= max_item))) { + NPY_END_THREADS; /* Try to be as clear as possible about what went wrong. */ if (axis >= 0) { PyErr_Format(PyExc_IndexError, @@ -97,8 +102,7 @@ check_and_adjust_index(npy_intp *index, npy_intp max_item, int axis) } else { PyErr_Format(PyExc_IndexError, "index %"NPY_INTP_FMT" is out of bounds " - "for size %"NPY_INTP_FMT, - *index, max_item); + "for size %"NPY_INTP_FMT, *index, max_item); } return -1; } diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index 7c1c62543..2ab46d258 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -2843,6 +2843,7 @@ PyArray_Arange(double start, double stop, double step, int type_num) PyArray_ArrFuncs *funcs; PyObject *obj; int ret; + NPY_BEGIN_THREADS_DEF; if (_safe_ceil_to_intp((stop - start)/step, &length)) { PyErr_SetString(PyExc_OverflowError, @@ -2890,7 +2891,9 @@ PyArray_Arange(double start, double stop, double step, int type_num) Py_DECREF(range); return NULL; } + NPY_BEGIN_THREADS_DESCR(PyArray_DESCR(range)); funcs->fill(PyArray_DATA(range), length, range); + NPY_END_THREADS; if (PyErr_Occurred()) { goto fail; } @@ -2987,6 +2990,7 @@ PyArray_ArangeObj(PyObject *start, PyObject *stop, PyObject *step, PyArray_Descr npy_intp length; PyArray_Descr *native = NULL; int swap; + NPY_BEGIN_THREADS_DEF; /* Datetime arange is handled specially */ if ((dtype != NULL && (dtype->type_num == NPY_DATETIME || @@ -3103,7 +3107,9 @@ PyArray_ArangeObj(PyObject *start, PyObject *stop, PyObject *step, PyArray_Descr Py_DECREF(range); goto fail; } + NPY_BEGIN_THREADS_DESCR(PyArray_DESCR(range)); funcs->fill(PyArray_DATA(range), length, range); + NPY_END_THREADS; if (PyErr_Occurred()) { goto fail; } diff --git a/numpy/core/src/multiarray/item_selection.c b/numpy/core/src/multiarray/item_selection.c index d93b5d6c9..71c17c9c9 100644 --- a/numpy/core/src/multiarray/item_selection.c +++ b/numpy/core/src/multiarray/item_selection.c @@ -129,12 +129,15 @@ PyArray_TakeFrom(PyArrayObject *self0, PyObject *indices0, int axis, func = PyArray_DESCR(self)->f->fasttake; if (func == NULL) { + NPY_BEGIN_THREADS_DEF; + NPY_BEGIN_THREADS_DESCR(PyArray_DESCR(self)); switch(clipmode) { case NPY_RAISE: for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { tmp = ((npy_intp *)(PyArray_DATA(indices)))[j]; - if (check_and_adjust_index(&tmp, max_item, axis) < 0) { + if (check_and_adjust_index(&tmp, max_item, axis, + _save) < 0) { goto fail; } tmp_src = src + tmp * chunk; @@ -216,8 +219,10 @@ PyArray_TakeFrom(PyArrayObject *self0, PyObject *indices0, int axis, } break; } + NPY_END_THREADS; } else { + /* no gil release, need it for error reporting */ err = func(dest, src, (npy_intp *)(PyArray_DATA(indices)), max_item, n, m, nelem, clipmode); if (err) { @@ -300,7 +305,7 @@ PyArray_PutTo(PyArrayObject *self, PyObject* values0, PyObject *indices0, for (i = 0; i < ni; i++) { src = PyArray_BYTES(values) + chunk*(i % nv); tmp = ((npy_intp *)(PyArray_DATA(indices)))[i]; - if (check_and_adjust_index(&tmp, max_item, 0) < 0) { + if (check_and_adjust_index(&tmp, max_item, 0, NULL) < 0) { goto fail; } PyArray_Item_INCREF(src, PyArray_DESCR(self)); @@ -350,7 +355,7 @@ PyArray_PutTo(PyArrayObject *self, PyObject* values0, PyObject *indices0, for (i = 0; i < ni; i++) { src = PyArray_BYTES(values) + chunk * (i % nv); tmp = ((npy_intp *)(PyArray_DATA(indices)))[i]; - if (check_and_adjust_index(&tmp, max_item, 0) < 0) { + if (check_and_adjust_index(&tmp, max_item, 0, NULL) < 0) { goto fail; } memmove(dest + tmp * chunk, src, chunk); @@ -2635,7 +2640,7 @@ PyArray_MultiIndexGetItem(PyArrayObject *self, npy_intp *multi_index) npy_intp shapevalue = shape[idim]; npy_intp ind = multi_index[idim]; - if (check_and_adjust_index(&ind, shapevalue, idim) < 0) { + if (check_and_adjust_index(&ind, shapevalue, idim, NULL) < 0) { return NULL; } data += ind * strides[idim]; @@ -2664,7 +2669,7 @@ PyArray_MultiIndexSetItem(PyArrayObject *self, npy_intp *multi_index, npy_intp shapevalue = shape[idim]; npy_intp ind = multi_index[idim]; - if (check_and_adjust_index(&ind, shapevalue, idim) < 0) { + if (check_and_adjust_index(&ind, shapevalue, idim, NULL) < 0) { return -1; } data += ind * strides[idim]; diff --git a/numpy/core/src/multiarray/iterators.c b/numpy/core/src/multiarray/iterators.c index e04ac3bec..81c6b2a8e 100644 --- a/numpy/core/src/multiarray/iterators.c +++ b/numpy/core/src/multiarray/iterators.c @@ -71,7 +71,7 @@ parse_index_entry(PyObject *op, npy_intp *step_size, *n_steps = SINGLE_INDEX; *step_size = 0; if (check_index) { - if (check_and_adjust_index(&i, max, axis) < 0) { + if (check_and_adjust_index(&i, max, axis, NULL) < 0) { goto fail; } } @@ -668,7 +668,7 @@ iter_subscript_int(PyArrayIterObject *self, PyArrayObject *ind) itemsize = PyArray_DESCR(self->ao)->elsize; if (PyArray_NDIM(ind) == 0) { num = *((npy_intp *)PyArray_DATA(ind)); - if (check_and_adjust_index(&num, self->size, -1) < 0) { + if (check_and_adjust_index(&num, self->size, -1, NULL) < 0) { PyArray_ITER_RESET(self); return NULL; } @@ -702,7 +702,7 @@ iter_subscript_int(PyArrayIterObject *self, PyArrayObject *ind) swap = (PyArray_ISNOTSWAPPED(ret) != PyArray_ISNOTSWAPPED(self->ao)); while (counter--) { num = *((npy_intp *)(ind_it->dataptr)); - if (check_and_adjust_index(&num, self->size, -1) < 0) { + if (check_and_adjust_index(&num, self->size, -1, NULL) < 0) { Py_DECREF(ind_it); Py_DECREF(ret); PyArray_ITER_RESET(self); @@ -926,7 +926,7 @@ iter_ass_sub_int(PyArrayIterObject *self, PyArrayObject *ind, copyswap = PyArray_DESCR(self->ao)->f->copyswap; if (PyArray_NDIM(ind) == 0) { num = *((npy_intp *)PyArray_DATA(ind)); - if (check_and_adjust_index(&num, self->size, -1) < 0) { + if (check_and_adjust_index(&num, self->size, -1, NULL) < 0) { return -1; } PyArray_ITER_GOTO1D(self, num); @@ -940,7 +940,7 @@ iter_ass_sub_int(PyArrayIterObject *self, PyArrayObject *ind, counter = ind_it->size; while (counter--) { num = *((npy_intp *)(ind_it->dataptr)); - if (check_and_adjust_index(&num, self->size, -1) < 0) { + if (check_and_adjust_index(&num, self->size, -1, NULL) < 0) { Py_DECREF(ind_it); return -1; } @@ -1017,7 +1017,7 @@ iter_ass_subscript(PyArrayIterObject *self, PyObject *ind, PyObject *val) PyErr_Clear(); } else { - if (check_and_adjust_index(&start, self->size, -1) < 0) { + if (check_and_adjust_index(&start, self->size, -1, NULL) < 0) { goto finish; } retval = 0; diff --git a/numpy/core/src/multiarray/lowlevel_strided_loops.c.src b/numpy/core/src/multiarray/lowlevel_strided_loops.c.src index 042aa2a62..bd69c5ca9 100644 --- a/numpy/core/src/multiarray/lowlevel_strided_loops.c.src +++ b/numpy/core/src/multiarray/lowlevel_strided_loops.c.src @@ -1396,7 +1396,7 @@ mapiter_trivial_@name@(PyArrayObject *self, PyArrayObject *ind, /* Check the indices beforehand */ while (itersize--) { indval = *((npy_intp*)ind_ptr); - if (check_and_adjust_index(&indval, fancy_dim, 1) < 0 ) { + if (check_and_adjust_index(&indval, fancy_dim, 1, NULL) < 0 ) { return -1; } ind_ptr += ind_stride; @@ -1426,7 +1426,7 @@ mapiter_trivial_@name@(PyArrayObject *self, PyArrayObject *ind, while (itersize--) { indval = *((npy_intp*)ind_ptr); #if @isget@ - if (check_and_adjust_index(&indval, fancy_dim, 1) < 0 ) { + if (check_and_adjust_index(&indval, fancy_dim, 1, NULL) < 0 ) { return -1; } #else @@ -1560,7 +1560,7 @@ mapiter_@name@(PyArrayMapIterObject *mit) #if @isget@ && @one_iter@ if (check_and_adjust_index(&indval, fancy_dims[i], - iteraxis) < 0 ) { + iteraxis, NULL) < 0 ) { return -1; } #else @@ -1662,7 +1662,7 @@ mapiter_@name@(PyArrayMapIterObject *mit) #if @isget@ && @one_iter@ if (check_and_adjust_index(&indval, fancy_dims[i], - iteraxis) < 0 ) { + iteraxis, NULL) < 0 ) { NPY_AUXDATA_FREE(transferdata); return -1; } diff --git a/numpy/core/src/multiarray/mapping.c b/numpy/core/src/multiarray/mapping.c index 6d7e4ffe3..cafec7689 100644 --- a/numpy/core/src/multiarray/mapping.c +++ b/numpy/core/src/multiarray/mapping.c @@ -799,7 +799,7 @@ get_item_pointer(PyArrayObject *self, char **ptr, *ptr = PyArray_BYTES(self); for (i=0; i < index_num; i++) { if ((check_and_adjust_index(&(indices[i].value), - PyArray_DIMS(self)[i], i)) < 0) { + PyArray_DIMS(self)[i], i, NULL)) < 0) { return -1; } *ptr += PyArray_STRIDE(self, i) * indices[i].value; @@ -842,7 +842,8 @@ get_view_from_index(PyArrayObject *self, PyArrayObject **view, switch (indices[i].type) { case HAS_INTEGER: if ((check_and_adjust_index(&indices[i].value, - PyArray_DIMS(self)[orig_dim], orig_dim)) < 0) { + PyArray_DIMS(self)[orig_dim], orig_dim, + NULL)) < 0) { return -1; } data_ptr += PyArray_STRIDE(self, orig_dim) * indices[i].value; @@ -2366,7 +2367,7 @@ PyArray_MapIterCheckIndices(PyArrayMapIterObject *mit) while (itersize--) { indval = *((npy_intp*)data); if (check_and_adjust_index(&indval, - outer_dim, outer_axis) < 0) { + outer_dim, outer_axis, NULL) < 0) { return -1; } data += stride; @@ -2400,7 +2401,7 @@ PyArray_MapIterCheckIndices(PyArrayMapIterObject *mit) while (itersize--) { indval = *((npy_intp*)*iterptr); if (check_and_adjust_index(&indval, - outer_dim, outer_axis) < 0) { + outer_dim, outer_axis, NULL) < 0) { Py_DECREF(intp_type); NpyIter_Deallocate(op_iter); diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index 91f639650..b68745085 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -647,7 +647,7 @@ array_toscalar(PyArrayObject *self, PyObject *args) return NULL; } - if (check_and_adjust_index(&value, size, -1) < 0) { + if (check_and_adjust_index(&value, size, -1, NULL) < 0) { return NULL; } @@ -724,7 +724,7 @@ array_setscalar(PyArrayObject *self, PyObject *args) return NULL; } - if (check_and_adjust_index(&value, size, -1) < 0) { + if (check_and_adjust_index(&value, size, -1, NULL) < 0) { return NULL; } |