Skip to content

Reuse large arrays across repeats - #25

Open
AdvaitTahilyani wants to merge 12 commits into
scale/09-decompositionfrom
scale/10-alloc-reuse
Open

AdvaitTahilyani wants to merge 12 commits into
scale/09-decompositionfrom
scale/10-alloc-reuse

Conversation

@AdvaitTahilyani

Copy link
Copy Markdown

Stacked on #24.

  • Retired arrays are reused by dtype and size; caches may use up to half the GPU while a reserve stays free; least-recently-freed eviction.

black_scholes: 174 ms (50–320 spread) → 41 ms flat; scan: ~45 → 22 ms (1 A100).

Tested (full stack): CPU +p1/+p2 15/15 originals, 25/25 ports, 30/30 ops; A100 1 GPU 15/15, 25/25, 30/30 and 4 GPUs (one process per GPU) 15/15, 25/25.

🤖 Generated with Claude Code

AdvaitTahilyani and others added 6 commits November 3, 2026 11:09
Retired arrays were keyed by dtype, local and global shape and the
decomposition, so an array of the same size from another DAG with a
different tile or offset missed the free list and paid a cudaMalloc (and
the surplus a cudaFree). The reuse path already rewrites region, shape and
decomposition, so the key is now the dtype and the local element count.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
black_scholes -n 400000 and scan -s 1e9 allocate 6-8 GB of arrays per
repeat, more than the 1/8-of-the-device free-list budget (4.9 GB on an
A100-40GB). Every repeat freed the surplus (13-15 cudaFree, ~2 ms each, a
device sync) and allocated it again; the cudaMallocs got slower as the
process freed more large blocks (up to 70 ms per call, 200-280 ms per
repeat), so repeats ranged 50-320 ms (nsys, CNUM_MEM_STATS).

- Retired arrays and large message/scratch blocks may now use up to 1/2 of
  the device per sharing process (CNUM_FREE_LIST_MB, CNUM_DEVICE_CACHE_MB).
  Up to 1/8 is kept unconditionally; beyond that a block is kept only while
  CNUM_DEVICE_RESERVE_MB (default 1/4 of the device) stays free.
- Before a fresh array or large block is allocated, cached blocks and this
  PE's retired arrays are evicted, least recently freed first, until it fits
  with 1/32 of the device to spare. A failed allocation still releases
  everything and retries once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Scratch from the stream-ordered pool is allocated outside the array and
message caches; with those caches allowed to grow, ds_alloc first evicts
cached blocks for what the pool cannot serve from memory it holds, and a
failed cudaMallocAsync releases the caches and retries instead of aborting.
Releasing the caches on OOM also trims the pool.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
cudaMemGetInfo costs ~24 us on an A100. Caches up to 1/8 of the device
(per sharing process) are kept without asking, and a fresh allocation only
evicts while the caches hold more than that; below it a failed allocation
still releases everything and retries.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Allocations outside these caches (cuBLAS, the stream-ordered pool, Kokkos
scratch, JIT temporaries) cannot release them. A fresh array or large
block now evicts cached ones until it fits with CNUM_DEVICE_RESERVE_MB still
free, not just a 1/32 margin, but never below the always-kept 1/8. A test
that fills the caches with one size and then allocates 25-28 GB of others
peaked at 38.9 of 40.4 GB used before this change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@adityapb adityapb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: approve with nits.

This is a static review. Line numbers are at this PR's head.

These are correct:

  • Reuse: matching by (dtype, size) is sound because storage is a flat 1-D view, and reuse is ordered after the old readers.
  • Accounting: retired_bytes and large_cached are updated symmetrically.
  • Eviction: it is truly least-recently-freed and always terminates.
  1. [minor] Eviction and the OOM fallback are per PE, but the raised budgets are per process.
    • Where: backend.hpp:1237-1281. evict_oldest_cached scans only the thread_local partitions.
    • Scenario (SMP, 2 PEs on a 40 GB GPU): PE A retires about 15 GB, then PE B allocates 12 GB. B owns nothing evictable, so device_oom aborts while 15 GB of freeable cache sits in A's free lists.
    • Same root cause as my note on #17. Still at #44.
  2. Nits:
    • Unguarded allocations: allocations outside the three guarded allocators (Kokkos::View temporaries, cuBLAS/cuSOLVER workspaces, cuFFT plans) get no make-room or retry, and the caches may now grow until only 1/4 of the device is free.
    • Budget size: "up to half the GPU" is really 1/2 per cache, about 3/4 combined.
    • Eviction cost: there's a cudaMemGetInfo call plus a full scan for every evicted object.

…ess under a lock; fix budget doc

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AdvaitTahilyani

Copy link
Copy Markdown
Author
  1. Per-PE eviction vs per-process budget: fixed in d7ef18b. New process-wide partition registry and a free_lists_mutex guarding every free list; evict_oldest_cached and release_device_caches scan all PEs' partitions under it (lock order: free-list mutex, then comm pool). retire_array, try_reuse, destructor and mem stats take the same lock.
  2. Budget doc: fixed in d7ef18b (1/2 per cache, about 3/4 combined).
  3. Unguarded allocations (Kokkos views, cuBLAS/cuSOLVER workspaces, cuFFT plans): follow-up, needs a wrapper around each allocator, not a small fix.
  4. Eviction cost (cudaMemGetInfo + scan per object): follow-up, performance only.
    Validated: nc build; CPU regress +p1 and +p2 15/15, 25/25, 29/29 each. The USE_KOKKOS code (evict/release) is not compiled in nc and was not run on GPU or SMP.

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.

2 participants