Skip to content

Fix strides for 0-sized empty arrays#36

Merged
inducer merged 1 commit into
inducer:mainfrom
alexfikl:fix-strides-empty-array
Oct 7, 2021
Merged

Fix strides for 0-sized empty arrays#36
inducer merged 1 commit into
inducer:mainfrom
alexfikl:fix-strides-empty-array

Conversation

@alexfikl

@alexfikl alexfikl commented Oct 1, 2021

Copy link
Copy Markdown
Contributor

For 0-sized arrays, this returned

>> c_contiguous_strides(8, (18, 0))
(0, 8)

which messes up cl.array.empty(queue, (18, 0), np.float64).strides. On the other hand, numpy gives

>>> np.empty((18, 0)).strides
(8, 8)

Not sure this is the right general fix, but it does the trick for this little example.

xref: inducer/arraycontext#91.

@inducer inducer left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks! I think this is fine, but I'd like better comments! :) And code duplication is bad (sorry!).

Comment thread array.py
Comment thread array.py
@alexfikl alexfikl force-pushed the fix-strides-empty-array branch 2 times, most recently from 9b5449f to 0c3bdcf Compare October 7, 2021 00:10
@alexfikl alexfikl force-pushed the fix-strides-empty-array branch from 0c3bdcf to dfa7a46 Compare October 7, 2021 00:31
@inducer

inducer commented Oct 7, 2021

Copy link
Copy Markdown
Owner

LGTM!

@inducer inducer merged commit 165b3ab into inducer:main Oct 7, 2021
@inducer

inducer commented Oct 7, 2021

Copy link
Copy Markdown
Owner

Can you pull this into the pyopencl change (inducer/pyopencl#514)? I'll pull it into pycuda and loopy.

@alexfikl alexfikl deleted the fix-strides-empty-array branch October 7, 2021 00:46
@inducer

inducer commented Oct 7, 2021

Copy link
Copy Markdown
Owner

@seberg

seberg commented Oct 8, 2021

Copy link
Copy Markdown

Super random, but NumPy indeed "ignores" size 1 dimensions when computing strides. However, NumPy will also when deciding whether strides are f or c-contiguous. So internally, numpy would consider the strides you had as valid f-contiguous strides (and will only sanitize this when exporting using the buffer protocol, which was necessary for cython at some point).

EDIT: To be clear, checking for contiguity, numpy completely ignores the stride value, and any stride value if the array is empty.

In that sense is_f_contiguous_strides is not the same as arr.flags.f_contiguous (it is not possible to do this by comparing strides only).

@inducer

inducer commented Oct 8, 2021

Copy link
Copy Markdown
Owner

Thanks for the heads-up! I've recorded this in #37.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants