Skip to content

GPU funnel ~1.25x faster with identical candidate output - #36

Closed
legendaryshirt wants to merge 1 commit into
MinecraftAtHome:mainfrom
legendaryshirt:pr/gpu-speedup
Closed

legendaryshirt wants to merge 1 commit into
MinecraftAtHome:mainfrom
legendaryshirt:pr/gpu-speedup

Conversation

@legendaryshirt

Copy link
Copy Markdown

Summary

This makes the GPU funnel about 1.25x faster end to end (about 1.29x with --pipelines 2) without changing what it finds: for the same seeds, every stage produces the same candidate set as main. That was checked as a sorted (seed, x, z) set comparison, not just a count comparison, for small biomes, large biomes and unbound.

Measured on an RTX 4070 Ti (sm_89), CUDA 13.4, 60 s wall clock including startup, default SEED_ORDER=linear:

variant main this PR (default) this PR, --pipelines 2
small biomes, bounded 1.246 T seeds 1.553 T (1.25x) 1.607 T (1.29x)
large biomes, bounded 1.569 T 1.950 T (1.24x) 2.009 T (1.28x)
small biomes, unbound 0.996 T 1.216 T (1.22x) 1.268 T (1.27x)

Per stage, fixed work (tools/bench.sh 512: 512 iterations x 2^28 seeds, small biomes, bounded, one pipeline):

stage main this PR
filter_seeds 2093 ms 1772 ms
filter_gradvecs_1 2468 ms 1888 ms
filter_2_01a 1427 ms 1030 ms
filter_2_01b 59 ms 44 ms
total 6583 ms 5276 ms

(Runs on different days drift by about ±3% with GPU clocks; each table above was measured in a single session.)

How output identity was checked

tools/dump_patch.py inserts the same dump code into any tree. After each of filter_2_01a, filter_gradvecs_2, filter_2_01b, filter_2a and filter_2b, it syncs the stream and writes every candidate as stage seed x z, before a later stage reuses the buffer. I applied it to main and to this branch, ran both from the same --start, and compared the sorted outputs:

variant (32 iterations = 8.6 G seeds) 2_01a gv2 2_01b 2a 2b
small biomes, bounded 93,275 828,567 25,416 1,656 110
large biomes, bounded 67,317 38,342 1,176 76 4
small biomes, unbound 93,275 6,879,777 211,446 13,544 931

All 15 sets are identical (same SHA-256 of the sorted lines). The earlier stages feed these, and the later stages run the same kernels on identical inputs. A 64-iteration run on small biomes was identical as well.

There are two deliberate differences outside the GPU candidate sets:

  • Duplicate islands are dropped before CPU verification. A candidate is skipped when one of the last 64 queued candidates has the same seed and lies within 1024 blocks in x and z. The CPU check covers ±12800 blocks, so both would get the same verdict. Previously the same island was verified and written to output.txt 2-3 times. On 4.4 T seeds with every candidate verified, this gave 48 -> 37 output lines with the identical 37 islands.
  • The seed filter builds its RNG state directly instead of through XrsrRandom's constructor. The constructor also replaces an all-zero state, which can only differ for a 2^-128 event.

Changes

filter_seeds (integer-ALU bound, ~96% ALU pipe)

  • Test 1 passes 21.7% of seeds, so almost every warp has a passing lane, and with per-lane branches every lane paid for tests 2..6. Each thread now walks 64 consecutive seeds and pushes test-1 survivors into a per-warp shared-memory queue, together with the state test 1 reached (RNG after the first fork, the a-yo fork, the partial score). The queue is drained 32 at a time, resuming exactly where test 1 stopped. Same seeds, same tests, same order of float operations.
  • The 24-bit nextFloat value is converted from uint32 instead of uint64 (exact, cheaper conversion).

filter_gradvecs_1 (shared-memory LSU bound)

  • Conv tables are column-major, so lanes read unit-stride. Row-major [513][6] put lanes 24 B apart, a 2-way bank conflict. This overlaps with optimize gpu.cu which leads to a ~4% speedup #28.
  • Gate columns 2 and 3 are packed as float2, so one load serves two consecutive candidates, and the last load of each 8-candidate group is carried into the next. That is one load per y-row per candidate.
  • Each thread handles 2 z-rows (nz, nz+128): the byte index v + nz is unpacked once, and the second row is the same register plus an immediate offset.
  • The full-score columns are read only after the gate passes (0.17% of candidates), so they keep a single 256-row copy with a masked index. The conv-kernel table is read through L1 during the per-seed build. Shared memory drops from ~30 KB to ~17 KB per block, so 5 blocks fit per SM instead of 3.
  • The next 8 index bytes per row come in one 8-byte load, and the 8 gate results become a predicate mask with a single warp vote.
  • The summation order is that of score_center_2x2 / score_full_12.

Noise kernels (filter_2_01a/b and filter_2a..d, LSU-issue bound)

  • Gradient table: all gradient components are -1, 0 or +1. Storing 2*g makes each one a float whose only non-zero byte is the top one (0x40000000, 0xC0000000, 0). One 32-bit word then holds all three, and one __byte_perm rebuilds each, which is 1 shared load per corner instead of 3. Doubling is exact, and callers fold the 0.5 into the octave weight, so every noise value is bit-identical.
  • filter_2_01a/b store each warp's permutation as byte pairs q[i] = p[i] | p[i+1] << 8. Every lookup in a noise cell comes as an (i, i+1) pair, so a cell needs 7 shared loads instead of 14. The pairs are built from the global octave during the copy each warp already did.

Pipeline and host

  • filter_2_01b compacted its output over its own input buffer (buffer_2), so a warp could overwrite an input entry that another warp had not read yet. It now writes to its own 4 MB buffer. This is the same fix as in optimize gpu.cu which leads to a ~4% speedup #28. It changes no result in practice, because outputs lagged far behind inputs, but nothing guaranteed that.
  • --pipelines <n> (default 1) runs n independent pipelines per device: one GpuThread each, with its own stream and buffers, sharing the atomic seed range. A second pipeline fills the GPU while the other one is in its small, latency-bound late stages. It needs about 3 GB of VRAM per pipeline, so the default and the README's VRAM requirement stay as they are.
  • MAX_ITERS=n (make): run n iterations, then shut down through the normal graceful path, so queued CPU candidates are still verified. tools/bench.sh [iters] [start] uses it for fixed-work benchmarks. stdout is line-buffered so that logs redirected to a file survive any exit.

Opt-in, off by default: SEED_ORDER=golden
Seed index n is checked as seed (n * GOLDEN) ^ SILVER. That is a bijection, since GOLDEN is odd, so it still covers every 64-bit seed exactly once. Seed n's second mix64 is then seed n+1's first, which makes filter_seeds about 1.08x faster. It changes which seeds a given --start covers, so it is not the default, and such builds use a different network handshake (OMISSION-GPZ) so they cannot share a server with normal builds.

Not included / related

  • The makefile ifeq (LARGE_BIOMES, 1) suffix bug is left to Fix variant suffix resolution and binary target name in makefile #33.
  • Things I tried that did not help are left out, for example: a second-level queue after test 2, putting gradients into the pair table, more than 2 z-rows per thread, ALU gradient decoding, and register caps. Happy to share numbers.

CI replica: the Linux build job of build.yml (nvidia/cuda:12.9.1-devel-ubuntu22.04, make ARCH=all CC=gcc-11 CXX=g++-11) builds cleanly for LARGE_BIOMES=0 UNBOUND=0 and LARGE_BIOMES=1 UNBOUND=1. I have not built it on Windows, and I have tested only on sm_89. Results on other GPUs would be welcome.

🤖 Generated with Claude Code

See the pull request description for the per-change breakdown, the
measurements and the set-identity check (tools/dump_patch.py).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ScriptLineStudios

Copy link
Copy Markdown

this was closed as per the following discord discussion: https://discord.com/channels/720723932738486323/1406730684537766088/1568747590366007356

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants