From 721ecc4da2f9e20354a169acb85d77fed83d9417 Mon Sep 17 00:00:00 2001 From: lkdvos Date: Tue, 13 Oct 2026 11:33:46 -0400 Subject: [PATCH 1/3] Account for the memory offset of `Array` parents Co-Authored-By: Claude Opus 5.5 --- src/auxiliary.jl | 5 ++++- src/stridedview.jl | 3 ++- test/jet/Project.toml | 3 +++ test/runtests.jl | 13 +++++++++++++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/auxiliary.jl b/src/auxiliary.jl index 0b19941..0c5e93b 100644 --- a/src/auxiliary.jl +++ b/src/auxiliary.jl @@ -46,10 +46,13 @@ function _normalizestrides(size::Dims{N}, strides::Dims{N}) where {N} end # 'Normalize' the layout of a DenseArray, in order to reduce the number of required -# specializations in functions. +# specializations in functions. The offset is adjusted for the start of `A` in the +# normalized parent. @inline _normalizeparent(A) = A +@inline _normalizeoffset(A, offset::Int) = offset @static if isdefined(Core, :Memory) @inline _normalizeparent(A::Array) = A.ref.mem + @inline _normalizeoffset(A::Array, offset::Int) = offset + Base.memoryrefoffset(A.ref) - 1 else @inline _normalizeparent(A::Array) = reshape(A, length(A)) end diff --git a/src/stridedview.jl b/src/stridedview.jl index a996aac..79e05ad 100644 --- a/src/stridedview.jl +++ b/src/stridedview.jl @@ -53,7 +53,8 @@ function StridedView{T}( ) where {T, N, F} parent′ = _normalizeparent(parent) strides′ = _normalizestrides(size, strides) - return StridedView{T, N, typeof(parent′), F}(parent′, size, strides′, offset, op) + offset′ = _normalizeoffset(parent, offset) + return StridedView{T, N, typeof(parent′), F}(parent′, size, strides′, offset′, op) end """ diff --git a/test/jet/Project.toml b/test/jet/Project.toml index 68090c6..7726680 100644 --- a/test/jet/Project.toml +++ b/test/jet/Project.toml @@ -4,5 +4,8 @@ name = "StridedViewsJETTest" JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b" StridedViews = "4db3bf67-4bd7-4b4e-b153-31dc3fb37143" +[sources] +StridedViews = {path = "/mnt/home/ldevos/Projects/StridedViews-offset/test/jet/../.."} + [compat] JET = "0.9, 0.10, 0.11" diff --git a/test/runtests.jl b/test/runtests.jl index ae42ec0..c00624a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -151,6 +151,19 @@ if !is_buildkite @test !isstrided(Diagonal([0.5, 1.0, 1.5])) end + @testset "arrays not starting at the beginning of their memory" begin + v = collect(1.0:6.0) + popfirst!(v) + @test StridedView(v) == v + @test pointer(StridedView(v)) == pointer(v) + M = reshape(deleteat!(collect(1.0:8.0), 1:2), 2, 3) + @test StridedView(M) == M + @test StridedView(M)[:, 2:3] == M[:, 2:3] + @test sview(M, 2, :) == M[2, :] + StridedView(M)[2, 1] = 0 + @test M[2, 1] == 0 + end + @testset "transpose and adjoint with vector StridedView" begin @testset for T in (Float32, Float64, Complex{Float32}, Complex{Float64}) A = randn(T, (60,)) From 4a6be27c30d40f5a2bb33dbbb2de240bba8bf8da Mon Sep 17 00:00:00 2001 From: lkdvos Date: Tue, 13 Oct 2026 11:43:30 -0400 Subject: [PATCH 2/3] Normalize parent and offset together Co-Authored-By: Claude Opus 5.5 --- ext/StridedViewsPtrArraysExt.jl | 2 +- src/auxiliary.jl | 11 +++++------ src/stridedview.jl | 3 +-- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/ext/StridedViewsPtrArraysExt.jl b/ext/StridedViewsPtrArraysExt.jl index 65f58f8..de48a78 100644 --- a/ext/StridedViewsPtrArraysExt.jl +++ b/ext/StridedViewsPtrArraysExt.jl @@ -3,6 +3,6 @@ module StridedViewsPtrArraysExt using StridedViews using PtrArrays -StridedViews._normalizeparent(A::PtrArray) = PtrArray(A.ptr, length(A)) +StridedViews._normalizeparent(A::PtrArray, offset::Int) = PtrArray(A.ptr, length(A)), offset end diff --git a/src/auxiliary.jl b/src/auxiliary.jl index 0c5e93b..c543189 100644 --- a/src/auxiliary.jl +++ b/src/auxiliary.jl @@ -46,15 +46,14 @@ function _normalizestrides(size::Dims{N}, strides::Dims{N}) where {N} end # 'Normalize' the layout of a DenseArray, in order to reduce the number of required -# specializations in functions. The offset is adjusted for the start of `A` in the +# specializations in functions. The offset is adjusted to the start of `A` in the # normalized parent. -@inline _normalizeparent(A) = A -@inline _normalizeoffset(A, offset::Int) = offset +@inline _normalizeparent(A, offset::Int) = A, offset @static if isdefined(Core, :Memory) - @inline _normalizeparent(A::Array) = A.ref.mem - @inline _normalizeoffset(A::Array, offset::Int) = offset + Base.memoryrefoffset(A.ref) - 1 + @inline _normalizeparent(A::Array, offset::Int) = + A.ref.mem, offset + Base.memoryrefoffset(A.ref) - 1 else - @inline _normalizeparent(A::Array) = reshape(A, length(A)) + @inline _normalizeparent(A::Array, offset::Int) = reshape(A, length(A)), offset end # Auxiliary methods for `sview` diff --git a/src/stridedview.jl b/src/stridedview.jl index 79e05ad..3af05e0 100644 --- a/src/stridedview.jl +++ b/src/stridedview.jl @@ -51,9 +51,8 @@ function StridedView{T}( offset::Int = 0, op::F = identity ) where {T, N, F} - parent′ = _normalizeparent(parent) + parent′, offset′ = _normalizeparent(parent, offset) strides′ = _normalizestrides(size, strides) - offset′ = _normalizeoffset(parent, offset) return StridedView{T, N, typeof(parent′), F}(parent′, size, strides′, offset′, op) end From ec2c847cc204af6bd841a3fefd7889f9355ed6ca Mon Sep 17 00:00:00 2001 From: lkdvos Date: Tue, 13 Oct 2026 11:52:53 -0400 Subject: [PATCH 3/3] Remove local test sources Co-Authored-By: Claude Opus 5.5 --- test/jet/Project.toml | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/jet/Project.toml b/test/jet/Project.toml index 7726680..68090c6 100644 --- a/test/jet/Project.toml +++ b/test/jet/Project.toml @@ -4,8 +4,5 @@ name = "StridedViewsJETTest" JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b" StridedViews = "4db3bf67-4bd7-4b4e-b153-31dc3fb37143" -[sources] -StridedViews = {path = "/mnt/home/ldevos/Projects/StridedViews-offset/test/jet/../.."} - [compat] JET = "0.9, 0.10, 0.11"