Skip to content

A safe way to give a fork tree's leaves disjoint parts of one Array to write (the counterpart of #1376) #1777

Closed as not planned

Description

@nuxyel

What Bend should do

Base already has the right shape in its model: an Array<T> is ALeaf{value} or ANode{xs, ys}, two disjoint halves. In the compiled program, though, matching an array copies each half into its own block (BendRT, "Arrays"), so a fork tree cannot use it. Something like

# split a into its two halves, both views of a's block: O(1), no copy
Array.halves(-T: Type, a: Array<T>) -> Array<T> & Array<T>      # model: match a { ANode{xs, ys}: (xs, ys) }
# put two halves of one block back together: O(1) when they came from Array.halves
Array.node(-T: Type, xs: Array<T>, ys: Array<T>) -> Array<T>     # model: ANode{xs, ys}

would have a model in which each leaf owns its half (writes included), so laws about the joined result could be stated and proved, and the runtime would do what the unsafe pattern does today. A leaf at depth d owns a contiguous range of 2^(capacity - d) cells. That fits tensor-fork (blocks of rows of C), stencil3d (slabs) and the histogram's rows. It does not fit a 2-D tile directly (tensor-gpu's leaves write 4 x 4 tiles of a row-major C): such a kernel would store C tile-major and permute it afterwards, or need a split by stride, which is a further question.

Questions:

  1. Is a safe split for writes on the roadmap, alongside the read-only borrow of A read-only borrow of an Array for a parallel let, without @unsafe #1376 (or the borrow inference Taelin mentioned)?
  2. Would it belong in Base (as above), or is a library the intended place, with @unsafe contained in it as bend-ml does now?
  3. Is the halves-of-a-node shape the right one for the runtime (a view into one block, freed once), or would ranges (Array.split_at(a, i)) fit it better?

I can share the packages' tests (every product against the sequential code, bit for bit, on the CPU threads and on the GPU) as a test bed for whatever shape it takes.

Why

The one-block pattern of tests/run/stencil3d.bend (and of bend-parallel's histogram, PR #1278): one Array, a fork tree whose every node holds a handle to it (Array.fork), each leaf writing its own part, the halves joined back (Array.join). It is how a GPU kernel is written in Bend today, and the fast one: in bend-ml I use it for matrix products on the CPU threads (bend-ml-tensor-fork: each leaf writes its rows of C) and on the GPU (bend-ml-tensor-gpu: each leaf writes its tile of C), and both give tensor-array's bits.

Both packages are @unsafe, and so is every program that uses them. That part is expected: Base models Array.fork(a) as (a, a) and Array.join(a, b) as a, so a write through the second handle is not in the model at all, and no law about the result can be stated. The alternatives cost what the pattern saves:

way to write disjoint parts in parallel what it costs
each leaf returns its part as a list, written into the array after the join 2-3.6x slower than not splitting, for products with a short inner dimension (bend-ml-tensor-array's NOTES); on CUDA, the lists' appends hit the lane stack (#1393)
each leaf gets its own array, concatenated after the join a copy of the whole output per call
Array.fork / Array.join O(1), the speed of the pattern, but @unsafe

Activity

  1. nicolas-abril commented on Nov 5, 2026

    @nicolas-abril
    Collaborator

    I don't think we should add Array.halves / Array.node for this. I'd rather make it a compiler optimization of match and ANode, as #2019 does.

    The model already says what we want. Array.halves(a) would have match a { ANode{xs, ys}: (xs, ys) } as its model, and Array.node(xs, ys) would have ANode{xs, ys}. Those are existing definitions, so the new functions would just be a second spelling of them. Bend is affine, so once an unshared array is matched, nothing else can reach its halves. Writing to a half in place therefore can't be observed. So a fork tree written as

    match a:
      case ANode{xs, ys}:
        ANode{go(xs), go(ys)}

    already gives each leaf its own part, writes included, and laws about the rebuilt array can be stated and proved today. The only thing wrong is the runtime: it copies on the match and copies again on the rebuild.

    Two ways to do the same thing cost more than they save. With separate functions, the compiler has two paths for splitting and joining an array, and users have to know which one is the fast one. Fixing the cost of match and ANode directly gives one way, and every program that walks an array by matching speeds up with no change, not just the ones written for it.

    The current behaviour stays available. If matching gives views, the case where you want a half as its own block (to keep a small slice without holding on to the whole array) becomes an explicit copy. That's the exception, so I'd rather it be the one that's spelled out.

    Where this stands. #2019 makes matching an ANode return views of the block on the host. Its second commit makes rebuilding ANode{xs, ys} from two side-by-side halves of one block return the parent as a view, with no copy. On 2^20 U32s, a fork tree 4 deep whose leaves each write their 2^16 cells with Array.set, 300 rounds, one thread:

    main #2019
    match / ANode, no @unsafe 0.24 s, 13.6 MB 0.09 s, 5.8 MB
    @unsafe Array.fork / Array.join 0.09 s, 13.6 MB 0.09 s, 9.7 MB

    So the safe version already runs as fast as the unsafe one. What's still missing:

    • Pieces more than 16x smaller than their root are still copied, so trees deeper than 4 levels copy below that. With explicit copies for the slice case, that cutoff can go.
    • The GPU still copies on both split and rebuild.

    Array.fork / Array.join would still be needed for racing atomic writes through shared handles. They just wouldn't be the only fast way for leaves to write disjoint parts.

  2. Lorenzobattistela commented on Nov 6, 2026

    @Lorenzobattistela
    Collaborator

    Closing as not planned in this shape. As discussed above, we won't add Array.halves / Array.node: they would only be a second spelling of matching and building an ANode. Making that match cheap is a compiler optimization, and we're tracking it in #1991 and #2019.

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