Skip to content

Commit

Permalink
Merge pull request #25321 from dlfivefifty/dl/stridedarrayinterface
Browse files Browse the repository at this point in the history
Fix #17812, fix #25247
  • Loading branch information
timholy authored Jan 5, 2018
2 parents b10f30b + 346605b commit 0ddf9ed
Show file tree
Hide file tree
Showing 14 changed files with 690 additions and 464 deletions.
36 changes: 14 additions & 22 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -219,46 +219,38 @@ julia> last([1; 2; 3; 4])
last(a) = a[end]

"""
stride(A, k::Integer)
strides(A)
Return the distance in memory (in number of elements) between adjacent elements in dimension `k`.
Return a tuple of the memory strides in each dimension.
# Examples
```jldoctest
julia> A = fill(1, (3,4,5));
julia> stride(A,2)
3
julia> stride(A,3)
12
julia> strides(A)
(1, 3, 12)
```
"""
function stride(a::AbstractArray, i::Integer)
if i > ndims(a)
return length(a)
end
s = 1
for n = 1:(i-1)
s *= size(a, n)
end
return s
end
function strides end

"""
strides(A)
stride(A, k::Integer)
Return a tuple of the memory strides in each dimension.
Return the distance in memory (in number of elements) between adjacent elements in dimension `k`.
# Examples
```jldoctest
julia> A = fill(1, (3,4,5));
julia> strides(A)
(1, 3, 12)
julia> stride(A,2)
3
julia> stride(A,3)
12
```
"""
strides(A::AbstractArray) = size_to_strides(1, size(A)...)
stride(A::AbstractArray, k::Integer) = strides(A)[k]

@inline size_to_strides(s, d, sz...) = (s, size_to_strides(s * d, sz...)...)
size_to_strides(s, d) = (s,)
size_to_strides(s) = ()
Expand Down
16 changes: 16 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3813,6 +3813,21 @@ end
@deprecate getq(F::Factorization) F.Q
end

# Issues #17812 Remove default stride implementation
function strides(a::AbstractArray)
depwarn("""
`strides(a::AbstractArray)` is deprecated for general arrays.
Specialize `strides` for custom array types that have the appropriate representation in memory.
Warning: inappropriately implementing this method for an array type that does not use strided
storage may lead to incorrect results or segfaults.
""", :strides)
size_to_strides(1, size(a)...)
end

@deprecate substrides(s, parent, dim, I::Tuple) substrides(parent, strides(parent), I)

# END 0.7 deprecations

@deprecate lexcmp(x::AbstractArray, y::AbstractArray) cmp(x, y)
@deprecate lexcmp(x::Real, y::Real) cmp(isless, x, y)
@deprecate lexcmp(x::Complex, y::Complex) cmp((real(x),imag(x)), (real(y),imag(y)))
Expand Down Expand Up @@ -3871,6 +3886,7 @@ end

@deprecate findin(a, b) find(occursin(b), a)


# END 0.7 deprecations
# BEGIN 1.0 deprecations

Expand Down
181 changes: 105 additions & 76 deletions base/linalg/blas.jl

Large diffs are not rendered by default.

44 changes: 15 additions & 29 deletions base/linalg/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,35 +112,21 @@ true
isposdef(A::AbstractMatrix) = ishermitian(A) && isposdef(cholfact(Hermitian(A)))
isposdef(x::Number) = imag(x)==0 && real(x) > 0

"""
stride1(A) -> Int
Return the distance between successive array elements
in dimension 1 in units of element size.
# Examples
```jldoctest
julia> A = [1,2,3,4]
4-element Array{Int64,1}:
1
2
3
4
julia> Base.LinAlg.stride1(A)
1
julia> B = view(A, 2:2:4)
2-element view(::Array{Int64,1}, 2:2:4) with eltype Int64:
2
4
julia> Base.LinAlg.stride1(B)
2
```
"""
stride1(x::Array) = 1
stride1(x::StridedVector) = stride(x, 1)::Int
# the definition of strides for Array{T,N} is tuple() if N = 0, otherwise it is
# a tuple containing 1 and a cumulative product of the first N-1 sizes
# this definition is also used for StridedReshapedArray and StridedReinterpretedArray
# which have the same memory storage as Array
function stride(a::Union{DenseArray,StridedReshapedArray,StridedReinterpretArray}, i::Int)
if i > ndims(a)
return length(a)
end
s = 1
for n = 1:(i-1)
s *= size(a, n)
end
return s
end
strides(a::Union{DenseArray,StridedReshapedArray,StridedReinterpretArray}) = size_to_strides(1, size(a)...)

function norm(x::StridedVector{T}, rx::Union{UnitRange{TI},AbstractRange{TI}}) where {T<:BlasFloat,TI<:Integer}
if minimum(rx) < 1 || maximum(rx) > length(x)
Expand Down
Loading

0 comments on commit 0ddf9ed

Please sign in to comment.