Repository navigation
CUDA: the dispatches' entry check reads the frontier from device memory, not from the managed H - #1877
Conversation
nicolas-abril
left a comment
There was a problem hiding this comment.
Thanks, this is the shape we were hoping for. Every round keeps the check, the kernel keeps its three arguments, and Metal is unchanged. I've posted on #1854 what I think the mechanism is (the check makes the header page's fault wait on its own, ahead of the kernel's other faults), with three experiments that would confirm it. If it holds, reading the frontier from device memory deals with the cause, for every round.
We have no CUDA machine, so we can't check the timings ourselves. On Metal I built this branch and ran tests/comptime/offload.bend and offload_def.bend with +RTS --gpu on; both print 4.
A few things before merging:
- The
cuMemcpyHtoDresult isn't checked. Every other CUDA call on this path fails loudly. If this copy fails, the check reads a stale frontier and a dispatch can be skipped without any error. cuModuleGetGlobal(...)is used as a bare truth value. The line above it compares with!= CUDA_SUCCESS; this one should too.#define FRONT bend_front =hides an assignment.H[H_FRONT] = FRONT f;reads as one store but does two. A plain#ifdef BEND_RTC/bend_front = f;/#endifinsideturn_set, with the__device__ u64 bend_front;declaration next to the other CUDA globals, would say what it does.
About the size cap: we'll handle comp.ts's cap separately, so don't trim unrelated code for it here. A quick simplification pass over these lines would still help, and point 3 may well come out shorter than the macro.
…ry, not from the managed H
6eb1d9e to
169d9c8
Compare
|
Thanks for the review. I rebased the branch on current main (4f99f4c), made the three changes and squashed them into the one commit:
On the RTX 4050 (driver 615.78.08, toolkit 12.9): |
|
Thanks, this addresses everything from my review, and the timings on #1854 support the theory. One more experiment before we merge, if you have time. Your table shows #1877 is still about 9 µs a bang slower than no check at all. One possible cause is that // A pass launched past an empty frontier returns at once.
static void gpu_pass(u64* H) {
u64 front = H[H_FRONT];
if (cuMemcpyHtoD(gpu_front, &front, 8) != CUDA_SUCCESS) {
err_fail("device write failed");
}
for (u32 n = 1; H[H_FRONT] != 0; n = n < 32 ? 2 * n : 64) {The rest of the function stays the same. A second variant also makes the copy asynchronous on the default stream, so the host doesn't wait for it before queueing the first launch: if (cuMemcpyHtoDAsync(gpu_front, &front, 8, NULL) != CUDA_SUCCESS) {This is safe with a stack variable. For pageable host memory, Could you time both variants the same way as the table on #1854 (per bang vs main, plus the training loop)?
If neither one is measurably faster, I'm happy to merge #1877 as it is. I couldn't run this myself because I don't have a CUDA machine, so it's untested. Also, |
|
I timed both variants on the PR's commit (169d9c8, on main 4f99f4c), the same way as the table on #1854: RTX 4050 Laptop, idle machine on AC power, 5 alternated rounds (even rounds in reverse order). The variants change one line each in
Both pass the bang tests on CUDA like the PR (43 of the 45 tests with a bang and a
Neither is faster than the spread between rounds of the same build (about 4 µs a bang on the reproduction), so the copy's source isn't where the rest goes, and I left the PR as it is. The gap to no check came out at about 7 µs a bang this time, against 9 in the #1854 run. Thanks for taking the size cap separately. |
Renames bend_front to dev_front and declares it in the existing BEND_RTC block, so the PR no longer needs an #elif of its own. dev_front keeps the frontier off the managed H, so the entry check doesn't wait on a page fault (bendlang#1854). Co-Authored-By: Claude Opus 5.5 <[email protected]>
Fixes #1854.
#1447's entry check,
if (!turn_groups(H[H_FRONT], at)) return;, makes every block of every CUDA dispatch wait on a load from the managed allocation, and that costs ~3.5% on programs made of many small bangs (#1854). A load from plain device memory costs nothing there, so this keeps a copy of the frontier in a__device__ u64 bend_front:turn_setwrites it on the device too (pass 2, behindBEND_RTC, so Metal and the host are unchanged);cuMemcpyHtoDat the start ofgpu_pass, after its ownturn_set, and fails loudly if that copy fails;gpu_loadtakes its address withcuModuleGetGlobal(checked againstCUDA_SUCCESS);H[H_FRONT].Every round keeps the check, the chained rounds included, the kernel keeps its three arguments, and Metal's code is unchanged.
RTX 4050 Laptop, driver 615.78.08, toolkit 12.9, alternated runs on an idle machine, the same output on both (the first two rows timed again on the rebased commit, 5 rounds; the benches before the rebase):
+RTS --gpu(median of 3 each)After the rebase on 4f99f4c, the tests with a bang and a
main(45 files), built with both compilers and run on the GPU: 43 print their#|lines on both; the other two,tests/printer/offload_bang_gand_h, refuse to build on both, as they should.bend2/comp.tsis at 66,059 ttok against the 66,000 cap (main is at 65,956), so the PR adds 103. I kept the change as small as I could (13 lines added, 2 changed) rather than trim unrelated code to fit. I have no Metal machine; the Metal path doesn't change, since every addition is behindBEND_RTCor in the CUDA host section.