Skip to content

Commit b27f572

Browse files
authored
Normalize parent array type (#15)
* Reduce required specializations * Fix `parent` test * Bump v0.3.3 * Unpack memory when possible * Bump v0.4.0 * Update Readme
1 parent 27b1c09 commit b27f572

File tree

5 files changed

+21
-5
lines changed

5 files changed

+21
-5
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "StridedViews"
22
uuid = "4db3bf67-4bd7-4b4e-b153-31dc3fb37143"
33
authors = ["Lukas Devos <lukas.devos@ugent.be>", "Jutho Haegeman <jutho.haegeman@ugent.be>"]
4-
version = "0.3.2"
4+
version = "0.4.0"
55

66
[deps]
77
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,10 @@ subsequent dimensions `i` and `i+1` can only be joined if `stride(A,i+1) ==
5050
size(A,i)*stride(A,i)`. Instead of overloading `reshape`, Strided.jl provides a separate
5151
function `sreshape` which returns a `StridedView` over the same parent data, or throws a
5252
runtime error if this is impossible.
53+
54+
### News
55+
56+
Since StridedViews v0.4.0, the `StridedView` type attempts to generate less specializations
57+
by normalizing the parent array type. In particular, for `DenseArray` parents we attempt to
58+
reshape the parent array to a vector, and for `Memory`-based arrays (Julia v1.11+) we unpack
59+
the `Memory` object directly. This should improve compile times.

src/auxiliary.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ function _normalizestrides(size::Dims{N}, strides::Dims{N}) where {N}
4545
return strides
4646
end
4747

48+
# 'Normalize' the layout of a DenseArray, in order to reduce the number of required
49+
# specializations in functions.
50+
@static if isdefined(Core, :Memory)
51+
@inline _normalizeparent(A::Array) = A.ref.mem
52+
end
53+
@inline _normalizeparent(A::DenseArray) = reshape(A, length(A))
54+
4855
# Auxiliary methods for `sview`
4956
#------------------------------
5057
# Compute the new dimensions of a strided view given the original size and the view slicing

src/stridedview.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ end
3434

3535
# Constructors
3636
#--------------
37-
function StridedView(parent::A,
37+
function StridedView(parent::DenseArray,
3838
size::NTuple{N,Int}=size(parent),
3939
strides::NTuple{N,Int}=strides(parent),
4040
offset::Int=0,
41-
op::F=identity) where {A<:DenseArray,N,F}
41+
op::F=identity) where {N,F}
4242
T = Base.promote_op(op, eltype(parent))
43-
return StridedView{T,N,A,F}(parent, size, _normalizestrides(size, strides), offset, op)
43+
parent′ = _normalizeparent(parent)
44+
strides′ = _normalizestrides(size, strides)
45+
return StridedView{T,N,typeof(parent′),F}(parent′, size, strides′, offset, op)
4446
end
4547

4648
StridedView(a::StridedView) = a

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Random.seed!(1234)
1313
@test isstrided(A1)
1414
@test isstrided(B1)
1515
@test C1 === B1
16-
@test parent(B1) === A1
16+
@test parent(B1) == reshape(A1, :)
1717
@test Base.elsize(B1) == Base.elsize(A1)
1818
for op1 in (identity, conj, transpose, adjoint)
1919
if op1 == transpose || op1 == adjoint

0 commit comments

Comments
 (0)