summaryrefslogtreecommitdiff
path: root/numpy/core/src/multiarray
diff options
context:
space:
mode:
authorSebastian Berg <sebastian@sipsolutions.net>2022-02-11 14:35:19 -0600
committerSebastian Berg <sebastian@sipsolutions.net>2022-02-11 14:53:01 -0600
commit0457cc7da06d98cc818a0ae1d3cc98ea93a1893d (patch)
tree49ce408981c79c7546dca44f783c527267b32891 /numpy/core/src/multiarray
parentf69ddd7111048111a7e486a2d7d008bd231af33d (diff)
downloadnumpy-0457cc7da06d98cc818a0ae1d3cc98ea93a1893d.tar.gz
MAINT: Remove the RELAXED_STRIDES_CHECKING env variable
An error is for now raised in `setup.py` if this is set, eventually we should just delete that.
Diffstat (limited to 'numpy/core/src/multiarray')
-rw-r--r--numpy/core/src/multiarray/buffer.c20
-rw-r--r--numpy/core/src/multiarray/ctors.c14
-rw-r--r--numpy/core/src/multiarray/flagsobject.c42
-rw-r--r--numpy/core/src/multiarray/multiarraymodule.c5
-rw-r--r--numpy/core/src/multiarray/shape.c7
5 files changed, 16 insertions, 72 deletions
diff --git a/numpy/core/src/multiarray/buffer.c b/numpy/core/src/multiarray/buffer.c
index 13d7038d3..0307d41a8 100644
--- a/numpy/core/src/multiarray/buffer.c
+++ b/numpy/core/src/multiarray/buffer.c
@@ -498,14 +498,11 @@ _buffer_info_new(PyObject *obj, int flags)
assert((size_t)info->shape % sizeof(npy_intp) == 0);
info->strides = info->shape + PyArray_NDIM(arr);
-#if NPY_RELAXED_STRIDES_CHECKING
/*
- * When NPY_RELAXED_STRIDES_CHECKING is used, some buffer users
- * may expect a contiguous buffer to have well formatted strides
- * also when a dimension is 1, but we do not guarantee this
- * internally. Thus, recalculate strides for contiguous arrays.
- * (This is unnecessary, but has no effect in the case where
- * NPY_RELAXED_STRIDES CHECKING is disabled.)
+ * Some buffer users may expect a contiguous buffer to have well
+ * formatted strides also when a dimension is 1, but we do not
+ * guarantee this internally. Thus, recalculate strides for
+ * contiguous arrays.
*/
int f_contiguous = (flags & PyBUF_F_CONTIGUOUS) == PyBUF_F_CONTIGUOUS;
if (PyArray_IS_C_CONTIGUOUS(arr) && !(
@@ -526,11 +523,6 @@ _buffer_info_new(PyObject *obj, int flags)
}
}
else {
-#else /* NPY_RELAXED_STRIDES_CHECKING */
- /* We can always use the arrays strides directly */
- {
-#endif
-
for (k = 0; k < PyArray_NDIM(arr); ++k) {
info->shape[k] = PyArray_DIMS(arr)[k];
info->strides[k] = PyArray_STRIDES(arr)[k];
@@ -708,8 +700,8 @@ _buffer_get_info(void **buffer_info_cache_ptr, PyObject *obj, int flags)
if (info->ndim > 1 && next_info != NULL) {
/*
* Some arrays are C- and F-contiguous and if they have more
- * than one dimension, the buffer-info may differ between
- * the two due to RELAXED_STRIDES_CHECKING.
+ * than one dimension, the buffer-info may differ between the
+ * two because strides for length 1 dimension may be adjusted.
* If we export both buffers, the first stored one may be
* the one for the other contiguity, so check both.
* This is generally very unlikely in all other cases, since
diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c
index c2842d7ba..58ba0c2a1 100644
--- a/numpy/core/src/multiarray/ctors.c
+++ b/numpy/core/src/multiarray/ctors.c
@@ -4020,7 +4020,6 @@ _array_fill_strides(npy_intp *strides, npy_intp const *dims, int nd, size_t item
int inflag, int *objflags)
{
int i;
-#if NPY_RELAXED_STRIDES_CHECKING
npy_bool not_cf_contig = 0;
npy_bool nod = 0; /* A dim != 1 was found */
@@ -4034,7 +4033,6 @@ _array_fill_strides(npy_intp *strides, npy_intp const *dims, int nd, size_t item
nod = 1;
}
}
-#endif /* NPY_RELAXED_STRIDES_CHECKING */
/* Only make Fortran strides if not contiguous as well */
if ((inflag & (NPY_ARRAY_F_CONTIGUOUS|NPY_ARRAY_C_CONTIGUOUS)) ==
@@ -4044,7 +4042,6 @@ _array_fill_strides(npy_intp *strides, npy_intp const *dims, int nd, size_t item
if (dims[i]) {
itemsize *= dims[i];
}
-#if NPY_RELAXED_STRIDES_CHECKING
else {
not_cf_contig = 0;
}
@@ -4054,13 +4051,8 @@ _array_fill_strides(npy_intp *strides, npy_intp const *dims, int nd, size_t item
strides[i] = NPY_MAX_INTP;
}
#endif /* NPY_RELAXED_STRIDES_DEBUG */
-#endif /* NPY_RELAXED_STRIDES_CHECKING */
}
-#if NPY_RELAXED_STRIDES_CHECKING
if (not_cf_contig) {
-#else /* not NPY_RELAXED_STRIDES_CHECKING */
- if ((nd > 1) && ((strides[0] != strides[nd-1]) || (dims[nd-1] > 1))) {
-#endif /* not NPY_RELAXED_STRIDES_CHECKING */
*objflags = ((*objflags)|NPY_ARRAY_F_CONTIGUOUS) &
~NPY_ARRAY_C_CONTIGUOUS;
}
@@ -4074,7 +4066,6 @@ _array_fill_strides(npy_intp *strides, npy_intp const *dims, int nd, size_t item
if (dims[i]) {
itemsize *= dims[i];
}
-#if NPY_RELAXED_STRIDES_CHECKING
else {
not_cf_contig = 0;
}
@@ -4084,13 +4075,8 @@ _array_fill_strides(npy_intp *strides, npy_intp const *dims, int nd, size_t item
strides[i] = NPY_MAX_INTP;
}
#endif /* NPY_RELAXED_STRIDES_DEBUG */
-#endif /* NPY_RELAXED_STRIDES_CHECKING */
}
-#if NPY_RELAXED_STRIDES_CHECKING
if (not_cf_contig) {
-#else /* not NPY_RELAXED_STRIDES_CHECKING */
- if ((nd > 1) && ((strides[0] != strides[nd-1]) || (dims[0] > 1))) {
-#endif /* not NPY_RELAXED_STRIDES_CHECKING */
*objflags = ((*objflags)|NPY_ARRAY_C_CONTIGUOUS) &
~NPY_ARRAY_F_CONTIGUOUS;
}
diff --git a/numpy/core/src/multiarray/flagsobject.c b/numpy/core/src/multiarray/flagsobject.c
index b5bd7c8c1..adbfb22e7 100644
--- a/numpy/core/src/multiarray/flagsobject.c
+++ b/numpy/core/src/multiarray/flagsobject.c
@@ -105,20 +105,11 @@ PyArray_UpdateFlags(PyArrayObject *ret, int flagmask)
*
* According to these rules, a 0- or 1-dimensional array is either both
* C- and F-contiguous, or neither; and an array with 2+ dimensions
- * can be C- or F- contiguous, or neither, but not both. Though there
- * there are exceptions for arrays with zero or one item, in the first
- * case the check is relaxed up to and including the first dimension
- * with shape[i] == 0. In the second case `strides == itemsize` will
- * can be true for all dimensions and both flags are set.
- *
- * When NPY_RELAXED_STRIDES_CHECKING is set, we use a more accurate
- * definition of C- and F-contiguity, in which all 0-sized arrays are
- * contiguous (regardless of dimensionality), and if shape[i] == 1
- * then we ignore strides[i] (since it has no affect on memory layout).
- * With these new rules, it is possible for e.g. a 10x1 array to be both
- * C- and F-contiguous -- but, they break downstream code which assumes
- * that for contiguous arrays strides[-1] (resp. strides[0]) always
- * contains the itemsize.
+ * can be C- or F- contiguous, or neither, but not both (unless it has only
+ * a single element).
+ * We correct this, however. When a dimension has length 1, its stride is
+ * never used and thus has no effect on the memory layout.
+ * The above rules thus only apply when ignorning all size 1 dimenions.
*/
static void
_UpdateContiguousFlags(PyArrayObject *ap)
@@ -131,7 +122,6 @@ _UpdateContiguousFlags(PyArrayObject *ap)
sd = PyArray_ITEMSIZE(ap);
for (i = PyArray_NDIM(ap) - 1; i >= 0; --i) {
dim = PyArray_DIMS(ap)[i];
-#if NPY_RELAXED_STRIDES_CHECKING
/* contiguous by definition */
if (dim == 0) {
PyArray_ENABLEFLAGS(ap, NPY_ARRAY_C_CONTIGUOUS);
@@ -144,17 +134,6 @@ _UpdateContiguousFlags(PyArrayObject *ap)
}
sd *= dim;
}
-#else /* not NPY_RELAXED_STRIDES_CHECKING */
- if (PyArray_STRIDES(ap)[i] != sd) {
- is_c_contig = 0;
- break;
- }
- /* contiguous, if it got this far */
- if (dim == 0) {
- break;
- }
- sd *= dim;
-#endif /* not NPY_RELAXED_STRIDES_CHECKING */
}
if (is_c_contig) {
PyArray_ENABLEFLAGS(ap, NPY_ARRAY_C_CONTIGUOUS);
@@ -167,7 +146,6 @@ _UpdateContiguousFlags(PyArrayObject *ap)
sd = PyArray_ITEMSIZE(ap);
for (i = 0; i < PyArray_NDIM(ap); ++i) {
dim = PyArray_DIMS(ap)[i];
-#if NPY_RELAXED_STRIDES_CHECKING
if (dim != 1) {
if (PyArray_STRIDES(ap)[i] != sd) {
PyArray_CLEARFLAGS(ap, NPY_ARRAY_F_CONTIGUOUS);
@@ -175,16 +153,6 @@ _UpdateContiguousFlags(PyArrayObject *ap)
}
sd *= dim;
}
-#else /* not NPY_RELAXED_STRIDES_CHECKING */
- if (PyArray_STRIDES(ap)[i] != sd) {
- PyArray_CLEARFLAGS(ap, NPY_ARRAY_F_CONTIGUOUS);
- return;
- }
- if (dim == 0) {
- break;
- }
- sd *= dim;
-#endif /* not NPY_RELAXED_STRIDES_CHECKING */
}
PyArray_ENABLEFLAGS(ap, NPY_ARRAY_F_CONTIGUOUS);
return;
diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c
index a7b6898e1..947e54d0a 100644
--- a/numpy/core/src/multiarray/multiarraymodule.c
+++ b/numpy/core/src/multiarray/multiarraymodule.c
@@ -1533,8 +1533,9 @@ PyArray_EquivTypenums(int typenum1, int typenum2)
/*** END C-API FUNCTIONS **/
/*
- * NPY_RELAXED_STRIDES_CHECKING: If the strides logic is changed, the
- * order specific stride setting is not necessary.
+ * NOTE: The order specific stride setting is not necessary to preserve
+ * contiguity and could be removed. However, this way the resulting
+ * strides do currently look clearer for fortran order inputs.
*/
static NPY_STEALS_REF_TO_ARG(1) PyObject *
_prepend_ones(PyArrayObject *arr, int nd, int ndmin, NPY_ORDER order)
diff --git a/numpy/core/src/multiarray/shape.c b/numpy/core/src/multiarray/shape.c
index 162abd6a4..98f65415b 100644
--- a/numpy/core/src/multiarray/shape.c
+++ b/numpy/core/src/multiarray/shape.c
@@ -244,11 +244,9 @@ PyArray_Newshape(PyArrayObject *self, PyArray_Dims *newdims,
* in order to get the right orientation and
* because we can't just re-use the buffer with the
* data in the order it is in.
- * NPY_RELAXED_STRIDES_CHECKING: size check is unnecessary when set.
*/
Py_INCREF(self);
- if ((PyArray_SIZE(self) > 1) &&
- ((order == NPY_CORDER && !PyArray_IS_C_CONTIGUOUS(self)) ||
+ if (((order == NPY_CORDER && !PyArray_IS_C_CONTIGUOUS(self)) ||
(order == NPY_FORTRANORDER && !PyArray_IS_F_CONTIGUOUS(self)))) {
int success = 0;
success = _attempt_nocopy_reshape(self, ndim, dimensions,
@@ -1000,7 +998,6 @@ PyArray_Flatten(PyArrayObject *a, NPY_ORDER order)
* If an axis flagged for removal has a shape larger than one,
* the aligned flag (and in the future the contiguous flags),
* may need explicit update.
- * (check also NPY_RELAXED_STRIDES_CHECKING)
*
* For example, this can be used to remove the reduction axes
* from a reduction result once its computation is complete.
@@ -1024,6 +1021,6 @@ PyArray_RemoveAxesInPlace(PyArrayObject *arr, const npy_bool *flags)
/* The final number of dimensions */
fa->nd = idim_out;
- /* May not be necessary for NPY_RELAXED_STRIDES_CHECKING (see comment) */
+ /* NOTE: This is only necessary if a dimension with size != 1 was removed */
PyArray_UpdateFlags(arr, NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_F_CONTIGUOUS);
}