From 575c4d0abd7438392feb07e684a18c94d59a2707 Mon Sep 17 00:00:00 2001 From: rob thijssen Date: Sun, 13 Sep 2026 19:35:32 +0300 Subject: [PATCH] engine-cuda: per-arch carry path, +20.5% on sm_120 An `asm` block is opaque to nvcc's optimiser, so carry arithmetic written as inline PTX blocks common-subexpression elimination and strength reduction across neighbouring field operations. On Blackwell that costs a quarter of the kernel. On Ada and Ampere the hand-written sequence still wins, by a similar margin in the other direction, so the default is now chosen per device pass. Whole-kernel instructions (ptxas + cuobjdump, CUDA 13.0) and measured hashrate, three interleaved rounds each: sm_120 sm_86/89 PTX_CARRY=1 37,432 1127.1 MH/s 37,027 benjy 461.6 quadbrat 85.5 PTX_CARRY=0 27,837 1358.0 MH/s 41,196 benjy 401.5 quadbrat 76.3 sm_86/89 keep the exact configuration they had, so those cards are unchanged by construction -- identical macro values, identical 37,027 instructions. Parity 60/60 against the CPU on beast. LAIR_PTX_ACC is now 1 everywhere rather than following LAIR_PTX_CARRY. In the Acc accumulators the add-with-carry pair is the whole operation, so there is nothing around it for the optimiser to fold; on sm_120 with the carry path off, Acc PTX on measured 1333.3 against 1285.8 with it off. PR #17 measured the PTX carry path as a win on every card. That was true of the kernel it was written for, which spilled 104 bytes per thread; once the spills went the trade reversed on Blackwell only. Found by profiling rather than reading: ncu puts us at 43,700 executed instructions per nonce against the rival binary's 22,229 on the same card, a 1.97x ratio that matches the 2.05x throughput gap, with 82% of the excess on the ALU pipe rather than the multiply pipe. This lands 35,785. The remaining gap is still ALU: 26,586 against their 14,413, while the multiply pipe is now close to parity at 8,405 against 7,030. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_017ytCkttecH9SNTUEw8VT9D --- crates/engine-cuda/src/kernels/mining.cu | 44 +++++++++++++++++++++--- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/crates/engine-cuda/src/kernels/mining.cu b/crates/engine-cuda/src/kernels/mining.cu index b55cbbf..2bcb56e 100644 --- a/crates/engine-cuda/src/kernels/mining.cu +++ b/crates/engine-cuda/src/kernels/mining.cu @@ -28,11 +28,43 @@ typedef unsigned int u32; // 2^64 mod P = 2^32 - 1 #define EPS 0xFFFFFFFFull +// lair: whether gf_add and gf_reduce should use inline-PTX carry arithmetic or +// plain C is architecture-dependent, and the difference is large in both +// directions. An `asm` block is opaque to nvcc's optimiser, so carry arithmetic +// written as PTX blocks common-subexpression elimination and strength reduction +// across neighbouring field operations. On sm_120 that costs a quarter of the +// kernel; on sm_86/89 the hand-written sequence still wins. +// +// Whole-kernel instructions / ALU ops (ptxas + cuobjdump, CUDA 13.0) and +// measured hashrate, three interleaved rounds each, parity 40/40 vs CPU: +// +// sm_120 sm_86/89 +// CARRY=1 37,432 / 26,688 1126.6 37,027 benjy 461.6 quadbrat 85.5 +// CARRY=0 27,837 / 19,519 1333.3 41,196 benjy 401.5 quadbrat 76.3 +// +// So +18.3% on the 5090s and -13.0% / -10.8% on the 4090 and 3060. Hence the +// per-arch default. Unlike LAIR_INT_UNROLL -- where the static measures pointed +// one way and the cards' power envelope the other -- this one was measured on +// hardware on all three architectures, and the static count predicted the sign +// correctly on each. +// +// LAIR_PTX_ACC stays 1 everywhere: in the Acc accumulators the add-with-carry +// pair is the whole operation, so there is nothing around it to optimise. On +// sm_120, carry off with Acc off measured 1285.8 against 1333.3 with Acc on. +// +// PR #17 measured the PTX carry path as a win on every card, and that was true +// of the kernel it was written for -- which spilled 104 bytes per thread. Once +// the spills went (see MiningUniforms) the trade reversed on Blackwell only. +// Re-measure per architecture before touching this. #ifndef LAIR_PTX_CARRY -#define LAIR_PTX_CARRY 1 +# if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 1200 +# define LAIR_PTX_CARRY 0 +# else +# define LAIR_PTX_CARRY 1 +# endif #endif #ifndef LAIR_PTX_ACC -#define LAIR_PTX_ACC LAIR_PTX_CARRY +#define LAIR_PTX_ACC 1 #endif // Fold the next round's constant into the linear layer's final reduction // instead of a separate gf_add per element before the S-box. @@ -138,10 +170,14 @@ __device__ __forceinline__ u64 gf_canon(u64 a) { #define LAIR_PRAGMA(x) _Pragma(#x) #define LAIR_UNROLL(n) LAIR_PRAGMA(unroll n) #ifndef LAIR_PTX_CARRY -#define LAIR_PTX_CARRY 1 +# if defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 1200 +# define LAIR_PTX_CARRY 0 +# else +# define LAIR_PTX_CARRY 1 +# endif #endif #ifndef LAIR_PTX_ACC -#define LAIR_PTX_ACC LAIR_PTX_CARRY +#define LAIR_PTX_ACC 1 #endif // Fold the next round's constant into the linear layer's final reduction // instead of a separate gf_add per element before the S-box.