Skip to content

DisjointSlice cannot express a thread owning N contiguous elements #583

Closed

Description

@honeyspoon

Summary

DisjointSlice::get_mut takes a ThreadIndex and yields one element. A thread that owns N contiguous elements — the normal shape for a coarsened or grid-stride kernel — has no way to say so, so those kernels drop to get_unchecked_mut and lose the disjointness proof along with the bounds check.

The gap, concretely

A kernel where each thread handles 4 contiguous elements has to write this today:

#[kernel]
pub fn scale_by_four(mut data: DisjointSlice<f32>, n: usize) {
    let tid = thread::index_1d().get();
    let base = tid * 4;
    // No `ThreadIndex` describes "I own base..base+4", so the proof is gone and
    // every access is unchecked. The disjointness argument now lives in a
    // comment instead of the type system.
    for k in 0..4 {
        if base + k < n {
            unsafe { *data.get_unchecked_mut(base + k) *= 2.0; }
        }
    }
}

get_mut cannot help: it resolves one ThreadIndex to one element. The thread genuinely does own a disjoint run, and nothing can express that.

Grid-stride is the same problem with an extra trap — the stride must equal the launch geometry:

// If this stride disagrees with the actual launch, threads silently overlap.
let stride = grid_dim_x() * block_dim_x();
let mut i = tid;
while i < n {
    unsafe { *data.get_unchecked_mut(i) *= 2.0; }
    i += stride;
}

What a solution needs

  1. A proof that a thread owns a contiguous run, derived from an existing ThreadIndex rather than constructed by the user. Multiplying a disjoint index by a constant preserves disjointness, so the derivation is sound; the point is that the user must not be able to state a run that is not.
  2. A grid-stride period derived from the launch, not passed in. A passed stride is the bug above.
  3. Partial-tail handling. Real launches round up, so the last thread's run is clipped. Whatever the API returns must distinguish a full run from a clipped one without forcing the caller back to raw indexing — otherwise the tail undoes the whole thing.

Why my own attempt was the wrong shape

I proposed this in #571 (now closed) as ThreadIndex::scale::<N>() -> DisjointBlock<N>, DisjointBlock::repeat::<K>() -> DisjointTiling<N, K>, and DisjointSlice::get_block_mut::<N>() -> Option<&mut [T; N]>.

The mechanism worked, but it invented a parallel vocabulary instead of composing with what #477 already landed in view.rs:

existing (view.rs) what I added
LocalIndex32<N> — bounded index proof DisjointBlock<N> — bounded run proof
InBounds32<'a, T> / InBoundsMut32<'a, T> Option<&mut [T; N]>
StaticView32::at(LocalIndex32<N>), at_const::<I>() get_block_mut::<N>(block)
StaticTileMut32<T, ROWS, COLS, ROW_STRIDE> DisjointTiling<N, K>

Two vocabularies for the same idea is worse than one gap. The established pattern is a view type parameterised by extents, indexed by a bounded-index proof, returning an InBounds wrapper — and a solution here should extend that (a thread obtaining a sub-view it owns) rather than bolt new Disjoint* types onto the slice.

It is also worth noting my proposal carried no performance benefit: measured, get_block_mut on a DisjointSlice<f32> yields align-4 arrays that stay scalar in SASS. The value would be purely that a multi-element write stops needing unsafe — which is worth having, but not at the cost of a second vocabulary.

Open design questions

  • Should the run proof be a new type, or LocalIndex32<N> generalised to cover a span?
  • Does the tail case want Full/Partial as an enum, or a sub-view whose extent is a runtime value (as RuntimeTileMut32 already does for tiles)?
  • Should N be a const generic (so &mut [T; N] is expressible) or runtime (composing with RuntimeTileMut32)? The const form is what makes the fixed-size array possible, which is what a vectorised access would need.

Activity

  1. added a commit that references this issue on Aug 5, 2026
    f1b52c0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions