Skip to content

Arrays readable on another device through copies Tack keeps (and spilling as a case of it) #469

Todo

Description

@berkgeveci

Plan: #511, docs/design/plans/device-memory.md (with #387).

An array whose home is one device should be readable on another through a copy Tack keeps and accounts for, as Viskores' ArrayHandle keeps a buffer per device. Today an array lives on one device. Reading it elsewhere means to_device, a new array that nothing tracks. #387's plan has the only second home, a spilled array's host copy, and keeps it private: it is never handed out, because restoring the array frees it.

The case that needs it

Advection of few particles on a GPU (#451). A GPU lane walks one particle's path serially, so 200 seeds take 1.09 s on Metal on AhmedML (21.5M cells) and 0.02 s on the CPU. The tail of a large run is the same problem: about half of 20,000 seeds' 3 s on Metal is launches moving a few dozen particles. Finishing on the CPU needs the mesh, positions, velocity and locator readable there while their home stays the GPU. Today that means data.to_device("cpu"): a second dataset and a second locator, 0.27 s of copies and well over 1 GB on AhmedML, held by nobody's accounting (numbers in #451).

The model

  • Copies stamped with a revision. An Array keeps its home buffer plus optional copies on other memory domains, each stamped with the revision it copied (DeviceBuffer.version, arrays._revision, which Tack already counts on every write). A copy is valid while its stamp matches, and a write makes the others stale with no work at write time.
  • Public read access. For example a.on("cpu"): a read-only array over the cached copy, made on first use. It holds the copy's buffer, so a restore or a later write never frees memory under it. It just stops being current, like a snapshot. Read-only arrays and their checks already exist (array_from_ptr's).
  • Launches. Dispatch chooses the device as it does today. A kernel run on the CPU takes each array's CPU copy, made or reused, so a dataset or locator needs no CPU twin, only its arrays' copies.
  • Spilling (Device memory: accounting, and spilling to host memory under a budget or when an allocation fails #387) is a case of it. Under the budget, drop the device copy of an array whose host copy is current, making one first if needed. Restoring copies back and keeps the host copy as a valid read copy instead of freeing it. The accounting counts copies as memory the budget can reclaim.
  • Unified memory. On Metal, and on integrated CUDA and Level Zero devices, the CPU copy is the same memory: a zero-copy alias that only waits for queued launches. That is what Jeff's Device memory: accounting, and spilling to host memory under a budget or when an allocation fails #387 review asks for, turning spilling off there, but here it falls out of the model rather than being special-cased.

To work out

  • The buffers: MetalBuffer and the GPU buffers need copies, and the CPU backend must accept a copy as an argument.
  • Dispatch cost: the revision checks sit in the fast path, one integer compare per array alongside _check_call's.
  • Debug mode: it should catch a stale read copy being read as current.
  • Unseen writes: writes through memory Tack doesn't see (DLPack, wrap_ptr, export_memory) leave copies stale, as they already do locators.
  • Eviction policy: which copies the budget drops first, and whether a cached copy outlives a run.
  • The API: the name and shape of the read access, and whether to_device reuses a current copy.

This changes #387's model. The plan update (a plan PR under docs/design/plans/) should cover both. #451's CPU lever is built on it.

Activity

  1. added this to the 0.9 milestone on Nov 16, 2026
  2. added
    enhancement
    on Nov 16, 2026
  3. self-assigned this
    on Dec 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions