diff options
author | seberg <sebastian@sipsolutions.net> | 2016-03-28 21:03:18 +0200 |
---|---|---|
committer | seberg <sebastian@sipsolutions.net> | 2016-03-28 21:03:18 +0200 |
commit | 75c5af30d7137fd308227028ebdee31dbe841e7f (patch) | |
tree | 09be52c3356d480a238d411c119625fddebacf2c /numpy/core | |
parent | 3327c388f519724a1877aaf9012eda21aa9593c2 (diff) | |
parent | 2df40376a944c39dbd68ac4510c3aa65a31ddf92 (diff) | |
download | numpy-75c5af30d7137fd308227028ebdee31dbe841e7f.tar.gz |
Merge pull request #7468 from jaimefrio/inplace_reduction_memmove
BUG: more on inplace reductions, fixes #615
Diffstat (limited to 'numpy/core')
-rw-r--r-- | numpy/core/src/umath/ufunc_object.c | 66 |
1 files changed, 38 insertions, 28 deletions
diff --git a/numpy/core/src/umath/ufunc_object.c b/numpy/core/src/umath/ufunc_object.c index 52299c1be..28c59f890 100644 --- a/numpy/core/src/umath/ufunc_object.c +++ b/numpy/core/src/umath/ufunc_object.c @@ -3293,13 +3293,16 @@ PyUFunc_Accumulate(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out, dataptr_copy[1] = dataptr[1]; dataptr_copy[2] = dataptr[0]; - /* Copy the first element to start the reduction */ + /* + * Copy the first element to start the reduction. + * + * Output (dataptr[0]) and input (dataptr[1]) may point to + * the same memory, e.g. np.add.accumulate(a, out=a). + */ if (otype == NPY_OBJECT) { /* - * Input (dataptr[0]) and output (dataptr[1]) may point - * to the same memory (i.e. np.add.accumulate(a, out=a)). - * In that case need to incref before decref to avoid the - * possibility of the reference count being zero temporarily. + * Incref before decref to avoid the possibility of the + * reference count being zero temporarily. */ Py_XINCREF(*(PyObject **)dataptr_copy[1]); Py_XDECREF(*(PyObject **)dataptr_copy[0]); @@ -3307,7 +3310,7 @@ PyUFunc_Accumulate(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out, *(PyObject **)dataptr_copy[1]; } else { - memcpy(dataptr_copy[0], dataptr_copy[1], itemsize); + memmove(dataptr_copy[0], dataptr_copy[1], itemsize); } if (count_m1 > 0) { @@ -3355,13 +3358,16 @@ PyUFunc_Accumulate(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out, dataptr_copy[1] = PyArray_BYTES(op[1]); dataptr_copy[2] = PyArray_BYTES(op[0]); - /* Copy the first element to start the reduction */ + /* + * Copy the first element to start the reduction. + * + * Output (dataptr[0]) and input (dataptr[1]) may point to the + * same memory, e.g. np.add.accumulate(a, out=a). + */ if (otype == NPY_OBJECT) { /* - * Input (dataptr[0]) and output (dataptr[1]) may point - * to the same memory (i.e. np.add.accumulate(a, out=a, axis=0)). - * In that case need to incref before decref to avoid the - * possibility of the reference count being zero temporarily. + * Incref before decref to avoid the possibility of the + * reference count being zero temporarily. */ Py_XINCREF(*(PyObject **)dataptr_copy[1]); Py_XDECREF(*(PyObject **)dataptr_copy[0]); @@ -3369,7 +3375,7 @@ PyUFunc_Accumulate(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out, *(PyObject **)dataptr_copy[1]; } else { - memcpy(dataptr_copy[0], dataptr_copy[1], itemsize); + memmove(dataptr_copy[0], dataptr_copy[1], itemsize); } if (count > 1) { @@ -3698,15 +3704,17 @@ PyUFunc_Reduceat(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *ind, dataptr_copy[1] = dataptr[1] + stride1*start; dataptr_copy[2] = dataptr[0] + stride0_ind*i; - /* Copy the first element to start the reduction */ + /* + * Copy the first element to start the reduction. + * + * Output (dataptr[0]) and input (dataptr[1]) may point + * to the same memory, e.g. + * np.add.reduceat(a, np.arange(len(a)), out=a). + */ if (otype == NPY_OBJECT) { /* - * Input (dataptr[0]) and output (dataptr[1]) may - * point to the same memory, e.g. - * np.add.reduceat(a, np.arange(len(a)), out=a). - * In that case need to incref before decref to - * avoid the possibility of the reference count - * being zero temporarily. + * Incref before decref to avoid the possibility of + * the reference count being zero temporarily. */ Py_XINCREF(*(PyObject **)dataptr_copy[1]); Py_XDECREF(*(PyObject **)dataptr_copy[0]); @@ -3714,7 +3722,7 @@ PyUFunc_Reduceat(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *ind, *(PyObject **)dataptr_copy[1]; } else { - memcpy(dataptr_copy[0], dataptr_copy[1], itemsize); + memmove(dataptr_copy[0], dataptr_copy[1], itemsize); } if (count > 1) { @@ -3764,15 +3772,17 @@ PyUFunc_Reduceat(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *ind, dataptr_copy[1] = PyArray_BYTES(op[1]) + stride1*start; dataptr_copy[2] = PyArray_BYTES(op[0]) + stride0_ind*i; - /* Copy the first element to start the reduction */ + /* + * Copy the first element to start the reduction. + * + * Output (dataptr[0]) and input (dataptr[1]) may point to + * the same memory, e.g. + * np.add.reduceat(a, np.arange(len(a)), out=a). + */ if (otype == NPY_OBJECT) { /* - * Input (dataptr[0]) and output (dataptr[1]) may point - * to the same memory, e.g. - * np.add.reduceat(a, np.arange(len(a)), out=a). - * In that case need to incref before decref to avoid - * the possibility of the reference count being zero - * temporarily. + * Incref before decref to avoid the possibility of the + * reference count being zero temporarily. */ Py_XINCREF(*(PyObject **)dataptr_copy[1]); Py_XDECREF(*(PyObject **)dataptr_copy[0]); @@ -3780,7 +3790,7 @@ PyUFunc_Reduceat(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *ind, *(PyObject **)dataptr_copy[1]; } else { - memcpy(dataptr_copy[0], dataptr_copy[1], itemsize); + memmove(dataptr_copy[0], dataptr_copy[1], itemsize); } if (count > 1) { |