Skip to content
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

Strides on when SubArray has a scalar sub index #11

Open
Tokazama opened this issue Aug 11, 2021 · 2 comments
Open

Strides on when SubArray has a scalar sub index #11

Tokazama opened this issue Aug 11, 2021 · 2 comments

Comments

@Tokazama
Copy link
Member

I've been getting incorrect results when using scalar indexing that doesn't start with one on views.

julia> A = zeros(3, 4, 5);

julia> A[:] = 1:60;

julia> Aview = view(A, :, 2, :);

julia> ArrayInterface.StrideIndex(Aview)[1,1]
1

julia> Aview[1,1]
4.0

julia> Aview = view(A, 2, :, :);

julia> ArrayInterface.StrideIndex(Aview)[1,1]
1

julia> Aview[1,1]
2.0

Should we be combining those scalar indices with strides?

@chriselrod
Copy link
Contributor

julia> A = zeros(3, 4, 5);

julia> A[:] = 1:60;

julia> Aview = view(A, :, 2, :);

julia> ArrayInterface.StrideIndex(Aview)[1,1]
1

julia> Aview[ArrayInterface.StrideIndex(Aview)[1,1]]
4.0

julia> Aview[1,1]
4.0

What's the desired behavior here?
I guess (for efficiency), that it returns a linear index into the memory of the array?

In that case, the StrideIndex object should carry an offset.
The offset should default to Zero() for most array types, for subarrays should be calculated using the offsets implied by the non-colon indices. E.g., (2 - offset(A,2)) * stride(A,2) for the first example, and (2 - offset(A,1)) * stride(A,1) for the second.

@chriselrod
Copy link
Contributor

Also, methods like pointer do the offsetting for you:

julia> using ArrayInterface

julia> A = zeros(3, 4, 5);

julia> A[:] = 1:60;

julia> Aview = view(A, :, 2, :);

julia> unsafe_load(pointer(Aview), ArrayInterface.StrideIndex(Aview)[1,1])
4.0

julia> Aview[1,1]
4.0

julia> Aview = view(A, 2, :, :);

julia> unsafe_load(pointer(Aview), ArrayInterface.StrideIndex(Aview)[1,1])
2.0

julia> Aview[1,1]
2.0

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

No branches or pull requests

2 participants