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 authored and andyferris committed Jan 8, 2022
1 parent ad129a9 commit 532aebd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
16 changes: 11 additions & 5 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,9 @@ 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`.
See also: [`stride`](@ref).
Expand All @@ -523,7 +525,7 @@ julia> strides(A)
(1, 3, 12)
```
"""
function strides end
strides(::AbstractArray) = nothing

"""
stride(A, k::Integer)
Expand All @@ -544,9 +546,13 @@ julia> stride(A,3)
```
"""
function stride(A::AbstractArray, k::Integer)
st = strides(A)
k ndims(A) && return st[k]
return sum(st .* size(A))
str = strides(A)
if str === nothing
return nothing
else
k ndims(A) && return st[k]
return sum(st .* size(A))
end
end

@inline size_to_strides(s, d, sz...) = (s, size_to_strides(s * d, sz...)...)
Expand Down
6 changes: 5 additions & 1 deletion base/permuteddimsarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,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
Base.elsize(::Type{<:PermutedDimsArray{<:Any, <:Any, <:Any, <:Any, P}}) where {P} = Base.elsize(P)

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 @@ -218,6 +218,25 @@ parent(A::AdjOrTrans) = A.parent
vec(v::TransposeAbsVec{<:Number}) = parent(v)
vec(v::AdjointAbsVec{<:Real}) = parent(v)

# 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 532aebd

Please sign in to comment.