Repository navigation
Stencil: fuse in-place updates into one kernel - #18
Open
AdvaitTahilyani wants to merge 13 commits into
Open
AdvaitTahilyani wants to merge 13 commits into
AdvaitTahilyani wants to merge 13 commits into
Conversation
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
reviewed
Nov 6, 2026
adityapb
left a comment
Member
There was a problem hiding this comment.
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 freshKokkos::View, blocks on a fence and frees withcudaFree, skipping both the pool and the OOM retry from #17. A largea[1:] = a[:-1]on a multi-GB tile can run out of memory wherecomm_allocwould have evicted caches first. - Retired set_region results.
finish_set_regions(execute_node.cpp:162-172) retires the set_region result immediately, so the objectset_region()returns names a pooled buffer, and a.get()on it would find nothing. An assert in the frontend would be cheap.
… are write-only Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Author
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. |
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>
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.
Stacked on #17. Needs UIUC-PPL/charmtyles#9.
set_regionfuses with the ops that read its target (the backend already computes into a scratch buffer); read-after-write is still rejected.a[1:] = a[:-1]) are staged through a temporary.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