Skip to content

Metal: heap_free's error check is an atomic load of one word on every free, 7-12% of the allocating tree benches #1143

Closed

Description

@AdrielSantana

On a device, heap_free checks the error flag before it frees:

INLINE void heap_free(Env e, u32 cls, u64 loc) {
  if (err_seen(e.mem)) {
    return;
  }

err_seen is a32_load(a32_at(H, H_ERROR_CODE)), so every free on every lane is an atomic load of the same word. On Metal this costs tree-bitonic about 12% of its GPU time and tree-radix about 7%.

I have not sent a PR, because the cheap forms of this read carry risks that are yours to weigh (below).

What I measured

These numbers come from the emitted C of c23aa1f4 (#1140 over main b2111cf4), with one site changed at a time.

  • The time is the GPU's, summed over each round's command buffer (GPUEndTime - GPUStartTime).
  • Runs alternate: in pairs, before then after and after then before in turn, or, with more than two builds, in an order that rotates and reverses every round. Each row gives the median ratio of before over after, and a bootstrap 95% interval of it.
  • Every output equals the unchanged build's.

There are three per-call checks of err_seen on the device. On tree-bitonic (--gpu 768MB, 12 rounds), heap_free's is the whole cost:

tree-bitonic, the check dropped from as is / changed 95% interval
heap_free 1.128 [1.115, 1.143]
BLK_ALLOC 0.998 [0.985, 1.000]
FID_EXIT in the work loop 0.981 [0.968, 0.995]
all three 1.125 [1.114, 1.133]

Across benches (10 rounds), I compared the build with all three dropped, as a ceiling, against a build that keeps heap_free's check but reads the flag with a volatile load, *(volatile DEV u32*)a32_at(e.mem, H_ERROR_CODE) != 0:

bench all three dropped 95% interval volatile read in heap_free 95% interval
tree-bitonic 1.127 [1.119, 1.138] 1.116 [1.104, 1.124]
tree-radix 1.073 [1.068, 1.085] 1.070 [1.066, 1.076]
tree-matmul 1.032 [1.015, 1.038] 0.977 [0.953, 1.001]
merkle 1.008 [0.992, 1.015]
hashmap 0.996 [0.988, 1.008]
bfs 0.997 [0.988, 1.010]
  • tree-matmul's own runs varied from 570 to 667 ms between sessions, so I read its rows as noise.
  • A Bendcraft frame did not move: its hot loop does not allocate.

The likely cause is that every lane loads one address atomically on each free, and atomics on one address are served one at a time. A volatile load of the same word can be served from the cache.

Why I have not sent a PR

  • A volatile device load is a new load form. At MSL 3.2 it compiles to air.load.device_coherent.volatile. No M1 pipeline has compiled it here, and a32_load's comment says M1's pipeline build dies on some load forms. If it did on this one, every ! on an M1 would fail.
  • Below MSL 3.2, the load is not coherent. There DEV is plain device. A lane that posts ERR_HEAP with a32_cmpx can keep reading 0 from its L1 and go on freeing what its failed allocations left at HEAP_OFF, which may write out of bounds.
  • Dropping the check changes what a lane does after an error, so I only took that as a ceiling.

Options I see

  • Read the flag with a volatile load on MSL 3.2 and later only, after the pipeline builds on an M1.
  • Poll the flag in heap_free every N frees, as err_spun does in the loops. A lane would then free a few blocks after another lane's error. Its own ERR_HEAP would need a local test instead, for example on heap_alloc_miss returning HEAP_OFF.
  • Keep it as it is, if 7-12% on the allocating trees is not worth either.

Environment

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

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