Skip to content

Fork depth for a ! call changes GPU frame time by up to 8x, and the same sweep is flat on the CPU #828

Closed

Description

@AdrielSantana

Summary

For a GPU (!) call, how deep the parallel fork goes before switching to
straight-line work changes the frame time by up to 8x. The same sweep on the CPU
is flat, so the variable is invisible unless you happen to test on the device.
Nothing in the guide mentions tuning it, and the one piece of guidance I found —
the Cell.fork comment in demos/app_slash_boss_3d/bend3d.bend, "a fork per
pixel drowns in scheduling, a fork per cell leaves lanes idle"
— steered me to
the coarse end, which is the wrong end for a raycaster.

Measurements

A voxel raycaster at 512². The benchmark sums every pixel colour and prints the
checksum, so the rows are verified to render the same image. 40 frames per run,
median of 5, IO.now() inside the process.

fork depth leaf --gpu 2GB --gpu off, 10 threads
6 levels 8×8 tile 14 (14–16) 19 (18–19)
7 levels 4×4 tile 6 (6–8) 20 (19–20)
8 levels 2×2 tile 8 (8–9) 20 (20–21)
9 levels 1 pixel 7 (7–8) 21 (20–21)

An interactive voxel world at 128², which also reads a shared Data quadtree
per DDA step. Checksum 747218888 on every row:

fork depth leaf --gpu 2GB
4 levels 8×8 tile 234 (232–236)
5 levels 4×4 tile 87 (87)
6 levels 2×2 tile 28 (28–34)

--gpu off on 10 threads: 178 ms. So at the coarse granularity that program was
slower on the GPU than on the CPU, and 8.4x slower than the same program
forked two levels deeper.

What is surprising

  • The CPU column is flat, so a CPU-side experiment reports "granularity does not
    matter" and is wrong about the device. I drew exactly that conclusion and
    shipped it.
  • The optimum is not universal: the raycaster wants a 4×4 leaf, the voxel world
    wants 2×2. It has to be swept per program.
  • Granularity is necessary but not sufficient. A third program forks per pixel
    and still does not win (48 with ! vs 43 on 10 threads) because it lacks a
    ray/box clip, so cheap rays never get cut.

Suggested fix

A paragraph in the guide saying that fork depth for ! is a tuning parameter,
that the CPU will not reveal it, and that the useful range is a leaf of a few
pixels rather than a tile. If bend3d.bend is the reference people are pointed
to, the Cell.fork comment could note that binning per tile pays when the tile
shares work (its triangle list), and not when every leaf is independent.

I am not reporting this as a bug — the scheduler behaving this way is
reasonable. It is that the one number that mattered most for my GPU performance
is undocumented and invisible from the CPU.

Environment

Bend 2.0.6, macOS 26.6.2, Apple M5 (10 CPU / 8 GPU cores), Metal.
Full sources: https://github.com/AdrielSantana/metal-bending

Activity

  1. AdrielSantana commented on Sep 18, 2026

    @AdrielSantana
    Author

    Re-verified on 2.0.9 (I was on 2.0.6 when I filed). Everything reproduces,
    and the sources changed slightly in between — the per-pixel read now picks its
    quadrant with nested Bool.pick instead of a match helper, which is why the
    absolute numbers moved a little.

    Raycaster, 512², 40 frames per run, median of 5. Checksum 1173697204 on every
    row, so all of them render the same image:

    fork depth leaf --gpu 2GB --gpu off, 10 threads no !, 10 threads
    6 levels 8×8 tile 14 (14–21) 19 (18–19) 20 (19–20)
    7 levels 4×4 tile 6 (6–8) 21 (19–21) 21 (20–21)
    8 levels 2×2 tile 8 (8–10) 20 (20–22) 21 (21–22)
    9 levels 1 pixel 8 (8–12) 22 (21–23) 22 (21–22)

    Voxel world, 128², checksum 747218888 on every row:

    fork depth leaf --gpu 2GB --gpu off, 10 threads
    4 levels 8×8 tile 202 (199–212) 177 (176–177)
    5 levels 4×4 tile 84 (83–84) 184 (184–184)
    6 levels 2×2 tile 26 (26–26) 184 (184–185)

    So on 2.0.9 the coarse-granularity version is still slower with the GPU than
    without it (202 vs 177), and still 7.8x slower than the same program forked two
    levels deeper. The CPU columns remain flat throughout.

  2. VictorTaelin commented on Sep 19, 2026

    @VictorTaelin
    Contributor

    Not a bug: your numbers fit the lane cube exactly. A bang runs 16384 lanes (128 x 128), so 4^7 leaves is the target: at 512^2 with 4x4 tiles, 7 levels = the cube (6 ms); 6 levels = a quarter (14 ms); 8-9 levels cost an extra kernel iteration each. At 128^2, 4 levels keep 256 lanes of 16384 busy (234 ms), 5 keep 1024 (87 ms), 6 keep 4096 (28 ms). The CPU pool is flat because 10 workers are fed by any of those leaf counts, so it cannot tune this. bend guide shaders (2.0.10) states the model, and 2.0.13 adds the rule of thumb you asked for: aim for 4^7 leaves per bang and sweep one level either side on the device.

    Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.

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