From 465134d925f7e93cf6c4cd990afc0788fc50d9a6 Mon Sep 17 00:00:00 2001 From: termi-official Date: Wed, 9 Sep 2026 12:59:31 +0200 Subject: [PATCH] Fix device-array aliasing blind spot in need_sync GPUArrays-family packages return a contiguous UnitRange view of a device vector as a *derived* array -- a fresh wrapper struct over the same buffer, not a SubArray. That derived array fell through to the generic need_sync(::AbstractVector, ::AbstractVector) method, which always answered `true`, so every forward and backward sync paid a redundant device-to-device self-copy each step. The copy was harmless (source == destination memory), so this is pure overhead removal, not a correctness fix. Route the generic case through a `_same_memory` predicate that compares `DenseVector`s by base pointer and length instead of hard-coding `true`. Both a plain `Vector` and a device array package's derived array are `DenseVector`s, and `pointer` on either is a cheap host-side field read (no device sync), so the added cost is two pointer reads and a length comparison per call. Cross-family pairs (host vs. device) fail the `T`-mismatched fallback, so they still always sync. The existing SubArray-keyed methods are untouched -- they already compare by parent identity for both host views and SubArray-wrapped device children. Verified against the local GPUArraysCore/CUDA.jl sources: AbstractGPUArray <: DenseArray, so CuArray <: DenseVector, and CUDA's Base.pointer on a StridedCuArray is a struct field read, matching the cheapness requirement. Full test suite (Pkg.test()) is green before and after: 1945/1945 passing on the clean branch, 1955/1955 after (the +10 are new need_sync unit tests added below), with every other file's count unchanged. Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_01RWfoaMxJzyy72Q7MzHEfYX --- src/utils.jl | 20 ++++++++++++++++---- test/sync.jl | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/utils.jl b/src/utils.jl index dd9c165..c547863 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -34,13 +34,25 @@ Return whether copying solution information from `b` into `a` is necessary. - `b`: Source vector or view. # Returns -`false` when both arguments share the same backing storage and copying would be -redundant; `true` otherwise. Extend this function for custom array wrappers whose -aliasing relationship cannot be determined by the built-in vector methods. +`false` when both arguments provably share the same backing storage and copying +would be redundant; `true` otherwise (including whenever that cannot be decided +cheaply). `SubArray` arguments are compared by parent identity. Plain +`DenseVector`s -- which includes a GPU array package's own contiguous view, since +those are commonly returned as a *new* dense wrapper over the same buffer rather +than a `SubArray` -- are compared by base pointer and length: equal on both means +the same memory. Extend this function for custom array wrappers whose aliasing +relationship cannot be determined by the built-in methods. """ need_sync -need_sync(a::AbstractVector, b::AbstractVector) = true +# Same dense memory extent ⇒ the copy would be a self-copy. `pointer` on a +# `DenseVector` is a cheap host-side field read (no device sync, even for a GPU +# array), so this stays cheap enough to call on every sync. +_same_memory(a, b) = false +_same_memory(a::DenseVector{T}, b::DenseVector{T}) where {T} = + pointer(a) == pointer(b) && length(a) == length(b) + +need_sync(a::AbstractVector, b::AbstractVector) = !_same_memory(a, b) need_sync(a::SubArray, b::AbstractVector) = a.parent !== b need_sync(a::AbstractVector, b::SubArray) = a !== b.parent need_sync(a::SubArray, b::SubArray) = a.parent !== b.parent diff --git a/test/sync.jl b/test/sync.jl index 13244c2..2a8fd4a 100644 --- a/test/sync.jl +++ b/test/sync.jl @@ -21,6 +21,42 @@ end DiffEqBase.u_modified!(m::MockLeaf, b) = m.u_modified = b end +@testset "need_sync" begin + # A device array package's own contiguous view (e.g. GPUArrays' `derive`) comes + # back as a *new* dense wrapper over the same buffer, not a `SubArray`. Emulate + # that aliasing relationship on the host with `unsafe_wrap` over the same + # pointer; `a` must outlive the wrap, hence `GC.@preserve`. + @testset "aliasing dense vectors need no sync" begin + a = [1.0, 2.0, 3.0] + GC.@preserve a begin + b = unsafe_wrap(Array, pointer(a), length(a)) + @test !OS.need_sync(a, b) + @test !OS.need_sync(b, a) + end + end + + @testset "non-aliasing dense vectors need a sync" begin + a = [1.0, 2.0, 3.0] + b = [1.0, 2.0, 3.0] # equal content, distinct memory + c = [4.0, 5.0, 6.0] # distinct content and memory + @test OS.need_sync(a, b) + @test OS.need_sync(a, c) + end + + @testset "SubArray cases are unchanged" begin + parent = [1.0, 2.0, 3.0] + other = [1.0, 2.0, 3.0] + view_of_parent = view(parent, 1:2) + + @test !OS.need_sync(view_of_parent, parent) + @test OS.need_sync(view_of_parent, other) + @test !OS.need_sync(parent, view_of_parent) + @test OS.need_sync(other, view_of_parent) + @test !OS.need_sync(view(parent, 1:2), view(parent, 1:3)) # same parent + @test OS.need_sync(view(parent, 1:2), view(other, 1:2)) # different parent + end +end + @testset "Nested children address their parent's buffers" begin # dof indices are parent-relative at every level, so a grandchild's initial # state is parent-slice-of-parent-slice -- NOT the root vector indexed with