Skip to content

Commit

Permalink
Make strides into a generic trait
Browse files Browse the repository at this point in the history
Returns `nothing` for non-strided arrays, otherwise gives the
give strides in memory. Useful as an extensible trait in generic
contexts, and simpler to overload for cases of "wrapped" arrays where
"stridedness" can be deferred to the parent rather than a complex
(and inextensible) method signature.
  • Loading branch information
Andy Ferris committed Dec 18, 2018
1 parent a0b7a76 commit 315964a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
15 changes: 12 additions & 3 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,9 @@ last(a) = a[end]
"""
strides(A)
Return a tuple of the memory strides in each dimension.
Return a tuple of the memory strides in each dimension, for an `AbstractArray` with a
strided memory layout. For arrays with a non-strided layout (such as sparse arrays), return
`nothing`.
# Examples
```jldoctest
Expand All @@ -321,7 +323,7 @@ julia> strides(A)
(1, 3, 12)
```
"""
function strides end
strides(::AbstractArray) = nothing

"""
stride(A, k::Integer)
Expand All @@ -339,7 +341,14 @@ julia> stride(A,3)
12
```
"""
stride(A::AbstractArray, k::Integer) = strides(A)[k]
function stride(A::AbstractArray, k::Integer)
str = strides(A)
if str === nothing
return nothing
else
return str[k]
end
end

@inline size_to_strides(s, d, sz...) = (s, size_to_strides(s * d, sz...)...)
size_to_strides(s, d) = (s,)
Expand Down
6 changes: 5 additions & 1 deletion base/permuteddimsarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ Base.pointer(A::PermutedDimsArray, i::Integer) = throw(ArgumentError("pointer(A,

function Base.strides(A::PermutedDimsArray{T,N,perm}) where {T,N,perm}
s = strides(parent(A))
ntuple(d->s[perm[d]], Val(N))
if s === nothing
return nothing
else
return ntuple(d->s[perm[d]], Val(N))
end
end

@inline function Base.getindex(A::PermutedDimsArray{T,N,perm,iperm}, I::Vararg{Int,N}) where {T,N,perm,iperm}
Expand Down
19 changes: 19 additions & 0 deletions stdlib/LinearAlgebra/src/adjtrans.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,25 @@ vec(v::TransposeAbsVec) = parent(v)
cmp(A::AdjOrTransAbsVec, B::AdjOrTransAbsVec) = cmp(parent(A), parent(B))
isless(A::AdjOrTransAbsVec, B::AdjOrTransAbsVec) = isless(parent(A), parent(B))

# provide strides, but only for eltypes that are directly stored in memory (i.e. unaffected
# by recursive `adjoint` and `transpose`, being `Real` and `Number` respectively)
function Base.strides(a::Union{Adjoint{<:Real, <:AbstractVector}, Transpose{<:Number, <:AbstractVector}})
str = strides(a.parent)
if str === nothing
return nothing
else
return (1, str[1])
end
end
function Base.strides(a::Union{Adjoint{<:Real, <:AbstractMatrix}, Transpose{<:Number, <:AbstractMatrix}})
str = strides(a.parent)
if str === nothing
return nothing
else
return (str[2], str[1])
end
end

### concatenation
# preserve Adjoint/Transpose wrapper around vectors
# to retain the associated semantics post-concatenation
Expand Down

0 comments on commit 315964a

Please sign in to comment.