Repository navigation
Fix device-array aliasing blind spot in need_sync - #121
Merged
Merged
Conversation
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 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RWfoaMxJzyy72Q7MzHEfYX
ChrisRackauckas
added a commit
that referenced
this pull request
Sep 10, 2026
- Add 32-bit Core CI lane via test_groups.toml (#119) - Require Symbolics 7.39.2 for 32-bit hasha_seed fix. (#120) - Fix device-array aliasing blind spot in need_sync (#121) Agent-Harness: Claude Code Agent-Model: claude-opus-5[1m] Claude-Session: https://claude.ai/code/session_014FEzNTLFutCmTEAZ3zBg5R Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_memorypredicate that comparesDenseVectors by base pointer and length instead of hard-codingtrue. Both a plainVectorand a device array package's derived array areDenseVectors, andpointeron 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 theT-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.
Claude-Session: https://claude.ai/code/session_01RWfoaMxJzyy72Q7MzHEfYX
Checklist
contributor guidelines, in particular the SciML Style Guide and
COLPRAC.
Additional context
Add any other context about the problem here.