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

stride/strides signature too broad? #17812

Closed
timholy opened this issue Aug 4, 2016 · 4 comments
Closed

stride/strides signature too broad? #17812

timholy opened this issue Aug 4, 2016 · 4 comments
Labels
arrays [a, r, r, a, y, s]

Comments

@timholy
Copy link
Member

timholy commented Aug 4, 2016

The docstring for stride:

help?> stride
search: stride strides StridedArray StridedVector StridedMatrix StridedVecOrMat strwidth SymTridiagonal

  stride(A, k)

  Returns the distance in memory (in number of elements) between adjacent elements in dimension k.

specifies "in memory", but the signature for stride is stride(a::AbstractArray, i::Integer), and some AbstractArrays do not even store their values in memory:

julia> stride(1:5, 1)
1

julia> isa(1:5, StridedArray)
false

Should we change it to StridedArray? The biggest problem with this is that StridedArray was the motivating poster child for the desirability of traits, because StridedArray cannot be extended to user types.

We could introduce an IsStrided trait,

strided(A::StridedArray) = IsStrided()
strided(A) = NotStrided()

and dispatch on it for strides and stride. But then that implicitly places some expectation that the linear algebra code might switch to the trait rather than dispatching on StridedArray, and that's a lot of code to modify. So I didn't want to do this lightly.

@stevengj
Copy link
Member

stevengj commented Aug 4, 2016

The stride implementation assumes column-major, so it seems like it should be specific to Array, not StridedArray. According to the manual, new DenseArray subtypes are supposed to provide their own stride implementations anyway.

@dlfivefifty
Copy link
Contributor

I'm working on resolving this issue (in conjunction with #25247) but there seems to be confusion on what stride is suppose to mean.

I take it this comment

https://github.com/JuliaLang/julia/blob/master/base/subarray.jl#L254

is wrong? And the code below involving substride should be re-written to call the parents stride?

@timholy
Copy link
Member Author

timholy commented Dec 28, 2017

Yes, the two notions (is it memory layout or is it essentially accumulate(*, size(A))?) are the main trouble here. I would be guided by the docstring, which specifies that the true meaning is memory layout.

In this case, though, I think the comment is referring to the fact that in

julia> A = [1 4; 2 5; 3 6]
3×2 Array{Int64,2}:
 1  4
 2  5
 3  6

julia> v = view(A, 1:2, :)
2×2 view(::Array{Int64,2}, 1:2, :) with eltype Int64:
 1  4
 2  5

v still has valid strides even though it doesn't have linear memory arrangement (in the sense that v[1, 2] is not the next location in memory after v[2, 1]).

As you suggest, perhaps the best rewrite would be in terms of calling stride on the parent.

@dlfivefifty
Copy link
Contributor

OK, I've done that in https://github.com/dlfivefifty/julia/tree/dl/stridedarrayinterface and just need to fix some lingering abuses in sparsevector.jl.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrays [a, r, r, a, y, s]
Projects
None yet
Development

No branches or pull requests

4 participants