Skip to content

Fix device-array aliasing blind spot in need_sync - #121

Merged
termi-official merged 1 commit into
mainfrom
do/gpu-sync
Sep 9, 2026
Merged

termi-official merged 1 commit into
mainfrom
do/gpu-sync

Conversation

@termi-official

Copy link
Copy Markdown
Collaborator

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 DenseVectors by base pointer and length instead of hard-coding true. Both a plain Vector and a device array package's derived array are DenseVectors, 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.

Claude-Session: https://claude.ai/code/session_01RWfoaMxJzyy72Q7MzHEfYX

Checklist

  • Appropriate tests were added
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the
    contributor guidelines, in particular the SciML Style Guide and
    COLPRAC.
  • Any new documentation only uses public API

Additional context

Add any other context about the problem here.

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
@termi-official
termi-official merged commit 3d75546 into main Sep 9, 2026
11 checks passed
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant