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