8000 Merge pull request #7468 from jaimefrio/inplace_reduction_memmove · numpy/numpy@75c5af3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 75c5af3

Browse files
committed
Merge pull request #7468 from jaimefrio/inplace_reduction_memmove
BUG: more on inplace reductions, fixes #615
2 parents 3327c38 + 2df4037 commit 75c5af3

File tree

1 file changed

+38
-28
lines changed

1 file changed

+38
-28
lines changed

numpy/core/src/umath/ufunc_object.c

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3293,21 +3293,24 @@ PyUFunc_Accumulate(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out,
32933293
dataptr_copy[1] = dataptr[1];
32943294
dataptr_copy[2] = dataptr[0];
32953295

3296-
/* Copy the first element to start the reduction */
3296+
/*
3297+
* Copy the first element to start the reduction.
3298+
*
3299+
* Output (dataptr[0]) and input (dataptr[1]) may point to
3300+
* the same memory, e.g. np.add.accumulate(a, out=a).
3301+
*/
32973302
if (otype == NPY_OBJECT) {
32983303
/*
3299-
* Input (dataptr[0]) and output (dataptr[1]) may point
3300-
* to the same memory (i.e. np.add.accumulate(a, out=a)).
3301-
* In that case need to incref before decref to avoid the
3302-
* possibility of the reference count being zero temporarily.
3304+
* Incref before decref to avoid the possibility of the
3305+
* reference count being zero temporarily.
33033306
*/
33043307
Py_XINCREF(*(PyObject **)dataptr_copy[1]);
33053308
Py_XDECREF(*(PyObject **)dataptr_copy[0]);
33063309
*(PyObject **)dataptr_copy[0] =
33073310
*(PyObject **)dataptr_copy[1];
33083311
}
33093312
else {
3310-
memcpy(dataptr_copy[0], dataptr_copy[1], itemsize);
3313+
memmove(dataptr_copy[0], dataptr_copy[1], itemsize);
33113314
}
33123315

33133316
if (count_m1 > 0) {
@@ -3355,21 +3358,24 @@ PyUFunc_Accumulate(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *out,
33553358
dataptr_copy[1] = PyArray_BYTES(op[1]);
33563359
dataptr_copy[2] = PyArray_BYTES(op[0]);
33573360

3358-
/* Copy the first element to start the reduction */
3361+
/*
3362+
* Copy the first element to start the reduction.
3363+
*
3364+
* Output (dataptr[0]) and input (dataptr[1]) may point to the
3365+
* same memory, e.g. np.add.accumulate(a, out=a).
3366+
*/
33593367
if (otype == NPY_OBJECT) {
33603368
/*
3361-
* Input (dataptr[0]) and output (dataptr[1]) may point
3362-
* to the same memory (i.e. np.add.accumulate(a, out=a, axis=0)).
3363-
* In that case need to incref before decref to avoid the
3364-
* possibility of the reference count being zero temporarily.
3369+
* Incref before decref to avoid the possibility of the
3370+
* reference count being zero temporarily.
33653371
*/
33663372
Py_XINCREF(*(PyObject **)dataptr_copy[1]);
33673373
Py_XDECREF(*(PyObject **)dataptr_copy[0]);
33683374
*(PyObject **)dataptr_copy[0] =
33693375
*(PyObject **)dataptr_copy[1];
33703376
}
33713377
else {
3372-
memcpy(dataptr_copy[0], dataptr_copy[1], itemsize);
3378+
memmove(dataptr_copy[0], dataptr_copy[1], itemsize);
33733379
}
33743380

33753381
if (count > 1) {
@@ -3698,23 +3704,25 @@ PyUFunc_Reduceat(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *ind,
36983704
dataptr_copy[1] = dataptr[1] + stride1*start;
36993705
dataptr_copy[2] = dataptr[0] + stride0_ind*i;
37003706

3701-
/* Copy the first element to start the reduction */
3707+
/*
3708+
* Copy the first element to start the reduction.
3709+
*
3710+
* Output (dataptr[0]) and input (dataptr[1]) may point
3711+
* to the same memory, e.g.
3712+
* np.add.reduceat(a, np.arange(len(a)), out=a).
3713+
*/
37023714
if (otype == NPY_OBJECT) {
37033715
/*
3704-
* Input (dataptr[0]) and output (dataptr[1]) may
3705-
* point to the same memory, e.g.
3706-
* np.add.reduceat(a, np.arange(len(a)), out=a).
3707-
* In that case need to incref before decref to
3708-
* avoid the possibility of the reference count
3709-
* being zero temporarily.
3716+
* Incref before decref to avoid the possibility of
3717+
* the reference count being zero temporarily.
37103718
*/
37113719
Py_XINCREF(*(PyObject **)dataptr_copy[1]);
37123720
Py_XDECREF(*(PyObject **)dataptr_copy[0]);
37133721
*(PyObject **)dataptr_copy[0] =
37143722
*(PyObject **)dataptr_copy[1];
37153723
}
37163724
else {
3717-
memcpy(dataptr_copy[0], dataptr_copy[1], itemsize);
3725+
memmove(dataptr_copy[0], dataptr_copy[1], itemsize);
37183726
}
37193727

37203728
if (count > 1) {
@@ -3764,23 +3772,25 @@ PyUFunc_Reduceat(PyUFuncObject *ufunc, PyArrayObject *arr, PyArrayObject *ind,
37643772
dataptr_copy[1] = PyArray_BYTES(op[1]) + stride1*start;
37653773
dataptr_copy[2] = PyArray_BYTES(op[0]) + stride0_ind*i;
37663774

3767-
/* Copy the first element to start the reduction */
3775+
/*
3776+
* Copy the first element to start the reduction.
3777+
*
3778+
* Output (dataptr[0]) and input (dataptr[1]) may point to
3779+
* the same memory, e.g.
3780+
* np.add.reduceat(a, np.arange(len(a)), out=a).
3781+
*/
37683782
if (otype == NPY_OBJECT) {
37693783
/*
3770-
* Input (dataptr[0]) and output (dataptr[1]) may point
3771-
* to the same memory, e.g.
3772-
* np.add.reduceat(a, np.arange(len(a)), out=a).
3773-
* In that case need to incref before decref to avoid
3774-
* the possibility of the reference count being zero
3775-
* temporarily.
3784+
* Incref before decref to avoid the possibility of the
3785+
* reference count being zero temporarily.
37763786
*/
37773787
Py_XINCREF(*(PyObject **)dataptr_copy[1]);
37783788
Py_XDECREF(*(PyObject **)dataptr_copy[0]);
37793789
*(PyObject **)dataptr_copy[0] =
37803790
*(PyObject **)dataptr_copy[1];
37813791
}
37823792
else {
3783-
memcpy(dataptr_copy[0], dataptr_copy[1], itemsize);
3793+
memmove(dataptr_copy[0], dataptr_copy[1], itemsize);
37843794
}
37853795

37863796
if (count > 1) {

0 commit comments

Comments
 (0)
0