8000 BUG: better detect memory overlap when calling gufuncs by mattip · Pull Request #11381 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: better detect memory overlap when calling gufuncs #11381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion numpy/core/src/multiarray/einsum.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -2769,8 +2769,11 @@ PyArray_EinsteinSum(char *subscripts, npy_intp nop,

/* Initialize the output to all zeros and reset the iterator */
ret = NpyIter_GetOperandArray(iter)[nop];
Py_INCREF(ret);
PyArray_AssignZero(ret, NULL);
if (out) {
ret = out;
}
Py_INCREF(ret);


/***************************/
Expand Down
13 changes: 9 additions & 4 deletions numpy/core/src/umath/ufunc_object.c
Original file line number Diff line number Diff line change
Expand Up @@ -2609,8 +2609,11 @@ PyUFunc_GeneralizedFunction(PyUFuncObject *ufunc,
for (i = 0; i < nin; ++i) {
op_flags[i] = NPY_ITER_READONLY |
NPY_ITER_COPY |
NPY_ITER_ALIGNED |
NPY_ITER_OVERLAP_ASSUME_ELEMENTWISE;
NPY_ITER_ALIGNED;
if (op[i] && PyArray_NDIM(op[i]) < 2) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's special about 0d / 1d arrays?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My fix was wrong. Any element-wise gufunc (one with an empty signature '(),()->()' will not hit this code path, it will not have core_enabled. Removing this flag.

op_flags[i] |= NPY_ITER_OVERLAP_ASSUME_ELEMENTWISE;
}

/*
* If READWRITE flag has been set for this operand,
* then clear default READONLY flag
Expand All @@ -2625,8 +2628,10 @@ PyUFunc_GeneralizedFunction(PyUFuncObject *ufunc,
NPY_ITER_UPDATEIFCOPY|
NPY_ITER_ALIGNED|
NPY_ITER_ALLOCATE|
NPY_ITER_NO_BROADCAST|
NPY_ITER_OVERLAP_ASSUME_ELEMENTWISE;
NPY_ITER_NO_BROADCAST;
if (op[i] && PyArray_NDIM(op[i]) < 2) {
op_flags[i] |= NPY_ITER_OVERLAP_ASSUME_ELEMENTWISE;
}
}

iter_flags = ufunc->iter_flags |
Expand Down
5 changes: 5 additions & 0 deletions numpy/core/tests/test_mem_overlap.py
Original file line number Diff line number Diff line change
Expand Up @@ -948,3 +948,8 @@ def test_inplace_op_simple_manual(self):

x += x.T
assert_array_equal(x - x.T, 0)

def test_matrix_multiply_overlap():
a = np.arange(9, dtype=int).reshape(3,3)
b = a.copy()
assert_copy_equivalent(_umath_tests.matrix_multiply, [a, b], out=b)
7 changes: 7 additions & 0 deletions numpy/core/tests/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -7387,6 +7387,13 @@ def test_dot_out(self):
# if HAVE_CBLAS, will use WRITEBACKIFCOPY
a = np.arange(9, dtype=float).reshape(3,3)
b = np.dot(a, a, out=a)
assert b is a
assert_equal(b, np.array([[15, 18, 21], [42, 54, 66], [69, 90, 111]]))

def test_matmul_out(self):
a = np.arange(9, dtype=float).reshape(3,3)
b = np.matmul(a, a, out=a)
assert b is a
assert_equal(b, np.array([[15, 18, 21], [42, 54, 66], [69, 90, 111]]))

def test_view_assign(self):
Expand Down
0