Skip to content

Stencil: fuse in-place updates into one kernel - #18

Open
AdvaitTahilyani wants to merge 13 commits into
scale/02-memoryfrom
scale/03-stencil-fusion
Open

AdvaitTahilyani wants to merge 13 commits into
scale/02-memoryfrom
scale/03-stencil-fusion

Conversation

@AdvaitTahilyani

Copy link
Copy Markdown

Stacked on #17. Needs UIUC-PPL/charmtyles#9.

  • An in-place set_region fuses with the ops that read its target (the backend already computes into a scratch buffer); read-after-write is still rejected.
  • Typed strided copy-back; scratch result buffers return to the pool immediately; aliased copies (a[1:] = a[:-1]) are staged through a temporary.
  • Coalesced GPU gemv for tall tiles.

stencil 8192²: 13.2 s → 0.2 s (1 A100), 13.0 s → 0.1 s (4 A100s); jacobi 1.97 → 0.95 s.

Tests: in-place halo and aliasing cases, also at max_fused_ops=2.

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 7 commits November 2, 2026 17:27
The copy of a set_region source (or result buffer) into its target computed
div/mod indices and copied byte by byte per element. Both offsets are affine in
the overlap's lattice coordinates, so plan the copy on the host (clipping cells
whose source lies off this chare per dim) and copy whole elements: on an A100
the 8192x8192 float64 copy-back in stencil goes from 2.5 ms to 0.79 ms
(~1.36 TB/s); the CPU path copies contiguous rows with memmove.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A set_region result is scratch (the frontend never reads it), but it stayed
allocated until the client's delete arrived with a later DAG. A loop of in-place
updates therefore held one result buffer per iteration of the DAG, and retiring
them in a batch overflowed the free list: stencil at 8192^2 paid 5-7 cudaFree
plus cudaMalloc of 512 MB buffers per flush (10-20 ms stalls), so repeated runs
slowed from 0.31 s to 0.86 s. Each node now pools its buffer, and the next
iteration reuses it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The chunked gemv gave each thread a 256-column chunk of a row, so neighbouring
threads read addresses 2 KB apart. For tiles with at least 2048 rows, lanes now
read consecutive columns of a row (TeamPolicy, double accumulation as before):
a 4096x4096 float64 tile takes 130 us instead of 240 us; jacobi -n 8192 on one
A100 goes from 1.97 s to 0.95 s.

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

A copy-back whose source is the target array itself (e.g. a[1:] = a[:-1] on the
interpreter path) copied overlapping elements in no defined order: the GPU
kernel raced and the CPU row memmove was wrong for shifts across rows. When the
source is the target and the element spans intersect, copy into a packed
temporary first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A pooled buffer may still be read by kernels queued on the compute stream (set
region result buffers are pooled right after their copy-back). Make the default
instance wait for the compute stream before deep_copy zero-fills it instead of
relying on deep_copy's implicit fence.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
u[1:-1] = u[:-2] + u[2:] (1-D, 2-D, partial edge tiles), a[1:] = a[:-1],
a[:, 1:] = a[:, :-1], a += a[::-1], and B[...] = f(A); A[...] = g(A) followed
by reads of A and B, each at the default fusion limit and at max_fused_ops=2.

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

# Conflicts:
#	src/backend.hpp

@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.

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

These are correct:

  • Copy plan: it matches the old per-element loop exactly.
  • Aliased copies: they are staged through a temporary.
  • gemv kernel: the indexing is right.
  • Hazard rules (with charmtyles#9):
    • The copy-back runs after the kernel's reads and after the node's halo packs.
    • A read-after-write on an in-place target is rejected on any overlap.
    • Stepped regions are safe.

Nits (both still at #44):

  • Alias staging bypasses the pool. Each aliased copy (executor_ast_visitor.cpp:66-75) allocates a fresh Kokkos::View, blocks on a fence and frees with cudaFree, skipping both the pool and the OOM retry from #17. A large a[1:] = a[:-1] on a multi-GB tile can run out of memory where comm_alloc would have evicted caches first.
  • Retired set_region results. finish_set_regions (execute_node.cpp:162-172) retires the set_region result immediately, so the object set_region() returns names a pooled buffer, and a .get() on it would find nothing. An assert in the frontend would be cheap.

AdvaitTahilyani and others added 2 commits November 9, 2026 22:22
… are write-only

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

Copy link
Copy Markdown
Author
  1. Alias staging bypasses the pool: fixed in 387bac3 (the staging buffer is a comm_alloc block freed with comm_free after the fence, so an OOM evicts the caches first and the block is reused).
  2. Retired set_region results: fixed in 387bac3 (the frontend marks the returned object write-only and reading it raises RuntimeError; test in test_frontend_fixes.py, 5bf5e6a).

Validated: nc build; CPU regress at +p1 and +p2: orig 15/15 and ports 25/25 each, ops 25/26 at both because of my own new test; after fixing the test (the target-value check I first added is invalid: a direct low-level set_region of a full region leaves the target wrong, which I reproduced on #17 without this change, not investigated; I kept only the guard check) test_frontend_fixes passes at +p1 and +p2 (rerun of that test alone). The alias staging change is device-only (the CPU path is unchanged): executor_ast_visitor.cpp compiles with the GPU flags, not run on a GPU.

AdvaitTahilyani and others added 4 commits November 10, 2026 16:00
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <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.

2 participants