Skip to content

Implements Region Masks and SubGrid for GPU scheduling - #82

Merged
massimim merged 29 commits into
Autodesk:carbonfrom
massimim:carbon-bitmask
Nov 23, 2026
Merged

massimim merged 29 commits into
Autodesk:carbonfrom
massimim:carbon-bitmask

Conversation

@massimim

Copy link
Copy Markdown
Collaborator

This pull request introduces advanced GPU scheduling features through RegionMask and SubGrid functionality, significantly enhancing the flexibility and performance of kernel launches. It also delivers substantial improvements to the SYCL backend and expands the continuous integration validation.

Region Masks

  • Adds RegionMask, a 256-bit user-defined label set, to refine sub-field access footprints.
  • Enables the Skeleton to parallelize kernels writing the same field if their masks are disjoint, allowing for more concurrent execution.
  • Introduces API extensions like KernelLoader::region() and per-access masked loading (e.g., read(f, M)).
  • Updates DOT exporters to visualize region masks and includes benchmarks demonstrating performance gains and host overheads.
  • Adds an MPI-aware consistency check to prevent deadlocks from rank-dependent mask definitions.

SubGrid Functionality

  • Introduces SubGrid, a mechanism for compact iteration over a selected subset of a parent grid's cells (Dense or Block).
  • Provides specialized SubGrid fields with compact storage and dedicated kernel types (make_kernel, make_host_kernel, reduction kernels).
  • Integrates seamlessly with the Skeleton, allowing OCC splitting and proper handling of parent field stencil reads.
  • Includes new examples and benchmarks showcasing its efficiency for sparse operations.

SYCL Backend Enhancements

  • Improves reliability of cross-device copies by introducing explicit event ordering.
  • Enables ReduceHostCopy::Deferred and implements ReduceFinalize::BlockAtomic for SYCL reductions, bringing feature parity with CUDA/HIP.
  • Fixes SYCL build issues related to .cu source file handling and includes a CMake helper for mirrored source compilation.

CI and Testing

  • Expands the gpu-ci.yml workflow to include sycl and sycl-mpi configurations.
  • Adds a 20x repeat lane for GPU tests to catch intermittent ordering bugs more reliably.
  • Introduces --docker-env and --ctest-args to docker-build-all.sh for more flexible CI execution.
  • Refreshes AGENTS.md and adds PROJECT_REVIEW.md for updated documentation and project status.

Other Improvements and Fixes

  • Corrects WAR hazards in various Halo implementations (Halo, HaloDisg, HaloBlock, HaloDistributed) by introducing trailing drain fences for intra-node peer copies.
  • Resolves a positional AccessToken initialization bug in libcarbonpy.
  • Fixes a test_block_dist_stencil_parity issue where static constexpr std::array was misread in SYCL device code.
  • Addresses a race condition in test_skeleton_scalar_pattern by ensuring proper stream synchronization.
  • Ensures copied Block objects rebuild their memoized host spans correctly, preventing use-after-free scenarios.

massimim and others added 29 commits November 18, 2026 16:22
…dded

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
AccessToken gains a trailing RegionMask (default all-ones); two accesses
conflict only when their masks also intersect. KernelLoader::region(M) and
per-call masked read/write/read_write overloads declare footprints; stencil
reads stay whole-field. Options::ignore_region_masks / CARBON_IGNORE_REGION_MASKS
restore the whole-field schedule, and under MPI the first run() of a collective
pipeline checks that every rank built the same pipeline shape.

Co-authored-by: Cursor <cursoragent@cursor.com>
Kernel-list DOT access cells and export_dot tooltips annotate non-all masks;
option summaries report when masks are ignored. Unmasked output is unchanged.

Co-authored-by: Cursor <cursoragent@cursor.com>
examples/region_mask_bc.cu runs an interior stencil and a Dirichlet boundary
kernel side by side on disjoint label masks and checks bit-identity against
the whole-field schedule. benchmarks/skeleton_region_mask measures interior +
K disjoint boundary kernels across masks / ignored / max_streams / CUDA graph.

Co-authored-by: Cursor <cursoragent@cursor.com>
docs/skeleton/README.md gains the RegionMask contract, API, exporter notes and
measured costs; AGENTS.md lists the feature in the Skeleton entry.

Co-authored-by: Cursor <cursoragent@cursor.com>
Masked numerics and launch-order parity across ranks, rank-local export, and
a rank-dependent mask surfacing as std::logic_error instead of a deadlock.

Co-authored-by: Cursor <cursoragent@cursor.com>
Each peer copy runs on the destination GPU's stream but reads the source
GPU's owned cells; only a leading fence ordered it. With one halo per
iteration the source GPU could overwrite those cells mid-copy. Add a
trailing per-GPU fence (drain_event_) after intra_. Exposed by
test_skeleton_region_mask under GPU contention (25/50 -> 0/50).

Co-authored-by: Cursor <cursoragent@cursor.com>
…path

12 tests and the wind tunnel examples no longer compiled under SYCL because
the .cpp twin in the build tree could not see headers next to the .cu.
Also fix wind_tunnel_common.h LbmParameters drift and only register
render_wind_tunnel_live with the viewer (both broke the CUDA build).

Co-authored-by: Cursor <cursoragent@cursor.com>
AdaptiveCpp's in-order queue did not order the submission after a
device-to-device memcpy between two GPUs, so the reduce combine read the
root's staging slot before the peer partial landed. Pin both edges of
the copy with custom-operation events. Adds test_reduce_partials_multi_gpu,
which checks every per-GPU partial and the combined total.

Co-authored-by: Cursor <cursoragent@cursor.com>
The deferred readback is backend-neutral; give ReductionResult a SYCL
slot-done record/wait built on the same custom-operation events as
EventGroup and lift the two constructor guards.

Co-authored-by: Cursor <cursoragent@cursor.com>
Work-group reduce_over_group with sycl::plus plus one atomic_ref add per
group into the per-GPU scalar, dispatched through ReduceLaunchTraits for
Dense and DenseDistributed; same admissible types/ops as CUDA.

Co-authored-by: Cursor <cursoragent@cursor.com>
Same WAR window as Halo before 433d3bb: the intra-process copies run on
the destination GPU's stream and read the source GPU's owned cells, with
only a leading fence. Add the trailing per-GPU drain fence.

Co-authored-by: Cursor <cursoragent@cursor.com>
The second Transfer into f ran on a user stream right after sk.run(),
racing the deferred reduction (lag-1 192 vs 128; 23/50 CUDA, 29/50 SYCL
under GPU load). Wait for the skeleton first. Found by the SYCL stress
sweep (93 gpu tests, 100x idle + 50x loaded, no other failure).

Co-authored-by: Cursor <cursoragent@cursor.com>
…test passthrough

The probe kernel read a host static constexpr offset array from device
code; under AdaptiveCpp that is a zero-filled copy, so every neighbour
probe returned the cell itself. Use literal offsets. sycl-mpi is now
135/135 (excluding the pre-existing format_check failure).

docker-build-all.sh: --docker-env KEY=VAL, --ctest-args, --ipc=host for
NVIDIA *-mpi configs, and usage prints the full header.

Co-authored-by: Cursor <cursoragent@cursor.com>
…NTS.md

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
SubGrid<Dense> packs the parent cells selected by a uint8 label + RegionMask
(or a general predicate loader) into per-GPU lane tables, ordered parent
Internal cells first and parent Boundary cells second so every DataView is
one contiguous lane range. S::Span / S::Idx derive from the parent's so S
kernels can hand them to parent-field partitions; S::Partition is
same-cell only (no halo). Kernels on SubGrid come in the next milestone.

Co-authored-by: Cursor <cursoragent@cursor.com>
…e 2)

- SubGrid::make_kernel / make_host_kernel: one thread per lane of the
  requested DataView (internal lanes first, so Internal / Boundary are
  contiguous ranges). The loader's kernel-wide region is preset to the
  SubGrid mask and the loader is bound to the SubGrid.
- KernelLoader rejects (std::invalid_argument, at kernel construction) a
  SubGrid field loaded by another SubGrid's kernel or by a parent kernel.
- read_stencil static_asserts that the field has a halo; SubGrid fields
  do not, so stencil-reading one is a compile error.
- CUDA/HIP and SYCL lane launchers.
- Tests: test_sub_grid_kernel (map, views, scatter to a parent field,
  parent stencil through the halo, host kernel, tokens, ownership, empty
  SubGrid) and sub_grid_compile_fail_{0..4} (CUDA-only build-and-expect-
  diagnostic entries).

Co-authored-by: Cursor <cursoragent@cursor.com>
…tone 3)

- Kernel::compute / Kernel::host_compute accept a SubGrid and forward to
  SubGrid::make_kernel / make_host_kernel; reduce, compute_universal,
  reduce_universal and host_reduce static_assert on a SubGrid. The
  is_sub_grid_v trait moves to detail/sub_grid/fwd.h so kernel.h can use it.
- test_sub_grid_skeleton: a pipeline mixing Dense and SubGrid kernels
  (S stencil-reads a parent field after its halo, S scatters into a parent
  field that a Dense kernel then stencil-reads) matches a hand-sequenced
  single-stream run bit for bit under every OverlapMode, a CUDA graph and
  ignore_region_masks. Standard OCC splits the S stencil kernel; Extended
  OCC also splits the S producer. Two SubGrids with disjoint masks writing
  one parent field get no edge and separate streams, and are ordered when
  masks are ignored.
- sub_grid_compile_fail_5: Kernel::compute_universal over a SubGrid.

Co-authored-by: Cursor <cursoragent@cursor.com>
- examples/sub_grid_cube.cu: the cube boundary as a SubGrid (predicate
  constructor), SubGrid and Dense kernels in one masked Skeleton (2
  streams, compact SubGrid field checked on the host), and the boundary
  fill timed against the filtered Dense kernel. 2x RTX A4000, us per
  launch: 128^3 9.6 vs 3.6, 256^3 58.8 vs 28.7 (SYCL 85.5 vs 32.0),
  384^3 189 vs 67.
- docs/skeleton/README.md: "SubGrid" section (construction, rules, costs).
- docs/CHANGELOG.md: design decisions.
- AGENTS.md: SubGrid in the architecture summary; test counts (CUDA 121,
  SYCL 111 measured; MPI rows derived).

Co-authored-by: Cursor <cursoragent@cursor.com>
- Kernel::reduce / host_reduce / reduce_universal accept a SubGrid, plus
  S.make_reduce_kernel / make_host_reduce_kernel /
  make_universal_reduce_kernel. ReduceLaunchTraits / HostLaunchTraits
  specialisations (detail/sub_grid/reduce_traits.h) run stage 1 as one
  thread per lane over the view's lane range (CUDA-like and SYCL
  launchers, OpenMP host loop); the rest of the reduce pipeline
  (multi-GPU combine, cross-rank, BlockAtomic, OCC Partial/Merge) is
  reused unchanged. BoundLoading applies the SubGrid loader rules
  (region preset, ownership). ReduceOptions::stage1_threads[_y] throw.
- tests/test_sub_grid_reduce.cu: Sum/Max/count, view split, BlockAtomic,
  host and universal, tokens, throws, empty SubGrid, Skeleton OCC under
  every OverlapMode.
- sub_grid_compile_fail_* also register under HIP; regexes accept clang
  wording.
- Docs: README SubGrid "Reductions", CHANGELOG, AGENTS counts.

2x RTX A4000, excluding carbon_format_check: CUDA 121/121, SYCL 111/111,
hip-nvidia 117/117.

Co-authored-by: Cursor <cursoragent@cursor.com>
Block::host_spans() memoises spans that point into the object's own host
bitmask / connectivity / origin vectors. The defaulted copy copied that
memo, so a copy taken after the memo was filled kept pointers into the
source and read freed memory once the source was destroyed (every Block
kernel holds a Block copy). The memo now lives in ResetOnCopy: copies
start empty and rebuild from their own tables; moves keep it.

test_block_host_parity checks that original, copy-constructed and
copy-assigned Blocks point into their own tables, and runs a host
reduction through a copy whose source is gone. Making the copy keep the
memo fails the check.

Co-authored-by: Cursor <cursoragent@cursor.com>
SubGrid<Block<SBlock>> uses the same flat lane table as SubGrid<Dense>:
one thread per selected voxel, lanes Internal bricks first, then Boundary
bricks, voxels z-y-x within a brick (their storage order). Launchers,
reductions, OCC and Kernel dispatch are unchanged; Block partitions only
use Idx and Span, so parent reads, writes and neighbour reads work from a
lane thread.

- detail/sub_grid/parent_traits.h: ParentTraits<G> for Dense and Block
  (host span, host enumeration of a view, flag offset). The Builder uses
  it, and SubGrid static_asserts a supported parent.
- SubGrid host spans now come from ParentTraits::host_span over the
  State's own parent copy. For Block that is a span over the host
  bitmask / connectivity / origin tables; the device span used before
  would make host kernels dereference device pointers.
- Block::new_sub_grid(labels, mask) and new_sub_grid(select, mask): a
  Block selection kernel, download_to_host, Builder.
- SubGrid partition debug check compares block_idx/intra for Block.
- tests/test_sub_grid_block.cu: 655 scattered voxels in a 64^3 ball
  (brick faces, inactive neighbours, slab cut); construction, device and
  host kernels, reductions, throws, Skeleton stencil reads under every
  OverlapMode. Building host spans from device spans segfaults the test;
  a shifted flag offset or a wrong voxel order fails it.
- sub_grid_compile_fail 6 (read_stencil on a SubGrid<Block> field) and 7
  (unsupported parent); case 0 also builds a SubGrid<Block> kernel.

CUDA, 2x RTX A4000: 124/124 excluding carbon_format_check; the new test
20/20 on 2 GPUs and on 1 GPU.

Co-authored-by: Cursor <cursoragent@cursor.com>
… SubGrid over Block

The example updates scattered marker voxels of a Block ball through a SubGrid<Block> and
checks the result against a Block kernel that filters on a label (256^3, 1 in 1000:
5.8 us vs 79.1 us per launch on 2x RTX A4000). Test counts: CUDA 125, SYCL 113,
hip-nvidia 121.

Co-authored-by: Cursor <cursoragent@cursor.com>
100 int64 iterations; voxels missing a neighbour get their own rule. Runs as one filtered
kernel, as an interior kernel plus a SubGrid kernel, and the latter under every OCC mode,
all compared exactly with a host reference. CUDA 126, SYCL 114, hip-nvidia 122 tests.

Co-authored-by: Cursor <cursoragent@cursor.com>
B (voxels missing a neighbour) is a SubGrid; C is either a masked parent kernel or a second
SubGrid. Assert different streams, no B-C edge, and exactly 2 internal + 2 boundary launches
per Jacobi step under OCC. Values still match the host reference after 100 int64 iterations.

Co-authored-by: Cursor <cursoragent@cursor.com>
Architecture and product notes after SubGrid + region-mask B/C work: strengths,
uneven areas, and a short priority list. Complements current_status.md and
carbon-review.md.

Co-authored-by: Cursor <cursoragent@cursor.com>
@massimim massimim self-assigned this Nov 23, 2026
@autodesk-chorus

Copy link
Copy Markdown

Chorus detected one or more security issues with this pull request. See the Checks tab for more details.

As a reminder, please follow the secure code review process as part of the Secure Coding Trust Commitment requirement.

@massimim
massimim merged commit 77cb5d4 into Autodesk:carbon Nov 23, 2026
13 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 23, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant