Skip to content

Commit 0ddf9ed

Browse files
authored
Merge pull request #25321 from dlfivefifty/dl/stridedarrayinterface
Fix #17812, fix #25247
2 parents b10f30b + 346605b commit 0ddf9ed

File tree

14 files changed

+690
-464
lines changed

14 files changed

+690
-464
lines changed

base/abstractarray.jl

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -219,46 +219,38 @@ julia> last([1; 2; 3; 4])
219219
last(a) = a[end]
220220

221221
"""
222-
stride(A, k::Integer)
222+
strides(A)
223223
224-
Return the distance in memory (in number of elements) between adjacent elements in dimension `k`.
224+
Return a tuple of the memory strides in each dimension.
225225
226226
# Examples
227227
```jldoctest
228228
julia> A = fill(1, (3,4,5));
229229
230-
julia> stride(A,2)
231-
3
232-
233-
julia> stride(A,3)
234-
12
230+
julia> strides(A)
231+
(1, 3, 12)
235232
```
236233
"""
237-
function stride(a::AbstractArray, i::Integer)
238-
if i > ndims(a)
239-
return length(a)
240-
end
241-
s = 1
242-
for n = 1:(i-1)
243-
s *= size(a, n)
244-
end
245-
return s
246-
end
234+
function strides end
247235

248236
"""
249-
strides(A)
237+
stride(A, k::Integer)
250238
251-
Return a tuple of the memory strides in each dimension.
239+
Return the distance in memory (in number of elements) between adjacent elements in dimension `k`.
252240
253241
# Examples
254242
```jldoctest
255243
julia> A = fill(1, (3,4,5));
256244
257-
julia> strides(A)
258-
(1, 3, 12)
245+
julia> stride(A,2)
246+
3
247+
248+
julia> stride(A,3)
249+
12
259250
```
260251
"""
261-
strides(A::AbstractArray) = size_to_strides(1, size(A)...)
252+
stride(A::AbstractArray, k::Integer) = strides(A)[k]
253+
262254
@inline size_to_strides(s, d, sz...) = (s, size_to_strides(s * d, sz...)...)
263255
size_to_strides(s, d) = (s,)
264256
size_to_strides(s) = ()

base/deprecated.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3813,6 +3813,21 @@ end
38133813
@deprecate getq(F::Factorization) F.Q
38143814
end
38153815

3816+
# Issues #17812 Remove default stride implementation
3817+
function strides(a::AbstractArray)
3818+
depwarn("""
3819+
`strides(a::AbstractArray)` is deprecated for general arrays.
3820+
Specialize `strides` for custom array types that have the appropriate representation in memory.
3821+
Warning: inappropriately implementing this method for an array type that does not use strided
3822+
storage may lead to incorrect results or segfaults.
3823+
""", :strides)
3824+
size_to_strides(1, size(a)...)
3825+
end
3826+
3827+
@deprecate substrides(s, parent, dim, I::Tuple) substrides(parent, strides(parent), I)
3828+
3829+
# END 0.7 deprecations
3830+
38163831
@deprecate lexcmp(x::AbstractArray, y::AbstractArray) cmp(x, y)
38173832
@deprecate lexcmp(x::Real, y::Real) cmp(isless, x, y)
38183833
@deprecate lexcmp(x::Complex, y::Complex) cmp((real(x),imag(x)), (real(y),imag(y)))
@@ -3871,6 +3886,7 @@ end
38713886

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

3889+
38743890
# END 0.7 deprecations
38753891
# BEGIN 1.0 deprecations
38763892

base/linalg/blas.jl

Lines changed: 105 additions & 76 deletions
Large diffs are not rendered by default.

base/linalg/dense.jl

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -112,35 +112,21 @@ true
112112
isposdef(A::AbstractMatrix) = ishermitian(A) && isposdef(cholfact(Hermitian(A)))
113113
isposdef(x::Number) = imag(x)==0 && real(x) > 0
114114

115-
"""
116-
stride1(A) -> Int
117-
118-
Return the distance between successive array elements
119-
in dimension 1 in units of element size.
120-
121-
# Examples
122-
```jldoctest
123-
julia> A = [1,2,3,4]
124-
4-element Array{Int64,1}:
125-
1
126-
2
127-
3
128-
4
129-
130-
julia> Base.LinAlg.stride1(A)
131-
1
132-
133-
julia> B = view(A, 2:2:4)
134-
2-element view(::Array{Int64,1}, 2:2:4) with eltype Int64:
135-
2
136-
4
137-
138-
julia> Base.LinAlg.stride1(B)
139-
2
140-
```
141-
"""
142-
stride1(x::Array) = 1
143-
stride1(x::StridedVector) = stride(x, 1)::Int
115+
# the definition of strides for Array{T,N} is tuple() if N = 0, otherwise it is
116+
# a tuple containing 1 and a cumulative product of the first N-1 sizes
117+
# this definition is also used for StridedReshapedArray and StridedReinterpretedArray
118+
# which have the same memory storage as Array
119+
function stride(a::Union{DenseArray,StridedReshapedArray,StridedReinterpretArray}, i::Int)
120+
if i > ndims(a)
121+
return length(a)
122+
end
123+
s = 1
124+
for n = 1:(i-1)
125+
s *= size(a, n)
126+
end
127+
return s
128+
end
129+
strides(a::Union{DenseArray,StridedReshapedArray,StridedReinterpretArray}) = size_to_strides(1, size(a)...)
144130

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

0 commit comments

Comments
 (0)