Skip to content

Metal: a ! tree's arity decides the lanes' load; the same leaves take 12 ms four-way, 23-34 ms four-way pruned, 3 ms two-way #925

Open

Description

@AdrielSantana

Summary

On the Metal lane, the arity of a ! tree decides how its work lands on the lanes: the same leaves forked four ways a level take 5x longer than forked two ways, and a four-way tree that prunes its empty branches takes 10x. A voxel raycaster hit it at 46 ms (four-way), 110–150 ms (four-way, pruned) and 16 ms (two-way) for one frame; below, a 90-line program shows it alone.

Reproduction

frontier.bend: three ! trees over a 2048 x 2048 square of 4x4 leaves, where a leaf inside a 1470 x 796 frame runs a fixed loop and a leaf outside returns 0. A forks four ways a level and every leaf is a task. B forks four ways and a node whose corner lies past the frame returns 0 at once. C forks two ways a level (a square into its two rows, a row into its two squares) and a half past the frame is run in place, never a task. The three sum the same leaves.

import Base

# The same work three ways: a `!` tree over a 2048 x 2048 square of 4x4
# leaves, and a leaf inside a 1470 x 796 frame does a fixed loop, the rest
# nothing. A: four kids a level, the leaves past the frame return 0. B: four
# kids a level, a node whose corner is past the frame returns 0 at once.
# C: two kids a level (a square into its rows, a row into its squares), a
# half past the frame is run in place, never a task.

def spin(fuel: Nat, +a: U32) -> U32:
  match fuel:
    case 0n:
      a
    case 1n+f:
      spin(f, (a * 1664525 + 1013904223 : U32))

def leaf(vis: Bool, +x: U32, +y: U32) -> U32:
  match vis:
    case True{}:
      spin(U32.to_nat(2000), (x * 4096 + y : U32))
    case False{}:
      0

def four(+k: Nat, +x: U32, +y: U32) -> U32:
  match k:
    case 0n:
      leaf((x < 1470 : U32) && (y < 796 : U32), x, y)
    case 1n+e:
      +h = U32.shln(4, e)
      p q r s = four(e, x, y) four(e, (x + h : U32), y) four(e, x, (y + h : U32)) four(e, (x + h : U32), (y + h : U32))
      (p + q + r + s : U32)

def fourp(+k: Nat, vis: Bool, +x: U32, +y: U32) -> U32:
  match k:
    case 0n:
      leaf(vis, x, y)
    case 1n+e:
      match vis:
        case False{}:
          0
        case True{}:
          +h = U32.shln(4, e)
          +x1 = (x + h : U32)
          +y1 = (y + h : U32)
          +v1 = (x1 < 1470 : U32)
          +v2 = (y1 < 796 : U32)
          p q r s = fourp(e, True{}, x, y) fourp(e, v1, x1, y) fourp(e, v2, x, y1) fourp(e, v1 && v2, x1, y1)
          (p + q + r + s : U32)

def two(+f: Nat, +wide: Bool, v: Bool, +h: U32, +x: U32, +y: U32) -> U32:
  match f:
    case 0n:
      leaf(True{}, x, y)
    case 1n+g:
      match wide:
        case True{}:
          match v:
            case True{}:
              +h2 = (h / 2 : U32)
              +vy = ((y + h2 : U32) < 796 : U32)
              l r = two(g, False{}, vy, h2, x, y) two(g, False{}, vy, h2, (x + h : U32), y)
              (l + r : U32)
            case False{}:
              +h2 = (h / 2 : U32)
              two(g, False{}, ((y + h2 : U32) < 796 : U32), h2, x, y)
        case False{}:
          match v:
            case True{}:
              +vx = ((x + h : U32) < 1470 : U32)
              t b = two(g, True{}, vx, h, x, y) two(g, True{}, vx, h, x, (y + h : U32))
              (t + b : U32)
            case False{}:
              two(g, True{}, ((x + h : U32) < 1470 : U32), h, x, y)

def show(name: String, +t0: Nat, +t1: Nat, +sum: U32) -> IO(Unit):
  IO.print(name ++ ": " ++ Nat.show((t1 - t0 : Nat)) ++ " ms, sum " ++ U32.show(sum))

def run_a(n: Nat) -> IO(Unit):
  match n:
    case 0n:
      IO.pure(Unit, Unit{})
    case 1n+m:
      do IO<Unit>:
        t0 : Nat <- IO.now()
        a : U32 = four!(U32.to_nat(9), 0, 0)
        t1 : Nat <- IO.now()
        show("A four-way, leaves past the frame return 0", t0, t1, a)
        run_a(m)

def run_b(n: Nat) -> IO(Unit):
  match n:
    case 0n:
      IO.pure(Unit, Unit{})
    case 1n+m:
      do IO<Unit>:
        t0 : Nat <- IO.now()
        a : U32 = fourp!(U32.to_nat(9), True{}, 0, 0)
        t1 : Nat <- IO.now()
        show("B four-way, a node past the frame returns 0 at once", t0, t1, a)
        run_b(m)

def run_c(n: Nat) -> IO(Unit):
  match n:
    case 0n:
      IO.pure(Unit, Unit{})
    case 1n+m:
      do IO<Unit>:
        t0 : Nat <- IO.now()
        a : U32 = two!(U32.to_nat(18), False{}, (1024 < 796 : U32), 1024, 0, 0)
        t1 : Nat <- IO.now()
        show("C two-way, a half past the frame run in place", t0, t1, a)
        run_c(m)

def main() -> IO(Unit):
  do IO<Unit>:
    run_a(4n)
    run_b(4n)
    run_c(4n)

bend frontier.bend -o frontier && ./frontier (Apple M5, Bend 2.0.22; the same numbers on 2.0.23 at fd7d9e6, whose grow pass ends when the group is full or nothing grew; the first line of A includes loading the GPU program):

A four-way, leaves past the frame return 0: 352 ms, sum 1017840064
A four-way, leaves past the frame return 0: 15 ms, sum 1017840064
A four-way, leaves past the frame return 0: 14 ms, sum 1017840064
A four-way, leaves past the frame return 0: 12 ms, sum 1017840064
B four-way, a node past the frame returns 0 at once: 34 ms, sum 1017840064
B four-way, a node past the frame returns 0 at once: 29 ms, sum 1017840064
B four-way, a node past the frame returns 0 at once: 26 ms, sum 1017840064
B four-way, a node past the frame returns 0 at once: 23 ms, sum 1017840064
C two-way, a half past the frame run in place: 3 ms, sum 1017840064
C two-way, a half past the frame run in place: 3 ms, sum 1017840064
C two-way, a half past the frame run in place: 2 ms, sum 1017840064
C two-way, a half past the frame run in place: 3 ms, sum 1017840064

What I read in comp.ts

A ! is one dispatch of four kernels. grow on one group forks the root, a task a lane a turn, until each of the group's 128 rings holds a task; grow on 128 groups does the same, each from its own task; work (rings transposed) runs each task's whole subtree on its lane in sequence, making no new tasks; pack. So a frame takes as long as the slowest lane's subtree, and the subtrees' sizes come from how the tree fills the 128 rings:

  • four kids a turn: 64 tasks give 256 kids, 2 to 3 a ring, at mixed levels, so a ring may hold 64 leaves next to one holding 4 (A);
  • a kid that returns at once still takes a ring and a turn, and the group's growth is skewed by where the frame's edge falls; which lanes get the big subtrees then depends on the race of the deals, so B swings (a 2-way tree with such kids swung between 16 and 154 ms on the same view);
  • two kids a turn fill the 128 rings exactly, one each, at one level (C).

Traces from the raycaster's frame (1470 x 796), with cube_run/gpu_pass of the emitted C patched to run one command buffer a kernel and count put - get over the rings after each:

four-way (A)      grow1: 631 us -> tasks=256 rings=128 max=2    grow128: 572 us -> tasks=34816 rings=16384 max=3    work: 46313 us
four-way pruned   grow1: 406 us -> tasks=336 rings=128 max=3    grow128: 859 us -> tasks=29596 rings=15526 max=4    work: 136659 us
two-way pruned    grow1: 559 us -> tasks=168 rings=128 max=2    grow128: 609 us -> tasks=16900 rings=15364 max=2    work: 153831 us  (16077 us on another frame)
two-way (C)       grow1: 493 us -> tasks=128 rings=128 max=1    grow128: 1243 us -> tasks=13576 rings=13544 max=2   work: 14706 us

In the game

AdrielSantana/bendcraft, 57bccee, make bench (the ! alone, five frames, the same 30 checksums for every shape):

rays a frame four-way (A) four-way, pruned (B) two-way (C)
512 x 512 7 ms 3–4 ms 3 ms
735 x 398 18–29 ms 5 ms
960 x 540 14–17 ms 10 ms 9–10 ms
1470 x 796 46 ms 110–150 ms 16 ms
1920 x 1080 50 ms 37 or 208 ms 28–31 ms

A second note

At 512 x 512 the four-way tree's leaves ran inside grow128 (2 tasks of 64 leaves a group, so the group never filled) in 3 ms, and the two-way tree's 16384 leaves, one a lane, ran in work in 6 ms: the same 16 rays a lane. In grow a group's lanes hold the kids their own group dealt, siblings side by side; in work ring j*128+c goes to lane j of group c, so a SIMD group holds one kid of each of 32 unrelated subtrees.

Question

Is the fill of a group meant to stop at "every lane has a task", whatever the sizes? A frontier that keeps splitting the largest tasks until the sizes are even, or that skips kids which hold no fork, would take the arity out of the program's hands; and a work that keeps siblings in one group would give the raycaster the 2x the grow pass shows. I am glad to try a patch if either sounds right.

Written by Claude (Anthropic) with Adriel Santana driving; the numbers are from his M5.

Activity

  1. changed the title [-]Metal: the frontier follows the fork's arity, a 4-way tree left lanes 64 leaves; a 2-way tree with no early-returning kids ran the voxel frame 3x faster[/-] [+]Metal: a `!` tree's arity decides the lanes' load; the same leaves take 12 ms four-way, 23-34 ms four-way pruned, 3 ms two-way[/+] on Sep 21, 2026
  2. added
    later
    on Sep 27, 2026
  3. AdrielSantana commented on Sep 27, 2026

    @AdrielSantana
    Author

    On the last question, measured: after a grow, the work pass's ring_flip gives a SIMD group one task from each of 32 unrelated subtrees. Keeping a grow group's rings side by side (rg = me) alone is a disaster on trees (tree-bitonic 1.9 → 45 s on 2.0.29). A work group that starts in a later wave takes the tasks an earlier wave's joins dealt in the same pass, and runs each whole on one lane.

    #1132 drains each lane's own ring after a grow, up to the put the grow left. By GPU time in before/after pairs on 2.0.32, the raycaster's frame is 1.7x to 2.8x faster and raytrace 1.39x, while tree-matmul loses 7%. The other runtime benches stay within their 95% intervals, and every output is the same.

    The first question, a fill that stops at "every lane has a task" whatever the sizes, is untouched. With the PR, the program above reads A 4.6, B 15.6 and C 1.35 ms (5.8, 18.5 and 2.1 before), so the pruned four-way tree is still about 11x the two-way one.

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

    later

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions