Skip to content

Commit

Permalink
normalize strides; avoids (mul!) issues with with size zero arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Jutho committed Oct 2, 2020
1 parent 41b7952 commit 5ab2938
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/abstractstridedview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,19 @@ function _simplify(size::Dims{N}, strides::Dims{N}) where {N}
end
end

_normalizestrides(size::Tuple{}, strides::Tuple{}) = strides
function _normalizestrides(size::Dims{N}, strides::Dims{N}) where {N}
if size[1] <= 1 # 0 or 1
strides = Base.setindex(strides, 1, 1)
end
for i = 2:N
if size[i] <= 1 # 0 or 1
strides = Base.setindex(strides, strides[i-1]*max(1, size[i-1]), i)
end
end
return strides
end

_computereshapestrides(newsize::Tuple{}, oldsize::Tuple{}, strides::Tuple{}) = strides
function _computereshapestrides(newsize::Tuple{}, oldsize::Dims{N}, strides::Dims{N}) where {N}
all(isequal(1), oldsize) || throw(DimensionMismatch())
Expand Down
3 changes: 2 additions & 1 deletion src/stridedview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ function StridedView(parent::Array{S},

T = Base.promote_op(op, S)
# reshape array to vector in order to reduce number of element types
StridedView{T,N,Vector{S},F}(reshape(parent, length(parent)), size, strides, offset, op)
StridedView{T,N,Vector{S},F}(reshape(parent, length(parent)), size,
_normalizestrides(size, strides), offset, op)
end
function StridedView(parent::A,
size::NTuple{N,Int} = size(parent),
Expand Down
2 changes: 1 addition & 1 deletion src/unsafestridedview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function UnsafeStridedView(a::Ptr{PT}, size::NTuple{N,Int}, strides::NTuple{N,In

@assert isbitstype(PT)
T = Base.promote_op(op, PT)
UnsafeStridedView{T,N,PT,F}(a, size, strides, offset, op)
UnsafeStridedView{T,N,PT,F}(a, size, _normalizestrides(size, strides), offset, op)
end

UnsafeStridedView(a::Ptr{T}, size::NTuple{N,Int}, strides::NTuple{N,Int},
Expand Down

2 comments on commit 5ab2938

@Jutho
Copy link
Owner Author

@Jutho Jutho commented on 5ab2938 Oct 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/22356

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.1.0 -m "<description of version>" 5ab29382419c00ab87974225149bba79e756f05d
git push origin v1.1.0

Please sign in to comment.